blob: dadeaea4b3fc7595fe667fd9b20e07bc8a3d5eda [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);
407 if (IS_ERR(css))
408 return PTR_ERR(css);
409
410 cgrp = container_of(css, struct perf_cgroup, css);
411 event->cgrp = cgrp;
412
413 /*
414 * all events in a group must monitor
415 * the same cgroup because a task belongs
416 * to only one perf cgroup at a time
417 */
418 if (group_leader && group_leader->cgrp != cgrp) {
419 perf_detach_cgroup(event);
420 ret = -EINVAL;
421 } else {
422 /* must be done before we fput() the file */
423 perf_get_cgroup(event);
424 }
425 fput_light(file, fput_needed);
426 return ret;
427}
428
429static inline void
430perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
431{
432 struct perf_cgroup_info *t;
433 t = per_cpu_ptr(event->cgrp->info, event->cpu);
434 event->shadow_ctx_time = now - t->timestamp;
435}
436
437static inline void
438perf_cgroup_defer_enabled(struct perf_event *event)
439{
440 /*
441 * when the current task's perf cgroup does not match
442 * the event's, we need to remember to call the
443 * perf_mark_enable() function the first time a task with
444 * a matching perf cgroup is scheduled in.
445 */
446 if (is_cgroup_event(event) && !perf_cgroup_match(event))
447 event->cgrp_defer_enabled = 1;
448}
449
450static inline void
451perf_cgroup_mark_enabled(struct perf_event *event,
452 struct perf_event_context *ctx)
453{
454 struct perf_event *sub;
455 u64 tstamp = perf_event_time(event);
456
457 if (!event->cgrp_defer_enabled)
458 return;
459
460 event->cgrp_defer_enabled = 0;
461
462 event->tstamp_enabled = tstamp - event->total_time_enabled;
463 list_for_each_entry(sub, &event->sibling_list, group_entry) {
464 if (sub->state >= PERF_EVENT_STATE_INACTIVE) {
465 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
466 sub->cgrp_defer_enabled = 0;
467 }
468 }
469}
470#else /* !CONFIG_CGROUP_PERF */
471
472static inline bool
473perf_cgroup_match(struct perf_event *event)
474{
475 return true;
476}
477
478static inline void perf_detach_cgroup(struct perf_event *event)
479{}
480
481static inline int is_cgroup_event(struct perf_event *event)
482{
483 return 0;
484}
485
486static inline u64 perf_cgroup_event_cgrp_time(struct perf_event *event)
487{
488 return 0;
489}
490
491static inline void update_cgrp_time_from_event(struct perf_event *event)
492{
493}
494
495static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
496{
497}
498
499static inline void perf_cgroup_sched_out(struct task_struct *task)
500{
501}
502
503static inline void perf_cgroup_sched_in(struct task_struct *task)
504{
505}
506
507static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event,
508 struct perf_event_attr *attr,
509 struct perf_event *group_leader)
510{
511 return -EINVAL;
512}
513
514static inline void
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200515perf_cgroup_set_timestamp(struct task_struct *task,
516 struct perf_event_context *ctx)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200517{
518}
519
520void
521perf_cgroup_switch(struct task_struct *task, struct task_struct *next)
522{
523}
524
525static inline void
526perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
527{
528}
529
530static inline u64 perf_cgroup_event_time(struct perf_event *event)
531{
532 return 0;
533}
534
535static inline void
536perf_cgroup_defer_enabled(struct perf_event *event)
537{
538}
539
540static inline void
541perf_cgroup_mark_enabled(struct perf_event *event,
542 struct perf_event_context *ctx)
543{
544}
545#endif
546
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200547void perf_pmu_disable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200548{
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200549 int *count = this_cpu_ptr(pmu->pmu_disable_count);
550 if (!(*count)++)
551 pmu->pmu_disable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200552}
553
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200554void perf_pmu_enable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200555{
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200556 int *count = this_cpu_ptr(pmu->pmu_disable_count);
557 if (!--(*count))
558 pmu->pmu_enable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200559}
560
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200561static DEFINE_PER_CPU(struct list_head, rotation_list);
562
563/*
564 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
565 * because they're strictly cpu affine and rotate_start is called with IRQs
566 * disabled, while rotate_context is called from IRQ context.
567 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200568static void perf_pmu_rotate_start(struct pmu *pmu)
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200569{
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200570 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200571 struct list_head *head = &__get_cpu_var(rotation_list);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200572
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200573 WARN_ON(!irqs_disabled());
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200574
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200575 if (list_empty(&cpuctx->rotation_list))
576 list_add(&cpuctx->rotation_list, head);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200577}
578
579static void get_ctx(struct perf_event_context *ctx)
580{
581 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
582}
583
584static void free_ctx(struct rcu_head *head)
585{
586 struct perf_event_context *ctx;
587
588 ctx = container_of(head, struct perf_event_context, rcu_head);
589 kfree(ctx);
590}
591
592static void put_ctx(struct perf_event_context *ctx)
593{
594 if (atomic_dec_and_test(&ctx->refcount)) {
595 if (ctx->parent_ctx)
596 put_ctx(ctx->parent_ctx);
597 if (ctx->task)
598 put_task_struct(ctx->task);
599 call_rcu(&ctx->rcu_head, free_ctx);
600 }
601}
602
603static void unclone_ctx(struct perf_event_context *ctx)
604{
605 if (ctx->parent_ctx) {
606 put_ctx(ctx->parent_ctx);
607 ctx->parent_ctx = NULL;
608 }
609}
610
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -0200611static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
612{
613 /*
614 * only top level events have the pid namespace they were created in
615 */
616 if (event->parent)
617 event = event->parent;
618
619 return task_tgid_nr_ns(p, event->ns);
620}
621
622static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
623{
624 /*
625 * only top level events have the pid namespace they were created in
626 */
627 if (event->parent)
628 event = event->parent;
629
630 return task_pid_nr_ns(p, event->ns);
631}
632
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200633/*
634 * If we inherit events we want to return the parent event id
635 * to userspace.
636 */
637static u64 primary_event_id(struct perf_event *event)
638{
639 u64 id = event->id;
640
641 if (event->parent)
642 id = event->parent->id;
643
644 return id;
645}
646
647/*
648 * Get the perf_event_context for a task and lock it.
649 * This has to cope with with the fact that until it is locked,
650 * the context could get moved to another task.
651 */
652static struct perf_event_context *
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200653perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200654{
655 struct perf_event_context *ctx;
656
657 rcu_read_lock();
Peter Zijlstra9ed60602010-06-11 17:36:35 +0200658retry:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200659 ctx = rcu_dereference(task->perf_event_ctxp[ctxn]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200660 if (ctx) {
661 /*
662 * If this context is a clone of another, it might
663 * get swapped for another underneath us by
664 * perf_event_task_sched_out, though the
665 * rcu_read_lock() protects us from any context
666 * getting freed. Lock the context and check if it
667 * got swapped before we could get the lock, and retry
668 * if so. If we locked the right context, then it
669 * can't get swapped on us any more.
670 */
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100671 raw_spin_lock_irqsave(&ctx->lock, *flags);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200672 if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) {
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100673 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200674 goto retry;
675 }
676
677 if (!atomic_inc_not_zero(&ctx->refcount)) {
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100678 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200679 ctx = NULL;
680 }
681 }
682 rcu_read_unlock();
683 return ctx;
684}
685
686/*
687 * Get the context for a task and increment its pin_count so it
688 * can't get swapped to another task. This also increments its
689 * reference count so that the context can't get freed.
690 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200691static struct perf_event_context *
692perf_pin_task_context(struct task_struct *task, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200693{
694 struct perf_event_context *ctx;
695 unsigned long flags;
696
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200697 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200698 if (ctx) {
699 ++ctx->pin_count;
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100700 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200701 }
702 return ctx;
703}
704
705static void perf_unpin_context(struct perf_event_context *ctx)
706{
707 unsigned long flags;
708
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100709 raw_spin_lock_irqsave(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200710 --ctx->pin_count;
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100711 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200712}
713
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100714/*
715 * Update the record of the current time in a context.
716 */
717static void update_context_time(struct perf_event_context *ctx)
718{
719 u64 now = perf_clock();
720
721 ctx->time += now - ctx->timestamp;
722 ctx->timestamp = now;
723}
724
Stephane Eranian41587552011-01-03 18:20:01 +0200725static u64 perf_event_time(struct perf_event *event)
726{
727 struct perf_event_context *ctx = event->ctx;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200728
729 if (is_cgroup_event(event))
730 return perf_cgroup_event_time(event);
731
Stephane Eranian41587552011-01-03 18:20:01 +0200732 return ctx ? ctx->time : 0;
733}
734
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100735/*
736 * Update the total_time_enabled and total_time_running fields for a event.
737 */
738static void update_event_times(struct perf_event *event)
739{
740 struct perf_event_context *ctx = event->ctx;
741 u64 run_end;
742
743 if (event->state < PERF_EVENT_STATE_INACTIVE ||
744 event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
745 return;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200746 /*
747 * in cgroup mode, time_enabled represents
748 * the time the event was enabled AND active
749 * tasks were in the monitored cgroup. This is
750 * independent of the activity of the context as
751 * there may be a mix of cgroup and non-cgroup events.
752 *
753 * That is why we treat cgroup events differently
754 * here.
755 */
756 if (is_cgroup_event(event))
Stephane Eranian41587552011-01-03 18:20:01 +0200757 run_end = perf_event_time(event);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200758 else if (ctx->is_active)
759 run_end = ctx->time;
Peter Zijlstraacd1d7c2009-11-23 15:00:36 +0100760 else
761 run_end = event->tstamp_stopped;
762
763 event->total_time_enabled = run_end - event->tstamp_enabled;
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100764
765 if (event->state == PERF_EVENT_STATE_INACTIVE)
766 run_end = event->tstamp_stopped;
767 else
Stephane Eranian41587552011-01-03 18:20:01 +0200768 run_end = perf_event_time(event);
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100769
770 event->total_time_running = run_end - event->tstamp_running;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200771
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100772}
773
Peter Zijlstra96c21a42010-05-11 16:19:10 +0200774/*
775 * Update total_time_enabled and total_time_running for all events in a group.
776 */
777static void update_group_times(struct perf_event *leader)
778{
779 struct perf_event *event;
780
781 update_event_times(leader);
782 list_for_each_entry(event, &leader->sibling_list, group_entry)
783 update_event_times(event);
784}
785
Frederic Weisbecker889ff012010-01-09 20:04:47 +0100786static struct list_head *
787ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
788{
789 if (event->attr.pinned)
790 return &ctx->pinned_groups;
791 else
792 return &ctx->flexible_groups;
793}
794
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200795/*
796 * Add a event from the lists for its context.
797 * Must be called with ctx->mutex and ctx->lock held.
798 */
799static void
800list_add_event(struct perf_event *event, struct perf_event_context *ctx)
801{
Peter Zijlstra8a495422010-05-27 15:47:49 +0200802 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
803 event->attach_state |= PERF_ATTACH_CONTEXT;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200804
805 /*
Peter Zijlstra8a495422010-05-27 15:47:49 +0200806 * If we're a stand alone event or group leader, we go to the context
807 * list, group events are kept attached to the group so that
808 * perf_group_detach can, at all times, locate all siblings.
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200809 */
Peter Zijlstra8a495422010-05-27 15:47:49 +0200810 if (event->group_leader == event) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +0100811 struct list_head *list;
812
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +0100813 if (is_software_event(event))
814 event->group_flags |= PERF_GROUP_SOFTWARE;
815
Frederic Weisbecker889ff012010-01-09 20:04:47 +0100816 list = ctx_group_list(event, ctx);
817 list_add_tail(&event->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200818 }
819
Stephane Eraniane5d13672011-02-14 11:20:01 +0200820 if (is_cgroup_event(event)) {
821 ctx->nr_cgroups++;
822 /*
823 * one more event:
824 * - that has cgroup constraint on event->cpu
825 * - that may need work on context switch
826 */
827 atomic_inc(&per_cpu(perf_cgroup_events, event->cpu));
828 jump_label_inc(&perf_sched_events);
829 }
830
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200831 list_add_rcu(&event->event_entry, &ctx->event_list);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200832 if (!ctx->nr_events)
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200833 perf_pmu_rotate_start(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200834 ctx->nr_events++;
835 if (event->attr.inherit_stat)
836 ctx->nr_stat++;
837}
838
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -0200839/*
840 * Called at perf_event creation and when events are attached/detached from a
841 * group.
842 */
843static void perf_event__read_size(struct perf_event *event)
844{
845 int entry = sizeof(u64); /* value */
846 int size = 0;
847 int nr = 1;
848
849 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
850 size += sizeof(u64);
851
852 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
853 size += sizeof(u64);
854
855 if (event->attr.read_format & PERF_FORMAT_ID)
856 entry += sizeof(u64);
857
858 if (event->attr.read_format & PERF_FORMAT_GROUP) {
859 nr += event->group_leader->nr_siblings;
860 size += sizeof(u64);
861 }
862
863 size += entry * nr;
864 event->read_size = size;
865}
866
867static void perf_event__header_size(struct perf_event *event)
868{
869 struct perf_sample_data *data;
870 u64 sample_type = event->attr.sample_type;
871 u16 size = 0;
872
873 perf_event__read_size(event);
874
875 if (sample_type & PERF_SAMPLE_IP)
876 size += sizeof(data->ip);
877
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -0200878 if (sample_type & PERF_SAMPLE_ADDR)
879 size += sizeof(data->addr);
880
881 if (sample_type & PERF_SAMPLE_PERIOD)
882 size += sizeof(data->period);
883
884 if (sample_type & PERF_SAMPLE_READ)
885 size += event->read_size;
886
887 event->header_size = size;
888}
889
890static void perf_event__id_header_size(struct perf_event *event)
891{
892 struct perf_sample_data *data;
893 u64 sample_type = event->attr.sample_type;
894 u16 size = 0;
895
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -0200896 if (sample_type & PERF_SAMPLE_TID)
897 size += sizeof(data->tid_entry);
898
899 if (sample_type & PERF_SAMPLE_TIME)
900 size += sizeof(data->time);
901
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -0200902 if (sample_type & PERF_SAMPLE_ID)
903 size += sizeof(data->id);
904
905 if (sample_type & PERF_SAMPLE_STREAM_ID)
906 size += sizeof(data->stream_id);
907
908 if (sample_type & PERF_SAMPLE_CPU)
909 size += sizeof(data->cpu_entry);
910
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -0200911 event->id_header_size = size;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -0200912}
913
Peter Zijlstra8a495422010-05-27 15:47:49 +0200914static void perf_group_attach(struct perf_event *event)
915{
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -0200916 struct perf_event *group_leader = event->group_leader, *pos;
Peter Zijlstra8a495422010-05-27 15:47:49 +0200917
Peter Zijlstra74c33372010-10-15 11:40:29 +0200918 /*
919 * We can have double attach due to group movement in perf_event_open.
920 */
921 if (event->attach_state & PERF_ATTACH_GROUP)
922 return;
923
Peter Zijlstra8a495422010-05-27 15:47:49 +0200924 event->attach_state |= PERF_ATTACH_GROUP;
925
926 if (group_leader == event)
927 return;
928
929 if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
930 !is_software_event(event))
931 group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
932
933 list_add_tail(&event->group_entry, &group_leader->sibling_list);
934 group_leader->nr_siblings++;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -0200935
936 perf_event__header_size(group_leader);
937
938 list_for_each_entry(pos, &group_leader->sibling_list, group_entry)
939 perf_event__header_size(pos);
Peter Zijlstra8a495422010-05-27 15:47:49 +0200940}
941
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200942/*
943 * Remove a event from the lists for its context.
944 * Must be called with ctx->mutex and ctx->lock held.
945 */
946static void
947list_del_event(struct perf_event *event, struct perf_event_context *ctx)
948{
Peter Zijlstra8a495422010-05-27 15:47:49 +0200949 /*
950 * We can have double detach due to exit/hot-unplug + close.
951 */
952 if (!(event->attach_state & PERF_ATTACH_CONTEXT))
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200953 return;
Peter Zijlstra8a495422010-05-27 15:47:49 +0200954
955 event->attach_state &= ~PERF_ATTACH_CONTEXT;
956
Stephane Eraniane5d13672011-02-14 11:20:01 +0200957 if (is_cgroup_event(event)) {
958 ctx->nr_cgroups--;
959 atomic_dec(&per_cpu(perf_cgroup_events, event->cpu));
960 jump_label_dec(&perf_sched_events);
961 }
962
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200963 ctx->nr_events--;
964 if (event->attr.inherit_stat)
965 ctx->nr_stat--;
966
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200967 list_del_rcu(&event->event_entry);
968
Peter Zijlstra8a495422010-05-27 15:47:49 +0200969 if (event->group_leader == event)
970 list_del_init(&event->group_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200971
Peter Zijlstra96c21a42010-05-11 16:19:10 +0200972 update_group_times(event);
Stephane Eranianb2e74a22009-11-26 09:24:30 -0800973
974 /*
975 * If event was in error state, then keep it
976 * that way, otherwise bogus counts will be
977 * returned on read(). The only way to get out
978 * of error state is by explicit re-enabling
979 * of the event
980 */
981 if (event->state > PERF_EVENT_STATE_OFF)
982 event->state = PERF_EVENT_STATE_OFF;
Peter Zijlstra050735b2010-05-11 11:51:53 +0200983}
984
Peter Zijlstra8a495422010-05-27 15:47:49 +0200985static void perf_group_detach(struct perf_event *event)
Peter Zijlstra050735b2010-05-11 11:51:53 +0200986{
987 struct perf_event *sibling, *tmp;
Peter Zijlstra8a495422010-05-27 15:47:49 +0200988 struct list_head *list = NULL;
989
990 /*
991 * We can have double detach due to exit/hot-unplug + close.
992 */
993 if (!(event->attach_state & PERF_ATTACH_GROUP))
994 return;
995
996 event->attach_state &= ~PERF_ATTACH_GROUP;
997
998 /*
999 * If this is a sibling, remove it from its group.
1000 */
1001 if (event->group_leader != event) {
1002 list_del_init(&event->group_entry);
1003 event->group_leader->nr_siblings--;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001004 goto out;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001005 }
1006
1007 if (!list_empty(&event->group_entry))
1008 list = &event->group_entry;
Peter Zijlstra2e2af502009-11-23 11:37:25 +01001009
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001010 /*
1011 * If this was a group event with sibling events then
1012 * upgrade the siblings to singleton events by adding them
Peter Zijlstra8a495422010-05-27 15:47:49 +02001013 * to whatever list we are on.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001014 */
1015 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
Peter Zijlstra8a495422010-05-27 15:47:49 +02001016 if (list)
1017 list_move_tail(&sibling->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001018 sibling->group_leader = sibling;
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01001019
1020 /* Inherit group flags from the previous leader */
1021 sibling->group_flags = event->group_flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001022 }
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001023
1024out:
1025 perf_event__header_size(event->group_leader);
1026
1027 list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry)
1028 perf_event__header_size(tmp);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001029}
1030
Stephane Eranianfa66f072010-08-26 16:40:01 +02001031static inline int
1032event_filter_match(struct perf_event *event)
1033{
Stephane Eraniane5d13672011-02-14 11:20:01 +02001034 return (event->cpu == -1 || event->cpu == smp_processor_id())
1035 && perf_cgroup_match(event);
Stephane Eranianfa66f072010-08-26 16:40:01 +02001036}
1037
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001038static void
1039event_sched_out(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001040 struct perf_cpu_context *cpuctx,
1041 struct perf_event_context *ctx)
1042{
Stephane Eranian41587552011-01-03 18:20:01 +02001043 u64 tstamp = perf_event_time(event);
Stephane Eranianfa66f072010-08-26 16:40:01 +02001044 u64 delta;
1045 /*
1046 * An event which could not be activated because of
1047 * filter mismatch still needs to have its timings
1048 * maintained, otherwise bogus information is return
1049 * via read() for time_enabled, time_running:
1050 */
1051 if (event->state == PERF_EVENT_STATE_INACTIVE
1052 && !event_filter_match(event)) {
Stephane Eraniane5d13672011-02-14 11:20:01 +02001053 delta = tstamp - event->tstamp_stopped;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001054 event->tstamp_running += delta;
Stephane Eranian41587552011-01-03 18:20:01 +02001055 event->tstamp_stopped = tstamp;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001056 }
1057
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001058 if (event->state != PERF_EVENT_STATE_ACTIVE)
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001059 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001060
1061 event->state = PERF_EVENT_STATE_INACTIVE;
1062 if (event->pending_disable) {
1063 event->pending_disable = 0;
1064 event->state = PERF_EVENT_STATE_OFF;
1065 }
Stephane Eranian41587552011-01-03 18:20:01 +02001066 event->tstamp_stopped = tstamp;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001067 event->pmu->del(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001068 event->oncpu = -1;
1069
1070 if (!is_software_event(event))
1071 cpuctx->active_oncpu--;
1072 ctx->nr_active--;
1073 if (event->attr.exclusive || !cpuctx->active_oncpu)
1074 cpuctx->exclusive = 0;
1075}
1076
1077static void
1078group_sched_out(struct perf_event *group_event,
1079 struct perf_cpu_context *cpuctx,
1080 struct perf_event_context *ctx)
1081{
1082 struct perf_event *event;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001083 int state = group_event->state;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001084
1085 event_sched_out(group_event, cpuctx, ctx);
1086
1087 /*
1088 * Schedule out siblings (if any):
1089 */
1090 list_for_each_entry(event, &group_event->sibling_list, group_entry)
1091 event_sched_out(event, cpuctx, ctx);
1092
Stephane Eranianfa66f072010-08-26 16:40:01 +02001093 if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001094 cpuctx->exclusive = 0;
1095}
1096
1097/*
1098 * Cross CPU call to remove a performance event
1099 *
1100 * We disable the event on the hardware level first. After that we
1101 * remove it from the context list.
1102 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001103static int __perf_remove_from_context(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001104{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001105 struct perf_event *event = info;
1106 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001107 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001108
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001109 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001110 event_sched_out(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001111 list_del_event(event, ctx);
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001112 raw_spin_unlock(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001113
1114 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001115}
1116
1117
1118/*
1119 * Remove the event from a task's (or a CPU's) list of events.
1120 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001121 * CPU events are removed with a smp call. For task events we only
1122 * call when the task is on a CPU.
1123 *
1124 * If event->ctx is a cloned context, callers must make sure that
1125 * every task struct that event->ctx->task could possibly point to
1126 * remains valid. This is OK when called from perf_release since
1127 * that only calls us on the top-level context, which can't be a clone.
1128 * When called from perf_event_exit_task, it's OK because the
1129 * context has been detached from its task.
1130 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001131static void perf_remove_from_context(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001132{
1133 struct perf_event_context *ctx = event->ctx;
1134 struct task_struct *task = ctx->task;
1135
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001136 lockdep_assert_held(&ctx->mutex);
1137
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001138 if (!task) {
1139 /*
1140 * Per cpu events are removed via an smp call and
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001141 * the removal is always successful.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001142 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001143 cpu_function_call(event->cpu, __perf_remove_from_context, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001144 return;
1145 }
1146
1147retry:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001148 if (!task_function_call(task, __perf_remove_from_context, event))
1149 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001150
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001151 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001152 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001153 * If we failed to find a running task, but find the context active now
1154 * that we've acquired the ctx->lock, retry.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001155 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001156 if (ctx->is_active) {
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001157 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001158 goto retry;
1159 }
1160
1161 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001162 * Since the task isn't running, its safe to remove the event, us
1163 * holding the ctx->lock ensures the task won't get scheduled in.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001164 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001165 list_del_event(event, ctx);
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001166 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001167}
1168
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001169/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001170 * Cross CPU call to disable a performance event
1171 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001172static int __perf_event_disable(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001173{
1174 struct perf_event *event = info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001175 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001176 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001177
1178 /*
1179 * If this is a per-task event, need to check whether this
1180 * event's task is the current task on this cpu.
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001181 *
1182 * Can trigger due to concurrent perf_event_context_sched_out()
1183 * flipping contexts around.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001184 */
1185 if (ctx->task && cpuctx->task_ctx != ctx)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001186 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001187
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001188 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001189
1190 /*
1191 * If the event is on, turn it off.
1192 * If it is in error state, leave it in error state.
1193 */
1194 if (event->state >= PERF_EVENT_STATE_INACTIVE) {
1195 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001196 update_cgrp_time_from_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001197 update_group_times(event);
1198 if (event == event->group_leader)
1199 group_sched_out(event, cpuctx, ctx);
1200 else
1201 event_sched_out(event, cpuctx, ctx);
1202 event->state = PERF_EVENT_STATE_OFF;
1203 }
1204
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001205 raw_spin_unlock(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001206
1207 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001208}
1209
1210/*
1211 * Disable a event.
1212 *
1213 * If event->ctx is a cloned context, callers must make sure that
1214 * every task struct that event->ctx->task could possibly point to
1215 * remains valid. This condition is satisifed when called through
1216 * perf_event_for_each_child or perf_event_for_each because they
1217 * hold the top-level event's child_mutex, so any descendant that
1218 * goes to exit will block in sync_child_event.
1219 * When called from perf_pending_event it's OK because event->ctx
1220 * is the current context on this CPU and preemption is disabled,
1221 * hence we can't get into perf_event_task_sched_out for this context.
1222 */
Frederic Weisbecker44234ad2009-12-09 09:25:48 +01001223void perf_event_disable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001224{
1225 struct perf_event_context *ctx = event->ctx;
1226 struct task_struct *task = ctx->task;
1227
1228 if (!task) {
1229 /*
1230 * Disable the event on the cpu that it's on
1231 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001232 cpu_function_call(event->cpu, __perf_event_disable, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001233 return;
1234 }
1235
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001236retry:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001237 if (!task_function_call(task, __perf_event_disable, event))
1238 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001239
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001240 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001241 /*
1242 * If the event is still active, we need to retry the cross-call.
1243 */
1244 if (event->state == PERF_EVENT_STATE_ACTIVE) {
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001245 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001246 /*
1247 * Reload the task pointer, it might have been changed by
1248 * a concurrent perf_event_context_sched_out().
1249 */
1250 task = ctx->task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001251 goto retry;
1252 }
1253
1254 /*
1255 * Since we have the lock this context can't be scheduled
1256 * in, so we can change the state safely.
1257 */
1258 if (event->state == PERF_EVENT_STATE_INACTIVE) {
1259 update_group_times(event);
1260 event->state = PERF_EVENT_STATE_OFF;
1261 }
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001262 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001263}
1264
Stephane Eraniane5d13672011-02-14 11:20:01 +02001265static void perf_set_shadow_time(struct perf_event *event,
1266 struct perf_event_context *ctx,
1267 u64 tstamp)
1268{
1269 /*
1270 * use the correct time source for the time snapshot
1271 *
1272 * We could get by without this by leveraging the
1273 * fact that to get to this function, the caller
1274 * has most likely already called update_context_time()
1275 * and update_cgrp_time_xx() and thus both timestamp
1276 * are identical (or very close). Given that tstamp is,
1277 * already adjusted for cgroup, we could say that:
1278 * tstamp - ctx->timestamp
1279 * is equivalent to
1280 * tstamp - cgrp->timestamp.
1281 *
1282 * Then, in perf_output_read(), the calculation would
1283 * work with no changes because:
1284 * - event is guaranteed scheduled in
1285 * - no scheduled out in between
1286 * - thus the timestamp would be the same
1287 *
1288 * But this is a bit hairy.
1289 *
1290 * So instead, we have an explicit cgroup call to remain
1291 * within the time time source all along. We believe it
1292 * is cleaner and simpler to understand.
1293 */
1294 if (is_cgroup_event(event))
1295 perf_cgroup_set_shadow_time(event, tstamp);
1296 else
1297 event->shadow_ctx_time = tstamp - ctx->timestamp;
1298}
1299
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01001300#define MAX_INTERRUPTS (~0ULL)
1301
1302static void perf_log_throttle(struct perf_event *event, int enable);
1303
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001304static int
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001305event_sched_in(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001306 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001307 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001308{
Stephane Eranian41587552011-01-03 18:20:01 +02001309 u64 tstamp = perf_event_time(event);
1310
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001311 if (event->state <= PERF_EVENT_STATE_OFF)
1312 return 0;
1313
1314 event->state = PERF_EVENT_STATE_ACTIVE;
Peter Zijlstra6e377382010-02-11 13:21:58 +01001315 event->oncpu = smp_processor_id();
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01001316
1317 /*
1318 * Unthrottle events, since we scheduled we might have missed several
1319 * ticks already, also for a heavily scheduling task there is little
1320 * guarantee it'll get a tick in a timely manner.
1321 */
1322 if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) {
1323 perf_log_throttle(event, 1);
1324 event->hw.interrupts = 0;
1325 }
1326
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001327 /*
1328 * The new state must be visible before we turn it on in the hardware:
1329 */
1330 smp_wmb();
1331
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001332 if (event->pmu->add(event, PERF_EF_START)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001333 event->state = PERF_EVENT_STATE_INACTIVE;
1334 event->oncpu = -1;
1335 return -EAGAIN;
1336 }
1337
Stephane Eranian41587552011-01-03 18:20:01 +02001338 event->tstamp_running += tstamp - event->tstamp_stopped;
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001339
Stephane Eraniane5d13672011-02-14 11:20:01 +02001340 perf_set_shadow_time(event, ctx, tstamp);
Stephane Eranianeed01522010-10-26 16:08:01 +02001341
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001342 if (!is_software_event(event))
1343 cpuctx->active_oncpu++;
1344 ctx->nr_active++;
1345
1346 if (event->attr.exclusive)
1347 cpuctx->exclusive = 1;
1348
1349 return 0;
1350}
1351
1352static int
1353group_sched_in(struct perf_event *group_event,
1354 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001355 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001356{
Lin Ming6bde9b62010-04-23 13:56:00 +08001357 struct perf_event *event, *partial_group = NULL;
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02001358 struct pmu *pmu = group_event->pmu;
Stephane Eraniand7842da2010-10-20 15:25:01 +02001359 u64 now = ctx->time;
1360 bool simulate = false;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001361
1362 if (group_event->state == PERF_EVENT_STATE_OFF)
1363 return 0;
1364
Peter Zijlstraad5133b2010-06-15 12:22:39 +02001365 pmu->start_txn(pmu);
Lin Ming6bde9b62010-04-23 13:56:00 +08001366
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001367 if (event_sched_in(group_event, cpuctx, ctx)) {
Peter Zijlstraad5133b2010-06-15 12:22:39 +02001368 pmu->cancel_txn(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001369 return -EAGAIN;
Stephane Eranian90151c32010-05-25 16:23:10 +02001370 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001371
1372 /*
1373 * Schedule in siblings as one group (if any):
1374 */
1375 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001376 if (event_sched_in(event, cpuctx, ctx)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001377 partial_group = event;
1378 goto group_error;
1379 }
1380 }
1381
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001382 if (!pmu->commit_txn(pmu))
Paul Mackerras6e851582010-05-08 20:58:00 +10001383 return 0;
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001384
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001385group_error:
1386 /*
1387 * Groups can be scheduled in as one unit only, so undo any
1388 * partial group before returning:
Stephane Eraniand7842da2010-10-20 15:25:01 +02001389 * The events up to the failed event are scheduled out normally,
1390 * tstamp_stopped will be updated.
1391 *
1392 * The failed events and the remaining siblings need to have
1393 * their timings updated as if they had gone thru event_sched_in()
1394 * and event_sched_out(). This is required to get consistent timings
1395 * across the group. This also takes care of the case where the group
1396 * could never be scheduled by ensuring tstamp_stopped is set to mark
1397 * the time the event was actually stopped, such that time delta
1398 * calculation in update_event_times() is correct.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001399 */
1400 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
1401 if (event == partial_group)
Stephane Eraniand7842da2010-10-20 15:25:01 +02001402 simulate = true;
1403
1404 if (simulate) {
1405 event->tstamp_running += now - event->tstamp_stopped;
1406 event->tstamp_stopped = now;
1407 } else {
1408 event_sched_out(event, cpuctx, ctx);
1409 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001410 }
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001411 event_sched_out(group_event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001412
Peter Zijlstraad5133b2010-06-15 12:22:39 +02001413 pmu->cancel_txn(pmu);
Stephane Eranian90151c32010-05-25 16:23:10 +02001414
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001415 return -EAGAIN;
1416}
1417
1418/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001419 * Work out whether we can put this event group on the CPU now.
1420 */
1421static int group_can_go_on(struct perf_event *event,
1422 struct perf_cpu_context *cpuctx,
1423 int can_add_hw)
1424{
1425 /*
1426 * Groups consisting entirely of software events can always go on.
1427 */
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01001428 if (event->group_flags & PERF_GROUP_SOFTWARE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001429 return 1;
1430 /*
1431 * If an exclusive group is already on, no other hardware
1432 * events can go on.
1433 */
1434 if (cpuctx->exclusive)
1435 return 0;
1436 /*
1437 * If this group is exclusive and there are already
1438 * events on the CPU, it can't go on.
1439 */
1440 if (event->attr.exclusive && cpuctx->active_oncpu)
1441 return 0;
1442 /*
1443 * Otherwise, try to add it if all previous groups were able
1444 * to go on.
1445 */
1446 return can_add_hw;
1447}
1448
1449static void add_event_to_ctx(struct perf_event *event,
1450 struct perf_event_context *ctx)
1451{
Stephane Eranian41587552011-01-03 18:20:01 +02001452 u64 tstamp = perf_event_time(event);
1453
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001454 list_add_event(event, ctx);
Peter Zijlstra8a495422010-05-27 15:47:49 +02001455 perf_group_attach(event);
Stephane Eranian41587552011-01-03 18:20:01 +02001456 event->tstamp_enabled = tstamp;
1457 event->tstamp_running = tstamp;
1458 event->tstamp_stopped = tstamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001459}
1460
Stephane Eraniane5d13672011-02-14 11:20:01 +02001461static void perf_event_context_sched_in(struct perf_event_context *ctx,
1462 struct task_struct *tsk);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001463
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001464/*
1465 * Cross CPU call to install and enable a performance event
1466 *
1467 * Must be called with ctx->mutex held
1468 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001469static int __perf_install_in_context(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001470{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001471 struct perf_event *event = info;
1472 struct perf_event_context *ctx = event->ctx;
1473 struct perf_event *leader = event->group_leader;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001474 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001475 int err;
1476
1477 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001478 * In case we're installing a new context to an already running task,
1479 * could also happen before perf_event_task_sched_in() on architectures
1480 * which do context switches with IRQs enabled.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001481 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001482 if (ctx->task && !cpuctx->task_ctx)
Stephane Eraniane5d13672011-02-14 11:20:01 +02001483 perf_event_context_sched_in(ctx, ctx->task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001484
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001485 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001486 ctx->is_active = 1;
1487 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001488 /*
1489 * update cgrp time only if current cgrp
1490 * matches event->cgrp. Must be done before
1491 * calling add_event_to_ctx()
1492 */
1493 update_cgrp_time_from_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001494
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001495 add_event_to_ctx(event, ctx);
1496
Stephane Eranian5632ab12011-01-03 18:20:01 +02001497 if (!event_filter_match(event))
Peter Zijlstraf4c41762009-12-16 17:55:54 +01001498 goto unlock;
1499
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001500 /*
1501 * Don't put the event on if it is disabled or if
1502 * it is in a group and the group isn't on.
1503 */
1504 if (event->state != PERF_EVENT_STATE_INACTIVE ||
1505 (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE))
1506 goto unlock;
1507
1508 /*
1509 * An exclusive event can't go on if there are already active
1510 * hardware events, and no hardware event can go on if there
1511 * is already an exclusive event on.
1512 */
1513 if (!group_can_go_on(event, cpuctx, 1))
1514 err = -EEXIST;
1515 else
Peter Zijlstra6e377382010-02-11 13:21:58 +01001516 err = event_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001517
1518 if (err) {
1519 /*
1520 * This event couldn't go on. If it is in a group
1521 * then we have to pull the whole group off.
1522 * If the event group is pinned then put it in error state.
1523 */
1524 if (leader != event)
1525 group_sched_out(leader, cpuctx, ctx);
1526 if (leader->attr.pinned) {
1527 update_group_times(leader);
1528 leader->state = PERF_EVENT_STATE_ERROR;
1529 }
1530 }
1531
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001532unlock:
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001533 raw_spin_unlock(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001534
1535 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001536}
1537
1538/*
1539 * Attach a performance event to a context
1540 *
1541 * First we add the event to the list with the hardware enable bit
1542 * in event->hw_config cleared.
1543 *
1544 * If the event is attached to a task which is on a CPU we use a smp
1545 * call to enable it in the task context. The task might have been
1546 * scheduled away, but we check this in the smp call again.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001547 */
1548static void
1549perf_install_in_context(struct perf_event_context *ctx,
1550 struct perf_event *event,
1551 int cpu)
1552{
1553 struct task_struct *task = ctx->task;
1554
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001555 lockdep_assert_held(&ctx->mutex);
1556
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02001557 event->ctx = ctx;
1558
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001559 if (!task) {
1560 /*
1561 * Per cpu events are installed via an smp call and
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001562 * the install is always successful.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001563 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001564 cpu_function_call(cpu, __perf_install_in_context, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001565 return;
1566 }
1567
1568retry:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001569 if (!task_function_call(task, __perf_install_in_context, event))
1570 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001571
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001572 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001573 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001574 * If we failed to find a running task, but find the context active now
1575 * that we've acquired the ctx->lock, retry.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001576 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001577 if (ctx->is_active) {
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001578 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001579 goto retry;
1580 }
1581
1582 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001583 * Since the task isn't running, its safe to add the event, us holding
1584 * the ctx->lock ensures the task won't get scheduled in.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001585 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001586 add_event_to_ctx(event, ctx);
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001587 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001588}
1589
1590/*
1591 * Put a event into inactive state and update time fields.
1592 * Enabling the leader of a group effectively enables all
1593 * the group members that aren't explicitly disabled, so we
1594 * have to update their ->tstamp_enabled also.
1595 * Note: this works for group members as well as group leaders
1596 * since the non-leader members' sibling_lists will be empty.
1597 */
1598static void __perf_event_mark_enabled(struct perf_event *event,
1599 struct perf_event_context *ctx)
1600{
1601 struct perf_event *sub;
Stephane Eranian41587552011-01-03 18:20:01 +02001602 u64 tstamp = perf_event_time(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001603
1604 event->state = PERF_EVENT_STATE_INACTIVE;
Stephane Eranian41587552011-01-03 18:20:01 +02001605 event->tstamp_enabled = tstamp - event->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001606 list_for_each_entry(sub, &event->sibling_list, group_entry) {
Stephane Eranian41587552011-01-03 18:20:01 +02001607 if (sub->state >= PERF_EVENT_STATE_INACTIVE)
1608 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001609 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001610}
1611
1612/*
1613 * Cross CPU call to enable a performance event
1614 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001615static int __perf_event_enable(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001616{
1617 struct perf_event *event = info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001618 struct perf_event_context *ctx = event->ctx;
1619 struct perf_event *leader = event->group_leader;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001620 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001621 int err;
1622
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001623 if (WARN_ON_ONCE(!ctx->is_active))
1624 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001625
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001626 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001627 update_context_time(ctx);
1628
1629 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1630 goto unlock;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001631
1632 /*
1633 * set current task's cgroup time reference point
1634 */
Stephane Eranian3f7cce32011-02-18 14:40:01 +02001635 perf_cgroup_set_timestamp(current, ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001636
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001637 __perf_event_mark_enabled(event, ctx);
1638
Stephane Eraniane5d13672011-02-14 11:20:01 +02001639 if (!event_filter_match(event)) {
1640 if (is_cgroup_event(event))
1641 perf_cgroup_defer_enabled(event);
Peter Zijlstraf4c41762009-12-16 17:55:54 +01001642 goto unlock;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001643 }
Peter Zijlstraf4c41762009-12-16 17:55:54 +01001644
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001645 /*
1646 * If the event is in a group and isn't the group leader,
1647 * then don't put it on unless the group is on.
1648 */
1649 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
1650 goto unlock;
1651
1652 if (!group_can_go_on(event, cpuctx, 1)) {
1653 err = -EEXIST;
1654 } else {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001655 if (event == leader)
Peter Zijlstra6e377382010-02-11 13:21:58 +01001656 err = group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001657 else
Peter Zijlstra6e377382010-02-11 13:21:58 +01001658 err = event_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001659 }
1660
1661 if (err) {
1662 /*
1663 * If this event can't go on and it's part of a
1664 * group, then the whole group has to come off.
1665 */
1666 if (leader != event)
1667 group_sched_out(leader, cpuctx, ctx);
1668 if (leader->attr.pinned) {
1669 update_group_times(leader);
1670 leader->state = PERF_EVENT_STATE_ERROR;
1671 }
1672 }
1673
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001674unlock:
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001675 raw_spin_unlock(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001676
1677 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001678}
1679
1680/*
1681 * Enable a event.
1682 *
1683 * If event->ctx is a cloned context, callers must make sure that
1684 * every task struct that event->ctx->task could possibly point to
1685 * remains valid. This condition is satisfied when called through
1686 * perf_event_for_each_child or perf_event_for_each as described
1687 * for perf_event_disable.
1688 */
Frederic Weisbecker44234ad2009-12-09 09:25:48 +01001689void perf_event_enable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001690{
1691 struct perf_event_context *ctx = event->ctx;
1692 struct task_struct *task = ctx->task;
1693
1694 if (!task) {
1695 /*
1696 * Enable the event on the cpu that it's on
1697 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001698 cpu_function_call(event->cpu, __perf_event_enable, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001699 return;
1700 }
1701
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001702 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001703 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1704 goto out;
1705
1706 /*
1707 * If the event is in error state, clear that first.
1708 * That way, if we see the event in error state below, we
1709 * know that it has gone back into error state, as distinct
1710 * from the task having been scheduled away before the
1711 * cross-call arrived.
1712 */
1713 if (event->state == PERF_EVENT_STATE_ERROR)
1714 event->state = PERF_EVENT_STATE_OFF;
1715
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001716retry:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001717 if (!ctx->is_active) {
1718 __perf_event_mark_enabled(event, ctx);
1719 goto out;
1720 }
1721
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001722 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001723
1724 if (!task_function_call(task, __perf_event_enable, event))
1725 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001726
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001727 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001728
1729 /*
1730 * If the context is active and the event is still off,
1731 * we need to retry the cross-call.
1732 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001733 if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF) {
1734 /*
1735 * task could have been flipped by a concurrent
1736 * perf_event_context_sched_out()
1737 */
1738 task = ctx->task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001739 goto retry;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001740 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001741
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001742out:
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001743 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001744}
1745
1746static int perf_event_refresh(struct perf_event *event, int refresh)
1747{
1748 /*
1749 * not supported on inherited events
1750 */
Franck Bui-Huu2e939d12010-11-23 16:21:44 +01001751 if (event->attr.inherit || !is_sampling_event(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001752 return -EINVAL;
1753
1754 atomic_add(refresh, &event->event_limit);
1755 perf_event_enable(event);
1756
1757 return 0;
1758}
1759
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001760static void ctx_sched_out(struct perf_event_context *ctx,
1761 struct perf_cpu_context *cpuctx,
1762 enum event_type_t event_type)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001763{
1764 struct perf_event *event;
1765
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001766 raw_spin_lock(&ctx->lock);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02001767 perf_pmu_disable(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001768 ctx->is_active = 0;
1769 if (likely(!ctx->nr_events))
1770 goto out;
1771 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001772 update_cgrp_time_from_cpuctx(cpuctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001773
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001774 if (!ctx->nr_active)
Peter Zijlstra24cd7f52010-06-11 17:32:03 +02001775 goto out;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001776
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001777 if (event_type & EVENT_PINNED) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001778 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
1779 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001780 }
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001781
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001782 if (event_type & EVENT_FLEXIBLE) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001783 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08001784 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001785 }
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001786out:
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02001787 perf_pmu_enable(ctx->pmu);
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001788 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001789}
1790
1791/*
1792 * Test whether two contexts are equivalent, i.e. whether they
1793 * have both been cloned from the same version of the same context
1794 * and they both have the same number of enabled events.
1795 * If the number of enabled events is the same, then the set
1796 * of enabled events should be the same, because these are both
1797 * inherited contexts, therefore we can't access individual events
1798 * in them directly with an fd; we can only enable/disable all
1799 * events via prctl, or enable/disable all events in a family
1800 * via ioctl, which will have the same effect on both contexts.
1801 */
1802static int context_equiv(struct perf_event_context *ctx1,
1803 struct perf_event_context *ctx2)
1804{
1805 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
1806 && ctx1->parent_gen == ctx2->parent_gen
1807 && !ctx1->pin_count && !ctx2->pin_count;
1808}
1809
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001810static void __perf_event_sync_stat(struct perf_event *event,
1811 struct perf_event *next_event)
1812{
1813 u64 value;
1814
1815 if (!event->attr.inherit_stat)
1816 return;
1817
1818 /*
1819 * Update the event value, we cannot use perf_event_read()
1820 * because we're in the middle of a context switch and have IRQs
1821 * disabled, which upsets smp_call_function_single(), however
1822 * we know the event must be on the current CPU, therefore we
1823 * don't need to use it.
1824 */
1825 switch (event->state) {
1826 case PERF_EVENT_STATE_ACTIVE:
Peter Zijlstra3dbebf12009-11-20 22:19:52 +01001827 event->pmu->read(event);
1828 /* fall-through */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001829
1830 case PERF_EVENT_STATE_INACTIVE:
1831 update_event_times(event);
1832 break;
1833
1834 default:
1835 break;
1836 }
1837
1838 /*
1839 * In order to keep per-task stats reliable we need to flip the event
1840 * values when we flip the contexts.
1841 */
Peter Zijlstrae7850592010-05-21 14:43:08 +02001842 value = local64_read(&next_event->count);
1843 value = local64_xchg(&event->count, value);
1844 local64_set(&next_event->count, value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001845
1846 swap(event->total_time_enabled, next_event->total_time_enabled);
1847 swap(event->total_time_running, next_event->total_time_running);
1848
1849 /*
1850 * Since we swizzled the values, update the user visible data too.
1851 */
1852 perf_event_update_userpage(event);
1853 perf_event_update_userpage(next_event);
1854}
1855
1856#define list_next_entry(pos, member) \
1857 list_entry(pos->member.next, typeof(*pos), member)
1858
1859static void perf_event_sync_stat(struct perf_event_context *ctx,
1860 struct perf_event_context *next_ctx)
1861{
1862 struct perf_event *event, *next_event;
1863
1864 if (!ctx->nr_stat)
1865 return;
1866
Peter Zijlstra02ffdbc2009-11-20 22:19:50 +01001867 update_context_time(ctx);
1868
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001869 event = list_first_entry(&ctx->event_list,
1870 struct perf_event, event_entry);
1871
1872 next_event = list_first_entry(&next_ctx->event_list,
1873 struct perf_event, event_entry);
1874
1875 while (&event->event_entry != &ctx->event_list &&
1876 &next_event->event_entry != &next_ctx->event_list) {
1877
1878 __perf_event_sync_stat(event, next_event);
1879
1880 event = list_next_entry(event, event_entry);
1881 next_event = list_next_entry(next_event, event_entry);
1882 }
1883}
1884
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001885static void perf_event_context_sched_out(struct task_struct *task, int ctxn,
1886 struct task_struct *next)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001887{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001888 struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001889 struct perf_event_context *next_ctx;
1890 struct perf_event_context *parent;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001891 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001892 int do_switch = 1;
1893
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001894 if (likely(!ctx))
1895 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001896
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001897 cpuctx = __get_cpu_context(ctx);
1898 if (!cpuctx->task_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001899 return;
1900
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001901 rcu_read_lock();
1902 parent = rcu_dereference(ctx->parent_ctx);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001903 next_ctx = next->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001904 if (parent && next_ctx &&
1905 rcu_dereference(next_ctx->parent_ctx) == parent) {
1906 /*
1907 * Looks like the two contexts are clones, so we might be
1908 * able to optimize the context switch. We lock both
1909 * contexts and check that they are clones under the
1910 * lock (including re-checking that neither has been
1911 * uncloned in the meantime). It doesn't matter which
1912 * order we take the locks because no other cpu could
1913 * be trying to lock both of these tasks.
1914 */
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001915 raw_spin_lock(&ctx->lock);
1916 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001917 if (context_equiv(ctx, next_ctx)) {
1918 /*
1919 * XXX do we need a memory barrier of sorts
1920 * wrt to rcu_dereference() of perf_event_ctxp
1921 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001922 task->perf_event_ctxp[ctxn] = next_ctx;
1923 next->perf_event_ctxp[ctxn] = ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001924 ctx->task = next;
1925 next_ctx->task = task;
1926 do_switch = 0;
1927
1928 perf_event_sync_stat(ctx, next_ctx);
1929 }
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001930 raw_spin_unlock(&next_ctx->lock);
1931 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001932 }
1933 rcu_read_unlock();
1934
1935 if (do_switch) {
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001936 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001937 cpuctx->task_ctx = NULL;
1938 }
1939}
1940
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001941#define for_each_task_context_nr(ctxn) \
1942 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
1943
1944/*
1945 * Called from scheduler to remove the events of the current task,
1946 * with interrupts disabled.
1947 *
1948 * We stop each event and update the event value in event->count.
1949 *
1950 * This does not protect us against NMI, but disable()
1951 * sets the disabled bit in the control field of event _before_
1952 * accessing the event control register. If a NMI hits, then it will
1953 * not restart the event.
1954 */
Peter Zijlstra82cd6de2010-10-14 17:57:23 +02001955void __perf_event_task_sched_out(struct task_struct *task,
1956 struct task_struct *next)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001957{
1958 int ctxn;
1959
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001960 for_each_task_context_nr(ctxn)
1961 perf_event_context_sched_out(task, ctxn, next);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001962
1963 /*
1964 * if cgroup events exist on this CPU, then we need
1965 * to check if we have to switch out PMU state.
1966 * cgroup event are system-wide mode only
1967 */
1968 if (atomic_read(&__get_cpu_var(perf_cgroup_events)))
1969 perf_cgroup_sched_out(task);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001970}
1971
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001972static void task_ctx_sched_out(struct perf_event_context *ctx,
1973 enum event_type_t event_type)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001974{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001975 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001976
1977 if (!cpuctx->task_ctx)
1978 return;
1979
1980 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
1981 return;
1982
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001983 ctx_sched_out(ctx, cpuctx, event_type);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001984 cpuctx->task_ctx = NULL;
1985}
1986
1987/*
1988 * Called with IRQs disabled
1989 */
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001990static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
1991 enum event_type_t event_type)
1992{
1993 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001994}
1995
1996static void
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001997ctx_pinned_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001998 struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001999{
2000 struct perf_event *event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002001
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002002 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
2003 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002004 continue;
Stephane Eranian5632ab12011-01-03 18:20:01 +02002005 if (!event_filter_match(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002006 continue;
2007
Stephane Eraniane5d13672011-02-14 11:20:01 +02002008 /* may need to reset tstamp_enabled */
2009 if (is_cgroup_event(event))
2010 perf_cgroup_mark_enabled(event, ctx);
2011
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08002012 if (group_can_go_on(event, cpuctx, 1))
Peter Zijlstra6e377382010-02-11 13:21:58 +01002013 group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002014
2015 /*
2016 * If this pinned group hasn't been scheduled,
2017 * put it in error state.
2018 */
2019 if (event->state == PERF_EVENT_STATE_INACTIVE) {
2020 update_group_times(event);
2021 event->state = PERF_EVENT_STATE_ERROR;
2022 }
2023 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002024}
2025
2026static void
2027ctx_flexible_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01002028 struct perf_cpu_context *cpuctx)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002029{
2030 struct perf_event *event;
2031 int can_add_hw = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002032
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002033 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
2034 /* Ignore events in OFF or ERROR state */
2035 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002036 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002037 /*
2038 * Listen to the 'cpu' scheduling filter constraint
2039 * of events:
2040 */
Stephane Eranian5632ab12011-01-03 18:20:01 +02002041 if (!event_filter_match(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002042 continue;
2043
Stephane Eraniane5d13672011-02-14 11:20:01 +02002044 /* may need to reset tstamp_enabled */
2045 if (is_cgroup_event(event))
2046 perf_cgroup_mark_enabled(event, ctx);
2047
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002048 if (group_can_go_on(event, cpuctx, can_add_hw)) {
Peter Zijlstra6e377382010-02-11 13:21:58 +01002049 if (group_sched_in(event, cpuctx, ctx))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002050 can_add_hw = 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002051 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002052 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002053}
2054
2055static void
2056ctx_sched_in(struct perf_event_context *ctx,
2057 struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +02002058 enum event_type_t event_type,
2059 struct task_struct *task)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002060{
Stephane Eraniane5d13672011-02-14 11:20:01 +02002061 u64 now;
2062
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002063 raw_spin_lock(&ctx->lock);
2064 ctx->is_active = 1;
2065 if (likely(!ctx->nr_events))
2066 goto out;
2067
Stephane Eraniane5d13672011-02-14 11:20:01 +02002068 now = perf_clock();
2069 ctx->timestamp = now;
Stephane Eranian3f7cce32011-02-18 14:40:01 +02002070 perf_cgroup_set_timestamp(task, ctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002071 /*
2072 * First go through the list and put on any pinned groups
2073 * in order to give them the best chance of going on.
2074 */
2075 if (event_type & EVENT_PINNED)
Peter Zijlstra6e377382010-02-11 13:21:58 +01002076 ctx_pinned_sched_in(ctx, cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002077
2078 /* Then walk through the lower prio flexible groups */
2079 if (event_type & EVENT_FLEXIBLE)
Peter Zijlstra6e377382010-02-11 13:21:58 +01002080 ctx_flexible_sched_in(ctx, cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002081
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002082out:
Thomas Gleixnere625cce2009-11-17 18:02:06 +01002083 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002084}
2085
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002086static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +02002087 enum event_type_t event_type,
2088 struct task_struct *task)
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002089{
2090 struct perf_event_context *ctx = &cpuctx->ctx;
2091
Stephane Eraniane5d13672011-02-14 11:20:01 +02002092 ctx_sched_in(ctx, cpuctx, event_type, task);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002093}
2094
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002095static void task_ctx_sched_in(struct perf_event_context *ctx,
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002096 enum event_type_t event_type)
2097{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002098 struct perf_cpu_context *cpuctx;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002099
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002100 cpuctx = __get_cpu_context(ctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002101 if (cpuctx->task_ctx == ctx)
2102 return;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002103
Stephane Eraniane5d13672011-02-14 11:20:01 +02002104 ctx_sched_in(ctx, cpuctx, event_type, NULL);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002105 cpuctx->task_ctx = ctx;
2106}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002107
Stephane Eraniane5d13672011-02-14 11:20:01 +02002108static void perf_event_context_sched_in(struct perf_event_context *ctx,
2109 struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002110{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002111 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002112
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002113 cpuctx = __get_cpu_context(ctx);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002114 if (cpuctx->task_ctx == ctx)
2115 return;
2116
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02002117 perf_pmu_disable(ctx->pmu);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002118 /*
2119 * We want to keep the following priority order:
2120 * cpu pinned (that don't need to move), task pinned,
2121 * cpu flexible, task flexible.
2122 */
2123 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
2124
Stephane Eraniane5d13672011-02-14 11:20:01 +02002125 ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task);
2126 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task);
2127 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002128
2129 cpuctx->task_ctx = ctx;
eranian@google.com9b33fa62010-03-10 22:26:05 -08002130
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002131 /*
2132 * Since these rotations are per-cpu, we need to ensure the
2133 * cpu-context we got scheduled on is actually rotating.
2134 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002135 perf_pmu_rotate_start(ctx->pmu);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02002136 perf_pmu_enable(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002137}
2138
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002139/*
2140 * Called from scheduler to add the events of the current task
2141 * with interrupts disabled.
2142 *
2143 * We restore the event value and then enable it.
2144 *
2145 * This does not protect us against NMI, but enable()
2146 * sets the enabled bit in the control field of event _before_
2147 * accessing the event control register. If a NMI hits, then it will
2148 * keep the event running.
2149 */
Peter Zijlstra82cd6de2010-10-14 17:57:23 +02002150void __perf_event_task_sched_in(struct task_struct *task)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002151{
2152 struct perf_event_context *ctx;
2153 int ctxn;
2154
2155 for_each_task_context_nr(ctxn) {
2156 ctx = task->perf_event_ctxp[ctxn];
2157 if (likely(!ctx))
2158 continue;
2159
Stephane Eraniane5d13672011-02-14 11:20:01 +02002160 perf_event_context_sched_in(ctx, task);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002161 }
Stephane Eraniane5d13672011-02-14 11:20:01 +02002162 /*
2163 * if cgroup events exist on this CPU, then we need
2164 * to check if we have to switch in PMU state.
2165 * cgroup event are system-wide mode only
2166 */
2167 if (atomic_read(&__get_cpu_var(perf_cgroup_events)))
2168 perf_cgroup_sched_in(task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002169}
2170
Peter Zijlstraabd50712010-01-26 18:50:16 +01002171static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
2172{
2173 u64 frequency = event->attr.sample_freq;
2174 u64 sec = NSEC_PER_SEC;
2175 u64 divisor, dividend;
2176
2177 int count_fls, nsec_fls, frequency_fls, sec_fls;
2178
2179 count_fls = fls64(count);
2180 nsec_fls = fls64(nsec);
2181 frequency_fls = fls64(frequency);
2182 sec_fls = 30;
2183
2184 /*
2185 * We got @count in @nsec, with a target of sample_freq HZ
2186 * the target period becomes:
2187 *
2188 * @count * 10^9
2189 * period = -------------------
2190 * @nsec * sample_freq
2191 *
2192 */
2193
2194 /*
2195 * Reduce accuracy by one bit such that @a and @b converge
2196 * to a similar magnitude.
2197 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002198#define REDUCE_FLS(a, b) \
Peter Zijlstraabd50712010-01-26 18:50:16 +01002199do { \
2200 if (a##_fls > b##_fls) { \
2201 a >>= 1; \
2202 a##_fls--; \
2203 } else { \
2204 b >>= 1; \
2205 b##_fls--; \
2206 } \
2207} while (0)
2208
2209 /*
2210 * Reduce accuracy until either term fits in a u64, then proceed with
2211 * the other, so that finally we can do a u64/u64 division.
2212 */
2213 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
2214 REDUCE_FLS(nsec, frequency);
2215 REDUCE_FLS(sec, count);
2216 }
2217
2218 if (count_fls + sec_fls > 64) {
2219 divisor = nsec * frequency;
2220
2221 while (count_fls + sec_fls > 64) {
2222 REDUCE_FLS(count, sec);
2223 divisor >>= 1;
2224 }
2225
2226 dividend = count * sec;
2227 } else {
2228 dividend = count * sec;
2229
2230 while (nsec_fls + frequency_fls > 64) {
2231 REDUCE_FLS(nsec, frequency);
2232 dividend >>= 1;
2233 }
2234
2235 divisor = nsec * frequency;
2236 }
2237
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02002238 if (!divisor)
2239 return dividend;
2240
Peter Zijlstraabd50712010-01-26 18:50:16 +01002241 return div64_u64(dividend, divisor);
2242}
2243
2244static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002245{
2246 struct hw_perf_event *hwc = &event->hw;
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02002247 s64 period, sample_period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002248 s64 delta;
2249
Peter Zijlstraabd50712010-01-26 18:50:16 +01002250 period = perf_calculate_period(event, nsec, count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002251
2252 delta = (s64)(period - hwc->sample_period);
2253 delta = (delta + 7) / 8; /* low pass filter */
2254
2255 sample_period = hwc->sample_period + delta;
2256
2257 if (!sample_period)
2258 sample_period = 1;
2259
2260 hwc->sample_period = sample_period;
Peter Zijlstraabd50712010-01-26 18:50:16 +01002261
Peter Zijlstrae7850592010-05-21 14:43:08 +02002262 if (local64_read(&hwc->period_left) > 8*sample_period) {
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02002263 event->pmu->stop(event, PERF_EF_UPDATE);
Peter Zijlstrae7850592010-05-21 14:43:08 +02002264 local64_set(&hwc->period_left, 0);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02002265 event->pmu->start(event, PERF_EF_RELOAD);
Peter Zijlstraabd50712010-01-26 18:50:16 +01002266 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002267}
2268
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002269static void perf_ctx_adjust_freq(struct perf_event_context *ctx, u64 period)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002270{
2271 struct perf_event *event;
2272 struct hw_perf_event *hwc;
Peter Zijlstraabd50712010-01-26 18:50:16 +01002273 u64 interrupts, now;
2274 s64 delta;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002275
Thomas Gleixnere625cce2009-11-17 18:02:06 +01002276 raw_spin_lock(&ctx->lock);
Paul Mackerras03541f82009-10-14 16:58:03 +11002277 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002278 if (event->state != PERF_EVENT_STATE_ACTIVE)
2279 continue;
2280
Stephane Eranian5632ab12011-01-03 18:20:01 +02002281 if (!event_filter_match(event))
Peter Zijlstra5d27c232009-12-17 13:16:32 +01002282 continue;
2283
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002284 hwc = &event->hw;
2285
2286 interrupts = hwc->interrupts;
2287 hwc->interrupts = 0;
2288
2289 /*
2290 * unthrottle events on the tick
2291 */
2292 if (interrupts == MAX_INTERRUPTS) {
2293 perf_log_throttle(event, 1);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02002294 event->pmu->start(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002295 }
2296
2297 if (!event->attr.freq || !event->attr.sample_freq)
2298 continue;
2299
Peter Zijlstraabd50712010-01-26 18:50:16 +01002300 event->pmu->read(event);
Peter Zijlstrae7850592010-05-21 14:43:08 +02002301 now = local64_read(&event->count);
Peter Zijlstraabd50712010-01-26 18:50:16 +01002302 delta = now - hwc->freq_count_stamp;
2303 hwc->freq_count_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002304
Peter Zijlstraabd50712010-01-26 18:50:16 +01002305 if (delta > 0)
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002306 perf_adjust_period(event, period, delta);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002307 }
Thomas Gleixnere625cce2009-11-17 18:02:06 +01002308 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002309}
2310
2311/*
2312 * Round-robin a context's events:
2313 */
2314static void rotate_ctx(struct perf_event_context *ctx)
2315{
Thomas Gleixnere625cce2009-11-17 18:02:06 +01002316 raw_spin_lock(&ctx->lock);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002317
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01002318 /*
2319 * Rotate the first entry last of non-pinned groups. Rotation might be
2320 * disabled by the inheritance code.
2321 */
2322 if (!ctx->rotate_disable)
2323 list_rotate_left(&ctx->flexible_groups);
Frederic Weisbeckere2864172010-01-09 21:05:28 +01002324
Thomas Gleixnere625cce2009-11-17 18:02:06 +01002325 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002326}
2327
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002328/*
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002329 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
2330 * because they're strictly cpu affine and rotate_start is called with IRQs
2331 * disabled, while rotate_context is called from IRQ context.
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002332 */
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002333static void perf_rotate_context(struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002334{
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002335 u64 interval = (u64)cpuctx->jiffies_interval * TICK_NSEC;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002336 struct perf_event_context *ctx = NULL;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002337 int rotate = 0, remove = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002338
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002339 if (cpuctx->ctx.nr_events) {
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002340 remove = 0;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002341 if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
2342 rotate = 1;
2343 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002344
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002345 ctx = cpuctx->task_ctx;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002346 if (ctx && ctx->nr_events) {
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002347 remove = 0;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002348 if (ctx->nr_events != ctx->nr_active)
2349 rotate = 1;
2350 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002351
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02002352 perf_pmu_disable(cpuctx->ctx.pmu);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002353 perf_ctx_adjust_freq(&cpuctx->ctx, interval);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002354 if (ctx)
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002355 perf_ctx_adjust_freq(ctx, interval);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002356
Peter Zijlstrad4944a02010-03-08 13:51:20 +01002357 if (!rotate)
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002358 goto done;
Peter Zijlstrad4944a02010-03-08 13:51:20 +01002359
Frederic Weisbecker7defb0f2010-01-17 12:15:31 +01002360 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002361 if (ctx)
Frederic Weisbecker7defb0f2010-01-17 12:15:31 +01002362 task_ctx_sched_out(ctx, EVENT_FLEXIBLE);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002363
2364 rotate_ctx(&cpuctx->ctx);
2365 if (ctx)
2366 rotate_ctx(ctx);
2367
Stephane Eraniane5d13672011-02-14 11:20:01 +02002368 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, current);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002369 if (ctx)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002370 task_ctx_sched_in(ctx, EVENT_FLEXIBLE);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002371
2372done:
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002373 if (remove)
2374 list_del_init(&cpuctx->rotation_list);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002375
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002376 perf_pmu_enable(cpuctx->ctx.pmu);
2377}
2378
2379void perf_event_task_tick(void)
2380{
2381 struct list_head *head = &__get_cpu_var(rotation_list);
2382 struct perf_cpu_context *cpuctx, *tmp;
2383
2384 WARN_ON(!irqs_disabled());
2385
2386 list_for_each_entry_safe(cpuctx, tmp, head, rotation_list) {
2387 if (cpuctx->jiffies_interval == 1 ||
2388 !(jiffies % cpuctx->jiffies_interval))
2389 perf_rotate_context(cpuctx);
2390 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002391}
2392
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002393static int event_enable_on_exec(struct perf_event *event,
2394 struct perf_event_context *ctx)
2395{
2396 if (!event->attr.enable_on_exec)
2397 return 0;
2398
2399 event->attr.enable_on_exec = 0;
2400 if (event->state >= PERF_EVENT_STATE_INACTIVE)
2401 return 0;
2402
2403 __perf_event_mark_enabled(event, ctx);
2404
2405 return 1;
2406}
2407
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002408/*
2409 * Enable all of a task's events that have been marked enable-on-exec.
2410 * This expects task == current.
2411 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002412static void perf_event_enable_on_exec(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002413{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002414 struct perf_event *event;
2415 unsigned long flags;
2416 int enabled = 0;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002417 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002418
2419 local_irq_save(flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002420 if (!ctx || !ctx->nr_events)
2421 goto out;
2422
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002423 task_ctx_sched_out(ctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002424
Thomas Gleixnere625cce2009-11-17 18:02:06 +01002425 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002426
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002427 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
2428 ret = event_enable_on_exec(event, ctx);
2429 if (ret)
2430 enabled = 1;
2431 }
2432
2433 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
2434 ret = event_enable_on_exec(event, ctx);
2435 if (ret)
2436 enabled = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002437 }
2438
2439 /*
2440 * Unclone this context if we enabled any event.
2441 */
2442 if (enabled)
2443 unclone_ctx(ctx);
2444
Thomas Gleixnere625cce2009-11-17 18:02:06 +01002445 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002446
Stephane Eraniane5d13672011-02-14 11:20:01 +02002447 perf_event_context_sched_in(ctx, ctx->task);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002448out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002449 local_irq_restore(flags);
2450}
2451
2452/*
2453 * Cross CPU call to read the hardware event
2454 */
2455static void __perf_event_read(void *info)
2456{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002457 struct perf_event *event = info;
2458 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002459 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002460
2461 /*
2462 * If this is a task context, we need to check whether it is
2463 * the current task context of this cpu. If not it has been
2464 * scheduled out before the smp call arrived. In that case
2465 * event->count would have been updated to a recent sample
2466 * when the event was scheduled out.
2467 */
2468 if (ctx->task && cpuctx->task_ctx != ctx)
2469 return;
2470
Thomas Gleixnere625cce2009-11-17 18:02:06 +01002471 raw_spin_lock(&ctx->lock);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002472 if (ctx->is_active) {
Peter Zijlstra542e72f2011-01-26 15:38:35 +01002473 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002474 update_cgrp_time_from_event(event);
2475 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002476 update_event_times(event);
Peter Zijlstra542e72f2011-01-26 15:38:35 +01002477 if (event->state == PERF_EVENT_STATE_ACTIVE)
2478 event->pmu->read(event);
Thomas Gleixnere625cce2009-11-17 18:02:06 +01002479 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002480}
2481
Peter Zijlstrab5e58792010-05-21 14:43:12 +02002482static inline u64 perf_event_count(struct perf_event *event)
2483{
Peter Zijlstrae7850592010-05-21 14:43:08 +02002484 return local64_read(&event->count) + atomic64_read(&event->child_count);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02002485}
2486
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002487static u64 perf_event_read(struct perf_event *event)
2488{
2489 /*
2490 * If event is enabled and currently active on a CPU, update the
2491 * value in the event structure:
2492 */
2493 if (event->state == PERF_EVENT_STATE_ACTIVE) {
2494 smp_call_function_single(event->oncpu,
2495 __perf_event_read, event, 1);
2496 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01002497 struct perf_event_context *ctx = event->ctx;
2498 unsigned long flags;
2499
Thomas Gleixnere625cce2009-11-17 18:02:06 +01002500 raw_spin_lock_irqsave(&ctx->lock, flags);
Stephane Eranianc530ccd2010-10-15 15:26:01 +02002501 /*
2502 * may read while context is not active
2503 * (e.g., thread is blocked), in that case
2504 * we cannot update context time
2505 */
Stephane Eraniane5d13672011-02-14 11:20:01 +02002506 if (ctx->is_active) {
Stephane Eranianc530ccd2010-10-15 15:26:01 +02002507 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002508 update_cgrp_time_from_event(event);
2509 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002510 update_event_times(event);
Thomas Gleixnere625cce2009-11-17 18:02:06 +01002511 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002512 }
2513
Peter Zijlstrab5e58792010-05-21 14:43:12 +02002514 return perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002515}
2516
2517/*
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02002518 * Callchain support
2519 */
2520
2521struct callchain_cpus_entries {
2522 struct rcu_head rcu_head;
2523 struct perf_callchain_entry *cpu_entries[0];
2524};
2525
Frederic Weisbecker7ae07ea2010-08-14 20:45:13 +02002526static DEFINE_PER_CPU(int, callchain_recursion[PERF_NR_CONTEXTS]);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02002527static atomic_t nr_callchain_events;
2528static DEFINE_MUTEX(callchain_mutex);
2529struct callchain_cpus_entries *callchain_cpus_entries;
2530
2531
2532__weak void perf_callchain_kernel(struct perf_callchain_entry *entry,
2533 struct pt_regs *regs)
2534{
2535}
2536
2537__weak void perf_callchain_user(struct perf_callchain_entry *entry,
2538 struct pt_regs *regs)
2539{
2540}
2541
2542static void release_callchain_buffers_rcu(struct rcu_head *head)
2543{
2544 struct callchain_cpus_entries *entries;
2545 int cpu;
2546
2547 entries = container_of(head, struct callchain_cpus_entries, rcu_head);
2548
2549 for_each_possible_cpu(cpu)
2550 kfree(entries->cpu_entries[cpu]);
2551
2552 kfree(entries);
2553}
2554
2555static void release_callchain_buffers(void)
2556{
2557 struct callchain_cpus_entries *entries;
2558
2559 entries = callchain_cpus_entries;
2560 rcu_assign_pointer(callchain_cpus_entries, NULL);
2561 call_rcu(&entries->rcu_head, release_callchain_buffers_rcu);
2562}
2563
2564static int alloc_callchain_buffers(void)
2565{
2566 int cpu;
2567 int size;
2568 struct callchain_cpus_entries *entries;
2569
2570 /*
2571 * We can't use the percpu allocation API for data that can be
2572 * accessed from NMI. Use a temporary manual per cpu allocation
2573 * until that gets sorted out.
2574 */
Eric Dumazet88d4f0d2011-01-25 19:40:51 +01002575 size = offsetof(struct callchain_cpus_entries, cpu_entries[nr_cpu_ids]);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02002576
2577 entries = kzalloc(size, GFP_KERNEL);
2578 if (!entries)
2579 return -ENOMEM;
2580
Frederic Weisbecker7ae07ea2010-08-14 20:45:13 +02002581 size = sizeof(struct perf_callchain_entry) * PERF_NR_CONTEXTS;
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02002582
2583 for_each_possible_cpu(cpu) {
2584 entries->cpu_entries[cpu] = kmalloc_node(size, GFP_KERNEL,
2585 cpu_to_node(cpu));
2586 if (!entries->cpu_entries[cpu])
2587 goto fail;
2588 }
2589
2590 rcu_assign_pointer(callchain_cpus_entries, entries);
2591
2592 return 0;
2593
2594fail:
2595 for_each_possible_cpu(cpu)
2596 kfree(entries->cpu_entries[cpu]);
2597 kfree(entries);
2598
2599 return -ENOMEM;
2600}
2601
2602static int get_callchain_buffers(void)
2603{
2604 int err = 0;
2605 int count;
2606
2607 mutex_lock(&callchain_mutex);
2608
2609 count = atomic_inc_return(&nr_callchain_events);
2610 if (WARN_ON_ONCE(count < 1)) {
2611 err = -EINVAL;
2612 goto exit;
2613 }
2614
2615 if (count > 1) {
2616 /* If the allocation failed, give up */
2617 if (!callchain_cpus_entries)
2618 err = -ENOMEM;
2619 goto exit;
2620 }
2621
2622 err = alloc_callchain_buffers();
2623 if (err)
2624 release_callchain_buffers();
2625exit:
2626 mutex_unlock(&callchain_mutex);
2627
2628 return err;
2629}
2630
2631static void put_callchain_buffers(void)
2632{
2633 if (atomic_dec_and_mutex_lock(&nr_callchain_events, &callchain_mutex)) {
2634 release_callchain_buffers();
2635 mutex_unlock(&callchain_mutex);
2636 }
2637}
2638
2639static int get_recursion_context(int *recursion)
2640{
2641 int rctx;
2642
2643 if (in_nmi())
2644 rctx = 3;
2645 else if (in_irq())
2646 rctx = 2;
2647 else if (in_softirq())
2648 rctx = 1;
2649 else
2650 rctx = 0;
2651
2652 if (recursion[rctx])
2653 return -1;
2654
2655 recursion[rctx]++;
2656 barrier();
2657
2658 return rctx;
2659}
2660
2661static inline void put_recursion_context(int *recursion, int rctx)
2662{
2663 barrier();
2664 recursion[rctx]--;
2665}
2666
2667static struct perf_callchain_entry *get_callchain_entry(int *rctx)
2668{
2669 int cpu;
2670 struct callchain_cpus_entries *entries;
2671
2672 *rctx = get_recursion_context(__get_cpu_var(callchain_recursion));
2673 if (*rctx == -1)
2674 return NULL;
2675
2676 entries = rcu_dereference(callchain_cpus_entries);
2677 if (!entries)
2678 return NULL;
2679
2680 cpu = smp_processor_id();
2681
2682 return &entries->cpu_entries[cpu][*rctx];
2683}
2684
2685static void
2686put_callchain_entry(int rctx)
2687{
2688 put_recursion_context(__get_cpu_var(callchain_recursion), rctx);
2689}
2690
2691static struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
2692{
2693 int rctx;
2694 struct perf_callchain_entry *entry;
2695
2696
2697 entry = get_callchain_entry(&rctx);
2698 if (rctx == -1)
2699 return NULL;
2700
2701 if (!entry)
2702 goto exit_put;
2703
2704 entry->nr = 0;
2705
2706 if (!user_mode(regs)) {
2707 perf_callchain_store(entry, PERF_CONTEXT_KERNEL);
2708 perf_callchain_kernel(entry, regs);
2709 if (current->mm)
2710 regs = task_pt_regs(current);
2711 else
2712 regs = NULL;
2713 }
2714
2715 if (regs) {
2716 perf_callchain_store(entry, PERF_CONTEXT_USER);
2717 perf_callchain_user(entry, regs);
2718 }
2719
2720exit_put:
2721 put_callchain_entry(rctx);
2722
2723 return entry;
2724}
2725
2726/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002727 * Initialize the perf_event context in a task_struct:
2728 */
Peter Zijlstraeb184472010-09-07 15:55:13 +02002729static void __perf_event_init_context(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002730{
Thomas Gleixnere625cce2009-11-17 18:02:06 +01002731 raw_spin_lock_init(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002732 mutex_init(&ctx->mutex);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002733 INIT_LIST_HEAD(&ctx->pinned_groups);
2734 INIT_LIST_HEAD(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002735 INIT_LIST_HEAD(&ctx->event_list);
2736 atomic_set(&ctx->refcount, 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002737}
2738
Peter Zijlstraeb184472010-09-07 15:55:13 +02002739static struct perf_event_context *
2740alloc_perf_context(struct pmu *pmu, struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002741{
2742 struct perf_event_context *ctx;
Peter Zijlstraeb184472010-09-07 15:55:13 +02002743
2744 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
2745 if (!ctx)
2746 return NULL;
2747
2748 __perf_event_init_context(ctx);
2749 if (task) {
2750 ctx->task = task;
2751 get_task_struct(task);
2752 }
2753 ctx->pmu = pmu;
2754
2755 return ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002756}
2757
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07002758static struct task_struct *
2759find_lively_task_by_vpid(pid_t vpid)
2760{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002761 struct task_struct *task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002762 int err;
2763
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002764 rcu_read_lock();
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07002765 if (!vpid)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002766 task = current;
2767 else
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07002768 task = find_task_by_vpid(vpid);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002769 if (task)
2770 get_task_struct(task);
2771 rcu_read_unlock();
2772
2773 if (!task)
2774 return ERR_PTR(-ESRCH);
2775
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002776 /* Reuse ptrace permission checks for now. */
2777 err = -EACCES;
2778 if (!ptrace_may_access(task, PTRACE_MODE_READ))
2779 goto errout;
2780
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07002781 return task;
2782errout:
2783 put_task_struct(task);
2784 return ERR_PTR(err);
2785
2786}
2787
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002788/*
2789 * Returns a matching context with refcount and pincount.
2790 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002791static struct perf_event_context *
Matt Helsley38a81da2010-09-13 13:01:20 -07002792find_get_context(struct pmu *pmu, struct task_struct *task, int cpu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002793{
2794 struct perf_event_context *ctx;
2795 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002796 unsigned long flags;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002797 int ctxn, err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002798
Oleg Nesterov22a4ec72011-01-18 17:10:08 +01002799 if (!task) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002800 /* Must be root to operate on a CPU event: */
2801 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
2802 return ERR_PTR(-EACCES);
2803
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002804 /*
2805 * We could be clever and allow to attach a event to an
2806 * offline CPU and activate it when the CPU comes up, but
2807 * that's for later.
2808 */
2809 if (!cpu_online(cpu))
2810 return ERR_PTR(-ENODEV);
2811
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002812 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002813 ctx = &cpuctx->ctx;
2814 get_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002815 ++ctx->pin_count;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002816
2817 return ctx;
2818 }
2819
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002820 err = -EINVAL;
2821 ctxn = pmu->task_ctx_nr;
2822 if (ctxn < 0)
2823 goto errout;
2824
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002825retry:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002826 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002827 if (ctx) {
2828 unclone_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002829 ++ctx->pin_count;
Thomas Gleixnere625cce2009-11-17 18:02:06 +01002830 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002831 }
2832
2833 if (!ctx) {
Peter Zijlstraeb184472010-09-07 15:55:13 +02002834 ctx = alloc_perf_context(pmu, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002835 err = -ENOMEM;
2836 if (!ctx)
2837 goto errout;
Peter Zijlstraeb184472010-09-07 15:55:13 +02002838
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002839 get_ctx(ctx);
Peter Zijlstraeb184472010-09-07 15:55:13 +02002840
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01002841 err = 0;
2842 mutex_lock(&task->perf_event_mutex);
2843 /*
2844 * If it has already passed perf_event_exit_task().
2845 * we must see PF_EXITING, it takes this mutex too.
2846 */
2847 if (task->flags & PF_EXITING)
2848 err = -ESRCH;
2849 else if (task->perf_event_ctxp[ctxn])
2850 err = -EAGAIN;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002851 else {
2852 ++ctx->pin_count;
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01002853 rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002854 }
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01002855 mutex_unlock(&task->perf_event_mutex);
2856
2857 if (unlikely(err)) {
Peter Zijlstraeb184472010-09-07 15:55:13 +02002858 put_task_struct(task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002859 kfree(ctx);
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01002860
2861 if (err == -EAGAIN)
2862 goto retry;
2863 goto errout;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002864 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002865 }
2866
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002867 return ctx;
2868
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002869errout:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002870 return ERR_PTR(err);
2871}
2872
Li Zefan6fb29152009-10-15 11:21:42 +08002873static void perf_event_free_filter(struct perf_event *event);
2874
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002875static void free_event_rcu(struct rcu_head *head)
2876{
2877 struct perf_event *event;
2878
2879 event = container_of(head, struct perf_event, rcu_head);
2880 if (event->ns)
2881 put_pid_ns(event->ns);
Li Zefan6fb29152009-10-15 11:21:42 +08002882 perf_event_free_filter(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002883 kfree(event);
2884}
2885
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002886static void perf_buffer_put(struct perf_buffer *buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002887
2888static void free_event(struct perf_event *event)
2889{
Peter Zijlstrae360adb2010-10-14 14:01:34 +08002890 irq_work_sync(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002891
2892 if (!event->parent) {
Peter Zijlstra82cd6de2010-10-14 17:57:23 +02002893 if (event->attach_state & PERF_ATTACH_TASK)
Stephane Eraniane5d13672011-02-14 11:20:01 +02002894 jump_label_dec(&perf_sched_events);
Eric B Munson3af9e852010-05-18 15:30:49 +01002895 if (event->attr.mmap || event->attr.mmap_data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002896 atomic_dec(&nr_mmap_events);
2897 if (event->attr.comm)
2898 atomic_dec(&nr_comm_events);
2899 if (event->attr.task)
2900 atomic_dec(&nr_task_events);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02002901 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
2902 put_callchain_buffers();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002903 }
2904
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002905 if (event->buffer) {
2906 perf_buffer_put(event->buffer);
2907 event->buffer = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002908 }
2909
Stephane Eraniane5d13672011-02-14 11:20:01 +02002910 if (is_cgroup_event(event))
2911 perf_detach_cgroup(event);
2912
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002913 if (event->destroy)
2914 event->destroy(event);
2915
Peter Zijlstra0c67b402010-09-13 11:15:58 +02002916 if (event->ctx)
2917 put_ctx(event->ctx);
2918
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002919 call_rcu(&event->rcu_head, free_event_rcu);
2920}
2921
Arjan van de Venfb0459d2009-09-25 12:25:56 +02002922int perf_event_release_kernel(struct perf_event *event)
2923{
2924 struct perf_event_context *ctx = event->ctx;
2925
Peter Zijlstra050735b2010-05-11 11:51:53 +02002926 /*
2927 * Remove from the PMU, can't get re-enabled since we got
2928 * here because the last ref went.
2929 */
2930 perf_event_disable(event);
2931
Arjan van de Venfb0459d2009-09-25 12:25:56 +02002932 WARN_ON_ONCE(ctx->parent_ctx);
Peter Zijlstraa0507c82010-05-06 15:42:53 +02002933 /*
2934 * There are two ways this annotation is useful:
2935 *
2936 * 1) there is a lock recursion from perf_event_exit_task
2937 * see the comment there.
2938 *
2939 * 2) there is a lock-inversion with mmap_sem through
2940 * perf_event_read_group(), which takes faults while
2941 * holding ctx->mutex, however this is called after
2942 * the last filedesc died, so there is no possibility
2943 * to trigger the AB-BA case.
2944 */
2945 mutex_lock_nested(&ctx->mutex, SINGLE_DEPTH_NESTING);
Peter Zijlstra050735b2010-05-11 11:51:53 +02002946 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra8a495422010-05-27 15:47:49 +02002947 perf_group_detach(event);
Peter Zijlstra050735b2010-05-11 11:51:53 +02002948 list_del_event(event, ctx);
Peter Zijlstra050735b2010-05-11 11:51:53 +02002949 raw_spin_unlock_irq(&ctx->lock);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02002950 mutex_unlock(&ctx->mutex);
2951
Arjan van de Venfb0459d2009-09-25 12:25:56 +02002952 free_event(event);
2953
2954 return 0;
2955}
2956EXPORT_SYMBOL_GPL(perf_event_release_kernel);
2957
Peter Zijlstraa66a3052009-11-23 11:37:23 +01002958/*
2959 * Called when the last reference to the file is gone.
2960 */
2961static int perf_release(struct inode *inode, struct file *file)
2962{
2963 struct perf_event *event = file->private_data;
Peter Zijlstra88821352010-11-09 19:01:43 +01002964 struct task_struct *owner;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01002965
2966 file->private_data = NULL;
2967
Peter Zijlstra88821352010-11-09 19:01:43 +01002968 rcu_read_lock();
2969 owner = ACCESS_ONCE(event->owner);
2970 /*
2971 * Matches the smp_wmb() in perf_event_exit_task(). If we observe
2972 * !owner it means the list deletion is complete and we can indeed
2973 * free this event, otherwise we need to serialize on
2974 * owner->perf_event_mutex.
2975 */
2976 smp_read_barrier_depends();
2977 if (owner) {
2978 /*
2979 * Since delayed_put_task_struct() also drops the last
2980 * task reference we can safely take a new reference
2981 * while holding the rcu_read_lock().
2982 */
2983 get_task_struct(owner);
2984 }
2985 rcu_read_unlock();
2986
2987 if (owner) {
2988 mutex_lock(&owner->perf_event_mutex);
2989 /*
2990 * We have to re-check the event->owner field, if it is cleared
2991 * we raced with perf_event_exit_task(), acquiring the mutex
2992 * ensured they're done, and we can proceed with freeing the
2993 * event.
2994 */
2995 if (event->owner)
2996 list_del_init(&event->owner_entry);
2997 mutex_unlock(&owner->perf_event_mutex);
2998 put_task_struct(owner);
2999 }
3000
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003001 return perf_event_release_kernel(event);
3002}
3003
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003004u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003005{
3006 struct perf_event *child;
3007 u64 total = 0;
3008
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003009 *enabled = 0;
3010 *running = 0;
3011
Peter Zijlstra6f105812009-11-20 22:19:56 +01003012 mutex_lock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003013 total += perf_event_read(event);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003014 *enabled += event->total_time_enabled +
3015 atomic64_read(&event->child_total_time_enabled);
3016 *running += event->total_time_running +
3017 atomic64_read(&event->child_total_time_running);
3018
3019 list_for_each_entry(child, &event->child_list, child_list) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003020 total += perf_event_read(child);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003021 *enabled += child->total_time_enabled;
3022 *running += child->total_time_running;
3023 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01003024 mutex_unlock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003025
3026 return total;
3027}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003028EXPORT_SYMBOL_GPL(perf_event_read_value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003029
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003030static int perf_event_read_group(struct perf_event *event,
3031 u64 read_format, char __user *buf)
3032{
3033 struct perf_event *leader = event->group_leader, *sub;
Peter Zijlstra6f105812009-11-20 22:19:56 +01003034 int n = 0, size = 0, ret = -EFAULT;
3035 struct perf_event_context *ctx = leader->ctx;
Peter Zijlstraabf48682009-11-20 22:19:49 +01003036 u64 values[5];
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003037 u64 count, enabled, running;
Peter Zijlstraabf48682009-11-20 22:19:49 +01003038
Peter Zijlstra6f105812009-11-20 22:19:56 +01003039 mutex_lock(&ctx->mutex);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003040 count = perf_event_read_value(leader, &enabled, &running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003041
3042 values[n++] = 1 + leader->nr_siblings;
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003043 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3044 values[n++] = enabled;
3045 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3046 values[n++] = running;
Peter Zijlstraabf48682009-11-20 22:19:49 +01003047 values[n++] = count;
3048 if (read_format & PERF_FORMAT_ID)
3049 values[n++] = primary_event_id(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003050
3051 size = n * sizeof(u64);
3052
3053 if (copy_to_user(buf, values, size))
Peter Zijlstra6f105812009-11-20 22:19:56 +01003054 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003055
Peter Zijlstra6f105812009-11-20 22:19:56 +01003056 ret = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003057
3058 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
Peter Zijlstraabf48682009-11-20 22:19:49 +01003059 n = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003060
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003061 values[n++] = perf_event_read_value(sub, &enabled, &running);
Peter Zijlstraabf48682009-11-20 22:19:49 +01003062 if (read_format & PERF_FORMAT_ID)
3063 values[n++] = primary_event_id(sub);
3064
3065 size = n * sizeof(u64);
3066
Stephane Eranian184d3da2009-11-23 21:40:49 -08003067 if (copy_to_user(buf + ret, values, size)) {
Peter Zijlstra6f105812009-11-20 22:19:56 +01003068 ret = -EFAULT;
3069 goto unlock;
3070 }
Peter Zijlstraabf48682009-11-20 22:19:49 +01003071
3072 ret += size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003073 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01003074unlock:
3075 mutex_unlock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003076
Peter Zijlstraabf48682009-11-20 22:19:49 +01003077 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003078}
3079
3080static int perf_event_read_one(struct perf_event *event,
3081 u64 read_format, char __user *buf)
3082{
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003083 u64 enabled, running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003084 u64 values[4];
3085 int n = 0;
3086
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003087 values[n++] = perf_event_read_value(event, &enabled, &running);
3088 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3089 values[n++] = enabled;
3090 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3091 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003092 if (read_format & PERF_FORMAT_ID)
3093 values[n++] = primary_event_id(event);
3094
3095 if (copy_to_user(buf, values, n * sizeof(u64)))
3096 return -EFAULT;
3097
3098 return n * sizeof(u64);
3099}
3100
3101/*
3102 * Read the performance event - simple non blocking version for now
3103 */
3104static ssize_t
3105perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
3106{
3107 u64 read_format = event->attr.read_format;
3108 int ret;
3109
3110 /*
3111 * Return end-of-file for a read on a event that is in
3112 * error state (i.e. because it was pinned but it couldn't be
3113 * scheduled on to the CPU at some point).
3114 */
3115 if (event->state == PERF_EVENT_STATE_ERROR)
3116 return 0;
3117
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02003118 if (count < event->read_size)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003119 return -ENOSPC;
3120
3121 WARN_ON_ONCE(event->ctx->parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003122 if (read_format & PERF_FORMAT_GROUP)
3123 ret = perf_event_read_group(event, read_format, buf);
3124 else
3125 ret = perf_event_read_one(event, read_format, buf);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003126
3127 return ret;
3128}
3129
3130static ssize_t
3131perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
3132{
3133 struct perf_event *event = file->private_data;
3134
3135 return perf_read_hw(event, buf, count);
3136}
3137
3138static unsigned int perf_poll(struct file *file, poll_table *wait)
3139{
3140 struct perf_event *event = file->private_data;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003141 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003142 unsigned int events = POLL_HUP;
3143
3144 rcu_read_lock();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003145 buffer = rcu_dereference(event->buffer);
3146 if (buffer)
3147 events = atomic_xchg(&buffer->poll, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003148 rcu_read_unlock();
3149
3150 poll_wait(file, &event->waitq, wait);
3151
3152 return events;
3153}
3154
3155static void perf_event_reset(struct perf_event *event)
3156{
3157 (void)perf_event_read(event);
Peter Zijlstrae7850592010-05-21 14:43:08 +02003158 local64_set(&event->count, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003159 perf_event_update_userpage(event);
3160}
3161
3162/*
3163 * Holding the top-level event's child_mutex means that any
3164 * descendant process that has inherited this event will block
3165 * in sync_child_event if it goes to exit, thus satisfying the
3166 * task existence requirements of perf_event_enable/disable.
3167 */
3168static void perf_event_for_each_child(struct perf_event *event,
3169 void (*func)(struct perf_event *))
3170{
3171 struct perf_event *child;
3172
3173 WARN_ON_ONCE(event->ctx->parent_ctx);
3174 mutex_lock(&event->child_mutex);
3175 func(event);
3176 list_for_each_entry(child, &event->child_list, child_list)
3177 func(child);
3178 mutex_unlock(&event->child_mutex);
3179}
3180
3181static void perf_event_for_each(struct perf_event *event,
3182 void (*func)(struct perf_event *))
3183{
3184 struct perf_event_context *ctx = event->ctx;
3185 struct perf_event *sibling;
3186
3187 WARN_ON_ONCE(ctx->parent_ctx);
3188 mutex_lock(&ctx->mutex);
3189 event = event->group_leader;
3190
3191 perf_event_for_each_child(event, func);
3192 func(event);
3193 list_for_each_entry(sibling, &event->sibling_list, group_entry)
3194 perf_event_for_each_child(event, func);
3195 mutex_unlock(&ctx->mutex);
3196}
3197
3198static int perf_event_period(struct perf_event *event, u64 __user *arg)
3199{
3200 struct perf_event_context *ctx = event->ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003201 int ret = 0;
3202 u64 value;
3203
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01003204 if (!is_sampling_event(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003205 return -EINVAL;
3206
John Blackwoodad0cf342010-09-28 18:03:11 -04003207 if (copy_from_user(&value, arg, sizeof(value)))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003208 return -EFAULT;
3209
3210 if (!value)
3211 return -EINVAL;
3212
Thomas Gleixnere625cce2009-11-17 18:02:06 +01003213 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003214 if (event->attr.freq) {
3215 if (value > sysctl_perf_event_sample_rate) {
3216 ret = -EINVAL;
3217 goto unlock;
3218 }
3219
3220 event->attr.sample_freq = value;
3221 } else {
3222 event->attr.sample_period = value;
3223 event->hw.sample_period = value;
3224 }
3225unlock:
Thomas Gleixnere625cce2009-11-17 18:02:06 +01003226 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003227
3228 return ret;
3229}
3230
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003231static const struct file_operations perf_fops;
3232
3233static struct perf_event *perf_fget_light(int fd, int *fput_needed)
3234{
3235 struct file *file;
3236
3237 file = fget_light(fd, fput_needed);
3238 if (!file)
3239 return ERR_PTR(-EBADF);
3240
3241 if (file->f_op != &perf_fops) {
3242 fput_light(file, *fput_needed);
3243 *fput_needed = 0;
3244 return ERR_PTR(-EBADF);
3245 }
3246
3247 return file->private_data;
3248}
3249
3250static int perf_event_set_output(struct perf_event *event,
3251 struct perf_event *output_event);
Li Zefan6fb29152009-10-15 11:21:42 +08003252static int perf_event_set_filter(struct perf_event *event, void __user *arg);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003253
3254static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3255{
3256 struct perf_event *event = file->private_data;
3257 void (*func)(struct perf_event *);
3258 u32 flags = arg;
3259
3260 switch (cmd) {
3261 case PERF_EVENT_IOC_ENABLE:
3262 func = perf_event_enable;
3263 break;
3264 case PERF_EVENT_IOC_DISABLE:
3265 func = perf_event_disable;
3266 break;
3267 case PERF_EVENT_IOC_RESET:
3268 func = perf_event_reset;
3269 break;
3270
3271 case PERF_EVENT_IOC_REFRESH:
3272 return perf_event_refresh(event, arg);
3273
3274 case PERF_EVENT_IOC_PERIOD:
3275 return perf_event_period(event, (u64 __user *)arg);
3276
3277 case PERF_EVENT_IOC_SET_OUTPUT:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003278 {
3279 struct perf_event *output_event = NULL;
3280 int fput_needed = 0;
3281 int ret;
3282
3283 if (arg != -1) {
3284 output_event = perf_fget_light(arg, &fput_needed);
3285 if (IS_ERR(output_event))
3286 return PTR_ERR(output_event);
3287 }
3288
3289 ret = perf_event_set_output(event, output_event);
3290 if (output_event)
3291 fput_light(output_event->filp, fput_needed);
3292
3293 return ret;
3294 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003295
Li Zefan6fb29152009-10-15 11:21:42 +08003296 case PERF_EVENT_IOC_SET_FILTER:
3297 return perf_event_set_filter(event, (void __user *)arg);
3298
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003299 default:
3300 return -ENOTTY;
3301 }
3302
3303 if (flags & PERF_IOC_FLAG_GROUP)
3304 perf_event_for_each(event, func);
3305 else
3306 perf_event_for_each_child(event, func);
3307
3308 return 0;
3309}
3310
3311int perf_event_task_enable(void)
3312{
3313 struct perf_event *event;
3314
3315 mutex_lock(&current->perf_event_mutex);
3316 list_for_each_entry(event, &current->perf_event_list, owner_entry)
3317 perf_event_for_each_child(event, perf_event_enable);
3318 mutex_unlock(&current->perf_event_mutex);
3319
3320 return 0;
3321}
3322
3323int perf_event_task_disable(void)
3324{
3325 struct perf_event *event;
3326
3327 mutex_lock(&current->perf_event_mutex);
3328 list_for_each_entry(event, &current->perf_event_list, owner_entry)
3329 perf_event_for_each_child(event, perf_event_disable);
3330 mutex_unlock(&current->perf_event_mutex);
3331
3332 return 0;
3333}
3334
3335#ifndef PERF_EVENT_INDEX_OFFSET
3336# define PERF_EVENT_INDEX_OFFSET 0
3337#endif
3338
3339static int perf_event_index(struct perf_event *event)
3340{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02003341 if (event->hw.state & PERF_HES_STOPPED)
3342 return 0;
3343
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003344 if (event->state != PERF_EVENT_STATE_ACTIVE)
3345 return 0;
3346
3347 return event->hw.idx + 1 - PERF_EVENT_INDEX_OFFSET;
3348}
3349
3350/*
3351 * Callers need to ensure there can be no nesting of this function, otherwise
3352 * the seqlock logic goes bad. We can not serialize this because the arch
3353 * code calls this from NMI context.
3354 */
3355void perf_event_update_userpage(struct perf_event *event)
3356{
3357 struct perf_event_mmap_page *userpg;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003358 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003359
3360 rcu_read_lock();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003361 buffer = rcu_dereference(event->buffer);
3362 if (!buffer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003363 goto unlock;
3364
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003365 userpg = buffer->user_page;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003366
3367 /*
3368 * Disable preemption so as to not let the corresponding user-space
3369 * spin too long if we get preempted.
3370 */
3371 preempt_disable();
3372 ++userpg->lock;
3373 barrier();
3374 userpg->index = perf_event_index(event);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003375 userpg->offset = perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003376 if (event->state == PERF_EVENT_STATE_ACTIVE)
Peter Zijlstrae7850592010-05-21 14:43:08 +02003377 userpg->offset -= local64_read(&event->hw.prev_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003378
3379 userpg->time_enabled = event->total_time_enabled +
3380 atomic64_read(&event->child_total_time_enabled);
3381
3382 userpg->time_running = event->total_time_running +
3383 atomic64_read(&event->child_total_time_running);
3384
3385 barrier();
3386 ++userpg->lock;
3387 preempt_enable();
3388unlock:
3389 rcu_read_unlock();
3390}
3391
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02003392static unsigned long perf_data_size(struct perf_buffer *buffer);
3393
3394static void
3395perf_buffer_init(struct perf_buffer *buffer, long watermark, int flags)
3396{
3397 long max_size = perf_data_size(buffer);
3398
3399 if (watermark)
3400 buffer->watermark = min(max_size, watermark);
3401
3402 if (!buffer->watermark)
3403 buffer->watermark = max_size / 2;
3404
3405 if (flags & PERF_BUFFER_WRITABLE)
3406 buffer->writable = 1;
3407
3408 atomic_set(&buffer->refcount, 1);
3409}
3410
Peter Zijlstra906010b2009-09-21 16:08:49 +02003411#ifndef CONFIG_PERF_USE_VMALLOC
3412
3413/*
3414 * Back perf_mmap() with regular GFP_KERNEL-0 pages.
3415 */
3416
3417static struct page *
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003418perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff)
Peter Zijlstra906010b2009-09-21 16:08:49 +02003419{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003420 if (pgoff > buffer->nr_pages)
Peter Zijlstra906010b2009-09-21 16:08:49 +02003421 return NULL;
3422
3423 if (pgoff == 0)
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003424 return virt_to_page(buffer->user_page);
Peter Zijlstra906010b2009-09-21 16:08:49 +02003425
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003426 return virt_to_page(buffer->data_pages[pgoff - 1]);
Peter Zijlstra906010b2009-09-21 16:08:49 +02003427}
3428
Peter Zijlstraa19d35c2010-05-17 18:48:00 +02003429static void *perf_mmap_alloc_page(int cpu)
3430{
3431 struct page *page;
3432 int node;
3433
3434 node = (cpu == -1) ? cpu : cpu_to_node(cpu);
3435 page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
3436 if (!page)
3437 return NULL;
3438
3439 return page_address(page);
3440}
3441
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003442static struct perf_buffer *
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02003443perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003444{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003445 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003446 unsigned long size;
3447 int i;
3448
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003449 size = sizeof(struct perf_buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003450 size += nr_pages * sizeof(void *);
3451
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003452 buffer = kzalloc(size, GFP_KERNEL);
3453 if (!buffer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003454 goto fail;
3455
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02003456 buffer->user_page = perf_mmap_alloc_page(cpu);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003457 if (!buffer->user_page)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003458 goto fail_user_page;
3459
3460 for (i = 0; i < nr_pages; i++) {
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02003461 buffer->data_pages[i] = perf_mmap_alloc_page(cpu);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003462 if (!buffer->data_pages[i])
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003463 goto fail_data_pages;
3464 }
3465
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003466 buffer->nr_pages = nr_pages;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003467
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02003468 perf_buffer_init(buffer, watermark, flags);
3469
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003470 return buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003471
3472fail_data_pages:
3473 for (i--; i >= 0; i--)
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003474 free_page((unsigned long)buffer->data_pages[i]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003475
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003476 free_page((unsigned long)buffer->user_page);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003477
3478fail_user_page:
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003479 kfree(buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003480
3481fail:
Peter Zijlstra906010b2009-09-21 16:08:49 +02003482 return NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003483}
3484
3485static void perf_mmap_free_page(unsigned long addr)
3486{
3487 struct page *page = virt_to_page((void *)addr);
3488
3489 page->mapping = NULL;
3490 __free_page(page);
3491}
3492
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003493static void perf_buffer_free(struct perf_buffer *buffer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003494{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003495 int i;
3496
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003497 perf_mmap_free_page((unsigned long)buffer->user_page);
3498 for (i = 0; i < buffer->nr_pages; i++)
3499 perf_mmap_free_page((unsigned long)buffer->data_pages[i]);
3500 kfree(buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02003501}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003502
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003503static inline int page_order(struct perf_buffer *buffer)
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02003504{
3505 return 0;
3506}
3507
Peter Zijlstra906010b2009-09-21 16:08:49 +02003508#else
3509
3510/*
3511 * Back perf_mmap() with vmalloc memory.
3512 *
3513 * Required for architectures that have d-cache aliasing issues.
3514 */
3515
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003516static inline int page_order(struct perf_buffer *buffer)
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02003517{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003518 return buffer->page_order;
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02003519}
3520
Peter Zijlstra906010b2009-09-21 16:08:49 +02003521static struct page *
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003522perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff)
Peter Zijlstra906010b2009-09-21 16:08:49 +02003523{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003524 if (pgoff > (1UL << page_order(buffer)))
Peter Zijlstra906010b2009-09-21 16:08:49 +02003525 return NULL;
3526
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003527 return vmalloc_to_page((void *)buffer->user_page + pgoff * PAGE_SIZE);
Peter Zijlstra906010b2009-09-21 16:08:49 +02003528}
3529
3530static void perf_mmap_unmark_page(void *addr)
3531{
3532 struct page *page = vmalloc_to_page(addr);
3533
3534 page->mapping = NULL;
3535}
3536
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003537static void perf_buffer_free_work(struct work_struct *work)
Peter Zijlstra906010b2009-09-21 16:08:49 +02003538{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003539 struct perf_buffer *buffer;
Peter Zijlstra906010b2009-09-21 16:08:49 +02003540 void *base;
3541 int i, nr;
3542
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003543 buffer = container_of(work, struct perf_buffer, work);
3544 nr = 1 << page_order(buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02003545
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003546 base = buffer->user_page;
Peter Zijlstra906010b2009-09-21 16:08:49 +02003547 for (i = 0; i < nr + 1; i++)
3548 perf_mmap_unmark_page(base + (i * PAGE_SIZE));
3549
3550 vfree(base);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003551 kfree(buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02003552}
3553
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003554static void perf_buffer_free(struct perf_buffer *buffer)
Peter Zijlstra906010b2009-09-21 16:08:49 +02003555{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003556 schedule_work(&buffer->work);
Peter Zijlstra906010b2009-09-21 16:08:49 +02003557}
3558
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003559static struct perf_buffer *
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02003560perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags)
Peter Zijlstra906010b2009-09-21 16:08:49 +02003561{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003562 struct perf_buffer *buffer;
Peter Zijlstra906010b2009-09-21 16:08:49 +02003563 unsigned long size;
3564 void *all_buf;
3565
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003566 size = sizeof(struct perf_buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02003567 size += sizeof(void *);
3568
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003569 buffer = kzalloc(size, GFP_KERNEL);
3570 if (!buffer)
Peter Zijlstra906010b2009-09-21 16:08:49 +02003571 goto fail;
3572
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003573 INIT_WORK(&buffer->work, perf_buffer_free_work);
Peter Zijlstra906010b2009-09-21 16:08:49 +02003574
3575 all_buf = vmalloc_user((nr_pages + 1) * PAGE_SIZE);
3576 if (!all_buf)
3577 goto fail_all_buf;
3578
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003579 buffer->user_page = all_buf;
3580 buffer->data_pages[0] = all_buf + PAGE_SIZE;
3581 buffer->page_order = ilog2(nr_pages);
3582 buffer->nr_pages = 1;
Peter Zijlstra906010b2009-09-21 16:08:49 +02003583
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02003584 perf_buffer_init(buffer, watermark, flags);
3585
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003586 return buffer;
Peter Zijlstra906010b2009-09-21 16:08:49 +02003587
3588fail_all_buf:
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003589 kfree(buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02003590
3591fail:
3592 return NULL;
3593}
3594
3595#endif
3596
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003597static unsigned long perf_data_size(struct perf_buffer *buffer)
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02003598{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003599 return buffer->nr_pages << (PAGE_SHIFT + page_order(buffer));
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02003600}
3601
Peter Zijlstra906010b2009-09-21 16:08:49 +02003602static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
3603{
3604 struct perf_event *event = vma->vm_file->private_data;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003605 struct perf_buffer *buffer;
Peter Zijlstra906010b2009-09-21 16:08:49 +02003606 int ret = VM_FAULT_SIGBUS;
3607
3608 if (vmf->flags & FAULT_FLAG_MKWRITE) {
3609 if (vmf->pgoff == 0)
3610 ret = 0;
3611 return ret;
3612 }
3613
3614 rcu_read_lock();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003615 buffer = rcu_dereference(event->buffer);
3616 if (!buffer)
Peter Zijlstra906010b2009-09-21 16:08:49 +02003617 goto unlock;
3618
3619 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
3620 goto unlock;
3621
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003622 vmf->page = perf_mmap_to_page(buffer, vmf->pgoff);
Peter Zijlstra906010b2009-09-21 16:08:49 +02003623 if (!vmf->page)
3624 goto unlock;
3625
3626 get_page(vmf->page);
3627 vmf->page->mapping = vma->vm_file->f_mapping;
3628 vmf->page->index = vmf->pgoff;
3629
3630 ret = 0;
3631unlock:
3632 rcu_read_unlock();
3633
3634 return ret;
3635}
3636
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003637static void perf_buffer_free_rcu(struct rcu_head *rcu_head)
Peter Zijlstra906010b2009-09-21 16:08:49 +02003638{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003639 struct perf_buffer *buffer;
Peter Zijlstra906010b2009-09-21 16:08:49 +02003640
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003641 buffer = container_of(rcu_head, struct perf_buffer, rcu_head);
3642 perf_buffer_free(buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003643}
3644
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003645static struct perf_buffer *perf_buffer_get(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003646{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003647 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003648
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003649 rcu_read_lock();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003650 buffer = rcu_dereference(event->buffer);
3651 if (buffer) {
3652 if (!atomic_inc_not_zero(&buffer->refcount))
3653 buffer = NULL;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003654 }
3655 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003656
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003657 return buffer;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003658}
3659
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003660static void perf_buffer_put(struct perf_buffer *buffer)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003661{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003662 if (!atomic_dec_and_test(&buffer->refcount))
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003663 return;
3664
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003665 call_rcu(&buffer->rcu_head, perf_buffer_free_rcu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003666}
3667
3668static void perf_mmap_open(struct vm_area_struct *vma)
3669{
3670 struct perf_event *event = vma->vm_file->private_data;
3671
3672 atomic_inc(&event->mmap_count);
3673}
3674
3675static void perf_mmap_close(struct vm_area_struct *vma)
3676{
3677 struct perf_event *event = vma->vm_file->private_data;
3678
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003679 if (atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) {
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003680 unsigned long size = perf_data_size(event->buffer);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003681 struct user_struct *user = event->mmap_user;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003682 struct perf_buffer *buffer = event->buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003683
Peter Zijlstra906010b2009-09-21 16:08:49 +02003684 atomic_long_sub((size >> PAGE_SHIFT) + 1, &user->locked_vm);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003685 vma->vm_mm->locked_vm -= event->mmap_locked;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003686 rcu_assign_pointer(event->buffer, NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003687 mutex_unlock(&event->mmap_mutex);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003688
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003689 perf_buffer_put(buffer);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003690 free_uid(user);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003691 }
3692}
3693
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04003694static const struct vm_operations_struct perf_mmap_vmops = {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003695 .open = perf_mmap_open,
3696 .close = perf_mmap_close,
3697 .fault = perf_mmap_fault,
3698 .page_mkwrite = perf_mmap_fault,
3699};
3700
3701static int perf_mmap(struct file *file, struct vm_area_struct *vma)
3702{
3703 struct perf_event *event = file->private_data;
3704 unsigned long user_locked, user_lock_limit;
3705 struct user_struct *user = current_user();
3706 unsigned long locked, lock_limit;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003707 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003708 unsigned long vma_size;
3709 unsigned long nr_pages;
3710 long user_extra, extra;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02003711 int ret = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003712
Peter Zijlstrac7920612010-05-18 10:33:24 +02003713 /*
3714 * Don't allow mmap() of inherited per-task counters. This would
3715 * create a performance issue due to all children writing to the
3716 * same buffer.
3717 */
3718 if (event->cpu == -1 && event->attr.inherit)
3719 return -EINVAL;
3720
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003721 if (!(vma->vm_flags & VM_SHARED))
3722 return -EINVAL;
3723
3724 vma_size = vma->vm_end - vma->vm_start;
3725 nr_pages = (vma_size / PAGE_SIZE) - 1;
3726
3727 /*
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003728 * If we have buffer pages ensure they're a power-of-two number, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003729 * can do bitmasks instead of modulo.
3730 */
3731 if (nr_pages != 0 && !is_power_of_2(nr_pages))
3732 return -EINVAL;
3733
3734 if (vma_size != PAGE_SIZE * (1 + nr_pages))
3735 return -EINVAL;
3736
3737 if (vma->vm_pgoff != 0)
3738 return -EINVAL;
3739
3740 WARN_ON_ONCE(event->ctx->parent_ctx);
3741 mutex_lock(&event->mmap_mutex);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003742 if (event->buffer) {
3743 if (event->buffer->nr_pages == nr_pages)
3744 atomic_inc(&event->buffer->refcount);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003745 else
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003746 ret = -EINVAL;
3747 goto unlock;
3748 }
3749
3750 user_extra = nr_pages + 1;
3751 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
3752
3753 /*
3754 * Increase the limit linearly with more CPUs:
3755 */
3756 user_lock_limit *= num_online_cpus();
3757
3758 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
3759
3760 extra = 0;
3761 if (user_locked > user_lock_limit)
3762 extra = user_locked - user_lock_limit;
3763
Jiri Slaby78d7d402010-03-05 13:42:54 -08003764 lock_limit = rlimit(RLIMIT_MEMLOCK);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003765 lock_limit >>= PAGE_SHIFT;
3766 locked = vma->vm_mm->locked_vm + extra;
3767
3768 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
3769 !capable(CAP_IPC_LOCK)) {
3770 ret = -EPERM;
3771 goto unlock;
3772 }
3773
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003774 WARN_ON(event->buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02003775
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02003776 if (vma->vm_flags & VM_WRITE)
3777 flags |= PERF_BUFFER_WRITABLE;
3778
3779 buffer = perf_buffer_alloc(nr_pages, event->attr.wakeup_watermark,
3780 event->cpu, flags);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003781 if (!buffer) {
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003782 ret = -ENOMEM;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003783 goto unlock;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003784 }
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02003785 rcu_assign_pointer(event->buffer, buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003786
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003787 atomic_long_add(user_extra, &user->locked_vm);
3788 event->mmap_locked = extra;
3789 event->mmap_user = get_current_user();
3790 vma->vm_mm->locked_vm += event->mmap_locked;
3791
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003792unlock:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003793 if (!ret)
3794 atomic_inc(&event->mmap_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003795 mutex_unlock(&event->mmap_mutex);
3796
3797 vma->vm_flags |= VM_RESERVED;
3798 vma->vm_ops = &perf_mmap_vmops;
3799
3800 return ret;
3801}
3802
3803static int perf_fasync(int fd, struct file *filp, int on)
3804{
3805 struct inode *inode = filp->f_path.dentry->d_inode;
3806 struct perf_event *event = filp->private_data;
3807 int retval;
3808
3809 mutex_lock(&inode->i_mutex);
3810 retval = fasync_helper(fd, filp, on, &event->fasync);
3811 mutex_unlock(&inode->i_mutex);
3812
3813 if (retval < 0)
3814 return retval;
3815
3816 return 0;
3817}
3818
3819static const struct file_operations perf_fops = {
Arnd Bergmann3326c1c2010-03-23 19:09:33 +01003820 .llseek = no_llseek,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003821 .release = perf_release,
3822 .read = perf_read,
3823 .poll = perf_poll,
3824 .unlocked_ioctl = perf_ioctl,
3825 .compat_ioctl = perf_ioctl,
3826 .mmap = perf_mmap,
3827 .fasync = perf_fasync,
3828};
3829
3830/*
3831 * Perf event wakeup
3832 *
3833 * If there's data, ensure we set the poll() state and publish everything
3834 * to user-space before waking everybody up.
3835 */
3836
3837void perf_event_wakeup(struct perf_event *event)
3838{
3839 wake_up_all(&event->waitq);
3840
3841 if (event->pending_kill) {
3842 kill_fasync(&event->fasync, SIGIO, event->pending_kill);
3843 event->pending_kill = 0;
3844 }
3845}
3846
Peter Zijlstrae360adb2010-10-14 14:01:34 +08003847static void perf_pending_event(struct irq_work *entry)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003848{
3849 struct perf_event *event = container_of(entry,
3850 struct perf_event, pending);
3851
3852 if (event->pending_disable) {
3853 event->pending_disable = 0;
3854 __perf_event_disable(event);
3855 }
3856
3857 if (event->pending_wakeup) {
3858 event->pending_wakeup = 0;
3859 perf_event_wakeup(event);
3860 }
3861}
3862
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003863/*
Zhang, Yanmin39447b32010-04-19 13:32:41 +08003864 * We assume there is only KVM supporting the callbacks.
3865 * Later on, we might change it to a list if there is
3866 * another virtualization implementation supporting the callbacks.
3867 */
3868struct perf_guest_info_callbacks *perf_guest_cbs;
3869
3870int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3871{
3872 perf_guest_cbs = cbs;
3873 return 0;
3874}
3875EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
3876
3877int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3878{
3879 perf_guest_cbs = NULL;
3880 return 0;
3881}
3882EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
3883
3884/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003885 * Output
3886 */
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003887static bool perf_output_space(struct perf_buffer *buffer, unsigned long tail,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003888 unsigned long offset, unsigned long head)
3889{
3890 unsigned long mask;
3891
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003892 if (!buffer->writable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003893 return true;
3894
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003895 mask = perf_data_size(buffer) - 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003896
3897 offset = (offset - tail) & mask;
3898 head = (head - tail) & mask;
3899
3900 if ((int)(head - offset) < 0)
3901 return false;
3902
3903 return true;
3904}
3905
3906static void perf_output_wakeup(struct perf_output_handle *handle)
3907{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003908 atomic_set(&handle->buffer->poll, POLL_IN);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003909
3910 if (handle->nmi) {
3911 handle->event->pending_wakeup = 1;
Peter Zijlstrae360adb2010-10-14 14:01:34 +08003912 irq_work_queue(&handle->event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003913 } else
3914 perf_event_wakeup(handle->event);
3915}
3916
3917/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003918 * We need to ensure a later event_id doesn't publish a head when a former
Peter Zijlstraef607772010-05-18 10:50:41 +02003919 * event isn't done writing. However since we need to deal with NMIs we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003920 * cannot fully serialize things.
3921 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003922 * We only publish the head (and generate a wakeup) when the outer-most
Peter Zijlstraef607772010-05-18 10:50:41 +02003923 * event completes.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003924 */
Peter Zijlstraef607772010-05-18 10:50:41 +02003925static void perf_output_get_handle(struct perf_output_handle *handle)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003926{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003927 struct perf_buffer *buffer = handle->buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003928
Peter Zijlstraef607772010-05-18 10:50:41 +02003929 preempt_disable();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003930 local_inc(&buffer->nest);
3931 handle->wakeup = local_read(&buffer->wakeup);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003932}
3933
Peter Zijlstraef607772010-05-18 10:50:41 +02003934static void perf_output_put_handle(struct perf_output_handle *handle)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003935{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003936 struct perf_buffer *buffer = handle->buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003937 unsigned long head;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003938
3939again:
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003940 head = local_read(&buffer->head);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003941
3942 /*
Peter Zijlstraef607772010-05-18 10:50:41 +02003943 * IRQ/NMI can happen here, which means we can miss a head update.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003944 */
3945
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003946 if (!local_dec_and_test(&buffer->nest))
Frederic Weisbeckeracd35a42010-05-20 21:28:34 +02003947 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003948
3949 /*
Peter Zijlstraef607772010-05-18 10:50:41 +02003950 * Publish the known good head. Rely on the full barrier implied
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003951 * by atomic_dec_and_test() order the buffer->head read and this
Peter Zijlstraef607772010-05-18 10:50:41 +02003952 * write.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003953 */
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003954 buffer->user_page->data_head = head;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003955
Peter Zijlstraef607772010-05-18 10:50:41 +02003956 /*
3957 * Now check if we missed an update, rely on the (compiler)
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003958 * barrier in atomic_dec_and_test() to re-read buffer->head.
Peter Zijlstraef607772010-05-18 10:50:41 +02003959 */
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003960 if (unlikely(head != local_read(&buffer->head))) {
3961 local_inc(&buffer->nest);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003962 goto again;
3963 }
3964
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003965 if (handle->wakeup != local_read(&buffer->wakeup))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003966 perf_output_wakeup(handle);
Peter Zijlstraef607772010-05-18 10:50:41 +02003967
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003968out:
Peter Zijlstraef607772010-05-18 10:50:41 +02003969 preempt_enable();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003970}
3971
Peter Zijlstraa94ffaa2010-05-20 19:50:07 +02003972__always_inline void perf_output_copy(struct perf_output_handle *handle,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003973 const void *buf, unsigned int len)
3974{
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003975 do {
Peter Zijlstraa94ffaa2010-05-20 19:50:07 +02003976 unsigned long size = min_t(unsigned long, handle->size, len);
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003977
3978 memcpy(handle->addr, buf, size);
3979
3980 len -= size;
3981 handle->addr += size;
Frederic Weisbecker74048f82010-05-27 21:34:58 +02003982 buf += size;
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003983 handle->size -= size;
3984 if (!handle->size) {
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003985 struct perf_buffer *buffer = handle->buffer;
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02003986
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003987 handle->page++;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003988 handle->page &= buffer->nr_pages - 1;
3989 handle->addr = buffer->data_pages[handle->page];
3990 handle->size = PAGE_SIZE << page_order(buffer);
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003991 }
3992 } while (len);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003993}
3994
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02003995static void __perf_event_header__init_id(struct perf_event_header *header,
3996 struct perf_sample_data *data,
3997 struct perf_event *event)
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02003998{
3999 u64 sample_type = event->attr.sample_type;
4000
4001 data->type = sample_type;
4002 header->size += event->id_header_size;
4003
4004 if (sample_type & PERF_SAMPLE_TID) {
4005 /* namespace issues */
4006 data->tid_entry.pid = perf_event_pid(event, current);
4007 data->tid_entry.tid = perf_event_tid(event, current);
4008 }
4009
4010 if (sample_type & PERF_SAMPLE_TIME)
4011 data->time = perf_clock();
4012
4013 if (sample_type & PERF_SAMPLE_ID)
4014 data->id = primary_event_id(event);
4015
4016 if (sample_type & PERF_SAMPLE_STREAM_ID)
4017 data->stream_id = event->id;
4018
4019 if (sample_type & PERF_SAMPLE_CPU) {
4020 data->cpu_entry.cpu = raw_smp_processor_id();
4021 data->cpu_entry.reserved = 0;
4022 }
4023}
4024
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004025static void perf_event_header__init_id(struct perf_event_header *header,
4026 struct perf_sample_data *data,
4027 struct perf_event *event)
4028{
4029 if (event->attr.sample_id_all)
4030 __perf_event_header__init_id(header, data, event);
4031}
4032
4033static void __perf_event__output_id_sample(struct perf_output_handle *handle,
4034 struct perf_sample_data *data)
4035{
4036 u64 sample_type = data->type;
4037
4038 if (sample_type & PERF_SAMPLE_TID)
4039 perf_output_put(handle, data->tid_entry);
4040
4041 if (sample_type & PERF_SAMPLE_TIME)
4042 perf_output_put(handle, data->time);
4043
4044 if (sample_type & PERF_SAMPLE_ID)
4045 perf_output_put(handle, data->id);
4046
4047 if (sample_type & PERF_SAMPLE_STREAM_ID)
4048 perf_output_put(handle, data->stream_id);
4049
4050 if (sample_type & PERF_SAMPLE_CPU)
4051 perf_output_put(handle, data->cpu_entry);
4052}
4053
4054static void perf_event__output_id_sample(struct perf_event *event,
4055 struct perf_output_handle *handle,
4056 struct perf_sample_data *sample)
4057{
4058 if (event->attr.sample_id_all)
4059 __perf_event__output_id_sample(handle, sample);
4060}
4061
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004062int perf_output_begin(struct perf_output_handle *handle,
4063 struct perf_event *event, unsigned int size,
4064 int nmi, int sample)
4065{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02004066 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004067 unsigned long tail, offset, head;
4068 int have_lost;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004069 struct perf_sample_data sample_data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004070 struct {
4071 struct perf_event_header header;
4072 u64 id;
4073 u64 lost;
4074 } lost_event;
4075
4076 rcu_read_lock();
4077 /*
4078 * For inherited events we send all the output towards the parent.
4079 */
4080 if (event->parent)
4081 event = event->parent;
4082
Peter Zijlstraca5135e2010-05-28 19:33:23 +02004083 buffer = rcu_dereference(event->buffer);
4084 if (!buffer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004085 goto out;
4086
Peter Zijlstraca5135e2010-05-28 19:33:23 +02004087 handle->buffer = buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004088 handle->event = event;
4089 handle->nmi = nmi;
4090 handle->sample = sample;
4091
Peter Zijlstraca5135e2010-05-28 19:33:23 +02004092 if (!buffer->nr_pages)
Stephane Eranian00d1d0b2010-05-17 12:46:01 +02004093 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004094
Peter Zijlstraca5135e2010-05-28 19:33:23 +02004095 have_lost = local_read(&buffer->lost);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004096 if (have_lost) {
4097 lost_event.header.size = sizeof(lost_event);
4098 perf_event_header__init_id(&lost_event.header, &sample_data,
4099 event);
4100 size += lost_event.header.size;
4101 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004102
Peter Zijlstraef607772010-05-18 10:50:41 +02004103 perf_output_get_handle(handle);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004104
4105 do {
4106 /*
4107 * Userspace could choose to issue a mb() before updating the
4108 * tail pointer. So that all reads will be completed before the
4109 * write is issued.
4110 */
Peter Zijlstraca5135e2010-05-28 19:33:23 +02004111 tail = ACCESS_ONCE(buffer->user_page->data_tail);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004112 smp_rmb();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02004113 offset = head = local_read(&buffer->head);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004114 head += size;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02004115 if (unlikely(!perf_output_space(buffer, tail, offset, head)))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004116 goto fail;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02004117 } while (local_cmpxchg(&buffer->head, offset, head) != offset);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004118
Peter Zijlstraca5135e2010-05-28 19:33:23 +02004119 if (head - local_read(&buffer->wakeup) > buffer->watermark)
4120 local_add(buffer->watermark, &buffer->wakeup);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004121
Peter Zijlstraca5135e2010-05-28 19:33:23 +02004122 handle->page = offset >> (PAGE_SHIFT + page_order(buffer));
4123 handle->page &= buffer->nr_pages - 1;
4124 handle->size = offset & ((PAGE_SIZE << page_order(buffer)) - 1);
4125 handle->addr = buffer->data_pages[handle->page];
Peter Zijlstra5d967a82010-05-20 16:46:39 +02004126 handle->addr += handle->size;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02004127 handle->size = (PAGE_SIZE << page_order(buffer)) - handle->size;
Peter Zijlstra5d967a82010-05-20 16:46:39 +02004128
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004129 if (have_lost) {
4130 lost_event.header.type = PERF_RECORD_LOST;
4131 lost_event.header.misc = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004132 lost_event.id = event->id;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02004133 lost_event.lost = local_xchg(&buffer->lost, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004134
4135 perf_output_put(handle, lost_event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004136 perf_event__output_id_sample(event, handle, &sample_data);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004137 }
4138
4139 return 0;
4140
4141fail:
Peter Zijlstraca5135e2010-05-28 19:33:23 +02004142 local_inc(&buffer->lost);
Peter Zijlstraef607772010-05-18 10:50:41 +02004143 perf_output_put_handle(handle);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004144out:
4145 rcu_read_unlock();
4146
4147 return -ENOSPC;
4148}
4149
4150void perf_output_end(struct perf_output_handle *handle)
4151{
4152 struct perf_event *event = handle->event;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02004153 struct perf_buffer *buffer = handle->buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004154
4155 int wakeup_events = event->attr.wakeup_events;
4156
4157 if (handle->sample && wakeup_events) {
Peter Zijlstraca5135e2010-05-28 19:33:23 +02004158 int events = local_inc_return(&buffer->events);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004159 if (events >= wakeup_events) {
Peter Zijlstraca5135e2010-05-28 19:33:23 +02004160 local_sub(wakeup_events, &buffer->events);
4161 local_inc(&buffer->wakeup);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004162 }
4163 }
4164
Peter Zijlstraef607772010-05-18 10:50:41 +02004165 perf_output_put_handle(handle);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004166 rcu_read_unlock();
4167}
4168
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004169static void perf_output_read_one(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02004170 struct perf_event *event,
4171 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004172{
4173 u64 read_format = event->attr.read_format;
4174 u64 values[4];
4175 int n = 0;
4176
Peter Zijlstrab5e58792010-05-21 14:43:12 +02004177 values[n++] = perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004178 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
Stephane Eranianeed01522010-10-26 16:08:01 +02004179 values[n++] = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004180 atomic64_read(&event->child_total_time_enabled);
4181 }
4182 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
Stephane Eranianeed01522010-10-26 16:08:01 +02004183 values[n++] = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004184 atomic64_read(&event->child_total_time_running);
4185 }
4186 if (read_format & PERF_FORMAT_ID)
4187 values[n++] = primary_event_id(event);
4188
4189 perf_output_copy(handle, values, n * sizeof(u64));
4190}
4191
4192/*
4193 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
4194 */
4195static void perf_output_read_group(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02004196 struct perf_event *event,
4197 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004198{
4199 struct perf_event *leader = event->group_leader, *sub;
4200 u64 read_format = event->attr.read_format;
4201 u64 values[5];
4202 int n = 0;
4203
4204 values[n++] = 1 + leader->nr_siblings;
4205
4206 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
Stephane Eranianeed01522010-10-26 16:08:01 +02004207 values[n++] = enabled;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004208
4209 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
Stephane Eranianeed01522010-10-26 16:08:01 +02004210 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004211
4212 if (leader != event)
4213 leader->pmu->read(leader);
4214
Peter Zijlstrab5e58792010-05-21 14:43:12 +02004215 values[n++] = perf_event_count(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004216 if (read_format & PERF_FORMAT_ID)
4217 values[n++] = primary_event_id(leader);
4218
4219 perf_output_copy(handle, values, n * sizeof(u64));
4220
4221 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
4222 n = 0;
4223
4224 if (sub != event)
4225 sub->pmu->read(sub);
4226
Peter Zijlstrab5e58792010-05-21 14:43:12 +02004227 values[n++] = perf_event_count(sub);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004228 if (read_format & PERF_FORMAT_ID)
4229 values[n++] = primary_event_id(sub);
4230
4231 perf_output_copy(handle, values, n * sizeof(u64));
4232 }
4233}
4234
Stephane Eranianeed01522010-10-26 16:08:01 +02004235#define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
4236 PERF_FORMAT_TOTAL_TIME_RUNNING)
4237
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004238static void perf_output_read(struct perf_output_handle *handle,
4239 struct perf_event *event)
4240{
Stephane Eranianeed01522010-10-26 16:08:01 +02004241 u64 enabled = 0, running = 0, now, ctx_time;
4242 u64 read_format = event->attr.read_format;
4243
4244 /*
4245 * compute total_time_enabled, total_time_running
4246 * based on snapshot values taken when the event
4247 * was last scheduled in.
4248 *
4249 * we cannot simply called update_context_time()
4250 * because of locking issue as we are called in
4251 * NMI context
4252 */
4253 if (read_format & PERF_FORMAT_TOTAL_TIMES) {
4254 now = perf_clock();
4255 ctx_time = event->shadow_ctx_time + now;
4256 enabled = ctx_time - event->tstamp_enabled;
4257 running = ctx_time - event->tstamp_running;
4258 }
4259
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004260 if (event->attr.read_format & PERF_FORMAT_GROUP)
Stephane Eranianeed01522010-10-26 16:08:01 +02004261 perf_output_read_group(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004262 else
Stephane Eranianeed01522010-10-26 16:08:01 +02004263 perf_output_read_one(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004264}
4265
4266void perf_output_sample(struct perf_output_handle *handle,
4267 struct perf_event_header *header,
4268 struct perf_sample_data *data,
4269 struct perf_event *event)
4270{
4271 u64 sample_type = data->type;
4272
4273 perf_output_put(handle, *header);
4274
4275 if (sample_type & PERF_SAMPLE_IP)
4276 perf_output_put(handle, data->ip);
4277
4278 if (sample_type & PERF_SAMPLE_TID)
4279 perf_output_put(handle, data->tid_entry);
4280
4281 if (sample_type & PERF_SAMPLE_TIME)
4282 perf_output_put(handle, data->time);
4283
4284 if (sample_type & PERF_SAMPLE_ADDR)
4285 perf_output_put(handle, data->addr);
4286
4287 if (sample_type & PERF_SAMPLE_ID)
4288 perf_output_put(handle, data->id);
4289
4290 if (sample_type & PERF_SAMPLE_STREAM_ID)
4291 perf_output_put(handle, data->stream_id);
4292
4293 if (sample_type & PERF_SAMPLE_CPU)
4294 perf_output_put(handle, data->cpu_entry);
4295
4296 if (sample_type & PERF_SAMPLE_PERIOD)
4297 perf_output_put(handle, data->period);
4298
4299 if (sample_type & PERF_SAMPLE_READ)
4300 perf_output_read(handle, event);
4301
4302 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
4303 if (data->callchain) {
4304 int size = 1;
4305
4306 if (data->callchain)
4307 size += data->callchain->nr;
4308
4309 size *= sizeof(u64);
4310
4311 perf_output_copy(handle, data->callchain, size);
4312 } else {
4313 u64 nr = 0;
4314 perf_output_put(handle, nr);
4315 }
4316 }
4317
4318 if (sample_type & PERF_SAMPLE_RAW) {
4319 if (data->raw) {
4320 perf_output_put(handle, data->raw->size);
4321 perf_output_copy(handle, data->raw->data,
4322 data->raw->size);
4323 } else {
4324 struct {
4325 u32 size;
4326 u32 data;
4327 } raw = {
4328 .size = sizeof(u32),
4329 .data = 0,
4330 };
4331 perf_output_put(handle, raw);
4332 }
4333 }
4334}
4335
4336void perf_prepare_sample(struct perf_event_header *header,
4337 struct perf_sample_data *data,
4338 struct perf_event *event,
4339 struct pt_regs *regs)
4340{
4341 u64 sample_type = event->attr.sample_type;
4342
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004343 header->type = PERF_RECORD_SAMPLE;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02004344 header->size = sizeof(*header) + event->header_size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004345
4346 header->misc = 0;
4347 header->misc |= perf_misc_flags(regs);
4348
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004349 __perf_event_header__init_id(header, data, event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02004350
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02004351 if (sample_type & PERF_SAMPLE_IP)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004352 data->ip = perf_instruction_pointer(regs);
4353
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004354 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
4355 int size = 1;
4356
4357 data->callchain = perf_callchain(regs);
4358
4359 if (data->callchain)
4360 size += data->callchain->nr;
4361
4362 header->size += size * sizeof(u64);
4363 }
4364
4365 if (sample_type & PERF_SAMPLE_RAW) {
4366 int size = sizeof(u32);
4367
4368 if (data->raw)
4369 size += data->raw->size;
4370 else
4371 size += sizeof(u32);
4372
4373 WARN_ON_ONCE(size & (sizeof(u64)-1));
4374 header->size += size;
4375 }
4376}
4377
4378static void perf_event_output(struct perf_event *event, int nmi,
4379 struct perf_sample_data *data,
4380 struct pt_regs *regs)
4381{
4382 struct perf_output_handle handle;
4383 struct perf_event_header header;
4384
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02004385 /* protect the callchain buffers */
4386 rcu_read_lock();
4387
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004388 perf_prepare_sample(&header, data, event, regs);
4389
4390 if (perf_output_begin(&handle, event, header.size, nmi, 1))
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02004391 goto exit;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004392
4393 perf_output_sample(&handle, &header, data, event);
4394
4395 perf_output_end(&handle);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02004396
4397exit:
4398 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004399}
4400
4401/*
4402 * read event_id
4403 */
4404
4405struct perf_read_event {
4406 struct perf_event_header header;
4407
4408 u32 pid;
4409 u32 tid;
4410};
4411
4412static void
4413perf_event_read_event(struct perf_event *event,
4414 struct task_struct *task)
4415{
4416 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004417 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004418 struct perf_read_event read_event = {
4419 .header = {
4420 .type = PERF_RECORD_READ,
4421 .misc = 0,
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02004422 .size = sizeof(read_event) + event->read_size,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004423 },
4424 .pid = perf_event_pid(event, task),
4425 .tid = perf_event_tid(event, task),
4426 };
4427 int ret;
4428
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004429 perf_event_header__init_id(&read_event.header, &sample, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004430 ret = perf_output_begin(&handle, event, read_event.header.size, 0, 0);
4431 if (ret)
4432 return;
4433
4434 perf_output_put(&handle, read_event);
4435 perf_output_read(&handle, event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004436 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004437
4438 perf_output_end(&handle);
4439}
4440
4441/*
4442 * task tracking -- fork/exit
4443 *
Eric B Munson3af9e852010-05-18 15:30:49 +01004444 * enabled by: attr.comm | attr.mmap | attr.mmap_data | attr.task
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004445 */
4446
4447struct perf_task_event {
4448 struct task_struct *task;
4449 struct perf_event_context *task_ctx;
4450
4451 struct {
4452 struct perf_event_header header;
4453
4454 u32 pid;
4455 u32 ppid;
4456 u32 tid;
4457 u32 ptid;
4458 u64 time;
4459 } event_id;
4460};
4461
4462static void perf_event_task_output(struct perf_event *event,
4463 struct perf_task_event *task_event)
4464{
4465 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004466 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004467 struct task_struct *task = task_event->task;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004468 int ret, size = task_event->event_id.header.size;
Mike Galbraith8bb39f92010-03-26 11:11:33 +01004469
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004470 perf_event_header__init_id(&task_event->event_id.header, &sample, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004471
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004472 ret = perf_output_begin(&handle, event,
4473 task_event->event_id.header.size, 0, 0);
Peter Zijlstraef607772010-05-18 10:50:41 +02004474 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004475 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004476
4477 task_event->event_id.pid = perf_event_pid(event, task);
4478 task_event->event_id.ppid = perf_event_pid(event, current);
4479
4480 task_event->event_id.tid = perf_event_tid(event, task);
4481 task_event->event_id.ptid = perf_event_tid(event, current);
4482
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004483 perf_output_put(&handle, task_event->event_id);
4484
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004485 perf_event__output_id_sample(event, &handle, &sample);
4486
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004487 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004488out:
4489 task_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004490}
4491
4492static int perf_event_task_match(struct perf_event *event)
4493{
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01004494 if (event->state < PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra22e19082010-01-18 09:12:32 +01004495 return 0;
4496
Stephane Eranian5632ab12011-01-03 18:20:01 +02004497 if (!event_filter_match(event))
Peter Zijlstra5d27c232009-12-17 13:16:32 +01004498 return 0;
4499
Eric B Munson3af9e852010-05-18 15:30:49 +01004500 if (event->attr.comm || event->attr.mmap ||
4501 event->attr.mmap_data || event->attr.task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004502 return 1;
4503
4504 return 0;
4505}
4506
4507static void perf_event_task_ctx(struct perf_event_context *ctx,
4508 struct perf_task_event *task_event)
4509{
4510 struct perf_event *event;
4511
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004512 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4513 if (perf_event_task_match(event))
4514 perf_event_task_output(event, task_event);
4515 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004516}
4517
4518static void perf_event_task_event(struct perf_task_event *task_event)
4519{
4520 struct perf_cpu_context *cpuctx;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004521 struct perf_event_context *ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004522 struct pmu *pmu;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004523 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004524
Peter Zijlstrad6ff86c2009-11-20 22:19:46 +01004525 rcu_read_lock();
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004526 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra41945f62010-09-16 19:17:24 +02004527 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra51676952010-12-07 14:18:20 +01004528 if (cpuctx->active_pmu != pmu)
4529 goto next;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004530 perf_event_task_ctx(&cpuctx->ctx, task_event);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004531
4532 ctx = task_event->task_ctx;
4533 if (!ctx) {
4534 ctxn = pmu->task_ctx_nr;
4535 if (ctxn < 0)
Peter Zijlstra41945f62010-09-16 19:17:24 +02004536 goto next;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004537 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4538 }
4539 if (ctx)
4540 perf_event_task_ctx(ctx, task_event);
Peter Zijlstra41945f62010-09-16 19:17:24 +02004541next:
4542 put_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004543 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004544 rcu_read_unlock();
4545}
4546
4547static void perf_event_task(struct task_struct *task,
4548 struct perf_event_context *task_ctx,
4549 int new)
4550{
4551 struct perf_task_event task_event;
4552
4553 if (!atomic_read(&nr_comm_events) &&
4554 !atomic_read(&nr_mmap_events) &&
4555 !atomic_read(&nr_task_events))
4556 return;
4557
4558 task_event = (struct perf_task_event){
4559 .task = task,
4560 .task_ctx = task_ctx,
4561 .event_id = {
4562 .header = {
4563 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
4564 .misc = 0,
4565 .size = sizeof(task_event.event_id),
4566 },
4567 /* .pid */
4568 /* .ppid */
4569 /* .tid */
4570 /* .ptid */
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01004571 .time = perf_clock(),
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004572 },
4573 };
4574
4575 perf_event_task_event(&task_event);
4576}
4577
4578void perf_event_fork(struct task_struct *task)
4579{
4580 perf_event_task(task, NULL, 1);
4581}
4582
4583/*
4584 * comm tracking
4585 */
4586
4587struct perf_comm_event {
4588 struct task_struct *task;
4589 char *comm;
4590 int comm_size;
4591
4592 struct {
4593 struct perf_event_header header;
4594
4595 u32 pid;
4596 u32 tid;
4597 } event_id;
4598};
4599
4600static void perf_event_comm_output(struct perf_event *event,
4601 struct perf_comm_event *comm_event)
4602{
4603 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004604 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004605 int size = comm_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004606 int ret;
4607
4608 perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
4609 ret = perf_output_begin(&handle, event,
4610 comm_event->event_id.header.size, 0, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004611
4612 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004613 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004614
4615 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
4616 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
4617
4618 perf_output_put(&handle, comm_event->event_id);
4619 perf_output_copy(&handle, comm_event->comm,
4620 comm_event->comm_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004621
4622 perf_event__output_id_sample(event, &handle, &sample);
4623
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004624 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004625out:
4626 comm_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004627}
4628
4629static int perf_event_comm_match(struct perf_event *event)
4630{
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01004631 if (event->state < PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra22e19082010-01-18 09:12:32 +01004632 return 0;
4633
Stephane Eranian5632ab12011-01-03 18:20:01 +02004634 if (!event_filter_match(event))
Peter Zijlstra5d27c232009-12-17 13:16:32 +01004635 return 0;
4636
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004637 if (event->attr.comm)
4638 return 1;
4639
4640 return 0;
4641}
4642
4643static void perf_event_comm_ctx(struct perf_event_context *ctx,
4644 struct perf_comm_event *comm_event)
4645{
4646 struct perf_event *event;
4647
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004648 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4649 if (perf_event_comm_match(event))
4650 perf_event_comm_output(event, comm_event);
4651 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004652}
4653
4654static void perf_event_comm_event(struct perf_comm_event *comm_event)
4655{
4656 struct perf_cpu_context *cpuctx;
4657 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004658 char comm[TASK_COMM_LEN];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004659 unsigned int size;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004660 struct pmu *pmu;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004661 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004662
4663 memset(comm, 0, sizeof(comm));
Márton Németh96b02d72009-11-21 23:10:15 +01004664 strlcpy(comm, comm_event->task->comm, sizeof(comm));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004665 size = ALIGN(strlen(comm)+1, sizeof(u64));
4666
4667 comm_event->comm = comm;
4668 comm_event->comm_size = size;
4669
4670 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
Peter Zijlstraf6595f32009-11-20 22:19:47 +01004671 rcu_read_lock();
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004672 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra41945f62010-09-16 19:17:24 +02004673 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra51676952010-12-07 14:18:20 +01004674 if (cpuctx->active_pmu != pmu)
4675 goto next;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004676 perf_event_comm_ctx(&cpuctx->ctx, comm_event);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004677
4678 ctxn = pmu->task_ctx_nr;
4679 if (ctxn < 0)
Peter Zijlstra41945f62010-09-16 19:17:24 +02004680 goto next;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004681
4682 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4683 if (ctx)
4684 perf_event_comm_ctx(ctx, comm_event);
Peter Zijlstra41945f62010-09-16 19:17:24 +02004685next:
4686 put_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004687 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004688 rcu_read_unlock();
4689}
4690
4691void perf_event_comm(struct task_struct *task)
4692{
4693 struct perf_comm_event comm_event;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004694 struct perf_event_context *ctx;
4695 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004696
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004697 for_each_task_context_nr(ctxn) {
4698 ctx = task->perf_event_ctxp[ctxn];
4699 if (!ctx)
4700 continue;
4701
4702 perf_event_enable_on_exec(ctx);
4703 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004704
4705 if (!atomic_read(&nr_comm_events))
4706 return;
4707
4708 comm_event = (struct perf_comm_event){
4709 .task = task,
4710 /* .comm */
4711 /* .comm_size */
4712 .event_id = {
4713 .header = {
4714 .type = PERF_RECORD_COMM,
4715 .misc = 0,
4716 /* .size */
4717 },
4718 /* .pid */
4719 /* .tid */
4720 },
4721 };
4722
4723 perf_event_comm_event(&comm_event);
4724}
4725
4726/*
4727 * mmap tracking
4728 */
4729
4730struct perf_mmap_event {
4731 struct vm_area_struct *vma;
4732
4733 const char *file_name;
4734 int file_size;
4735
4736 struct {
4737 struct perf_event_header header;
4738
4739 u32 pid;
4740 u32 tid;
4741 u64 start;
4742 u64 len;
4743 u64 pgoff;
4744 } event_id;
4745};
4746
4747static void perf_event_mmap_output(struct perf_event *event,
4748 struct perf_mmap_event *mmap_event)
4749{
4750 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004751 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004752 int size = mmap_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004753 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004754
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004755 perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
4756 ret = perf_output_begin(&handle, event,
4757 mmap_event->event_id.header.size, 0, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004758 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004759 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004760
4761 mmap_event->event_id.pid = perf_event_pid(event, current);
4762 mmap_event->event_id.tid = perf_event_tid(event, current);
4763
4764 perf_output_put(&handle, mmap_event->event_id);
4765 perf_output_copy(&handle, mmap_event->file_name,
4766 mmap_event->file_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004767
4768 perf_event__output_id_sample(event, &handle, &sample);
4769
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004770 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004771out:
4772 mmap_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004773}
4774
4775static int perf_event_mmap_match(struct perf_event *event,
Eric B Munson3af9e852010-05-18 15:30:49 +01004776 struct perf_mmap_event *mmap_event,
4777 int executable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004778{
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01004779 if (event->state < PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra22e19082010-01-18 09:12:32 +01004780 return 0;
4781
Stephane Eranian5632ab12011-01-03 18:20:01 +02004782 if (!event_filter_match(event))
Peter Zijlstra5d27c232009-12-17 13:16:32 +01004783 return 0;
4784
Eric B Munson3af9e852010-05-18 15:30:49 +01004785 if ((!executable && event->attr.mmap_data) ||
4786 (executable && event->attr.mmap))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004787 return 1;
4788
4789 return 0;
4790}
4791
4792static void perf_event_mmap_ctx(struct perf_event_context *ctx,
Eric B Munson3af9e852010-05-18 15:30:49 +01004793 struct perf_mmap_event *mmap_event,
4794 int executable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004795{
4796 struct perf_event *event;
4797
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004798 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Eric B Munson3af9e852010-05-18 15:30:49 +01004799 if (perf_event_mmap_match(event, mmap_event, executable))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004800 perf_event_mmap_output(event, mmap_event);
4801 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004802}
4803
4804static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
4805{
4806 struct perf_cpu_context *cpuctx;
4807 struct perf_event_context *ctx;
4808 struct vm_area_struct *vma = mmap_event->vma;
4809 struct file *file = vma->vm_file;
4810 unsigned int size;
4811 char tmp[16];
4812 char *buf = NULL;
4813 const char *name;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004814 struct pmu *pmu;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004815 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004816
4817 memset(tmp, 0, sizeof(tmp));
4818
4819 if (file) {
4820 /*
4821 * d_path works from the end of the buffer backwards, so we
4822 * need to add enough zero bytes after the string to handle
4823 * the 64bit alignment we do later.
4824 */
4825 buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
4826 if (!buf) {
4827 name = strncpy(tmp, "//enomem", sizeof(tmp));
4828 goto got_name;
4829 }
4830 name = d_path(&file->f_path, buf, PATH_MAX);
4831 if (IS_ERR(name)) {
4832 name = strncpy(tmp, "//toolong", sizeof(tmp));
4833 goto got_name;
4834 }
4835 } else {
4836 if (arch_vma_name(mmap_event->vma)) {
4837 name = strncpy(tmp, arch_vma_name(mmap_event->vma),
4838 sizeof(tmp));
4839 goto got_name;
4840 }
4841
4842 if (!vma->vm_mm) {
4843 name = strncpy(tmp, "[vdso]", sizeof(tmp));
4844 goto got_name;
Eric B Munson3af9e852010-05-18 15:30:49 +01004845 } else if (vma->vm_start <= vma->vm_mm->start_brk &&
4846 vma->vm_end >= vma->vm_mm->brk) {
4847 name = strncpy(tmp, "[heap]", sizeof(tmp));
4848 goto got_name;
4849 } else if (vma->vm_start <= vma->vm_mm->start_stack &&
4850 vma->vm_end >= vma->vm_mm->start_stack) {
4851 name = strncpy(tmp, "[stack]", sizeof(tmp));
4852 goto got_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004853 }
4854
4855 name = strncpy(tmp, "//anon", sizeof(tmp));
4856 goto got_name;
4857 }
4858
4859got_name:
4860 size = ALIGN(strlen(name)+1, sizeof(u64));
4861
4862 mmap_event->file_name = name;
4863 mmap_event->file_size = size;
4864
4865 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
4866
Peter Zijlstraf6d9dd22009-11-20 22:19:48 +01004867 rcu_read_lock();
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004868 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra41945f62010-09-16 19:17:24 +02004869 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra51676952010-12-07 14:18:20 +01004870 if (cpuctx->active_pmu != pmu)
4871 goto next;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004872 perf_event_mmap_ctx(&cpuctx->ctx, mmap_event,
4873 vma->vm_flags & VM_EXEC);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004874
4875 ctxn = pmu->task_ctx_nr;
4876 if (ctxn < 0)
Peter Zijlstra41945f62010-09-16 19:17:24 +02004877 goto next;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004878
4879 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4880 if (ctx) {
4881 perf_event_mmap_ctx(ctx, mmap_event,
4882 vma->vm_flags & VM_EXEC);
4883 }
Peter Zijlstra41945f62010-09-16 19:17:24 +02004884next:
4885 put_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004886 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004887 rcu_read_unlock();
4888
4889 kfree(buf);
4890}
4891
Eric B Munson3af9e852010-05-18 15:30:49 +01004892void perf_event_mmap(struct vm_area_struct *vma)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004893{
4894 struct perf_mmap_event mmap_event;
4895
4896 if (!atomic_read(&nr_mmap_events))
4897 return;
4898
4899 mmap_event = (struct perf_mmap_event){
4900 .vma = vma,
4901 /* .file_name */
4902 /* .file_size */
4903 .event_id = {
4904 .header = {
4905 .type = PERF_RECORD_MMAP,
Zhang, Yanmin39447b32010-04-19 13:32:41 +08004906 .misc = PERF_RECORD_MISC_USER,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004907 /* .size */
4908 },
4909 /* .pid */
4910 /* .tid */
4911 .start = vma->vm_start,
4912 .len = vma->vm_end - vma->vm_start,
Peter Zijlstra3a0304e2010-02-26 10:33:41 +01004913 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004914 },
4915 };
4916
4917 perf_event_mmap_event(&mmap_event);
4918}
4919
4920/*
4921 * IRQ throttle logging
4922 */
4923
4924static void perf_log_throttle(struct perf_event *event, int enable)
4925{
4926 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004927 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004928 int ret;
4929
4930 struct {
4931 struct perf_event_header header;
4932 u64 time;
4933 u64 id;
4934 u64 stream_id;
4935 } throttle_event = {
4936 .header = {
4937 .type = PERF_RECORD_THROTTLE,
4938 .misc = 0,
4939 .size = sizeof(throttle_event),
4940 },
4941 .time = perf_clock(),
4942 .id = primary_event_id(event),
4943 .stream_id = event->id,
4944 };
4945
4946 if (enable)
4947 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
4948
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004949 perf_event_header__init_id(&throttle_event.header, &sample, event);
4950
4951 ret = perf_output_begin(&handle, event,
4952 throttle_event.header.size, 1, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004953 if (ret)
4954 return;
4955
4956 perf_output_put(&handle, throttle_event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004957 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004958 perf_output_end(&handle);
4959}
4960
4961/*
4962 * Generic event overflow handling, sampling.
4963 */
4964
4965static int __perf_event_overflow(struct perf_event *event, int nmi,
4966 int throttle, struct perf_sample_data *data,
4967 struct pt_regs *regs)
4968{
4969 int events = atomic_read(&event->event_limit);
4970 struct hw_perf_event *hwc = &event->hw;
4971 int ret = 0;
4972
Peter Zijlstra96398822010-11-24 18:55:29 +01004973 /*
4974 * Non-sampling counters might still use the PMI to fold short
4975 * hardware counters, ignore those.
4976 */
4977 if (unlikely(!is_sampling_event(event)))
4978 return 0;
4979
Peter Zijlstra163ec432011-02-16 11:22:34 +01004980 if (unlikely(hwc->interrupts >= max_samples_per_tick)) {
4981 if (throttle) {
4982 hwc->interrupts = MAX_INTERRUPTS;
4983 perf_log_throttle(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004984 ret = 1;
4985 }
Peter Zijlstra163ec432011-02-16 11:22:34 +01004986 } else
4987 hwc->interrupts++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004988
4989 if (event->attr.freq) {
4990 u64 now = perf_clock();
Peter Zijlstraabd50712010-01-26 18:50:16 +01004991 s64 delta = now - hwc->freq_time_stamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004992
Peter Zijlstraabd50712010-01-26 18:50:16 +01004993 hwc->freq_time_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004994
Peter Zijlstraabd50712010-01-26 18:50:16 +01004995 if (delta > 0 && delta < 2*TICK_NSEC)
4996 perf_adjust_period(event, delta, hwc->last_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004997 }
4998
4999 /*
5000 * XXX event_limit might not quite work as expected on inherited
5001 * events
5002 */
5003
5004 event->pending_kill = POLL_IN;
5005 if (events && atomic_dec_and_test(&event->event_limit)) {
5006 ret = 1;
5007 event->pending_kill = POLL_HUP;
5008 if (nmi) {
5009 event->pending_disable = 1;
Peter Zijlstrae360adb2010-10-14 14:01:34 +08005010 irq_work_queue(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005011 } else
5012 perf_event_disable(event);
5013 }
5014
Peter Zijlstra453f19e2009-11-20 22:19:43 +01005015 if (event->overflow_handler)
5016 event->overflow_handler(event, nmi, data, regs);
5017 else
5018 perf_event_output(event, nmi, data, regs);
5019
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005020 return ret;
5021}
5022
5023int perf_event_overflow(struct perf_event *event, int nmi,
5024 struct perf_sample_data *data,
5025 struct pt_regs *regs)
5026{
5027 return __perf_event_overflow(event, nmi, 1, data, regs);
5028}
5029
5030/*
5031 * Generic software event infrastructure
5032 */
5033
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005034struct swevent_htable {
5035 struct swevent_hlist *swevent_hlist;
5036 struct mutex hlist_mutex;
5037 int hlist_refcount;
5038
5039 /* Recursion avoidance in each contexts */
5040 int recursion[PERF_NR_CONTEXTS];
5041};
5042
5043static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
5044
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005045/*
5046 * We directly increment event->count and keep a second value in
5047 * event->hw.period_left to count intervals. This period event
5048 * is kept in the range [-sample_period, 0] so that we can use the
5049 * sign as trigger.
5050 */
5051
5052static u64 perf_swevent_set_period(struct perf_event *event)
5053{
5054 struct hw_perf_event *hwc = &event->hw;
5055 u64 period = hwc->last_period;
5056 u64 nr, offset;
5057 s64 old, val;
5058
5059 hwc->last_period = hwc->sample_period;
5060
5061again:
Peter Zijlstrae7850592010-05-21 14:43:08 +02005062 old = val = local64_read(&hwc->period_left);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005063 if (val < 0)
5064 return 0;
5065
5066 nr = div64_u64(period + val, period);
5067 offset = nr * period;
5068 val -= offset;
Peter Zijlstrae7850592010-05-21 14:43:08 +02005069 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005070 goto again;
5071
5072 return nr;
5073}
5074
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005075static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005076 int nmi, struct perf_sample_data *data,
5077 struct pt_regs *regs)
5078{
5079 struct hw_perf_event *hwc = &event->hw;
5080 int throttle = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005081
5082 data->period = event->hw.last_period;
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005083 if (!overflow)
5084 overflow = perf_swevent_set_period(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005085
5086 if (hwc->interrupts == MAX_INTERRUPTS)
5087 return;
5088
5089 for (; overflow; overflow--) {
5090 if (__perf_event_overflow(event, nmi, throttle,
5091 data, regs)) {
5092 /*
5093 * We inhibit the overflow from happening when
5094 * hwc->interrupts == MAX_INTERRUPTS.
5095 */
5096 break;
5097 }
5098 throttle = 1;
5099 }
5100}
5101
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005102static void perf_swevent_event(struct perf_event *event, u64 nr,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005103 int nmi, struct perf_sample_data *data,
5104 struct pt_regs *regs)
5105{
5106 struct hw_perf_event *hwc = &event->hw;
5107
Peter Zijlstrae7850592010-05-21 14:43:08 +02005108 local64_add(nr, &event->count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005109
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005110 if (!regs)
5111 return;
5112
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01005113 if (!is_sampling_event(event))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005114 return;
5115
5116 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
5117 return perf_swevent_overflow(event, 1, nmi, data, regs);
5118
Peter Zijlstrae7850592010-05-21 14:43:08 +02005119 if (local64_add_negative(nr, &hwc->period_left))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005120 return;
5121
5122 perf_swevent_overflow(event, 0, nmi, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005123}
5124
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005125static int perf_exclude_event(struct perf_event *event,
5126 struct pt_regs *regs)
5127{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005128 if (event->hw.state & PERF_HES_STOPPED)
5129 return 0;
5130
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005131 if (regs) {
5132 if (event->attr.exclude_user && user_mode(regs))
5133 return 1;
5134
5135 if (event->attr.exclude_kernel && !user_mode(regs))
5136 return 1;
5137 }
5138
5139 return 0;
5140}
5141
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005142static int perf_swevent_match(struct perf_event *event,
5143 enum perf_type_id type,
Li Zefan6fb29152009-10-15 11:21:42 +08005144 u32 event_id,
5145 struct perf_sample_data *data,
5146 struct pt_regs *regs)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005147{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005148 if (event->attr.type != type)
5149 return 0;
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005150
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005151 if (event->attr.config != event_id)
5152 return 0;
5153
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005154 if (perf_exclude_event(event, regs))
5155 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005156
5157 return 1;
5158}
5159
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005160static inline u64 swevent_hash(u64 type, u32 event_id)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005161{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005162 u64 val = event_id | (type << 32);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005163
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005164 return hash_64(val, SWEVENT_HLIST_BITS);
5165}
5166
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005167static inline struct hlist_head *
5168__find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005169{
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005170 u64 hash = swevent_hash(type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005171
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005172 return &hlist->heads[hash];
5173}
5174
5175/* For the read side: events when they trigger */
5176static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005177find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005178{
5179 struct swevent_hlist *hlist;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005180
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005181 hlist = rcu_dereference(swhash->swevent_hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005182 if (!hlist)
5183 return NULL;
5184
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005185 return __find_swevent_head(hlist, type, event_id);
5186}
5187
5188/* For the event head insertion and removal in the hlist */
5189static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005190find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005191{
5192 struct swevent_hlist *hlist;
5193 u32 event_id = event->attr.config;
5194 u64 type = event->attr.type;
5195
5196 /*
5197 * Event scheduling is always serialized against hlist allocation
5198 * and release. Which makes the protected version suitable here.
5199 * The context lock guarantees that.
5200 */
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005201 hlist = rcu_dereference_protected(swhash->swevent_hlist,
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005202 lockdep_is_held(&event->ctx->lock));
5203 if (!hlist)
5204 return NULL;
5205
5206 return __find_swevent_head(hlist, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005207}
5208
5209static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
5210 u64 nr, int nmi,
5211 struct perf_sample_data *data,
5212 struct pt_regs *regs)
5213{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005214 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005215 struct perf_event *event;
5216 struct hlist_node *node;
5217 struct hlist_head *head;
5218
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005219 rcu_read_lock();
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005220 head = find_swevent_head_rcu(swhash, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005221 if (!head)
5222 goto end;
5223
5224 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
Li Zefan6fb29152009-10-15 11:21:42 +08005225 if (perf_swevent_match(event, type, event_id, data, regs))
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005226 perf_swevent_event(event, nr, nmi, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005227 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005228end:
5229 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005230}
5231
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01005232int perf_swevent_get_recursion_context(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005233{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005234 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01005235
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005236 return get_recursion_context(swhash->recursion);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005237}
Ingo Molnar645e8cc2009-11-22 12:20:19 +01005238EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005239
Jesper Juhlfa9f90b2010-11-28 21:39:34 +01005240inline void perf_swevent_put_recursion_context(int rctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005241{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005242 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02005243
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005244 put_recursion_context(swhash->recursion, rctx);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01005245}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005246
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005247void __perf_sw_event(u32 event_id, u64 nr, int nmi,
5248 struct pt_regs *regs, u64 addr)
5249{
Ingo Molnara4234bf2009-11-23 10:57:59 +01005250 struct perf_sample_data data;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01005251 int rctx;
5252
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005253 preempt_disable_notrace();
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01005254 rctx = perf_swevent_get_recursion_context();
5255 if (rctx < 0)
5256 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005257
Peter Zijlstradc1d6282010-03-03 15:55:04 +01005258 perf_sample_data_init(&data, addr);
Ingo Molnara4234bf2009-11-23 10:57:59 +01005259
5260 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, nmi, &data, regs);
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01005261
5262 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005263 preempt_enable_notrace();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005264}
5265
5266static void perf_swevent_read(struct perf_event *event)
5267{
5268}
5269
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005270static int perf_swevent_add(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005271{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005272 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005273 struct hw_perf_event *hwc = &event->hw;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005274 struct hlist_head *head;
5275
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01005276 if (is_sampling_event(event)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005277 hwc->last_period = hwc->sample_period;
5278 perf_swevent_set_period(event);
5279 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005280
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005281 hwc->state = !(flags & PERF_EF_START);
5282
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005283 head = find_swevent_head(swhash, event);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005284 if (WARN_ON_ONCE(!head))
5285 return -EINVAL;
5286
5287 hlist_add_head_rcu(&event->hlist_entry, head);
5288
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005289 return 0;
5290}
5291
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005292static void perf_swevent_del(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005293{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005294 hlist_del_rcu(&event->hlist_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005295}
5296
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005297static void perf_swevent_start(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02005298{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005299 event->hw.state = 0;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02005300}
5301
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005302static void perf_swevent_stop(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02005303{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005304 event->hw.state = PERF_HES_STOPPED;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02005305}
5306
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005307/* Deref the hlist from the update side */
5308static inline struct swevent_hlist *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005309swevent_hlist_deref(struct swevent_htable *swhash)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005310{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005311 return rcu_dereference_protected(swhash->swevent_hlist,
5312 lockdep_is_held(&swhash->hlist_mutex));
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005313}
5314
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005315static void swevent_hlist_release_rcu(struct rcu_head *rcu_head)
5316{
5317 struct swevent_hlist *hlist;
5318
5319 hlist = container_of(rcu_head, struct swevent_hlist, rcu_head);
5320 kfree(hlist);
5321}
5322
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005323static void swevent_hlist_release(struct swevent_htable *swhash)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005324{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005325 struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005326
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005327 if (!hlist)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005328 return;
5329
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005330 rcu_assign_pointer(swhash->swevent_hlist, NULL);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005331 call_rcu(&hlist->rcu_head, swevent_hlist_release_rcu);
5332}
5333
5334static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
5335{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005336 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005337
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005338 mutex_lock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005339
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005340 if (!--swhash->hlist_refcount)
5341 swevent_hlist_release(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005342
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005343 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005344}
5345
5346static void swevent_hlist_put(struct perf_event *event)
5347{
5348 int cpu;
5349
5350 if (event->cpu != -1) {
5351 swevent_hlist_put_cpu(event, event->cpu);
5352 return;
5353 }
5354
5355 for_each_possible_cpu(cpu)
5356 swevent_hlist_put_cpu(event, cpu);
5357}
5358
5359static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
5360{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005361 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005362 int err = 0;
5363
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005364 mutex_lock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005365
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005366 if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005367 struct swevent_hlist *hlist;
5368
5369 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
5370 if (!hlist) {
5371 err = -ENOMEM;
5372 goto exit;
5373 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005374 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005375 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005376 swhash->hlist_refcount++;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02005377exit:
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005378 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005379
5380 return err;
5381}
5382
5383static int swevent_hlist_get(struct perf_event *event)
5384{
5385 int err;
5386 int cpu, failed_cpu;
5387
5388 if (event->cpu != -1)
5389 return swevent_hlist_get_cpu(event, event->cpu);
5390
5391 get_online_cpus();
5392 for_each_possible_cpu(cpu) {
5393 err = swevent_hlist_get_cpu(event, cpu);
5394 if (err) {
5395 failed_cpu = cpu;
5396 goto fail;
5397 }
5398 }
5399 put_online_cpus();
5400
5401 return 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02005402fail:
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005403 for_each_possible_cpu(cpu) {
5404 if (cpu == failed_cpu)
5405 break;
5406 swevent_hlist_put_cpu(event, cpu);
5407 }
5408
5409 put_online_cpus();
5410 return err;
5411}
5412
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005413atomic_t perf_swevent_enabled[PERF_COUNT_SW_MAX];
Frederic Weisbecker95476b62010-04-14 23:42:18 +02005414
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005415static void sw_perf_event_destroy(struct perf_event *event)
5416{
5417 u64 event_id = event->attr.config;
5418
5419 WARN_ON(event->parent);
5420
Peter Zijlstra7e54a5a2010-10-14 22:32:45 +02005421 jump_label_dec(&perf_swevent_enabled[event_id]);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005422 swevent_hlist_put(event);
5423}
5424
5425static int perf_swevent_init(struct perf_event *event)
5426{
5427 int event_id = event->attr.config;
5428
5429 if (event->attr.type != PERF_TYPE_SOFTWARE)
5430 return -ENOENT;
5431
5432 switch (event_id) {
5433 case PERF_COUNT_SW_CPU_CLOCK:
5434 case PERF_COUNT_SW_TASK_CLOCK:
5435 return -ENOENT;
5436
5437 default:
5438 break;
5439 }
5440
Dan Carpenterce677832010-10-24 21:50:42 +02005441 if (event_id >= PERF_COUNT_SW_MAX)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005442 return -ENOENT;
5443
5444 if (!event->parent) {
5445 int err;
5446
5447 err = swevent_hlist_get(event);
5448 if (err)
5449 return err;
5450
Peter Zijlstra7e54a5a2010-10-14 22:32:45 +02005451 jump_label_inc(&perf_swevent_enabled[event_id]);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005452 event->destroy = sw_perf_event_destroy;
5453 }
5454
5455 return 0;
5456}
5457
5458static struct pmu perf_swevent = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005459 .task_ctx_nr = perf_sw_context,
5460
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005461 .event_init = perf_swevent_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005462 .add = perf_swevent_add,
5463 .del = perf_swevent_del,
5464 .start = perf_swevent_start,
5465 .stop = perf_swevent_stop,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005466 .read = perf_swevent_read,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005467};
Frederic Weisbecker95476b62010-04-14 23:42:18 +02005468
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005469#ifdef CONFIG_EVENT_TRACING
5470
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005471static int perf_tp_filter_match(struct perf_event *event,
Frederic Weisbecker95476b62010-04-14 23:42:18 +02005472 struct perf_sample_data *data)
5473{
5474 void *record = data->raw->data;
5475
5476 if (likely(!event->filter) || filter_match_preds(event->filter, record))
5477 return 1;
5478 return 0;
5479}
5480
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005481static int perf_tp_event_match(struct perf_event *event,
5482 struct perf_sample_data *data,
5483 struct pt_regs *regs)
5484{
Peter Zijlstra580d6072010-05-20 20:54:31 +02005485 /*
5486 * All tracepoints are from kernel-space.
5487 */
5488 if (event->attr.exclude_kernel)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005489 return 0;
5490
5491 if (!perf_tp_filter_match(event, data))
5492 return 0;
5493
5494 return 1;
5495}
5496
5497void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
Peter Zijlstraecc55f82010-05-21 15:11:34 +02005498 struct pt_regs *regs, struct hlist_head *head, int rctx)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005499{
5500 struct perf_sample_data data;
5501 struct perf_event *event;
5502 struct hlist_node *node;
5503
5504 struct perf_raw_record raw = {
5505 .size = entry_size,
5506 .data = record,
5507 };
5508
5509 perf_sample_data_init(&data, addr);
5510 data.raw = &raw;
5511
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005512 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
5513 if (perf_tp_event_match(event, &data, regs))
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005514 perf_swevent_event(event, count, 1, &data, regs);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005515 }
Peter Zijlstraecc55f82010-05-21 15:11:34 +02005516
5517 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005518}
5519EXPORT_SYMBOL_GPL(perf_tp_event);
5520
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005521static void tp_perf_event_destroy(struct perf_event *event)
5522{
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005523 perf_trace_destroy(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005524}
5525
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005526static int perf_tp_event_init(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005527{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005528 int err;
5529
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005530 if (event->attr.type != PERF_TYPE_TRACEPOINT)
5531 return -ENOENT;
5532
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005533 err = perf_trace_init(event);
5534 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005535 return err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005536
5537 event->destroy = tp_perf_event_destroy;
5538
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005539 return 0;
5540}
5541
5542static struct pmu perf_tracepoint = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005543 .task_ctx_nr = perf_sw_context,
5544
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005545 .event_init = perf_tp_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005546 .add = perf_trace_add,
5547 .del = perf_trace_del,
5548 .start = perf_swevent_start,
5549 .stop = perf_swevent_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005550 .read = perf_swevent_read,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005551};
5552
5553static inline void perf_tp_register(void)
5554{
Peter Zijlstra2e80a822010-11-17 23:17:36 +01005555 perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005556}
Li Zefan6fb29152009-10-15 11:21:42 +08005557
5558static int perf_event_set_filter(struct perf_event *event, void __user *arg)
5559{
5560 char *filter_str;
5561 int ret;
5562
5563 if (event->attr.type != PERF_TYPE_TRACEPOINT)
5564 return -EINVAL;
5565
5566 filter_str = strndup_user(arg, PAGE_SIZE);
5567 if (IS_ERR(filter_str))
5568 return PTR_ERR(filter_str);
5569
5570 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
5571
5572 kfree(filter_str);
5573 return ret;
5574}
5575
5576static void perf_event_free_filter(struct perf_event *event)
5577{
5578 ftrace_profile_free_filter(event);
5579}
5580
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005581#else
Li Zefan6fb29152009-10-15 11:21:42 +08005582
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005583static inline void perf_tp_register(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005584{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005585}
Li Zefan6fb29152009-10-15 11:21:42 +08005586
5587static int perf_event_set_filter(struct perf_event *event, void __user *arg)
5588{
5589 return -ENOENT;
5590}
5591
5592static void perf_event_free_filter(struct perf_event *event)
5593{
5594}
5595
Li Zefan07b139c2009-12-21 14:27:35 +08005596#endif /* CONFIG_EVENT_TRACING */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005597
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02005598#ifdef CONFIG_HAVE_HW_BREAKPOINT
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005599void perf_bp_event(struct perf_event *bp, void *data)
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02005600{
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005601 struct perf_sample_data sample;
5602 struct pt_regs *regs = data;
5603
Peter Zijlstradc1d6282010-03-03 15:55:04 +01005604 perf_sample_data_init(&sample, bp->attr.bp_addr);
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005605
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005606 if (!bp->hw.state && !perf_exclude_event(bp, regs))
5607 perf_swevent_event(bp, 1, 1, &sample, regs);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02005608}
5609#endif
5610
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005611/*
5612 * hrtimer based swevent callback
5613 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005614
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005615static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005616{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005617 enum hrtimer_restart ret = HRTIMER_RESTART;
5618 struct perf_sample_data data;
5619 struct pt_regs *regs;
5620 struct perf_event *event;
5621 u64 period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005622
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005623 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
Peter Zijlstraba3dd362011-02-15 12:41:46 +01005624
5625 if (event->state != PERF_EVENT_STATE_ACTIVE)
5626 return HRTIMER_NORESTART;
5627
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005628 event->pmu->read(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005629
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005630 perf_sample_data_init(&data, 0);
5631 data.period = event->hw.last_period;
5632 regs = get_irq_regs();
5633
5634 if (regs && !perf_exclude_event(event, regs)) {
5635 if (!(event->attr.exclude_idle && current->pid == 0))
5636 if (perf_event_overflow(event, 0, &data, regs))
5637 ret = HRTIMER_NORESTART;
5638 }
5639
5640 period = max_t(u64, 10000, event->hw.sample_period);
5641 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
5642
5643 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005644}
5645
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005646static void perf_swevent_start_hrtimer(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005647{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005648 struct hw_perf_event *hwc = &event->hw;
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01005649 s64 period;
5650
5651 if (!is_sampling_event(event))
5652 return;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005653
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01005654 period = local64_read(&hwc->period_left);
5655 if (period) {
5656 if (period < 0)
5657 period = 10000;
Peter Zijlstrafa407f32010-06-24 12:35:12 +02005658
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01005659 local64_set(&hwc->period_left, 0);
5660 } else {
5661 period = max_t(u64, 10000, hwc->sample_period);
5662 }
5663 __hrtimer_start_range_ns(&hwc->hrtimer,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005664 ns_to_ktime(period), 0,
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02005665 HRTIMER_MODE_REL_PINNED, 0);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005666}
5667
5668static void perf_swevent_cancel_hrtimer(struct perf_event *event)
5669{
5670 struct hw_perf_event *hwc = &event->hw;
5671
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01005672 if (is_sampling_event(event)) {
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005673 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
Peter Zijlstrafa407f32010-06-24 12:35:12 +02005674 local64_set(&hwc->period_left, ktime_to_ns(remaining));
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005675
5676 hrtimer_cancel(&hwc->hrtimer);
5677 }
5678}
5679
Peter Zijlstraba3dd362011-02-15 12:41:46 +01005680static void perf_swevent_init_hrtimer(struct perf_event *event)
5681{
5682 struct hw_perf_event *hwc = &event->hw;
5683
5684 if (!is_sampling_event(event))
5685 return;
5686
5687 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
5688 hwc->hrtimer.function = perf_swevent_hrtimer;
5689
5690 /*
5691 * Since hrtimers have a fixed rate, we can do a static freq->period
5692 * mapping and avoid the whole period adjust feedback stuff.
5693 */
5694 if (event->attr.freq) {
5695 long freq = event->attr.sample_freq;
5696
5697 event->attr.sample_period = NSEC_PER_SEC / freq;
5698 hwc->sample_period = event->attr.sample_period;
5699 local64_set(&hwc->period_left, hwc->sample_period);
5700 event->attr.freq = 0;
5701 }
5702}
5703
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005704/*
5705 * Software event: cpu wall time clock
5706 */
5707
5708static void cpu_clock_event_update(struct perf_event *event)
5709{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005710 s64 prev;
5711 u64 now;
5712
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005713 now = local_clock();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005714 prev = local64_xchg(&event->hw.prev_count, now);
5715 local64_add(now - prev, &event->count);
5716}
5717
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005718static void cpu_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005719{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005720 local64_set(&event->hw.prev_count, local_clock());
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005721 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005722}
5723
5724static void cpu_clock_event_stop(struct perf_event *event, int flags)
5725{
5726 perf_swevent_cancel_hrtimer(event);
5727 cpu_clock_event_update(event);
5728}
5729
5730static int cpu_clock_event_add(struct perf_event *event, int flags)
5731{
5732 if (flags & PERF_EF_START)
5733 cpu_clock_event_start(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005734
5735 return 0;
5736}
5737
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005738static void cpu_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005739{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005740 cpu_clock_event_stop(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005741}
5742
5743static void cpu_clock_event_read(struct perf_event *event)
5744{
5745 cpu_clock_event_update(event);
5746}
5747
5748static int cpu_clock_event_init(struct perf_event *event)
5749{
5750 if (event->attr.type != PERF_TYPE_SOFTWARE)
5751 return -ENOENT;
5752
5753 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
5754 return -ENOENT;
5755
Peter Zijlstraba3dd362011-02-15 12:41:46 +01005756 perf_swevent_init_hrtimer(event);
5757
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005758 return 0;
5759}
5760
5761static struct pmu perf_cpu_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005762 .task_ctx_nr = perf_sw_context,
5763
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005764 .event_init = cpu_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005765 .add = cpu_clock_event_add,
5766 .del = cpu_clock_event_del,
5767 .start = cpu_clock_event_start,
5768 .stop = cpu_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005769 .read = cpu_clock_event_read,
5770};
5771
5772/*
5773 * Software event: task time clock
5774 */
5775
5776static void task_clock_event_update(struct perf_event *event, u64 now)
5777{
5778 u64 prev;
5779 s64 delta;
5780
5781 prev = local64_xchg(&event->hw.prev_count, now);
5782 delta = now - prev;
5783 local64_add(delta, &event->count);
5784}
5785
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005786static void task_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005787{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005788 local64_set(&event->hw.prev_count, event->ctx->time);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005789 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005790}
5791
5792static void task_clock_event_stop(struct perf_event *event, int flags)
5793{
5794 perf_swevent_cancel_hrtimer(event);
5795 task_clock_event_update(event, event->ctx->time);
5796}
5797
5798static int task_clock_event_add(struct perf_event *event, int flags)
5799{
5800 if (flags & PERF_EF_START)
5801 task_clock_event_start(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005802
5803 return 0;
5804}
5805
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005806static void task_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005807{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005808 task_clock_event_stop(event, PERF_EF_UPDATE);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005809}
5810
5811static void task_clock_event_read(struct perf_event *event)
5812{
5813 u64 time;
5814
5815 if (!in_nmi()) {
5816 update_context_time(event->ctx);
5817 time = event->ctx->time;
5818 } else {
5819 u64 now = perf_clock();
5820 u64 delta = now - event->ctx->timestamp;
5821 time = event->ctx->time + delta;
5822 }
5823
5824 task_clock_event_update(event, time);
5825}
5826
5827static int task_clock_event_init(struct perf_event *event)
5828{
5829 if (event->attr.type != PERF_TYPE_SOFTWARE)
5830 return -ENOENT;
5831
5832 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
5833 return -ENOENT;
5834
Peter Zijlstraba3dd362011-02-15 12:41:46 +01005835 perf_swevent_init_hrtimer(event);
5836
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005837 return 0;
5838}
5839
5840static struct pmu perf_task_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005841 .task_ctx_nr = perf_sw_context,
5842
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005843 .event_init = task_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005844 .add = task_clock_event_add,
5845 .del = task_clock_event_del,
5846 .start = task_clock_event_start,
5847 .stop = task_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005848 .read = task_clock_event_read,
5849};
5850
Peter Zijlstraad5133b2010-06-15 12:22:39 +02005851static void perf_pmu_nop_void(struct pmu *pmu)
5852{
5853}
5854
5855static int perf_pmu_nop_int(struct pmu *pmu)
5856{
5857 return 0;
5858}
5859
5860static void perf_pmu_start_txn(struct pmu *pmu)
5861{
5862 perf_pmu_disable(pmu);
5863}
5864
5865static int perf_pmu_commit_txn(struct pmu *pmu)
5866{
5867 perf_pmu_enable(pmu);
5868 return 0;
5869}
5870
5871static void perf_pmu_cancel_txn(struct pmu *pmu)
5872{
5873 perf_pmu_enable(pmu);
5874}
5875
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005876/*
5877 * Ensures all contexts with the same task_ctx_nr have the same
5878 * pmu_cpu_context too.
5879 */
5880static void *find_pmu_context(int ctxn)
5881{
5882 struct pmu *pmu;
5883
5884 if (ctxn < 0)
5885 return NULL;
5886
5887 list_for_each_entry(pmu, &pmus, entry) {
5888 if (pmu->task_ctx_nr == ctxn)
5889 return pmu->pmu_cpu_context;
5890 }
5891
5892 return NULL;
5893}
5894
Peter Zijlstra51676952010-12-07 14:18:20 +01005895static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005896{
Peter Zijlstra51676952010-12-07 14:18:20 +01005897 int cpu;
5898
5899 for_each_possible_cpu(cpu) {
5900 struct perf_cpu_context *cpuctx;
5901
5902 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
5903
5904 if (cpuctx->active_pmu == old_pmu)
5905 cpuctx->active_pmu = pmu;
5906 }
5907}
5908
5909static void free_pmu_context(struct pmu *pmu)
5910{
5911 struct pmu *i;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005912
5913 mutex_lock(&pmus_lock);
5914 /*
5915 * Like a real lame refcount.
5916 */
Peter Zijlstra51676952010-12-07 14:18:20 +01005917 list_for_each_entry(i, &pmus, entry) {
5918 if (i->pmu_cpu_context == pmu->pmu_cpu_context) {
5919 update_pmu_context(i, pmu);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005920 goto out;
Peter Zijlstra51676952010-12-07 14:18:20 +01005921 }
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005922 }
5923
Peter Zijlstra51676952010-12-07 14:18:20 +01005924 free_percpu(pmu->pmu_cpu_context);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005925out:
5926 mutex_unlock(&pmus_lock);
5927}
Peter Zijlstra2e80a822010-11-17 23:17:36 +01005928static struct idr pmu_idr;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005929
Peter Zijlstraabe43402010-11-17 23:17:37 +01005930static ssize_t
5931type_show(struct device *dev, struct device_attribute *attr, char *page)
5932{
5933 struct pmu *pmu = dev_get_drvdata(dev);
5934
5935 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type);
5936}
5937
5938static struct device_attribute pmu_dev_attrs[] = {
5939 __ATTR_RO(type),
5940 __ATTR_NULL,
5941};
5942
5943static int pmu_bus_running;
5944static struct bus_type pmu_bus = {
5945 .name = "event_source",
5946 .dev_attrs = pmu_dev_attrs,
5947};
5948
5949static void pmu_dev_release(struct device *dev)
5950{
5951 kfree(dev);
5952}
5953
5954static int pmu_dev_alloc(struct pmu *pmu)
5955{
5956 int ret = -ENOMEM;
5957
5958 pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
5959 if (!pmu->dev)
5960 goto out;
5961
5962 device_initialize(pmu->dev);
5963 ret = dev_set_name(pmu->dev, "%s", pmu->name);
5964 if (ret)
5965 goto free_dev;
5966
5967 dev_set_drvdata(pmu->dev, pmu);
5968 pmu->dev->bus = &pmu_bus;
5969 pmu->dev->release = pmu_dev_release;
5970 ret = device_add(pmu->dev);
5971 if (ret)
5972 goto free_dev;
5973
5974out:
5975 return ret;
5976
5977free_dev:
5978 put_device(pmu->dev);
5979 goto out;
5980}
5981
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01005982static struct lock_class_key cpuctx_mutex;
5983
Peter Zijlstra2e80a822010-11-17 23:17:36 +01005984int perf_pmu_register(struct pmu *pmu, char *name, int type)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005985{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005986 int cpu, ret;
Peter Zijlstra33696fc2010-06-14 08:49:00 +02005987
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005988 mutex_lock(&pmus_lock);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02005989 ret = -ENOMEM;
5990 pmu->pmu_disable_count = alloc_percpu(int);
5991 if (!pmu->pmu_disable_count)
5992 goto unlock;
Peter Zijlstraad5133b2010-06-15 12:22:39 +02005993
Peter Zijlstra2e80a822010-11-17 23:17:36 +01005994 pmu->type = -1;
5995 if (!name)
5996 goto skip_type;
5997 pmu->name = name;
5998
5999 if (type < 0) {
6000 int err = idr_pre_get(&pmu_idr, GFP_KERNEL);
6001 if (!err)
6002 goto free_pdc;
6003
6004 err = idr_get_new_above(&pmu_idr, pmu, PERF_TYPE_MAX, &type);
6005 if (err) {
6006 ret = err;
6007 goto free_pdc;
6008 }
6009 }
6010 pmu->type = type;
6011
Peter Zijlstraabe43402010-11-17 23:17:37 +01006012 if (pmu_bus_running) {
6013 ret = pmu_dev_alloc(pmu);
6014 if (ret)
6015 goto free_idr;
6016 }
6017
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006018skip_type:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006019 pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
6020 if (pmu->pmu_cpu_context)
6021 goto got_cpu_context;
6022
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006023 pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
6024 if (!pmu->pmu_cpu_context)
Peter Zijlstraabe43402010-11-17 23:17:37 +01006025 goto free_dev;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006026
6027 for_each_possible_cpu(cpu) {
6028 struct perf_cpu_context *cpuctx;
6029
6030 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Peter Zijlstraeb184472010-09-07 15:55:13 +02006031 __perf_event_init_context(&cpuctx->ctx);
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01006032 lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006033 cpuctx->ctx.type = cpu_context;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006034 cpuctx->ctx.pmu = pmu;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02006035 cpuctx->jiffies_interval = 1;
6036 INIT_LIST_HEAD(&cpuctx->rotation_list);
Peter Zijlstra51676952010-12-07 14:18:20 +01006037 cpuctx->active_pmu = pmu;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006038 }
6039
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006040got_cpu_context:
Peter Zijlstraad5133b2010-06-15 12:22:39 +02006041 if (!pmu->start_txn) {
6042 if (pmu->pmu_enable) {
6043 /*
6044 * If we have pmu_enable/pmu_disable calls, install
6045 * transaction stubs that use that to try and batch
6046 * hardware accesses.
6047 */
6048 pmu->start_txn = perf_pmu_start_txn;
6049 pmu->commit_txn = perf_pmu_commit_txn;
6050 pmu->cancel_txn = perf_pmu_cancel_txn;
6051 } else {
6052 pmu->start_txn = perf_pmu_nop_void;
6053 pmu->commit_txn = perf_pmu_nop_int;
6054 pmu->cancel_txn = perf_pmu_nop_void;
6055 }
6056 }
6057
6058 if (!pmu->pmu_enable) {
6059 pmu->pmu_enable = perf_pmu_nop_void;
6060 pmu->pmu_disable = perf_pmu_nop_void;
6061 }
6062
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006063 list_add_rcu(&pmu->entry, &pmus);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02006064 ret = 0;
6065unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006066 mutex_unlock(&pmus_lock);
6067
Peter Zijlstra33696fc2010-06-14 08:49:00 +02006068 return ret;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006069
Peter Zijlstraabe43402010-11-17 23:17:37 +01006070free_dev:
6071 device_del(pmu->dev);
6072 put_device(pmu->dev);
6073
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006074free_idr:
6075 if (pmu->type >= PERF_TYPE_MAX)
6076 idr_remove(&pmu_idr, pmu->type);
6077
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006078free_pdc:
6079 free_percpu(pmu->pmu_disable_count);
6080 goto unlock;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006081}
6082
6083void perf_pmu_unregister(struct pmu *pmu)
6084{
6085 mutex_lock(&pmus_lock);
6086 list_del_rcu(&pmu->entry);
6087 mutex_unlock(&pmus_lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006088
6089 /*
Peter Zijlstracde8e882010-09-13 11:06:55 +02006090 * We dereference the pmu list under both SRCU and regular RCU, so
6091 * synchronize against both of those.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006092 */
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006093 synchronize_srcu(&pmus_srcu);
Peter Zijlstracde8e882010-09-13 11:06:55 +02006094 synchronize_rcu();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006095
Peter Zijlstra33696fc2010-06-14 08:49:00 +02006096 free_percpu(pmu->pmu_disable_count);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006097 if (pmu->type >= PERF_TYPE_MAX)
6098 idr_remove(&pmu_idr, pmu->type);
Peter Zijlstraabe43402010-11-17 23:17:37 +01006099 device_del(pmu->dev);
6100 put_device(pmu->dev);
Peter Zijlstra51676952010-12-07 14:18:20 +01006101 free_pmu_context(pmu);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006102}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006103
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006104struct pmu *perf_init_event(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006105{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02006106 struct pmu *pmu = NULL;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006107 int idx;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006108
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006109 idx = srcu_read_lock(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006110
6111 rcu_read_lock();
6112 pmu = idr_find(&pmu_idr, event->attr.type);
6113 rcu_read_unlock();
6114 if (pmu)
6115 goto unlock;
6116
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006117 list_for_each_entry_rcu(pmu, &pmus, entry) {
6118 int ret = pmu->event_init(event);
6119 if (!ret)
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02006120 goto unlock;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006121
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006122 if (ret != -ENOENT) {
6123 pmu = ERR_PTR(ret);
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02006124 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006125 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006126 }
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02006127 pmu = ERR_PTR(-ENOENT);
6128unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006129 srcu_read_unlock(&pmus_srcu, idx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006130
6131 return pmu;
6132}
6133
6134/*
6135 * Allocate and initialize a event structure
6136 */
6137static struct perf_event *
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006138perf_event_alloc(struct perf_event_attr *attr, int cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +02006139 struct task_struct *task,
6140 struct perf_event *group_leader,
6141 struct perf_event *parent_event,
6142 perf_overflow_handler_t overflow_handler)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006143{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02006144 struct pmu *pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006145 struct perf_event *event;
6146 struct hw_perf_event *hwc;
6147 long err;
6148
Oleg Nesterov66832eb2011-01-18 17:10:32 +01006149 if ((unsigned)cpu >= nr_cpu_ids) {
6150 if (!task || cpu != -1)
6151 return ERR_PTR(-EINVAL);
6152 }
6153
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006154 event = kzalloc(sizeof(*event), GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006155 if (!event)
6156 return ERR_PTR(-ENOMEM);
6157
6158 /*
6159 * Single events are their own group leaders, with an
6160 * empty sibling list:
6161 */
6162 if (!group_leader)
6163 group_leader = event;
6164
6165 mutex_init(&event->child_mutex);
6166 INIT_LIST_HEAD(&event->child_list);
6167
6168 INIT_LIST_HEAD(&event->group_entry);
6169 INIT_LIST_HEAD(&event->event_entry);
6170 INIT_LIST_HEAD(&event->sibling_list);
6171 init_waitqueue_head(&event->waitq);
Peter Zijlstrae360adb2010-10-14 14:01:34 +08006172 init_irq_work(&event->pending, perf_pending_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006173
6174 mutex_init(&event->mmap_mutex);
6175
6176 event->cpu = cpu;
6177 event->attr = *attr;
6178 event->group_leader = group_leader;
6179 event->pmu = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006180 event->oncpu = -1;
6181
6182 event->parent = parent_event;
6183
6184 event->ns = get_pid_ns(current->nsproxy->pid_ns);
6185 event->id = atomic64_inc_return(&perf_event_id);
6186
6187 event->state = PERF_EVENT_STATE_INACTIVE;
6188
Peter Zijlstrad580ff82010-10-14 17:43:23 +02006189 if (task) {
6190 event->attach_state = PERF_ATTACH_TASK;
6191#ifdef CONFIG_HAVE_HW_BREAKPOINT
6192 /*
6193 * hw_breakpoint is a bit difficult here..
6194 */
6195 if (attr->type == PERF_TYPE_BREAKPOINT)
6196 event->hw.bp_target = task;
6197#endif
6198 }
6199
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01006200 if (!overflow_handler && parent_event)
6201 overflow_handler = parent_event->overflow_handler;
Oleg Nesterov66832eb2011-01-18 17:10:32 +01006202
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01006203 event->overflow_handler = overflow_handler;
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02006204
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006205 if (attr->disabled)
6206 event->state = PERF_EVENT_STATE_OFF;
6207
6208 pmu = NULL;
6209
6210 hwc = &event->hw;
6211 hwc->sample_period = attr->sample_period;
6212 if (attr->freq && attr->sample_freq)
6213 hwc->sample_period = 1;
6214 hwc->last_period = hwc->sample_period;
6215
Peter Zijlstrae7850592010-05-21 14:43:08 +02006216 local64_set(&hwc->period_left, hwc->sample_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006217
6218 /*
6219 * we currently do not support PERF_FORMAT_GROUP on inherited events
6220 */
6221 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
6222 goto done;
6223
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006224 pmu = perf_init_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006225
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006226done:
6227 err = 0;
6228 if (!pmu)
6229 err = -EINVAL;
6230 else if (IS_ERR(pmu))
6231 err = PTR_ERR(pmu);
6232
6233 if (err) {
6234 if (event->ns)
6235 put_pid_ns(event->ns);
6236 kfree(event);
6237 return ERR_PTR(err);
6238 }
6239
6240 event->pmu = pmu;
6241
6242 if (!event->parent) {
Peter Zijlstra82cd6de2010-10-14 17:57:23 +02006243 if (event->attach_state & PERF_ATTACH_TASK)
Stephane Eraniane5d13672011-02-14 11:20:01 +02006244 jump_label_inc(&perf_sched_events);
Eric B Munson3af9e852010-05-18 15:30:49 +01006245 if (event->attr.mmap || event->attr.mmap_data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006246 atomic_inc(&nr_mmap_events);
6247 if (event->attr.comm)
6248 atomic_inc(&nr_comm_events);
6249 if (event->attr.task)
6250 atomic_inc(&nr_task_events);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02006251 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
6252 err = get_callchain_buffers();
6253 if (err) {
6254 free_event(event);
6255 return ERR_PTR(err);
6256 }
6257 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006258 }
6259
6260 return event;
6261}
6262
6263static int perf_copy_attr(struct perf_event_attr __user *uattr,
6264 struct perf_event_attr *attr)
6265{
6266 u32 size;
6267 int ret;
6268
6269 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
6270 return -EFAULT;
6271
6272 /*
6273 * zero the full structure, so that a short copy will be nice.
6274 */
6275 memset(attr, 0, sizeof(*attr));
6276
6277 ret = get_user(size, &uattr->size);
6278 if (ret)
6279 return ret;
6280
6281 if (size > PAGE_SIZE) /* silly large */
6282 goto err_size;
6283
6284 if (!size) /* abi compat */
6285 size = PERF_ATTR_SIZE_VER0;
6286
6287 if (size < PERF_ATTR_SIZE_VER0)
6288 goto err_size;
6289
6290 /*
6291 * If we're handed a bigger struct than we know of,
6292 * ensure all the unknown bits are 0 - i.e. new
6293 * user-space does not rely on any kernel feature
6294 * extensions we dont know about yet.
6295 */
6296 if (size > sizeof(*attr)) {
6297 unsigned char __user *addr;
6298 unsigned char __user *end;
6299 unsigned char val;
6300
6301 addr = (void __user *)uattr + sizeof(*attr);
6302 end = (void __user *)uattr + size;
6303
6304 for (; addr < end; addr++) {
6305 ret = get_user(val, addr);
6306 if (ret)
6307 return ret;
6308 if (val)
6309 goto err_size;
6310 }
6311 size = sizeof(*attr);
6312 }
6313
6314 ret = copy_from_user(attr, uattr, size);
6315 if (ret)
6316 return -EFAULT;
6317
6318 /*
6319 * If the type exists, the corresponding creation will verify
6320 * the attr->config.
6321 */
6322 if (attr->type >= PERF_TYPE_MAX)
6323 return -EINVAL;
6324
Mahesh Salgaonkarcd757642010-01-30 10:25:18 +05306325 if (attr->__reserved_1)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006326 return -EINVAL;
6327
6328 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
6329 return -EINVAL;
6330
6331 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
6332 return -EINVAL;
6333
6334out:
6335 return ret;
6336
6337err_size:
6338 put_user(sizeof(*attr), &uattr->size);
6339 ret = -E2BIG;
6340 goto out;
6341}
6342
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006343static int
6344perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006345{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02006346 struct perf_buffer *buffer = NULL, *old_buffer = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006347 int ret = -EINVAL;
6348
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006349 if (!output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006350 goto set;
6351
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006352 /* don't allow circular references */
6353 if (event == output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006354 goto out;
6355
Peter Zijlstra0f139302010-05-20 14:35:15 +02006356 /*
6357 * Don't allow cross-cpu buffers
6358 */
6359 if (output_event->cpu != event->cpu)
6360 goto out;
6361
6362 /*
6363 * If its not a per-cpu buffer, it must be the same task.
6364 */
6365 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
6366 goto out;
6367
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006368set:
6369 mutex_lock(&event->mmap_mutex);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006370 /* Can't redirect output if we've got an active mmap() */
6371 if (atomic_read(&event->mmap_count))
6372 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006373
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006374 if (output_event) {
6375 /* get the buffer we want to redirect to */
Peter Zijlstraca5135e2010-05-28 19:33:23 +02006376 buffer = perf_buffer_get(output_event);
6377 if (!buffer)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006378 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006379 }
6380
Peter Zijlstraca5135e2010-05-28 19:33:23 +02006381 old_buffer = event->buffer;
6382 rcu_assign_pointer(event->buffer, buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006383 ret = 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006384unlock:
6385 mutex_unlock(&event->mmap_mutex);
6386
Peter Zijlstraca5135e2010-05-28 19:33:23 +02006387 if (old_buffer)
6388 perf_buffer_put(old_buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006389out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006390 return ret;
6391}
6392
6393/**
6394 * sys_perf_event_open - open a performance event, associate it to a task/cpu
6395 *
6396 * @attr_uptr: event_id type attributes for monitoring/sampling
6397 * @pid: target pid
6398 * @cpu: target cpu
6399 * @group_fd: group leader event fd
6400 */
6401SYSCALL_DEFINE5(perf_event_open,
6402 struct perf_event_attr __user *, attr_uptr,
6403 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
6404{
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006405 struct perf_event *group_leader = NULL, *output_event = NULL;
6406 struct perf_event *event, *sibling;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006407 struct perf_event_attr attr;
6408 struct perf_event_context *ctx;
6409 struct file *event_file = NULL;
6410 struct file *group_file = NULL;
Matt Helsley38a81da2010-09-13 13:01:20 -07006411 struct task_struct *task = NULL;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006412 struct pmu *pmu;
Al Viroea635c62010-05-26 17:40:29 -04006413 int event_fd;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006414 int move_group = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006415 int fput_needed = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006416 int err;
6417
6418 /* for future expandability... */
Stephane Eraniane5d13672011-02-14 11:20:01 +02006419 if (flags & ~PERF_FLAG_ALL)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006420 return -EINVAL;
6421
6422 err = perf_copy_attr(attr_uptr, &attr);
6423 if (err)
6424 return err;
6425
6426 if (!attr.exclude_kernel) {
6427 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
6428 return -EACCES;
6429 }
6430
6431 if (attr.freq) {
6432 if (attr.sample_freq > sysctl_perf_event_sample_rate)
6433 return -EINVAL;
6434 }
6435
Stephane Eraniane5d13672011-02-14 11:20:01 +02006436 /*
6437 * In cgroup mode, the pid argument is used to pass the fd
6438 * opened to the cgroup directory in cgroupfs. The cpu argument
6439 * designates the cpu on which to monitor threads from that
6440 * cgroup.
6441 */
6442 if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1))
6443 return -EINVAL;
6444
Al Viroea635c62010-05-26 17:40:29 -04006445 event_fd = get_unused_fd_flags(O_RDWR);
6446 if (event_fd < 0)
6447 return event_fd;
6448
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006449 if (group_fd != -1) {
6450 group_leader = perf_fget_light(group_fd, &fput_needed);
6451 if (IS_ERR(group_leader)) {
6452 err = PTR_ERR(group_leader);
Stephane Eraniand14b12d2010-09-17 11:28:47 +02006453 goto err_fd;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006454 }
6455 group_file = group_leader->filp;
6456 if (flags & PERF_FLAG_FD_OUTPUT)
6457 output_event = group_leader;
6458 if (flags & PERF_FLAG_FD_NO_GROUP)
6459 group_leader = NULL;
6460 }
6461
Stephane Eraniane5d13672011-02-14 11:20:01 +02006462 if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) {
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02006463 task = find_lively_task_by_vpid(pid);
6464 if (IS_ERR(task)) {
6465 err = PTR_ERR(task);
6466 goto err_group_fd;
6467 }
6468 }
6469
Peter Zijlstrad580ff82010-10-14 17:43:23 +02006470 event = perf_event_alloc(&attr, cpu, task, group_leader, NULL, NULL);
Stephane Eraniand14b12d2010-09-17 11:28:47 +02006471 if (IS_ERR(event)) {
6472 err = PTR_ERR(event);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02006473 goto err_task;
Stephane Eraniand14b12d2010-09-17 11:28:47 +02006474 }
6475
Stephane Eraniane5d13672011-02-14 11:20:01 +02006476 if (flags & PERF_FLAG_PID_CGROUP) {
6477 err = perf_cgroup_connect(pid, event, &attr, group_leader);
6478 if (err)
6479 goto err_alloc;
6480 }
6481
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006482 /*
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006483 * Special case software events and allow them to be part of
6484 * any hardware group.
6485 */
6486 pmu = event->pmu;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006487
6488 if (group_leader &&
6489 (is_software_event(event) != is_software_event(group_leader))) {
6490 if (is_software_event(event)) {
6491 /*
6492 * If event and group_leader are not both a software
6493 * event, and event is, then group leader is not.
6494 *
6495 * Allow the addition of software events to !software
6496 * groups, this is safe because software events never
6497 * fail to schedule.
6498 */
6499 pmu = group_leader->pmu;
6500 } else if (is_software_event(group_leader) &&
6501 (group_leader->group_flags & PERF_GROUP_SOFTWARE)) {
6502 /*
6503 * In case the group is a pure software group, and we
6504 * try to add a hardware event, move the whole group to
6505 * the hardware context.
6506 */
6507 move_group = 1;
6508 }
6509 }
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006510
6511 /*
6512 * Get the target context (task or percpu):
6513 */
Matt Helsley38a81da2010-09-13 13:01:20 -07006514 ctx = find_get_context(pmu, task, cpu);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006515 if (IS_ERR(ctx)) {
6516 err = PTR_ERR(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02006517 goto err_alloc;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006518 }
6519
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006520 /*
6521 * Look up the group leader (we will attach this event to it):
6522 */
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006523 if (group_leader) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006524 err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006525
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006526 /*
6527 * Do not allow a recursive hierarchy (this new sibling
6528 * becoming part of another group-sibling):
6529 */
6530 if (group_leader->group_leader != group_leader)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006531 goto err_context;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006532 /*
6533 * Do not allow to attach to a group in a different
6534 * task or CPU context:
6535 */
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006536 if (move_group) {
6537 if (group_leader->ctx->type != ctx->type)
6538 goto err_context;
6539 } else {
6540 if (group_leader->ctx != ctx)
6541 goto err_context;
6542 }
6543
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006544 /*
6545 * Only a group leader can be exclusive or pinned
6546 */
6547 if (attr.exclusive || attr.pinned)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006548 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006549 }
6550
6551 if (output_event) {
6552 err = perf_event_set_output(event, output_event);
6553 if (err)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006554 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006555 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006556
Al Viroea635c62010-05-26 17:40:29 -04006557 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, O_RDWR);
6558 if (IS_ERR(event_file)) {
6559 err = PTR_ERR(event_file);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006560 goto err_context;
Al Viroea635c62010-05-26 17:40:29 -04006561 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006562
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006563 if (move_group) {
6564 struct perf_event_context *gctx = group_leader->ctx;
6565
6566 mutex_lock(&gctx->mutex);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01006567 perf_remove_from_context(group_leader);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006568 list_for_each_entry(sibling, &group_leader->sibling_list,
6569 group_entry) {
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01006570 perf_remove_from_context(sibling);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006571 put_ctx(gctx);
6572 }
6573 mutex_unlock(&gctx->mutex);
6574 put_ctx(gctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006575 }
6576
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006577 event->filp = event_file;
6578 WARN_ON_ONCE(ctx->parent_ctx);
6579 mutex_lock(&ctx->mutex);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006580
6581 if (move_group) {
6582 perf_install_in_context(ctx, group_leader, cpu);
6583 get_ctx(ctx);
6584 list_for_each_entry(sibling, &group_leader->sibling_list,
6585 group_entry) {
6586 perf_install_in_context(ctx, sibling, cpu);
6587 get_ctx(ctx);
6588 }
6589 }
6590
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006591 perf_install_in_context(ctx, event, cpu);
6592 ++ctx->generation;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01006593 perf_unpin_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006594 mutex_unlock(&ctx->mutex);
6595
6596 event->owner = current;
Peter Zijlstra88821352010-11-09 19:01:43 +01006597
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006598 mutex_lock(&current->perf_event_mutex);
6599 list_add_tail(&event->owner_entry, &current->perf_event_list);
6600 mutex_unlock(&current->perf_event_mutex);
6601
Peter Zijlstra8a495422010-05-27 15:47:49 +02006602 /*
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02006603 * Precalculate sample_data sizes
6604 */
6605 perf_event__header_size(event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02006606 perf_event__id_header_size(event);
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02006607
6608 /*
Peter Zijlstra8a495422010-05-27 15:47:49 +02006609 * Drop the reference on the group_event after placing the
6610 * new event on the sibling_list. This ensures destruction
6611 * of the group leader will find the pointer to itself in
6612 * perf_group_detach().
6613 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006614 fput_light(group_file, fput_needed);
Al Viroea635c62010-05-26 17:40:29 -04006615 fd_install(event_fd, event_file);
6616 return event_fd;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006617
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006618err_context:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01006619 perf_unpin_context(ctx);
Al Viroea635c62010-05-26 17:40:29 -04006620 put_ctx(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02006621err_alloc:
6622 free_event(event);
Peter Zijlstrae7d0bc02010-10-14 16:54:51 +02006623err_task:
6624 if (task)
6625 put_task_struct(task);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006626err_group_fd:
6627 fput_light(group_file, fput_needed);
Al Viroea635c62010-05-26 17:40:29 -04006628err_fd:
6629 put_unused_fd(event_fd);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006630 return err;
6631}
6632
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006633/**
6634 * perf_event_create_kernel_counter
6635 *
6636 * @attr: attributes of the counter to create
6637 * @cpu: cpu in which the counter is bound
Matt Helsley38a81da2010-09-13 13:01:20 -07006638 * @task: task to profile (NULL for percpu)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006639 */
6640struct perf_event *
6641perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
Matt Helsley38a81da2010-09-13 13:01:20 -07006642 struct task_struct *task,
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01006643 perf_overflow_handler_t overflow_handler)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006644{
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006645 struct perf_event_context *ctx;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006646 struct perf_event *event;
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006647 int err;
6648
6649 /*
6650 * Get the target context (task or percpu):
6651 */
6652
Peter Zijlstrad580ff82010-10-14 17:43:23 +02006653 event = perf_event_alloc(attr, cpu, task, NULL, NULL, overflow_handler);
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01006654 if (IS_ERR(event)) {
6655 err = PTR_ERR(event);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006656 goto err;
6657 }
6658
Matt Helsley38a81da2010-09-13 13:01:20 -07006659 ctx = find_get_context(event->pmu, task, cpu);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006660 if (IS_ERR(ctx)) {
6661 err = PTR_ERR(ctx);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006662 goto err_free;
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01006663 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006664
6665 event->filp = NULL;
6666 WARN_ON_ONCE(ctx->parent_ctx);
6667 mutex_lock(&ctx->mutex);
6668 perf_install_in_context(ctx, event, cpu);
6669 ++ctx->generation;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01006670 perf_unpin_context(ctx);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006671 mutex_unlock(&ctx->mutex);
6672
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006673 return event;
6674
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006675err_free:
6676 free_event(event);
6677err:
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01006678 return ERR_PTR(err);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006679}
6680EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
6681
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006682static void sync_child_event(struct perf_event *child_event,
6683 struct task_struct *child)
6684{
6685 struct perf_event *parent_event = child_event->parent;
6686 u64 child_val;
6687
6688 if (child_event->attr.inherit_stat)
6689 perf_event_read_event(child_event, child);
6690
Peter Zijlstrab5e58792010-05-21 14:43:12 +02006691 child_val = perf_event_count(child_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006692
6693 /*
6694 * Add back the child's count to the parent's count:
6695 */
Peter Zijlstraa6e6dea2010-05-21 14:27:58 +02006696 atomic64_add(child_val, &parent_event->child_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006697 atomic64_add(child_event->total_time_enabled,
6698 &parent_event->child_total_time_enabled);
6699 atomic64_add(child_event->total_time_running,
6700 &parent_event->child_total_time_running);
6701
6702 /*
6703 * Remove this event from the parent's list
6704 */
6705 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
6706 mutex_lock(&parent_event->child_mutex);
6707 list_del_init(&child_event->child_list);
6708 mutex_unlock(&parent_event->child_mutex);
6709
6710 /*
6711 * Release the parent event, if this was the last
6712 * reference to it.
6713 */
6714 fput(parent_event->filp);
6715}
6716
6717static void
6718__perf_event_exit_task(struct perf_event *child_event,
6719 struct perf_event_context *child_ctx,
6720 struct task_struct *child)
6721{
6722 struct perf_event *parent_event;
6723
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01006724 perf_remove_from_context(child_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006725
6726 parent_event = child_event->parent;
6727 /*
6728 * It can happen that parent exits first, and has events
6729 * that are still around due to the child reference. These
6730 * events need to be zapped - but otherwise linger.
6731 */
6732 if (parent_event) {
6733 sync_child_event(child_event, child);
6734 free_event(child_event);
6735 }
6736}
6737
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006738static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006739{
6740 struct perf_event *child_event, *tmp;
6741 struct perf_event_context *child_ctx;
6742 unsigned long flags;
6743
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006744 if (likely(!child->perf_event_ctxp[ctxn])) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006745 perf_event_task(child, NULL, 0);
6746 return;
6747 }
6748
6749 local_irq_save(flags);
6750 /*
6751 * We can't reschedule here because interrupts are disabled,
6752 * and either child is current or it is a task that can't be
6753 * scheduled, so we are now safe from rescheduling changing
6754 * our context.
6755 */
Oleg Nesterov806839b2011-01-21 18:45:47 +01006756 child_ctx = rcu_dereference_raw(child->perf_event_ctxp[ctxn]);
Peter Zijlstra82cd6de2010-10-14 17:57:23 +02006757 task_ctx_sched_out(child_ctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006758
6759 /*
6760 * Take the context lock here so that if find_get_context is
6761 * reading child->perf_event_ctxp, we wait until it has
6762 * incremented the context's refcount before we do put_ctx below.
6763 */
Thomas Gleixnere625cce2009-11-17 18:02:06 +01006764 raw_spin_lock(&child_ctx->lock);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006765 child->perf_event_ctxp[ctxn] = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006766 /*
6767 * If this context is a clone; unclone it so it can't get
6768 * swapped to another process while we're removing all
6769 * the events from it.
6770 */
6771 unclone_ctx(child_ctx);
Peter Zijlstra5e942bb2009-11-23 11:37:26 +01006772 update_context_time(child_ctx);
Thomas Gleixnere625cce2009-11-17 18:02:06 +01006773 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006774
6775 /*
6776 * Report the task dead after unscheduling the events so that we
6777 * won't get any samples after PERF_RECORD_EXIT. We can however still
6778 * get a few PERF_RECORD_READ events.
6779 */
6780 perf_event_task(child, child_ctx, 0);
6781
6782 /*
6783 * We can recurse on the same lock type through:
6784 *
6785 * __perf_event_exit_task()
6786 * sync_child_event()
6787 * fput(parent_event->filp)
6788 * perf_release()
6789 * mutex_lock(&ctx->mutex)
6790 *
6791 * But since its the parent context it won't be the same instance.
6792 */
Peter Zijlstraa0507c82010-05-06 15:42:53 +02006793 mutex_lock(&child_ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006794
6795again:
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006796 list_for_each_entry_safe(child_event, tmp, &child_ctx->pinned_groups,
6797 group_entry)
6798 __perf_event_exit_task(child_event, child_ctx, child);
6799
6800 list_for_each_entry_safe(child_event, tmp, &child_ctx->flexible_groups,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006801 group_entry)
6802 __perf_event_exit_task(child_event, child_ctx, child);
6803
6804 /*
6805 * If the last event was a group event, it will have appended all
6806 * its siblings to the list, but we obtained 'tmp' before that which
6807 * will still point to the list head terminating the iteration.
6808 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006809 if (!list_empty(&child_ctx->pinned_groups) ||
6810 !list_empty(&child_ctx->flexible_groups))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006811 goto again;
6812
6813 mutex_unlock(&child_ctx->mutex);
6814
6815 put_ctx(child_ctx);
6816}
6817
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006818/*
6819 * When a child task exits, feed back event values to parent events.
6820 */
6821void perf_event_exit_task(struct task_struct *child)
6822{
Peter Zijlstra88821352010-11-09 19:01:43 +01006823 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006824 int ctxn;
6825
Peter Zijlstra88821352010-11-09 19:01:43 +01006826 mutex_lock(&child->perf_event_mutex);
6827 list_for_each_entry_safe(event, tmp, &child->perf_event_list,
6828 owner_entry) {
6829 list_del_init(&event->owner_entry);
6830
6831 /*
6832 * Ensure the list deletion is visible before we clear
6833 * the owner, closes a race against perf_release() where
6834 * we need to serialize on the owner->perf_event_mutex.
6835 */
6836 smp_wmb();
6837 event->owner = NULL;
6838 }
6839 mutex_unlock(&child->perf_event_mutex);
6840
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006841 for_each_task_context_nr(ctxn)
6842 perf_event_exit_task_context(child, ctxn);
6843}
6844
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006845static void perf_free_event(struct perf_event *event,
6846 struct perf_event_context *ctx)
6847{
6848 struct perf_event *parent = event->parent;
6849
6850 if (WARN_ON_ONCE(!parent))
6851 return;
6852
6853 mutex_lock(&parent->child_mutex);
6854 list_del_init(&event->child_list);
6855 mutex_unlock(&parent->child_mutex);
6856
6857 fput(parent->filp);
6858
Peter Zijlstra8a495422010-05-27 15:47:49 +02006859 perf_group_detach(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006860 list_del_event(event, ctx);
6861 free_event(event);
6862}
6863
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006864/*
6865 * free an unexposed, unused context as created by inheritance by
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006866 * perf_event_init_task below, used by fork() in case of fail.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006867 */
6868void perf_event_free_task(struct task_struct *task)
6869{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006870 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006871 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006872 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006873
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006874 for_each_task_context_nr(ctxn) {
6875 ctx = task->perf_event_ctxp[ctxn];
6876 if (!ctx)
6877 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006878
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006879 mutex_lock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006880again:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006881 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
6882 group_entry)
6883 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006884
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006885 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
6886 group_entry)
6887 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006888
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006889 if (!list_empty(&ctx->pinned_groups) ||
6890 !list_empty(&ctx->flexible_groups))
6891 goto again;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006892
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006893 mutex_unlock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006894
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006895 put_ctx(ctx);
6896 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006897}
6898
Peter Zijlstra4e231c72010-09-09 21:01:59 +02006899void perf_event_delayed_put(struct task_struct *task)
6900{
6901 int ctxn;
6902
6903 for_each_task_context_nr(ctxn)
6904 WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
6905}
6906
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02006907/*
6908 * inherit a event from parent task to child task:
6909 */
6910static struct perf_event *
6911inherit_event(struct perf_event *parent_event,
6912 struct task_struct *parent,
6913 struct perf_event_context *parent_ctx,
6914 struct task_struct *child,
6915 struct perf_event *group_leader,
6916 struct perf_event_context *child_ctx)
6917{
6918 struct perf_event *child_event;
Peter Zijlstracee010e2010-09-10 12:51:54 +02006919 unsigned long flags;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02006920
6921 /*
6922 * Instead of creating recursive hierarchies of events,
6923 * we link inherited events back to the original parent,
6924 * which has a filp for sure, which we use as the reference
6925 * count:
6926 */
6927 if (parent_event->parent)
6928 parent_event = parent_event->parent;
6929
6930 child_event = perf_event_alloc(&parent_event->attr,
6931 parent_event->cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +02006932 child,
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02006933 group_leader, parent_event,
6934 NULL);
6935 if (IS_ERR(child_event))
6936 return child_event;
6937 get_ctx(child_ctx);
6938
6939 /*
6940 * Make the child state follow the state of the parent event,
6941 * not its attr.disabled bit. We hold the parent's mutex,
6942 * so we won't race with perf_event_{en, dis}able_family.
6943 */
6944 if (parent_event->state >= PERF_EVENT_STATE_INACTIVE)
6945 child_event->state = PERF_EVENT_STATE_INACTIVE;
6946 else
6947 child_event->state = PERF_EVENT_STATE_OFF;
6948
6949 if (parent_event->attr.freq) {
6950 u64 sample_period = parent_event->hw.sample_period;
6951 struct hw_perf_event *hwc = &child_event->hw;
6952
6953 hwc->sample_period = sample_period;
6954 hwc->last_period = sample_period;
6955
6956 local64_set(&hwc->period_left, sample_period);
6957 }
6958
6959 child_event->ctx = child_ctx;
6960 child_event->overflow_handler = parent_event->overflow_handler;
6961
6962 /*
Thomas Gleixner614b6782010-12-03 16:24:32 -02006963 * Precalculate sample_data sizes
6964 */
6965 perf_event__header_size(child_event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02006966 perf_event__id_header_size(child_event);
Thomas Gleixner614b6782010-12-03 16:24:32 -02006967
6968 /*
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02006969 * Link it up in the child's context:
6970 */
Peter Zijlstracee010e2010-09-10 12:51:54 +02006971 raw_spin_lock_irqsave(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02006972 add_event_to_ctx(child_event, child_ctx);
Peter Zijlstracee010e2010-09-10 12:51:54 +02006973 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02006974
6975 /*
6976 * Get a reference to the parent filp - we will fput it
6977 * when the child event exits. This is safe to do because
6978 * we are in the parent and we know that the filp still
6979 * exists and has a nonzero count:
6980 */
6981 atomic_long_inc(&parent_event->filp->f_count);
6982
6983 /*
6984 * Link this into the parent event's child list
6985 */
6986 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
6987 mutex_lock(&parent_event->child_mutex);
6988 list_add_tail(&child_event->child_list, &parent_event->child_list);
6989 mutex_unlock(&parent_event->child_mutex);
6990
6991 return child_event;
6992}
6993
6994static int inherit_group(struct perf_event *parent_event,
6995 struct task_struct *parent,
6996 struct perf_event_context *parent_ctx,
6997 struct task_struct *child,
6998 struct perf_event_context *child_ctx)
6999{
7000 struct perf_event *leader;
7001 struct perf_event *sub;
7002 struct perf_event *child_ctr;
7003
7004 leader = inherit_event(parent_event, parent, parent_ctx,
7005 child, NULL, child_ctx);
7006 if (IS_ERR(leader))
7007 return PTR_ERR(leader);
7008 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
7009 child_ctr = inherit_event(sub, parent, parent_ctx,
7010 child, leader, child_ctx);
7011 if (IS_ERR(child_ctr))
7012 return PTR_ERR(child_ctr);
7013 }
7014 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007015}
7016
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007017static int
7018inherit_task_group(struct perf_event *event, struct task_struct *parent,
7019 struct perf_event_context *parent_ctx,
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007020 struct task_struct *child, int ctxn,
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007021 int *inherited_all)
7022{
7023 int ret;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007024 struct perf_event_context *child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007025
7026 if (!event->attr.inherit) {
7027 *inherited_all = 0;
7028 return 0;
7029 }
7030
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007031 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007032 if (!child_ctx) {
7033 /*
7034 * This is executed from the parent task context, so
7035 * inherit events that have been marked for cloning.
7036 * First allocate and initialize a context for the
7037 * child.
7038 */
7039
Peter Zijlstraeb184472010-09-07 15:55:13 +02007040 child_ctx = alloc_perf_context(event->pmu, child);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007041 if (!child_ctx)
7042 return -ENOMEM;
7043
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007044 child->perf_event_ctxp[ctxn] = child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007045 }
7046
7047 ret = inherit_group(event, parent, parent_ctx,
7048 child, child_ctx);
7049
7050 if (ret)
7051 *inherited_all = 0;
7052
7053 return ret;
7054}
7055
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007056/*
7057 * Initialize the perf_event context in task_struct
7058 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007059int perf_event_init_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007060{
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007061 struct perf_event_context *child_ctx, *parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007062 struct perf_event_context *cloned_ctx;
7063 struct perf_event *event;
7064 struct task_struct *parent = current;
7065 int inherited_all = 1;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01007066 unsigned long flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007067 int ret = 0;
7068
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007069 if (likely(!parent->perf_event_ctxp[ctxn]))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007070 return 0;
7071
7072 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007073 * If the parent's context is a clone, pin it so it won't get
7074 * swapped under us.
7075 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007076 parent_ctx = perf_pin_task_context(parent, ctxn);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007077
7078 /*
7079 * No need to check if parent_ctx != NULL here; since we saw
7080 * it non-NULL earlier, the only reason for it to become NULL
7081 * is if we exit, and since we're currently in the middle of
7082 * a fork we can't be exiting at the same time.
7083 */
7084
7085 /*
7086 * Lock the parent list. No need to lock the child - not PID
7087 * hashed yet and not running, so nobody can access it.
7088 */
7089 mutex_lock(&parent_ctx->mutex);
7090
7091 /*
7092 * We dont have to disable NMIs - we are only looking at
7093 * the list, not manipulating it:
7094 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007095 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007096 ret = inherit_task_group(event, parent, parent_ctx,
7097 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007098 if (ret)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007099 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007100 }
7101
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01007102 /*
7103 * We can't hold ctx->lock when iterating the ->flexible_group list due
7104 * to allocations, but we need to prevent rotation because
7105 * rotate_ctx() will change the list from interrupt context.
7106 */
7107 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
7108 parent_ctx->rotate_disable = 1;
7109 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
7110
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007111 list_for_each_entry(event, &parent_ctx->flexible_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)
7115 break;
7116 }
7117
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01007118 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
7119 parent_ctx->rotate_disable = 0;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01007120
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007121 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007122
Peter Zijlstra05cbaa22009-12-30 16:00:35 +01007123 if (child_ctx && inherited_all) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007124 /*
7125 * Mark the child context as a clone of the parent
7126 * context, or of whatever the parent is a clone of.
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01007127 *
7128 * Note that if the parent is a clone, the holding of
7129 * parent_ctx->lock avoids it from being uncloned.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007130 */
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01007131 cloned_ctx = parent_ctx->parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007132 if (cloned_ctx) {
7133 child_ctx->parent_ctx = cloned_ctx;
7134 child_ctx->parent_gen = parent_ctx->parent_gen;
7135 } else {
7136 child_ctx->parent_ctx = parent_ctx;
7137 child_ctx->parent_gen = parent_ctx->generation;
7138 }
7139 get_ctx(child_ctx->parent_ctx);
7140 }
7141
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01007142 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007143 mutex_unlock(&parent_ctx->mutex);
7144
7145 perf_unpin_context(parent_ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007146 put_ctx(parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007147
7148 return ret;
7149}
7150
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007151/*
7152 * Initialize the perf_event context in task_struct
7153 */
7154int perf_event_init_task(struct task_struct *child)
7155{
7156 int ctxn, ret;
7157
Oleg Nesterov8550d7c2011-01-19 19:22:28 +01007158 memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp));
7159 mutex_init(&child->perf_event_mutex);
7160 INIT_LIST_HEAD(&child->perf_event_list);
7161
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007162 for_each_task_context_nr(ctxn) {
7163 ret = perf_event_init_context(child, ctxn);
7164 if (ret)
7165 return ret;
7166 }
7167
7168 return 0;
7169}
7170
Paul Mackerras220b1402010-03-10 20:45:52 +11007171static void __init perf_event_init_all_cpus(void)
7172{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007173 struct swevent_htable *swhash;
Paul Mackerras220b1402010-03-10 20:45:52 +11007174 int cpu;
Paul Mackerras220b1402010-03-10 20:45:52 +11007175
7176 for_each_possible_cpu(cpu) {
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007177 swhash = &per_cpu(swevent_htable, cpu);
7178 mutex_init(&swhash->hlist_mutex);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02007179 INIT_LIST_HEAD(&per_cpu(rotation_list, cpu));
Paul Mackerras220b1402010-03-10 20:45:52 +11007180 }
7181}
7182
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007183static void __cpuinit perf_event_init_cpu(int cpu)
7184{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007185 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007186
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007187 mutex_lock(&swhash->hlist_mutex);
7188 if (swhash->hlist_refcount > 0) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007189 struct swevent_hlist *hlist;
7190
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007191 hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
7192 WARN_ON(!hlist);
7193 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007194 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007195 mutex_unlock(&swhash->hlist_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007196}
7197
Peter Zijlstrac2774432010-12-08 15:29:02 +01007198#if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02007199static void perf_pmu_rotate_stop(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007200{
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02007201 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
7202
7203 WARN_ON(!irqs_disabled());
7204
7205 list_del_init(&cpuctx->rotation_list);
7206}
7207
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007208static void __perf_event_exit_context(void *__info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007209{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007210 struct perf_event_context *ctx = __info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007211 struct perf_event *event, *tmp;
7212
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007213 perf_pmu_rotate_stop(ctx->pmu);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02007214
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007215 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007216 __perf_remove_from_context(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007217 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, group_entry)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007218 __perf_remove_from_context(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007219}
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007220
7221static void perf_event_exit_cpu_context(int cpu)
7222{
7223 struct perf_event_context *ctx;
7224 struct pmu *pmu;
7225 int idx;
7226
7227 idx = srcu_read_lock(&pmus_srcu);
7228 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra917bdd12010-09-17 11:28:49 +02007229 ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007230
7231 mutex_lock(&ctx->mutex);
7232 smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
7233 mutex_unlock(&ctx->mutex);
7234 }
7235 srcu_read_unlock(&pmus_srcu, idx);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007236}
7237
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007238static void perf_event_exit_cpu(int cpu)
7239{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007240 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007241
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007242 mutex_lock(&swhash->hlist_mutex);
7243 swevent_hlist_release(swhash);
7244 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007245
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007246 perf_event_exit_cpu_context(cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007247}
7248#else
7249static inline void perf_event_exit_cpu(int cpu) { }
7250#endif
7251
Peter Zijlstrac2774432010-12-08 15:29:02 +01007252static int
7253perf_reboot(struct notifier_block *notifier, unsigned long val, void *v)
7254{
7255 int cpu;
7256
7257 for_each_online_cpu(cpu)
7258 perf_event_exit_cpu(cpu);
7259
7260 return NOTIFY_OK;
7261}
7262
7263/*
7264 * Run the perf reboot notifier at the very last possible moment so that
7265 * the generic watchdog code runs as long as possible.
7266 */
7267static struct notifier_block perf_reboot_notifier = {
7268 .notifier_call = perf_reboot,
7269 .priority = INT_MIN,
7270};
7271
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007272static int __cpuinit
7273perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
7274{
7275 unsigned int cpu = (long)hcpu;
7276
Peter Zijlstra5e116372010-06-11 13:35:08 +02007277 switch (action & ~CPU_TASKS_FROZEN) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007278
7279 case CPU_UP_PREPARE:
Peter Zijlstra5e116372010-06-11 13:35:08 +02007280 case CPU_DOWN_FAILED:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007281 perf_event_init_cpu(cpu);
7282 break;
7283
Peter Zijlstra5e116372010-06-11 13:35:08 +02007284 case CPU_UP_CANCELED:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007285 case CPU_DOWN_PREPARE:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007286 perf_event_exit_cpu(cpu);
7287 break;
7288
7289 default:
7290 break;
7291 }
7292
7293 return NOTIFY_OK;
7294}
7295
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007296void __init perf_event_init(void)
7297{
Jason Wessel3c502e72010-11-04 17:33:01 -05007298 int ret;
7299
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007300 idr_init(&pmu_idr);
7301
Paul Mackerras220b1402010-03-10 20:45:52 +11007302 perf_event_init_all_cpus();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007303 init_srcu_struct(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007304 perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE);
7305 perf_pmu_register(&perf_cpu_clock, NULL, -1);
7306 perf_pmu_register(&perf_task_clock, NULL, -1);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007307 perf_tp_register();
7308 perf_cpu_notifier(perf_cpu_notify);
Peter Zijlstrac2774432010-12-08 15:29:02 +01007309 register_reboot_notifier(&perf_reboot_notifier);
Jason Wessel3c502e72010-11-04 17:33:01 -05007310
7311 ret = init_hw_breakpoint();
7312 WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007313}
Peter Zijlstraabe43402010-11-17 23:17:37 +01007314
7315static int __init perf_event_sysfs_init(void)
7316{
7317 struct pmu *pmu;
7318 int ret;
7319
7320 mutex_lock(&pmus_lock);
7321
7322 ret = bus_register(&pmu_bus);
7323 if (ret)
7324 goto unlock;
7325
7326 list_for_each_entry(pmu, &pmus, entry) {
7327 if (!pmu->name || pmu->type < 0)
7328 continue;
7329
7330 ret = pmu_dev_alloc(pmu);
7331 WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret);
7332 }
7333 pmu_bus_running = 1;
7334 ret = 0;
7335
7336unlock:
7337 mutex_unlock(&pmus_lock);
7338
7339 return ret;
7340}
7341device_initcall(perf_event_sysfs_init);
Stephane Eraniane5d13672011-02-14 11:20:01 +02007342
7343#ifdef CONFIG_CGROUP_PERF
7344static struct cgroup_subsys_state *perf_cgroup_create(
7345 struct cgroup_subsys *ss, struct cgroup *cont)
7346{
7347 struct perf_cgroup *jc;
7348 struct perf_cgroup_info *t;
7349 int c;
7350
7351 jc = kmalloc(sizeof(*jc), GFP_KERNEL);
7352 if (!jc)
7353 return ERR_PTR(-ENOMEM);
7354
7355 memset(jc, 0, sizeof(*jc));
7356
7357 jc->info = alloc_percpu(struct perf_cgroup_info);
7358 if (!jc->info) {
7359 kfree(jc);
7360 return ERR_PTR(-ENOMEM);
7361 }
7362
7363 for_each_possible_cpu(c) {
7364 t = per_cpu_ptr(jc->info, c);
7365 t->time = 0;
7366 t->timestamp = 0;
7367 }
7368 return &jc->css;
7369}
7370
7371static void perf_cgroup_destroy(struct cgroup_subsys *ss,
7372 struct cgroup *cont)
7373{
7374 struct perf_cgroup *jc;
7375 jc = container_of(cgroup_subsys_state(cont, perf_subsys_id),
7376 struct perf_cgroup, css);
7377 free_percpu(jc->info);
7378 kfree(jc);
7379}
7380
7381static int __perf_cgroup_move(void *info)
7382{
7383 struct task_struct *task = info;
7384 perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN);
7385 return 0;
7386}
7387
7388static void perf_cgroup_move(struct task_struct *task)
7389{
7390 task_function_call(task, __perf_cgroup_move, task);
7391}
7392
7393static void perf_cgroup_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
7394 struct cgroup *old_cgrp, struct task_struct *task,
7395 bool threadgroup)
7396{
7397 perf_cgroup_move(task);
7398 if (threadgroup) {
7399 struct task_struct *c;
7400 rcu_read_lock();
7401 list_for_each_entry_rcu(c, &task->thread_group, thread_group) {
7402 perf_cgroup_move(c);
7403 }
7404 rcu_read_unlock();
7405 }
7406}
7407
7408static void perf_cgroup_exit(struct cgroup_subsys *ss, struct cgroup *cgrp,
7409 struct cgroup *old_cgrp, struct task_struct *task)
7410{
7411 /*
7412 * cgroup_exit() is called in the copy_process() failure path.
7413 * Ignore this case since the task hasn't ran yet, this avoids
7414 * trying to poke a half freed task state from generic code.
7415 */
7416 if (!(task->flags & PF_EXITING))
7417 return;
7418
7419 perf_cgroup_move(task);
7420}
7421
7422struct cgroup_subsys perf_subsys = {
7423 .name = "perf_event",
7424 .subsys_id = perf_subsys_id,
7425 .create = perf_cgroup_create,
7426 .destroy = perf_cgroup_destroy,
7427 .exit = perf_cgroup_exit,
7428 .attach = perf_cgroup_attach,
7429};
7430#endif /* CONFIG_CGROUP_PERF */