blob: 81de28dcca8c65399fd8932aa2940b84c0944c47 [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
Frederic Weisbecker76369132011-05-19 19:55:04 +020039#include "internal.h"
40
Ingo Molnarcdd6c482009-09-21 12:02:48 +020041#include <asm/irq_regs.h>
42
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010043struct remote_function_call {
Ingo Molnare7e7ee22011-05-04 08:42:29 +020044 struct task_struct *p;
45 int (*func)(void *info);
46 void *info;
47 int ret;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010048};
49
50static void remote_function(void *data)
51{
52 struct remote_function_call *tfc = data;
53 struct task_struct *p = tfc->p;
54
55 if (p) {
56 tfc->ret = -EAGAIN;
57 if (task_cpu(p) != smp_processor_id() || !task_curr(p))
58 return;
59 }
60
61 tfc->ret = tfc->func(tfc->info);
62}
63
64/**
65 * task_function_call - call a function on the cpu on which a task runs
66 * @p: the task to evaluate
67 * @func: the function to be called
68 * @info: the function call argument
69 *
70 * Calls the function @func when the task is currently running. This might
71 * be on the current CPU, which just calls the function directly
72 *
73 * returns: @func return value, or
74 * -ESRCH - when the process isn't running
75 * -EAGAIN - when the process moved away
76 */
77static int
78task_function_call(struct task_struct *p, int (*func) (void *info), void *info)
79{
80 struct remote_function_call data = {
Ingo Molnare7e7ee22011-05-04 08:42:29 +020081 .p = p,
82 .func = func,
83 .info = info,
84 .ret = -ESRCH, /* No such (running) process */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010085 };
86
87 if (task_curr(p))
88 smp_call_function_single(task_cpu(p), remote_function, &data, 1);
89
90 return data.ret;
91}
92
93/**
94 * cpu_function_call - call a function on the cpu
95 * @func: the function to be called
96 * @info: the function call argument
97 *
98 * Calls the function @func on the remote cpu.
99 *
100 * returns: @func return value or -ENXIO when the cpu is offline
101 */
102static int cpu_function_call(int cpu, int (*func) (void *info), void *info)
103{
104 struct remote_function_call data = {
Ingo Molnare7e7ee22011-05-04 08:42:29 +0200105 .p = NULL,
106 .func = func,
107 .info = info,
108 .ret = -ENXIO, /* No such CPU */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +0100109 };
110
111 smp_call_function_single(cpu, remote_function, &data, 1);
112
113 return data.ret;
114}
115
Stephane Eraniane5d13672011-02-14 11:20:01 +0200116#define PERF_FLAG_ALL (PERF_FLAG_FD_NO_GROUP |\
117 PERF_FLAG_FD_OUTPUT |\
118 PERF_FLAG_PID_CGROUP)
119
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200120enum event_type_t {
121 EVENT_FLEXIBLE = 0x1,
122 EVENT_PINNED = 0x2,
123 EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED,
124};
125
Stephane Eraniane5d13672011-02-14 11:20:01 +0200126/*
127 * perf_sched_events : >0 events exist
128 * perf_cgroup_events: >0 per-cpu cgroup events exist on this cpu
129 */
Jason Barond430d3d2011-03-16 17:29:47 -0400130struct jump_label_key perf_sched_events __read_mostly;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200131static DEFINE_PER_CPU(atomic_t, perf_cgroup_events);
132
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200133static atomic_t nr_mmap_events __read_mostly;
134static atomic_t nr_comm_events __read_mostly;
135static atomic_t nr_task_events __read_mostly;
136
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200137static LIST_HEAD(pmus);
138static DEFINE_MUTEX(pmus_lock);
139static struct srcu_struct pmus_srcu;
140
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200141/*
142 * perf event paranoia level:
143 * -1 - not paranoid at all
144 * 0 - disallow raw tracepoint access for unpriv
145 * 1 - disallow cpu events for unpriv
146 * 2 - disallow kernel profiling for unpriv
147 */
148int sysctl_perf_event_paranoid __read_mostly = 1;
149
Frederic Weisbecker20443382011-03-31 03:33:29 +0200150/* Minimum for 512 kiB + 1 user control page */
151int sysctl_perf_event_mlock __read_mostly = 512 + (PAGE_SIZE / 1024); /* 'free' kiB per user */
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200152
153/*
154 * max perf event sample rate
155 */
Peter Zijlstra163ec432011-02-16 11:22:34 +0100156#define DEFAULT_MAX_SAMPLE_RATE 100000
157int sysctl_perf_event_sample_rate __read_mostly = DEFAULT_MAX_SAMPLE_RATE;
158static int max_samples_per_tick __read_mostly =
159 DIV_ROUND_UP(DEFAULT_MAX_SAMPLE_RATE, HZ);
160
161int perf_proc_update_handler(struct ctl_table *table, int write,
162 void __user *buffer, size_t *lenp,
163 loff_t *ppos)
164{
165 int ret = proc_dointvec(table, write, buffer, lenp, ppos);
166
167 if (ret || !write)
168 return ret;
169
170 max_samples_per_tick = DIV_ROUND_UP(sysctl_perf_event_sample_rate, HZ);
171
172 return 0;
173}
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200174
175static atomic64_t perf_event_id;
176
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200177static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
178 enum event_type_t event_type);
179
180static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +0200181 enum event_type_t event_type,
182 struct task_struct *task);
183
184static void update_context_time(struct perf_event_context *ctx);
185static u64 perf_event_time(struct perf_event *event);
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200186
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200187void __weak perf_event_print_debug(void) { }
188
Matt Fleming84c79912010-10-03 21:41:13 +0100189extern __weak const char *perf_pmu_name(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200190{
Matt Fleming84c79912010-10-03 21:41:13 +0100191 return "pmu";
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200192}
193
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200194static inline u64 perf_clock(void)
195{
196 return local_clock();
197}
198
Stephane Eraniane5d13672011-02-14 11:20:01 +0200199static inline struct perf_cpu_context *
200__get_cpu_context(struct perf_event_context *ctx)
201{
202 return this_cpu_ptr(ctx->pmu->pmu_cpu_context);
203}
204
Peter Zijlstrafacc4302011-04-09 21:17:42 +0200205static void perf_ctx_lock(struct perf_cpu_context *cpuctx,
206 struct perf_event_context *ctx)
207{
208 raw_spin_lock(&cpuctx->ctx.lock);
209 if (ctx)
210 raw_spin_lock(&ctx->lock);
211}
212
213static void perf_ctx_unlock(struct perf_cpu_context *cpuctx,
214 struct perf_event_context *ctx)
215{
216 if (ctx)
217 raw_spin_unlock(&ctx->lock);
218 raw_spin_unlock(&cpuctx->ctx.lock);
219}
220
Stephane Eraniane5d13672011-02-14 11:20:01 +0200221#ifdef CONFIG_CGROUP_PERF
222
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200223/*
224 * Must ensure cgroup is pinned (css_get) before calling
225 * this function. In other words, we cannot call this function
226 * if there is no cgroup event for the current CPU context.
227 */
Stephane Eraniane5d13672011-02-14 11:20:01 +0200228static inline struct perf_cgroup *
229perf_cgroup_from_task(struct task_struct *task)
230{
231 return container_of(task_subsys_state(task, perf_subsys_id),
232 struct perf_cgroup, css);
233}
234
235static inline bool
236perf_cgroup_match(struct perf_event *event)
237{
238 struct perf_event_context *ctx = event->ctx;
239 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
240
241 return !event->cgrp || event->cgrp == cpuctx->cgrp;
242}
243
244static inline void perf_get_cgroup(struct perf_event *event)
245{
246 css_get(&event->cgrp->css);
247}
248
249static inline void perf_put_cgroup(struct perf_event *event)
250{
251 css_put(&event->cgrp->css);
252}
253
254static inline void perf_detach_cgroup(struct perf_event *event)
255{
256 perf_put_cgroup(event);
257 event->cgrp = NULL;
258}
259
260static inline int is_cgroup_event(struct perf_event *event)
261{
262 return event->cgrp != NULL;
263}
264
265static inline u64 perf_cgroup_event_time(struct perf_event *event)
266{
267 struct perf_cgroup_info *t;
268
269 t = per_cpu_ptr(event->cgrp->info, event->cpu);
270 return t->time;
271}
272
273static inline void __update_cgrp_time(struct perf_cgroup *cgrp)
274{
275 struct perf_cgroup_info *info;
276 u64 now;
277
278 now = perf_clock();
279
280 info = this_cpu_ptr(cgrp->info);
281
282 info->time += now - info->timestamp;
283 info->timestamp = now;
284}
285
286static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
287{
288 struct perf_cgroup *cgrp_out = cpuctx->cgrp;
289 if (cgrp_out)
290 __update_cgrp_time(cgrp_out);
291}
292
293static inline void update_cgrp_time_from_event(struct perf_event *event)
294{
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200295 struct perf_cgroup *cgrp;
296
Stephane Eraniane5d13672011-02-14 11:20:01 +0200297 /*
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200298 * ensure we access cgroup data only when needed and
299 * when we know the cgroup is pinned (css_get)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200300 */
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200301 if (!is_cgroup_event(event))
Stephane Eraniane5d13672011-02-14 11:20:01 +0200302 return;
303
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200304 cgrp = perf_cgroup_from_task(current);
305 /*
306 * Do not update time when cgroup is not active
307 */
308 if (cgrp == event->cgrp)
309 __update_cgrp_time(event->cgrp);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200310}
311
312static inline void
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200313perf_cgroup_set_timestamp(struct task_struct *task,
314 struct perf_event_context *ctx)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200315{
316 struct perf_cgroup *cgrp;
317 struct perf_cgroup_info *info;
318
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200319 /*
320 * ctx->lock held by caller
321 * ensure we do not access cgroup data
322 * unless we have the cgroup pinned (css_get)
323 */
324 if (!task || !ctx->nr_cgroups)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200325 return;
326
327 cgrp = perf_cgroup_from_task(task);
328 info = this_cpu_ptr(cgrp->info);
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200329 info->timestamp = ctx->timestamp;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200330}
331
332#define PERF_CGROUP_SWOUT 0x1 /* cgroup switch out every event */
333#define PERF_CGROUP_SWIN 0x2 /* cgroup switch in events based on task */
334
335/*
336 * reschedule events based on the cgroup constraint of task.
337 *
338 * mode SWOUT : schedule out everything
339 * mode SWIN : schedule in based on cgroup for next
340 */
341void perf_cgroup_switch(struct task_struct *task, int mode)
342{
343 struct perf_cpu_context *cpuctx;
344 struct pmu *pmu;
345 unsigned long flags;
346
347 /*
348 * disable interrupts to avoid geting nr_cgroup
349 * changes via __perf_event_disable(). Also
350 * avoids preemption.
351 */
352 local_irq_save(flags);
353
354 /*
355 * we reschedule only in the presence of cgroup
356 * constrained events.
357 */
358 rcu_read_lock();
359
360 list_for_each_entry_rcu(pmu, &pmus, entry) {
Stephane Eraniane5d13672011-02-14 11:20:01 +0200361 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
362
Stephane Eraniane5d13672011-02-14 11:20:01 +0200363 /*
364 * perf_cgroup_events says at least one
365 * context on this CPU has cgroup events.
366 *
367 * ctx->nr_cgroups reports the number of cgroup
368 * events for a context.
369 */
370 if (cpuctx->ctx.nr_cgroups > 0) {
Peter Zijlstrafacc4302011-04-09 21:17:42 +0200371 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
372 perf_pmu_disable(cpuctx->ctx.pmu);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200373
374 if (mode & PERF_CGROUP_SWOUT) {
375 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
376 /*
377 * must not be done before ctxswout due
378 * to event_filter_match() in event_sched_out()
379 */
380 cpuctx->cgrp = NULL;
381 }
382
383 if (mode & PERF_CGROUP_SWIN) {
Stephane Eraniane566b762011-04-06 02:54:54 +0200384 WARN_ON_ONCE(cpuctx->cgrp);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200385 /* set cgrp before ctxsw in to
386 * allow event_filter_match() to not
387 * have to pass task around
388 */
389 cpuctx->cgrp = perf_cgroup_from_task(task);
390 cpu_ctx_sched_in(cpuctx, EVENT_ALL, task);
391 }
Peter Zijlstrafacc4302011-04-09 21:17:42 +0200392 perf_pmu_enable(cpuctx->ctx.pmu);
393 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200394 }
Stephane Eraniane5d13672011-02-14 11:20:01 +0200395 }
396
397 rcu_read_unlock();
398
399 local_irq_restore(flags);
400}
401
402static inline void perf_cgroup_sched_out(struct task_struct *task)
403{
404 perf_cgroup_switch(task, PERF_CGROUP_SWOUT);
405}
406
407static inline void perf_cgroup_sched_in(struct task_struct *task)
408{
409 perf_cgroup_switch(task, PERF_CGROUP_SWIN);
410}
411
412static inline int perf_cgroup_connect(int fd, struct perf_event *event,
413 struct perf_event_attr *attr,
414 struct perf_event *group_leader)
415{
416 struct perf_cgroup *cgrp;
417 struct cgroup_subsys_state *css;
418 struct file *file;
419 int ret = 0, fput_needed;
420
421 file = fget_light(fd, &fput_needed);
422 if (!file)
423 return -EBADF;
424
425 css = cgroup_css_from_dir(file, perf_subsys_id);
Li Zefan3db272c2011-03-03 14:25:37 +0800426 if (IS_ERR(css)) {
427 ret = PTR_ERR(css);
428 goto out;
429 }
Stephane Eraniane5d13672011-02-14 11:20:01 +0200430
431 cgrp = container_of(css, struct perf_cgroup, css);
432 event->cgrp = cgrp;
433
Li Zefanf75e18c2011-03-03 14:25:50 +0800434 /* must be done before we fput() the file */
435 perf_get_cgroup(event);
436
Stephane Eraniane5d13672011-02-14 11:20:01 +0200437 /*
438 * all events in a group must monitor
439 * the same cgroup because a task belongs
440 * to only one perf cgroup at a time
441 */
442 if (group_leader && group_leader->cgrp != cgrp) {
443 perf_detach_cgroup(event);
444 ret = -EINVAL;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200445 }
Li Zefan3db272c2011-03-03 14:25:37 +0800446out:
Stephane Eraniane5d13672011-02-14 11:20:01 +0200447 fput_light(file, fput_needed);
448 return ret;
449}
450
451static inline void
452perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
453{
454 struct perf_cgroup_info *t;
455 t = per_cpu_ptr(event->cgrp->info, event->cpu);
456 event->shadow_ctx_time = now - t->timestamp;
457}
458
459static inline void
460perf_cgroup_defer_enabled(struct perf_event *event)
461{
462 /*
463 * when the current task's perf cgroup does not match
464 * the event's, we need to remember to call the
465 * perf_mark_enable() function the first time a task with
466 * a matching perf cgroup is scheduled in.
467 */
468 if (is_cgroup_event(event) && !perf_cgroup_match(event))
469 event->cgrp_defer_enabled = 1;
470}
471
472static inline void
473perf_cgroup_mark_enabled(struct perf_event *event,
474 struct perf_event_context *ctx)
475{
476 struct perf_event *sub;
477 u64 tstamp = perf_event_time(event);
478
479 if (!event->cgrp_defer_enabled)
480 return;
481
482 event->cgrp_defer_enabled = 0;
483
484 event->tstamp_enabled = tstamp - event->total_time_enabled;
485 list_for_each_entry(sub, &event->sibling_list, group_entry) {
486 if (sub->state >= PERF_EVENT_STATE_INACTIVE) {
487 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
488 sub->cgrp_defer_enabled = 0;
489 }
490 }
491}
492#else /* !CONFIG_CGROUP_PERF */
493
494static inline bool
495perf_cgroup_match(struct perf_event *event)
496{
497 return true;
498}
499
500static inline void perf_detach_cgroup(struct perf_event *event)
501{}
502
503static inline int is_cgroup_event(struct perf_event *event)
504{
505 return 0;
506}
507
508static inline u64 perf_cgroup_event_cgrp_time(struct perf_event *event)
509{
510 return 0;
511}
512
513static inline void update_cgrp_time_from_event(struct perf_event *event)
514{
515}
516
517static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
518{
519}
520
521static inline void perf_cgroup_sched_out(struct task_struct *task)
522{
523}
524
525static inline void perf_cgroup_sched_in(struct task_struct *task)
526{
527}
528
529static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event,
530 struct perf_event_attr *attr,
531 struct perf_event *group_leader)
532{
533 return -EINVAL;
534}
535
536static inline void
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200537perf_cgroup_set_timestamp(struct task_struct *task,
538 struct perf_event_context *ctx)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200539{
540}
541
542void
543perf_cgroup_switch(struct task_struct *task, struct task_struct *next)
544{
545}
546
547static inline void
548perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
549{
550}
551
552static inline u64 perf_cgroup_event_time(struct perf_event *event)
553{
554 return 0;
555}
556
557static inline void
558perf_cgroup_defer_enabled(struct perf_event *event)
559{
560}
561
562static inline void
563perf_cgroup_mark_enabled(struct perf_event *event,
564 struct perf_event_context *ctx)
565{
566}
567#endif
568
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200569void perf_pmu_disable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200570{
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200571 int *count = this_cpu_ptr(pmu->pmu_disable_count);
572 if (!(*count)++)
573 pmu->pmu_disable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200574}
575
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200576void perf_pmu_enable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200577{
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200578 int *count = this_cpu_ptr(pmu->pmu_disable_count);
579 if (!--(*count))
580 pmu->pmu_enable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200581}
582
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200583static DEFINE_PER_CPU(struct list_head, rotation_list);
584
585/*
586 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
587 * because they're strictly cpu affine and rotate_start is called with IRQs
588 * disabled, while rotate_context is called from IRQ context.
589 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200590static void perf_pmu_rotate_start(struct pmu *pmu)
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200591{
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200592 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200593 struct list_head *head = &__get_cpu_var(rotation_list);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200594
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200595 WARN_ON(!irqs_disabled());
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200596
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200597 if (list_empty(&cpuctx->rotation_list))
598 list_add(&cpuctx->rotation_list, head);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200599}
600
601static void get_ctx(struct perf_event_context *ctx)
602{
603 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
604}
605
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200606static void put_ctx(struct perf_event_context *ctx)
607{
608 if (atomic_dec_and_test(&ctx->refcount)) {
609 if (ctx->parent_ctx)
610 put_ctx(ctx->parent_ctx);
611 if (ctx->task)
612 put_task_struct(ctx->task);
Lai Jiangshancb796ff2011-03-18 12:07:41 +0800613 kfree_rcu(ctx, rcu_head);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200614 }
615}
616
617static void unclone_ctx(struct perf_event_context *ctx)
618{
619 if (ctx->parent_ctx) {
620 put_ctx(ctx->parent_ctx);
621 ctx->parent_ctx = NULL;
622 }
623}
624
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -0200625static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
626{
627 /*
628 * only top level events have the pid namespace they were created in
629 */
630 if (event->parent)
631 event = event->parent;
632
633 return task_tgid_nr_ns(p, event->ns);
634}
635
636static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
637{
638 /*
639 * only top level events have the pid namespace they were created in
640 */
641 if (event->parent)
642 event = event->parent;
643
644 return task_pid_nr_ns(p, event->ns);
645}
646
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200647/*
648 * If we inherit events we want to return the parent event id
649 * to userspace.
650 */
651static u64 primary_event_id(struct perf_event *event)
652{
653 u64 id = event->id;
654
655 if (event->parent)
656 id = event->parent->id;
657
658 return id;
659}
660
661/*
662 * Get the perf_event_context for a task and lock it.
663 * This has to cope with with the fact that until it is locked,
664 * the context could get moved to another task.
665 */
666static struct perf_event_context *
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200667perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200668{
669 struct perf_event_context *ctx;
670
671 rcu_read_lock();
Peter Zijlstra9ed60602010-06-11 17:36:35 +0200672retry:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200673 ctx = rcu_dereference(task->perf_event_ctxp[ctxn]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200674 if (ctx) {
675 /*
676 * If this context is a clone of another, it might
677 * get swapped for another underneath us by
678 * perf_event_task_sched_out, though the
679 * rcu_read_lock() protects us from any context
680 * getting freed. Lock the context and check if it
681 * got swapped before we could get the lock, and retry
682 * if so. If we locked the right context, then it
683 * can't get swapped on us any more.
684 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100685 raw_spin_lock_irqsave(&ctx->lock, *flags);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200686 if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100687 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200688 goto retry;
689 }
690
691 if (!atomic_inc_not_zero(&ctx->refcount)) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100692 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200693 ctx = NULL;
694 }
695 }
696 rcu_read_unlock();
697 return ctx;
698}
699
700/*
701 * Get the context for a task and increment its pin_count so it
702 * can't get swapped to another task. This also increments its
703 * reference count so that the context can't get freed.
704 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200705static struct perf_event_context *
706perf_pin_task_context(struct task_struct *task, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200707{
708 struct perf_event_context *ctx;
709 unsigned long flags;
710
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200711 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200712 if (ctx) {
713 ++ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100714 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200715 }
716 return ctx;
717}
718
719static void perf_unpin_context(struct perf_event_context *ctx)
720{
721 unsigned long flags;
722
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100723 raw_spin_lock_irqsave(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200724 --ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100725 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200726}
727
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100728/*
729 * Update the record of the current time in a context.
730 */
731static void update_context_time(struct perf_event_context *ctx)
732{
733 u64 now = perf_clock();
734
735 ctx->time += now - ctx->timestamp;
736 ctx->timestamp = now;
737}
738
Stephane Eranian41587552011-01-03 18:20:01 +0200739static u64 perf_event_time(struct perf_event *event)
740{
741 struct perf_event_context *ctx = event->ctx;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200742
743 if (is_cgroup_event(event))
744 return perf_cgroup_event_time(event);
745
Stephane Eranian41587552011-01-03 18:20:01 +0200746 return ctx ? ctx->time : 0;
747}
748
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100749/*
750 * Update the total_time_enabled and total_time_running fields for a event.
Eric B Munsonb7526f02011-06-23 16:34:37 -0400751 * The caller of this function needs to hold the ctx->lock.
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100752 */
753static void update_event_times(struct perf_event *event)
754{
755 struct perf_event_context *ctx = event->ctx;
756 u64 run_end;
757
758 if (event->state < PERF_EVENT_STATE_INACTIVE ||
759 event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
760 return;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200761 /*
762 * in cgroup mode, time_enabled represents
763 * the time the event was enabled AND active
764 * tasks were in the monitored cgroup. This is
765 * independent of the activity of the context as
766 * there may be a mix of cgroup and non-cgroup events.
767 *
768 * That is why we treat cgroup events differently
769 * here.
770 */
771 if (is_cgroup_event(event))
Stephane Eranian41587552011-01-03 18:20:01 +0200772 run_end = perf_event_time(event);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200773 else if (ctx->is_active)
774 run_end = ctx->time;
Peter Zijlstraacd1d7c2009-11-23 15:00:36 +0100775 else
776 run_end = event->tstamp_stopped;
777
778 event->total_time_enabled = run_end - event->tstamp_enabled;
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100779
780 if (event->state == PERF_EVENT_STATE_INACTIVE)
781 run_end = event->tstamp_stopped;
782 else
Stephane Eranian41587552011-01-03 18:20:01 +0200783 run_end = perf_event_time(event);
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100784
785 event->total_time_running = run_end - event->tstamp_running;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200786
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100787}
788
Peter Zijlstra96c21a42010-05-11 16:19:10 +0200789/*
790 * Update total_time_enabled and total_time_running for all events in a group.
791 */
792static void update_group_times(struct perf_event *leader)
793{
794 struct perf_event *event;
795
796 update_event_times(leader);
797 list_for_each_entry(event, &leader->sibling_list, group_entry)
798 update_event_times(event);
799}
800
Frederic Weisbecker889ff012010-01-09 20:04:47 +0100801static struct list_head *
802ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
803{
804 if (event->attr.pinned)
805 return &ctx->pinned_groups;
806 else
807 return &ctx->flexible_groups;
808}
809
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200810/*
811 * Add a event from the lists for its context.
812 * Must be called with ctx->mutex and ctx->lock held.
813 */
814static void
815list_add_event(struct perf_event *event, struct perf_event_context *ctx)
816{
Peter Zijlstra8a495422010-05-27 15:47:49 +0200817 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
818 event->attach_state |= PERF_ATTACH_CONTEXT;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200819
820 /*
Peter Zijlstra8a495422010-05-27 15:47:49 +0200821 * If we're a stand alone event or group leader, we go to the context
822 * list, group events are kept attached to the group so that
823 * perf_group_detach can, at all times, locate all siblings.
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200824 */
Peter Zijlstra8a495422010-05-27 15:47:49 +0200825 if (event->group_leader == event) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +0100826 struct list_head *list;
827
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +0100828 if (is_software_event(event))
829 event->group_flags |= PERF_GROUP_SOFTWARE;
830
Frederic Weisbecker889ff012010-01-09 20:04:47 +0100831 list = ctx_group_list(event, ctx);
832 list_add_tail(&event->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200833 }
834
Peter Zijlstra08309372011-03-03 11:31:20 +0100835 if (is_cgroup_event(event))
Stephane Eraniane5d13672011-02-14 11:20:01 +0200836 ctx->nr_cgroups++;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200837
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200838 list_add_rcu(&event->event_entry, &ctx->event_list);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200839 if (!ctx->nr_events)
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200840 perf_pmu_rotate_start(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200841 ctx->nr_events++;
842 if (event->attr.inherit_stat)
843 ctx->nr_stat++;
844}
845
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -0200846/*
847 * Called at perf_event creation and when events are attached/detached from a
848 * group.
849 */
850static void perf_event__read_size(struct perf_event *event)
851{
852 int entry = sizeof(u64); /* value */
853 int size = 0;
854 int nr = 1;
855
856 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
857 size += sizeof(u64);
858
859 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
860 size += sizeof(u64);
861
862 if (event->attr.read_format & PERF_FORMAT_ID)
863 entry += sizeof(u64);
864
865 if (event->attr.read_format & PERF_FORMAT_GROUP) {
866 nr += event->group_leader->nr_siblings;
867 size += sizeof(u64);
868 }
869
870 size += entry * nr;
871 event->read_size = size;
872}
873
874static void perf_event__header_size(struct perf_event *event)
875{
876 struct perf_sample_data *data;
877 u64 sample_type = event->attr.sample_type;
878 u16 size = 0;
879
880 perf_event__read_size(event);
881
882 if (sample_type & PERF_SAMPLE_IP)
883 size += sizeof(data->ip);
884
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -0200885 if (sample_type & PERF_SAMPLE_ADDR)
886 size += sizeof(data->addr);
887
888 if (sample_type & PERF_SAMPLE_PERIOD)
889 size += sizeof(data->period);
890
891 if (sample_type & PERF_SAMPLE_READ)
892 size += event->read_size;
893
894 event->header_size = size;
895}
896
897static void perf_event__id_header_size(struct perf_event *event)
898{
899 struct perf_sample_data *data;
900 u64 sample_type = event->attr.sample_type;
901 u16 size = 0;
902
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -0200903 if (sample_type & PERF_SAMPLE_TID)
904 size += sizeof(data->tid_entry);
905
906 if (sample_type & PERF_SAMPLE_TIME)
907 size += sizeof(data->time);
908
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -0200909 if (sample_type & PERF_SAMPLE_ID)
910 size += sizeof(data->id);
911
912 if (sample_type & PERF_SAMPLE_STREAM_ID)
913 size += sizeof(data->stream_id);
914
915 if (sample_type & PERF_SAMPLE_CPU)
916 size += sizeof(data->cpu_entry);
917
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -0200918 event->id_header_size = size;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -0200919}
920
Peter Zijlstra8a495422010-05-27 15:47:49 +0200921static void perf_group_attach(struct perf_event *event)
922{
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -0200923 struct perf_event *group_leader = event->group_leader, *pos;
Peter Zijlstra8a495422010-05-27 15:47:49 +0200924
Peter Zijlstra74c33372010-10-15 11:40:29 +0200925 /*
926 * We can have double attach due to group movement in perf_event_open.
927 */
928 if (event->attach_state & PERF_ATTACH_GROUP)
929 return;
930
Peter Zijlstra8a495422010-05-27 15:47:49 +0200931 event->attach_state |= PERF_ATTACH_GROUP;
932
933 if (group_leader == event)
934 return;
935
936 if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
937 !is_software_event(event))
938 group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
939
940 list_add_tail(&event->group_entry, &group_leader->sibling_list);
941 group_leader->nr_siblings++;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -0200942
943 perf_event__header_size(group_leader);
944
945 list_for_each_entry(pos, &group_leader->sibling_list, group_entry)
946 perf_event__header_size(pos);
Peter Zijlstra8a495422010-05-27 15:47:49 +0200947}
948
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200949/*
950 * Remove a event from the lists for its context.
951 * Must be called with ctx->mutex and ctx->lock held.
952 */
953static void
954list_del_event(struct perf_event *event, struct perf_event_context *ctx)
955{
Stephane Eranian68cacd22011-03-23 16:03:06 +0100956 struct perf_cpu_context *cpuctx;
Peter Zijlstra8a495422010-05-27 15:47:49 +0200957 /*
958 * We can have double detach due to exit/hot-unplug + close.
959 */
960 if (!(event->attach_state & PERF_ATTACH_CONTEXT))
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200961 return;
Peter Zijlstra8a495422010-05-27 15:47:49 +0200962
963 event->attach_state &= ~PERF_ATTACH_CONTEXT;
964
Stephane Eranian68cacd22011-03-23 16:03:06 +0100965 if (is_cgroup_event(event)) {
Stephane Eraniane5d13672011-02-14 11:20:01 +0200966 ctx->nr_cgroups--;
Stephane Eranian68cacd22011-03-23 16:03:06 +0100967 cpuctx = __get_cpu_context(ctx);
968 /*
969 * if there are no more cgroup events
970 * then cler cgrp to avoid stale pointer
971 * in update_cgrp_time_from_cpuctx()
972 */
973 if (!ctx->nr_cgroups)
974 cpuctx->cgrp = NULL;
975 }
Stephane Eraniane5d13672011-02-14 11:20:01 +0200976
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200977 ctx->nr_events--;
978 if (event->attr.inherit_stat)
979 ctx->nr_stat--;
980
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200981 list_del_rcu(&event->event_entry);
982
Peter Zijlstra8a495422010-05-27 15:47:49 +0200983 if (event->group_leader == event)
984 list_del_init(&event->group_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200985
Peter Zijlstra96c21a42010-05-11 16:19:10 +0200986 update_group_times(event);
Stephane Eranianb2e74a22009-11-26 09:24:30 -0800987
988 /*
989 * If event was in error state, then keep it
990 * that way, otherwise bogus counts will be
991 * returned on read(). The only way to get out
992 * of error state is by explicit re-enabling
993 * of the event
994 */
995 if (event->state > PERF_EVENT_STATE_OFF)
996 event->state = PERF_EVENT_STATE_OFF;
Peter Zijlstra050735b2010-05-11 11:51:53 +0200997}
998
Peter Zijlstra8a495422010-05-27 15:47:49 +0200999static void perf_group_detach(struct perf_event *event)
Peter Zijlstra050735b2010-05-11 11:51:53 +02001000{
1001 struct perf_event *sibling, *tmp;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001002 struct list_head *list = NULL;
1003
1004 /*
1005 * We can have double detach due to exit/hot-unplug + close.
1006 */
1007 if (!(event->attach_state & PERF_ATTACH_GROUP))
1008 return;
1009
1010 event->attach_state &= ~PERF_ATTACH_GROUP;
1011
1012 /*
1013 * If this is a sibling, remove it from its group.
1014 */
1015 if (event->group_leader != event) {
1016 list_del_init(&event->group_entry);
1017 event->group_leader->nr_siblings--;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001018 goto out;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001019 }
1020
1021 if (!list_empty(&event->group_entry))
1022 list = &event->group_entry;
Peter Zijlstra2e2af502009-11-23 11:37:25 +01001023
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001024 /*
1025 * If this was a group event with sibling events then
1026 * upgrade the siblings to singleton events by adding them
Peter Zijlstra8a495422010-05-27 15:47:49 +02001027 * to whatever list we are on.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001028 */
1029 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
Peter Zijlstra8a495422010-05-27 15:47:49 +02001030 if (list)
1031 list_move_tail(&sibling->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001032 sibling->group_leader = sibling;
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01001033
1034 /* Inherit group flags from the previous leader */
1035 sibling->group_flags = event->group_flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001036 }
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001037
1038out:
1039 perf_event__header_size(event->group_leader);
1040
1041 list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry)
1042 perf_event__header_size(tmp);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001043}
1044
Stephane Eranianfa66f072010-08-26 16:40:01 +02001045static inline int
1046event_filter_match(struct perf_event *event)
1047{
Stephane Eraniane5d13672011-02-14 11:20:01 +02001048 return (event->cpu == -1 || event->cpu == smp_processor_id())
1049 && perf_cgroup_match(event);
Stephane Eranianfa66f072010-08-26 16:40:01 +02001050}
1051
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001052static void
1053event_sched_out(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001054 struct perf_cpu_context *cpuctx,
1055 struct perf_event_context *ctx)
1056{
Stephane Eranian41587552011-01-03 18:20:01 +02001057 u64 tstamp = perf_event_time(event);
Stephane Eranianfa66f072010-08-26 16:40:01 +02001058 u64 delta;
1059 /*
1060 * An event which could not be activated because of
1061 * filter mismatch still needs to have its timings
1062 * maintained, otherwise bogus information is return
1063 * via read() for time_enabled, time_running:
1064 */
1065 if (event->state == PERF_EVENT_STATE_INACTIVE
1066 && !event_filter_match(event)) {
Stephane Eraniane5d13672011-02-14 11:20:01 +02001067 delta = tstamp - event->tstamp_stopped;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001068 event->tstamp_running += delta;
Stephane Eranian41587552011-01-03 18:20:01 +02001069 event->tstamp_stopped = tstamp;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001070 }
1071
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001072 if (event->state != PERF_EVENT_STATE_ACTIVE)
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001073 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001074
1075 event->state = PERF_EVENT_STATE_INACTIVE;
1076 if (event->pending_disable) {
1077 event->pending_disable = 0;
1078 event->state = PERF_EVENT_STATE_OFF;
1079 }
Stephane Eranian41587552011-01-03 18:20:01 +02001080 event->tstamp_stopped = tstamp;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001081 event->pmu->del(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001082 event->oncpu = -1;
1083
1084 if (!is_software_event(event))
1085 cpuctx->active_oncpu--;
1086 ctx->nr_active--;
1087 if (event->attr.exclusive || !cpuctx->active_oncpu)
1088 cpuctx->exclusive = 0;
1089}
1090
1091static void
1092group_sched_out(struct perf_event *group_event,
1093 struct perf_cpu_context *cpuctx,
1094 struct perf_event_context *ctx)
1095{
1096 struct perf_event *event;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001097 int state = group_event->state;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001098
1099 event_sched_out(group_event, cpuctx, ctx);
1100
1101 /*
1102 * Schedule out siblings (if any):
1103 */
1104 list_for_each_entry(event, &group_event->sibling_list, group_entry)
1105 event_sched_out(event, cpuctx, ctx);
1106
Stephane Eranianfa66f072010-08-26 16:40:01 +02001107 if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001108 cpuctx->exclusive = 0;
1109}
1110
1111/*
1112 * Cross CPU call to remove a performance event
1113 *
1114 * We disable the event on the hardware level first. After that we
1115 * remove it from the context list.
1116 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001117static int __perf_remove_from_context(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001118{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001119 struct perf_event *event = info;
1120 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001121 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001122
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001123 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001124 event_sched_out(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001125 list_del_event(event, ctx);
Peter Zijlstra64ce3122011-04-09 21:17:48 +02001126 if (!ctx->nr_events && cpuctx->task_ctx == ctx) {
1127 ctx->is_active = 0;
1128 cpuctx->task_ctx = NULL;
1129 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001130 raw_spin_unlock(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001131
1132 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001133}
1134
1135
1136/*
1137 * Remove the event from a task's (or a CPU's) list of events.
1138 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001139 * CPU events are removed with a smp call. For task events we only
1140 * call when the task is on a CPU.
1141 *
1142 * If event->ctx is a cloned context, callers must make sure that
1143 * every task struct that event->ctx->task could possibly point to
1144 * remains valid. This is OK when called from perf_release since
1145 * that only calls us on the top-level context, which can't be a clone.
1146 * When called from perf_event_exit_task, it's OK because the
1147 * context has been detached from its task.
1148 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001149static void perf_remove_from_context(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001150{
1151 struct perf_event_context *ctx = event->ctx;
1152 struct task_struct *task = ctx->task;
1153
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001154 lockdep_assert_held(&ctx->mutex);
1155
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001156 if (!task) {
1157 /*
1158 * Per cpu events are removed via an smp call and
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001159 * the removal is always successful.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001160 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001161 cpu_function_call(event->cpu, __perf_remove_from_context, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001162 return;
1163 }
1164
1165retry:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001166 if (!task_function_call(task, __perf_remove_from_context, event))
1167 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001168
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001169 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001170 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001171 * If we failed to find a running task, but find the context active now
1172 * that we've acquired the ctx->lock, retry.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001173 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001174 if (ctx->is_active) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001175 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001176 goto retry;
1177 }
1178
1179 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001180 * Since the task isn't running, its safe to remove the event, us
1181 * holding the ctx->lock ensures the task won't get scheduled in.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001182 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001183 list_del_event(event, ctx);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001184 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001185}
1186
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001187/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001188 * Cross CPU call to disable a performance event
1189 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001190static int __perf_event_disable(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001191{
1192 struct perf_event *event = info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001193 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001194 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001195
1196 /*
1197 * If this is a per-task event, need to check whether this
1198 * event's task is the current task on this cpu.
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001199 *
1200 * Can trigger due to concurrent perf_event_context_sched_out()
1201 * flipping contexts around.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001202 */
1203 if (ctx->task && cpuctx->task_ctx != ctx)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001204 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001205
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001206 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001207
1208 /*
1209 * If the event is on, turn it off.
1210 * If it is in error state, leave it in error state.
1211 */
1212 if (event->state >= PERF_EVENT_STATE_INACTIVE) {
1213 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001214 update_cgrp_time_from_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001215 update_group_times(event);
1216 if (event == event->group_leader)
1217 group_sched_out(event, cpuctx, ctx);
1218 else
1219 event_sched_out(event, cpuctx, ctx);
1220 event->state = PERF_EVENT_STATE_OFF;
1221 }
1222
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001223 raw_spin_unlock(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001224
1225 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001226}
1227
1228/*
1229 * Disable a event.
1230 *
1231 * If event->ctx is a cloned context, callers must make sure that
1232 * every task struct that event->ctx->task could possibly point to
1233 * remains valid. This condition is satisifed when called through
1234 * perf_event_for_each_child or perf_event_for_each because they
1235 * hold the top-level event's child_mutex, so any descendant that
1236 * goes to exit will block in sync_child_event.
1237 * When called from perf_pending_event it's OK because event->ctx
1238 * is the current context on this CPU and preemption is disabled,
1239 * hence we can't get into perf_event_task_sched_out for this context.
1240 */
Frederic Weisbecker44234ad2009-12-09 09:25:48 +01001241void perf_event_disable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001242{
1243 struct perf_event_context *ctx = event->ctx;
1244 struct task_struct *task = ctx->task;
1245
1246 if (!task) {
1247 /*
1248 * Disable the event on the cpu that it's on
1249 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001250 cpu_function_call(event->cpu, __perf_event_disable, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001251 return;
1252 }
1253
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001254retry:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001255 if (!task_function_call(task, __perf_event_disable, event))
1256 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001257
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001258 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001259 /*
1260 * If the event is still active, we need to retry the cross-call.
1261 */
1262 if (event->state == PERF_EVENT_STATE_ACTIVE) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001263 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001264 /*
1265 * Reload the task pointer, it might have been changed by
1266 * a concurrent perf_event_context_sched_out().
1267 */
1268 task = ctx->task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001269 goto retry;
1270 }
1271
1272 /*
1273 * Since we have the lock this context can't be scheduled
1274 * in, so we can change the state safely.
1275 */
1276 if (event->state == PERF_EVENT_STATE_INACTIVE) {
1277 update_group_times(event);
1278 event->state = PERF_EVENT_STATE_OFF;
1279 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001280 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001281}
1282
Stephane Eraniane5d13672011-02-14 11:20:01 +02001283static void perf_set_shadow_time(struct perf_event *event,
1284 struct perf_event_context *ctx,
1285 u64 tstamp)
1286{
1287 /*
1288 * use the correct time source for the time snapshot
1289 *
1290 * We could get by without this by leveraging the
1291 * fact that to get to this function, the caller
1292 * has most likely already called update_context_time()
1293 * and update_cgrp_time_xx() and thus both timestamp
1294 * are identical (or very close). Given that tstamp is,
1295 * already adjusted for cgroup, we could say that:
1296 * tstamp - ctx->timestamp
1297 * is equivalent to
1298 * tstamp - cgrp->timestamp.
1299 *
1300 * Then, in perf_output_read(), the calculation would
1301 * work with no changes because:
1302 * - event is guaranteed scheduled in
1303 * - no scheduled out in between
1304 * - thus the timestamp would be the same
1305 *
1306 * But this is a bit hairy.
1307 *
1308 * So instead, we have an explicit cgroup call to remain
1309 * within the time time source all along. We believe it
1310 * is cleaner and simpler to understand.
1311 */
1312 if (is_cgroup_event(event))
1313 perf_cgroup_set_shadow_time(event, tstamp);
1314 else
1315 event->shadow_ctx_time = tstamp - ctx->timestamp;
1316}
1317
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01001318#define MAX_INTERRUPTS (~0ULL)
1319
1320static void perf_log_throttle(struct perf_event *event, int enable);
1321
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001322static int
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001323event_sched_in(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001324 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001325 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001326{
Stephane Eranian41587552011-01-03 18:20:01 +02001327 u64 tstamp = perf_event_time(event);
1328
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001329 if (event->state <= PERF_EVENT_STATE_OFF)
1330 return 0;
1331
1332 event->state = PERF_EVENT_STATE_ACTIVE;
Peter Zijlstra6e377382010-02-11 13:21:58 +01001333 event->oncpu = smp_processor_id();
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01001334
1335 /*
1336 * Unthrottle events, since we scheduled we might have missed several
1337 * ticks already, also for a heavily scheduling task there is little
1338 * guarantee it'll get a tick in a timely manner.
1339 */
1340 if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) {
1341 perf_log_throttle(event, 1);
1342 event->hw.interrupts = 0;
1343 }
1344
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001345 /*
1346 * The new state must be visible before we turn it on in the hardware:
1347 */
1348 smp_wmb();
1349
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001350 if (event->pmu->add(event, PERF_EF_START)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001351 event->state = PERF_EVENT_STATE_INACTIVE;
1352 event->oncpu = -1;
1353 return -EAGAIN;
1354 }
1355
Stephane Eranian41587552011-01-03 18:20:01 +02001356 event->tstamp_running += tstamp - event->tstamp_stopped;
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001357
Stephane Eraniane5d13672011-02-14 11:20:01 +02001358 perf_set_shadow_time(event, ctx, tstamp);
Stephane Eranianeed01522010-10-26 16:08:01 +02001359
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001360 if (!is_software_event(event))
1361 cpuctx->active_oncpu++;
1362 ctx->nr_active++;
1363
1364 if (event->attr.exclusive)
1365 cpuctx->exclusive = 1;
1366
1367 return 0;
1368}
1369
1370static int
1371group_sched_in(struct perf_event *group_event,
1372 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001373 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001374{
Lin Ming6bde9b62010-04-23 13:56:00 +08001375 struct perf_event *event, *partial_group = NULL;
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02001376 struct pmu *pmu = group_event->pmu;
Stephane Eraniand7842da2010-10-20 15:25:01 +02001377 u64 now = ctx->time;
1378 bool simulate = false;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001379
1380 if (group_event->state == PERF_EVENT_STATE_OFF)
1381 return 0;
1382
Peter Zijlstraad5133b2010-06-15 12:22:39 +02001383 pmu->start_txn(pmu);
Lin Ming6bde9b62010-04-23 13:56:00 +08001384
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001385 if (event_sched_in(group_event, cpuctx, ctx)) {
Peter Zijlstraad5133b2010-06-15 12:22:39 +02001386 pmu->cancel_txn(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001387 return -EAGAIN;
Stephane Eranian90151c352010-05-25 16:23:10 +02001388 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001389
1390 /*
1391 * Schedule in siblings as one group (if any):
1392 */
1393 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001394 if (event_sched_in(event, cpuctx, ctx)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001395 partial_group = event;
1396 goto group_error;
1397 }
1398 }
1399
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001400 if (!pmu->commit_txn(pmu))
Paul Mackerras6e851582010-05-08 20:58:00 +10001401 return 0;
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001402
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001403group_error:
1404 /*
1405 * Groups can be scheduled in as one unit only, so undo any
1406 * partial group before returning:
Stephane Eraniand7842da2010-10-20 15:25:01 +02001407 * The events up to the failed event are scheduled out normally,
1408 * tstamp_stopped will be updated.
1409 *
1410 * The failed events and the remaining siblings need to have
1411 * their timings updated as if they had gone thru event_sched_in()
1412 * and event_sched_out(). This is required to get consistent timings
1413 * across the group. This also takes care of the case where the group
1414 * could never be scheduled by ensuring tstamp_stopped is set to mark
1415 * the time the event was actually stopped, such that time delta
1416 * calculation in update_event_times() is correct.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001417 */
1418 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
1419 if (event == partial_group)
Stephane Eraniand7842da2010-10-20 15:25:01 +02001420 simulate = true;
1421
1422 if (simulate) {
1423 event->tstamp_running += now - event->tstamp_stopped;
1424 event->tstamp_stopped = now;
1425 } else {
1426 event_sched_out(event, cpuctx, ctx);
1427 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001428 }
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001429 event_sched_out(group_event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001430
Peter Zijlstraad5133b2010-06-15 12:22:39 +02001431 pmu->cancel_txn(pmu);
Stephane Eranian90151c352010-05-25 16:23:10 +02001432
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001433 return -EAGAIN;
1434}
1435
1436/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001437 * Work out whether we can put this event group on the CPU now.
1438 */
1439static int group_can_go_on(struct perf_event *event,
1440 struct perf_cpu_context *cpuctx,
1441 int can_add_hw)
1442{
1443 /*
1444 * Groups consisting entirely of software events can always go on.
1445 */
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01001446 if (event->group_flags & PERF_GROUP_SOFTWARE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001447 return 1;
1448 /*
1449 * If an exclusive group is already on, no other hardware
1450 * events can go on.
1451 */
1452 if (cpuctx->exclusive)
1453 return 0;
1454 /*
1455 * If this group is exclusive and there are already
1456 * events on the CPU, it can't go on.
1457 */
1458 if (event->attr.exclusive && cpuctx->active_oncpu)
1459 return 0;
1460 /*
1461 * Otherwise, try to add it if all previous groups were able
1462 * to go on.
1463 */
1464 return can_add_hw;
1465}
1466
1467static void add_event_to_ctx(struct perf_event *event,
1468 struct perf_event_context *ctx)
1469{
Stephane Eranian41587552011-01-03 18:20:01 +02001470 u64 tstamp = perf_event_time(event);
1471
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001472 list_add_event(event, ctx);
Peter Zijlstra8a495422010-05-27 15:47:49 +02001473 perf_group_attach(event);
Stephane Eranian41587552011-01-03 18:20:01 +02001474 event->tstamp_enabled = tstamp;
1475 event->tstamp_running = tstamp;
1476 event->tstamp_stopped = tstamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001477}
1478
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001479static void task_ctx_sched_out(struct perf_event_context *ctx);
1480static void
1481ctx_sched_in(struct perf_event_context *ctx,
1482 struct perf_cpu_context *cpuctx,
1483 enum event_type_t event_type,
1484 struct task_struct *task);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001485
Peter Zijlstradce58552011-04-09 21:17:46 +02001486static void perf_event_sched_in(struct perf_cpu_context *cpuctx,
1487 struct perf_event_context *ctx,
1488 struct task_struct *task)
1489{
1490 cpu_ctx_sched_in(cpuctx, EVENT_PINNED, task);
1491 if (ctx)
1492 ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task);
1493 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task);
1494 if (ctx)
1495 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task);
1496}
1497
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001498/*
1499 * Cross CPU call to install and enable a performance event
1500 *
1501 * Must be called with ctx->mutex held
1502 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001503static int __perf_install_in_context(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001504{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001505 struct perf_event *event = info;
1506 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001507 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001508 struct perf_event_context *task_ctx = cpuctx->task_ctx;
1509 struct task_struct *task = current;
1510
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02001511 perf_ctx_lock(cpuctx, task_ctx);
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001512 perf_pmu_disable(cpuctx->ctx.pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001513
1514 /*
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001515 * If there was an active task_ctx schedule it out.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001516 */
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02001517 if (task_ctx)
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001518 task_ctx_sched_out(task_ctx);
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02001519
1520 /*
1521 * If the context we're installing events in is not the
1522 * active task_ctx, flip them.
1523 */
1524 if (ctx->task && task_ctx != ctx) {
1525 if (task_ctx)
1526 raw_spin_unlock(&task_ctx->lock);
1527 raw_spin_lock(&ctx->lock);
1528 task_ctx = ctx;
1529 }
1530
1531 if (task_ctx) {
1532 cpuctx->task_ctx = task_ctx;
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001533 task = task_ctx->task;
1534 }
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02001535
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001536 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001537
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001538 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001539 /*
1540 * update cgrp time only if current cgrp
1541 * matches event->cgrp. Must be done before
1542 * calling add_event_to_ctx()
1543 */
1544 update_cgrp_time_from_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001545
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001546 add_event_to_ctx(event, ctx);
1547
1548 /*
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001549 * Schedule everything back in
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001550 */
Peter Zijlstradce58552011-04-09 21:17:46 +02001551 perf_event_sched_in(cpuctx, task_ctx, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001552
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001553 perf_pmu_enable(cpuctx->ctx.pmu);
1554 perf_ctx_unlock(cpuctx, task_ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001555
1556 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001557}
1558
1559/*
1560 * Attach a performance event to a context
1561 *
1562 * First we add the event to the list with the hardware enable bit
1563 * in event->hw_config cleared.
1564 *
1565 * If the event is attached to a task which is on a CPU we use a smp
1566 * call to enable it in the task context. The task might have been
1567 * scheduled away, but we check this in the smp call again.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001568 */
1569static void
1570perf_install_in_context(struct perf_event_context *ctx,
1571 struct perf_event *event,
1572 int cpu)
1573{
1574 struct task_struct *task = ctx->task;
1575
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001576 lockdep_assert_held(&ctx->mutex);
1577
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02001578 event->ctx = ctx;
1579
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001580 if (!task) {
1581 /*
1582 * Per cpu events are installed via an smp call and
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001583 * the install is always successful.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001584 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001585 cpu_function_call(cpu, __perf_install_in_context, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001586 return;
1587 }
1588
1589retry:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001590 if (!task_function_call(task, __perf_install_in_context, event))
1591 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001592
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001593 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001594 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001595 * If we failed to find a running task, but find the context active now
1596 * that we've acquired the ctx->lock, retry.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001597 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001598 if (ctx->is_active) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001599 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001600 goto retry;
1601 }
1602
1603 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001604 * Since the task isn't running, its safe to add the event, us holding
1605 * the ctx->lock ensures the task won't get scheduled in.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001606 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001607 add_event_to_ctx(event, ctx);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001608 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001609}
1610
1611/*
1612 * Put a event into inactive state and update time fields.
1613 * Enabling the leader of a group effectively enables all
1614 * the group members that aren't explicitly disabled, so we
1615 * have to update their ->tstamp_enabled also.
1616 * Note: this works for group members as well as group leaders
1617 * since the non-leader members' sibling_lists will be empty.
1618 */
1619static void __perf_event_mark_enabled(struct perf_event *event,
1620 struct perf_event_context *ctx)
1621{
1622 struct perf_event *sub;
Stephane Eranian41587552011-01-03 18:20:01 +02001623 u64 tstamp = perf_event_time(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001624
1625 event->state = PERF_EVENT_STATE_INACTIVE;
Stephane Eranian41587552011-01-03 18:20:01 +02001626 event->tstamp_enabled = tstamp - event->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001627 list_for_each_entry(sub, &event->sibling_list, group_entry) {
Stephane Eranian41587552011-01-03 18:20:01 +02001628 if (sub->state >= PERF_EVENT_STATE_INACTIVE)
1629 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001630 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001631}
1632
1633/*
1634 * Cross CPU call to enable a performance event
1635 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001636static int __perf_event_enable(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001637{
1638 struct perf_event *event = info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001639 struct perf_event_context *ctx = event->ctx;
1640 struct perf_event *leader = event->group_leader;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001641 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001642 int err;
1643
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001644 if (WARN_ON_ONCE(!ctx->is_active))
1645 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001646
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001647 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001648 update_context_time(ctx);
1649
1650 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1651 goto unlock;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001652
1653 /*
1654 * set current task's cgroup time reference point
1655 */
Stephane Eranian3f7cce32011-02-18 14:40:01 +02001656 perf_cgroup_set_timestamp(current, ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001657
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001658 __perf_event_mark_enabled(event, ctx);
1659
Stephane Eraniane5d13672011-02-14 11:20:01 +02001660 if (!event_filter_match(event)) {
1661 if (is_cgroup_event(event))
1662 perf_cgroup_defer_enabled(event);
Peter Zijlstraf4c41762009-12-16 17:55:54 +01001663 goto unlock;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001664 }
Peter Zijlstraf4c41762009-12-16 17:55:54 +01001665
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001666 /*
1667 * If the event is in a group and isn't the group leader,
1668 * then don't put it on unless the group is on.
1669 */
1670 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
1671 goto unlock;
1672
1673 if (!group_can_go_on(event, cpuctx, 1)) {
1674 err = -EEXIST;
1675 } else {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001676 if (event == leader)
Peter Zijlstra6e377382010-02-11 13:21:58 +01001677 err = group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001678 else
Peter Zijlstra6e377382010-02-11 13:21:58 +01001679 err = event_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001680 }
1681
1682 if (err) {
1683 /*
1684 * If this event can't go on and it's part of a
1685 * group, then the whole group has to come off.
1686 */
1687 if (leader != event)
1688 group_sched_out(leader, cpuctx, ctx);
1689 if (leader->attr.pinned) {
1690 update_group_times(leader);
1691 leader->state = PERF_EVENT_STATE_ERROR;
1692 }
1693 }
1694
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001695unlock:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001696 raw_spin_unlock(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001697
1698 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001699}
1700
1701/*
1702 * Enable a event.
1703 *
1704 * If event->ctx is a cloned context, callers must make sure that
1705 * every task struct that event->ctx->task could possibly point to
1706 * remains valid. This condition is satisfied when called through
1707 * perf_event_for_each_child or perf_event_for_each as described
1708 * for perf_event_disable.
1709 */
Frederic Weisbecker44234ad2009-12-09 09:25:48 +01001710void perf_event_enable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001711{
1712 struct perf_event_context *ctx = event->ctx;
1713 struct task_struct *task = ctx->task;
1714
1715 if (!task) {
1716 /*
1717 * Enable the event on the cpu that it's on
1718 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001719 cpu_function_call(event->cpu, __perf_event_enable, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001720 return;
1721 }
1722
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001723 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001724 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1725 goto out;
1726
1727 /*
1728 * If the event is in error state, clear that first.
1729 * That way, if we see the event in error state below, we
1730 * know that it has gone back into error state, as distinct
1731 * from the task having been scheduled away before the
1732 * cross-call arrived.
1733 */
1734 if (event->state == PERF_EVENT_STATE_ERROR)
1735 event->state = PERF_EVENT_STATE_OFF;
1736
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001737retry:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001738 if (!ctx->is_active) {
1739 __perf_event_mark_enabled(event, ctx);
1740 goto out;
1741 }
1742
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001743 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001744
1745 if (!task_function_call(task, __perf_event_enable, event))
1746 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001747
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001748 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001749
1750 /*
1751 * If the context is active and the event is still off,
1752 * we need to retry the cross-call.
1753 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001754 if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF) {
1755 /*
1756 * task could have been flipped by a concurrent
1757 * perf_event_context_sched_out()
1758 */
1759 task = ctx->task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001760 goto retry;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001761 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001762
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001763out:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001764 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001765}
1766
1767static int perf_event_refresh(struct perf_event *event, int refresh)
1768{
1769 /*
1770 * not supported on inherited events
1771 */
Franck Bui-Huu2e939d12010-11-23 16:21:44 +01001772 if (event->attr.inherit || !is_sampling_event(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001773 return -EINVAL;
1774
1775 atomic_add(refresh, &event->event_limit);
1776 perf_event_enable(event);
1777
1778 return 0;
1779}
1780
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001781static void ctx_sched_out(struct perf_event_context *ctx,
1782 struct perf_cpu_context *cpuctx,
1783 enum event_type_t event_type)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001784{
1785 struct perf_event *event;
Peter Zijlstradb24d332011-04-09 21:17:45 +02001786 int is_active = ctx->is_active;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001787
Peter Zijlstradb24d332011-04-09 21:17:45 +02001788 ctx->is_active &= ~event_type;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001789 if (likely(!ctx->nr_events))
Peter Zijlstrafacc4302011-04-09 21:17:42 +02001790 return;
1791
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001792 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001793 update_cgrp_time_from_cpuctx(cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001794 if (!ctx->nr_active)
Peter Zijlstrafacc4302011-04-09 21:17:42 +02001795 return;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001796
Peter Zijlstra075e0b02011-04-09 21:17:40 +02001797 perf_pmu_disable(ctx->pmu);
Peter Zijlstradb24d332011-04-09 21:17:45 +02001798 if ((is_active & EVENT_PINNED) && (event_type & EVENT_PINNED)) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001799 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
1800 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001801 }
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001802
Peter Zijlstradb24d332011-04-09 21:17:45 +02001803 if ((is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE)) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001804 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08001805 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001806 }
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02001807 perf_pmu_enable(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001808}
1809
1810/*
1811 * Test whether two contexts are equivalent, i.e. whether they
1812 * have both been cloned from the same version of the same context
1813 * and they both have the same number of enabled events.
1814 * If the number of enabled events is the same, then the set
1815 * of enabled events should be the same, because these are both
1816 * inherited contexts, therefore we can't access individual events
1817 * in them directly with an fd; we can only enable/disable all
1818 * events via prctl, or enable/disable all events in a family
1819 * via ioctl, which will have the same effect on both contexts.
1820 */
1821static int context_equiv(struct perf_event_context *ctx1,
1822 struct perf_event_context *ctx2)
1823{
1824 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
1825 && ctx1->parent_gen == ctx2->parent_gen
1826 && !ctx1->pin_count && !ctx2->pin_count;
1827}
1828
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001829static void __perf_event_sync_stat(struct perf_event *event,
1830 struct perf_event *next_event)
1831{
1832 u64 value;
1833
1834 if (!event->attr.inherit_stat)
1835 return;
1836
1837 /*
1838 * Update the event value, we cannot use perf_event_read()
1839 * because we're in the middle of a context switch and have IRQs
1840 * disabled, which upsets smp_call_function_single(), however
1841 * we know the event must be on the current CPU, therefore we
1842 * don't need to use it.
1843 */
1844 switch (event->state) {
1845 case PERF_EVENT_STATE_ACTIVE:
Peter Zijlstra3dbebf12009-11-20 22:19:52 +01001846 event->pmu->read(event);
1847 /* fall-through */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001848
1849 case PERF_EVENT_STATE_INACTIVE:
1850 update_event_times(event);
1851 break;
1852
1853 default:
1854 break;
1855 }
1856
1857 /*
1858 * In order to keep per-task stats reliable we need to flip the event
1859 * values when we flip the contexts.
1860 */
Peter Zijlstrae7850592010-05-21 14:43:08 +02001861 value = local64_read(&next_event->count);
1862 value = local64_xchg(&event->count, value);
1863 local64_set(&next_event->count, value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001864
1865 swap(event->total_time_enabled, next_event->total_time_enabled);
1866 swap(event->total_time_running, next_event->total_time_running);
1867
1868 /*
1869 * Since we swizzled the values, update the user visible data too.
1870 */
1871 perf_event_update_userpage(event);
1872 perf_event_update_userpage(next_event);
1873}
1874
1875#define list_next_entry(pos, member) \
1876 list_entry(pos->member.next, typeof(*pos), member)
1877
1878static void perf_event_sync_stat(struct perf_event_context *ctx,
1879 struct perf_event_context *next_ctx)
1880{
1881 struct perf_event *event, *next_event;
1882
1883 if (!ctx->nr_stat)
1884 return;
1885
Peter Zijlstra02ffdbc2009-11-20 22:19:50 +01001886 update_context_time(ctx);
1887
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001888 event = list_first_entry(&ctx->event_list,
1889 struct perf_event, event_entry);
1890
1891 next_event = list_first_entry(&next_ctx->event_list,
1892 struct perf_event, event_entry);
1893
1894 while (&event->event_entry != &ctx->event_list &&
1895 &next_event->event_entry != &next_ctx->event_list) {
1896
1897 __perf_event_sync_stat(event, next_event);
1898
1899 event = list_next_entry(event, event_entry);
1900 next_event = list_next_entry(next_event, event_entry);
1901 }
1902}
1903
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001904static void perf_event_context_sched_out(struct task_struct *task, int ctxn,
1905 struct task_struct *next)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001906{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001907 struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001908 struct perf_event_context *next_ctx;
1909 struct perf_event_context *parent;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001910 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001911 int do_switch = 1;
1912
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001913 if (likely(!ctx))
1914 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001915
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001916 cpuctx = __get_cpu_context(ctx);
1917 if (!cpuctx->task_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001918 return;
1919
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001920 rcu_read_lock();
1921 parent = rcu_dereference(ctx->parent_ctx);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001922 next_ctx = next->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001923 if (parent && next_ctx &&
1924 rcu_dereference(next_ctx->parent_ctx) == parent) {
1925 /*
1926 * Looks like the two contexts are clones, so we might be
1927 * able to optimize the context switch. We lock both
1928 * contexts and check that they are clones under the
1929 * lock (including re-checking that neither has been
1930 * uncloned in the meantime). It doesn't matter which
1931 * order we take the locks because no other cpu could
1932 * be trying to lock both of these tasks.
1933 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001934 raw_spin_lock(&ctx->lock);
1935 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001936 if (context_equiv(ctx, next_ctx)) {
1937 /*
1938 * XXX do we need a memory barrier of sorts
1939 * wrt to rcu_dereference() of perf_event_ctxp
1940 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001941 task->perf_event_ctxp[ctxn] = next_ctx;
1942 next->perf_event_ctxp[ctxn] = ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001943 ctx->task = next;
1944 next_ctx->task = task;
1945 do_switch = 0;
1946
1947 perf_event_sync_stat(ctx, next_ctx);
1948 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001949 raw_spin_unlock(&next_ctx->lock);
1950 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001951 }
1952 rcu_read_unlock();
1953
1954 if (do_switch) {
Peter Zijlstrafacc4302011-04-09 21:17:42 +02001955 raw_spin_lock(&ctx->lock);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001956 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001957 cpuctx->task_ctx = NULL;
Peter Zijlstrafacc4302011-04-09 21:17:42 +02001958 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001959 }
1960}
1961
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001962#define for_each_task_context_nr(ctxn) \
1963 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
1964
1965/*
1966 * Called from scheduler to remove the events of the current task,
1967 * with interrupts disabled.
1968 *
1969 * We stop each event and update the event value in event->count.
1970 *
1971 * This does not protect us against NMI, but disable()
1972 * sets the disabled bit in the control field of event _before_
1973 * accessing the event control register. If a NMI hits, then it will
1974 * not restart the event.
1975 */
Peter Zijlstra82cd6de2010-10-14 17:57:23 +02001976void __perf_event_task_sched_out(struct task_struct *task,
1977 struct task_struct *next)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001978{
1979 int ctxn;
1980
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001981 for_each_task_context_nr(ctxn)
1982 perf_event_context_sched_out(task, ctxn, next);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001983
1984 /*
1985 * if cgroup events exist on this CPU, then we need
1986 * to check if we have to switch out PMU state.
1987 * cgroup event are system-wide mode only
1988 */
1989 if (atomic_read(&__get_cpu_var(perf_cgroup_events)))
1990 perf_cgroup_sched_out(task);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001991}
1992
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02001993static void task_ctx_sched_out(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001994{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001995 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001996
1997 if (!cpuctx->task_ctx)
1998 return;
1999
2000 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
2001 return;
2002
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02002003 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002004 cpuctx->task_ctx = NULL;
2005}
2006
2007/*
2008 * Called with IRQs disabled
2009 */
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002010static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
2011 enum event_type_t event_type)
2012{
2013 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002014}
2015
2016static void
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002017ctx_pinned_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01002018 struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002019{
2020 struct perf_event *event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002021
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002022 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
2023 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002024 continue;
Stephane Eranian5632ab12011-01-03 18:20:01 +02002025 if (!event_filter_match(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002026 continue;
2027
Stephane Eraniane5d13672011-02-14 11:20:01 +02002028 /* may need to reset tstamp_enabled */
2029 if (is_cgroup_event(event))
2030 perf_cgroup_mark_enabled(event, ctx);
2031
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08002032 if (group_can_go_on(event, cpuctx, 1))
Peter Zijlstra6e377382010-02-11 13:21:58 +01002033 group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002034
2035 /*
2036 * If this pinned group hasn't been scheduled,
2037 * put it in error state.
2038 */
2039 if (event->state == PERF_EVENT_STATE_INACTIVE) {
2040 update_group_times(event);
2041 event->state = PERF_EVENT_STATE_ERROR;
2042 }
2043 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002044}
2045
2046static void
2047ctx_flexible_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01002048 struct perf_cpu_context *cpuctx)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002049{
2050 struct perf_event *event;
2051 int can_add_hw = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002052
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002053 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
2054 /* Ignore events in OFF or ERROR state */
2055 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002056 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002057 /*
2058 * Listen to the 'cpu' scheduling filter constraint
2059 * of events:
2060 */
Stephane Eranian5632ab12011-01-03 18:20:01 +02002061 if (!event_filter_match(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002062 continue;
2063
Stephane Eraniane5d13672011-02-14 11:20:01 +02002064 /* may need to reset tstamp_enabled */
2065 if (is_cgroup_event(event))
2066 perf_cgroup_mark_enabled(event, ctx);
2067
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002068 if (group_can_go_on(event, cpuctx, can_add_hw)) {
Peter Zijlstra6e377382010-02-11 13:21:58 +01002069 if (group_sched_in(event, cpuctx, ctx))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002070 can_add_hw = 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002071 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002072 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002073}
2074
2075static void
2076ctx_sched_in(struct perf_event_context *ctx,
2077 struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +02002078 enum event_type_t event_type,
2079 struct task_struct *task)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002080{
Stephane Eraniane5d13672011-02-14 11:20:01 +02002081 u64 now;
Peter Zijlstradb24d332011-04-09 21:17:45 +02002082 int is_active = ctx->is_active;
Stephane Eraniane5d13672011-02-14 11:20:01 +02002083
Peter Zijlstradb24d332011-04-09 21:17:45 +02002084 ctx->is_active |= event_type;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002085 if (likely(!ctx->nr_events))
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002086 return;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002087
Stephane Eraniane5d13672011-02-14 11:20:01 +02002088 now = perf_clock();
2089 ctx->timestamp = now;
Stephane Eranian3f7cce32011-02-18 14:40:01 +02002090 perf_cgroup_set_timestamp(task, ctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002091 /*
2092 * First go through the list and put on any pinned groups
2093 * in order to give them the best chance of going on.
2094 */
Peter Zijlstradb24d332011-04-09 21:17:45 +02002095 if (!(is_active & EVENT_PINNED) && (event_type & EVENT_PINNED))
Peter Zijlstra6e377382010-02-11 13:21:58 +01002096 ctx_pinned_sched_in(ctx, cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002097
2098 /* Then walk through the lower prio flexible groups */
Peter Zijlstradb24d332011-04-09 21:17:45 +02002099 if (!(is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE))
Peter Zijlstra6e377382010-02-11 13:21:58 +01002100 ctx_flexible_sched_in(ctx, cpuctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002101}
2102
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002103static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +02002104 enum event_type_t event_type,
2105 struct task_struct *task)
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002106{
2107 struct perf_event_context *ctx = &cpuctx->ctx;
2108
Stephane Eraniane5d13672011-02-14 11:20:01 +02002109 ctx_sched_in(ctx, cpuctx, event_type, task);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002110}
2111
Stephane Eraniane5d13672011-02-14 11:20:01 +02002112static void perf_event_context_sched_in(struct perf_event_context *ctx,
2113 struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002114{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002115 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002116
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002117 cpuctx = __get_cpu_context(ctx);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002118 if (cpuctx->task_ctx == ctx)
2119 return;
2120
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002121 perf_ctx_lock(cpuctx, ctx);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02002122 perf_pmu_disable(ctx->pmu);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002123 /*
2124 * We want to keep the following priority order:
2125 * cpu pinned (that don't need to move), task pinned,
2126 * cpu flexible, task flexible.
2127 */
2128 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
2129
Peter Zijlstradce58552011-04-09 21:17:46 +02002130 perf_event_sched_in(cpuctx, ctx, task);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002131
2132 cpuctx->task_ctx = ctx;
eranian@google.com9b33fa62010-03-10 22:26:05 -08002133
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002134 perf_pmu_enable(ctx->pmu);
2135 perf_ctx_unlock(cpuctx, ctx);
2136
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002137 /*
2138 * Since these rotations are per-cpu, we need to ensure the
2139 * cpu-context we got scheduled on is actually rotating.
2140 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002141 perf_pmu_rotate_start(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002142}
2143
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002144/*
2145 * Called from scheduler to add the events of the current task
2146 * with interrupts disabled.
2147 *
2148 * We restore the event value and then enable it.
2149 *
2150 * This does not protect us against NMI, but enable()
2151 * sets the enabled bit in the control field of event _before_
2152 * accessing the event control register. If a NMI hits, then it will
2153 * keep the event running.
2154 */
Peter Zijlstra82cd6de2010-10-14 17:57:23 +02002155void __perf_event_task_sched_in(struct task_struct *task)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002156{
2157 struct perf_event_context *ctx;
2158 int ctxn;
2159
2160 for_each_task_context_nr(ctxn) {
2161 ctx = task->perf_event_ctxp[ctxn];
2162 if (likely(!ctx))
2163 continue;
2164
Stephane Eraniane5d13672011-02-14 11:20:01 +02002165 perf_event_context_sched_in(ctx, task);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002166 }
Stephane Eraniane5d13672011-02-14 11:20:01 +02002167 /*
2168 * if cgroup events exist on this CPU, then we need
2169 * to check if we have to switch in PMU state.
2170 * cgroup event are system-wide mode only
2171 */
2172 if (atomic_read(&__get_cpu_var(perf_cgroup_events)))
2173 perf_cgroup_sched_in(task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002174}
2175
Peter Zijlstraabd50712010-01-26 18:50:16 +01002176static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
2177{
2178 u64 frequency = event->attr.sample_freq;
2179 u64 sec = NSEC_PER_SEC;
2180 u64 divisor, dividend;
2181
2182 int count_fls, nsec_fls, frequency_fls, sec_fls;
2183
2184 count_fls = fls64(count);
2185 nsec_fls = fls64(nsec);
2186 frequency_fls = fls64(frequency);
2187 sec_fls = 30;
2188
2189 /*
2190 * We got @count in @nsec, with a target of sample_freq HZ
2191 * the target period becomes:
2192 *
2193 * @count * 10^9
2194 * period = -------------------
2195 * @nsec * sample_freq
2196 *
2197 */
2198
2199 /*
2200 * Reduce accuracy by one bit such that @a and @b converge
2201 * to a similar magnitude.
2202 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002203#define REDUCE_FLS(a, b) \
Peter Zijlstraabd50712010-01-26 18:50:16 +01002204do { \
2205 if (a##_fls > b##_fls) { \
2206 a >>= 1; \
2207 a##_fls--; \
2208 } else { \
2209 b >>= 1; \
2210 b##_fls--; \
2211 } \
2212} while (0)
2213
2214 /*
2215 * Reduce accuracy until either term fits in a u64, then proceed with
2216 * the other, so that finally we can do a u64/u64 division.
2217 */
2218 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
2219 REDUCE_FLS(nsec, frequency);
2220 REDUCE_FLS(sec, count);
2221 }
2222
2223 if (count_fls + sec_fls > 64) {
2224 divisor = nsec * frequency;
2225
2226 while (count_fls + sec_fls > 64) {
2227 REDUCE_FLS(count, sec);
2228 divisor >>= 1;
2229 }
2230
2231 dividend = count * sec;
2232 } else {
2233 dividend = count * sec;
2234
2235 while (nsec_fls + frequency_fls > 64) {
2236 REDUCE_FLS(nsec, frequency);
2237 dividend >>= 1;
2238 }
2239
2240 divisor = nsec * frequency;
2241 }
2242
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02002243 if (!divisor)
2244 return dividend;
2245
Peter Zijlstraabd50712010-01-26 18:50:16 +01002246 return div64_u64(dividend, divisor);
2247}
2248
2249static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002250{
2251 struct hw_perf_event *hwc = &event->hw;
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02002252 s64 period, sample_period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002253 s64 delta;
2254
Peter Zijlstraabd50712010-01-26 18:50:16 +01002255 period = perf_calculate_period(event, nsec, count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002256
2257 delta = (s64)(period - hwc->sample_period);
2258 delta = (delta + 7) / 8; /* low pass filter */
2259
2260 sample_period = hwc->sample_period + delta;
2261
2262 if (!sample_period)
2263 sample_period = 1;
2264
2265 hwc->sample_period = sample_period;
Peter Zijlstraabd50712010-01-26 18:50:16 +01002266
Peter Zijlstrae7850592010-05-21 14:43:08 +02002267 if (local64_read(&hwc->period_left) > 8*sample_period) {
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02002268 event->pmu->stop(event, PERF_EF_UPDATE);
Peter Zijlstrae7850592010-05-21 14:43:08 +02002269 local64_set(&hwc->period_left, 0);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02002270 event->pmu->start(event, PERF_EF_RELOAD);
Peter Zijlstraabd50712010-01-26 18:50:16 +01002271 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002272}
2273
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002274static void perf_ctx_adjust_freq(struct perf_event_context *ctx, u64 period)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002275{
2276 struct perf_event *event;
2277 struct hw_perf_event *hwc;
Peter Zijlstraabd50712010-01-26 18:50:16 +01002278 u64 interrupts, now;
2279 s64 delta;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002280
Paul Mackerras03541f82009-10-14 16:58:03 +11002281 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002282 if (event->state != PERF_EVENT_STATE_ACTIVE)
2283 continue;
2284
Stephane Eranian5632ab12011-01-03 18:20:01 +02002285 if (!event_filter_match(event))
Peter Zijlstra5d27c232009-12-17 13:16:32 +01002286 continue;
2287
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002288 hwc = &event->hw;
2289
2290 interrupts = hwc->interrupts;
2291 hwc->interrupts = 0;
2292
2293 /*
2294 * unthrottle events on the tick
2295 */
2296 if (interrupts == MAX_INTERRUPTS) {
2297 perf_log_throttle(event, 1);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02002298 event->pmu->start(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002299 }
2300
2301 if (!event->attr.freq || !event->attr.sample_freq)
2302 continue;
2303
Peter Zijlstraabd50712010-01-26 18:50:16 +01002304 event->pmu->read(event);
Peter Zijlstrae7850592010-05-21 14:43:08 +02002305 now = local64_read(&event->count);
Peter Zijlstraabd50712010-01-26 18:50:16 +01002306 delta = now - hwc->freq_count_stamp;
2307 hwc->freq_count_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002308
Peter Zijlstraabd50712010-01-26 18:50:16 +01002309 if (delta > 0)
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002310 perf_adjust_period(event, period, delta);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002311 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002312}
2313
2314/*
2315 * Round-robin a context's events:
2316 */
2317static void rotate_ctx(struct perf_event_context *ctx)
2318{
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01002319 /*
2320 * Rotate the first entry last of non-pinned groups. Rotation might be
2321 * disabled by the inheritance code.
2322 */
2323 if (!ctx->rotate_disable)
2324 list_rotate_left(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002325}
2326
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002327/*
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002328 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
2329 * because they're strictly cpu affine and rotate_start is called with IRQs
2330 * disabled, while rotate_context is called from IRQ context.
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002331 */
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002332static void perf_rotate_context(struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002333{
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002334 u64 interval = (u64)cpuctx->jiffies_interval * TICK_NSEC;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002335 struct perf_event_context *ctx = NULL;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002336 int rotate = 0, remove = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002337
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002338 if (cpuctx->ctx.nr_events) {
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002339 remove = 0;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002340 if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
2341 rotate = 1;
2342 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002343
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002344 ctx = cpuctx->task_ctx;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002345 if (ctx && ctx->nr_events) {
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002346 remove = 0;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002347 if (ctx->nr_events != ctx->nr_active)
2348 rotate = 1;
2349 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002350
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002351 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02002352 perf_pmu_disable(cpuctx->ctx.pmu);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002353 perf_ctx_adjust_freq(&cpuctx->ctx, interval);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002354 if (ctx)
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002355 perf_ctx_adjust_freq(ctx, interval);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002356
Peter Zijlstrad4944a02010-03-08 13:51:20 +01002357 if (!rotate)
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002358 goto done;
Peter Zijlstrad4944a02010-03-08 13:51:20 +01002359
Frederic Weisbecker7defb0f2010-01-17 12:15:31 +01002360 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002361 if (ctx)
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02002362 ctx_sched_out(ctx, cpuctx, EVENT_FLEXIBLE);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002363
2364 rotate_ctx(&cpuctx->ctx);
2365 if (ctx)
2366 rotate_ctx(ctx);
2367
Peter Zijlstradce58552011-04-09 21:17:46 +02002368 perf_event_sched_in(cpuctx, ctx, current);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002369
2370done:
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002371 if (remove)
2372 list_del_init(&cpuctx->rotation_list);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002373
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002374 perf_pmu_enable(cpuctx->ctx.pmu);
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002375 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002376}
2377
2378void perf_event_task_tick(void)
2379{
2380 struct list_head *head = &__get_cpu_var(rotation_list);
2381 struct perf_cpu_context *cpuctx, *tmp;
2382
2383 WARN_ON(!irqs_disabled());
2384
2385 list_for_each_entry_safe(cpuctx, tmp, head, rotation_list) {
2386 if (cpuctx->jiffies_interval == 1 ||
2387 !(jiffies % cpuctx->jiffies_interval))
2388 perf_rotate_context(cpuctx);
2389 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002390}
2391
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002392static int event_enable_on_exec(struct perf_event *event,
2393 struct perf_event_context *ctx)
2394{
2395 if (!event->attr.enable_on_exec)
2396 return 0;
2397
2398 event->attr.enable_on_exec = 0;
2399 if (event->state >= PERF_EVENT_STATE_INACTIVE)
2400 return 0;
2401
2402 __perf_event_mark_enabled(event, ctx);
2403
2404 return 1;
2405}
2406
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002407/*
2408 * Enable all of a task's events that have been marked enable-on-exec.
2409 * This expects task == current.
2410 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002411static void perf_event_enable_on_exec(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002412{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002413 struct perf_event *event;
2414 unsigned long flags;
2415 int enabled = 0;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002416 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002417
2418 local_irq_save(flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002419 if (!ctx || !ctx->nr_events)
2420 goto out;
2421
Stephane Eraniane566b762011-04-06 02:54:54 +02002422 /*
2423 * We must ctxsw out cgroup events to avoid conflict
2424 * when invoking perf_task_event_sched_in() later on
2425 * in this function. Otherwise we end up trying to
2426 * ctxswin cgroup events which are already scheduled
2427 * in.
2428 */
2429 perf_cgroup_sched_out(current);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002430
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002431 raw_spin_lock(&ctx->lock);
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02002432 task_ctx_sched_out(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002433
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002434 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
2435 ret = event_enable_on_exec(event, ctx);
2436 if (ret)
2437 enabled = 1;
2438 }
2439
2440 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
2441 ret = event_enable_on_exec(event, ctx);
2442 if (ret)
2443 enabled = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002444 }
2445
2446 /*
2447 * Unclone this context if we enabled any event.
2448 */
2449 if (enabled)
2450 unclone_ctx(ctx);
2451
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002452 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002453
Stephane Eraniane566b762011-04-06 02:54:54 +02002454 /*
2455 * Also calls ctxswin for cgroup events, if any:
2456 */
Stephane Eraniane5d13672011-02-14 11:20:01 +02002457 perf_event_context_sched_in(ctx, ctx->task);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002458out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002459 local_irq_restore(flags);
2460}
2461
2462/*
2463 * Cross CPU call to read the hardware event
2464 */
2465static void __perf_event_read(void *info)
2466{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002467 struct perf_event *event = info;
2468 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002469 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002470
2471 /*
2472 * If this is a task context, we need to check whether it is
2473 * the current task context of this cpu. If not it has been
2474 * scheduled out before the smp call arrived. In that case
2475 * event->count would have been updated to a recent sample
2476 * when the event was scheduled out.
2477 */
2478 if (ctx->task && cpuctx->task_ctx != ctx)
2479 return;
2480
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002481 raw_spin_lock(&ctx->lock);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002482 if (ctx->is_active) {
Peter Zijlstra542e72f2011-01-26 15:38:35 +01002483 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002484 update_cgrp_time_from_event(event);
2485 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002486 update_event_times(event);
Peter Zijlstra542e72f2011-01-26 15:38:35 +01002487 if (event->state == PERF_EVENT_STATE_ACTIVE)
2488 event->pmu->read(event);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002489 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002490}
2491
Peter Zijlstrab5e58792010-05-21 14:43:12 +02002492static inline u64 perf_event_count(struct perf_event *event)
2493{
Peter Zijlstrae7850592010-05-21 14:43:08 +02002494 return local64_read(&event->count) + atomic64_read(&event->child_count);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02002495}
2496
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002497static u64 perf_event_read(struct perf_event *event)
2498{
2499 /*
2500 * If event is enabled and currently active on a CPU, update the
2501 * value in the event structure:
2502 */
2503 if (event->state == PERF_EVENT_STATE_ACTIVE) {
2504 smp_call_function_single(event->oncpu,
2505 __perf_event_read, event, 1);
2506 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01002507 struct perf_event_context *ctx = event->ctx;
2508 unsigned long flags;
2509
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002510 raw_spin_lock_irqsave(&ctx->lock, flags);
Stephane Eranianc530ccd2010-10-15 15:26:01 +02002511 /*
2512 * may read while context is not active
2513 * (e.g., thread is blocked), in that case
2514 * we cannot update context time
2515 */
Stephane Eraniane5d13672011-02-14 11:20:01 +02002516 if (ctx->is_active) {
Stephane Eranianc530ccd2010-10-15 15:26:01 +02002517 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002518 update_cgrp_time_from_event(event);
2519 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002520 update_event_times(event);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002521 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002522 }
2523
Peter Zijlstrab5e58792010-05-21 14:43:12 +02002524 return perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002525}
2526
2527/*
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02002528 * Callchain support
2529 */
2530
2531struct callchain_cpus_entries {
2532 struct rcu_head rcu_head;
2533 struct perf_callchain_entry *cpu_entries[0];
2534};
2535
Frederic Weisbecker7ae07ea2010-08-14 20:45:13 +02002536static DEFINE_PER_CPU(int, callchain_recursion[PERF_NR_CONTEXTS]);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02002537static atomic_t nr_callchain_events;
2538static DEFINE_MUTEX(callchain_mutex);
2539struct callchain_cpus_entries *callchain_cpus_entries;
2540
2541
2542__weak void perf_callchain_kernel(struct perf_callchain_entry *entry,
2543 struct pt_regs *regs)
2544{
2545}
2546
2547__weak void perf_callchain_user(struct perf_callchain_entry *entry,
2548 struct pt_regs *regs)
2549{
2550}
2551
2552static void release_callchain_buffers_rcu(struct rcu_head *head)
2553{
2554 struct callchain_cpus_entries *entries;
2555 int cpu;
2556
2557 entries = container_of(head, struct callchain_cpus_entries, rcu_head);
2558
2559 for_each_possible_cpu(cpu)
2560 kfree(entries->cpu_entries[cpu]);
2561
2562 kfree(entries);
2563}
2564
2565static void release_callchain_buffers(void)
2566{
2567 struct callchain_cpus_entries *entries;
2568
2569 entries = callchain_cpus_entries;
2570 rcu_assign_pointer(callchain_cpus_entries, NULL);
2571 call_rcu(&entries->rcu_head, release_callchain_buffers_rcu);
2572}
2573
2574static int alloc_callchain_buffers(void)
2575{
2576 int cpu;
2577 int size;
2578 struct callchain_cpus_entries *entries;
2579
2580 /*
2581 * We can't use the percpu allocation API for data that can be
2582 * accessed from NMI. Use a temporary manual per cpu allocation
2583 * until that gets sorted out.
2584 */
Eric Dumazet88d4f0d2011-01-25 19:40:51 +01002585 size = offsetof(struct callchain_cpus_entries, cpu_entries[nr_cpu_ids]);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02002586
2587 entries = kzalloc(size, GFP_KERNEL);
2588 if (!entries)
2589 return -ENOMEM;
2590
Frederic Weisbecker7ae07ea2010-08-14 20:45:13 +02002591 size = sizeof(struct perf_callchain_entry) * PERF_NR_CONTEXTS;
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02002592
2593 for_each_possible_cpu(cpu) {
2594 entries->cpu_entries[cpu] = kmalloc_node(size, GFP_KERNEL,
2595 cpu_to_node(cpu));
2596 if (!entries->cpu_entries[cpu])
2597 goto fail;
2598 }
2599
2600 rcu_assign_pointer(callchain_cpus_entries, entries);
2601
2602 return 0;
2603
2604fail:
2605 for_each_possible_cpu(cpu)
2606 kfree(entries->cpu_entries[cpu]);
2607 kfree(entries);
2608
2609 return -ENOMEM;
2610}
2611
2612static int get_callchain_buffers(void)
2613{
2614 int err = 0;
2615 int count;
2616
2617 mutex_lock(&callchain_mutex);
2618
2619 count = atomic_inc_return(&nr_callchain_events);
2620 if (WARN_ON_ONCE(count < 1)) {
2621 err = -EINVAL;
2622 goto exit;
2623 }
2624
2625 if (count > 1) {
2626 /* If the allocation failed, give up */
2627 if (!callchain_cpus_entries)
2628 err = -ENOMEM;
2629 goto exit;
2630 }
2631
2632 err = alloc_callchain_buffers();
2633 if (err)
2634 release_callchain_buffers();
2635exit:
2636 mutex_unlock(&callchain_mutex);
2637
2638 return err;
2639}
2640
2641static void put_callchain_buffers(void)
2642{
2643 if (atomic_dec_and_mutex_lock(&nr_callchain_events, &callchain_mutex)) {
2644 release_callchain_buffers();
2645 mutex_unlock(&callchain_mutex);
2646 }
2647}
2648
2649static int get_recursion_context(int *recursion)
2650{
2651 int rctx;
2652
2653 if (in_nmi())
2654 rctx = 3;
2655 else if (in_irq())
2656 rctx = 2;
2657 else if (in_softirq())
2658 rctx = 1;
2659 else
2660 rctx = 0;
2661
2662 if (recursion[rctx])
2663 return -1;
2664
2665 recursion[rctx]++;
2666 barrier();
2667
2668 return rctx;
2669}
2670
2671static inline void put_recursion_context(int *recursion, int rctx)
2672{
2673 barrier();
2674 recursion[rctx]--;
2675}
2676
2677static struct perf_callchain_entry *get_callchain_entry(int *rctx)
2678{
2679 int cpu;
2680 struct callchain_cpus_entries *entries;
2681
2682 *rctx = get_recursion_context(__get_cpu_var(callchain_recursion));
2683 if (*rctx == -1)
2684 return NULL;
2685
2686 entries = rcu_dereference(callchain_cpus_entries);
2687 if (!entries)
2688 return NULL;
2689
2690 cpu = smp_processor_id();
2691
2692 return &entries->cpu_entries[cpu][*rctx];
2693}
2694
2695static void
2696put_callchain_entry(int rctx)
2697{
2698 put_recursion_context(__get_cpu_var(callchain_recursion), rctx);
2699}
2700
2701static struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
2702{
2703 int rctx;
2704 struct perf_callchain_entry *entry;
2705
2706
2707 entry = get_callchain_entry(&rctx);
2708 if (rctx == -1)
2709 return NULL;
2710
2711 if (!entry)
2712 goto exit_put;
2713
2714 entry->nr = 0;
2715
2716 if (!user_mode(regs)) {
2717 perf_callchain_store(entry, PERF_CONTEXT_KERNEL);
2718 perf_callchain_kernel(entry, regs);
2719 if (current->mm)
2720 regs = task_pt_regs(current);
2721 else
2722 regs = NULL;
2723 }
2724
2725 if (regs) {
2726 perf_callchain_store(entry, PERF_CONTEXT_USER);
2727 perf_callchain_user(entry, regs);
2728 }
2729
2730exit_put:
2731 put_callchain_entry(rctx);
2732
2733 return entry;
2734}
2735
2736/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002737 * Initialize the perf_event context in a task_struct:
2738 */
Peter Zijlstraeb184472010-09-07 15:55:13 +02002739static void __perf_event_init_context(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002740{
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002741 raw_spin_lock_init(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002742 mutex_init(&ctx->mutex);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002743 INIT_LIST_HEAD(&ctx->pinned_groups);
2744 INIT_LIST_HEAD(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002745 INIT_LIST_HEAD(&ctx->event_list);
2746 atomic_set(&ctx->refcount, 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002747}
2748
Peter Zijlstraeb184472010-09-07 15:55:13 +02002749static struct perf_event_context *
2750alloc_perf_context(struct pmu *pmu, struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002751{
2752 struct perf_event_context *ctx;
Peter Zijlstraeb184472010-09-07 15:55:13 +02002753
2754 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
2755 if (!ctx)
2756 return NULL;
2757
2758 __perf_event_init_context(ctx);
2759 if (task) {
2760 ctx->task = task;
2761 get_task_struct(task);
2762 }
2763 ctx->pmu = pmu;
2764
2765 return ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002766}
2767
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07002768static struct task_struct *
2769find_lively_task_by_vpid(pid_t vpid)
2770{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002771 struct task_struct *task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002772 int err;
2773
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002774 rcu_read_lock();
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07002775 if (!vpid)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002776 task = current;
2777 else
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07002778 task = find_task_by_vpid(vpid);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002779 if (task)
2780 get_task_struct(task);
2781 rcu_read_unlock();
2782
2783 if (!task)
2784 return ERR_PTR(-ESRCH);
2785
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002786 /* Reuse ptrace permission checks for now. */
2787 err = -EACCES;
2788 if (!ptrace_may_access(task, PTRACE_MODE_READ))
2789 goto errout;
2790
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07002791 return task;
2792errout:
2793 put_task_struct(task);
2794 return ERR_PTR(err);
2795
2796}
2797
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002798/*
2799 * Returns a matching context with refcount and pincount.
2800 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002801static struct perf_event_context *
Matt Helsley38a81da2010-09-13 13:01:20 -07002802find_get_context(struct pmu *pmu, struct task_struct *task, int cpu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002803{
2804 struct perf_event_context *ctx;
2805 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002806 unsigned long flags;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002807 int ctxn, err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002808
Oleg Nesterov22a4ec72011-01-18 17:10:08 +01002809 if (!task) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002810 /* Must be root to operate on a CPU event: */
2811 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
2812 return ERR_PTR(-EACCES);
2813
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002814 /*
2815 * We could be clever and allow to attach a event to an
2816 * offline CPU and activate it when the CPU comes up, but
2817 * that's for later.
2818 */
2819 if (!cpu_online(cpu))
2820 return ERR_PTR(-ENODEV);
2821
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002822 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002823 ctx = &cpuctx->ctx;
2824 get_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002825 ++ctx->pin_count;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002826
2827 return ctx;
2828 }
2829
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002830 err = -EINVAL;
2831 ctxn = pmu->task_ctx_nr;
2832 if (ctxn < 0)
2833 goto errout;
2834
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002835retry:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002836 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002837 if (ctx) {
2838 unclone_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002839 ++ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002840 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Peter Zijlstra9137fb22011-04-09 21:17:41 +02002841 } else {
Peter Zijlstraeb184472010-09-07 15:55:13 +02002842 ctx = alloc_perf_context(pmu, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002843 err = -ENOMEM;
2844 if (!ctx)
2845 goto errout;
Peter Zijlstraeb184472010-09-07 15:55:13 +02002846
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01002847 err = 0;
2848 mutex_lock(&task->perf_event_mutex);
2849 /*
2850 * If it has already passed perf_event_exit_task().
2851 * we must see PF_EXITING, it takes this mutex too.
2852 */
2853 if (task->flags & PF_EXITING)
2854 err = -ESRCH;
2855 else if (task->perf_event_ctxp[ctxn])
2856 err = -EAGAIN;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002857 else {
Peter Zijlstra9137fb22011-04-09 21:17:41 +02002858 get_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002859 ++ctx->pin_count;
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01002860 rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002861 }
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01002862 mutex_unlock(&task->perf_event_mutex);
2863
2864 if (unlikely(err)) {
Peter Zijlstra9137fb22011-04-09 21:17:41 +02002865 put_ctx(ctx);
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01002866
2867 if (err == -EAGAIN)
2868 goto retry;
2869 goto errout;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002870 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002871 }
2872
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002873 return ctx;
2874
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002875errout:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002876 return ERR_PTR(err);
2877}
2878
Li Zefan6fb29152009-10-15 11:21:42 +08002879static void perf_event_free_filter(struct perf_event *event);
2880
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002881static void free_event_rcu(struct rcu_head *head)
2882{
2883 struct perf_event *event;
2884
2885 event = container_of(head, struct perf_event, rcu_head);
2886 if (event->ns)
2887 put_pid_ns(event->ns);
Li Zefan6fb29152009-10-15 11:21:42 +08002888 perf_event_free_filter(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002889 kfree(event);
2890}
2891
Frederic Weisbecker76369132011-05-19 19:55:04 +02002892static void ring_buffer_put(struct ring_buffer *rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002893
2894static void free_event(struct perf_event *event)
2895{
Peter Zijlstrae360adb2010-10-14 14:01:34 +08002896 irq_work_sync(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002897
2898 if (!event->parent) {
Peter Zijlstra82cd6de2010-10-14 17:57:23 +02002899 if (event->attach_state & PERF_ATTACH_TASK)
Stephane Eraniane5d13672011-02-14 11:20:01 +02002900 jump_label_dec(&perf_sched_events);
Eric B Munson3af9e852010-05-18 15:30:49 +01002901 if (event->attr.mmap || event->attr.mmap_data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002902 atomic_dec(&nr_mmap_events);
2903 if (event->attr.comm)
2904 atomic_dec(&nr_comm_events);
2905 if (event->attr.task)
2906 atomic_dec(&nr_task_events);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02002907 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
2908 put_callchain_buffers();
Peter Zijlstra08309372011-03-03 11:31:20 +01002909 if (is_cgroup_event(event)) {
2910 atomic_dec(&per_cpu(perf_cgroup_events, event->cpu));
2911 jump_label_dec(&perf_sched_events);
2912 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002913 }
2914
Frederic Weisbecker76369132011-05-19 19:55:04 +02002915 if (event->rb) {
2916 ring_buffer_put(event->rb);
2917 event->rb = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002918 }
2919
Stephane Eraniane5d13672011-02-14 11:20:01 +02002920 if (is_cgroup_event(event))
2921 perf_detach_cgroup(event);
2922
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002923 if (event->destroy)
2924 event->destroy(event);
2925
Peter Zijlstra0c67b402010-09-13 11:15:58 +02002926 if (event->ctx)
2927 put_ctx(event->ctx);
2928
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002929 call_rcu(&event->rcu_head, free_event_rcu);
2930}
2931
Arjan van de Venfb0459d2009-09-25 12:25:56 +02002932int perf_event_release_kernel(struct perf_event *event)
2933{
2934 struct perf_event_context *ctx = event->ctx;
2935
2936 WARN_ON_ONCE(ctx->parent_ctx);
Peter Zijlstraa0507c82010-05-06 15:42:53 +02002937 /*
2938 * There are two ways this annotation is useful:
2939 *
2940 * 1) there is a lock recursion from perf_event_exit_task
2941 * see the comment there.
2942 *
2943 * 2) there is a lock-inversion with mmap_sem through
2944 * perf_event_read_group(), which takes faults while
2945 * holding ctx->mutex, however this is called after
2946 * the last filedesc died, so there is no possibility
2947 * to trigger the AB-BA case.
2948 */
2949 mutex_lock_nested(&ctx->mutex, SINGLE_DEPTH_NESTING);
Peter Zijlstra050735b2010-05-11 11:51:53 +02002950 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra8a495422010-05-27 15:47:49 +02002951 perf_group_detach(event);
Peter Zijlstra050735b2010-05-11 11:51:53 +02002952 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstrae03a9a52011-04-09 21:17:47 +02002953 perf_remove_from_context(event);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02002954 mutex_unlock(&ctx->mutex);
2955
Arjan van de Venfb0459d2009-09-25 12:25:56 +02002956 free_event(event);
2957
2958 return 0;
2959}
2960EXPORT_SYMBOL_GPL(perf_event_release_kernel);
2961
Peter Zijlstraa66a3052009-11-23 11:37:23 +01002962/*
2963 * Called when the last reference to the file is gone.
2964 */
2965static int perf_release(struct inode *inode, struct file *file)
2966{
2967 struct perf_event *event = file->private_data;
Peter Zijlstra88821352010-11-09 19:01:43 +01002968 struct task_struct *owner;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01002969
2970 file->private_data = NULL;
2971
Peter Zijlstra88821352010-11-09 19:01:43 +01002972 rcu_read_lock();
2973 owner = ACCESS_ONCE(event->owner);
2974 /*
2975 * Matches the smp_wmb() in perf_event_exit_task(). If we observe
2976 * !owner it means the list deletion is complete and we can indeed
2977 * free this event, otherwise we need to serialize on
2978 * owner->perf_event_mutex.
2979 */
2980 smp_read_barrier_depends();
2981 if (owner) {
2982 /*
2983 * Since delayed_put_task_struct() also drops the last
2984 * task reference we can safely take a new reference
2985 * while holding the rcu_read_lock().
2986 */
2987 get_task_struct(owner);
2988 }
2989 rcu_read_unlock();
2990
2991 if (owner) {
2992 mutex_lock(&owner->perf_event_mutex);
2993 /*
2994 * We have to re-check the event->owner field, if it is cleared
2995 * we raced with perf_event_exit_task(), acquiring the mutex
2996 * ensured they're done, and we can proceed with freeing the
2997 * event.
2998 */
2999 if (event->owner)
3000 list_del_init(&event->owner_entry);
3001 mutex_unlock(&owner->perf_event_mutex);
3002 put_task_struct(owner);
3003 }
3004
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003005 return perf_event_release_kernel(event);
3006}
3007
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003008u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003009{
3010 struct perf_event *child;
3011 u64 total = 0;
3012
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003013 *enabled = 0;
3014 *running = 0;
3015
Peter Zijlstra6f105812009-11-20 22:19:56 +01003016 mutex_lock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003017 total += perf_event_read(event);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003018 *enabled += event->total_time_enabled +
3019 atomic64_read(&event->child_total_time_enabled);
3020 *running += event->total_time_running +
3021 atomic64_read(&event->child_total_time_running);
3022
3023 list_for_each_entry(child, &event->child_list, child_list) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003024 total += perf_event_read(child);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003025 *enabled += child->total_time_enabled;
3026 *running += child->total_time_running;
3027 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01003028 mutex_unlock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003029
3030 return total;
3031}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003032EXPORT_SYMBOL_GPL(perf_event_read_value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003033
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003034static int perf_event_read_group(struct perf_event *event,
3035 u64 read_format, char __user *buf)
3036{
3037 struct perf_event *leader = event->group_leader, *sub;
Peter Zijlstra6f105812009-11-20 22:19:56 +01003038 int n = 0, size = 0, ret = -EFAULT;
3039 struct perf_event_context *ctx = leader->ctx;
Peter Zijlstraabf48682009-11-20 22:19:49 +01003040 u64 values[5];
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003041 u64 count, enabled, running;
Peter Zijlstraabf48682009-11-20 22:19:49 +01003042
Peter Zijlstra6f105812009-11-20 22:19:56 +01003043 mutex_lock(&ctx->mutex);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003044 count = perf_event_read_value(leader, &enabled, &running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003045
3046 values[n++] = 1 + leader->nr_siblings;
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003047 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3048 values[n++] = enabled;
3049 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3050 values[n++] = running;
Peter Zijlstraabf48682009-11-20 22:19:49 +01003051 values[n++] = count;
3052 if (read_format & PERF_FORMAT_ID)
3053 values[n++] = primary_event_id(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003054
3055 size = n * sizeof(u64);
3056
3057 if (copy_to_user(buf, values, size))
Peter Zijlstra6f105812009-11-20 22:19:56 +01003058 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003059
Peter Zijlstra6f105812009-11-20 22:19:56 +01003060 ret = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003061
3062 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
Peter Zijlstraabf48682009-11-20 22:19:49 +01003063 n = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003064
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003065 values[n++] = perf_event_read_value(sub, &enabled, &running);
Peter Zijlstraabf48682009-11-20 22:19:49 +01003066 if (read_format & PERF_FORMAT_ID)
3067 values[n++] = primary_event_id(sub);
3068
3069 size = n * sizeof(u64);
3070
Stephane Eranian184d3da2009-11-23 21:40:49 -08003071 if (copy_to_user(buf + ret, values, size)) {
Peter Zijlstra6f105812009-11-20 22:19:56 +01003072 ret = -EFAULT;
3073 goto unlock;
3074 }
Peter Zijlstraabf48682009-11-20 22:19:49 +01003075
3076 ret += size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003077 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01003078unlock:
3079 mutex_unlock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003080
Peter Zijlstraabf48682009-11-20 22:19:49 +01003081 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003082}
3083
3084static int perf_event_read_one(struct perf_event *event,
3085 u64 read_format, char __user *buf)
3086{
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003087 u64 enabled, running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003088 u64 values[4];
3089 int n = 0;
3090
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003091 values[n++] = perf_event_read_value(event, &enabled, &running);
3092 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3093 values[n++] = enabled;
3094 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3095 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003096 if (read_format & PERF_FORMAT_ID)
3097 values[n++] = primary_event_id(event);
3098
3099 if (copy_to_user(buf, values, n * sizeof(u64)))
3100 return -EFAULT;
3101
3102 return n * sizeof(u64);
3103}
3104
3105/*
3106 * Read the performance event - simple non blocking version for now
3107 */
3108static ssize_t
3109perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
3110{
3111 u64 read_format = event->attr.read_format;
3112 int ret;
3113
3114 /*
3115 * Return end-of-file for a read on a event that is in
3116 * error state (i.e. because it was pinned but it couldn't be
3117 * scheduled on to the CPU at some point).
3118 */
3119 if (event->state == PERF_EVENT_STATE_ERROR)
3120 return 0;
3121
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02003122 if (count < event->read_size)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003123 return -ENOSPC;
3124
3125 WARN_ON_ONCE(event->ctx->parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003126 if (read_format & PERF_FORMAT_GROUP)
3127 ret = perf_event_read_group(event, read_format, buf);
3128 else
3129 ret = perf_event_read_one(event, read_format, buf);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003130
3131 return ret;
3132}
3133
3134static ssize_t
3135perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
3136{
3137 struct perf_event *event = file->private_data;
3138
3139 return perf_read_hw(event, buf, count);
3140}
3141
3142static unsigned int perf_poll(struct file *file, poll_table *wait)
3143{
3144 struct perf_event *event = file->private_data;
Frederic Weisbecker76369132011-05-19 19:55:04 +02003145 struct ring_buffer *rb;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003146 unsigned int events = POLL_HUP;
3147
3148 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02003149 rb = rcu_dereference(event->rb);
3150 if (rb)
3151 events = atomic_xchg(&rb->poll, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003152 rcu_read_unlock();
3153
3154 poll_wait(file, &event->waitq, wait);
3155
3156 return events;
3157}
3158
3159static void perf_event_reset(struct perf_event *event)
3160{
3161 (void)perf_event_read(event);
Peter Zijlstrae7850592010-05-21 14:43:08 +02003162 local64_set(&event->count, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003163 perf_event_update_userpage(event);
3164}
3165
3166/*
3167 * Holding the top-level event's child_mutex means that any
3168 * descendant process that has inherited this event will block
3169 * in sync_child_event if it goes to exit, thus satisfying the
3170 * task existence requirements of perf_event_enable/disable.
3171 */
3172static void perf_event_for_each_child(struct perf_event *event,
3173 void (*func)(struct perf_event *))
3174{
3175 struct perf_event *child;
3176
3177 WARN_ON_ONCE(event->ctx->parent_ctx);
3178 mutex_lock(&event->child_mutex);
3179 func(event);
3180 list_for_each_entry(child, &event->child_list, child_list)
3181 func(child);
3182 mutex_unlock(&event->child_mutex);
3183}
3184
3185static void perf_event_for_each(struct perf_event *event,
3186 void (*func)(struct perf_event *))
3187{
3188 struct perf_event_context *ctx = event->ctx;
3189 struct perf_event *sibling;
3190
3191 WARN_ON_ONCE(ctx->parent_ctx);
3192 mutex_lock(&ctx->mutex);
3193 event = event->group_leader;
3194
3195 perf_event_for_each_child(event, func);
3196 func(event);
3197 list_for_each_entry(sibling, &event->sibling_list, group_entry)
3198 perf_event_for_each_child(event, func);
3199 mutex_unlock(&ctx->mutex);
3200}
3201
3202static int perf_event_period(struct perf_event *event, u64 __user *arg)
3203{
3204 struct perf_event_context *ctx = event->ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003205 int ret = 0;
3206 u64 value;
3207
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01003208 if (!is_sampling_event(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003209 return -EINVAL;
3210
John Blackwoodad0cf342010-09-28 18:03:11 -04003211 if (copy_from_user(&value, arg, sizeof(value)))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003212 return -EFAULT;
3213
3214 if (!value)
3215 return -EINVAL;
3216
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003217 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003218 if (event->attr.freq) {
3219 if (value > sysctl_perf_event_sample_rate) {
3220 ret = -EINVAL;
3221 goto unlock;
3222 }
3223
3224 event->attr.sample_freq = value;
3225 } else {
3226 event->attr.sample_period = value;
3227 event->hw.sample_period = value;
3228 }
3229unlock:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003230 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003231
3232 return ret;
3233}
3234
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003235static const struct file_operations perf_fops;
3236
3237static struct perf_event *perf_fget_light(int fd, int *fput_needed)
3238{
3239 struct file *file;
3240
3241 file = fget_light(fd, fput_needed);
3242 if (!file)
3243 return ERR_PTR(-EBADF);
3244
3245 if (file->f_op != &perf_fops) {
3246 fput_light(file, *fput_needed);
3247 *fput_needed = 0;
3248 return ERR_PTR(-EBADF);
3249 }
3250
3251 return file->private_data;
3252}
3253
3254static int perf_event_set_output(struct perf_event *event,
3255 struct perf_event *output_event);
Li Zefan6fb29152009-10-15 11:21:42 +08003256static int perf_event_set_filter(struct perf_event *event, void __user *arg);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003257
3258static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3259{
3260 struct perf_event *event = file->private_data;
3261 void (*func)(struct perf_event *);
3262 u32 flags = arg;
3263
3264 switch (cmd) {
3265 case PERF_EVENT_IOC_ENABLE:
3266 func = perf_event_enable;
3267 break;
3268 case PERF_EVENT_IOC_DISABLE:
3269 func = perf_event_disable;
3270 break;
3271 case PERF_EVENT_IOC_RESET:
3272 func = perf_event_reset;
3273 break;
3274
3275 case PERF_EVENT_IOC_REFRESH:
3276 return perf_event_refresh(event, arg);
3277
3278 case PERF_EVENT_IOC_PERIOD:
3279 return perf_event_period(event, (u64 __user *)arg);
3280
3281 case PERF_EVENT_IOC_SET_OUTPUT:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003282 {
3283 struct perf_event *output_event = NULL;
3284 int fput_needed = 0;
3285 int ret;
3286
3287 if (arg != -1) {
3288 output_event = perf_fget_light(arg, &fput_needed);
3289 if (IS_ERR(output_event))
3290 return PTR_ERR(output_event);
3291 }
3292
3293 ret = perf_event_set_output(event, output_event);
3294 if (output_event)
3295 fput_light(output_event->filp, fput_needed);
3296
3297 return ret;
3298 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003299
Li Zefan6fb29152009-10-15 11:21:42 +08003300 case PERF_EVENT_IOC_SET_FILTER:
3301 return perf_event_set_filter(event, (void __user *)arg);
3302
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003303 default:
3304 return -ENOTTY;
3305 }
3306
3307 if (flags & PERF_IOC_FLAG_GROUP)
3308 perf_event_for_each(event, func);
3309 else
3310 perf_event_for_each_child(event, func);
3311
3312 return 0;
3313}
3314
3315int perf_event_task_enable(void)
3316{
3317 struct perf_event *event;
3318
3319 mutex_lock(&current->perf_event_mutex);
3320 list_for_each_entry(event, &current->perf_event_list, owner_entry)
3321 perf_event_for_each_child(event, perf_event_enable);
3322 mutex_unlock(&current->perf_event_mutex);
3323
3324 return 0;
3325}
3326
3327int perf_event_task_disable(void)
3328{
3329 struct perf_event *event;
3330
3331 mutex_lock(&current->perf_event_mutex);
3332 list_for_each_entry(event, &current->perf_event_list, owner_entry)
3333 perf_event_for_each_child(event, perf_event_disable);
3334 mutex_unlock(&current->perf_event_mutex);
3335
3336 return 0;
3337}
3338
3339#ifndef PERF_EVENT_INDEX_OFFSET
3340# define PERF_EVENT_INDEX_OFFSET 0
3341#endif
3342
3343static int perf_event_index(struct perf_event *event)
3344{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02003345 if (event->hw.state & PERF_HES_STOPPED)
3346 return 0;
3347
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003348 if (event->state != PERF_EVENT_STATE_ACTIVE)
3349 return 0;
3350
3351 return event->hw.idx + 1 - PERF_EVENT_INDEX_OFFSET;
3352}
3353
Eric B Munsonc4794292011-06-23 16:34:38 -04003354static void calc_timer_values(struct perf_event *event,
3355 u64 *running,
3356 u64 *enabled)
3357{
3358 u64 now, ctx_time;
3359
3360 now = perf_clock();
3361 ctx_time = event->shadow_ctx_time + now;
3362 *enabled = ctx_time - event->tstamp_enabled;
3363 *running = ctx_time - event->tstamp_running;
3364}
3365
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003366/*
3367 * Callers need to ensure there can be no nesting of this function, otherwise
3368 * the seqlock logic goes bad. We can not serialize this because the arch
3369 * code calls this from NMI context.
3370 */
3371void perf_event_update_userpage(struct perf_event *event)
3372{
3373 struct perf_event_mmap_page *userpg;
Frederic Weisbecker76369132011-05-19 19:55:04 +02003374 struct ring_buffer *rb;
Eric B Munson0d641202011-06-24 12:26:26 -04003375 u64 enabled, running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003376
3377 rcu_read_lock();
Eric B Munson0d641202011-06-24 12:26:26 -04003378 /*
3379 * compute total_time_enabled, total_time_running
3380 * based on snapshot values taken when the event
3381 * was last scheduled in.
3382 *
3383 * we cannot simply called update_context_time()
3384 * because of locking issue as we can be called in
3385 * NMI context
3386 */
3387 calc_timer_values(event, &enabled, &running);
Frederic Weisbecker76369132011-05-19 19:55:04 +02003388 rb = rcu_dereference(event->rb);
3389 if (!rb)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003390 goto unlock;
3391
Frederic Weisbecker76369132011-05-19 19:55:04 +02003392 userpg = rb->user_page;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003393
3394 /*
3395 * Disable preemption so as to not let the corresponding user-space
3396 * spin too long if we get preempted.
3397 */
3398 preempt_disable();
3399 ++userpg->lock;
3400 barrier();
3401 userpg->index = perf_event_index(event);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003402 userpg->offset = perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003403 if (event->state == PERF_EVENT_STATE_ACTIVE)
Peter Zijlstrae7850592010-05-21 14:43:08 +02003404 userpg->offset -= local64_read(&event->hw.prev_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003405
Eric B Munson0d641202011-06-24 12:26:26 -04003406 userpg->time_enabled = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003407 atomic64_read(&event->child_total_time_enabled);
3408
Eric B Munson0d641202011-06-24 12:26:26 -04003409 userpg->time_running = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003410 atomic64_read(&event->child_total_time_running);
3411
3412 barrier();
3413 ++userpg->lock;
3414 preempt_enable();
3415unlock:
3416 rcu_read_unlock();
3417}
3418
Peter Zijlstra906010b2009-09-21 16:08:49 +02003419static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
3420{
3421 struct perf_event *event = vma->vm_file->private_data;
Frederic Weisbecker76369132011-05-19 19:55:04 +02003422 struct ring_buffer *rb;
Peter Zijlstra906010b2009-09-21 16:08:49 +02003423 int ret = VM_FAULT_SIGBUS;
3424
3425 if (vmf->flags & FAULT_FLAG_MKWRITE) {
3426 if (vmf->pgoff == 0)
3427 ret = 0;
3428 return ret;
3429 }
3430
3431 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02003432 rb = rcu_dereference(event->rb);
3433 if (!rb)
Peter Zijlstra906010b2009-09-21 16:08:49 +02003434 goto unlock;
3435
3436 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
3437 goto unlock;
3438
Frederic Weisbecker76369132011-05-19 19:55:04 +02003439 vmf->page = perf_mmap_to_page(rb, vmf->pgoff);
Peter Zijlstra906010b2009-09-21 16:08:49 +02003440 if (!vmf->page)
3441 goto unlock;
3442
3443 get_page(vmf->page);
3444 vmf->page->mapping = vma->vm_file->f_mapping;
3445 vmf->page->index = vmf->pgoff;
3446
3447 ret = 0;
3448unlock:
3449 rcu_read_unlock();
3450
3451 return ret;
3452}
3453
Frederic Weisbecker76369132011-05-19 19:55:04 +02003454static void rb_free_rcu(struct rcu_head *rcu_head)
Peter Zijlstra906010b2009-09-21 16:08:49 +02003455{
Frederic Weisbecker76369132011-05-19 19:55:04 +02003456 struct ring_buffer *rb;
Peter Zijlstra906010b2009-09-21 16:08:49 +02003457
Frederic Weisbecker76369132011-05-19 19:55:04 +02003458 rb = container_of(rcu_head, struct ring_buffer, rcu_head);
3459 rb_free(rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003460}
3461
Frederic Weisbecker76369132011-05-19 19:55:04 +02003462static struct ring_buffer *ring_buffer_get(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003463{
Frederic Weisbecker76369132011-05-19 19:55:04 +02003464 struct ring_buffer *rb;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003465
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003466 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02003467 rb = rcu_dereference(event->rb);
3468 if (rb) {
3469 if (!atomic_inc_not_zero(&rb->refcount))
3470 rb = NULL;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003471 }
3472 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003473
Frederic Weisbecker76369132011-05-19 19:55:04 +02003474 return rb;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003475}
3476
Frederic Weisbecker76369132011-05-19 19:55:04 +02003477static void ring_buffer_put(struct ring_buffer *rb)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003478{
Frederic Weisbecker76369132011-05-19 19:55:04 +02003479 if (!atomic_dec_and_test(&rb->refcount))
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003480 return;
3481
Frederic Weisbecker76369132011-05-19 19:55:04 +02003482 call_rcu(&rb->rcu_head, rb_free_rcu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003483}
3484
3485static void perf_mmap_open(struct vm_area_struct *vma)
3486{
3487 struct perf_event *event = vma->vm_file->private_data;
3488
3489 atomic_inc(&event->mmap_count);
3490}
3491
3492static void perf_mmap_close(struct vm_area_struct *vma)
3493{
3494 struct perf_event *event = vma->vm_file->private_data;
3495
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003496 if (atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) {
Frederic Weisbecker76369132011-05-19 19:55:04 +02003497 unsigned long size = perf_data_size(event->rb);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003498 struct user_struct *user = event->mmap_user;
Frederic Weisbecker76369132011-05-19 19:55:04 +02003499 struct ring_buffer *rb = event->rb;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003500
Peter Zijlstra906010b2009-09-21 16:08:49 +02003501 atomic_long_sub((size >> PAGE_SHIFT) + 1, &user->locked_vm);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003502 vma->vm_mm->locked_vm -= event->mmap_locked;
Frederic Weisbecker76369132011-05-19 19:55:04 +02003503 rcu_assign_pointer(event->rb, NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003504 mutex_unlock(&event->mmap_mutex);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003505
Frederic Weisbecker76369132011-05-19 19:55:04 +02003506 ring_buffer_put(rb);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003507 free_uid(user);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003508 }
3509}
3510
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04003511static const struct vm_operations_struct perf_mmap_vmops = {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003512 .open = perf_mmap_open,
3513 .close = perf_mmap_close,
3514 .fault = perf_mmap_fault,
3515 .page_mkwrite = perf_mmap_fault,
3516};
3517
3518static int perf_mmap(struct file *file, struct vm_area_struct *vma)
3519{
3520 struct perf_event *event = file->private_data;
3521 unsigned long user_locked, user_lock_limit;
3522 struct user_struct *user = current_user();
3523 unsigned long locked, lock_limit;
Frederic Weisbecker76369132011-05-19 19:55:04 +02003524 struct ring_buffer *rb;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003525 unsigned long vma_size;
3526 unsigned long nr_pages;
3527 long user_extra, extra;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02003528 int ret = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003529
Peter Zijlstrac7920612010-05-18 10:33:24 +02003530 /*
3531 * Don't allow mmap() of inherited per-task counters. This would
3532 * create a performance issue due to all children writing to the
Frederic Weisbecker76369132011-05-19 19:55:04 +02003533 * same rb.
Peter Zijlstrac7920612010-05-18 10:33:24 +02003534 */
3535 if (event->cpu == -1 && event->attr.inherit)
3536 return -EINVAL;
3537
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003538 if (!(vma->vm_flags & VM_SHARED))
3539 return -EINVAL;
3540
3541 vma_size = vma->vm_end - vma->vm_start;
3542 nr_pages = (vma_size / PAGE_SIZE) - 1;
3543
3544 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +02003545 * If we have rb pages ensure they're a power-of-two number, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003546 * can do bitmasks instead of modulo.
3547 */
3548 if (nr_pages != 0 && !is_power_of_2(nr_pages))
3549 return -EINVAL;
3550
3551 if (vma_size != PAGE_SIZE * (1 + nr_pages))
3552 return -EINVAL;
3553
3554 if (vma->vm_pgoff != 0)
3555 return -EINVAL;
3556
3557 WARN_ON_ONCE(event->ctx->parent_ctx);
3558 mutex_lock(&event->mmap_mutex);
Frederic Weisbecker76369132011-05-19 19:55:04 +02003559 if (event->rb) {
3560 if (event->rb->nr_pages == nr_pages)
3561 atomic_inc(&event->rb->refcount);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003562 else
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003563 ret = -EINVAL;
3564 goto unlock;
3565 }
3566
3567 user_extra = nr_pages + 1;
3568 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
3569
3570 /*
3571 * Increase the limit linearly with more CPUs:
3572 */
3573 user_lock_limit *= num_online_cpus();
3574
3575 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
3576
3577 extra = 0;
3578 if (user_locked > user_lock_limit)
3579 extra = user_locked - user_lock_limit;
3580
Jiri Slaby78d7d402010-03-05 13:42:54 -08003581 lock_limit = rlimit(RLIMIT_MEMLOCK);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003582 lock_limit >>= PAGE_SHIFT;
3583 locked = vma->vm_mm->locked_vm + extra;
3584
3585 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
3586 !capable(CAP_IPC_LOCK)) {
3587 ret = -EPERM;
3588 goto unlock;
3589 }
3590
Frederic Weisbecker76369132011-05-19 19:55:04 +02003591 WARN_ON(event->rb);
Peter Zijlstra906010b2009-09-21 16:08:49 +02003592
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02003593 if (vma->vm_flags & VM_WRITE)
Frederic Weisbecker76369132011-05-19 19:55:04 +02003594 flags |= RING_BUFFER_WRITABLE;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02003595
Vince Weaver4ec83632011-06-01 15:15:36 -04003596 rb = rb_alloc(nr_pages,
3597 event->attr.watermark ? event->attr.wakeup_watermark : 0,
3598 event->cpu, flags);
3599
Frederic Weisbecker76369132011-05-19 19:55:04 +02003600 if (!rb) {
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003601 ret = -ENOMEM;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003602 goto unlock;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003603 }
Frederic Weisbecker76369132011-05-19 19:55:04 +02003604 rcu_assign_pointer(event->rb, rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003605
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003606 atomic_long_add(user_extra, &user->locked_vm);
3607 event->mmap_locked = extra;
3608 event->mmap_user = get_current_user();
3609 vma->vm_mm->locked_vm += event->mmap_locked;
3610
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003611unlock:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003612 if (!ret)
3613 atomic_inc(&event->mmap_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003614 mutex_unlock(&event->mmap_mutex);
3615
3616 vma->vm_flags |= VM_RESERVED;
3617 vma->vm_ops = &perf_mmap_vmops;
3618
3619 return ret;
3620}
3621
3622static int perf_fasync(int fd, struct file *filp, int on)
3623{
3624 struct inode *inode = filp->f_path.dentry->d_inode;
3625 struct perf_event *event = filp->private_data;
3626 int retval;
3627
3628 mutex_lock(&inode->i_mutex);
3629 retval = fasync_helper(fd, filp, on, &event->fasync);
3630 mutex_unlock(&inode->i_mutex);
3631
3632 if (retval < 0)
3633 return retval;
3634
3635 return 0;
3636}
3637
3638static const struct file_operations perf_fops = {
Arnd Bergmann3326c1c2010-03-23 19:09:33 +01003639 .llseek = no_llseek,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003640 .release = perf_release,
3641 .read = perf_read,
3642 .poll = perf_poll,
3643 .unlocked_ioctl = perf_ioctl,
3644 .compat_ioctl = perf_ioctl,
3645 .mmap = perf_mmap,
3646 .fasync = perf_fasync,
3647};
3648
3649/*
3650 * Perf event wakeup
3651 *
3652 * If there's data, ensure we set the poll() state and publish everything
3653 * to user-space before waking everybody up.
3654 */
3655
3656void perf_event_wakeup(struct perf_event *event)
3657{
3658 wake_up_all(&event->waitq);
3659
3660 if (event->pending_kill) {
3661 kill_fasync(&event->fasync, SIGIO, event->pending_kill);
3662 event->pending_kill = 0;
3663 }
3664}
3665
Peter Zijlstrae360adb2010-10-14 14:01:34 +08003666static void perf_pending_event(struct irq_work *entry)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003667{
3668 struct perf_event *event = container_of(entry,
3669 struct perf_event, pending);
3670
3671 if (event->pending_disable) {
3672 event->pending_disable = 0;
3673 __perf_event_disable(event);
3674 }
3675
3676 if (event->pending_wakeup) {
3677 event->pending_wakeup = 0;
3678 perf_event_wakeup(event);
3679 }
3680}
3681
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003682/*
Zhang, Yanmin39447b32010-04-19 13:32:41 +08003683 * We assume there is only KVM supporting the callbacks.
3684 * Later on, we might change it to a list if there is
3685 * another virtualization implementation supporting the callbacks.
3686 */
3687struct perf_guest_info_callbacks *perf_guest_cbs;
3688
3689int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3690{
3691 perf_guest_cbs = cbs;
3692 return 0;
3693}
3694EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
3695
3696int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3697{
3698 perf_guest_cbs = NULL;
3699 return 0;
3700}
3701EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
3702
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02003703static void __perf_event_header__init_id(struct perf_event_header *header,
3704 struct perf_sample_data *data,
3705 struct perf_event *event)
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02003706{
3707 u64 sample_type = event->attr.sample_type;
3708
3709 data->type = sample_type;
3710 header->size += event->id_header_size;
3711
3712 if (sample_type & PERF_SAMPLE_TID) {
3713 /* namespace issues */
3714 data->tid_entry.pid = perf_event_pid(event, current);
3715 data->tid_entry.tid = perf_event_tid(event, current);
3716 }
3717
3718 if (sample_type & PERF_SAMPLE_TIME)
3719 data->time = perf_clock();
3720
3721 if (sample_type & PERF_SAMPLE_ID)
3722 data->id = primary_event_id(event);
3723
3724 if (sample_type & PERF_SAMPLE_STREAM_ID)
3725 data->stream_id = event->id;
3726
3727 if (sample_type & PERF_SAMPLE_CPU) {
3728 data->cpu_entry.cpu = raw_smp_processor_id();
3729 data->cpu_entry.reserved = 0;
3730 }
3731}
3732
Frederic Weisbecker76369132011-05-19 19:55:04 +02003733void perf_event_header__init_id(struct perf_event_header *header,
3734 struct perf_sample_data *data,
3735 struct perf_event *event)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02003736{
3737 if (event->attr.sample_id_all)
3738 __perf_event_header__init_id(header, data, event);
3739}
3740
3741static void __perf_event__output_id_sample(struct perf_output_handle *handle,
3742 struct perf_sample_data *data)
3743{
3744 u64 sample_type = data->type;
3745
3746 if (sample_type & PERF_SAMPLE_TID)
3747 perf_output_put(handle, data->tid_entry);
3748
3749 if (sample_type & PERF_SAMPLE_TIME)
3750 perf_output_put(handle, data->time);
3751
3752 if (sample_type & PERF_SAMPLE_ID)
3753 perf_output_put(handle, data->id);
3754
3755 if (sample_type & PERF_SAMPLE_STREAM_ID)
3756 perf_output_put(handle, data->stream_id);
3757
3758 if (sample_type & PERF_SAMPLE_CPU)
3759 perf_output_put(handle, data->cpu_entry);
3760}
3761
Frederic Weisbecker76369132011-05-19 19:55:04 +02003762void perf_event__output_id_sample(struct perf_event *event,
3763 struct perf_output_handle *handle,
3764 struct perf_sample_data *sample)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02003765{
3766 if (event->attr.sample_id_all)
3767 __perf_event__output_id_sample(handle, sample);
3768}
3769
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003770static void perf_output_read_one(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02003771 struct perf_event *event,
3772 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003773{
3774 u64 read_format = event->attr.read_format;
3775 u64 values[4];
3776 int n = 0;
3777
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003778 values[n++] = perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003779 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
Stephane Eranianeed01522010-10-26 16:08:01 +02003780 values[n++] = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003781 atomic64_read(&event->child_total_time_enabled);
3782 }
3783 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
Stephane Eranianeed01522010-10-26 16:08:01 +02003784 values[n++] = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003785 atomic64_read(&event->child_total_time_running);
3786 }
3787 if (read_format & PERF_FORMAT_ID)
3788 values[n++] = primary_event_id(event);
3789
Frederic Weisbecker76369132011-05-19 19:55:04 +02003790 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003791}
3792
3793/*
3794 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
3795 */
3796static void perf_output_read_group(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02003797 struct perf_event *event,
3798 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003799{
3800 struct perf_event *leader = event->group_leader, *sub;
3801 u64 read_format = event->attr.read_format;
3802 u64 values[5];
3803 int n = 0;
3804
3805 values[n++] = 1 + leader->nr_siblings;
3806
3807 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
Stephane Eranianeed01522010-10-26 16:08:01 +02003808 values[n++] = enabled;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003809
3810 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
Stephane Eranianeed01522010-10-26 16:08:01 +02003811 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003812
3813 if (leader != event)
3814 leader->pmu->read(leader);
3815
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003816 values[n++] = perf_event_count(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003817 if (read_format & PERF_FORMAT_ID)
3818 values[n++] = primary_event_id(leader);
3819
Frederic Weisbecker76369132011-05-19 19:55:04 +02003820 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003821
3822 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
3823 n = 0;
3824
3825 if (sub != event)
3826 sub->pmu->read(sub);
3827
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003828 values[n++] = perf_event_count(sub);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003829 if (read_format & PERF_FORMAT_ID)
3830 values[n++] = primary_event_id(sub);
3831
Frederic Weisbecker76369132011-05-19 19:55:04 +02003832 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003833 }
3834}
3835
Stephane Eranianeed01522010-10-26 16:08:01 +02003836#define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
3837 PERF_FORMAT_TOTAL_TIME_RUNNING)
3838
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003839static void perf_output_read(struct perf_output_handle *handle,
3840 struct perf_event *event)
3841{
Eric B Munsonc4794292011-06-23 16:34:38 -04003842 u64 enabled = 0, running = 0;
Stephane Eranianeed01522010-10-26 16:08:01 +02003843 u64 read_format = event->attr.read_format;
3844
3845 /*
3846 * compute total_time_enabled, total_time_running
3847 * based on snapshot values taken when the event
3848 * was last scheduled in.
3849 *
3850 * we cannot simply called update_context_time()
3851 * because of locking issue as we are called in
3852 * NMI context
3853 */
Eric B Munsonc4794292011-06-23 16:34:38 -04003854 if (read_format & PERF_FORMAT_TOTAL_TIMES)
3855 calc_timer_values(event, &enabled, &running);
Stephane Eranianeed01522010-10-26 16:08:01 +02003856
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003857 if (event->attr.read_format & PERF_FORMAT_GROUP)
Stephane Eranianeed01522010-10-26 16:08:01 +02003858 perf_output_read_group(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003859 else
Stephane Eranianeed01522010-10-26 16:08:01 +02003860 perf_output_read_one(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003861}
3862
3863void perf_output_sample(struct perf_output_handle *handle,
3864 struct perf_event_header *header,
3865 struct perf_sample_data *data,
3866 struct perf_event *event)
3867{
3868 u64 sample_type = data->type;
3869
3870 perf_output_put(handle, *header);
3871
3872 if (sample_type & PERF_SAMPLE_IP)
3873 perf_output_put(handle, data->ip);
3874
3875 if (sample_type & PERF_SAMPLE_TID)
3876 perf_output_put(handle, data->tid_entry);
3877
3878 if (sample_type & PERF_SAMPLE_TIME)
3879 perf_output_put(handle, data->time);
3880
3881 if (sample_type & PERF_SAMPLE_ADDR)
3882 perf_output_put(handle, data->addr);
3883
3884 if (sample_type & PERF_SAMPLE_ID)
3885 perf_output_put(handle, data->id);
3886
3887 if (sample_type & PERF_SAMPLE_STREAM_ID)
3888 perf_output_put(handle, data->stream_id);
3889
3890 if (sample_type & PERF_SAMPLE_CPU)
3891 perf_output_put(handle, data->cpu_entry);
3892
3893 if (sample_type & PERF_SAMPLE_PERIOD)
3894 perf_output_put(handle, data->period);
3895
3896 if (sample_type & PERF_SAMPLE_READ)
3897 perf_output_read(handle, event);
3898
3899 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
3900 if (data->callchain) {
3901 int size = 1;
3902
3903 if (data->callchain)
3904 size += data->callchain->nr;
3905
3906 size *= sizeof(u64);
3907
Frederic Weisbecker76369132011-05-19 19:55:04 +02003908 __output_copy(handle, data->callchain, size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003909 } else {
3910 u64 nr = 0;
3911 perf_output_put(handle, nr);
3912 }
3913 }
3914
3915 if (sample_type & PERF_SAMPLE_RAW) {
3916 if (data->raw) {
3917 perf_output_put(handle, data->raw->size);
Frederic Weisbecker76369132011-05-19 19:55:04 +02003918 __output_copy(handle, data->raw->data,
3919 data->raw->size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003920 } else {
3921 struct {
3922 u32 size;
3923 u32 data;
3924 } raw = {
3925 .size = sizeof(u32),
3926 .data = 0,
3927 };
3928 perf_output_put(handle, raw);
3929 }
3930 }
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02003931
3932 if (!event->attr.watermark) {
3933 int wakeup_events = event->attr.wakeup_events;
3934
3935 if (wakeup_events) {
3936 struct ring_buffer *rb = handle->rb;
3937 int events = local_inc_return(&rb->events);
3938
3939 if (events >= wakeup_events) {
3940 local_sub(wakeup_events, &rb->events);
3941 local_inc(&rb->wakeup);
3942 }
3943 }
3944 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003945}
3946
3947void perf_prepare_sample(struct perf_event_header *header,
3948 struct perf_sample_data *data,
3949 struct perf_event *event,
3950 struct pt_regs *regs)
3951{
3952 u64 sample_type = event->attr.sample_type;
3953
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003954 header->type = PERF_RECORD_SAMPLE;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02003955 header->size = sizeof(*header) + event->header_size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003956
3957 header->misc = 0;
3958 header->misc |= perf_misc_flags(regs);
3959
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02003960 __perf_event_header__init_id(header, data, event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02003961
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02003962 if (sample_type & PERF_SAMPLE_IP)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003963 data->ip = perf_instruction_pointer(regs);
3964
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003965 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
3966 int size = 1;
3967
3968 data->callchain = perf_callchain(regs);
3969
3970 if (data->callchain)
3971 size += data->callchain->nr;
3972
3973 header->size += size * sizeof(u64);
3974 }
3975
3976 if (sample_type & PERF_SAMPLE_RAW) {
3977 int size = sizeof(u32);
3978
3979 if (data->raw)
3980 size += data->raw->size;
3981 else
3982 size += sizeof(u32);
3983
3984 WARN_ON_ONCE(size & (sizeof(u64)-1));
3985 header->size += size;
3986 }
3987}
3988
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02003989static void perf_event_output(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003990 struct perf_sample_data *data,
3991 struct pt_regs *regs)
3992{
3993 struct perf_output_handle handle;
3994 struct perf_event_header header;
3995
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02003996 /* protect the callchain buffers */
3997 rcu_read_lock();
3998
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003999 perf_prepare_sample(&header, data, event, regs);
4000
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02004001 if (perf_output_begin(&handle, event, header.size))
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02004002 goto exit;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004003
4004 perf_output_sample(&handle, &header, data, event);
4005
4006 perf_output_end(&handle);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02004007
4008exit:
4009 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004010}
4011
4012/*
4013 * read event_id
4014 */
4015
4016struct perf_read_event {
4017 struct perf_event_header header;
4018
4019 u32 pid;
4020 u32 tid;
4021};
4022
4023static void
4024perf_event_read_event(struct perf_event *event,
4025 struct task_struct *task)
4026{
4027 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004028 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004029 struct perf_read_event read_event = {
4030 .header = {
4031 .type = PERF_RECORD_READ,
4032 .misc = 0,
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02004033 .size = sizeof(read_event) + event->read_size,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004034 },
4035 .pid = perf_event_pid(event, task),
4036 .tid = perf_event_tid(event, task),
4037 };
4038 int ret;
4039
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004040 perf_event_header__init_id(&read_event.header, &sample, event);
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02004041 ret = perf_output_begin(&handle, event, read_event.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004042 if (ret)
4043 return;
4044
4045 perf_output_put(&handle, read_event);
4046 perf_output_read(&handle, event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004047 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004048
4049 perf_output_end(&handle);
4050}
4051
4052/*
4053 * task tracking -- fork/exit
4054 *
Eric B Munson3af9e852010-05-18 15:30:49 +01004055 * enabled by: attr.comm | attr.mmap | attr.mmap_data | attr.task
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004056 */
4057
4058struct perf_task_event {
4059 struct task_struct *task;
4060 struct perf_event_context *task_ctx;
4061
4062 struct {
4063 struct perf_event_header header;
4064
4065 u32 pid;
4066 u32 ppid;
4067 u32 tid;
4068 u32 ptid;
4069 u64 time;
4070 } event_id;
4071};
4072
4073static void perf_event_task_output(struct perf_event *event,
4074 struct perf_task_event *task_event)
4075{
4076 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004077 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004078 struct task_struct *task = task_event->task;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004079 int ret, size = task_event->event_id.header.size;
Mike Galbraith8bb39f92010-03-26 11:11:33 +01004080
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004081 perf_event_header__init_id(&task_event->event_id.header, &sample, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004082
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004083 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02004084 task_event->event_id.header.size);
Peter Zijlstraef607772010-05-18 10:50:41 +02004085 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004086 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004087
4088 task_event->event_id.pid = perf_event_pid(event, task);
4089 task_event->event_id.ppid = perf_event_pid(event, current);
4090
4091 task_event->event_id.tid = perf_event_tid(event, task);
4092 task_event->event_id.ptid = perf_event_tid(event, current);
4093
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004094 perf_output_put(&handle, task_event->event_id);
4095
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004096 perf_event__output_id_sample(event, &handle, &sample);
4097
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004098 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004099out:
4100 task_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004101}
4102
4103static int perf_event_task_match(struct perf_event *event)
4104{
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01004105 if (event->state < PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra22e19082010-01-18 09:12:32 +01004106 return 0;
4107
Stephane Eranian5632ab12011-01-03 18:20:01 +02004108 if (!event_filter_match(event))
Peter Zijlstra5d27c232009-12-17 13:16:32 +01004109 return 0;
4110
Eric B Munson3af9e852010-05-18 15:30:49 +01004111 if (event->attr.comm || event->attr.mmap ||
4112 event->attr.mmap_data || event->attr.task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004113 return 1;
4114
4115 return 0;
4116}
4117
4118static void perf_event_task_ctx(struct perf_event_context *ctx,
4119 struct perf_task_event *task_event)
4120{
4121 struct perf_event *event;
4122
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004123 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4124 if (perf_event_task_match(event))
4125 perf_event_task_output(event, task_event);
4126 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004127}
4128
4129static void perf_event_task_event(struct perf_task_event *task_event)
4130{
4131 struct perf_cpu_context *cpuctx;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004132 struct perf_event_context *ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004133 struct pmu *pmu;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004134 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004135
Peter Zijlstrad6ff86c2009-11-20 22:19:46 +01004136 rcu_read_lock();
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004137 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra41945f62010-09-16 19:17:24 +02004138 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra51676952010-12-07 14:18:20 +01004139 if (cpuctx->active_pmu != pmu)
4140 goto next;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004141 perf_event_task_ctx(&cpuctx->ctx, task_event);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004142
4143 ctx = task_event->task_ctx;
4144 if (!ctx) {
4145 ctxn = pmu->task_ctx_nr;
4146 if (ctxn < 0)
Peter Zijlstra41945f62010-09-16 19:17:24 +02004147 goto next;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004148 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4149 }
4150 if (ctx)
4151 perf_event_task_ctx(ctx, task_event);
Peter Zijlstra41945f62010-09-16 19:17:24 +02004152next:
4153 put_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004154 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004155 rcu_read_unlock();
4156}
4157
4158static void perf_event_task(struct task_struct *task,
4159 struct perf_event_context *task_ctx,
4160 int new)
4161{
4162 struct perf_task_event task_event;
4163
4164 if (!atomic_read(&nr_comm_events) &&
4165 !atomic_read(&nr_mmap_events) &&
4166 !atomic_read(&nr_task_events))
4167 return;
4168
4169 task_event = (struct perf_task_event){
4170 .task = task,
4171 .task_ctx = task_ctx,
4172 .event_id = {
4173 .header = {
4174 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
4175 .misc = 0,
4176 .size = sizeof(task_event.event_id),
4177 },
4178 /* .pid */
4179 /* .ppid */
4180 /* .tid */
4181 /* .ptid */
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01004182 .time = perf_clock(),
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004183 },
4184 };
4185
4186 perf_event_task_event(&task_event);
4187}
4188
4189void perf_event_fork(struct task_struct *task)
4190{
4191 perf_event_task(task, NULL, 1);
4192}
4193
4194/*
4195 * comm tracking
4196 */
4197
4198struct perf_comm_event {
4199 struct task_struct *task;
4200 char *comm;
4201 int comm_size;
4202
4203 struct {
4204 struct perf_event_header header;
4205
4206 u32 pid;
4207 u32 tid;
4208 } event_id;
4209};
4210
4211static void perf_event_comm_output(struct perf_event *event,
4212 struct perf_comm_event *comm_event)
4213{
4214 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004215 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004216 int size = comm_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004217 int ret;
4218
4219 perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
4220 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02004221 comm_event->event_id.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004222
4223 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004224 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004225
4226 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
4227 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
4228
4229 perf_output_put(&handle, comm_event->event_id);
Frederic Weisbecker76369132011-05-19 19:55:04 +02004230 __output_copy(&handle, comm_event->comm,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004231 comm_event->comm_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004232
4233 perf_event__output_id_sample(event, &handle, &sample);
4234
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004235 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004236out:
4237 comm_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004238}
4239
4240static int perf_event_comm_match(struct perf_event *event)
4241{
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01004242 if (event->state < PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra22e19082010-01-18 09:12:32 +01004243 return 0;
4244
Stephane Eranian5632ab12011-01-03 18:20:01 +02004245 if (!event_filter_match(event))
Peter Zijlstra5d27c232009-12-17 13:16:32 +01004246 return 0;
4247
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004248 if (event->attr.comm)
4249 return 1;
4250
4251 return 0;
4252}
4253
4254static void perf_event_comm_ctx(struct perf_event_context *ctx,
4255 struct perf_comm_event *comm_event)
4256{
4257 struct perf_event *event;
4258
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004259 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4260 if (perf_event_comm_match(event))
4261 perf_event_comm_output(event, comm_event);
4262 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004263}
4264
4265static void perf_event_comm_event(struct perf_comm_event *comm_event)
4266{
4267 struct perf_cpu_context *cpuctx;
4268 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004269 char comm[TASK_COMM_LEN];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004270 unsigned int size;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004271 struct pmu *pmu;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004272 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004273
4274 memset(comm, 0, sizeof(comm));
Márton Németh96b02d72009-11-21 23:10:15 +01004275 strlcpy(comm, comm_event->task->comm, sizeof(comm));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004276 size = ALIGN(strlen(comm)+1, sizeof(u64));
4277
4278 comm_event->comm = comm;
4279 comm_event->comm_size = size;
4280
4281 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
Peter Zijlstraf6595f32009-11-20 22:19:47 +01004282 rcu_read_lock();
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004283 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra41945f62010-09-16 19:17:24 +02004284 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra51676952010-12-07 14:18:20 +01004285 if (cpuctx->active_pmu != pmu)
4286 goto next;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004287 perf_event_comm_ctx(&cpuctx->ctx, comm_event);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004288
4289 ctxn = pmu->task_ctx_nr;
4290 if (ctxn < 0)
Peter Zijlstra41945f62010-09-16 19:17:24 +02004291 goto next;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004292
4293 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4294 if (ctx)
4295 perf_event_comm_ctx(ctx, comm_event);
Peter Zijlstra41945f62010-09-16 19:17:24 +02004296next:
4297 put_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004298 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004299 rcu_read_unlock();
4300}
4301
4302void perf_event_comm(struct task_struct *task)
4303{
4304 struct perf_comm_event comm_event;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004305 struct perf_event_context *ctx;
4306 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004307
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004308 for_each_task_context_nr(ctxn) {
4309 ctx = task->perf_event_ctxp[ctxn];
4310 if (!ctx)
4311 continue;
4312
4313 perf_event_enable_on_exec(ctx);
4314 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004315
4316 if (!atomic_read(&nr_comm_events))
4317 return;
4318
4319 comm_event = (struct perf_comm_event){
4320 .task = task,
4321 /* .comm */
4322 /* .comm_size */
4323 .event_id = {
4324 .header = {
4325 .type = PERF_RECORD_COMM,
4326 .misc = 0,
4327 /* .size */
4328 },
4329 /* .pid */
4330 /* .tid */
4331 },
4332 };
4333
4334 perf_event_comm_event(&comm_event);
4335}
4336
4337/*
4338 * mmap tracking
4339 */
4340
4341struct perf_mmap_event {
4342 struct vm_area_struct *vma;
4343
4344 const char *file_name;
4345 int file_size;
4346
4347 struct {
4348 struct perf_event_header header;
4349
4350 u32 pid;
4351 u32 tid;
4352 u64 start;
4353 u64 len;
4354 u64 pgoff;
4355 } event_id;
4356};
4357
4358static void perf_event_mmap_output(struct perf_event *event,
4359 struct perf_mmap_event *mmap_event)
4360{
4361 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004362 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004363 int size = mmap_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004364 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004365
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004366 perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
4367 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02004368 mmap_event->event_id.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004369 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004370 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004371
4372 mmap_event->event_id.pid = perf_event_pid(event, current);
4373 mmap_event->event_id.tid = perf_event_tid(event, current);
4374
4375 perf_output_put(&handle, mmap_event->event_id);
Frederic Weisbecker76369132011-05-19 19:55:04 +02004376 __output_copy(&handle, mmap_event->file_name,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004377 mmap_event->file_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004378
4379 perf_event__output_id_sample(event, &handle, &sample);
4380
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004381 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004382out:
4383 mmap_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004384}
4385
4386static int perf_event_mmap_match(struct perf_event *event,
Eric B Munson3af9e852010-05-18 15:30:49 +01004387 struct perf_mmap_event *mmap_event,
4388 int executable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004389{
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01004390 if (event->state < PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra22e19082010-01-18 09:12:32 +01004391 return 0;
4392
Stephane Eranian5632ab12011-01-03 18:20:01 +02004393 if (!event_filter_match(event))
Peter Zijlstra5d27c232009-12-17 13:16:32 +01004394 return 0;
4395
Eric B Munson3af9e852010-05-18 15:30:49 +01004396 if ((!executable && event->attr.mmap_data) ||
4397 (executable && event->attr.mmap))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004398 return 1;
4399
4400 return 0;
4401}
4402
4403static void perf_event_mmap_ctx(struct perf_event_context *ctx,
Eric B Munson3af9e852010-05-18 15:30:49 +01004404 struct perf_mmap_event *mmap_event,
4405 int executable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004406{
4407 struct perf_event *event;
4408
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004409 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Eric B Munson3af9e852010-05-18 15:30:49 +01004410 if (perf_event_mmap_match(event, mmap_event, executable))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004411 perf_event_mmap_output(event, mmap_event);
4412 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004413}
4414
4415static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
4416{
4417 struct perf_cpu_context *cpuctx;
4418 struct perf_event_context *ctx;
4419 struct vm_area_struct *vma = mmap_event->vma;
4420 struct file *file = vma->vm_file;
4421 unsigned int size;
4422 char tmp[16];
4423 char *buf = NULL;
4424 const char *name;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004425 struct pmu *pmu;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004426 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004427
4428 memset(tmp, 0, sizeof(tmp));
4429
4430 if (file) {
4431 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +02004432 * d_path works from the end of the rb backwards, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004433 * need to add enough zero bytes after the string to handle
4434 * the 64bit alignment we do later.
4435 */
4436 buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
4437 if (!buf) {
4438 name = strncpy(tmp, "//enomem", sizeof(tmp));
4439 goto got_name;
4440 }
4441 name = d_path(&file->f_path, buf, PATH_MAX);
4442 if (IS_ERR(name)) {
4443 name = strncpy(tmp, "//toolong", sizeof(tmp));
4444 goto got_name;
4445 }
4446 } else {
4447 if (arch_vma_name(mmap_event->vma)) {
4448 name = strncpy(tmp, arch_vma_name(mmap_event->vma),
4449 sizeof(tmp));
4450 goto got_name;
4451 }
4452
4453 if (!vma->vm_mm) {
4454 name = strncpy(tmp, "[vdso]", sizeof(tmp));
4455 goto got_name;
Eric B Munson3af9e852010-05-18 15:30:49 +01004456 } else if (vma->vm_start <= vma->vm_mm->start_brk &&
4457 vma->vm_end >= vma->vm_mm->brk) {
4458 name = strncpy(tmp, "[heap]", sizeof(tmp));
4459 goto got_name;
4460 } else if (vma->vm_start <= vma->vm_mm->start_stack &&
4461 vma->vm_end >= vma->vm_mm->start_stack) {
4462 name = strncpy(tmp, "[stack]", sizeof(tmp));
4463 goto got_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004464 }
4465
4466 name = strncpy(tmp, "//anon", sizeof(tmp));
4467 goto got_name;
4468 }
4469
4470got_name:
4471 size = ALIGN(strlen(name)+1, sizeof(u64));
4472
4473 mmap_event->file_name = name;
4474 mmap_event->file_size = size;
4475
4476 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
4477
Peter Zijlstraf6d9dd22009-11-20 22:19:48 +01004478 rcu_read_lock();
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004479 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra41945f62010-09-16 19:17:24 +02004480 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra51676952010-12-07 14:18:20 +01004481 if (cpuctx->active_pmu != pmu)
4482 goto next;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004483 perf_event_mmap_ctx(&cpuctx->ctx, mmap_event,
4484 vma->vm_flags & VM_EXEC);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004485
4486 ctxn = pmu->task_ctx_nr;
4487 if (ctxn < 0)
Peter Zijlstra41945f62010-09-16 19:17:24 +02004488 goto next;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004489
4490 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4491 if (ctx) {
4492 perf_event_mmap_ctx(ctx, mmap_event,
4493 vma->vm_flags & VM_EXEC);
4494 }
Peter Zijlstra41945f62010-09-16 19:17:24 +02004495next:
4496 put_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004497 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004498 rcu_read_unlock();
4499
4500 kfree(buf);
4501}
4502
Eric B Munson3af9e852010-05-18 15:30:49 +01004503void perf_event_mmap(struct vm_area_struct *vma)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004504{
4505 struct perf_mmap_event mmap_event;
4506
4507 if (!atomic_read(&nr_mmap_events))
4508 return;
4509
4510 mmap_event = (struct perf_mmap_event){
4511 .vma = vma,
4512 /* .file_name */
4513 /* .file_size */
4514 .event_id = {
4515 .header = {
4516 .type = PERF_RECORD_MMAP,
Zhang, Yanmin39447b32010-04-19 13:32:41 +08004517 .misc = PERF_RECORD_MISC_USER,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004518 /* .size */
4519 },
4520 /* .pid */
4521 /* .tid */
4522 .start = vma->vm_start,
4523 .len = vma->vm_end - vma->vm_start,
Peter Zijlstra3a0304e2010-02-26 10:33:41 +01004524 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004525 },
4526 };
4527
4528 perf_event_mmap_event(&mmap_event);
4529}
4530
4531/*
4532 * IRQ throttle logging
4533 */
4534
4535static void perf_log_throttle(struct perf_event *event, int enable)
4536{
4537 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004538 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004539 int ret;
4540
4541 struct {
4542 struct perf_event_header header;
4543 u64 time;
4544 u64 id;
4545 u64 stream_id;
4546 } throttle_event = {
4547 .header = {
4548 .type = PERF_RECORD_THROTTLE,
4549 .misc = 0,
4550 .size = sizeof(throttle_event),
4551 },
4552 .time = perf_clock(),
4553 .id = primary_event_id(event),
4554 .stream_id = event->id,
4555 };
4556
4557 if (enable)
4558 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
4559
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004560 perf_event_header__init_id(&throttle_event.header, &sample, event);
4561
4562 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02004563 throttle_event.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004564 if (ret)
4565 return;
4566
4567 perf_output_put(&handle, throttle_event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004568 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004569 perf_output_end(&handle);
4570}
4571
4572/*
4573 * Generic event overflow handling, sampling.
4574 */
4575
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004576static int __perf_event_overflow(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004577 int throttle, struct perf_sample_data *data,
4578 struct pt_regs *regs)
4579{
4580 int events = atomic_read(&event->event_limit);
4581 struct hw_perf_event *hwc = &event->hw;
4582 int ret = 0;
4583
Peter Zijlstra96398822010-11-24 18:55:29 +01004584 /*
4585 * Non-sampling counters might still use the PMI to fold short
4586 * hardware counters, ignore those.
4587 */
4588 if (unlikely(!is_sampling_event(event)))
4589 return 0;
4590
Peter Zijlstra163ec432011-02-16 11:22:34 +01004591 if (unlikely(hwc->interrupts >= max_samples_per_tick)) {
4592 if (throttle) {
4593 hwc->interrupts = MAX_INTERRUPTS;
4594 perf_log_throttle(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004595 ret = 1;
4596 }
Peter Zijlstra163ec432011-02-16 11:22:34 +01004597 } else
4598 hwc->interrupts++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004599
4600 if (event->attr.freq) {
4601 u64 now = perf_clock();
Peter Zijlstraabd50712010-01-26 18:50:16 +01004602 s64 delta = now - hwc->freq_time_stamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004603
Peter Zijlstraabd50712010-01-26 18:50:16 +01004604 hwc->freq_time_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004605
Peter Zijlstraabd50712010-01-26 18:50:16 +01004606 if (delta > 0 && delta < 2*TICK_NSEC)
4607 perf_adjust_period(event, delta, hwc->last_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004608 }
4609
4610 /*
4611 * XXX event_limit might not quite work as expected on inherited
4612 * events
4613 */
4614
4615 event->pending_kill = POLL_IN;
4616 if (events && atomic_dec_and_test(&event->event_limit)) {
4617 ret = 1;
4618 event->pending_kill = POLL_HUP;
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004619 event->pending_disable = 1;
4620 irq_work_queue(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004621 }
4622
Peter Zijlstra453f19e2009-11-20 22:19:43 +01004623 if (event->overflow_handler)
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004624 event->overflow_handler(event, data, regs);
Peter Zijlstra453f19e2009-11-20 22:19:43 +01004625 else
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004626 perf_event_output(event, data, regs);
Peter Zijlstra453f19e2009-11-20 22:19:43 +01004627
Peter Zijlstraf506b3d2011-05-26 17:02:53 +02004628 if (event->fasync && event->pending_kill) {
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004629 event->pending_wakeup = 1;
4630 irq_work_queue(&event->pending);
Peter Zijlstraf506b3d2011-05-26 17:02:53 +02004631 }
4632
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004633 return ret;
4634}
4635
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004636int perf_event_overflow(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004637 struct perf_sample_data *data,
4638 struct pt_regs *regs)
4639{
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004640 return __perf_event_overflow(event, 1, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004641}
4642
4643/*
4644 * Generic software event infrastructure
4645 */
4646
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004647struct swevent_htable {
4648 struct swevent_hlist *swevent_hlist;
4649 struct mutex hlist_mutex;
4650 int hlist_refcount;
4651
4652 /* Recursion avoidance in each contexts */
4653 int recursion[PERF_NR_CONTEXTS];
4654};
4655
4656static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
4657
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004658/*
4659 * We directly increment event->count and keep a second value in
4660 * event->hw.period_left to count intervals. This period event
4661 * is kept in the range [-sample_period, 0] so that we can use the
4662 * sign as trigger.
4663 */
4664
4665static u64 perf_swevent_set_period(struct perf_event *event)
4666{
4667 struct hw_perf_event *hwc = &event->hw;
4668 u64 period = hwc->last_period;
4669 u64 nr, offset;
4670 s64 old, val;
4671
4672 hwc->last_period = hwc->sample_period;
4673
4674again:
Peter Zijlstrae7850592010-05-21 14:43:08 +02004675 old = val = local64_read(&hwc->period_left);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004676 if (val < 0)
4677 return 0;
4678
4679 nr = div64_u64(period + val, period);
4680 offset = nr * period;
4681 val -= offset;
Peter Zijlstrae7850592010-05-21 14:43:08 +02004682 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004683 goto again;
4684
4685 return nr;
4686}
4687
Peter Zijlstra0cff7842009-11-20 22:19:44 +01004688static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004689 struct perf_sample_data *data,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004690 struct pt_regs *regs)
4691{
4692 struct hw_perf_event *hwc = &event->hw;
4693 int throttle = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004694
4695 data->period = event->hw.last_period;
Peter Zijlstra0cff7842009-11-20 22:19:44 +01004696 if (!overflow)
4697 overflow = perf_swevent_set_period(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004698
4699 if (hwc->interrupts == MAX_INTERRUPTS)
4700 return;
4701
4702 for (; overflow; overflow--) {
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004703 if (__perf_event_overflow(event, throttle,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004704 data, regs)) {
4705 /*
4706 * We inhibit the overflow from happening when
4707 * hwc->interrupts == MAX_INTERRUPTS.
4708 */
4709 break;
4710 }
4711 throttle = 1;
4712 }
4713}
4714
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004715static void perf_swevent_event(struct perf_event *event, u64 nr,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004716 struct perf_sample_data *data,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004717 struct pt_regs *regs)
4718{
4719 struct hw_perf_event *hwc = &event->hw;
4720
Peter Zijlstrae7850592010-05-21 14:43:08 +02004721 local64_add(nr, &event->count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004722
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004723 if (!regs)
4724 return;
4725
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01004726 if (!is_sampling_event(event))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01004727 return;
4728
4729 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004730 return perf_swevent_overflow(event, 1, data, regs);
Peter Zijlstra0cff7842009-11-20 22:19:44 +01004731
Peter Zijlstrae7850592010-05-21 14:43:08 +02004732 if (local64_add_negative(nr, &hwc->period_left))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01004733 return;
4734
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004735 perf_swevent_overflow(event, 0, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004736}
4737
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004738static int perf_exclude_event(struct perf_event *event,
4739 struct pt_regs *regs)
4740{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004741 if (event->hw.state & PERF_HES_STOPPED)
Frederic Weisbecker91b2f482011-03-07 21:27:08 +01004742 return 1;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004743
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004744 if (regs) {
4745 if (event->attr.exclude_user && user_mode(regs))
4746 return 1;
4747
4748 if (event->attr.exclude_kernel && !user_mode(regs))
4749 return 1;
4750 }
4751
4752 return 0;
4753}
4754
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004755static int perf_swevent_match(struct perf_event *event,
4756 enum perf_type_id type,
Li Zefan6fb29152009-10-15 11:21:42 +08004757 u32 event_id,
4758 struct perf_sample_data *data,
4759 struct pt_regs *regs)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004760{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004761 if (event->attr.type != type)
4762 return 0;
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004763
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004764 if (event->attr.config != event_id)
4765 return 0;
4766
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004767 if (perf_exclude_event(event, regs))
4768 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004769
4770 return 1;
4771}
4772
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004773static inline u64 swevent_hash(u64 type, u32 event_id)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004774{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004775 u64 val = event_id | (type << 32);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004776
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004777 return hash_64(val, SWEVENT_HLIST_BITS);
4778}
4779
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004780static inline struct hlist_head *
4781__find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004782{
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004783 u64 hash = swevent_hash(type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004784
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004785 return &hlist->heads[hash];
4786}
4787
4788/* For the read side: events when they trigger */
4789static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004790find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004791{
4792 struct swevent_hlist *hlist;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004793
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004794 hlist = rcu_dereference(swhash->swevent_hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004795 if (!hlist)
4796 return NULL;
4797
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004798 return __find_swevent_head(hlist, type, event_id);
4799}
4800
4801/* For the event head insertion and removal in the hlist */
4802static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004803find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004804{
4805 struct swevent_hlist *hlist;
4806 u32 event_id = event->attr.config;
4807 u64 type = event->attr.type;
4808
4809 /*
4810 * Event scheduling is always serialized against hlist allocation
4811 * and release. Which makes the protected version suitable here.
4812 * The context lock guarantees that.
4813 */
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004814 hlist = rcu_dereference_protected(swhash->swevent_hlist,
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004815 lockdep_is_held(&event->ctx->lock));
4816 if (!hlist)
4817 return NULL;
4818
4819 return __find_swevent_head(hlist, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004820}
4821
4822static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004823 u64 nr,
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004824 struct perf_sample_data *data,
4825 struct pt_regs *regs)
4826{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004827 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004828 struct perf_event *event;
4829 struct hlist_node *node;
4830 struct hlist_head *head;
4831
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004832 rcu_read_lock();
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004833 head = find_swevent_head_rcu(swhash, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004834 if (!head)
4835 goto end;
4836
4837 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
Li Zefan6fb29152009-10-15 11:21:42 +08004838 if (perf_swevent_match(event, type, event_id, data, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004839 perf_swevent_event(event, nr, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004840 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004841end:
4842 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004843}
4844
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004845int perf_swevent_get_recursion_context(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004846{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004847 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01004848
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004849 return get_recursion_context(swhash->recursion);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004850}
Ingo Molnar645e8cc2009-11-22 12:20:19 +01004851EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004852
Jesper Juhlfa9f90b2010-11-28 21:39:34 +01004853inline void perf_swevent_put_recursion_context(int rctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004854{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004855 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02004856
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004857 put_recursion_context(swhash->recursion, rctx);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01004858}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004859
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004860void __perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004861{
Ingo Molnara4234bf2009-11-23 10:57:59 +01004862 struct perf_sample_data data;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004863 int rctx;
4864
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004865 preempt_disable_notrace();
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004866 rctx = perf_swevent_get_recursion_context();
4867 if (rctx < 0)
4868 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004869
Peter Zijlstradc1d6282010-03-03 15:55:04 +01004870 perf_sample_data_init(&data, addr);
Ingo Molnara4234bf2009-11-23 10:57:59 +01004871
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004872 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs);
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004873
4874 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004875 preempt_enable_notrace();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004876}
4877
4878static void perf_swevent_read(struct perf_event *event)
4879{
4880}
4881
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004882static int perf_swevent_add(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004883{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004884 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004885 struct hw_perf_event *hwc = &event->hw;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004886 struct hlist_head *head;
4887
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01004888 if (is_sampling_event(event)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004889 hwc->last_period = hwc->sample_period;
4890 perf_swevent_set_period(event);
4891 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004892
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004893 hwc->state = !(flags & PERF_EF_START);
4894
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004895 head = find_swevent_head(swhash, event);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004896 if (WARN_ON_ONCE(!head))
4897 return -EINVAL;
4898
4899 hlist_add_head_rcu(&event->hlist_entry, head);
4900
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004901 return 0;
4902}
4903
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004904static void perf_swevent_del(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004905{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004906 hlist_del_rcu(&event->hlist_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004907}
4908
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004909static void perf_swevent_start(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02004910{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004911 event->hw.state = 0;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02004912}
4913
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004914static void perf_swevent_stop(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02004915{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004916 event->hw.state = PERF_HES_STOPPED;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02004917}
4918
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004919/* Deref the hlist from the update side */
4920static inline struct swevent_hlist *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004921swevent_hlist_deref(struct swevent_htable *swhash)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004922{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004923 return rcu_dereference_protected(swhash->swevent_hlist,
4924 lockdep_is_held(&swhash->hlist_mutex));
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004925}
4926
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004927static void swevent_hlist_release(struct swevent_htable *swhash)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004928{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004929 struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004930
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004931 if (!hlist)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004932 return;
4933
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004934 rcu_assign_pointer(swhash->swevent_hlist, NULL);
Lai Jiangshanfa4bbc42011-03-18 12:08:29 +08004935 kfree_rcu(hlist, rcu_head);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004936}
4937
4938static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
4939{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004940 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004941
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004942 mutex_lock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004943
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004944 if (!--swhash->hlist_refcount)
4945 swevent_hlist_release(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004946
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004947 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004948}
4949
4950static void swevent_hlist_put(struct perf_event *event)
4951{
4952 int cpu;
4953
4954 if (event->cpu != -1) {
4955 swevent_hlist_put_cpu(event, event->cpu);
4956 return;
4957 }
4958
4959 for_each_possible_cpu(cpu)
4960 swevent_hlist_put_cpu(event, cpu);
4961}
4962
4963static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
4964{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004965 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004966 int err = 0;
4967
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004968 mutex_lock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004969
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004970 if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004971 struct swevent_hlist *hlist;
4972
4973 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
4974 if (!hlist) {
4975 err = -ENOMEM;
4976 goto exit;
4977 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004978 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004979 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004980 swhash->hlist_refcount++;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02004981exit:
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004982 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004983
4984 return err;
4985}
4986
4987static int swevent_hlist_get(struct perf_event *event)
4988{
4989 int err;
4990 int cpu, failed_cpu;
4991
4992 if (event->cpu != -1)
4993 return swevent_hlist_get_cpu(event, event->cpu);
4994
4995 get_online_cpus();
4996 for_each_possible_cpu(cpu) {
4997 err = swevent_hlist_get_cpu(event, cpu);
4998 if (err) {
4999 failed_cpu = cpu;
5000 goto fail;
5001 }
5002 }
5003 put_online_cpus();
5004
5005 return 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02005006fail:
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005007 for_each_possible_cpu(cpu) {
5008 if (cpu == failed_cpu)
5009 break;
5010 swevent_hlist_put_cpu(event, cpu);
5011 }
5012
5013 put_online_cpus();
5014 return err;
5015}
5016
Jason Barond430d3d2011-03-16 17:29:47 -04005017struct jump_label_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
Frederic Weisbecker95476b62010-04-14 23:42:18 +02005018
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005019static void sw_perf_event_destroy(struct perf_event *event)
5020{
5021 u64 event_id = event->attr.config;
5022
5023 WARN_ON(event->parent);
5024
Peter Zijlstra7e54a5a2010-10-14 22:32:45 +02005025 jump_label_dec(&perf_swevent_enabled[event_id]);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005026 swevent_hlist_put(event);
5027}
5028
5029static int perf_swevent_init(struct perf_event *event)
5030{
5031 int event_id = event->attr.config;
5032
5033 if (event->attr.type != PERF_TYPE_SOFTWARE)
5034 return -ENOENT;
5035
5036 switch (event_id) {
5037 case PERF_COUNT_SW_CPU_CLOCK:
5038 case PERF_COUNT_SW_TASK_CLOCK:
5039 return -ENOENT;
5040
5041 default:
5042 break;
5043 }
5044
Dan Carpenterce677832010-10-24 21:50:42 +02005045 if (event_id >= PERF_COUNT_SW_MAX)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005046 return -ENOENT;
5047
5048 if (!event->parent) {
5049 int err;
5050
5051 err = swevent_hlist_get(event);
5052 if (err)
5053 return err;
5054
Peter Zijlstra7e54a5a2010-10-14 22:32:45 +02005055 jump_label_inc(&perf_swevent_enabled[event_id]);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005056 event->destroy = sw_perf_event_destroy;
5057 }
5058
5059 return 0;
5060}
5061
5062static struct pmu perf_swevent = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005063 .task_ctx_nr = perf_sw_context,
5064
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005065 .event_init = perf_swevent_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005066 .add = perf_swevent_add,
5067 .del = perf_swevent_del,
5068 .start = perf_swevent_start,
5069 .stop = perf_swevent_stop,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005070 .read = perf_swevent_read,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005071};
Frederic Weisbecker95476b62010-04-14 23:42:18 +02005072
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005073#ifdef CONFIG_EVENT_TRACING
5074
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005075static int perf_tp_filter_match(struct perf_event *event,
Frederic Weisbecker95476b62010-04-14 23:42:18 +02005076 struct perf_sample_data *data)
5077{
5078 void *record = data->raw->data;
5079
5080 if (likely(!event->filter) || filter_match_preds(event->filter, record))
5081 return 1;
5082 return 0;
5083}
5084
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005085static int perf_tp_event_match(struct perf_event *event,
5086 struct perf_sample_data *data,
5087 struct pt_regs *regs)
5088{
Frederic Weisbeckera0f7d0f2011-03-07 21:27:09 +01005089 if (event->hw.state & PERF_HES_STOPPED)
5090 return 0;
Peter Zijlstra580d6072010-05-20 20:54:31 +02005091 /*
5092 * All tracepoints are from kernel-space.
5093 */
5094 if (event->attr.exclude_kernel)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005095 return 0;
5096
5097 if (!perf_tp_filter_match(event, data))
5098 return 0;
5099
5100 return 1;
5101}
5102
5103void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
Peter Zijlstraecc55f82010-05-21 15:11:34 +02005104 struct pt_regs *regs, struct hlist_head *head, int rctx)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005105{
5106 struct perf_sample_data data;
5107 struct perf_event *event;
5108 struct hlist_node *node;
5109
5110 struct perf_raw_record raw = {
5111 .size = entry_size,
5112 .data = record,
5113 };
5114
5115 perf_sample_data_init(&data, addr);
5116 data.raw = &raw;
5117
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005118 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
5119 if (perf_tp_event_match(event, &data, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005120 perf_swevent_event(event, count, &data, regs);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005121 }
Peter Zijlstraecc55f82010-05-21 15:11:34 +02005122
5123 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005124}
5125EXPORT_SYMBOL_GPL(perf_tp_event);
5126
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005127static void tp_perf_event_destroy(struct perf_event *event)
5128{
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005129 perf_trace_destroy(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005130}
5131
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005132static int perf_tp_event_init(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005133{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005134 int err;
5135
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005136 if (event->attr.type != PERF_TYPE_TRACEPOINT)
5137 return -ENOENT;
5138
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005139 err = perf_trace_init(event);
5140 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005141 return err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005142
5143 event->destroy = tp_perf_event_destroy;
5144
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005145 return 0;
5146}
5147
5148static struct pmu perf_tracepoint = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005149 .task_ctx_nr = perf_sw_context,
5150
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005151 .event_init = perf_tp_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005152 .add = perf_trace_add,
5153 .del = perf_trace_del,
5154 .start = perf_swevent_start,
5155 .stop = perf_swevent_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005156 .read = perf_swevent_read,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005157};
5158
5159static inline void perf_tp_register(void)
5160{
Peter Zijlstra2e80a822010-11-17 23:17:36 +01005161 perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005162}
Li Zefan6fb29152009-10-15 11:21:42 +08005163
5164static int perf_event_set_filter(struct perf_event *event, void __user *arg)
5165{
5166 char *filter_str;
5167 int ret;
5168
5169 if (event->attr.type != PERF_TYPE_TRACEPOINT)
5170 return -EINVAL;
5171
5172 filter_str = strndup_user(arg, PAGE_SIZE);
5173 if (IS_ERR(filter_str))
5174 return PTR_ERR(filter_str);
5175
5176 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
5177
5178 kfree(filter_str);
5179 return ret;
5180}
5181
5182static void perf_event_free_filter(struct perf_event *event)
5183{
5184 ftrace_profile_free_filter(event);
5185}
5186
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005187#else
Li Zefan6fb29152009-10-15 11:21:42 +08005188
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005189static inline void perf_tp_register(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005190{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005191}
Li Zefan6fb29152009-10-15 11:21:42 +08005192
5193static int perf_event_set_filter(struct perf_event *event, void __user *arg)
5194{
5195 return -ENOENT;
5196}
5197
5198static void perf_event_free_filter(struct perf_event *event)
5199{
5200}
5201
Li Zefan07b139c2009-12-21 14:27:35 +08005202#endif /* CONFIG_EVENT_TRACING */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005203
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02005204#ifdef CONFIG_HAVE_HW_BREAKPOINT
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005205void perf_bp_event(struct perf_event *bp, void *data)
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02005206{
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005207 struct perf_sample_data sample;
5208 struct pt_regs *regs = data;
5209
Peter Zijlstradc1d6282010-03-03 15:55:04 +01005210 perf_sample_data_init(&sample, bp->attr.bp_addr);
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005211
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005212 if (!bp->hw.state && !perf_exclude_event(bp, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005213 perf_swevent_event(bp, 1, &sample, regs);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02005214}
5215#endif
5216
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005217/*
5218 * hrtimer based swevent callback
5219 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005220
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005221static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005222{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005223 enum hrtimer_restart ret = HRTIMER_RESTART;
5224 struct perf_sample_data data;
5225 struct pt_regs *regs;
5226 struct perf_event *event;
5227 u64 period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005228
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005229 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
Peter Zijlstraba3dd362011-02-15 12:41:46 +01005230
5231 if (event->state != PERF_EVENT_STATE_ACTIVE)
5232 return HRTIMER_NORESTART;
5233
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005234 event->pmu->read(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005235
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005236 perf_sample_data_init(&data, 0);
5237 data.period = event->hw.last_period;
5238 regs = get_irq_regs();
5239
5240 if (regs && !perf_exclude_event(event, regs)) {
5241 if (!(event->attr.exclude_idle && current->pid == 0))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005242 if (perf_event_overflow(event, &data, regs))
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005243 ret = HRTIMER_NORESTART;
5244 }
5245
5246 period = max_t(u64, 10000, event->hw.sample_period);
5247 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
5248
5249 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005250}
5251
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005252static void perf_swevent_start_hrtimer(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005253{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005254 struct hw_perf_event *hwc = &event->hw;
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01005255 s64 period;
5256
5257 if (!is_sampling_event(event))
5258 return;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005259
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01005260 period = local64_read(&hwc->period_left);
5261 if (period) {
5262 if (period < 0)
5263 period = 10000;
Peter Zijlstrafa407f32010-06-24 12:35:12 +02005264
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01005265 local64_set(&hwc->period_left, 0);
5266 } else {
5267 period = max_t(u64, 10000, hwc->sample_period);
5268 }
5269 __hrtimer_start_range_ns(&hwc->hrtimer,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005270 ns_to_ktime(period), 0,
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02005271 HRTIMER_MODE_REL_PINNED, 0);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005272}
5273
5274static void perf_swevent_cancel_hrtimer(struct perf_event *event)
5275{
5276 struct hw_perf_event *hwc = &event->hw;
5277
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01005278 if (is_sampling_event(event)) {
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005279 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
Peter Zijlstrafa407f32010-06-24 12:35:12 +02005280 local64_set(&hwc->period_left, ktime_to_ns(remaining));
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005281
5282 hrtimer_cancel(&hwc->hrtimer);
5283 }
5284}
5285
Peter Zijlstraba3dd362011-02-15 12:41:46 +01005286static void perf_swevent_init_hrtimer(struct perf_event *event)
5287{
5288 struct hw_perf_event *hwc = &event->hw;
5289
5290 if (!is_sampling_event(event))
5291 return;
5292
5293 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
5294 hwc->hrtimer.function = perf_swevent_hrtimer;
5295
5296 /*
5297 * Since hrtimers have a fixed rate, we can do a static freq->period
5298 * mapping and avoid the whole period adjust feedback stuff.
5299 */
5300 if (event->attr.freq) {
5301 long freq = event->attr.sample_freq;
5302
5303 event->attr.sample_period = NSEC_PER_SEC / freq;
5304 hwc->sample_period = event->attr.sample_period;
5305 local64_set(&hwc->period_left, hwc->sample_period);
5306 event->attr.freq = 0;
5307 }
5308}
5309
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005310/*
5311 * Software event: cpu wall time clock
5312 */
5313
5314static void cpu_clock_event_update(struct perf_event *event)
5315{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005316 s64 prev;
5317 u64 now;
5318
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005319 now = local_clock();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005320 prev = local64_xchg(&event->hw.prev_count, now);
5321 local64_add(now - prev, &event->count);
5322}
5323
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005324static void cpu_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005325{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005326 local64_set(&event->hw.prev_count, local_clock());
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005327 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005328}
5329
5330static void cpu_clock_event_stop(struct perf_event *event, int flags)
5331{
5332 perf_swevent_cancel_hrtimer(event);
5333 cpu_clock_event_update(event);
5334}
5335
5336static int cpu_clock_event_add(struct perf_event *event, int flags)
5337{
5338 if (flags & PERF_EF_START)
5339 cpu_clock_event_start(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005340
5341 return 0;
5342}
5343
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005344static void cpu_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005345{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005346 cpu_clock_event_stop(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005347}
5348
5349static void cpu_clock_event_read(struct perf_event *event)
5350{
5351 cpu_clock_event_update(event);
5352}
5353
5354static int cpu_clock_event_init(struct perf_event *event)
5355{
5356 if (event->attr.type != PERF_TYPE_SOFTWARE)
5357 return -ENOENT;
5358
5359 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
5360 return -ENOENT;
5361
Peter Zijlstraba3dd362011-02-15 12:41:46 +01005362 perf_swevent_init_hrtimer(event);
5363
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005364 return 0;
5365}
5366
5367static struct pmu perf_cpu_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005368 .task_ctx_nr = perf_sw_context,
5369
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005370 .event_init = cpu_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005371 .add = cpu_clock_event_add,
5372 .del = cpu_clock_event_del,
5373 .start = cpu_clock_event_start,
5374 .stop = cpu_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005375 .read = cpu_clock_event_read,
5376};
5377
5378/*
5379 * Software event: task time clock
5380 */
5381
5382static void task_clock_event_update(struct perf_event *event, u64 now)
5383{
5384 u64 prev;
5385 s64 delta;
5386
5387 prev = local64_xchg(&event->hw.prev_count, now);
5388 delta = now - prev;
5389 local64_add(delta, &event->count);
5390}
5391
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005392static void task_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005393{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005394 local64_set(&event->hw.prev_count, event->ctx->time);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005395 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005396}
5397
5398static void task_clock_event_stop(struct perf_event *event, int flags)
5399{
5400 perf_swevent_cancel_hrtimer(event);
5401 task_clock_event_update(event, event->ctx->time);
5402}
5403
5404static int task_clock_event_add(struct perf_event *event, int flags)
5405{
5406 if (flags & PERF_EF_START)
5407 task_clock_event_start(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005408
5409 return 0;
5410}
5411
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005412static void task_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005413{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005414 task_clock_event_stop(event, PERF_EF_UPDATE);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005415}
5416
5417static void task_clock_event_read(struct perf_event *event)
5418{
Peter Zijlstra768a06e2011-02-22 16:52:24 +01005419 u64 now = perf_clock();
5420 u64 delta = now - event->ctx->timestamp;
5421 u64 time = event->ctx->time + delta;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005422
5423 task_clock_event_update(event, time);
5424}
5425
5426static int task_clock_event_init(struct perf_event *event)
5427{
5428 if (event->attr.type != PERF_TYPE_SOFTWARE)
5429 return -ENOENT;
5430
5431 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
5432 return -ENOENT;
5433
Peter Zijlstraba3dd362011-02-15 12:41:46 +01005434 perf_swevent_init_hrtimer(event);
5435
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005436 return 0;
5437}
5438
5439static struct pmu perf_task_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005440 .task_ctx_nr = perf_sw_context,
5441
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005442 .event_init = task_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005443 .add = task_clock_event_add,
5444 .del = task_clock_event_del,
5445 .start = task_clock_event_start,
5446 .stop = task_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005447 .read = task_clock_event_read,
5448};
5449
Peter Zijlstraad5133b2010-06-15 12:22:39 +02005450static void perf_pmu_nop_void(struct pmu *pmu)
5451{
5452}
5453
5454static int perf_pmu_nop_int(struct pmu *pmu)
5455{
5456 return 0;
5457}
5458
5459static void perf_pmu_start_txn(struct pmu *pmu)
5460{
5461 perf_pmu_disable(pmu);
5462}
5463
5464static int perf_pmu_commit_txn(struct pmu *pmu)
5465{
5466 perf_pmu_enable(pmu);
5467 return 0;
5468}
5469
5470static void perf_pmu_cancel_txn(struct pmu *pmu)
5471{
5472 perf_pmu_enable(pmu);
5473}
5474
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005475/*
5476 * Ensures all contexts with the same task_ctx_nr have the same
5477 * pmu_cpu_context too.
5478 */
5479static void *find_pmu_context(int ctxn)
5480{
5481 struct pmu *pmu;
5482
5483 if (ctxn < 0)
5484 return NULL;
5485
5486 list_for_each_entry(pmu, &pmus, entry) {
5487 if (pmu->task_ctx_nr == ctxn)
5488 return pmu->pmu_cpu_context;
5489 }
5490
5491 return NULL;
5492}
5493
Peter Zijlstra51676952010-12-07 14:18:20 +01005494static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005495{
Peter Zijlstra51676952010-12-07 14:18:20 +01005496 int cpu;
5497
5498 for_each_possible_cpu(cpu) {
5499 struct perf_cpu_context *cpuctx;
5500
5501 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
5502
5503 if (cpuctx->active_pmu == old_pmu)
5504 cpuctx->active_pmu = pmu;
5505 }
5506}
5507
5508static void free_pmu_context(struct pmu *pmu)
5509{
5510 struct pmu *i;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005511
5512 mutex_lock(&pmus_lock);
5513 /*
5514 * Like a real lame refcount.
5515 */
Peter Zijlstra51676952010-12-07 14:18:20 +01005516 list_for_each_entry(i, &pmus, entry) {
5517 if (i->pmu_cpu_context == pmu->pmu_cpu_context) {
5518 update_pmu_context(i, pmu);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005519 goto out;
Peter Zijlstra51676952010-12-07 14:18:20 +01005520 }
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005521 }
5522
Peter Zijlstra51676952010-12-07 14:18:20 +01005523 free_percpu(pmu->pmu_cpu_context);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005524out:
5525 mutex_unlock(&pmus_lock);
5526}
Peter Zijlstra2e80a822010-11-17 23:17:36 +01005527static struct idr pmu_idr;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005528
Peter Zijlstraabe43402010-11-17 23:17:37 +01005529static ssize_t
5530type_show(struct device *dev, struct device_attribute *attr, char *page)
5531{
5532 struct pmu *pmu = dev_get_drvdata(dev);
5533
5534 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type);
5535}
5536
5537static struct device_attribute pmu_dev_attrs[] = {
5538 __ATTR_RO(type),
5539 __ATTR_NULL,
5540};
5541
5542static int pmu_bus_running;
5543static struct bus_type pmu_bus = {
5544 .name = "event_source",
5545 .dev_attrs = pmu_dev_attrs,
5546};
5547
5548static void pmu_dev_release(struct device *dev)
5549{
5550 kfree(dev);
5551}
5552
5553static int pmu_dev_alloc(struct pmu *pmu)
5554{
5555 int ret = -ENOMEM;
5556
5557 pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
5558 if (!pmu->dev)
5559 goto out;
5560
5561 device_initialize(pmu->dev);
5562 ret = dev_set_name(pmu->dev, "%s", pmu->name);
5563 if (ret)
5564 goto free_dev;
5565
5566 dev_set_drvdata(pmu->dev, pmu);
5567 pmu->dev->bus = &pmu_bus;
5568 pmu->dev->release = pmu_dev_release;
5569 ret = device_add(pmu->dev);
5570 if (ret)
5571 goto free_dev;
5572
5573out:
5574 return ret;
5575
5576free_dev:
5577 put_device(pmu->dev);
5578 goto out;
5579}
5580
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01005581static struct lock_class_key cpuctx_mutex;
Peter Zijlstrafacc4302011-04-09 21:17:42 +02005582static struct lock_class_key cpuctx_lock;
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01005583
Peter Zijlstra2e80a822010-11-17 23:17:36 +01005584int perf_pmu_register(struct pmu *pmu, char *name, int type)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005585{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005586 int cpu, ret;
Peter Zijlstra33696fc2010-06-14 08:49:00 +02005587
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005588 mutex_lock(&pmus_lock);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02005589 ret = -ENOMEM;
5590 pmu->pmu_disable_count = alloc_percpu(int);
5591 if (!pmu->pmu_disable_count)
5592 goto unlock;
Peter Zijlstraad5133b2010-06-15 12:22:39 +02005593
Peter Zijlstra2e80a822010-11-17 23:17:36 +01005594 pmu->type = -1;
5595 if (!name)
5596 goto skip_type;
5597 pmu->name = name;
5598
5599 if (type < 0) {
5600 int err = idr_pre_get(&pmu_idr, GFP_KERNEL);
5601 if (!err)
5602 goto free_pdc;
5603
5604 err = idr_get_new_above(&pmu_idr, pmu, PERF_TYPE_MAX, &type);
5605 if (err) {
5606 ret = err;
5607 goto free_pdc;
5608 }
5609 }
5610 pmu->type = type;
5611
Peter Zijlstraabe43402010-11-17 23:17:37 +01005612 if (pmu_bus_running) {
5613 ret = pmu_dev_alloc(pmu);
5614 if (ret)
5615 goto free_idr;
5616 }
5617
Peter Zijlstra2e80a822010-11-17 23:17:36 +01005618skip_type:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005619 pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
5620 if (pmu->pmu_cpu_context)
5621 goto got_cpu_context;
5622
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005623 pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
5624 if (!pmu->pmu_cpu_context)
Peter Zijlstraabe43402010-11-17 23:17:37 +01005625 goto free_dev;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005626
5627 for_each_possible_cpu(cpu) {
5628 struct perf_cpu_context *cpuctx;
5629
5630 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Peter Zijlstraeb184472010-09-07 15:55:13 +02005631 __perf_event_init_context(&cpuctx->ctx);
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01005632 lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex);
Peter Zijlstrafacc4302011-04-09 21:17:42 +02005633 lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02005634 cpuctx->ctx.type = cpu_context;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005635 cpuctx->ctx.pmu = pmu;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02005636 cpuctx->jiffies_interval = 1;
5637 INIT_LIST_HEAD(&cpuctx->rotation_list);
Peter Zijlstra51676952010-12-07 14:18:20 +01005638 cpuctx->active_pmu = pmu;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005639 }
5640
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005641got_cpu_context:
Peter Zijlstraad5133b2010-06-15 12:22:39 +02005642 if (!pmu->start_txn) {
5643 if (pmu->pmu_enable) {
5644 /*
5645 * If we have pmu_enable/pmu_disable calls, install
5646 * transaction stubs that use that to try and batch
5647 * hardware accesses.
5648 */
5649 pmu->start_txn = perf_pmu_start_txn;
5650 pmu->commit_txn = perf_pmu_commit_txn;
5651 pmu->cancel_txn = perf_pmu_cancel_txn;
5652 } else {
5653 pmu->start_txn = perf_pmu_nop_void;
5654 pmu->commit_txn = perf_pmu_nop_int;
5655 pmu->cancel_txn = perf_pmu_nop_void;
5656 }
5657 }
5658
5659 if (!pmu->pmu_enable) {
5660 pmu->pmu_enable = perf_pmu_nop_void;
5661 pmu->pmu_disable = perf_pmu_nop_void;
5662 }
5663
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005664 list_add_rcu(&pmu->entry, &pmus);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02005665 ret = 0;
5666unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005667 mutex_unlock(&pmus_lock);
5668
Peter Zijlstra33696fc2010-06-14 08:49:00 +02005669 return ret;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005670
Peter Zijlstraabe43402010-11-17 23:17:37 +01005671free_dev:
5672 device_del(pmu->dev);
5673 put_device(pmu->dev);
5674
Peter Zijlstra2e80a822010-11-17 23:17:36 +01005675free_idr:
5676 if (pmu->type >= PERF_TYPE_MAX)
5677 idr_remove(&pmu_idr, pmu->type);
5678
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005679free_pdc:
5680 free_percpu(pmu->pmu_disable_count);
5681 goto unlock;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005682}
5683
5684void perf_pmu_unregister(struct pmu *pmu)
5685{
5686 mutex_lock(&pmus_lock);
5687 list_del_rcu(&pmu->entry);
5688 mutex_unlock(&pmus_lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005689
5690 /*
Peter Zijlstracde8e882010-09-13 11:06:55 +02005691 * We dereference the pmu list under both SRCU and regular RCU, so
5692 * synchronize against both of those.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005693 */
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005694 synchronize_srcu(&pmus_srcu);
Peter Zijlstracde8e882010-09-13 11:06:55 +02005695 synchronize_rcu();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005696
Peter Zijlstra33696fc2010-06-14 08:49:00 +02005697 free_percpu(pmu->pmu_disable_count);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01005698 if (pmu->type >= PERF_TYPE_MAX)
5699 idr_remove(&pmu_idr, pmu->type);
Peter Zijlstraabe43402010-11-17 23:17:37 +01005700 device_del(pmu->dev);
5701 put_device(pmu->dev);
Peter Zijlstra51676952010-12-07 14:18:20 +01005702 free_pmu_context(pmu);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005703}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005704
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005705struct pmu *perf_init_event(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005706{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02005707 struct pmu *pmu = NULL;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005708 int idx;
Lin Ming940c5b22011-02-27 21:13:31 +08005709 int ret;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005710
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005711 idx = srcu_read_lock(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01005712
5713 rcu_read_lock();
5714 pmu = idr_find(&pmu_idr, event->attr.type);
5715 rcu_read_unlock();
Lin Ming940c5b22011-02-27 21:13:31 +08005716 if (pmu) {
5717 ret = pmu->event_init(event);
5718 if (ret)
5719 pmu = ERR_PTR(ret);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01005720 goto unlock;
Lin Ming940c5b22011-02-27 21:13:31 +08005721 }
Peter Zijlstra2e80a822010-11-17 23:17:36 +01005722
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005723 list_for_each_entry_rcu(pmu, &pmus, entry) {
Lin Ming940c5b22011-02-27 21:13:31 +08005724 ret = pmu->event_init(event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005725 if (!ret)
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02005726 goto unlock;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005727
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005728 if (ret != -ENOENT) {
5729 pmu = ERR_PTR(ret);
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02005730 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005731 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005732 }
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02005733 pmu = ERR_PTR(-ENOENT);
5734unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005735 srcu_read_unlock(&pmus_srcu, idx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005736
5737 return pmu;
5738}
5739
5740/*
5741 * Allocate and initialize a event structure
5742 */
5743static struct perf_event *
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005744perf_event_alloc(struct perf_event_attr *attr, int cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +02005745 struct task_struct *task,
5746 struct perf_event *group_leader,
5747 struct perf_event *parent_event,
5748 perf_overflow_handler_t overflow_handler)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005749{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02005750 struct pmu *pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005751 struct perf_event *event;
5752 struct hw_perf_event *hwc;
5753 long err;
5754
Oleg Nesterov66832eb2011-01-18 17:10:32 +01005755 if ((unsigned)cpu >= nr_cpu_ids) {
5756 if (!task || cpu != -1)
5757 return ERR_PTR(-EINVAL);
5758 }
5759
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005760 event = kzalloc(sizeof(*event), GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005761 if (!event)
5762 return ERR_PTR(-ENOMEM);
5763
5764 /*
5765 * Single events are their own group leaders, with an
5766 * empty sibling list:
5767 */
5768 if (!group_leader)
5769 group_leader = event;
5770
5771 mutex_init(&event->child_mutex);
5772 INIT_LIST_HEAD(&event->child_list);
5773
5774 INIT_LIST_HEAD(&event->group_entry);
5775 INIT_LIST_HEAD(&event->event_entry);
5776 INIT_LIST_HEAD(&event->sibling_list);
5777 init_waitqueue_head(&event->waitq);
Peter Zijlstrae360adb2010-10-14 14:01:34 +08005778 init_irq_work(&event->pending, perf_pending_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005779
5780 mutex_init(&event->mmap_mutex);
5781
5782 event->cpu = cpu;
5783 event->attr = *attr;
5784 event->group_leader = group_leader;
5785 event->pmu = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005786 event->oncpu = -1;
5787
5788 event->parent = parent_event;
5789
5790 event->ns = get_pid_ns(current->nsproxy->pid_ns);
5791 event->id = atomic64_inc_return(&perf_event_id);
5792
5793 event->state = PERF_EVENT_STATE_INACTIVE;
5794
Peter Zijlstrad580ff82010-10-14 17:43:23 +02005795 if (task) {
5796 event->attach_state = PERF_ATTACH_TASK;
5797#ifdef CONFIG_HAVE_HW_BREAKPOINT
5798 /*
5799 * hw_breakpoint is a bit difficult here..
5800 */
5801 if (attr->type == PERF_TYPE_BREAKPOINT)
5802 event->hw.bp_target = task;
5803#endif
5804 }
5805
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01005806 if (!overflow_handler && parent_event)
5807 overflow_handler = parent_event->overflow_handler;
Oleg Nesterov66832eb2011-01-18 17:10:32 +01005808
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01005809 event->overflow_handler = overflow_handler;
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02005810
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005811 if (attr->disabled)
5812 event->state = PERF_EVENT_STATE_OFF;
5813
5814 pmu = NULL;
5815
5816 hwc = &event->hw;
5817 hwc->sample_period = attr->sample_period;
5818 if (attr->freq && attr->sample_freq)
5819 hwc->sample_period = 1;
5820 hwc->last_period = hwc->sample_period;
5821
Peter Zijlstrae7850592010-05-21 14:43:08 +02005822 local64_set(&hwc->period_left, hwc->sample_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005823
5824 /*
5825 * we currently do not support PERF_FORMAT_GROUP on inherited events
5826 */
5827 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
5828 goto done;
5829
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005830 pmu = perf_init_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005831
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005832done:
5833 err = 0;
5834 if (!pmu)
5835 err = -EINVAL;
5836 else if (IS_ERR(pmu))
5837 err = PTR_ERR(pmu);
5838
5839 if (err) {
5840 if (event->ns)
5841 put_pid_ns(event->ns);
5842 kfree(event);
5843 return ERR_PTR(err);
5844 }
5845
5846 event->pmu = pmu;
5847
5848 if (!event->parent) {
Peter Zijlstra82cd6de2010-10-14 17:57:23 +02005849 if (event->attach_state & PERF_ATTACH_TASK)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005850 jump_label_inc(&perf_sched_events);
Eric B Munson3af9e852010-05-18 15:30:49 +01005851 if (event->attr.mmap || event->attr.mmap_data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005852 atomic_inc(&nr_mmap_events);
5853 if (event->attr.comm)
5854 atomic_inc(&nr_comm_events);
5855 if (event->attr.task)
5856 atomic_inc(&nr_task_events);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02005857 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
5858 err = get_callchain_buffers();
5859 if (err) {
5860 free_event(event);
5861 return ERR_PTR(err);
5862 }
5863 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005864 }
5865
5866 return event;
5867}
5868
5869static int perf_copy_attr(struct perf_event_attr __user *uattr,
5870 struct perf_event_attr *attr)
5871{
5872 u32 size;
5873 int ret;
5874
5875 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
5876 return -EFAULT;
5877
5878 /*
5879 * zero the full structure, so that a short copy will be nice.
5880 */
5881 memset(attr, 0, sizeof(*attr));
5882
5883 ret = get_user(size, &uattr->size);
5884 if (ret)
5885 return ret;
5886
5887 if (size > PAGE_SIZE) /* silly large */
5888 goto err_size;
5889
5890 if (!size) /* abi compat */
5891 size = PERF_ATTR_SIZE_VER0;
5892
5893 if (size < PERF_ATTR_SIZE_VER0)
5894 goto err_size;
5895
5896 /*
5897 * If we're handed a bigger struct than we know of,
5898 * ensure all the unknown bits are 0 - i.e. new
5899 * user-space does not rely on any kernel feature
5900 * extensions we dont know about yet.
5901 */
5902 if (size > sizeof(*attr)) {
5903 unsigned char __user *addr;
5904 unsigned char __user *end;
5905 unsigned char val;
5906
5907 addr = (void __user *)uattr + sizeof(*attr);
5908 end = (void __user *)uattr + size;
5909
5910 for (; addr < end; addr++) {
5911 ret = get_user(val, addr);
5912 if (ret)
5913 return ret;
5914 if (val)
5915 goto err_size;
5916 }
5917 size = sizeof(*attr);
5918 }
5919
5920 ret = copy_from_user(attr, uattr, size);
5921 if (ret)
5922 return -EFAULT;
5923
5924 /*
5925 * If the type exists, the corresponding creation will verify
5926 * the attr->config.
5927 */
5928 if (attr->type >= PERF_TYPE_MAX)
5929 return -EINVAL;
5930
Mahesh Salgaonkarcd757642010-01-30 10:25:18 +05305931 if (attr->__reserved_1)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005932 return -EINVAL;
5933
5934 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
5935 return -EINVAL;
5936
5937 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
5938 return -EINVAL;
5939
5940out:
5941 return ret;
5942
5943err_size:
5944 put_user(sizeof(*attr), &uattr->size);
5945 ret = -E2BIG;
5946 goto out;
5947}
5948
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005949static int
5950perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005951{
Frederic Weisbecker76369132011-05-19 19:55:04 +02005952 struct ring_buffer *rb = NULL, *old_rb = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005953 int ret = -EINVAL;
5954
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005955 if (!output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005956 goto set;
5957
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005958 /* don't allow circular references */
5959 if (event == output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005960 goto out;
5961
Peter Zijlstra0f139302010-05-20 14:35:15 +02005962 /*
5963 * Don't allow cross-cpu buffers
5964 */
5965 if (output_event->cpu != event->cpu)
5966 goto out;
5967
5968 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +02005969 * If its not a per-cpu rb, it must be the same task.
Peter Zijlstra0f139302010-05-20 14:35:15 +02005970 */
5971 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
5972 goto out;
5973
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005974set:
5975 mutex_lock(&event->mmap_mutex);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005976 /* Can't redirect output if we've got an active mmap() */
5977 if (atomic_read(&event->mmap_count))
5978 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005979
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005980 if (output_event) {
Frederic Weisbecker76369132011-05-19 19:55:04 +02005981 /* get the rb we want to redirect to */
5982 rb = ring_buffer_get(output_event);
5983 if (!rb)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005984 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005985 }
5986
Frederic Weisbecker76369132011-05-19 19:55:04 +02005987 old_rb = event->rb;
5988 rcu_assign_pointer(event->rb, rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005989 ret = 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005990unlock:
5991 mutex_unlock(&event->mmap_mutex);
5992
Frederic Weisbecker76369132011-05-19 19:55:04 +02005993 if (old_rb)
5994 ring_buffer_put(old_rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005995out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005996 return ret;
5997}
5998
5999/**
6000 * sys_perf_event_open - open a performance event, associate it to a task/cpu
6001 *
6002 * @attr_uptr: event_id type attributes for monitoring/sampling
6003 * @pid: target pid
6004 * @cpu: target cpu
6005 * @group_fd: group leader event fd
6006 */
6007SYSCALL_DEFINE5(perf_event_open,
6008 struct perf_event_attr __user *, attr_uptr,
6009 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
6010{
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006011 struct perf_event *group_leader = NULL, *output_event = NULL;
6012 struct perf_event *event, *sibling;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006013 struct perf_event_attr attr;
6014 struct perf_event_context *ctx;
6015 struct file *event_file = NULL;
6016 struct file *group_file = NULL;
Matt Helsley38a81da2010-09-13 13:01:20 -07006017 struct task_struct *task = NULL;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006018 struct pmu *pmu;
Al Viroea635c62010-05-26 17:40:29 -04006019 int event_fd;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006020 int move_group = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006021 int fput_needed = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006022 int err;
6023
6024 /* for future expandability... */
Stephane Eraniane5d13672011-02-14 11:20:01 +02006025 if (flags & ~PERF_FLAG_ALL)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006026 return -EINVAL;
6027
6028 err = perf_copy_attr(attr_uptr, &attr);
6029 if (err)
6030 return err;
6031
6032 if (!attr.exclude_kernel) {
6033 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
6034 return -EACCES;
6035 }
6036
6037 if (attr.freq) {
6038 if (attr.sample_freq > sysctl_perf_event_sample_rate)
6039 return -EINVAL;
6040 }
6041
Stephane Eraniane5d13672011-02-14 11:20:01 +02006042 /*
6043 * In cgroup mode, the pid argument is used to pass the fd
6044 * opened to the cgroup directory in cgroupfs. The cpu argument
6045 * designates the cpu on which to monitor threads from that
6046 * cgroup.
6047 */
6048 if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1))
6049 return -EINVAL;
6050
Al Viroea635c62010-05-26 17:40:29 -04006051 event_fd = get_unused_fd_flags(O_RDWR);
6052 if (event_fd < 0)
6053 return event_fd;
6054
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006055 if (group_fd != -1) {
6056 group_leader = perf_fget_light(group_fd, &fput_needed);
6057 if (IS_ERR(group_leader)) {
6058 err = PTR_ERR(group_leader);
Stephane Eraniand14b12d2010-09-17 11:28:47 +02006059 goto err_fd;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006060 }
6061 group_file = group_leader->filp;
6062 if (flags & PERF_FLAG_FD_OUTPUT)
6063 output_event = group_leader;
6064 if (flags & PERF_FLAG_FD_NO_GROUP)
6065 group_leader = NULL;
6066 }
6067
Stephane Eraniane5d13672011-02-14 11:20:01 +02006068 if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) {
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02006069 task = find_lively_task_by_vpid(pid);
6070 if (IS_ERR(task)) {
6071 err = PTR_ERR(task);
6072 goto err_group_fd;
6073 }
6074 }
6075
Peter Zijlstrad580ff82010-10-14 17:43:23 +02006076 event = perf_event_alloc(&attr, cpu, task, group_leader, NULL, NULL);
Stephane Eraniand14b12d2010-09-17 11:28:47 +02006077 if (IS_ERR(event)) {
6078 err = PTR_ERR(event);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02006079 goto err_task;
Stephane Eraniand14b12d2010-09-17 11:28:47 +02006080 }
6081
Stephane Eraniane5d13672011-02-14 11:20:01 +02006082 if (flags & PERF_FLAG_PID_CGROUP) {
6083 err = perf_cgroup_connect(pid, event, &attr, group_leader);
6084 if (err)
6085 goto err_alloc;
Peter Zijlstra08309372011-03-03 11:31:20 +01006086 /*
6087 * one more event:
6088 * - that has cgroup constraint on event->cpu
6089 * - that may need work on context switch
6090 */
6091 atomic_inc(&per_cpu(perf_cgroup_events, event->cpu));
6092 jump_label_inc(&perf_sched_events);
Stephane Eraniane5d13672011-02-14 11:20:01 +02006093 }
6094
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006095 /*
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006096 * Special case software events and allow them to be part of
6097 * any hardware group.
6098 */
6099 pmu = event->pmu;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006100
6101 if (group_leader &&
6102 (is_software_event(event) != is_software_event(group_leader))) {
6103 if (is_software_event(event)) {
6104 /*
6105 * If event and group_leader are not both a software
6106 * event, and event is, then group leader is not.
6107 *
6108 * Allow the addition of software events to !software
6109 * groups, this is safe because software events never
6110 * fail to schedule.
6111 */
6112 pmu = group_leader->pmu;
6113 } else if (is_software_event(group_leader) &&
6114 (group_leader->group_flags & PERF_GROUP_SOFTWARE)) {
6115 /*
6116 * In case the group is a pure software group, and we
6117 * try to add a hardware event, move the whole group to
6118 * the hardware context.
6119 */
6120 move_group = 1;
6121 }
6122 }
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006123
6124 /*
6125 * Get the target context (task or percpu):
6126 */
Matt Helsley38a81da2010-09-13 13:01:20 -07006127 ctx = find_get_context(pmu, task, cpu);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006128 if (IS_ERR(ctx)) {
6129 err = PTR_ERR(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02006130 goto err_alloc;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006131 }
6132
Peter Zijlstrafd1edb32011-03-28 13:13:56 +02006133 if (task) {
6134 put_task_struct(task);
6135 task = NULL;
6136 }
6137
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006138 /*
6139 * Look up the group leader (we will attach this event to it):
6140 */
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006141 if (group_leader) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006142 err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006143
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006144 /*
6145 * Do not allow a recursive hierarchy (this new sibling
6146 * becoming part of another group-sibling):
6147 */
6148 if (group_leader->group_leader != group_leader)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006149 goto err_context;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006150 /*
6151 * Do not allow to attach to a group in a different
6152 * task or CPU context:
6153 */
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006154 if (move_group) {
6155 if (group_leader->ctx->type != ctx->type)
6156 goto err_context;
6157 } else {
6158 if (group_leader->ctx != ctx)
6159 goto err_context;
6160 }
6161
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006162 /*
6163 * Only a group leader can be exclusive or pinned
6164 */
6165 if (attr.exclusive || attr.pinned)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006166 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006167 }
6168
6169 if (output_event) {
6170 err = perf_event_set_output(event, output_event);
6171 if (err)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006172 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006173 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006174
Al Viroea635c62010-05-26 17:40:29 -04006175 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, O_RDWR);
6176 if (IS_ERR(event_file)) {
6177 err = PTR_ERR(event_file);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006178 goto err_context;
Al Viroea635c62010-05-26 17:40:29 -04006179 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006180
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006181 if (move_group) {
6182 struct perf_event_context *gctx = group_leader->ctx;
6183
6184 mutex_lock(&gctx->mutex);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01006185 perf_remove_from_context(group_leader);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006186 list_for_each_entry(sibling, &group_leader->sibling_list,
6187 group_entry) {
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01006188 perf_remove_from_context(sibling);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006189 put_ctx(gctx);
6190 }
6191 mutex_unlock(&gctx->mutex);
6192 put_ctx(gctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006193 }
6194
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006195 event->filp = event_file;
6196 WARN_ON_ONCE(ctx->parent_ctx);
6197 mutex_lock(&ctx->mutex);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006198
6199 if (move_group) {
6200 perf_install_in_context(ctx, group_leader, cpu);
6201 get_ctx(ctx);
6202 list_for_each_entry(sibling, &group_leader->sibling_list,
6203 group_entry) {
6204 perf_install_in_context(ctx, sibling, cpu);
6205 get_ctx(ctx);
6206 }
6207 }
6208
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006209 perf_install_in_context(ctx, event, cpu);
6210 ++ctx->generation;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01006211 perf_unpin_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006212 mutex_unlock(&ctx->mutex);
6213
6214 event->owner = current;
Peter Zijlstra88821352010-11-09 19:01:43 +01006215
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006216 mutex_lock(&current->perf_event_mutex);
6217 list_add_tail(&event->owner_entry, &current->perf_event_list);
6218 mutex_unlock(&current->perf_event_mutex);
6219
Peter Zijlstra8a495422010-05-27 15:47:49 +02006220 /*
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02006221 * Precalculate sample_data sizes
6222 */
6223 perf_event__header_size(event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02006224 perf_event__id_header_size(event);
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02006225
6226 /*
Peter Zijlstra8a495422010-05-27 15:47:49 +02006227 * Drop the reference on the group_event after placing the
6228 * new event on the sibling_list. This ensures destruction
6229 * of the group leader will find the pointer to itself in
6230 * perf_group_detach().
6231 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006232 fput_light(group_file, fput_needed);
Al Viroea635c62010-05-26 17:40:29 -04006233 fd_install(event_fd, event_file);
6234 return event_fd;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006235
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006236err_context:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01006237 perf_unpin_context(ctx);
Al Viroea635c62010-05-26 17:40:29 -04006238 put_ctx(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02006239err_alloc:
6240 free_event(event);
Peter Zijlstrae7d0bc02010-10-14 16:54:51 +02006241err_task:
6242 if (task)
6243 put_task_struct(task);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006244err_group_fd:
6245 fput_light(group_file, fput_needed);
Al Viroea635c62010-05-26 17:40:29 -04006246err_fd:
6247 put_unused_fd(event_fd);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006248 return err;
6249}
6250
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006251/**
6252 * perf_event_create_kernel_counter
6253 *
6254 * @attr: attributes of the counter to create
6255 * @cpu: cpu in which the counter is bound
Matt Helsley38a81da2010-09-13 13:01:20 -07006256 * @task: task to profile (NULL for percpu)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006257 */
6258struct perf_event *
6259perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
Matt Helsley38a81da2010-09-13 13:01:20 -07006260 struct task_struct *task,
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01006261 perf_overflow_handler_t overflow_handler)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006262{
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006263 struct perf_event_context *ctx;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006264 struct perf_event *event;
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006265 int err;
6266
6267 /*
6268 * Get the target context (task or percpu):
6269 */
6270
Peter Zijlstrad580ff82010-10-14 17:43:23 +02006271 event = perf_event_alloc(attr, cpu, task, NULL, NULL, overflow_handler);
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01006272 if (IS_ERR(event)) {
6273 err = PTR_ERR(event);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006274 goto err;
6275 }
6276
Matt Helsley38a81da2010-09-13 13:01:20 -07006277 ctx = find_get_context(event->pmu, task, cpu);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006278 if (IS_ERR(ctx)) {
6279 err = PTR_ERR(ctx);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006280 goto err_free;
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01006281 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006282
6283 event->filp = NULL;
6284 WARN_ON_ONCE(ctx->parent_ctx);
6285 mutex_lock(&ctx->mutex);
6286 perf_install_in_context(ctx, event, cpu);
6287 ++ctx->generation;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01006288 perf_unpin_context(ctx);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006289 mutex_unlock(&ctx->mutex);
6290
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006291 return event;
6292
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006293err_free:
6294 free_event(event);
6295err:
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01006296 return ERR_PTR(err);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006297}
6298EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
6299
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006300static void sync_child_event(struct perf_event *child_event,
6301 struct task_struct *child)
6302{
6303 struct perf_event *parent_event = child_event->parent;
6304 u64 child_val;
6305
6306 if (child_event->attr.inherit_stat)
6307 perf_event_read_event(child_event, child);
6308
Peter Zijlstrab5e58792010-05-21 14:43:12 +02006309 child_val = perf_event_count(child_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006310
6311 /*
6312 * Add back the child's count to the parent's count:
6313 */
Peter Zijlstraa6e6dea2010-05-21 14:27:58 +02006314 atomic64_add(child_val, &parent_event->child_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006315 atomic64_add(child_event->total_time_enabled,
6316 &parent_event->child_total_time_enabled);
6317 atomic64_add(child_event->total_time_running,
6318 &parent_event->child_total_time_running);
6319
6320 /*
6321 * Remove this event from the parent's list
6322 */
6323 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
6324 mutex_lock(&parent_event->child_mutex);
6325 list_del_init(&child_event->child_list);
6326 mutex_unlock(&parent_event->child_mutex);
6327
6328 /*
6329 * Release the parent event, if this was the last
6330 * reference to it.
6331 */
6332 fput(parent_event->filp);
6333}
6334
6335static void
6336__perf_event_exit_task(struct perf_event *child_event,
6337 struct perf_event_context *child_ctx,
6338 struct task_struct *child)
6339{
Peter Zijlstra38b435b2011-03-15 14:37:10 +01006340 if (child_event->parent) {
6341 raw_spin_lock_irq(&child_ctx->lock);
6342 perf_group_detach(child_event);
6343 raw_spin_unlock_irq(&child_ctx->lock);
6344 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006345
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01006346 perf_remove_from_context(child_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006347
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006348 /*
Peter Zijlstra38b435b2011-03-15 14:37:10 +01006349 * It can happen that the parent exits first, and has events
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006350 * that are still around due to the child reference. These
Peter Zijlstra38b435b2011-03-15 14:37:10 +01006351 * events need to be zapped.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006352 */
Peter Zijlstra38b435b2011-03-15 14:37:10 +01006353 if (child_event->parent) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006354 sync_child_event(child_event, child);
6355 free_event(child_event);
6356 }
6357}
6358
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006359static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006360{
6361 struct perf_event *child_event, *tmp;
6362 struct perf_event_context *child_ctx;
6363 unsigned long flags;
6364
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006365 if (likely(!child->perf_event_ctxp[ctxn])) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006366 perf_event_task(child, NULL, 0);
6367 return;
6368 }
6369
6370 local_irq_save(flags);
6371 /*
6372 * We can't reschedule here because interrupts are disabled,
6373 * and either child is current or it is a task that can't be
6374 * scheduled, so we are now safe from rescheduling changing
6375 * our context.
6376 */
Oleg Nesterov806839b2011-01-21 18:45:47 +01006377 child_ctx = rcu_dereference_raw(child->perf_event_ctxp[ctxn]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006378
6379 /*
6380 * Take the context lock here so that if find_get_context is
6381 * reading child->perf_event_ctxp, we wait until it has
6382 * incremented the context's refcount before we do put_ctx below.
6383 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +01006384 raw_spin_lock(&child_ctx->lock);
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02006385 task_ctx_sched_out(child_ctx);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006386 child->perf_event_ctxp[ctxn] = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006387 /*
6388 * If this context is a clone; unclone it so it can't get
6389 * swapped to another process while we're removing all
6390 * the events from it.
6391 */
6392 unclone_ctx(child_ctx);
Peter Zijlstra5e942bb2009-11-23 11:37:26 +01006393 update_context_time(child_ctx);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01006394 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006395
6396 /*
6397 * Report the task dead after unscheduling the events so that we
6398 * won't get any samples after PERF_RECORD_EXIT. We can however still
6399 * get a few PERF_RECORD_READ events.
6400 */
6401 perf_event_task(child, child_ctx, 0);
6402
6403 /*
6404 * We can recurse on the same lock type through:
6405 *
6406 * __perf_event_exit_task()
6407 * sync_child_event()
6408 * fput(parent_event->filp)
6409 * perf_release()
6410 * mutex_lock(&ctx->mutex)
6411 *
6412 * But since its the parent context it won't be the same instance.
6413 */
Peter Zijlstraa0507c82010-05-06 15:42:53 +02006414 mutex_lock(&child_ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006415
6416again:
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006417 list_for_each_entry_safe(child_event, tmp, &child_ctx->pinned_groups,
6418 group_entry)
6419 __perf_event_exit_task(child_event, child_ctx, child);
6420
6421 list_for_each_entry_safe(child_event, tmp, &child_ctx->flexible_groups,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006422 group_entry)
6423 __perf_event_exit_task(child_event, child_ctx, child);
6424
6425 /*
6426 * If the last event was a group event, it will have appended all
6427 * its siblings to the list, but we obtained 'tmp' before that which
6428 * will still point to the list head terminating the iteration.
6429 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006430 if (!list_empty(&child_ctx->pinned_groups) ||
6431 !list_empty(&child_ctx->flexible_groups))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006432 goto again;
6433
6434 mutex_unlock(&child_ctx->mutex);
6435
6436 put_ctx(child_ctx);
6437}
6438
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006439/*
6440 * When a child task exits, feed back event values to parent events.
6441 */
6442void perf_event_exit_task(struct task_struct *child)
6443{
Peter Zijlstra88821352010-11-09 19:01:43 +01006444 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006445 int ctxn;
6446
Peter Zijlstra88821352010-11-09 19:01:43 +01006447 mutex_lock(&child->perf_event_mutex);
6448 list_for_each_entry_safe(event, tmp, &child->perf_event_list,
6449 owner_entry) {
6450 list_del_init(&event->owner_entry);
6451
6452 /*
6453 * Ensure the list deletion is visible before we clear
6454 * the owner, closes a race against perf_release() where
6455 * we need to serialize on the owner->perf_event_mutex.
6456 */
6457 smp_wmb();
6458 event->owner = NULL;
6459 }
6460 mutex_unlock(&child->perf_event_mutex);
6461
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006462 for_each_task_context_nr(ctxn)
6463 perf_event_exit_task_context(child, ctxn);
6464}
6465
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006466static void perf_free_event(struct perf_event *event,
6467 struct perf_event_context *ctx)
6468{
6469 struct perf_event *parent = event->parent;
6470
6471 if (WARN_ON_ONCE(!parent))
6472 return;
6473
6474 mutex_lock(&parent->child_mutex);
6475 list_del_init(&event->child_list);
6476 mutex_unlock(&parent->child_mutex);
6477
6478 fput(parent->filp);
6479
Peter Zijlstra8a495422010-05-27 15:47:49 +02006480 perf_group_detach(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006481 list_del_event(event, ctx);
6482 free_event(event);
6483}
6484
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006485/*
6486 * free an unexposed, unused context as created by inheritance by
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006487 * perf_event_init_task below, used by fork() in case of fail.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006488 */
6489void perf_event_free_task(struct task_struct *task)
6490{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006491 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006492 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006493 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006494
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006495 for_each_task_context_nr(ctxn) {
6496 ctx = task->perf_event_ctxp[ctxn];
6497 if (!ctx)
6498 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006499
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006500 mutex_lock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006501again:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006502 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
6503 group_entry)
6504 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006505
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006506 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
6507 group_entry)
6508 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006509
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006510 if (!list_empty(&ctx->pinned_groups) ||
6511 !list_empty(&ctx->flexible_groups))
6512 goto again;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006513
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006514 mutex_unlock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006515
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006516 put_ctx(ctx);
6517 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006518}
6519
Peter Zijlstra4e231c72010-09-09 21:01:59 +02006520void perf_event_delayed_put(struct task_struct *task)
6521{
6522 int ctxn;
6523
6524 for_each_task_context_nr(ctxn)
6525 WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
6526}
6527
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02006528/*
6529 * inherit a event from parent task to child task:
6530 */
6531static struct perf_event *
6532inherit_event(struct perf_event *parent_event,
6533 struct task_struct *parent,
6534 struct perf_event_context *parent_ctx,
6535 struct task_struct *child,
6536 struct perf_event *group_leader,
6537 struct perf_event_context *child_ctx)
6538{
6539 struct perf_event *child_event;
Peter Zijlstracee010e2010-09-10 12:51:54 +02006540 unsigned long flags;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02006541
6542 /*
6543 * Instead of creating recursive hierarchies of events,
6544 * we link inherited events back to the original parent,
6545 * which has a filp for sure, which we use as the reference
6546 * count:
6547 */
6548 if (parent_event->parent)
6549 parent_event = parent_event->parent;
6550
6551 child_event = perf_event_alloc(&parent_event->attr,
6552 parent_event->cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +02006553 child,
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02006554 group_leader, parent_event,
6555 NULL);
6556 if (IS_ERR(child_event))
6557 return child_event;
6558 get_ctx(child_ctx);
6559
6560 /*
6561 * Make the child state follow the state of the parent event,
6562 * not its attr.disabled bit. We hold the parent's mutex,
6563 * so we won't race with perf_event_{en, dis}able_family.
6564 */
6565 if (parent_event->state >= PERF_EVENT_STATE_INACTIVE)
6566 child_event->state = PERF_EVENT_STATE_INACTIVE;
6567 else
6568 child_event->state = PERF_EVENT_STATE_OFF;
6569
6570 if (parent_event->attr.freq) {
6571 u64 sample_period = parent_event->hw.sample_period;
6572 struct hw_perf_event *hwc = &child_event->hw;
6573
6574 hwc->sample_period = sample_period;
6575 hwc->last_period = sample_period;
6576
6577 local64_set(&hwc->period_left, sample_period);
6578 }
6579
6580 child_event->ctx = child_ctx;
6581 child_event->overflow_handler = parent_event->overflow_handler;
6582
6583 /*
Thomas Gleixner614b6782010-12-03 16:24:32 -02006584 * Precalculate sample_data sizes
6585 */
6586 perf_event__header_size(child_event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02006587 perf_event__id_header_size(child_event);
Thomas Gleixner614b6782010-12-03 16:24:32 -02006588
6589 /*
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02006590 * Link it up in the child's context:
6591 */
Peter Zijlstracee010e2010-09-10 12:51:54 +02006592 raw_spin_lock_irqsave(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02006593 add_event_to_ctx(child_event, child_ctx);
Peter Zijlstracee010e2010-09-10 12:51:54 +02006594 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02006595
6596 /*
6597 * Get a reference to the parent filp - we will fput it
6598 * when the child event exits. This is safe to do because
6599 * we are in the parent and we know that the filp still
6600 * exists and has a nonzero count:
6601 */
6602 atomic_long_inc(&parent_event->filp->f_count);
6603
6604 /*
6605 * Link this into the parent event's child list
6606 */
6607 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
6608 mutex_lock(&parent_event->child_mutex);
6609 list_add_tail(&child_event->child_list, &parent_event->child_list);
6610 mutex_unlock(&parent_event->child_mutex);
6611
6612 return child_event;
6613}
6614
6615static int inherit_group(struct perf_event *parent_event,
6616 struct task_struct *parent,
6617 struct perf_event_context *parent_ctx,
6618 struct task_struct *child,
6619 struct perf_event_context *child_ctx)
6620{
6621 struct perf_event *leader;
6622 struct perf_event *sub;
6623 struct perf_event *child_ctr;
6624
6625 leader = inherit_event(parent_event, parent, parent_ctx,
6626 child, NULL, child_ctx);
6627 if (IS_ERR(leader))
6628 return PTR_ERR(leader);
6629 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
6630 child_ctr = inherit_event(sub, parent, parent_ctx,
6631 child, leader, child_ctx);
6632 if (IS_ERR(child_ctr))
6633 return PTR_ERR(child_ctr);
6634 }
6635 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006636}
6637
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006638static int
6639inherit_task_group(struct perf_event *event, struct task_struct *parent,
6640 struct perf_event_context *parent_ctx,
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006641 struct task_struct *child, int ctxn,
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006642 int *inherited_all)
6643{
6644 int ret;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006645 struct perf_event_context *child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006646
6647 if (!event->attr.inherit) {
6648 *inherited_all = 0;
6649 return 0;
6650 }
6651
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01006652 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006653 if (!child_ctx) {
6654 /*
6655 * This is executed from the parent task context, so
6656 * inherit events that have been marked for cloning.
6657 * First allocate and initialize a context for the
6658 * child.
6659 */
6660
Peter Zijlstraeb184472010-09-07 15:55:13 +02006661 child_ctx = alloc_perf_context(event->pmu, child);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006662 if (!child_ctx)
6663 return -ENOMEM;
6664
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006665 child->perf_event_ctxp[ctxn] = child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006666 }
6667
6668 ret = inherit_group(event, parent, parent_ctx,
6669 child, child_ctx);
6670
6671 if (ret)
6672 *inherited_all = 0;
6673
6674 return ret;
6675}
6676
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006677/*
6678 * Initialize the perf_event context in task_struct
6679 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006680int perf_event_init_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006681{
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006682 struct perf_event_context *child_ctx, *parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006683 struct perf_event_context *cloned_ctx;
6684 struct perf_event *event;
6685 struct task_struct *parent = current;
6686 int inherited_all = 1;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01006687 unsigned long flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006688 int ret = 0;
6689
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006690 if (likely(!parent->perf_event_ctxp[ctxn]))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006691 return 0;
6692
6693 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006694 * If the parent's context is a clone, pin it so it won't get
6695 * swapped under us.
6696 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006697 parent_ctx = perf_pin_task_context(parent, ctxn);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006698
6699 /*
6700 * No need to check if parent_ctx != NULL here; since we saw
6701 * it non-NULL earlier, the only reason for it to become NULL
6702 * is if we exit, and since we're currently in the middle of
6703 * a fork we can't be exiting at the same time.
6704 */
6705
6706 /*
6707 * Lock the parent list. No need to lock the child - not PID
6708 * hashed yet and not running, so nobody can access it.
6709 */
6710 mutex_lock(&parent_ctx->mutex);
6711
6712 /*
6713 * We dont have to disable NMIs - we are only looking at
6714 * the list, not manipulating it:
6715 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006716 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006717 ret = inherit_task_group(event, parent, parent_ctx,
6718 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006719 if (ret)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006720 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006721 }
6722
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01006723 /*
6724 * We can't hold ctx->lock when iterating the ->flexible_group list due
6725 * to allocations, but we need to prevent rotation because
6726 * rotate_ctx() will change the list from interrupt context.
6727 */
6728 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
6729 parent_ctx->rotate_disable = 1;
6730 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
6731
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006732 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006733 ret = inherit_task_group(event, parent, parent_ctx,
6734 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006735 if (ret)
6736 break;
6737 }
6738
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01006739 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
6740 parent_ctx->rotate_disable = 0;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01006741
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006742 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006743
Peter Zijlstra05cbaa22009-12-30 16:00:35 +01006744 if (child_ctx && inherited_all) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006745 /*
6746 * Mark the child context as a clone of the parent
6747 * context, or of whatever the parent is a clone of.
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01006748 *
6749 * Note that if the parent is a clone, the holding of
6750 * parent_ctx->lock avoids it from being uncloned.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006751 */
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01006752 cloned_ctx = parent_ctx->parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006753 if (cloned_ctx) {
6754 child_ctx->parent_ctx = cloned_ctx;
6755 child_ctx->parent_gen = parent_ctx->parent_gen;
6756 } else {
6757 child_ctx->parent_ctx = parent_ctx;
6758 child_ctx->parent_gen = parent_ctx->generation;
6759 }
6760 get_ctx(child_ctx->parent_ctx);
6761 }
6762
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01006763 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006764 mutex_unlock(&parent_ctx->mutex);
6765
6766 perf_unpin_context(parent_ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01006767 put_ctx(parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006768
6769 return ret;
6770}
6771
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006772/*
6773 * Initialize the perf_event context in task_struct
6774 */
6775int perf_event_init_task(struct task_struct *child)
6776{
6777 int ctxn, ret;
6778
Oleg Nesterov8550d7c2011-01-19 19:22:28 +01006779 memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp));
6780 mutex_init(&child->perf_event_mutex);
6781 INIT_LIST_HEAD(&child->perf_event_list);
6782
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006783 for_each_task_context_nr(ctxn) {
6784 ret = perf_event_init_context(child, ctxn);
6785 if (ret)
6786 return ret;
6787 }
6788
6789 return 0;
6790}
6791
Paul Mackerras220b1402010-03-10 20:45:52 +11006792static void __init perf_event_init_all_cpus(void)
6793{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006794 struct swevent_htable *swhash;
Paul Mackerras220b1402010-03-10 20:45:52 +11006795 int cpu;
Paul Mackerras220b1402010-03-10 20:45:52 +11006796
6797 for_each_possible_cpu(cpu) {
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006798 swhash = &per_cpu(swevent_htable, cpu);
6799 mutex_init(&swhash->hlist_mutex);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02006800 INIT_LIST_HEAD(&per_cpu(rotation_list, cpu));
Paul Mackerras220b1402010-03-10 20:45:52 +11006801 }
6802}
6803
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006804static void __cpuinit perf_event_init_cpu(int cpu)
6805{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006806 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006807
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006808 mutex_lock(&swhash->hlist_mutex);
6809 if (swhash->hlist_refcount > 0) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006810 struct swevent_hlist *hlist;
6811
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006812 hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
6813 WARN_ON(!hlist);
6814 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006815 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006816 mutex_unlock(&swhash->hlist_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006817}
6818
Peter Zijlstrac2774432010-12-08 15:29:02 +01006819#if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02006820static void perf_pmu_rotate_stop(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006821{
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02006822 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
6823
6824 WARN_ON(!irqs_disabled());
6825
6826 list_del_init(&cpuctx->rotation_list);
6827}
6828
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006829static void __perf_event_exit_context(void *__info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006830{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006831 struct perf_event_context *ctx = __info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006832 struct perf_event *event, *tmp;
6833
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006834 perf_pmu_rotate_stop(ctx->pmu);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02006835
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006836 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01006837 __perf_remove_from_context(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006838 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, group_entry)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01006839 __perf_remove_from_context(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006840}
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006841
6842static void perf_event_exit_cpu_context(int cpu)
6843{
6844 struct perf_event_context *ctx;
6845 struct pmu *pmu;
6846 int idx;
6847
6848 idx = srcu_read_lock(&pmus_srcu);
6849 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra917bdd12010-09-17 11:28:49 +02006850 ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006851
6852 mutex_lock(&ctx->mutex);
6853 smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
6854 mutex_unlock(&ctx->mutex);
6855 }
6856 srcu_read_unlock(&pmus_srcu, idx);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006857}
6858
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006859static void perf_event_exit_cpu(int cpu)
6860{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006861 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006862
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006863 mutex_lock(&swhash->hlist_mutex);
6864 swevent_hlist_release(swhash);
6865 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006866
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006867 perf_event_exit_cpu_context(cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006868}
6869#else
6870static inline void perf_event_exit_cpu(int cpu) { }
6871#endif
6872
Peter Zijlstrac2774432010-12-08 15:29:02 +01006873static int
6874perf_reboot(struct notifier_block *notifier, unsigned long val, void *v)
6875{
6876 int cpu;
6877
6878 for_each_online_cpu(cpu)
6879 perf_event_exit_cpu(cpu);
6880
6881 return NOTIFY_OK;
6882}
6883
6884/*
6885 * Run the perf reboot notifier at the very last possible moment so that
6886 * the generic watchdog code runs as long as possible.
6887 */
6888static struct notifier_block perf_reboot_notifier = {
6889 .notifier_call = perf_reboot,
6890 .priority = INT_MIN,
6891};
6892
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006893static int __cpuinit
6894perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
6895{
6896 unsigned int cpu = (long)hcpu;
6897
Peter Zijlstra5e116372010-06-11 13:35:08 +02006898 switch (action & ~CPU_TASKS_FROZEN) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006899
6900 case CPU_UP_PREPARE:
Peter Zijlstra5e116372010-06-11 13:35:08 +02006901 case CPU_DOWN_FAILED:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006902 perf_event_init_cpu(cpu);
6903 break;
6904
Peter Zijlstra5e116372010-06-11 13:35:08 +02006905 case CPU_UP_CANCELED:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006906 case CPU_DOWN_PREPARE:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006907 perf_event_exit_cpu(cpu);
6908 break;
6909
6910 default:
6911 break;
6912 }
6913
6914 return NOTIFY_OK;
6915}
6916
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006917void __init perf_event_init(void)
6918{
Jason Wessel3c502e72010-11-04 17:33:01 -05006919 int ret;
6920
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006921 idr_init(&pmu_idr);
6922
Paul Mackerras220b1402010-03-10 20:45:52 +11006923 perf_event_init_all_cpus();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006924 init_srcu_struct(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006925 perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE);
6926 perf_pmu_register(&perf_cpu_clock, NULL, -1);
6927 perf_pmu_register(&perf_task_clock, NULL, -1);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006928 perf_tp_register();
6929 perf_cpu_notifier(perf_cpu_notify);
Peter Zijlstrac2774432010-12-08 15:29:02 +01006930 register_reboot_notifier(&perf_reboot_notifier);
Jason Wessel3c502e72010-11-04 17:33:01 -05006931
6932 ret = init_hw_breakpoint();
6933 WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006934}
Peter Zijlstraabe43402010-11-17 23:17:37 +01006935
6936static int __init perf_event_sysfs_init(void)
6937{
6938 struct pmu *pmu;
6939 int ret;
6940
6941 mutex_lock(&pmus_lock);
6942
6943 ret = bus_register(&pmu_bus);
6944 if (ret)
6945 goto unlock;
6946
6947 list_for_each_entry(pmu, &pmus, entry) {
6948 if (!pmu->name || pmu->type < 0)
6949 continue;
6950
6951 ret = pmu_dev_alloc(pmu);
6952 WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret);
6953 }
6954 pmu_bus_running = 1;
6955 ret = 0;
6956
6957unlock:
6958 mutex_unlock(&pmus_lock);
6959
6960 return ret;
6961}
6962device_initcall(perf_event_sysfs_init);
Stephane Eraniane5d13672011-02-14 11:20:01 +02006963
6964#ifdef CONFIG_CGROUP_PERF
6965static struct cgroup_subsys_state *perf_cgroup_create(
6966 struct cgroup_subsys *ss, struct cgroup *cont)
6967{
6968 struct perf_cgroup *jc;
Stephane Eraniane5d13672011-02-14 11:20:01 +02006969
Li Zefan1b15d052011-03-03 14:26:06 +08006970 jc = kzalloc(sizeof(*jc), GFP_KERNEL);
Stephane Eraniane5d13672011-02-14 11:20:01 +02006971 if (!jc)
6972 return ERR_PTR(-ENOMEM);
6973
Stephane Eraniane5d13672011-02-14 11:20:01 +02006974 jc->info = alloc_percpu(struct perf_cgroup_info);
6975 if (!jc->info) {
6976 kfree(jc);
6977 return ERR_PTR(-ENOMEM);
6978 }
6979
Stephane Eraniane5d13672011-02-14 11:20:01 +02006980 return &jc->css;
6981}
6982
6983static void perf_cgroup_destroy(struct cgroup_subsys *ss,
6984 struct cgroup *cont)
6985{
6986 struct perf_cgroup *jc;
6987 jc = container_of(cgroup_subsys_state(cont, perf_subsys_id),
6988 struct perf_cgroup, css);
6989 free_percpu(jc->info);
6990 kfree(jc);
6991}
6992
6993static int __perf_cgroup_move(void *info)
6994{
6995 struct task_struct *task = info;
6996 perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN);
6997 return 0;
6998}
6999
Peter Zijlstra74c355f2011-05-30 16:48:06 +02007000static void
7001perf_cgroup_attach_task(struct cgroup *cgrp, struct task_struct *task)
Stephane Eraniane5d13672011-02-14 11:20:01 +02007002{
7003 task_function_call(task, __perf_cgroup_move, task);
7004}
7005
Stephane Eraniane5d13672011-02-14 11:20:01 +02007006static void perf_cgroup_exit(struct cgroup_subsys *ss, struct cgroup *cgrp,
7007 struct cgroup *old_cgrp, struct task_struct *task)
7008{
7009 /*
7010 * cgroup_exit() is called in the copy_process() failure path.
7011 * Ignore this case since the task hasn't ran yet, this avoids
7012 * trying to poke a half freed task state from generic code.
7013 */
7014 if (!(task->flags & PF_EXITING))
7015 return;
7016
Peter Zijlstra74c355f2011-05-30 16:48:06 +02007017 perf_cgroup_attach_task(cgrp, task);
Stephane Eraniane5d13672011-02-14 11:20:01 +02007018}
7019
7020struct cgroup_subsys perf_subsys = {
Ingo Molnare7e7ee22011-05-04 08:42:29 +02007021 .name = "perf_event",
7022 .subsys_id = perf_subsys_id,
7023 .create = perf_cgroup_create,
7024 .destroy = perf_cgroup_destroy,
7025 .exit = perf_cgroup_exit,
Peter Zijlstra74c355f2011-05-30 16:48:06 +02007026 .attach_task = perf_cgroup_attach_task,
Stephane Eraniane5d13672011-02-14 11:20:01 +02007027};
7028#endif /* CONFIG_CGROUP_PERF */