blob: 71c2d44ff95d8fa6c5b78463b5282829bdd792eb [file] [log] [blame]
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001/*
Ingo Molnar57c0c152009-09-21 12:20:38 +02002 * Performance events core code:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003 *
4 * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
Ingo Molnare7e7ee22011-05-04 08:42:29 +02005 * Copyright (C) 2008-2011 Red Hat, Inc., Ingo Molnar
6 * Copyright (C) 2008-2011 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007 * 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 {
Ingo Molnare7e7ee22011-05-04 08:42:29 +020042 struct task_struct *p;
43 int (*func)(void *info);
44 void *info;
45 int ret;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010046};
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 = {
Ingo Molnare7e7ee22011-05-04 08:42:29 +020079 .p = p,
80 .func = func,
81 .info = info,
82 .ret = -ESRCH, /* No such (running) process */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010083 };
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 = {
Ingo Molnare7e7ee22011-05-04 08:42:29 +0200103 .p = NULL,
104 .func = func,
105 .info = info,
106 .ret = -ENXIO, /* No such CPU */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +0100107 };
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 */
Jason Barond430d3d2011-03-16 17:29:47 -0400128struct jump_label_key perf_sched_events __read_mostly;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200129static 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
Frederic Weisbecker20443382011-03-31 03:33:29 +0200148/* Minimum for 512 kiB + 1 user control page */
149int sysctl_perf_event_mlock __read_mostly = 512 + (PAGE_SIZE / 1024); /* 'free' kiB per user */
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200150
151/*
152 * max perf event sample rate
153 */
Peter Zijlstra163ec432011-02-16 11:22:34 +0100154#define DEFAULT_MAX_SAMPLE_RATE 100000
155int sysctl_perf_event_sample_rate __read_mostly = DEFAULT_MAX_SAMPLE_RATE;
156static int max_samples_per_tick __read_mostly =
157 DIV_ROUND_UP(DEFAULT_MAX_SAMPLE_RATE, HZ);
158
159int perf_proc_update_handler(struct ctl_table *table, int write,
160 void __user *buffer, size_t *lenp,
161 loff_t *ppos)
162{
163 int ret = proc_dointvec(table, write, buffer, lenp, ppos);
164
165 if (ret || !write)
166 return ret;
167
168 max_samples_per_tick = DIV_ROUND_UP(sysctl_perf_event_sample_rate, HZ);
169
170 return 0;
171}
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200172
173static atomic64_t perf_event_id;
174
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200175static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
176 enum event_type_t event_type);
177
178static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +0200179 enum event_type_t event_type,
180 struct task_struct *task);
181
182static void update_context_time(struct perf_event_context *ctx);
183static u64 perf_event_time(struct perf_event *event);
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200184
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200185void __weak perf_event_print_debug(void) { }
186
Matt Fleming84c79912010-10-03 21:41:13 +0100187extern __weak const char *perf_pmu_name(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200188{
Matt Fleming84c79912010-10-03 21:41:13 +0100189 return "pmu";
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200190}
191
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200192static inline u64 perf_clock(void)
193{
194 return local_clock();
195}
196
Stephane Eraniane5d13672011-02-14 11:20:01 +0200197static inline struct perf_cpu_context *
198__get_cpu_context(struct perf_event_context *ctx)
199{
200 return this_cpu_ptr(ctx->pmu->pmu_cpu_context);
201}
202
Peter Zijlstrafacc4302011-04-09 21:17:42 +0200203static void perf_ctx_lock(struct perf_cpu_context *cpuctx,
204 struct perf_event_context *ctx)
205{
206 raw_spin_lock(&cpuctx->ctx.lock);
207 if (ctx)
208 raw_spin_lock(&ctx->lock);
209}
210
211static void perf_ctx_unlock(struct perf_cpu_context *cpuctx,
212 struct perf_event_context *ctx)
213{
214 if (ctx)
215 raw_spin_unlock(&ctx->lock);
216 raw_spin_unlock(&cpuctx->ctx.lock);
217}
218
Stephane Eraniane5d13672011-02-14 11:20:01 +0200219#ifdef CONFIG_CGROUP_PERF
220
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200221/*
222 * Must ensure cgroup is pinned (css_get) before calling
223 * this function. In other words, we cannot call this function
224 * if there is no cgroup event for the current CPU context.
225 */
Stephane Eraniane5d13672011-02-14 11:20:01 +0200226static inline struct perf_cgroup *
227perf_cgroup_from_task(struct task_struct *task)
228{
229 return container_of(task_subsys_state(task, perf_subsys_id),
230 struct perf_cgroup, css);
231}
232
233static inline bool
234perf_cgroup_match(struct perf_event *event)
235{
236 struct perf_event_context *ctx = event->ctx;
237 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
238
239 return !event->cgrp || event->cgrp == cpuctx->cgrp;
240}
241
242static inline void perf_get_cgroup(struct perf_event *event)
243{
244 css_get(&event->cgrp->css);
245}
246
247static inline void perf_put_cgroup(struct perf_event *event)
248{
249 css_put(&event->cgrp->css);
250}
251
252static inline void perf_detach_cgroup(struct perf_event *event)
253{
254 perf_put_cgroup(event);
255 event->cgrp = NULL;
256}
257
258static inline int is_cgroup_event(struct perf_event *event)
259{
260 return event->cgrp != NULL;
261}
262
263static inline u64 perf_cgroup_event_time(struct perf_event *event)
264{
265 struct perf_cgroup_info *t;
266
267 t = per_cpu_ptr(event->cgrp->info, event->cpu);
268 return t->time;
269}
270
271static inline void __update_cgrp_time(struct perf_cgroup *cgrp)
272{
273 struct perf_cgroup_info *info;
274 u64 now;
275
276 now = perf_clock();
277
278 info = this_cpu_ptr(cgrp->info);
279
280 info->time += now - info->timestamp;
281 info->timestamp = now;
282}
283
284static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
285{
286 struct perf_cgroup *cgrp_out = cpuctx->cgrp;
287 if (cgrp_out)
288 __update_cgrp_time(cgrp_out);
289}
290
291static inline void update_cgrp_time_from_event(struct perf_event *event)
292{
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200293 struct perf_cgroup *cgrp;
294
Stephane Eraniane5d13672011-02-14 11:20:01 +0200295 /*
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200296 * ensure we access cgroup data only when needed and
297 * when we know the cgroup is pinned (css_get)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200298 */
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200299 if (!is_cgroup_event(event))
Stephane Eraniane5d13672011-02-14 11:20:01 +0200300 return;
301
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200302 cgrp = perf_cgroup_from_task(current);
303 /*
304 * Do not update time when cgroup is not active
305 */
306 if (cgrp == event->cgrp)
307 __update_cgrp_time(event->cgrp);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200308}
309
310static inline void
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200311perf_cgroup_set_timestamp(struct task_struct *task,
312 struct perf_event_context *ctx)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200313{
314 struct perf_cgroup *cgrp;
315 struct perf_cgroup_info *info;
316
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200317 /*
318 * ctx->lock held by caller
319 * ensure we do not access cgroup data
320 * unless we have the cgroup pinned (css_get)
321 */
322 if (!task || !ctx->nr_cgroups)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200323 return;
324
325 cgrp = perf_cgroup_from_task(task);
326 info = this_cpu_ptr(cgrp->info);
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200327 info->timestamp = ctx->timestamp;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200328}
329
330#define PERF_CGROUP_SWOUT 0x1 /* cgroup switch out every event */
331#define PERF_CGROUP_SWIN 0x2 /* cgroup switch in events based on task */
332
333/*
334 * reschedule events based on the cgroup constraint of task.
335 *
336 * mode SWOUT : schedule out everything
337 * mode SWIN : schedule in based on cgroup for next
338 */
339void perf_cgroup_switch(struct task_struct *task, int mode)
340{
341 struct perf_cpu_context *cpuctx;
342 struct pmu *pmu;
343 unsigned long flags;
344
345 /*
346 * disable interrupts to avoid geting nr_cgroup
347 * changes via __perf_event_disable(). Also
348 * avoids preemption.
349 */
350 local_irq_save(flags);
351
352 /*
353 * we reschedule only in the presence of cgroup
354 * constrained events.
355 */
356 rcu_read_lock();
357
358 list_for_each_entry_rcu(pmu, &pmus, entry) {
Stephane Eraniane5d13672011-02-14 11:20:01 +0200359 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
360
Stephane Eraniane5d13672011-02-14 11:20:01 +0200361 /*
362 * perf_cgroup_events says at least one
363 * context on this CPU has cgroup events.
364 *
365 * ctx->nr_cgroups reports the number of cgroup
366 * events for a context.
367 */
368 if (cpuctx->ctx.nr_cgroups > 0) {
Peter Zijlstrafacc4302011-04-09 21:17:42 +0200369 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
370 perf_pmu_disable(cpuctx->ctx.pmu);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200371
372 if (mode & PERF_CGROUP_SWOUT) {
373 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
374 /*
375 * must not be done before ctxswout due
376 * to event_filter_match() in event_sched_out()
377 */
378 cpuctx->cgrp = NULL;
379 }
380
381 if (mode & PERF_CGROUP_SWIN) {
Stephane Eraniane566b762011-04-06 02:54:54 +0200382 WARN_ON_ONCE(cpuctx->cgrp);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200383 /* set cgrp before ctxsw in to
384 * allow event_filter_match() to not
385 * have to pass task around
386 */
387 cpuctx->cgrp = perf_cgroup_from_task(task);
388 cpu_ctx_sched_in(cpuctx, EVENT_ALL, task);
389 }
Peter Zijlstrafacc4302011-04-09 21:17:42 +0200390 perf_pmu_enable(cpuctx->ctx.pmu);
391 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200392 }
Stephane Eraniane5d13672011-02-14 11:20:01 +0200393 }
394
395 rcu_read_unlock();
396
397 local_irq_restore(flags);
398}
399
400static inline void perf_cgroup_sched_out(struct task_struct *task)
401{
402 perf_cgroup_switch(task, PERF_CGROUP_SWOUT);
403}
404
405static inline void perf_cgroup_sched_in(struct task_struct *task)
406{
407 perf_cgroup_switch(task, PERF_CGROUP_SWIN);
408}
409
410static inline int perf_cgroup_connect(int fd, struct perf_event *event,
411 struct perf_event_attr *attr,
412 struct perf_event *group_leader)
413{
414 struct perf_cgroup *cgrp;
415 struct cgroup_subsys_state *css;
416 struct file *file;
417 int ret = 0, fput_needed;
418
419 file = fget_light(fd, &fput_needed);
420 if (!file)
421 return -EBADF;
422
423 css = cgroup_css_from_dir(file, perf_subsys_id);
Li Zefan3db272c2011-03-03 14:25:37 +0800424 if (IS_ERR(css)) {
425 ret = PTR_ERR(css);
426 goto out;
427 }
Stephane Eraniane5d13672011-02-14 11:20:01 +0200428
429 cgrp = container_of(css, struct perf_cgroup, css);
430 event->cgrp = cgrp;
431
Li Zefanf75e18c2011-03-03 14:25:50 +0800432 /* must be done before we fput() the file */
433 perf_get_cgroup(event);
434
Stephane Eraniane5d13672011-02-14 11:20:01 +0200435 /*
436 * all events in a group must monitor
437 * the same cgroup because a task belongs
438 * to only one perf cgroup at a time
439 */
440 if (group_leader && group_leader->cgrp != cgrp) {
441 perf_detach_cgroup(event);
442 ret = -EINVAL;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200443 }
Li Zefan3db272c2011-03-03 14:25:37 +0800444out:
Stephane Eraniane5d13672011-02-14 11:20:01 +0200445 fput_light(file, fput_needed);
446 return ret;
447}
448
449static inline void
450perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
451{
452 struct perf_cgroup_info *t;
453 t = per_cpu_ptr(event->cgrp->info, event->cpu);
454 event->shadow_ctx_time = now - t->timestamp;
455}
456
457static inline void
458perf_cgroup_defer_enabled(struct perf_event *event)
459{
460 /*
461 * when the current task's perf cgroup does not match
462 * the event's, we need to remember to call the
463 * perf_mark_enable() function the first time a task with
464 * a matching perf cgroup is scheduled in.
465 */
466 if (is_cgroup_event(event) && !perf_cgroup_match(event))
467 event->cgrp_defer_enabled = 1;
468}
469
470static inline void
471perf_cgroup_mark_enabled(struct perf_event *event,
472 struct perf_event_context *ctx)
473{
474 struct perf_event *sub;
475 u64 tstamp = perf_event_time(event);
476
477 if (!event->cgrp_defer_enabled)
478 return;
479
480 event->cgrp_defer_enabled = 0;
481
482 event->tstamp_enabled = tstamp - event->total_time_enabled;
483 list_for_each_entry(sub, &event->sibling_list, group_entry) {
484 if (sub->state >= PERF_EVENT_STATE_INACTIVE) {
485 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
486 sub->cgrp_defer_enabled = 0;
487 }
488 }
489}
490#else /* !CONFIG_CGROUP_PERF */
491
492static inline bool
493perf_cgroup_match(struct perf_event *event)
494{
495 return true;
496}
497
498static inline void perf_detach_cgroup(struct perf_event *event)
499{}
500
501static inline int is_cgroup_event(struct perf_event *event)
502{
503 return 0;
504}
505
506static inline u64 perf_cgroup_event_cgrp_time(struct perf_event *event)
507{
508 return 0;
509}
510
511static inline void update_cgrp_time_from_event(struct perf_event *event)
512{
513}
514
515static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
516{
517}
518
519static inline void perf_cgroup_sched_out(struct task_struct *task)
520{
521}
522
523static inline void perf_cgroup_sched_in(struct task_struct *task)
524{
525}
526
527static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event,
528 struct perf_event_attr *attr,
529 struct perf_event *group_leader)
530{
531 return -EINVAL;
532}
533
534static inline void
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200535perf_cgroup_set_timestamp(struct task_struct *task,
536 struct perf_event_context *ctx)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200537{
538}
539
540void
541perf_cgroup_switch(struct task_struct *task, struct task_struct *next)
542{
543}
544
545static inline void
546perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
547{
548}
549
550static inline u64 perf_cgroup_event_time(struct perf_event *event)
551{
552 return 0;
553}
554
555static inline void
556perf_cgroup_defer_enabled(struct perf_event *event)
557{
558}
559
560static inline void
561perf_cgroup_mark_enabled(struct perf_event *event,
562 struct perf_event_context *ctx)
563{
564}
565#endif
566
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200567void perf_pmu_disable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200568{
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200569 int *count = this_cpu_ptr(pmu->pmu_disable_count);
570 if (!(*count)++)
571 pmu->pmu_disable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200572}
573
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200574void perf_pmu_enable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200575{
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200576 int *count = this_cpu_ptr(pmu->pmu_disable_count);
577 if (!--(*count))
578 pmu->pmu_enable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200579}
580
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200581static DEFINE_PER_CPU(struct list_head, rotation_list);
582
583/*
584 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
585 * because they're strictly cpu affine and rotate_start is called with IRQs
586 * disabled, while rotate_context is called from IRQ context.
587 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200588static void perf_pmu_rotate_start(struct pmu *pmu)
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200589{
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200590 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200591 struct list_head *head = &__get_cpu_var(rotation_list);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200592
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200593 WARN_ON(!irqs_disabled());
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200594
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200595 if (list_empty(&cpuctx->rotation_list))
596 list_add(&cpuctx->rotation_list, head);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200597}
598
599static void get_ctx(struct perf_event_context *ctx)
600{
601 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
602}
603
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200604static void put_ctx(struct perf_event_context *ctx)
605{
606 if (atomic_dec_and_test(&ctx->refcount)) {
607 if (ctx->parent_ctx)
608 put_ctx(ctx->parent_ctx);
609 if (ctx->task)
610 put_task_struct(ctx->task);
Lai Jiangshancb796ff2011-03-18 12:07:41 +0800611 kfree_rcu(ctx, rcu_head);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200612 }
613}
614
615static void unclone_ctx(struct perf_event_context *ctx)
616{
617 if (ctx->parent_ctx) {
618 put_ctx(ctx->parent_ctx);
619 ctx->parent_ctx = NULL;
620 }
621}
622
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -0200623static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
624{
625 /*
626 * only top level events have the pid namespace they were created in
627 */
628 if (event->parent)
629 event = event->parent;
630
631 return task_tgid_nr_ns(p, event->ns);
632}
633
634static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
635{
636 /*
637 * only top level events have the pid namespace they were created in
638 */
639 if (event->parent)
640 event = event->parent;
641
642 return task_pid_nr_ns(p, event->ns);
643}
644
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200645/*
646 * If we inherit events we want to return the parent event id
647 * to userspace.
648 */
649static u64 primary_event_id(struct perf_event *event)
650{
651 u64 id = event->id;
652
653 if (event->parent)
654 id = event->parent->id;
655
656 return id;
657}
658
659/*
660 * Get the perf_event_context for a task and lock it.
661 * This has to cope with with the fact that until it is locked,
662 * the context could get moved to another task.
663 */
664static struct perf_event_context *
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +0200665perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200666{
667 struct perf_event_context *ctx;
668
669 rcu_read_lock();
Peter Zijlstra9ed60602010-06-11 17:36:35 +0200670retry:
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +0200671 ctx = rcu_dereference(task->perf_event_ctxp[ctxn]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200672 if (ctx) {
673 /*
674 * If this context is a clone of another, it might
675 * get swapped for another underneath us by
676 * perf_event_task_sched_out, though the
677 * rcu_read_lock() protects us from any context
678 * getting freed. Lock the context and check if it
679 * got swapped before we could get the lock, and retry
680 * if so. If we locked the right context, then it
681 * can't get swapped on us any more.
682 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100683 raw_spin_lock_irqsave(&ctx->lock, *flags);
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +0200684 if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100685 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200686 goto retry;
687 }
688
689 if (!atomic_inc_not_zero(&ctx->refcount)) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100690 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200691 ctx = NULL;
692 }
693 }
694 rcu_read_unlock();
695 return ctx;
696}
697
698/*
699 * Get the context for a task and increment its pin_count so it
700 * can't get swapped to another task. This also increments its
701 * reference count so that the context can't get freed.
702 */
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +0200703static struct perf_event_context *
704perf_pin_task_context(struct task_struct *task, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200705{
706 struct perf_event_context *ctx;
707 unsigned long flags;
708
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +0200709 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200710 if (ctx) {
711 ++ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100712 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200713 }
714 return ctx;
715}
716
717static void perf_unpin_context(struct perf_event_context *ctx)
718{
719 unsigned long flags;
720
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100721 raw_spin_lock_irqsave(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200722 --ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100723 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200724}
725
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100726/*
727 * Update the record of the current time in a context.
728 */
729static void update_context_time(struct perf_event_context *ctx)
730{
731 u64 now = perf_clock();
732
733 ctx->time += now - ctx->timestamp;
734 ctx->timestamp = now;
735}
736
Stephane Eranian41587552011-01-03 18:20:01 +0200737static u64 perf_event_time(struct perf_event *event)
738{
739 struct perf_event_context *ctx = event->ctx;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200740
741 if (is_cgroup_event(event))
742 return perf_cgroup_event_time(event);
743
Stephane Eranian41587552011-01-03 18:20:01 +0200744 return ctx ? ctx->time : 0;
745}
746
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100747/*
748 * Update the total_time_enabled and total_time_running fields for a event.
749 */
750static void update_event_times(struct perf_event *event)
751{
752 struct perf_event_context *ctx = event->ctx;
753 u64 run_end;
754
755 if (event->state < PERF_EVENT_STATE_INACTIVE ||
756 event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
757 return;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200758 /*
759 * in cgroup mode, time_enabled represents
760 * the time the event was enabled AND active
761 * tasks were in the monitored cgroup. This is
762 * independent of the activity of the context as
763 * there may be a mix of cgroup and non-cgroup events.
764 *
765 * That is why we treat cgroup events differently
766 * here.
767 */
768 if (is_cgroup_event(event))
Stephane Eranian41587552011-01-03 18:20:01 +0200769 run_end = perf_event_time(event);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200770 else if (ctx->is_active)
771 run_end = ctx->time;
Peter Zijlstraacd1d7c2009-11-23 15:00:36 +0100772 else
773 run_end = event->tstamp_stopped;
774
775 event->total_time_enabled = run_end - event->tstamp_enabled;
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100776
777 if (event->state == PERF_EVENT_STATE_INACTIVE)
778 run_end = event->tstamp_stopped;
779 else
Stephane Eranian41587552011-01-03 18:20:01 +0200780 run_end = perf_event_time(event);
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100781
782 event->total_time_running = run_end - event->tstamp_running;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200783
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100784}
785
Peter Zijlstra96c21a42010-05-11 16:19:10 +0200786/*
787 * Update total_time_enabled and total_time_running for all events in a group.
788 */
789static void update_group_times(struct perf_event *leader)
790{
791 struct perf_event *event;
792
793 update_event_times(leader);
794 list_for_each_entry(event, &leader->sibling_list, group_entry)
795 update_event_times(event);
796}
797
Frederic Weisbecker889ff012010-01-09 20:04:47 +0100798static struct list_head *
799ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
800{
801 if (event->attr.pinned)
802 return &ctx->pinned_groups;
803 else
804 return &ctx->flexible_groups;
805}
806
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200807/*
808 * Add a event from the lists for its context.
809 * Must be called with ctx->mutex and ctx->lock held.
810 */
811static void
812list_add_event(struct perf_event *event, struct perf_event_context *ctx)
813{
Peter Zijlstra8a495422010-05-27 15:47:49 +0200814 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
815 event->attach_state |= PERF_ATTACH_CONTEXT;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200816
817 /*
Peter Zijlstra8a495422010-05-27 15:47:49 +0200818 * If we're a stand alone event or group leader, we go to the context
819 * list, group events are kept attached to the group so that
820 * perf_group_detach can, at all times, locate all siblings.
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200821 */
Peter Zijlstra8a495422010-05-27 15:47:49 +0200822 if (event->group_leader == event) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +0100823 struct list_head *list;
824
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +0100825 if (is_software_event(event))
826 event->group_flags |= PERF_GROUP_SOFTWARE;
827
Frederic Weisbecker889ff012010-01-09 20:04:47 +0100828 list = ctx_group_list(event, ctx);
829 list_add_tail(&event->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200830 }
831
Peter Zijlstra08309372011-03-03 11:31:20 +0100832 if (is_cgroup_event(event))
Stephane Eraniane5d13672011-02-14 11:20:01 +0200833 ctx->nr_cgroups++;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200834
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200835 list_add_rcu(&event->event_entry, &ctx->event_list);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200836 if (!ctx->nr_events)
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200837 perf_pmu_rotate_start(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200838 ctx->nr_events++;
839 if (event->attr.inherit_stat)
840 ctx->nr_stat++;
841}
842
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -0200843/*
844 * Called at perf_event creation and when events are attached/detached from a
845 * group.
846 */
847static void perf_event__read_size(struct perf_event *event)
848{
849 int entry = sizeof(u64); /* value */
850 int size = 0;
851 int nr = 1;
852
853 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
854 size += sizeof(u64);
855
856 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
857 size += sizeof(u64);
858
859 if (event->attr.read_format & PERF_FORMAT_ID)
860 entry += sizeof(u64);
861
862 if (event->attr.read_format & PERF_FORMAT_GROUP) {
863 nr += event->group_leader->nr_siblings;
864 size += sizeof(u64);
865 }
866
867 size += entry * nr;
868 event->read_size = size;
869}
870
871static void perf_event__header_size(struct perf_event *event)
872{
873 struct perf_sample_data *data;
874 u64 sample_type = event->attr.sample_type;
875 u16 size = 0;
876
877 perf_event__read_size(event);
878
879 if (sample_type & PERF_SAMPLE_IP)
880 size += sizeof(data->ip);
881
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -0200882 if (sample_type & PERF_SAMPLE_ADDR)
883 size += sizeof(data->addr);
884
885 if (sample_type & PERF_SAMPLE_PERIOD)
886 size += sizeof(data->period);
887
888 if (sample_type & PERF_SAMPLE_READ)
889 size += event->read_size;
890
891 event->header_size = size;
892}
893
894static void perf_event__id_header_size(struct perf_event *event)
895{
896 struct perf_sample_data *data;
897 u64 sample_type = event->attr.sample_type;
898 u16 size = 0;
899
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -0200900 if (sample_type & PERF_SAMPLE_TID)
901 size += sizeof(data->tid_entry);
902
903 if (sample_type & PERF_SAMPLE_TIME)
904 size += sizeof(data->time);
905
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -0200906 if (sample_type & PERF_SAMPLE_ID)
907 size += sizeof(data->id);
908
909 if (sample_type & PERF_SAMPLE_STREAM_ID)
910 size += sizeof(data->stream_id);
911
912 if (sample_type & PERF_SAMPLE_CPU)
913 size += sizeof(data->cpu_entry);
914
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -0200915 event->id_header_size = size;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -0200916}
917
Peter Zijlstra8a495422010-05-27 15:47:49 +0200918static void perf_group_attach(struct perf_event *event)
919{
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -0200920 struct perf_event *group_leader = event->group_leader, *pos;
Peter Zijlstra8a495422010-05-27 15:47:49 +0200921
Peter Zijlstra74c33372010-10-15 11:40:29 +0200922 /*
923 * We can have double attach due to group movement in perf_event_open.
924 */
925 if (event->attach_state & PERF_ATTACH_GROUP)
926 return;
927
Peter Zijlstra8a495422010-05-27 15:47:49 +0200928 event->attach_state |= PERF_ATTACH_GROUP;
929
930 if (group_leader == event)
931 return;
932
933 if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
934 !is_software_event(event))
935 group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
936
937 list_add_tail(&event->group_entry, &group_leader->sibling_list);
938 group_leader->nr_siblings++;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -0200939
940 perf_event__header_size(group_leader);
941
942 list_for_each_entry(pos, &group_leader->sibling_list, group_entry)
943 perf_event__header_size(pos);
Peter Zijlstra8a495422010-05-27 15:47:49 +0200944}
945
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200946/*
947 * Remove a event from the lists for its context.
948 * Must be called with ctx->mutex and ctx->lock held.
949 */
950static void
951list_del_event(struct perf_event *event, struct perf_event_context *ctx)
952{
Stephane Eranian68cacd22011-03-23 16:03:06 +0100953 struct perf_cpu_context *cpuctx;
Peter Zijlstra8a495422010-05-27 15:47:49 +0200954 /*
955 * We can have double detach due to exit/hot-unplug + close.
956 */
957 if (!(event->attach_state & PERF_ATTACH_CONTEXT))
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200958 return;
Peter Zijlstra8a495422010-05-27 15:47:49 +0200959
960 event->attach_state &= ~PERF_ATTACH_CONTEXT;
961
Stephane Eranian68cacd22011-03-23 16:03:06 +0100962 if (is_cgroup_event(event)) {
Stephane Eraniane5d13672011-02-14 11:20:01 +0200963 ctx->nr_cgroups--;
Stephane Eranian68cacd22011-03-23 16:03:06 +0100964 cpuctx = __get_cpu_context(ctx);
965 /*
966 * if there are no more cgroup events
967 * then cler cgrp to avoid stale pointer
968 * in update_cgrp_time_from_cpuctx()
969 */
970 if (!ctx->nr_cgroups)
971 cpuctx->cgrp = NULL;
972 }
Stephane Eraniane5d13672011-02-14 11:20:01 +0200973
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200974 ctx->nr_events--;
975 if (event->attr.inherit_stat)
976 ctx->nr_stat--;
977
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200978 list_del_rcu(&event->event_entry);
979
Peter Zijlstra8a495422010-05-27 15:47:49 +0200980 if (event->group_leader == event)
981 list_del_init(&event->group_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200982
Peter Zijlstra96c21a42010-05-11 16:19:10 +0200983 update_group_times(event);
Stephane Eranianb2e74a22009-11-26 09:24:30 -0800984
985 /*
986 * If event was in error state, then keep it
987 * that way, otherwise bogus counts will be
988 * returned on read(). The only way to get out
989 * of error state is by explicit re-enabling
990 * of the event
991 */
992 if (event->state > PERF_EVENT_STATE_OFF)
993 event->state = PERF_EVENT_STATE_OFF;
Peter Zijlstra050735b2010-05-11 11:51:53 +0200994}
995
Peter Zijlstra8a495422010-05-27 15:47:49 +0200996static void perf_group_detach(struct perf_event *event)
Peter Zijlstra050735b2010-05-11 11:51:53 +0200997{
998 struct perf_event *sibling, *tmp;
Peter Zijlstra8a495422010-05-27 15:47:49 +0200999 struct list_head *list = NULL;
1000
1001 /*
1002 * We can have double detach due to exit/hot-unplug + close.
1003 */
1004 if (!(event->attach_state & PERF_ATTACH_GROUP))
1005 return;
1006
1007 event->attach_state &= ~PERF_ATTACH_GROUP;
1008
1009 /*
1010 * If this is a sibling, remove it from its group.
1011 */
1012 if (event->group_leader != event) {
1013 list_del_init(&event->group_entry);
1014 event->group_leader->nr_siblings--;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001015 goto out;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001016 }
1017
1018 if (!list_empty(&event->group_entry))
1019 list = &event->group_entry;
Peter Zijlstra2e2af502009-11-23 11:37:25 +01001020
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001021 /*
1022 * If this was a group event with sibling events then
1023 * upgrade the siblings to singleton events by adding them
Peter Zijlstra8a495422010-05-27 15:47:49 +02001024 * to whatever list we are on.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001025 */
1026 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
Peter Zijlstra8a495422010-05-27 15:47:49 +02001027 if (list)
1028 list_move_tail(&sibling->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001029 sibling->group_leader = sibling;
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01001030
1031 /* Inherit group flags from the previous leader */
1032 sibling->group_flags = event->group_flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001033 }
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001034
1035out:
1036 perf_event__header_size(event->group_leader);
1037
1038 list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry)
1039 perf_event__header_size(tmp);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001040}
1041
Stephane Eranianfa66f072010-08-26 16:40:01 +02001042static inline int
1043event_filter_match(struct perf_event *event)
1044{
Stephane Eraniane5d13672011-02-14 11:20:01 +02001045 return (event->cpu == -1 || event->cpu == smp_processor_id())
1046 && perf_cgroup_match(event);
Stephane Eranianfa66f072010-08-26 16:40:01 +02001047}
1048
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001049static void
1050event_sched_out(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001051 struct perf_cpu_context *cpuctx,
1052 struct perf_event_context *ctx)
1053{
Stephane Eranian41587552011-01-03 18:20:01 +02001054 u64 tstamp = perf_event_time(event);
Stephane Eranianfa66f072010-08-26 16:40:01 +02001055 u64 delta;
1056 /*
1057 * An event which could not be activated because of
1058 * filter mismatch still needs to have its timings
1059 * maintained, otherwise bogus information is return
1060 * via read() for time_enabled, time_running:
1061 */
1062 if (event->state == PERF_EVENT_STATE_INACTIVE
1063 && !event_filter_match(event)) {
Stephane Eraniane5d13672011-02-14 11:20:01 +02001064 delta = tstamp - event->tstamp_stopped;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001065 event->tstamp_running += delta;
Stephane Eranian41587552011-01-03 18:20:01 +02001066 event->tstamp_stopped = tstamp;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001067 }
1068
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001069 if (event->state != PERF_EVENT_STATE_ACTIVE)
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001070 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001071
1072 event->state = PERF_EVENT_STATE_INACTIVE;
1073 if (event->pending_disable) {
1074 event->pending_disable = 0;
1075 event->state = PERF_EVENT_STATE_OFF;
1076 }
Stephane Eranian41587552011-01-03 18:20:01 +02001077 event->tstamp_stopped = tstamp;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001078 event->pmu->del(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001079 event->oncpu = -1;
1080
1081 if (!is_software_event(event))
1082 cpuctx->active_oncpu--;
1083 ctx->nr_active--;
1084 if (event->attr.exclusive || !cpuctx->active_oncpu)
1085 cpuctx->exclusive = 0;
1086}
1087
1088static void
1089group_sched_out(struct perf_event *group_event,
1090 struct perf_cpu_context *cpuctx,
1091 struct perf_event_context *ctx)
1092{
1093 struct perf_event *event;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001094 int state = group_event->state;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001095
1096 event_sched_out(group_event, cpuctx, ctx);
1097
1098 /*
1099 * Schedule out siblings (if any):
1100 */
1101 list_for_each_entry(event, &group_event->sibling_list, group_entry)
1102 event_sched_out(event, cpuctx, ctx);
1103
Stephane Eranianfa66f072010-08-26 16:40:01 +02001104 if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001105 cpuctx->exclusive = 0;
1106}
1107
1108/*
1109 * Cross CPU call to remove a performance event
1110 *
1111 * We disable the event on the hardware level first. After that we
1112 * remove it from the context list.
1113 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001114static int __perf_remove_from_context(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001115{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001116 struct perf_event *event = info;
1117 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001118 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001119
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001120 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001121 event_sched_out(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001122 list_del_event(event, ctx);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001123 raw_spin_unlock(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001124
1125 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001126}
1127
1128
1129/*
1130 * Remove the event from a task's (or a CPU's) list of events.
1131 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001132 * CPU events are removed with a smp call. For task events we only
1133 * call when the task is on a CPU.
1134 *
1135 * If event->ctx is a cloned context, callers must make sure that
1136 * every task struct that event->ctx->task could possibly point to
1137 * remains valid. This is OK when called from perf_release since
1138 * that only calls us on the top-level context, which can't be a clone.
1139 * When called from perf_event_exit_task, it's OK because the
1140 * context has been detached from its task.
1141 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001142static void perf_remove_from_context(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001143{
1144 struct perf_event_context *ctx = event->ctx;
1145 struct task_struct *task = ctx->task;
1146
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001147 lockdep_assert_held(&ctx->mutex);
1148
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001149 if (!task) {
1150 /*
1151 * Per cpu events are removed via an smp call and
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001152 * the removal is always successful.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001153 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001154 cpu_function_call(event->cpu, __perf_remove_from_context, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001155 return;
1156 }
1157
1158retry:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001159 if (!task_function_call(task, __perf_remove_from_context, event))
1160 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001161
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001162 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001163 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001164 * If we failed to find a running task, but find the context active now
1165 * that we've acquired the ctx->lock, retry.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001166 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001167 if (ctx->is_active) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001168 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001169 goto retry;
1170 }
1171
1172 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001173 * Since the task isn't running, its safe to remove the event, us
1174 * holding the ctx->lock ensures the task won't get scheduled in.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001175 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001176 list_del_event(event, ctx);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001177 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001178}
1179
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001180/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001181 * Cross CPU call to disable a performance event
1182 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001183static int __perf_event_disable(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001184{
1185 struct perf_event *event = info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001186 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001187 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001188
1189 /*
1190 * If this is a per-task event, need to check whether this
1191 * event's task is the current task on this cpu.
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001192 *
1193 * Can trigger due to concurrent perf_event_context_sched_out()
1194 * flipping contexts around.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001195 */
1196 if (ctx->task && cpuctx->task_ctx != ctx)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001197 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001198
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001199 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001200
1201 /*
1202 * If the event is on, turn it off.
1203 * If it is in error state, leave it in error state.
1204 */
1205 if (event->state >= PERF_EVENT_STATE_INACTIVE) {
1206 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001207 update_cgrp_time_from_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001208 update_group_times(event);
1209 if (event == event->group_leader)
1210 group_sched_out(event, cpuctx, ctx);
1211 else
1212 event_sched_out(event, cpuctx, ctx);
1213 event->state = PERF_EVENT_STATE_OFF;
1214 }
1215
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001216 raw_spin_unlock(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001217
1218 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001219}
1220
1221/*
1222 * Disable a event.
1223 *
1224 * If event->ctx is a cloned context, callers must make sure that
1225 * every task struct that event->ctx->task could possibly point to
1226 * remains valid. This condition is satisifed when called through
1227 * perf_event_for_each_child or perf_event_for_each because they
1228 * hold the top-level event's child_mutex, so any descendant that
1229 * goes to exit will block in sync_child_event.
1230 * When called from perf_pending_event it's OK because event->ctx
1231 * is the current context on this CPU and preemption is disabled,
1232 * hence we can't get into perf_event_task_sched_out for this context.
1233 */
Frederic Weisbecker44234ad2009-12-09 09:25:48 +01001234void perf_event_disable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001235{
1236 struct perf_event_context *ctx = event->ctx;
1237 struct task_struct *task = ctx->task;
1238
1239 if (!task) {
1240 /*
1241 * Disable the event on the cpu that it's on
1242 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001243 cpu_function_call(event->cpu, __perf_event_disable, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001244 return;
1245 }
1246
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001247retry:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001248 if (!task_function_call(task, __perf_event_disable, event))
1249 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001250
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001251 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001252 /*
1253 * If the event is still active, we need to retry the cross-call.
1254 */
1255 if (event->state == PERF_EVENT_STATE_ACTIVE) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001256 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001257 /*
1258 * Reload the task pointer, it might have been changed by
1259 * a concurrent perf_event_context_sched_out().
1260 */
1261 task = ctx->task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001262 goto retry;
1263 }
1264
1265 /*
1266 * Since we have the lock this context can't be scheduled
1267 * in, so we can change the state safely.
1268 */
1269 if (event->state == PERF_EVENT_STATE_INACTIVE) {
1270 update_group_times(event);
1271 event->state = PERF_EVENT_STATE_OFF;
1272 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001273 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001274}
1275
Stephane Eraniane5d13672011-02-14 11:20:01 +02001276static void perf_set_shadow_time(struct perf_event *event,
1277 struct perf_event_context *ctx,
1278 u64 tstamp)
1279{
1280 /*
1281 * use the correct time source for the time snapshot
1282 *
1283 * We could get by without this by leveraging the
1284 * fact that to get to this function, the caller
1285 * has most likely already called update_context_time()
1286 * and update_cgrp_time_xx() and thus both timestamp
1287 * are identical (or very close). Given that tstamp is,
1288 * already adjusted for cgroup, we could say that:
1289 * tstamp - ctx->timestamp
1290 * is equivalent to
1291 * tstamp - cgrp->timestamp.
1292 *
1293 * Then, in perf_output_read(), the calculation would
1294 * work with no changes because:
1295 * - event is guaranteed scheduled in
1296 * - no scheduled out in between
1297 * - thus the timestamp would be the same
1298 *
1299 * But this is a bit hairy.
1300 *
1301 * So instead, we have an explicit cgroup call to remain
1302 * within the time time source all along. We believe it
1303 * is cleaner and simpler to understand.
1304 */
1305 if (is_cgroup_event(event))
1306 perf_cgroup_set_shadow_time(event, tstamp);
1307 else
1308 event->shadow_ctx_time = tstamp - ctx->timestamp;
1309}
1310
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01001311#define MAX_INTERRUPTS (~0ULL)
1312
1313static void perf_log_throttle(struct perf_event *event, int enable);
1314
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001315static int
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001316event_sched_in(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001317 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001318 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001319{
Stephane Eranian41587552011-01-03 18:20:01 +02001320 u64 tstamp = perf_event_time(event);
1321
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001322 if (event->state <= PERF_EVENT_STATE_OFF)
1323 return 0;
1324
1325 event->state = PERF_EVENT_STATE_ACTIVE;
Peter Zijlstra6e377382010-02-11 13:21:58 +01001326 event->oncpu = smp_processor_id();
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01001327
1328 /*
1329 * Unthrottle events, since we scheduled we might have missed several
1330 * ticks already, also for a heavily scheduling task there is little
1331 * guarantee it'll get a tick in a timely manner.
1332 */
1333 if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) {
1334 perf_log_throttle(event, 1);
1335 event->hw.interrupts = 0;
1336 }
1337
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001338 /*
1339 * The new state must be visible before we turn it on in the hardware:
1340 */
1341 smp_wmb();
1342
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001343 if (event->pmu->add(event, PERF_EF_START)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001344 event->state = PERF_EVENT_STATE_INACTIVE;
1345 event->oncpu = -1;
1346 return -EAGAIN;
1347 }
1348
Stephane Eranian41587552011-01-03 18:20:01 +02001349 event->tstamp_running += tstamp - event->tstamp_stopped;
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001350
Stephane Eraniane5d13672011-02-14 11:20:01 +02001351 perf_set_shadow_time(event, ctx, tstamp);
Stephane Eranianeed01522010-10-26 16:08:01 +02001352
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001353 if (!is_software_event(event))
1354 cpuctx->active_oncpu++;
1355 ctx->nr_active++;
1356
1357 if (event->attr.exclusive)
1358 cpuctx->exclusive = 1;
1359
1360 return 0;
1361}
1362
1363static int
1364group_sched_in(struct perf_event *group_event,
1365 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001366 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001367{
Lin Ming6bde9b62010-04-23 13:56:00 +08001368 struct perf_event *event, *partial_group = NULL;
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02001369 struct pmu *pmu = group_event->pmu;
Stephane Eraniand7842da2010-10-20 15:25:01 +02001370 u64 now = ctx->time;
1371 bool simulate = false;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001372
1373 if (group_event->state == PERF_EVENT_STATE_OFF)
1374 return 0;
1375
Peter Zijlstraad5133b2010-06-15 12:22:39 +02001376 pmu->start_txn(pmu);
Lin Ming6bde9b62010-04-23 13:56:00 +08001377
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001378 if (event_sched_in(group_event, cpuctx, ctx)) {
Peter Zijlstraad5133b2010-06-15 12:22:39 +02001379 pmu->cancel_txn(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001380 return -EAGAIN;
Stephane Eranian90151c352010-05-25 16:23:10 +02001381 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001382
1383 /*
1384 * Schedule in siblings as one group (if any):
1385 */
1386 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001387 if (event_sched_in(event, cpuctx, ctx)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001388 partial_group = event;
1389 goto group_error;
1390 }
1391 }
1392
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001393 if (!pmu->commit_txn(pmu))
Paul Mackerras6e851582010-05-08 20:58:00 +10001394 return 0;
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001395
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001396group_error:
1397 /*
1398 * Groups can be scheduled in as one unit only, so undo any
1399 * partial group before returning:
Stephane Eraniand7842da2010-10-20 15:25:01 +02001400 * The events up to the failed event are scheduled out normally,
1401 * tstamp_stopped will be updated.
1402 *
1403 * The failed events and the remaining siblings need to have
1404 * their timings updated as if they had gone thru event_sched_in()
1405 * and event_sched_out(). This is required to get consistent timings
1406 * across the group. This also takes care of the case where the group
1407 * could never be scheduled by ensuring tstamp_stopped is set to mark
1408 * the time the event was actually stopped, such that time delta
1409 * calculation in update_event_times() is correct.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001410 */
1411 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
1412 if (event == partial_group)
Stephane Eraniand7842da2010-10-20 15:25:01 +02001413 simulate = true;
1414
1415 if (simulate) {
1416 event->tstamp_running += now - event->tstamp_stopped;
1417 event->tstamp_stopped = now;
1418 } else {
1419 event_sched_out(event, cpuctx, ctx);
1420 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001421 }
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001422 event_sched_out(group_event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001423
Peter Zijlstraad5133b2010-06-15 12:22:39 +02001424 pmu->cancel_txn(pmu);
Stephane Eranian90151c352010-05-25 16:23:10 +02001425
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001426 return -EAGAIN;
1427}
1428
1429/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001430 * Work out whether we can put this event group on the CPU now.
1431 */
1432static int group_can_go_on(struct perf_event *event,
1433 struct perf_cpu_context *cpuctx,
1434 int can_add_hw)
1435{
1436 /*
1437 * Groups consisting entirely of software events can always go on.
1438 */
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01001439 if (event->group_flags & PERF_GROUP_SOFTWARE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001440 return 1;
1441 /*
1442 * If an exclusive group is already on, no other hardware
1443 * events can go on.
1444 */
1445 if (cpuctx->exclusive)
1446 return 0;
1447 /*
1448 * If this group is exclusive and there are already
1449 * events on the CPU, it can't go on.
1450 */
1451 if (event->attr.exclusive && cpuctx->active_oncpu)
1452 return 0;
1453 /*
1454 * Otherwise, try to add it if all previous groups were able
1455 * to go on.
1456 */
1457 return can_add_hw;
1458}
1459
1460static void add_event_to_ctx(struct perf_event *event,
1461 struct perf_event_context *ctx)
1462{
Stephane Eranian41587552011-01-03 18:20:01 +02001463 u64 tstamp = perf_event_time(event);
1464
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001465 list_add_event(event, ctx);
Peter Zijlstra8a495422010-05-27 15:47:49 +02001466 perf_group_attach(event);
Stephane Eranian41587552011-01-03 18:20:01 +02001467 event->tstamp_enabled = tstamp;
1468 event->tstamp_running = tstamp;
1469 event->tstamp_stopped = tstamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001470}
1471
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001472static void task_ctx_sched_out(struct perf_event_context *ctx);
1473static void
1474ctx_sched_in(struct perf_event_context *ctx,
1475 struct perf_cpu_context *cpuctx,
1476 enum event_type_t event_type,
1477 struct task_struct *task);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001478
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001479/*
1480 * Cross CPU call to install and enable a performance event
1481 *
1482 * Must be called with ctx->mutex held
1483 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001484static int __perf_install_in_context(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001485{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001486 struct perf_event *event = info;
1487 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001488 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001489 struct perf_event_context *task_ctx = cpuctx->task_ctx;
1490 struct task_struct *task = current;
1491
1492 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
1493 perf_pmu_disable(cpuctx->ctx.pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001494
1495 /*
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001496 * If there was an active task_ctx schedule it out.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001497 */
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001498 if (task_ctx) {
1499 task_ctx_sched_out(task_ctx);
1500 /*
1501 * If the context we're installing events in is not the
1502 * active task_ctx, flip them.
1503 */
1504 if (ctx->task && task_ctx != ctx) {
1505 raw_spin_unlock(&cpuctx->ctx.lock);
1506 raw_spin_lock(&ctx->lock);
1507 cpuctx->task_ctx = task_ctx = ctx;
1508 }
1509 task = task_ctx->task;
1510 }
1511 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001512
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001513 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001514 /*
1515 * update cgrp time only if current cgrp
1516 * matches event->cgrp. Must be done before
1517 * calling add_event_to_ctx()
1518 */
1519 update_cgrp_time_from_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001520
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001521 add_event_to_ctx(event, ctx);
1522
1523 /*
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001524 * Schedule everything back in
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001525 */
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001526 cpu_ctx_sched_in(cpuctx, EVENT_PINNED, task);
1527 if (task_ctx)
1528 ctx_sched_in(task_ctx, cpuctx, EVENT_PINNED, task);
1529 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task);
1530 if (task_ctx)
1531 ctx_sched_in(task_ctx, cpuctx, EVENT_FLEXIBLE, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001532
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001533 perf_pmu_enable(cpuctx->ctx.pmu);
1534 perf_ctx_unlock(cpuctx, task_ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001535
1536 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001537}
1538
1539/*
1540 * Attach a performance event to a context
1541 *
1542 * First we add the event to the list with the hardware enable bit
1543 * in event->hw_config cleared.
1544 *
1545 * If the event is attached to a task which is on a CPU we use a smp
1546 * call to enable it in the task context. The task might have been
1547 * scheduled away, but we check this in the smp call again.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001548 */
1549static void
1550perf_install_in_context(struct perf_event_context *ctx,
1551 struct perf_event *event,
1552 int cpu)
1553{
1554 struct task_struct *task = ctx->task;
1555
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001556 lockdep_assert_held(&ctx->mutex);
1557
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02001558 event->ctx = ctx;
1559
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001560 if (!task) {
1561 /*
1562 * Per cpu events are installed via an smp call and
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001563 * the install is always successful.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001564 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001565 cpu_function_call(cpu, __perf_install_in_context, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001566 return;
1567 }
1568
1569retry:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001570 if (!task_function_call(task, __perf_install_in_context, event))
1571 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001572
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001573 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001574 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001575 * If we failed to find a running task, but find the context active now
1576 * that we've acquired the ctx->lock, retry.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001577 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001578 if (ctx->is_active) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001579 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001580 goto retry;
1581 }
1582
1583 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001584 * Since the task isn't running, its safe to add the event, us holding
1585 * the ctx->lock ensures the task won't get scheduled in.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001586 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001587 add_event_to_ctx(event, ctx);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001588 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001589}
1590
1591/*
1592 * Put a event into inactive state and update time fields.
1593 * Enabling the leader of a group effectively enables all
1594 * the group members that aren't explicitly disabled, so we
1595 * have to update their ->tstamp_enabled also.
1596 * Note: this works for group members as well as group leaders
1597 * since the non-leader members' sibling_lists will be empty.
1598 */
1599static void __perf_event_mark_enabled(struct perf_event *event,
1600 struct perf_event_context *ctx)
1601{
1602 struct perf_event *sub;
Stephane Eranian41587552011-01-03 18:20:01 +02001603 u64 tstamp = perf_event_time(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001604
1605 event->state = PERF_EVENT_STATE_INACTIVE;
Stephane Eranian41587552011-01-03 18:20:01 +02001606 event->tstamp_enabled = tstamp - event->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001607 list_for_each_entry(sub, &event->sibling_list, group_entry) {
Stephane Eranian41587552011-01-03 18:20:01 +02001608 if (sub->state >= PERF_EVENT_STATE_INACTIVE)
1609 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001610 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001611}
1612
1613/*
1614 * Cross CPU call to enable a performance event
1615 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001616static int __perf_event_enable(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001617{
1618 struct perf_event *event = info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001619 struct perf_event_context *ctx = event->ctx;
1620 struct perf_event *leader = event->group_leader;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001621 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001622 int err;
1623
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001624 if (WARN_ON_ONCE(!ctx->is_active))
1625 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001626
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001627 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001628 update_context_time(ctx);
1629
1630 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1631 goto unlock;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001632
1633 /*
1634 * set current task's cgroup time reference point
1635 */
Stephane Eranian3f7cce32011-02-18 14:40:01 +02001636 perf_cgroup_set_timestamp(current, ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001637
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001638 __perf_event_mark_enabled(event, ctx);
1639
Stephane Eraniane5d13672011-02-14 11:20:01 +02001640 if (!event_filter_match(event)) {
1641 if (is_cgroup_event(event))
1642 perf_cgroup_defer_enabled(event);
Peter Zijlstraf4c41762009-12-16 17:55:54 +01001643 goto unlock;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001644 }
Peter Zijlstraf4c41762009-12-16 17:55:54 +01001645
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001646 /*
1647 * If the event is in a group and isn't the group leader,
1648 * then don't put it on unless the group is on.
1649 */
1650 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
1651 goto unlock;
1652
1653 if (!group_can_go_on(event, cpuctx, 1)) {
1654 err = -EEXIST;
1655 } else {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001656 if (event == leader)
Peter Zijlstra6e377382010-02-11 13:21:58 +01001657 err = group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001658 else
Peter Zijlstra6e377382010-02-11 13:21:58 +01001659 err = event_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001660 }
1661
1662 if (err) {
1663 /*
1664 * If this event can't go on and it's part of a
1665 * group, then the whole group has to come off.
1666 */
1667 if (leader != event)
1668 group_sched_out(leader, cpuctx, ctx);
1669 if (leader->attr.pinned) {
1670 update_group_times(leader);
1671 leader->state = PERF_EVENT_STATE_ERROR;
1672 }
1673 }
1674
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001675unlock:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001676 raw_spin_unlock(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001677
1678 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001679}
1680
1681/*
1682 * Enable a event.
1683 *
1684 * If event->ctx is a cloned context, callers must make sure that
1685 * every task struct that event->ctx->task could possibly point to
1686 * remains valid. This condition is satisfied when called through
1687 * perf_event_for_each_child or perf_event_for_each as described
1688 * for perf_event_disable.
1689 */
Frederic Weisbecker44234ad2009-12-09 09:25:48 +01001690void perf_event_enable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001691{
1692 struct perf_event_context *ctx = event->ctx;
1693 struct task_struct *task = ctx->task;
1694
1695 if (!task) {
1696 /*
1697 * Enable the event on the cpu that it's on
1698 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001699 cpu_function_call(event->cpu, __perf_event_enable, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001700 return;
1701 }
1702
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001703 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001704 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1705 goto out;
1706
1707 /*
1708 * If the event is in error state, clear that first.
1709 * That way, if we see the event in error state below, we
1710 * know that it has gone back into error state, as distinct
1711 * from the task having been scheduled away before the
1712 * cross-call arrived.
1713 */
1714 if (event->state == PERF_EVENT_STATE_ERROR)
1715 event->state = PERF_EVENT_STATE_OFF;
1716
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001717retry:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001718 if (!ctx->is_active) {
1719 __perf_event_mark_enabled(event, ctx);
1720 goto out;
1721 }
1722
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001723 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001724
1725 if (!task_function_call(task, __perf_event_enable, event))
1726 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001727
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001728 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001729
1730 /*
1731 * If the context is active and the event is still off,
1732 * we need to retry the cross-call.
1733 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001734 if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF) {
1735 /*
1736 * task could have been flipped by a concurrent
1737 * perf_event_context_sched_out()
1738 */
1739 task = ctx->task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001740 goto retry;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001741 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001742
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001743out:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001744 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001745}
1746
1747static int perf_event_refresh(struct perf_event *event, int refresh)
1748{
1749 /*
1750 * not supported on inherited events
1751 */
Franck Bui-Huu2e939d12010-11-23 16:21:44 +01001752 if (event->attr.inherit || !is_sampling_event(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001753 return -EINVAL;
1754
1755 atomic_add(refresh, &event->event_limit);
1756 perf_event_enable(event);
1757
1758 return 0;
1759}
1760
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001761static void ctx_sched_out(struct perf_event_context *ctx,
1762 struct perf_cpu_context *cpuctx,
1763 enum event_type_t event_type)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001764{
1765 struct perf_event *event;
Peter Zijlstradb24d332011-04-09 21:17:45 +02001766 int is_active = ctx->is_active;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001767
Peter Zijlstradb24d332011-04-09 21:17:45 +02001768 ctx->is_active &= ~event_type;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001769 if (likely(!ctx->nr_events))
Peter Zijlstrafacc4302011-04-09 21:17:42 +02001770 return;
1771
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001772 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001773 update_cgrp_time_from_cpuctx(cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001774 if (!ctx->nr_active)
Peter Zijlstrafacc4302011-04-09 21:17:42 +02001775 return;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001776
Peter Zijlstra075e0b02011-04-09 21:17:40 +02001777 perf_pmu_disable(ctx->pmu);
Peter Zijlstradb24d332011-04-09 21:17:45 +02001778 if ((is_active & EVENT_PINNED) && (event_type & EVENT_PINNED)) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001779 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
1780 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001781 }
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001782
Peter Zijlstradb24d332011-04-09 21:17:45 +02001783 if ((is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE)) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001784 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08001785 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001786 }
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02001787 perf_pmu_enable(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001788}
1789
1790/*
1791 * Test whether two contexts are equivalent, i.e. whether they
1792 * have both been cloned from the same version of the same context
1793 * and they both have the same number of enabled events.
1794 * If the number of enabled events is the same, then the set
1795 * of enabled events should be the same, because these are both
1796 * inherited contexts, therefore we can't access individual events
1797 * in them directly with an fd; we can only enable/disable all
1798 * events via prctl, or enable/disable all events in a family
1799 * via ioctl, which will have the same effect on both contexts.
1800 */
1801static int context_equiv(struct perf_event_context *ctx1,
1802 struct perf_event_context *ctx2)
1803{
1804 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
1805 && ctx1->parent_gen == ctx2->parent_gen
1806 && !ctx1->pin_count && !ctx2->pin_count;
1807}
1808
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001809static void __perf_event_sync_stat(struct perf_event *event,
1810 struct perf_event *next_event)
1811{
1812 u64 value;
1813
1814 if (!event->attr.inherit_stat)
1815 return;
1816
1817 /*
1818 * Update the event value, we cannot use perf_event_read()
1819 * because we're in the middle of a context switch and have IRQs
1820 * disabled, which upsets smp_call_function_single(), however
1821 * we know the event must be on the current CPU, therefore we
1822 * don't need to use it.
1823 */
1824 switch (event->state) {
1825 case PERF_EVENT_STATE_ACTIVE:
Peter Zijlstra3dbebf12009-11-20 22:19:52 +01001826 event->pmu->read(event);
1827 /* fall-through */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001828
1829 case PERF_EVENT_STATE_INACTIVE:
1830 update_event_times(event);
1831 break;
1832
1833 default:
1834 break;
1835 }
1836
1837 /*
1838 * In order to keep per-task stats reliable we need to flip the event
1839 * values when we flip the contexts.
1840 */
Peter Zijlstrae7850592010-05-21 14:43:08 +02001841 value = local64_read(&next_event->count);
1842 value = local64_xchg(&event->count, value);
1843 local64_set(&next_event->count, value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001844
1845 swap(event->total_time_enabled, next_event->total_time_enabled);
1846 swap(event->total_time_running, next_event->total_time_running);
1847
1848 /*
1849 * Since we swizzled the values, update the user visible data too.
1850 */
1851 perf_event_update_userpage(event);
1852 perf_event_update_userpage(next_event);
1853}
1854
1855#define list_next_entry(pos, member) \
1856 list_entry(pos->member.next, typeof(*pos), member)
1857
1858static void perf_event_sync_stat(struct perf_event_context *ctx,
1859 struct perf_event_context *next_ctx)
1860{
1861 struct perf_event *event, *next_event;
1862
1863 if (!ctx->nr_stat)
1864 return;
1865
Peter Zijlstra02ffdbc2009-11-20 22:19:50 +01001866 update_context_time(ctx);
1867
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001868 event = list_first_entry(&ctx->event_list,
1869 struct perf_event, event_entry);
1870
1871 next_event = list_first_entry(&next_ctx->event_list,
1872 struct perf_event, event_entry);
1873
1874 while (&event->event_entry != &ctx->event_list &&
1875 &next_event->event_entry != &next_ctx->event_list) {
1876
1877 __perf_event_sync_stat(event, next_event);
1878
1879 event = list_next_entry(event, event_entry);
1880 next_event = list_next_entry(next_event, event_entry);
1881 }
1882}
1883
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001884static void perf_event_context_sched_out(struct task_struct *task, int ctxn,
1885 struct task_struct *next)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001886{
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02001887 struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001888 struct perf_event_context *next_ctx;
1889 struct perf_event_context *parent;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001890 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001891 int do_switch = 1;
1892
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001893 if (likely(!ctx))
1894 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001895
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001896 cpuctx = __get_cpu_context(ctx);
1897 if (!cpuctx->task_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001898 return;
1899
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001900 rcu_read_lock();
1901 parent = rcu_dereference(ctx->parent_ctx);
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02001902 next_ctx = next->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001903 if (parent && next_ctx &&
1904 rcu_dereference(next_ctx->parent_ctx) == parent) {
1905 /*
1906 * Looks like the two contexts are clones, so we might be
1907 * able to optimize the context switch. We lock both
1908 * contexts and check that they are clones under the
1909 * lock (including re-checking that neither has been
1910 * uncloned in the meantime). It doesn't matter which
1911 * order we take the locks because no other cpu could
1912 * be trying to lock both of these tasks.
1913 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001914 raw_spin_lock(&ctx->lock);
1915 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001916 if (context_equiv(ctx, next_ctx)) {
1917 /*
1918 * XXX do we need a memory barrier of sorts
1919 * wrt to rcu_dereference() of perf_event_ctxp
1920 */
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02001921 task->perf_event_ctxp[ctxn] = next_ctx;
1922 next->perf_event_ctxp[ctxn] = ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001923 ctx->task = next;
1924 next_ctx->task = task;
1925 do_switch = 0;
1926
1927 perf_event_sync_stat(ctx, next_ctx);
1928 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001929 raw_spin_unlock(&next_ctx->lock);
1930 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001931 }
1932 rcu_read_unlock();
1933
1934 if (do_switch) {
Peter Zijlstrafacc4302011-04-09 21:17:42 +02001935 raw_spin_lock(&ctx->lock);
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;
Peter Zijlstrafacc4302011-04-09 21:17:42 +02001938 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001939 }
1940}
1941
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02001942#define for_each_task_context_nr(ctxn) \
1943 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
1944
1945/*
1946 * Called from scheduler to remove the events of the current task,
1947 * with interrupts disabled.
1948 *
1949 * We stop each event and update the event value in event->count.
1950 *
1951 * This does not protect us against NMI, but disable()
1952 * sets the disabled bit in the control field of event _before_
1953 * accessing the event control register. If a NMI hits, then it will
1954 * not restart the event.
1955 */
Peter Zijlstra82cd6de2010-10-14 17:57:23 +02001956void __perf_event_task_sched_out(struct task_struct *task,
1957 struct task_struct *next)
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02001958{
1959 int ctxn;
1960
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02001961 for_each_task_context_nr(ctxn)
1962 perf_event_context_sched_out(task, ctxn, next);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001963
1964 /*
1965 * if cgroup events exist on this CPU, then we need
1966 * to check if we have to switch out PMU state.
1967 * cgroup event are system-wide mode only
1968 */
1969 if (atomic_read(&__get_cpu_var(perf_cgroup_events)))
1970 perf_cgroup_sched_out(task);
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02001971}
1972
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02001973static void task_ctx_sched_out(struct perf_event_context *ctx)
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
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02001983 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
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;
Peter Zijlstradb24d332011-04-09 21:17:45 +02002062 int is_active = ctx->is_active;
Stephane Eraniane5d13672011-02-14 11:20:01 +02002063
Peter Zijlstradb24d332011-04-09 21:17:45 +02002064 ctx->is_active |= event_type;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002065 if (likely(!ctx->nr_events))
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002066 return;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002067
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 */
Peter Zijlstradb24d332011-04-09 21:17:45 +02002075 if (!(is_active & EVENT_PINNED) && (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 */
Peter Zijlstradb24d332011-04-09 21:17:45 +02002079 if (!(is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE))
Peter Zijlstra6e377382010-02-11 13:21:58 +01002080 ctx_flexible_sched_in(ctx, cpuctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002081}
2082
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002083static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +02002084 enum event_type_t event_type,
2085 struct task_struct *task)
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002086{
2087 struct perf_event_context *ctx = &cpuctx->ctx;
2088
Stephane Eraniane5d13672011-02-14 11:20:01 +02002089 ctx_sched_in(ctx, cpuctx, event_type, task);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002090}
2091
Stephane Eraniane5d13672011-02-14 11:20:01 +02002092static void perf_event_context_sched_in(struct perf_event_context *ctx,
2093 struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002094{
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02002095 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002096
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002097 cpuctx = __get_cpu_context(ctx);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002098 if (cpuctx->task_ctx == ctx)
2099 return;
2100
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002101 perf_ctx_lock(cpuctx, ctx);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02002102 perf_pmu_disable(ctx->pmu);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002103 /*
2104 * We want to keep the following priority order:
2105 * cpu pinned (that don't need to move), task pinned,
2106 * cpu flexible, task flexible.
2107 */
2108 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
2109
Stephane Eraniane5d13672011-02-14 11:20:01 +02002110 ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task);
2111 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task);
2112 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002113
2114 cpuctx->task_ctx = ctx;
eranian@google.com9b33fa62010-03-10 22:26:05 -08002115
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002116 perf_pmu_enable(ctx->pmu);
2117 perf_ctx_unlock(cpuctx, ctx);
2118
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002119 /*
2120 * Since these rotations are per-cpu, we need to ensure the
2121 * cpu-context we got scheduled on is actually rotating.
2122 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002123 perf_pmu_rotate_start(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002124}
2125
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02002126/*
2127 * Called from scheduler to add the events of the current task
2128 * with interrupts disabled.
2129 *
2130 * We restore the event value and then enable it.
2131 *
2132 * This does not protect us against NMI, but enable()
2133 * sets the enabled bit in the control field of event _before_
2134 * accessing the event control register. If a NMI hits, then it will
2135 * keep the event running.
2136 */
Peter Zijlstra82cd6de2010-10-14 17:57:23 +02002137void __perf_event_task_sched_in(struct task_struct *task)
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02002138{
2139 struct perf_event_context *ctx;
2140 int ctxn;
2141
2142 for_each_task_context_nr(ctxn) {
2143 ctx = task->perf_event_ctxp[ctxn];
2144 if (likely(!ctx))
2145 continue;
2146
Stephane Eraniane5d13672011-02-14 11:20:01 +02002147 perf_event_context_sched_in(ctx, task);
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02002148 }
Stephane Eraniane5d13672011-02-14 11:20:01 +02002149 /*
2150 * if cgroup events exist on this CPU, then we need
2151 * to check if we have to switch in PMU state.
2152 * cgroup event are system-wide mode only
2153 */
2154 if (atomic_read(&__get_cpu_var(perf_cgroup_events)))
2155 perf_cgroup_sched_in(task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002156}
2157
Peter Zijlstraabd50712010-01-26 18:50:16 +01002158static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
2159{
2160 u64 frequency = event->attr.sample_freq;
2161 u64 sec = NSEC_PER_SEC;
2162 u64 divisor, dividend;
2163
2164 int count_fls, nsec_fls, frequency_fls, sec_fls;
2165
2166 count_fls = fls64(count);
2167 nsec_fls = fls64(nsec);
2168 frequency_fls = fls64(frequency);
2169 sec_fls = 30;
2170
2171 /*
2172 * We got @count in @nsec, with a target of sample_freq HZ
2173 * the target period becomes:
2174 *
2175 * @count * 10^9
2176 * period = -------------------
2177 * @nsec * sample_freq
2178 *
2179 */
2180
2181 /*
2182 * Reduce accuracy by one bit such that @a and @b converge
2183 * to a similar magnitude.
2184 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002185#define REDUCE_FLS(a, b) \
Peter Zijlstraabd50712010-01-26 18:50:16 +01002186do { \
2187 if (a##_fls > b##_fls) { \
2188 a >>= 1; \
2189 a##_fls--; \
2190 } else { \
2191 b >>= 1; \
2192 b##_fls--; \
2193 } \
2194} while (0)
2195
2196 /*
2197 * Reduce accuracy until either term fits in a u64, then proceed with
2198 * the other, so that finally we can do a u64/u64 division.
2199 */
2200 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
2201 REDUCE_FLS(nsec, frequency);
2202 REDUCE_FLS(sec, count);
2203 }
2204
2205 if (count_fls + sec_fls > 64) {
2206 divisor = nsec * frequency;
2207
2208 while (count_fls + sec_fls > 64) {
2209 REDUCE_FLS(count, sec);
2210 divisor >>= 1;
2211 }
2212
2213 dividend = count * sec;
2214 } else {
2215 dividend = count * sec;
2216
2217 while (nsec_fls + frequency_fls > 64) {
2218 REDUCE_FLS(nsec, frequency);
2219 dividend >>= 1;
2220 }
2221
2222 divisor = nsec * frequency;
2223 }
2224
Peter Zijlstraf6ab91ad2010-06-04 15:18:01 +02002225 if (!divisor)
2226 return dividend;
2227
Peter Zijlstraabd50712010-01-26 18:50:16 +01002228 return div64_u64(dividend, divisor);
2229}
2230
2231static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002232{
2233 struct hw_perf_event *hwc = &event->hw;
Peter Zijlstraf6ab91ad2010-06-04 15:18:01 +02002234 s64 period, sample_period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002235 s64 delta;
2236
Peter Zijlstraabd50712010-01-26 18:50:16 +01002237 period = perf_calculate_period(event, nsec, count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002238
2239 delta = (s64)(period - hwc->sample_period);
2240 delta = (delta + 7) / 8; /* low pass filter */
2241
2242 sample_period = hwc->sample_period + delta;
2243
2244 if (!sample_period)
2245 sample_period = 1;
2246
2247 hwc->sample_period = sample_period;
Peter Zijlstraabd50712010-01-26 18:50:16 +01002248
Peter Zijlstrae7850592010-05-21 14:43:08 +02002249 if (local64_read(&hwc->period_left) > 8*sample_period) {
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02002250 event->pmu->stop(event, PERF_EF_UPDATE);
Peter Zijlstrae7850592010-05-21 14:43:08 +02002251 local64_set(&hwc->period_left, 0);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02002252 event->pmu->start(event, PERF_EF_RELOAD);
Peter Zijlstraabd50712010-01-26 18:50:16 +01002253 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002254}
2255
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002256static void perf_ctx_adjust_freq(struct perf_event_context *ctx, u64 period)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002257{
2258 struct perf_event *event;
2259 struct hw_perf_event *hwc;
Peter Zijlstraabd50712010-01-26 18:50:16 +01002260 u64 interrupts, now;
2261 s64 delta;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002262
Paul Mackerras03541f82009-10-14 16:58:03 +11002263 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002264 if (event->state != PERF_EVENT_STATE_ACTIVE)
2265 continue;
2266
Stephane Eranian5632ab12011-01-03 18:20:01 +02002267 if (!event_filter_match(event))
Peter Zijlstra5d27c232009-12-17 13:16:32 +01002268 continue;
2269
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002270 hwc = &event->hw;
2271
2272 interrupts = hwc->interrupts;
2273 hwc->interrupts = 0;
2274
2275 /*
2276 * unthrottle events on the tick
2277 */
2278 if (interrupts == MAX_INTERRUPTS) {
2279 perf_log_throttle(event, 1);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02002280 event->pmu->start(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002281 }
2282
2283 if (!event->attr.freq || !event->attr.sample_freq)
2284 continue;
2285
Peter Zijlstraabd50712010-01-26 18:50:16 +01002286 event->pmu->read(event);
Peter Zijlstrae7850592010-05-21 14:43:08 +02002287 now = local64_read(&event->count);
Peter Zijlstraabd50712010-01-26 18:50:16 +01002288 delta = now - hwc->freq_count_stamp;
2289 hwc->freq_count_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002290
Peter Zijlstraabd50712010-01-26 18:50:16 +01002291 if (delta > 0)
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002292 perf_adjust_period(event, period, delta);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002293 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002294}
2295
2296/*
2297 * Round-robin a context's events:
2298 */
2299static void rotate_ctx(struct perf_event_context *ctx)
2300{
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01002301 /*
2302 * Rotate the first entry last of non-pinned groups. Rotation might be
2303 * disabled by the inheritance code.
2304 */
2305 if (!ctx->rotate_disable)
2306 list_rotate_left(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002307}
2308
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002309/*
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002310 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
2311 * because they're strictly cpu affine and rotate_start is called with IRQs
2312 * disabled, while rotate_context is called from IRQ context.
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002313 */
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002314static void perf_rotate_context(struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002315{
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002316 u64 interval = (u64)cpuctx->jiffies_interval * TICK_NSEC;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02002317 struct perf_event_context *ctx = NULL;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002318 int rotate = 0, remove = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002319
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002320 if (cpuctx->ctx.nr_events) {
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002321 remove = 0;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002322 if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
2323 rotate = 1;
2324 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002325
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02002326 ctx = cpuctx->task_ctx;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002327 if (ctx && ctx->nr_events) {
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002328 remove = 0;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002329 if (ctx->nr_events != ctx->nr_active)
2330 rotate = 1;
2331 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002332
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002333 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02002334 perf_pmu_disable(cpuctx->ctx.pmu);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002335 perf_ctx_adjust_freq(&cpuctx->ctx, interval);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002336 if (ctx)
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002337 perf_ctx_adjust_freq(ctx, interval);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002338
Peter Zijlstrad4944a02010-03-08 13:51:20 +01002339 if (!rotate)
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002340 goto done;
Peter Zijlstrad4944a02010-03-08 13:51:20 +01002341
Frederic Weisbecker7defb0f2010-01-17 12:15:31 +01002342 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002343 if (ctx)
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02002344 ctx_sched_out(ctx, cpuctx, EVENT_FLEXIBLE);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002345
2346 rotate_ctx(&cpuctx->ctx);
2347 if (ctx)
2348 rotate_ctx(ctx);
2349
Stephane Eraniane5d13672011-02-14 11:20:01 +02002350 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, current);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002351 if (ctx)
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02002352 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, current);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002353
2354done:
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002355 if (remove)
2356 list_del_init(&cpuctx->rotation_list);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002357
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002358 perf_pmu_enable(cpuctx->ctx.pmu);
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002359 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002360}
2361
2362void perf_event_task_tick(void)
2363{
2364 struct list_head *head = &__get_cpu_var(rotation_list);
2365 struct perf_cpu_context *cpuctx, *tmp;
2366
2367 WARN_ON(!irqs_disabled());
2368
2369 list_for_each_entry_safe(cpuctx, tmp, head, rotation_list) {
2370 if (cpuctx->jiffies_interval == 1 ||
2371 !(jiffies % cpuctx->jiffies_interval))
2372 perf_rotate_context(cpuctx);
2373 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002374}
2375
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002376static int event_enable_on_exec(struct perf_event *event,
2377 struct perf_event_context *ctx)
2378{
2379 if (!event->attr.enable_on_exec)
2380 return 0;
2381
2382 event->attr.enable_on_exec = 0;
2383 if (event->state >= PERF_EVENT_STATE_INACTIVE)
2384 return 0;
2385
2386 __perf_event_mark_enabled(event, ctx);
2387
2388 return 1;
2389}
2390
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002391/*
2392 * Enable all of a task's events that have been marked enable-on-exec.
2393 * This expects task == current.
2394 */
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02002395static void perf_event_enable_on_exec(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002396{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002397 struct perf_event *event;
2398 unsigned long flags;
2399 int enabled = 0;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002400 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002401
2402 local_irq_save(flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002403 if (!ctx || !ctx->nr_events)
2404 goto out;
2405
Stephane Eraniane566b762011-04-06 02:54:54 +02002406 /*
2407 * We must ctxsw out cgroup events to avoid conflict
2408 * when invoking perf_task_event_sched_in() later on
2409 * in this function. Otherwise we end up trying to
2410 * ctxswin cgroup events which are already scheduled
2411 * in.
2412 */
2413 perf_cgroup_sched_out(current);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002414
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002415 raw_spin_lock(&ctx->lock);
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02002416 task_ctx_sched_out(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002417
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002418 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
2419 ret = event_enable_on_exec(event, ctx);
2420 if (ret)
2421 enabled = 1;
2422 }
2423
2424 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
2425 ret = event_enable_on_exec(event, ctx);
2426 if (ret)
2427 enabled = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002428 }
2429
2430 /*
2431 * Unclone this context if we enabled any event.
2432 */
2433 if (enabled)
2434 unclone_ctx(ctx);
2435
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002436 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002437
Stephane Eraniane566b762011-04-06 02:54:54 +02002438 /*
2439 * Also calls ctxswin for cgroup events, if any:
2440 */
Stephane Eraniane5d13672011-02-14 11:20:01 +02002441 perf_event_context_sched_in(ctx, ctx->task);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002442out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002443 local_irq_restore(flags);
2444}
2445
2446/*
2447 * Cross CPU call to read the hardware event
2448 */
2449static void __perf_event_read(void *info)
2450{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002451 struct perf_event *event = info;
2452 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002453 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002454
2455 /*
2456 * If this is a task context, we need to check whether it is
2457 * the current task context of this cpu. If not it has been
2458 * scheduled out before the smp call arrived. In that case
2459 * event->count would have been updated to a recent sample
2460 * when the event was scheduled out.
2461 */
2462 if (ctx->task && cpuctx->task_ctx != ctx)
2463 return;
2464
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002465 raw_spin_lock(&ctx->lock);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002466 if (ctx->is_active) {
Peter Zijlstra542e72f2011-01-26 15:38:35 +01002467 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002468 update_cgrp_time_from_event(event);
2469 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002470 update_event_times(event);
Peter Zijlstra542e72f2011-01-26 15:38:35 +01002471 if (event->state == PERF_EVENT_STATE_ACTIVE)
2472 event->pmu->read(event);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002473 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002474}
2475
Peter Zijlstrab5e58792010-05-21 14:43:12 +02002476static inline u64 perf_event_count(struct perf_event *event)
2477{
Peter Zijlstrae7850592010-05-21 14:43:08 +02002478 return local64_read(&event->count) + atomic64_read(&event->child_count);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02002479}
2480
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002481static u64 perf_event_read(struct perf_event *event)
2482{
2483 /*
2484 * If event is enabled and currently active on a CPU, update the
2485 * value in the event structure:
2486 */
2487 if (event->state == PERF_EVENT_STATE_ACTIVE) {
2488 smp_call_function_single(event->oncpu,
2489 __perf_event_read, event, 1);
2490 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01002491 struct perf_event_context *ctx = event->ctx;
2492 unsigned long flags;
2493
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002494 raw_spin_lock_irqsave(&ctx->lock, flags);
Stephane Eranianc530ccd2010-10-15 15:26:01 +02002495 /*
2496 * may read while context is not active
2497 * (e.g., thread is blocked), in that case
2498 * we cannot update context time
2499 */
Stephane Eraniane5d13672011-02-14 11:20:01 +02002500 if (ctx->is_active) {
Stephane Eranianc530ccd2010-10-15 15:26:01 +02002501 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002502 update_cgrp_time_from_event(event);
2503 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002504 update_event_times(event);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002505 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002506 }
2507
Peter Zijlstrab5e58792010-05-21 14:43:12 +02002508 return perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002509}
2510
2511/*
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02002512 * Callchain support
2513 */
2514
2515struct callchain_cpus_entries {
2516 struct rcu_head rcu_head;
2517 struct perf_callchain_entry *cpu_entries[0];
2518};
2519
Frederic Weisbecker7ae07ea2010-08-14 20:45:13 +02002520static DEFINE_PER_CPU(int, callchain_recursion[PERF_NR_CONTEXTS]);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02002521static atomic_t nr_callchain_events;
2522static DEFINE_MUTEX(callchain_mutex);
2523struct callchain_cpus_entries *callchain_cpus_entries;
2524
2525
2526__weak void perf_callchain_kernel(struct perf_callchain_entry *entry,
2527 struct pt_regs *regs)
2528{
2529}
2530
2531__weak void perf_callchain_user(struct perf_callchain_entry *entry,
2532 struct pt_regs *regs)
2533{
2534}
2535
2536static void release_callchain_buffers_rcu(struct rcu_head *head)
2537{
2538 struct callchain_cpus_entries *entries;
2539 int cpu;
2540
2541 entries = container_of(head, struct callchain_cpus_entries, rcu_head);
2542
2543 for_each_possible_cpu(cpu)
2544 kfree(entries->cpu_entries[cpu]);
2545
2546 kfree(entries);
2547}
2548
2549static void release_callchain_buffers(void)
2550{
2551 struct callchain_cpus_entries *entries;
2552
2553 entries = callchain_cpus_entries;
2554 rcu_assign_pointer(callchain_cpus_entries, NULL);
2555 call_rcu(&entries->rcu_head, release_callchain_buffers_rcu);
2556}
2557
2558static int alloc_callchain_buffers(void)
2559{
2560 int cpu;
2561 int size;
2562 struct callchain_cpus_entries *entries;
2563
2564 /*
2565 * We can't use the percpu allocation API for data that can be
2566 * accessed from NMI. Use a temporary manual per cpu allocation
2567 * until that gets sorted out.
2568 */
Eric Dumazet88d4f0d2011-01-25 19:40:51 +01002569 size = offsetof(struct callchain_cpus_entries, cpu_entries[nr_cpu_ids]);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02002570
2571 entries = kzalloc(size, GFP_KERNEL);
2572 if (!entries)
2573 return -ENOMEM;
2574
Frederic Weisbecker7ae07ea2010-08-14 20:45:13 +02002575 size = sizeof(struct perf_callchain_entry) * PERF_NR_CONTEXTS;
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02002576
2577 for_each_possible_cpu(cpu) {
2578 entries->cpu_entries[cpu] = kmalloc_node(size, GFP_KERNEL,
2579 cpu_to_node(cpu));
2580 if (!entries->cpu_entries[cpu])
2581 goto fail;
2582 }
2583
2584 rcu_assign_pointer(callchain_cpus_entries, entries);
2585
2586 return 0;
2587
2588fail:
2589 for_each_possible_cpu(cpu)
2590 kfree(entries->cpu_entries[cpu]);
2591 kfree(entries);
2592
2593 return -ENOMEM;
2594}
2595
2596static int get_callchain_buffers(void)
2597{
2598 int err = 0;
2599 int count;
2600
2601 mutex_lock(&callchain_mutex);
2602
2603 count = atomic_inc_return(&nr_callchain_events);
2604 if (WARN_ON_ONCE(count < 1)) {
2605 err = -EINVAL;
2606 goto exit;
2607 }
2608
2609 if (count > 1) {
2610 /* If the allocation failed, give up */
2611 if (!callchain_cpus_entries)
2612 err = -ENOMEM;
2613 goto exit;
2614 }
2615
2616 err = alloc_callchain_buffers();
2617 if (err)
2618 release_callchain_buffers();
2619exit:
2620 mutex_unlock(&callchain_mutex);
2621
2622 return err;
2623}
2624
2625static void put_callchain_buffers(void)
2626{
2627 if (atomic_dec_and_mutex_lock(&nr_callchain_events, &callchain_mutex)) {
2628 release_callchain_buffers();
2629 mutex_unlock(&callchain_mutex);
2630 }
2631}
2632
2633static int get_recursion_context(int *recursion)
2634{
2635 int rctx;
2636
2637 if (in_nmi())
2638 rctx = 3;
2639 else if (in_irq())
2640 rctx = 2;
2641 else if (in_softirq())
2642 rctx = 1;
2643 else
2644 rctx = 0;
2645
2646 if (recursion[rctx])
2647 return -1;
2648
2649 recursion[rctx]++;
2650 barrier();
2651
2652 return rctx;
2653}
2654
2655static inline void put_recursion_context(int *recursion, int rctx)
2656{
2657 barrier();
2658 recursion[rctx]--;
2659}
2660
2661static struct perf_callchain_entry *get_callchain_entry(int *rctx)
2662{
2663 int cpu;
2664 struct callchain_cpus_entries *entries;
2665
2666 *rctx = get_recursion_context(__get_cpu_var(callchain_recursion));
2667 if (*rctx == -1)
2668 return NULL;
2669
2670 entries = rcu_dereference(callchain_cpus_entries);
2671 if (!entries)
2672 return NULL;
2673
2674 cpu = smp_processor_id();
2675
2676 return &entries->cpu_entries[cpu][*rctx];
2677}
2678
2679static void
2680put_callchain_entry(int rctx)
2681{
2682 put_recursion_context(__get_cpu_var(callchain_recursion), rctx);
2683}
2684
2685static struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
2686{
2687 int rctx;
2688 struct perf_callchain_entry *entry;
2689
2690
2691 entry = get_callchain_entry(&rctx);
2692 if (rctx == -1)
2693 return NULL;
2694
2695 if (!entry)
2696 goto exit_put;
2697
2698 entry->nr = 0;
2699
2700 if (!user_mode(regs)) {
2701 perf_callchain_store(entry, PERF_CONTEXT_KERNEL);
2702 perf_callchain_kernel(entry, regs);
2703 if (current->mm)
2704 regs = task_pt_regs(current);
2705 else
2706 regs = NULL;
2707 }
2708
2709 if (regs) {
2710 perf_callchain_store(entry, PERF_CONTEXT_USER);
2711 perf_callchain_user(entry, regs);
2712 }
2713
2714exit_put:
2715 put_callchain_entry(rctx);
2716
2717 return entry;
2718}
2719
2720/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002721 * Initialize the perf_event context in a task_struct:
2722 */
Peter Zijlstraeb184472010-09-07 15:55:13 +02002723static void __perf_event_init_context(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002724{
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002725 raw_spin_lock_init(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002726 mutex_init(&ctx->mutex);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002727 INIT_LIST_HEAD(&ctx->pinned_groups);
2728 INIT_LIST_HEAD(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002729 INIT_LIST_HEAD(&ctx->event_list);
2730 atomic_set(&ctx->refcount, 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002731}
2732
Peter Zijlstraeb184472010-09-07 15:55:13 +02002733static struct perf_event_context *
2734alloc_perf_context(struct pmu *pmu, struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002735{
2736 struct perf_event_context *ctx;
Peter Zijlstraeb184472010-09-07 15:55:13 +02002737
2738 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
2739 if (!ctx)
2740 return NULL;
2741
2742 __perf_event_init_context(ctx);
2743 if (task) {
2744 ctx->task = task;
2745 get_task_struct(task);
2746 }
2747 ctx->pmu = pmu;
2748
2749 return ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002750}
2751
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07002752static struct task_struct *
2753find_lively_task_by_vpid(pid_t vpid)
2754{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002755 struct task_struct *task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002756 int err;
2757
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002758 rcu_read_lock();
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07002759 if (!vpid)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002760 task = current;
2761 else
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07002762 task = find_task_by_vpid(vpid);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002763 if (task)
2764 get_task_struct(task);
2765 rcu_read_unlock();
2766
2767 if (!task)
2768 return ERR_PTR(-ESRCH);
2769
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002770 /* Reuse ptrace permission checks for now. */
2771 err = -EACCES;
2772 if (!ptrace_may_access(task, PTRACE_MODE_READ))
2773 goto errout;
2774
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07002775 return task;
2776errout:
2777 put_task_struct(task);
2778 return ERR_PTR(err);
2779
2780}
2781
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002782/*
2783 * Returns a matching context with refcount and pincount.
2784 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002785static struct perf_event_context *
Matt Helsley38a81da2010-09-13 13:01:20 -07002786find_get_context(struct pmu *pmu, struct task_struct *task, int cpu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002787{
2788 struct perf_event_context *ctx;
2789 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002790 unsigned long flags;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02002791 int ctxn, err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002792
Oleg Nesterov22a4ec72011-01-18 17:10:08 +01002793 if (!task) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002794 /* Must be root to operate on a CPU event: */
2795 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
2796 return ERR_PTR(-EACCES);
2797
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002798 /*
2799 * We could be clever and allow to attach a event to an
2800 * offline CPU and activate it when the CPU comes up, but
2801 * that's for later.
2802 */
2803 if (!cpu_online(cpu))
2804 return ERR_PTR(-ENODEV);
2805
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002806 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002807 ctx = &cpuctx->ctx;
2808 get_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002809 ++ctx->pin_count;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002810
2811 return ctx;
2812 }
2813
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02002814 err = -EINVAL;
2815 ctxn = pmu->task_ctx_nr;
2816 if (ctxn < 0)
2817 goto errout;
2818
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002819retry:
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02002820 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002821 if (ctx) {
2822 unclone_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002823 ++ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002824 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Peter Zijlstra9137fb22011-04-09 21:17:41 +02002825 } else {
Peter Zijlstraeb184472010-09-07 15:55:13 +02002826 ctx = alloc_perf_context(pmu, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002827 err = -ENOMEM;
2828 if (!ctx)
2829 goto errout;
Peter Zijlstraeb184472010-09-07 15:55:13 +02002830
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01002831 err = 0;
2832 mutex_lock(&task->perf_event_mutex);
2833 /*
2834 * If it has already passed perf_event_exit_task().
2835 * we must see PF_EXITING, it takes this mutex too.
2836 */
2837 if (task->flags & PF_EXITING)
2838 err = -ESRCH;
2839 else if (task->perf_event_ctxp[ctxn])
2840 err = -EAGAIN;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002841 else {
Peter Zijlstra9137fb22011-04-09 21:17:41 +02002842 get_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002843 ++ctx->pin_count;
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01002844 rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002845 }
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01002846 mutex_unlock(&task->perf_event_mutex);
2847
2848 if (unlikely(err)) {
Peter Zijlstra9137fb22011-04-09 21:17:41 +02002849 put_ctx(ctx);
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01002850
2851 if (err == -EAGAIN)
2852 goto retry;
2853 goto errout;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002854 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002855 }
2856
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002857 return ctx;
2858
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002859errout:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002860 return ERR_PTR(err);
2861}
2862
Li Zefan6fb29152009-10-15 11:21:42 +08002863static void perf_event_free_filter(struct perf_event *event);
2864
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002865static void free_event_rcu(struct rcu_head *head)
2866{
2867 struct perf_event *event;
2868
2869 event = container_of(head, struct perf_event, rcu_head);
2870 if (event->ns)
2871 put_pid_ns(event->ns);
Li Zefan6fb29152009-10-15 11:21:42 +08002872 perf_event_free_filter(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002873 kfree(event);
2874}
2875
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002876static void perf_buffer_put(struct perf_buffer *buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002877
2878static void free_event(struct perf_event *event)
2879{
Peter Zijlstrae360adb2010-10-14 14:01:34 +08002880 irq_work_sync(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002881
2882 if (!event->parent) {
Peter Zijlstra82cd6de2010-10-14 17:57:23 +02002883 if (event->attach_state & PERF_ATTACH_TASK)
Stephane Eraniane5d13672011-02-14 11:20:01 +02002884 jump_label_dec(&perf_sched_events);
Eric B Munson3af9e852010-05-18 15:30:49 +01002885 if (event->attr.mmap || event->attr.mmap_data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002886 atomic_dec(&nr_mmap_events);
2887 if (event->attr.comm)
2888 atomic_dec(&nr_comm_events);
2889 if (event->attr.task)
2890 atomic_dec(&nr_task_events);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02002891 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
2892 put_callchain_buffers();
Peter Zijlstra08309372011-03-03 11:31:20 +01002893 if (is_cgroup_event(event)) {
2894 atomic_dec(&per_cpu(perf_cgroup_events, event->cpu));
2895 jump_label_dec(&perf_sched_events);
2896 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002897 }
2898
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002899 if (event->buffer) {
2900 perf_buffer_put(event->buffer);
2901 event->buffer = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002902 }
2903
Stephane Eraniane5d13672011-02-14 11:20:01 +02002904 if (is_cgroup_event(event))
2905 perf_detach_cgroup(event);
2906
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002907 if (event->destroy)
2908 event->destroy(event);
2909
Peter Zijlstra0c67b402010-09-13 11:15:58 +02002910 if (event->ctx)
2911 put_ctx(event->ctx);
2912
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002913 call_rcu(&event->rcu_head, free_event_rcu);
2914}
2915
Arjan van de Venfb0459d2009-09-25 12:25:56 +02002916int perf_event_release_kernel(struct perf_event *event)
2917{
2918 struct perf_event_context *ctx = event->ctx;
2919
Peter Zijlstra050735b2010-05-11 11:51:53 +02002920 /*
2921 * Remove from the PMU, can't get re-enabled since we got
2922 * here because the last ref went.
2923 */
2924 perf_event_disable(event);
2925
Arjan van de Venfb0459d2009-09-25 12:25:56 +02002926 WARN_ON_ONCE(ctx->parent_ctx);
Peter Zijlstraa0507c82010-05-06 15:42:53 +02002927 /*
2928 * There are two ways this annotation is useful:
2929 *
2930 * 1) there is a lock recursion from perf_event_exit_task
2931 * see the comment there.
2932 *
2933 * 2) there is a lock-inversion with mmap_sem through
2934 * perf_event_read_group(), which takes faults while
2935 * holding ctx->mutex, however this is called after
2936 * the last filedesc died, so there is no possibility
2937 * to trigger the AB-BA case.
2938 */
2939 mutex_lock_nested(&ctx->mutex, SINGLE_DEPTH_NESTING);
Peter Zijlstra050735b2010-05-11 11:51:53 +02002940 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra8a495422010-05-27 15:47:49 +02002941 perf_group_detach(event);
Peter Zijlstra050735b2010-05-11 11:51:53 +02002942 list_del_event(event, ctx);
Peter Zijlstra050735b2010-05-11 11:51:53 +02002943 raw_spin_unlock_irq(&ctx->lock);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02002944 mutex_unlock(&ctx->mutex);
2945
Arjan van de Venfb0459d2009-09-25 12:25:56 +02002946 free_event(event);
2947
2948 return 0;
2949}
2950EXPORT_SYMBOL_GPL(perf_event_release_kernel);
2951
Peter Zijlstraa66a3052009-11-23 11:37:23 +01002952/*
2953 * Called when the last reference to the file is gone.
2954 */
2955static int perf_release(struct inode *inode, struct file *file)
2956{
2957 struct perf_event *event = file->private_data;
Peter Zijlstra88821352010-11-09 19:01:43 +01002958 struct task_struct *owner;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01002959
2960 file->private_data = NULL;
2961
Peter Zijlstra88821352010-11-09 19:01:43 +01002962 rcu_read_lock();
2963 owner = ACCESS_ONCE(event->owner);
2964 /*
2965 * Matches the smp_wmb() in perf_event_exit_task(). If we observe
2966 * !owner it means the list deletion is complete and we can indeed
2967 * free this event, otherwise we need to serialize on
2968 * owner->perf_event_mutex.
2969 */
2970 smp_read_barrier_depends();
2971 if (owner) {
2972 /*
2973 * Since delayed_put_task_struct() also drops the last
2974 * task reference we can safely take a new reference
2975 * while holding the rcu_read_lock().
2976 */
2977 get_task_struct(owner);
2978 }
2979 rcu_read_unlock();
2980
2981 if (owner) {
2982 mutex_lock(&owner->perf_event_mutex);
2983 /*
2984 * We have to re-check the event->owner field, if it is cleared
2985 * we raced with perf_event_exit_task(), acquiring the mutex
2986 * ensured they're done, and we can proceed with freeing the
2987 * event.
2988 */
2989 if (event->owner)
2990 list_del_init(&event->owner_entry);
2991 mutex_unlock(&owner->perf_event_mutex);
2992 put_task_struct(owner);
2993 }
2994
Peter Zijlstraa66a3052009-11-23 11:37:23 +01002995 return perf_event_release_kernel(event);
2996}
2997
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002998u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002999{
3000 struct perf_event *child;
3001 u64 total = 0;
3002
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003003 *enabled = 0;
3004 *running = 0;
3005
Peter Zijlstra6f105812009-11-20 22:19:56 +01003006 mutex_lock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003007 total += perf_event_read(event);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003008 *enabled += event->total_time_enabled +
3009 atomic64_read(&event->child_total_time_enabled);
3010 *running += event->total_time_running +
3011 atomic64_read(&event->child_total_time_running);
3012
3013 list_for_each_entry(child, &event->child_list, child_list) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003014 total += perf_event_read(child);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003015 *enabled += child->total_time_enabled;
3016 *running += child->total_time_running;
3017 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01003018 mutex_unlock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003019
3020 return total;
3021}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003022EXPORT_SYMBOL_GPL(perf_event_read_value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003023
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003024static int perf_event_read_group(struct perf_event *event,
3025 u64 read_format, char __user *buf)
3026{
3027 struct perf_event *leader = event->group_leader, *sub;
Peter Zijlstra6f105812009-11-20 22:19:56 +01003028 int n = 0, size = 0, ret = -EFAULT;
3029 struct perf_event_context *ctx = leader->ctx;
Peter Zijlstraabf48682009-11-20 22:19:49 +01003030 u64 values[5];
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003031 u64 count, enabled, running;
Peter Zijlstraabf48682009-11-20 22:19:49 +01003032
Peter Zijlstra6f105812009-11-20 22:19:56 +01003033 mutex_lock(&ctx->mutex);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003034 count = perf_event_read_value(leader, &enabled, &running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003035
3036 values[n++] = 1 + leader->nr_siblings;
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003037 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3038 values[n++] = enabled;
3039 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3040 values[n++] = running;
Peter Zijlstraabf48682009-11-20 22:19:49 +01003041 values[n++] = count;
3042 if (read_format & PERF_FORMAT_ID)
3043 values[n++] = primary_event_id(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003044
3045 size = n * sizeof(u64);
3046
3047 if (copy_to_user(buf, values, size))
Peter Zijlstra6f105812009-11-20 22:19:56 +01003048 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003049
Peter Zijlstra6f105812009-11-20 22:19:56 +01003050 ret = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003051
3052 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
Peter Zijlstraabf48682009-11-20 22:19:49 +01003053 n = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003054
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003055 values[n++] = perf_event_read_value(sub, &enabled, &running);
Peter Zijlstraabf48682009-11-20 22:19:49 +01003056 if (read_format & PERF_FORMAT_ID)
3057 values[n++] = primary_event_id(sub);
3058
3059 size = n * sizeof(u64);
3060
Stephane Eranian184d3da2009-11-23 21:40:49 -08003061 if (copy_to_user(buf + ret, values, size)) {
Peter Zijlstra6f105812009-11-20 22:19:56 +01003062 ret = -EFAULT;
3063 goto unlock;
3064 }
Peter Zijlstraabf48682009-11-20 22:19:49 +01003065
3066 ret += size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003067 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01003068unlock:
3069 mutex_unlock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003070
Peter Zijlstraabf48682009-11-20 22:19:49 +01003071 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003072}
3073
3074static int perf_event_read_one(struct perf_event *event,
3075 u64 read_format, char __user *buf)
3076{
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003077 u64 enabled, running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003078 u64 values[4];
3079 int n = 0;
3080
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003081 values[n++] = perf_event_read_value(event, &enabled, &running);
3082 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3083 values[n++] = enabled;
3084 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3085 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003086 if (read_format & PERF_FORMAT_ID)
3087 values[n++] = primary_event_id(event);
3088
3089 if (copy_to_user(buf, values, n * sizeof(u64)))
3090 return -EFAULT;
3091
3092 return n * sizeof(u64);
3093}
3094
3095/*
3096 * Read the performance event - simple non blocking version for now
3097 */
3098static ssize_t
3099perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
3100{
3101 u64 read_format = event->attr.read_format;
3102 int ret;
3103
3104 /*
3105 * Return end-of-file for a read on a event that is in
3106 * error state (i.e. because it was pinned but it couldn't be
3107 * scheduled on to the CPU at some point).
3108 */
3109 if (event->state == PERF_EVENT_STATE_ERROR)
3110 return 0;
3111
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02003112 if (count < event->read_size)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003113 return -ENOSPC;
3114
3115 WARN_ON_ONCE(event->ctx->parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003116 if (read_format & PERF_FORMAT_GROUP)
3117 ret = perf_event_read_group(event, read_format, buf);
3118 else
3119 ret = perf_event_read_one(event, read_format, buf);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003120
3121 return ret;
3122}
3123
3124static ssize_t
3125perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
3126{
3127 struct perf_event *event = file->private_data;
3128
3129 return perf_read_hw(event, buf, count);
3130}
3131
3132static unsigned int perf_poll(struct file *file, poll_table *wait)
3133{
3134 struct perf_event *event = file->private_data;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003135 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003136 unsigned int events = POLL_HUP;
3137
3138 rcu_read_lock();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003139 buffer = rcu_dereference(event->buffer);
3140 if (buffer)
3141 events = atomic_xchg(&buffer->poll, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003142 rcu_read_unlock();
3143
3144 poll_wait(file, &event->waitq, wait);
3145
3146 return events;
3147}
3148
3149static void perf_event_reset(struct perf_event *event)
3150{
3151 (void)perf_event_read(event);
Peter Zijlstrae7850592010-05-21 14:43:08 +02003152 local64_set(&event->count, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003153 perf_event_update_userpage(event);
3154}
3155
3156/*
3157 * Holding the top-level event's child_mutex means that any
3158 * descendant process that has inherited this event will block
3159 * in sync_child_event if it goes to exit, thus satisfying the
3160 * task existence requirements of perf_event_enable/disable.
3161 */
3162static void perf_event_for_each_child(struct perf_event *event,
3163 void (*func)(struct perf_event *))
3164{
3165 struct perf_event *child;
3166
3167 WARN_ON_ONCE(event->ctx->parent_ctx);
3168 mutex_lock(&event->child_mutex);
3169 func(event);
3170 list_for_each_entry(child, &event->child_list, child_list)
3171 func(child);
3172 mutex_unlock(&event->child_mutex);
3173}
3174
3175static void perf_event_for_each(struct perf_event *event,
3176 void (*func)(struct perf_event *))
3177{
3178 struct perf_event_context *ctx = event->ctx;
3179 struct perf_event *sibling;
3180
3181 WARN_ON_ONCE(ctx->parent_ctx);
3182 mutex_lock(&ctx->mutex);
3183 event = event->group_leader;
3184
3185 perf_event_for_each_child(event, func);
3186 func(event);
3187 list_for_each_entry(sibling, &event->sibling_list, group_entry)
3188 perf_event_for_each_child(event, func);
3189 mutex_unlock(&ctx->mutex);
3190}
3191
3192static int perf_event_period(struct perf_event *event, u64 __user *arg)
3193{
3194 struct perf_event_context *ctx = event->ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003195 int ret = 0;
3196 u64 value;
3197
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01003198 if (!is_sampling_event(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003199 return -EINVAL;
3200
John Blackwoodad0cf342010-09-28 18:03:11 -04003201 if (copy_from_user(&value, arg, sizeof(value)))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003202 return -EFAULT;
3203
3204 if (!value)
3205 return -EINVAL;
3206
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003207 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003208 if (event->attr.freq) {
3209 if (value > sysctl_perf_event_sample_rate) {
3210 ret = -EINVAL;
3211 goto unlock;
3212 }
3213
3214 event->attr.sample_freq = value;
3215 } else {
3216 event->attr.sample_period = value;
3217 event->hw.sample_period = value;
3218 }
3219unlock:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003220 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003221
3222 return ret;
3223}
3224
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003225static const struct file_operations perf_fops;
3226
3227static struct perf_event *perf_fget_light(int fd, int *fput_needed)
3228{
3229 struct file *file;
3230
3231 file = fget_light(fd, fput_needed);
3232 if (!file)
3233 return ERR_PTR(-EBADF);
3234
3235 if (file->f_op != &perf_fops) {
3236 fput_light(file, *fput_needed);
3237 *fput_needed = 0;
3238 return ERR_PTR(-EBADF);
3239 }
3240
3241 return file->private_data;
3242}
3243
3244static int perf_event_set_output(struct perf_event *event,
3245 struct perf_event *output_event);
Li Zefan6fb29152009-10-15 11:21:42 +08003246static int perf_event_set_filter(struct perf_event *event, void __user *arg);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003247
3248static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3249{
3250 struct perf_event *event = file->private_data;
3251 void (*func)(struct perf_event *);
3252 u32 flags = arg;
3253
3254 switch (cmd) {
3255 case PERF_EVENT_IOC_ENABLE:
3256 func = perf_event_enable;
3257 break;
3258 case PERF_EVENT_IOC_DISABLE:
3259 func = perf_event_disable;
3260 break;
3261 case PERF_EVENT_IOC_RESET:
3262 func = perf_event_reset;
3263 break;
3264
3265 case PERF_EVENT_IOC_REFRESH:
3266 return perf_event_refresh(event, arg);
3267
3268 case PERF_EVENT_IOC_PERIOD:
3269 return perf_event_period(event, (u64 __user *)arg);
3270
3271 case PERF_EVENT_IOC_SET_OUTPUT:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003272 {
3273 struct perf_event *output_event = NULL;
3274 int fput_needed = 0;
3275 int ret;
3276
3277 if (arg != -1) {
3278 output_event = perf_fget_light(arg, &fput_needed);
3279 if (IS_ERR(output_event))
3280 return PTR_ERR(output_event);
3281 }
3282
3283 ret = perf_event_set_output(event, output_event);
3284 if (output_event)
3285 fput_light(output_event->filp, fput_needed);
3286
3287 return ret;
3288 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003289
Li Zefan6fb29152009-10-15 11:21:42 +08003290 case PERF_EVENT_IOC_SET_FILTER:
3291 return perf_event_set_filter(event, (void __user *)arg);
3292
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003293 default:
3294 return -ENOTTY;
3295 }
3296
3297 if (flags & PERF_IOC_FLAG_GROUP)
3298 perf_event_for_each(event, func);
3299 else
3300 perf_event_for_each_child(event, func);
3301
3302 return 0;
3303}
3304
3305int perf_event_task_enable(void)
3306{
3307 struct perf_event *event;
3308
3309 mutex_lock(&current->perf_event_mutex);
3310 list_for_each_entry(event, &current->perf_event_list, owner_entry)
3311 perf_event_for_each_child(event, perf_event_enable);
3312 mutex_unlock(&current->perf_event_mutex);
3313
3314 return 0;
3315}
3316
3317int perf_event_task_disable(void)
3318{
3319 struct perf_event *event;
3320
3321 mutex_lock(&current->perf_event_mutex);
3322 list_for_each_entry(event, &current->perf_event_list, owner_entry)
3323 perf_event_for_each_child(event, perf_event_disable);
3324 mutex_unlock(&current->perf_event_mutex);
3325
3326 return 0;
3327}
3328
3329#ifndef PERF_EVENT_INDEX_OFFSET
3330# define PERF_EVENT_INDEX_OFFSET 0
3331#endif
3332
3333static int perf_event_index(struct perf_event *event)
3334{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02003335 if (event->hw.state & PERF_HES_STOPPED)
3336 return 0;
3337
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003338 if (event->state != PERF_EVENT_STATE_ACTIVE)
3339 return 0;
3340
3341 return event->hw.idx + 1 - PERF_EVENT_INDEX_OFFSET;
3342}
3343
3344/*
3345 * Callers need to ensure there can be no nesting of this function, otherwise
3346 * the seqlock logic goes bad. We can not serialize this because the arch
3347 * code calls this from NMI context.
3348 */
3349void perf_event_update_userpage(struct perf_event *event)
3350{
3351 struct perf_event_mmap_page *userpg;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003352 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003353
3354 rcu_read_lock();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003355 buffer = rcu_dereference(event->buffer);
3356 if (!buffer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003357 goto unlock;
3358
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003359 userpg = buffer->user_page;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003360
3361 /*
3362 * Disable preemption so as to not let the corresponding user-space
3363 * spin too long if we get preempted.
3364 */
3365 preempt_disable();
3366 ++userpg->lock;
3367 barrier();
3368 userpg->index = perf_event_index(event);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003369 userpg->offset = perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003370 if (event->state == PERF_EVENT_STATE_ACTIVE)
Peter Zijlstrae7850592010-05-21 14:43:08 +02003371 userpg->offset -= local64_read(&event->hw.prev_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003372
3373 userpg->time_enabled = event->total_time_enabled +
3374 atomic64_read(&event->child_total_time_enabled);
3375
3376 userpg->time_running = event->total_time_running +
3377 atomic64_read(&event->child_total_time_running);
3378
3379 barrier();
3380 ++userpg->lock;
3381 preempt_enable();
3382unlock:
3383 rcu_read_unlock();
3384}
3385
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02003386static unsigned long perf_data_size(struct perf_buffer *buffer);
3387
3388static void
3389perf_buffer_init(struct perf_buffer *buffer, long watermark, int flags)
3390{
3391 long max_size = perf_data_size(buffer);
3392
3393 if (watermark)
3394 buffer->watermark = min(max_size, watermark);
3395
3396 if (!buffer->watermark)
3397 buffer->watermark = max_size / 2;
3398
3399 if (flags & PERF_BUFFER_WRITABLE)
3400 buffer->writable = 1;
3401
3402 atomic_set(&buffer->refcount, 1);
3403}
3404
Peter Zijlstra906010b2009-09-21 16:08:49 +02003405#ifndef CONFIG_PERF_USE_VMALLOC
3406
3407/*
3408 * Back perf_mmap() with regular GFP_KERNEL-0 pages.
3409 */
3410
3411static struct page *
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003412perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff)
Peter Zijlstra906010b2009-09-21 16:08:49 +02003413{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003414 if (pgoff > buffer->nr_pages)
Peter Zijlstra906010b2009-09-21 16:08:49 +02003415 return NULL;
3416
3417 if (pgoff == 0)
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003418 return virt_to_page(buffer->user_page);
Peter Zijlstra906010b2009-09-21 16:08:49 +02003419
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003420 return virt_to_page(buffer->data_pages[pgoff - 1]);
Peter Zijlstra906010b2009-09-21 16:08:49 +02003421}
3422
Peter Zijlstraa19d35c2010-05-17 18:48:00 +02003423static void *perf_mmap_alloc_page(int cpu)
3424{
3425 struct page *page;
3426 int node;
3427
3428 node = (cpu == -1) ? cpu : cpu_to_node(cpu);
3429 page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
3430 if (!page)
3431 return NULL;
3432
3433 return page_address(page);
3434}
3435
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003436static struct perf_buffer *
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02003437perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003438{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003439 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003440 unsigned long size;
3441 int i;
3442
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003443 size = sizeof(struct perf_buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003444 size += nr_pages * sizeof(void *);
3445
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003446 buffer = kzalloc(size, GFP_KERNEL);
3447 if (!buffer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003448 goto fail;
3449
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02003450 buffer->user_page = perf_mmap_alloc_page(cpu);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003451 if (!buffer->user_page)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003452 goto fail_user_page;
3453
3454 for (i = 0; i < nr_pages; i++) {
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02003455 buffer->data_pages[i] = perf_mmap_alloc_page(cpu);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003456 if (!buffer->data_pages[i])
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003457 goto fail_data_pages;
3458 }
3459
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003460 buffer->nr_pages = nr_pages;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003461
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02003462 perf_buffer_init(buffer, watermark, flags);
3463
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003464 return buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003465
3466fail_data_pages:
3467 for (i--; i >= 0; i--)
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003468 free_page((unsigned long)buffer->data_pages[i]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003469
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003470 free_page((unsigned long)buffer->user_page);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003471
3472fail_user_page:
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003473 kfree(buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003474
3475fail:
Peter Zijlstra906010b2009-09-21 16:08:49 +02003476 return NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003477}
3478
3479static void perf_mmap_free_page(unsigned long addr)
3480{
3481 struct page *page = virt_to_page((void *)addr);
3482
3483 page->mapping = NULL;
3484 __free_page(page);
3485}
3486
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003487static void perf_buffer_free(struct perf_buffer *buffer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003488{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003489 int i;
3490
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003491 perf_mmap_free_page((unsigned long)buffer->user_page);
3492 for (i = 0; i < buffer->nr_pages; i++)
3493 perf_mmap_free_page((unsigned long)buffer->data_pages[i]);
3494 kfree(buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02003495}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003496
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003497static inline int page_order(struct perf_buffer *buffer)
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02003498{
3499 return 0;
3500}
3501
Peter Zijlstra906010b2009-09-21 16:08:49 +02003502#else
3503
3504/*
3505 * Back perf_mmap() with vmalloc memory.
3506 *
3507 * Required for architectures that have d-cache aliasing issues.
3508 */
3509
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003510static inline int page_order(struct perf_buffer *buffer)
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02003511{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003512 return buffer->page_order;
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02003513}
3514
Peter Zijlstra906010b2009-09-21 16:08:49 +02003515static struct page *
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003516perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff)
Peter Zijlstra906010b2009-09-21 16:08:49 +02003517{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003518 if (pgoff > (1UL << page_order(buffer)))
Peter Zijlstra906010b2009-09-21 16:08:49 +02003519 return NULL;
3520
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003521 return vmalloc_to_page((void *)buffer->user_page + pgoff * PAGE_SIZE);
Peter Zijlstra906010b2009-09-21 16:08:49 +02003522}
3523
3524static void perf_mmap_unmark_page(void *addr)
3525{
3526 struct page *page = vmalloc_to_page(addr);
3527
3528 page->mapping = NULL;
3529}
3530
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003531static void perf_buffer_free_work(struct work_struct *work)
Peter Zijlstra906010b2009-09-21 16:08:49 +02003532{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003533 struct perf_buffer *buffer;
Peter Zijlstra906010b2009-09-21 16:08:49 +02003534 void *base;
3535 int i, nr;
3536
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003537 buffer = container_of(work, struct perf_buffer, work);
3538 nr = 1 << page_order(buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02003539
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003540 base = buffer->user_page;
Peter Zijlstra906010b2009-09-21 16:08:49 +02003541 for (i = 0; i < nr + 1; i++)
3542 perf_mmap_unmark_page(base + (i * PAGE_SIZE));
3543
3544 vfree(base);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003545 kfree(buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02003546}
3547
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003548static void perf_buffer_free(struct perf_buffer *buffer)
Peter Zijlstra906010b2009-09-21 16:08:49 +02003549{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003550 schedule_work(&buffer->work);
Peter Zijlstra906010b2009-09-21 16:08:49 +02003551}
3552
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003553static struct perf_buffer *
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02003554perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags)
Peter Zijlstra906010b2009-09-21 16:08:49 +02003555{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003556 struct perf_buffer *buffer;
Peter Zijlstra906010b2009-09-21 16:08:49 +02003557 unsigned long size;
3558 void *all_buf;
3559
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003560 size = sizeof(struct perf_buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02003561 size += sizeof(void *);
3562
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003563 buffer = kzalloc(size, GFP_KERNEL);
3564 if (!buffer)
Peter Zijlstra906010b2009-09-21 16:08:49 +02003565 goto fail;
3566
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003567 INIT_WORK(&buffer->work, perf_buffer_free_work);
Peter Zijlstra906010b2009-09-21 16:08:49 +02003568
3569 all_buf = vmalloc_user((nr_pages + 1) * PAGE_SIZE);
3570 if (!all_buf)
3571 goto fail_all_buf;
3572
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003573 buffer->user_page = all_buf;
3574 buffer->data_pages[0] = all_buf + PAGE_SIZE;
3575 buffer->page_order = ilog2(nr_pages);
3576 buffer->nr_pages = 1;
Peter Zijlstra906010b2009-09-21 16:08:49 +02003577
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02003578 perf_buffer_init(buffer, watermark, flags);
3579
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003580 return buffer;
Peter Zijlstra906010b2009-09-21 16:08:49 +02003581
3582fail_all_buf:
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003583 kfree(buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02003584
3585fail:
3586 return NULL;
3587}
3588
3589#endif
3590
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003591static unsigned long perf_data_size(struct perf_buffer *buffer)
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02003592{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003593 return buffer->nr_pages << (PAGE_SHIFT + page_order(buffer));
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02003594}
3595
Peter Zijlstra906010b2009-09-21 16:08:49 +02003596static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
3597{
3598 struct perf_event *event = vma->vm_file->private_data;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003599 struct perf_buffer *buffer;
Peter Zijlstra906010b2009-09-21 16:08:49 +02003600 int ret = VM_FAULT_SIGBUS;
3601
3602 if (vmf->flags & FAULT_FLAG_MKWRITE) {
3603 if (vmf->pgoff == 0)
3604 ret = 0;
3605 return ret;
3606 }
3607
3608 rcu_read_lock();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003609 buffer = rcu_dereference(event->buffer);
3610 if (!buffer)
Peter Zijlstra906010b2009-09-21 16:08:49 +02003611 goto unlock;
3612
3613 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
3614 goto unlock;
3615
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003616 vmf->page = perf_mmap_to_page(buffer, vmf->pgoff);
Peter Zijlstra906010b2009-09-21 16:08:49 +02003617 if (!vmf->page)
3618 goto unlock;
3619
3620 get_page(vmf->page);
3621 vmf->page->mapping = vma->vm_file->f_mapping;
3622 vmf->page->index = vmf->pgoff;
3623
3624 ret = 0;
3625unlock:
3626 rcu_read_unlock();
3627
3628 return ret;
3629}
3630
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003631static void perf_buffer_free_rcu(struct rcu_head *rcu_head)
Peter Zijlstra906010b2009-09-21 16:08:49 +02003632{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003633 struct perf_buffer *buffer;
Peter Zijlstra906010b2009-09-21 16:08:49 +02003634
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003635 buffer = container_of(rcu_head, struct perf_buffer, rcu_head);
3636 perf_buffer_free(buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003637}
3638
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003639static struct perf_buffer *perf_buffer_get(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003640{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003641 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003642
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003643 rcu_read_lock();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003644 buffer = rcu_dereference(event->buffer);
3645 if (buffer) {
3646 if (!atomic_inc_not_zero(&buffer->refcount))
3647 buffer = NULL;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003648 }
3649 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003650
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003651 return buffer;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003652}
3653
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003654static void perf_buffer_put(struct perf_buffer *buffer)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003655{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003656 if (!atomic_dec_and_test(&buffer->refcount))
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003657 return;
3658
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003659 call_rcu(&buffer->rcu_head, perf_buffer_free_rcu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003660}
3661
3662static void perf_mmap_open(struct vm_area_struct *vma)
3663{
3664 struct perf_event *event = vma->vm_file->private_data;
3665
3666 atomic_inc(&event->mmap_count);
3667}
3668
3669static void perf_mmap_close(struct vm_area_struct *vma)
3670{
3671 struct perf_event *event = vma->vm_file->private_data;
3672
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003673 if (atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) {
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003674 unsigned long size = perf_data_size(event->buffer);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003675 struct user_struct *user = event->mmap_user;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003676 struct perf_buffer *buffer = event->buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003677
Peter Zijlstra906010b2009-09-21 16:08:49 +02003678 atomic_long_sub((size >> PAGE_SHIFT) + 1, &user->locked_vm);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003679 vma->vm_mm->locked_vm -= event->mmap_locked;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003680 rcu_assign_pointer(event->buffer, NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003681 mutex_unlock(&event->mmap_mutex);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003682
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003683 perf_buffer_put(buffer);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003684 free_uid(user);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003685 }
3686}
3687
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04003688static const struct vm_operations_struct perf_mmap_vmops = {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003689 .open = perf_mmap_open,
3690 .close = perf_mmap_close,
3691 .fault = perf_mmap_fault,
3692 .page_mkwrite = perf_mmap_fault,
3693};
3694
3695static int perf_mmap(struct file *file, struct vm_area_struct *vma)
3696{
3697 struct perf_event *event = file->private_data;
3698 unsigned long user_locked, user_lock_limit;
3699 struct user_struct *user = current_user();
3700 unsigned long locked, lock_limit;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003701 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003702 unsigned long vma_size;
3703 unsigned long nr_pages;
3704 long user_extra, extra;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02003705 int ret = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003706
Peter Zijlstrac7920612010-05-18 10:33:24 +02003707 /*
3708 * Don't allow mmap() of inherited per-task counters. This would
3709 * create a performance issue due to all children writing to the
3710 * same buffer.
3711 */
3712 if (event->cpu == -1 && event->attr.inherit)
3713 return -EINVAL;
3714
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003715 if (!(vma->vm_flags & VM_SHARED))
3716 return -EINVAL;
3717
3718 vma_size = vma->vm_end - vma->vm_start;
3719 nr_pages = (vma_size / PAGE_SIZE) - 1;
3720
3721 /*
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003722 * If we have buffer pages ensure they're a power-of-two number, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003723 * can do bitmasks instead of modulo.
3724 */
3725 if (nr_pages != 0 && !is_power_of_2(nr_pages))
3726 return -EINVAL;
3727
3728 if (vma_size != PAGE_SIZE * (1 + nr_pages))
3729 return -EINVAL;
3730
3731 if (vma->vm_pgoff != 0)
3732 return -EINVAL;
3733
3734 WARN_ON_ONCE(event->ctx->parent_ctx);
3735 mutex_lock(&event->mmap_mutex);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003736 if (event->buffer) {
3737 if (event->buffer->nr_pages == nr_pages)
3738 atomic_inc(&event->buffer->refcount);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003739 else
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003740 ret = -EINVAL;
3741 goto unlock;
3742 }
3743
3744 user_extra = nr_pages + 1;
3745 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
3746
3747 /*
3748 * Increase the limit linearly with more CPUs:
3749 */
3750 user_lock_limit *= num_online_cpus();
3751
3752 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
3753
3754 extra = 0;
3755 if (user_locked > user_lock_limit)
3756 extra = user_locked - user_lock_limit;
3757
Jiri Slaby78d7d402010-03-05 13:42:54 -08003758 lock_limit = rlimit(RLIMIT_MEMLOCK);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003759 lock_limit >>= PAGE_SHIFT;
3760 locked = vma->vm_mm->locked_vm + extra;
3761
3762 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
3763 !capable(CAP_IPC_LOCK)) {
3764 ret = -EPERM;
3765 goto unlock;
3766 }
3767
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003768 WARN_ON(event->buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02003769
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02003770 if (vma->vm_flags & VM_WRITE)
3771 flags |= PERF_BUFFER_WRITABLE;
3772
3773 buffer = perf_buffer_alloc(nr_pages, event->attr.wakeup_watermark,
3774 event->cpu, flags);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003775 if (!buffer) {
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003776 ret = -ENOMEM;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003777 goto unlock;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003778 }
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02003779 rcu_assign_pointer(event->buffer, buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003780
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003781 atomic_long_add(user_extra, &user->locked_vm);
3782 event->mmap_locked = extra;
3783 event->mmap_user = get_current_user();
3784 vma->vm_mm->locked_vm += event->mmap_locked;
3785
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003786unlock:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003787 if (!ret)
3788 atomic_inc(&event->mmap_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003789 mutex_unlock(&event->mmap_mutex);
3790
3791 vma->vm_flags |= VM_RESERVED;
3792 vma->vm_ops = &perf_mmap_vmops;
3793
3794 return ret;
3795}
3796
3797static int perf_fasync(int fd, struct file *filp, int on)
3798{
3799 struct inode *inode = filp->f_path.dentry->d_inode;
3800 struct perf_event *event = filp->private_data;
3801 int retval;
3802
3803 mutex_lock(&inode->i_mutex);
3804 retval = fasync_helper(fd, filp, on, &event->fasync);
3805 mutex_unlock(&inode->i_mutex);
3806
3807 if (retval < 0)
3808 return retval;
3809
3810 return 0;
3811}
3812
3813static const struct file_operations perf_fops = {
Arnd Bergmann3326c1c2010-03-23 19:09:33 +01003814 .llseek = no_llseek,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003815 .release = perf_release,
3816 .read = perf_read,
3817 .poll = perf_poll,
3818 .unlocked_ioctl = perf_ioctl,
3819 .compat_ioctl = perf_ioctl,
3820 .mmap = perf_mmap,
3821 .fasync = perf_fasync,
3822};
3823
3824/*
3825 * Perf event wakeup
3826 *
3827 * If there's data, ensure we set the poll() state and publish everything
3828 * to user-space before waking everybody up.
3829 */
3830
3831void perf_event_wakeup(struct perf_event *event)
3832{
3833 wake_up_all(&event->waitq);
3834
3835 if (event->pending_kill) {
3836 kill_fasync(&event->fasync, SIGIO, event->pending_kill);
3837 event->pending_kill = 0;
3838 }
3839}
3840
Peter Zijlstrae360adb2010-10-14 14:01:34 +08003841static void perf_pending_event(struct irq_work *entry)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003842{
3843 struct perf_event *event = container_of(entry,
3844 struct perf_event, pending);
3845
3846 if (event->pending_disable) {
3847 event->pending_disable = 0;
3848 __perf_event_disable(event);
3849 }
3850
3851 if (event->pending_wakeup) {
3852 event->pending_wakeup = 0;
3853 perf_event_wakeup(event);
3854 }
3855}
3856
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003857/*
Zhang, Yanmin39447b32010-04-19 13:32:41 +08003858 * We assume there is only KVM supporting the callbacks.
3859 * Later on, we might change it to a list if there is
3860 * another virtualization implementation supporting the callbacks.
3861 */
3862struct perf_guest_info_callbacks *perf_guest_cbs;
3863
3864int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3865{
3866 perf_guest_cbs = cbs;
3867 return 0;
3868}
3869EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
3870
3871int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3872{
3873 perf_guest_cbs = NULL;
3874 return 0;
3875}
3876EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
3877
3878/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003879 * Output
3880 */
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003881static bool perf_output_space(struct perf_buffer *buffer, unsigned long tail,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003882 unsigned long offset, unsigned long head)
3883{
3884 unsigned long mask;
3885
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003886 if (!buffer->writable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003887 return true;
3888
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003889 mask = perf_data_size(buffer) - 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003890
3891 offset = (offset - tail) & mask;
3892 head = (head - tail) & mask;
3893
3894 if ((int)(head - offset) < 0)
3895 return false;
3896
3897 return true;
3898}
3899
3900static void perf_output_wakeup(struct perf_output_handle *handle)
3901{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003902 atomic_set(&handle->buffer->poll, POLL_IN);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003903
3904 if (handle->nmi) {
3905 handle->event->pending_wakeup = 1;
Peter Zijlstrae360adb2010-10-14 14:01:34 +08003906 irq_work_queue(&handle->event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003907 } else
3908 perf_event_wakeup(handle->event);
3909}
3910
3911/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003912 * We need to ensure a later event_id doesn't publish a head when a former
Peter Zijlstraef607772010-05-18 10:50:41 +02003913 * event isn't done writing. However since we need to deal with NMIs we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003914 * cannot fully serialize things.
3915 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003916 * We only publish the head (and generate a wakeup) when the outer-most
Peter Zijlstraef607772010-05-18 10:50:41 +02003917 * event completes.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003918 */
Peter Zijlstraef607772010-05-18 10:50:41 +02003919static void perf_output_get_handle(struct perf_output_handle *handle)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003920{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003921 struct perf_buffer *buffer = handle->buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003922
Peter Zijlstraef607772010-05-18 10:50:41 +02003923 preempt_disable();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003924 local_inc(&buffer->nest);
3925 handle->wakeup = local_read(&buffer->wakeup);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003926}
3927
Peter Zijlstraef607772010-05-18 10:50:41 +02003928static void perf_output_put_handle(struct perf_output_handle *handle)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003929{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003930 struct perf_buffer *buffer = handle->buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003931 unsigned long head;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003932
3933again:
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003934 head = local_read(&buffer->head);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003935
3936 /*
Peter Zijlstraef607772010-05-18 10:50:41 +02003937 * IRQ/NMI can happen here, which means we can miss a head update.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003938 */
3939
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003940 if (!local_dec_and_test(&buffer->nest))
Frederic Weisbeckeracd35a42010-05-20 21:28:34 +02003941 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003942
3943 /*
Peter Zijlstraef607772010-05-18 10:50:41 +02003944 * Publish the known good head. Rely on the full barrier implied
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003945 * by atomic_dec_and_test() order the buffer->head read and this
Peter Zijlstraef607772010-05-18 10:50:41 +02003946 * write.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003947 */
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003948 buffer->user_page->data_head = head;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003949
Peter Zijlstraef607772010-05-18 10:50:41 +02003950 /*
3951 * Now check if we missed an update, rely on the (compiler)
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003952 * barrier in atomic_dec_and_test() to re-read buffer->head.
Peter Zijlstraef607772010-05-18 10:50:41 +02003953 */
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003954 if (unlikely(head != local_read(&buffer->head))) {
3955 local_inc(&buffer->nest);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003956 goto again;
3957 }
3958
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003959 if (handle->wakeup != local_read(&buffer->wakeup))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003960 perf_output_wakeup(handle);
Peter Zijlstraef607772010-05-18 10:50:41 +02003961
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003962out:
Peter Zijlstraef607772010-05-18 10:50:41 +02003963 preempt_enable();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003964}
3965
Peter Zijlstraa94ffaa2010-05-20 19:50:07 +02003966__always_inline void perf_output_copy(struct perf_output_handle *handle,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003967 const void *buf, unsigned int len)
3968{
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003969 do {
Peter Zijlstraa94ffaa2010-05-20 19:50:07 +02003970 unsigned long size = min_t(unsigned long, handle->size, len);
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003971
3972 memcpy(handle->addr, buf, size);
3973
3974 len -= size;
3975 handle->addr += size;
Frederic Weisbecker74048f82010-05-27 21:34:58 +02003976 buf += size;
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003977 handle->size -= size;
3978 if (!handle->size) {
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003979 struct perf_buffer *buffer = handle->buffer;
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02003980
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003981 handle->page++;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003982 handle->page &= buffer->nr_pages - 1;
3983 handle->addr = buffer->data_pages[handle->page];
3984 handle->size = PAGE_SIZE << page_order(buffer);
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003985 }
3986 } while (len);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003987}
3988
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02003989static void __perf_event_header__init_id(struct perf_event_header *header,
3990 struct perf_sample_data *data,
3991 struct perf_event *event)
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02003992{
3993 u64 sample_type = event->attr.sample_type;
3994
3995 data->type = sample_type;
3996 header->size += event->id_header_size;
3997
3998 if (sample_type & PERF_SAMPLE_TID) {
3999 /* namespace issues */
4000 data->tid_entry.pid = perf_event_pid(event, current);
4001 data->tid_entry.tid = perf_event_tid(event, current);
4002 }
4003
4004 if (sample_type & PERF_SAMPLE_TIME)
4005 data->time = perf_clock();
4006
4007 if (sample_type & PERF_SAMPLE_ID)
4008 data->id = primary_event_id(event);
4009
4010 if (sample_type & PERF_SAMPLE_STREAM_ID)
4011 data->stream_id = event->id;
4012
4013 if (sample_type & PERF_SAMPLE_CPU) {
4014 data->cpu_entry.cpu = raw_smp_processor_id();
4015 data->cpu_entry.reserved = 0;
4016 }
4017}
4018
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004019static void perf_event_header__init_id(struct perf_event_header *header,
4020 struct perf_sample_data *data,
4021 struct perf_event *event)
4022{
4023 if (event->attr.sample_id_all)
4024 __perf_event_header__init_id(header, data, event);
4025}
4026
4027static void __perf_event__output_id_sample(struct perf_output_handle *handle,
4028 struct perf_sample_data *data)
4029{
4030 u64 sample_type = data->type;
4031
4032 if (sample_type & PERF_SAMPLE_TID)
4033 perf_output_put(handle, data->tid_entry);
4034
4035 if (sample_type & PERF_SAMPLE_TIME)
4036 perf_output_put(handle, data->time);
4037
4038 if (sample_type & PERF_SAMPLE_ID)
4039 perf_output_put(handle, data->id);
4040
4041 if (sample_type & PERF_SAMPLE_STREAM_ID)
4042 perf_output_put(handle, data->stream_id);
4043
4044 if (sample_type & PERF_SAMPLE_CPU)
4045 perf_output_put(handle, data->cpu_entry);
4046}
4047
4048static void perf_event__output_id_sample(struct perf_event *event,
4049 struct perf_output_handle *handle,
4050 struct perf_sample_data *sample)
4051{
4052 if (event->attr.sample_id_all)
4053 __perf_event__output_id_sample(handle, sample);
4054}
4055
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004056int perf_output_begin(struct perf_output_handle *handle,
4057 struct perf_event *event, unsigned int size,
4058 int nmi, int sample)
4059{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02004060 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004061 unsigned long tail, offset, head;
4062 int have_lost;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004063 struct perf_sample_data sample_data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004064 struct {
4065 struct perf_event_header header;
4066 u64 id;
4067 u64 lost;
4068 } lost_event;
4069
4070 rcu_read_lock();
4071 /*
4072 * For inherited events we send all the output towards the parent.
4073 */
4074 if (event->parent)
4075 event = event->parent;
4076
Peter Zijlstraca5135e2010-05-28 19:33:23 +02004077 buffer = rcu_dereference(event->buffer);
4078 if (!buffer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004079 goto out;
4080
Peter Zijlstraca5135e2010-05-28 19:33:23 +02004081 handle->buffer = buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004082 handle->event = event;
4083 handle->nmi = nmi;
4084 handle->sample = sample;
4085
Peter Zijlstraca5135e2010-05-28 19:33:23 +02004086 if (!buffer->nr_pages)
Stephane Eranian00d1d0b2010-05-17 12:46:01 +02004087 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004088
Peter Zijlstraca5135e2010-05-28 19:33:23 +02004089 have_lost = local_read(&buffer->lost);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004090 if (have_lost) {
4091 lost_event.header.size = sizeof(lost_event);
4092 perf_event_header__init_id(&lost_event.header, &sample_data,
4093 event);
4094 size += lost_event.header.size;
4095 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004096
Peter Zijlstraef607772010-05-18 10:50:41 +02004097 perf_output_get_handle(handle);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004098
4099 do {
4100 /*
4101 * Userspace could choose to issue a mb() before updating the
4102 * tail pointer. So that all reads will be completed before the
4103 * write is issued.
4104 */
Peter Zijlstraca5135e2010-05-28 19:33:23 +02004105 tail = ACCESS_ONCE(buffer->user_page->data_tail);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004106 smp_rmb();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02004107 offset = head = local_read(&buffer->head);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004108 head += size;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02004109 if (unlikely(!perf_output_space(buffer, tail, offset, head)))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004110 goto fail;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02004111 } while (local_cmpxchg(&buffer->head, offset, head) != offset);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004112
Peter Zijlstraca5135e2010-05-28 19:33:23 +02004113 if (head - local_read(&buffer->wakeup) > buffer->watermark)
4114 local_add(buffer->watermark, &buffer->wakeup);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004115
Peter Zijlstraca5135e2010-05-28 19:33:23 +02004116 handle->page = offset >> (PAGE_SHIFT + page_order(buffer));
4117 handle->page &= buffer->nr_pages - 1;
4118 handle->size = offset & ((PAGE_SIZE << page_order(buffer)) - 1);
4119 handle->addr = buffer->data_pages[handle->page];
Peter Zijlstra5d967a82010-05-20 16:46:39 +02004120 handle->addr += handle->size;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02004121 handle->size = (PAGE_SIZE << page_order(buffer)) - handle->size;
Peter Zijlstra5d967a82010-05-20 16:46:39 +02004122
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004123 if (have_lost) {
4124 lost_event.header.type = PERF_RECORD_LOST;
4125 lost_event.header.misc = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004126 lost_event.id = event->id;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02004127 lost_event.lost = local_xchg(&buffer->lost, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004128
4129 perf_output_put(handle, lost_event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004130 perf_event__output_id_sample(event, handle, &sample_data);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004131 }
4132
4133 return 0;
4134
4135fail:
Peter Zijlstraca5135e2010-05-28 19:33:23 +02004136 local_inc(&buffer->lost);
Peter Zijlstraef607772010-05-18 10:50:41 +02004137 perf_output_put_handle(handle);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004138out:
4139 rcu_read_unlock();
4140
4141 return -ENOSPC;
4142}
4143
4144void perf_output_end(struct perf_output_handle *handle)
4145{
4146 struct perf_event *event = handle->event;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02004147 struct perf_buffer *buffer = handle->buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004148
4149 int wakeup_events = event->attr.wakeup_events;
4150
4151 if (handle->sample && wakeup_events) {
Peter Zijlstraca5135e2010-05-28 19:33:23 +02004152 int events = local_inc_return(&buffer->events);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004153 if (events >= wakeup_events) {
Peter Zijlstraca5135e2010-05-28 19:33:23 +02004154 local_sub(wakeup_events, &buffer->events);
4155 local_inc(&buffer->wakeup);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004156 }
4157 }
4158
Peter Zijlstraef607772010-05-18 10:50:41 +02004159 perf_output_put_handle(handle);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004160 rcu_read_unlock();
4161}
4162
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004163static void perf_output_read_one(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02004164 struct perf_event *event,
4165 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004166{
4167 u64 read_format = event->attr.read_format;
4168 u64 values[4];
4169 int n = 0;
4170
Peter Zijlstrab5e58792010-05-21 14:43:12 +02004171 values[n++] = perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004172 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
Stephane Eranianeed01522010-10-26 16:08:01 +02004173 values[n++] = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004174 atomic64_read(&event->child_total_time_enabled);
4175 }
4176 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
Stephane Eranianeed01522010-10-26 16:08:01 +02004177 values[n++] = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004178 atomic64_read(&event->child_total_time_running);
4179 }
4180 if (read_format & PERF_FORMAT_ID)
4181 values[n++] = primary_event_id(event);
4182
4183 perf_output_copy(handle, values, n * sizeof(u64));
4184}
4185
4186/*
4187 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
4188 */
4189static void perf_output_read_group(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02004190 struct perf_event *event,
4191 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004192{
4193 struct perf_event *leader = event->group_leader, *sub;
4194 u64 read_format = event->attr.read_format;
4195 u64 values[5];
4196 int n = 0;
4197
4198 values[n++] = 1 + leader->nr_siblings;
4199
4200 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
Stephane Eranianeed01522010-10-26 16:08:01 +02004201 values[n++] = enabled;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004202
4203 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
Stephane Eranianeed01522010-10-26 16:08:01 +02004204 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004205
4206 if (leader != event)
4207 leader->pmu->read(leader);
4208
Peter Zijlstrab5e58792010-05-21 14:43:12 +02004209 values[n++] = perf_event_count(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004210 if (read_format & PERF_FORMAT_ID)
4211 values[n++] = primary_event_id(leader);
4212
4213 perf_output_copy(handle, values, n * sizeof(u64));
4214
4215 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
4216 n = 0;
4217
4218 if (sub != event)
4219 sub->pmu->read(sub);
4220
Peter Zijlstrab5e58792010-05-21 14:43:12 +02004221 values[n++] = perf_event_count(sub);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004222 if (read_format & PERF_FORMAT_ID)
4223 values[n++] = primary_event_id(sub);
4224
4225 perf_output_copy(handle, values, n * sizeof(u64));
4226 }
4227}
4228
Stephane Eranianeed01522010-10-26 16:08:01 +02004229#define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
4230 PERF_FORMAT_TOTAL_TIME_RUNNING)
4231
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004232static void perf_output_read(struct perf_output_handle *handle,
4233 struct perf_event *event)
4234{
Stephane Eranianeed01522010-10-26 16:08:01 +02004235 u64 enabled = 0, running = 0, now, ctx_time;
4236 u64 read_format = event->attr.read_format;
4237
4238 /*
4239 * compute total_time_enabled, total_time_running
4240 * based on snapshot values taken when the event
4241 * was last scheduled in.
4242 *
4243 * we cannot simply called update_context_time()
4244 * because of locking issue as we are called in
4245 * NMI context
4246 */
4247 if (read_format & PERF_FORMAT_TOTAL_TIMES) {
4248 now = perf_clock();
4249 ctx_time = event->shadow_ctx_time + now;
4250 enabled = ctx_time - event->tstamp_enabled;
4251 running = ctx_time - event->tstamp_running;
4252 }
4253
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004254 if (event->attr.read_format & PERF_FORMAT_GROUP)
Stephane Eranianeed01522010-10-26 16:08:01 +02004255 perf_output_read_group(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004256 else
Stephane Eranianeed01522010-10-26 16:08:01 +02004257 perf_output_read_one(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004258}
4259
4260void perf_output_sample(struct perf_output_handle *handle,
4261 struct perf_event_header *header,
4262 struct perf_sample_data *data,
4263 struct perf_event *event)
4264{
4265 u64 sample_type = data->type;
4266
4267 perf_output_put(handle, *header);
4268
4269 if (sample_type & PERF_SAMPLE_IP)
4270 perf_output_put(handle, data->ip);
4271
4272 if (sample_type & PERF_SAMPLE_TID)
4273 perf_output_put(handle, data->tid_entry);
4274
4275 if (sample_type & PERF_SAMPLE_TIME)
4276 perf_output_put(handle, data->time);
4277
4278 if (sample_type & PERF_SAMPLE_ADDR)
4279 perf_output_put(handle, data->addr);
4280
4281 if (sample_type & PERF_SAMPLE_ID)
4282 perf_output_put(handle, data->id);
4283
4284 if (sample_type & PERF_SAMPLE_STREAM_ID)
4285 perf_output_put(handle, data->stream_id);
4286
4287 if (sample_type & PERF_SAMPLE_CPU)
4288 perf_output_put(handle, data->cpu_entry);
4289
4290 if (sample_type & PERF_SAMPLE_PERIOD)
4291 perf_output_put(handle, data->period);
4292
4293 if (sample_type & PERF_SAMPLE_READ)
4294 perf_output_read(handle, event);
4295
4296 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
4297 if (data->callchain) {
4298 int size = 1;
4299
4300 if (data->callchain)
4301 size += data->callchain->nr;
4302
4303 size *= sizeof(u64);
4304
4305 perf_output_copy(handle, data->callchain, size);
4306 } else {
4307 u64 nr = 0;
4308 perf_output_put(handle, nr);
4309 }
4310 }
4311
4312 if (sample_type & PERF_SAMPLE_RAW) {
4313 if (data->raw) {
4314 perf_output_put(handle, data->raw->size);
4315 perf_output_copy(handle, data->raw->data,
4316 data->raw->size);
4317 } else {
4318 struct {
4319 u32 size;
4320 u32 data;
4321 } raw = {
4322 .size = sizeof(u32),
4323 .data = 0,
4324 };
4325 perf_output_put(handle, raw);
4326 }
4327 }
4328}
4329
4330void perf_prepare_sample(struct perf_event_header *header,
4331 struct perf_sample_data *data,
4332 struct perf_event *event,
4333 struct pt_regs *regs)
4334{
4335 u64 sample_type = event->attr.sample_type;
4336
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004337 header->type = PERF_RECORD_SAMPLE;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02004338 header->size = sizeof(*header) + event->header_size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004339
4340 header->misc = 0;
4341 header->misc |= perf_misc_flags(regs);
4342
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004343 __perf_event_header__init_id(header, data, event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02004344
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02004345 if (sample_type & PERF_SAMPLE_IP)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004346 data->ip = perf_instruction_pointer(regs);
4347
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004348 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
4349 int size = 1;
4350
4351 data->callchain = perf_callchain(regs);
4352
4353 if (data->callchain)
4354 size += data->callchain->nr;
4355
4356 header->size += size * sizeof(u64);
4357 }
4358
4359 if (sample_type & PERF_SAMPLE_RAW) {
4360 int size = sizeof(u32);
4361
4362 if (data->raw)
4363 size += data->raw->size;
4364 else
4365 size += sizeof(u32);
4366
4367 WARN_ON_ONCE(size & (sizeof(u64)-1));
4368 header->size += size;
4369 }
4370}
4371
4372static void perf_event_output(struct perf_event *event, int nmi,
4373 struct perf_sample_data *data,
4374 struct pt_regs *regs)
4375{
4376 struct perf_output_handle handle;
4377 struct perf_event_header header;
4378
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02004379 /* protect the callchain buffers */
4380 rcu_read_lock();
4381
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004382 perf_prepare_sample(&header, data, event, regs);
4383
4384 if (perf_output_begin(&handle, event, header.size, nmi, 1))
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02004385 goto exit;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004386
4387 perf_output_sample(&handle, &header, data, event);
4388
4389 perf_output_end(&handle);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02004390
4391exit:
4392 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004393}
4394
4395/*
4396 * read event_id
4397 */
4398
4399struct perf_read_event {
4400 struct perf_event_header header;
4401
4402 u32 pid;
4403 u32 tid;
4404};
4405
4406static void
4407perf_event_read_event(struct perf_event *event,
4408 struct task_struct *task)
4409{
4410 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004411 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004412 struct perf_read_event read_event = {
4413 .header = {
4414 .type = PERF_RECORD_READ,
4415 .misc = 0,
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02004416 .size = sizeof(read_event) + event->read_size,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004417 },
4418 .pid = perf_event_pid(event, task),
4419 .tid = perf_event_tid(event, task),
4420 };
4421 int ret;
4422
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004423 perf_event_header__init_id(&read_event.header, &sample, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004424 ret = perf_output_begin(&handle, event, read_event.header.size, 0, 0);
4425 if (ret)
4426 return;
4427
4428 perf_output_put(&handle, read_event);
4429 perf_output_read(&handle, event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004430 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004431
4432 perf_output_end(&handle);
4433}
4434
4435/*
4436 * task tracking -- fork/exit
4437 *
Eric B Munson3af9e852010-05-18 15:30:49 +01004438 * enabled by: attr.comm | attr.mmap | attr.mmap_data | attr.task
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004439 */
4440
4441struct perf_task_event {
4442 struct task_struct *task;
4443 struct perf_event_context *task_ctx;
4444
4445 struct {
4446 struct perf_event_header header;
4447
4448 u32 pid;
4449 u32 ppid;
4450 u32 tid;
4451 u32 ptid;
4452 u64 time;
4453 } event_id;
4454};
4455
4456static void perf_event_task_output(struct perf_event *event,
4457 struct perf_task_event *task_event)
4458{
4459 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004460 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004461 struct task_struct *task = task_event->task;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004462 int ret, size = task_event->event_id.header.size;
Mike Galbraith8bb39f92010-03-26 11:11:33 +01004463
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004464 perf_event_header__init_id(&task_event->event_id.header, &sample, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004465
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004466 ret = perf_output_begin(&handle, event,
4467 task_event->event_id.header.size, 0, 0);
Peter Zijlstraef607772010-05-18 10:50:41 +02004468 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004469 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004470
4471 task_event->event_id.pid = perf_event_pid(event, task);
4472 task_event->event_id.ppid = perf_event_pid(event, current);
4473
4474 task_event->event_id.tid = perf_event_tid(event, task);
4475 task_event->event_id.ptid = perf_event_tid(event, current);
4476
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004477 perf_output_put(&handle, task_event->event_id);
4478
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004479 perf_event__output_id_sample(event, &handle, &sample);
4480
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004481 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004482out:
4483 task_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004484}
4485
4486static int perf_event_task_match(struct perf_event *event)
4487{
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01004488 if (event->state < PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra22e19082010-01-18 09:12:32 +01004489 return 0;
4490
Stephane Eranian5632ab12011-01-03 18:20:01 +02004491 if (!event_filter_match(event))
Peter Zijlstra5d27c232009-12-17 13:16:32 +01004492 return 0;
4493
Eric B Munson3af9e852010-05-18 15:30:49 +01004494 if (event->attr.comm || event->attr.mmap ||
4495 event->attr.mmap_data || event->attr.task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004496 return 1;
4497
4498 return 0;
4499}
4500
4501static void perf_event_task_ctx(struct perf_event_context *ctx,
4502 struct perf_task_event *task_event)
4503{
4504 struct perf_event *event;
4505
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004506 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4507 if (perf_event_task_match(event))
4508 perf_event_task_output(event, task_event);
4509 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004510}
4511
4512static void perf_event_task_event(struct perf_task_event *task_event)
4513{
4514 struct perf_cpu_context *cpuctx;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02004515 struct perf_event_context *ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004516 struct pmu *pmu;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02004517 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004518
Peter Zijlstrad6ff86c2009-11-20 22:19:46 +01004519 rcu_read_lock();
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004520 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra41945f62010-09-16 19:17:24 +02004521 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra51676952010-12-07 14:18:20 +01004522 if (cpuctx->active_pmu != pmu)
4523 goto next;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004524 perf_event_task_ctx(&cpuctx->ctx, task_event);
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02004525
4526 ctx = task_event->task_ctx;
4527 if (!ctx) {
4528 ctxn = pmu->task_ctx_nr;
4529 if (ctxn < 0)
Peter Zijlstra41945f62010-09-16 19:17:24 +02004530 goto next;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02004531 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4532 }
4533 if (ctx)
4534 perf_event_task_ctx(ctx, task_event);
Peter Zijlstra41945f62010-09-16 19:17:24 +02004535next:
4536 put_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004537 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004538 rcu_read_unlock();
4539}
4540
4541static void perf_event_task(struct task_struct *task,
4542 struct perf_event_context *task_ctx,
4543 int new)
4544{
4545 struct perf_task_event task_event;
4546
4547 if (!atomic_read(&nr_comm_events) &&
4548 !atomic_read(&nr_mmap_events) &&
4549 !atomic_read(&nr_task_events))
4550 return;
4551
4552 task_event = (struct perf_task_event){
4553 .task = task,
4554 .task_ctx = task_ctx,
4555 .event_id = {
4556 .header = {
4557 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
4558 .misc = 0,
4559 .size = sizeof(task_event.event_id),
4560 },
4561 /* .pid */
4562 /* .ppid */
4563 /* .tid */
4564 /* .ptid */
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01004565 .time = perf_clock(),
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004566 },
4567 };
4568
4569 perf_event_task_event(&task_event);
4570}
4571
4572void perf_event_fork(struct task_struct *task)
4573{
4574 perf_event_task(task, NULL, 1);
4575}
4576
4577/*
4578 * comm tracking
4579 */
4580
4581struct perf_comm_event {
4582 struct task_struct *task;
4583 char *comm;
4584 int comm_size;
4585
4586 struct {
4587 struct perf_event_header header;
4588
4589 u32 pid;
4590 u32 tid;
4591 } event_id;
4592};
4593
4594static void perf_event_comm_output(struct perf_event *event,
4595 struct perf_comm_event *comm_event)
4596{
4597 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004598 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004599 int size = comm_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004600 int ret;
4601
4602 perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
4603 ret = perf_output_begin(&handle, event,
4604 comm_event->event_id.header.size, 0, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004605
4606 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004607 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004608
4609 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
4610 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
4611
4612 perf_output_put(&handle, comm_event->event_id);
4613 perf_output_copy(&handle, comm_event->comm,
4614 comm_event->comm_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004615
4616 perf_event__output_id_sample(event, &handle, &sample);
4617
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004618 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004619out:
4620 comm_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004621}
4622
4623static int perf_event_comm_match(struct perf_event *event)
4624{
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01004625 if (event->state < PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra22e19082010-01-18 09:12:32 +01004626 return 0;
4627
Stephane Eranian5632ab12011-01-03 18:20:01 +02004628 if (!event_filter_match(event))
Peter Zijlstra5d27c232009-12-17 13:16:32 +01004629 return 0;
4630
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004631 if (event->attr.comm)
4632 return 1;
4633
4634 return 0;
4635}
4636
4637static void perf_event_comm_ctx(struct perf_event_context *ctx,
4638 struct perf_comm_event *comm_event)
4639{
4640 struct perf_event *event;
4641
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004642 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4643 if (perf_event_comm_match(event))
4644 perf_event_comm_output(event, comm_event);
4645 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004646}
4647
4648static void perf_event_comm_event(struct perf_comm_event *comm_event)
4649{
4650 struct perf_cpu_context *cpuctx;
4651 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004652 char comm[TASK_COMM_LEN];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004653 unsigned int size;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004654 struct pmu *pmu;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02004655 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004656
4657 memset(comm, 0, sizeof(comm));
Márton Németh96b02d72009-11-21 23:10:15 +01004658 strlcpy(comm, comm_event->task->comm, sizeof(comm));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004659 size = ALIGN(strlen(comm)+1, sizeof(u64));
4660
4661 comm_event->comm = comm;
4662 comm_event->comm_size = size;
4663
4664 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
Peter Zijlstraf6595f32009-11-20 22:19:47 +01004665 rcu_read_lock();
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004666 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra41945f62010-09-16 19:17:24 +02004667 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra51676952010-12-07 14:18:20 +01004668 if (cpuctx->active_pmu != pmu)
4669 goto next;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004670 perf_event_comm_ctx(&cpuctx->ctx, comm_event);
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02004671
4672 ctxn = pmu->task_ctx_nr;
4673 if (ctxn < 0)
Peter Zijlstra41945f62010-09-16 19:17:24 +02004674 goto next;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02004675
4676 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4677 if (ctx)
4678 perf_event_comm_ctx(ctx, comm_event);
Peter Zijlstra41945f62010-09-16 19:17:24 +02004679next:
4680 put_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004681 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004682 rcu_read_unlock();
4683}
4684
4685void perf_event_comm(struct task_struct *task)
4686{
4687 struct perf_comm_event comm_event;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02004688 struct perf_event_context *ctx;
4689 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004690
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02004691 for_each_task_context_nr(ctxn) {
4692 ctx = task->perf_event_ctxp[ctxn];
4693 if (!ctx)
4694 continue;
4695
4696 perf_event_enable_on_exec(ctx);
4697 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004698
4699 if (!atomic_read(&nr_comm_events))
4700 return;
4701
4702 comm_event = (struct perf_comm_event){
4703 .task = task,
4704 /* .comm */
4705 /* .comm_size */
4706 .event_id = {
4707 .header = {
4708 .type = PERF_RECORD_COMM,
4709 .misc = 0,
4710 /* .size */
4711 },
4712 /* .pid */
4713 /* .tid */
4714 },
4715 };
4716
4717 perf_event_comm_event(&comm_event);
4718}
4719
4720/*
4721 * mmap tracking
4722 */
4723
4724struct perf_mmap_event {
4725 struct vm_area_struct *vma;
4726
4727 const char *file_name;
4728 int file_size;
4729
4730 struct {
4731 struct perf_event_header header;
4732
4733 u32 pid;
4734 u32 tid;
4735 u64 start;
4736 u64 len;
4737 u64 pgoff;
4738 } event_id;
4739};
4740
4741static void perf_event_mmap_output(struct perf_event *event,
4742 struct perf_mmap_event *mmap_event)
4743{
4744 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004745 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004746 int size = mmap_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004747 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004748
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004749 perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
4750 ret = perf_output_begin(&handle, event,
4751 mmap_event->event_id.header.size, 0, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004752 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004753 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004754
4755 mmap_event->event_id.pid = perf_event_pid(event, current);
4756 mmap_event->event_id.tid = perf_event_tid(event, current);
4757
4758 perf_output_put(&handle, mmap_event->event_id);
4759 perf_output_copy(&handle, mmap_event->file_name,
4760 mmap_event->file_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004761
4762 perf_event__output_id_sample(event, &handle, &sample);
4763
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004764 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004765out:
4766 mmap_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004767}
4768
4769static int perf_event_mmap_match(struct perf_event *event,
Eric B Munson3af9e852010-05-18 15:30:49 +01004770 struct perf_mmap_event *mmap_event,
4771 int executable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004772{
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01004773 if (event->state < PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra22e19082010-01-18 09:12:32 +01004774 return 0;
4775
Stephane Eranian5632ab12011-01-03 18:20:01 +02004776 if (!event_filter_match(event))
Peter Zijlstra5d27c232009-12-17 13:16:32 +01004777 return 0;
4778
Eric B Munson3af9e852010-05-18 15:30:49 +01004779 if ((!executable && event->attr.mmap_data) ||
4780 (executable && event->attr.mmap))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004781 return 1;
4782
4783 return 0;
4784}
4785
4786static void perf_event_mmap_ctx(struct perf_event_context *ctx,
Eric B Munson3af9e852010-05-18 15:30:49 +01004787 struct perf_mmap_event *mmap_event,
4788 int executable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004789{
4790 struct perf_event *event;
4791
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004792 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Eric B Munson3af9e852010-05-18 15:30:49 +01004793 if (perf_event_mmap_match(event, mmap_event, executable))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004794 perf_event_mmap_output(event, mmap_event);
4795 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004796}
4797
4798static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
4799{
4800 struct perf_cpu_context *cpuctx;
4801 struct perf_event_context *ctx;
4802 struct vm_area_struct *vma = mmap_event->vma;
4803 struct file *file = vma->vm_file;
4804 unsigned int size;
4805 char tmp[16];
4806 char *buf = NULL;
4807 const char *name;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004808 struct pmu *pmu;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02004809 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004810
4811 memset(tmp, 0, sizeof(tmp));
4812
4813 if (file) {
4814 /*
4815 * d_path works from the end of the buffer backwards, so we
4816 * need to add enough zero bytes after the string to handle
4817 * the 64bit alignment we do later.
4818 */
4819 buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
4820 if (!buf) {
4821 name = strncpy(tmp, "//enomem", sizeof(tmp));
4822 goto got_name;
4823 }
4824 name = d_path(&file->f_path, buf, PATH_MAX);
4825 if (IS_ERR(name)) {
4826 name = strncpy(tmp, "//toolong", sizeof(tmp));
4827 goto got_name;
4828 }
4829 } else {
4830 if (arch_vma_name(mmap_event->vma)) {
4831 name = strncpy(tmp, arch_vma_name(mmap_event->vma),
4832 sizeof(tmp));
4833 goto got_name;
4834 }
4835
4836 if (!vma->vm_mm) {
4837 name = strncpy(tmp, "[vdso]", sizeof(tmp));
4838 goto got_name;
Eric B Munson3af9e852010-05-18 15:30:49 +01004839 } else if (vma->vm_start <= vma->vm_mm->start_brk &&
4840 vma->vm_end >= vma->vm_mm->brk) {
4841 name = strncpy(tmp, "[heap]", sizeof(tmp));
4842 goto got_name;
4843 } else if (vma->vm_start <= vma->vm_mm->start_stack &&
4844 vma->vm_end >= vma->vm_mm->start_stack) {
4845 name = strncpy(tmp, "[stack]", sizeof(tmp));
4846 goto got_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004847 }
4848
4849 name = strncpy(tmp, "//anon", sizeof(tmp));
4850 goto got_name;
4851 }
4852
4853got_name:
4854 size = ALIGN(strlen(name)+1, sizeof(u64));
4855
4856 mmap_event->file_name = name;
4857 mmap_event->file_size = size;
4858
4859 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
4860
Peter Zijlstraf6d9dd22009-11-20 22:19:48 +01004861 rcu_read_lock();
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004862 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra41945f62010-09-16 19:17:24 +02004863 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra51676952010-12-07 14:18:20 +01004864 if (cpuctx->active_pmu != pmu)
4865 goto next;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004866 perf_event_mmap_ctx(&cpuctx->ctx, mmap_event,
4867 vma->vm_flags & VM_EXEC);
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02004868
4869 ctxn = pmu->task_ctx_nr;
4870 if (ctxn < 0)
Peter Zijlstra41945f62010-09-16 19:17:24 +02004871 goto next;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02004872
4873 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4874 if (ctx) {
4875 perf_event_mmap_ctx(ctx, mmap_event,
4876 vma->vm_flags & VM_EXEC);
4877 }
Peter Zijlstra41945f62010-09-16 19:17:24 +02004878next:
4879 put_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004880 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004881 rcu_read_unlock();
4882
4883 kfree(buf);
4884}
4885
Eric B Munson3af9e852010-05-18 15:30:49 +01004886void perf_event_mmap(struct vm_area_struct *vma)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004887{
4888 struct perf_mmap_event mmap_event;
4889
4890 if (!atomic_read(&nr_mmap_events))
4891 return;
4892
4893 mmap_event = (struct perf_mmap_event){
4894 .vma = vma,
4895 /* .file_name */
4896 /* .file_size */
4897 .event_id = {
4898 .header = {
4899 .type = PERF_RECORD_MMAP,
Zhang, Yanmin39447b32010-04-19 13:32:41 +08004900 .misc = PERF_RECORD_MISC_USER,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004901 /* .size */
4902 },
4903 /* .pid */
4904 /* .tid */
4905 .start = vma->vm_start,
4906 .len = vma->vm_end - vma->vm_start,
Peter Zijlstra3a0304e2010-02-26 10:33:41 +01004907 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004908 },
4909 };
4910
4911 perf_event_mmap_event(&mmap_event);
4912}
4913
4914/*
4915 * IRQ throttle logging
4916 */
4917
4918static void perf_log_throttle(struct perf_event *event, int enable)
4919{
4920 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004921 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004922 int ret;
4923
4924 struct {
4925 struct perf_event_header header;
4926 u64 time;
4927 u64 id;
4928 u64 stream_id;
4929 } throttle_event = {
4930 .header = {
4931 .type = PERF_RECORD_THROTTLE,
4932 .misc = 0,
4933 .size = sizeof(throttle_event),
4934 },
4935 .time = perf_clock(),
4936 .id = primary_event_id(event),
4937 .stream_id = event->id,
4938 };
4939
4940 if (enable)
4941 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
4942
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004943 perf_event_header__init_id(&throttle_event.header, &sample, event);
4944
4945 ret = perf_output_begin(&handle, event,
4946 throttle_event.header.size, 1, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004947 if (ret)
4948 return;
4949
4950 perf_output_put(&handle, throttle_event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004951 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004952 perf_output_end(&handle);
4953}
4954
4955/*
4956 * Generic event overflow handling, sampling.
4957 */
4958
4959static int __perf_event_overflow(struct perf_event *event, int nmi,
4960 int throttle, struct perf_sample_data *data,
4961 struct pt_regs *regs)
4962{
4963 int events = atomic_read(&event->event_limit);
4964 struct hw_perf_event *hwc = &event->hw;
4965 int ret = 0;
4966
Peter Zijlstra96398822010-11-24 18:55:29 +01004967 /*
4968 * Non-sampling counters might still use the PMI to fold short
4969 * hardware counters, ignore those.
4970 */
4971 if (unlikely(!is_sampling_event(event)))
4972 return 0;
4973
Peter Zijlstra163ec432011-02-16 11:22:34 +01004974 if (unlikely(hwc->interrupts >= max_samples_per_tick)) {
4975 if (throttle) {
4976 hwc->interrupts = MAX_INTERRUPTS;
4977 perf_log_throttle(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004978 ret = 1;
4979 }
Peter Zijlstra163ec432011-02-16 11:22:34 +01004980 } else
4981 hwc->interrupts++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004982
4983 if (event->attr.freq) {
4984 u64 now = perf_clock();
Peter Zijlstraabd50712010-01-26 18:50:16 +01004985 s64 delta = now - hwc->freq_time_stamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004986
Peter Zijlstraabd50712010-01-26 18:50:16 +01004987 hwc->freq_time_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004988
Peter Zijlstraabd50712010-01-26 18:50:16 +01004989 if (delta > 0 && delta < 2*TICK_NSEC)
4990 perf_adjust_period(event, delta, hwc->last_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004991 }
4992
4993 /*
4994 * XXX event_limit might not quite work as expected on inherited
4995 * events
4996 */
4997
4998 event->pending_kill = POLL_IN;
4999 if (events && atomic_dec_and_test(&event->event_limit)) {
5000 ret = 1;
5001 event->pending_kill = POLL_HUP;
5002 if (nmi) {
5003 event->pending_disable = 1;
Peter Zijlstrae360adb2010-10-14 14:01:34 +08005004 irq_work_queue(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005005 } else
5006 perf_event_disable(event);
5007 }
5008
Peter Zijlstra453f19e2009-11-20 22:19:43 +01005009 if (event->overflow_handler)
5010 event->overflow_handler(event, nmi, data, regs);
5011 else
5012 perf_event_output(event, nmi, data, regs);
5013
Peter Zijlstraf506b3d2011-05-26 17:02:53 +02005014 if (event->fasync && event->pending_kill) {
5015 if (nmi) {
5016 event->pending_wakeup = 1;
5017 irq_work_queue(&event->pending);
5018 } else
5019 perf_event_wakeup(event);
5020 }
5021
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005022 return ret;
5023}
5024
5025int perf_event_overflow(struct perf_event *event, int nmi,
5026 struct perf_sample_data *data,
5027 struct pt_regs *regs)
5028{
5029 return __perf_event_overflow(event, nmi, 1, data, regs);
5030}
5031
5032/*
5033 * Generic software event infrastructure
5034 */
5035
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005036struct swevent_htable {
5037 struct swevent_hlist *swevent_hlist;
5038 struct mutex hlist_mutex;
5039 int hlist_refcount;
5040
5041 /* Recursion avoidance in each contexts */
5042 int recursion[PERF_NR_CONTEXTS];
5043};
5044
5045static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
5046
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005047/*
5048 * We directly increment event->count and keep a second value in
5049 * event->hw.period_left to count intervals. This period event
5050 * is kept in the range [-sample_period, 0] so that we can use the
5051 * sign as trigger.
5052 */
5053
5054static u64 perf_swevent_set_period(struct perf_event *event)
5055{
5056 struct hw_perf_event *hwc = &event->hw;
5057 u64 period = hwc->last_period;
5058 u64 nr, offset;
5059 s64 old, val;
5060
5061 hwc->last_period = hwc->sample_period;
5062
5063again:
Peter Zijlstrae7850592010-05-21 14:43:08 +02005064 old = val = local64_read(&hwc->period_left);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005065 if (val < 0)
5066 return 0;
5067
5068 nr = div64_u64(period + val, period);
5069 offset = nr * period;
5070 val -= offset;
Peter Zijlstrae7850592010-05-21 14:43:08 +02005071 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005072 goto again;
5073
5074 return nr;
5075}
5076
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005077static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005078 int nmi, struct perf_sample_data *data,
5079 struct pt_regs *regs)
5080{
5081 struct hw_perf_event *hwc = &event->hw;
5082 int throttle = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005083
5084 data->period = event->hw.last_period;
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005085 if (!overflow)
5086 overflow = perf_swevent_set_period(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005087
5088 if (hwc->interrupts == MAX_INTERRUPTS)
5089 return;
5090
5091 for (; overflow; overflow--) {
5092 if (__perf_event_overflow(event, nmi, throttle,
5093 data, regs)) {
5094 /*
5095 * We inhibit the overflow from happening when
5096 * hwc->interrupts == MAX_INTERRUPTS.
5097 */
5098 break;
5099 }
5100 throttle = 1;
5101 }
5102}
5103
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005104static void perf_swevent_event(struct perf_event *event, u64 nr,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005105 int nmi, struct perf_sample_data *data,
5106 struct pt_regs *regs)
5107{
5108 struct hw_perf_event *hwc = &event->hw;
5109
Peter Zijlstrae7850592010-05-21 14:43:08 +02005110 local64_add(nr, &event->count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005111
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005112 if (!regs)
5113 return;
5114
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01005115 if (!is_sampling_event(event))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005116 return;
5117
5118 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
5119 return perf_swevent_overflow(event, 1, nmi, data, regs);
5120
Peter Zijlstrae7850592010-05-21 14:43:08 +02005121 if (local64_add_negative(nr, &hwc->period_left))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005122 return;
5123
5124 perf_swevent_overflow(event, 0, nmi, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005125}
5126
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005127static int perf_exclude_event(struct perf_event *event,
5128 struct pt_regs *regs)
5129{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005130 if (event->hw.state & PERF_HES_STOPPED)
Frederic Weisbecker91b2f482011-03-07 21:27:08 +01005131 return 1;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005132
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005133 if (regs) {
5134 if (event->attr.exclude_user && user_mode(regs))
5135 return 1;
5136
5137 if (event->attr.exclude_kernel && !user_mode(regs))
5138 return 1;
5139 }
5140
5141 return 0;
5142}
5143
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005144static int perf_swevent_match(struct perf_event *event,
5145 enum perf_type_id type,
Li Zefan6fb29152009-10-15 11:21:42 +08005146 u32 event_id,
5147 struct perf_sample_data *data,
5148 struct pt_regs *regs)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005149{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005150 if (event->attr.type != type)
5151 return 0;
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005152
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005153 if (event->attr.config != event_id)
5154 return 0;
5155
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005156 if (perf_exclude_event(event, regs))
5157 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005158
5159 return 1;
5160}
5161
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005162static inline u64 swevent_hash(u64 type, u32 event_id)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005163{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005164 u64 val = event_id | (type << 32);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005165
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005166 return hash_64(val, SWEVENT_HLIST_BITS);
5167}
5168
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005169static inline struct hlist_head *
5170__find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005171{
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005172 u64 hash = swevent_hash(type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005173
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005174 return &hlist->heads[hash];
5175}
5176
5177/* For the read side: events when they trigger */
5178static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005179find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005180{
5181 struct swevent_hlist *hlist;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005182
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005183 hlist = rcu_dereference(swhash->swevent_hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005184 if (!hlist)
5185 return NULL;
5186
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005187 return __find_swevent_head(hlist, type, event_id);
5188}
5189
5190/* For the event head insertion and removal in the hlist */
5191static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005192find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005193{
5194 struct swevent_hlist *hlist;
5195 u32 event_id = event->attr.config;
5196 u64 type = event->attr.type;
5197
5198 /*
5199 * Event scheduling is always serialized against hlist allocation
5200 * and release. Which makes the protected version suitable here.
5201 * The context lock guarantees that.
5202 */
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005203 hlist = rcu_dereference_protected(swhash->swevent_hlist,
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005204 lockdep_is_held(&event->ctx->lock));
5205 if (!hlist)
5206 return NULL;
5207
5208 return __find_swevent_head(hlist, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005209}
5210
5211static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
5212 u64 nr, int nmi,
5213 struct perf_sample_data *data,
5214 struct pt_regs *regs)
5215{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005216 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005217 struct perf_event *event;
5218 struct hlist_node *node;
5219 struct hlist_head *head;
5220
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005221 rcu_read_lock();
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005222 head = find_swevent_head_rcu(swhash, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005223 if (!head)
5224 goto end;
5225
5226 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
Li Zefan6fb29152009-10-15 11:21:42 +08005227 if (perf_swevent_match(event, type, event_id, data, regs))
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005228 perf_swevent_event(event, nr, nmi, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005229 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005230end:
5231 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005232}
5233
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01005234int perf_swevent_get_recursion_context(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005235{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005236 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01005237
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005238 return get_recursion_context(swhash->recursion);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005239}
Ingo Molnar645e8cc2009-11-22 12:20:19 +01005240EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005241
Jesper Juhlfa9f90b2010-11-28 21:39:34 +01005242inline void perf_swevent_put_recursion_context(int rctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005243{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005244 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02005245
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005246 put_recursion_context(swhash->recursion, rctx);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01005247}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005248
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005249void __perf_sw_event(u32 event_id, u64 nr, int nmi,
5250 struct pt_regs *regs, u64 addr)
5251{
Ingo Molnara4234bf2009-11-23 10:57:59 +01005252 struct perf_sample_data data;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01005253 int rctx;
5254
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005255 preempt_disable_notrace();
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01005256 rctx = perf_swevent_get_recursion_context();
5257 if (rctx < 0)
5258 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005259
Peter Zijlstradc1d6282010-03-03 15:55:04 +01005260 perf_sample_data_init(&data, addr);
Ingo Molnara4234bf2009-11-23 10:57:59 +01005261
5262 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, nmi, &data, regs);
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01005263
5264 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005265 preempt_enable_notrace();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005266}
5267
5268static void perf_swevent_read(struct perf_event *event)
5269{
5270}
5271
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005272static int perf_swevent_add(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005273{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005274 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005275 struct hw_perf_event *hwc = &event->hw;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005276 struct hlist_head *head;
5277
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01005278 if (is_sampling_event(event)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005279 hwc->last_period = hwc->sample_period;
5280 perf_swevent_set_period(event);
5281 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005282
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005283 hwc->state = !(flags & PERF_EF_START);
5284
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005285 head = find_swevent_head(swhash, event);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005286 if (WARN_ON_ONCE(!head))
5287 return -EINVAL;
5288
5289 hlist_add_head_rcu(&event->hlist_entry, head);
5290
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005291 return 0;
5292}
5293
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005294static void perf_swevent_del(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005295{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005296 hlist_del_rcu(&event->hlist_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005297}
5298
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005299static void perf_swevent_start(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02005300{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005301 event->hw.state = 0;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02005302}
5303
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005304static void perf_swevent_stop(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02005305{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005306 event->hw.state = PERF_HES_STOPPED;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02005307}
5308
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005309/* Deref the hlist from the update side */
5310static inline struct swevent_hlist *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005311swevent_hlist_deref(struct swevent_htable *swhash)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005312{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005313 return rcu_dereference_protected(swhash->swevent_hlist,
5314 lockdep_is_held(&swhash->hlist_mutex));
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005315}
5316
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005317static void swevent_hlist_release(struct swevent_htable *swhash)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005318{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005319 struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005320
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005321 if (!hlist)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005322 return;
5323
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005324 rcu_assign_pointer(swhash->swevent_hlist, NULL);
Lai Jiangshanfa4bbc42011-03-18 12:08:29 +08005325 kfree_rcu(hlist, rcu_head);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005326}
5327
5328static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
5329{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005330 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005331
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005332 mutex_lock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005333
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005334 if (!--swhash->hlist_refcount)
5335 swevent_hlist_release(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005336
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005337 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005338}
5339
5340static void swevent_hlist_put(struct perf_event *event)
5341{
5342 int cpu;
5343
5344 if (event->cpu != -1) {
5345 swevent_hlist_put_cpu(event, event->cpu);
5346 return;
5347 }
5348
5349 for_each_possible_cpu(cpu)
5350 swevent_hlist_put_cpu(event, cpu);
5351}
5352
5353static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
5354{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005355 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005356 int err = 0;
5357
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005358 mutex_lock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005359
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005360 if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005361 struct swevent_hlist *hlist;
5362
5363 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
5364 if (!hlist) {
5365 err = -ENOMEM;
5366 goto exit;
5367 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005368 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005369 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005370 swhash->hlist_refcount++;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02005371exit:
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005372 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005373
5374 return err;
5375}
5376
5377static int swevent_hlist_get(struct perf_event *event)
5378{
5379 int err;
5380 int cpu, failed_cpu;
5381
5382 if (event->cpu != -1)
5383 return swevent_hlist_get_cpu(event, event->cpu);
5384
5385 get_online_cpus();
5386 for_each_possible_cpu(cpu) {
5387 err = swevent_hlist_get_cpu(event, cpu);
5388 if (err) {
5389 failed_cpu = cpu;
5390 goto fail;
5391 }
5392 }
5393 put_online_cpus();
5394
5395 return 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02005396fail:
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005397 for_each_possible_cpu(cpu) {
5398 if (cpu == failed_cpu)
5399 break;
5400 swevent_hlist_put_cpu(event, cpu);
5401 }
5402
5403 put_online_cpus();
5404 return err;
5405}
5406
Jason Barond430d3d2011-03-16 17:29:47 -04005407struct jump_label_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
Frederic Weisbecker95476b62010-04-14 23:42:18 +02005408
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005409static void sw_perf_event_destroy(struct perf_event *event)
5410{
5411 u64 event_id = event->attr.config;
5412
5413 WARN_ON(event->parent);
5414
Peter Zijlstra7e54a5a2010-10-14 22:32:45 +02005415 jump_label_dec(&perf_swevent_enabled[event_id]);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005416 swevent_hlist_put(event);
5417}
5418
5419static int perf_swevent_init(struct perf_event *event)
5420{
5421 int event_id = event->attr.config;
5422
5423 if (event->attr.type != PERF_TYPE_SOFTWARE)
5424 return -ENOENT;
5425
5426 switch (event_id) {
5427 case PERF_COUNT_SW_CPU_CLOCK:
5428 case PERF_COUNT_SW_TASK_CLOCK:
5429 return -ENOENT;
5430
5431 default:
5432 break;
5433 }
5434
Dan Carpenterce677832010-10-24 21:50:42 +02005435 if (event_id >= PERF_COUNT_SW_MAX)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005436 return -ENOENT;
5437
5438 if (!event->parent) {
5439 int err;
5440
5441 err = swevent_hlist_get(event);
5442 if (err)
5443 return err;
5444
Peter Zijlstra7e54a5a2010-10-14 22:32:45 +02005445 jump_label_inc(&perf_swevent_enabled[event_id]);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005446 event->destroy = sw_perf_event_destroy;
5447 }
5448
5449 return 0;
5450}
5451
5452static struct pmu perf_swevent = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005453 .task_ctx_nr = perf_sw_context,
5454
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005455 .event_init = perf_swevent_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005456 .add = perf_swevent_add,
5457 .del = perf_swevent_del,
5458 .start = perf_swevent_start,
5459 .stop = perf_swevent_stop,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005460 .read = perf_swevent_read,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005461};
Frederic Weisbecker95476b62010-04-14 23:42:18 +02005462
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005463#ifdef CONFIG_EVENT_TRACING
5464
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005465static int perf_tp_filter_match(struct perf_event *event,
Frederic Weisbecker95476b62010-04-14 23:42:18 +02005466 struct perf_sample_data *data)
5467{
5468 void *record = data->raw->data;
5469
5470 if (likely(!event->filter) || filter_match_preds(event->filter, record))
5471 return 1;
5472 return 0;
5473}
5474
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005475static int perf_tp_event_match(struct perf_event *event,
5476 struct perf_sample_data *data,
5477 struct pt_regs *regs)
5478{
Frederic Weisbeckera0f7d0f2011-03-07 21:27:09 +01005479 if (event->hw.state & PERF_HES_STOPPED)
5480 return 0;
Peter Zijlstra580d6072010-05-20 20:54:31 +02005481 /*
5482 * All tracepoints are from kernel-space.
5483 */
5484 if (event->attr.exclude_kernel)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005485 return 0;
5486
5487 if (!perf_tp_filter_match(event, data))
5488 return 0;
5489
5490 return 1;
5491}
5492
5493void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
Peter Zijlstraecc55f82010-05-21 15:11:34 +02005494 struct pt_regs *regs, struct hlist_head *head, int rctx)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005495{
5496 struct perf_sample_data data;
5497 struct perf_event *event;
5498 struct hlist_node *node;
5499
5500 struct perf_raw_record raw = {
5501 .size = entry_size,
5502 .data = record,
5503 };
5504
5505 perf_sample_data_init(&data, addr);
5506 data.raw = &raw;
5507
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005508 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
5509 if (perf_tp_event_match(event, &data, regs))
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005510 perf_swevent_event(event, count, 1, &data, regs);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005511 }
Peter Zijlstraecc55f82010-05-21 15:11:34 +02005512
5513 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005514}
5515EXPORT_SYMBOL_GPL(perf_tp_event);
5516
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005517static void tp_perf_event_destroy(struct perf_event *event)
5518{
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005519 perf_trace_destroy(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005520}
5521
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005522static int perf_tp_event_init(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005523{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005524 int err;
5525
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005526 if (event->attr.type != PERF_TYPE_TRACEPOINT)
5527 return -ENOENT;
5528
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005529 err = perf_trace_init(event);
5530 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005531 return err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005532
5533 event->destroy = tp_perf_event_destroy;
5534
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005535 return 0;
5536}
5537
5538static struct pmu perf_tracepoint = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005539 .task_ctx_nr = perf_sw_context,
5540
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005541 .event_init = perf_tp_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005542 .add = perf_trace_add,
5543 .del = perf_trace_del,
5544 .start = perf_swevent_start,
5545 .stop = perf_swevent_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005546 .read = perf_swevent_read,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005547};
5548
5549static inline void perf_tp_register(void)
5550{
Peter Zijlstra2e80a822010-11-17 23:17:36 +01005551 perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005552}
Li Zefan6fb29152009-10-15 11:21:42 +08005553
5554static int perf_event_set_filter(struct perf_event *event, void __user *arg)
5555{
5556 char *filter_str;
5557 int ret;
5558
5559 if (event->attr.type != PERF_TYPE_TRACEPOINT)
5560 return -EINVAL;
5561
5562 filter_str = strndup_user(arg, PAGE_SIZE);
5563 if (IS_ERR(filter_str))
5564 return PTR_ERR(filter_str);
5565
5566 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
5567
5568 kfree(filter_str);
5569 return ret;
5570}
5571
5572static void perf_event_free_filter(struct perf_event *event)
5573{
5574 ftrace_profile_free_filter(event);
5575}
5576
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005577#else
Li Zefan6fb29152009-10-15 11:21:42 +08005578
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005579static inline void perf_tp_register(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005580{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005581}
Li Zefan6fb29152009-10-15 11:21:42 +08005582
5583static int perf_event_set_filter(struct perf_event *event, void __user *arg)
5584{
5585 return -ENOENT;
5586}
5587
5588static void perf_event_free_filter(struct perf_event *event)
5589{
5590}
5591
Li Zefan07b139c2009-12-21 14:27:35 +08005592#endif /* CONFIG_EVENT_TRACING */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005593
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02005594#ifdef CONFIG_HAVE_HW_BREAKPOINT
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005595void perf_bp_event(struct perf_event *bp, void *data)
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02005596{
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005597 struct perf_sample_data sample;
5598 struct pt_regs *regs = data;
5599
Peter Zijlstradc1d6282010-03-03 15:55:04 +01005600 perf_sample_data_init(&sample, bp->attr.bp_addr);
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005601
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005602 if (!bp->hw.state && !perf_exclude_event(bp, regs))
5603 perf_swevent_event(bp, 1, 1, &sample, regs);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02005604}
5605#endif
5606
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005607/*
5608 * hrtimer based swevent callback
5609 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005610
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005611static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005612{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005613 enum hrtimer_restart ret = HRTIMER_RESTART;
5614 struct perf_sample_data data;
5615 struct pt_regs *regs;
5616 struct perf_event *event;
5617 u64 period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005618
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005619 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
Peter Zijlstraba3dd362011-02-15 12:41:46 +01005620
5621 if (event->state != PERF_EVENT_STATE_ACTIVE)
5622 return HRTIMER_NORESTART;
5623
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005624 event->pmu->read(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005625
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005626 perf_sample_data_init(&data, 0);
5627 data.period = event->hw.last_period;
5628 regs = get_irq_regs();
5629
5630 if (regs && !perf_exclude_event(event, regs)) {
5631 if (!(event->attr.exclude_idle && current->pid == 0))
5632 if (perf_event_overflow(event, 0, &data, regs))
5633 ret = HRTIMER_NORESTART;
5634 }
5635
5636 period = max_t(u64, 10000, event->hw.sample_period);
5637 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
5638
5639 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005640}
5641
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005642static void perf_swevent_start_hrtimer(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005643{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005644 struct hw_perf_event *hwc = &event->hw;
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01005645 s64 period;
5646
5647 if (!is_sampling_event(event))
5648 return;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005649
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01005650 period = local64_read(&hwc->period_left);
5651 if (period) {
5652 if (period < 0)
5653 period = 10000;
Peter Zijlstrafa407f32010-06-24 12:35:12 +02005654
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01005655 local64_set(&hwc->period_left, 0);
5656 } else {
5657 period = max_t(u64, 10000, hwc->sample_period);
5658 }
5659 __hrtimer_start_range_ns(&hwc->hrtimer,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005660 ns_to_ktime(period), 0,
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02005661 HRTIMER_MODE_REL_PINNED, 0);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005662}
5663
5664static void perf_swevent_cancel_hrtimer(struct perf_event *event)
5665{
5666 struct hw_perf_event *hwc = &event->hw;
5667
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01005668 if (is_sampling_event(event)) {
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005669 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
Peter Zijlstrafa407f32010-06-24 12:35:12 +02005670 local64_set(&hwc->period_left, ktime_to_ns(remaining));
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005671
5672 hrtimer_cancel(&hwc->hrtimer);
5673 }
5674}
5675
Peter Zijlstraba3dd362011-02-15 12:41:46 +01005676static void perf_swevent_init_hrtimer(struct perf_event *event)
5677{
5678 struct hw_perf_event *hwc = &event->hw;
5679
5680 if (!is_sampling_event(event))
5681 return;
5682
5683 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
5684 hwc->hrtimer.function = perf_swevent_hrtimer;
5685
5686 /*
5687 * Since hrtimers have a fixed rate, we can do a static freq->period
5688 * mapping and avoid the whole period adjust feedback stuff.
5689 */
5690 if (event->attr.freq) {
5691 long freq = event->attr.sample_freq;
5692
5693 event->attr.sample_period = NSEC_PER_SEC / freq;
5694 hwc->sample_period = event->attr.sample_period;
5695 local64_set(&hwc->period_left, hwc->sample_period);
5696 event->attr.freq = 0;
5697 }
5698}
5699
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005700/*
5701 * Software event: cpu wall time clock
5702 */
5703
5704static void cpu_clock_event_update(struct perf_event *event)
5705{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005706 s64 prev;
5707 u64 now;
5708
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005709 now = local_clock();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005710 prev = local64_xchg(&event->hw.prev_count, now);
5711 local64_add(now - prev, &event->count);
5712}
5713
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005714static void cpu_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005715{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005716 local64_set(&event->hw.prev_count, local_clock());
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005717 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005718}
5719
5720static void cpu_clock_event_stop(struct perf_event *event, int flags)
5721{
5722 perf_swevent_cancel_hrtimer(event);
5723 cpu_clock_event_update(event);
5724}
5725
5726static int cpu_clock_event_add(struct perf_event *event, int flags)
5727{
5728 if (flags & PERF_EF_START)
5729 cpu_clock_event_start(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005730
5731 return 0;
5732}
5733
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005734static void cpu_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005735{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005736 cpu_clock_event_stop(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005737}
5738
5739static void cpu_clock_event_read(struct perf_event *event)
5740{
5741 cpu_clock_event_update(event);
5742}
5743
5744static int cpu_clock_event_init(struct perf_event *event)
5745{
5746 if (event->attr.type != PERF_TYPE_SOFTWARE)
5747 return -ENOENT;
5748
5749 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
5750 return -ENOENT;
5751
Peter Zijlstraba3dd362011-02-15 12:41:46 +01005752 perf_swevent_init_hrtimer(event);
5753
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005754 return 0;
5755}
5756
5757static struct pmu perf_cpu_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005758 .task_ctx_nr = perf_sw_context,
5759
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005760 .event_init = cpu_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005761 .add = cpu_clock_event_add,
5762 .del = cpu_clock_event_del,
5763 .start = cpu_clock_event_start,
5764 .stop = cpu_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005765 .read = cpu_clock_event_read,
5766};
5767
5768/*
5769 * Software event: task time clock
5770 */
5771
5772static void task_clock_event_update(struct perf_event *event, u64 now)
5773{
5774 u64 prev;
5775 s64 delta;
5776
5777 prev = local64_xchg(&event->hw.prev_count, now);
5778 delta = now - prev;
5779 local64_add(delta, &event->count);
5780}
5781
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005782static void task_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005783{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005784 local64_set(&event->hw.prev_count, event->ctx->time);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005785 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005786}
5787
5788static void task_clock_event_stop(struct perf_event *event, int flags)
5789{
5790 perf_swevent_cancel_hrtimer(event);
5791 task_clock_event_update(event, event->ctx->time);
5792}
5793
5794static int task_clock_event_add(struct perf_event *event, int flags)
5795{
5796 if (flags & PERF_EF_START)
5797 task_clock_event_start(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005798
5799 return 0;
5800}
5801
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005802static void task_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005803{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005804 task_clock_event_stop(event, PERF_EF_UPDATE);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005805}
5806
5807static void task_clock_event_read(struct perf_event *event)
5808{
Peter Zijlstra768a06e2011-02-22 16:52:24 +01005809 u64 now = perf_clock();
5810 u64 delta = now - event->ctx->timestamp;
5811 u64 time = event->ctx->time + delta;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005812
5813 task_clock_event_update(event, time);
5814}
5815
5816static int task_clock_event_init(struct perf_event *event)
5817{
5818 if (event->attr.type != PERF_TYPE_SOFTWARE)
5819 return -ENOENT;
5820
5821 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
5822 return -ENOENT;
5823
Peter Zijlstraba3dd362011-02-15 12:41:46 +01005824 perf_swevent_init_hrtimer(event);
5825
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005826 return 0;
5827}
5828
5829static struct pmu perf_task_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005830 .task_ctx_nr = perf_sw_context,
5831
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005832 .event_init = task_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005833 .add = task_clock_event_add,
5834 .del = task_clock_event_del,
5835 .start = task_clock_event_start,
5836 .stop = task_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005837 .read = task_clock_event_read,
5838};
5839
Peter Zijlstraad5133b2010-06-15 12:22:39 +02005840static void perf_pmu_nop_void(struct pmu *pmu)
5841{
5842}
5843
5844static int perf_pmu_nop_int(struct pmu *pmu)
5845{
5846 return 0;
5847}
5848
5849static void perf_pmu_start_txn(struct pmu *pmu)
5850{
5851 perf_pmu_disable(pmu);
5852}
5853
5854static int perf_pmu_commit_txn(struct pmu *pmu)
5855{
5856 perf_pmu_enable(pmu);
5857 return 0;
5858}
5859
5860static void perf_pmu_cancel_txn(struct pmu *pmu)
5861{
5862 perf_pmu_enable(pmu);
5863}
5864
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02005865/*
5866 * Ensures all contexts with the same task_ctx_nr have the same
5867 * pmu_cpu_context too.
5868 */
5869static void *find_pmu_context(int ctxn)
5870{
5871 struct pmu *pmu;
5872
5873 if (ctxn < 0)
5874 return NULL;
5875
5876 list_for_each_entry(pmu, &pmus, entry) {
5877 if (pmu->task_ctx_nr == ctxn)
5878 return pmu->pmu_cpu_context;
5879 }
5880
5881 return NULL;
5882}
5883
Peter Zijlstra51676952010-12-07 14:18:20 +01005884static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu)
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02005885{
Peter Zijlstra51676952010-12-07 14:18:20 +01005886 int cpu;
5887
5888 for_each_possible_cpu(cpu) {
5889 struct perf_cpu_context *cpuctx;
5890
5891 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
5892
5893 if (cpuctx->active_pmu == old_pmu)
5894 cpuctx->active_pmu = pmu;
5895 }
5896}
5897
5898static void free_pmu_context(struct pmu *pmu)
5899{
5900 struct pmu *i;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02005901
5902 mutex_lock(&pmus_lock);
5903 /*
5904 * Like a real lame refcount.
5905 */
Peter Zijlstra51676952010-12-07 14:18:20 +01005906 list_for_each_entry(i, &pmus, entry) {
5907 if (i->pmu_cpu_context == pmu->pmu_cpu_context) {
5908 update_pmu_context(i, pmu);
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02005909 goto out;
Peter Zijlstra51676952010-12-07 14:18:20 +01005910 }
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02005911 }
5912
Peter Zijlstra51676952010-12-07 14:18:20 +01005913 free_percpu(pmu->pmu_cpu_context);
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02005914out:
5915 mutex_unlock(&pmus_lock);
5916}
Peter Zijlstra2e80a822010-11-17 23:17:36 +01005917static struct idr pmu_idr;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02005918
Peter Zijlstraabe43402010-11-17 23:17:37 +01005919static ssize_t
5920type_show(struct device *dev, struct device_attribute *attr, char *page)
5921{
5922 struct pmu *pmu = dev_get_drvdata(dev);
5923
5924 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type);
5925}
5926
5927static struct device_attribute pmu_dev_attrs[] = {
5928 __ATTR_RO(type),
5929 __ATTR_NULL,
5930};
5931
5932static int pmu_bus_running;
5933static struct bus_type pmu_bus = {
5934 .name = "event_source",
5935 .dev_attrs = pmu_dev_attrs,
5936};
5937
5938static void pmu_dev_release(struct device *dev)
5939{
5940 kfree(dev);
5941}
5942
5943static int pmu_dev_alloc(struct pmu *pmu)
5944{
5945 int ret = -ENOMEM;
5946
5947 pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
5948 if (!pmu->dev)
5949 goto out;
5950
5951 device_initialize(pmu->dev);
5952 ret = dev_set_name(pmu->dev, "%s", pmu->name);
5953 if (ret)
5954 goto free_dev;
5955
5956 dev_set_drvdata(pmu->dev, pmu);
5957 pmu->dev->bus = &pmu_bus;
5958 pmu->dev->release = pmu_dev_release;
5959 ret = device_add(pmu->dev);
5960 if (ret)
5961 goto free_dev;
5962
5963out:
5964 return ret;
5965
5966free_dev:
5967 put_device(pmu->dev);
5968 goto out;
5969}
5970
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01005971static struct lock_class_key cpuctx_mutex;
Peter Zijlstrafacc4302011-04-09 21:17:42 +02005972static struct lock_class_key cpuctx_lock;
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01005973
Peter Zijlstra2e80a822010-11-17 23:17:36 +01005974int perf_pmu_register(struct pmu *pmu, char *name, int type)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005975{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005976 int cpu, ret;
Peter Zijlstra33696fc2010-06-14 08:49:00 +02005977
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005978 mutex_lock(&pmus_lock);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02005979 ret = -ENOMEM;
5980 pmu->pmu_disable_count = alloc_percpu(int);
5981 if (!pmu->pmu_disable_count)
5982 goto unlock;
Peter Zijlstraad5133b2010-06-15 12:22:39 +02005983
Peter Zijlstra2e80a822010-11-17 23:17:36 +01005984 pmu->type = -1;
5985 if (!name)
5986 goto skip_type;
5987 pmu->name = name;
5988
5989 if (type < 0) {
5990 int err = idr_pre_get(&pmu_idr, GFP_KERNEL);
5991 if (!err)
5992 goto free_pdc;
5993
5994 err = idr_get_new_above(&pmu_idr, pmu, PERF_TYPE_MAX, &type);
5995 if (err) {
5996 ret = err;
5997 goto free_pdc;
5998 }
5999 }
6000 pmu->type = type;
6001
Peter Zijlstraabe43402010-11-17 23:17:37 +01006002 if (pmu_bus_running) {
6003 ret = pmu_dev_alloc(pmu);
6004 if (ret)
6005 goto free_idr;
6006 }
6007
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006008skip_type:
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02006009 pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
6010 if (pmu->pmu_cpu_context)
6011 goto got_cpu_context;
6012
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006013 pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
6014 if (!pmu->pmu_cpu_context)
Peter Zijlstraabe43402010-11-17 23:17:37 +01006015 goto free_dev;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006016
6017 for_each_possible_cpu(cpu) {
6018 struct perf_cpu_context *cpuctx;
6019
6020 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Peter Zijlstraeb184472010-09-07 15:55:13 +02006021 __perf_event_init_context(&cpuctx->ctx);
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01006022 lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex);
Peter Zijlstrafacc4302011-04-09 21:17:42 +02006023 lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006024 cpuctx->ctx.type = cpu_context;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006025 cpuctx->ctx.pmu = pmu;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02006026 cpuctx->jiffies_interval = 1;
6027 INIT_LIST_HEAD(&cpuctx->rotation_list);
Peter Zijlstra51676952010-12-07 14:18:20 +01006028 cpuctx->active_pmu = pmu;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006029 }
6030
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02006031got_cpu_context:
Peter Zijlstraad5133b2010-06-15 12:22:39 +02006032 if (!pmu->start_txn) {
6033 if (pmu->pmu_enable) {
6034 /*
6035 * If we have pmu_enable/pmu_disable calls, install
6036 * transaction stubs that use that to try and batch
6037 * hardware accesses.
6038 */
6039 pmu->start_txn = perf_pmu_start_txn;
6040 pmu->commit_txn = perf_pmu_commit_txn;
6041 pmu->cancel_txn = perf_pmu_cancel_txn;
6042 } else {
6043 pmu->start_txn = perf_pmu_nop_void;
6044 pmu->commit_txn = perf_pmu_nop_int;
6045 pmu->cancel_txn = perf_pmu_nop_void;
6046 }
6047 }
6048
6049 if (!pmu->pmu_enable) {
6050 pmu->pmu_enable = perf_pmu_nop_void;
6051 pmu->pmu_disable = perf_pmu_nop_void;
6052 }
6053
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006054 list_add_rcu(&pmu->entry, &pmus);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02006055 ret = 0;
6056unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006057 mutex_unlock(&pmus_lock);
6058
Peter Zijlstra33696fc2010-06-14 08:49:00 +02006059 return ret;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006060
Peter Zijlstraabe43402010-11-17 23:17:37 +01006061free_dev:
6062 device_del(pmu->dev);
6063 put_device(pmu->dev);
6064
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006065free_idr:
6066 if (pmu->type >= PERF_TYPE_MAX)
6067 idr_remove(&pmu_idr, pmu->type);
6068
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006069free_pdc:
6070 free_percpu(pmu->pmu_disable_count);
6071 goto unlock;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006072}
6073
6074void perf_pmu_unregister(struct pmu *pmu)
6075{
6076 mutex_lock(&pmus_lock);
6077 list_del_rcu(&pmu->entry);
6078 mutex_unlock(&pmus_lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006079
6080 /*
Peter Zijlstracde8e882010-09-13 11:06:55 +02006081 * We dereference the pmu list under both SRCU and regular RCU, so
6082 * synchronize against both of those.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006083 */
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006084 synchronize_srcu(&pmus_srcu);
Peter Zijlstracde8e882010-09-13 11:06:55 +02006085 synchronize_rcu();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006086
Peter Zijlstra33696fc2010-06-14 08:49:00 +02006087 free_percpu(pmu->pmu_disable_count);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006088 if (pmu->type >= PERF_TYPE_MAX)
6089 idr_remove(&pmu_idr, pmu->type);
Peter Zijlstraabe43402010-11-17 23:17:37 +01006090 device_del(pmu->dev);
6091 put_device(pmu->dev);
Peter Zijlstra51676952010-12-07 14:18:20 +01006092 free_pmu_context(pmu);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006093}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006094
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006095struct pmu *perf_init_event(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006096{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02006097 struct pmu *pmu = NULL;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006098 int idx;
Lin Ming940c5b22011-02-27 21:13:31 +08006099 int ret;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006100
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006101 idx = srcu_read_lock(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006102
6103 rcu_read_lock();
6104 pmu = idr_find(&pmu_idr, event->attr.type);
6105 rcu_read_unlock();
Lin Ming940c5b22011-02-27 21:13:31 +08006106 if (pmu) {
6107 ret = pmu->event_init(event);
6108 if (ret)
6109 pmu = ERR_PTR(ret);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006110 goto unlock;
Lin Ming940c5b22011-02-27 21:13:31 +08006111 }
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006112
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006113 list_for_each_entry_rcu(pmu, &pmus, entry) {
Lin Ming940c5b22011-02-27 21:13:31 +08006114 ret = pmu->event_init(event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006115 if (!ret)
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02006116 goto unlock;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006117
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006118 if (ret != -ENOENT) {
6119 pmu = ERR_PTR(ret);
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02006120 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006121 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006122 }
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02006123 pmu = ERR_PTR(-ENOENT);
6124unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006125 srcu_read_unlock(&pmus_srcu, idx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006126
6127 return pmu;
6128}
6129
6130/*
6131 * Allocate and initialize a event structure
6132 */
6133static struct perf_event *
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006134perf_event_alloc(struct perf_event_attr *attr, int cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +02006135 struct task_struct *task,
6136 struct perf_event *group_leader,
6137 struct perf_event *parent_event,
6138 perf_overflow_handler_t overflow_handler)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006139{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02006140 struct pmu *pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006141 struct perf_event *event;
6142 struct hw_perf_event *hwc;
6143 long err;
6144
Oleg Nesterov66832eb2011-01-18 17:10:32 +01006145 if ((unsigned)cpu >= nr_cpu_ids) {
6146 if (!task || cpu != -1)
6147 return ERR_PTR(-EINVAL);
6148 }
6149
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006150 event = kzalloc(sizeof(*event), GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006151 if (!event)
6152 return ERR_PTR(-ENOMEM);
6153
6154 /*
6155 * Single events are their own group leaders, with an
6156 * empty sibling list:
6157 */
6158 if (!group_leader)
6159 group_leader = event;
6160
6161 mutex_init(&event->child_mutex);
6162 INIT_LIST_HEAD(&event->child_list);
6163
6164 INIT_LIST_HEAD(&event->group_entry);
6165 INIT_LIST_HEAD(&event->event_entry);
6166 INIT_LIST_HEAD(&event->sibling_list);
6167 init_waitqueue_head(&event->waitq);
Peter Zijlstrae360adb2010-10-14 14:01:34 +08006168 init_irq_work(&event->pending, perf_pending_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006169
6170 mutex_init(&event->mmap_mutex);
6171
6172 event->cpu = cpu;
6173 event->attr = *attr;
6174 event->group_leader = group_leader;
6175 event->pmu = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006176 event->oncpu = -1;
6177
6178 event->parent = parent_event;
6179
6180 event->ns = get_pid_ns(current->nsproxy->pid_ns);
6181 event->id = atomic64_inc_return(&perf_event_id);
6182
6183 event->state = PERF_EVENT_STATE_INACTIVE;
6184
Peter Zijlstrad580ff82010-10-14 17:43:23 +02006185 if (task) {
6186 event->attach_state = PERF_ATTACH_TASK;
6187#ifdef CONFIG_HAVE_HW_BREAKPOINT
6188 /*
6189 * hw_breakpoint is a bit difficult here..
6190 */
6191 if (attr->type == PERF_TYPE_BREAKPOINT)
6192 event->hw.bp_target = task;
6193#endif
6194 }
6195
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01006196 if (!overflow_handler && parent_event)
6197 overflow_handler = parent_event->overflow_handler;
Oleg Nesterov66832eb2011-01-18 17:10:32 +01006198
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01006199 event->overflow_handler = overflow_handler;
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02006200
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006201 if (attr->disabled)
6202 event->state = PERF_EVENT_STATE_OFF;
6203
6204 pmu = NULL;
6205
6206 hwc = &event->hw;
6207 hwc->sample_period = attr->sample_period;
6208 if (attr->freq && attr->sample_freq)
6209 hwc->sample_period = 1;
6210 hwc->last_period = hwc->sample_period;
6211
Peter Zijlstrae7850592010-05-21 14:43:08 +02006212 local64_set(&hwc->period_left, hwc->sample_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006213
6214 /*
6215 * we currently do not support PERF_FORMAT_GROUP on inherited events
6216 */
6217 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
6218 goto done;
6219
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006220 pmu = perf_init_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006221
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006222done:
6223 err = 0;
6224 if (!pmu)
6225 err = -EINVAL;
6226 else if (IS_ERR(pmu))
6227 err = PTR_ERR(pmu);
6228
6229 if (err) {
6230 if (event->ns)
6231 put_pid_ns(event->ns);
6232 kfree(event);
6233 return ERR_PTR(err);
6234 }
6235
6236 event->pmu = pmu;
6237
6238 if (!event->parent) {
Peter Zijlstra82cd6de2010-10-14 17:57:23 +02006239 if (event->attach_state & PERF_ATTACH_TASK)
Stephane Eraniane5d13672011-02-14 11:20:01 +02006240 jump_label_inc(&perf_sched_events);
Eric B Munson3af9e852010-05-18 15:30:49 +01006241 if (event->attr.mmap || event->attr.mmap_data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006242 atomic_inc(&nr_mmap_events);
6243 if (event->attr.comm)
6244 atomic_inc(&nr_comm_events);
6245 if (event->attr.task)
6246 atomic_inc(&nr_task_events);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02006247 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
6248 err = get_callchain_buffers();
6249 if (err) {
6250 free_event(event);
6251 return ERR_PTR(err);
6252 }
6253 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006254 }
6255
6256 return event;
6257}
6258
6259static int perf_copy_attr(struct perf_event_attr __user *uattr,
6260 struct perf_event_attr *attr)
6261{
6262 u32 size;
6263 int ret;
6264
6265 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
6266 return -EFAULT;
6267
6268 /*
6269 * zero the full structure, so that a short copy will be nice.
6270 */
6271 memset(attr, 0, sizeof(*attr));
6272
6273 ret = get_user(size, &uattr->size);
6274 if (ret)
6275 return ret;
6276
6277 if (size > PAGE_SIZE) /* silly large */
6278 goto err_size;
6279
6280 if (!size) /* abi compat */
6281 size = PERF_ATTR_SIZE_VER0;
6282
6283 if (size < PERF_ATTR_SIZE_VER0)
6284 goto err_size;
6285
6286 /*
6287 * If we're handed a bigger struct than we know of,
6288 * ensure all the unknown bits are 0 - i.e. new
6289 * user-space does not rely on any kernel feature
6290 * extensions we dont know about yet.
6291 */
6292 if (size > sizeof(*attr)) {
6293 unsigned char __user *addr;
6294 unsigned char __user *end;
6295 unsigned char val;
6296
6297 addr = (void __user *)uattr + sizeof(*attr);
6298 end = (void __user *)uattr + size;
6299
6300 for (; addr < end; addr++) {
6301 ret = get_user(val, addr);
6302 if (ret)
6303 return ret;
6304 if (val)
6305 goto err_size;
6306 }
6307 size = sizeof(*attr);
6308 }
6309
6310 ret = copy_from_user(attr, uattr, size);
6311 if (ret)
6312 return -EFAULT;
6313
6314 /*
6315 * If the type exists, the corresponding creation will verify
6316 * the attr->config.
6317 */
6318 if (attr->type >= PERF_TYPE_MAX)
6319 return -EINVAL;
6320
Mahesh Salgaonkarcd757642010-01-30 10:25:18 +05306321 if (attr->__reserved_1)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006322 return -EINVAL;
6323
6324 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
6325 return -EINVAL;
6326
6327 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
6328 return -EINVAL;
6329
6330out:
6331 return ret;
6332
6333err_size:
6334 put_user(sizeof(*attr), &uattr->size);
6335 ret = -E2BIG;
6336 goto out;
6337}
6338
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006339static int
6340perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006341{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02006342 struct perf_buffer *buffer = NULL, *old_buffer = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006343 int ret = -EINVAL;
6344
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006345 if (!output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006346 goto set;
6347
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006348 /* don't allow circular references */
6349 if (event == output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006350 goto out;
6351
Peter Zijlstra0f139302010-05-20 14:35:15 +02006352 /*
6353 * Don't allow cross-cpu buffers
6354 */
6355 if (output_event->cpu != event->cpu)
6356 goto out;
6357
6358 /*
6359 * If its not a per-cpu buffer, it must be the same task.
6360 */
6361 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
6362 goto out;
6363
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006364set:
6365 mutex_lock(&event->mmap_mutex);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006366 /* Can't redirect output if we've got an active mmap() */
6367 if (atomic_read(&event->mmap_count))
6368 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006369
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006370 if (output_event) {
6371 /* get the buffer we want to redirect to */
Peter Zijlstraca5135e2010-05-28 19:33:23 +02006372 buffer = perf_buffer_get(output_event);
6373 if (!buffer)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006374 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006375 }
6376
Peter Zijlstraca5135e2010-05-28 19:33:23 +02006377 old_buffer = event->buffer;
6378 rcu_assign_pointer(event->buffer, buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006379 ret = 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006380unlock:
6381 mutex_unlock(&event->mmap_mutex);
6382
Peter Zijlstraca5135e2010-05-28 19:33:23 +02006383 if (old_buffer)
6384 perf_buffer_put(old_buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006385out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006386 return ret;
6387}
6388
6389/**
6390 * sys_perf_event_open - open a performance event, associate it to a task/cpu
6391 *
6392 * @attr_uptr: event_id type attributes for monitoring/sampling
6393 * @pid: target pid
6394 * @cpu: target cpu
6395 * @group_fd: group leader event fd
6396 */
6397SYSCALL_DEFINE5(perf_event_open,
6398 struct perf_event_attr __user *, attr_uptr,
6399 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
6400{
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006401 struct perf_event *group_leader = NULL, *output_event = NULL;
6402 struct perf_event *event, *sibling;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006403 struct perf_event_attr attr;
6404 struct perf_event_context *ctx;
6405 struct file *event_file = NULL;
6406 struct file *group_file = NULL;
Matt Helsley38a81da2010-09-13 13:01:20 -07006407 struct task_struct *task = NULL;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006408 struct pmu *pmu;
Al Viroea635c62010-05-26 17:40:29 -04006409 int event_fd;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006410 int move_group = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006411 int fput_needed = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006412 int err;
6413
6414 /* for future expandability... */
Stephane Eraniane5d13672011-02-14 11:20:01 +02006415 if (flags & ~PERF_FLAG_ALL)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006416 return -EINVAL;
6417
6418 err = perf_copy_attr(attr_uptr, &attr);
6419 if (err)
6420 return err;
6421
6422 if (!attr.exclude_kernel) {
6423 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
6424 return -EACCES;
6425 }
6426
6427 if (attr.freq) {
6428 if (attr.sample_freq > sysctl_perf_event_sample_rate)
6429 return -EINVAL;
6430 }
6431
Stephane Eraniane5d13672011-02-14 11:20:01 +02006432 /*
6433 * In cgroup mode, the pid argument is used to pass the fd
6434 * opened to the cgroup directory in cgroupfs. The cpu argument
6435 * designates the cpu on which to monitor threads from that
6436 * cgroup.
6437 */
6438 if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1))
6439 return -EINVAL;
6440
Al Viroea635c62010-05-26 17:40:29 -04006441 event_fd = get_unused_fd_flags(O_RDWR);
6442 if (event_fd < 0)
6443 return event_fd;
6444
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006445 if (group_fd != -1) {
6446 group_leader = perf_fget_light(group_fd, &fput_needed);
6447 if (IS_ERR(group_leader)) {
6448 err = PTR_ERR(group_leader);
Stephane Eraniand14b12d2010-09-17 11:28:47 +02006449 goto err_fd;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006450 }
6451 group_file = group_leader->filp;
6452 if (flags & PERF_FLAG_FD_OUTPUT)
6453 output_event = group_leader;
6454 if (flags & PERF_FLAG_FD_NO_GROUP)
6455 group_leader = NULL;
6456 }
6457
Stephane Eraniane5d13672011-02-14 11:20:01 +02006458 if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) {
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02006459 task = find_lively_task_by_vpid(pid);
6460 if (IS_ERR(task)) {
6461 err = PTR_ERR(task);
6462 goto err_group_fd;
6463 }
6464 }
6465
Peter Zijlstrad580ff82010-10-14 17:43:23 +02006466 event = perf_event_alloc(&attr, cpu, task, group_leader, NULL, NULL);
Stephane Eraniand14b12d2010-09-17 11:28:47 +02006467 if (IS_ERR(event)) {
6468 err = PTR_ERR(event);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02006469 goto err_task;
Stephane Eraniand14b12d2010-09-17 11:28:47 +02006470 }
6471
Stephane Eraniane5d13672011-02-14 11:20:01 +02006472 if (flags & PERF_FLAG_PID_CGROUP) {
6473 err = perf_cgroup_connect(pid, event, &attr, group_leader);
6474 if (err)
6475 goto err_alloc;
Peter Zijlstra08309372011-03-03 11:31:20 +01006476 /*
6477 * one more event:
6478 * - that has cgroup constraint on event->cpu
6479 * - that may need work on context switch
6480 */
6481 atomic_inc(&per_cpu(perf_cgroup_events, event->cpu));
6482 jump_label_inc(&perf_sched_events);
Stephane Eraniane5d13672011-02-14 11:20:01 +02006483 }
6484
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006485 /*
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006486 * Special case software events and allow them to be part of
6487 * any hardware group.
6488 */
6489 pmu = event->pmu;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006490
6491 if (group_leader &&
6492 (is_software_event(event) != is_software_event(group_leader))) {
6493 if (is_software_event(event)) {
6494 /*
6495 * If event and group_leader are not both a software
6496 * event, and event is, then group leader is not.
6497 *
6498 * Allow the addition of software events to !software
6499 * groups, this is safe because software events never
6500 * fail to schedule.
6501 */
6502 pmu = group_leader->pmu;
6503 } else if (is_software_event(group_leader) &&
6504 (group_leader->group_flags & PERF_GROUP_SOFTWARE)) {
6505 /*
6506 * In case the group is a pure software group, and we
6507 * try to add a hardware event, move the whole group to
6508 * the hardware context.
6509 */
6510 move_group = 1;
6511 }
6512 }
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006513
6514 /*
6515 * Get the target context (task or percpu):
6516 */
Matt Helsley38a81da2010-09-13 13:01:20 -07006517 ctx = find_get_context(pmu, task, cpu);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006518 if (IS_ERR(ctx)) {
6519 err = PTR_ERR(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02006520 goto err_alloc;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006521 }
6522
Peter Zijlstrafd1edb32011-03-28 13:13:56 +02006523 if (task) {
6524 put_task_struct(task);
6525 task = NULL;
6526 }
6527
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006528 /*
6529 * Look up the group leader (we will attach this event to it):
6530 */
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006531 if (group_leader) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006532 err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006533
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006534 /*
6535 * Do not allow a recursive hierarchy (this new sibling
6536 * becoming part of another group-sibling):
6537 */
6538 if (group_leader->group_leader != group_leader)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006539 goto err_context;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006540 /*
6541 * Do not allow to attach to a group in a different
6542 * task or CPU context:
6543 */
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006544 if (move_group) {
6545 if (group_leader->ctx->type != ctx->type)
6546 goto err_context;
6547 } else {
6548 if (group_leader->ctx != ctx)
6549 goto err_context;
6550 }
6551
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006552 /*
6553 * Only a group leader can be exclusive or pinned
6554 */
6555 if (attr.exclusive || attr.pinned)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006556 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006557 }
6558
6559 if (output_event) {
6560 err = perf_event_set_output(event, output_event);
6561 if (err)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006562 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006563 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006564
Al Viroea635c62010-05-26 17:40:29 -04006565 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, O_RDWR);
6566 if (IS_ERR(event_file)) {
6567 err = PTR_ERR(event_file);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006568 goto err_context;
Al Viroea635c62010-05-26 17:40:29 -04006569 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006570
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006571 if (move_group) {
6572 struct perf_event_context *gctx = group_leader->ctx;
6573
6574 mutex_lock(&gctx->mutex);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01006575 perf_remove_from_context(group_leader);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006576 list_for_each_entry(sibling, &group_leader->sibling_list,
6577 group_entry) {
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01006578 perf_remove_from_context(sibling);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006579 put_ctx(gctx);
6580 }
6581 mutex_unlock(&gctx->mutex);
6582 put_ctx(gctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006583 }
6584
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006585 event->filp = event_file;
6586 WARN_ON_ONCE(ctx->parent_ctx);
6587 mutex_lock(&ctx->mutex);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006588
6589 if (move_group) {
6590 perf_install_in_context(ctx, group_leader, cpu);
6591 get_ctx(ctx);
6592 list_for_each_entry(sibling, &group_leader->sibling_list,
6593 group_entry) {
6594 perf_install_in_context(ctx, sibling, cpu);
6595 get_ctx(ctx);
6596 }
6597 }
6598
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006599 perf_install_in_context(ctx, event, cpu);
6600 ++ctx->generation;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01006601 perf_unpin_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006602 mutex_unlock(&ctx->mutex);
6603
6604 event->owner = current;
Peter Zijlstra88821352010-11-09 19:01:43 +01006605
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006606 mutex_lock(&current->perf_event_mutex);
6607 list_add_tail(&event->owner_entry, &current->perf_event_list);
6608 mutex_unlock(&current->perf_event_mutex);
6609
Peter Zijlstra8a495422010-05-27 15:47:49 +02006610 /*
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02006611 * Precalculate sample_data sizes
6612 */
6613 perf_event__header_size(event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02006614 perf_event__id_header_size(event);
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02006615
6616 /*
Peter Zijlstra8a495422010-05-27 15:47:49 +02006617 * Drop the reference on the group_event after placing the
6618 * new event on the sibling_list. This ensures destruction
6619 * of the group leader will find the pointer to itself in
6620 * perf_group_detach().
6621 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006622 fput_light(group_file, fput_needed);
Al Viroea635c62010-05-26 17:40:29 -04006623 fd_install(event_fd, event_file);
6624 return event_fd;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006625
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006626err_context:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01006627 perf_unpin_context(ctx);
Al Viroea635c62010-05-26 17:40:29 -04006628 put_ctx(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02006629err_alloc:
6630 free_event(event);
Peter Zijlstrae7d0bc02010-10-14 16:54:51 +02006631err_task:
6632 if (task)
6633 put_task_struct(task);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006634err_group_fd:
6635 fput_light(group_file, fput_needed);
Al Viroea635c62010-05-26 17:40:29 -04006636err_fd:
6637 put_unused_fd(event_fd);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006638 return err;
6639}
6640
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006641/**
6642 * perf_event_create_kernel_counter
6643 *
6644 * @attr: attributes of the counter to create
6645 * @cpu: cpu in which the counter is bound
Matt Helsley38a81da2010-09-13 13:01:20 -07006646 * @task: task to profile (NULL for percpu)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006647 */
6648struct perf_event *
6649perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
Matt Helsley38a81da2010-09-13 13:01:20 -07006650 struct task_struct *task,
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01006651 perf_overflow_handler_t overflow_handler)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006652{
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006653 struct perf_event_context *ctx;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006654 struct perf_event *event;
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006655 int err;
6656
6657 /*
6658 * Get the target context (task or percpu):
6659 */
6660
Peter Zijlstrad580ff82010-10-14 17:43:23 +02006661 event = perf_event_alloc(attr, cpu, task, NULL, NULL, overflow_handler);
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01006662 if (IS_ERR(event)) {
6663 err = PTR_ERR(event);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006664 goto err;
6665 }
6666
Matt Helsley38a81da2010-09-13 13:01:20 -07006667 ctx = find_get_context(event->pmu, task, cpu);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006668 if (IS_ERR(ctx)) {
6669 err = PTR_ERR(ctx);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006670 goto err_free;
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01006671 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006672
6673 event->filp = NULL;
6674 WARN_ON_ONCE(ctx->parent_ctx);
6675 mutex_lock(&ctx->mutex);
6676 perf_install_in_context(ctx, event, cpu);
6677 ++ctx->generation;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01006678 perf_unpin_context(ctx);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006679 mutex_unlock(&ctx->mutex);
6680
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006681 return event;
6682
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006683err_free:
6684 free_event(event);
6685err:
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01006686 return ERR_PTR(err);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006687}
6688EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
6689
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006690static void sync_child_event(struct perf_event *child_event,
6691 struct task_struct *child)
6692{
6693 struct perf_event *parent_event = child_event->parent;
6694 u64 child_val;
6695
6696 if (child_event->attr.inherit_stat)
6697 perf_event_read_event(child_event, child);
6698
Peter Zijlstrab5e58792010-05-21 14:43:12 +02006699 child_val = perf_event_count(child_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006700
6701 /*
6702 * Add back the child's count to the parent's count:
6703 */
Peter Zijlstraa6e6dea2010-05-21 14:27:58 +02006704 atomic64_add(child_val, &parent_event->child_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006705 atomic64_add(child_event->total_time_enabled,
6706 &parent_event->child_total_time_enabled);
6707 atomic64_add(child_event->total_time_running,
6708 &parent_event->child_total_time_running);
6709
6710 /*
6711 * Remove this event from the parent's list
6712 */
6713 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
6714 mutex_lock(&parent_event->child_mutex);
6715 list_del_init(&child_event->child_list);
6716 mutex_unlock(&parent_event->child_mutex);
6717
6718 /*
6719 * Release the parent event, if this was the last
6720 * reference to it.
6721 */
6722 fput(parent_event->filp);
6723}
6724
6725static void
6726__perf_event_exit_task(struct perf_event *child_event,
6727 struct perf_event_context *child_ctx,
6728 struct task_struct *child)
6729{
Peter Zijlstra38b435b2011-03-15 14:37:10 +01006730 if (child_event->parent) {
6731 raw_spin_lock_irq(&child_ctx->lock);
6732 perf_group_detach(child_event);
6733 raw_spin_unlock_irq(&child_ctx->lock);
6734 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006735
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01006736 perf_remove_from_context(child_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006737
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006738 /*
Peter Zijlstra38b435b2011-03-15 14:37:10 +01006739 * It can happen that the parent exits first, and has events
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006740 * that are still around due to the child reference. These
Peter Zijlstra38b435b2011-03-15 14:37:10 +01006741 * events need to be zapped.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006742 */
Peter Zijlstra38b435b2011-03-15 14:37:10 +01006743 if (child_event->parent) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006744 sync_child_event(child_event, child);
6745 free_event(child_event);
6746 }
6747}
6748
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02006749static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006750{
6751 struct perf_event *child_event, *tmp;
6752 struct perf_event_context *child_ctx;
6753 unsigned long flags;
6754
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02006755 if (likely(!child->perf_event_ctxp[ctxn])) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006756 perf_event_task(child, NULL, 0);
6757 return;
6758 }
6759
6760 local_irq_save(flags);
6761 /*
6762 * We can't reschedule here because interrupts are disabled,
6763 * and either child is current or it is a task that can't be
6764 * scheduled, so we are now safe from rescheduling changing
6765 * our context.
6766 */
Oleg Nesterov806839b2011-01-21 18:45:47 +01006767 child_ctx = rcu_dereference_raw(child->perf_event_ctxp[ctxn]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006768
6769 /*
6770 * Take the context lock here so that if find_get_context is
6771 * reading child->perf_event_ctxp, we wait until it has
6772 * incremented the context's refcount before we do put_ctx below.
6773 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +01006774 raw_spin_lock(&child_ctx->lock);
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02006775 task_ctx_sched_out(child_ctx);
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02006776 child->perf_event_ctxp[ctxn] = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006777 /*
6778 * If this context is a clone; unclone it so it can't get
6779 * swapped to another process while we're removing all
6780 * the events from it.
6781 */
6782 unclone_ctx(child_ctx);
Peter Zijlstra5e942bb2009-11-23 11:37:26 +01006783 update_context_time(child_ctx);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01006784 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006785
6786 /*
6787 * Report the task dead after unscheduling the events so that we
6788 * won't get any samples after PERF_RECORD_EXIT. We can however still
6789 * get a few PERF_RECORD_READ events.
6790 */
6791 perf_event_task(child, child_ctx, 0);
6792
6793 /*
6794 * We can recurse on the same lock type through:
6795 *
6796 * __perf_event_exit_task()
6797 * sync_child_event()
6798 * fput(parent_event->filp)
6799 * perf_release()
6800 * mutex_lock(&ctx->mutex)
6801 *
6802 * But since its the parent context it won't be the same instance.
6803 */
Peter Zijlstraa0507c82010-05-06 15:42:53 +02006804 mutex_lock(&child_ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006805
6806again:
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006807 list_for_each_entry_safe(child_event, tmp, &child_ctx->pinned_groups,
6808 group_entry)
6809 __perf_event_exit_task(child_event, child_ctx, child);
6810
6811 list_for_each_entry_safe(child_event, tmp, &child_ctx->flexible_groups,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006812 group_entry)
6813 __perf_event_exit_task(child_event, child_ctx, child);
6814
6815 /*
6816 * If the last event was a group event, it will have appended all
6817 * its siblings to the list, but we obtained 'tmp' before that which
6818 * will still point to the list head terminating the iteration.
6819 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006820 if (!list_empty(&child_ctx->pinned_groups) ||
6821 !list_empty(&child_ctx->flexible_groups))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006822 goto again;
6823
6824 mutex_unlock(&child_ctx->mutex);
6825
6826 put_ctx(child_ctx);
6827}
6828
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02006829/*
6830 * When a child task exits, feed back event values to parent events.
6831 */
6832void perf_event_exit_task(struct task_struct *child)
6833{
Peter Zijlstra88821352010-11-09 19:01:43 +01006834 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02006835 int ctxn;
6836
Peter Zijlstra88821352010-11-09 19:01:43 +01006837 mutex_lock(&child->perf_event_mutex);
6838 list_for_each_entry_safe(event, tmp, &child->perf_event_list,
6839 owner_entry) {
6840 list_del_init(&event->owner_entry);
6841
6842 /*
6843 * Ensure the list deletion is visible before we clear
6844 * the owner, closes a race against perf_release() where
6845 * we need to serialize on the owner->perf_event_mutex.
6846 */
6847 smp_wmb();
6848 event->owner = NULL;
6849 }
6850 mutex_unlock(&child->perf_event_mutex);
6851
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02006852 for_each_task_context_nr(ctxn)
6853 perf_event_exit_task_context(child, ctxn);
6854}
6855
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006856static void perf_free_event(struct perf_event *event,
6857 struct perf_event_context *ctx)
6858{
6859 struct perf_event *parent = event->parent;
6860
6861 if (WARN_ON_ONCE(!parent))
6862 return;
6863
6864 mutex_lock(&parent->child_mutex);
6865 list_del_init(&event->child_list);
6866 mutex_unlock(&parent->child_mutex);
6867
6868 fput(parent->filp);
6869
Peter Zijlstra8a495422010-05-27 15:47:49 +02006870 perf_group_detach(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006871 list_del_event(event, ctx);
6872 free_event(event);
6873}
6874
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006875/*
6876 * free an unexposed, unused context as created by inheritance by
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02006877 * perf_event_init_task below, used by fork() in case of fail.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006878 */
6879void perf_event_free_task(struct task_struct *task)
6880{
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02006881 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006882 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02006883 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006884
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02006885 for_each_task_context_nr(ctxn) {
6886 ctx = task->perf_event_ctxp[ctxn];
6887 if (!ctx)
6888 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006889
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02006890 mutex_lock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006891again:
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02006892 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
6893 group_entry)
6894 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006895
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02006896 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
6897 group_entry)
6898 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006899
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02006900 if (!list_empty(&ctx->pinned_groups) ||
6901 !list_empty(&ctx->flexible_groups))
6902 goto again;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006903
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02006904 mutex_unlock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006905
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02006906 put_ctx(ctx);
6907 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006908}
6909
Peter Zijlstra4e231c72010-09-09 21:01:59 +02006910void perf_event_delayed_put(struct task_struct *task)
6911{
6912 int ctxn;
6913
6914 for_each_task_context_nr(ctxn)
6915 WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
6916}
6917
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02006918/*
6919 * inherit a event from parent task to child task:
6920 */
6921static struct perf_event *
6922inherit_event(struct perf_event *parent_event,
6923 struct task_struct *parent,
6924 struct perf_event_context *parent_ctx,
6925 struct task_struct *child,
6926 struct perf_event *group_leader,
6927 struct perf_event_context *child_ctx)
6928{
6929 struct perf_event *child_event;
Peter Zijlstracee010e2010-09-10 12:51:54 +02006930 unsigned long flags;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02006931
6932 /*
6933 * Instead of creating recursive hierarchies of events,
6934 * we link inherited events back to the original parent,
6935 * which has a filp for sure, which we use as the reference
6936 * count:
6937 */
6938 if (parent_event->parent)
6939 parent_event = parent_event->parent;
6940
6941 child_event = perf_event_alloc(&parent_event->attr,
6942 parent_event->cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +02006943 child,
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02006944 group_leader, parent_event,
6945 NULL);
6946 if (IS_ERR(child_event))
6947 return child_event;
6948 get_ctx(child_ctx);
6949
6950 /*
6951 * Make the child state follow the state of the parent event,
6952 * not its attr.disabled bit. We hold the parent's mutex,
6953 * so we won't race with perf_event_{en, dis}able_family.
6954 */
6955 if (parent_event->state >= PERF_EVENT_STATE_INACTIVE)
6956 child_event->state = PERF_EVENT_STATE_INACTIVE;
6957 else
6958 child_event->state = PERF_EVENT_STATE_OFF;
6959
6960 if (parent_event->attr.freq) {
6961 u64 sample_period = parent_event->hw.sample_period;
6962 struct hw_perf_event *hwc = &child_event->hw;
6963
6964 hwc->sample_period = sample_period;
6965 hwc->last_period = sample_period;
6966
6967 local64_set(&hwc->period_left, sample_period);
6968 }
6969
6970 child_event->ctx = child_ctx;
6971 child_event->overflow_handler = parent_event->overflow_handler;
6972
6973 /*
Thomas Gleixner614b6782010-12-03 16:24:32 -02006974 * Precalculate sample_data sizes
6975 */
6976 perf_event__header_size(child_event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02006977 perf_event__id_header_size(child_event);
Thomas Gleixner614b6782010-12-03 16:24:32 -02006978
6979 /*
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02006980 * Link it up in the child's context:
6981 */
Peter Zijlstracee010e2010-09-10 12:51:54 +02006982 raw_spin_lock_irqsave(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02006983 add_event_to_ctx(child_event, child_ctx);
Peter Zijlstracee010e2010-09-10 12:51:54 +02006984 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02006985
6986 /*
6987 * Get a reference to the parent filp - we will fput it
6988 * when the child event exits. This is safe to do because
6989 * we are in the parent and we know that the filp still
6990 * exists and has a nonzero count:
6991 */
6992 atomic_long_inc(&parent_event->filp->f_count);
6993
6994 /*
6995 * Link this into the parent event's child list
6996 */
6997 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
6998 mutex_lock(&parent_event->child_mutex);
6999 list_add_tail(&child_event->child_list, &parent_event->child_list);
7000 mutex_unlock(&parent_event->child_mutex);
7001
7002 return child_event;
7003}
7004
7005static int inherit_group(struct perf_event *parent_event,
7006 struct task_struct *parent,
7007 struct perf_event_context *parent_ctx,
7008 struct task_struct *child,
7009 struct perf_event_context *child_ctx)
7010{
7011 struct perf_event *leader;
7012 struct perf_event *sub;
7013 struct perf_event *child_ctr;
7014
7015 leader = inherit_event(parent_event, parent, parent_ctx,
7016 child, NULL, child_ctx);
7017 if (IS_ERR(leader))
7018 return PTR_ERR(leader);
7019 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
7020 child_ctr = inherit_event(sub, parent, parent_ctx,
7021 child, leader, child_ctx);
7022 if (IS_ERR(child_ctr))
7023 return PTR_ERR(child_ctr);
7024 }
7025 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007026}
7027
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007028static int
7029inherit_task_group(struct perf_event *event, struct task_struct *parent,
7030 struct perf_event_context *parent_ctx,
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02007031 struct task_struct *child, int ctxn,
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007032 int *inherited_all)
7033{
7034 int ret;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02007035 struct perf_event_context *child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007036
7037 if (!event->attr.inherit) {
7038 *inherited_all = 0;
7039 return 0;
7040 }
7041
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007042 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007043 if (!child_ctx) {
7044 /*
7045 * This is executed from the parent task context, so
7046 * inherit events that have been marked for cloning.
7047 * First allocate and initialize a context for the
7048 * child.
7049 */
7050
Peter Zijlstraeb184472010-09-07 15:55:13 +02007051 child_ctx = alloc_perf_context(event->pmu, child);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007052 if (!child_ctx)
7053 return -ENOMEM;
7054
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02007055 child->perf_event_ctxp[ctxn] = child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007056 }
7057
7058 ret = inherit_group(event, parent, parent_ctx,
7059 child, child_ctx);
7060
7061 if (ret)
7062 *inherited_all = 0;
7063
7064 return ret;
7065}
7066
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007067/*
7068 * Initialize the perf_event context in task_struct
7069 */
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02007070int perf_event_init_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007071{
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007072 struct perf_event_context *child_ctx, *parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007073 struct perf_event_context *cloned_ctx;
7074 struct perf_event *event;
7075 struct task_struct *parent = current;
7076 int inherited_all = 1;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01007077 unsigned long flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007078 int ret = 0;
7079
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02007080 if (likely(!parent->perf_event_ctxp[ctxn]))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007081 return 0;
7082
7083 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007084 * If the parent's context is a clone, pin it so it won't get
7085 * swapped under us.
7086 */
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02007087 parent_ctx = perf_pin_task_context(parent, ctxn);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007088
7089 /*
7090 * No need to check if parent_ctx != NULL here; since we saw
7091 * it non-NULL earlier, the only reason for it to become NULL
7092 * is if we exit, and since we're currently in the middle of
7093 * a fork we can't be exiting at the same time.
7094 */
7095
7096 /*
7097 * Lock the parent list. No need to lock the child - not PID
7098 * hashed yet and not running, so nobody can access it.
7099 */
7100 mutex_lock(&parent_ctx->mutex);
7101
7102 /*
7103 * We dont have to disable NMIs - we are only looking at
7104 * the list, not manipulating it:
7105 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007106 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02007107 ret = inherit_task_group(event, parent, parent_ctx,
7108 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007109 if (ret)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007110 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007111 }
7112
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01007113 /*
7114 * We can't hold ctx->lock when iterating the ->flexible_group list due
7115 * to allocations, but we need to prevent rotation because
7116 * rotate_ctx() will change the list from interrupt context.
7117 */
7118 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
7119 parent_ctx->rotate_disable = 1;
7120 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
7121
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007122 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02007123 ret = inherit_task_group(event, parent, parent_ctx,
7124 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007125 if (ret)
7126 break;
7127 }
7128
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01007129 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
7130 parent_ctx->rotate_disable = 0;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01007131
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02007132 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007133
Peter Zijlstra05cbaa22009-12-30 16:00:35 +01007134 if (child_ctx && inherited_all) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007135 /*
7136 * Mark the child context as a clone of the parent
7137 * context, or of whatever the parent is a clone of.
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01007138 *
7139 * Note that if the parent is a clone, the holding of
7140 * parent_ctx->lock avoids it from being uncloned.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007141 */
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01007142 cloned_ctx = parent_ctx->parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007143 if (cloned_ctx) {
7144 child_ctx->parent_ctx = cloned_ctx;
7145 child_ctx->parent_gen = parent_ctx->parent_gen;
7146 } else {
7147 child_ctx->parent_ctx = parent_ctx;
7148 child_ctx->parent_gen = parent_ctx->generation;
7149 }
7150 get_ctx(child_ctx->parent_ctx);
7151 }
7152
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01007153 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007154 mutex_unlock(&parent_ctx->mutex);
7155
7156 perf_unpin_context(parent_ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007157 put_ctx(parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007158
7159 return ret;
7160}
7161
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02007162/*
7163 * Initialize the perf_event context in task_struct
7164 */
7165int perf_event_init_task(struct task_struct *child)
7166{
7167 int ctxn, ret;
7168
Oleg Nesterov8550d7c2011-01-19 19:22:28 +01007169 memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp));
7170 mutex_init(&child->perf_event_mutex);
7171 INIT_LIST_HEAD(&child->perf_event_list);
7172
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02007173 for_each_task_context_nr(ctxn) {
7174 ret = perf_event_init_context(child, ctxn);
7175 if (ret)
7176 return ret;
7177 }
7178
7179 return 0;
7180}
7181
Paul Mackerras220b1402010-03-10 20:45:52 +11007182static void __init perf_event_init_all_cpus(void)
7183{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007184 struct swevent_htable *swhash;
Paul Mackerras220b1402010-03-10 20:45:52 +11007185 int cpu;
Paul Mackerras220b1402010-03-10 20:45:52 +11007186
7187 for_each_possible_cpu(cpu) {
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007188 swhash = &per_cpu(swevent_htable, cpu);
7189 mutex_init(&swhash->hlist_mutex);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02007190 INIT_LIST_HEAD(&per_cpu(rotation_list, cpu));
Paul Mackerras220b1402010-03-10 20:45:52 +11007191 }
7192}
7193
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007194static void __cpuinit perf_event_init_cpu(int cpu)
7195{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007196 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007197
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007198 mutex_lock(&swhash->hlist_mutex);
7199 if (swhash->hlist_refcount > 0) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007200 struct swevent_hlist *hlist;
7201
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007202 hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
7203 WARN_ON(!hlist);
7204 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007205 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007206 mutex_unlock(&swhash->hlist_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007207}
7208
Peter Zijlstrac2774432010-12-08 15:29:02 +01007209#if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02007210static void perf_pmu_rotate_stop(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007211{
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02007212 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
7213
7214 WARN_ON(!irqs_disabled());
7215
7216 list_del_init(&cpuctx->rotation_list);
7217}
7218
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007219static void __perf_event_exit_context(void *__info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007220{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007221 struct perf_event_context *ctx = __info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007222 struct perf_event *event, *tmp;
7223
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007224 perf_pmu_rotate_stop(ctx->pmu);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02007225
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007226 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007227 __perf_remove_from_context(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007228 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, group_entry)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007229 __perf_remove_from_context(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007230}
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007231
7232static void perf_event_exit_cpu_context(int cpu)
7233{
7234 struct perf_event_context *ctx;
7235 struct pmu *pmu;
7236 int idx;
7237
7238 idx = srcu_read_lock(&pmus_srcu);
7239 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra917bdd12010-09-17 11:28:49 +02007240 ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007241
7242 mutex_lock(&ctx->mutex);
7243 smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
7244 mutex_unlock(&ctx->mutex);
7245 }
7246 srcu_read_unlock(&pmus_srcu, idx);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007247}
7248
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007249static void perf_event_exit_cpu(int cpu)
7250{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007251 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007252
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007253 mutex_lock(&swhash->hlist_mutex);
7254 swevent_hlist_release(swhash);
7255 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007256
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007257 perf_event_exit_cpu_context(cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007258}
7259#else
7260static inline void perf_event_exit_cpu(int cpu) { }
7261#endif
7262
Peter Zijlstrac2774432010-12-08 15:29:02 +01007263static int
7264perf_reboot(struct notifier_block *notifier, unsigned long val, void *v)
7265{
7266 int cpu;
7267
7268 for_each_online_cpu(cpu)
7269 perf_event_exit_cpu(cpu);
7270
7271 return NOTIFY_OK;
7272}
7273
7274/*
7275 * Run the perf reboot notifier at the very last possible moment so that
7276 * the generic watchdog code runs as long as possible.
7277 */
7278static struct notifier_block perf_reboot_notifier = {
7279 .notifier_call = perf_reboot,
7280 .priority = INT_MIN,
7281};
7282
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007283static int __cpuinit
7284perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
7285{
7286 unsigned int cpu = (long)hcpu;
7287
Peter Zijlstra5e116372010-06-11 13:35:08 +02007288 switch (action & ~CPU_TASKS_FROZEN) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007289
7290 case CPU_UP_PREPARE:
Peter Zijlstra5e116372010-06-11 13:35:08 +02007291 case CPU_DOWN_FAILED:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007292 perf_event_init_cpu(cpu);
7293 break;
7294
Peter Zijlstra5e116372010-06-11 13:35:08 +02007295 case CPU_UP_CANCELED:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007296 case CPU_DOWN_PREPARE:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007297 perf_event_exit_cpu(cpu);
7298 break;
7299
7300 default:
7301 break;
7302 }
7303
7304 return NOTIFY_OK;
7305}
7306
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007307void __init perf_event_init(void)
7308{
Jason Wessel3c502e72010-11-04 17:33:01 -05007309 int ret;
7310
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007311 idr_init(&pmu_idr);
7312
Paul Mackerras220b1402010-03-10 20:45:52 +11007313 perf_event_init_all_cpus();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007314 init_srcu_struct(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007315 perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE);
7316 perf_pmu_register(&perf_cpu_clock, NULL, -1);
7317 perf_pmu_register(&perf_task_clock, NULL, -1);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007318 perf_tp_register();
7319 perf_cpu_notifier(perf_cpu_notify);
Peter Zijlstrac2774432010-12-08 15:29:02 +01007320 register_reboot_notifier(&perf_reboot_notifier);
Jason Wessel3c502e72010-11-04 17:33:01 -05007321
7322 ret = init_hw_breakpoint();
7323 WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007324}
Peter Zijlstraabe43402010-11-17 23:17:37 +01007325
7326static int __init perf_event_sysfs_init(void)
7327{
7328 struct pmu *pmu;
7329 int ret;
7330
7331 mutex_lock(&pmus_lock);
7332
7333 ret = bus_register(&pmu_bus);
7334 if (ret)
7335 goto unlock;
7336
7337 list_for_each_entry(pmu, &pmus, entry) {
7338 if (!pmu->name || pmu->type < 0)
7339 continue;
7340
7341 ret = pmu_dev_alloc(pmu);
7342 WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret);
7343 }
7344 pmu_bus_running = 1;
7345 ret = 0;
7346
7347unlock:
7348 mutex_unlock(&pmus_lock);
7349
7350 return ret;
7351}
7352device_initcall(perf_event_sysfs_init);
Stephane Eraniane5d13672011-02-14 11:20:01 +02007353
7354#ifdef CONFIG_CGROUP_PERF
7355static struct cgroup_subsys_state *perf_cgroup_create(
7356 struct cgroup_subsys *ss, struct cgroup *cont)
7357{
7358 struct perf_cgroup *jc;
Stephane Eraniane5d13672011-02-14 11:20:01 +02007359
Li Zefan1b15d052011-03-03 14:26:06 +08007360 jc = kzalloc(sizeof(*jc), GFP_KERNEL);
Stephane Eraniane5d13672011-02-14 11:20:01 +02007361 if (!jc)
7362 return ERR_PTR(-ENOMEM);
7363
Stephane Eraniane5d13672011-02-14 11:20:01 +02007364 jc->info = alloc_percpu(struct perf_cgroup_info);
7365 if (!jc->info) {
7366 kfree(jc);
7367 return ERR_PTR(-ENOMEM);
7368 }
7369
Stephane Eraniane5d13672011-02-14 11:20:01 +02007370 return &jc->css;
7371}
7372
7373static void perf_cgroup_destroy(struct cgroup_subsys *ss,
7374 struct cgroup *cont)
7375{
7376 struct perf_cgroup *jc;
7377 jc = container_of(cgroup_subsys_state(cont, perf_subsys_id),
7378 struct perf_cgroup, css);
7379 free_percpu(jc->info);
7380 kfree(jc);
7381}
7382
7383static int __perf_cgroup_move(void *info)
7384{
7385 struct task_struct *task = info;
7386 perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN);
7387 return 0;
7388}
7389
7390static void perf_cgroup_move(struct task_struct *task)
7391{
7392 task_function_call(task, __perf_cgroup_move, task);
7393}
7394
7395static void perf_cgroup_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
7396 struct cgroup *old_cgrp, struct task_struct *task,
7397 bool threadgroup)
7398{
7399 perf_cgroup_move(task);
7400 if (threadgroup) {
7401 struct task_struct *c;
7402 rcu_read_lock();
7403 list_for_each_entry_rcu(c, &task->thread_group, thread_group) {
7404 perf_cgroup_move(c);
7405 }
7406 rcu_read_unlock();
7407 }
7408}
7409
7410static void perf_cgroup_exit(struct cgroup_subsys *ss, struct cgroup *cgrp,
7411 struct cgroup *old_cgrp, struct task_struct *task)
7412{
7413 /*
7414 * cgroup_exit() is called in the copy_process() failure path.
7415 * Ignore this case since the task hasn't ran yet, this avoids
7416 * trying to poke a half freed task state from generic code.
7417 */
7418 if (!(task->flags & PF_EXITING))
7419 return;
7420
7421 perf_cgroup_move(task);
7422}
7423
7424struct cgroup_subsys perf_subsys = {
Ingo Molnare7e7ee22011-05-04 08:42:29 +02007425 .name = "perf_event",
7426 .subsys_id = perf_subsys_id,
7427 .create = perf_cgroup_create,
7428 .destroy = perf_cgroup_destroy,
7429 .exit = perf_cgroup_exit,
7430 .attach = perf_cgroup_attach,
Stephane Eraniane5d13672011-02-14 11:20:01 +02007431};
7432#endif /* CONFIG_CGROUP_PERF */