blob: 0c714226ae0cfdaab73fb8333a9fd676a8f2d295 [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>
5 * Copyright (C) 2008-2009 Red Hat, Inc., Ingo Molnar
6 * Copyright (C) 2008-2009 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
7 * Copyright © 2009 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
8 *
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>
Peter Zijlstra906010b2009-09-21 16:08:49 +020028#include <linux/vmalloc.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020029#include <linux/hardirq.h>
30#include <linux/rculist.h>
31#include <linux/uaccess.h>
32#include <linux/syscalls.h>
33#include <linux/anon_inodes.h>
34#include <linux/kernel_stat.h>
35#include <linux/perf_event.h>
Li Zefan6fb29152009-10-15 11:21:42 +080036#include <linux/ftrace_event.h>
Jason Wessel3c502e72010-11-04 17:33:01 -050037#include <linux/hw_breakpoint.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020038
39#include <asm/irq_regs.h>
40
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010041struct remote_function_call {
42 struct task_struct *p;
43 int (*func)(void *info);
44 void *info;
45 int ret;
46};
47
48static void remote_function(void *data)
49{
50 struct remote_function_call *tfc = data;
51 struct task_struct *p = tfc->p;
52
53 if (p) {
54 tfc->ret = -EAGAIN;
55 if (task_cpu(p) != smp_processor_id() || !task_curr(p))
56 return;
57 }
58
59 tfc->ret = tfc->func(tfc->info);
60}
61
62/**
63 * task_function_call - call a function on the cpu on which a task runs
64 * @p: the task to evaluate
65 * @func: the function to be called
66 * @info: the function call argument
67 *
68 * Calls the function @func when the task is currently running. This might
69 * be on the current CPU, which just calls the function directly
70 *
71 * returns: @func return value, or
72 * -ESRCH - when the process isn't running
73 * -EAGAIN - when the process moved away
74 */
75static int
76task_function_call(struct task_struct *p, int (*func) (void *info), void *info)
77{
78 struct remote_function_call data = {
79 .p = p,
80 .func = func,
81 .info = info,
82 .ret = -ESRCH, /* No such (running) process */
83 };
84
85 if (task_curr(p))
86 smp_call_function_single(task_cpu(p), remote_function, &data, 1);
87
88 return data.ret;
89}
90
91/**
92 * cpu_function_call - call a function on the cpu
93 * @func: the function to be called
94 * @info: the function call argument
95 *
96 * Calls the function @func on the remote cpu.
97 *
98 * returns: @func return value or -ENXIO when the cpu is offline
99 */
100static int cpu_function_call(int cpu, int (*func) (void *info), void *info)
101{
102 struct remote_function_call data = {
103 .p = NULL,
104 .func = func,
105 .info = info,
106 .ret = -ENXIO, /* No such CPU */
107 };
108
109 smp_call_function_single(cpu, remote_function, &data, 1);
110
111 return data.ret;
112}
113
Stephane Eraniane5d13672011-02-14 11:20:01 +0200114#define PERF_FLAG_ALL (PERF_FLAG_FD_NO_GROUP |\
115 PERF_FLAG_FD_OUTPUT |\
116 PERF_FLAG_PID_CGROUP)
117
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200118enum event_type_t {
119 EVENT_FLEXIBLE = 0x1,
120 EVENT_PINNED = 0x2,
121 EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED,
122};
123
Stephane Eraniane5d13672011-02-14 11:20:01 +0200124/*
125 * perf_sched_events : >0 events exist
126 * perf_cgroup_events: >0 per-cpu cgroup events exist on this cpu
127 */
128atomic_t perf_sched_events __read_mostly;
129static DEFINE_PER_CPU(atomic_t, perf_cgroup_events);
130
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200131static atomic_t nr_mmap_events __read_mostly;
132static atomic_t nr_comm_events __read_mostly;
133static atomic_t nr_task_events __read_mostly;
134
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200135static LIST_HEAD(pmus);
136static DEFINE_MUTEX(pmus_lock);
137static struct srcu_struct pmus_srcu;
138
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200139/*
140 * perf event paranoia level:
141 * -1 - not paranoid at all
142 * 0 - disallow raw tracepoint access for unpriv
143 * 1 - disallow cpu events for unpriv
144 * 2 - disallow kernel profiling for unpriv
145 */
146int sysctl_perf_event_paranoid __read_mostly = 1;
147
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200148int sysctl_perf_event_mlock __read_mostly = 512; /* 'free' kb per user */
149
150/*
151 * max perf event sample rate
152 */
Peter Zijlstra163ec432011-02-16 11:22:34 +0100153#define DEFAULT_MAX_SAMPLE_RATE 100000
154int sysctl_perf_event_sample_rate __read_mostly = DEFAULT_MAX_SAMPLE_RATE;
155static int max_samples_per_tick __read_mostly =
156 DIV_ROUND_UP(DEFAULT_MAX_SAMPLE_RATE, HZ);
157
158int perf_proc_update_handler(struct ctl_table *table, int write,
159 void __user *buffer, size_t *lenp,
160 loff_t *ppos)
161{
162 int ret = proc_dointvec(table, write, buffer, lenp, ppos);
163
164 if (ret || !write)
165 return ret;
166
167 max_samples_per_tick = DIV_ROUND_UP(sysctl_perf_event_sample_rate, HZ);
168
169 return 0;
170}
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200171
172static atomic64_t perf_event_id;
173
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200174static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
175 enum event_type_t event_type);
176
177static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +0200178 enum event_type_t event_type,
179 struct task_struct *task);
180
181static void update_context_time(struct perf_event_context *ctx);
182static u64 perf_event_time(struct perf_event *event);
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200183
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200184void __weak perf_event_print_debug(void) { }
185
Matt Fleming84c79912010-10-03 21:41:13 +0100186extern __weak const char *perf_pmu_name(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200187{
Matt Fleming84c79912010-10-03 21:41:13 +0100188 return "pmu";
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200189}
190
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200191static inline u64 perf_clock(void)
192{
193 return local_clock();
194}
195
Stephane Eraniane5d13672011-02-14 11:20:01 +0200196static inline struct perf_cpu_context *
197__get_cpu_context(struct perf_event_context *ctx)
198{
199 return this_cpu_ptr(ctx->pmu->pmu_cpu_context);
200}
201
202#ifdef CONFIG_CGROUP_PERF
203
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200204/*
205 * Must ensure cgroup is pinned (css_get) before calling
206 * this function. In other words, we cannot call this function
207 * if there is no cgroup event for the current CPU context.
208 */
Stephane Eraniane5d13672011-02-14 11:20:01 +0200209static inline struct perf_cgroup *
210perf_cgroup_from_task(struct task_struct *task)
211{
212 return container_of(task_subsys_state(task, perf_subsys_id),
213 struct perf_cgroup, css);
214}
215
216static inline bool
217perf_cgroup_match(struct perf_event *event)
218{
219 struct perf_event_context *ctx = event->ctx;
220 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
221
222 return !event->cgrp || event->cgrp == cpuctx->cgrp;
223}
224
225static inline void perf_get_cgroup(struct perf_event *event)
226{
227 css_get(&event->cgrp->css);
228}
229
230static inline void perf_put_cgroup(struct perf_event *event)
231{
232 css_put(&event->cgrp->css);
233}
234
235static inline void perf_detach_cgroup(struct perf_event *event)
236{
237 perf_put_cgroup(event);
238 event->cgrp = NULL;
239}
240
241static inline int is_cgroup_event(struct perf_event *event)
242{
243 return event->cgrp != NULL;
244}
245
246static inline u64 perf_cgroup_event_time(struct perf_event *event)
247{
248 struct perf_cgroup_info *t;
249
250 t = per_cpu_ptr(event->cgrp->info, event->cpu);
251 return t->time;
252}
253
254static inline void __update_cgrp_time(struct perf_cgroup *cgrp)
255{
256 struct perf_cgroup_info *info;
257 u64 now;
258
259 now = perf_clock();
260
261 info = this_cpu_ptr(cgrp->info);
262
263 info->time += now - info->timestamp;
264 info->timestamp = now;
265}
266
267static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
268{
269 struct perf_cgroup *cgrp_out = cpuctx->cgrp;
270 if (cgrp_out)
271 __update_cgrp_time(cgrp_out);
272}
273
274static inline void update_cgrp_time_from_event(struct perf_event *event)
275{
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200276 struct perf_cgroup *cgrp;
277
Stephane Eraniane5d13672011-02-14 11:20:01 +0200278 /*
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200279 * ensure we access cgroup data only when needed and
280 * when we know the cgroup is pinned (css_get)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200281 */
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200282 if (!is_cgroup_event(event))
Stephane Eraniane5d13672011-02-14 11:20:01 +0200283 return;
284
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200285 cgrp = perf_cgroup_from_task(current);
286 /*
287 * Do not update time when cgroup is not active
288 */
289 if (cgrp == event->cgrp)
290 __update_cgrp_time(event->cgrp);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200291}
292
293static inline void
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200294perf_cgroup_set_timestamp(struct task_struct *task,
295 struct perf_event_context *ctx)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200296{
297 struct perf_cgroup *cgrp;
298 struct perf_cgroup_info *info;
299
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200300 /*
301 * ctx->lock held by caller
302 * ensure we do not access cgroup data
303 * unless we have the cgroup pinned (css_get)
304 */
305 if (!task || !ctx->nr_cgroups)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200306 return;
307
308 cgrp = perf_cgroup_from_task(task);
309 info = this_cpu_ptr(cgrp->info);
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200310 info->timestamp = ctx->timestamp;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200311}
312
313#define PERF_CGROUP_SWOUT 0x1 /* cgroup switch out every event */
314#define PERF_CGROUP_SWIN 0x2 /* cgroup switch in events based on task */
315
316/*
317 * reschedule events based on the cgroup constraint of task.
318 *
319 * mode SWOUT : schedule out everything
320 * mode SWIN : schedule in based on cgroup for next
321 */
322void perf_cgroup_switch(struct task_struct *task, int mode)
323{
324 struct perf_cpu_context *cpuctx;
325 struct pmu *pmu;
326 unsigned long flags;
327
328 /*
329 * disable interrupts to avoid geting nr_cgroup
330 * changes via __perf_event_disable(). Also
331 * avoids preemption.
332 */
333 local_irq_save(flags);
334
335 /*
336 * we reschedule only in the presence of cgroup
337 * constrained events.
338 */
339 rcu_read_lock();
340
341 list_for_each_entry_rcu(pmu, &pmus, entry) {
342
343 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
344
345 perf_pmu_disable(cpuctx->ctx.pmu);
346
347 /*
348 * perf_cgroup_events says at least one
349 * context on this CPU has cgroup events.
350 *
351 * ctx->nr_cgroups reports the number of cgroup
352 * events for a context.
353 */
354 if (cpuctx->ctx.nr_cgroups > 0) {
355
356 if (mode & PERF_CGROUP_SWOUT) {
357 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
358 /*
359 * must not be done before ctxswout due
360 * to event_filter_match() in event_sched_out()
361 */
362 cpuctx->cgrp = NULL;
363 }
364
365 if (mode & PERF_CGROUP_SWIN) {
366 /* set cgrp before ctxsw in to
367 * allow event_filter_match() to not
368 * have to pass task around
369 */
370 cpuctx->cgrp = perf_cgroup_from_task(task);
371 cpu_ctx_sched_in(cpuctx, EVENT_ALL, task);
372 }
373 }
374
375 perf_pmu_enable(cpuctx->ctx.pmu);
376 }
377
378 rcu_read_unlock();
379
380 local_irq_restore(flags);
381}
382
383static inline void perf_cgroup_sched_out(struct task_struct *task)
384{
385 perf_cgroup_switch(task, PERF_CGROUP_SWOUT);
386}
387
388static inline void perf_cgroup_sched_in(struct task_struct *task)
389{
390 perf_cgroup_switch(task, PERF_CGROUP_SWIN);
391}
392
393static inline int perf_cgroup_connect(int fd, struct perf_event *event,
394 struct perf_event_attr *attr,
395 struct perf_event *group_leader)
396{
397 struct perf_cgroup *cgrp;
398 struct cgroup_subsys_state *css;
399 struct file *file;
400 int ret = 0, fput_needed;
401
402 file = fget_light(fd, &fput_needed);
403 if (!file)
404 return -EBADF;
405
406 css = cgroup_css_from_dir(file, perf_subsys_id);
Li Zefan3db272c2011-03-03 14:25:37 +0800407 if (IS_ERR(css)) {
408 ret = PTR_ERR(css);
409 goto out;
410 }
Stephane Eraniane5d13672011-02-14 11:20:01 +0200411
412 cgrp = container_of(css, struct perf_cgroup, css);
413 event->cgrp = cgrp;
414
Li Zefanf75e18c2011-03-03 14:25:50 +0800415 /* must be done before we fput() the file */
416 perf_get_cgroup(event);
417
Stephane Eraniane5d13672011-02-14 11:20:01 +0200418 /*
419 * all events in a group must monitor
420 * the same cgroup because a task belongs
421 * to only one perf cgroup at a time
422 */
423 if (group_leader && group_leader->cgrp != cgrp) {
424 perf_detach_cgroup(event);
425 ret = -EINVAL;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200426 }
Li Zefan3db272c2011-03-03 14:25:37 +0800427out:
Stephane Eraniane5d13672011-02-14 11:20:01 +0200428 fput_light(file, fput_needed);
429 return ret;
430}
431
432static inline void
433perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
434{
435 struct perf_cgroup_info *t;
436 t = per_cpu_ptr(event->cgrp->info, event->cpu);
437 event->shadow_ctx_time = now - t->timestamp;
438}
439
440static inline void
441perf_cgroup_defer_enabled(struct perf_event *event)
442{
443 /*
444 * when the current task's perf cgroup does not match
445 * the event's, we need to remember to call the
446 * perf_mark_enable() function the first time a task with
447 * a matching perf cgroup is scheduled in.
448 */
449 if (is_cgroup_event(event) && !perf_cgroup_match(event))
450 event->cgrp_defer_enabled = 1;
451}
452
453static inline void
454perf_cgroup_mark_enabled(struct perf_event *event,
455 struct perf_event_context *ctx)
456{
457 struct perf_event *sub;
458 u64 tstamp = perf_event_time(event);
459
460 if (!event->cgrp_defer_enabled)
461 return;
462
463 event->cgrp_defer_enabled = 0;
464
465 event->tstamp_enabled = tstamp - event->total_time_enabled;
466 list_for_each_entry(sub, &event->sibling_list, group_entry) {
467 if (sub->state >= PERF_EVENT_STATE_INACTIVE) {
468 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
469 sub->cgrp_defer_enabled = 0;
470 }
471 }
472}
473#else /* !CONFIG_CGROUP_PERF */
474
475static inline bool
476perf_cgroup_match(struct perf_event *event)
477{
478 return true;
479}
480
481static inline void perf_detach_cgroup(struct perf_event *event)
482{}
483
484static inline int is_cgroup_event(struct perf_event *event)
485{
486 return 0;
487}
488
489static inline u64 perf_cgroup_event_cgrp_time(struct perf_event *event)
490{
491 return 0;
492}
493
494static inline void update_cgrp_time_from_event(struct perf_event *event)
495{
496}
497
498static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
499{
500}
501
502static inline void perf_cgroup_sched_out(struct task_struct *task)
503{
504}
505
506static inline void perf_cgroup_sched_in(struct task_struct *task)
507{
508}
509
510static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event,
511 struct perf_event_attr *attr,
512 struct perf_event *group_leader)
513{
514 return -EINVAL;
515}
516
517static inline void
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200518perf_cgroup_set_timestamp(struct task_struct *task,
519 struct perf_event_context *ctx)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200520{
521}
522
523void
524perf_cgroup_switch(struct task_struct *task, struct task_struct *next)
525{
526}
527
528static inline void
529perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
530{
531}
532
533static inline u64 perf_cgroup_event_time(struct perf_event *event)
534{
535 return 0;
536}
537
538static inline void
539perf_cgroup_defer_enabled(struct perf_event *event)
540{
541}
542
543static inline void
544perf_cgroup_mark_enabled(struct perf_event *event,
545 struct perf_event_context *ctx)
546{
547}
548#endif
549
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200550void perf_pmu_disable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200551{
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200552 int *count = this_cpu_ptr(pmu->pmu_disable_count);
553 if (!(*count)++)
554 pmu->pmu_disable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200555}
556
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200557void perf_pmu_enable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200558{
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200559 int *count = this_cpu_ptr(pmu->pmu_disable_count);
560 if (!--(*count))
561 pmu->pmu_enable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200562}
563
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200564static DEFINE_PER_CPU(struct list_head, rotation_list);
565
566/*
567 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
568 * because they're strictly cpu affine and rotate_start is called with IRQs
569 * disabled, while rotate_context is called from IRQ context.
570 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200571static void perf_pmu_rotate_start(struct pmu *pmu)
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200572{
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200573 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200574 struct list_head *head = &__get_cpu_var(rotation_list);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200575
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200576 WARN_ON(!irqs_disabled());
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200577
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200578 if (list_empty(&cpuctx->rotation_list))
579 list_add(&cpuctx->rotation_list, head);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200580}
581
582static void get_ctx(struct perf_event_context *ctx)
583{
584 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
585}
586
587static void free_ctx(struct rcu_head *head)
588{
589 struct perf_event_context *ctx;
590
591 ctx = container_of(head, struct perf_event_context, rcu_head);
592 kfree(ctx);
593}
594
595static void put_ctx(struct perf_event_context *ctx)
596{
597 if (atomic_dec_and_test(&ctx->refcount)) {
598 if (ctx->parent_ctx)
599 put_ctx(ctx->parent_ctx);
600 if (ctx->task)
601 put_task_struct(ctx->task);
602 call_rcu(&ctx->rcu_head, free_ctx);
603 }
604}
605
606static void unclone_ctx(struct perf_event_context *ctx)
607{
608 if (ctx->parent_ctx) {
609 put_ctx(ctx->parent_ctx);
610 ctx->parent_ctx = NULL;
611 }
612}
613
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -0200614static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
615{
616 /*
617 * only top level events have the pid namespace they were created in
618 */
619 if (event->parent)
620 event = event->parent;
621
622 return task_tgid_nr_ns(p, event->ns);
623}
624
625static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
626{
627 /*
628 * only top level events have the pid namespace they were created in
629 */
630 if (event->parent)
631 event = event->parent;
632
633 return task_pid_nr_ns(p, event->ns);
634}
635
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200636/*
637 * If we inherit events we want to return the parent event id
638 * to userspace.
639 */
640static u64 primary_event_id(struct perf_event *event)
641{
642 u64 id = event->id;
643
644 if (event->parent)
645 id = event->parent->id;
646
647 return id;
648}
649
650/*
651 * Get the perf_event_context for a task and lock it.
652 * This has to cope with with the fact that until it is locked,
653 * the context could get moved to another task.
654 */
655static struct perf_event_context *
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200656perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200657{
658 struct perf_event_context *ctx;
659
660 rcu_read_lock();
Peter Zijlstra9ed60602010-06-11 17:36:35 +0200661retry:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200662 ctx = rcu_dereference(task->perf_event_ctxp[ctxn]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200663 if (ctx) {
664 /*
665 * If this context is a clone of another, it might
666 * get swapped for another underneath us by
667 * perf_event_task_sched_out, though the
668 * rcu_read_lock() protects us from any context
669 * getting freed. Lock the context and check if it
670 * got swapped before we could get the lock, and retry
671 * if so. If we locked the right context, then it
672 * can't get swapped on us any more.
673 */
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100674 raw_spin_lock_irqsave(&ctx->lock, *flags);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200675 if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) {
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100676 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200677 goto retry;
678 }
679
680 if (!atomic_inc_not_zero(&ctx->refcount)) {
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100681 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200682 ctx = NULL;
683 }
684 }
685 rcu_read_unlock();
686 return ctx;
687}
688
689/*
690 * Get the context for a task and increment its pin_count so it
691 * can't get swapped to another task. This also increments its
692 * reference count so that the context can't get freed.
693 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200694static struct perf_event_context *
695perf_pin_task_context(struct task_struct *task, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200696{
697 struct perf_event_context *ctx;
698 unsigned long flags;
699
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200700 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200701 if (ctx) {
702 ++ctx->pin_count;
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100703 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200704 }
705 return ctx;
706}
707
708static void perf_unpin_context(struct perf_event_context *ctx)
709{
710 unsigned long flags;
711
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100712 raw_spin_lock_irqsave(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200713 --ctx->pin_count;
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100714 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200715}
716
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100717/*
718 * Update the record of the current time in a context.
719 */
720static void update_context_time(struct perf_event_context *ctx)
721{
722 u64 now = perf_clock();
723
724 ctx->time += now - ctx->timestamp;
725 ctx->timestamp = now;
726}
727
Stephane Eranian41587552011-01-03 18:20:01 +0200728static u64 perf_event_time(struct perf_event *event)
729{
730 struct perf_event_context *ctx = event->ctx;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200731
732 if (is_cgroup_event(event))
733 return perf_cgroup_event_time(event);
734
Stephane Eranian41587552011-01-03 18:20:01 +0200735 return ctx ? ctx->time : 0;
736}
737
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100738/*
739 * Update the total_time_enabled and total_time_running fields for a event.
740 */
741static void update_event_times(struct perf_event *event)
742{
743 struct perf_event_context *ctx = event->ctx;
744 u64 run_end;
745
746 if (event->state < PERF_EVENT_STATE_INACTIVE ||
747 event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
748 return;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200749 /*
750 * in cgroup mode, time_enabled represents
751 * the time the event was enabled AND active
752 * tasks were in the monitored cgroup. This is
753 * independent of the activity of the context as
754 * there may be a mix of cgroup and non-cgroup events.
755 *
756 * That is why we treat cgroup events differently
757 * here.
758 */
759 if (is_cgroup_event(event))
Stephane Eranian41587552011-01-03 18:20:01 +0200760 run_end = perf_event_time(event);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200761 else if (ctx->is_active)
762 run_end = ctx->time;
Peter Zijlstraacd1d7c2009-11-23 15:00:36 +0100763 else
764 run_end = event->tstamp_stopped;
765
766 event->total_time_enabled = run_end - event->tstamp_enabled;
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100767
768 if (event->state == PERF_EVENT_STATE_INACTIVE)
769 run_end = event->tstamp_stopped;
770 else
Stephane Eranian41587552011-01-03 18:20:01 +0200771 run_end = perf_event_time(event);
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100772
773 event->total_time_running = run_end - event->tstamp_running;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200774
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100775}
776
Peter Zijlstra96c21a42010-05-11 16:19:10 +0200777/*
778 * Update total_time_enabled and total_time_running for all events in a group.
779 */
780static void update_group_times(struct perf_event *leader)
781{
782 struct perf_event *event;
783
784 update_event_times(leader);
785 list_for_each_entry(event, &leader->sibling_list, group_entry)
786 update_event_times(event);
787}
788
Frederic Weisbecker889ff012010-01-09 20:04:47 +0100789static struct list_head *
790ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
791{
792 if (event->attr.pinned)
793 return &ctx->pinned_groups;
794 else
795 return &ctx->flexible_groups;
796}
797
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200798/*
799 * Add a event from the lists for its context.
800 * Must be called with ctx->mutex and ctx->lock held.
801 */
802static void
803list_add_event(struct perf_event *event, struct perf_event_context *ctx)
804{
Peter Zijlstra8a495422010-05-27 15:47:49 +0200805 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
806 event->attach_state |= PERF_ATTACH_CONTEXT;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200807
808 /*
Peter Zijlstra8a495422010-05-27 15:47:49 +0200809 * If we're a stand alone event or group leader, we go to the context
810 * list, group events are kept attached to the group so that
811 * perf_group_detach can, at all times, locate all siblings.
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200812 */
Peter Zijlstra8a495422010-05-27 15:47:49 +0200813 if (event->group_leader == event) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +0100814 struct list_head *list;
815
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +0100816 if (is_software_event(event))
817 event->group_flags |= PERF_GROUP_SOFTWARE;
818
Frederic Weisbecker889ff012010-01-09 20:04:47 +0100819 list = ctx_group_list(event, ctx);
820 list_add_tail(&event->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200821 }
822
Peter Zijlstra08309372011-03-03 11:31:20 +0100823 if (is_cgroup_event(event))
Stephane Eraniane5d13672011-02-14 11:20:01 +0200824 ctx->nr_cgroups++;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200825
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200826 list_add_rcu(&event->event_entry, &ctx->event_list);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200827 if (!ctx->nr_events)
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200828 perf_pmu_rotate_start(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200829 ctx->nr_events++;
830 if (event->attr.inherit_stat)
831 ctx->nr_stat++;
832}
833
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -0200834/*
835 * Called at perf_event creation and when events are attached/detached from a
836 * group.
837 */
838static void perf_event__read_size(struct perf_event *event)
839{
840 int entry = sizeof(u64); /* value */
841 int size = 0;
842 int nr = 1;
843
844 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
845 size += sizeof(u64);
846
847 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
848 size += sizeof(u64);
849
850 if (event->attr.read_format & PERF_FORMAT_ID)
851 entry += sizeof(u64);
852
853 if (event->attr.read_format & PERF_FORMAT_GROUP) {
854 nr += event->group_leader->nr_siblings;
855 size += sizeof(u64);
856 }
857
858 size += entry * nr;
859 event->read_size = size;
860}
861
862static void perf_event__header_size(struct perf_event *event)
863{
864 struct perf_sample_data *data;
865 u64 sample_type = event->attr.sample_type;
866 u16 size = 0;
867
868 perf_event__read_size(event);
869
870 if (sample_type & PERF_SAMPLE_IP)
871 size += sizeof(data->ip);
872
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -0200873 if (sample_type & PERF_SAMPLE_ADDR)
874 size += sizeof(data->addr);
875
876 if (sample_type & PERF_SAMPLE_PERIOD)
877 size += sizeof(data->period);
878
879 if (sample_type & PERF_SAMPLE_READ)
880 size += event->read_size;
881
882 event->header_size = size;
883}
884
885static void perf_event__id_header_size(struct perf_event *event)
886{
887 struct perf_sample_data *data;
888 u64 sample_type = event->attr.sample_type;
889 u16 size = 0;
890
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -0200891 if (sample_type & PERF_SAMPLE_TID)
892 size += sizeof(data->tid_entry);
893
894 if (sample_type & PERF_SAMPLE_TIME)
895 size += sizeof(data->time);
896
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -0200897 if (sample_type & PERF_SAMPLE_ID)
898 size += sizeof(data->id);
899
900 if (sample_type & PERF_SAMPLE_STREAM_ID)
901 size += sizeof(data->stream_id);
902
903 if (sample_type & PERF_SAMPLE_CPU)
904 size += sizeof(data->cpu_entry);
905
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -0200906 event->id_header_size = size;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -0200907}
908
Peter Zijlstra8a495422010-05-27 15:47:49 +0200909static void perf_group_attach(struct perf_event *event)
910{
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -0200911 struct perf_event *group_leader = event->group_leader, *pos;
Peter Zijlstra8a495422010-05-27 15:47:49 +0200912
Peter Zijlstra74c33372010-10-15 11:40:29 +0200913 /*
914 * We can have double attach due to group movement in perf_event_open.
915 */
916 if (event->attach_state & PERF_ATTACH_GROUP)
917 return;
918
Peter Zijlstra8a495422010-05-27 15:47:49 +0200919 event->attach_state |= PERF_ATTACH_GROUP;
920
921 if (group_leader == event)
922 return;
923
924 if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
925 !is_software_event(event))
926 group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
927
928 list_add_tail(&event->group_entry, &group_leader->sibling_list);
929 group_leader->nr_siblings++;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -0200930
931 perf_event__header_size(group_leader);
932
933 list_for_each_entry(pos, &group_leader->sibling_list, group_entry)
934 perf_event__header_size(pos);
Peter Zijlstra8a495422010-05-27 15:47:49 +0200935}
936
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200937/*
938 * Remove a event from the lists for its context.
939 * Must be called with ctx->mutex and ctx->lock held.
940 */
941static void
942list_del_event(struct perf_event *event, struct perf_event_context *ctx)
943{
Stephane Eranian68cacd22011-03-23 16:03:06 +0100944 struct perf_cpu_context *cpuctx;
Peter Zijlstra8a495422010-05-27 15:47:49 +0200945 /*
946 * We can have double detach due to exit/hot-unplug + close.
947 */
948 if (!(event->attach_state & PERF_ATTACH_CONTEXT))
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200949 return;
Peter Zijlstra8a495422010-05-27 15:47:49 +0200950
951 event->attach_state &= ~PERF_ATTACH_CONTEXT;
952
Stephane Eranian68cacd22011-03-23 16:03:06 +0100953 if (is_cgroup_event(event)) {
Stephane Eraniane5d13672011-02-14 11:20:01 +0200954 ctx->nr_cgroups--;
Stephane Eranian68cacd22011-03-23 16:03:06 +0100955 cpuctx = __get_cpu_context(ctx);
956 /*
957 * if there are no more cgroup events
958 * then cler cgrp to avoid stale pointer
959 * in update_cgrp_time_from_cpuctx()
960 */
961 if (!ctx->nr_cgroups)
962 cpuctx->cgrp = NULL;
963 }
Stephane Eraniane5d13672011-02-14 11:20:01 +0200964
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200965 ctx->nr_events--;
966 if (event->attr.inherit_stat)
967 ctx->nr_stat--;
968
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200969 list_del_rcu(&event->event_entry);
970
Peter Zijlstra8a495422010-05-27 15:47:49 +0200971 if (event->group_leader == event)
972 list_del_init(&event->group_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200973
Peter Zijlstra96c21a42010-05-11 16:19:10 +0200974 update_group_times(event);
Stephane Eranianb2e74a22009-11-26 09:24:30 -0800975
976 /*
977 * If event was in error state, then keep it
978 * that way, otherwise bogus counts will be
979 * returned on read(). The only way to get out
980 * of error state is by explicit re-enabling
981 * of the event
982 */
983 if (event->state > PERF_EVENT_STATE_OFF)
984 event->state = PERF_EVENT_STATE_OFF;
Peter Zijlstra050735b2010-05-11 11:51:53 +0200985}
986
Peter Zijlstra8a495422010-05-27 15:47:49 +0200987static void perf_group_detach(struct perf_event *event)
Peter Zijlstra050735b2010-05-11 11:51:53 +0200988{
989 struct perf_event *sibling, *tmp;
Peter Zijlstra8a495422010-05-27 15:47:49 +0200990 struct list_head *list = NULL;
991
992 /*
993 * We can have double detach due to exit/hot-unplug + close.
994 */
995 if (!(event->attach_state & PERF_ATTACH_GROUP))
996 return;
997
998 event->attach_state &= ~PERF_ATTACH_GROUP;
999
1000 /*
1001 * If this is a sibling, remove it from its group.
1002 */
1003 if (event->group_leader != event) {
1004 list_del_init(&event->group_entry);
1005 event->group_leader->nr_siblings--;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001006 goto out;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001007 }
1008
1009 if (!list_empty(&event->group_entry))
1010 list = &event->group_entry;
Peter Zijlstra2e2af502009-11-23 11:37:25 +01001011
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001012 /*
1013 * If this was a group event with sibling events then
1014 * upgrade the siblings to singleton events by adding them
Peter Zijlstra8a495422010-05-27 15:47:49 +02001015 * to whatever list we are on.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001016 */
1017 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
Peter Zijlstra8a495422010-05-27 15:47:49 +02001018 if (list)
1019 list_move_tail(&sibling->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001020 sibling->group_leader = sibling;
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01001021
1022 /* Inherit group flags from the previous leader */
1023 sibling->group_flags = event->group_flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001024 }
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001025
1026out:
1027 perf_event__header_size(event->group_leader);
1028
1029 list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry)
1030 perf_event__header_size(tmp);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001031}
1032
Stephane Eranianfa66f072010-08-26 16:40:01 +02001033static inline int
1034event_filter_match(struct perf_event *event)
1035{
Stephane Eraniane5d13672011-02-14 11:20:01 +02001036 return (event->cpu == -1 || event->cpu == smp_processor_id())
1037 && perf_cgroup_match(event);
Stephane Eranianfa66f072010-08-26 16:40:01 +02001038}
1039
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001040static void
1041event_sched_out(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001042 struct perf_cpu_context *cpuctx,
1043 struct perf_event_context *ctx)
1044{
Stephane Eranian41587552011-01-03 18:20:01 +02001045 u64 tstamp = perf_event_time(event);
Stephane Eranianfa66f072010-08-26 16:40:01 +02001046 u64 delta;
1047 /*
1048 * An event which could not be activated because of
1049 * filter mismatch still needs to have its timings
1050 * maintained, otherwise bogus information is return
1051 * via read() for time_enabled, time_running:
1052 */
1053 if (event->state == PERF_EVENT_STATE_INACTIVE
1054 && !event_filter_match(event)) {
Stephane Eraniane5d13672011-02-14 11:20:01 +02001055 delta = tstamp - event->tstamp_stopped;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001056 event->tstamp_running += delta;
Stephane Eranian41587552011-01-03 18:20:01 +02001057 event->tstamp_stopped = tstamp;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001058 }
1059
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001060 if (event->state != PERF_EVENT_STATE_ACTIVE)
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001061 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001062
1063 event->state = PERF_EVENT_STATE_INACTIVE;
1064 if (event->pending_disable) {
1065 event->pending_disable = 0;
1066 event->state = PERF_EVENT_STATE_OFF;
1067 }
Stephane Eranian41587552011-01-03 18:20:01 +02001068 event->tstamp_stopped = tstamp;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001069 event->pmu->del(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001070 event->oncpu = -1;
1071
1072 if (!is_software_event(event))
1073 cpuctx->active_oncpu--;
1074 ctx->nr_active--;
1075 if (event->attr.exclusive || !cpuctx->active_oncpu)
1076 cpuctx->exclusive = 0;
1077}
1078
1079static void
1080group_sched_out(struct perf_event *group_event,
1081 struct perf_cpu_context *cpuctx,
1082 struct perf_event_context *ctx)
1083{
1084 struct perf_event *event;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001085 int state = group_event->state;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001086
1087 event_sched_out(group_event, cpuctx, ctx);
1088
1089 /*
1090 * Schedule out siblings (if any):
1091 */
1092 list_for_each_entry(event, &group_event->sibling_list, group_entry)
1093 event_sched_out(event, cpuctx, ctx);
1094
Stephane Eranianfa66f072010-08-26 16:40:01 +02001095 if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001096 cpuctx->exclusive = 0;
1097}
1098
1099/*
1100 * Cross CPU call to remove a performance event
1101 *
1102 * We disable the event on the hardware level first. After that we
1103 * remove it from the context list.
1104 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001105static int __perf_remove_from_context(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001106{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001107 struct perf_event *event = info;
1108 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001109 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001110
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001111 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001112 event_sched_out(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001113 list_del_event(event, ctx);
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001114 raw_spin_unlock(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001115
1116 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001117}
1118
1119
1120/*
1121 * Remove the event from a task's (or a CPU's) list of events.
1122 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001123 * CPU events are removed with a smp call. For task events we only
1124 * call when the task is on a CPU.
1125 *
1126 * If event->ctx is a cloned context, callers must make sure that
1127 * every task struct that event->ctx->task could possibly point to
1128 * remains valid. This is OK when called from perf_release since
1129 * that only calls us on the top-level context, which can't be a clone.
1130 * When called from perf_event_exit_task, it's OK because the
1131 * context has been detached from its task.
1132 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001133static void perf_remove_from_context(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001134{
1135 struct perf_event_context *ctx = event->ctx;
1136 struct task_struct *task = ctx->task;
1137
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001138 lockdep_assert_held(&ctx->mutex);
1139
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001140 if (!task) {
1141 /*
1142 * Per cpu events are removed via an smp call and
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001143 * the removal is always successful.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001144 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001145 cpu_function_call(event->cpu, __perf_remove_from_context, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001146 return;
1147 }
1148
1149retry:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001150 if (!task_function_call(task, __perf_remove_from_context, event))
1151 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001152
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001153 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001154 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001155 * If we failed to find a running task, but find the context active now
1156 * that we've acquired the ctx->lock, retry.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001157 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001158 if (ctx->is_active) {
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001159 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001160 goto retry;
1161 }
1162
1163 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001164 * Since the task isn't running, its safe to remove the event, us
1165 * holding the ctx->lock ensures the task won't get scheduled in.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001166 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001167 list_del_event(event, ctx);
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001168 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001169}
1170
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001171/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001172 * Cross CPU call to disable a performance event
1173 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001174static int __perf_event_disable(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001175{
1176 struct perf_event *event = info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001177 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001178 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001179
1180 /*
1181 * If this is a per-task event, need to check whether this
1182 * event's task is the current task on this cpu.
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001183 *
1184 * Can trigger due to concurrent perf_event_context_sched_out()
1185 * flipping contexts around.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001186 */
1187 if (ctx->task && cpuctx->task_ctx != ctx)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001188 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001189
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001190 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001191
1192 /*
1193 * If the event is on, turn it off.
1194 * If it is in error state, leave it in error state.
1195 */
1196 if (event->state >= PERF_EVENT_STATE_INACTIVE) {
1197 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001198 update_cgrp_time_from_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001199 update_group_times(event);
1200 if (event == event->group_leader)
1201 group_sched_out(event, cpuctx, ctx);
1202 else
1203 event_sched_out(event, cpuctx, ctx);
1204 event->state = PERF_EVENT_STATE_OFF;
1205 }
1206
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001207 raw_spin_unlock(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001208
1209 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001210}
1211
1212/*
1213 * Disable a event.
1214 *
1215 * If event->ctx is a cloned context, callers must make sure that
1216 * every task struct that event->ctx->task could possibly point to
1217 * remains valid. This condition is satisifed when called through
1218 * perf_event_for_each_child or perf_event_for_each because they
1219 * hold the top-level event's child_mutex, so any descendant that
1220 * goes to exit will block in sync_child_event.
1221 * When called from perf_pending_event it's OK because event->ctx
1222 * is the current context on this CPU and preemption is disabled,
1223 * hence we can't get into perf_event_task_sched_out for this context.
1224 */
Frederic Weisbecker44234ad2009-12-09 09:25:48 +01001225void perf_event_disable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001226{
1227 struct perf_event_context *ctx = event->ctx;
1228 struct task_struct *task = ctx->task;
1229
1230 if (!task) {
1231 /*
1232 * Disable the event on the cpu that it's on
1233 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001234 cpu_function_call(event->cpu, __perf_event_disable, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001235 return;
1236 }
1237
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001238retry:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001239 if (!task_function_call(task, __perf_event_disable, event))
1240 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001241
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001242 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001243 /*
1244 * If the event is still active, we need to retry the cross-call.
1245 */
1246 if (event->state == PERF_EVENT_STATE_ACTIVE) {
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001247 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001248 /*
1249 * Reload the task pointer, it might have been changed by
1250 * a concurrent perf_event_context_sched_out().
1251 */
1252 task = ctx->task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001253 goto retry;
1254 }
1255
1256 /*
1257 * Since we have the lock this context can't be scheduled
1258 * in, so we can change the state safely.
1259 */
1260 if (event->state == PERF_EVENT_STATE_INACTIVE) {
1261 update_group_times(event);
1262 event->state = PERF_EVENT_STATE_OFF;
1263 }
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001264 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001265}
1266
Stephane Eraniane5d13672011-02-14 11:20:01 +02001267static void perf_set_shadow_time(struct perf_event *event,
1268 struct perf_event_context *ctx,
1269 u64 tstamp)
1270{
1271 /*
1272 * use the correct time source for the time snapshot
1273 *
1274 * We could get by without this by leveraging the
1275 * fact that to get to this function, the caller
1276 * has most likely already called update_context_time()
1277 * and update_cgrp_time_xx() and thus both timestamp
1278 * are identical (or very close). Given that tstamp is,
1279 * already adjusted for cgroup, we could say that:
1280 * tstamp - ctx->timestamp
1281 * is equivalent to
1282 * tstamp - cgrp->timestamp.
1283 *
1284 * Then, in perf_output_read(), the calculation would
1285 * work with no changes because:
1286 * - event is guaranteed scheduled in
1287 * - no scheduled out in between
1288 * - thus the timestamp would be the same
1289 *
1290 * But this is a bit hairy.
1291 *
1292 * So instead, we have an explicit cgroup call to remain
1293 * within the time time source all along. We believe it
1294 * is cleaner and simpler to understand.
1295 */
1296 if (is_cgroup_event(event))
1297 perf_cgroup_set_shadow_time(event, tstamp);
1298 else
1299 event->shadow_ctx_time = tstamp - ctx->timestamp;
1300}
1301
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01001302#define MAX_INTERRUPTS (~0ULL)
1303
1304static void perf_log_throttle(struct perf_event *event, int enable);
1305
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001306static int
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001307event_sched_in(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001308 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001309 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001310{
Stephane Eranian41587552011-01-03 18:20:01 +02001311 u64 tstamp = perf_event_time(event);
1312
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001313 if (event->state <= PERF_EVENT_STATE_OFF)
1314 return 0;
1315
1316 event->state = PERF_EVENT_STATE_ACTIVE;
Peter Zijlstra6e377382010-02-11 13:21:58 +01001317 event->oncpu = smp_processor_id();
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01001318
1319 /*
1320 * Unthrottle events, since we scheduled we might have missed several
1321 * ticks already, also for a heavily scheduling task there is little
1322 * guarantee it'll get a tick in a timely manner.
1323 */
1324 if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) {
1325 perf_log_throttle(event, 1);
1326 event->hw.interrupts = 0;
1327 }
1328
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001329 /*
1330 * The new state must be visible before we turn it on in the hardware:
1331 */
1332 smp_wmb();
1333
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001334 if (event->pmu->add(event, PERF_EF_START)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001335 event->state = PERF_EVENT_STATE_INACTIVE;
1336 event->oncpu = -1;
1337 return -EAGAIN;
1338 }
1339
Stephane Eranian41587552011-01-03 18:20:01 +02001340 event->tstamp_running += tstamp - event->tstamp_stopped;
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001341
Stephane Eraniane5d13672011-02-14 11:20:01 +02001342 perf_set_shadow_time(event, ctx, tstamp);
Stephane Eranianeed01522010-10-26 16:08:01 +02001343
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001344 if (!is_software_event(event))
1345 cpuctx->active_oncpu++;
1346 ctx->nr_active++;
1347
1348 if (event->attr.exclusive)
1349 cpuctx->exclusive = 1;
1350
1351 return 0;
1352}
1353
1354static int
1355group_sched_in(struct perf_event *group_event,
1356 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001357 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001358{
Lin Ming6bde9b62010-04-23 13:56:00 +08001359 struct perf_event *event, *partial_group = NULL;
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02001360 struct pmu *pmu = group_event->pmu;
Stephane Eraniand7842da2010-10-20 15:25:01 +02001361 u64 now = ctx->time;
1362 bool simulate = false;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001363
1364 if (group_event->state == PERF_EVENT_STATE_OFF)
1365 return 0;
1366
Peter Zijlstraad5133b2010-06-15 12:22:39 +02001367 pmu->start_txn(pmu);
Lin Ming6bde9b62010-04-23 13:56:00 +08001368
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001369 if (event_sched_in(group_event, cpuctx, ctx)) {
Peter Zijlstraad5133b2010-06-15 12:22:39 +02001370 pmu->cancel_txn(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001371 return -EAGAIN;
Stephane Eranian90151c32010-05-25 16:23:10 +02001372 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001373
1374 /*
1375 * Schedule in siblings as one group (if any):
1376 */
1377 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001378 if (event_sched_in(event, cpuctx, ctx)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001379 partial_group = event;
1380 goto group_error;
1381 }
1382 }
1383
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001384 if (!pmu->commit_txn(pmu))
Paul Mackerras6e851582010-05-08 20:58:00 +10001385 return 0;
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001386
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001387group_error:
1388 /*
1389 * Groups can be scheduled in as one unit only, so undo any
1390 * partial group before returning:
Stephane Eraniand7842da2010-10-20 15:25:01 +02001391 * The events up to the failed event are scheduled out normally,
1392 * tstamp_stopped will be updated.
1393 *
1394 * The failed events and the remaining siblings need to have
1395 * their timings updated as if they had gone thru event_sched_in()
1396 * and event_sched_out(). This is required to get consistent timings
1397 * across the group. This also takes care of the case where the group
1398 * could never be scheduled by ensuring tstamp_stopped is set to mark
1399 * the time the event was actually stopped, such that time delta
1400 * calculation in update_event_times() is correct.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001401 */
1402 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
1403 if (event == partial_group)
Stephane Eraniand7842da2010-10-20 15:25:01 +02001404 simulate = true;
1405
1406 if (simulate) {
1407 event->tstamp_running += now - event->tstamp_stopped;
1408 event->tstamp_stopped = now;
1409 } else {
1410 event_sched_out(event, cpuctx, ctx);
1411 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001412 }
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001413 event_sched_out(group_event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001414
Peter Zijlstraad5133b2010-06-15 12:22:39 +02001415 pmu->cancel_txn(pmu);
Stephane Eranian90151c32010-05-25 16:23:10 +02001416
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001417 return -EAGAIN;
1418}
1419
1420/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001421 * Work out whether we can put this event group on the CPU now.
1422 */
1423static int group_can_go_on(struct perf_event *event,
1424 struct perf_cpu_context *cpuctx,
1425 int can_add_hw)
1426{
1427 /*
1428 * Groups consisting entirely of software events can always go on.
1429 */
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01001430 if (event->group_flags & PERF_GROUP_SOFTWARE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001431 return 1;
1432 /*
1433 * If an exclusive group is already on, no other hardware
1434 * events can go on.
1435 */
1436 if (cpuctx->exclusive)
1437 return 0;
1438 /*
1439 * If this group is exclusive and there are already
1440 * events on the CPU, it can't go on.
1441 */
1442 if (event->attr.exclusive && cpuctx->active_oncpu)
1443 return 0;
1444 /*
1445 * Otherwise, try to add it if all previous groups were able
1446 * to go on.
1447 */
1448 return can_add_hw;
1449}
1450
1451static void add_event_to_ctx(struct perf_event *event,
1452 struct perf_event_context *ctx)
1453{
Stephane Eranian41587552011-01-03 18:20:01 +02001454 u64 tstamp = perf_event_time(event);
1455
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001456 list_add_event(event, ctx);
Peter Zijlstra8a495422010-05-27 15:47:49 +02001457 perf_group_attach(event);
Stephane Eranian41587552011-01-03 18:20:01 +02001458 event->tstamp_enabled = tstamp;
1459 event->tstamp_running = tstamp;
1460 event->tstamp_stopped = tstamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001461}
1462
Stephane Eraniane5d13672011-02-14 11:20:01 +02001463static void perf_event_context_sched_in(struct perf_event_context *ctx,
1464 struct task_struct *tsk);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001465
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001466/*
1467 * Cross CPU call to install and enable a performance event
1468 *
1469 * Must be called with ctx->mutex held
1470 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001471static int __perf_install_in_context(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001472{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001473 struct perf_event *event = info;
1474 struct perf_event_context *ctx = event->ctx;
1475 struct perf_event *leader = event->group_leader;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001476 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001477 int err;
1478
1479 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001480 * In case we're installing a new context to an already running task,
1481 * could also happen before perf_event_task_sched_in() on architectures
1482 * which do context switches with IRQs enabled.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001483 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001484 if (ctx->task && !cpuctx->task_ctx)
Stephane Eraniane5d13672011-02-14 11:20:01 +02001485 perf_event_context_sched_in(ctx, ctx->task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001486
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001487 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001488 ctx->is_active = 1;
1489 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001490 /*
1491 * update cgrp time only if current cgrp
1492 * matches event->cgrp. Must be done before
1493 * calling add_event_to_ctx()
1494 */
1495 update_cgrp_time_from_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001496
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001497 add_event_to_ctx(event, ctx);
1498
Stephane Eranian5632ab12011-01-03 18:20:01 +02001499 if (!event_filter_match(event))
Peter Zijlstraf4c41762009-12-16 17:55:54 +01001500 goto unlock;
1501
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001502 /*
1503 * Don't put the event on if it is disabled or if
1504 * it is in a group and the group isn't on.
1505 */
1506 if (event->state != PERF_EVENT_STATE_INACTIVE ||
1507 (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE))
1508 goto unlock;
1509
1510 /*
1511 * An exclusive event can't go on if there are already active
1512 * hardware events, and no hardware event can go on if there
1513 * is already an exclusive event on.
1514 */
1515 if (!group_can_go_on(event, cpuctx, 1))
1516 err = -EEXIST;
1517 else
Peter Zijlstra6e377382010-02-11 13:21:58 +01001518 err = event_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001519
1520 if (err) {
1521 /*
1522 * This event couldn't go on. If it is in a group
1523 * then we have to pull the whole group off.
1524 * If the event group is pinned then put it in error state.
1525 */
1526 if (leader != event)
1527 group_sched_out(leader, cpuctx, ctx);
1528 if (leader->attr.pinned) {
1529 update_group_times(leader);
1530 leader->state = PERF_EVENT_STATE_ERROR;
1531 }
1532 }
1533
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001534unlock:
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001535 raw_spin_unlock(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001536
1537 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001538}
1539
1540/*
1541 * Attach a performance event to a context
1542 *
1543 * First we add the event to the list with the hardware enable bit
1544 * in event->hw_config cleared.
1545 *
1546 * If the event is attached to a task which is on a CPU we use a smp
1547 * call to enable it in the task context. The task might have been
1548 * scheduled away, but we check this in the smp call again.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001549 */
1550static void
1551perf_install_in_context(struct perf_event_context *ctx,
1552 struct perf_event *event,
1553 int cpu)
1554{
1555 struct task_struct *task = ctx->task;
1556
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001557 lockdep_assert_held(&ctx->mutex);
1558
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02001559 event->ctx = ctx;
1560
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001561 if (!task) {
1562 /*
1563 * Per cpu events are installed via an smp call and
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001564 * the install is always successful.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001565 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001566 cpu_function_call(cpu, __perf_install_in_context, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001567 return;
1568 }
1569
1570retry:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001571 if (!task_function_call(task, __perf_install_in_context, event))
1572 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001573
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001574 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001575 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001576 * If we failed to find a running task, but find the context active now
1577 * that we've acquired the ctx->lock, retry.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001578 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001579 if (ctx->is_active) {
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001580 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001581 goto retry;
1582 }
1583
1584 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001585 * Since the task isn't running, its safe to add the event, us holding
1586 * the ctx->lock ensures the task won't get scheduled in.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001587 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001588 add_event_to_ctx(event, ctx);
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001589 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001590}
1591
1592/*
1593 * Put a event into inactive state and update time fields.
1594 * Enabling the leader of a group effectively enables all
1595 * the group members that aren't explicitly disabled, so we
1596 * have to update their ->tstamp_enabled also.
1597 * Note: this works for group members as well as group leaders
1598 * since the non-leader members' sibling_lists will be empty.
1599 */
1600static void __perf_event_mark_enabled(struct perf_event *event,
1601 struct perf_event_context *ctx)
1602{
1603 struct perf_event *sub;
Stephane Eranian41587552011-01-03 18:20:01 +02001604 u64 tstamp = perf_event_time(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001605
1606 event->state = PERF_EVENT_STATE_INACTIVE;
Stephane Eranian41587552011-01-03 18:20:01 +02001607 event->tstamp_enabled = tstamp - event->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001608 list_for_each_entry(sub, &event->sibling_list, group_entry) {
Stephane Eranian41587552011-01-03 18:20:01 +02001609 if (sub->state >= PERF_EVENT_STATE_INACTIVE)
1610 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001611 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001612}
1613
1614/*
1615 * Cross CPU call to enable a performance event
1616 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001617static int __perf_event_enable(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001618{
1619 struct perf_event *event = info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001620 struct perf_event_context *ctx = event->ctx;
1621 struct perf_event *leader = event->group_leader;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001622 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001623 int err;
1624
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001625 if (WARN_ON_ONCE(!ctx->is_active))
1626 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001627
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001628 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001629 update_context_time(ctx);
1630
1631 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1632 goto unlock;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001633
1634 /*
1635 * set current task's cgroup time reference point
1636 */
Stephane Eranian3f7cce32011-02-18 14:40:01 +02001637 perf_cgroup_set_timestamp(current, ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001638
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001639 __perf_event_mark_enabled(event, ctx);
1640
Stephane Eraniane5d13672011-02-14 11:20:01 +02001641 if (!event_filter_match(event)) {
1642 if (is_cgroup_event(event))
1643 perf_cgroup_defer_enabled(event);
Peter Zijlstraf4c41762009-12-16 17:55:54 +01001644 goto unlock;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001645 }
Peter Zijlstraf4c41762009-12-16 17:55:54 +01001646
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001647 /*
1648 * If the event is in a group and isn't the group leader,
1649 * then don't put it on unless the group is on.
1650 */
1651 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
1652 goto unlock;
1653
1654 if (!group_can_go_on(event, cpuctx, 1)) {
1655 err = -EEXIST;
1656 } else {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001657 if (event == leader)
Peter Zijlstra6e377382010-02-11 13:21:58 +01001658 err = group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001659 else
Peter Zijlstra6e377382010-02-11 13:21:58 +01001660 err = event_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001661 }
1662
1663 if (err) {
1664 /*
1665 * If this event can't go on and it's part of a
1666 * group, then the whole group has to come off.
1667 */
1668 if (leader != event)
1669 group_sched_out(leader, cpuctx, ctx);
1670 if (leader->attr.pinned) {
1671 update_group_times(leader);
1672 leader->state = PERF_EVENT_STATE_ERROR;
1673 }
1674 }
1675
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001676unlock:
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001677 raw_spin_unlock(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001678
1679 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001680}
1681
1682/*
1683 * Enable a event.
1684 *
1685 * If event->ctx is a cloned context, callers must make sure that
1686 * every task struct that event->ctx->task could possibly point to
1687 * remains valid. This condition is satisfied when called through
1688 * perf_event_for_each_child or perf_event_for_each as described
1689 * for perf_event_disable.
1690 */
Frederic Weisbecker44234ad2009-12-09 09:25:48 +01001691void perf_event_enable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001692{
1693 struct perf_event_context *ctx = event->ctx;
1694 struct task_struct *task = ctx->task;
1695
1696 if (!task) {
1697 /*
1698 * Enable the event on the cpu that it's on
1699 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001700 cpu_function_call(event->cpu, __perf_event_enable, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001701 return;
1702 }
1703
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001704 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001705 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1706 goto out;
1707
1708 /*
1709 * If the event is in error state, clear that first.
1710 * That way, if we see the event in error state below, we
1711 * know that it has gone back into error state, as distinct
1712 * from the task having been scheduled away before the
1713 * cross-call arrived.
1714 */
1715 if (event->state == PERF_EVENT_STATE_ERROR)
1716 event->state = PERF_EVENT_STATE_OFF;
1717
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001718retry:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001719 if (!ctx->is_active) {
1720 __perf_event_mark_enabled(event, ctx);
1721 goto out;
1722 }
1723
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001724 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001725
1726 if (!task_function_call(task, __perf_event_enable, event))
1727 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001728
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001729 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001730
1731 /*
1732 * If the context is active and the event is still off,
1733 * we need to retry the cross-call.
1734 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001735 if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF) {
1736 /*
1737 * task could have been flipped by a concurrent
1738 * perf_event_context_sched_out()
1739 */
1740 task = ctx->task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001741 goto retry;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001742 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001743
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001744out:
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001745 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001746}
1747
1748static int perf_event_refresh(struct perf_event *event, int refresh)
1749{
1750 /*
1751 * not supported on inherited events
1752 */
Franck Bui-Huu2e939d12010-11-23 16:21:44 +01001753 if (event->attr.inherit || !is_sampling_event(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001754 return -EINVAL;
1755
1756 atomic_add(refresh, &event->event_limit);
1757 perf_event_enable(event);
1758
1759 return 0;
1760}
1761
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001762static void ctx_sched_out(struct perf_event_context *ctx,
1763 struct perf_cpu_context *cpuctx,
1764 enum event_type_t event_type)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001765{
1766 struct perf_event *event;
1767
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001768 raw_spin_lock(&ctx->lock);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02001769 perf_pmu_disable(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001770 ctx->is_active = 0;
1771 if (likely(!ctx->nr_events))
1772 goto out;
1773 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001774 update_cgrp_time_from_cpuctx(cpuctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001775
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001776 if (!ctx->nr_active)
Peter Zijlstra24cd7f52010-06-11 17:32:03 +02001777 goto out;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001778
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001779 if (event_type & EVENT_PINNED) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001780 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
1781 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001782 }
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001783
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001784 if (event_type & EVENT_FLEXIBLE) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001785 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08001786 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001787 }
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001788out:
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02001789 perf_pmu_enable(ctx->pmu);
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001790 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001791}
1792
1793/*
1794 * Test whether two contexts are equivalent, i.e. whether they
1795 * have both been cloned from the same version of the same context
1796 * and they both have the same number of enabled events.
1797 * If the number of enabled events is the same, then the set
1798 * of enabled events should be the same, because these are both
1799 * inherited contexts, therefore we can't access individual events
1800 * in them directly with an fd; we can only enable/disable all
1801 * events via prctl, or enable/disable all events in a family
1802 * via ioctl, which will have the same effect on both contexts.
1803 */
1804static int context_equiv(struct perf_event_context *ctx1,
1805 struct perf_event_context *ctx2)
1806{
1807 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
1808 && ctx1->parent_gen == ctx2->parent_gen
1809 && !ctx1->pin_count && !ctx2->pin_count;
1810}
1811
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001812static void __perf_event_sync_stat(struct perf_event *event,
1813 struct perf_event *next_event)
1814{
1815 u64 value;
1816
1817 if (!event->attr.inherit_stat)
1818 return;
1819
1820 /*
1821 * Update the event value, we cannot use perf_event_read()
1822 * because we're in the middle of a context switch and have IRQs
1823 * disabled, which upsets smp_call_function_single(), however
1824 * we know the event must be on the current CPU, therefore we
1825 * don't need to use it.
1826 */
1827 switch (event->state) {
1828 case PERF_EVENT_STATE_ACTIVE:
Peter Zijlstra3dbebf12009-11-20 22:19:52 +01001829 event->pmu->read(event);
1830 /* fall-through */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001831
1832 case PERF_EVENT_STATE_INACTIVE:
1833 update_event_times(event);
1834 break;
1835
1836 default:
1837 break;
1838 }
1839
1840 /*
1841 * In order to keep per-task stats reliable we need to flip the event
1842 * values when we flip the contexts.
1843 */
Peter Zijlstrae7850592010-05-21 14:43:08 +02001844 value = local64_read(&next_event->count);
1845 value = local64_xchg(&event->count, value);
1846 local64_set(&next_event->count, value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001847
1848 swap(event->total_time_enabled, next_event->total_time_enabled);
1849 swap(event->total_time_running, next_event->total_time_running);
1850
1851 /*
1852 * Since we swizzled the values, update the user visible data too.
1853 */
1854 perf_event_update_userpage(event);
1855 perf_event_update_userpage(next_event);
1856}
1857
1858#define list_next_entry(pos, member) \
1859 list_entry(pos->member.next, typeof(*pos), member)
1860
1861static void perf_event_sync_stat(struct perf_event_context *ctx,
1862 struct perf_event_context *next_ctx)
1863{
1864 struct perf_event *event, *next_event;
1865
1866 if (!ctx->nr_stat)
1867 return;
1868
Peter Zijlstra02ffdbc2009-11-20 22:19:50 +01001869 update_context_time(ctx);
1870
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001871 event = list_first_entry(&ctx->event_list,
1872 struct perf_event, event_entry);
1873
1874 next_event = list_first_entry(&next_ctx->event_list,
1875 struct perf_event, event_entry);
1876
1877 while (&event->event_entry != &ctx->event_list &&
1878 &next_event->event_entry != &next_ctx->event_list) {
1879
1880 __perf_event_sync_stat(event, next_event);
1881
1882 event = list_next_entry(event, event_entry);
1883 next_event = list_next_entry(next_event, event_entry);
1884 }
1885}
1886
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001887static void perf_event_context_sched_out(struct task_struct *task, int ctxn,
1888 struct task_struct *next)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001889{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001890 struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001891 struct perf_event_context *next_ctx;
1892 struct perf_event_context *parent;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001893 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001894 int do_switch = 1;
1895
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001896 if (likely(!ctx))
1897 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001898
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001899 cpuctx = __get_cpu_context(ctx);
1900 if (!cpuctx->task_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001901 return;
1902
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001903 rcu_read_lock();
1904 parent = rcu_dereference(ctx->parent_ctx);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001905 next_ctx = next->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001906 if (parent && next_ctx &&
1907 rcu_dereference(next_ctx->parent_ctx) == parent) {
1908 /*
1909 * Looks like the two contexts are clones, so we might be
1910 * able to optimize the context switch. We lock both
1911 * contexts and check that they are clones under the
1912 * lock (including re-checking that neither has been
1913 * uncloned in the meantime). It doesn't matter which
1914 * order we take the locks because no other cpu could
1915 * be trying to lock both of these tasks.
1916 */
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001917 raw_spin_lock(&ctx->lock);
1918 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001919 if (context_equiv(ctx, next_ctx)) {
1920 /*
1921 * XXX do we need a memory barrier of sorts
1922 * wrt to rcu_dereference() of perf_event_ctxp
1923 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001924 task->perf_event_ctxp[ctxn] = next_ctx;
1925 next->perf_event_ctxp[ctxn] = ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001926 ctx->task = next;
1927 next_ctx->task = task;
1928 do_switch = 0;
1929
1930 perf_event_sync_stat(ctx, next_ctx);
1931 }
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001932 raw_spin_unlock(&next_ctx->lock);
1933 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001934 }
1935 rcu_read_unlock();
1936
1937 if (do_switch) {
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001938 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001939 cpuctx->task_ctx = NULL;
1940 }
1941}
1942
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001943#define for_each_task_context_nr(ctxn) \
1944 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
1945
1946/*
1947 * Called from scheduler to remove the events of the current task,
1948 * with interrupts disabled.
1949 *
1950 * We stop each event and update the event value in event->count.
1951 *
1952 * This does not protect us against NMI, but disable()
1953 * sets the disabled bit in the control field of event _before_
1954 * accessing the event control register. If a NMI hits, then it will
1955 * not restart the event.
1956 */
Peter Zijlstra82cd6de2010-10-14 17:57:23 +02001957void __perf_event_task_sched_out(struct task_struct *task,
1958 struct task_struct *next)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001959{
1960 int ctxn;
1961
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001962 for_each_task_context_nr(ctxn)
1963 perf_event_context_sched_out(task, ctxn, next);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001964
1965 /*
1966 * if cgroup events exist on this CPU, then we need
1967 * to check if we have to switch out PMU state.
1968 * cgroup event are system-wide mode only
1969 */
1970 if (atomic_read(&__get_cpu_var(perf_cgroup_events)))
1971 perf_cgroup_sched_out(task);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001972}
1973
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001974static void task_ctx_sched_out(struct perf_event_context *ctx,
1975 enum event_type_t event_type)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001976{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001977 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001978
1979 if (!cpuctx->task_ctx)
1980 return;
1981
1982 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
1983 return;
1984
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001985 ctx_sched_out(ctx, cpuctx, event_type);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001986 cpuctx->task_ctx = NULL;
1987}
1988
1989/*
1990 * Called with IRQs disabled
1991 */
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001992static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
1993 enum event_type_t event_type)
1994{
1995 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001996}
1997
1998static void
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001999ctx_pinned_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01002000 struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002001{
2002 struct perf_event *event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002003
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002004 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
2005 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002006 continue;
Stephane Eranian5632ab12011-01-03 18:20:01 +02002007 if (!event_filter_match(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002008 continue;
2009
Stephane Eraniane5d13672011-02-14 11:20:01 +02002010 /* may need to reset tstamp_enabled */
2011 if (is_cgroup_event(event))
2012 perf_cgroup_mark_enabled(event, ctx);
2013
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08002014 if (group_can_go_on(event, cpuctx, 1))
Peter Zijlstra6e377382010-02-11 13:21:58 +01002015 group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002016
2017 /*
2018 * If this pinned group hasn't been scheduled,
2019 * put it in error state.
2020 */
2021 if (event->state == PERF_EVENT_STATE_INACTIVE) {
2022 update_group_times(event);
2023 event->state = PERF_EVENT_STATE_ERROR;
2024 }
2025 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002026}
2027
2028static void
2029ctx_flexible_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01002030 struct perf_cpu_context *cpuctx)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002031{
2032 struct perf_event *event;
2033 int can_add_hw = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002034
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002035 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
2036 /* Ignore events in OFF or ERROR state */
2037 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002038 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002039 /*
2040 * Listen to the 'cpu' scheduling filter constraint
2041 * of events:
2042 */
Stephane Eranian5632ab12011-01-03 18:20:01 +02002043 if (!event_filter_match(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002044 continue;
2045
Stephane Eraniane5d13672011-02-14 11:20:01 +02002046 /* may need to reset tstamp_enabled */
2047 if (is_cgroup_event(event))
2048 perf_cgroup_mark_enabled(event, ctx);
2049
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002050 if (group_can_go_on(event, cpuctx, can_add_hw)) {
Peter Zijlstra6e377382010-02-11 13:21:58 +01002051 if (group_sched_in(event, cpuctx, ctx))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002052 can_add_hw = 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002053 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002054 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002055}
2056
2057static void
2058ctx_sched_in(struct perf_event_context *ctx,
2059 struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +02002060 enum event_type_t event_type,
2061 struct task_struct *task)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002062{
Stephane Eraniane5d13672011-02-14 11:20:01 +02002063 u64 now;
2064
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002065 raw_spin_lock(&ctx->lock);
2066 ctx->is_active = 1;
2067 if (likely(!ctx->nr_events))
2068 goto out;
2069
Stephane Eraniane5d13672011-02-14 11:20:01 +02002070 now = perf_clock();
2071 ctx->timestamp = now;
Stephane Eranian3f7cce32011-02-18 14:40:01 +02002072 perf_cgroup_set_timestamp(task, ctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002073 /*
2074 * First go through the list and put on any pinned groups
2075 * in order to give them the best chance of going on.
2076 */
2077 if (event_type & EVENT_PINNED)
Peter Zijlstra6e377382010-02-11 13:21:58 +01002078 ctx_pinned_sched_in(ctx, cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002079
2080 /* Then walk through the lower prio flexible groups */
2081 if (event_type & EVENT_FLEXIBLE)
Peter Zijlstra6e377382010-02-11 13:21:58 +01002082 ctx_flexible_sched_in(ctx, cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002083
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002084out:
Thomas Gleixnere625cce2009-11-17 18:02:06 +01002085 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002086}
2087
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002088static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +02002089 enum event_type_t event_type,
2090 struct task_struct *task)
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002091{
2092 struct perf_event_context *ctx = &cpuctx->ctx;
2093
Stephane Eraniane5d13672011-02-14 11:20:01 +02002094 ctx_sched_in(ctx, cpuctx, event_type, task);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002095}
2096
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002097static void task_ctx_sched_in(struct perf_event_context *ctx,
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002098 enum event_type_t event_type)
2099{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002100 struct perf_cpu_context *cpuctx;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002101
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002102 cpuctx = __get_cpu_context(ctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002103 if (cpuctx->task_ctx == ctx)
2104 return;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002105
Stephane Eraniane5d13672011-02-14 11:20:01 +02002106 ctx_sched_in(ctx, cpuctx, event_type, NULL);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002107 cpuctx->task_ctx = ctx;
2108}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002109
Stephane Eraniane5d13672011-02-14 11:20:01 +02002110static void perf_event_context_sched_in(struct perf_event_context *ctx,
2111 struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002112{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002113 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002114
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002115 cpuctx = __get_cpu_context(ctx);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002116 if (cpuctx->task_ctx == ctx)
2117 return;
2118
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02002119 perf_pmu_disable(ctx->pmu);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002120 /*
2121 * We want to keep the following priority order:
2122 * cpu pinned (that don't need to move), task pinned,
2123 * cpu flexible, task flexible.
2124 */
2125 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
2126
Stephane Eraniane5d13672011-02-14 11:20:01 +02002127 ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task);
2128 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task);
2129 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002130
2131 cpuctx->task_ctx = ctx;
eranian@google.com9b33fa62010-03-10 22:26:05 -08002132
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002133 /*
2134 * Since these rotations are per-cpu, we need to ensure the
2135 * cpu-context we got scheduled on is actually rotating.
2136 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002137 perf_pmu_rotate_start(ctx->pmu);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02002138 perf_pmu_enable(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002139}
2140
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002141/*
2142 * Called from scheduler to add the events of the current task
2143 * with interrupts disabled.
2144 *
2145 * We restore the event value and then enable it.
2146 *
2147 * This does not protect us against NMI, but enable()
2148 * sets the enabled bit in the control field of event _before_
2149 * accessing the event control register. If a NMI hits, then it will
2150 * keep the event running.
2151 */
Peter Zijlstra82cd6de2010-10-14 17:57:23 +02002152void __perf_event_task_sched_in(struct task_struct *task)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002153{
2154 struct perf_event_context *ctx;
2155 int ctxn;
2156
2157 for_each_task_context_nr(ctxn) {
2158 ctx = task->perf_event_ctxp[ctxn];
2159 if (likely(!ctx))
2160 continue;
2161
Stephane Eraniane5d13672011-02-14 11:20:01 +02002162 perf_event_context_sched_in(ctx, task);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002163 }
Stephane Eraniane5d13672011-02-14 11:20:01 +02002164 /*
2165 * if cgroup events exist on this CPU, then we need
2166 * to check if we have to switch in PMU state.
2167 * cgroup event are system-wide mode only
2168 */
2169 if (atomic_read(&__get_cpu_var(perf_cgroup_events)))
2170 perf_cgroup_sched_in(task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002171}
2172
Peter Zijlstraabd50712010-01-26 18:50:16 +01002173static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
2174{
2175 u64 frequency = event->attr.sample_freq;
2176 u64 sec = NSEC_PER_SEC;
2177 u64 divisor, dividend;
2178
2179 int count_fls, nsec_fls, frequency_fls, sec_fls;
2180
2181 count_fls = fls64(count);
2182 nsec_fls = fls64(nsec);
2183 frequency_fls = fls64(frequency);
2184 sec_fls = 30;
2185
2186 /*
2187 * We got @count in @nsec, with a target of sample_freq HZ
2188 * the target period becomes:
2189 *
2190 * @count * 10^9
2191 * period = -------------------
2192 * @nsec * sample_freq
2193 *
2194 */
2195
2196 /*
2197 * Reduce accuracy by one bit such that @a and @b converge
2198 * to a similar magnitude.
2199 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002200#define REDUCE_FLS(a, b) \
Peter Zijlstraabd50712010-01-26 18:50:16 +01002201do { \
2202 if (a##_fls > b##_fls) { \
2203 a >>= 1; \
2204 a##_fls--; \
2205 } else { \
2206 b >>= 1; \
2207 b##_fls--; \
2208 } \
2209} while (0)
2210
2211 /*
2212 * Reduce accuracy until either term fits in a u64, then proceed with
2213 * the other, so that finally we can do a u64/u64 division.
2214 */
2215 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
2216 REDUCE_FLS(nsec, frequency);
2217 REDUCE_FLS(sec, count);
2218 }
2219
2220 if (count_fls + sec_fls > 64) {
2221 divisor = nsec * frequency;
2222
2223 while (count_fls + sec_fls > 64) {
2224 REDUCE_FLS(count, sec);
2225 divisor >>= 1;
2226 }
2227
2228 dividend = count * sec;
2229 } else {
2230 dividend = count * sec;
2231
2232 while (nsec_fls + frequency_fls > 64) {
2233 REDUCE_FLS(nsec, frequency);
2234 dividend >>= 1;
2235 }
2236
2237 divisor = nsec * frequency;
2238 }
2239
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02002240 if (!divisor)
2241 return dividend;
2242
Peter Zijlstraabd50712010-01-26 18:50:16 +01002243 return div64_u64(dividend, divisor);
2244}
2245
2246static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002247{
2248 struct hw_perf_event *hwc = &event->hw;
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02002249 s64 period, sample_period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002250 s64 delta;
2251
Peter Zijlstraabd50712010-01-26 18:50:16 +01002252 period = perf_calculate_period(event, nsec, count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002253
2254 delta = (s64)(period - hwc->sample_period);
2255 delta = (delta + 7) / 8; /* low pass filter */
2256
2257 sample_period = hwc->sample_period + delta;
2258
2259 if (!sample_period)
2260 sample_period = 1;
2261
2262 hwc->sample_period = sample_period;
Peter Zijlstraabd50712010-01-26 18:50:16 +01002263
Peter Zijlstrae7850592010-05-21 14:43:08 +02002264 if (local64_read(&hwc->period_left) > 8*sample_period) {
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02002265 event->pmu->stop(event, PERF_EF_UPDATE);
Peter Zijlstrae7850592010-05-21 14:43:08 +02002266 local64_set(&hwc->period_left, 0);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02002267 event->pmu->start(event, PERF_EF_RELOAD);
Peter Zijlstraabd50712010-01-26 18:50:16 +01002268 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002269}
2270
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002271static void perf_ctx_adjust_freq(struct perf_event_context *ctx, u64 period)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002272{
2273 struct perf_event *event;
2274 struct hw_perf_event *hwc;
Peter Zijlstraabd50712010-01-26 18:50:16 +01002275 u64 interrupts, now;
2276 s64 delta;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002277
Thomas Gleixnere625cce2009-11-17 18:02:06 +01002278 raw_spin_lock(&ctx->lock);
Paul Mackerras03541f82009-10-14 16:58:03 +11002279 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002280 if (event->state != PERF_EVENT_STATE_ACTIVE)
2281 continue;
2282
Stephane Eranian5632ab12011-01-03 18:20:01 +02002283 if (!event_filter_match(event))
Peter Zijlstra5d27c232009-12-17 13:16:32 +01002284 continue;
2285
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002286 hwc = &event->hw;
2287
2288 interrupts = hwc->interrupts;
2289 hwc->interrupts = 0;
2290
2291 /*
2292 * unthrottle events on the tick
2293 */
2294 if (interrupts == MAX_INTERRUPTS) {
2295 perf_log_throttle(event, 1);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02002296 event->pmu->start(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002297 }
2298
2299 if (!event->attr.freq || !event->attr.sample_freq)
2300 continue;
2301
Peter Zijlstraabd50712010-01-26 18:50:16 +01002302 event->pmu->read(event);
Peter Zijlstrae7850592010-05-21 14:43:08 +02002303 now = local64_read(&event->count);
Peter Zijlstraabd50712010-01-26 18:50:16 +01002304 delta = now - hwc->freq_count_stamp;
2305 hwc->freq_count_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002306
Peter Zijlstraabd50712010-01-26 18:50:16 +01002307 if (delta > 0)
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002308 perf_adjust_period(event, period, delta);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002309 }
Thomas Gleixnere625cce2009-11-17 18:02:06 +01002310 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002311}
2312
2313/*
2314 * Round-robin a context's events:
2315 */
2316static void rotate_ctx(struct perf_event_context *ctx)
2317{
Thomas Gleixnere625cce2009-11-17 18:02:06 +01002318 raw_spin_lock(&ctx->lock);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002319
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01002320 /*
2321 * Rotate the first entry last of non-pinned groups. Rotation might be
2322 * disabled by the inheritance code.
2323 */
2324 if (!ctx->rotate_disable)
2325 list_rotate_left(&ctx->flexible_groups);
Frederic Weisbeckere2864172010-01-09 21:05:28 +01002326
Thomas Gleixnere625cce2009-11-17 18:02:06 +01002327 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002328}
2329
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002330/*
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002331 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
2332 * because they're strictly cpu affine and rotate_start is called with IRQs
2333 * disabled, while rotate_context is called from IRQ context.
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002334 */
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002335static void perf_rotate_context(struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002336{
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002337 u64 interval = (u64)cpuctx->jiffies_interval * TICK_NSEC;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002338 struct perf_event_context *ctx = NULL;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002339 int rotate = 0, remove = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002340
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002341 if (cpuctx->ctx.nr_events) {
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002342 remove = 0;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002343 if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
2344 rotate = 1;
2345 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002346
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002347 ctx = cpuctx->task_ctx;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002348 if (ctx && ctx->nr_events) {
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002349 remove = 0;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002350 if (ctx->nr_events != ctx->nr_active)
2351 rotate = 1;
2352 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002353
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02002354 perf_pmu_disable(cpuctx->ctx.pmu);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002355 perf_ctx_adjust_freq(&cpuctx->ctx, interval);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002356 if (ctx)
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002357 perf_ctx_adjust_freq(ctx, interval);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002358
Peter Zijlstrad4944a02010-03-08 13:51:20 +01002359 if (!rotate)
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002360 goto done;
Peter Zijlstrad4944a02010-03-08 13:51:20 +01002361
Frederic Weisbecker7defb0f2010-01-17 12:15:31 +01002362 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002363 if (ctx)
Frederic Weisbecker7defb0f2010-01-17 12:15:31 +01002364 task_ctx_sched_out(ctx, EVENT_FLEXIBLE);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002365
2366 rotate_ctx(&cpuctx->ctx);
2367 if (ctx)
2368 rotate_ctx(ctx);
2369
Stephane Eraniane5d13672011-02-14 11:20:01 +02002370 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, current);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002371 if (ctx)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002372 task_ctx_sched_in(ctx, EVENT_FLEXIBLE);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002373
2374done:
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002375 if (remove)
2376 list_del_init(&cpuctx->rotation_list);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002377
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002378 perf_pmu_enable(cpuctx->ctx.pmu);
2379}
2380
2381void perf_event_task_tick(void)
2382{
2383 struct list_head *head = &__get_cpu_var(rotation_list);
2384 struct perf_cpu_context *cpuctx, *tmp;
2385
2386 WARN_ON(!irqs_disabled());
2387
2388 list_for_each_entry_safe(cpuctx, tmp, head, rotation_list) {
2389 if (cpuctx->jiffies_interval == 1 ||
2390 !(jiffies % cpuctx->jiffies_interval))
2391 perf_rotate_context(cpuctx);
2392 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002393}
2394
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002395static int event_enable_on_exec(struct perf_event *event,
2396 struct perf_event_context *ctx)
2397{
2398 if (!event->attr.enable_on_exec)
2399 return 0;
2400
2401 event->attr.enable_on_exec = 0;
2402 if (event->state >= PERF_EVENT_STATE_INACTIVE)
2403 return 0;
2404
2405 __perf_event_mark_enabled(event, ctx);
2406
2407 return 1;
2408}
2409
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002410/*
2411 * Enable all of a task's events that have been marked enable-on-exec.
2412 * This expects task == current.
2413 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002414static void perf_event_enable_on_exec(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002415{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002416 struct perf_event *event;
2417 unsigned long flags;
2418 int enabled = 0;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002419 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002420
2421 local_irq_save(flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002422 if (!ctx || !ctx->nr_events)
2423 goto out;
2424
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002425 task_ctx_sched_out(ctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002426
Thomas Gleixnere625cce2009-11-17 18:02:06 +01002427 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002428
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002429 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
2430 ret = event_enable_on_exec(event, ctx);
2431 if (ret)
2432 enabled = 1;
2433 }
2434
2435 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
2436 ret = event_enable_on_exec(event, ctx);
2437 if (ret)
2438 enabled = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002439 }
2440
2441 /*
2442 * Unclone this context if we enabled any event.
2443 */
2444 if (enabled)
2445 unclone_ctx(ctx);
2446
Thomas Gleixnere625cce2009-11-17 18:02:06 +01002447 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002448
Stephane Eraniane5d13672011-02-14 11:20:01 +02002449 perf_event_context_sched_in(ctx, ctx->task);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002450out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002451 local_irq_restore(flags);
2452}
2453
2454/*
2455 * Cross CPU call to read the hardware event
2456 */
2457static void __perf_event_read(void *info)
2458{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002459 struct perf_event *event = info;
2460 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002461 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002462
2463 /*
2464 * If this is a task context, we need to check whether it is
2465 * the current task context of this cpu. If not it has been
2466 * scheduled out before the smp call arrived. In that case
2467 * event->count would have been updated to a recent sample
2468 * when the event was scheduled out.
2469 */
2470 if (ctx->task && cpuctx->task_ctx != ctx)
2471 return;
2472
Thomas Gleixnere625cce2009-11-17 18:02:06 +01002473 raw_spin_lock(&ctx->lock);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002474 if (ctx->is_active) {
Peter Zijlstra542e72f2011-01-26 15:38:35 +01002475 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002476 update_cgrp_time_from_event(event);
2477 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002478 update_event_times(event);
Peter Zijlstra542e72f2011-01-26 15:38:35 +01002479 if (event->state == PERF_EVENT_STATE_ACTIVE)
2480 event->pmu->read(event);
Thomas Gleixnere625cce2009-11-17 18:02:06 +01002481 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002482}
2483
Peter Zijlstrab5e58792010-05-21 14:43:12 +02002484static inline u64 perf_event_count(struct perf_event *event)
2485{
Peter Zijlstrae7850592010-05-21 14:43:08 +02002486 return local64_read(&event->count) + atomic64_read(&event->child_count);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02002487}
2488
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002489static u64 perf_event_read(struct perf_event *event)
2490{
2491 /*
2492 * If event is enabled and currently active on a CPU, update the
2493 * value in the event structure:
2494 */
2495 if (event->state == PERF_EVENT_STATE_ACTIVE) {
2496 smp_call_function_single(event->oncpu,
2497 __perf_event_read, event, 1);
2498 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01002499 struct perf_event_context *ctx = event->ctx;
2500 unsigned long flags;
2501
Thomas Gleixnere625cce2009-11-17 18:02:06 +01002502 raw_spin_lock_irqsave(&ctx->lock, flags);
Stephane Eranianc530ccd2010-10-15 15:26:01 +02002503 /*
2504 * may read while context is not active
2505 * (e.g., thread is blocked), in that case
2506 * we cannot update context time
2507 */
Stephane Eraniane5d13672011-02-14 11:20:01 +02002508 if (ctx->is_active) {
Stephane Eranianc530ccd2010-10-15 15:26:01 +02002509 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002510 update_cgrp_time_from_event(event);
2511 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002512 update_event_times(event);
Thomas Gleixnere625cce2009-11-17 18:02:06 +01002513 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002514 }
2515
Peter Zijlstrab5e58792010-05-21 14:43:12 +02002516 return perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002517}
2518
2519/*
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02002520 * Callchain support
2521 */
2522
2523struct callchain_cpus_entries {
2524 struct rcu_head rcu_head;
2525 struct perf_callchain_entry *cpu_entries[0];
2526};
2527
Frederic Weisbecker7ae07ea2010-08-14 20:45:13 +02002528static DEFINE_PER_CPU(int, callchain_recursion[PERF_NR_CONTEXTS]);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02002529static atomic_t nr_callchain_events;
2530static DEFINE_MUTEX(callchain_mutex);
2531struct callchain_cpus_entries *callchain_cpus_entries;
2532
2533
2534__weak void perf_callchain_kernel(struct perf_callchain_entry *entry,
2535 struct pt_regs *regs)
2536{
2537}
2538
2539__weak void perf_callchain_user(struct perf_callchain_entry *entry,
2540 struct pt_regs *regs)
2541{
2542}
2543
2544static void release_callchain_buffers_rcu(struct rcu_head *head)
2545{
2546 struct callchain_cpus_entries *entries;
2547 int cpu;
2548
2549 entries = container_of(head, struct callchain_cpus_entries, rcu_head);
2550
2551 for_each_possible_cpu(cpu)
2552 kfree(entries->cpu_entries[cpu]);
2553
2554 kfree(entries);
2555}
2556
2557static void release_callchain_buffers(void)
2558{
2559 struct callchain_cpus_entries *entries;
2560
2561 entries = callchain_cpus_entries;
2562 rcu_assign_pointer(callchain_cpus_entries, NULL);
2563 call_rcu(&entries->rcu_head, release_callchain_buffers_rcu);
2564}
2565
2566static int alloc_callchain_buffers(void)
2567{
2568 int cpu;
2569 int size;
2570 struct callchain_cpus_entries *entries;
2571
2572 /*
2573 * We can't use the percpu allocation API for data that can be
2574 * accessed from NMI. Use a temporary manual per cpu allocation
2575 * until that gets sorted out.
2576 */
Eric Dumazet88d4f0d2011-01-25 19:40:51 +01002577 size = offsetof(struct callchain_cpus_entries, cpu_entries[nr_cpu_ids]);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02002578
2579 entries = kzalloc(size, GFP_KERNEL);
2580 if (!entries)
2581 return -ENOMEM;
2582
Frederic Weisbecker7ae07ea2010-08-14 20:45:13 +02002583 size = sizeof(struct perf_callchain_entry) * PERF_NR_CONTEXTS;
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02002584
2585 for_each_possible_cpu(cpu) {
2586 entries->cpu_entries[cpu] = kmalloc_node(size, GFP_KERNEL,
2587 cpu_to_node(cpu));
2588 if (!entries->cpu_entries[cpu])
2589 goto fail;
2590 }
2591
2592 rcu_assign_pointer(callchain_cpus_entries, entries);
2593
2594 return 0;
2595
2596fail:
2597 for_each_possible_cpu(cpu)
2598 kfree(entries->cpu_entries[cpu]);
2599 kfree(entries);
2600
2601 return -ENOMEM;
2602}
2603
2604static int get_callchain_buffers(void)
2605{
2606 int err = 0;
2607 int count;
2608
2609 mutex_lock(&callchain_mutex);
2610
2611 count = atomic_inc_return(&nr_callchain_events);
2612 if (WARN_ON_ONCE(count < 1)) {
2613 err = -EINVAL;
2614 goto exit;
2615 }
2616
2617 if (count > 1) {
2618 /* If the allocation failed, give up */
2619 if (!callchain_cpus_entries)
2620 err = -ENOMEM;
2621 goto exit;
2622 }
2623
2624 err = alloc_callchain_buffers();
2625 if (err)
2626 release_callchain_buffers();
2627exit:
2628 mutex_unlock(&callchain_mutex);
2629
2630 return err;
2631}
2632
2633static void put_callchain_buffers(void)
2634{
2635 if (atomic_dec_and_mutex_lock(&nr_callchain_events, &callchain_mutex)) {
2636 release_callchain_buffers();
2637 mutex_unlock(&callchain_mutex);
2638 }
2639}
2640
2641static int get_recursion_context(int *recursion)
2642{
2643 int rctx;
2644
2645 if (in_nmi())
2646 rctx = 3;
2647 else if (in_irq())
2648 rctx = 2;
2649 else if (in_softirq())
2650 rctx = 1;
2651 else
2652 rctx = 0;
2653
2654 if (recursion[rctx])
2655 return -1;
2656
2657 recursion[rctx]++;
2658 barrier();
2659
2660 return rctx;
2661}
2662
2663static inline void put_recursion_context(int *recursion, int rctx)
2664{
2665 barrier();
2666 recursion[rctx]--;
2667}
2668
2669static struct perf_callchain_entry *get_callchain_entry(int *rctx)
2670{
2671 int cpu;
2672 struct callchain_cpus_entries *entries;
2673
2674 *rctx = get_recursion_context(__get_cpu_var(callchain_recursion));
2675 if (*rctx == -1)
2676 return NULL;
2677
2678 entries = rcu_dereference(callchain_cpus_entries);
2679 if (!entries)
2680 return NULL;
2681
2682 cpu = smp_processor_id();
2683
2684 return &entries->cpu_entries[cpu][*rctx];
2685}
2686
2687static void
2688put_callchain_entry(int rctx)
2689{
2690 put_recursion_context(__get_cpu_var(callchain_recursion), rctx);
2691}
2692
2693static struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
2694{
2695 int rctx;
2696 struct perf_callchain_entry *entry;
2697
2698
2699 entry = get_callchain_entry(&rctx);
2700 if (rctx == -1)
2701 return NULL;
2702
2703 if (!entry)
2704 goto exit_put;
2705
2706 entry->nr = 0;
2707
2708 if (!user_mode(regs)) {
2709 perf_callchain_store(entry, PERF_CONTEXT_KERNEL);
2710 perf_callchain_kernel(entry, regs);
2711 if (current->mm)
2712 regs = task_pt_regs(current);
2713 else
2714 regs = NULL;
2715 }
2716
2717 if (regs) {
2718 perf_callchain_store(entry, PERF_CONTEXT_USER);
2719 perf_callchain_user(entry, regs);
2720 }
2721
2722exit_put:
2723 put_callchain_entry(rctx);
2724
2725 return entry;
2726}
2727
2728/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002729 * Initialize the perf_event context in a task_struct:
2730 */
Peter Zijlstraeb184472010-09-07 15:55:13 +02002731static void __perf_event_init_context(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002732{
Thomas Gleixnere625cce2009-11-17 18:02:06 +01002733 raw_spin_lock_init(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002734 mutex_init(&ctx->mutex);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002735 INIT_LIST_HEAD(&ctx->pinned_groups);
2736 INIT_LIST_HEAD(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002737 INIT_LIST_HEAD(&ctx->event_list);
2738 atomic_set(&ctx->refcount, 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002739}
2740
Peter Zijlstraeb184472010-09-07 15:55:13 +02002741static struct perf_event_context *
2742alloc_perf_context(struct pmu *pmu, struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002743{
2744 struct perf_event_context *ctx;
Peter Zijlstraeb184472010-09-07 15:55:13 +02002745
2746 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
2747 if (!ctx)
2748 return NULL;
2749
2750 __perf_event_init_context(ctx);
2751 if (task) {
2752 ctx->task = task;
2753 get_task_struct(task);
2754 }
2755 ctx->pmu = pmu;
2756
2757 return ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002758}
2759
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07002760static struct task_struct *
2761find_lively_task_by_vpid(pid_t vpid)
2762{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002763 struct task_struct *task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002764 int err;
2765
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002766 rcu_read_lock();
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07002767 if (!vpid)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002768 task = current;
2769 else
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07002770 task = find_task_by_vpid(vpid);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002771 if (task)
2772 get_task_struct(task);
2773 rcu_read_unlock();
2774
2775 if (!task)
2776 return ERR_PTR(-ESRCH);
2777
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002778 /* Reuse ptrace permission checks for now. */
2779 err = -EACCES;
2780 if (!ptrace_may_access(task, PTRACE_MODE_READ))
2781 goto errout;
2782
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07002783 return task;
2784errout:
2785 put_task_struct(task);
2786 return ERR_PTR(err);
2787
2788}
2789
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002790/*
2791 * Returns a matching context with refcount and pincount.
2792 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002793static struct perf_event_context *
Matt Helsley38a81da2010-09-13 13:01:20 -07002794find_get_context(struct pmu *pmu, struct task_struct *task, int cpu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002795{
2796 struct perf_event_context *ctx;
2797 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002798 unsigned long flags;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002799 int ctxn, err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002800
Oleg Nesterov22a4ec72011-01-18 17:10:08 +01002801 if (!task) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002802 /* Must be root to operate on a CPU event: */
2803 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
2804 return ERR_PTR(-EACCES);
2805
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002806 /*
2807 * We could be clever and allow to attach a event to an
2808 * offline CPU and activate it when the CPU comes up, but
2809 * that's for later.
2810 */
2811 if (!cpu_online(cpu))
2812 return ERR_PTR(-ENODEV);
2813
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002814 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002815 ctx = &cpuctx->ctx;
2816 get_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002817 ++ctx->pin_count;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002818
2819 return ctx;
2820 }
2821
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002822 err = -EINVAL;
2823 ctxn = pmu->task_ctx_nr;
2824 if (ctxn < 0)
2825 goto errout;
2826
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002827retry:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002828 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002829 if (ctx) {
2830 unclone_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002831 ++ctx->pin_count;
Thomas Gleixnere625cce2009-11-17 18:02:06 +01002832 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002833 }
2834
2835 if (!ctx) {
Peter Zijlstraeb184472010-09-07 15:55:13 +02002836 ctx = alloc_perf_context(pmu, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002837 err = -ENOMEM;
2838 if (!ctx)
2839 goto errout;
Peter Zijlstraeb184472010-09-07 15:55:13 +02002840
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002841 get_ctx(ctx);
Peter Zijlstraeb184472010-09-07 15:55:13 +02002842
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01002843 err = 0;
2844 mutex_lock(&task->perf_event_mutex);
2845 /*
2846 * If it has already passed perf_event_exit_task().
2847 * we must see PF_EXITING, it takes this mutex too.
2848 */
2849 if (task->flags & PF_EXITING)
2850 err = -ESRCH;
2851 else if (task->perf_event_ctxp[ctxn])
2852 err = -EAGAIN;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002853 else {
2854 ++ctx->pin_count;
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01002855 rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002856 }
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01002857 mutex_unlock(&task->perf_event_mutex);
2858
2859 if (unlikely(err)) {
Peter Zijlstraeb184472010-09-07 15:55:13 +02002860 put_task_struct(task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002861 kfree(ctx);
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01002862
2863 if (err == -EAGAIN)
2864 goto retry;
2865 goto errout;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002866 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002867 }
2868
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002869 return ctx;
2870
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002871errout:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002872 return ERR_PTR(err);
2873}
2874
Li Zefan6fb29152009-10-15 11:21:42 +08002875static void perf_event_free_filter(struct perf_event *event);
2876
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002877static void free_event_rcu(struct rcu_head *head)
2878{
2879 struct perf_event *event;
2880
2881 event = container_of(head, struct perf_event, rcu_head);
2882 if (event->ns)
2883 put_pid_ns(event->ns);
Li Zefan6fb29152009-10-15 11:21:42 +08002884 perf_event_free_filter(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002885 kfree(event);
2886}
2887
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002888static void perf_buffer_put(struct perf_buffer *buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002889
2890static void free_event(struct perf_event *event)
2891{
Peter Zijlstrae360adb2010-10-14 14:01:34 +08002892 irq_work_sync(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002893
2894 if (!event->parent) {
Peter Zijlstra82cd6de2010-10-14 17:57:23 +02002895 if (event->attach_state & PERF_ATTACH_TASK)
Stephane Eraniane5d13672011-02-14 11:20:01 +02002896 jump_label_dec(&perf_sched_events);
Eric B Munson3af9e852010-05-18 15:30:49 +01002897 if (event->attr.mmap || event->attr.mmap_data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002898 atomic_dec(&nr_mmap_events);
2899 if (event->attr.comm)
2900 atomic_dec(&nr_comm_events);
2901 if (event->attr.task)
2902 atomic_dec(&nr_task_events);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02002903 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
2904 put_callchain_buffers();
Peter Zijlstra08309372011-03-03 11:31:20 +01002905 if (is_cgroup_event(event)) {
2906 atomic_dec(&per_cpu(perf_cgroup_events, event->cpu));
2907 jump_label_dec(&perf_sched_events);
2908 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002909 }
2910
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002911 if (event->buffer) {
2912 perf_buffer_put(event->buffer);
2913 event->buffer = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002914 }
2915
Stephane Eraniane5d13672011-02-14 11:20:01 +02002916 if (is_cgroup_event(event))
2917 perf_detach_cgroup(event);
2918
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002919 if (event->destroy)
2920 event->destroy(event);
2921
Peter Zijlstra0c67b402010-09-13 11:15:58 +02002922 if (event->ctx)
2923 put_ctx(event->ctx);
2924
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002925 call_rcu(&event->rcu_head, free_event_rcu);
2926}
2927
Arjan van de Venfb0459d2009-09-25 12:25:56 +02002928int perf_event_release_kernel(struct perf_event *event)
2929{
2930 struct perf_event_context *ctx = event->ctx;
2931
Peter Zijlstra050735b2010-05-11 11:51:53 +02002932 /*
2933 * Remove from the PMU, can't get re-enabled since we got
2934 * here because the last ref went.
2935 */
2936 perf_event_disable(event);
2937
Arjan van de Venfb0459d2009-09-25 12:25:56 +02002938 WARN_ON_ONCE(ctx->parent_ctx);
Peter Zijlstraa0507c82010-05-06 15:42:53 +02002939 /*
2940 * There are two ways this annotation is useful:
2941 *
2942 * 1) there is a lock recursion from perf_event_exit_task
2943 * see the comment there.
2944 *
2945 * 2) there is a lock-inversion with mmap_sem through
2946 * perf_event_read_group(), which takes faults while
2947 * holding ctx->mutex, however this is called after
2948 * the last filedesc died, so there is no possibility
2949 * to trigger the AB-BA case.
2950 */
2951 mutex_lock_nested(&ctx->mutex, SINGLE_DEPTH_NESTING);
Peter Zijlstra050735b2010-05-11 11:51:53 +02002952 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra8a495422010-05-27 15:47:49 +02002953 perf_group_detach(event);
Peter Zijlstra050735b2010-05-11 11:51:53 +02002954 list_del_event(event, ctx);
Peter Zijlstra050735b2010-05-11 11:51:53 +02002955 raw_spin_unlock_irq(&ctx->lock);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02002956 mutex_unlock(&ctx->mutex);
2957
Arjan van de Venfb0459d2009-09-25 12:25:56 +02002958 free_event(event);
2959
2960 return 0;
2961}
2962EXPORT_SYMBOL_GPL(perf_event_release_kernel);
2963
Peter Zijlstraa66a3052009-11-23 11:37:23 +01002964/*
2965 * Called when the last reference to the file is gone.
2966 */
2967static int perf_release(struct inode *inode, struct file *file)
2968{
2969 struct perf_event *event = file->private_data;
Peter Zijlstra8882135b2010-11-09 19:01:43 +01002970 struct task_struct *owner;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01002971
2972 file->private_data = NULL;
2973
Peter Zijlstra8882135b2010-11-09 19:01:43 +01002974 rcu_read_lock();
2975 owner = ACCESS_ONCE(event->owner);
2976 /*
2977 * Matches the smp_wmb() in perf_event_exit_task(). If we observe
2978 * !owner it means the list deletion is complete and we can indeed
2979 * free this event, otherwise we need to serialize on
2980 * owner->perf_event_mutex.
2981 */
2982 smp_read_barrier_depends();
2983 if (owner) {
2984 /*
2985 * Since delayed_put_task_struct() also drops the last
2986 * task reference we can safely take a new reference
2987 * while holding the rcu_read_lock().
2988 */
2989 get_task_struct(owner);
2990 }
2991 rcu_read_unlock();
2992
2993 if (owner) {
2994 mutex_lock(&owner->perf_event_mutex);
2995 /*
2996 * We have to re-check the event->owner field, if it is cleared
2997 * we raced with perf_event_exit_task(), acquiring the mutex
2998 * ensured they're done, and we can proceed with freeing the
2999 * event.
3000 */
3001 if (event->owner)
3002 list_del_init(&event->owner_entry);
3003 mutex_unlock(&owner->perf_event_mutex);
3004 put_task_struct(owner);
3005 }
3006
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003007 return perf_event_release_kernel(event);
3008}
3009
Peter Zijlstra59ed446f2009-11-20 22:19:55 +01003010u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003011{
3012 struct perf_event *child;
3013 u64 total = 0;
3014
Peter Zijlstra59ed446f2009-11-20 22:19:55 +01003015 *enabled = 0;
3016 *running = 0;
3017
Peter Zijlstra6f105812009-11-20 22:19:56 +01003018 mutex_lock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003019 total += perf_event_read(event);
Peter Zijlstra59ed446f2009-11-20 22:19:55 +01003020 *enabled += event->total_time_enabled +
3021 atomic64_read(&event->child_total_time_enabled);
3022 *running += event->total_time_running +
3023 atomic64_read(&event->child_total_time_running);
3024
3025 list_for_each_entry(child, &event->child_list, child_list) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003026 total += perf_event_read(child);
Peter Zijlstra59ed446f2009-11-20 22:19:55 +01003027 *enabled += child->total_time_enabled;
3028 *running += child->total_time_running;
3029 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01003030 mutex_unlock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003031
3032 return total;
3033}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003034EXPORT_SYMBOL_GPL(perf_event_read_value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003035
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003036static int perf_event_read_group(struct perf_event *event,
3037 u64 read_format, char __user *buf)
3038{
3039 struct perf_event *leader = event->group_leader, *sub;
Peter Zijlstra6f105812009-11-20 22:19:56 +01003040 int n = 0, size = 0, ret = -EFAULT;
3041 struct perf_event_context *ctx = leader->ctx;
Peter Zijlstraabf48682009-11-20 22:19:49 +01003042 u64 values[5];
Peter Zijlstra59ed446f2009-11-20 22:19:55 +01003043 u64 count, enabled, running;
Peter Zijlstraabf48682009-11-20 22:19:49 +01003044
Peter Zijlstra6f105812009-11-20 22:19:56 +01003045 mutex_lock(&ctx->mutex);
Peter Zijlstra59ed446f2009-11-20 22:19:55 +01003046 count = perf_event_read_value(leader, &enabled, &running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003047
3048 values[n++] = 1 + leader->nr_siblings;
Peter Zijlstra59ed446f2009-11-20 22:19:55 +01003049 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3050 values[n++] = enabled;
3051 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3052 values[n++] = running;
Peter Zijlstraabf48682009-11-20 22:19:49 +01003053 values[n++] = count;
3054 if (read_format & PERF_FORMAT_ID)
3055 values[n++] = primary_event_id(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003056
3057 size = n * sizeof(u64);
3058
3059 if (copy_to_user(buf, values, size))
Peter Zijlstra6f105812009-11-20 22:19:56 +01003060 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003061
Peter Zijlstra6f105812009-11-20 22:19:56 +01003062 ret = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003063
3064 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
Peter Zijlstraabf48682009-11-20 22:19:49 +01003065 n = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003066
Peter Zijlstra59ed446f2009-11-20 22:19:55 +01003067 values[n++] = perf_event_read_value(sub, &enabled, &running);
Peter Zijlstraabf48682009-11-20 22:19:49 +01003068 if (read_format & PERF_FORMAT_ID)
3069 values[n++] = primary_event_id(sub);
3070
3071 size = n * sizeof(u64);
3072
Stephane Eranian184d3da2009-11-23 21:40:49 -08003073 if (copy_to_user(buf + ret, values, size)) {
Peter Zijlstra6f105812009-11-20 22:19:56 +01003074 ret = -EFAULT;
3075 goto unlock;
3076 }
Peter Zijlstraabf48682009-11-20 22:19:49 +01003077
3078 ret += size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003079 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01003080unlock:
3081 mutex_unlock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003082
Peter Zijlstraabf48682009-11-20 22:19:49 +01003083 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003084}
3085
3086static int perf_event_read_one(struct perf_event *event,
3087 u64 read_format, char __user *buf)
3088{
Peter Zijlstra59ed446f2009-11-20 22:19:55 +01003089 u64 enabled, running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003090 u64 values[4];
3091 int n = 0;
3092
Peter Zijlstra59ed446f2009-11-20 22:19:55 +01003093 values[n++] = perf_event_read_value(event, &enabled, &running);
3094 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3095 values[n++] = enabled;
3096 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3097 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003098 if (read_format & PERF_FORMAT_ID)
3099 values[n++] = primary_event_id(event);
3100
3101 if (copy_to_user(buf, values, n * sizeof(u64)))
3102 return -EFAULT;
3103
3104 return n * sizeof(u64);
3105}
3106
3107/*
3108 * Read the performance event - simple non blocking version for now
3109 */
3110static ssize_t
3111perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
3112{
3113 u64 read_format = event->attr.read_format;
3114 int ret;
3115
3116 /*
3117 * Return end-of-file for a read on a event that is in
3118 * error state (i.e. because it was pinned but it couldn't be
3119 * scheduled on to the CPU at some point).
3120 */
3121 if (event->state == PERF_EVENT_STATE_ERROR)
3122 return 0;
3123
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02003124 if (count < event->read_size)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003125 return -ENOSPC;
3126
3127 WARN_ON_ONCE(event->ctx->parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003128 if (read_format & PERF_FORMAT_GROUP)
3129 ret = perf_event_read_group(event, read_format, buf);
3130 else
3131 ret = perf_event_read_one(event, read_format, buf);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003132
3133 return ret;
3134}
3135
3136static ssize_t
3137perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
3138{
3139 struct perf_event *event = file->private_data;
3140
3141 return perf_read_hw(event, buf, count);
3142}
3143
3144static unsigned int perf_poll(struct file *file, poll_table *wait)
3145{
3146 struct perf_event *event = file->private_data;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003147 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003148 unsigned int events = POLL_HUP;
3149
3150 rcu_read_lock();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003151 buffer = rcu_dereference(event->buffer);
3152 if (buffer)
3153 events = atomic_xchg(&buffer->poll, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003154 rcu_read_unlock();
3155
3156 poll_wait(file, &event->waitq, wait);
3157
3158 return events;
3159}
3160
3161static void perf_event_reset(struct perf_event *event)
3162{
3163 (void)perf_event_read(event);
Peter Zijlstrae7850592010-05-21 14:43:08 +02003164 local64_set(&event->count, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003165 perf_event_update_userpage(event);
3166}
3167
3168/*
3169 * Holding the top-level event's child_mutex means that any
3170 * descendant process that has inherited this event will block
3171 * in sync_child_event if it goes to exit, thus satisfying the
3172 * task existence requirements of perf_event_enable/disable.
3173 */
3174static void perf_event_for_each_child(struct perf_event *event,
3175 void (*func)(struct perf_event *))
3176{
3177 struct perf_event *child;
3178
3179 WARN_ON_ONCE(event->ctx->parent_ctx);
3180 mutex_lock(&event->child_mutex);
3181 func(event);
3182 list_for_each_entry(child, &event->child_list, child_list)
3183 func(child);
3184 mutex_unlock(&event->child_mutex);
3185}
3186
3187static void perf_event_for_each(struct perf_event *event,
3188 void (*func)(struct perf_event *))
3189{
3190 struct perf_event_context *ctx = event->ctx;
3191 struct perf_event *sibling;
3192
3193 WARN_ON_ONCE(ctx->parent_ctx);
3194 mutex_lock(&ctx->mutex);
3195 event = event->group_leader;
3196
3197 perf_event_for_each_child(event, func);
3198 func(event);
3199 list_for_each_entry(sibling, &event->sibling_list, group_entry)
3200 perf_event_for_each_child(event, func);
3201 mutex_unlock(&ctx->mutex);
3202}
3203
3204static int perf_event_period(struct perf_event *event, u64 __user *arg)
3205{
3206 struct perf_event_context *ctx = event->ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003207 int ret = 0;
3208 u64 value;
3209
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01003210 if (!is_sampling_event(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003211 return -EINVAL;
3212
John Blackwoodad0cf342010-09-28 18:03:11 -04003213 if (copy_from_user(&value, arg, sizeof(value)))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003214 return -EFAULT;
3215
3216 if (!value)
3217 return -EINVAL;
3218
Thomas Gleixnere625cce2009-11-17 18:02:06 +01003219 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003220 if (event->attr.freq) {
3221 if (value > sysctl_perf_event_sample_rate) {
3222 ret = -EINVAL;
3223 goto unlock;
3224 }
3225
3226 event->attr.sample_freq = value;
3227 } else {
3228 event->attr.sample_period = value;
3229 event->hw.sample_period = value;
3230 }
3231unlock:
Thomas Gleixnere625cce2009-11-17 18:02:06 +01003232 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003233
3234 return ret;
3235}
3236
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003237static const struct file_operations perf_fops;
3238
3239static struct perf_event *perf_fget_light(int fd, int *fput_needed)
3240{
3241 struct file *file;
3242
3243 file = fget_light(fd, fput_needed);
3244 if (!file)
3245 return ERR_PTR(-EBADF);
3246
3247 if (file->f_op != &perf_fops) {
3248 fput_light(file, *fput_needed);
3249 *fput_needed = 0;
3250 return ERR_PTR(-EBADF);
3251 }
3252
3253 return file->private_data;
3254}
3255
3256static int perf_event_set_output(struct perf_event *event,
3257 struct perf_event *output_event);
Li Zefan6fb29152009-10-15 11:21:42 +08003258static int perf_event_set_filter(struct perf_event *event, void __user *arg);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003259
3260static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3261{
3262 struct perf_event *event = file->private_data;
3263 void (*func)(struct perf_event *);
3264 u32 flags = arg;
3265
3266 switch (cmd) {
3267 case PERF_EVENT_IOC_ENABLE:
3268 func = perf_event_enable;
3269 break;
3270 case PERF_EVENT_IOC_DISABLE:
3271 func = perf_event_disable;
3272 break;
3273 case PERF_EVENT_IOC_RESET:
3274 func = perf_event_reset;
3275 break;
3276
3277 case PERF_EVENT_IOC_REFRESH:
3278 return perf_event_refresh(event, arg);
3279
3280 case PERF_EVENT_IOC_PERIOD:
3281 return perf_event_period(event, (u64 __user *)arg);
3282
3283 case PERF_EVENT_IOC_SET_OUTPUT:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003284 {
3285 struct perf_event *output_event = NULL;
3286 int fput_needed = 0;
3287 int ret;
3288
3289 if (arg != -1) {
3290 output_event = perf_fget_light(arg, &fput_needed);
3291 if (IS_ERR(output_event))
3292 return PTR_ERR(output_event);
3293 }
3294
3295 ret = perf_event_set_output(event, output_event);
3296 if (output_event)
3297 fput_light(output_event->filp, fput_needed);
3298
3299 return ret;
3300 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003301
Li Zefan6fb29152009-10-15 11:21:42 +08003302 case PERF_EVENT_IOC_SET_FILTER:
3303 return perf_event_set_filter(event, (void __user *)arg);
3304
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003305 default:
3306 return -ENOTTY;
3307 }
3308
3309 if (flags & PERF_IOC_FLAG_GROUP)
3310 perf_event_for_each(event, func);
3311 else
3312 perf_event_for_each_child(event, func);
3313
3314 return 0;
3315}
3316
3317int perf_event_task_enable(void)
3318{
3319 struct perf_event *event;
3320
3321 mutex_lock(&current->perf_event_mutex);
3322 list_for_each_entry(event, &current->perf_event_list, owner_entry)
3323 perf_event_for_each_child(event, perf_event_enable);
3324 mutex_unlock(&current->perf_event_mutex);
3325
3326 return 0;
3327}
3328
3329int perf_event_task_disable(void)
3330{
3331 struct perf_event *event;
3332
3333 mutex_lock(&current->perf_event_mutex);
3334 list_for_each_entry(event, &current->perf_event_list, owner_entry)
3335 perf_event_for_each_child(event, perf_event_disable);
3336 mutex_unlock(&current->perf_event_mutex);
3337
3338 return 0;
3339}
3340
3341#ifndef PERF_EVENT_INDEX_OFFSET
3342# define PERF_EVENT_INDEX_OFFSET 0
3343#endif
3344
3345static int perf_event_index(struct perf_event *event)
3346{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02003347 if (event->hw.state & PERF_HES_STOPPED)
3348 return 0;
3349
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003350 if (event->state != PERF_EVENT_STATE_ACTIVE)
3351 return 0;
3352
3353 return event->hw.idx + 1 - PERF_EVENT_INDEX_OFFSET;
3354}
3355
3356/*
3357 * Callers need to ensure there can be no nesting of this function, otherwise
3358 * the seqlock logic goes bad. We can not serialize this because the arch
3359 * code calls this from NMI context.
3360 */
3361void perf_event_update_userpage(struct perf_event *event)
3362{
3363 struct perf_event_mmap_page *userpg;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003364 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003365
3366 rcu_read_lock();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003367 buffer = rcu_dereference(event->buffer);
3368 if (!buffer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003369 goto unlock;
3370
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003371 userpg = buffer->user_page;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003372
3373 /*
3374 * Disable preemption so as to not let the corresponding user-space
3375 * spin too long if we get preempted.
3376 */
3377 preempt_disable();
3378 ++userpg->lock;
3379 barrier();
3380 userpg->index = perf_event_index(event);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003381 userpg->offset = perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003382 if (event->state == PERF_EVENT_STATE_ACTIVE)
Peter Zijlstrae7850592010-05-21 14:43:08 +02003383 userpg->offset -= local64_read(&event->hw.prev_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003384
3385 userpg->time_enabled = event->total_time_enabled +
3386 atomic64_read(&event->child_total_time_enabled);
3387
3388 userpg->time_running = event->total_time_running +
3389 atomic64_read(&event->child_total_time_running);
3390
3391 barrier();
3392 ++userpg->lock;
3393 preempt_enable();
3394unlock:
3395 rcu_read_unlock();
3396}
3397
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02003398static unsigned long perf_data_size(struct perf_buffer *buffer);
3399
3400static void
3401perf_buffer_init(struct perf_buffer *buffer, long watermark, int flags)
3402{
3403 long max_size = perf_data_size(buffer);
3404
3405 if (watermark)
3406 buffer->watermark = min(max_size, watermark);
3407
3408 if (!buffer->watermark)
3409 buffer->watermark = max_size / 2;
3410
3411 if (flags & PERF_BUFFER_WRITABLE)
3412 buffer->writable = 1;
3413
3414 atomic_set(&buffer->refcount, 1);
3415}
3416
Peter Zijlstra906010b2009-09-21 16:08:49 +02003417#ifndef CONFIG_PERF_USE_VMALLOC
3418
3419/*
3420 * Back perf_mmap() with regular GFP_KERNEL-0 pages.
3421 */
3422
3423static struct page *
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003424perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff)
Peter Zijlstra906010b2009-09-21 16:08:49 +02003425{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003426 if (pgoff > buffer->nr_pages)
Peter Zijlstra906010b2009-09-21 16:08:49 +02003427 return NULL;
3428
3429 if (pgoff == 0)
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003430 return virt_to_page(buffer->user_page);
Peter Zijlstra906010b2009-09-21 16:08:49 +02003431
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003432 return virt_to_page(buffer->data_pages[pgoff - 1]);
Peter Zijlstra906010b2009-09-21 16:08:49 +02003433}
3434
Peter Zijlstraa19d35c2010-05-17 18:48:00 +02003435static void *perf_mmap_alloc_page(int cpu)
3436{
3437 struct page *page;
3438 int node;
3439
3440 node = (cpu == -1) ? cpu : cpu_to_node(cpu);
3441 page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
3442 if (!page)
3443 return NULL;
3444
3445 return page_address(page);
3446}
3447
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003448static struct perf_buffer *
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02003449perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003450{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003451 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003452 unsigned long size;
3453 int i;
3454
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003455 size = sizeof(struct perf_buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003456 size += nr_pages * sizeof(void *);
3457
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003458 buffer = kzalloc(size, GFP_KERNEL);
3459 if (!buffer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003460 goto fail;
3461
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02003462 buffer->user_page = perf_mmap_alloc_page(cpu);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003463 if (!buffer->user_page)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003464 goto fail_user_page;
3465
3466 for (i = 0; i < nr_pages; i++) {
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02003467 buffer->data_pages[i] = perf_mmap_alloc_page(cpu);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003468 if (!buffer->data_pages[i])
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003469 goto fail_data_pages;
3470 }
3471
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003472 buffer->nr_pages = nr_pages;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003473
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02003474 perf_buffer_init(buffer, watermark, flags);
3475
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003476 return buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003477
3478fail_data_pages:
3479 for (i--; i >= 0; i--)
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003480 free_page((unsigned long)buffer->data_pages[i]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003481
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003482 free_page((unsigned long)buffer->user_page);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003483
3484fail_user_page:
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003485 kfree(buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003486
3487fail:
Peter Zijlstra906010b2009-09-21 16:08:49 +02003488 return NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003489}
3490
3491static void perf_mmap_free_page(unsigned long addr)
3492{
3493 struct page *page = virt_to_page((void *)addr);
3494
3495 page->mapping = NULL;
3496 __free_page(page);
3497}
3498
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003499static void perf_buffer_free(struct perf_buffer *buffer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003500{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003501 int i;
3502
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003503 perf_mmap_free_page((unsigned long)buffer->user_page);
3504 for (i = 0; i < buffer->nr_pages; i++)
3505 perf_mmap_free_page((unsigned long)buffer->data_pages[i]);
3506 kfree(buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02003507}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003508
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003509static inline int page_order(struct perf_buffer *buffer)
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02003510{
3511 return 0;
3512}
3513
Peter Zijlstra906010b2009-09-21 16:08:49 +02003514#else
3515
3516/*
3517 * Back perf_mmap() with vmalloc memory.
3518 *
3519 * Required for architectures that have d-cache aliasing issues.
3520 */
3521
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003522static inline int page_order(struct perf_buffer *buffer)
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02003523{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003524 return buffer->page_order;
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02003525}
3526
Peter Zijlstra906010b2009-09-21 16:08:49 +02003527static struct page *
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003528perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff)
Peter Zijlstra906010b2009-09-21 16:08:49 +02003529{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003530 if (pgoff > (1UL << page_order(buffer)))
Peter Zijlstra906010b2009-09-21 16:08:49 +02003531 return NULL;
3532
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003533 return vmalloc_to_page((void *)buffer->user_page + pgoff * PAGE_SIZE);
Peter Zijlstra906010b2009-09-21 16:08:49 +02003534}
3535
3536static void perf_mmap_unmark_page(void *addr)
3537{
3538 struct page *page = vmalloc_to_page(addr);
3539
3540 page->mapping = NULL;
3541}
3542
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003543static void perf_buffer_free_work(struct work_struct *work)
Peter Zijlstra906010b2009-09-21 16:08:49 +02003544{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003545 struct perf_buffer *buffer;
Peter Zijlstra906010b2009-09-21 16:08:49 +02003546 void *base;
3547 int i, nr;
3548
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003549 buffer = container_of(work, struct perf_buffer, work);
3550 nr = 1 << page_order(buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02003551
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003552 base = buffer->user_page;
Peter Zijlstra906010b2009-09-21 16:08:49 +02003553 for (i = 0; i < nr + 1; i++)
3554 perf_mmap_unmark_page(base + (i * PAGE_SIZE));
3555
3556 vfree(base);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003557 kfree(buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02003558}
3559
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003560static void perf_buffer_free(struct perf_buffer *buffer)
Peter Zijlstra906010b2009-09-21 16:08:49 +02003561{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003562 schedule_work(&buffer->work);
Peter Zijlstra906010b2009-09-21 16:08:49 +02003563}
3564
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003565static struct perf_buffer *
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02003566perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags)
Peter Zijlstra906010b2009-09-21 16:08:49 +02003567{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003568 struct perf_buffer *buffer;
Peter Zijlstra906010b2009-09-21 16:08:49 +02003569 unsigned long size;
3570 void *all_buf;
3571
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003572 size = sizeof(struct perf_buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02003573 size += sizeof(void *);
3574
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003575 buffer = kzalloc(size, GFP_KERNEL);
3576 if (!buffer)
Peter Zijlstra906010b2009-09-21 16:08:49 +02003577 goto fail;
3578
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003579 INIT_WORK(&buffer->work, perf_buffer_free_work);
Peter Zijlstra906010b2009-09-21 16:08:49 +02003580
3581 all_buf = vmalloc_user((nr_pages + 1) * PAGE_SIZE);
3582 if (!all_buf)
3583 goto fail_all_buf;
3584
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003585 buffer->user_page = all_buf;
3586 buffer->data_pages[0] = all_buf + PAGE_SIZE;
3587 buffer->page_order = ilog2(nr_pages);
3588 buffer->nr_pages = 1;
Peter Zijlstra906010b2009-09-21 16:08:49 +02003589
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02003590 perf_buffer_init(buffer, watermark, flags);
3591
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003592 return buffer;
Peter Zijlstra906010b2009-09-21 16:08:49 +02003593
3594fail_all_buf:
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003595 kfree(buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02003596
3597fail:
3598 return NULL;
3599}
3600
3601#endif
3602
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003603static unsigned long perf_data_size(struct perf_buffer *buffer)
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02003604{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003605 return buffer->nr_pages << (PAGE_SHIFT + page_order(buffer));
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02003606}
3607
Peter Zijlstra906010b2009-09-21 16:08:49 +02003608static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
3609{
3610 struct perf_event *event = vma->vm_file->private_data;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003611 struct perf_buffer *buffer;
Peter Zijlstra906010b2009-09-21 16:08:49 +02003612 int ret = VM_FAULT_SIGBUS;
3613
3614 if (vmf->flags & FAULT_FLAG_MKWRITE) {
3615 if (vmf->pgoff == 0)
3616 ret = 0;
3617 return ret;
3618 }
3619
3620 rcu_read_lock();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003621 buffer = rcu_dereference(event->buffer);
3622 if (!buffer)
Peter Zijlstra906010b2009-09-21 16:08:49 +02003623 goto unlock;
3624
3625 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
3626 goto unlock;
3627
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003628 vmf->page = perf_mmap_to_page(buffer, vmf->pgoff);
Peter Zijlstra906010b2009-09-21 16:08:49 +02003629 if (!vmf->page)
3630 goto unlock;
3631
3632 get_page(vmf->page);
3633 vmf->page->mapping = vma->vm_file->f_mapping;
3634 vmf->page->index = vmf->pgoff;
3635
3636 ret = 0;
3637unlock:
3638 rcu_read_unlock();
3639
3640 return ret;
3641}
3642
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003643static void perf_buffer_free_rcu(struct rcu_head *rcu_head)
Peter Zijlstra906010b2009-09-21 16:08:49 +02003644{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003645 struct perf_buffer *buffer;
Peter Zijlstra906010b2009-09-21 16:08:49 +02003646
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003647 buffer = container_of(rcu_head, struct perf_buffer, rcu_head);
3648 perf_buffer_free(buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003649}
3650
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003651static struct perf_buffer *perf_buffer_get(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003652{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003653 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003654
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003655 rcu_read_lock();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003656 buffer = rcu_dereference(event->buffer);
3657 if (buffer) {
3658 if (!atomic_inc_not_zero(&buffer->refcount))
3659 buffer = NULL;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003660 }
3661 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003662
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003663 return buffer;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003664}
3665
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003666static void perf_buffer_put(struct perf_buffer *buffer)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003667{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003668 if (!atomic_dec_and_test(&buffer->refcount))
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003669 return;
3670
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003671 call_rcu(&buffer->rcu_head, perf_buffer_free_rcu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003672}
3673
3674static void perf_mmap_open(struct vm_area_struct *vma)
3675{
3676 struct perf_event *event = vma->vm_file->private_data;
3677
3678 atomic_inc(&event->mmap_count);
3679}
3680
3681static void perf_mmap_close(struct vm_area_struct *vma)
3682{
3683 struct perf_event *event = vma->vm_file->private_data;
3684
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003685 if (atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) {
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003686 unsigned long size = perf_data_size(event->buffer);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003687 struct user_struct *user = event->mmap_user;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003688 struct perf_buffer *buffer = event->buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003689
Peter Zijlstra906010b2009-09-21 16:08:49 +02003690 atomic_long_sub((size >> PAGE_SHIFT) + 1, &user->locked_vm);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003691 vma->vm_mm->locked_vm -= event->mmap_locked;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003692 rcu_assign_pointer(event->buffer, NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003693 mutex_unlock(&event->mmap_mutex);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003694
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003695 perf_buffer_put(buffer);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003696 free_uid(user);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003697 }
3698}
3699
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04003700static const struct vm_operations_struct perf_mmap_vmops = {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003701 .open = perf_mmap_open,
3702 .close = perf_mmap_close,
3703 .fault = perf_mmap_fault,
3704 .page_mkwrite = perf_mmap_fault,
3705};
3706
3707static int perf_mmap(struct file *file, struct vm_area_struct *vma)
3708{
3709 struct perf_event *event = file->private_data;
3710 unsigned long user_locked, user_lock_limit;
3711 struct user_struct *user = current_user();
3712 unsigned long locked, lock_limit;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003713 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003714 unsigned long vma_size;
3715 unsigned long nr_pages;
3716 long user_extra, extra;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02003717 int ret = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003718
Peter Zijlstrac7920612010-05-18 10:33:24 +02003719 /*
3720 * Don't allow mmap() of inherited per-task counters. This would
3721 * create a performance issue due to all children writing to the
3722 * same buffer.
3723 */
3724 if (event->cpu == -1 && event->attr.inherit)
3725 return -EINVAL;
3726
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003727 if (!(vma->vm_flags & VM_SHARED))
3728 return -EINVAL;
3729
3730 vma_size = vma->vm_end - vma->vm_start;
3731 nr_pages = (vma_size / PAGE_SIZE) - 1;
3732
3733 /*
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003734 * If we have buffer pages ensure they're a power-of-two number, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003735 * can do bitmasks instead of modulo.
3736 */
3737 if (nr_pages != 0 && !is_power_of_2(nr_pages))
3738 return -EINVAL;
3739
3740 if (vma_size != PAGE_SIZE * (1 + nr_pages))
3741 return -EINVAL;
3742
3743 if (vma->vm_pgoff != 0)
3744 return -EINVAL;
3745
3746 WARN_ON_ONCE(event->ctx->parent_ctx);
3747 mutex_lock(&event->mmap_mutex);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003748 if (event->buffer) {
3749 if (event->buffer->nr_pages == nr_pages)
3750 atomic_inc(&event->buffer->refcount);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003751 else
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003752 ret = -EINVAL;
3753 goto unlock;
3754 }
3755
3756 user_extra = nr_pages + 1;
3757 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
3758
3759 /*
3760 * Increase the limit linearly with more CPUs:
3761 */
3762 user_lock_limit *= num_online_cpus();
3763
3764 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
3765
3766 extra = 0;
3767 if (user_locked > user_lock_limit)
3768 extra = user_locked - user_lock_limit;
3769
Jiri Slaby78d7d402010-03-05 13:42:54 -08003770 lock_limit = rlimit(RLIMIT_MEMLOCK);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003771 lock_limit >>= PAGE_SHIFT;
3772 locked = vma->vm_mm->locked_vm + extra;
3773
3774 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
3775 !capable(CAP_IPC_LOCK)) {
3776 ret = -EPERM;
3777 goto unlock;
3778 }
3779
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003780 WARN_ON(event->buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02003781
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02003782 if (vma->vm_flags & VM_WRITE)
3783 flags |= PERF_BUFFER_WRITABLE;
3784
3785 buffer = perf_buffer_alloc(nr_pages, event->attr.wakeup_watermark,
3786 event->cpu, flags);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003787 if (!buffer) {
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003788 ret = -ENOMEM;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003789 goto unlock;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003790 }
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02003791 rcu_assign_pointer(event->buffer, buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003792
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003793 atomic_long_add(user_extra, &user->locked_vm);
3794 event->mmap_locked = extra;
3795 event->mmap_user = get_current_user();
3796 vma->vm_mm->locked_vm += event->mmap_locked;
3797
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003798unlock:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003799 if (!ret)
3800 atomic_inc(&event->mmap_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003801 mutex_unlock(&event->mmap_mutex);
3802
3803 vma->vm_flags |= VM_RESERVED;
3804 vma->vm_ops = &perf_mmap_vmops;
3805
3806 return ret;
3807}
3808
3809static int perf_fasync(int fd, struct file *filp, int on)
3810{
3811 struct inode *inode = filp->f_path.dentry->d_inode;
3812 struct perf_event *event = filp->private_data;
3813 int retval;
3814
3815 mutex_lock(&inode->i_mutex);
3816 retval = fasync_helper(fd, filp, on, &event->fasync);
3817 mutex_unlock(&inode->i_mutex);
3818
3819 if (retval < 0)
3820 return retval;
3821
3822 return 0;
3823}
3824
3825static const struct file_operations perf_fops = {
Arnd Bergmann3326c1c2010-03-23 19:09:33 +01003826 .llseek = no_llseek,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003827 .release = perf_release,
3828 .read = perf_read,
3829 .poll = perf_poll,
3830 .unlocked_ioctl = perf_ioctl,
3831 .compat_ioctl = perf_ioctl,
3832 .mmap = perf_mmap,
3833 .fasync = perf_fasync,
3834};
3835
3836/*
3837 * Perf event wakeup
3838 *
3839 * If there's data, ensure we set the poll() state and publish everything
3840 * to user-space before waking everybody up.
3841 */
3842
3843void perf_event_wakeup(struct perf_event *event)
3844{
3845 wake_up_all(&event->waitq);
3846
3847 if (event->pending_kill) {
3848 kill_fasync(&event->fasync, SIGIO, event->pending_kill);
3849 event->pending_kill = 0;
3850 }
3851}
3852
Peter Zijlstrae360adb2010-10-14 14:01:34 +08003853static void perf_pending_event(struct irq_work *entry)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003854{
3855 struct perf_event *event = container_of(entry,
3856 struct perf_event, pending);
3857
3858 if (event->pending_disable) {
3859 event->pending_disable = 0;
3860 __perf_event_disable(event);
3861 }
3862
3863 if (event->pending_wakeup) {
3864 event->pending_wakeup = 0;
3865 perf_event_wakeup(event);
3866 }
3867}
3868
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003869/*
Zhang, Yanmin39447b32010-04-19 13:32:41 +08003870 * We assume there is only KVM supporting the callbacks.
3871 * Later on, we might change it to a list if there is
3872 * another virtualization implementation supporting the callbacks.
3873 */
3874struct perf_guest_info_callbacks *perf_guest_cbs;
3875
3876int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3877{
3878 perf_guest_cbs = cbs;
3879 return 0;
3880}
3881EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
3882
3883int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3884{
3885 perf_guest_cbs = NULL;
3886 return 0;
3887}
3888EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
3889
3890/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003891 * Output
3892 */
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003893static bool perf_output_space(struct perf_buffer *buffer, unsigned long tail,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003894 unsigned long offset, unsigned long head)
3895{
3896 unsigned long mask;
3897
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003898 if (!buffer->writable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003899 return true;
3900
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003901 mask = perf_data_size(buffer) - 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003902
3903 offset = (offset - tail) & mask;
3904 head = (head - tail) & mask;
3905
3906 if ((int)(head - offset) < 0)
3907 return false;
3908
3909 return true;
3910}
3911
3912static void perf_output_wakeup(struct perf_output_handle *handle)
3913{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003914 atomic_set(&handle->buffer->poll, POLL_IN);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003915
3916 if (handle->nmi) {
3917 handle->event->pending_wakeup = 1;
Peter Zijlstrae360adb2010-10-14 14:01:34 +08003918 irq_work_queue(&handle->event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003919 } else
3920 perf_event_wakeup(handle->event);
3921}
3922
3923/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003924 * We need to ensure a later event_id doesn't publish a head when a former
Peter Zijlstraef607772010-05-18 10:50:41 +02003925 * event isn't done writing. However since we need to deal with NMIs we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003926 * cannot fully serialize things.
3927 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003928 * We only publish the head (and generate a wakeup) when the outer-most
Peter Zijlstraef607772010-05-18 10:50:41 +02003929 * event completes.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003930 */
Peter Zijlstraef607772010-05-18 10:50:41 +02003931static void perf_output_get_handle(struct perf_output_handle *handle)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003932{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003933 struct perf_buffer *buffer = handle->buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003934
Peter Zijlstraef607772010-05-18 10:50:41 +02003935 preempt_disable();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003936 local_inc(&buffer->nest);
3937 handle->wakeup = local_read(&buffer->wakeup);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003938}
3939
Peter Zijlstraef607772010-05-18 10:50:41 +02003940static void perf_output_put_handle(struct perf_output_handle *handle)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003941{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003942 struct perf_buffer *buffer = handle->buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003943 unsigned long head;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003944
3945again:
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003946 head = local_read(&buffer->head);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003947
3948 /*
Peter Zijlstraef607772010-05-18 10:50:41 +02003949 * IRQ/NMI can happen here, which means we can miss a head update.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003950 */
3951
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003952 if (!local_dec_and_test(&buffer->nest))
Frederic Weisbeckeracd35a42010-05-20 21:28:34 +02003953 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003954
3955 /*
Peter Zijlstraef607772010-05-18 10:50:41 +02003956 * Publish the known good head. Rely on the full barrier implied
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003957 * by atomic_dec_and_test() order the buffer->head read and this
Peter Zijlstraef607772010-05-18 10:50:41 +02003958 * write.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003959 */
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003960 buffer->user_page->data_head = head;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003961
Peter Zijlstraef607772010-05-18 10:50:41 +02003962 /*
3963 * Now check if we missed an update, rely on the (compiler)
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003964 * barrier in atomic_dec_and_test() to re-read buffer->head.
Peter Zijlstraef607772010-05-18 10:50:41 +02003965 */
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003966 if (unlikely(head != local_read(&buffer->head))) {
3967 local_inc(&buffer->nest);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003968 goto again;
3969 }
3970
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003971 if (handle->wakeup != local_read(&buffer->wakeup))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003972 perf_output_wakeup(handle);
Peter Zijlstraef607772010-05-18 10:50:41 +02003973
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003974out:
Peter Zijlstraef607772010-05-18 10:50:41 +02003975 preempt_enable();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003976}
3977
Peter Zijlstraa94ffaa2010-05-20 19:50:07 +02003978__always_inline void perf_output_copy(struct perf_output_handle *handle,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003979 const void *buf, unsigned int len)
3980{
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003981 do {
Peter Zijlstraa94ffaa2010-05-20 19:50:07 +02003982 unsigned long size = min_t(unsigned long, handle->size, len);
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003983
3984 memcpy(handle->addr, buf, size);
3985
3986 len -= size;
3987 handle->addr += size;
Frederic Weisbecker74048f82010-05-27 21:34:58 +02003988 buf += size;
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003989 handle->size -= size;
3990 if (!handle->size) {
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003991 struct perf_buffer *buffer = handle->buffer;
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02003992
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003993 handle->page++;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003994 handle->page &= buffer->nr_pages - 1;
3995 handle->addr = buffer->data_pages[handle->page];
3996 handle->size = PAGE_SIZE << page_order(buffer);
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003997 }
3998 } while (len);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003999}
4000
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004001static void __perf_event_header__init_id(struct perf_event_header *header,
4002 struct perf_sample_data *data,
4003 struct perf_event *event)
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02004004{
4005 u64 sample_type = event->attr.sample_type;
4006
4007 data->type = sample_type;
4008 header->size += event->id_header_size;
4009
4010 if (sample_type & PERF_SAMPLE_TID) {
4011 /* namespace issues */
4012 data->tid_entry.pid = perf_event_pid(event, current);
4013 data->tid_entry.tid = perf_event_tid(event, current);
4014 }
4015
4016 if (sample_type & PERF_SAMPLE_TIME)
4017 data->time = perf_clock();
4018
4019 if (sample_type & PERF_SAMPLE_ID)
4020 data->id = primary_event_id(event);
4021
4022 if (sample_type & PERF_SAMPLE_STREAM_ID)
4023 data->stream_id = event->id;
4024
4025 if (sample_type & PERF_SAMPLE_CPU) {
4026 data->cpu_entry.cpu = raw_smp_processor_id();
4027 data->cpu_entry.reserved = 0;
4028 }
4029}
4030
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004031static void perf_event_header__init_id(struct perf_event_header *header,
4032 struct perf_sample_data *data,
4033 struct perf_event *event)
4034{
4035 if (event->attr.sample_id_all)
4036 __perf_event_header__init_id(header, data, event);
4037}
4038
4039static void __perf_event__output_id_sample(struct perf_output_handle *handle,
4040 struct perf_sample_data *data)
4041{
4042 u64 sample_type = data->type;
4043
4044 if (sample_type & PERF_SAMPLE_TID)
4045 perf_output_put(handle, data->tid_entry);
4046
4047 if (sample_type & PERF_SAMPLE_TIME)
4048 perf_output_put(handle, data->time);
4049
4050 if (sample_type & PERF_SAMPLE_ID)
4051 perf_output_put(handle, data->id);
4052
4053 if (sample_type & PERF_SAMPLE_STREAM_ID)
4054 perf_output_put(handle, data->stream_id);
4055
4056 if (sample_type & PERF_SAMPLE_CPU)
4057 perf_output_put(handle, data->cpu_entry);
4058}
4059
4060static void perf_event__output_id_sample(struct perf_event *event,
4061 struct perf_output_handle *handle,
4062 struct perf_sample_data *sample)
4063{
4064 if (event->attr.sample_id_all)
4065 __perf_event__output_id_sample(handle, sample);
4066}
4067
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004068int perf_output_begin(struct perf_output_handle *handle,
4069 struct perf_event *event, unsigned int size,
4070 int nmi, int sample)
4071{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02004072 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004073 unsigned long tail, offset, head;
4074 int have_lost;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004075 struct perf_sample_data sample_data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004076 struct {
4077 struct perf_event_header header;
4078 u64 id;
4079 u64 lost;
4080 } lost_event;
4081
4082 rcu_read_lock();
4083 /*
4084 * For inherited events we send all the output towards the parent.
4085 */
4086 if (event->parent)
4087 event = event->parent;
4088
Peter Zijlstraca5135e2010-05-28 19:33:23 +02004089 buffer = rcu_dereference(event->buffer);
4090 if (!buffer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004091 goto out;
4092
Peter Zijlstraca5135e2010-05-28 19:33:23 +02004093 handle->buffer = buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004094 handle->event = event;
4095 handle->nmi = nmi;
4096 handle->sample = sample;
4097
Peter Zijlstraca5135e2010-05-28 19:33:23 +02004098 if (!buffer->nr_pages)
Stephane Eranian00d1d0b2010-05-17 12:46:01 +02004099 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004100
Peter Zijlstraca5135e2010-05-28 19:33:23 +02004101 have_lost = local_read(&buffer->lost);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004102 if (have_lost) {
4103 lost_event.header.size = sizeof(lost_event);
4104 perf_event_header__init_id(&lost_event.header, &sample_data,
4105 event);
4106 size += lost_event.header.size;
4107 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004108
Peter Zijlstraef607772010-05-18 10:50:41 +02004109 perf_output_get_handle(handle);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004110
4111 do {
4112 /*
4113 * Userspace could choose to issue a mb() before updating the
4114 * tail pointer. So that all reads will be completed before the
4115 * write is issued.
4116 */
Peter Zijlstraca5135e2010-05-28 19:33:23 +02004117 tail = ACCESS_ONCE(buffer->user_page->data_tail);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004118 smp_rmb();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02004119 offset = head = local_read(&buffer->head);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004120 head += size;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02004121 if (unlikely(!perf_output_space(buffer, tail, offset, head)))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004122 goto fail;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02004123 } while (local_cmpxchg(&buffer->head, offset, head) != offset);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004124
Peter Zijlstraca5135e2010-05-28 19:33:23 +02004125 if (head - local_read(&buffer->wakeup) > buffer->watermark)
4126 local_add(buffer->watermark, &buffer->wakeup);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004127
Peter Zijlstraca5135e2010-05-28 19:33:23 +02004128 handle->page = offset >> (PAGE_SHIFT + page_order(buffer));
4129 handle->page &= buffer->nr_pages - 1;
4130 handle->size = offset & ((PAGE_SIZE << page_order(buffer)) - 1);
4131 handle->addr = buffer->data_pages[handle->page];
Peter Zijlstra5d967a82010-05-20 16:46:39 +02004132 handle->addr += handle->size;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02004133 handle->size = (PAGE_SIZE << page_order(buffer)) - handle->size;
Peter Zijlstra5d967a82010-05-20 16:46:39 +02004134
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004135 if (have_lost) {
4136 lost_event.header.type = PERF_RECORD_LOST;
4137 lost_event.header.misc = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004138 lost_event.id = event->id;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02004139 lost_event.lost = local_xchg(&buffer->lost, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004140
4141 perf_output_put(handle, lost_event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004142 perf_event__output_id_sample(event, handle, &sample_data);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004143 }
4144
4145 return 0;
4146
4147fail:
Peter Zijlstraca5135e2010-05-28 19:33:23 +02004148 local_inc(&buffer->lost);
Peter Zijlstraef607772010-05-18 10:50:41 +02004149 perf_output_put_handle(handle);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004150out:
4151 rcu_read_unlock();
4152
4153 return -ENOSPC;
4154}
4155
4156void perf_output_end(struct perf_output_handle *handle)
4157{
4158 struct perf_event *event = handle->event;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02004159 struct perf_buffer *buffer = handle->buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004160
4161 int wakeup_events = event->attr.wakeup_events;
4162
4163 if (handle->sample && wakeup_events) {
Peter Zijlstraca5135e2010-05-28 19:33:23 +02004164 int events = local_inc_return(&buffer->events);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004165 if (events >= wakeup_events) {
Peter Zijlstraca5135e2010-05-28 19:33:23 +02004166 local_sub(wakeup_events, &buffer->events);
4167 local_inc(&buffer->wakeup);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004168 }
4169 }
4170
Peter Zijlstraef607772010-05-18 10:50:41 +02004171 perf_output_put_handle(handle);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004172 rcu_read_unlock();
4173}
4174
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004175static void perf_output_read_one(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02004176 struct perf_event *event,
4177 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004178{
4179 u64 read_format = event->attr.read_format;
4180 u64 values[4];
4181 int n = 0;
4182
Peter Zijlstrab5e58792010-05-21 14:43:12 +02004183 values[n++] = perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004184 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
Stephane Eranianeed01522010-10-26 16:08:01 +02004185 values[n++] = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004186 atomic64_read(&event->child_total_time_enabled);
4187 }
4188 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
Stephane Eranianeed01522010-10-26 16:08:01 +02004189 values[n++] = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004190 atomic64_read(&event->child_total_time_running);
4191 }
4192 if (read_format & PERF_FORMAT_ID)
4193 values[n++] = primary_event_id(event);
4194
4195 perf_output_copy(handle, values, n * sizeof(u64));
4196}
4197
4198/*
4199 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
4200 */
4201static void perf_output_read_group(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02004202 struct perf_event *event,
4203 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004204{
4205 struct perf_event *leader = event->group_leader, *sub;
4206 u64 read_format = event->attr.read_format;
4207 u64 values[5];
4208 int n = 0;
4209
4210 values[n++] = 1 + leader->nr_siblings;
4211
4212 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
Stephane Eranianeed01522010-10-26 16:08:01 +02004213 values[n++] = enabled;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004214
4215 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
Stephane Eranianeed01522010-10-26 16:08:01 +02004216 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004217
4218 if (leader != event)
4219 leader->pmu->read(leader);
4220
Peter Zijlstrab5e58792010-05-21 14:43:12 +02004221 values[n++] = perf_event_count(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004222 if (read_format & PERF_FORMAT_ID)
4223 values[n++] = primary_event_id(leader);
4224
4225 perf_output_copy(handle, values, n * sizeof(u64));
4226
4227 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
4228 n = 0;
4229
4230 if (sub != event)
4231 sub->pmu->read(sub);
4232
Peter Zijlstrab5e58792010-05-21 14:43:12 +02004233 values[n++] = perf_event_count(sub);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004234 if (read_format & PERF_FORMAT_ID)
4235 values[n++] = primary_event_id(sub);
4236
4237 perf_output_copy(handle, values, n * sizeof(u64));
4238 }
4239}
4240
Stephane Eranianeed01522010-10-26 16:08:01 +02004241#define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
4242 PERF_FORMAT_TOTAL_TIME_RUNNING)
4243
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004244static void perf_output_read(struct perf_output_handle *handle,
4245 struct perf_event *event)
4246{
Stephane Eranianeed01522010-10-26 16:08:01 +02004247 u64 enabled = 0, running = 0, now, ctx_time;
4248 u64 read_format = event->attr.read_format;
4249
4250 /*
4251 * compute total_time_enabled, total_time_running
4252 * based on snapshot values taken when the event
4253 * was last scheduled in.
4254 *
4255 * we cannot simply called update_context_time()
4256 * because of locking issue as we are called in
4257 * NMI context
4258 */
4259 if (read_format & PERF_FORMAT_TOTAL_TIMES) {
4260 now = perf_clock();
4261 ctx_time = event->shadow_ctx_time + now;
4262 enabled = ctx_time - event->tstamp_enabled;
4263 running = ctx_time - event->tstamp_running;
4264 }
4265
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004266 if (event->attr.read_format & PERF_FORMAT_GROUP)
Stephane Eranianeed01522010-10-26 16:08:01 +02004267 perf_output_read_group(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004268 else
Stephane Eranianeed01522010-10-26 16:08:01 +02004269 perf_output_read_one(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004270}
4271
4272void perf_output_sample(struct perf_output_handle *handle,
4273 struct perf_event_header *header,
4274 struct perf_sample_data *data,
4275 struct perf_event *event)
4276{
4277 u64 sample_type = data->type;
4278
4279 perf_output_put(handle, *header);
4280
4281 if (sample_type & PERF_SAMPLE_IP)
4282 perf_output_put(handle, data->ip);
4283
4284 if (sample_type & PERF_SAMPLE_TID)
4285 perf_output_put(handle, data->tid_entry);
4286
4287 if (sample_type & PERF_SAMPLE_TIME)
4288 perf_output_put(handle, data->time);
4289
4290 if (sample_type & PERF_SAMPLE_ADDR)
4291 perf_output_put(handle, data->addr);
4292
4293 if (sample_type & PERF_SAMPLE_ID)
4294 perf_output_put(handle, data->id);
4295
4296 if (sample_type & PERF_SAMPLE_STREAM_ID)
4297 perf_output_put(handle, data->stream_id);
4298
4299 if (sample_type & PERF_SAMPLE_CPU)
4300 perf_output_put(handle, data->cpu_entry);
4301
4302 if (sample_type & PERF_SAMPLE_PERIOD)
4303 perf_output_put(handle, data->period);
4304
4305 if (sample_type & PERF_SAMPLE_READ)
4306 perf_output_read(handle, event);
4307
4308 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
4309 if (data->callchain) {
4310 int size = 1;
4311
4312 if (data->callchain)
4313 size += data->callchain->nr;
4314
4315 size *= sizeof(u64);
4316
4317 perf_output_copy(handle, data->callchain, size);
4318 } else {
4319 u64 nr = 0;
4320 perf_output_put(handle, nr);
4321 }
4322 }
4323
4324 if (sample_type & PERF_SAMPLE_RAW) {
4325 if (data->raw) {
4326 perf_output_put(handle, data->raw->size);
4327 perf_output_copy(handle, data->raw->data,
4328 data->raw->size);
4329 } else {
4330 struct {
4331 u32 size;
4332 u32 data;
4333 } raw = {
4334 .size = sizeof(u32),
4335 .data = 0,
4336 };
4337 perf_output_put(handle, raw);
4338 }
4339 }
4340}
4341
4342void perf_prepare_sample(struct perf_event_header *header,
4343 struct perf_sample_data *data,
4344 struct perf_event *event,
4345 struct pt_regs *regs)
4346{
4347 u64 sample_type = event->attr.sample_type;
4348
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004349 header->type = PERF_RECORD_SAMPLE;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02004350 header->size = sizeof(*header) + event->header_size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004351
4352 header->misc = 0;
4353 header->misc |= perf_misc_flags(regs);
4354
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004355 __perf_event_header__init_id(header, data, event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02004356
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02004357 if (sample_type & PERF_SAMPLE_IP)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004358 data->ip = perf_instruction_pointer(regs);
4359
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004360 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
4361 int size = 1;
4362
4363 data->callchain = perf_callchain(regs);
4364
4365 if (data->callchain)
4366 size += data->callchain->nr;
4367
4368 header->size += size * sizeof(u64);
4369 }
4370
4371 if (sample_type & PERF_SAMPLE_RAW) {
4372 int size = sizeof(u32);
4373
4374 if (data->raw)
4375 size += data->raw->size;
4376 else
4377 size += sizeof(u32);
4378
4379 WARN_ON_ONCE(size & (sizeof(u64)-1));
4380 header->size += size;
4381 }
4382}
4383
4384static void perf_event_output(struct perf_event *event, int nmi,
4385 struct perf_sample_data *data,
4386 struct pt_regs *regs)
4387{
4388 struct perf_output_handle handle;
4389 struct perf_event_header header;
4390
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02004391 /* protect the callchain buffers */
4392 rcu_read_lock();
4393
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004394 perf_prepare_sample(&header, data, event, regs);
4395
4396 if (perf_output_begin(&handle, event, header.size, nmi, 1))
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02004397 goto exit;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004398
4399 perf_output_sample(&handle, &header, data, event);
4400
4401 perf_output_end(&handle);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02004402
4403exit:
4404 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004405}
4406
4407/*
4408 * read event_id
4409 */
4410
4411struct perf_read_event {
4412 struct perf_event_header header;
4413
4414 u32 pid;
4415 u32 tid;
4416};
4417
4418static void
4419perf_event_read_event(struct perf_event *event,
4420 struct task_struct *task)
4421{
4422 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004423 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004424 struct perf_read_event read_event = {
4425 .header = {
4426 .type = PERF_RECORD_READ,
4427 .misc = 0,
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02004428 .size = sizeof(read_event) + event->read_size,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004429 },
4430 .pid = perf_event_pid(event, task),
4431 .tid = perf_event_tid(event, task),
4432 };
4433 int ret;
4434
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004435 perf_event_header__init_id(&read_event.header, &sample, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004436 ret = perf_output_begin(&handle, event, read_event.header.size, 0, 0);
4437 if (ret)
4438 return;
4439
4440 perf_output_put(&handle, read_event);
4441 perf_output_read(&handle, event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004442 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004443
4444 perf_output_end(&handle);
4445}
4446
4447/*
4448 * task tracking -- fork/exit
4449 *
Eric B Munson3af9e852010-05-18 15:30:49 +01004450 * enabled by: attr.comm | attr.mmap | attr.mmap_data | attr.task
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004451 */
4452
4453struct perf_task_event {
4454 struct task_struct *task;
4455 struct perf_event_context *task_ctx;
4456
4457 struct {
4458 struct perf_event_header header;
4459
4460 u32 pid;
4461 u32 ppid;
4462 u32 tid;
4463 u32 ptid;
4464 u64 time;
4465 } event_id;
4466};
4467
4468static void perf_event_task_output(struct perf_event *event,
4469 struct perf_task_event *task_event)
4470{
4471 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004472 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004473 struct task_struct *task = task_event->task;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004474 int ret, size = task_event->event_id.header.size;
Mike Galbraith8bb39f92010-03-26 11:11:33 +01004475
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004476 perf_event_header__init_id(&task_event->event_id.header, &sample, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004477
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004478 ret = perf_output_begin(&handle, event,
4479 task_event->event_id.header.size, 0, 0);
Peter Zijlstraef607772010-05-18 10:50:41 +02004480 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004481 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004482
4483 task_event->event_id.pid = perf_event_pid(event, task);
4484 task_event->event_id.ppid = perf_event_pid(event, current);
4485
4486 task_event->event_id.tid = perf_event_tid(event, task);
4487 task_event->event_id.ptid = perf_event_tid(event, current);
4488
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004489 perf_output_put(&handle, task_event->event_id);
4490
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004491 perf_event__output_id_sample(event, &handle, &sample);
4492
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004493 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004494out:
4495 task_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004496}
4497
4498static int perf_event_task_match(struct perf_event *event)
4499{
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01004500 if (event->state < PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra22e19082010-01-18 09:12:32 +01004501 return 0;
4502
Stephane Eranian5632ab12011-01-03 18:20:01 +02004503 if (!event_filter_match(event))
Peter Zijlstra5d27c232009-12-17 13:16:32 +01004504 return 0;
4505
Eric B Munson3af9e852010-05-18 15:30:49 +01004506 if (event->attr.comm || event->attr.mmap ||
4507 event->attr.mmap_data || event->attr.task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004508 return 1;
4509
4510 return 0;
4511}
4512
4513static void perf_event_task_ctx(struct perf_event_context *ctx,
4514 struct perf_task_event *task_event)
4515{
4516 struct perf_event *event;
4517
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004518 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4519 if (perf_event_task_match(event))
4520 perf_event_task_output(event, task_event);
4521 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004522}
4523
4524static void perf_event_task_event(struct perf_task_event *task_event)
4525{
4526 struct perf_cpu_context *cpuctx;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004527 struct perf_event_context *ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004528 struct pmu *pmu;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004529 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004530
Peter Zijlstrad6ff86c2009-11-20 22:19:46 +01004531 rcu_read_lock();
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004532 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra41945f62010-09-16 19:17:24 +02004533 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra51676952010-12-07 14:18:20 +01004534 if (cpuctx->active_pmu != pmu)
4535 goto next;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004536 perf_event_task_ctx(&cpuctx->ctx, task_event);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004537
4538 ctx = task_event->task_ctx;
4539 if (!ctx) {
4540 ctxn = pmu->task_ctx_nr;
4541 if (ctxn < 0)
Peter Zijlstra41945f62010-09-16 19:17:24 +02004542 goto next;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004543 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4544 }
4545 if (ctx)
4546 perf_event_task_ctx(ctx, task_event);
Peter Zijlstra41945f62010-09-16 19:17:24 +02004547next:
4548 put_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004549 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004550 rcu_read_unlock();
4551}
4552
4553static void perf_event_task(struct task_struct *task,
4554 struct perf_event_context *task_ctx,
4555 int new)
4556{
4557 struct perf_task_event task_event;
4558
4559 if (!atomic_read(&nr_comm_events) &&
4560 !atomic_read(&nr_mmap_events) &&
4561 !atomic_read(&nr_task_events))
4562 return;
4563
4564 task_event = (struct perf_task_event){
4565 .task = task,
4566 .task_ctx = task_ctx,
4567 .event_id = {
4568 .header = {
4569 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
4570 .misc = 0,
4571 .size = sizeof(task_event.event_id),
4572 },
4573 /* .pid */
4574 /* .ppid */
4575 /* .tid */
4576 /* .ptid */
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01004577 .time = perf_clock(),
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004578 },
4579 };
4580
4581 perf_event_task_event(&task_event);
4582}
4583
4584void perf_event_fork(struct task_struct *task)
4585{
4586 perf_event_task(task, NULL, 1);
4587}
4588
4589/*
4590 * comm tracking
4591 */
4592
4593struct perf_comm_event {
4594 struct task_struct *task;
4595 char *comm;
4596 int comm_size;
4597
4598 struct {
4599 struct perf_event_header header;
4600
4601 u32 pid;
4602 u32 tid;
4603 } event_id;
4604};
4605
4606static void perf_event_comm_output(struct perf_event *event,
4607 struct perf_comm_event *comm_event)
4608{
4609 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004610 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004611 int size = comm_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004612 int ret;
4613
4614 perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
4615 ret = perf_output_begin(&handle, event,
4616 comm_event->event_id.header.size, 0, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004617
4618 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004619 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004620
4621 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
4622 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
4623
4624 perf_output_put(&handle, comm_event->event_id);
4625 perf_output_copy(&handle, comm_event->comm,
4626 comm_event->comm_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004627
4628 perf_event__output_id_sample(event, &handle, &sample);
4629
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004630 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004631out:
4632 comm_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004633}
4634
4635static int perf_event_comm_match(struct perf_event *event)
4636{
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01004637 if (event->state < PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra22e19082010-01-18 09:12:32 +01004638 return 0;
4639
Stephane Eranian5632ab12011-01-03 18:20:01 +02004640 if (!event_filter_match(event))
Peter Zijlstra5d27c232009-12-17 13:16:32 +01004641 return 0;
4642
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004643 if (event->attr.comm)
4644 return 1;
4645
4646 return 0;
4647}
4648
4649static void perf_event_comm_ctx(struct perf_event_context *ctx,
4650 struct perf_comm_event *comm_event)
4651{
4652 struct perf_event *event;
4653
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004654 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4655 if (perf_event_comm_match(event))
4656 perf_event_comm_output(event, comm_event);
4657 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004658}
4659
4660static void perf_event_comm_event(struct perf_comm_event *comm_event)
4661{
4662 struct perf_cpu_context *cpuctx;
4663 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004664 char comm[TASK_COMM_LEN];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004665 unsigned int size;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004666 struct pmu *pmu;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004667 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004668
4669 memset(comm, 0, sizeof(comm));
Márton Németh96b02d72009-11-21 23:10:15 +01004670 strlcpy(comm, comm_event->task->comm, sizeof(comm));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004671 size = ALIGN(strlen(comm)+1, sizeof(u64));
4672
4673 comm_event->comm = comm;
4674 comm_event->comm_size = size;
4675
4676 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
Peter Zijlstraf6595f32009-11-20 22:19:47 +01004677 rcu_read_lock();
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004678 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra41945f62010-09-16 19:17:24 +02004679 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra51676952010-12-07 14:18:20 +01004680 if (cpuctx->active_pmu != pmu)
4681 goto next;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004682 perf_event_comm_ctx(&cpuctx->ctx, comm_event);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004683
4684 ctxn = pmu->task_ctx_nr;
4685 if (ctxn < 0)
Peter Zijlstra41945f62010-09-16 19:17:24 +02004686 goto next;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004687
4688 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4689 if (ctx)
4690 perf_event_comm_ctx(ctx, comm_event);
Peter Zijlstra41945f62010-09-16 19:17:24 +02004691next:
4692 put_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004693 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004694 rcu_read_unlock();
4695}
4696
4697void perf_event_comm(struct task_struct *task)
4698{
4699 struct perf_comm_event comm_event;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004700 struct perf_event_context *ctx;
4701 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004702
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004703 for_each_task_context_nr(ctxn) {
4704 ctx = task->perf_event_ctxp[ctxn];
4705 if (!ctx)
4706 continue;
4707
4708 perf_event_enable_on_exec(ctx);
4709 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004710
4711 if (!atomic_read(&nr_comm_events))
4712 return;
4713
4714 comm_event = (struct perf_comm_event){
4715 .task = task,
4716 /* .comm */
4717 /* .comm_size */
4718 .event_id = {
4719 .header = {
4720 .type = PERF_RECORD_COMM,
4721 .misc = 0,
4722 /* .size */
4723 },
4724 /* .pid */
4725 /* .tid */
4726 },
4727 };
4728
4729 perf_event_comm_event(&comm_event);
4730}
4731
4732/*
4733 * mmap tracking
4734 */
4735
4736struct perf_mmap_event {
4737 struct vm_area_struct *vma;
4738
4739 const char *file_name;
4740 int file_size;
4741
4742 struct {
4743 struct perf_event_header header;
4744
4745 u32 pid;
4746 u32 tid;
4747 u64 start;
4748 u64 len;
4749 u64 pgoff;
4750 } event_id;
4751};
4752
4753static void perf_event_mmap_output(struct perf_event *event,
4754 struct perf_mmap_event *mmap_event)
4755{
4756 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004757 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004758 int size = mmap_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004759 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004760
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004761 perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
4762 ret = perf_output_begin(&handle, event,
4763 mmap_event->event_id.header.size, 0, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004764 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004765 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004766
4767 mmap_event->event_id.pid = perf_event_pid(event, current);
4768 mmap_event->event_id.tid = perf_event_tid(event, current);
4769
4770 perf_output_put(&handle, mmap_event->event_id);
4771 perf_output_copy(&handle, mmap_event->file_name,
4772 mmap_event->file_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004773
4774 perf_event__output_id_sample(event, &handle, &sample);
4775
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004776 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004777out:
4778 mmap_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004779}
4780
4781static int perf_event_mmap_match(struct perf_event *event,
Eric B Munson3af9e852010-05-18 15:30:49 +01004782 struct perf_mmap_event *mmap_event,
4783 int executable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004784{
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01004785 if (event->state < PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra22e19082010-01-18 09:12:32 +01004786 return 0;
4787
Stephane Eranian5632ab12011-01-03 18:20:01 +02004788 if (!event_filter_match(event))
Peter Zijlstra5d27c232009-12-17 13:16:32 +01004789 return 0;
4790
Eric B Munson3af9e852010-05-18 15:30:49 +01004791 if ((!executable && event->attr.mmap_data) ||
4792 (executable && event->attr.mmap))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004793 return 1;
4794
4795 return 0;
4796}
4797
4798static void perf_event_mmap_ctx(struct perf_event_context *ctx,
Eric B Munson3af9e852010-05-18 15:30:49 +01004799 struct perf_mmap_event *mmap_event,
4800 int executable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004801{
4802 struct perf_event *event;
4803
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004804 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Eric B Munson3af9e852010-05-18 15:30:49 +01004805 if (perf_event_mmap_match(event, mmap_event, executable))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004806 perf_event_mmap_output(event, mmap_event);
4807 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004808}
4809
4810static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
4811{
4812 struct perf_cpu_context *cpuctx;
4813 struct perf_event_context *ctx;
4814 struct vm_area_struct *vma = mmap_event->vma;
4815 struct file *file = vma->vm_file;
4816 unsigned int size;
4817 char tmp[16];
4818 char *buf = NULL;
4819 const char *name;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004820 struct pmu *pmu;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004821 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004822
4823 memset(tmp, 0, sizeof(tmp));
4824
4825 if (file) {
4826 /*
4827 * d_path works from the end of the buffer backwards, so we
4828 * need to add enough zero bytes after the string to handle
4829 * the 64bit alignment we do later.
4830 */
4831 buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
4832 if (!buf) {
4833 name = strncpy(tmp, "//enomem", sizeof(tmp));
4834 goto got_name;
4835 }
4836 name = d_path(&file->f_path, buf, PATH_MAX);
4837 if (IS_ERR(name)) {
4838 name = strncpy(tmp, "//toolong", sizeof(tmp));
4839 goto got_name;
4840 }
4841 } else {
4842 if (arch_vma_name(mmap_event->vma)) {
4843 name = strncpy(tmp, arch_vma_name(mmap_event->vma),
4844 sizeof(tmp));
4845 goto got_name;
4846 }
4847
4848 if (!vma->vm_mm) {
4849 name = strncpy(tmp, "[vdso]", sizeof(tmp));
4850 goto got_name;
Eric B Munson3af9e852010-05-18 15:30:49 +01004851 } else if (vma->vm_start <= vma->vm_mm->start_brk &&
4852 vma->vm_end >= vma->vm_mm->brk) {
4853 name = strncpy(tmp, "[heap]", sizeof(tmp));
4854 goto got_name;
4855 } else if (vma->vm_start <= vma->vm_mm->start_stack &&
4856 vma->vm_end >= vma->vm_mm->start_stack) {
4857 name = strncpy(tmp, "[stack]", sizeof(tmp));
4858 goto got_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004859 }
4860
4861 name = strncpy(tmp, "//anon", sizeof(tmp));
4862 goto got_name;
4863 }
4864
4865got_name:
4866 size = ALIGN(strlen(name)+1, sizeof(u64));
4867
4868 mmap_event->file_name = name;
4869 mmap_event->file_size = size;
4870
4871 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
4872
Peter Zijlstraf6d9dd22009-11-20 22:19:48 +01004873 rcu_read_lock();
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004874 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra41945f62010-09-16 19:17:24 +02004875 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra51676952010-12-07 14:18:20 +01004876 if (cpuctx->active_pmu != pmu)
4877 goto next;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004878 perf_event_mmap_ctx(&cpuctx->ctx, mmap_event,
4879 vma->vm_flags & VM_EXEC);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004880
4881 ctxn = pmu->task_ctx_nr;
4882 if (ctxn < 0)
Peter Zijlstra41945f62010-09-16 19:17:24 +02004883 goto next;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004884
4885 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4886 if (ctx) {
4887 perf_event_mmap_ctx(ctx, mmap_event,
4888 vma->vm_flags & VM_EXEC);
4889 }
Peter Zijlstra41945f62010-09-16 19:17:24 +02004890next:
4891 put_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004892 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004893 rcu_read_unlock();
4894
4895 kfree(buf);
4896}
4897
Eric B Munson3af9e852010-05-18 15:30:49 +01004898void perf_event_mmap(struct vm_area_struct *vma)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004899{
4900 struct perf_mmap_event mmap_event;
4901
4902 if (!atomic_read(&nr_mmap_events))
4903 return;
4904
4905 mmap_event = (struct perf_mmap_event){
4906 .vma = vma,
4907 /* .file_name */
4908 /* .file_size */
4909 .event_id = {
4910 .header = {
4911 .type = PERF_RECORD_MMAP,
Zhang, Yanmin39447b32010-04-19 13:32:41 +08004912 .misc = PERF_RECORD_MISC_USER,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004913 /* .size */
4914 },
4915 /* .pid */
4916 /* .tid */
4917 .start = vma->vm_start,
4918 .len = vma->vm_end - vma->vm_start,
Peter Zijlstra3a0304e2010-02-26 10:33:41 +01004919 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004920 },
4921 };
4922
4923 perf_event_mmap_event(&mmap_event);
4924}
4925
4926/*
4927 * IRQ throttle logging
4928 */
4929
4930static void perf_log_throttle(struct perf_event *event, int enable)
4931{
4932 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004933 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004934 int ret;
4935
4936 struct {
4937 struct perf_event_header header;
4938 u64 time;
4939 u64 id;
4940 u64 stream_id;
4941 } throttle_event = {
4942 .header = {
4943 .type = PERF_RECORD_THROTTLE,
4944 .misc = 0,
4945 .size = sizeof(throttle_event),
4946 },
4947 .time = perf_clock(),
4948 .id = primary_event_id(event),
4949 .stream_id = event->id,
4950 };
4951
4952 if (enable)
4953 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
4954
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004955 perf_event_header__init_id(&throttle_event.header, &sample, event);
4956
4957 ret = perf_output_begin(&handle, event,
4958 throttle_event.header.size, 1, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004959 if (ret)
4960 return;
4961
4962 perf_output_put(&handle, throttle_event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004963 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004964 perf_output_end(&handle);
4965}
4966
4967/*
4968 * Generic event overflow handling, sampling.
4969 */
4970
4971static int __perf_event_overflow(struct perf_event *event, int nmi,
4972 int throttle, struct perf_sample_data *data,
4973 struct pt_regs *regs)
4974{
4975 int events = atomic_read(&event->event_limit);
4976 struct hw_perf_event *hwc = &event->hw;
4977 int ret = 0;
4978
Peter Zijlstra96398822010-11-24 18:55:29 +01004979 /*
4980 * Non-sampling counters might still use the PMI to fold short
4981 * hardware counters, ignore those.
4982 */
4983 if (unlikely(!is_sampling_event(event)))
4984 return 0;
4985
Peter Zijlstra163ec432011-02-16 11:22:34 +01004986 if (unlikely(hwc->interrupts >= max_samples_per_tick)) {
4987 if (throttle) {
4988 hwc->interrupts = MAX_INTERRUPTS;
4989 perf_log_throttle(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004990 ret = 1;
4991 }
Peter Zijlstra163ec432011-02-16 11:22:34 +01004992 } else
4993 hwc->interrupts++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004994
4995 if (event->attr.freq) {
4996 u64 now = perf_clock();
Peter Zijlstraabd50712010-01-26 18:50:16 +01004997 s64 delta = now - hwc->freq_time_stamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004998
Peter Zijlstraabd50712010-01-26 18:50:16 +01004999 hwc->freq_time_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005000
Peter Zijlstraabd50712010-01-26 18:50:16 +01005001 if (delta > 0 && delta < 2*TICK_NSEC)
5002 perf_adjust_period(event, delta, hwc->last_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005003 }
5004
5005 /*
5006 * XXX event_limit might not quite work as expected on inherited
5007 * events
5008 */
5009
5010 event->pending_kill = POLL_IN;
5011 if (events && atomic_dec_and_test(&event->event_limit)) {
5012 ret = 1;
5013 event->pending_kill = POLL_HUP;
5014 if (nmi) {
5015 event->pending_disable = 1;
Peter Zijlstrae360adb2010-10-14 14:01:34 +08005016 irq_work_queue(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005017 } else
5018 perf_event_disable(event);
5019 }
5020
Peter Zijlstra453f19e2009-11-20 22:19:43 +01005021 if (event->overflow_handler)
5022 event->overflow_handler(event, nmi, data, regs);
5023 else
5024 perf_event_output(event, nmi, data, regs);
5025
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005026 return ret;
5027}
5028
5029int perf_event_overflow(struct perf_event *event, int nmi,
5030 struct perf_sample_data *data,
5031 struct pt_regs *regs)
5032{
5033 return __perf_event_overflow(event, nmi, 1, data, regs);
5034}
5035
5036/*
5037 * Generic software event infrastructure
5038 */
5039
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005040struct swevent_htable {
5041 struct swevent_hlist *swevent_hlist;
5042 struct mutex hlist_mutex;
5043 int hlist_refcount;
5044
5045 /* Recursion avoidance in each contexts */
5046 int recursion[PERF_NR_CONTEXTS];
5047};
5048
5049static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
5050
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005051/*
5052 * We directly increment event->count and keep a second value in
5053 * event->hw.period_left to count intervals. This period event
5054 * is kept in the range [-sample_period, 0] so that we can use the
5055 * sign as trigger.
5056 */
5057
5058static u64 perf_swevent_set_period(struct perf_event *event)
5059{
5060 struct hw_perf_event *hwc = &event->hw;
5061 u64 period = hwc->last_period;
5062 u64 nr, offset;
5063 s64 old, val;
5064
5065 hwc->last_period = hwc->sample_period;
5066
5067again:
Peter Zijlstrae7850592010-05-21 14:43:08 +02005068 old = val = local64_read(&hwc->period_left);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005069 if (val < 0)
5070 return 0;
5071
5072 nr = div64_u64(period + val, period);
5073 offset = nr * period;
5074 val -= offset;
Peter Zijlstrae7850592010-05-21 14:43:08 +02005075 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005076 goto again;
5077
5078 return nr;
5079}
5080
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005081static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005082 int nmi, struct perf_sample_data *data,
5083 struct pt_regs *regs)
5084{
5085 struct hw_perf_event *hwc = &event->hw;
5086 int throttle = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005087
5088 data->period = event->hw.last_period;
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005089 if (!overflow)
5090 overflow = perf_swevent_set_period(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005091
5092 if (hwc->interrupts == MAX_INTERRUPTS)
5093 return;
5094
5095 for (; overflow; overflow--) {
5096 if (__perf_event_overflow(event, nmi, throttle,
5097 data, regs)) {
5098 /*
5099 * We inhibit the overflow from happening when
5100 * hwc->interrupts == MAX_INTERRUPTS.
5101 */
5102 break;
5103 }
5104 throttle = 1;
5105 }
5106}
5107
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005108static void perf_swevent_event(struct perf_event *event, u64 nr,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005109 int nmi, struct perf_sample_data *data,
5110 struct pt_regs *regs)
5111{
5112 struct hw_perf_event *hwc = &event->hw;
5113
Peter Zijlstrae7850592010-05-21 14:43:08 +02005114 local64_add(nr, &event->count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005115
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005116 if (!regs)
5117 return;
5118
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01005119 if (!is_sampling_event(event))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005120 return;
5121
5122 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
5123 return perf_swevent_overflow(event, 1, nmi, data, regs);
5124
Peter Zijlstrae7850592010-05-21 14:43:08 +02005125 if (local64_add_negative(nr, &hwc->period_left))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005126 return;
5127
5128 perf_swevent_overflow(event, 0, nmi, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005129}
5130
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005131static int perf_exclude_event(struct perf_event *event,
5132 struct pt_regs *regs)
5133{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005134 if (event->hw.state & PERF_HES_STOPPED)
Frederic Weisbecker91b2f482011-03-07 21:27:08 +01005135 return 1;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005136
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005137 if (regs) {
5138 if (event->attr.exclude_user && user_mode(regs))
5139 return 1;
5140
5141 if (event->attr.exclude_kernel && !user_mode(regs))
5142 return 1;
5143 }
5144
5145 return 0;
5146}
5147
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005148static int perf_swevent_match(struct perf_event *event,
5149 enum perf_type_id type,
Li Zefan6fb29152009-10-15 11:21:42 +08005150 u32 event_id,
5151 struct perf_sample_data *data,
5152 struct pt_regs *regs)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005153{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005154 if (event->attr.type != type)
5155 return 0;
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005156
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005157 if (event->attr.config != event_id)
5158 return 0;
5159
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005160 if (perf_exclude_event(event, regs))
5161 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005162
5163 return 1;
5164}
5165
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005166static inline u64 swevent_hash(u64 type, u32 event_id)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005167{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005168 u64 val = event_id | (type << 32);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005169
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005170 return hash_64(val, SWEVENT_HLIST_BITS);
5171}
5172
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005173static inline struct hlist_head *
5174__find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005175{
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005176 u64 hash = swevent_hash(type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005177
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005178 return &hlist->heads[hash];
5179}
5180
5181/* For the read side: events when they trigger */
5182static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005183find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005184{
5185 struct swevent_hlist *hlist;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005186
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005187 hlist = rcu_dereference(swhash->swevent_hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005188 if (!hlist)
5189 return NULL;
5190
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005191 return __find_swevent_head(hlist, type, event_id);
5192}
5193
5194/* For the event head insertion and removal in the hlist */
5195static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005196find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005197{
5198 struct swevent_hlist *hlist;
5199 u32 event_id = event->attr.config;
5200 u64 type = event->attr.type;
5201
5202 /*
5203 * Event scheduling is always serialized against hlist allocation
5204 * and release. Which makes the protected version suitable here.
5205 * The context lock guarantees that.
5206 */
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005207 hlist = rcu_dereference_protected(swhash->swevent_hlist,
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005208 lockdep_is_held(&event->ctx->lock));
5209 if (!hlist)
5210 return NULL;
5211
5212 return __find_swevent_head(hlist, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005213}
5214
5215static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
5216 u64 nr, int nmi,
5217 struct perf_sample_data *data,
5218 struct pt_regs *regs)
5219{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005220 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005221 struct perf_event *event;
5222 struct hlist_node *node;
5223 struct hlist_head *head;
5224
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005225 rcu_read_lock();
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005226 head = find_swevent_head_rcu(swhash, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005227 if (!head)
5228 goto end;
5229
5230 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
Li Zefan6fb29152009-10-15 11:21:42 +08005231 if (perf_swevent_match(event, type, event_id, data, regs))
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005232 perf_swevent_event(event, nr, nmi, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005233 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005234end:
5235 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005236}
5237
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01005238int perf_swevent_get_recursion_context(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005239{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005240 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01005241
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005242 return get_recursion_context(swhash->recursion);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005243}
Ingo Molnar645e8cc2009-11-22 12:20:19 +01005244EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005245
Jesper Juhlfa9f90b2010-11-28 21:39:34 +01005246inline void perf_swevent_put_recursion_context(int rctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005247{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005248 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02005249
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005250 put_recursion_context(swhash->recursion, rctx);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01005251}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005252
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005253void __perf_sw_event(u32 event_id, u64 nr, int nmi,
5254 struct pt_regs *regs, u64 addr)
5255{
Ingo Molnara4234bf2009-11-23 10:57:59 +01005256 struct perf_sample_data data;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01005257 int rctx;
5258
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005259 preempt_disable_notrace();
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01005260 rctx = perf_swevent_get_recursion_context();
5261 if (rctx < 0)
5262 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005263
Peter Zijlstradc1d6282010-03-03 15:55:04 +01005264 perf_sample_data_init(&data, addr);
Ingo Molnara4234bf2009-11-23 10:57:59 +01005265
5266 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, nmi, &data, regs);
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01005267
5268 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005269 preempt_enable_notrace();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005270}
5271
5272static void perf_swevent_read(struct perf_event *event)
5273{
5274}
5275
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005276static int perf_swevent_add(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005277{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005278 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005279 struct hw_perf_event *hwc = &event->hw;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005280 struct hlist_head *head;
5281
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01005282 if (is_sampling_event(event)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005283 hwc->last_period = hwc->sample_period;
5284 perf_swevent_set_period(event);
5285 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005286
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005287 hwc->state = !(flags & PERF_EF_START);
5288
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005289 head = find_swevent_head(swhash, event);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005290 if (WARN_ON_ONCE(!head))
5291 return -EINVAL;
5292
5293 hlist_add_head_rcu(&event->hlist_entry, head);
5294
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005295 return 0;
5296}
5297
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005298static void perf_swevent_del(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005299{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005300 hlist_del_rcu(&event->hlist_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005301}
5302
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005303static void perf_swevent_start(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02005304{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005305 event->hw.state = 0;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02005306}
5307
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005308static void perf_swevent_stop(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02005309{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005310 event->hw.state = PERF_HES_STOPPED;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02005311}
5312
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005313/* Deref the hlist from the update side */
5314static inline struct swevent_hlist *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005315swevent_hlist_deref(struct swevent_htable *swhash)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005316{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005317 return rcu_dereference_protected(swhash->swevent_hlist,
5318 lockdep_is_held(&swhash->hlist_mutex));
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005319}
5320
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005321static void swevent_hlist_release_rcu(struct rcu_head *rcu_head)
5322{
5323 struct swevent_hlist *hlist;
5324
5325 hlist = container_of(rcu_head, struct swevent_hlist, rcu_head);
5326 kfree(hlist);
5327}
5328
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005329static void swevent_hlist_release(struct swevent_htable *swhash)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005330{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005331 struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005332
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005333 if (!hlist)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005334 return;
5335
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005336 rcu_assign_pointer(swhash->swevent_hlist, NULL);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005337 call_rcu(&hlist->rcu_head, swevent_hlist_release_rcu);
5338}
5339
5340static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
5341{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005342 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005343
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005344 mutex_lock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005345
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005346 if (!--swhash->hlist_refcount)
5347 swevent_hlist_release(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005348
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005349 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005350}
5351
5352static void swevent_hlist_put(struct perf_event *event)
5353{
5354 int cpu;
5355
5356 if (event->cpu != -1) {
5357 swevent_hlist_put_cpu(event, event->cpu);
5358 return;
5359 }
5360
5361 for_each_possible_cpu(cpu)
5362 swevent_hlist_put_cpu(event, cpu);
5363}
5364
5365static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
5366{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005367 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005368 int err = 0;
5369
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005370 mutex_lock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005371
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005372 if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005373 struct swevent_hlist *hlist;
5374
5375 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
5376 if (!hlist) {
5377 err = -ENOMEM;
5378 goto exit;
5379 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005380 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005381 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005382 swhash->hlist_refcount++;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02005383exit:
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005384 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005385
5386 return err;
5387}
5388
5389static int swevent_hlist_get(struct perf_event *event)
5390{
5391 int err;
5392 int cpu, failed_cpu;
5393
5394 if (event->cpu != -1)
5395 return swevent_hlist_get_cpu(event, event->cpu);
5396
5397 get_online_cpus();
5398 for_each_possible_cpu(cpu) {
5399 err = swevent_hlist_get_cpu(event, cpu);
5400 if (err) {
5401 failed_cpu = cpu;
5402 goto fail;
5403 }
5404 }
5405 put_online_cpus();
5406
5407 return 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02005408fail:
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005409 for_each_possible_cpu(cpu) {
5410 if (cpu == failed_cpu)
5411 break;
5412 swevent_hlist_put_cpu(event, cpu);
5413 }
5414
5415 put_online_cpus();
5416 return err;
5417}
5418
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005419atomic_t perf_swevent_enabled[PERF_COUNT_SW_MAX];
Frederic Weisbecker95476b62010-04-14 23:42:18 +02005420
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005421static void sw_perf_event_destroy(struct perf_event *event)
5422{
5423 u64 event_id = event->attr.config;
5424
5425 WARN_ON(event->parent);
5426
Peter Zijlstra7e54a5a2010-10-14 22:32:45 +02005427 jump_label_dec(&perf_swevent_enabled[event_id]);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005428 swevent_hlist_put(event);
5429}
5430
5431static int perf_swevent_init(struct perf_event *event)
5432{
5433 int event_id = event->attr.config;
5434
5435 if (event->attr.type != PERF_TYPE_SOFTWARE)
5436 return -ENOENT;
5437
5438 switch (event_id) {
5439 case PERF_COUNT_SW_CPU_CLOCK:
5440 case PERF_COUNT_SW_TASK_CLOCK:
5441 return -ENOENT;
5442
5443 default:
5444 break;
5445 }
5446
Dan Carpenterce677832010-10-24 21:50:42 +02005447 if (event_id >= PERF_COUNT_SW_MAX)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005448 return -ENOENT;
5449
5450 if (!event->parent) {
5451 int err;
5452
5453 err = swevent_hlist_get(event);
5454 if (err)
5455 return err;
5456
Peter Zijlstra7e54a5a2010-10-14 22:32:45 +02005457 jump_label_inc(&perf_swevent_enabled[event_id]);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005458 event->destroy = sw_perf_event_destroy;
5459 }
5460
5461 return 0;
5462}
5463
5464static struct pmu perf_swevent = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005465 .task_ctx_nr = perf_sw_context,
5466
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005467 .event_init = perf_swevent_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005468 .add = perf_swevent_add,
5469 .del = perf_swevent_del,
5470 .start = perf_swevent_start,
5471 .stop = perf_swevent_stop,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005472 .read = perf_swevent_read,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005473};
Frederic Weisbecker95476b62010-04-14 23:42:18 +02005474
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005475#ifdef CONFIG_EVENT_TRACING
5476
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005477static int perf_tp_filter_match(struct perf_event *event,
Frederic Weisbecker95476b62010-04-14 23:42:18 +02005478 struct perf_sample_data *data)
5479{
5480 void *record = data->raw->data;
5481
5482 if (likely(!event->filter) || filter_match_preds(event->filter, record))
5483 return 1;
5484 return 0;
5485}
5486
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005487static int perf_tp_event_match(struct perf_event *event,
5488 struct perf_sample_data *data,
5489 struct pt_regs *regs)
5490{
Frederic Weisbeckera0f7d0f2011-03-07 21:27:09 +01005491 if (event->hw.state & PERF_HES_STOPPED)
5492 return 0;
Peter Zijlstra580d6072010-05-20 20:54:31 +02005493 /*
5494 * All tracepoints are from kernel-space.
5495 */
5496 if (event->attr.exclude_kernel)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005497 return 0;
5498
5499 if (!perf_tp_filter_match(event, data))
5500 return 0;
5501
5502 return 1;
5503}
5504
5505void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
Peter Zijlstraecc55f82010-05-21 15:11:34 +02005506 struct pt_regs *regs, struct hlist_head *head, int rctx)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005507{
5508 struct perf_sample_data data;
5509 struct perf_event *event;
5510 struct hlist_node *node;
5511
5512 struct perf_raw_record raw = {
5513 .size = entry_size,
5514 .data = record,
5515 };
5516
5517 perf_sample_data_init(&data, addr);
5518 data.raw = &raw;
5519
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005520 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
5521 if (perf_tp_event_match(event, &data, regs))
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005522 perf_swevent_event(event, count, 1, &data, regs);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005523 }
Peter Zijlstraecc55f82010-05-21 15:11:34 +02005524
5525 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005526}
5527EXPORT_SYMBOL_GPL(perf_tp_event);
5528
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005529static void tp_perf_event_destroy(struct perf_event *event)
5530{
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005531 perf_trace_destroy(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005532}
5533
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005534static int perf_tp_event_init(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005535{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005536 int err;
5537
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005538 if (event->attr.type != PERF_TYPE_TRACEPOINT)
5539 return -ENOENT;
5540
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005541 err = perf_trace_init(event);
5542 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005543 return err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005544
5545 event->destroy = tp_perf_event_destroy;
5546
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005547 return 0;
5548}
5549
5550static struct pmu perf_tracepoint = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005551 .task_ctx_nr = perf_sw_context,
5552
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005553 .event_init = perf_tp_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005554 .add = perf_trace_add,
5555 .del = perf_trace_del,
5556 .start = perf_swevent_start,
5557 .stop = perf_swevent_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005558 .read = perf_swevent_read,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005559};
5560
5561static inline void perf_tp_register(void)
5562{
Peter Zijlstra2e80a822010-11-17 23:17:36 +01005563 perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005564}
Li Zefan6fb29152009-10-15 11:21:42 +08005565
5566static int perf_event_set_filter(struct perf_event *event, void __user *arg)
5567{
5568 char *filter_str;
5569 int ret;
5570
5571 if (event->attr.type != PERF_TYPE_TRACEPOINT)
5572 return -EINVAL;
5573
5574 filter_str = strndup_user(arg, PAGE_SIZE);
5575 if (IS_ERR(filter_str))
5576 return PTR_ERR(filter_str);
5577
5578 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
5579
5580 kfree(filter_str);
5581 return ret;
5582}
5583
5584static void perf_event_free_filter(struct perf_event *event)
5585{
5586 ftrace_profile_free_filter(event);
5587}
5588
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005589#else
Li Zefan6fb29152009-10-15 11:21:42 +08005590
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005591static inline void perf_tp_register(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005592{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005593}
Li Zefan6fb29152009-10-15 11:21:42 +08005594
5595static int perf_event_set_filter(struct perf_event *event, void __user *arg)
5596{
5597 return -ENOENT;
5598}
5599
5600static void perf_event_free_filter(struct perf_event *event)
5601{
5602}
5603
Li Zefan07b139c2009-12-21 14:27:35 +08005604#endif /* CONFIG_EVENT_TRACING */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005605
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02005606#ifdef CONFIG_HAVE_HW_BREAKPOINT
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005607void perf_bp_event(struct perf_event *bp, void *data)
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02005608{
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005609 struct perf_sample_data sample;
5610 struct pt_regs *regs = data;
5611
Peter Zijlstradc1d6282010-03-03 15:55:04 +01005612 perf_sample_data_init(&sample, bp->attr.bp_addr);
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005613
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005614 if (!bp->hw.state && !perf_exclude_event(bp, regs))
5615 perf_swevent_event(bp, 1, 1, &sample, regs);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02005616}
5617#endif
5618
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005619/*
5620 * hrtimer based swevent callback
5621 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005622
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005623static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005624{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005625 enum hrtimer_restart ret = HRTIMER_RESTART;
5626 struct perf_sample_data data;
5627 struct pt_regs *regs;
5628 struct perf_event *event;
5629 u64 period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005630
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005631 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
Peter Zijlstraba3dd362011-02-15 12:41:46 +01005632
5633 if (event->state != PERF_EVENT_STATE_ACTIVE)
5634 return HRTIMER_NORESTART;
5635
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005636 event->pmu->read(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005637
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005638 perf_sample_data_init(&data, 0);
5639 data.period = event->hw.last_period;
5640 regs = get_irq_regs();
5641
5642 if (regs && !perf_exclude_event(event, regs)) {
5643 if (!(event->attr.exclude_idle && current->pid == 0))
5644 if (perf_event_overflow(event, 0, &data, regs))
5645 ret = HRTIMER_NORESTART;
5646 }
5647
5648 period = max_t(u64, 10000, event->hw.sample_period);
5649 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
5650
5651 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005652}
5653
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005654static void perf_swevent_start_hrtimer(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005655{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005656 struct hw_perf_event *hwc = &event->hw;
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01005657 s64 period;
5658
5659 if (!is_sampling_event(event))
5660 return;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005661
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01005662 period = local64_read(&hwc->period_left);
5663 if (period) {
5664 if (period < 0)
5665 period = 10000;
Peter Zijlstrafa407f32010-06-24 12:35:12 +02005666
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01005667 local64_set(&hwc->period_left, 0);
5668 } else {
5669 period = max_t(u64, 10000, hwc->sample_period);
5670 }
5671 __hrtimer_start_range_ns(&hwc->hrtimer,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005672 ns_to_ktime(period), 0,
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02005673 HRTIMER_MODE_REL_PINNED, 0);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005674}
5675
5676static void perf_swevent_cancel_hrtimer(struct perf_event *event)
5677{
5678 struct hw_perf_event *hwc = &event->hw;
5679
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01005680 if (is_sampling_event(event)) {
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005681 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
Peter Zijlstrafa407f32010-06-24 12:35:12 +02005682 local64_set(&hwc->period_left, ktime_to_ns(remaining));
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005683
5684 hrtimer_cancel(&hwc->hrtimer);
5685 }
5686}
5687
Peter Zijlstraba3dd362011-02-15 12:41:46 +01005688static void perf_swevent_init_hrtimer(struct perf_event *event)
5689{
5690 struct hw_perf_event *hwc = &event->hw;
5691
5692 if (!is_sampling_event(event))
5693 return;
5694
5695 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
5696 hwc->hrtimer.function = perf_swevent_hrtimer;
5697
5698 /*
5699 * Since hrtimers have a fixed rate, we can do a static freq->period
5700 * mapping and avoid the whole period adjust feedback stuff.
5701 */
5702 if (event->attr.freq) {
5703 long freq = event->attr.sample_freq;
5704
5705 event->attr.sample_period = NSEC_PER_SEC / freq;
5706 hwc->sample_period = event->attr.sample_period;
5707 local64_set(&hwc->period_left, hwc->sample_period);
5708 event->attr.freq = 0;
5709 }
5710}
5711
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005712/*
5713 * Software event: cpu wall time clock
5714 */
5715
5716static void cpu_clock_event_update(struct perf_event *event)
5717{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005718 s64 prev;
5719 u64 now;
5720
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005721 now = local_clock();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005722 prev = local64_xchg(&event->hw.prev_count, now);
5723 local64_add(now - prev, &event->count);
5724}
5725
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005726static void cpu_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005727{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005728 local64_set(&event->hw.prev_count, local_clock());
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005729 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005730}
5731
5732static void cpu_clock_event_stop(struct perf_event *event, int flags)
5733{
5734 perf_swevent_cancel_hrtimer(event);
5735 cpu_clock_event_update(event);
5736}
5737
5738static int cpu_clock_event_add(struct perf_event *event, int flags)
5739{
5740 if (flags & PERF_EF_START)
5741 cpu_clock_event_start(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005742
5743 return 0;
5744}
5745
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005746static void cpu_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005747{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005748 cpu_clock_event_stop(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005749}
5750
5751static void cpu_clock_event_read(struct perf_event *event)
5752{
5753 cpu_clock_event_update(event);
5754}
5755
5756static int cpu_clock_event_init(struct perf_event *event)
5757{
5758 if (event->attr.type != PERF_TYPE_SOFTWARE)
5759 return -ENOENT;
5760
5761 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
5762 return -ENOENT;
5763
Peter Zijlstraba3dd362011-02-15 12:41:46 +01005764 perf_swevent_init_hrtimer(event);
5765
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005766 return 0;
5767}
5768
5769static struct pmu perf_cpu_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005770 .task_ctx_nr = perf_sw_context,
5771
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005772 .event_init = cpu_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005773 .add = cpu_clock_event_add,
5774 .del = cpu_clock_event_del,
5775 .start = cpu_clock_event_start,
5776 .stop = cpu_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005777 .read = cpu_clock_event_read,
5778};
5779
5780/*
5781 * Software event: task time clock
5782 */
5783
5784static void task_clock_event_update(struct perf_event *event, u64 now)
5785{
5786 u64 prev;
5787 s64 delta;
5788
5789 prev = local64_xchg(&event->hw.prev_count, now);
5790 delta = now - prev;
5791 local64_add(delta, &event->count);
5792}
5793
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005794static void task_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005795{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005796 local64_set(&event->hw.prev_count, event->ctx->time);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005797 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005798}
5799
5800static void task_clock_event_stop(struct perf_event *event, int flags)
5801{
5802 perf_swevent_cancel_hrtimer(event);
5803 task_clock_event_update(event, event->ctx->time);
5804}
5805
5806static int task_clock_event_add(struct perf_event *event, int flags)
5807{
5808 if (flags & PERF_EF_START)
5809 task_clock_event_start(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005810
5811 return 0;
5812}
5813
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005814static void task_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005815{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005816 task_clock_event_stop(event, PERF_EF_UPDATE);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005817}
5818
5819static void task_clock_event_read(struct perf_event *event)
5820{
Peter Zijlstra768a06e2011-02-22 16:52:24 +01005821 u64 now = perf_clock();
5822 u64 delta = now - event->ctx->timestamp;
5823 u64 time = event->ctx->time + delta;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005824
5825 task_clock_event_update(event, time);
5826}
5827
5828static int task_clock_event_init(struct perf_event *event)
5829{
5830 if (event->attr.type != PERF_TYPE_SOFTWARE)
5831 return -ENOENT;
5832
5833 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
5834 return -ENOENT;
5835
Peter Zijlstraba3dd362011-02-15 12:41:46 +01005836 perf_swevent_init_hrtimer(event);
5837
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005838 return 0;
5839}
5840
5841static struct pmu perf_task_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005842 .task_ctx_nr = perf_sw_context,
5843
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005844 .event_init = task_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005845 .add = task_clock_event_add,
5846 .del = task_clock_event_del,
5847 .start = task_clock_event_start,
5848 .stop = task_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005849 .read = task_clock_event_read,
5850};
5851
Peter Zijlstraad5133b2010-06-15 12:22:39 +02005852static void perf_pmu_nop_void(struct pmu *pmu)
5853{
5854}
5855
5856static int perf_pmu_nop_int(struct pmu *pmu)
5857{
5858 return 0;
5859}
5860
5861static void perf_pmu_start_txn(struct pmu *pmu)
5862{
5863 perf_pmu_disable(pmu);
5864}
5865
5866static int perf_pmu_commit_txn(struct pmu *pmu)
5867{
5868 perf_pmu_enable(pmu);
5869 return 0;
5870}
5871
5872static void perf_pmu_cancel_txn(struct pmu *pmu)
5873{
5874 perf_pmu_enable(pmu);
5875}
5876
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005877/*
5878 * Ensures all contexts with the same task_ctx_nr have the same
5879 * pmu_cpu_context too.
5880 */
5881static void *find_pmu_context(int ctxn)
5882{
5883 struct pmu *pmu;
5884
5885 if (ctxn < 0)
5886 return NULL;
5887
5888 list_for_each_entry(pmu, &pmus, entry) {
5889 if (pmu->task_ctx_nr == ctxn)
5890 return pmu->pmu_cpu_context;
5891 }
5892
5893 return NULL;
5894}
5895
Peter Zijlstra51676952010-12-07 14:18:20 +01005896static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005897{
Peter Zijlstra51676952010-12-07 14:18:20 +01005898 int cpu;
5899
5900 for_each_possible_cpu(cpu) {
5901 struct perf_cpu_context *cpuctx;
5902
5903 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
5904
5905 if (cpuctx->active_pmu == old_pmu)
5906 cpuctx->active_pmu = pmu;
5907 }
5908}
5909
5910static void free_pmu_context(struct pmu *pmu)
5911{
5912 struct pmu *i;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005913
5914 mutex_lock(&pmus_lock);
5915 /*
5916 * Like a real lame refcount.
5917 */
Peter Zijlstra51676952010-12-07 14:18:20 +01005918 list_for_each_entry(i, &pmus, entry) {
5919 if (i->pmu_cpu_context == pmu->pmu_cpu_context) {
5920 update_pmu_context(i, pmu);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005921 goto out;
Peter Zijlstra51676952010-12-07 14:18:20 +01005922 }
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005923 }
5924
Peter Zijlstra51676952010-12-07 14:18:20 +01005925 free_percpu(pmu->pmu_cpu_context);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005926out:
5927 mutex_unlock(&pmus_lock);
5928}
Peter Zijlstra2e80a822010-11-17 23:17:36 +01005929static struct idr pmu_idr;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005930
Peter Zijlstraabe43402010-11-17 23:17:37 +01005931static ssize_t
5932type_show(struct device *dev, struct device_attribute *attr, char *page)
5933{
5934 struct pmu *pmu = dev_get_drvdata(dev);
5935
5936 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type);
5937}
5938
5939static struct device_attribute pmu_dev_attrs[] = {
5940 __ATTR_RO(type),
5941 __ATTR_NULL,
5942};
5943
5944static int pmu_bus_running;
5945static struct bus_type pmu_bus = {
5946 .name = "event_source",
5947 .dev_attrs = pmu_dev_attrs,
5948};
5949
5950static void pmu_dev_release(struct device *dev)
5951{
5952 kfree(dev);
5953}
5954
5955static int pmu_dev_alloc(struct pmu *pmu)
5956{
5957 int ret = -ENOMEM;
5958
5959 pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
5960 if (!pmu->dev)
5961 goto out;
5962
5963 device_initialize(pmu->dev);
5964 ret = dev_set_name(pmu->dev, "%s", pmu->name);
5965 if (ret)
5966 goto free_dev;
5967
5968 dev_set_drvdata(pmu->dev, pmu);
5969 pmu->dev->bus = &pmu_bus;
5970 pmu->dev->release = pmu_dev_release;
5971 ret = device_add(pmu->dev);
5972 if (ret)
5973 goto free_dev;
5974
5975out:
5976 return ret;
5977
5978free_dev:
5979 put_device(pmu->dev);
5980 goto out;
5981}
5982
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01005983static struct lock_class_key cpuctx_mutex;
5984
Peter Zijlstra2e80a822010-11-17 23:17:36 +01005985int perf_pmu_register(struct pmu *pmu, char *name, int type)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005986{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005987 int cpu, ret;
Peter Zijlstra33696fc2010-06-14 08:49:00 +02005988
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005989 mutex_lock(&pmus_lock);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02005990 ret = -ENOMEM;
5991 pmu->pmu_disable_count = alloc_percpu(int);
5992 if (!pmu->pmu_disable_count)
5993 goto unlock;
Peter Zijlstraad5133b2010-06-15 12:22:39 +02005994
Peter Zijlstra2e80a822010-11-17 23:17:36 +01005995 pmu->type = -1;
5996 if (!name)
5997 goto skip_type;
5998 pmu->name = name;
5999
6000 if (type < 0) {
6001 int err = idr_pre_get(&pmu_idr, GFP_KERNEL);
6002 if (!err)
6003 goto free_pdc;
6004
6005 err = idr_get_new_above(&pmu_idr, pmu, PERF_TYPE_MAX, &type);
6006 if (err) {
6007 ret = err;
6008 goto free_pdc;
6009 }
6010 }
6011 pmu->type = type;
6012
Peter Zijlstraabe43402010-11-17 23:17:37 +01006013 if (pmu_bus_running) {
6014 ret = pmu_dev_alloc(pmu);
6015 if (ret)
6016 goto free_idr;
6017 }
6018
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006019skip_type:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006020 pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
6021 if (pmu->pmu_cpu_context)
6022 goto got_cpu_context;
6023
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006024 pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
6025 if (!pmu->pmu_cpu_context)
Peter Zijlstraabe43402010-11-17 23:17:37 +01006026 goto free_dev;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006027
6028 for_each_possible_cpu(cpu) {
6029 struct perf_cpu_context *cpuctx;
6030
6031 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Peter Zijlstraeb184472010-09-07 15:55:13 +02006032 __perf_event_init_context(&cpuctx->ctx);
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01006033 lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006034 cpuctx->ctx.type = cpu_context;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006035 cpuctx->ctx.pmu = pmu;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02006036 cpuctx->jiffies_interval = 1;
6037 INIT_LIST_HEAD(&cpuctx->rotation_list);
Peter Zijlstra51676952010-12-07 14:18:20 +01006038 cpuctx->active_pmu = pmu;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006039 }
6040
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006041got_cpu_context:
Peter Zijlstraad5133b2010-06-15 12:22:39 +02006042 if (!pmu->start_txn) {
6043 if (pmu->pmu_enable) {
6044 /*
6045 * If we have pmu_enable/pmu_disable calls, install
6046 * transaction stubs that use that to try and batch
6047 * hardware accesses.
6048 */
6049 pmu->start_txn = perf_pmu_start_txn;
6050 pmu->commit_txn = perf_pmu_commit_txn;
6051 pmu->cancel_txn = perf_pmu_cancel_txn;
6052 } else {
6053 pmu->start_txn = perf_pmu_nop_void;
6054 pmu->commit_txn = perf_pmu_nop_int;
6055 pmu->cancel_txn = perf_pmu_nop_void;
6056 }
6057 }
6058
6059 if (!pmu->pmu_enable) {
6060 pmu->pmu_enable = perf_pmu_nop_void;
6061 pmu->pmu_disable = perf_pmu_nop_void;
6062 }
6063
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006064 list_add_rcu(&pmu->entry, &pmus);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02006065 ret = 0;
6066unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006067 mutex_unlock(&pmus_lock);
6068
Peter Zijlstra33696fc2010-06-14 08:49:00 +02006069 return ret;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006070
Peter Zijlstraabe43402010-11-17 23:17:37 +01006071free_dev:
6072 device_del(pmu->dev);
6073 put_device(pmu->dev);
6074
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006075free_idr:
6076 if (pmu->type >= PERF_TYPE_MAX)
6077 idr_remove(&pmu_idr, pmu->type);
6078
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006079free_pdc:
6080 free_percpu(pmu->pmu_disable_count);
6081 goto unlock;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006082}
6083
6084void perf_pmu_unregister(struct pmu *pmu)
6085{
6086 mutex_lock(&pmus_lock);
6087 list_del_rcu(&pmu->entry);
6088 mutex_unlock(&pmus_lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006089
6090 /*
Peter Zijlstracde8e882010-09-13 11:06:55 +02006091 * We dereference the pmu list under both SRCU and regular RCU, so
6092 * synchronize against both of those.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006093 */
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006094 synchronize_srcu(&pmus_srcu);
Peter Zijlstracde8e882010-09-13 11:06:55 +02006095 synchronize_rcu();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006096
Peter Zijlstra33696fc2010-06-14 08:49:00 +02006097 free_percpu(pmu->pmu_disable_count);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006098 if (pmu->type >= PERF_TYPE_MAX)
6099 idr_remove(&pmu_idr, pmu->type);
Peter Zijlstraabe43402010-11-17 23:17:37 +01006100 device_del(pmu->dev);
6101 put_device(pmu->dev);
Peter Zijlstra51676952010-12-07 14:18:20 +01006102 free_pmu_context(pmu);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006103}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006104
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006105struct pmu *perf_init_event(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006106{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02006107 struct pmu *pmu = NULL;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006108 int idx;
Lin Ming940c5b22011-02-27 21:13:31 +08006109 int ret;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006110
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006111 idx = srcu_read_lock(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006112
6113 rcu_read_lock();
6114 pmu = idr_find(&pmu_idr, event->attr.type);
6115 rcu_read_unlock();
Lin Ming940c5b22011-02-27 21:13:31 +08006116 if (pmu) {
6117 ret = pmu->event_init(event);
6118 if (ret)
6119 pmu = ERR_PTR(ret);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006120 goto unlock;
Lin Ming940c5b22011-02-27 21:13:31 +08006121 }
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006122
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006123 list_for_each_entry_rcu(pmu, &pmus, entry) {
Lin Ming940c5b22011-02-27 21:13:31 +08006124 ret = pmu->event_init(event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006125 if (!ret)
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02006126 goto unlock;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006127
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006128 if (ret != -ENOENT) {
6129 pmu = ERR_PTR(ret);
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02006130 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006131 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006132 }
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02006133 pmu = ERR_PTR(-ENOENT);
6134unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006135 srcu_read_unlock(&pmus_srcu, idx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006136
6137 return pmu;
6138}
6139
6140/*
6141 * Allocate and initialize a event structure
6142 */
6143static struct perf_event *
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006144perf_event_alloc(struct perf_event_attr *attr, int cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +02006145 struct task_struct *task,
6146 struct perf_event *group_leader,
6147 struct perf_event *parent_event,
6148 perf_overflow_handler_t overflow_handler)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006149{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02006150 struct pmu *pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006151 struct perf_event *event;
6152 struct hw_perf_event *hwc;
6153 long err;
6154
Oleg Nesterov66832eb2011-01-18 17:10:32 +01006155 if ((unsigned)cpu >= nr_cpu_ids) {
6156 if (!task || cpu != -1)
6157 return ERR_PTR(-EINVAL);
6158 }
6159
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006160 event = kzalloc(sizeof(*event), GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006161 if (!event)
6162 return ERR_PTR(-ENOMEM);
6163
6164 /*
6165 * Single events are their own group leaders, with an
6166 * empty sibling list:
6167 */
6168 if (!group_leader)
6169 group_leader = event;
6170
6171 mutex_init(&event->child_mutex);
6172 INIT_LIST_HEAD(&event->child_list);
6173
6174 INIT_LIST_HEAD(&event->group_entry);
6175 INIT_LIST_HEAD(&event->event_entry);
6176 INIT_LIST_HEAD(&event->sibling_list);
6177 init_waitqueue_head(&event->waitq);
Peter Zijlstrae360adb2010-10-14 14:01:34 +08006178 init_irq_work(&event->pending, perf_pending_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006179
6180 mutex_init(&event->mmap_mutex);
6181
6182 event->cpu = cpu;
6183 event->attr = *attr;
6184 event->group_leader = group_leader;
6185 event->pmu = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006186 event->oncpu = -1;
6187
6188 event->parent = parent_event;
6189
6190 event->ns = get_pid_ns(current->nsproxy->pid_ns);
6191 event->id = atomic64_inc_return(&perf_event_id);
6192
6193 event->state = PERF_EVENT_STATE_INACTIVE;
6194
Peter Zijlstrad580ff82010-10-14 17:43:23 +02006195 if (task) {
6196 event->attach_state = PERF_ATTACH_TASK;
6197#ifdef CONFIG_HAVE_HW_BREAKPOINT
6198 /*
6199 * hw_breakpoint is a bit difficult here..
6200 */
6201 if (attr->type == PERF_TYPE_BREAKPOINT)
6202 event->hw.bp_target = task;
6203#endif
6204 }
6205
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01006206 if (!overflow_handler && parent_event)
6207 overflow_handler = parent_event->overflow_handler;
Oleg Nesterov66832eb2011-01-18 17:10:32 +01006208
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01006209 event->overflow_handler = overflow_handler;
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02006210
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006211 if (attr->disabled)
6212 event->state = PERF_EVENT_STATE_OFF;
6213
6214 pmu = NULL;
6215
6216 hwc = &event->hw;
6217 hwc->sample_period = attr->sample_period;
6218 if (attr->freq && attr->sample_freq)
6219 hwc->sample_period = 1;
6220 hwc->last_period = hwc->sample_period;
6221
Peter Zijlstrae7850592010-05-21 14:43:08 +02006222 local64_set(&hwc->period_left, hwc->sample_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006223
6224 /*
6225 * we currently do not support PERF_FORMAT_GROUP on inherited events
6226 */
6227 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
6228 goto done;
6229
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006230 pmu = perf_init_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006231
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006232done:
6233 err = 0;
6234 if (!pmu)
6235 err = -EINVAL;
6236 else if (IS_ERR(pmu))
6237 err = PTR_ERR(pmu);
6238
6239 if (err) {
6240 if (event->ns)
6241 put_pid_ns(event->ns);
6242 kfree(event);
6243 return ERR_PTR(err);
6244 }
6245
6246 event->pmu = pmu;
6247
6248 if (!event->parent) {
Peter Zijlstra82cd6de2010-10-14 17:57:23 +02006249 if (event->attach_state & PERF_ATTACH_TASK)
Stephane Eraniane5d13672011-02-14 11:20:01 +02006250 jump_label_inc(&perf_sched_events);
Eric B Munson3af9e852010-05-18 15:30:49 +01006251 if (event->attr.mmap || event->attr.mmap_data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006252 atomic_inc(&nr_mmap_events);
6253 if (event->attr.comm)
6254 atomic_inc(&nr_comm_events);
6255 if (event->attr.task)
6256 atomic_inc(&nr_task_events);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02006257 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
6258 err = get_callchain_buffers();
6259 if (err) {
6260 free_event(event);
6261 return ERR_PTR(err);
6262 }
6263 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006264 }
6265
6266 return event;
6267}
6268
6269static int perf_copy_attr(struct perf_event_attr __user *uattr,
6270 struct perf_event_attr *attr)
6271{
6272 u32 size;
6273 int ret;
6274
6275 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
6276 return -EFAULT;
6277
6278 /*
6279 * zero the full structure, so that a short copy will be nice.
6280 */
6281 memset(attr, 0, sizeof(*attr));
6282
6283 ret = get_user(size, &uattr->size);
6284 if (ret)
6285 return ret;
6286
6287 if (size > PAGE_SIZE) /* silly large */
6288 goto err_size;
6289
6290 if (!size) /* abi compat */
6291 size = PERF_ATTR_SIZE_VER0;
6292
6293 if (size < PERF_ATTR_SIZE_VER0)
6294 goto err_size;
6295
6296 /*
6297 * If we're handed a bigger struct than we know of,
6298 * ensure all the unknown bits are 0 - i.e. new
6299 * user-space does not rely on any kernel feature
6300 * extensions we dont know about yet.
6301 */
6302 if (size > sizeof(*attr)) {
6303 unsigned char __user *addr;
6304 unsigned char __user *end;
6305 unsigned char val;
6306
6307 addr = (void __user *)uattr + sizeof(*attr);
6308 end = (void __user *)uattr + size;
6309
6310 for (; addr < end; addr++) {
6311 ret = get_user(val, addr);
6312 if (ret)
6313 return ret;
6314 if (val)
6315 goto err_size;
6316 }
6317 size = sizeof(*attr);
6318 }
6319
6320 ret = copy_from_user(attr, uattr, size);
6321 if (ret)
6322 return -EFAULT;
6323
6324 /*
6325 * If the type exists, the corresponding creation will verify
6326 * the attr->config.
6327 */
6328 if (attr->type >= PERF_TYPE_MAX)
6329 return -EINVAL;
6330
Mahesh Salgaonkarcd757642010-01-30 10:25:18 +05306331 if (attr->__reserved_1)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006332 return -EINVAL;
6333
6334 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
6335 return -EINVAL;
6336
6337 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
6338 return -EINVAL;
6339
6340out:
6341 return ret;
6342
6343err_size:
6344 put_user(sizeof(*attr), &uattr->size);
6345 ret = -E2BIG;
6346 goto out;
6347}
6348
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006349static int
6350perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006351{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02006352 struct perf_buffer *buffer = NULL, *old_buffer = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006353 int ret = -EINVAL;
6354
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006355 if (!output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006356 goto set;
6357
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006358 /* don't allow circular references */
6359 if (event == output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006360 goto out;
6361
Peter Zijlstra0f139302010-05-20 14:35:15 +02006362 /*
6363 * Don't allow cross-cpu buffers
6364 */
6365 if (output_event->cpu != event->cpu)
6366 goto out;
6367
6368 /*
6369 * If its not a per-cpu buffer, it must be the same task.
6370 */
6371 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
6372 goto out;
6373
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006374set:
6375 mutex_lock(&event->mmap_mutex);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006376 /* Can't redirect output if we've got an active mmap() */
6377 if (atomic_read(&event->mmap_count))
6378 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006379
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006380 if (output_event) {
6381 /* get the buffer we want to redirect to */
Peter Zijlstraca5135e2010-05-28 19:33:23 +02006382 buffer = perf_buffer_get(output_event);
6383 if (!buffer)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006384 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006385 }
6386
Peter Zijlstraca5135e2010-05-28 19:33:23 +02006387 old_buffer = event->buffer;
6388 rcu_assign_pointer(event->buffer, buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006389 ret = 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006390unlock:
6391 mutex_unlock(&event->mmap_mutex);
6392
Peter Zijlstraca5135e2010-05-28 19:33:23 +02006393 if (old_buffer)
6394 perf_buffer_put(old_buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006395out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006396 return ret;
6397}
6398
6399/**
6400 * sys_perf_event_open - open a performance event, associate it to a task/cpu
6401 *
6402 * @attr_uptr: event_id type attributes for monitoring/sampling
6403 * @pid: target pid
6404 * @cpu: target cpu
6405 * @group_fd: group leader event fd
6406 */
6407SYSCALL_DEFINE5(perf_event_open,
6408 struct perf_event_attr __user *, attr_uptr,
6409 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
6410{
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006411 struct perf_event *group_leader = NULL, *output_event = NULL;
6412 struct perf_event *event, *sibling;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006413 struct perf_event_attr attr;
6414 struct perf_event_context *ctx;
6415 struct file *event_file = NULL;
6416 struct file *group_file = NULL;
Matt Helsley38a81da2010-09-13 13:01:20 -07006417 struct task_struct *task = NULL;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006418 struct pmu *pmu;
Al Viroea635c62010-05-26 17:40:29 -04006419 int event_fd;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006420 int move_group = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006421 int fput_needed = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006422 int err;
6423
6424 /* for future expandability... */
Stephane Eraniane5d13672011-02-14 11:20:01 +02006425 if (flags & ~PERF_FLAG_ALL)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006426 return -EINVAL;
6427
6428 err = perf_copy_attr(attr_uptr, &attr);
6429 if (err)
6430 return err;
6431
6432 if (!attr.exclude_kernel) {
6433 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
6434 return -EACCES;
6435 }
6436
6437 if (attr.freq) {
6438 if (attr.sample_freq > sysctl_perf_event_sample_rate)
6439 return -EINVAL;
6440 }
6441
Stephane Eraniane5d13672011-02-14 11:20:01 +02006442 /*
6443 * In cgroup mode, the pid argument is used to pass the fd
6444 * opened to the cgroup directory in cgroupfs. The cpu argument
6445 * designates the cpu on which to monitor threads from that
6446 * cgroup.
6447 */
6448 if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1))
6449 return -EINVAL;
6450
Al Viroea635c62010-05-26 17:40:29 -04006451 event_fd = get_unused_fd_flags(O_RDWR);
6452 if (event_fd < 0)
6453 return event_fd;
6454
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006455 if (group_fd != -1) {
6456 group_leader = perf_fget_light(group_fd, &fput_needed);
6457 if (IS_ERR(group_leader)) {
6458 err = PTR_ERR(group_leader);
Stephane Eraniand14b12d2010-09-17 11:28:47 +02006459 goto err_fd;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006460 }
6461 group_file = group_leader->filp;
6462 if (flags & PERF_FLAG_FD_OUTPUT)
6463 output_event = group_leader;
6464 if (flags & PERF_FLAG_FD_NO_GROUP)
6465 group_leader = NULL;
6466 }
6467
Stephane Eraniane5d13672011-02-14 11:20:01 +02006468 if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) {
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02006469 task = find_lively_task_by_vpid(pid);
6470 if (IS_ERR(task)) {
6471 err = PTR_ERR(task);
6472 goto err_group_fd;
6473 }
6474 }
6475
Peter Zijlstrad580ff82010-10-14 17:43:23 +02006476 event = perf_event_alloc(&attr, cpu, task, group_leader, NULL, NULL);
Stephane Eraniand14b12d2010-09-17 11:28:47 +02006477 if (IS_ERR(event)) {
6478 err = PTR_ERR(event);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02006479 goto err_task;
Stephane Eraniand14b12d2010-09-17 11:28:47 +02006480 }
6481
Stephane Eraniane5d13672011-02-14 11:20:01 +02006482 if (flags & PERF_FLAG_PID_CGROUP) {
6483 err = perf_cgroup_connect(pid, event, &attr, group_leader);
6484 if (err)
6485 goto err_alloc;
Peter Zijlstra08309372011-03-03 11:31:20 +01006486 /*
6487 * one more event:
6488 * - that has cgroup constraint on event->cpu
6489 * - that may need work on context switch
6490 */
6491 atomic_inc(&per_cpu(perf_cgroup_events, event->cpu));
6492 jump_label_inc(&perf_sched_events);
Stephane Eraniane5d13672011-02-14 11:20:01 +02006493 }
6494
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006495 /*
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006496 * Special case software events and allow them to be part of
6497 * any hardware group.
6498 */
6499 pmu = event->pmu;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006500
6501 if (group_leader &&
6502 (is_software_event(event) != is_software_event(group_leader))) {
6503 if (is_software_event(event)) {
6504 /*
6505 * If event and group_leader are not both a software
6506 * event, and event is, then group leader is not.
6507 *
6508 * Allow the addition of software events to !software
6509 * groups, this is safe because software events never
6510 * fail to schedule.
6511 */
6512 pmu = group_leader->pmu;
6513 } else if (is_software_event(group_leader) &&
6514 (group_leader->group_flags & PERF_GROUP_SOFTWARE)) {
6515 /*
6516 * In case the group is a pure software group, and we
6517 * try to add a hardware event, move the whole group to
6518 * the hardware context.
6519 */
6520 move_group = 1;
6521 }
6522 }
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006523
6524 /*
6525 * Get the target context (task or percpu):
6526 */
Matt Helsley38a81da2010-09-13 13:01:20 -07006527 ctx = find_get_context(pmu, task, cpu);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006528 if (IS_ERR(ctx)) {
6529 err = PTR_ERR(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02006530 goto err_alloc;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006531 }
6532
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006533 /*
6534 * Look up the group leader (we will attach this event to it):
6535 */
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006536 if (group_leader) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006537 err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006538
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006539 /*
6540 * Do not allow a recursive hierarchy (this new sibling
6541 * becoming part of another group-sibling):
6542 */
6543 if (group_leader->group_leader != group_leader)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006544 goto err_context;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006545 /*
6546 * Do not allow to attach to a group in a different
6547 * task or CPU context:
6548 */
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006549 if (move_group) {
6550 if (group_leader->ctx->type != ctx->type)
6551 goto err_context;
6552 } else {
6553 if (group_leader->ctx != ctx)
6554 goto err_context;
6555 }
6556
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006557 /*
6558 * Only a group leader can be exclusive or pinned
6559 */
6560 if (attr.exclusive || attr.pinned)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006561 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006562 }
6563
6564 if (output_event) {
6565 err = perf_event_set_output(event, output_event);
6566 if (err)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006567 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006568 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006569
Al Viroea635c62010-05-26 17:40:29 -04006570 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, O_RDWR);
6571 if (IS_ERR(event_file)) {
6572 err = PTR_ERR(event_file);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006573 goto err_context;
Al Viroea635c62010-05-26 17:40:29 -04006574 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006575
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006576 if (move_group) {
6577 struct perf_event_context *gctx = group_leader->ctx;
6578
6579 mutex_lock(&gctx->mutex);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01006580 perf_remove_from_context(group_leader);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006581 list_for_each_entry(sibling, &group_leader->sibling_list,
6582 group_entry) {
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01006583 perf_remove_from_context(sibling);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006584 put_ctx(gctx);
6585 }
6586 mutex_unlock(&gctx->mutex);
6587 put_ctx(gctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006588 }
6589
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006590 event->filp = event_file;
6591 WARN_ON_ONCE(ctx->parent_ctx);
6592 mutex_lock(&ctx->mutex);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006593
6594 if (move_group) {
6595 perf_install_in_context(ctx, group_leader, cpu);
6596 get_ctx(ctx);
6597 list_for_each_entry(sibling, &group_leader->sibling_list,
6598 group_entry) {
6599 perf_install_in_context(ctx, sibling, cpu);
6600 get_ctx(ctx);
6601 }
6602 }
6603
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006604 perf_install_in_context(ctx, event, cpu);
6605 ++ctx->generation;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01006606 perf_unpin_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006607 mutex_unlock(&ctx->mutex);
6608
6609 event->owner = current;
Peter Zijlstra8882135b2010-11-09 19:01:43 +01006610
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006611 mutex_lock(&current->perf_event_mutex);
6612 list_add_tail(&event->owner_entry, &current->perf_event_list);
6613 mutex_unlock(&current->perf_event_mutex);
6614
Peter Zijlstra8a495422010-05-27 15:47:49 +02006615 /*
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02006616 * Precalculate sample_data sizes
6617 */
6618 perf_event__header_size(event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02006619 perf_event__id_header_size(event);
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02006620
6621 /*
Peter Zijlstra8a495422010-05-27 15:47:49 +02006622 * Drop the reference on the group_event after placing the
6623 * new event on the sibling_list. This ensures destruction
6624 * of the group leader will find the pointer to itself in
6625 * perf_group_detach().
6626 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006627 fput_light(group_file, fput_needed);
Al Viroea635c62010-05-26 17:40:29 -04006628 fd_install(event_fd, event_file);
6629 return event_fd;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006630
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006631err_context:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01006632 perf_unpin_context(ctx);
Al Viroea635c62010-05-26 17:40:29 -04006633 put_ctx(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02006634err_alloc:
6635 free_event(event);
Peter Zijlstrae7d0bc02010-10-14 16:54:51 +02006636err_task:
6637 if (task)
6638 put_task_struct(task);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006639err_group_fd:
6640 fput_light(group_file, fput_needed);
Al Viroea635c62010-05-26 17:40:29 -04006641err_fd:
6642 put_unused_fd(event_fd);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006643 return err;
6644}
6645
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006646/**
6647 * perf_event_create_kernel_counter
6648 *
6649 * @attr: attributes of the counter to create
6650 * @cpu: cpu in which the counter is bound
Matt Helsley38a81da2010-09-13 13:01:20 -07006651 * @task: task to profile (NULL for percpu)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006652 */
6653struct perf_event *
6654perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
Matt Helsley38a81da2010-09-13 13:01:20 -07006655 struct task_struct *task,
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01006656 perf_overflow_handler_t overflow_handler)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006657{
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006658 struct perf_event_context *ctx;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006659 struct perf_event *event;
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006660 int err;
6661
6662 /*
6663 * Get the target context (task or percpu):
6664 */
6665
Peter Zijlstrad580ff82010-10-14 17:43:23 +02006666 event = perf_event_alloc(attr, cpu, task, NULL, NULL, overflow_handler);
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01006667 if (IS_ERR(event)) {
6668 err = PTR_ERR(event);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006669 goto err;
6670 }
6671
Matt Helsley38a81da2010-09-13 13:01:20 -07006672 ctx = find_get_context(event->pmu, task, cpu);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006673 if (IS_ERR(ctx)) {
6674 err = PTR_ERR(ctx);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006675 goto err_free;
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01006676 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006677
6678 event->filp = NULL;
6679 WARN_ON_ONCE(ctx->parent_ctx);
6680 mutex_lock(&ctx->mutex);
6681 perf_install_in_context(ctx, event, cpu);
6682 ++ctx->generation;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01006683 perf_unpin_context(ctx);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006684 mutex_unlock(&ctx->mutex);
6685
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006686 return event;
6687
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006688err_free:
6689 free_event(event);
6690err:
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01006691 return ERR_PTR(err);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006692}
6693EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
6694
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006695static void sync_child_event(struct perf_event *child_event,
6696 struct task_struct *child)
6697{
6698 struct perf_event *parent_event = child_event->parent;
6699 u64 child_val;
6700
6701 if (child_event->attr.inherit_stat)
6702 perf_event_read_event(child_event, child);
6703
Peter Zijlstrab5e58792010-05-21 14:43:12 +02006704 child_val = perf_event_count(child_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006705
6706 /*
6707 * Add back the child's count to the parent's count:
6708 */
Peter Zijlstraa6e6dea2010-05-21 14:27:58 +02006709 atomic64_add(child_val, &parent_event->child_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006710 atomic64_add(child_event->total_time_enabled,
6711 &parent_event->child_total_time_enabled);
6712 atomic64_add(child_event->total_time_running,
6713 &parent_event->child_total_time_running);
6714
6715 /*
6716 * Remove this event from the parent's list
6717 */
6718 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
6719 mutex_lock(&parent_event->child_mutex);
6720 list_del_init(&child_event->child_list);
6721 mutex_unlock(&parent_event->child_mutex);
6722
6723 /*
6724 * Release the parent event, if this was the last
6725 * reference to it.
6726 */
6727 fput(parent_event->filp);
6728}
6729
6730static void
6731__perf_event_exit_task(struct perf_event *child_event,
6732 struct perf_event_context *child_ctx,
6733 struct task_struct *child)
6734{
Peter Zijlstra38b435b2011-03-15 14:37:10 +01006735 if (child_event->parent) {
6736 raw_spin_lock_irq(&child_ctx->lock);
6737 perf_group_detach(child_event);
6738 raw_spin_unlock_irq(&child_ctx->lock);
6739 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006740
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01006741 perf_remove_from_context(child_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006742
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006743 /*
Peter Zijlstra38b435b2011-03-15 14:37:10 +01006744 * It can happen that the parent exits first, and has events
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006745 * that are still around due to the child reference. These
Peter Zijlstra38b435b2011-03-15 14:37:10 +01006746 * events need to be zapped.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006747 */
Peter Zijlstra38b435b2011-03-15 14:37:10 +01006748 if (child_event->parent) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006749 sync_child_event(child_event, child);
6750 free_event(child_event);
6751 }
6752}
6753
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006754static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006755{
6756 struct perf_event *child_event, *tmp;
6757 struct perf_event_context *child_ctx;
6758 unsigned long flags;
6759
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006760 if (likely(!child->perf_event_ctxp[ctxn])) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006761 perf_event_task(child, NULL, 0);
6762 return;
6763 }
6764
6765 local_irq_save(flags);
6766 /*
6767 * We can't reschedule here because interrupts are disabled,
6768 * and either child is current or it is a task that can't be
6769 * scheduled, so we are now safe from rescheduling changing
6770 * our context.
6771 */
Oleg Nesterov806839b2011-01-21 18:45:47 +01006772 child_ctx = rcu_dereference_raw(child->perf_event_ctxp[ctxn]);
Peter Zijlstra82cd6de2010-10-14 17:57:23 +02006773 task_ctx_sched_out(child_ctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006774
6775 /*
6776 * Take the context lock here so that if find_get_context is
6777 * reading child->perf_event_ctxp, we wait until it has
6778 * incremented the context's refcount before we do put_ctx below.
6779 */
Thomas Gleixnere625cce2009-11-17 18:02:06 +01006780 raw_spin_lock(&child_ctx->lock);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006781 child->perf_event_ctxp[ctxn] = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006782 /*
6783 * If this context is a clone; unclone it so it can't get
6784 * swapped to another process while we're removing all
6785 * the events from it.
6786 */
6787 unclone_ctx(child_ctx);
Peter Zijlstra5e942bb2009-11-23 11:37:26 +01006788 update_context_time(child_ctx);
Thomas Gleixnere625cce2009-11-17 18:02:06 +01006789 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006790
6791 /*
6792 * Report the task dead after unscheduling the events so that we
6793 * won't get any samples after PERF_RECORD_EXIT. We can however still
6794 * get a few PERF_RECORD_READ events.
6795 */
6796 perf_event_task(child, child_ctx, 0);
6797
6798 /*
6799 * We can recurse on the same lock type through:
6800 *
6801 * __perf_event_exit_task()
6802 * sync_child_event()
6803 * fput(parent_event->filp)
6804 * perf_release()
6805 * mutex_lock(&ctx->mutex)
6806 *
6807 * But since its the parent context it won't be the same instance.
6808 */
Peter Zijlstraa0507c82010-05-06 15:42:53 +02006809 mutex_lock(&child_ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006810
6811again:
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006812 list_for_each_entry_safe(child_event, tmp, &child_ctx->pinned_groups,
6813 group_entry)
6814 __perf_event_exit_task(child_event, child_ctx, child);
6815
6816 list_for_each_entry_safe(child_event, tmp, &child_ctx->flexible_groups,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006817 group_entry)
6818 __perf_event_exit_task(child_event, child_ctx, child);
6819
6820 /*
6821 * If the last event was a group event, it will have appended all
6822 * its siblings to the list, but we obtained 'tmp' before that which
6823 * will still point to the list head terminating the iteration.
6824 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006825 if (!list_empty(&child_ctx->pinned_groups) ||
6826 !list_empty(&child_ctx->flexible_groups))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006827 goto again;
6828
6829 mutex_unlock(&child_ctx->mutex);
6830
6831 put_ctx(child_ctx);
6832}
6833
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006834/*
6835 * When a child task exits, feed back event values to parent events.
6836 */
6837void perf_event_exit_task(struct task_struct *child)
6838{
Peter Zijlstra8882135b2010-11-09 19:01:43 +01006839 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006840 int ctxn;
6841
Peter Zijlstra8882135b2010-11-09 19:01:43 +01006842 mutex_lock(&child->perf_event_mutex);
6843 list_for_each_entry_safe(event, tmp, &child->perf_event_list,
6844 owner_entry) {
6845 list_del_init(&event->owner_entry);
6846
6847 /*
6848 * Ensure the list deletion is visible before we clear
6849 * the owner, closes a race against perf_release() where
6850 * we need to serialize on the owner->perf_event_mutex.
6851 */
6852 smp_wmb();
6853 event->owner = NULL;
6854 }
6855 mutex_unlock(&child->perf_event_mutex);
6856
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006857 for_each_task_context_nr(ctxn)
6858 perf_event_exit_task_context(child, ctxn);
6859}
6860
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006861static void perf_free_event(struct perf_event *event,
6862 struct perf_event_context *ctx)
6863{
6864 struct perf_event *parent = event->parent;
6865
6866 if (WARN_ON_ONCE(!parent))
6867 return;
6868
6869 mutex_lock(&parent->child_mutex);
6870 list_del_init(&event->child_list);
6871 mutex_unlock(&parent->child_mutex);
6872
6873 fput(parent->filp);
6874
Peter Zijlstra8a495422010-05-27 15:47:49 +02006875 perf_group_detach(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006876 list_del_event(event, ctx);
6877 free_event(event);
6878}
6879
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006880/*
6881 * free an unexposed, unused context as created by inheritance by
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006882 * perf_event_init_task below, used by fork() in case of fail.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006883 */
6884void perf_event_free_task(struct task_struct *task)
6885{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006886 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006887 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006888 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006889
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006890 for_each_task_context_nr(ctxn) {
6891 ctx = task->perf_event_ctxp[ctxn];
6892 if (!ctx)
6893 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006894
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006895 mutex_lock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006896again:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006897 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
6898 group_entry)
6899 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006900
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006901 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
6902 group_entry)
6903 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006904
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006905 if (!list_empty(&ctx->pinned_groups) ||
6906 !list_empty(&ctx->flexible_groups))
6907 goto again;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006908
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006909 mutex_unlock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006910
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006911 put_ctx(ctx);
6912 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006913}
6914
Peter Zijlstra4e231c72010-09-09 21:01:59 +02006915void perf_event_delayed_put(struct task_struct *task)
6916{
6917 int ctxn;
6918
6919 for_each_task_context_nr(ctxn)
6920 WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
6921}
6922
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02006923/*
6924 * inherit a event from parent task to child task:
6925 */
6926static struct perf_event *
6927inherit_event(struct perf_event *parent_event,
6928 struct task_struct *parent,
6929 struct perf_event_context *parent_ctx,
6930 struct task_struct *child,
6931 struct perf_event *group_leader,
6932 struct perf_event_context *child_ctx)
6933{
6934 struct perf_event *child_event;
Peter Zijlstracee010e2010-09-10 12:51:54 +02006935 unsigned long flags;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02006936
6937 /*
6938 * Instead of creating recursive hierarchies of events,
6939 * we link inherited events back to the original parent,
6940 * which has a filp for sure, which we use as the reference
6941 * count:
6942 */
6943 if (parent_event->parent)
6944 parent_event = parent_event->parent;
6945
6946 child_event = perf_event_alloc(&parent_event->attr,
6947 parent_event->cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +02006948 child,
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02006949 group_leader, parent_event,
6950 NULL);
6951 if (IS_ERR(child_event))
6952 return child_event;
6953 get_ctx(child_ctx);
6954
6955 /*
6956 * Make the child state follow the state of the parent event,
6957 * not its attr.disabled bit. We hold the parent's mutex,
6958 * so we won't race with perf_event_{en, dis}able_family.
6959 */
6960 if (parent_event->state >= PERF_EVENT_STATE_INACTIVE)
6961 child_event->state = PERF_EVENT_STATE_INACTIVE;
6962 else
6963 child_event->state = PERF_EVENT_STATE_OFF;
6964
6965 if (parent_event->attr.freq) {
6966 u64 sample_period = parent_event->hw.sample_period;
6967 struct hw_perf_event *hwc = &child_event->hw;
6968
6969 hwc->sample_period = sample_period;
6970 hwc->last_period = sample_period;
6971
6972 local64_set(&hwc->period_left, sample_period);
6973 }
6974
6975 child_event->ctx = child_ctx;
6976 child_event->overflow_handler = parent_event->overflow_handler;
6977
6978 /*
Thomas Gleixner614b6782010-12-03 16:24:32 -02006979 * Precalculate sample_data sizes
6980 */
6981 perf_event__header_size(child_event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02006982 perf_event__id_header_size(child_event);
Thomas Gleixner614b6782010-12-03 16:24:32 -02006983
6984 /*
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02006985 * Link it up in the child's context:
6986 */
Peter Zijlstracee010e2010-09-10 12:51:54 +02006987 raw_spin_lock_irqsave(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02006988 add_event_to_ctx(child_event, child_ctx);
Peter Zijlstracee010e2010-09-10 12:51:54 +02006989 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02006990
6991 /*
6992 * Get a reference to the parent filp - we will fput it
6993 * when the child event exits. This is safe to do because
6994 * we are in the parent and we know that the filp still
6995 * exists and has a nonzero count:
6996 */
6997 atomic_long_inc(&parent_event->filp->f_count);
6998
6999 /*
7000 * Link this into the parent event's child list
7001 */
7002 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
7003 mutex_lock(&parent_event->child_mutex);
7004 list_add_tail(&child_event->child_list, &parent_event->child_list);
7005 mutex_unlock(&parent_event->child_mutex);
7006
7007 return child_event;
7008}
7009
7010static int inherit_group(struct perf_event *parent_event,
7011 struct task_struct *parent,
7012 struct perf_event_context *parent_ctx,
7013 struct task_struct *child,
7014 struct perf_event_context *child_ctx)
7015{
7016 struct perf_event *leader;
7017 struct perf_event *sub;
7018 struct perf_event *child_ctr;
7019
7020 leader = inherit_event(parent_event, parent, parent_ctx,
7021 child, NULL, child_ctx);
7022 if (IS_ERR(leader))
7023 return PTR_ERR(leader);
7024 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
7025 child_ctr = inherit_event(sub, parent, parent_ctx,
7026 child, leader, child_ctx);
7027 if (IS_ERR(child_ctr))
7028 return PTR_ERR(child_ctr);
7029 }
7030 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007031}
7032
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007033static int
7034inherit_task_group(struct perf_event *event, struct task_struct *parent,
7035 struct perf_event_context *parent_ctx,
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007036 struct task_struct *child, int ctxn,
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007037 int *inherited_all)
7038{
7039 int ret;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007040 struct perf_event_context *child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007041
7042 if (!event->attr.inherit) {
7043 *inherited_all = 0;
7044 return 0;
7045 }
7046
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007047 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007048 if (!child_ctx) {
7049 /*
7050 * This is executed from the parent task context, so
7051 * inherit events that have been marked for cloning.
7052 * First allocate and initialize a context for the
7053 * child.
7054 */
7055
Peter Zijlstraeb184472010-09-07 15:55:13 +02007056 child_ctx = alloc_perf_context(event->pmu, child);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007057 if (!child_ctx)
7058 return -ENOMEM;
7059
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007060 child->perf_event_ctxp[ctxn] = child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007061 }
7062
7063 ret = inherit_group(event, parent, parent_ctx,
7064 child, child_ctx);
7065
7066 if (ret)
7067 *inherited_all = 0;
7068
7069 return ret;
7070}
7071
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007072/*
7073 * Initialize the perf_event context in task_struct
7074 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007075int perf_event_init_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007076{
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007077 struct perf_event_context *child_ctx, *parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007078 struct perf_event_context *cloned_ctx;
7079 struct perf_event *event;
7080 struct task_struct *parent = current;
7081 int inherited_all = 1;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01007082 unsigned long flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007083 int ret = 0;
7084
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007085 if (likely(!parent->perf_event_ctxp[ctxn]))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007086 return 0;
7087
7088 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007089 * If the parent's context is a clone, pin it so it won't get
7090 * swapped under us.
7091 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007092 parent_ctx = perf_pin_task_context(parent, ctxn);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007093
7094 /*
7095 * No need to check if parent_ctx != NULL here; since we saw
7096 * it non-NULL earlier, the only reason for it to become NULL
7097 * is if we exit, and since we're currently in the middle of
7098 * a fork we can't be exiting at the same time.
7099 */
7100
7101 /*
7102 * Lock the parent list. No need to lock the child - not PID
7103 * hashed yet and not running, so nobody can access it.
7104 */
7105 mutex_lock(&parent_ctx->mutex);
7106
7107 /*
7108 * We dont have to disable NMIs - we are only looking at
7109 * the list, not manipulating it:
7110 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007111 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007112 ret = inherit_task_group(event, parent, parent_ctx,
7113 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007114 if (ret)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007115 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007116 }
7117
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01007118 /*
7119 * We can't hold ctx->lock when iterating the ->flexible_group list due
7120 * to allocations, but we need to prevent rotation because
7121 * rotate_ctx() will change the list from interrupt context.
7122 */
7123 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
7124 parent_ctx->rotate_disable = 1;
7125 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
7126
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007127 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007128 ret = inherit_task_group(event, parent, parent_ctx,
7129 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007130 if (ret)
7131 break;
7132 }
7133
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01007134 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
7135 parent_ctx->rotate_disable = 0;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01007136
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007137 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007138
Peter Zijlstra05cbaa22009-12-30 16:00:35 +01007139 if (child_ctx && inherited_all) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007140 /*
7141 * Mark the child context as a clone of the parent
7142 * context, or of whatever the parent is a clone of.
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01007143 *
7144 * Note that if the parent is a clone, the holding of
7145 * parent_ctx->lock avoids it from being uncloned.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007146 */
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01007147 cloned_ctx = parent_ctx->parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007148 if (cloned_ctx) {
7149 child_ctx->parent_ctx = cloned_ctx;
7150 child_ctx->parent_gen = parent_ctx->parent_gen;
7151 } else {
7152 child_ctx->parent_ctx = parent_ctx;
7153 child_ctx->parent_gen = parent_ctx->generation;
7154 }
7155 get_ctx(child_ctx->parent_ctx);
7156 }
7157
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01007158 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007159 mutex_unlock(&parent_ctx->mutex);
7160
7161 perf_unpin_context(parent_ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007162 put_ctx(parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007163
7164 return ret;
7165}
7166
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007167/*
7168 * Initialize the perf_event context in task_struct
7169 */
7170int perf_event_init_task(struct task_struct *child)
7171{
7172 int ctxn, ret;
7173
Oleg Nesterov8550d7c2011-01-19 19:22:28 +01007174 memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp));
7175 mutex_init(&child->perf_event_mutex);
7176 INIT_LIST_HEAD(&child->perf_event_list);
7177
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007178 for_each_task_context_nr(ctxn) {
7179 ret = perf_event_init_context(child, ctxn);
7180 if (ret)
7181 return ret;
7182 }
7183
7184 return 0;
7185}
7186
Paul Mackerras220b1402010-03-10 20:45:52 +11007187static void __init perf_event_init_all_cpus(void)
7188{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007189 struct swevent_htable *swhash;
Paul Mackerras220b1402010-03-10 20:45:52 +11007190 int cpu;
Paul Mackerras220b1402010-03-10 20:45:52 +11007191
7192 for_each_possible_cpu(cpu) {
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007193 swhash = &per_cpu(swevent_htable, cpu);
7194 mutex_init(&swhash->hlist_mutex);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02007195 INIT_LIST_HEAD(&per_cpu(rotation_list, cpu));
Paul Mackerras220b1402010-03-10 20:45:52 +11007196 }
7197}
7198
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007199static void __cpuinit perf_event_init_cpu(int cpu)
7200{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007201 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007202
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007203 mutex_lock(&swhash->hlist_mutex);
7204 if (swhash->hlist_refcount > 0) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007205 struct swevent_hlist *hlist;
7206
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007207 hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
7208 WARN_ON(!hlist);
7209 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007210 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007211 mutex_unlock(&swhash->hlist_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007212}
7213
Peter Zijlstrac2774432010-12-08 15:29:02 +01007214#if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02007215static void perf_pmu_rotate_stop(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007216{
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02007217 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
7218
7219 WARN_ON(!irqs_disabled());
7220
7221 list_del_init(&cpuctx->rotation_list);
7222}
7223
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007224static void __perf_event_exit_context(void *__info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007225{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007226 struct perf_event_context *ctx = __info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007227 struct perf_event *event, *tmp;
7228
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007229 perf_pmu_rotate_stop(ctx->pmu);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02007230
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007231 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007232 __perf_remove_from_context(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007233 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, group_entry)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007234 __perf_remove_from_context(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007235}
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007236
7237static void perf_event_exit_cpu_context(int cpu)
7238{
7239 struct perf_event_context *ctx;
7240 struct pmu *pmu;
7241 int idx;
7242
7243 idx = srcu_read_lock(&pmus_srcu);
7244 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra917bdd12010-09-17 11:28:49 +02007245 ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007246
7247 mutex_lock(&ctx->mutex);
7248 smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
7249 mutex_unlock(&ctx->mutex);
7250 }
7251 srcu_read_unlock(&pmus_srcu, idx);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007252}
7253
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007254static void perf_event_exit_cpu(int cpu)
7255{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007256 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007257
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007258 mutex_lock(&swhash->hlist_mutex);
7259 swevent_hlist_release(swhash);
7260 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007261
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007262 perf_event_exit_cpu_context(cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007263}
7264#else
7265static inline void perf_event_exit_cpu(int cpu) { }
7266#endif
7267
Peter Zijlstrac2774432010-12-08 15:29:02 +01007268static int
7269perf_reboot(struct notifier_block *notifier, unsigned long val, void *v)
7270{
7271 int cpu;
7272
7273 for_each_online_cpu(cpu)
7274 perf_event_exit_cpu(cpu);
7275
7276 return NOTIFY_OK;
7277}
7278
7279/*
7280 * Run the perf reboot notifier at the very last possible moment so that
7281 * the generic watchdog code runs as long as possible.
7282 */
7283static struct notifier_block perf_reboot_notifier = {
7284 .notifier_call = perf_reboot,
7285 .priority = INT_MIN,
7286};
7287
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007288static int __cpuinit
7289perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
7290{
7291 unsigned int cpu = (long)hcpu;
7292
Peter Zijlstra5e116372010-06-11 13:35:08 +02007293 switch (action & ~CPU_TASKS_FROZEN) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007294
7295 case CPU_UP_PREPARE:
Peter Zijlstra5e116372010-06-11 13:35:08 +02007296 case CPU_DOWN_FAILED:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007297 perf_event_init_cpu(cpu);
7298 break;
7299
Peter Zijlstra5e116372010-06-11 13:35:08 +02007300 case CPU_UP_CANCELED:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007301 case CPU_DOWN_PREPARE:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007302 perf_event_exit_cpu(cpu);
7303 break;
7304
7305 default:
7306 break;
7307 }
7308
7309 return NOTIFY_OK;
7310}
7311
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007312void __init perf_event_init(void)
7313{
Jason Wessel3c502e72010-11-04 17:33:01 -05007314 int ret;
7315
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007316 idr_init(&pmu_idr);
7317
Paul Mackerras220b1402010-03-10 20:45:52 +11007318 perf_event_init_all_cpus();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007319 init_srcu_struct(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007320 perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE);
7321 perf_pmu_register(&perf_cpu_clock, NULL, -1);
7322 perf_pmu_register(&perf_task_clock, NULL, -1);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007323 perf_tp_register();
7324 perf_cpu_notifier(perf_cpu_notify);
Peter Zijlstrac2774432010-12-08 15:29:02 +01007325 register_reboot_notifier(&perf_reboot_notifier);
Jason Wessel3c502e72010-11-04 17:33:01 -05007326
7327 ret = init_hw_breakpoint();
7328 WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007329}
Peter Zijlstraabe43402010-11-17 23:17:37 +01007330
7331static int __init perf_event_sysfs_init(void)
7332{
7333 struct pmu *pmu;
7334 int ret;
7335
7336 mutex_lock(&pmus_lock);
7337
7338 ret = bus_register(&pmu_bus);
7339 if (ret)
7340 goto unlock;
7341
7342 list_for_each_entry(pmu, &pmus, entry) {
7343 if (!pmu->name || pmu->type < 0)
7344 continue;
7345
7346 ret = pmu_dev_alloc(pmu);
7347 WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret);
7348 }
7349 pmu_bus_running = 1;
7350 ret = 0;
7351
7352unlock:
7353 mutex_unlock(&pmus_lock);
7354
7355 return ret;
7356}
7357device_initcall(perf_event_sysfs_init);
Stephane Eraniane5d13672011-02-14 11:20:01 +02007358
7359#ifdef CONFIG_CGROUP_PERF
7360static struct cgroup_subsys_state *perf_cgroup_create(
7361 struct cgroup_subsys *ss, struct cgroup *cont)
7362{
7363 struct perf_cgroup *jc;
Stephane Eraniane5d13672011-02-14 11:20:01 +02007364
Li Zefan1b15d052011-03-03 14:26:06 +08007365 jc = kzalloc(sizeof(*jc), GFP_KERNEL);
Stephane Eraniane5d13672011-02-14 11:20:01 +02007366 if (!jc)
7367 return ERR_PTR(-ENOMEM);
7368
Stephane Eraniane5d13672011-02-14 11:20:01 +02007369 jc->info = alloc_percpu(struct perf_cgroup_info);
7370 if (!jc->info) {
7371 kfree(jc);
7372 return ERR_PTR(-ENOMEM);
7373 }
7374
Stephane Eraniane5d13672011-02-14 11:20:01 +02007375 return &jc->css;
7376}
7377
7378static void perf_cgroup_destroy(struct cgroup_subsys *ss,
7379 struct cgroup *cont)
7380{
7381 struct perf_cgroup *jc;
7382 jc = container_of(cgroup_subsys_state(cont, perf_subsys_id),
7383 struct perf_cgroup, css);
7384 free_percpu(jc->info);
7385 kfree(jc);
7386}
7387
7388static int __perf_cgroup_move(void *info)
7389{
7390 struct task_struct *task = info;
7391 perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN);
7392 return 0;
7393}
7394
7395static void perf_cgroup_move(struct task_struct *task)
7396{
7397 task_function_call(task, __perf_cgroup_move, task);
7398}
7399
7400static void perf_cgroup_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
7401 struct cgroup *old_cgrp, struct task_struct *task,
7402 bool threadgroup)
7403{
7404 perf_cgroup_move(task);
7405 if (threadgroup) {
7406 struct task_struct *c;
7407 rcu_read_lock();
7408 list_for_each_entry_rcu(c, &task->thread_group, thread_group) {
7409 perf_cgroup_move(c);
7410 }
7411 rcu_read_unlock();
7412 }
7413}
7414
7415static void perf_cgroup_exit(struct cgroup_subsys *ss, struct cgroup *cgrp,
7416 struct cgroup *old_cgrp, struct task_struct *task)
7417{
7418 /*
7419 * cgroup_exit() is called in the copy_process() failure path.
7420 * Ignore this case since the task hasn't ran yet, this avoids
7421 * trying to poke a half freed task state from generic code.
7422 */
7423 if (!(task->flags & PF_EXITING))
7424 return;
7425
7426 perf_cgroup_move(task);
7427}
7428
7429struct cgroup_subsys perf_subsys = {
7430 .name = "perf_event",
7431 .subsys_id = perf_subsys_id,
7432 .create = perf_cgroup_create,
7433 .destroy = perf_cgroup_destroy,
7434 .exit = perf_cgroup_exit,
7435 .attach = perf_cgroup_attach,
7436};
7437#endif /* CONFIG_CGROUP_PERF */