blob: 242bb51c67f26759b2a7c62e9b98f64524f4281c [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);
140
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200141static atomic_t nr_mmap_events __read_mostly;
142static atomic_t nr_comm_events __read_mostly;
143static atomic_t nr_task_events __read_mostly;
144
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200145static LIST_HEAD(pmus);
146static DEFINE_MUTEX(pmus_lock);
147static struct srcu_struct pmus_srcu;
148
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200149/*
150 * perf event paranoia level:
151 * -1 - not paranoid at all
152 * 0 - disallow raw tracepoint access for unpriv
153 * 1 - disallow cpu events for unpriv
154 * 2 - disallow kernel profiling for unpriv
155 */
156int sysctl_perf_event_paranoid __read_mostly = 1;
157
Frederic Weisbecker20443382011-03-31 03:33:29 +0200158/* Minimum for 512 kiB + 1 user control page */
159int sysctl_perf_event_mlock __read_mostly = 512 + (PAGE_SIZE / 1024); /* 'free' kiB per user */
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200160
161/*
162 * max perf event sample rate
163 */
Peter Zijlstra163ec432011-02-16 11:22:34 +0100164#define DEFAULT_MAX_SAMPLE_RATE 100000
165int sysctl_perf_event_sample_rate __read_mostly = DEFAULT_MAX_SAMPLE_RATE;
166static int max_samples_per_tick __read_mostly =
167 DIV_ROUND_UP(DEFAULT_MAX_SAMPLE_RATE, HZ);
168
169int perf_proc_update_handler(struct ctl_table *table, int write,
170 void __user *buffer, size_t *lenp,
171 loff_t *ppos)
172{
173 int ret = proc_dointvec(table, write, buffer, lenp, ppos);
174
175 if (ret || !write)
176 return ret;
177
178 max_samples_per_tick = DIV_ROUND_UP(sysctl_perf_event_sample_rate, HZ);
179
180 return 0;
181}
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200182
183static atomic64_t perf_event_id;
184
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200185static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
186 enum event_type_t event_type);
187
188static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +0200189 enum event_type_t event_type,
190 struct task_struct *task);
191
192static void update_context_time(struct perf_event_context *ctx);
193static u64 perf_event_time(struct perf_event *event);
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200194
Peter Zijlstra10c6db12011-11-26 02:47:31 +0100195static void ring_buffer_attach(struct perf_event *event,
196 struct ring_buffer *rb);
197
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200198void __weak perf_event_print_debug(void) { }
199
Matt Fleming84c79912010-10-03 21:41:13 +0100200extern __weak const char *perf_pmu_name(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200201{
Matt Fleming84c79912010-10-03 21:41:13 +0100202 return "pmu";
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200203}
204
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200205static inline u64 perf_clock(void)
206{
207 return local_clock();
208}
209
Stephane Eraniane5d13672011-02-14 11:20:01 +0200210static inline struct perf_cpu_context *
211__get_cpu_context(struct perf_event_context *ctx)
212{
213 return this_cpu_ptr(ctx->pmu->pmu_cpu_context);
214}
215
Peter Zijlstrafacc4302011-04-09 21:17:42 +0200216static void perf_ctx_lock(struct perf_cpu_context *cpuctx,
217 struct perf_event_context *ctx)
218{
219 raw_spin_lock(&cpuctx->ctx.lock);
220 if (ctx)
221 raw_spin_lock(&ctx->lock);
222}
223
224static void perf_ctx_unlock(struct perf_cpu_context *cpuctx,
225 struct perf_event_context *ctx)
226{
227 if (ctx)
228 raw_spin_unlock(&ctx->lock);
229 raw_spin_unlock(&cpuctx->ctx.lock);
230}
231
Stephane Eraniane5d13672011-02-14 11:20:01 +0200232#ifdef CONFIG_CGROUP_PERF
233
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200234/*
235 * Must ensure cgroup is pinned (css_get) before calling
236 * this function. In other words, we cannot call this function
237 * if there is no cgroup event for the current CPU context.
238 */
Stephane Eraniane5d13672011-02-14 11:20:01 +0200239static inline struct perf_cgroup *
240perf_cgroup_from_task(struct task_struct *task)
241{
242 return container_of(task_subsys_state(task, perf_subsys_id),
243 struct perf_cgroup, css);
244}
245
246static inline bool
247perf_cgroup_match(struct perf_event *event)
248{
249 struct perf_event_context *ctx = event->ctx;
250 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
251
252 return !event->cgrp || event->cgrp == cpuctx->cgrp;
253}
254
255static inline void perf_get_cgroup(struct perf_event *event)
256{
257 css_get(&event->cgrp->css);
258}
259
260static inline void perf_put_cgroup(struct perf_event *event)
261{
262 css_put(&event->cgrp->css);
263}
264
265static inline void perf_detach_cgroup(struct perf_event *event)
266{
267 perf_put_cgroup(event);
268 event->cgrp = NULL;
269}
270
271static inline int is_cgroup_event(struct perf_event *event)
272{
273 return event->cgrp != NULL;
274}
275
276static inline u64 perf_cgroup_event_time(struct perf_event *event)
277{
278 struct perf_cgroup_info *t;
279
280 t = per_cpu_ptr(event->cgrp->info, event->cpu);
281 return t->time;
282}
283
284static inline void __update_cgrp_time(struct perf_cgroup *cgrp)
285{
286 struct perf_cgroup_info *info;
287 u64 now;
288
289 now = perf_clock();
290
291 info = this_cpu_ptr(cgrp->info);
292
293 info->time += now - info->timestamp;
294 info->timestamp = now;
295}
296
297static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
298{
299 struct perf_cgroup *cgrp_out = cpuctx->cgrp;
300 if (cgrp_out)
301 __update_cgrp_time(cgrp_out);
302}
303
304static inline void update_cgrp_time_from_event(struct perf_event *event)
305{
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200306 struct perf_cgroup *cgrp;
307
Stephane Eraniane5d13672011-02-14 11:20:01 +0200308 /*
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200309 * ensure we access cgroup data only when needed and
310 * when we know the cgroup is pinned (css_get)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200311 */
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200312 if (!is_cgroup_event(event))
Stephane Eraniane5d13672011-02-14 11:20:01 +0200313 return;
314
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200315 cgrp = perf_cgroup_from_task(current);
316 /*
317 * Do not update time when cgroup is not active
318 */
319 if (cgrp == event->cgrp)
320 __update_cgrp_time(event->cgrp);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200321}
322
323static inline void
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200324perf_cgroup_set_timestamp(struct task_struct *task,
325 struct perf_event_context *ctx)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200326{
327 struct perf_cgroup *cgrp;
328 struct perf_cgroup_info *info;
329
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200330 /*
331 * ctx->lock held by caller
332 * ensure we do not access cgroup data
333 * unless we have the cgroup pinned (css_get)
334 */
335 if (!task || !ctx->nr_cgroups)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200336 return;
337
338 cgrp = perf_cgroup_from_task(task);
339 info = this_cpu_ptr(cgrp->info);
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200340 info->timestamp = ctx->timestamp;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200341}
342
343#define PERF_CGROUP_SWOUT 0x1 /* cgroup switch out every event */
344#define PERF_CGROUP_SWIN 0x2 /* cgroup switch in events based on task */
345
346/*
347 * reschedule events based on the cgroup constraint of task.
348 *
349 * mode SWOUT : schedule out everything
350 * mode SWIN : schedule in based on cgroup for next
351 */
352void perf_cgroup_switch(struct task_struct *task, int mode)
353{
354 struct perf_cpu_context *cpuctx;
355 struct pmu *pmu;
356 unsigned long flags;
357
358 /*
359 * disable interrupts to avoid geting nr_cgroup
360 * changes via __perf_event_disable(). Also
361 * avoids preemption.
362 */
363 local_irq_save(flags);
364
365 /*
366 * we reschedule only in the presence of cgroup
367 * constrained events.
368 */
369 rcu_read_lock();
370
371 list_for_each_entry_rcu(pmu, &pmus, entry) {
Stephane Eraniane5d13672011-02-14 11:20:01 +0200372 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
373
Stephane Eraniane5d13672011-02-14 11:20:01 +0200374 /*
375 * perf_cgroup_events says at least one
376 * context on this CPU has cgroup events.
377 *
378 * ctx->nr_cgroups reports the number of cgroup
379 * events for a context.
380 */
381 if (cpuctx->ctx.nr_cgroups > 0) {
Peter Zijlstrafacc4302011-04-09 21:17:42 +0200382 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
383 perf_pmu_disable(cpuctx->ctx.pmu);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200384
385 if (mode & PERF_CGROUP_SWOUT) {
386 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
387 /*
388 * must not be done before ctxswout due
389 * to event_filter_match() in event_sched_out()
390 */
391 cpuctx->cgrp = NULL;
392 }
393
394 if (mode & PERF_CGROUP_SWIN) {
Stephane Eraniane566b762011-04-06 02:54:54 +0200395 WARN_ON_ONCE(cpuctx->cgrp);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200396 /* set cgrp before ctxsw in to
397 * allow event_filter_match() to not
398 * have to pass task around
399 */
400 cpuctx->cgrp = perf_cgroup_from_task(task);
401 cpu_ctx_sched_in(cpuctx, EVENT_ALL, task);
402 }
Peter Zijlstrafacc4302011-04-09 21:17:42 +0200403 perf_pmu_enable(cpuctx->ctx.pmu);
404 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200405 }
Stephane Eraniane5d13672011-02-14 11:20:01 +0200406 }
407
408 rcu_read_unlock();
409
410 local_irq_restore(flags);
411}
412
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200413static inline void perf_cgroup_sched_out(struct task_struct *task,
414 struct task_struct *next)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200415{
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200416 struct perf_cgroup *cgrp1;
417 struct perf_cgroup *cgrp2 = NULL;
418
419 /*
420 * we come here when we know perf_cgroup_events > 0
421 */
422 cgrp1 = perf_cgroup_from_task(task);
423
424 /*
425 * next is NULL when called from perf_event_enable_on_exec()
426 * that will systematically cause a cgroup_switch()
427 */
428 if (next)
429 cgrp2 = perf_cgroup_from_task(next);
430
431 /*
432 * only schedule out current cgroup events if we know
433 * that we are switching to a different cgroup. Otherwise,
434 * do no touch the cgroup events.
435 */
436 if (cgrp1 != cgrp2)
437 perf_cgroup_switch(task, PERF_CGROUP_SWOUT);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200438}
439
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200440static inline void perf_cgroup_sched_in(struct task_struct *prev,
441 struct task_struct *task)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200442{
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200443 struct perf_cgroup *cgrp1;
444 struct perf_cgroup *cgrp2 = NULL;
445
446 /*
447 * we come here when we know perf_cgroup_events > 0
448 */
449 cgrp1 = perf_cgroup_from_task(task);
450
451 /* prev can never be NULL */
452 cgrp2 = perf_cgroup_from_task(prev);
453
454 /*
455 * only need to schedule in cgroup events if we are changing
456 * cgroup during ctxsw. Cgroup events were not scheduled
457 * out of ctxsw out if that was not the case.
458 */
459 if (cgrp1 != cgrp2)
460 perf_cgroup_switch(task, PERF_CGROUP_SWIN);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200461}
462
463static inline int perf_cgroup_connect(int fd, struct perf_event *event,
464 struct perf_event_attr *attr,
465 struct perf_event *group_leader)
466{
467 struct perf_cgroup *cgrp;
468 struct cgroup_subsys_state *css;
469 struct file *file;
470 int ret = 0, fput_needed;
471
472 file = fget_light(fd, &fput_needed);
473 if (!file)
474 return -EBADF;
475
476 css = cgroup_css_from_dir(file, perf_subsys_id);
Li Zefan3db272c2011-03-03 14:25:37 +0800477 if (IS_ERR(css)) {
478 ret = PTR_ERR(css);
479 goto out;
480 }
Stephane Eraniane5d13672011-02-14 11:20:01 +0200481
482 cgrp = container_of(css, struct perf_cgroup, css);
483 event->cgrp = cgrp;
484
Li Zefanf75e18c2011-03-03 14:25:50 +0800485 /* must be done before we fput() the file */
486 perf_get_cgroup(event);
487
Stephane Eraniane5d13672011-02-14 11:20:01 +0200488 /*
489 * all events in a group must monitor
490 * the same cgroup because a task belongs
491 * to only one perf cgroup at a time
492 */
493 if (group_leader && group_leader->cgrp != cgrp) {
494 perf_detach_cgroup(event);
495 ret = -EINVAL;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200496 }
Li Zefan3db272c2011-03-03 14:25:37 +0800497out:
Stephane Eraniane5d13672011-02-14 11:20:01 +0200498 fput_light(file, fput_needed);
499 return ret;
500}
501
502static inline void
503perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
504{
505 struct perf_cgroup_info *t;
506 t = per_cpu_ptr(event->cgrp->info, event->cpu);
507 event->shadow_ctx_time = now - t->timestamp;
508}
509
510static inline void
511perf_cgroup_defer_enabled(struct perf_event *event)
512{
513 /*
514 * when the current task's perf cgroup does not match
515 * the event's, we need to remember to call the
516 * perf_mark_enable() function the first time a task with
517 * a matching perf cgroup is scheduled in.
518 */
519 if (is_cgroup_event(event) && !perf_cgroup_match(event))
520 event->cgrp_defer_enabled = 1;
521}
522
523static inline void
524perf_cgroup_mark_enabled(struct perf_event *event,
525 struct perf_event_context *ctx)
526{
527 struct perf_event *sub;
528 u64 tstamp = perf_event_time(event);
529
530 if (!event->cgrp_defer_enabled)
531 return;
532
533 event->cgrp_defer_enabled = 0;
534
535 event->tstamp_enabled = tstamp - event->total_time_enabled;
536 list_for_each_entry(sub, &event->sibling_list, group_entry) {
537 if (sub->state >= PERF_EVENT_STATE_INACTIVE) {
538 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
539 sub->cgrp_defer_enabled = 0;
540 }
541 }
542}
543#else /* !CONFIG_CGROUP_PERF */
544
545static inline bool
546perf_cgroup_match(struct perf_event *event)
547{
548 return true;
549}
550
551static inline void perf_detach_cgroup(struct perf_event *event)
552{}
553
554static inline int is_cgroup_event(struct perf_event *event)
555{
556 return 0;
557}
558
559static inline u64 perf_cgroup_event_cgrp_time(struct perf_event *event)
560{
561 return 0;
562}
563
564static inline void update_cgrp_time_from_event(struct perf_event *event)
565{
566}
567
568static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
569{
570}
571
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200572static inline void perf_cgroup_sched_out(struct task_struct *task,
573 struct task_struct *next)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200574{
575}
576
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200577static inline void perf_cgroup_sched_in(struct task_struct *prev,
578 struct task_struct *task)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200579{
580}
581
582static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event,
583 struct perf_event_attr *attr,
584 struct perf_event *group_leader)
585{
586 return -EINVAL;
587}
588
589static inline void
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200590perf_cgroup_set_timestamp(struct task_struct *task,
591 struct perf_event_context *ctx)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200592{
593}
594
595void
596perf_cgroup_switch(struct task_struct *task, struct task_struct *next)
597{
598}
599
600static inline void
601perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
602{
603}
604
605static inline u64 perf_cgroup_event_time(struct perf_event *event)
606{
607 return 0;
608}
609
610static inline void
611perf_cgroup_defer_enabled(struct perf_event *event)
612{
613}
614
615static inline void
616perf_cgroup_mark_enabled(struct perf_event *event,
617 struct perf_event_context *ctx)
618{
619}
620#endif
621
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200622void perf_pmu_disable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200623{
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200624 int *count = this_cpu_ptr(pmu->pmu_disable_count);
625 if (!(*count)++)
626 pmu->pmu_disable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200627}
628
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200629void perf_pmu_enable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200630{
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200631 int *count = this_cpu_ptr(pmu->pmu_disable_count);
632 if (!--(*count))
633 pmu->pmu_enable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200634}
635
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200636static DEFINE_PER_CPU(struct list_head, rotation_list);
637
638/*
639 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
640 * because they're strictly cpu affine and rotate_start is called with IRQs
641 * disabled, while rotate_context is called from IRQ context.
642 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200643static void perf_pmu_rotate_start(struct pmu *pmu)
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200644{
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200645 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200646 struct list_head *head = &__get_cpu_var(rotation_list);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200647
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200648 WARN_ON(!irqs_disabled());
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200649
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200650 if (list_empty(&cpuctx->rotation_list))
651 list_add(&cpuctx->rotation_list, head);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200652}
653
654static void get_ctx(struct perf_event_context *ctx)
655{
656 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
657}
658
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200659static void put_ctx(struct perf_event_context *ctx)
660{
661 if (atomic_dec_and_test(&ctx->refcount)) {
662 if (ctx->parent_ctx)
663 put_ctx(ctx->parent_ctx);
664 if (ctx->task)
665 put_task_struct(ctx->task);
Lai Jiangshancb796ff2011-03-18 12:07:41 +0800666 kfree_rcu(ctx, rcu_head);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200667 }
668}
669
670static void unclone_ctx(struct perf_event_context *ctx)
671{
672 if (ctx->parent_ctx) {
673 put_ctx(ctx->parent_ctx);
674 ctx->parent_ctx = NULL;
675 }
676}
677
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -0200678static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
679{
680 /*
681 * only top level events have the pid namespace they were created in
682 */
683 if (event->parent)
684 event = event->parent;
685
686 return task_tgid_nr_ns(p, event->ns);
687}
688
689static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
690{
691 /*
692 * only top level events have the pid namespace they were created in
693 */
694 if (event->parent)
695 event = event->parent;
696
697 return task_pid_nr_ns(p, event->ns);
698}
699
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200700/*
701 * If we inherit events we want to return the parent event id
702 * to userspace.
703 */
704static u64 primary_event_id(struct perf_event *event)
705{
706 u64 id = event->id;
707
708 if (event->parent)
709 id = event->parent->id;
710
711 return id;
712}
713
714/*
715 * Get the perf_event_context for a task and lock it.
716 * This has to cope with with the fact that until it is locked,
717 * the context could get moved to another task.
718 */
719static struct perf_event_context *
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200720perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200721{
722 struct perf_event_context *ctx;
723
724 rcu_read_lock();
Peter Zijlstra9ed60602010-06-11 17:36:35 +0200725retry:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200726 ctx = rcu_dereference(task->perf_event_ctxp[ctxn]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200727 if (ctx) {
728 /*
729 * If this context is a clone of another, it might
730 * get swapped for another underneath us by
731 * perf_event_task_sched_out, though the
732 * rcu_read_lock() protects us from any context
733 * getting freed. Lock the context and check if it
734 * got swapped before we could get the lock, and retry
735 * if so. If we locked the right context, then it
736 * can't get swapped on us any more.
737 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100738 raw_spin_lock_irqsave(&ctx->lock, *flags);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200739 if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100740 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200741 goto retry;
742 }
743
744 if (!atomic_inc_not_zero(&ctx->refcount)) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100745 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200746 ctx = NULL;
747 }
748 }
749 rcu_read_unlock();
750 return ctx;
751}
752
753/*
754 * Get the context for a task and increment its pin_count so it
755 * can't get swapped to another task. This also increments its
756 * reference count so that the context can't get freed.
757 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200758static struct perf_event_context *
759perf_pin_task_context(struct task_struct *task, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200760{
761 struct perf_event_context *ctx;
762 unsigned long flags;
763
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200764 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200765 if (ctx) {
766 ++ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100767 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200768 }
769 return ctx;
770}
771
772static void perf_unpin_context(struct perf_event_context *ctx)
773{
774 unsigned long flags;
775
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100776 raw_spin_lock_irqsave(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200777 --ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100778 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200779}
780
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100781/*
782 * Update the record of the current time in a context.
783 */
784static void update_context_time(struct perf_event_context *ctx)
785{
786 u64 now = perf_clock();
787
788 ctx->time += now - ctx->timestamp;
789 ctx->timestamp = now;
790}
791
Stephane Eranian41587552011-01-03 18:20:01 +0200792static u64 perf_event_time(struct perf_event *event)
793{
794 struct perf_event_context *ctx = event->ctx;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200795
796 if (is_cgroup_event(event))
797 return perf_cgroup_event_time(event);
798
Stephane Eranian41587552011-01-03 18:20:01 +0200799 return ctx ? ctx->time : 0;
800}
801
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100802/*
803 * Update the total_time_enabled and total_time_running fields for a event.
Eric B Munsonb7526f02011-06-23 16:34:37 -0400804 * The caller of this function needs to hold the ctx->lock.
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100805 */
806static void update_event_times(struct perf_event *event)
807{
808 struct perf_event_context *ctx = event->ctx;
809 u64 run_end;
810
811 if (event->state < PERF_EVENT_STATE_INACTIVE ||
812 event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
813 return;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200814 /*
815 * in cgroup mode, time_enabled represents
816 * the time the event was enabled AND active
817 * tasks were in the monitored cgroup. This is
818 * independent of the activity of the context as
819 * there may be a mix of cgroup and non-cgroup events.
820 *
821 * That is why we treat cgroup events differently
822 * here.
823 */
824 if (is_cgroup_event(event))
Namhyung Kim46cd6a7f2012-01-20 10:12:46 +0900825 run_end = perf_cgroup_event_time(event);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200826 else if (ctx->is_active)
827 run_end = ctx->time;
Peter Zijlstraacd1d7c2009-11-23 15:00:36 +0100828 else
829 run_end = event->tstamp_stopped;
830
831 event->total_time_enabled = run_end - event->tstamp_enabled;
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100832
833 if (event->state == PERF_EVENT_STATE_INACTIVE)
834 run_end = event->tstamp_stopped;
835 else
Stephane Eranian41587552011-01-03 18:20:01 +0200836 run_end = perf_event_time(event);
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100837
838 event->total_time_running = run_end - event->tstamp_running;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200839
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100840}
841
Peter Zijlstra96c21a42010-05-11 16:19:10 +0200842/*
843 * Update total_time_enabled and total_time_running for all events in a group.
844 */
845static void update_group_times(struct perf_event *leader)
846{
847 struct perf_event *event;
848
849 update_event_times(leader);
850 list_for_each_entry(event, &leader->sibling_list, group_entry)
851 update_event_times(event);
852}
853
Frederic Weisbecker889ff012010-01-09 20:04:47 +0100854static struct list_head *
855ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
856{
857 if (event->attr.pinned)
858 return &ctx->pinned_groups;
859 else
860 return &ctx->flexible_groups;
861}
862
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200863/*
864 * Add a event from the lists for its context.
865 * Must be called with ctx->mutex and ctx->lock held.
866 */
867static void
868list_add_event(struct perf_event *event, struct perf_event_context *ctx)
869{
Peter Zijlstra8a495422010-05-27 15:47:49 +0200870 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
871 event->attach_state |= PERF_ATTACH_CONTEXT;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200872
873 /*
Peter Zijlstra8a495422010-05-27 15:47:49 +0200874 * If we're a stand alone event or group leader, we go to the context
875 * list, group events are kept attached to the group so that
876 * perf_group_detach can, at all times, locate all siblings.
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200877 */
Peter Zijlstra8a495422010-05-27 15:47:49 +0200878 if (event->group_leader == event) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +0100879 struct list_head *list;
880
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +0100881 if (is_software_event(event))
882 event->group_flags |= PERF_GROUP_SOFTWARE;
883
Frederic Weisbecker889ff012010-01-09 20:04:47 +0100884 list = ctx_group_list(event, ctx);
885 list_add_tail(&event->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200886 }
887
Peter Zijlstra08309372011-03-03 11:31:20 +0100888 if (is_cgroup_event(event))
Stephane Eraniane5d13672011-02-14 11:20:01 +0200889 ctx->nr_cgroups++;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200890
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200891 list_add_rcu(&event->event_entry, &ctx->event_list);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200892 if (!ctx->nr_events)
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200893 perf_pmu_rotate_start(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200894 ctx->nr_events++;
895 if (event->attr.inherit_stat)
896 ctx->nr_stat++;
897}
898
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -0200899/*
900 * Called at perf_event creation and when events are attached/detached from a
901 * group.
902 */
903static void perf_event__read_size(struct perf_event *event)
904{
905 int entry = sizeof(u64); /* value */
906 int size = 0;
907 int nr = 1;
908
909 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
910 size += sizeof(u64);
911
912 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
913 size += sizeof(u64);
914
915 if (event->attr.read_format & PERF_FORMAT_ID)
916 entry += sizeof(u64);
917
918 if (event->attr.read_format & PERF_FORMAT_GROUP) {
919 nr += event->group_leader->nr_siblings;
920 size += sizeof(u64);
921 }
922
923 size += entry * nr;
924 event->read_size = size;
925}
926
927static void perf_event__header_size(struct perf_event *event)
928{
929 struct perf_sample_data *data;
930 u64 sample_type = event->attr.sample_type;
931 u16 size = 0;
932
933 perf_event__read_size(event);
934
935 if (sample_type & PERF_SAMPLE_IP)
936 size += sizeof(data->ip);
937
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -0200938 if (sample_type & PERF_SAMPLE_ADDR)
939 size += sizeof(data->addr);
940
941 if (sample_type & PERF_SAMPLE_PERIOD)
942 size += sizeof(data->period);
943
944 if (sample_type & PERF_SAMPLE_READ)
945 size += event->read_size;
946
947 event->header_size = size;
948}
949
950static void perf_event__id_header_size(struct perf_event *event)
951{
952 struct perf_sample_data *data;
953 u64 sample_type = event->attr.sample_type;
954 u16 size = 0;
955
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -0200956 if (sample_type & PERF_SAMPLE_TID)
957 size += sizeof(data->tid_entry);
958
959 if (sample_type & PERF_SAMPLE_TIME)
960 size += sizeof(data->time);
961
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -0200962 if (sample_type & PERF_SAMPLE_ID)
963 size += sizeof(data->id);
964
965 if (sample_type & PERF_SAMPLE_STREAM_ID)
966 size += sizeof(data->stream_id);
967
968 if (sample_type & PERF_SAMPLE_CPU)
969 size += sizeof(data->cpu_entry);
970
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -0200971 event->id_header_size = size;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -0200972}
973
Peter Zijlstra8a495422010-05-27 15:47:49 +0200974static void perf_group_attach(struct perf_event *event)
975{
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -0200976 struct perf_event *group_leader = event->group_leader, *pos;
Peter Zijlstra8a495422010-05-27 15:47:49 +0200977
Peter Zijlstra74c33372010-10-15 11:40:29 +0200978 /*
979 * We can have double attach due to group movement in perf_event_open.
980 */
981 if (event->attach_state & PERF_ATTACH_GROUP)
982 return;
983
Peter Zijlstra8a495422010-05-27 15:47:49 +0200984 event->attach_state |= PERF_ATTACH_GROUP;
985
986 if (group_leader == event)
987 return;
988
989 if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
990 !is_software_event(event))
991 group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
992
993 list_add_tail(&event->group_entry, &group_leader->sibling_list);
994 group_leader->nr_siblings++;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -0200995
996 perf_event__header_size(group_leader);
997
998 list_for_each_entry(pos, &group_leader->sibling_list, group_entry)
999 perf_event__header_size(pos);
Peter Zijlstra8a495422010-05-27 15:47:49 +02001000}
1001
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001002/*
1003 * Remove a event from the lists for its context.
1004 * Must be called with ctx->mutex and ctx->lock held.
1005 */
1006static void
1007list_del_event(struct perf_event *event, struct perf_event_context *ctx)
1008{
Stephane Eranian68cacd22011-03-23 16:03:06 +01001009 struct perf_cpu_context *cpuctx;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001010 /*
1011 * We can have double detach due to exit/hot-unplug + close.
1012 */
1013 if (!(event->attach_state & PERF_ATTACH_CONTEXT))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001014 return;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001015
1016 event->attach_state &= ~PERF_ATTACH_CONTEXT;
1017
Stephane Eranian68cacd22011-03-23 16:03:06 +01001018 if (is_cgroup_event(event)) {
Stephane Eraniane5d13672011-02-14 11:20:01 +02001019 ctx->nr_cgroups--;
Stephane Eranian68cacd22011-03-23 16:03:06 +01001020 cpuctx = __get_cpu_context(ctx);
1021 /*
1022 * if there are no more cgroup events
1023 * then cler cgrp to avoid stale pointer
1024 * in update_cgrp_time_from_cpuctx()
1025 */
1026 if (!ctx->nr_cgroups)
1027 cpuctx->cgrp = NULL;
1028 }
Stephane Eraniane5d13672011-02-14 11:20:01 +02001029
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001030 ctx->nr_events--;
1031 if (event->attr.inherit_stat)
1032 ctx->nr_stat--;
1033
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001034 list_del_rcu(&event->event_entry);
1035
Peter Zijlstra8a495422010-05-27 15:47:49 +02001036 if (event->group_leader == event)
1037 list_del_init(&event->group_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001038
Peter Zijlstra96c21a42010-05-11 16:19:10 +02001039 update_group_times(event);
Stephane Eranianb2e74a22009-11-26 09:24:30 -08001040
1041 /*
1042 * If event was in error state, then keep it
1043 * that way, otherwise bogus counts will be
1044 * returned on read(). The only way to get out
1045 * of error state is by explicit re-enabling
1046 * of the event
1047 */
1048 if (event->state > PERF_EVENT_STATE_OFF)
1049 event->state = PERF_EVENT_STATE_OFF;
Peter Zijlstra050735b2010-05-11 11:51:53 +02001050}
1051
Peter Zijlstra8a495422010-05-27 15:47:49 +02001052static void perf_group_detach(struct perf_event *event)
Peter Zijlstra050735b2010-05-11 11:51:53 +02001053{
1054 struct perf_event *sibling, *tmp;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001055 struct list_head *list = NULL;
1056
1057 /*
1058 * We can have double detach due to exit/hot-unplug + close.
1059 */
1060 if (!(event->attach_state & PERF_ATTACH_GROUP))
1061 return;
1062
1063 event->attach_state &= ~PERF_ATTACH_GROUP;
1064
1065 /*
1066 * If this is a sibling, remove it from its group.
1067 */
1068 if (event->group_leader != event) {
1069 list_del_init(&event->group_entry);
1070 event->group_leader->nr_siblings--;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001071 goto out;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001072 }
1073
1074 if (!list_empty(&event->group_entry))
1075 list = &event->group_entry;
Peter Zijlstra2e2af502009-11-23 11:37:25 +01001076
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001077 /*
1078 * If this was a group event with sibling events then
1079 * upgrade the siblings to singleton events by adding them
Peter Zijlstra8a495422010-05-27 15:47:49 +02001080 * to whatever list we are on.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001081 */
1082 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
Peter Zijlstra8a495422010-05-27 15:47:49 +02001083 if (list)
1084 list_move_tail(&sibling->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001085 sibling->group_leader = sibling;
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01001086
1087 /* Inherit group flags from the previous leader */
1088 sibling->group_flags = event->group_flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001089 }
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001090
1091out:
1092 perf_event__header_size(event->group_leader);
1093
1094 list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry)
1095 perf_event__header_size(tmp);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001096}
1097
Stephane Eranianfa66f072010-08-26 16:40:01 +02001098static inline int
1099event_filter_match(struct perf_event *event)
1100{
Stephane Eraniane5d13672011-02-14 11:20:01 +02001101 return (event->cpu == -1 || event->cpu == smp_processor_id())
1102 && perf_cgroup_match(event);
Stephane Eranianfa66f072010-08-26 16:40:01 +02001103}
1104
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001105static void
1106event_sched_out(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001107 struct perf_cpu_context *cpuctx,
1108 struct perf_event_context *ctx)
1109{
Stephane Eranian41587552011-01-03 18:20:01 +02001110 u64 tstamp = perf_event_time(event);
Stephane Eranianfa66f072010-08-26 16:40:01 +02001111 u64 delta;
1112 /*
1113 * An event which could not be activated because of
1114 * filter mismatch still needs to have its timings
1115 * maintained, otherwise bogus information is return
1116 * via read() for time_enabled, time_running:
1117 */
1118 if (event->state == PERF_EVENT_STATE_INACTIVE
1119 && !event_filter_match(event)) {
Stephane Eraniane5d13672011-02-14 11:20:01 +02001120 delta = tstamp - event->tstamp_stopped;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001121 event->tstamp_running += delta;
Stephane Eranian41587552011-01-03 18:20:01 +02001122 event->tstamp_stopped = tstamp;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001123 }
1124
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001125 if (event->state != PERF_EVENT_STATE_ACTIVE)
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001126 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001127
1128 event->state = PERF_EVENT_STATE_INACTIVE;
1129 if (event->pending_disable) {
1130 event->pending_disable = 0;
1131 event->state = PERF_EVENT_STATE_OFF;
1132 }
Stephane Eranian41587552011-01-03 18:20:01 +02001133 event->tstamp_stopped = tstamp;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001134 event->pmu->del(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001135 event->oncpu = -1;
1136
1137 if (!is_software_event(event))
1138 cpuctx->active_oncpu--;
1139 ctx->nr_active--;
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01001140 if (event->attr.freq && event->attr.sample_freq)
1141 ctx->nr_freq--;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001142 if (event->attr.exclusive || !cpuctx->active_oncpu)
1143 cpuctx->exclusive = 0;
1144}
1145
1146static void
1147group_sched_out(struct perf_event *group_event,
1148 struct perf_cpu_context *cpuctx,
1149 struct perf_event_context *ctx)
1150{
1151 struct perf_event *event;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001152 int state = group_event->state;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001153
1154 event_sched_out(group_event, cpuctx, ctx);
1155
1156 /*
1157 * Schedule out siblings (if any):
1158 */
1159 list_for_each_entry(event, &group_event->sibling_list, group_entry)
1160 event_sched_out(event, cpuctx, ctx);
1161
Stephane Eranianfa66f072010-08-26 16:40:01 +02001162 if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001163 cpuctx->exclusive = 0;
1164}
1165
1166/*
1167 * Cross CPU call to remove a performance event
1168 *
1169 * We disable the event on the hardware level first. After that we
1170 * remove it from the context list.
1171 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001172static int __perf_remove_from_context(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001173{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001174 struct perf_event *event = info;
1175 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001176 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001177
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001178 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001179 event_sched_out(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001180 list_del_event(event, ctx);
Peter Zijlstra64ce3122011-04-09 21:17:48 +02001181 if (!ctx->nr_events && cpuctx->task_ctx == ctx) {
1182 ctx->is_active = 0;
1183 cpuctx->task_ctx = NULL;
1184 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001185 raw_spin_unlock(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001186
1187 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001188}
1189
1190
1191/*
1192 * Remove the event from a task's (or a CPU's) list of events.
1193 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001194 * CPU events are removed with a smp call. For task events we only
1195 * call when the task is on a CPU.
1196 *
1197 * If event->ctx is a cloned context, callers must make sure that
1198 * every task struct that event->ctx->task could possibly point to
1199 * remains valid. This is OK when called from perf_release since
1200 * that only calls us on the top-level context, which can't be a clone.
1201 * When called from perf_event_exit_task, it's OK because the
1202 * context has been detached from its task.
1203 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001204static void perf_remove_from_context(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001205{
1206 struct perf_event_context *ctx = event->ctx;
1207 struct task_struct *task = ctx->task;
1208
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001209 lockdep_assert_held(&ctx->mutex);
1210
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001211 if (!task) {
1212 /*
1213 * Per cpu events are removed via an smp call and
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001214 * the removal is always successful.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001215 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001216 cpu_function_call(event->cpu, __perf_remove_from_context, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001217 return;
1218 }
1219
1220retry:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001221 if (!task_function_call(task, __perf_remove_from_context, event))
1222 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001223
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001224 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001225 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001226 * If we failed to find a running task, but find the context active now
1227 * that we've acquired the ctx->lock, retry.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001228 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001229 if (ctx->is_active) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001230 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001231 goto retry;
1232 }
1233
1234 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001235 * Since the task isn't running, its safe to remove the event, us
1236 * holding the ctx->lock ensures the task won't get scheduled in.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001237 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001238 list_del_event(event, ctx);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001239 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001240}
1241
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001242/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001243 * Cross CPU call to disable a performance event
1244 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001245static int __perf_event_disable(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001246{
1247 struct perf_event *event = info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001248 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001249 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001250
1251 /*
1252 * If this is a per-task event, need to check whether this
1253 * event's task is the current task on this cpu.
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001254 *
1255 * Can trigger due to concurrent perf_event_context_sched_out()
1256 * flipping contexts around.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001257 */
1258 if (ctx->task && cpuctx->task_ctx != ctx)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001259 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001260
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001261 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001262
1263 /*
1264 * If the event is on, turn it off.
1265 * If it is in error state, leave it in error state.
1266 */
1267 if (event->state >= PERF_EVENT_STATE_INACTIVE) {
1268 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001269 update_cgrp_time_from_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001270 update_group_times(event);
1271 if (event == event->group_leader)
1272 group_sched_out(event, cpuctx, ctx);
1273 else
1274 event_sched_out(event, cpuctx, ctx);
1275 event->state = PERF_EVENT_STATE_OFF;
1276 }
1277
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001278 raw_spin_unlock(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001279
1280 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001281}
1282
1283/*
1284 * Disable a event.
1285 *
1286 * If event->ctx is a cloned context, callers must make sure that
1287 * every task struct that event->ctx->task could possibly point to
1288 * remains valid. This condition is satisifed when called through
1289 * perf_event_for_each_child or perf_event_for_each because they
1290 * hold the top-level event's child_mutex, so any descendant that
1291 * goes to exit will block in sync_child_event.
1292 * When called from perf_pending_event it's OK because event->ctx
1293 * is the current context on this CPU and preemption is disabled,
1294 * hence we can't get into perf_event_task_sched_out for this context.
1295 */
Frederic Weisbecker44234ad2009-12-09 09:25:48 +01001296void perf_event_disable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001297{
1298 struct perf_event_context *ctx = event->ctx;
1299 struct task_struct *task = ctx->task;
1300
1301 if (!task) {
1302 /*
1303 * Disable the event on the cpu that it's on
1304 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001305 cpu_function_call(event->cpu, __perf_event_disable, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001306 return;
1307 }
1308
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001309retry:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001310 if (!task_function_call(task, __perf_event_disable, event))
1311 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001312
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001313 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001314 /*
1315 * If the event is still active, we need to retry the cross-call.
1316 */
1317 if (event->state == PERF_EVENT_STATE_ACTIVE) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001318 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001319 /*
1320 * Reload the task pointer, it might have been changed by
1321 * a concurrent perf_event_context_sched_out().
1322 */
1323 task = ctx->task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001324 goto retry;
1325 }
1326
1327 /*
1328 * Since we have the lock this context can't be scheduled
1329 * in, so we can change the state safely.
1330 */
1331 if (event->state == PERF_EVENT_STATE_INACTIVE) {
1332 update_group_times(event);
1333 event->state = PERF_EVENT_STATE_OFF;
1334 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001335 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001336}
Robert Richterdcfce4a2011-10-11 17:11:08 +02001337EXPORT_SYMBOL_GPL(perf_event_disable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001338
Stephane Eraniane5d13672011-02-14 11:20:01 +02001339static void perf_set_shadow_time(struct perf_event *event,
1340 struct perf_event_context *ctx,
1341 u64 tstamp)
1342{
1343 /*
1344 * use the correct time source for the time snapshot
1345 *
1346 * We could get by without this by leveraging the
1347 * fact that to get to this function, the caller
1348 * has most likely already called update_context_time()
1349 * and update_cgrp_time_xx() and thus both timestamp
1350 * are identical (or very close). Given that tstamp is,
1351 * already adjusted for cgroup, we could say that:
1352 * tstamp - ctx->timestamp
1353 * is equivalent to
1354 * tstamp - cgrp->timestamp.
1355 *
1356 * Then, in perf_output_read(), the calculation would
1357 * work with no changes because:
1358 * - event is guaranteed scheduled in
1359 * - no scheduled out in between
1360 * - thus the timestamp would be the same
1361 *
1362 * But this is a bit hairy.
1363 *
1364 * So instead, we have an explicit cgroup call to remain
1365 * within the time time source all along. We believe it
1366 * is cleaner and simpler to understand.
1367 */
1368 if (is_cgroup_event(event))
1369 perf_cgroup_set_shadow_time(event, tstamp);
1370 else
1371 event->shadow_ctx_time = tstamp - ctx->timestamp;
1372}
1373
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01001374#define MAX_INTERRUPTS (~0ULL)
1375
1376static void perf_log_throttle(struct perf_event *event, int enable);
1377
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001378static int
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001379event_sched_in(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001380 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001381 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001382{
Stephane Eranian41587552011-01-03 18:20:01 +02001383 u64 tstamp = perf_event_time(event);
1384
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001385 if (event->state <= PERF_EVENT_STATE_OFF)
1386 return 0;
1387
1388 event->state = PERF_EVENT_STATE_ACTIVE;
Peter Zijlstra6e377382010-02-11 13:21:58 +01001389 event->oncpu = smp_processor_id();
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01001390
1391 /*
1392 * Unthrottle events, since we scheduled we might have missed several
1393 * ticks already, also for a heavily scheduling task there is little
1394 * guarantee it'll get a tick in a timely manner.
1395 */
1396 if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) {
1397 perf_log_throttle(event, 1);
1398 event->hw.interrupts = 0;
1399 }
1400
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001401 /*
1402 * The new state must be visible before we turn it on in the hardware:
1403 */
1404 smp_wmb();
1405
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001406 if (event->pmu->add(event, PERF_EF_START)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001407 event->state = PERF_EVENT_STATE_INACTIVE;
1408 event->oncpu = -1;
1409 return -EAGAIN;
1410 }
1411
Stephane Eranian41587552011-01-03 18:20:01 +02001412 event->tstamp_running += tstamp - event->tstamp_stopped;
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001413
Stephane Eraniane5d13672011-02-14 11:20:01 +02001414 perf_set_shadow_time(event, ctx, tstamp);
Stephane Eranianeed01522010-10-26 16:08:01 +02001415
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001416 if (!is_software_event(event))
1417 cpuctx->active_oncpu++;
1418 ctx->nr_active++;
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01001419 if (event->attr.freq && event->attr.sample_freq)
1420 ctx->nr_freq++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001421
1422 if (event->attr.exclusive)
1423 cpuctx->exclusive = 1;
1424
1425 return 0;
1426}
1427
1428static int
1429group_sched_in(struct perf_event *group_event,
1430 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001431 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001432{
Lin Ming6bde9b62010-04-23 13:56:00 +08001433 struct perf_event *event, *partial_group = NULL;
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02001434 struct pmu *pmu = group_event->pmu;
Stephane Eraniand7842da2010-10-20 15:25:01 +02001435 u64 now = ctx->time;
1436 bool simulate = false;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001437
1438 if (group_event->state == PERF_EVENT_STATE_OFF)
1439 return 0;
1440
Peter Zijlstraad5133b2010-06-15 12:22:39 +02001441 pmu->start_txn(pmu);
Lin Ming6bde9b62010-04-23 13:56:00 +08001442
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001443 if (event_sched_in(group_event, cpuctx, ctx)) {
Peter Zijlstraad5133b2010-06-15 12:22:39 +02001444 pmu->cancel_txn(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001445 return -EAGAIN;
Stephane Eranian90151c352010-05-25 16:23:10 +02001446 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001447
1448 /*
1449 * Schedule in siblings as one group (if any):
1450 */
1451 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001452 if (event_sched_in(event, cpuctx, ctx)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001453 partial_group = event;
1454 goto group_error;
1455 }
1456 }
1457
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001458 if (!pmu->commit_txn(pmu))
Paul Mackerras6e851582010-05-08 20:58:00 +10001459 return 0;
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001460
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001461group_error:
1462 /*
1463 * Groups can be scheduled in as one unit only, so undo any
1464 * partial group before returning:
Stephane Eraniand7842da2010-10-20 15:25:01 +02001465 * The events up to the failed event are scheduled out normally,
1466 * tstamp_stopped will be updated.
1467 *
1468 * The failed events and the remaining siblings need to have
1469 * their timings updated as if they had gone thru event_sched_in()
1470 * and event_sched_out(). This is required to get consistent timings
1471 * across the group. This also takes care of the case where the group
1472 * could never be scheduled by ensuring tstamp_stopped is set to mark
1473 * the time the event was actually stopped, such that time delta
1474 * calculation in update_event_times() is correct.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001475 */
1476 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
1477 if (event == partial_group)
Stephane Eraniand7842da2010-10-20 15:25:01 +02001478 simulate = true;
1479
1480 if (simulate) {
1481 event->tstamp_running += now - event->tstamp_stopped;
1482 event->tstamp_stopped = now;
1483 } else {
1484 event_sched_out(event, cpuctx, ctx);
1485 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001486 }
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001487 event_sched_out(group_event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001488
Peter Zijlstraad5133b2010-06-15 12:22:39 +02001489 pmu->cancel_txn(pmu);
Stephane Eranian90151c352010-05-25 16:23:10 +02001490
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001491 return -EAGAIN;
1492}
1493
1494/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001495 * Work out whether we can put this event group on the CPU now.
1496 */
1497static int group_can_go_on(struct perf_event *event,
1498 struct perf_cpu_context *cpuctx,
1499 int can_add_hw)
1500{
1501 /*
1502 * Groups consisting entirely of software events can always go on.
1503 */
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01001504 if (event->group_flags & PERF_GROUP_SOFTWARE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001505 return 1;
1506 /*
1507 * If an exclusive group is already on, no other hardware
1508 * events can go on.
1509 */
1510 if (cpuctx->exclusive)
1511 return 0;
1512 /*
1513 * If this group is exclusive and there are already
1514 * events on the CPU, it can't go on.
1515 */
1516 if (event->attr.exclusive && cpuctx->active_oncpu)
1517 return 0;
1518 /*
1519 * Otherwise, try to add it if all previous groups were able
1520 * to go on.
1521 */
1522 return can_add_hw;
1523}
1524
1525static void add_event_to_ctx(struct perf_event *event,
1526 struct perf_event_context *ctx)
1527{
Stephane Eranian41587552011-01-03 18:20:01 +02001528 u64 tstamp = perf_event_time(event);
1529
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001530 list_add_event(event, ctx);
Peter Zijlstra8a495422010-05-27 15:47:49 +02001531 perf_group_attach(event);
Stephane Eranian41587552011-01-03 18:20:01 +02001532 event->tstamp_enabled = tstamp;
1533 event->tstamp_running = tstamp;
1534 event->tstamp_stopped = tstamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001535}
1536
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001537static void task_ctx_sched_out(struct perf_event_context *ctx);
1538static void
1539ctx_sched_in(struct perf_event_context *ctx,
1540 struct perf_cpu_context *cpuctx,
1541 enum event_type_t event_type,
1542 struct task_struct *task);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001543
Peter Zijlstradce58552011-04-09 21:17:46 +02001544static void perf_event_sched_in(struct perf_cpu_context *cpuctx,
1545 struct perf_event_context *ctx,
1546 struct task_struct *task)
1547{
1548 cpu_ctx_sched_in(cpuctx, EVENT_PINNED, task);
1549 if (ctx)
1550 ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task);
1551 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task);
1552 if (ctx)
1553 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task);
1554}
1555
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001556/*
1557 * Cross CPU call to install and enable a performance event
1558 *
1559 * Must be called with ctx->mutex held
1560 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001561static int __perf_install_in_context(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001562{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001563 struct perf_event *event = info;
1564 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001565 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001566 struct perf_event_context *task_ctx = cpuctx->task_ctx;
1567 struct task_struct *task = current;
1568
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02001569 perf_ctx_lock(cpuctx, task_ctx);
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001570 perf_pmu_disable(cpuctx->ctx.pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001571
1572 /*
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001573 * If there was an active task_ctx schedule it out.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001574 */
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02001575 if (task_ctx)
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001576 task_ctx_sched_out(task_ctx);
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02001577
1578 /*
1579 * If the context we're installing events in is not the
1580 * active task_ctx, flip them.
1581 */
1582 if (ctx->task && task_ctx != ctx) {
1583 if (task_ctx)
1584 raw_spin_unlock(&task_ctx->lock);
1585 raw_spin_lock(&ctx->lock);
1586 task_ctx = ctx;
1587 }
1588
1589 if (task_ctx) {
1590 cpuctx->task_ctx = task_ctx;
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001591 task = task_ctx->task;
1592 }
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02001593
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001594 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001595
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001596 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001597 /*
1598 * update cgrp time only if current cgrp
1599 * matches event->cgrp. Must be done before
1600 * calling add_event_to_ctx()
1601 */
1602 update_cgrp_time_from_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001603
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001604 add_event_to_ctx(event, ctx);
1605
1606 /*
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001607 * Schedule everything back in
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001608 */
Peter Zijlstradce58552011-04-09 21:17:46 +02001609 perf_event_sched_in(cpuctx, task_ctx, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001610
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001611 perf_pmu_enable(cpuctx->ctx.pmu);
1612 perf_ctx_unlock(cpuctx, task_ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001613
1614 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001615}
1616
1617/*
1618 * Attach a performance event to a context
1619 *
1620 * First we add the event to the list with the hardware enable bit
1621 * in event->hw_config cleared.
1622 *
1623 * If the event is attached to a task which is on a CPU we use a smp
1624 * call to enable it in the task context. The task might have been
1625 * scheduled away, but we check this in the smp call again.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001626 */
1627static void
1628perf_install_in_context(struct perf_event_context *ctx,
1629 struct perf_event *event,
1630 int cpu)
1631{
1632 struct task_struct *task = ctx->task;
1633
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001634 lockdep_assert_held(&ctx->mutex);
1635
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02001636 event->ctx = ctx;
1637
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001638 if (!task) {
1639 /*
1640 * Per cpu events are installed via an smp call and
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001641 * the install is always successful.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001642 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001643 cpu_function_call(cpu, __perf_install_in_context, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001644 return;
1645 }
1646
1647retry:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001648 if (!task_function_call(task, __perf_install_in_context, event))
1649 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001650
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001651 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001652 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001653 * If we failed to find a running task, but find the context active now
1654 * that we've acquired the ctx->lock, retry.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001655 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001656 if (ctx->is_active) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001657 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001658 goto retry;
1659 }
1660
1661 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001662 * Since the task isn't running, its safe to add the event, us holding
1663 * the ctx->lock ensures the task won't get scheduled in.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001664 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001665 add_event_to_ctx(event, ctx);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001666 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001667}
1668
1669/*
1670 * Put a event into inactive state and update time fields.
1671 * Enabling the leader of a group effectively enables all
1672 * the group members that aren't explicitly disabled, so we
1673 * have to update their ->tstamp_enabled also.
1674 * Note: this works for group members as well as group leaders
1675 * since the non-leader members' sibling_lists will be empty.
1676 */
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01001677static void __perf_event_mark_enabled(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001678{
1679 struct perf_event *sub;
Stephane Eranian41587552011-01-03 18:20:01 +02001680 u64 tstamp = perf_event_time(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001681
1682 event->state = PERF_EVENT_STATE_INACTIVE;
Stephane Eranian41587552011-01-03 18:20:01 +02001683 event->tstamp_enabled = tstamp - event->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001684 list_for_each_entry(sub, &event->sibling_list, group_entry) {
Stephane Eranian41587552011-01-03 18:20:01 +02001685 if (sub->state >= PERF_EVENT_STATE_INACTIVE)
1686 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001687 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001688}
1689
1690/*
1691 * Cross CPU call to enable a performance event
1692 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001693static int __perf_event_enable(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001694{
1695 struct perf_event *event = info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001696 struct perf_event_context *ctx = event->ctx;
1697 struct perf_event *leader = event->group_leader;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001698 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001699 int err;
1700
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001701 if (WARN_ON_ONCE(!ctx->is_active))
1702 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001703
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001704 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001705 update_context_time(ctx);
1706
1707 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1708 goto unlock;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001709
1710 /*
1711 * set current task's cgroup time reference point
1712 */
Stephane Eranian3f7cce32011-02-18 14:40:01 +02001713 perf_cgroup_set_timestamp(current, ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001714
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01001715 __perf_event_mark_enabled(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001716
Stephane Eraniane5d13672011-02-14 11:20:01 +02001717 if (!event_filter_match(event)) {
1718 if (is_cgroup_event(event))
1719 perf_cgroup_defer_enabled(event);
Peter Zijlstraf4c41762009-12-16 17:55:54 +01001720 goto unlock;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001721 }
Peter Zijlstraf4c41762009-12-16 17:55:54 +01001722
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001723 /*
1724 * If the event is in a group and isn't the group leader,
1725 * then don't put it on unless the group is on.
1726 */
1727 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
1728 goto unlock;
1729
1730 if (!group_can_go_on(event, cpuctx, 1)) {
1731 err = -EEXIST;
1732 } else {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001733 if (event == leader)
Peter Zijlstra6e377382010-02-11 13:21:58 +01001734 err = group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001735 else
Peter Zijlstra6e377382010-02-11 13:21:58 +01001736 err = event_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001737 }
1738
1739 if (err) {
1740 /*
1741 * If this event can't go on and it's part of a
1742 * group, then the whole group has to come off.
1743 */
1744 if (leader != event)
1745 group_sched_out(leader, cpuctx, ctx);
1746 if (leader->attr.pinned) {
1747 update_group_times(leader);
1748 leader->state = PERF_EVENT_STATE_ERROR;
1749 }
1750 }
1751
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001752unlock:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001753 raw_spin_unlock(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001754
1755 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001756}
1757
1758/*
1759 * Enable a event.
1760 *
1761 * If event->ctx is a cloned context, callers must make sure that
1762 * every task struct that event->ctx->task could possibly point to
1763 * remains valid. This condition is satisfied when called through
1764 * perf_event_for_each_child or perf_event_for_each as described
1765 * for perf_event_disable.
1766 */
Frederic Weisbecker44234ad2009-12-09 09:25:48 +01001767void perf_event_enable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001768{
1769 struct perf_event_context *ctx = event->ctx;
1770 struct task_struct *task = ctx->task;
1771
1772 if (!task) {
1773 /*
1774 * Enable the event on the cpu that it's on
1775 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001776 cpu_function_call(event->cpu, __perf_event_enable, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001777 return;
1778 }
1779
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001780 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001781 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1782 goto out;
1783
1784 /*
1785 * If the event is in error state, clear that first.
1786 * That way, if we see the event in error state below, we
1787 * know that it has gone back into error state, as distinct
1788 * from the task having been scheduled away before the
1789 * cross-call arrived.
1790 */
1791 if (event->state == PERF_EVENT_STATE_ERROR)
1792 event->state = PERF_EVENT_STATE_OFF;
1793
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001794retry:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001795 if (!ctx->is_active) {
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01001796 __perf_event_mark_enabled(event);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001797 goto out;
1798 }
1799
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001800 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001801
1802 if (!task_function_call(task, __perf_event_enable, event))
1803 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001804
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001805 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001806
1807 /*
1808 * If the context is active and the event is still off,
1809 * we need to retry the cross-call.
1810 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001811 if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF) {
1812 /*
1813 * task could have been flipped by a concurrent
1814 * perf_event_context_sched_out()
1815 */
1816 task = ctx->task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001817 goto retry;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001818 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001819
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001820out:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001821 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001822}
Robert Richterdcfce4a2011-10-11 17:11:08 +02001823EXPORT_SYMBOL_GPL(perf_event_enable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001824
Avi Kivity26ca5c12011-06-29 18:42:37 +03001825int perf_event_refresh(struct perf_event *event, int refresh)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001826{
1827 /*
1828 * not supported on inherited events
1829 */
Franck Bui-Huu2e939d12010-11-23 16:21:44 +01001830 if (event->attr.inherit || !is_sampling_event(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001831 return -EINVAL;
1832
1833 atomic_add(refresh, &event->event_limit);
1834 perf_event_enable(event);
1835
1836 return 0;
1837}
Avi Kivity26ca5c12011-06-29 18:42:37 +03001838EXPORT_SYMBOL_GPL(perf_event_refresh);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001839
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001840static void ctx_sched_out(struct perf_event_context *ctx,
1841 struct perf_cpu_context *cpuctx,
1842 enum event_type_t event_type)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001843{
1844 struct perf_event *event;
Peter Zijlstradb24d332011-04-09 21:17:45 +02001845 int is_active = ctx->is_active;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001846
Peter Zijlstradb24d332011-04-09 21:17:45 +02001847 ctx->is_active &= ~event_type;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001848 if (likely(!ctx->nr_events))
Peter Zijlstrafacc4302011-04-09 21:17:42 +02001849 return;
1850
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001851 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001852 update_cgrp_time_from_cpuctx(cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001853 if (!ctx->nr_active)
Peter Zijlstrafacc4302011-04-09 21:17:42 +02001854 return;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001855
Peter Zijlstra075e0b02011-04-09 21:17:40 +02001856 perf_pmu_disable(ctx->pmu);
Peter Zijlstradb24d332011-04-09 21:17:45 +02001857 if ((is_active & EVENT_PINNED) && (event_type & EVENT_PINNED)) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001858 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
1859 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001860 }
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001861
Peter Zijlstradb24d332011-04-09 21:17:45 +02001862 if ((is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE)) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001863 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08001864 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001865 }
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02001866 perf_pmu_enable(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001867}
1868
1869/*
1870 * Test whether two contexts are equivalent, i.e. whether they
1871 * have both been cloned from the same version of the same context
1872 * and they both have the same number of enabled events.
1873 * If the number of enabled events is the same, then the set
1874 * of enabled events should be the same, because these are both
1875 * inherited contexts, therefore we can't access individual events
1876 * in them directly with an fd; we can only enable/disable all
1877 * events via prctl, or enable/disable all events in a family
1878 * via ioctl, which will have the same effect on both contexts.
1879 */
1880static int context_equiv(struct perf_event_context *ctx1,
1881 struct perf_event_context *ctx2)
1882{
1883 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
1884 && ctx1->parent_gen == ctx2->parent_gen
1885 && !ctx1->pin_count && !ctx2->pin_count;
1886}
1887
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001888static void __perf_event_sync_stat(struct perf_event *event,
1889 struct perf_event *next_event)
1890{
1891 u64 value;
1892
1893 if (!event->attr.inherit_stat)
1894 return;
1895
1896 /*
1897 * Update the event value, we cannot use perf_event_read()
1898 * because we're in the middle of a context switch and have IRQs
1899 * disabled, which upsets smp_call_function_single(), however
1900 * we know the event must be on the current CPU, therefore we
1901 * don't need to use it.
1902 */
1903 switch (event->state) {
1904 case PERF_EVENT_STATE_ACTIVE:
Peter Zijlstra3dbebf12009-11-20 22:19:52 +01001905 event->pmu->read(event);
1906 /* fall-through */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001907
1908 case PERF_EVENT_STATE_INACTIVE:
1909 update_event_times(event);
1910 break;
1911
1912 default:
1913 break;
1914 }
1915
1916 /*
1917 * In order to keep per-task stats reliable we need to flip the event
1918 * values when we flip the contexts.
1919 */
Peter Zijlstrae7850592010-05-21 14:43:08 +02001920 value = local64_read(&next_event->count);
1921 value = local64_xchg(&event->count, value);
1922 local64_set(&next_event->count, value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001923
1924 swap(event->total_time_enabled, next_event->total_time_enabled);
1925 swap(event->total_time_running, next_event->total_time_running);
1926
1927 /*
1928 * Since we swizzled the values, update the user visible data too.
1929 */
1930 perf_event_update_userpage(event);
1931 perf_event_update_userpage(next_event);
1932}
1933
1934#define list_next_entry(pos, member) \
1935 list_entry(pos->member.next, typeof(*pos), member)
1936
1937static void perf_event_sync_stat(struct perf_event_context *ctx,
1938 struct perf_event_context *next_ctx)
1939{
1940 struct perf_event *event, *next_event;
1941
1942 if (!ctx->nr_stat)
1943 return;
1944
Peter Zijlstra02ffdbc2009-11-20 22:19:50 +01001945 update_context_time(ctx);
1946
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001947 event = list_first_entry(&ctx->event_list,
1948 struct perf_event, event_entry);
1949
1950 next_event = list_first_entry(&next_ctx->event_list,
1951 struct perf_event, event_entry);
1952
1953 while (&event->event_entry != &ctx->event_list &&
1954 &next_event->event_entry != &next_ctx->event_list) {
1955
1956 __perf_event_sync_stat(event, next_event);
1957
1958 event = list_next_entry(event, event_entry);
1959 next_event = list_next_entry(next_event, event_entry);
1960 }
1961}
1962
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001963static void perf_event_context_sched_out(struct task_struct *task, int ctxn,
1964 struct task_struct *next)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001965{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001966 struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001967 struct perf_event_context *next_ctx;
1968 struct perf_event_context *parent;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001969 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001970 int do_switch = 1;
1971
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001972 if (likely(!ctx))
1973 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001974
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001975 cpuctx = __get_cpu_context(ctx);
1976 if (!cpuctx->task_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001977 return;
1978
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001979 rcu_read_lock();
1980 parent = rcu_dereference(ctx->parent_ctx);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001981 next_ctx = next->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001982 if (parent && next_ctx &&
1983 rcu_dereference(next_ctx->parent_ctx) == parent) {
1984 /*
1985 * Looks like the two contexts are clones, so we might be
1986 * able to optimize the context switch. We lock both
1987 * contexts and check that they are clones under the
1988 * lock (including re-checking that neither has been
1989 * uncloned in the meantime). It doesn't matter which
1990 * order we take the locks because no other cpu could
1991 * be trying to lock both of these tasks.
1992 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001993 raw_spin_lock(&ctx->lock);
1994 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001995 if (context_equiv(ctx, next_ctx)) {
1996 /*
1997 * XXX do we need a memory barrier of sorts
1998 * wrt to rcu_dereference() of perf_event_ctxp
1999 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002000 task->perf_event_ctxp[ctxn] = next_ctx;
2001 next->perf_event_ctxp[ctxn] = ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002002 ctx->task = next;
2003 next_ctx->task = task;
2004 do_switch = 0;
2005
2006 perf_event_sync_stat(ctx, next_ctx);
2007 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002008 raw_spin_unlock(&next_ctx->lock);
2009 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002010 }
2011 rcu_read_unlock();
2012
2013 if (do_switch) {
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002014 raw_spin_lock(&ctx->lock);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002015 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002016 cpuctx->task_ctx = NULL;
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002017 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002018 }
2019}
2020
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002021#define for_each_task_context_nr(ctxn) \
2022 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
2023
2024/*
2025 * Called from scheduler to remove the events of the current task,
2026 * with interrupts disabled.
2027 *
2028 * We stop each event and update the event value in event->count.
2029 *
2030 * This does not protect us against NMI, but disable()
2031 * sets the disabled bit in the control field of event _before_
2032 * accessing the event control register. If a NMI hits, then it will
2033 * not restart the event.
2034 */
Peter Zijlstra82cd6de2010-10-14 17:57:23 +02002035void __perf_event_task_sched_out(struct task_struct *task,
2036 struct task_struct *next)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002037{
2038 int ctxn;
2039
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002040 for_each_task_context_nr(ctxn)
2041 perf_event_context_sched_out(task, ctxn, next);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002042
2043 /*
2044 * if cgroup events exist on this CPU, then we need
2045 * to check if we have to switch out PMU state.
2046 * cgroup event are system-wide mode only
2047 */
2048 if (atomic_read(&__get_cpu_var(perf_cgroup_events)))
Stephane Eraniana8d757e2011-08-25 15:58:03 +02002049 perf_cgroup_sched_out(task, next);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002050}
2051
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02002052static void task_ctx_sched_out(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002053{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002054 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002055
2056 if (!cpuctx->task_ctx)
2057 return;
2058
2059 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
2060 return;
2061
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02002062 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002063 cpuctx->task_ctx = NULL;
2064}
2065
2066/*
2067 * Called with IRQs disabled
2068 */
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002069static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
2070 enum event_type_t event_type)
2071{
2072 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002073}
2074
2075static void
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002076ctx_pinned_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01002077 struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002078{
2079 struct perf_event *event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002080
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002081 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
2082 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002083 continue;
Stephane Eranian5632ab12011-01-03 18:20:01 +02002084 if (!event_filter_match(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002085 continue;
2086
Stephane Eraniane5d13672011-02-14 11:20:01 +02002087 /* may need to reset tstamp_enabled */
2088 if (is_cgroup_event(event))
2089 perf_cgroup_mark_enabled(event, ctx);
2090
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08002091 if (group_can_go_on(event, cpuctx, 1))
Peter Zijlstra6e377382010-02-11 13:21:58 +01002092 group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002093
2094 /*
2095 * If this pinned group hasn't been scheduled,
2096 * put it in error state.
2097 */
2098 if (event->state == PERF_EVENT_STATE_INACTIVE) {
2099 update_group_times(event);
2100 event->state = PERF_EVENT_STATE_ERROR;
2101 }
2102 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002103}
2104
2105static void
2106ctx_flexible_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01002107 struct perf_cpu_context *cpuctx)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002108{
2109 struct perf_event *event;
2110 int can_add_hw = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002111
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002112 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
2113 /* Ignore events in OFF or ERROR state */
2114 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002115 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002116 /*
2117 * Listen to the 'cpu' scheduling filter constraint
2118 * of events:
2119 */
Stephane Eranian5632ab12011-01-03 18:20:01 +02002120 if (!event_filter_match(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002121 continue;
2122
Stephane Eraniane5d13672011-02-14 11:20:01 +02002123 /* may need to reset tstamp_enabled */
2124 if (is_cgroup_event(event))
2125 perf_cgroup_mark_enabled(event, ctx);
2126
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002127 if (group_can_go_on(event, cpuctx, can_add_hw)) {
Peter Zijlstra6e377382010-02-11 13:21:58 +01002128 if (group_sched_in(event, cpuctx, ctx))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002129 can_add_hw = 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002130 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002131 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002132}
2133
2134static void
2135ctx_sched_in(struct perf_event_context *ctx,
2136 struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +02002137 enum event_type_t event_type,
2138 struct task_struct *task)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002139{
Stephane Eraniane5d13672011-02-14 11:20:01 +02002140 u64 now;
Peter Zijlstradb24d332011-04-09 21:17:45 +02002141 int is_active = ctx->is_active;
Stephane Eraniane5d13672011-02-14 11:20:01 +02002142
Peter Zijlstradb24d332011-04-09 21:17:45 +02002143 ctx->is_active |= event_type;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002144 if (likely(!ctx->nr_events))
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002145 return;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002146
Stephane Eraniane5d13672011-02-14 11:20:01 +02002147 now = perf_clock();
2148 ctx->timestamp = now;
Stephane Eranian3f7cce32011-02-18 14:40:01 +02002149 perf_cgroup_set_timestamp(task, ctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002150 /*
2151 * First go through the list and put on any pinned groups
2152 * in order to give them the best chance of going on.
2153 */
Peter Zijlstradb24d332011-04-09 21:17:45 +02002154 if (!(is_active & EVENT_PINNED) && (event_type & EVENT_PINNED))
Peter Zijlstra6e377382010-02-11 13:21:58 +01002155 ctx_pinned_sched_in(ctx, cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002156
2157 /* Then walk through the lower prio flexible groups */
Peter Zijlstradb24d332011-04-09 21:17:45 +02002158 if (!(is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE))
Peter Zijlstra6e377382010-02-11 13:21:58 +01002159 ctx_flexible_sched_in(ctx, cpuctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002160}
2161
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002162static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +02002163 enum event_type_t event_type,
2164 struct task_struct *task)
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002165{
2166 struct perf_event_context *ctx = &cpuctx->ctx;
2167
Stephane Eraniane5d13672011-02-14 11:20:01 +02002168 ctx_sched_in(ctx, cpuctx, event_type, task);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002169}
2170
Stephane Eraniane5d13672011-02-14 11:20:01 +02002171static void perf_event_context_sched_in(struct perf_event_context *ctx,
2172 struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002173{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002174 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002175
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002176 cpuctx = __get_cpu_context(ctx);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002177 if (cpuctx->task_ctx == ctx)
2178 return;
2179
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002180 perf_ctx_lock(cpuctx, ctx);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02002181 perf_pmu_disable(ctx->pmu);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002182 /*
2183 * We want to keep the following priority order:
2184 * cpu pinned (that don't need to move), task pinned,
2185 * cpu flexible, task flexible.
2186 */
2187 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
2188
Gleb Natapov1d5f0032011-10-23 19:10:33 +02002189 if (ctx->nr_events)
2190 cpuctx->task_ctx = ctx;
eranian@google.com9b33fa62010-03-10 22:26:05 -08002191
Gleb Natapov86b47c22011-11-22 16:08:21 +02002192 perf_event_sched_in(cpuctx, cpuctx->task_ctx, task);
2193
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002194 perf_pmu_enable(ctx->pmu);
2195 perf_ctx_unlock(cpuctx, ctx);
2196
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002197 /*
2198 * Since these rotations are per-cpu, we need to ensure the
2199 * cpu-context we got scheduled on is actually rotating.
2200 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002201 perf_pmu_rotate_start(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002202}
2203
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002204/*
2205 * Called from scheduler to add the events of the current task
2206 * with interrupts disabled.
2207 *
2208 * We restore the event value and then enable it.
2209 *
2210 * This does not protect us against NMI, but enable()
2211 * sets the enabled bit in the control field of event _before_
2212 * accessing the event control register. If a NMI hits, then it will
2213 * keep the event running.
2214 */
Stephane Eraniana8d757e2011-08-25 15:58:03 +02002215void __perf_event_task_sched_in(struct task_struct *prev,
2216 struct task_struct *task)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002217{
2218 struct perf_event_context *ctx;
2219 int ctxn;
2220
2221 for_each_task_context_nr(ctxn) {
2222 ctx = task->perf_event_ctxp[ctxn];
2223 if (likely(!ctx))
2224 continue;
2225
Stephane Eraniane5d13672011-02-14 11:20:01 +02002226 perf_event_context_sched_in(ctx, task);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002227 }
Stephane Eraniane5d13672011-02-14 11:20:01 +02002228 /*
2229 * if cgroup events exist on this CPU, then we need
2230 * to check if we have to switch in PMU state.
2231 * cgroup event are system-wide mode only
2232 */
2233 if (atomic_read(&__get_cpu_var(perf_cgroup_events)))
Stephane Eraniana8d757e2011-08-25 15:58:03 +02002234 perf_cgroup_sched_in(prev, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002235}
2236
Peter Zijlstraabd50712010-01-26 18:50:16 +01002237static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
2238{
2239 u64 frequency = event->attr.sample_freq;
2240 u64 sec = NSEC_PER_SEC;
2241 u64 divisor, dividend;
2242
2243 int count_fls, nsec_fls, frequency_fls, sec_fls;
2244
2245 count_fls = fls64(count);
2246 nsec_fls = fls64(nsec);
2247 frequency_fls = fls64(frequency);
2248 sec_fls = 30;
2249
2250 /*
2251 * We got @count in @nsec, with a target of sample_freq HZ
2252 * the target period becomes:
2253 *
2254 * @count * 10^9
2255 * period = -------------------
2256 * @nsec * sample_freq
2257 *
2258 */
2259
2260 /*
2261 * Reduce accuracy by one bit such that @a and @b converge
2262 * to a similar magnitude.
2263 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002264#define REDUCE_FLS(a, b) \
Peter Zijlstraabd50712010-01-26 18:50:16 +01002265do { \
2266 if (a##_fls > b##_fls) { \
2267 a >>= 1; \
2268 a##_fls--; \
2269 } else { \
2270 b >>= 1; \
2271 b##_fls--; \
2272 } \
2273} while (0)
2274
2275 /*
2276 * Reduce accuracy until either term fits in a u64, then proceed with
2277 * the other, so that finally we can do a u64/u64 division.
2278 */
2279 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
2280 REDUCE_FLS(nsec, frequency);
2281 REDUCE_FLS(sec, count);
2282 }
2283
2284 if (count_fls + sec_fls > 64) {
2285 divisor = nsec * frequency;
2286
2287 while (count_fls + sec_fls > 64) {
2288 REDUCE_FLS(count, sec);
2289 divisor >>= 1;
2290 }
2291
2292 dividend = count * sec;
2293 } else {
2294 dividend = count * sec;
2295
2296 while (nsec_fls + frequency_fls > 64) {
2297 REDUCE_FLS(nsec, frequency);
2298 dividend >>= 1;
2299 }
2300
2301 divisor = nsec * frequency;
2302 }
2303
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02002304 if (!divisor)
2305 return dividend;
2306
Peter Zijlstraabd50712010-01-26 18:50:16 +01002307 return div64_u64(dividend, divisor);
2308}
2309
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002310static DEFINE_PER_CPU(int, perf_throttled_count);
2311static DEFINE_PER_CPU(u64, perf_throttled_seq);
2312
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002313static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count, bool disable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002314{
2315 struct hw_perf_event *hwc = &event->hw;
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02002316 s64 period, sample_period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002317 s64 delta;
2318
Peter Zijlstraabd50712010-01-26 18:50:16 +01002319 period = perf_calculate_period(event, nsec, count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002320
2321 delta = (s64)(period - hwc->sample_period);
2322 delta = (delta + 7) / 8; /* low pass filter */
2323
2324 sample_period = hwc->sample_period + delta;
2325
2326 if (!sample_period)
2327 sample_period = 1;
2328
2329 hwc->sample_period = sample_period;
Peter Zijlstraabd50712010-01-26 18:50:16 +01002330
Peter Zijlstrae7850592010-05-21 14:43:08 +02002331 if (local64_read(&hwc->period_left) > 8*sample_period) {
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002332 if (disable)
2333 event->pmu->stop(event, PERF_EF_UPDATE);
2334
Peter Zijlstrae7850592010-05-21 14:43:08 +02002335 local64_set(&hwc->period_left, 0);
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002336
2337 if (disable)
2338 event->pmu->start(event, PERF_EF_RELOAD);
Peter Zijlstraabd50712010-01-26 18:50:16 +01002339 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002340}
2341
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002342/*
2343 * combine freq adjustment with unthrottling to avoid two passes over the
2344 * events. At the same time, make sure, having freq events does not change
2345 * the rate of unthrottling as that would introduce bias.
2346 */
2347static void perf_adjust_freq_unthr_context(struct perf_event_context *ctx,
2348 int needs_unthr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002349{
2350 struct perf_event *event;
2351 struct hw_perf_event *hwc;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002352 u64 now, period = TICK_NSEC;
Peter Zijlstraabd50712010-01-26 18:50:16 +01002353 s64 delta;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002354
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002355 /*
2356 * only need to iterate over all events iff:
2357 * - context have events in frequency mode (needs freq adjust)
2358 * - there are events to unthrottle on this cpu
2359 */
2360 if (!(ctx->nr_freq || needs_unthr))
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01002361 return;
2362
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002363 raw_spin_lock(&ctx->lock);
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002364 perf_pmu_disable(ctx->pmu);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002365
Paul Mackerras03541f82009-10-14 16:58:03 +11002366 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002367 if (event->state != PERF_EVENT_STATE_ACTIVE)
2368 continue;
2369
Stephane Eranian5632ab12011-01-03 18:20:01 +02002370 if (!event_filter_match(event))
Peter Zijlstra5d27c232009-12-17 13:16:32 +01002371 continue;
2372
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002373 hwc = &event->hw;
2374
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002375 if (needs_unthr && hwc->interrupts == MAX_INTERRUPTS) {
2376 hwc->interrupts = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002377 perf_log_throttle(event, 1);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02002378 event->pmu->start(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002379 }
2380
2381 if (!event->attr.freq || !event->attr.sample_freq)
2382 continue;
2383
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002384 /*
2385 * stop the event and update event->count
2386 */
2387 event->pmu->stop(event, PERF_EF_UPDATE);
2388
Peter Zijlstrae7850592010-05-21 14:43:08 +02002389 now = local64_read(&event->count);
Peter Zijlstraabd50712010-01-26 18:50:16 +01002390 delta = now - hwc->freq_count_stamp;
2391 hwc->freq_count_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002392
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002393 /*
2394 * restart the event
2395 * reload only if value has changed
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002396 * we have stopped the event so tell that
2397 * to perf_adjust_period() to avoid stopping it
2398 * twice.
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002399 */
Peter Zijlstraabd50712010-01-26 18:50:16 +01002400 if (delta > 0)
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002401 perf_adjust_period(event, period, delta, false);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002402
2403 event->pmu->start(event, delta > 0 ? PERF_EF_RELOAD : 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002404 }
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002405
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002406 perf_pmu_enable(ctx->pmu);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002407 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002408}
2409
2410/*
2411 * Round-robin a context's events:
2412 */
2413static void rotate_ctx(struct perf_event_context *ctx)
2414{
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01002415 /*
2416 * Rotate the first entry last of non-pinned groups. Rotation might be
2417 * disabled by the inheritance code.
2418 */
2419 if (!ctx->rotate_disable)
2420 list_rotate_left(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002421}
2422
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002423/*
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002424 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
2425 * because they're strictly cpu affine and rotate_start is called with IRQs
2426 * disabled, while rotate_context is called from IRQ context.
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002427 */
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002428static void perf_rotate_context(struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002429{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002430 struct perf_event_context *ctx = NULL;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002431 int rotate = 0, remove = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002432
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002433 if (cpuctx->ctx.nr_events) {
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002434 remove = 0;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002435 if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
2436 rotate = 1;
2437 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002438
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002439 ctx = cpuctx->task_ctx;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002440 if (ctx && ctx->nr_events) {
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002441 remove = 0;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002442 if (ctx->nr_events != ctx->nr_active)
2443 rotate = 1;
2444 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002445
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002446 if (!rotate)
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01002447 goto done;
2448
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002449 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02002450 perf_pmu_disable(cpuctx->ctx.pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002451
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002452 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
2453 if (ctx)
2454 ctx_sched_out(ctx, cpuctx, EVENT_FLEXIBLE);
Peter Zijlstrad4944a02010-03-08 13:51:20 +01002455
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002456 rotate_ctx(&cpuctx->ctx);
2457 if (ctx)
2458 rotate_ctx(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002459
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002460 perf_event_sched_in(cpuctx, ctx, current);
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01002461
2462 perf_pmu_enable(cpuctx->ctx.pmu);
2463 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002464done:
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002465 if (remove)
2466 list_del_init(&cpuctx->rotation_list);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002467}
2468
2469void perf_event_task_tick(void)
2470{
2471 struct list_head *head = &__get_cpu_var(rotation_list);
2472 struct perf_cpu_context *cpuctx, *tmp;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002473 struct perf_event_context *ctx;
2474 int throttled;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002475
2476 WARN_ON(!irqs_disabled());
2477
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002478 __this_cpu_inc(perf_throttled_seq);
2479 throttled = __this_cpu_xchg(perf_throttled_count, 0);
2480
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002481 list_for_each_entry_safe(cpuctx, tmp, head, rotation_list) {
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002482 ctx = &cpuctx->ctx;
2483 perf_adjust_freq_unthr_context(ctx, throttled);
2484
2485 ctx = cpuctx->task_ctx;
2486 if (ctx)
2487 perf_adjust_freq_unthr_context(ctx, throttled);
2488
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002489 if (cpuctx->jiffies_interval == 1 ||
2490 !(jiffies % cpuctx->jiffies_interval))
2491 perf_rotate_context(cpuctx);
2492 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002493}
2494
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002495static int event_enable_on_exec(struct perf_event *event,
2496 struct perf_event_context *ctx)
2497{
2498 if (!event->attr.enable_on_exec)
2499 return 0;
2500
2501 event->attr.enable_on_exec = 0;
2502 if (event->state >= PERF_EVENT_STATE_INACTIVE)
2503 return 0;
2504
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01002505 __perf_event_mark_enabled(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002506
2507 return 1;
2508}
2509
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002510/*
2511 * Enable all of a task's events that have been marked enable-on-exec.
2512 * This expects task == current.
2513 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002514static void perf_event_enable_on_exec(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002515{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002516 struct perf_event *event;
2517 unsigned long flags;
2518 int enabled = 0;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002519 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002520
2521 local_irq_save(flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002522 if (!ctx || !ctx->nr_events)
2523 goto out;
2524
Stephane Eraniane566b762011-04-06 02:54:54 +02002525 /*
2526 * We must ctxsw out cgroup events to avoid conflict
2527 * when invoking perf_task_event_sched_in() later on
2528 * in this function. Otherwise we end up trying to
2529 * ctxswin cgroup events which are already scheduled
2530 * in.
2531 */
Stephane Eraniana8d757e2011-08-25 15:58:03 +02002532 perf_cgroup_sched_out(current, NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002533
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002534 raw_spin_lock(&ctx->lock);
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02002535 task_ctx_sched_out(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002536
Peter Zijlstrab79387e2011-11-22 11:25:43 +01002537 list_for_each_entry(event, &ctx->event_list, event_entry) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002538 ret = event_enable_on_exec(event, ctx);
2539 if (ret)
2540 enabled = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002541 }
2542
2543 /*
2544 * Unclone this context if we enabled any event.
2545 */
2546 if (enabled)
2547 unclone_ctx(ctx);
2548
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002549 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002550
Stephane Eraniane566b762011-04-06 02:54:54 +02002551 /*
2552 * Also calls ctxswin for cgroup events, if any:
2553 */
Stephane Eraniane5d13672011-02-14 11:20:01 +02002554 perf_event_context_sched_in(ctx, ctx->task);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002555out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002556 local_irq_restore(flags);
2557}
2558
2559/*
2560 * Cross CPU call to read the hardware event
2561 */
2562static void __perf_event_read(void *info)
2563{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002564 struct perf_event *event = info;
2565 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002566 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002567
2568 /*
2569 * If this is a task context, we need to check whether it is
2570 * the current task context of this cpu. If not it has been
2571 * scheduled out before the smp call arrived. In that case
2572 * event->count would have been updated to a recent sample
2573 * when the event was scheduled out.
2574 */
2575 if (ctx->task && cpuctx->task_ctx != ctx)
2576 return;
2577
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002578 raw_spin_lock(&ctx->lock);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002579 if (ctx->is_active) {
Peter Zijlstra542e72f2011-01-26 15:38:35 +01002580 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002581 update_cgrp_time_from_event(event);
2582 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002583 update_event_times(event);
Peter Zijlstra542e72f2011-01-26 15:38:35 +01002584 if (event->state == PERF_EVENT_STATE_ACTIVE)
2585 event->pmu->read(event);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002586 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002587}
2588
Peter Zijlstrab5e58792010-05-21 14:43:12 +02002589static inline u64 perf_event_count(struct perf_event *event)
2590{
Peter Zijlstrae7850592010-05-21 14:43:08 +02002591 return local64_read(&event->count) + atomic64_read(&event->child_count);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02002592}
2593
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002594static u64 perf_event_read(struct perf_event *event)
2595{
2596 /*
2597 * If event is enabled and currently active on a CPU, update the
2598 * value in the event structure:
2599 */
2600 if (event->state == PERF_EVENT_STATE_ACTIVE) {
2601 smp_call_function_single(event->oncpu,
2602 __perf_event_read, event, 1);
2603 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01002604 struct perf_event_context *ctx = event->ctx;
2605 unsigned long flags;
2606
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002607 raw_spin_lock_irqsave(&ctx->lock, flags);
Stephane Eranianc530ccd2010-10-15 15:26:01 +02002608 /*
2609 * may read while context is not active
2610 * (e.g., thread is blocked), in that case
2611 * we cannot update context time
2612 */
Stephane Eraniane5d13672011-02-14 11:20:01 +02002613 if (ctx->is_active) {
Stephane Eranianc530ccd2010-10-15 15:26:01 +02002614 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002615 update_cgrp_time_from_event(event);
2616 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002617 update_event_times(event);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002618 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002619 }
2620
Peter Zijlstrab5e58792010-05-21 14:43:12 +02002621 return perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002622}
2623
2624/*
2625 * Initialize the perf_event context in a task_struct:
2626 */
Peter Zijlstraeb184472010-09-07 15:55:13 +02002627static void __perf_event_init_context(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002628{
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002629 raw_spin_lock_init(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002630 mutex_init(&ctx->mutex);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002631 INIT_LIST_HEAD(&ctx->pinned_groups);
2632 INIT_LIST_HEAD(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002633 INIT_LIST_HEAD(&ctx->event_list);
2634 atomic_set(&ctx->refcount, 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002635}
2636
Peter Zijlstraeb184472010-09-07 15:55:13 +02002637static struct perf_event_context *
2638alloc_perf_context(struct pmu *pmu, struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002639{
2640 struct perf_event_context *ctx;
Peter Zijlstraeb184472010-09-07 15:55:13 +02002641
2642 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
2643 if (!ctx)
2644 return NULL;
2645
2646 __perf_event_init_context(ctx);
2647 if (task) {
2648 ctx->task = task;
2649 get_task_struct(task);
2650 }
2651 ctx->pmu = pmu;
2652
2653 return ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002654}
2655
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07002656static struct task_struct *
2657find_lively_task_by_vpid(pid_t vpid)
2658{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002659 struct task_struct *task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002660 int err;
2661
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002662 rcu_read_lock();
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07002663 if (!vpid)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002664 task = current;
2665 else
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07002666 task = find_task_by_vpid(vpid);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002667 if (task)
2668 get_task_struct(task);
2669 rcu_read_unlock();
2670
2671 if (!task)
2672 return ERR_PTR(-ESRCH);
2673
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002674 /* Reuse ptrace permission checks for now. */
2675 err = -EACCES;
2676 if (!ptrace_may_access(task, PTRACE_MODE_READ))
2677 goto errout;
2678
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07002679 return task;
2680errout:
2681 put_task_struct(task);
2682 return ERR_PTR(err);
2683
2684}
2685
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002686/*
2687 * Returns a matching context with refcount and pincount.
2688 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002689static struct perf_event_context *
Matt Helsley38a81da2010-09-13 13:01:20 -07002690find_get_context(struct pmu *pmu, struct task_struct *task, int cpu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002691{
2692 struct perf_event_context *ctx;
2693 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002694 unsigned long flags;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002695 int ctxn, err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002696
Oleg Nesterov22a4ec72011-01-18 17:10:08 +01002697 if (!task) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002698 /* Must be root to operate on a CPU event: */
2699 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
2700 return ERR_PTR(-EACCES);
2701
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002702 /*
2703 * We could be clever and allow to attach a event to an
2704 * offline CPU and activate it when the CPU comes up, but
2705 * that's for later.
2706 */
2707 if (!cpu_online(cpu))
2708 return ERR_PTR(-ENODEV);
2709
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002710 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002711 ctx = &cpuctx->ctx;
2712 get_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002713 ++ctx->pin_count;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002714
2715 return ctx;
2716 }
2717
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002718 err = -EINVAL;
2719 ctxn = pmu->task_ctx_nr;
2720 if (ctxn < 0)
2721 goto errout;
2722
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002723retry:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002724 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002725 if (ctx) {
2726 unclone_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002727 ++ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002728 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Peter Zijlstra9137fb22011-04-09 21:17:41 +02002729 } else {
Peter Zijlstraeb184472010-09-07 15:55:13 +02002730 ctx = alloc_perf_context(pmu, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002731 err = -ENOMEM;
2732 if (!ctx)
2733 goto errout;
Peter Zijlstraeb184472010-09-07 15:55:13 +02002734
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01002735 err = 0;
2736 mutex_lock(&task->perf_event_mutex);
2737 /*
2738 * If it has already passed perf_event_exit_task().
2739 * we must see PF_EXITING, it takes this mutex too.
2740 */
2741 if (task->flags & PF_EXITING)
2742 err = -ESRCH;
2743 else if (task->perf_event_ctxp[ctxn])
2744 err = -EAGAIN;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002745 else {
Peter Zijlstra9137fb22011-04-09 21:17:41 +02002746 get_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002747 ++ctx->pin_count;
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01002748 rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002749 }
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01002750 mutex_unlock(&task->perf_event_mutex);
2751
2752 if (unlikely(err)) {
Peter Zijlstra9137fb22011-04-09 21:17:41 +02002753 put_ctx(ctx);
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01002754
2755 if (err == -EAGAIN)
2756 goto retry;
2757 goto errout;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002758 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002759 }
2760
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002761 return ctx;
2762
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002763errout:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002764 return ERR_PTR(err);
2765}
2766
Li Zefan6fb29152009-10-15 11:21:42 +08002767static void perf_event_free_filter(struct perf_event *event);
2768
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002769static void free_event_rcu(struct rcu_head *head)
2770{
2771 struct perf_event *event;
2772
2773 event = container_of(head, struct perf_event, rcu_head);
2774 if (event->ns)
2775 put_pid_ns(event->ns);
Li Zefan6fb29152009-10-15 11:21:42 +08002776 perf_event_free_filter(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002777 kfree(event);
2778}
2779
Frederic Weisbecker76369132011-05-19 19:55:04 +02002780static void ring_buffer_put(struct ring_buffer *rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002781
2782static void free_event(struct perf_event *event)
2783{
Peter Zijlstrae360adb2010-10-14 14:01:34 +08002784 irq_work_sync(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002785
2786 if (!event->parent) {
Peter Zijlstra82cd6de2010-10-14 17:57:23 +02002787 if (event->attach_state & PERF_ATTACH_TASK)
Ingo Molnarc5905af2012-02-24 08:31:31 +01002788 static_key_slow_dec_deferred(&perf_sched_events);
Eric B Munson3af9e852010-05-18 15:30:49 +01002789 if (event->attr.mmap || event->attr.mmap_data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002790 atomic_dec(&nr_mmap_events);
2791 if (event->attr.comm)
2792 atomic_dec(&nr_comm_events);
2793 if (event->attr.task)
2794 atomic_dec(&nr_task_events);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02002795 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
2796 put_callchain_buffers();
Peter Zijlstra08309372011-03-03 11:31:20 +01002797 if (is_cgroup_event(event)) {
2798 atomic_dec(&per_cpu(perf_cgroup_events, event->cpu));
Ingo Molnarc5905af2012-02-24 08:31:31 +01002799 static_key_slow_dec_deferred(&perf_sched_events);
Peter Zijlstra08309372011-03-03 11:31:20 +01002800 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002801 }
2802
Frederic Weisbecker76369132011-05-19 19:55:04 +02002803 if (event->rb) {
2804 ring_buffer_put(event->rb);
2805 event->rb = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002806 }
2807
Stephane Eraniane5d13672011-02-14 11:20:01 +02002808 if (is_cgroup_event(event))
2809 perf_detach_cgroup(event);
2810
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002811 if (event->destroy)
2812 event->destroy(event);
2813
Peter Zijlstra0c67b402010-09-13 11:15:58 +02002814 if (event->ctx)
2815 put_ctx(event->ctx);
2816
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002817 call_rcu(&event->rcu_head, free_event_rcu);
2818}
2819
Arjan van de Venfb0459d2009-09-25 12:25:56 +02002820int perf_event_release_kernel(struct perf_event *event)
2821{
2822 struct perf_event_context *ctx = event->ctx;
2823
2824 WARN_ON_ONCE(ctx->parent_ctx);
Peter Zijlstraa0507c82010-05-06 15:42:53 +02002825 /*
2826 * There are two ways this annotation is useful:
2827 *
2828 * 1) there is a lock recursion from perf_event_exit_task
2829 * see the comment there.
2830 *
2831 * 2) there is a lock-inversion with mmap_sem through
2832 * perf_event_read_group(), which takes faults while
2833 * holding ctx->mutex, however this is called after
2834 * the last filedesc died, so there is no possibility
2835 * to trigger the AB-BA case.
2836 */
2837 mutex_lock_nested(&ctx->mutex, SINGLE_DEPTH_NESTING);
Peter Zijlstra050735b2010-05-11 11:51:53 +02002838 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra8a495422010-05-27 15:47:49 +02002839 perf_group_detach(event);
Peter Zijlstra050735b2010-05-11 11:51:53 +02002840 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstrae03a9a52011-04-09 21:17:47 +02002841 perf_remove_from_context(event);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02002842 mutex_unlock(&ctx->mutex);
2843
Arjan van de Venfb0459d2009-09-25 12:25:56 +02002844 free_event(event);
2845
2846 return 0;
2847}
2848EXPORT_SYMBOL_GPL(perf_event_release_kernel);
2849
Peter Zijlstraa66a3052009-11-23 11:37:23 +01002850/*
2851 * Called when the last reference to the file is gone.
2852 */
2853static int perf_release(struct inode *inode, struct file *file)
2854{
2855 struct perf_event *event = file->private_data;
Peter Zijlstra88821352010-11-09 19:01:43 +01002856 struct task_struct *owner;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01002857
2858 file->private_data = NULL;
2859
Peter Zijlstra88821352010-11-09 19:01:43 +01002860 rcu_read_lock();
2861 owner = ACCESS_ONCE(event->owner);
2862 /*
2863 * Matches the smp_wmb() in perf_event_exit_task(). If we observe
2864 * !owner it means the list deletion is complete and we can indeed
2865 * free this event, otherwise we need to serialize on
2866 * owner->perf_event_mutex.
2867 */
2868 smp_read_barrier_depends();
2869 if (owner) {
2870 /*
2871 * Since delayed_put_task_struct() also drops the last
2872 * task reference we can safely take a new reference
2873 * while holding the rcu_read_lock().
2874 */
2875 get_task_struct(owner);
2876 }
2877 rcu_read_unlock();
2878
2879 if (owner) {
2880 mutex_lock(&owner->perf_event_mutex);
2881 /*
2882 * We have to re-check the event->owner field, if it is cleared
2883 * we raced with perf_event_exit_task(), acquiring the mutex
2884 * ensured they're done, and we can proceed with freeing the
2885 * event.
2886 */
2887 if (event->owner)
2888 list_del_init(&event->owner_entry);
2889 mutex_unlock(&owner->perf_event_mutex);
2890 put_task_struct(owner);
2891 }
2892
Peter Zijlstraa66a3052009-11-23 11:37:23 +01002893 return perf_event_release_kernel(event);
2894}
2895
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002896u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002897{
2898 struct perf_event *child;
2899 u64 total = 0;
2900
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002901 *enabled = 0;
2902 *running = 0;
2903
Peter Zijlstra6f105812009-11-20 22:19:56 +01002904 mutex_lock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002905 total += perf_event_read(event);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002906 *enabled += event->total_time_enabled +
2907 atomic64_read(&event->child_total_time_enabled);
2908 *running += event->total_time_running +
2909 atomic64_read(&event->child_total_time_running);
2910
2911 list_for_each_entry(child, &event->child_list, child_list) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002912 total += perf_event_read(child);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002913 *enabled += child->total_time_enabled;
2914 *running += child->total_time_running;
2915 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01002916 mutex_unlock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002917
2918 return total;
2919}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02002920EXPORT_SYMBOL_GPL(perf_event_read_value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002921
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002922static int perf_event_read_group(struct perf_event *event,
2923 u64 read_format, char __user *buf)
2924{
2925 struct perf_event *leader = event->group_leader, *sub;
Peter Zijlstra6f105812009-11-20 22:19:56 +01002926 int n = 0, size = 0, ret = -EFAULT;
2927 struct perf_event_context *ctx = leader->ctx;
Peter Zijlstraabf48682009-11-20 22:19:49 +01002928 u64 values[5];
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002929 u64 count, enabled, running;
Peter Zijlstraabf48682009-11-20 22:19:49 +01002930
Peter Zijlstra6f105812009-11-20 22:19:56 +01002931 mutex_lock(&ctx->mutex);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002932 count = perf_event_read_value(leader, &enabled, &running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002933
2934 values[n++] = 1 + leader->nr_siblings;
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002935 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2936 values[n++] = enabled;
2937 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2938 values[n++] = running;
Peter Zijlstraabf48682009-11-20 22:19:49 +01002939 values[n++] = count;
2940 if (read_format & PERF_FORMAT_ID)
2941 values[n++] = primary_event_id(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002942
2943 size = n * sizeof(u64);
2944
2945 if (copy_to_user(buf, values, size))
Peter Zijlstra6f105812009-11-20 22:19:56 +01002946 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002947
Peter Zijlstra6f105812009-11-20 22:19:56 +01002948 ret = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002949
2950 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
Peter Zijlstraabf48682009-11-20 22:19:49 +01002951 n = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002952
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002953 values[n++] = perf_event_read_value(sub, &enabled, &running);
Peter Zijlstraabf48682009-11-20 22:19:49 +01002954 if (read_format & PERF_FORMAT_ID)
2955 values[n++] = primary_event_id(sub);
2956
2957 size = n * sizeof(u64);
2958
Stephane Eranian184d3da2009-11-23 21:40:49 -08002959 if (copy_to_user(buf + ret, values, size)) {
Peter Zijlstra6f105812009-11-20 22:19:56 +01002960 ret = -EFAULT;
2961 goto unlock;
2962 }
Peter Zijlstraabf48682009-11-20 22:19:49 +01002963
2964 ret += size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002965 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01002966unlock:
2967 mutex_unlock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002968
Peter Zijlstraabf48682009-11-20 22:19:49 +01002969 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002970}
2971
2972static int perf_event_read_one(struct perf_event *event,
2973 u64 read_format, char __user *buf)
2974{
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002975 u64 enabled, running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002976 u64 values[4];
2977 int n = 0;
2978
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002979 values[n++] = perf_event_read_value(event, &enabled, &running);
2980 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2981 values[n++] = enabled;
2982 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2983 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002984 if (read_format & PERF_FORMAT_ID)
2985 values[n++] = primary_event_id(event);
2986
2987 if (copy_to_user(buf, values, n * sizeof(u64)))
2988 return -EFAULT;
2989
2990 return n * sizeof(u64);
2991}
2992
2993/*
2994 * Read the performance event - simple non blocking version for now
2995 */
2996static ssize_t
2997perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
2998{
2999 u64 read_format = event->attr.read_format;
3000 int ret;
3001
3002 /*
3003 * Return end-of-file for a read on a event that is in
3004 * error state (i.e. because it was pinned but it couldn't be
3005 * scheduled on to the CPU at some point).
3006 */
3007 if (event->state == PERF_EVENT_STATE_ERROR)
3008 return 0;
3009
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02003010 if (count < event->read_size)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003011 return -ENOSPC;
3012
3013 WARN_ON_ONCE(event->ctx->parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003014 if (read_format & PERF_FORMAT_GROUP)
3015 ret = perf_event_read_group(event, read_format, buf);
3016 else
3017 ret = perf_event_read_one(event, read_format, buf);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003018
3019 return ret;
3020}
3021
3022static ssize_t
3023perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
3024{
3025 struct perf_event *event = file->private_data;
3026
3027 return perf_read_hw(event, buf, count);
3028}
3029
3030static unsigned int perf_poll(struct file *file, poll_table *wait)
3031{
3032 struct perf_event *event = file->private_data;
Frederic Weisbecker76369132011-05-19 19:55:04 +02003033 struct ring_buffer *rb;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003034 unsigned int events = POLL_HUP;
3035
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003036 /*
3037 * Race between perf_event_set_output() and perf_poll(): perf_poll()
3038 * grabs the rb reference but perf_event_set_output() overrides it.
3039 * Here is the timeline for two threads T1, T2:
3040 * t0: T1, rb = rcu_dereference(event->rb)
3041 * t1: T2, old_rb = event->rb
3042 * t2: T2, event->rb = new rb
3043 * t3: T2, ring_buffer_detach(old_rb)
3044 * t4: T1, ring_buffer_attach(rb1)
3045 * t5: T1, poll_wait(event->waitq)
3046 *
3047 * To avoid this problem, we grab mmap_mutex in perf_poll()
3048 * thereby ensuring that the assignment of the new ring buffer
3049 * and the detachment of the old buffer appear atomic to perf_poll()
3050 */
3051 mutex_lock(&event->mmap_mutex);
3052
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003053 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02003054 rb = rcu_dereference(event->rb);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003055 if (rb) {
3056 ring_buffer_attach(event, rb);
Frederic Weisbecker76369132011-05-19 19:55:04 +02003057 events = atomic_xchg(&rb->poll, 0);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003058 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003059 rcu_read_unlock();
3060
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003061 mutex_unlock(&event->mmap_mutex);
3062
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003063 poll_wait(file, &event->waitq, wait);
3064
3065 return events;
3066}
3067
3068static void perf_event_reset(struct perf_event *event)
3069{
3070 (void)perf_event_read(event);
Peter Zijlstrae7850592010-05-21 14:43:08 +02003071 local64_set(&event->count, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003072 perf_event_update_userpage(event);
3073}
3074
3075/*
3076 * Holding the top-level event's child_mutex means that any
3077 * descendant process that has inherited this event will block
3078 * in sync_child_event if it goes to exit, thus satisfying the
3079 * task existence requirements of perf_event_enable/disable.
3080 */
3081static void perf_event_for_each_child(struct perf_event *event,
3082 void (*func)(struct perf_event *))
3083{
3084 struct perf_event *child;
3085
3086 WARN_ON_ONCE(event->ctx->parent_ctx);
3087 mutex_lock(&event->child_mutex);
3088 func(event);
3089 list_for_each_entry(child, &event->child_list, child_list)
3090 func(child);
3091 mutex_unlock(&event->child_mutex);
3092}
3093
3094static void perf_event_for_each(struct perf_event *event,
3095 void (*func)(struct perf_event *))
3096{
3097 struct perf_event_context *ctx = event->ctx;
3098 struct perf_event *sibling;
3099
3100 WARN_ON_ONCE(ctx->parent_ctx);
3101 mutex_lock(&ctx->mutex);
3102 event = event->group_leader;
3103
3104 perf_event_for_each_child(event, func);
3105 func(event);
3106 list_for_each_entry(sibling, &event->sibling_list, group_entry)
3107 perf_event_for_each_child(event, func);
3108 mutex_unlock(&ctx->mutex);
3109}
3110
3111static int perf_event_period(struct perf_event *event, u64 __user *arg)
3112{
3113 struct perf_event_context *ctx = event->ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003114 int ret = 0;
3115 u64 value;
3116
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01003117 if (!is_sampling_event(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003118 return -EINVAL;
3119
John Blackwoodad0cf342010-09-28 18:03:11 -04003120 if (copy_from_user(&value, arg, sizeof(value)))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003121 return -EFAULT;
3122
3123 if (!value)
3124 return -EINVAL;
3125
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003126 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003127 if (event->attr.freq) {
3128 if (value > sysctl_perf_event_sample_rate) {
3129 ret = -EINVAL;
3130 goto unlock;
3131 }
3132
3133 event->attr.sample_freq = value;
3134 } else {
3135 event->attr.sample_period = value;
3136 event->hw.sample_period = value;
3137 }
3138unlock:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003139 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003140
3141 return ret;
3142}
3143
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003144static const struct file_operations perf_fops;
3145
3146static struct perf_event *perf_fget_light(int fd, int *fput_needed)
3147{
3148 struct file *file;
3149
3150 file = fget_light(fd, fput_needed);
3151 if (!file)
3152 return ERR_PTR(-EBADF);
3153
3154 if (file->f_op != &perf_fops) {
3155 fput_light(file, *fput_needed);
3156 *fput_needed = 0;
3157 return ERR_PTR(-EBADF);
3158 }
3159
3160 return file->private_data;
3161}
3162
3163static int perf_event_set_output(struct perf_event *event,
3164 struct perf_event *output_event);
Li Zefan6fb29152009-10-15 11:21:42 +08003165static int perf_event_set_filter(struct perf_event *event, void __user *arg);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003166
3167static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3168{
3169 struct perf_event *event = file->private_data;
3170 void (*func)(struct perf_event *);
3171 u32 flags = arg;
3172
3173 switch (cmd) {
3174 case PERF_EVENT_IOC_ENABLE:
3175 func = perf_event_enable;
3176 break;
3177 case PERF_EVENT_IOC_DISABLE:
3178 func = perf_event_disable;
3179 break;
3180 case PERF_EVENT_IOC_RESET:
3181 func = perf_event_reset;
3182 break;
3183
3184 case PERF_EVENT_IOC_REFRESH:
3185 return perf_event_refresh(event, arg);
3186
3187 case PERF_EVENT_IOC_PERIOD:
3188 return perf_event_period(event, (u64 __user *)arg);
3189
3190 case PERF_EVENT_IOC_SET_OUTPUT:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003191 {
3192 struct perf_event *output_event = NULL;
3193 int fput_needed = 0;
3194 int ret;
3195
3196 if (arg != -1) {
3197 output_event = perf_fget_light(arg, &fput_needed);
3198 if (IS_ERR(output_event))
3199 return PTR_ERR(output_event);
3200 }
3201
3202 ret = perf_event_set_output(event, output_event);
3203 if (output_event)
3204 fput_light(output_event->filp, fput_needed);
3205
3206 return ret;
3207 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003208
Li Zefan6fb29152009-10-15 11:21:42 +08003209 case PERF_EVENT_IOC_SET_FILTER:
3210 return perf_event_set_filter(event, (void __user *)arg);
3211
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003212 default:
3213 return -ENOTTY;
3214 }
3215
3216 if (flags & PERF_IOC_FLAG_GROUP)
3217 perf_event_for_each(event, func);
3218 else
3219 perf_event_for_each_child(event, func);
3220
3221 return 0;
3222}
3223
3224int perf_event_task_enable(void)
3225{
3226 struct perf_event *event;
3227
3228 mutex_lock(&current->perf_event_mutex);
3229 list_for_each_entry(event, &current->perf_event_list, owner_entry)
3230 perf_event_for_each_child(event, perf_event_enable);
3231 mutex_unlock(&current->perf_event_mutex);
3232
3233 return 0;
3234}
3235
3236int perf_event_task_disable(void)
3237{
3238 struct perf_event *event;
3239
3240 mutex_lock(&current->perf_event_mutex);
3241 list_for_each_entry(event, &current->perf_event_list, owner_entry)
3242 perf_event_for_each_child(event, perf_event_disable);
3243 mutex_unlock(&current->perf_event_mutex);
3244
3245 return 0;
3246}
3247
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003248static int perf_event_index(struct perf_event *event)
3249{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02003250 if (event->hw.state & PERF_HES_STOPPED)
3251 return 0;
3252
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003253 if (event->state != PERF_EVENT_STATE_ACTIVE)
3254 return 0;
3255
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01003256 return event->pmu->event_idx(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003257}
3258
Eric B Munsonc4794292011-06-23 16:34:38 -04003259static void calc_timer_values(struct perf_event *event,
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003260 u64 *now,
Eric B Munson7f310a52011-06-23 16:34:38 -04003261 u64 *enabled,
3262 u64 *running)
Eric B Munsonc4794292011-06-23 16:34:38 -04003263{
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003264 u64 ctx_time;
Eric B Munsonc4794292011-06-23 16:34:38 -04003265
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003266 *now = perf_clock();
3267 ctx_time = event->shadow_ctx_time + *now;
Eric B Munsonc4794292011-06-23 16:34:38 -04003268 *enabled = ctx_time - event->tstamp_enabled;
3269 *running = ctx_time - event->tstamp_running;
3270}
3271
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003272void __weak perf_update_user_clock(struct perf_event_mmap_page *userpg, u64 now)
3273{
3274}
3275
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003276/*
3277 * Callers need to ensure there can be no nesting of this function, otherwise
3278 * the seqlock logic goes bad. We can not serialize this because the arch
3279 * code calls this from NMI context.
3280 */
3281void perf_event_update_userpage(struct perf_event *event)
3282{
3283 struct perf_event_mmap_page *userpg;
Frederic Weisbecker76369132011-05-19 19:55:04 +02003284 struct ring_buffer *rb;
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003285 u64 enabled, running, now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003286
3287 rcu_read_lock();
Eric B Munson0d641202011-06-24 12:26:26 -04003288 /*
3289 * compute total_time_enabled, total_time_running
3290 * based on snapshot values taken when the event
3291 * was last scheduled in.
3292 *
3293 * we cannot simply called update_context_time()
3294 * because of locking issue as we can be called in
3295 * NMI context
3296 */
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003297 calc_timer_values(event, &now, &enabled, &running);
Frederic Weisbecker76369132011-05-19 19:55:04 +02003298 rb = rcu_dereference(event->rb);
3299 if (!rb)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003300 goto unlock;
3301
Frederic Weisbecker76369132011-05-19 19:55:04 +02003302 userpg = rb->user_page;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003303
3304 /*
3305 * Disable preemption so as to not let the corresponding user-space
3306 * spin too long if we get preempted.
3307 */
3308 preempt_disable();
3309 ++userpg->lock;
3310 barrier();
3311 userpg->index = perf_event_index(event);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003312 userpg->offset = perf_event_count(event);
Peter Zijlstra365a4032011-11-21 20:58:59 +01003313 if (userpg->index)
Peter Zijlstrae7850592010-05-21 14:43:08 +02003314 userpg->offset -= local64_read(&event->hw.prev_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003315
Eric B Munson0d641202011-06-24 12:26:26 -04003316 userpg->time_enabled = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003317 atomic64_read(&event->child_total_time_enabled);
3318
Eric B Munson0d641202011-06-24 12:26:26 -04003319 userpg->time_running = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003320 atomic64_read(&event->child_total_time_running);
3321
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003322 perf_update_user_clock(userpg, now);
3323
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003324 barrier();
3325 ++userpg->lock;
3326 preempt_enable();
3327unlock:
3328 rcu_read_unlock();
3329}
3330
Peter Zijlstra906010b2009-09-21 16:08:49 +02003331static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
3332{
3333 struct perf_event *event = vma->vm_file->private_data;
Frederic Weisbecker76369132011-05-19 19:55:04 +02003334 struct ring_buffer *rb;
Peter Zijlstra906010b2009-09-21 16:08:49 +02003335 int ret = VM_FAULT_SIGBUS;
3336
3337 if (vmf->flags & FAULT_FLAG_MKWRITE) {
3338 if (vmf->pgoff == 0)
3339 ret = 0;
3340 return ret;
3341 }
3342
3343 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02003344 rb = rcu_dereference(event->rb);
3345 if (!rb)
Peter Zijlstra906010b2009-09-21 16:08:49 +02003346 goto unlock;
3347
3348 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
3349 goto unlock;
3350
Frederic Weisbecker76369132011-05-19 19:55:04 +02003351 vmf->page = perf_mmap_to_page(rb, vmf->pgoff);
Peter Zijlstra906010b2009-09-21 16:08:49 +02003352 if (!vmf->page)
3353 goto unlock;
3354
3355 get_page(vmf->page);
3356 vmf->page->mapping = vma->vm_file->f_mapping;
3357 vmf->page->index = vmf->pgoff;
3358
3359 ret = 0;
3360unlock:
3361 rcu_read_unlock();
3362
3363 return ret;
3364}
3365
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003366static void ring_buffer_attach(struct perf_event *event,
3367 struct ring_buffer *rb)
3368{
3369 unsigned long flags;
3370
3371 if (!list_empty(&event->rb_entry))
3372 return;
3373
3374 spin_lock_irqsave(&rb->event_lock, flags);
3375 if (!list_empty(&event->rb_entry))
3376 goto unlock;
3377
3378 list_add(&event->rb_entry, &rb->event_list);
3379unlock:
3380 spin_unlock_irqrestore(&rb->event_lock, flags);
3381}
3382
3383static void ring_buffer_detach(struct perf_event *event,
3384 struct ring_buffer *rb)
3385{
3386 unsigned long flags;
3387
3388 if (list_empty(&event->rb_entry))
3389 return;
3390
3391 spin_lock_irqsave(&rb->event_lock, flags);
3392 list_del_init(&event->rb_entry);
3393 wake_up_all(&event->waitq);
3394 spin_unlock_irqrestore(&rb->event_lock, flags);
3395}
3396
3397static void ring_buffer_wakeup(struct perf_event *event)
3398{
3399 struct ring_buffer *rb;
3400
3401 rcu_read_lock();
3402 rb = rcu_dereference(event->rb);
Will Deacon44b7f4b2011-12-13 20:40:45 +01003403 if (!rb)
3404 goto unlock;
3405
3406 list_for_each_entry_rcu(event, &rb->event_list, rb_entry)
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003407 wake_up_all(&event->waitq);
Will Deacon44b7f4b2011-12-13 20:40:45 +01003408
3409unlock:
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003410 rcu_read_unlock();
3411}
3412
Frederic Weisbecker76369132011-05-19 19:55:04 +02003413static void rb_free_rcu(struct rcu_head *rcu_head)
Peter Zijlstra906010b2009-09-21 16:08:49 +02003414{
Frederic Weisbecker76369132011-05-19 19:55:04 +02003415 struct ring_buffer *rb;
Peter Zijlstra906010b2009-09-21 16:08:49 +02003416
Frederic Weisbecker76369132011-05-19 19:55:04 +02003417 rb = container_of(rcu_head, struct ring_buffer, rcu_head);
3418 rb_free(rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003419}
3420
Frederic Weisbecker76369132011-05-19 19:55:04 +02003421static struct ring_buffer *ring_buffer_get(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003422{
Frederic Weisbecker76369132011-05-19 19:55:04 +02003423 struct ring_buffer *rb;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003424
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003425 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02003426 rb = rcu_dereference(event->rb);
3427 if (rb) {
3428 if (!atomic_inc_not_zero(&rb->refcount))
3429 rb = NULL;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003430 }
3431 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003432
Frederic Weisbecker76369132011-05-19 19:55:04 +02003433 return rb;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003434}
3435
Frederic Weisbecker76369132011-05-19 19:55:04 +02003436static void ring_buffer_put(struct ring_buffer *rb)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003437{
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003438 struct perf_event *event, *n;
3439 unsigned long flags;
3440
Frederic Weisbecker76369132011-05-19 19:55:04 +02003441 if (!atomic_dec_and_test(&rb->refcount))
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003442 return;
3443
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003444 spin_lock_irqsave(&rb->event_lock, flags);
3445 list_for_each_entry_safe(event, n, &rb->event_list, rb_entry) {
3446 list_del_init(&event->rb_entry);
3447 wake_up_all(&event->waitq);
3448 }
3449 spin_unlock_irqrestore(&rb->event_lock, flags);
3450
Frederic Weisbecker76369132011-05-19 19:55:04 +02003451 call_rcu(&rb->rcu_head, rb_free_rcu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003452}
3453
3454static void perf_mmap_open(struct vm_area_struct *vma)
3455{
3456 struct perf_event *event = vma->vm_file->private_data;
3457
3458 atomic_inc(&event->mmap_count);
3459}
3460
3461static void perf_mmap_close(struct vm_area_struct *vma)
3462{
3463 struct perf_event *event = vma->vm_file->private_data;
3464
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003465 if (atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) {
Frederic Weisbecker76369132011-05-19 19:55:04 +02003466 unsigned long size = perf_data_size(event->rb);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003467 struct user_struct *user = event->mmap_user;
Frederic Weisbecker76369132011-05-19 19:55:04 +02003468 struct ring_buffer *rb = event->rb;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003469
Peter Zijlstra906010b2009-09-21 16:08:49 +02003470 atomic_long_sub((size >> PAGE_SHIFT) + 1, &user->locked_vm);
Christoph Lameterbc3e53f2011-10-31 17:07:30 -07003471 vma->vm_mm->pinned_vm -= event->mmap_locked;
Frederic Weisbecker76369132011-05-19 19:55:04 +02003472 rcu_assign_pointer(event->rb, NULL);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003473 ring_buffer_detach(event, rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003474 mutex_unlock(&event->mmap_mutex);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003475
Frederic Weisbecker76369132011-05-19 19:55:04 +02003476 ring_buffer_put(rb);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003477 free_uid(user);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003478 }
3479}
3480
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04003481static const struct vm_operations_struct perf_mmap_vmops = {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003482 .open = perf_mmap_open,
3483 .close = perf_mmap_close,
3484 .fault = perf_mmap_fault,
3485 .page_mkwrite = perf_mmap_fault,
3486};
3487
3488static int perf_mmap(struct file *file, struct vm_area_struct *vma)
3489{
3490 struct perf_event *event = file->private_data;
3491 unsigned long user_locked, user_lock_limit;
3492 struct user_struct *user = current_user();
3493 unsigned long locked, lock_limit;
Frederic Weisbecker76369132011-05-19 19:55:04 +02003494 struct ring_buffer *rb;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003495 unsigned long vma_size;
3496 unsigned long nr_pages;
3497 long user_extra, extra;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02003498 int ret = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003499
Peter Zijlstrac7920612010-05-18 10:33:24 +02003500 /*
3501 * Don't allow mmap() of inherited per-task counters. This would
3502 * create a performance issue due to all children writing to the
Frederic Weisbecker76369132011-05-19 19:55:04 +02003503 * same rb.
Peter Zijlstrac7920612010-05-18 10:33:24 +02003504 */
3505 if (event->cpu == -1 && event->attr.inherit)
3506 return -EINVAL;
3507
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003508 if (!(vma->vm_flags & VM_SHARED))
3509 return -EINVAL;
3510
3511 vma_size = vma->vm_end - vma->vm_start;
3512 nr_pages = (vma_size / PAGE_SIZE) - 1;
3513
3514 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +02003515 * If we have rb pages ensure they're a power-of-two number, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003516 * can do bitmasks instead of modulo.
3517 */
3518 if (nr_pages != 0 && !is_power_of_2(nr_pages))
3519 return -EINVAL;
3520
3521 if (vma_size != PAGE_SIZE * (1 + nr_pages))
3522 return -EINVAL;
3523
3524 if (vma->vm_pgoff != 0)
3525 return -EINVAL;
3526
3527 WARN_ON_ONCE(event->ctx->parent_ctx);
3528 mutex_lock(&event->mmap_mutex);
Frederic Weisbecker76369132011-05-19 19:55:04 +02003529 if (event->rb) {
3530 if (event->rb->nr_pages == nr_pages)
3531 atomic_inc(&event->rb->refcount);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003532 else
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003533 ret = -EINVAL;
3534 goto unlock;
3535 }
3536
3537 user_extra = nr_pages + 1;
3538 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
3539
3540 /*
3541 * Increase the limit linearly with more CPUs:
3542 */
3543 user_lock_limit *= num_online_cpus();
3544
3545 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
3546
3547 extra = 0;
3548 if (user_locked > user_lock_limit)
3549 extra = user_locked - user_lock_limit;
3550
Jiri Slaby78d7d402010-03-05 13:42:54 -08003551 lock_limit = rlimit(RLIMIT_MEMLOCK);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003552 lock_limit >>= PAGE_SHIFT;
Christoph Lameterbc3e53f2011-10-31 17:07:30 -07003553 locked = vma->vm_mm->pinned_vm + extra;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003554
3555 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
3556 !capable(CAP_IPC_LOCK)) {
3557 ret = -EPERM;
3558 goto unlock;
3559 }
3560
Frederic Weisbecker76369132011-05-19 19:55:04 +02003561 WARN_ON(event->rb);
Peter Zijlstra906010b2009-09-21 16:08:49 +02003562
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02003563 if (vma->vm_flags & VM_WRITE)
Frederic Weisbecker76369132011-05-19 19:55:04 +02003564 flags |= RING_BUFFER_WRITABLE;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02003565
Vince Weaver4ec83632011-06-01 15:15:36 -04003566 rb = rb_alloc(nr_pages,
3567 event->attr.watermark ? event->attr.wakeup_watermark : 0,
3568 event->cpu, flags);
3569
Frederic Weisbecker76369132011-05-19 19:55:04 +02003570 if (!rb) {
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003571 ret = -ENOMEM;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003572 goto unlock;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003573 }
Frederic Weisbecker76369132011-05-19 19:55:04 +02003574 rcu_assign_pointer(event->rb, rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003575
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003576 atomic_long_add(user_extra, &user->locked_vm);
3577 event->mmap_locked = extra;
3578 event->mmap_user = get_current_user();
Christoph Lameterbc3e53f2011-10-31 17:07:30 -07003579 vma->vm_mm->pinned_vm += event->mmap_locked;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003580
Peter Zijlstra9a0f05c2011-11-21 15:13:29 +01003581 perf_event_update_userpage(event);
3582
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003583unlock:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003584 if (!ret)
3585 atomic_inc(&event->mmap_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003586 mutex_unlock(&event->mmap_mutex);
3587
3588 vma->vm_flags |= VM_RESERVED;
3589 vma->vm_ops = &perf_mmap_vmops;
3590
3591 return ret;
3592}
3593
3594static int perf_fasync(int fd, struct file *filp, int on)
3595{
3596 struct inode *inode = filp->f_path.dentry->d_inode;
3597 struct perf_event *event = filp->private_data;
3598 int retval;
3599
3600 mutex_lock(&inode->i_mutex);
3601 retval = fasync_helper(fd, filp, on, &event->fasync);
3602 mutex_unlock(&inode->i_mutex);
3603
3604 if (retval < 0)
3605 return retval;
3606
3607 return 0;
3608}
3609
3610static const struct file_operations perf_fops = {
Arnd Bergmann3326c1c2010-03-23 19:09:33 +01003611 .llseek = no_llseek,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003612 .release = perf_release,
3613 .read = perf_read,
3614 .poll = perf_poll,
3615 .unlocked_ioctl = perf_ioctl,
3616 .compat_ioctl = perf_ioctl,
3617 .mmap = perf_mmap,
3618 .fasync = perf_fasync,
3619};
3620
3621/*
3622 * Perf event wakeup
3623 *
3624 * If there's data, ensure we set the poll() state and publish everything
3625 * to user-space before waking everybody up.
3626 */
3627
3628void perf_event_wakeup(struct perf_event *event)
3629{
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003630 ring_buffer_wakeup(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003631
3632 if (event->pending_kill) {
3633 kill_fasync(&event->fasync, SIGIO, event->pending_kill);
3634 event->pending_kill = 0;
3635 }
3636}
3637
Peter Zijlstrae360adb2010-10-14 14:01:34 +08003638static void perf_pending_event(struct irq_work *entry)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003639{
3640 struct perf_event *event = container_of(entry,
3641 struct perf_event, pending);
3642
3643 if (event->pending_disable) {
3644 event->pending_disable = 0;
3645 __perf_event_disable(event);
3646 }
3647
3648 if (event->pending_wakeup) {
3649 event->pending_wakeup = 0;
3650 perf_event_wakeup(event);
3651 }
3652}
3653
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003654/*
Zhang, Yanmin39447b32010-04-19 13:32:41 +08003655 * We assume there is only KVM supporting the callbacks.
3656 * Later on, we might change it to a list if there is
3657 * another virtualization implementation supporting the callbacks.
3658 */
3659struct perf_guest_info_callbacks *perf_guest_cbs;
3660
3661int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3662{
3663 perf_guest_cbs = cbs;
3664 return 0;
3665}
3666EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
3667
3668int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3669{
3670 perf_guest_cbs = NULL;
3671 return 0;
3672}
3673EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
3674
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02003675static void __perf_event_header__init_id(struct perf_event_header *header,
3676 struct perf_sample_data *data,
3677 struct perf_event *event)
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02003678{
3679 u64 sample_type = event->attr.sample_type;
3680
3681 data->type = sample_type;
3682 header->size += event->id_header_size;
3683
3684 if (sample_type & PERF_SAMPLE_TID) {
3685 /* namespace issues */
3686 data->tid_entry.pid = perf_event_pid(event, current);
3687 data->tid_entry.tid = perf_event_tid(event, current);
3688 }
3689
3690 if (sample_type & PERF_SAMPLE_TIME)
3691 data->time = perf_clock();
3692
3693 if (sample_type & PERF_SAMPLE_ID)
3694 data->id = primary_event_id(event);
3695
3696 if (sample_type & PERF_SAMPLE_STREAM_ID)
3697 data->stream_id = event->id;
3698
3699 if (sample_type & PERF_SAMPLE_CPU) {
3700 data->cpu_entry.cpu = raw_smp_processor_id();
3701 data->cpu_entry.reserved = 0;
3702 }
3703}
3704
Frederic Weisbecker76369132011-05-19 19:55:04 +02003705void perf_event_header__init_id(struct perf_event_header *header,
3706 struct perf_sample_data *data,
3707 struct perf_event *event)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02003708{
3709 if (event->attr.sample_id_all)
3710 __perf_event_header__init_id(header, data, event);
3711}
3712
3713static void __perf_event__output_id_sample(struct perf_output_handle *handle,
3714 struct perf_sample_data *data)
3715{
3716 u64 sample_type = data->type;
3717
3718 if (sample_type & PERF_SAMPLE_TID)
3719 perf_output_put(handle, data->tid_entry);
3720
3721 if (sample_type & PERF_SAMPLE_TIME)
3722 perf_output_put(handle, data->time);
3723
3724 if (sample_type & PERF_SAMPLE_ID)
3725 perf_output_put(handle, data->id);
3726
3727 if (sample_type & PERF_SAMPLE_STREAM_ID)
3728 perf_output_put(handle, data->stream_id);
3729
3730 if (sample_type & PERF_SAMPLE_CPU)
3731 perf_output_put(handle, data->cpu_entry);
3732}
3733
Frederic Weisbecker76369132011-05-19 19:55:04 +02003734void perf_event__output_id_sample(struct perf_event *event,
3735 struct perf_output_handle *handle,
3736 struct perf_sample_data *sample)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02003737{
3738 if (event->attr.sample_id_all)
3739 __perf_event__output_id_sample(handle, sample);
3740}
3741
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003742static void perf_output_read_one(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02003743 struct perf_event *event,
3744 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003745{
3746 u64 read_format = event->attr.read_format;
3747 u64 values[4];
3748 int n = 0;
3749
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003750 values[n++] = perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003751 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
Stephane Eranianeed01522010-10-26 16:08:01 +02003752 values[n++] = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003753 atomic64_read(&event->child_total_time_enabled);
3754 }
3755 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
Stephane Eranianeed01522010-10-26 16:08:01 +02003756 values[n++] = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003757 atomic64_read(&event->child_total_time_running);
3758 }
3759 if (read_format & PERF_FORMAT_ID)
3760 values[n++] = primary_event_id(event);
3761
Frederic Weisbecker76369132011-05-19 19:55:04 +02003762 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003763}
3764
3765/*
3766 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
3767 */
3768static void perf_output_read_group(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02003769 struct perf_event *event,
3770 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003771{
3772 struct perf_event *leader = event->group_leader, *sub;
3773 u64 read_format = event->attr.read_format;
3774 u64 values[5];
3775 int n = 0;
3776
3777 values[n++] = 1 + leader->nr_siblings;
3778
3779 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
Stephane Eranianeed01522010-10-26 16:08:01 +02003780 values[n++] = enabled;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003781
3782 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
Stephane Eranianeed01522010-10-26 16:08:01 +02003783 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003784
3785 if (leader != event)
3786 leader->pmu->read(leader);
3787
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003788 values[n++] = perf_event_count(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003789 if (read_format & PERF_FORMAT_ID)
3790 values[n++] = primary_event_id(leader);
3791
Frederic Weisbecker76369132011-05-19 19:55:04 +02003792 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003793
3794 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
3795 n = 0;
3796
3797 if (sub != event)
3798 sub->pmu->read(sub);
3799
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003800 values[n++] = perf_event_count(sub);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003801 if (read_format & PERF_FORMAT_ID)
3802 values[n++] = primary_event_id(sub);
3803
Frederic Weisbecker76369132011-05-19 19:55:04 +02003804 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003805 }
3806}
3807
Stephane Eranianeed01522010-10-26 16:08:01 +02003808#define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
3809 PERF_FORMAT_TOTAL_TIME_RUNNING)
3810
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003811static void perf_output_read(struct perf_output_handle *handle,
3812 struct perf_event *event)
3813{
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003814 u64 enabled = 0, running = 0, now;
Stephane Eranianeed01522010-10-26 16:08:01 +02003815 u64 read_format = event->attr.read_format;
3816
3817 /*
3818 * compute total_time_enabled, total_time_running
3819 * based on snapshot values taken when the event
3820 * was last scheduled in.
3821 *
3822 * we cannot simply called update_context_time()
3823 * because of locking issue as we are called in
3824 * NMI context
3825 */
Eric B Munsonc4794292011-06-23 16:34:38 -04003826 if (read_format & PERF_FORMAT_TOTAL_TIMES)
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003827 calc_timer_values(event, &now, &enabled, &running);
Stephane Eranianeed01522010-10-26 16:08:01 +02003828
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003829 if (event->attr.read_format & PERF_FORMAT_GROUP)
Stephane Eranianeed01522010-10-26 16:08:01 +02003830 perf_output_read_group(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003831 else
Stephane Eranianeed01522010-10-26 16:08:01 +02003832 perf_output_read_one(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003833}
3834
3835void perf_output_sample(struct perf_output_handle *handle,
3836 struct perf_event_header *header,
3837 struct perf_sample_data *data,
3838 struct perf_event *event)
3839{
3840 u64 sample_type = data->type;
3841
3842 perf_output_put(handle, *header);
3843
3844 if (sample_type & PERF_SAMPLE_IP)
3845 perf_output_put(handle, data->ip);
3846
3847 if (sample_type & PERF_SAMPLE_TID)
3848 perf_output_put(handle, data->tid_entry);
3849
3850 if (sample_type & PERF_SAMPLE_TIME)
3851 perf_output_put(handle, data->time);
3852
3853 if (sample_type & PERF_SAMPLE_ADDR)
3854 perf_output_put(handle, data->addr);
3855
3856 if (sample_type & PERF_SAMPLE_ID)
3857 perf_output_put(handle, data->id);
3858
3859 if (sample_type & PERF_SAMPLE_STREAM_ID)
3860 perf_output_put(handle, data->stream_id);
3861
3862 if (sample_type & PERF_SAMPLE_CPU)
3863 perf_output_put(handle, data->cpu_entry);
3864
3865 if (sample_type & PERF_SAMPLE_PERIOD)
3866 perf_output_put(handle, data->period);
3867
3868 if (sample_type & PERF_SAMPLE_READ)
3869 perf_output_read(handle, event);
3870
3871 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
3872 if (data->callchain) {
3873 int size = 1;
3874
3875 if (data->callchain)
3876 size += data->callchain->nr;
3877
3878 size *= sizeof(u64);
3879
Frederic Weisbecker76369132011-05-19 19:55:04 +02003880 __output_copy(handle, data->callchain, size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003881 } else {
3882 u64 nr = 0;
3883 perf_output_put(handle, nr);
3884 }
3885 }
3886
3887 if (sample_type & PERF_SAMPLE_RAW) {
3888 if (data->raw) {
3889 perf_output_put(handle, data->raw->size);
Frederic Weisbecker76369132011-05-19 19:55:04 +02003890 __output_copy(handle, data->raw->data,
3891 data->raw->size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003892 } else {
3893 struct {
3894 u32 size;
3895 u32 data;
3896 } raw = {
3897 .size = sizeof(u32),
3898 .data = 0,
3899 };
3900 perf_output_put(handle, raw);
3901 }
3902 }
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02003903
3904 if (!event->attr.watermark) {
3905 int wakeup_events = event->attr.wakeup_events;
3906
3907 if (wakeup_events) {
3908 struct ring_buffer *rb = handle->rb;
3909 int events = local_inc_return(&rb->events);
3910
3911 if (events >= wakeup_events) {
3912 local_sub(wakeup_events, &rb->events);
3913 local_inc(&rb->wakeup);
3914 }
3915 }
3916 }
Stephane Eranianbce38cd2012-02-09 23:20:51 +01003917
3918 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
3919 if (data->br_stack) {
3920 size_t size;
3921
3922 size = data->br_stack->nr
3923 * sizeof(struct perf_branch_entry);
3924
3925 perf_output_put(handle, data->br_stack->nr);
3926 perf_output_copy(handle, data->br_stack->entries, size);
3927 } else {
3928 /*
3929 * we always store at least the value of nr
3930 */
3931 u64 nr = 0;
3932 perf_output_put(handle, nr);
3933 }
3934 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003935}
3936
3937void perf_prepare_sample(struct perf_event_header *header,
3938 struct perf_sample_data *data,
3939 struct perf_event *event,
3940 struct pt_regs *regs)
3941{
3942 u64 sample_type = event->attr.sample_type;
3943
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003944 header->type = PERF_RECORD_SAMPLE;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02003945 header->size = sizeof(*header) + event->header_size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003946
3947 header->misc = 0;
3948 header->misc |= perf_misc_flags(regs);
3949
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02003950 __perf_event_header__init_id(header, data, event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02003951
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02003952 if (sample_type & PERF_SAMPLE_IP)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003953 data->ip = perf_instruction_pointer(regs);
3954
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003955 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
3956 int size = 1;
3957
3958 data->callchain = perf_callchain(regs);
3959
3960 if (data->callchain)
3961 size += data->callchain->nr;
3962
3963 header->size += size * sizeof(u64);
3964 }
3965
3966 if (sample_type & PERF_SAMPLE_RAW) {
3967 int size = sizeof(u32);
3968
3969 if (data->raw)
3970 size += data->raw->size;
3971 else
3972 size += sizeof(u32);
3973
3974 WARN_ON_ONCE(size & (sizeof(u64)-1));
3975 header->size += size;
3976 }
Stephane Eranianbce38cd2012-02-09 23:20:51 +01003977
3978 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
3979 int size = sizeof(u64); /* nr */
3980 if (data->br_stack) {
3981 size += data->br_stack->nr
3982 * sizeof(struct perf_branch_entry);
3983 }
3984 header->size += size;
3985 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003986}
3987
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02003988static void perf_event_output(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003989 struct perf_sample_data *data,
3990 struct pt_regs *regs)
3991{
3992 struct perf_output_handle handle;
3993 struct perf_event_header header;
3994
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02003995 /* protect the callchain buffers */
3996 rcu_read_lock();
3997
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003998 perf_prepare_sample(&header, data, event, regs);
3999
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02004000 if (perf_output_begin(&handle, event, header.size))
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02004001 goto exit;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004002
4003 perf_output_sample(&handle, &header, data, event);
4004
4005 perf_output_end(&handle);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02004006
4007exit:
4008 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004009}
4010
4011/*
4012 * read event_id
4013 */
4014
4015struct perf_read_event {
4016 struct perf_event_header header;
4017
4018 u32 pid;
4019 u32 tid;
4020};
4021
4022static void
4023perf_event_read_event(struct perf_event *event,
4024 struct task_struct *task)
4025{
4026 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004027 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004028 struct perf_read_event read_event = {
4029 .header = {
4030 .type = PERF_RECORD_READ,
4031 .misc = 0,
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02004032 .size = sizeof(read_event) + event->read_size,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004033 },
4034 .pid = perf_event_pid(event, task),
4035 .tid = perf_event_tid(event, task),
4036 };
4037 int ret;
4038
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004039 perf_event_header__init_id(&read_event.header, &sample, event);
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02004040 ret = perf_output_begin(&handle, event, read_event.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004041 if (ret)
4042 return;
4043
4044 perf_output_put(&handle, read_event);
4045 perf_output_read(&handle, event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004046 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004047
4048 perf_output_end(&handle);
4049}
4050
4051/*
4052 * task tracking -- fork/exit
4053 *
Eric B Munson3af9e852010-05-18 15:30:49 +01004054 * enabled by: attr.comm | attr.mmap | attr.mmap_data | attr.task
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004055 */
4056
4057struct perf_task_event {
4058 struct task_struct *task;
4059 struct perf_event_context *task_ctx;
4060
4061 struct {
4062 struct perf_event_header header;
4063
4064 u32 pid;
4065 u32 ppid;
4066 u32 tid;
4067 u32 ptid;
4068 u64 time;
4069 } event_id;
4070};
4071
4072static void perf_event_task_output(struct perf_event *event,
4073 struct perf_task_event *task_event)
4074{
4075 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004076 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004077 struct task_struct *task = task_event->task;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004078 int ret, size = task_event->event_id.header.size;
Mike Galbraith8bb39f92010-03-26 11:11:33 +01004079
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004080 perf_event_header__init_id(&task_event->event_id.header, &sample, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004081
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004082 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02004083 task_event->event_id.header.size);
Peter Zijlstraef607772010-05-18 10:50:41 +02004084 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004085 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004086
4087 task_event->event_id.pid = perf_event_pid(event, task);
4088 task_event->event_id.ppid = perf_event_pid(event, current);
4089
4090 task_event->event_id.tid = perf_event_tid(event, task);
4091 task_event->event_id.ptid = perf_event_tid(event, current);
4092
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004093 perf_output_put(&handle, task_event->event_id);
4094
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004095 perf_event__output_id_sample(event, &handle, &sample);
4096
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004097 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004098out:
4099 task_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004100}
4101
4102static int perf_event_task_match(struct perf_event *event)
4103{
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01004104 if (event->state < PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra22e19082010-01-18 09:12:32 +01004105 return 0;
4106
Stephane Eranian5632ab12011-01-03 18:20:01 +02004107 if (!event_filter_match(event))
Peter Zijlstra5d27c232009-12-17 13:16:32 +01004108 return 0;
4109
Eric B Munson3af9e852010-05-18 15:30:49 +01004110 if (event->attr.comm || event->attr.mmap ||
4111 event->attr.mmap_data || event->attr.task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004112 return 1;
4113
4114 return 0;
4115}
4116
4117static void perf_event_task_ctx(struct perf_event_context *ctx,
4118 struct perf_task_event *task_event)
4119{
4120 struct perf_event *event;
4121
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004122 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4123 if (perf_event_task_match(event))
4124 perf_event_task_output(event, task_event);
4125 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004126}
4127
4128static void perf_event_task_event(struct perf_task_event *task_event)
4129{
4130 struct perf_cpu_context *cpuctx;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004131 struct perf_event_context *ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004132 struct pmu *pmu;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004133 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004134
Peter Zijlstrad6ff86c2009-11-20 22:19:46 +01004135 rcu_read_lock();
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004136 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra41945f62010-09-16 19:17:24 +02004137 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra51676952010-12-07 14:18:20 +01004138 if (cpuctx->active_pmu != pmu)
4139 goto next;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004140 perf_event_task_ctx(&cpuctx->ctx, task_event);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004141
4142 ctx = task_event->task_ctx;
4143 if (!ctx) {
4144 ctxn = pmu->task_ctx_nr;
4145 if (ctxn < 0)
Peter Zijlstra41945f62010-09-16 19:17:24 +02004146 goto next;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004147 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4148 }
4149 if (ctx)
4150 perf_event_task_ctx(ctx, task_event);
Peter Zijlstra41945f62010-09-16 19:17:24 +02004151next:
4152 put_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004153 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004154 rcu_read_unlock();
4155}
4156
4157static void perf_event_task(struct task_struct *task,
4158 struct perf_event_context *task_ctx,
4159 int new)
4160{
4161 struct perf_task_event task_event;
4162
4163 if (!atomic_read(&nr_comm_events) &&
4164 !atomic_read(&nr_mmap_events) &&
4165 !atomic_read(&nr_task_events))
4166 return;
4167
4168 task_event = (struct perf_task_event){
4169 .task = task,
4170 .task_ctx = task_ctx,
4171 .event_id = {
4172 .header = {
4173 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
4174 .misc = 0,
4175 .size = sizeof(task_event.event_id),
4176 },
4177 /* .pid */
4178 /* .ppid */
4179 /* .tid */
4180 /* .ptid */
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01004181 .time = perf_clock(),
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004182 },
4183 };
4184
4185 perf_event_task_event(&task_event);
4186}
4187
4188void perf_event_fork(struct task_struct *task)
4189{
4190 perf_event_task(task, NULL, 1);
4191}
4192
4193/*
4194 * comm tracking
4195 */
4196
4197struct perf_comm_event {
4198 struct task_struct *task;
4199 char *comm;
4200 int comm_size;
4201
4202 struct {
4203 struct perf_event_header header;
4204
4205 u32 pid;
4206 u32 tid;
4207 } event_id;
4208};
4209
4210static void perf_event_comm_output(struct perf_event *event,
4211 struct perf_comm_event *comm_event)
4212{
4213 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004214 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004215 int size = comm_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004216 int ret;
4217
4218 perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
4219 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02004220 comm_event->event_id.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004221
4222 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004223 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004224
4225 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
4226 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
4227
4228 perf_output_put(&handle, comm_event->event_id);
Frederic Weisbecker76369132011-05-19 19:55:04 +02004229 __output_copy(&handle, comm_event->comm,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004230 comm_event->comm_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004231
4232 perf_event__output_id_sample(event, &handle, &sample);
4233
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004234 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004235out:
4236 comm_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004237}
4238
4239static int perf_event_comm_match(struct perf_event *event)
4240{
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01004241 if (event->state < PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra22e19082010-01-18 09:12:32 +01004242 return 0;
4243
Stephane Eranian5632ab12011-01-03 18:20:01 +02004244 if (!event_filter_match(event))
Peter Zijlstra5d27c232009-12-17 13:16:32 +01004245 return 0;
4246
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004247 if (event->attr.comm)
4248 return 1;
4249
4250 return 0;
4251}
4252
4253static void perf_event_comm_ctx(struct perf_event_context *ctx,
4254 struct perf_comm_event *comm_event)
4255{
4256 struct perf_event *event;
4257
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004258 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4259 if (perf_event_comm_match(event))
4260 perf_event_comm_output(event, comm_event);
4261 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004262}
4263
4264static void perf_event_comm_event(struct perf_comm_event *comm_event)
4265{
4266 struct perf_cpu_context *cpuctx;
4267 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004268 char comm[TASK_COMM_LEN];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004269 unsigned int size;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004270 struct pmu *pmu;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004271 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004272
4273 memset(comm, 0, sizeof(comm));
Márton Németh96b02d72009-11-21 23:10:15 +01004274 strlcpy(comm, comm_event->task->comm, sizeof(comm));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004275 size = ALIGN(strlen(comm)+1, sizeof(u64));
4276
4277 comm_event->comm = comm;
4278 comm_event->comm_size = size;
4279
4280 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
Peter Zijlstraf6595f32009-11-20 22:19:47 +01004281 rcu_read_lock();
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004282 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra41945f62010-09-16 19:17:24 +02004283 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra51676952010-12-07 14:18:20 +01004284 if (cpuctx->active_pmu != pmu)
4285 goto next;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004286 perf_event_comm_ctx(&cpuctx->ctx, comm_event);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004287
4288 ctxn = pmu->task_ctx_nr;
4289 if (ctxn < 0)
Peter Zijlstra41945f62010-09-16 19:17:24 +02004290 goto next;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004291
4292 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4293 if (ctx)
4294 perf_event_comm_ctx(ctx, comm_event);
Peter Zijlstra41945f62010-09-16 19:17:24 +02004295next:
4296 put_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004297 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004298 rcu_read_unlock();
4299}
4300
4301void perf_event_comm(struct task_struct *task)
4302{
4303 struct perf_comm_event comm_event;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004304 struct perf_event_context *ctx;
4305 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004306
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004307 for_each_task_context_nr(ctxn) {
4308 ctx = task->perf_event_ctxp[ctxn];
4309 if (!ctx)
4310 continue;
4311
4312 perf_event_enable_on_exec(ctx);
4313 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004314
4315 if (!atomic_read(&nr_comm_events))
4316 return;
4317
4318 comm_event = (struct perf_comm_event){
4319 .task = task,
4320 /* .comm */
4321 /* .comm_size */
4322 .event_id = {
4323 .header = {
4324 .type = PERF_RECORD_COMM,
4325 .misc = 0,
4326 /* .size */
4327 },
4328 /* .pid */
4329 /* .tid */
4330 },
4331 };
4332
4333 perf_event_comm_event(&comm_event);
4334}
4335
4336/*
4337 * mmap tracking
4338 */
4339
4340struct perf_mmap_event {
4341 struct vm_area_struct *vma;
4342
4343 const char *file_name;
4344 int file_size;
4345
4346 struct {
4347 struct perf_event_header header;
4348
4349 u32 pid;
4350 u32 tid;
4351 u64 start;
4352 u64 len;
4353 u64 pgoff;
4354 } event_id;
4355};
4356
4357static void perf_event_mmap_output(struct perf_event *event,
4358 struct perf_mmap_event *mmap_event)
4359{
4360 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004361 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004362 int size = mmap_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004363 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004364
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004365 perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
4366 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02004367 mmap_event->event_id.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004368 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004369 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004370
4371 mmap_event->event_id.pid = perf_event_pid(event, current);
4372 mmap_event->event_id.tid = perf_event_tid(event, current);
4373
4374 perf_output_put(&handle, mmap_event->event_id);
Frederic Weisbecker76369132011-05-19 19:55:04 +02004375 __output_copy(&handle, mmap_event->file_name,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004376 mmap_event->file_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004377
4378 perf_event__output_id_sample(event, &handle, &sample);
4379
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004380 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004381out:
4382 mmap_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004383}
4384
4385static int perf_event_mmap_match(struct perf_event *event,
Eric B Munson3af9e852010-05-18 15:30:49 +01004386 struct perf_mmap_event *mmap_event,
4387 int executable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004388{
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01004389 if (event->state < PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra22e19082010-01-18 09:12:32 +01004390 return 0;
4391
Stephane Eranian5632ab12011-01-03 18:20:01 +02004392 if (!event_filter_match(event))
Peter Zijlstra5d27c232009-12-17 13:16:32 +01004393 return 0;
4394
Eric B Munson3af9e852010-05-18 15:30:49 +01004395 if ((!executable && event->attr.mmap_data) ||
4396 (executable && event->attr.mmap))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004397 return 1;
4398
4399 return 0;
4400}
4401
4402static void perf_event_mmap_ctx(struct perf_event_context *ctx,
Eric B Munson3af9e852010-05-18 15:30:49 +01004403 struct perf_mmap_event *mmap_event,
4404 int executable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004405{
4406 struct perf_event *event;
4407
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004408 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Eric B Munson3af9e852010-05-18 15:30:49 +01004409 if (perf_event_mmap_match(event, mmap_event, executable))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004410 perf_event_mmap_output(event, mmap_event);
4411 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004412}
4413
4414static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
4415{
4416 struct perf_cpu_context *cpuctx;
4417 struct perf_event_context *ctx;
4418 struct vm_area_struct *vma = mmap_event->vma;
4419 struct file *file = vma->vm_file;
4420 unsigned int size;
4421 char tmp[16];
4422 char *buf = NULL;
4423 const char *name;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004424 struct pmu *pmu;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004425 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004426
4427 memset(tmp, 0, sizeof(tmp));
4428
4429 if (file) {
4430 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +02004431 * d_path works from the end of the rb backwards, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004432 * need to add enough zero bytes after the string to handle
4433 * the 64bit alignment we do later.
4434 */
4435 buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
4436 if (!buf) {
4437 name = strncpy(tmp, "//enomem", sizeof(tmp));
4438 goto got_name;
4439 }
4440 name = d_path(&file->f_path, buf, PATH_MAX);
4441 if (IS_ERR(name)) {
4442 name = strncpy(tmp, "//toolong", sizeof(tmp));
4443 goto got_name;
4444 }
4445 } else {
4446 if (arch_vma_name(mmap_event->vma)) {
4447 name = strncpy(tmp, arch_vma_name(mmap_event->vma),
4448 sizeof(tmp));
4449 goto got_name;
4450 }
4451
4452 if (!vma->vm_mm) {
4453 name = strncpy(tmp, "[vdso]", sizeof(tmp));
4454 goto got_name;
Eric B Munson3af9e852010-05-18 15:30:49 +01004455 } else if (vma->vm_start <= vma->vm_mm->start_brk &&
4456 vma->vm_end >= vma->vm_mm->brk) {
4457 name = strncpy(tmp, "[heap]", sizeof(tmp));
4458 goto got_name;
4459 } else if (vma->vm_start <= vma->vm_mm->start_stack &&
4460 vma->vm_end >= vma->vm_mm->start_stack) {
4461 name = strncpy(tmp, "[stack]", sizeof(tmp));
4462 goto got_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004463 }
4464
4465 name = strncpy(tmp, "//anon", sizeof(tmp));
4466 goto got_name;
4467 }
4468
4469got_name:
4470 size = ALIGN(strlen(name)+1, sizeof(u64));
4471
4472 mmap_event->file_name = name;
4473 mmap_event->file_size = size;
4474
4475 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
4476
Peter Zijlstraf6d9dd22009-11-20 22:19:48 +01004477 rcu_read_lock();
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004478 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra41945f62010-09-16 19:17:24 +02004479 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra51676952010-12-07 14:18:20 +01004480 if (cpuctx->active_pmu != pmu)
4481 goto next;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004482 perf_event_mmap_ctx(&cpuctx->ctx, mmap_event,
4483 vma->vm_flags & VM_EXEC);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004484
4485 ctxn = pmu->task_ctx_nr;
4486 if (ctxn < 0)
Peter Zijlstra41945f62010-09-16 19:17:24 +02004487 goto next;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004488
4489 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4490 if (ctx) {
4491 perf_event_mmap_ctx(ctx, mmap_event,
4492 vma->vm_flags & VM_EXEC);
4493 }
Peter Zijlstra41945f62010-09-16 19:17:24 +02004494next:
4495 put_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004496 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004497 rcu_read_unlock();
4498
4499 kfree(buf);
4500}
4501
Eric B Munson3af9e852010-05-18 15:30:49 +01004502void perf_event_mmap(struct vm_area_struct *vma)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004503{
4504 struct perf_mmap_event mmap_event;
4505
4506 if (!atomic_read(&nr_mmap_events))
4507 return;
4508
4509 mmap_event = (struct perf_mmap_event){
4510 .vma = vma,
4511 /* .file_name */
4512 /* .file_size */
4513 .event_id = {
4514 .header = {
4515 .type = PERF_RECORD_MMAP,
Zhang, Yanmin39447b32010-04-19 13:32:41 +08004516 .misc = PERF_RECORD_MISC_USER,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004517 /* .size */
4518 },
4519 /* .pid */
4520 /* .tid */
4521 .start = vma->vm_start,
4522 .len = vma->vm_end - vma->vm_start,
Peter Zijlstra3a0304e2010-02-26 10:33:41 +01004523 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004524 },
4525 };
4526
4527 perf_event_mmap_event(&mmap_event);
4528}
4529
4530/*
4531 * IRQ throttle logging
4532 */
4533
4534static void perf_log_throttle(struct perf_event *event, int enable)
4535{
4536 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004537 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004538 int ret;
4539
4540 struct {
4541 struct perf_event_header header;
4542 u64 time;
4543 u64 id;
4544 u64 stream_id;
4545 } throttle_event = {
4546 .header = {
4547 .type = PERF_RECORD_THROTTLE,
4548 .misc = 0,
4549 .size = sizeof(throttle_event),
4550 },
4551 .time = perf_clock(),
4552 .id = primary_event_id(event),
4553 .stream_id = event->id,
4554 };
4555
4556 if (enable)
4557 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
4558
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004559 perf_event_header__init_id(&throttle_event.header, &sample, event);
4560
4561 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02004562 throttle_event.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004563 if (ret)
4564 return;
4565
4566 perf_output_put(&handle, throttle_event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004567 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004568 perf_output_end(&handle);
4569}
4570
4571/*
4572 * Generic event overflow handling, sampling.
4573 */
4574
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004575static int __perf_event_overflow(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004576 int throttle, struct perf_sample_data *data,
4577 struct pt_regs *regs)
4578{
4579 int events = atomic_read(&event->event_limit);
4580 struct hw_perf_event *hwc = &event->hw;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01004581 u64 seq;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004582 int ret = 0;
4583
Peter Zijlstra96398822010-11-24 18:55:29 +01004584 /*
4585 * Non-sampling counters might still use the PMI to fold short
4586 * hardware counters, ignore those.
4587 */
4588 if (unlikely(!is_sampling_event(event)))
4589 return 0;
4590
Stephane Eraniane050e3f2012-01-26 17:03:19 +01004591 seq = __this_cpu_read(perf_throttled_seq);
4592 if (seq != hwc->interrupts_seq) {
4593 hwc->interrupts_seq = seq;
4594 hwc->interrupts = 1;
4595 } else {
4596 hwc->interrupts++;
4597 if (unlikely(throttle
4598 && hwc->interrupts >= max_samples_per_tick)) {
4599 __this_cpu_inc(perf_throttled_count);
Peter Zijlstra163ec432011-02-16 11:22:34 +01004600 hwc->interrupts = MAX_INTERRUPTS;
4601 perf_log_throttle(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004602 ret = 1;
4603 }
Stephane Eraniane050e3f2012-01-26 17:03:19 +01004604 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004605
4606 if (event->attr.freq) {
4607 u64 now = perf_clock();
Peter Zijlstraabd50712010-01-26 18:50:16 +01004608 s64 delta = now - hwc->freq_time_stamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004609
Peter Zijlstraabd50712010-01-26 18:50:16 +01004610 hwc->freq_time_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004611
Peter Zijlstraabd50712010-01-26 18:50:16 +01004612 if (delta > 0 && delta < 2*TICK_NSEC)
Stephane Eranianf39d47f2012-02-07 14:39:57 +01004613 perf_adjust_period(event, delta, hwc->last_period, true);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004614 }
4615
4616 /*
4617 * XXX event_limit might not quite work as expected on inherited
4618 * events
4619 */
4620
4621 event->pending_kill = POLL_IN;
4622 if (events && atomic_dec_and_test(&event->event_limit)) {
4623 ret = 1;
4624 event->pending_kill = POLL_HUP;
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004625 event->pending_disable = 1;
4626 irq_work_queue(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004627 }
4628
Peter Zijlstra453f19e2009-11-20 22:19:43 +01004629 if (event->overflow_handler)
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004630 event->overflow_handler(event, data, regs);
Peter Zijlstra453f19e2009-11-20 22:19:43 +01004631 else
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004632 perf_event_output(event, data, regs);
Peter Zijlstra453f19e2009-11-20 22:19:43 +01004633
Peter Zijlstraf506b3d2011-05-26 17:02:53 +02004634 if (event->fasync && event->pending_kill) {
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004635 event->pending_wakeup = 1;
4636 irq_work_queue(&event->pending);
Peter Zijlstraf506b3d2011-05-26 17:02:53 +02004637 }
4638
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004639 return ret;
4640}
4641
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004642int perf_event_overflow(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004643 struct perf_sample_data *data,
4644 struct pt_regs *regs)
4645{
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004646 return __perf_event_overflow(event, 1, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004647}
4648
4649/*
4650 * Generic software event infrastructure
4651 */
4652
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004653struct swevent_htable {
4654 struct swevent_hlist *swevent_hlist;
4655 struct mutex hlist_mutex;
4656 int hlist_refcount;
4657
4658 /* Recursion avoidance in each contexts */
4659 int recursion[PERF_NR_CONTEXTS];
4660};
4661
4662static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
4663
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004664/*
4665 * We directly increment event->count and keep a second value in
4666 * event->hw.period_left to count intervals. This period event
4667 * is kept in the range [-sample_period, 0] so that we can use the
4668 * sign as trigger.
4669 */
4670
4671static u64 perf_swevent_set_period(struct perf_event *event)
4672{
4673 struct hw_perf_event *hwc = &event->hw;
4674 u64 period = hwc->last_period;
4675 u64 nr, offset;
4676 s64 old, val;
4677
4678 hwc->last_period = hwc->sample_period;
4679
4680again:
Peter Zijlstrae7850592010-05-21 14:43:08 +02004681 old = val = local64_read(&hwc->period_left);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004682 if (val < 0)
4683 return 0;
4684
4685 nr = div64_u64(period + val, period);
4686 offset = nr * period;
4687 val -= offset;
Peter Zijlstrae7850592010-05-21 14:43:08 +02004688 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004689 goto again;
4690
4691 return nr;
4692}
4693
Peter Zijlstra0cff7842009-11-20 22:19:44 +01004694static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004695 struct perf_sample_data *data,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004696 struct pt_regs *regs)
4697{
4698 struct hw_perf_event *hwc = &event->hw;
4699 int throttle = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004700
Peter Zijlstra0cff7842009-11-20 22:19:44 +01004701 if (!overflow)
4702 overflow = perf_swevent_set_period(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004703
4704 if (hwc->interrupts == MAX_INTERRUPTS)
4705 return;
4706
4707 for (; overflow; overflow--) {
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004708 if (__perf_event_overflow(event, throttle,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004709 data, regs)) {
4710 /*
4711 * We inhibit the overflow from happening when
4712 * hwc->interrupts == MAX_INTERRUPTS.
4713 */
4714 break;
4715 }
4716 throttle = 1;
4717 }
4718}
4719
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004720static void perf_swevent_event(struct perf_event *event, u64 nr,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004721 struct perf_sample_data *data,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004722 struct pt_regs *regs)
4723{
4724 struct hw_perf_event *hwc = &event->hw;
4725
Peter Zijlstrae7850592010-05-21 14:43:08 +02004726 local64_add(nr, &event->count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004727
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004728 if (!regs)
4729 return;
4730
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01004731 if (!is_sampling_event(event))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01004732 return;
4733
Andrew Vagin5d81e5c2011-11-07 15:54:12 +03004734 if ((event->attr.sample_type & PERF_SAMPLE_PERIOD) && !event->attr.freq) {
4735 data->period = nr;
4736 return perf_swevent_overflow(event, 1, data, regs);
4737 } else
4738 data->period = event->hw.last_period;
4739
Peter Zijlstra0cff7842009-11-20 22:19:44 +01004740 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004741 return perf_swevent_overflow(event, 1, data, regs);
Peter Zijlstra0cff7842009-11-20 22:19:44 +01004742
Peter Zijlstrae7850592010-05-21 14:43:08 +02004743 if (local64_add_negative(nr, &hwc->period_left))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01004744 return;
4745
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004746 perf_swevent_overflow(event, 0, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004747}
4748
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004749static int perf_exclude_event(struct perf_event *event,
4750 struct pt_regs *regs)
4751{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004752 if (event->hw.state & PERF_HES_STOPPED)
Frederic Weisbecker91b2f482011-03-07 21:27:08 +01004753 return 1;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004754
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004755 if (regs) {
4756 if (event->attr.exclude_user && user_mode(regs))
4757 return 1;
4758
4759 if (event->attr.exclude_kernel && !user_mode(regs))
4760 return 1;
4761 }
4762
4763 return 0;
4764}
4765
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004766static int perf_swevent_match(struct perf_event *event,
4767 enum perf_type_id type,
Li Zefan6fb29152009-10-15 11:21:42 +08004768 u32 event_id,
4769 struct perf_sample_data *data,
4770 struct pt_regs *regs)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004771{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004772 if (event->attr.type != type)
4773 return 0;
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004774
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004775 if (event->attr.config != event_id)
4776 return 0;
4777
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004778 if (perf_exclude_event(event, regs))
4779 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004780
4781 return 1;
4782}
4783
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004784static inline u64 swevent_hash(u64 type, u32 event_id)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004785{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004786 u64 val = event_id | (type << 32);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004787
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004788 return hash_64(val, SWEVENT_HLIST_BITS);
4789}
4790
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004791static inline struct hlist_head *
4792__find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004793{
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004794 u64 hash = swevent_hash(type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004795
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004796 return &hlist->heads[hash];
4797}
4798
4799/* For the read side: events when they trigger */
4800static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004801find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004802{
4803 struct swevent_hlist *hlist;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004804
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004805 hlist = rcu_dereference(swhash->swevent_hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004806 if (!hlist)
4807 return NULL;
4808
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004809 return __find_swevent_head(hlist, type, event_id);
4810}
4811
4812/* For the event head insertion and removal in the hlist */
4813static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004814find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004815{
4816 struct swevent_hlist *hlist;
4817 u32 event_id = event->attr.config;
4818 u64 type = event->attr.type;
4819
4820 /*
4821 * Event scheduling is always serialized against hlist allocation
4822 * and release. Which makes the protected version suitable here.
4823 * The context lock guarantees that.
4824 */
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004825 hlist = rcu_dereference_protected(swhash->swevent_hlist,
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004826 lockdep_is_held(&event->ctx->lock));
4827 if (!hlist)
4828 return NULL;
4829
4830 return __find_swevent_head(hlist, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004831}
4832
4833static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004834 u64 nr,
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004835 struct perf_sample_data *data,
4836 struct pt_regs *regs)
4837{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004838 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004839 struct perf_event *event;
4840 struct hlist_node *node;
4841 struct hlist_head *head;
4842
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004843 rcu_read_lock();
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004844 head = find_swevent_head_rcu(swhash, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004845 if (!head)
4846 goto end;
4847
4848 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
Li Zefan6fb29152009-10-15 11:21:42 +08004849 if (perf_swevent_match(event, type, event_id, data, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004850 perf_swevent_event(event, nr, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004851 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004852end:
4853 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004854}
4855
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004856int perf_swevent_get_recursion_context(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004857{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004858 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01004859
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004860 return get_recursion_context(swhash->recursion);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004861}
Ingo Molnar645e8cc2009-11-22 12:20:19 +01004862EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004863
Jesper Juhlfa9f90b2010-11-28 21:39:34 +01004864inline void perf_swevent_put_recursion_context(int rctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004865{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004866 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02004867
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004868 put_recursion_context(swhash->recursion, rctx);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01004869}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004870
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004871void __perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004872{
Ingo Molnara4234bf2009-11-23 10:57:59 +01004873 struct perf_sample_data data;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004874 int rctx;
4875
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004876 preempt_disable_notrace();
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004877 rctx = perf_swevent_get_recursion_context();
4878 if (rctx < 0)
4879 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004880
Peter Zijlstradc1d6282010-03-03 15:55:04 +01004881 perf_sample_data_init(&data, addr);
Ingo Molnara4234bf2009-11-23 10:57:59 +01004882
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004883 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs);
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004884
4885 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004886 preempt_enable_notrace();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004887}
4888
4889static void perf_swevent_read(struct perf_event *event)
4890{
4891}
4892
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004893static int perf_swevent_add(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004894{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004895 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004896 struct hw_perf_event *hwc = &event->hw;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004897 struct hlist_head *head;
4898
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01004899 if (is_sampling_event(event)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004900 hwc->last_period = hwc->sample_period;
4901 perf_swevent_set_period(event);
4902 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004903
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004904 hwc->state = !(flags & PERF_EF_START);
4905
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004906 head = find_swevent_head(swhash, event);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004907 if (WARN_ON_ONCE(!head))
4908 return -EINVAL;
4909
4910 hlist_add_head_rcu(&event->hlist_entry, head);
4911
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004912 return 0;
4913}
4914
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004915static void perf_swevent_del(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004916{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004917 hlist_del_rcu(&event->hlist_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004918}
4919
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004920static void perf_swevent_start(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02004921{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004922 event->hw.state = 0;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02004923}
4924
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004925static void perf_swevent_stop(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02004926{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004927 event->hw.state = PERF_HES_STOPPED;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02004928}
4929
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004930/* Deref the hlist from the update side */
4931static inline struct swevent_hlist *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004932swevent_hlist_deref(struct swevent_htable *swhash)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004933{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004934 return rcu_dereference_protected(swhash->swevent_hlist,
4935 lockdep_is_held(&swhash->hlist_mutex));
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004936}
4937
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004938static void swevent_hlist_release(struct swevent_htable *swhash)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004939{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004940 struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004941
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004942 if (!hlist)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004943 return;
4944
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004945 rcu_assign_pointer(swhash->swevent_hlist, NULL);
Lai Jiangshanfa4bbc42011-03-18 12:08:29 +08004946 kfree_rcu(hlist, rcu_head);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004947}
4948
4949static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
4950{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004951 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004952
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004953 mutex_lock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004954
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004955 if (!--swhash->hlist_refcount)
4956 swevent_hlist_release(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004957
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004958 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004959}
4960
4961static void swevent_hlist_put(struct perf_event *event)
4962{
4963 int cpu;
4964
4965 if (event->cpu != -1) {
4966 swevent_hlist_put_cpu(event, event->cpu);
4967 return;
4968 }
4969
4970 for_each_possible_cpu(cpu)
4971 swevent_hlist_put_cpu(event, cpu);
4972}
4973
4974static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
4975{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004976 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004977 int err = 0;
4978
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004979 mutex_lock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004980
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004981 if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004982 struct swevent_hlist *hlist;
4983
4984 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
4985 if (!hlist) {
4986 err = -ENOMEM;
4987 goto exit;
4988 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004989 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004990 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004991 swhash->hlist_refcount++;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02004992exit:
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004993 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004994
4995 return err;
4996}
4997
4998static int swevent_hlist_get(struct perf_event *event)
4999{
5000 int err;
5001 int cpu, failed_cpu;
5002
5003 if (event->cpu != -1)
5004 return swevent_hlist_get_cpu(event, event->cpu);
5005
5006 get_online_cpus();
5007 for_each_possible_cpu(cpu) {
5008 err = swevent_hlist_get_cpu(event, cpu);
5009 if (err) {
5010 failed_cpu = cpu;
5011 goto fail;
5012 }
5013 }
5014 put_online_cpus();
5015
5016 return 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02005017fail:
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005018 for_each_possible_cpu(cpu) {
5019 if (cpu == failed_cpu)
5020 break;
5021 swevent_hlist_put_cpu(event, cpu);
5022 }
5023
5024 put_online_cpus();
5025 return err;
5026}
5027
Ingo Molnarc5905af2012-02-24 08:31:31 +01005028struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
Frederic Weisbecker95476b62010-04-14 23:42:18 +02005029
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005030static void sw_perf_event_destroy(struct perf_event *event)
5031{
5032 u64 event_id = event->attr.config;
5033
5034 WARN_ON(event->parent);
5035
Ingo Molnarc5905af2012-02-24 08:31:31 +01005036 static_key_slow_dec(&perf_swevent_enabled[event_id]);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005037 swevent_hlist_put(event);
5038}
5039
5040static int perf_swevent_init(struct perf_event *event)
5041{
5042 int event_id = event->attr.config;
5043
5044 if (event->attr.type != PERF_TYPE_SOFTWARE)
5045 return -ENOENT;
5046
Stephane Eranian2481c5f2012-02-09 23:20:59 +01005047 /*
5048 * no branch sampling for software events
5049 */
5050 if (has_branch_stack(event))
5051 return -EOPNOTSUPP;
5052
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005053 switch (event_id) {
5054 case PERF_COUNT_SW_CPU_CLOCK:
5055 case PERF_COUNT_SW_TASK_CLOCK:
5056 return -ENOENT;
5057
5058 default:
5059 break;
5060 }
5061
Dan Carpenterce677832010-10-24 21:50:42 +02005062 if (event_id >= PERF_COUNT_SW_MAX)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005063 return -ENOENT;
5064
5065 if (!event->parent) {
5066 int err;
5067
5068 err = swevent_hlist_get(event);
5069 if (err)
5070 return err;
5071
Ingo Molnarc5905af2012-02-24 08:31:31 +01005072 static_key_slow_inc(&perf_swevent_enabled[event_id]);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005073 event->destroy = sw_perf_event_destroy;
5074 }
5075
5076 return 0;
5077}
5078
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01005079static int perf_swevent_event_idx(struct perf_event *event)
5080{
5081 return 0;
5082}
5083
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005084static struct pmu perf_swevent = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005085 .task_ctx_nr = perf_sw_context,
5086
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005087 .event_init = perf_swevent_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005088 .add = perf_swevent_add,
5089 .del = perf_swevent_del,
5090 .start = perf_swevent_start,
5091 .stop = perf_swevent_stop,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005092 .read = perf_swevent_read,
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01005093
5094 .event_idx = perf_swevent_event_idx,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005095};
Frederic Weisbecker95476b62010-04-14 23:42:18 +02005096
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005097#ifdef CONFIG_EVENT_TRACING
5098
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005099static int perf_tp_filter_match(struct perf_event *event,
Frederic Weisbecker95476b62010-04-14 23:42:18 +02005100 struct perf_sample_data *data)
5101{
5102 void *record = data->raw->data;
5103
5104 if (likely(!event->filter) || filter_match_preds(event->filter, record))
5105 return 1;
5106 return 0;
5107}
5108
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005109static int perf_tp_event_match(struct perf_event *event,
5110 struct perf_sample_data *data,
5111 struct pt_regs *regs)
5112{
Frederic Weisbeckera0f7d0f2011-03-07 21:27:09 +01005113 if (event->hw.state & PERF_HES_STOPPED)
5114 return 0;
Peter Zijlstra580d6072010-05-20 20:54:31 +02005115 /*
5116 * All tracepoints are from kernel-space.
5117 */
5118 if (event->attr.exclude_kernel)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005119 return 0;
5120
5121 if (!perf_tp_filter_match(event, data))
5122 return 0;
5123
5124 return 1;
5125}
5126
5127void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
Peter Zijlstraecc55f82010-05-21 15:11:34 +02005128 struct pt_regs *regs, struct hlist_head *head, int rctx)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005129{
5130 struct perf_sample_data data;
5131 struct perf_event *event;
5132 struct hlist_node *node;
5133
5134 struct perf_raw_record raw = {
5135 .size = entry_size,
5136 .data = record,
5137 };
5138
5139 perf_sample_data_init(&data, addr);
5140 data.raw = &raw;
5141
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005142 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
5143 if (perf_tp_event_match(event, &data, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005144 perf_swevent_event(event, count, &data, regs);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005145 }
Peter Zijlstraecc55f82010-05-21 15:11:34 +02005146
5147 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005148}
5149EXPORT_SYMBOL_GPL(perf_tp_event);
5150
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005151static void tp_perf_event_destroy(struct perf_event *event)
5152{
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005153 perf_trace_destroy(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005154}
5155
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005156static int perf_tp_event_init(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005157{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005158 int err;
5159
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005160 if (event->attr.type != PERF_TYPE_TRACEPOINT)
5161 return -ENOENT;
5162
Stephane Eranian2481c5f2012-02-09 23:20:59 +01005163 /*
5164 * no branch sampling for tracepoint events
5165 */
5166 if (has_branch_stack(event))
5167 return -EOPNOTSUPP;
5168
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005169 err = perf_trace_init(event);
5170 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005171 return err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005172
5173 event->destroy = tp_perf_event_destroy;
5174
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005175 return 0;
5176}
5177
5178static struct pmu perf_tracepoint = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005179 .task_ctx_nr = perf_sw_context,
5180
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005181 .event_init = perf_tp_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005182 .add = perf_trace_add,
5183 .del = perf_trace_del,
5184 .start = perf_swevent_start,
5185 .stop = perf_swevent_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005186 .read = perf_swevent_read,
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01005187
5188 .event_idx = perf_swevent_event_idx,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005189};
5190
5191static inline void perf_tp_register(void)
5192{
Peter Zijlstra2e80a822010-11-17 23:17:36 +01005193 perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005194}
Li Zefan6fb29152009-10-15 11:21:42 +08005195
5196static int perf_event_set_filter(struct perf_event *event, void __user *arg)
5197{
5198 char *filter_str;
5199 int ret;
5200
5201 if (event->attr.type != PERF_TYPE_TRACEPOINT)
5202 return -EINVAL;
5203
5204 filter_str = strndup_user(arg, PAGE_SIZE);
5205 if (IS_ERR(filter_str))
5206 return PTR_ERR(filter_str);
5207
5208 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
5209
5210 kfree(filter_str);
5211 return ret;
5212}
5213
5214static void perf_event_free_filter(struct perf_event *event)
5215{
5216 ftrace_profile_free_filter(event);
5217}
5218
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005219#else
Li Zefan6fb29152009-10-15 11:21:42 +08005220
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005221static inline void perf_tp_register(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005222{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005223}
Li Zefan6fb29152009-10-15 11:21:42 +08005224
5225static int perf_event_set_filter(struct perf_event *event, void __user *arg)
5226{
5227 return -ENOENT;
5228}
5229
5230static void perf_event_free_filter(struct perf_event *event)
5231{
5232}
5233
Li Zefan07b139c2009-12-21 14:27:35 +08005234#endif /* CONFIG_EVENT_TRACING */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005235
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02005236#ifdef CONFIG_HAVE_HW_BREAKPOINT
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005237void perf_bp_event(struct perf_event *bp, void *data)
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02005238{
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005239 struct perf_sample_data sample;
5240 struct pt_regs *regs = data;
5241
Peter Zijlstradc1d6282010-03-03 15:55:04 +01005242 perf_sample_data_init(&sample, bp->attr.bp_addr);
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005243
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005244 if (!bp->hw.state && !perf_exclude_event(bp, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005245 perf_swevent_event(bp, 1, &sample, regs);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02005246}
5247#endif
5248
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005249/*
5250 * hrtimer based swevent callback
5251 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005252
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005253static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005254{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005255 enum hrtimer_restart ret = HRTIMER_RESTART;
5256 struct perf_sample_data data;
5257 struct pt_regs *regs;
5258 struct perf_event *event;
5259 u64 period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005260
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005261 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
Peter Zijlstraba3dd362011-02-15 12:41:46 +01005262
5263 if (event->state != PERF_EVENT_STATE_ACTIVE)
5264 return HRTIMER_NORESTART;
5265
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005266 event->pmu->read(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005267
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005268 perf_sample_data_init(&data, 0);
5269 data.period = event->hw.last_period;
5270 regs = get_irq_regs();
5271
5272 if (regs && !perf_exclude_event(event, regs)) {
Paul E. McKenney77aeeeb2011-11-10 16:02:52 -08005273 if (!(event->attr.exclude_idle && is_idle_task(current)))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005274 if (perf_event_overflow(event, &data, regs))
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005275 ret = HRTIMER_NORESTART;
5276 }
5277
5278 period = max_t(u64, 10000, event->hw.sample_period);
5279 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
5280
5281 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005282}
5283
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005284static void perf_swevent_start_hrtimer(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005285{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005286 struct hw_perf_event *hwc = &event->hw;
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01005287 s64 period;
5288
5289 if (!is_sampling_event(event))
5290 return;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005291
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01005292 period = local64_read(&hwc->period_left);
5293 if (period) {
5294 if (period < 0)
5295 period = 10000;
Peter Zijlstrafa407f32010-06-24 12:35:12 +02005296
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01005297 local64_set(&hwc->period_left, 0);
5298 } else {
5299 period = max_t(u64, 10000, hwc->sample_period);
5300 }
5301 __hrtimer_start_range_ns(&hwc->hrtimer,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005302 ns_to_ktime(period), 0,
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02005303 HRTIMER_MODE_REL_PINNED, 0);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005304}
5305
5306static void perf_swevent_cancel_hrtimer(struct perf_event *event)
5307{
5308 struct hw_perf_event *hwc = &event->hw;
5309
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01005310 if (is_sampling_event(event)) {
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005311 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
Peter Zijlstrafa407f32010-06-24 12:35:12 +02005312 local64_set(&hwc->period_left, ktime_to_ns(remaining));
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005313
5314 hrtimer_cancel(&hwc->hrtimer);
5315 }
5316}
5317
Peter Zijlstraba3dd362011-02-15 12:41:46 +01005318static void perf_swevent_init_hrtimer(struct perf_event *event)
5319{
5320 struct hw_perf_event *hwc = &event->hw;
5321
5322 if (!is_sampling_event(event))
5323 return;
5324
5325 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
5326 hwc->hrtimer.function = perf_swevent_hrtimer;
5327
5328 /*
5329 * Since hrtimers have a fixed rate, we can do a static freq->period
5330 * mapping and avoid the whole period adjust feedback stuff.
5331 */
5332 if (event->attr.freq) {
5333 long freq = event->attr.sample_freq;
5334
5335 event->attr.sample_period = NSEC_PER_SEC / freq;
5336 hwc->sample_period = event->attr.sample_period;
5337 local64_set(&hwc->period_left, hwc->sample_period);
5338 event->attr.freq = 0;
5339 }
5340}
5341
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005342/*
5343 * Software event: cpu wall time clock
5344 */
5345
5346static void cpu_clock_event_update(struct perf_event *event)
5347{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005348 s64 prev;
5349 u64 now;
5350
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005351 now = local_clock();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005352 prev = local64_xchg(&event->hw.prev_count, now);
5353 local64_add(now - prev, &event->count);
5354}
5355
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005356static void cpu_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005357{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005358 local64_set(&event->hw.prev_count, local_clock());
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005359 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005360}
5361
5362static void cpu_clock_event_stop(struct perf_event *event, int flags)
5363{
5364 perf_swevent_cancel_hrtimer(event);
5365 cpu_clock_event_update(event);
5366}
5367
5368static int cpu_clock_event_add(struct perf_event *event, int flags)
5369{
5370 if (flags & PERF_EF_START)
5371 cpu_clock_event_start(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005372
5373 return 0;
5374}
5375
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005376static void cpu_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005377{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005378 cpu_clock_event_stop(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005379}
5380
5381static void cpu_clock_event_read(struct perf_event *event)
5382{
5383 cpu_clock_event_update(event);
5384}
5385
5386static int cpu_clock_event_init(struct perf_event *event)
5387{
5388 if (event->attr.type != PERF_TYPE_SOFTWARE)
5389 return -ENOENT;
5390
5391 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
5392 return -ENOENT;
5393
Stephane Eranian2481c5f2012-02-09 23:20:59 +01005394 /*
5395 * no branch sampling for software events
5396 */
5397 if (has_branch_stack(event))
5398 return -EOPNOTSUPP;
5399
Peter Zijlstraba3dd362011-02-15 12:41:46 +01005400 perf_swevent_init_hrtimer(event);
5401
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005402 return 0;
5403}
5404
5405static struct pmu perf_cpu_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005406 .task_ctx_nr = perf_sw_context,
5407
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005408 .event_init = cpu_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005409 .add = cpu_clock_event_add,
5410 .del = cpu_clock_event_del,
5411 .start = cpu_clock_event_start,
5412 .stop = cpu_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005413 .read = cpu_clock_event_read,
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01005414
5415 .event_idx = perf_swevent_event_idx,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005416};
5417
5418/*
5419 * Software event: task time clock
5420 */
5421
5422static void task_clock_event_update(struct perf_event *event, u64 now)
5423{
5424 u64 prev;
5425 s64 delta;
5426
5427 prev = local64_xchg(&event->hw.prev_count, now);
5428 delta = now - prev;
5429 local64_add(delta, &event->count);
5430}
5431
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005432static void task_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005433{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005434 local64_set(&event->hw.prev_count, event->ctx->time);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005435 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005436}
5437
5438static void task_clock_event_stop(struct perf_event *event, int flags)
5439{
5440 perf_swevent_cancel_hrtimer(event);
5441 task_clock_event_update(event, event->ctx->time);
5442}
5443
5444static int task_clock_event_add(struct perf_event *event, int flags)
5445{
5446 if (flags & PERF_EF_START)
5447 task_clock_event_start(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005448
5449 return 0;
5450}
5451
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005452static void task_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005453{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005454 task_clock_event_stop(event, PERF_EF_UPDATE);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005455}
5456
5457static void task_clock_event_read(struct perf_event *event)
5458{
Peter Zijlstra768a06e2011-02-22 16:52:24 +01005459 u64 now = perf_clock();
5460 u64 delta = now - event->ctx->timestamp;
5461 u64 time = event->ctx->time + delta;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005462
5463 task_clock_event_update(event, time);
5464}
5465
5466static int task_clock_event_init(struct perf_event *event)
5467{
5468 if (event->attr.type != PERF_TYPE_SOFTWARE)
5469 return -ENOENT;
5470
5471 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
5472 return -ENOENT;
5473
Stephane Eranian2481c5f2012-02-09 23:20:59 +01005474 /*
5475 * no branch sampling for software events
5476 */
5477 if (has_branch_stack(event))
5478 return -EOPNOTSUPP;
5479
Peter Zijlstraba3dd362011-02-15 12:41:46 +01005480 perf_swevent_init_hrtimer(event);
5481
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005482 return 0;
5483}
5484
5485static struct pmu perf_task_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005486 .task_ctx_nr = perf_sw_context,
5487
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005488 .event_init = task_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005489 .add = task_clock_event_add,
5490 .del = task_clock_event_del,
5491 .start = task_clock_event_start,
5492 .stop = task_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005493 .read = task_clock_event_read,
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01005494
5495 .event_idx = perf_swevent_event_idx,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005496};
5497
Peter Zijlstraad5133b2010-06-15 12:22:39 +02005498static void perf_pmu_nop_void(struct pmu *pmu)
5499{
5500}
5501
5502static int perf_pmu_nop_int(struct pmu *pmu)
5503{
5504 return 0;
5505}
5506
5507static void perf_pmu_start_txn(struct pmu *pmu)
5508{
5509 perf_pmu_disable(pmu);
5510}
5511
5512static int perf_pmu_commit_txn(struct pmu *pmu)
5513{
5514 perf_pmu_enable(pmu);
5515 return 0;
5516}
5517
5518static void perf_pmu_cancel_txn(struct pmu *pmu)
5519{
5520 perf_pmu_enable(pmu);
5521}
5522
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01005523static int perf_event_idx_default(struct perf_event *event)
5524{
5525 return event->hw.idx + 1;
5526}
5527
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005528/*
5529 * Ensures all contexts with the same task_ctx_nr have the same
5530 * pmu_cpu_context too.
5531 */
5532static void *find_pmu_context(int ctxn)
5533{
5534 struct pmu *pmu;
5535
5536 if (ctxn < 0)
5537 return NULL;
5538
5539 list_for_each_entry(pmu, &pmus, entry) {
5540 if (pmu->task_ctx_nr == ctxn)
5541 return pmu->pmu_cpu_context;
5542 }
5543
5544 return NULL;
5545}
5546
Peter Zijlstra51676952010-12-07 14:18:20 +01005547static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005548{
Peter Zijlstra51676952010-12-07 14:18:20 +01005549 int cpu;
5550
5551 for_each_possible_cpu(cpu) {
5552 struct perf_cpu_context *cpuctx;
5553
5554 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
5555
5556 if (cpuctx->active_pmu == old_pmu)
5557 cpuctx->active_pmu = pmu;
5558 }
5559}
5560
5561static void free_pmu_context(struct pmu *pmu)
5562{
5563 struct pmu *i;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005564
5565 mutex_lock(&pmus_lock);
5566 /*
5567 * Like a real lame refcount.
5568 */
Peter Zijlstra51676952010-12-07 14:18:20 +01005569 list_for_each_entry(i, &pmus, entry) {
5570 if (i->pmu_cpu_context == pmu->pmu_cpu_context) {
5571 update_pmu_context(i, pmu);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005572 goto out;
Peter Zijlstra51676952010-12-07 14:18:20 +01005573 }
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005574 }
5575
Peter Zijlstra51676952010-12-07 14:18:20 +01005576 free_percpu(pmu->pmu_cpu_context);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005577out:
5578 mutex_unlock(&pmus_lock);
5579}
Peter Zijlstra2e80a822010-11-17 23:17:36 +01005580static struct idr pmu_idr;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005581
Peter Zijlstraabe43402010-11-17 23:17:37 +01005582static ssize_t
5583type_show(struct device *dev, struct device_attribute *attr, char *page)
5584{
5585 struct pmu *pmu = dev_get_drvdata(dev);
5586
5587 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type);
5588}
5589
5590static struct device_attribute pmu_dev_attrs[] = {
5591 __ATTR_RO(type),
5592 __ATTR_NULL,
5593};
5594
5595static int pmu_bus_running;
5596static struct bus_type pmu_bus = {
5597 .name = "event_source",
5598 .dev_attrs = pmu_dev_attrs,
5599};
5600
5601static void pmu_dev_release(struct device *dev)
5602{
5603 kfree(dev);
5604}
5605
5606static int pmu_dev_alloc(struct pmu *pmu)
5607{
5608 int ret = -ENOMEM;
5609
5610 pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
5611 if (!pmu->dev)
5612 goto out;
5613
Peter Zijlstra0c9d42e2011-11-20 23:30:47 +01005614 pmu->dev->groups = pmu->attr_groups;
Peter Zijlstraabe43402010-11-17 23:17:37 +01005615 device_initialize(pmu->dev);
5616 ret = dev_set_name(pmu->dev, "%s", pmu->name);
5617 if (ret)
5618 goto free_dev;
5619
5620 dev_set_drvdata(pmu->dev, pmu);
5621 pmu->dev->bus = &pmu_bus;
5622 pmu->dev->release = pmu_dev_release;
5623 ret = device_add(pmu->dev);
5624 if (ret)
5625 goto free_dev;
5626
5627out:
5628 return ret;
5629
5630free_dev:
5631 put_device(pmu->dev);
5632 goto out;
5633}
5634
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01005635static struct lock_class_key cpuctx_mutex;
Peter Zijlstrafacc4302011-04-09 21:17:42 +02005636static struct lock_class_key cpuctx_lock;
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01005637
Peter Zijlstra2e80a822010-11-17 23:17:36 +01005638int perf_pmu_register(struct pmu *pmu, char *name, int type)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005639{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005640 int cpu, ret;
Peter Zijlstra33696fc2010-06-14 08:49:00 +02005641
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005642 mutex_lock(&pmus_lock);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02005643 ret = -ENOMEM;
5644 pmu->pmu_disable_count = alloc_percpu(int);
5645 if (!pmu->pmu_disable_count)
5646 goto unlock;
Peter Zijlstraad5133b2010-06-15 12:22:39 +02005647
Peter Zijlstra2e80a822010-11-17 23:17:36 +01005648 pmu->type = -1;
5649 if (!name)
5650 goto skip_type;
5651 pmu->name = name;
5652
5653 if (type < 0) {
5654 int err = idr_pre_get(&pmu_idr, GFP_KERNEL);
5655 if (!err)
5656 goto free_pdc;
5657
5658 err = idr_get_new_above(&pmu_idr, pmu, PERF_TYPE_MAX, &type);
5659 if (err) {
5660 ret = err;
5661 goto free_pdc;
5662 }
5663 }
5664 pmu->type = type;
5665
Peter Zijlstraabe43402010-11-17 23:17:37 +01005666 if (pmu_bus_running) {
5667 ret = pmu_dev_alloc(pmu);
5668 if (ret)
5669 goto free_idr;
5670 }
5671
Peter Zijlstra2e80a822010-11-17 23:17:36 +01005672skip_type:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005673 pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
5674 if (pmu->pmu_cpu_context)
5675 goto got_cpu_context;
5676
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005677 pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
5678 if (!pmu->pmu_cpu_context)
Peter Zijlstraabe43402010-11-17 23:17:37 +01005679 goto free_dev;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005680
5681 for_each_possible_cpu(cpu) {
5682 struct perf_cpu_context *cpuctx;
5683
5684 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Peter Zijlstraeb184472010-09-07 15:55:13 +02005685 __perf_event_init_context(&cpuctx->ctx);
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01005686 lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex);
Peter Zijlstrafacc4302011-04-09 21:17:42 +02005687 lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02005688 cpuctx->ctx.type = cpu_context;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005689 cpuctx->ctx.pmu = pmu;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02005690 cpuctx->jiffies_interval = 1;
5691 INIT_LIST_HEAD(&cpuctx->rotation_list);
Peter Zijlstra51676952010-12-07 14:18:20 +01005692 cpuctx->active_pmu = pmu;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005693 }
5694
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005695got_cpu_context:
Peter Zijlstraad5133b2010-06-15 12:22:39 +02005696 if (!pmu->start_txn) {
5697 if (pmu->pmu_enable) {
5698 /*
5699 * If we have pmu_enable/pmu_disable calls, install
5700 * transaction stubs that use that to try and batch
5701 * hardware accesses.
5702 */
5703 pmu->start_txn = perf_pmu_start_txn;
5704 pmu->commit_txn = perf_pmu_commit_txn;
5705 pmu->cancel_txn = perf_pmu_cancel_txn;
5706 } else {
5707 pmu->start_txn = perf_pmu_nop_void;
5708 pmu->commit_txn = perf_pmu_nop_int;
5709 pmu->cancel_txn = perf_pmu_nop_void;
5710 }
5711 }
5712
5713 if (!pmu->pmu_enable) {
5714 pmu->pmu_enable = perf_pmu_nop_void;
5715 pmu->pmu_disable = perf_pmu_nop_void;
5716 }
5717
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01005718 if (!pmu->event_idx)
5719 pmu->event_idx = perf_event_idx_default;
5720
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005721 list_add_rcu(&pmu->entry, &pmus);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02005722 ret = 0;
5723unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005724 mutex_unlock(&pmus_lock);
5725
Peter Zijlstra33696fc2010-06-14 08:49:00 +02005726 return ret;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005727
Peter Zijlstraabe43402010-11-17 23:17:37 +01005728free_dev:
5729 device_del(pmu->dev);
5730 put_device(pmu->dev);
5731
Peter Zijlstra2e80a822010-11-17 23:17:36 +01005732free_idr:
5733 if (pmu->type >= PERF_TYPE_MAX)
5734 idr_remove(&pmu_idr, pmu->type);
5735
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005736free_pdc:
5737 free_percpu(pmu->pmu_disable_count);
5738 goto unlock;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005739}
5740
5741void perf_pmu_unregister(struct pmu *pmu)
5742{
5743 mutex_lock(&pmus_lock);
5744 list_del_rcu(&pmu->entry);
5745 mutex_unlock(&pmus_lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005746
5747 /*
Peter Zijlstracde8e882010-09-13 11:06:55 +02005748 * We dereference the pmu list under both SRCU and regular RCU, so
5749 * synchronize against both of those.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005750 */
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005751 synchronize_srcu(&pmus_srcu);
Peter Zijlstracde8e882010-09-13 11:06:55 +02005752 synchronize_rcu();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005753
Peter Zijlstra33696fc2010-06-14 08:49:00 +02005754 free_percpu(pmu->pmu_disable_count);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01005755 if (pmu->type >= PERF_TYPE_MAX)
5756 idr_remove(&pmu_idr, pmu->type);
Peter Zijlstraabe43402010-11-17 23:17:37 +01005757 device_del(pmu->dev);
5758 put_device(pmu->dev);
Peter Zijlstra51676952010-12-07 14:18:20 +01005759 free_pmu_context(pmu);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005760}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005761
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005762struct pmu *perf_init_event(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005763{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02005764 struct pmu *pmu = NULL;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005765 int idx;
Lin Ming940c5b22011-02-27 21:13:31 +08005766 int ret;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005767
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005768 idx = srcu_read_lock(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01005769
5770 rcu_read_lock();
5771 pmu = idr_find(&pmu_idr, event->attr.type);
5772 rcu_read_unlock();
Lin Ming940c5b22011-02-27 21:13:31 +08005773 if (pmu) {
Mark Rutland7e5b2a02011-08-11 12:31:20 +01005774 event->pmu = pmu;
Lin Ming940c5b22011-02-27 21:13:31 +08005775 ret = pmu->event_init(event);
5776 if (ret)
5777 pmu = ERR_PTR(ret);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01005778 goto unlock;
Lin Ming940c5b22011-02-27 21:13:31 +08005779 }
Peter Zijlstra2e80a822010-11-17 23:17:36 +01005780
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005781 list_for_each_entry_rcu(pmu, &pmus, entry) {
Mark Rutland7e5b2a02011-08-11 12:31:20 +01005782 event->pmu = pmu;
Lin Ming940c5b22011-02-27 21:13:31 +08005783 ret = pmu->event_init(event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005784 if (!ret)
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02005785 goto unlock;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005786
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005787 if (ret != -ENOENT) {
5788 pmu = ERR_PTR(ret);
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02005789 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005790 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005791 }
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02005792 pmu = ERR_PTR(-ENOENT);
5793unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005794 srcu_read_unlock(&pmus_srcu, idx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005795
5796 return pmu;
5797}
5798
5799/*
5800 * Allocate and initialize a event structure
5801 */
5802static struct perf_event *
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005803perf_event_alloc(struct perf_event_attr *attr, int cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +02005804 struct task_struct *task,
5805 struct perf_event *group_leader,
5806 struct perf_event *parent_event,
Avi Kivity4dc0da82011-06-29 18:42:35 +03005807 perf_overflow_handler_t overflow_handler,
5808 void *context)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005809{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02005810 struct pmu *pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005811 struct perf_event *event;
5812 struct hw_perf_event *hwc;
5813 long err;
5814
Oleg Nesterov66832eb2011-01-18 17:10:32 +01005815 if ((unsigned)cpu >= nr_cpu_ids) {
5816 if (!task || cpu != -1)
5817 return ERR_PTR(-EINVAL);
5818 }
5819
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005820 event = kzalloc(sizeof(*event), GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005821 if (!event)
5822 return ERR_PTR(-ENOMEM);
5823
5824 /*
5825 * Single events are their own group leaders, with an
5826 * empty sibling list:
5827 */
5828 if (!group_leader)
5829 group_leader = event;
5830
5831 mutex_init(&event->child_mutex);
5832 INIT_LIST_HEAD(&event->child_list);
5833
5834 INIT_LIST_HEAD(&event->group_entry);
5835 INIT_LIST_HEAD(&event->event_entry);
5836 INIT_LIST_HEAD(&event->sibling_list);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01005837 INIT_LIST_HEAD(&event->rb_entry);
5838
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005839 init_waitqueue_head(&event->waitq);
Peter Zijlstrae360adb2010-10-14 14:01:34 +08005840 init_irq_work(&event->pending, perf_pending_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005841
5842 mutex_init(&event->mmap_mutex);
5843
5844 event->cpu = cpu;
5845 event->attr = *attr;
5846 event->group_leader = group_leader;
5847 event->pmu = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005848 event->oncpu = -1;
5849
5850 event->parent = parent_event;
5851
5852 event->ns = get_pid_ns(current->nsproxy->pid_ns);
5853 event->id = atomic64_inc_return(&perf_event_id);
5854
5855 event->state = PERF_EVENT_STATE_INACTIVE;
5856
Peter Zijlstrad580ff82010-10-14 17:43:23 +02005857 if (task) {
5858 event->attach_state = PERF_ATTACH_TASK;
5859#ifdef CONFIG_HAVE_HW_BREAKPOINT
5860 /*
5861 * hw_breakpoint is a bit difficult here..
5862 */
5863 if (attr->type == PERF_TYPE_BREAKPOINT)
5864 event->hw.bp_target = task;
5865#endif
5866 }
5867
Avi Kivity4dc0da82011-06-29 18:42:35 +03005868 if (!overflow_handler && parent_event) {
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01005869 overflow_handler = parent_event->overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03005870 context = parent_event->overflow_handler_context;
5871 }
Oleg Nesterov66832eb2011-01-18 17:10:32 +01005872
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01005873 event->overflow_handler = overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03005874 event->overflow_handler_context = context;
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02005875
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005876 if (attr->disabled)
5877 event->state = PERF_EVENT_STATE_OFF;
5878
5879 pmu = NULL;
5880
5881 hwc = &event->hw;
5882 hwc->sample_period = attr->sample_period;
5883 if (attr->freq && attr->sample_freq)
5884 hwc->sample_period = 1;
5885 hwc->last_period = hwc->sample_period;
5886
Peter Zijlstrae7850592010-05-21 14:43:08 +02005887 local64_set(&hwc->period_left, hwc->sample_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005888
5889 /*
5890 * we currently do not support PERF_FORMAT_GROUP on inherited events
5891 */
5892 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
5893 goto done;
5894
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005895 pmu = perf_init_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005896
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005897done:
5898 err = 0;
5899 if (!pmu)
5900 err = -EINVAL;
5901 else if (IS_ERR(pmu))
5902 err = PTR_ERR(pmu);
5903
5904 if (err) {
5905 if (event->ns)
5906 put_pid_ns(event->ns);
5907 kfree(event);
5908 return ERR_PTR(err);
5909 }
5910
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005911 if (!event->parent) {
Peter Zijlstra82cd6de2010-10-14 17:57:23 +02005912 if (event->attach_state & PERF_ATTACH_TASK)
Ingo Molnarc5905af2012-02-24 08:31:31 +01005913 static_key_slow_inc(&perf_sched_events.key);
Eric B Munson3af9e852010-05-18 15:30:49 +01005914 if (event->attr.mmap || event->attr.mmap_data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005915 atomic_inc(&nr_mmap_events);
5916 if (event->attr.comm)
5917 atomic_inc(&nr_comm_events);
5918 if (event->attr.task)
5919 atomic_inc(&nr_task_events);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02005920 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
5921 err = get_callchain_buffers();
5922 if (err) {
5923 free_event(event);
5924 return ERR_PTR(err);
5925 }
5926 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005927 }
5928
5929 return event;
5930}
5931
5932static int perf_copy_attr(struct perf_event_attr __user *uattr,
5933 struct perf_event_attr *attr)
5934{
5935 u32 size;
5936 int ret;
5937
5938 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
5939 return -EFAULT;
5940
5941 /*
5942 * zero the full structure, so that a short copy will be nice.
5943 */
5944 memset(attr, 0, sizeof(*attr));
5945
5946 ret = get_user(size, &uattr->size);
5947 if (ret)
5948 return ret;
5949
5950 if (size > PAGE_SIZE) /* silly large */
5951 goto err_size;
5952
5953 if (!size) /* abi compat */
5954 size = PERF_ATTR_SIZE_VER0;
5955
5956 if (size < PERF_ATTR_SIZE_VER0)
5957 goto err_size;
5958
5959 /*
5960 * If we're handed a bigger struct than we know of,
5961 * ensure all the unknown bits are 0 - i.e. new
5962 * user-space does not rely on any kernel feature
5963 * extensions we dont know about yet.
5964 */
5965 if (size > sizeof(*attr)) {
5966 unsigned char __user *addr;
5967 unsigned char __user *end;
5968 unsigned char val;
5969
5970 addr = (void __user *)uattr + sizeof(*attr);
5971 end = (void __user *)uattr + size;
5972
5973 for (; addr < end; addr++) {
5974 ret = get_user(val, addr);
5975 if (ret)
5976 return ret;
5977 if (val)
5978 goto err_size;
5979 }
5980 size = sizeof(*attr);
5981 }
5982
5983 ret = copy_from_user(attr, uattr, size);
5984 if (ret)
5985 return -EFAULT;
5986
Mahesh Salgaonkarcd757642010-01-30 10:25:18 +05305987 if (attr->__reserved_1)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005988 return -EINVAL;
5989
5990 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
5991 return -EINVAL;
5992
5993 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
5994 return -EINVAL;
5995
Stephane Eranianbce38cd2012-02-09 23:20:51 +01005996 if (attr->sample_type & PERF_SAMPLE_BRANCH_STACK) {
5997 u64 mask = attr->branch_sample_type;
5998
5999 /* only using defined bits */
6000 if (mask & ~(PERF_SAMPLE_BRANCH_MAX-1))
6001 return -EINVAL;
6002
6003 /* at least one branch bit must be set */
6004 if (!(mask & ~PERF_SAMPLE_BRANCH_PLM_ALL))
6005 return -EINVAL;
6006
6007 /* kernel level capture: check permissions */
6008 if ((mask & PERF_SAMPLE_BRANCH_PERM_PLM)
6009 && perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
6010 return -EACCES;
6011
6012 /* propagate priv level, when not set for branch */
6013 if (!(mask & PERF_SAMPLE_BRANCH_PLM_ALL)) {
6014
6015 /* exclude_kernel checked on syscall entry */
6016 if (!attr->exclude_kernel)
6017 mask |= PERF_SAMPLE_BRANCH_KERNEL;
6018
6019 if (!attr->exclude_user)
6020 mask |= PERF_SAMPLE_BRANCH_USER;
6021
6022 if (!attr->exclude_hv)
6023 mask |= PERF_SAMPLE_BRANCH_HV;
6024 /*
6025 * adjust user setting (for HW filter setup)
6026 */
6027 attr->branch_sample_type = mask;
6028 }
6029 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006030out:
6031 return ret;
6032
6033err_size:
6034 put_user(sizeof(*attr), &uattr->size);
6035 ret = -E2BIG;
6036 goto out;
6037}
6038
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006039static int
6040perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006041{
Frederic Weisbecker76369132011-05-19 19:55:04 +02006042 struct ring_buffer *rb = NULL, *old_rb = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006043 int ret = -EINVAL;
6044
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006045 if (!output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006046 goto set;
6047
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006048 /* don't allow circular references */
6049 if (event == output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006050 goto out;
6051
Peter Zijlstra0f139302010-05-20 14:35:15 +02006052 /*
6053 * Don't allow cross-cpu buffers
6054 */
6055 if (output_event->cpu != event->cpu)
6056 goto out;
6057
6058 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +02006059 * If its not a per-cpu rb, it must be the same task.
Peter Zijlstra0f139302010-05-20 14:35:15 +02006060 */
6061 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
6062 goto out;
6063
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006064set:
6065 mutex_lock(&event->mmap_mutex);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006066 /* Can't redirect output if we've got an active mmap() */
6067 if (atomic_read(&event->mmap_count))
6068 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006069
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006070 if (output_event) {
Frederic Weisbecker76369132011-05-19 19:55:04 +02006071 /* get the rb we want to redirect to */
6072 rb = ring_buffer_get(output_event);
6073 if (!rb)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006074 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006075 }
6076
Frederic Weisbecker76369132011-05-19 19:55:04 +02006077 old_rb = event->rb;
6078 rcu_assign_pointer(event->rb, rb);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01006079 if (old_rb)
6080 ring_buffer_detach(event, old_rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006081 ret = 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006082unlock:
6083 mutex_unlock(&event->mmap_mutex);
6084
Frederic Weisbecker76369132011-05-19 19:55:04 +02006085 if (old_rb)
6086 ring_buffer_put(old_rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006087out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006088 return ret;
6089}
6090
6091/**
6092 * sys_perf_event_open - open a performance event, associate it to a task/cpu
6093 *
6094 * @attr_uptr: event_id type attributes for monitoring/sampling
6095 * @pid: target pid
6096 * @cpu: target cpu
6097 * @group_fd: group leader event fd
6098 */
6099SYSCALL_DEFINE5(perf_event_open,
6100 struct perf_event_attr __user *, attr_uptr,
6101 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
6102{
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006103 struct perf_event *group_leader = NULL, *output_event = NULL;
6104 struct perf_event *event, *sibling;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006105 struct perf_event_attr attr;
6106 struct perf_event_context *ctx;
6107 struct file *event_file = NULL;
6108 struct file *group_file = NULL;
Matt Helsley38a81da2010-09-13 13:01:20 -07006109 struct task_struct *task = NULL;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006110 struct pmu *pmu;
Al Viroea635c62010-05-26 17:40:29 -04006111 int event_fd;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006112 int move_group = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006113 int fput_needed = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006114 int err;
6115
6116 /* for future expandability... */
Stephane Eraniane5d13672011-02-14 11:20:01 +02006117 if (flags & ~PERF_FLAG_ALL)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006118 return -EINVAL;
6119
6120 err = perf_copy_attr(attr_uptr, &attr);
6121 if (err)
6122 return err;
6123
6124 if (!attr.exclude_kernel) {
6125 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
6126 return -EACCES;
6127 }
6128
6129 if (attr.freq) {
6130 if (attr.sample_freq > sysctl_perf_event_sample_rate)
6131 return -EINVAL;
6132 }
6133
Stephane Eraniane5d13672011-02-14 11:20:01 +02006134 /*
6135 * In cgroup mode, the pid argument is used to pass the fd
6136 * opened to the cgroup directory in cgroupfs. The cpu argument
6137 * designates the cpu on which to monitor threads from that
6138 * cgroup.
6139 */
6140 if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1))
6141 return -EINVAL;
6142
Al Viroea635c62010-05-26 17:40:29 -04006143 event_fd = get_unused_fd_flags(O_RDWR);
6144 if (event_fd < 0)
6145 return event_fd;
6146
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006147 if (group_fd != -1) {
6148 group_leader = perf_fget_light(group_fd, &fput_needed);
6149 if (IS_ERR(group_leader)) {
6150 err = PTR_ERR(group_leader);
Stephane Eraniand14b12d2010-09-17 11:28:47 +02006151 goto err_fd;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006152 }
6153 group_file = group_leader->filp;
6154 if (flags & PERF_FLAG_FD_OUTPUT)
6155 output_event = group_leader;
6156 if (flags & PERF_FLAG_FD_NO_GROUP)
6157 group_leader = NULL;
6158 }
6159
Stephane Eraniane5d13672011-02-14 11:20:01 +02006160 if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) {
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02006161 task = find_lively_task_by_vpid(pid);
6162 if (IS_ERR(task)) {
6163 err = PTR_ERR(task);
6164 goto err_group_fd;
6165 }
6166 }
6167
Avi Kivity4dc0da82011-06-29 18:42:35 +03006168 event = perf_event_alloc(&attr, cpu, task, group_leader, NULL,
6169 NULL, NULL);
Stephane Eraniand14b12d2010-09-17 11:28:47 +02006170 if (IS_ERR(event)) {
6171 err = PTR_ERR(event);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02006172 goto err_task;
Stephane Eraniand14b12d2010-09-17 11:28:47 +02006173 }
6174
Stephane Eraniane5d13672011-02-14 11:20:01 +02006175 if (flags & PERF_FLAG_PID_CGROUP) {
6176 err = perf_cgroup_connect(pid, event, &attr, group_leader);
6177 if (err)
6178 goto err_alloc;
Peter Zijlstra08309372011-03-03 11:31:20 +01006179 /*
6180 * one more event:
6181 * - that has cgroup constraint on event->cpu
6182 * - that may need work on context switch
6183 */
6184 atomic_inc(&per_cpu(perf_cgroup_events, event->cpu));
Ingo Molnarc5905af2012-02-24 08:31:31 +01006185 static_key_slow_inc(&perf_sched_events.key);
Stephane Eraniane5d13672011-02-14 11:20:01 +02006186 }
6187
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006188 /*
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006189 * Special case software events and allow them to be part of
6190 * any hardware group.
6191 */
6192 pmu = event->pmu;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006193
6194 if (group_leader &&
6195 (is_software_event(event) != is_software_event(group_leader))) {
6196 if (is_software_event(event)) {
6197 /*
6198 * If event and group_leader are not both a software
6199 * event, and event is, then group leader is not.
6200 *
6201 * Allow the addition of software events to !software
6202 * groups, this is safe because software events never
6203 * fail to schedule.
6204 */
6205 pmu = group_leader->pmu;
6206 } else if (is_software_event(group_leader) &&
6207 (group_leader->group_flags & PERF_GROUP_SOFTWARE)) {
6208 /*
6209 * In case the group is a pure software group, and we
6210 * try to add a hardware event, move the whole group to
6211 * the hardware context.
6212 */
6213 move_group = 1;
6214 }
6215 }
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006216
6217 /*
6218 * Get the target context (task or percpu):
6219 */
Matt Helsley38a81da2010-09-13 13:01:20 -07006220 ctx = find_get_context(pmu, task, cpu);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006221 if (IS_ERR(ctx)) {
6222 err = PTR_ERR(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02006223 goto err_alloc;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006224 }
6225
Peter Zijlstrafd1edb32011-03-28 13:13:56 +02006226 if (task) {
6227 put_task_struct(task);
6228 task = NULL;
6229 }
6230
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006231 /*
6232 * Look up the group leader (we will attach this event to it):
6233 */
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006234 if (group_leader) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006235 err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006236
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006237 /*
6238 * Do not allow a recursive hierarchy (this new sibling
6239 * becoming part of another group-sibling):
6240 */
6241 if (group_leader->group_leader != group_leader)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006242 goto err_context;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006243 /*
6244 * Do not allow to attach to a group in a different
6245 * task or CPU context:
6246 */
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006247 if (move_group) {
6248 if (group_leader->ctx->type != ctx->type)
6249 goto err_context;
6250 } else {
6251 if (group_leader->ctx != ctx)
6252 goto err_context;
6253 }
6254
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006255 /*
6256 * Only a group leader can be exclusive or pinned
6257 */
6258 if (attr.exclusive || attr.pinned)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006259 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006260 }
6261
6262 if (output_event) {
6263 err = perf_event_set_output(event, output_event);
6264 if (err)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006265 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006266 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006267
Al Viroea635c62010-05-26 17:40:29 -04006268 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, O_RDWR);
6269 if (IS_ERR(event_file)) {
6270 err = PTR_ERR(event_file);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006271 goto err_context;
Al Viroea635c62010-05-26 17:40:29 -04006272 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006273
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006274 if (move_group) {
6275 struct perf_event_context *gctx = group_leader->ctx;
6276
6277 mutex_lock(&gctx->mutex);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01006278 perf_remove_from_context(group_leader);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006279 list_for_each_entry(sibling, &group_leader->sibling_list,
6280 group_entry) {
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01006281 perf_remove_from_context(sibling);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006282 put_ctx(gctx);
6283 }
6284 mutex_unlock(&gctx->mutex);
6285 put_ctx(gctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006286 }
6287
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006288 event->filp = event_file;
6289 WARN_ON_ONCE(ctx->parent_ctx);
6290 mutex_lock(&ctx->mutex);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006291
6292 if (move_group) {
6293 perf_install_in_context(ctx, group_leader, cpu);
6294 get_ctx(ctx);
6295 list_for_each_entry(sibling, &group_leader->sibling_list,
6296 group_entry) {
6297 perf_install_in_context(ctx, sibling, cpu);
6298 get_ctx(ctx);
6299 }
6300 }
6301
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006302 perf_install_in_context(ctx, event, cpu);
6303 ++ctx->generation;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01006304 perf_unpin_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006305 mutex_unlock(&ctx->mutex);
6306
6307 event->owner = current;
Peter Zijlstra88821352010-11-09 19:01:43 +01006308
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006309 mutex_lock(&current->perf_event_mutex);
6310 list_add_tail(&event->owner_entry, &current->perf_event_list);
6311 mutex_unlock(&current->perf_event_mutex);
6312
Peter Zijlstra8a495422010-05-27 15:47:49 +02006313 /*
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02006314 * Precalculate sample_data sizes
6315 */
6316 perf_event__header_size(event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02006317 perf_event__id_header_size(event);
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02006318
6319 /*
Peter Zijlstra8a495422010-05-27 15:47:49 +02006320 * Drop the reference on the group_event after placing the
6321 * new event on the sibling_list. This ensures destruction
6322 * of the group leader will find the pointer to itself in
6323 * perf_group_detach().
6324 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006325 fput_light(group_file, fput_needed);
Al Viroea635c62010-05-26 17:40:29 -04006326 fd_install(event_fd, event_file);
6327 return event_fd;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006328
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006329err_context:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01006330 perf_unpin_context(ctx);
Al Viroea635c62010-05-26 17:40:29 -04006331 put_ctx(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02006332err_alloc:
6333 free_event(event);
Peter Zijlstrae7d0bc02010-10-14 16:54:51 +02006334err_task:
6335 if (task)
6336 put_task_struct(task);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006337err_group_fd:
6338 fput_light(group_file, fput_needed);
Al Viroea635c62010-05-26 17:40:29 -04006339err_fd:
6340 put_unused_fd(event_fd);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006341 return err;
6342}
6343
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006344/**
6345 * perf_event_create_kernel_counter
6346 *
6347 * @attr: attributes of the counter to create
6348 * @cpu: cpu in which the counter is bound
Matt Helsley38a81da2010-09-13 13:01:20 -07006349 * @task: task to profile (NULL for percpu)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006350 */
6351struct perf_event *
6352perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
Matt Helsley38a81da2010-09-13 13:01:20 -07006353 struct task_struct *task,
Avi Kivity4dc0da82011-06-29 18:42:35 +03006354 perf_overflow_handler_t overflow_handler,
6355 void *context)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006356{
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006357 struct perf_event_context *ctx;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006358 struct perf_event *event;
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006359 int err;
6360
6361 /*
6362 * Get the target context (task or percpu):
6363 */
6364
Avi Kivity4dc0da82011-06-29 18:42:35 +03006365 event = perf_event_alloc(attr, cpu, task, NULL, NULL,
6366 overflow_handler, context);
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01006367 if (IS_ERR(event)) {
6368 err = PTR_ERR(event);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006369 goto err;
6370 }
6371
Matt Helsley38a81da2010-09-13 13:01:20 -07006372 ctx = find_get_context(event->pmu, task, cpu);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006373 if (IS_ERR(ctx)) {
6374 err = PTR_ERR(ctx);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006375 goto err_free;
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01006376 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006377
6378 event->filp = NULL;
6379 WARN_ON_ONCE(ctx->parent_ctx);
6380 mutex_lock(&ctx->mutex);
6381 perf_install_in_context(ctx, event, cpu);
6382 ++ctx->generation;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01006383 perf_unpin_context(ctx);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006384 mutex_unlock(&ctx->mutex);
6385
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006386 return event;
6387
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006388err_free:
6389 free_event(event);
6390err:
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01006391 return ERR_PTR(err);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006392}
6393EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
6394
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006395static void sync_child_event(struct perf_event *child_event,
6396 struct task_struct *child)
6397{
6398 struct perf_event *parent_event = child_event->parent;
6399 u64 child_val;
6400
6401 if (child_event->attr.inherit_stat)
6402 perf_event_read_event(child_event, child);
6403
Peter Zijlstrab5e58792010-05-21 14:43:12 +02006404 child_val = perf_event_count(child_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006405
6406 /*
6407 * Add back the child's count to the parent's count:
6408 */
Peter Zijlstraa6e6dea2010-05-21 14:27:58 +02006409 atomic64_add(child_val, &parent_event->child_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006410 atomic64_add(child_event->total_time_enabled,
6411 &parent_event->child_total_time_enabled);
6412 atomic64_add(child_event->total_time_running,
6413 &parent_event->child_total_time_running);
6414
6415 /*
6416 * Remove this event from the parent's list
6417 */
6418 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
6419 mutex_lock(&parent_event->child_mutex);
6420 list_del_init(&child_event->child_list);
6421 mutex_unlock(&parent_event->child_mutex);
6422
6423 /*
6424 * Release the parent event, if this was the last
6425 * reference to it.
6426 */
6427 fput(parent_event->filp);
6428}
6429
6430static void
6431__perf_event_exit_task(struct perf_event *child_event,
6432 struct perf_event_context *child_ctx,
6433 struct task_struct *child)
6434{
Peter Zijlstra38b435b2011-03-15 14:37:10 +01006435 if (child_event->parent) {
6436 raw_spin_lock_irq(&child_ctx->lock);
6437 perf_group_detach(child_event);
6438 raw_spin_unlock_irq(&child_ctx->lock);
6439 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006440
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01006441 perf_remove_from_context(child_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006442
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006443 /*
Peter Zijlstra38b435b2011-03-15 14:37:10 +01006444 * It can happen that the parent exits first, and has events
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006445 * that are still around due to the child reference. These
Peter Zijlstra38b435b2011-03-15 14:37:10 +01006446 * events need to be zapped.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006447 */
Peter Zijlstra38b435b2011-03-15 14:37:10 +01006448 if (child_event->parent) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006449 sync_child_event(child_event, child);
6450 free_event(child_event);
6451 }
6452}
6453
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006454static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006455{
6456 struct perf_event *child_event, *tmp;
6457 struct perf_event_context *child_ctx;
6458 unsigned long flags;
6459
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006460 if (likely(!child->perf_event_ctxp[ctxn])) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006461 perf_event_task(child, NULL, 0);
6462 return;
6463 }
6464
6465 local_irq_save(flags);
6466 /*
6467 * We can't reschedule here because interrupts are disabled,
6468 * and either child is current or it is a task that can't be
6469 * scheduled, so we are now safe from rescheduling changing
6470 * our context.
6471 */
Oleg Nesterov806839b2011-01-21 18:45:47 +01006472 child_ctx = rcu_dereference_raw(child->perf_event_ctxp[ctxn]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006473
6474 /*
6475 * Take the context lock here so that if find_get_context is
6476 * reading child->perf_event_ctxp, we wait until it has
6477 * incremented the context's refcount before we do put_ctx below.
6478 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +01006479 raw_spin_lock(&child_ctx->lock);
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02006480 task_ctx_sched_out(child_ctx);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006481 child->perf_event_ctxp[ctxn] = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006482 /*
6483 * If this context is a clone; unclone it so it can't get
6484 * swapped to another process while we're removing all
6485 * the events from it.
6486 */
6487 unclone_ctx(child_ctx);
Peter Zijlstra5e942bb2009-11-23 11:37:26 +01006488 update_context_time(child_ctx);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01006489 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006490
6491 /*
6492 * Report the task dead after unscheduling the events so that we
6493 * won't get any samples after PERF_RECORD_EXIT. We can however still
6494 * get a few PERF_RECORD_READ events.
6495 */
6496 perf_event_task(child, child_ctx, 0);
6497
6498 /*
6499 * We can recurse on the same lock type through:
6500 *
6501 * __perf_event_exit_task()
6502 * sync_child_event()
6503 * fput(parent_event->filp)
6504 * perf_release()
6505 * mutex_lock(&ctx->mutex)
6506 *
6507 * But since its the parent context it won't be the same instance.
6508 */
Peter Zijlstraa0507c82010-05-06 15:42:53 +02006509 mutex_lock(&child_ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006510
6511again:
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006512 list_for_each_entry_safe(child_event, tmp, &child_ctx->pinned_groups,
6513 group_entry)
6514 __perf_event_exit_task(child_event, child_ctx, child);
6515
6516 list_for_each_entry_safe(child_event, tmp, &child_ctx->flexible_groups,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006517 group_entry)
6518 __perf_event_exit_task(child_event, child_ctx, child);
6519
6520 /*
6521 * If the last event was a group event, it will have appended all
6522 * its siblings to the list, but we obtained 'tmp' before that which
6523 * will still point to the list head terminating the iteration.
6524 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006525 if (!list_empty(&child_ctx->pinned_groups) ||
6526 !list_empty(&child_ctx->flexible_groups))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006527 goto again;
6528
6529 mutex_unlock(&child_ctx->mutex);
6530
6531 put_ctx(child_ctx);
6532}
6533
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006534/*
6535 * When a child task exits, feed back event values to parent events.
6536 */
6537void perf_event_exit_task(struct task_struct *child)
6538{
Peter Zijlstra88821352010-11-09 19:01:43 +01006539 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006540 int ctxn;
6541
Peter Zijlstra88821352010-11-09 19:01:43 +01006542 mutex_lock(&child->perf_event_mutex);
6543 list_for_each_entry_safe(event, tmp, &child->perf_event_list,
6544 owner_entry) {
6545 list_del_init(&event->owner_entry);
6546
6547 /*
6548 * Ensure the list deletion is visible before we clear
6549 * the owner, closes a race against perf_release() where
6550 * we need to serialize on the owner->perf_event_mutex.
6551 */
6552 smp_wmb();
6553 event->owner = NULL;
6554 }
6555 mutex_unlock(&child->perf_event_mutex);
6556
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006557 for_each_task_context_nr(ctxn)
6558 perf_event_exit_task_context(child, ctxn);
6559}
6560
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006561static void perf_free_event(struct perf_event *event,
6562 struct perf_event_context *ctx)
6563{
6564 struct perf_event *parent = event->parent;
6565
6566 if (WARN_ON_ONCE(!parent))
6567 return;
6568
6569 mutex_lock(&parent->child_mutex);
6570 list_del_init(&event->child_list);
6571 mutex_unlock(&parent->child_mutex);
6572
6573 fput(parent->filp);
6574
Peter Zijlstra8a495422010-05-27 15:47:49 +02006575 perf_group_detach(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006576 list_del_event(event, ctx);
6577 free_event(event);
6578}
6579
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006580/*
6581 * free an unexposed, unused context as created by inheritance by
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006582 * perf_event_init_task below, used by fork() in case of fail.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006583 */
6584void perf_event_free_task(struct task_struct *task)
6585{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006586 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006587 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006588 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006589
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006590 for_each_task_context_nr(ctxn) {
6591 ctx = task->perf_event_ctxp[ctxn];
6592 if (!ctx)
6593 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006594
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006595 mutex_lock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006596again:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006597 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
6598 group_entry)
6599 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006600
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006601 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
6602 group_entry)
6603 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006604
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006605 if (!list_empty(&ctx->pinned_groups) ||
6606 !list_empty(&ctx->flexible_groups))
6607 goto again;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006608
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006609 mutex_unlock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006610
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006611 put_ctx(ctx);
6612 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006613}
6614
Peter Zijlstra4e231c72010-09-09 21:01:59 +02006615void perf_event_delayed_put(struct task_struct *task)
6616{
6617 int ctxn;
6618
6619 for_each_task_context_nr(ctxn)
6620 WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
6621}
6622
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02006623/*
6624 * inherit a event from parent task to child task:
6625 */
6626static struct perf_event *
6627inherit_event(struct perf_event *parent_event,
6628 struct task_struct *parent,
6629 struct perf_event_context *parent_ctx,
6630 struct task_struct *child,
6631 struct perf_event *group_leader,
6632 struct perf_event_context *child_ctx)
6633{
6634 struct perf_event *child_event;
Peter Zijlstracee010e2010-09-10 12:51:54 +02006635 unsigned long flags;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02006636
6637 /*
6638 * Instead of creating recursive hierarchies of events,
6639 * we link inherited events back to the original parent,
6640 * which has a filp for sure, which we use as the reference
6641 * count:
6642 */
6643 if (parent_event->parent)
6644 parent_event = parent_event->parent;
6645
6646 child_event = perf_event_alloc(&parent_event->attr,
6647 parent_event->cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +02006648 child,
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02006649 group_leader, parent_event,
Avi Kivity4dc0da82011-06-29 18:42:35 +03006650 NULL, NULL);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02006651 if (IS_ERR(child_event))
6652 return child_event;
6653 get_ctx(child_ctx);
6654
6655 /*
6656 * Make the child state follow the state of the parent event,
6657 * not its attr.disabled bit. We hold the parent's mutex,
6658 * so we won't race with perf_event_{en, dis}able_family.
6659 */
6660 if (parent_event->state >= PERF_EVENT_STATE_INACTIVE)
6661 child_event->state = PERF_EVENT_STATE_INACTIVE;
6662 else
6663 child_event->state = PERF_EVENT_STATE_OFF;
6664
6665 if (parent_event->attr.freq) {
6666 u64 sample_period = parent_event->hw.sample_period;
6667 struct hw_perf_event *hwc = &child_event->hw;
6668
6669 hwc->sample_period = sample_period;
6670 hwc->last_period = sample_period;
6671
6672 local64_set(&hwc->period_left, sample_period);
6673 }
6674
6675 child_event->ctx = child_ctx;
6676 child_event->overflow_handler = parent_event->overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03006677 child_event->overflow_handler_context
6678 = parent_event->overflow_handler_context;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02006679
6680 /*
Thomas Gleixner614b6782010-12-03 16:24:32 -02006681 * Precalculate sample_data sizes
6682 */
6683 perf_event__header_size(child_event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02006684 perf_event__id_header_size(child_event);
Thomas Gleixner614b6782010-12-03 16:24:32 -02006685
6686 /*
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02006687 * Link it up in the child's context:
6688 */
Peter Zijlstracee010e2010-09-10 12:51:54 +02006689 raw_spin_lock_irqsave(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02006690 add_event_to_ctx(child_event, child_ctx);
Peter Zijlstracee010e2010-09-10 12:51:54 +02006691 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02006692
6693 /*
6694 * Get a reference to the parent filp - we will fput it
6695 * when the child event exits. This is safe to do because
6696 * we are in the parent and we know that the filp still
6697 * exists and has a nonzero count:
6698 */
6699 atomic_long_inc(&parent_event->filp->f_count);
6700
6701 /*
6702 * Link this into the parent event's child list
6703 */
6704 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
6705 mutex_lock(&parent_event->child_mutex);
6706 list_add_tail(&child_event->child_list, &parent_event->child_list);
6707 mutex_unlock(&parent_event->child_mutex);
6708
6709 return child_event;
6710}
6711
6712static int inherit_group(struct perf_event *parent_event,
6713 struct task_struct *parent,
6714 struct perf_event_context *parent_ctx,
6715 struct task_struct *child,
6716 struct perf_event_context *child_ctx)
6717{
6718 struct perf_event *leader;
6719 struct perf_event *sub;
6720 struct perf_event *child_ctr;
6721
6722 leader = inherit_event(parent_event, parent, parent_ctx,
6723 child, NULL, child_ctx);
6724 if (IS_ERR(leader))
6725 return PTR_ERR(leader);
6726 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
6727 child_ctr = inherit_event(sub, parent, parent_ctx,
6728 child, leader, child_ctx);
6729 if (IS_ERR(child_ctr))
6730 return PTR_ERR(child_ctr);
6731 }
6732 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006733}
6734
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006735static int
6736inherit_task_group(struct perf_event *event, struct task_struct *parent,
6737 struct perf_event_context *parent_ctx,
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006738 struct task_struct *child, int ctxn,
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006739 int *inherited_all)
6740{
6741 int ret;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006742 struct perf_event_context *child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006743
6744 if (!event->attr.inherit) {
6745 *inherited_all = 0;
6746 return 0;
6747 }
6748
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01006749 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006750 if (!child_ctx) {
6751 /*
6752 * This is executed from the parent task context, so
6753 * inherit events that have been marked for cloning.
6754 * First allocate and initialize a context for the
6755 * child.
6756 */
6757
Peter Zijlstraeb184472010-09-07 15:55:13 +02006758 child_ctx = alloc_perf_context(event->pmu, child);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006759 if (!child_ctx)
6760 return -ENOMEM;
6761
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006762 child->perf_event_ctxp[ctxn] = child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006763 }
6764
6765 ret = inherit_group(event, parent, parent_ctx,
6766 child, child_ctx);
6767
6768 if (ret)
6769 *inherited_all = 0;
6770
6771 return ret;
6772}
6773
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006774/*
6775 * Initialize the perf_event context in task_struct
6776 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006777int perf_event_init_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006778{
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006779 struct perf_event_context *child_ctx, *parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006780 struct perf_event_context *cloned_ctx;
6781 struct perf_event *event;
6782 struct task_struct *parent = current;
6783 int inherited_all = 1;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01006784 unsigned long flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006785 int ret = 0;
6786
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006787 if (likely(!parent->perf_event_ctxp[ctxn]))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006788 return 0;
6789
6790 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006791 * If the parent's context is a clone, pin it so it won't get
6792 * swapped under us.
6793 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006794 parent_ctx = perf_pin_task_context(parent, ctxn);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006795
6796 /*
6797 * No need to check if parent_ctx != NULL here; since we saw
6798 * it non-NULL earlier, the only reason for it to become NULL
6799 * is if we exit, and since we're currently in the middle of
6800 * a fork we can't be exiting at the same time.
6801 */
6802
6803 /*
6804 * Lock the parent list. No need to lock the child - not PID
6805 * hashed yet and not running, so nobody can access it.
6806 */
6807 mutex_lock(&parent_ctx->mutex);
6808
6809 /*
6810 * We dont have to disable NMIs - we are only looking at
6811 * the list, not manipulating it:
6812 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006813 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006814 ret = inherit_task_group(event, parent, parent_ctx,
6815 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006816 if (ret)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006817 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006818 }
6819
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01006820 /*
6821 * We can't hold ctx->lock when iterating the ->flexible_group list due
6822 * to allocations, but we need to prevent rotation because
6823 * rotate_ctx() will change the list from interrupt context.
6824 */
6825 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
6826 parent_ctx->rotate_disable = 1;
6827 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
6828
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006829 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006830 ret = inherit_task_group(event, parent, parent_ctx,
6831 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006832 if (ret)
6833 break;
6834 }
6835
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01006836 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
6837 parent_ctx->rotate_disable = 0;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01006838
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006839 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006840
Peter Zijlstra05cbaa22009-12-30 16:00:35 +01006841 if (child_ctx && inherited_all) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006842 /*
6843 * Mark the child context as a clone of the parent
6844 * context, or of whatever the parent is a clone of.
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01006845 *
6846 * Note that if the parent is a clone, the holding of
6847 * parent_ctx->lock avoids it from being uncloned.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006848 */
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01006849 cloned_ctx = parent_ctx->parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006850 if (cloned_ctx) {
6851 child_ctx->parent_ctx = cloned_ctx;
6852 child_ctx->parent_gen = parent_ctx->parent_gen;
6853 } else {
6854 child_ctx->parent_ctx = parent_ctx;
6855 child_ctx->parent_gen = parent_ctx->generation;
6856 }
6857 get_ctx(child_ctx->parent_ctx);
6858 }
6859
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01006860 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006861 mutex_unlock(&parent_ctx->mutex);
6862
6863 perf_unpin_context(parent_ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01006864 put_ctx(parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006865
6866 return ret;
6867}
6868
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006869/*
6870 * Initialize the perf_event context in task_struct
6871 */
6872int perf_event_init_task(struct task_struct *child)
6873{
6874 int ctxn, ret;
6875
Oleg Nesterov8550d7c2011-01-19 19:22:28 +01006876 memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp));
6877 mutex_init(&child->perf_event_mutex);
6878 INIT_LIST_HEAD(&child->perf_event_list);
6879
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006880 for_each_task_context_nr(ctxn) {
6881 ret = perf_event_init_context(child, ctxn);
6882 if (ret)
6883 return ret;
6884 }
6885
6886 return 0;
6887}
6888
Paul Mackerras220b1402010-03-10 20:45:52 +11006889static void __init perf_event_init_all_cpus(void)
6890{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006891 struct swevent_htable *swhash;
Paul Mackerras220b1402010-03-10 20:45:52 +11006892 int cpu;
Paul Mackerras220b1402010-03-10 20:45:52 +11006893
6894 for_each_possible_cpu(cpu) {
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006895 swhash = &per_cpu(swevent_htable, cpu);
6896 mutex_init(&swhash->hlist_mutex);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02006897 INIT_LIST_HEAD(&per_cpu(rotation_list, cpu));
Paul Mackerras220b1402010-03-10 20:45:52 +11006898 }
6899}
6900
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006901static void __cpuinit perf_event_init_cpu(int cpu)
6902{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006903 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006904
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006905 mutex_lock(&swhash->hlist_mutex);
Linus Torvalds4536e4d2011-11-03 07:44:04 -07006906 if (swhash->hlist_refcount > 0) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006907 struct swevent_hlist *hlist;
6908
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006909 hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
6910 WARN_ON(!hlist);
6911 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006912 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006913 mutex_unlock(&swhash->hlist_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006914}
6915
Peter Zijlstrac2774432010-12-08 15:29:02 +01006916#if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02006917static void perf_pmu_rotate_stop(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006918{
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02006919 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
6920
6921 WARN_ON(!irqs_disabled());
6922
6923 list_del_init(&cpuctx->rotation_list);
6924}
6925
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006926static void __perf_event_exit_context(void *__info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006927{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006928 struct perf_event_context *ctx = __info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006929 struct perf_event *event, *tmp;
6930
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006931 perf_pmu_rotate_stop(ctx->pmu);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02006932
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006933 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01006934 __perf_remove_from_context(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006935 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, group_entry)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01006936 __perf_remove_from_context(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006937}
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006938
6939static void perf_event_exit_cpu_context(int cpu)
6940{
6941 struct perf_event_context *ctx;
6942 struct pmu *pmu;
6943 int idx;
6944
6945 idx = srcu_read_lock(&pmus_srcu);
6946 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra917bdd12010-09-17 11:28:49 +02006947 ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006948
6949 mutex_lock(&ctx->mutex);
6950 smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
6951 mutex_unlock(&ctx->mutex);
6952 }
6953 srcu_read_unlock(&pmus_srcu, idx);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006954}
6955
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006956static void perf_event_exit_cpu(int cpu)
6957{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006958 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006959
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006960 mutex_lock(&swhash->hlist_mutex);
6961 swevent_hlist_release(swhash);
6962 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006963
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006964 perf_event_exit_cpu_context(cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006965}
6966#else
6967static inline void perf_event_exit_cpu(int cpu) { }
6968#endif
6969
Peter Zijlstrac2774432010-12-08 15:29:02 +01006970static int
6971perf_reboot(struct notifier_block *notifier, unsigned long val, void *v)
6972{
6973 int cpu;
6974
6975 for_each_online_cpu(cpu)
6976 perf_event_exit_cpu(cpu);
6977
6978 return NOTIFY_OK;
6979}
6980
6981/*
6982 * Run the perf reboot notifier at the very last possible moment so that
6983 * the generic watchdog code runs as long as possible.
6984 */
6985static struct notifier_block perf_reboot_notifier = {
6986 .notifier_call = perf_reboot,
6987 .priority = INT_MIN,
6988};
6989
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006990static int __cpuinit
6991perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
6992{
6993 unsigned int cpu = (long)hcpu;
6994
Linus Torvalds4536e4d2011-11-03 07:44:04 -07006995 switch (action & ~CPU_TASKS_FROZEN) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006996
6997 case CPU_UP_PREPARE:
Peter Zijlstra5e116372010-06-11 13:35:08 +02006998 case CPU_DOWN_FAILED:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006999 perf_event_init_cpu(cpu);
7000 break;
7001
Peter Zijlstra5e116372010-06-11 13:35:08 +02007002 case CPU_UP_CANCELED:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007003 case CPU_DOWN_PREPARE:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007004 perf_event_exit_cpu(cpu);
7005 break;
7006
7007 default:
7008 break;
7009 }
7010
7011 return NOTIFY_OK;
7012}
7013
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007014void __init perf_event_init(void)
7015{
Jason Wessel3c502e72010-11-04 17:33:01 -05007016 int ret;
7017
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007018 idr_init(&pmu_idr);
7019
Paul Mackerras220b1402010-03-10 20:45:52 +11007020 perf_event_init_all_cpus();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007021 init_srcu_struct(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007022 perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE);
7023 perf_pmu_register(&perf_cpu_clock, NULL, -1);
7024 perf_pmu_register(&perf_task_clock, NULL, -1);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007025 perf_tp_register();
7026 perf_cpu_notifier(perf_cpu_notify);
Peter Zijlstrac2774432010-12-08 15:29:02 +01007027 register_reboot_notifier(&perf_reboot_notifier);
Jason Wessel3c502e72010-11-04 17:33:01 -05007028
7029 ret = init_hw_breakpoint();
7030 WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
Gleb Natapovb2029522011-11-27 17:59:09 +02007031
7032 /* do not patch jump label more than once per second */
7033 jump_label_rate_limit(&perf_sched_events, HZ);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007034}
Peter Zijlstraabe43402010-11-17 23:17:37 +01007035
7036static int __init perf_event_sysfs_init(void)
7037{
7038 struct pmu *pmu;
7039 int ret;
7040
7041 mutex_lock(&pmus_lock);
7042
7043 ret = bus_register(&pmu_bus);
7044 if (ret)
7045 goto unlock;
7046
7047 list_for_each_entry(pmu, &pmus, entry) {
7048 if (!pmu->name || pmu->type < 0)
7049 continue;
7050
7051 ret = pmu_dev_alloc(pmu);
7052 WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret);
7053 }
7054 pmu_bus_running = 1;
7055 ret = 0;
7056
7057unlock:
7058 mutex_unlock(&pmus_lock);
7059
7060 return ret;
7061}
7062device_initcall(perf_event_sysfs_init);
Stephane Eraniane5d13672011-02-14 11:20:01 +02007063
7064#ifdef CONFIG_CGROUP_PERF
7065static struct cgroup_subsys_state *perf_cgroup_create(
7066 struct cgroup_subsys *ss, struct cgroup *cont)
7067{
7068 struct perf_cgroup *jc;
Stephane Eraniane5d13672011-02-14 11:20:01 +02007069
Li Zefan1b15d052011-03-03 14:26:06 +08007070 jc = kzalloc(sizeof(*jc), GFP_KERNEL);
Stephane Eraniane5d13672011-02-14 11:20:01 +02007071 if (!jc)
7072 return ERR_PTR(-ENOMEM);
7073
Stephane Eraniane5d13672011-02-14 11:20:01 +02007074 jc->info = alloc_percpu(struct perf_cgroup_info);
7075 if (!jc->info) {
7076 kfree(jc);
7077 return ERR_PTR(-ENOMEM);
7078 }
7079
Stephane Eraniane5d13672011-02-14 11:20:01 +02007080 return &jc->css;
7081}
7082
7083static void perf_cgroup_destroy(struct cgroup_subsys *ss,
7084 struct cgroup *cont)
7085{
7086 struct perf_cgroup *jc;
7087 jc = container_of(cgroup_subsys_state(cont, perf_subsys_id),
7088 struct perf_cgroup, css);
7089 free_percpu(jc->info);
7090 kfree(jc);
7091}
7092
7093static int __perf_cgroup_move(void *info)
7094{
7095 struct task_struct *task = info;
7096 perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN);
7097 return 0;
7098}
7099
Tejun Heobb9d97b2011-12-12 18:12:21 -08007100static void perf_cgroup_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
7101 struct cgroup_taskset *tset)
Stephane Eraniane5d13672011-02-14 11:20:01 +02007102{
Tejun Heobb9d97b2011-12-12 18:12:21 -08007103 struct task_struct *task;
7104
7105 cgroup_taskset_for_each(task, cgrp, tset)
7106 task_function_call(task, __perf_cgroup_move, task);
Stephane Eraniane5d13672011-02-14 11:20:01 +02007107}
7108
Stephane Eraniane5d13672011-02-14 11:20:01 +02007109static void perf_cgroup_exit(struct cgroup_subsys *ss, struct cgroup *cgrp,
7110 struct cgroup *old_cgrp, struct task_struct *task)
7111{
7112 /*
7113 * cgroup_exit() is called in the copy_process() failure path.
7114 * Ignore this case since the task hasn't ran yet, this avoids
7115 * trying to poke a half freed task state from generic code.
7116 */
7117 if (!(task->flags & PF_EXITING))
7118 return;
7119
Tejun Heobb9d97b2011-12-12 18:12:21 -08007120 task_function_call(task, __perf_cgroup_move, task);
Stephane Eraniane5d13672011-02-14 11:20:01 +02007121}
7122
7123struct cgroup_subsys perf_subsys = {
Ingo Molnare7e7ee22011-05-04 08:42:29 +02007124 .name = "perf_event",
7125 .subsys_id = perf_subsys_id,
7126 .create = perf_cgroup_create,
7127 .destroy = perf_cgroup_destroy,
7128 .exit = perf_cgroup_exit,
Tejun Heobb9d97b2011-12-12 18:12:21 -08007129 .attach = perf_cgroup_attach,
Stephane Eraniane5d13672011-02-14 11:20:01 +02007130};
7131#endif /* CONFIG_CGROUP_PERF */