blob: 63f1987c1c1cc431efd96898980e285967a8c0ea [file] [log] [blame]
Thomas Gleixner0793a612008-12-04 20:12:29 +01001/*
2 * Performance counter core code
3 *
Ingo Molnar98144512009-04-29 14:52:50 +02004 * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
5 * Copyright (C) 2008-2009 Red Hat, Inc., Ingo Molnar
6 * Copyright (C) 2008-2009 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
Paul Mackerrasc5dd0162009-04-30 09:48:16 +10007 * Copyright © 2009 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
Peter Zijlstra7b732a72009-03-23 18:22:10 +01008 *
9 * For licensing details see kernel-base/COPYING
Thomas Gleixner0793a612008-12-04 20:12:29 +010010 */
11
12#include <linux/fs.h>
Peter Zijlstrab9cacc72009-03-25 12:30:22 +010013#include <linux/mm.h>
Thomas Gleixner0793a612008-12-04 20:12:29 +010014#include <linux/cpu.h>
15#include <linux/smp.h>
Ingo Molnar04289bb2008-12-11 08:38:42 +010016#include <linux/file.h>
Thomas Gleixner0793a612008-12-04 20:12:29 +010017#include <linux/poll.h>
18#include <linux/sysfs.h>
Ingo Molnar22a4f652009-06-01 10:13:37 +020019#include <linux/dcache.h>
Thomas Gleixner0793a612008-12-04 20:12:29 +010020#include <linux/percpu.h>
Ingo Molnar22a4f652009-06-01 10:13:37 +020021#include <linux/ptrace.h>
Peter Zijlstrab9cacc72009-03-25 12:30:22 +010022#include <linux/vmstat.h>
23#include <linux/hardirq.h>
24#include <linux/rculist.h>
Thomas Gleixner0793a612008-12-04 20:12:29 +010025#include <linux/uaccess.h>
26#include <linux/syscalls.h>
27#include <linux/anon_inodes.h>
Ingo Molnaraa9c4c02008-12-17 14:10:57 +010028#include <linux/kernel_stat.h>
Thomas Gleixner0793a612008-12-04 20:12:29 +010029#include <linux/perf_counter.h>
30
Tim Blechmann4e193bd2009-03-14 14:29:25 +010031#include <asm/irq_regs.h>
32
Thomas Gleixner0793a612008-12-04 20:12:29 +010033/*
34 * Each CPU has a list of per CPU counters:
35 */
36DEFINE_PER_CPU(struct perf_cpu_context, perf_cpu_context);
37
Ingo Molnar088e2852008-12-14 20:21:00 +010038int perf_max_counters __read_mostly = 1;
Thomas Gleixner0793a612008-12-04 20:12:29 +010039static int perf_reserved_percpu __read_mostly;
40static int perf_overcommit __read_mostly = 1;
41
Peter Zijlstra7fc23a52009-05-08 18:52:21 +020042static atomic_t nr_counters __read_mostly;
Peter Zijlstra60313eb2009-06-04 16:53:44 +020043static atomic_t nr_mmap_counters __read_mostly;
Peter Zijlstra60313eb2009-06-04 16:53:44 +020044static atomic_t nr_comm_counters __read_mostly;
Peter Zijlstra9ee318a2009-04-09 10:53:44 +020045
Peter Zijlstra07647712009-06-11 11:18:36 +020046/*
47 * 0 - not paranoid
48 * 1 - disallow cpu counters to unpriv
49 * 2 - disallow kernel profiling to unpriv
50 */
51int sysctl_perf_counter_paranoid __read_mostly; /* do we need to be privileged */
52
53static inline bool perf_paranoid_cpu(void)
54{
55 return sysctl_perf_counter_paranoid > 0;
56}
57
58static inline bool perf_paranoid_kernel(void)
59{
60 return sysctl_perf_counter_paranoid > 1;
61}
62
Peter Zijlstra789f90f2009-05-15 15:19:27 +020063int sysctl_perf_counter_mlock __read_mostly = 512; /* 'free' kb per user */
Peter Zijlstraa78ac322009-05-25 17:39:05 +020064int sysctl_perf_counter_limit __read_mostly = 100000; /* max NMIs per second */
Peter Zijlstra1ccd1542009-04-09 10:53:45 +020065
Peter Zijlstraa96bbc12009-06-03 14:01:36 +020066static atomic64_t perf_counter_id;
67
Thomas Gleixner0793a612008-12-04 20:12:29 +010068/*
Ingo Molnar1dce8d92009-05-04 19:23:18 +020069 * Lock for (sysadmin-configurable) counter reservations:
Thomas Gleixner0793a612008-12-04 20:12:29 +010070 */
Ingo Molnar1dce8d92009-05-04 19:23:18 +020071static DEFINE_SPINLOCK(perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +010072
73/*
74 * Architecture provided APIs - weak aliases:
75 */
Robert Richter4aeb0b42009-04-29 12:47:03 +020076extern __weak const struct pmu *hw_perf_counter_init(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +010077{
Paul Mackerrasff6f0542009-01-09 16:19:25 +110078 return NULL;
Thomas Gleixner0793a612008-12-04 20:12:29 +010079}
80
Peter Zijlstra9e35ad32009-05-13 16:21:38 +020081void __weak hw_perf_disable(void) { barrier(); }
82void __weak hw_perf_enable(void) { barrier(); }
83
Paul Mackerras01d02872009-01-14 13:44:19 +110084void __weak hw_perf_counter_setup(int cpu) { barrier(); }
Ingo Molnar22a4f652009-06-01 10:13:37 +020085
86int __weak
87hw_perf_group_sched_in(struct perf_counter *group_leader,
Paul Mackerras3cbed422009-01-09 16:43:42 +110088 struct perf_cpu_context *cpuctx,
89 struct perf_counter_context *ctx, int cpu)
90{
91 return 0;
92}
Thomas Gleixner0793a612008-12-04 20:12:29 +010093
Paul Mackerras4eb96fc2009-01-09 17:24:34 +110094void __weak perf_counter_print_debug(void) { }
95
Peter Zijlstra9e35ad32009-05-13 16:21:38 +020096static DEFINE_PER_CPU(int, disable_count);
97
98void __perf_disable(void)
99{
100 __get_cpu_var(disable_count)++;
101}
102
103bool __perf_enable(void)
104{
105 return !--__get_cpu_var(disable_count);
106}
107
108void perf_disable(void)
109{
110 __perf_disable();
111 hw_perf_disable();
112}
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200113
114void perf_enable(void)
115{
116 if (__perf_enable())
117 hw_perf_enable();
118}
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200119
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000120static void get_ctx(struct perf_counter_context *ctx)
121{
122 atomic_inc(&ctx->refcount);
123}
124
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000125static void free_ctx(struct rcu_head *head)
126{
127 struct perf_counter_context *ctx;
128
129 ctx = container_of(head, struct perf_counter_context, rcu_head);
130 kfree(ctx);
131}
132
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000133static void put_ctx(struct perf_counter_context *ctx)
134{
Paul Mackerras564c2b22009-05-22 14:27:22 +1000135 if (atomic_dec_and_test(&ctx->refcount)) {
136 if (ctx->parent_ctx)
137 put_ctx(ctx->parent_ctx);
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000138 if (ctx->task)
139 put_task_struct(ctx->task);
140 call_rcu(&ctx->rcu_head, free_ctx);
Paul Mackerras564c2b22009-05-22 14:27:22 +1000141 }
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000142}
143
Peter Zijlstrafccc7142009-05-23 18:28:56 +0200144/*
Paul Mackerras25346b932009-06-01 17:48:12 +1000145 * Get the perf_counter_context for a task and lock it.
146 * This has to cope with with the fact that until it is locked,
147 * the context could get moved to another task.
148 */
Ingo Molnar22a4f652009-06-01 10:13:37 +0200149static struct perf_counter_context *
150perf_lock_task_context(struct task_struct *task, unsigned long *flags)
Paul Mackerras25346b932009-06-01 17:48:12 +1000151{
152 struct perf_counter_context *ctx;
153
154 rcu_read_lock();
155 retry:
156 ctx = rcu_dereference(task->perf_counter_ctxp);
157 if (ctx) {
158 /*
159 * If this context is a clone of another, it might
160 * get swapped for another underneath us by
161 * perf_counter_task_sched_out, though the
162 * rcu_read_lock() protects us from any context
163 * getting freed. Lock the context and check if it
164 * got swapped before we could get the lock, and retry
165 * if so. If we locked the right context, then it
166 * can't get swapped on us any more.
167 */
168 spin_lock_irqsave(&ctx->lock, *flags);
169 if (ctx != rcu_dereference(task->perf_counter_ctxp)) {
170 spin_unlock_irqrestore(&ctx->lock, *flags);
171 goto retry;
172 }
173 }
174 rcu_read_unlock();
175 return ctx;
176}
177
178/*
179 * Get the context for a task and increment its pin_count so it
180 * can't get swapped to another task. This also increments its
181 * reference count so that the context can't get freed.
182 */
183static struct perf_counter_context *perf_pin_task_context(struct task_struct *task)
184{
185 struct perf_counter_context *ctx;
186 unsigned long flags;
187
188 ctx = perf_lock_task_context(task, &flags);
189 if (ctx) {
190 ++ctx->pin_count;
191 get_ctx(ctx);
192 spin_unlock_irqrestore(&ctx->lock, flags);
193 }
194 return ctx;
195}
196
197static void perf_unpin_context(struct perf_counter_context *ctx)
198{
199 unsigned long flags;
200
201 spin_lock_irqsave(&ctx->lock, flags);
202 --ctx->pin_count;
203 spin_unlock_irqrestore(&ctx->lock, flags);
204 put_ctx(ctx);
205}
206
207/*
Peter Zijlstrafccc7142009-05-23 18:28:56 +0200208 * Add a counter from the lists for its context.
209 * Must be called with ctx->mutex and ctx->lock held.
210 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100211static void
212list_add_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
213{
214 struct perf_counter *group_leader = counter->group_leader;
215
216 /*
217 * Depending on whether it is a standalone or sibling counter,
218 * add it straight to the context's counter list, or to the group
219 * leader's sibling list:
220 */
Peter Zijlstra3df5eda2009-05-08 18:52:22 +0200221 if (group_leader == counter)
Ingo Molnar04289bb2008-12-11 08:38:42 +0100222 list_add_tail(&counter->list_entry, &ctx->counter_list);
Peter Zijlstra5c148192009-03-25 12:30:23 +0100223 else {
Ingo Molnar04289bb2008-12-11 08:38:42 +0100224 list_add_tail(&counter->list_entry, &group_leader->sibling_list);
Peter Zijlstra5c148192009-03-25 12:30:23 +0100225 group_leader->nr_siblings++;
226 }
Peter Zijlstra592903c2009-03-13 12:21:36 +0100227
228 list_add_rcu(&counter->event_entry, &ctx->event_list);
Peter Zijlstra8bc20952009-05-15 20:45:59 +0200229 ctx->nr_counters++;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100230}
231
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000232/*
233 * Remove a counter from the lists for its context.
Peter Zijlstrafccc7142009-05-23 18:28:56 +0200234 * Must be called with ctx->mutex and ctx->lock held.
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000235 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100236static void
237list_del_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
238{
239 struct perf_counter *sibling, *tmp;
240
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000241 if (list_empty(&counter->list_entry))
242 return;
Peter Zijlstra8bc20952009-05-15 20:45:59 +0200243 ctx->nr_counters--;
244
Ingo Molnar04289bb2008-12-11 08:38:42 +0100245 list_del_init(&counter->list_entry);
Peter Zijlstra592903c2009-03-13 12:21:36 +0100246 list_del_rcu(&counter->event_entry);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100247
Peter Zijlstra5c148192009-03-25 12:30:23 +0100248 if (counter->group_leader != counter)
249 counter->group_leader->nr_siblings--;
250
Ingo Molnar04289bb2008-12-11 08:38:42 +0100251 /*
252 * If this was a group counter with sibling counters then
253 * upgrade the siblings to singleton counters by adding them
254 * to the context list directly:
255 */
256 list_for_each_entry_safe(sibling, tmp,
257 &counter->sibling_list, list_entry) {
258
Peter Zijlstra75564232009-03-13 12:21:29 +0100259 list_move_tail(&sibling->list_entry, &ctx->counter_list);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100260 sibling->group_leader = sibling;
261 }
262}
263
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100264static void
265counter_sched_out(struct perf_counter *counter,
266 struct perf_cpu_context *cpuctx,
267 struct perf_counter_context *ctx)
268{
269 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
270 return;
271
272 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200273 counter->tstamp_stopped = ctx->time;
Robert Richter4aeb0b42009-04-29 12:47:03 +0200274 counter->pmu->disable(counter);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100275 counter->oncpu = -1;
276
277 if (!is_software_counter(counter))
278 cpuctx->active_oncpu--;
279 ctx->nr_active--;
Peter Zijlstra0d486962009-06-02 19:22:16 +0200280 if (counter->attr.exclusive || !cpuctx->active_oncpu)
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100281 cpuctx->exclusive = 0;
282}
283
Paul Mackerrasd859e292009-01-17 18:10:22 +1100284static void
285group_sched_out(struct perf_counter *group_counter,
286 struct perf_cpu_context *cpuctx,
287 struct perf_counter_context *ctx)
288{
289 struct perf_counter *counter;
290
291 if (group_counter->state != PERF_COUNTER_STATE_ACTIVE)
292 return;
293
294 counter_sched_out(group_counter, cpuctx, ctx);
295
296 /*
297 * Schedule out siblings (if any):
298 */
299 list_for_each_entry(counter, &group_counter->sibling_list, list_entry)
300 counter_sched_out(counter, cpuctx, ctx);
301
Peter Zijlstra0d486962009-06-02 19:22:16 +0200302 if (group_counter->attr.exclusive)
Paul Mackerrasd859e292009-01-17 18:10:22 +1100303 cpuctx->exclusive = 0;
304}
305
Thomas Gleixner0793a612008-12-04 20:12:29 +0100306/*
307 * Cross CPU call to remove a performance counter
308 *
309 * We disable the counter on the hardware level first. After that we
310 * remove it from the context list.
311 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100312static void __perf_counter_remove_from_context(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100313{
314 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
315 struct perf_counter *counter = info;
316 struct perf_counter_context *ctx = counter->ctx;
317
318 /*
319 * If this is a task context, we need to check whether it is
320 * the current task context of this cpu. If not it has been
321 * scheduled out before the smp call arrived.
322 */
Peter Zijlstra665c2142009-05-29 14:51:57 +0200323 if (ctx->task && cpuctx->task_ctx != ctx)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100324 return;
325
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200326 spin_lock(&ctx->lock);
Ingo Molnar34adc802009-05-20 20:13:28 +0200327 /*
328 * Protect the list operation against NMI by disabling the
329 * counters on a global level.
330 */
331 perf_disable();
Thomas Gleixner0793a612008-12-04 20:12:29 +0100332
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100333 counter_sched_out(counter, cpuctx, ctx);
334
Ingo Molnar04289bb2008-12-11 08:38:42 +0100335 list_del_counter(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100336
337 if (!ctx->task) {
338 /*
339 * Allow more per task counters with respect to the
340 * reservation:
341 */
342 cpuctx->max_pertask =
343 min(perf_max_counters - ctx->nr_counters,
344 perf_max_counters - perf_reserved_percpu);
345 }
346
Ingo Molnar34adc802009-05-20 20:13:28 +0200347 perf_enable();
Peter Zijlstra665c2142009-05-29 14:51:57 +0200348 spin_unlock(&ctx->lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100349}
350
351
352/*
353 * Remove the counter from a task's (or a CPU's) list of counters.
354 *
Peter Zijlstrafccc7142009-05-23 18:28:56 +0200355 * Must be called with ctx->mutex held.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100356 *
357 * CPU counters are removed with a smp call. For task counters we only
358 * call when the task is on a CPU.
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000359 *
360 * If counter->ctx is a cloned context, callers must make sure that
361 * every task struct that counter->ctx->task could possibly point to
362 * remains valid. This is OK when called from perf_release since
363 * that only calls us on the top-level context, which can't be a clone.
364 * When called from perf_counter_exit_task, it's OK because the
365 * context has been detached from its task.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100366 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100367static void perf_counter_remove_from_context(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100368{
369 struct perf_counter_context *ctx = counter->ctx;
370 struct task_struct *task = ctx->task;
371
372 if (!task) {
373 /*
374 * Per cpu counters are removed via an smp call and
375 * the removal is always sucessful.
376 */
377 smp_call_function_single(counter->cpu,
Ingo Molnar04289bb2008-12-11 08:38:42 +0100378 __perf_counter_remove_from_context,
Thomas Gleixner0793a612008-12-04 20:12:29 +0100379 counter, 1);
380 return;
381 }
382
383retry:
Ingo Molnar04289bb2008-12-11 08:38:42 +0100384 task_oncpu_function_call(task, __perf_counter_remove_from_context,
Thomas Gleixner0793a612008-12-04 20:12:29 +0100385 counter);
386
387 spin_lock_irq(&ctx->lock);
388 /*
389 * If the context is active we need to retry the smp call.
390 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100391 if (ctx->nr_active && !list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100392 spin_unlock_irq(&ctx->lock);
393 goto retry;
394 }
395
396 /*
397 * The lock prevents that this context is scheduled in so we
Ingo Molnar04289bb2008-12-11 08:38:42 +0100398 * can remove the counter safely, if the call above did not
Thomas Gleixner0793a612008-12-04 20:12:29 +0100399 * succeed.
400 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100401 if (!list_empty(&counter->list_entry)) {
Ingo Molnar04289bb2008-12-11 08:38:42 +0100402 list_del_counter(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100403 }
404 spin_unlock_irq(&ctx->lock);
405}
406
Peter Zijlstra4af49982009-04-06 11:45:10 +0200407static inline u64 perf_clock(void)
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100408{
Peter Zijlstra4af49982009-04-06 11:45:10 +0200409 return cpu_clock(smp_processor_id());
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100410}
411
412/*
413 * Update the record of the current time in a context.
414 */
Peter Zijlstra4af49982009-04-06 11:45:10 +0200415static void update_context_time(struct perf_counter_context *ctx)
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100416{
Peter Zijlstra4af49982009-04-06 11:45:10 +0200417 u64 now = perf_clock();
418
419 ctx->time += now - ctx->timestamp;
420 ctx->timestamp = now;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100421}
422
423/*
424 * Update the total_time_enabled and total_time_running fields for a counter.
425 */
426static void update_counter_times(struct perf_counter *counter)
427{
428 struct perf_counter_context *ctx = counter->ctx;
429 u64 run_end;
430
Peter Zijlstra4af49982009-04-06 11:45:10 +0200431 if (counter->state < PERF_COUNTER_STATE_INACTIVE)
432 return;
433
434 counter->total_time_enabled = ctx->time - counter->tstamp_enabled;
435
436 if (counter->state == PERF_COUNTER_STATE_INACTIVE)
437 run_end = counter->tstamp_stopped;
438 else
439 run_end = ctx->time;
440
441 counter->total_time_running = run_end - counter->tstamp_running;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100442}
443
444/*
445 * Update total_time_enabled and total_time_running for all counters in a group.
446 */
447static void update_group_times(struct perf_counter *leader)
448{
449 struct perf_counter *counter;
450
451 update_counter_times(leader);
452 list_for_each_entry(counter, &leader->sibling_list, list_entry)
453 update_counter_times(counter);
454}
455
456/*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100457 * Cross CPU call to disable a performance counter
458 */
459static void __perf_counter_disable(void *info)
460{
461 struct perf_counter *counter = info;
462 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
463 struct perf_counter_context *ctx = counter->ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100464
465 /*
466 * If this is a per-task counter, need to check whether this
467 * counter's task is the current task on this cpu.
468 */
Peter Zijlstra665c2142009-05-29 14:51:57 +0200469 if (ctx->task && cpuctx->task_ctx != ctx)
Paul Mackerrasd859e292009-01-17 18:10:22 +1100470 return;
471
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200472 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100473
474 /*
475 * If the counter is on, turn it off.
476 * If it is in error state, leave it in error state.
477 */
478 if (counter->state >= PERF_COUNTER_STATE_INACTIVE) {
Peter Zijlstra4af49982009-04-06 11:45:10 +0200479 update_context_time(ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100480 update_counter_times(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100481 if (counter == counter->group_leader)
482 group_sched_out(counter, cpuctx, ctx);
483 else
484 counter_sched_out(counter, cpuctx, ctx);
485 counter->state = PERF_COUNTER_STATE_OFF;
486 }
487
Peter Zijlstra665c2142009-05-29 14:51:57 +0200488 spin_unlock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100489}
490
491/*
492 * Disable a counter.
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000493 *
494 * If counter->ctx is a cloned context, callers must make sure that
495 * every task struct that counter->ctx->task could possibly point to
496 * remains valid. This condition is satisifed when called through
497 * perf_counter_for_each_child or perf_counter_for_each because they
498 * hold the top-level counter's child_mutex, so any descendant that
499 * goes to exit will block in sync_child_counter.
500 * When called from perf_pending_counter it's OK because counter->ctx
501 * is the current context on this CPU and preemption is disabled,
502 * hence we can't get into perf_counter_task_sched_out for this context.
Paul Mackerrasd859e292009-01-17 18:10:22 +1100503 */
504static void perf_counter_disable(struct perf_counter *counter)
505{
506 struct perf_counter_context *ctx = counter->ctx;
507 struct task_struct *task = ctx->task;
508
509 if (!task) {
510 /*
511 * Disable the counter on the cpu that it's on
512 */
513 smp_call_function_single(counter->cpu, __perf_counter_disable,
514 counter, 1);
515 return;
516 }
517
518 retry:
519 task_oncpu_function_call(task, __perf_counter_disable, counter);
520
521 spin_lock_irq(&ctx->lock);
522 /*
523 * If the counter is still active, we need to retry the cross-call.
524 */
525 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
526 spin_unlock_irq(&ctx->lock);
527 goto retry;
528 }
529
530 /*
531 * Since we have the lock this context can't be scheduled
532 * in, so we can change the state safely.
533 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100534 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
535 update_counter_times(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100536 counter->state = PERF_COUNTER_STATE_OFF;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100537 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100538
539 spin_unlock_irq(&ctx->lock);
540}
541
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100542static int
543counter_sched_in(struct perf_counter *counter,
544 struct perf_cpu_context *cpuctx,
545 struct perf_counter_context *ctx,
546 int cpu)
547{
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100548 if (counter->state <= PERF_COUNTER_STATE_OFF)
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100549 return 0;
550
551 counter->state = PERF_COUNTER_STATE_ACTIVE;
552 counter->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */
553 /*
554 * The new state must be visible before we turn it on in the hardware:
555 */
556 smp_wmb();
557
Robert Richter4aeb0b42009-04-29 12:47:03 +0200558 if (counter->pmu->enable(counter)) {
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100559 counter->state = PERF_COUNTER_STATE_INACTIVE;
560 counter->oncpu = -1;
561 return -EAGAIN;
562 }
563
Peter Zijlstra4af49982009-04-06 11:45:10 +0200564 counter->tstamp_running += ctx->time - counter->tstamp_stopped;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100565
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100566 if (!is_software_counter(counter))
567 cpuctx->active_oncpu++;
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100568 ctx->nr_active++;
569
Peter Zijlstra0d486962009-06-02 19:22:16 +0200570 if (counter->attr.exclusive)
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100571 cpuctx->exclusive = 1;
572
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100573 return 0;
574}
575
Paul Mackerras6751b712009-05-11 12:08:02 +1000576static int
577group_sched_in(struct perf_counter *group_counter,
578 struct perf_cpu_context *cpuctx,
579 struct perf_counter_context *ctx,
580 int cpu)
581{
582 struct perf_counter *counter, *partial_group;
583 int ret;
584
585 if (group_counter->state == PERF_COUNTER_STATE_OFF)
586 return 0;
587
588 ret = hw_perf_group_sched_in(group_counter, cpuctx, ctx, cpu);
589 if (ret)
590 return ret < 0 ? ret : 0;
591
Paul Mackerras6751b712009-05-11 12:08:02 +1000592 if (counter_sched_in(group_counter, cpuctx, ctx, cpu))
593 return -EAGAIN;
594
595 /*
596 * Schedule in siblings as one group (if any):
597 */
598 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
Paul Mackerras6751b712009-05-11 12:08:02 +1000599 if (counter_sched_in(counter, cpuctx, ctx, cpu)) {
600 partial_group = counter;
601 goto group_error;
602 }
603 }
604
605 return 0;
606
607group_error:
608 /*
609 * Groups can be scheduled in as one unit only, so undo any
610 * partial group before returning:
611 */
612 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
613 if (counter == partial_group)
614 break;
615 counter_sched_out(counter, cpuctx, ctx);
616 }
617 counter_sched_out(group_counter, cpuctx, ctx);
618
619 return -EAGAIN;
620}
621
Thomas Gleixner0793a612008-12-04 20:12:29 +0100622/*
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100623 * Return 1 for a group consisting entirely of software counters,
624 * 0 if the group contains any hardware counters.
625 */
626static int is_software_only_group(struct perf_counter *leader)
627{
628 struct perf_counter *counter;
629
630 if (!is_software_counter(leader))
631 return 0;
Peter Zijlstra5c148192009-03-25 12:30:23 +0100632
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100633 list_for_each_entry(counter, &leader->sibling_list, list_entry)
634 if (!is_software_counter(counter))
635 return 0;
Peter Zijlstra5c148192009-03-25 12:30:23 +0100636
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100637 return 1;
638}
639
640/*
641 * Work out whether we can put this counter group on the CPU now.
642 */
643static int group_can_go_on(struct perf_counter *counter,
644 struct perf_cpu_context *cpuctx,
645 int can_add_hw)
646{
647 /*
648 * Groups consisting entirely of software counters can always go on.
649 */
650 if (is_software_only_group(counter))
651 return 1;
652 /*
653 * If an exclusive group is already on, no other hardware
654 * counters can go on.
655 */
656 if (cpuctx->exclusive)
657 return 0;
658 /*
659 * If this group is exclusive and there are already
660 * counters on the CPU, it can't go on.
661 */
Peter Zijlstra0d486962009-06-02 19:22:16 +0200662 if (counter->attr.exclusive && cpuctx->active_oncpu)
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100663 return 0;
664 /*
665 * Otherwise, try to add it if all previous groups were able
666 * to go on.
667 */
668 return can_add_hw;
669}
670
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100671static void add_counter_to_ctx(struct perf_counter *counter,
672 struct perf_counter_context *ctx)
673{
674 list_add_counter(counter, ctx);
Peter Zijlstra4af49982009-04-06 11:45:10 +0200675 counter->tstamp_enabled = ctx->time;
676 counter->tstamp_running = ctx->time;
677 counter->tstamp_stopped = ctx->time;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100678}
679
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100680/*
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100681 * Cross CPU call to install and enable a performance counter
Peter Zijlstra682076a2009-05-23 18:28:57 +0200682 *
683 * Must be called with ctx->mutex held
Thomas Gleixner0793a612008-12-04 20:12:29 +0100684 */
685static void __perf_install_in_context(void *info)
686{
687 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
688 struct perf_counter *counter = info;
689 struct perf_counter_context *ctx = counter->ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100690 struct perf_counter *leader = counter->group_leader;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100691 int cpu = smp_processor_id();
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100692 int err;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100693
694 /*
695 * If this is a task context, we need to check whether it is
696 * the current task context of this cpu. If not it has been
697 * scheduled out before the smp call arrived.
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000698 * Or possibly this is the right context but it isn't
699 * on this cpu because it had no counters.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100700 */
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000701 if (ctx->task && cpuctx->task_ctx != ctx) {
Peter Zijlstra665c2142009-05-29 14:51:57 +0200702 if (cpuctx->task_ctx || ctx->task != current)
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000703 return;
704 cpuctx->task_ctx = ctx;
705 }
Thomas Gleixner0793a612008-12-04 20:12:29 +0100706
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200707 spin_lock(&ctx->lock);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000708 ctx->is_active = 1;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200709 update_context_time(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100710
711 /*
712 * Protect the list operation against NMI by disabling the
713 * counters on a global level. NOP for non NMI based counters.
714 */
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200715 perf_disable();
Thomas Gleixner0793a612008-12-04 20:12:29 +0100716
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100717 add_counter_to_ctx(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100718
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100719 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100720 * Don't put the counter on if it is disabled or if
721 * it is in a group and the group isn't on.
722 */
723 if (counter->state != PERF_COUNTER_STATE_INACTIVE ||
724 (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE))
725 goto unlock;
726
727 /*
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100728 * An exclusive counter can't go on if there are already active
729 * hardware counters, and no hardware counter can go on if there
730 * is already an exclusive counter on.
731 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100732 if (!group_can_go_on(counter, cpuctx, 1))
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100733 err = -EEXIST;
734 else
735 err = counter_sched_in(counter, cpuctx, ctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100736
Paul Mackerrasd859e292009-01-17 18:10:22 +1100737 if (err) {
738 /*
739 * This counter couldn't go on. If it is in a group
740 * then we have to pull the whole group off.
741 * If the counter group is pinned then put it in error state.
742 */
743 if (leader != counter)
744 group_sched_out(leader, cpuctx, ctx);
Peter Zijlstra0d486962009-06-02 19:22:16 +0200745 if (leader->attr.pinned) {
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100746 update_group_times(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100747 leader->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100748 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100749 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100750
751 if (!err && !ctx->task && cpuctx->max_pertask)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100752 cpuctx->max_pertask--;
753
Paul Mackerrasd859e292009-01-17 18:10:22 +1100754 unlock:
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200755 perf_enable();
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100756
Peter Zijlstra665c2142009-05-29 14:51:57 +0200757 spin_unlock(&ctx->lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100758}
759
760/*
761 * Attach a performance counter to a context
762 *
763 * First we add the counter to the list with the hardware enable bit
764 * in counter->hw_config cleared.
765 *
766 * If the counter is attached to a task which is on a CPU we use a smp
767 * call to enable it in the task context. The task might have been
768 * scheduled away, but we check this in the smp call again.
Paul Mackerrasd859e292009-01-17 18:10:22 +1100769 *
770 * Must be called with ctx->mutex held.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100771 */
772static void
773perf_install_in_context(struct perf_counter_context *ctx,
774 struct perf_counter *counter,
775 int cpu)
776{
777 struct task_struct *task = ctx->task;
778
Thomas Gleixner0793a612008-12-04 20:12:29 +0100779 if (!task) {
780 /*
781 * Per cpu counters are installed via an smp call and
782 * the install is always sucessful.
783 */
784 smp_call_function_single(cpu, __perf_install_in_context,
785 counter, 1);
786 return;
787 }
788
Thomas Gleixner0793a612008-12-04 20:12:29 +0100789retry:
790 task_oncpu_function_call(task, __perf_install_in_context,
791 counter);
792
793 spin_lock_irq(&ctx->lock);
794 /*
Thomas Gleixner0793a612008-12-04 20:12:29 +0100795 * we need to retry the smp call.
796 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100797 if (ctx->is_active && list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100798 spin_unlock_irq(&ctx->lock);
799 goto retry;
800 }
801
802 /*
803 * The lock prevents that this context is scheduled in so we
804 * can add the counter safely, if it the call above did not
805 * succeed.
806 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100807 if (list_empty(&counter->list_entry))
808 add_counter_to_ctx(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100809 spin_unlock_irq(&ctx->lock);
810}
811
Paul Mackerrasd859e292009-01-17 18:10:22 +1100812/*
813 * Cross CPU call to enable a performance counter
814 */
815static void __perf_counter_enable(void *info)
Ingo Molnar04289bb2008-12-11 08:38:42 +0100816{
Paul Mackerrasd859e292009-01-17 18:10:22 +1100817 struct perf_counter *counter = info;
818 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
819 struct perf_counter_context *ctx = counter->ctx;
820 struct perf_counter *leader = counter->group_leader;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100821 int err;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100822
823 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100824 * If this is a per-task counter, need to check whether this
825 * counter's task is the current task on this cpu.
Ingo Molnar04289bb2008-12-11 08:38:42 +0100826 */
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000827 if (ctx->task && cpuctx->task_ctx != ctx) {
Peter Zijlstra665c2142009-05-29 14:51:57 +0200828 if (cpuctx->task_ctx || ctx->task != current)
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000829 return;
830 cpuctx->task_ctx = ctx;
831 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100832
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200833 spin_lock(&ctx->lock);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000834 ctx->is_active = 1;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200835 update_context_time(ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100836
837 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
838 goto unlock;
839 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200840 counter->tstamp_enabled = ctx->time - counter->total_time_enabled;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100841
842 /*
843 * If the counter is in a group and isn't the group leader,
844 * then don't put it on unless the group is on.
845 */
846 if (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE)
847 goto unlock;
848
Paul Mackerrase758a332009-05-12 21:59:01 +1000849 if (!group_can_go_on(counter, cpuctx, 1)) {
Paul Mackerrasd859e292009-01-17 18:10:22 +1100850 err = -EEXIST;
Paul Mackerrase758a332009-05-12 21:59:01 +1000851 } else {
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200852 perf_disable();
Paul Mackerrase758a332009-05-12 21:59:01 +1000853 if (counter == leader)
854 err = group_sched_in(counter, cpuctx, ctx,
855 smp_processor_id());
856 else
857 err = counter_sched_in(counter, cpuctx, ctx,
858 smp_processor_id());
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200859 perf_enable();
Paul Mackerrase758a332009-05-12 21:59:01 +1000860 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100861
862 if (err) {
863 /*
864 * If this counter can't go on and it's part of a
865 * group, then the whole group has to come off.
866 */
867 if (leader != counter)
868 group_sched_out(leader, cpuctx, ctx);
Peter Zijlstra0d486962009-06-02 19:22:16 +0200869 if (leader->attr.pinned) {
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100870 update_group_times(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100871 leader->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100872 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100873 }
874
875 unlock:
Peter Zijlstra665c2142009-05-29 14:51:57 +0200876 spin_unlock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100877}
878
879/*
880 * Enable a counter.
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000881 *
882 * If counter->ctx is a cloned context, callers must make sure that
883 * every task struct that counter->ctx->task could possibly point to
884 * remains valid. This condition is satisfied when called through
885 * perf_counter_for_each_child or perf_counter_for_each as described
886 * for perf_counter_disable.
Paul Mackerrasd859e292009-01-17 18:10:22 +1100887 */
888static void perf_counter_enable(struct perf_counter *counter)
889{
890 struct perf_counter_context *ctx = counter->ctx;
891 struct task_struct *task = ctx->task;
892
893 if (!task) {
894 /*
895 * Enable the counter on the cpu that it's on
896 */
897 smp_call_function_single(counter->cpu, __perf_counter_enable,
898 counter, 1);
899 return;
900 }
901
902 spin_lock_irq(&ctx->lock);
903 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
904 goto out;
905
906 /*
907 * If the counter is in error state, clear that first.
908 * That way, if we see the counter in error state below, we
909 * know that it has gone back into error state, as distinct
910 * from the task having been scheduled away before the
911 * cross-call arrived.
912 */
913 if (counter->state == PERF_COUNTER_STATE_ERROR)
914 counter->state = PERF_COUNTER_STATE_OFF;
915
916 retry:
917 spin_unlock_irq(&ctx->lock);
918 task_oncpu_function_call(task, __perf_counter_enable, counter);
919
920 spin_lock_irq(&ctx->lock);
921
922 /*
923 * If the context is active and the counter is still off,
924 * we need to retry the cross-call.
925 */
926 if (ctx->is_active && counter->state == PERF_COUNTER_STATE_OFF)
927 goto retry;
928
929 /*
930 * Since we have the lock this context can't be scheduled
931 * in, so we can change the state safely.
932 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100933 if (counter->state == PERF_COUNTER_STATE_OFF) {
Paul Mackerrasd859e292009-01-17 18:10:22 +1100934 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200935 counter->tstamp_enabled =
936 ctx->time - counter->total_time_enabled;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100937 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100938 out:
939 spin_unlock_irq(&ctx->lock);
940}
941
Peter Zijlstra2023b352009-05-05 17:50:26 +0200942static int perf_counter_refresh(struct perf_counter *counter, int refresh)
Peter Zijlstra79f14642009-04-06 11:45:07 +0200943{
Peter Zijlstra2023b352009-05-05 17:50:26 +0200944 /*
945 * not supported on inherited counters
946 */
Peter Zijlstra0d486962009-06-02 19:22:16 +0200947 if (counter->attr.inherit)
Peter Zijlstra2023b352009-05-05 17:50:26 +0200948 return -EINVAL;
949
Peter Zijlstra79f14642009-04-06 11:45:07 +0200950 atomic_add(refresh, &counter->event_limit);
951 perf_counter_enable(counter);
Peter Zijlstra2023b352009-05-05 17:50:26 +0200952
953 return 0;
Peter Zijlstra79f14642009-04-06 11:45:07 +0200954}
955
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100956void __perf_counter_sched_out(struct perf_counter_context *ctx,
957 struct perf_cpu_context *cpuctx)
958{
959 struct perf_counter *counter;
960
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100961 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100962 ctx->is_active = 0;
963 if (likely(!ctx->nr_counters))
964 goto out;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200965 update_context_time(ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100966
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200967 perf_disable();
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100968 if (ctx->nr_active) {
Peter Zijlstraafedadf2009-05-20 12:21:22 +0200969 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
970 if (counter != counter->group_leader)
971 counter_sched_out(counter, cpuctx, ctx);
972 else
973 group_sched_out(counter, cpuctx, ctx);
974 }
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100975 }
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200976 perf_enable();
Paul Mackerrasd859e292009-01-17 18:10:22 +1100977 out:
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100978 spin_unlock(&ctx->lock);
979}
980
Thomas Gleixner0793a612008-12-04 20:12:29 +0100981/*
Paul Mackerras564c2b22009-05-22 14:27:22 +1000982 * Test whether two contexts are equivalent, i.e. whether they
983 * have both been cloned from the same version of the same context
984 * and they both have the same number of enabled counters.
985 * If the number of enabled counters is the same, then the set
986 * of enabled counters should be the same, because these are both
987 * inherited contexts, therefore we can't access individual counters
988 * in them directly with an fd; we can only enable/disable all
989 * counters via prctl, or enable/disable all counters in a family
990 * via ioctl, which will have the same effect on both contexts.
991 */
992static int context_equiv(struct perf_counter_context *ctx1,
993 struct perf_counter_context *ctx2)
994{
995 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
Paul Mackerrasad3a37d2009-05-29 16:06:20 +1000996 && ctx1->parent_gen == ctx2->parent_gen
Paul Mackerras25346b932009-06-01 17:48:12 +1000997 && !ctx1->pin_count && !ctx2->pin_count;
Paul Mackerras564c2b22009-05-22 14:27:22 +1000998}
999
1000/*
Thomas Gleixner0793a612008-12-04 20:12:29 +01001001 * Called from scheduler to remove the counters of the current task,
1002 * with interrupts disabled.
1003 *
1004 * We stop each counter and update the counter value in counter->count.
1005 *
Ingo Molnar76715812008-12-17 14:20:28 +01001006 * This does not protect us against NMI, but disable()
Thomas Gleixner0793a612008-12-04 20:12:29 +01001007 * sets the disabled bit in the control field of counter _before_
1008 * accessing the counter control register. If a NMI hits, then it will
1009 * not restart the counter.
1010 */
Paul Mackerras564c2b22009-05-22 14:27:22 +10001011void perf_counter_task_sched_out(struct task_struct *task,
1012 struct task_struct *next, int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001013{
1014 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001015 struct perf_counter_context *ctx = task->perf_counter_ctxp;
Paul Mackerras564c2b22009-05-22 14:27:22 +10001016 struct perf_counter_context *next_ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001017 struct perf_counter_context *parent;
Peter Zijlstra4a0deca2009-03-19 20:26:12 +01001018 struct pt_regs *regs;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001019 int do_switch = 1;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001020
Peter Zijlstra10989fb2009-05-25 14:45:28 +02001021 regs = task_pt_regs(task);
1022 perf_swcounter_event(PERF_COUNT_CONTEXT_SWITCHES, 1, 1, regs, 0);
1023
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001024 if (likely(!ctx || !cpuctx->task_ctx))
Thomas Gleixner0793a612008-12-04 20:12:29 +01001025 return;
1026
Peter Zijlstrabce379b2009-04-06 11:45:13 +02001027 update_context_time(ctx);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001028
1029 rcu_read_lock();
1030 parent = rcu_dereference(ctx->parent_ctx);
Paul Mackerras564c2b22009-05-22 14:27:22 +10001031 next_ctx = next->perf_counter_ctxp;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001032 if (parent && next_ctx &&
1033 rcu_dereference(next_ctx->parent_ctx) == parent) {
1034 /*
1035 * Looks like the two contexts are clones, so we might be
1036 * able to optimize the context switch. We lock both
1037 * contexts and check that they are clones under the
1038 * lock (including re-checking that neither has been
1039 * uncloned in the meantime). It doesn't matter which
1040 * order we take the locks because no other cpu could
1041 * be trying to lock both of these tasks.
1042 */
1043 spin_lock(&ctx->lock);
1044 spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
1045 if (context_equiv(ctx, next_ctx)) {
Peter Zijlstra665c2142009-05-29 14:51:57 +02001046 /*
1047 * XXX do we need a memory barrier of sorts
1048 * wrt to rcu_dereference() of perf_counter_ctxp
1049 */
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001050 task->perf_counter_ctxp = next_ctx;
1051 next->perf_counter_ctxp = ctx;
1052 ctx->task = next;
1053 next_ctx->task = task;
1054 do_switch = 0;
1055 }
1056 spin_unlock(&next_ctx->lock);
1057 spin_unlock(&ctx->lock);
Paul Mackerras564c2b22009-05-22 14:27:22 +10001058 }
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001059 rcu_read_unlock();
Paul Mackerras564c2b22009-05-22 14:27:22 +10001060
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001061 if (do_switch) {
1062 __perf_counter_sched_out(ctx, cpuctx);
1063 cpuctx->task_ctx = NULL;
1064 }
Thomas Gleixner0793a612008-12-04 20:12:29 +01001065}
1066
Peter Zijlstra665c2142009-05-29 14:51:57 +02001067/*
1068 * Called with IRQs disabled
1069 */
Paul Mackerrasa08b1592009-05-11 15:46:10 +10001070static void __perf_counter_task_sched_out(struct perf_counter_context *ctx)
1071{
1072 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1073
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001074 if (!cpuctx->task_ctx)
1075 return;
Ingo Molnar012b84d2009-05-17 11:08:41 +02001076
1077 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
1078 return;
1079
Paul Mackerrasa08b1592009-05-11 15:46:10 +10001080 __perf_counter_sched_out(ctx, cpuctx);
1081 cpuctx->task_ctx = NULL;
1082}
1083
Peter Zijlstra665c2142009-05-29 14:51:57 +02001084/*
1085 * Called with IRQs disabled
1086 */
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001087static void perf_counter_cpu_sched_out(struct perf_cpu_context *cpuctx)
Ingo Molnar04289bb2008-12-11 08:38:42 +01001088{
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001089 __perf_counter_sched_out(&cpuctx->ctx, cpuctx);
Ingo Molnar04289bb2008-12-11 08:38:42 +01001090}
1091
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001092static void
1093__perf_counter_sched_in(struct perf_counter_context *ctx,
1094 struct perf_cpu_context *cpuctx, int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001095{
Thomas Gleixner0793a612008-12-04 20:12:29 +01001096 struct perf_counter *counter;
Paul Mackerrasdd0e6ba2009-01-12 15:11:00 +11001097 int can_add_hw = 1;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001098
Thomas Gleixner0793a612008-12-04 20:12:29 +01001099 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001100 ctx->is_active = 1;
1101 if (likely(!ctx->nr_counters))
1102 goto out;
1103
Peter Zijlstra4af49982009-04-06 11:45:10 +02001104 ctx->timestamp = perf_clock();
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001105
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001106 perf_disable();
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001107
1108 /*
1109 * First go through the list and put on any pinned groups
1110 * in order to give them the best chance of going on.
1111 */
Ingo Molnar04289bb2008-12-11 08:38:42 +01001112 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001113 if (counter->state <= PERF_COUNTER_STATE_OFF ||
Peter Zijlstra0d486962009-06-02 19:22:16 +02001114 !counter->attr.pinned)
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001115 continue;
1116 if (counter->cpu != -1 && counter->cpu != cpu)
1117 continue;
1118
Peter Zijlstraafedadf2009-05-20 12:21:22 +02001119 if (counter != counter->group_leader)
1120 counter_sched_in(counter, cpuctx, ctx, cpu);
1121 else {
1122 if (group_can_go_on(counter, cpuctx, 1))
1123 group_sched_in(counter, cpuctx, ctx, cpu);
1124 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001125
1126 /*
1127 * If this pinned group hasn't been scheduled,
1128 * put it in error state.
1129 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001130 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
1131 update_group_times(counter);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001132 counter->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001133 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001134 }
1135
1136 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1137 /*
1138 * Ignore counters in OFF or ERROR state, and
1139 * ignore pinned counters since we did them already.
1140 */
1141 if (counter->state <= PERF_COUNTER_STATE_OFF ||
Peter Zijlstra0d486962009-06-02 19:22:16 +02001142 counter->attr.pinned)
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001143 continue;
1144
Ingo Molnar04289bb2008-12-11 08:38:42 +01001145 /*
1146 * Listen to the 'cpu' scheduling filter constraint
1147 * of counters:
1148 */
Thomas Gleixner0793a612008-12-04 20:12:29 +01001149 if (counter->cpu != -1 && counter->cpu != cpu)
1150 continue;
1151
Peter Zijlstraafedadf2009-05-20 12:21:22 +02001152 if (counter != counter->group_leader) {
1153 if (counter_sched_in(counter, cpuctx, ctx, cpu))
Paul Mackerrasdd0e6ba2009-01-12 15:11:00 +11001154 can_add_hw = 0;
Peter Zijlstraafedadf2009-05-20 12:21:22 +02001155 } else {
1156 if (group_can_go_on(counter, cpuctx, can_add_hw)) {
1157 if (group_sched_in(counter, cpuctx, ctx, cpu))
1158 can_add_hw = 0;
1159 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001160 }
Thomas Gleixner0793a612008-12-04 20:12:29 +01001161 }
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001162 perf_enable();
Paul Mackerrasd859e292009-01-17 18:10:22 +11001163 out:
Thomas Gleixner0793a612008-12-04 20:12:29 +01001164 spin_unlock(&ctx->lock);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001165}
Ingo Molnar04289bb2008-12-11 08:38:42 +01001166
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001167/*
1168 * Called from scheduler to add the counters of the current task
1169 * with interrupts disabled.
1170 *
1171 * We restore the counter value and then enable it.
1172 *
1173 * This does not protect us against NMI, but enable()
1174 * sets the enabled bit in the control field of counter _before_
1175 * accessing the counter control register. If a NMI hits, then it will
1176 * keep the counter running.
1177 */
1178void perf_counter_task_sched_in(struct task_struct *task, int cpu)
1179{
1180 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001181 struct perf_counter_context *ctx = task->perf_counter_ctxp;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001182
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001183 if (likely(!ctx))
1184 return;
Paul Mackerras564c2b22009-05-22 14:27:22 +10001185 if (cpuctx->task_ctx == ctx)
1186 return;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001187 __perf_counter_sched_in(ctx, cpuctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001188 cpuctx->task_ctx = ctx;
1189}
1190
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001191static void perf_counter_cpu_sched_in(struct perf_cpu_context *cpuctx, int cpu)
1192{
1193 struct perf_counter_context *ctx = &cpuctx->ctx;
1194
1195 __perf_counter_sched_in(ctx, cpuctx, cpu);
1196}
1197
Peter Zijlstraa78ac322009-05-25 17:39:05 +02001198#define MAX_INTERRUPTS (~0ULL)
1199
1200static void perf_log_throttle(struct perf_counter *counter, int enable);
Peter Zijlstra26b119b2009-05-20 12:21:20 +02001201static void perf_log_period(struct perf_counter *counter, u64 period);
1202
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001203static void perf_adjust_period(struct perf_counter *counter, u64 events)
1204{
1205 struct hw_perf_counter *hwc = &counter->hw;
1206 u64 period, sample_period;
1207 s64 delta;
1208
1209 events *= hwc->sample_period;
1210 period = div64_u64(events, counter->attr.sample_freq);
1211
1212 delta = (s64)(period - hwc->sample_period);
1213 delta = (delta + 7) / 8; /* low pass filter */
1214
1215 sample_period = hwc->sample_period + delta;
1216
1217 if (!sample_period)
1218 sample_period = 1;
1219
1220 perf_log_period(counter, sample_period);
1221
1222 hwc->sample_period = sample_period;
1223}
1224
1225static void perf_ctx_adjust_freq(struct perf_counter_context *ctx)
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001226{
1227 struct perf_counter *counter;
Peter Zijlstra6a24ed6c2009-06-05 18:01:29 +02001228 struct hw_perf_counter *hwc;
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001229 u64 interrupts, freq;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001230
1231 spin_lock(&ctx->lock);
1232 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1233 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
1234 continue;
1235
Peter Zijlstra6a24ed6c2009-06-05 18:01:29 +02001236 hwc = &counter->hw;
1237
1238 interrupts = hwc->interrupts;
1239 hwc->interrupts = 0;
Peter Zijlstraa78ac322009-05-25 17:39:05 +02001240
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001241 /*
1242 * unthrottle counters on the tick
1243 */
Peter Zijlstraa78ac322009-05-25 17:39:05 +02001244 if (interrupts == MAX_INTERRUPTS) {
1245 perf_log_throttle(counter, 1);
1246 counter->pmu->unthrottle(counter);
1247 interrupts = 2*sysctl_perf_counter_limit/HZ;
1248 }
1249
Peter Zijlstra0d486962009-06-02 19:22:16 +02001250 if (!counter->attr.freq || !counter->attr.sample_freq)
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001251 continue;
1252
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001253 /*
1254 * if the specified freq < HZ then we need to skip ticks
1255 */
Peter Zijlstra6a24ed6c2009-06-05 18:01:29 +02001256 if (counter->attr.sample_freq < HZ) {
1257 freq = counter->attr.sample_freq;
1258
1259 hwc->freq_count += freq;
1260 hwc->freq_interrupts += interrupts;
1261
1262 if (hwc->freq_count < HZ)
1263 continue;
1264
1265 interrupts = hwc->freq_interrupts;
1266 hwc->freq_interrupts = 0;
1267 hwc->freq_count -= HZ;
1268 } else
1269 freq = HZ;
1270
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001271 perf_adjust_period(counter, freq * interrupts);
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001272
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001273 /*
1274 * In order to avoid being stalled by an (accidental) huge
1275 * sample period, force reset the sample period if we didn't
1276 * get any events in this freq period.
1277 */
1278 if (!interrupts) {
1279 perf_disable();
1280 counter->pmu->disable(counter);
1281 atomic_set(&hwc->period_left, 0);
1282 counter->pmu->enable(counter);
1283 perf_enable();
1284 }
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001285 }
1286 spin_unlock(&ctx->lock);
1287}
1288
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001289/*
1290 * Round-robin a context's counters:
1291 */
1292static void rotate_ctx(struct perf_counter_context *ctx)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001293{
Thomas Gleixner0793a612008-12-04 20:12:29 +01001294 struct perf_counter *counter;
1295
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001296 if (!ctx->nr_counters)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001297 return;
1298
Thomas Gleixner0793a612008-12-04 20:12:29 +01001299 spin_lock(&ctx->lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001300 /*
Ingo Molnar04289bb2008-12-11 08:38:42 +01001301 * Rotate the first entry last (works just fine for group counters too):
Thomas Gleixner0793a612008-12-04 20:12:29 +01001302 */
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001303 perf_disable();
Ingo Molnar04289bb2008-12-11 08:38:42 +01001304 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Peter Zijlstra75564232009-03-13 12:21:29 +01001305 list_move_tail(&counter->list_entry, &ctx->counter_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001306 break;
1307 }
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001308 perf_enable();
Thomas Gleixner0793a612008-12-04 20:12:29 +01001309
1310 spin_unlock(&ctx->lock);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001311}
Thomas Gleixner0793a612008-12-04 20:12:29 +01001312
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001313void perf_counter_task_tick(struct task_struct *curr, int cpu)
1314{
Peter Zijlstra7fc23a52009-05-08 18:52:21 +02001315 struct perf_cpu_context *cpuctx;
1316 struct perf_counter_context *ctx;
1317
1318 if (!atomic_read(&nr_counters))
1319 return;
1320
1321 cpuctx = &per_cpu(perf_cpu_context, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001322 ctx = curr->perf_counter_ctxp;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001323
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001324 perf_ctx_adjust_freq(&cpuctx->ctx);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001325 if (ctx)
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001326 perf_ctx_adjust_freq(ctx);
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001327
Ingo Molnarb82914c2009-05-04 18:54:32 +02001328 perf_counter_cpu_sched_out(cpuctx);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001329 if (ctx)
1330 __perf_counter_task_sched_out(ctx);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001331
Ingo Molnarb82914c2009-05-04 18:54:32 +02001332 rotate_ctx(&cpuctx->ctx);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001333 if (ctx)
1334 rotate_ctx(ctx);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001335
Ingo Molnarb82914c2009-05-04 18:54:32 +02001336 perf_counter_cpu_sched_in(cpuctx, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001337 if (ctx)
1338 perf_counter_task_sched_in(curr, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001339}
1340
1341/*
Thomas Gleixner0793a612008-12-04 20:12:29 +01001342 * Cross CPU call to read the hardware counter
1343 */
Ingo Molnar76715812008-12-17 14:20:28 +01001344static void __read(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001345{
Ingo Molnar621a01e2008-12-11 12:46:46 +01001346 struct perf_counter *counter = info;
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001347 struct perf_counter_context *ctx = counter->ctx;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001348 unsigned long flags;
Ingo Molnar621a01e2008-12-11 12:46:46 +01001349
Peter Zijlstra849691a2009-04-06 11:45:12 +02001350 local_irq_save(flags);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001351 if (ctx->is_active)
Peter Zijlstra4af49982009-04-06 11:45:10 +02001352 update_context_time(ctx);
Robert Richter4aeb0b42009-04-29 12:47:03 +02001353 counter->pmu->read(counter);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001354 update_counter_times(counter);
Peter Zijlstra849691a2009-04-06 11:45:12 +02001355 local_irq_restore(flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001356}
1357
Ingo Molnar04289bb2008-12-11 08:38:42 +01001358static u64 perf_counter_read(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001359{
1360 /*
1361 * If counter is enabled and currently active on a CPU, update the
1362 * value in the counter structure:
1363 */
Ingo Molnar6a930702008-12-11 15:17:03 +01001364 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
Thomas Gleixner0793a612008-12-04 20:12:29 +01001365 smp_call_function_single(counter->oncpu,
Ingo Molnar76715812008-12-17 14:20:28 +01001366 __read, counter, 1);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001367 } else if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
1368 update_counter_times(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001369 }
1370
Ingo Molnaree060942008-12-13 09:00:03 +01001371 return atomic64_read(&counter->count);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001372}
1373
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001374/*
1375 * Initialize the perf_counter context in a task_struct:
1376 */
1377static void
1378__perf_counter_init_context(struct perf_counter_context *ctx,
1379 struct task_struct *task)
1380{
1381 memset(ctx, 0, sizeof(*ctx));
1382 spin_lock_init(&ctx->lock);
1383 mutex_init(&ctx->mutex);
1384 INIT_LIST_HEAD(&ctx->counter_list);
1385 INIT_LIST_HEAD(&ctx->event_list);
1386 atomic_set(&ctx->refcount, 1);
1387 ctx->task = task;
1388}
1389
Thomas Gleixner0793a612008-12-04 20:12:29 +01001390static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
1391{
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001392 struct perf_counter_context *parent_ctx;
Ingo Molnar22a4f652009-06-01 10:13:37 +02001393 struct perf_counter_context *ctx;
1394 struct perf_cpu_context *cpuctx;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001395 struct task_struct *task;
Paul Mackerras25346b932009-06-01 17:48:12 +10001396 unsigned long flags;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001397 int err;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001398
1399 /*
1400 * If cpu is not a wildcard then this is a percpu counter:
1401 */
1402 if (cpu != -1) {
1403 /* Must be root to operate on a CPU counter: */
Peter Zijlstra07647712009-06-11 11:18:36 +02001404 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
Thomas Gleixner0793a612008-12-04 20:12:29 +01001405 return ERR_PTR(-EACCES);
1406
1407 if (cpu < 0 || cpu > num_possible_cpus())
1408 return ERR_PTR(-EINVAL);
1409
1410 /*
1411 * We could be clever and allow to attach a counter to an
1412 * offline CPU and activate it when the CPU comes up, but
1413 * that's for later.
1414 */
1415 if (!cpu_isset(cpu, cpu_online_map))
1416 return ERR_PTR(-ENODEV);
1417
1418 cpuctx = &per_cpu(perf_cpu_context, cpu);
1419 ctx = &cpuctx->ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001420 get_ctx(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001421
Thomas Gleixner0793a612008-12-04 20:12:29 +01001422 return ctx;
1423 }
1424
1425 rcu_read_lock();
1426 if (!pid)
1427 task = current;
1428 else
1429 task = find_task_by_vpid(pid);
1430 if (task)
1431 get_task_struct(task);
1432 rcu_read_unlock();
1433
1434 if (!task)
1435 return ERR_PTR(-ESRCH);
1436
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001437 /*
1438 * Can't attach counters to a dying task.
1439 */
1440 err = -ESRCH;
1441 if (task->flags & PF_EXITING)
1442 goto errout;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001443
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001444 /* Reuse ptrace permission checks for now. */
1445 err = -EACCES;
1446 if (!ptrace_may_access(task, PTRACE_MODE_READ))
1447 goto errout;
1448
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001449 retry:
Paul Mackerras25346b932009-06-01 17:48:12 +10001450 ctx = perf_lock_task_context(task, &flags);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001451 if (ctx) {
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001452 parent_ctx = ctx->parent_ctx;
1453 if (parent_ctx) {
1454 put_ctx(parent_ctx);
1455 ctx->parent_ctx = NULL; /* no longer a clone */
1456 }
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001457 /*
1458 * Get an extra reference before dropping the lock so that
1459 * this context won't get freed if the task exits.
1460 */
1461 get_ctx(ctx);
Paul Mackerras25346b932009-06-01 17:48:12 +10001462 spin_unlock_irqrestore(&ctx->lock, flags);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001463 }
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001464
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001465 if (!ctx) {
1466 ctx = kmalloc(sizeof(struct perf_counter_context), GFP_KERNEL);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001467 err = -ENOMEM;
1468 if (!ctx)
1469 goto errout;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001470 __perf_counter_init_context(ctx, task);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001471 get_ctx(ctx);
1472 if (cmpxchg(&task->perf_counter_ctxp, NULL, ctx)) {
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001473 /*
1474 * We raced with some other task; use
1475 * the context they set.
1476 */
1477 kfree(ctx);
Paul Mackerras25346b932009-06-01 17:48:12 +10001478 goto retry;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001479 }
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001480 get_task_struct(task);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001481 }
1482
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001483 put_task_struct(task);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001484 return ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001485
1486 errout:
1487 put_task_struct(task);
1488 return ERR_PTR(err);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001489}
1490
Peter Zijlstra592903c2009-03-13 12:21:36 +01001491static void free_counter_rcu(struct rcu_head *head)
1492{
1493 struct perf_counter *counter;
1494
1495 counter = container_of(head, struct perf_counter, rcu_head);
Peter Zijlstra709e50c2009-06-02 14:13:15 +02001496 if (counter->ns)
1497 put_pid_ns(counter->ns);
Peter Zijlstra592903c2009-03-13 12:21:36 +01001498 kfree(counter);
1499}
1500
Peter Zijlstra925d5192009-03-30 19:07:02 +02001501static void perf_pending_sync(struct perf_counter *counter);
1502
Peter Zijlstraf1600952009-03-19 20:26:16 +01001503static void free_counter(struct perf_counter *counter)
1504{
Peter Zijlstra925d5192009-03-30 19:07:02 +02001505 perf_pending_sync(counter);
1506
Peter Zijlstra7fc23a52009-05-08 18:52:21 +02001507 atomic_dec(&nr_counters);
Peter Zijlstra0d486962009-06-02 19:22:16 +02001508 if (counter->attr.mmap)
Peter Zijlstra60313eb2009-06-04 16:53:44 +02001509 atomic_dec(&nr_mmap_counters);
Peter Zijlstra0d486962009-06-02 19:22:16 +02001510 if (counter->attr.comm)
Peter Zijlstra60313eb2009-06-04 16:53:44 +02001511 atomic_dec(&nr_comm_counters);
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02001512
Peter Zijlstrae077df42009-03-19 20:26:17 +01001513 if (counter->destroy)
1514 counter->destroy(counter);
1515
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001516 put_ctx(counter->ctx);
Peter Zijlstraf1600952009-03-19 20:26:16 +01001517 call_rcu(&counter->rcu_head, free_counter_rcu);
1518}
1519
Thomas Gleixner0793a612008-12-04 20:12:29 +01001520/*
1521 * Called when the last reference to the file is gone.
1522 */
1523static int perf_release(struct inode *inode, struct file *file)
1524{
1525 struct perf_counter *counter = file->private_data;
1526 struct perf_counter_context *ctx = counter->ctx;
1527
1528 file->private_data = NULL;
1529
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001530 WARN_ON_ONCE(ctx->parent_ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001531 mutex_lock(&ctx->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01001532 perf_counter_remove_from_context(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001533 mutex_unlock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001534
Peter Zijlstra082ff5a2009-05-23 18:29:00 +02001535 mutex_lock(&counter->owner->perf_counter_mutex);
1536 list_del_init(&counter->owner_entry);
1537 mutex_unlock(&counter->owner->perf_counter_mutex);
1538 put_task_struct(counter->owner);
1539
Peter Zijlstraf1600952009-03-19 20:26:16 +01001540 free_counter(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001541
1542 return 0;
1543}
1544
1545/*
1546 * Read the performance counter - simple non blocking version for now
1547 */
1548static ssize_t
1549perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
1550{
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001551 u64 values[3];
1552 int n;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001553
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001554 /*
1555 * Return end-of-file for a read on a counter that is in
1556 * error state (i.e. because it was pinned but it couldn't be
1557 * scheduled on to the CPU at some point).
1558 */
1559 if (counter->state == PERF_COUNTER_STATE_ERROR)
1560 return 0;
1561
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001562 WARN_ON_ONCE(counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001563 mutex_lock(&counter->child_mutex);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001564 values[0] = perf_counter_read(counter);
1565 n = 1;
Peter Zijlstra0d486962009-06-02 19:22:16 +02001566 if (counter->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001567 values[n++] = counter->total_time_enabled +
1568 atomic64_read(&counter->child_total_time_enabled);
Peter Zijlstra0d486962009-06-02 19:22:16 +02001569 if (counter->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001570 values[n++] = counter->total_time_running +
1571 atomic64_read(&counter->child_total_time_running);
Peter Zijlstra0d486962009-06-02 19:22:16 +02001572 if (counter->attr.read_format & PERF_FORMAT_ID)
Peter Zijlstra8e5799b2009-06-02 15:08:15 +02001573 values[n++] = counter->id;
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001574 mutex_unlock(&counter->child_mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001575
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001576 if (count < n * sizeof(u64))
1577 return -EINVAL;
1578 count = n * sizeof(u64);
1579
1580 if (copy_to_user(buf, values, count))
1581 return -EFAULT;
1582
1583 return count;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001584}
1585
1586static ssize_t
Thomas Gleixner0793a612008-12-04 20:12:29 +01001587perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
1588{
1589 struct perf_counter *counter = file->private_data;
1590
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001591 return perf_read_hw(counter, buf, count);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001592}
1593
1594static unsigned int perf_poll(struct file *file, poll_table *wait)
1595{
1596 struct perf_counter *counter = file->private_data;
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001597 struct perf_mmap_data *data;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001598 unsigned int events = POLL_HUP;
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001599
1600 rcu_read_lock();
1601 data = rcu_dereference(counter->data);
1602 if (data)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001603 events = atomic_xchg(&data->poll, 0);
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001604 rcu_read_unlock();
Thomas Gleixner0793a612008-12-04 20:12:29 +01001605
1606 poll_wait(file, &counter->waitq, wait);
1607
Thomas Gleixner0793a612008-12-04 20:12:29 +01001608 return events;
1609}
1610
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001611static void perf_counter_reset(struct perf_counter *counter)
1612{
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001613 (void)perf_counter_read(counter);
Paul Mackerras615a3f12009-05-11 15:50:21 +10001614 atomic64_set(&counter->count, 0);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001615 perf_counter_update_userpage(counter);
1616}
1617
1618static void perf_counter_for_each_sibling(struct perf_counter *counter,
1619 void (*func)(struct perf_counter *))
1620{
1621 struct perf_counter_context *ctx = counter->ctx;
1622 struct perf_counter *sibling;
1623
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001624 WARN_ON_ONCE(ctx->parent_ctx);
Peter Zijlstra682076a2009-05-23 18:28:57 +02001625 mutex_lock(&ctx->mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001626 counter = counter->group_leader;
1627
1628 func(counter);
1629 list_for_each_entry(sibling, &counter->sibling_list, list_entry)
1630 func(sibling);
Peter Zijlstra682076a2009-05-23 18:28:57 +02001631 mutex_unlock(&ctx->mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001632}
1633
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001634/*
1635 * Holding the top-level counter's child_mutex means that any
1636 * descendant process that has inherited this counter will block
1637 * in sync_child_counter if it goes to exit, thus satisfying the
1638 * task existence requirements of perf_counter_enable/disable.
1639 */
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001640static void perf_counter_for_each_child(struct perf_counter *counter,
1641 void (*func)(struct perf_counter *))
1642{
1643 struct perf_counter *child;
1644
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001645 WARN_ON_ONCE(counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001646 mutex_lock(&counter->child_mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001647 func(counter);
1648 list_for_each_entry(child, &counter->child_list, child_list)
1649 func(child);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001650 mutex_unlock(&counter->child_mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001651}
1652
1653static void perf_counter_for_each(struct perf_counter *counter,
1654 void (*func)(struct perf_counter *))
1655{
1656 struct perf_counter *child;
1657
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001658 WARN_ON_ONCE(counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001659 mutex_lock(&counter->child_mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001660 perf_counter_for_each_sibling(counter, func);
1661 list_for_each_entry(child, &counter->child_list, child_list)
1662 perf_counter_for_each_sibling(child, func);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001663 mutex_unlock(&counter->child_mutex);
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001664}
1665
Peter Zijlstra08247e32009-06-02 16:46:57 +02001666static int perf_counter_period(struct perf_counter *counter, u64 __user *arg)
1667{
1668 struct perf_counter_context *ctx = counter->ctx;
1669 unsigned long size;
1670 int ret = 0;
1671 u64 value;
1672
Peter Zijlstra0d486962009-06-02 19:22:16 +02001673 if (!counter->attr.sample_period)
Peter Zijlstra08247e32009-06-02 16:46:57 +02001674 return -EINVAL;
1675
1676 size = copy_from_user(&value, arg, sizeof(value));
1677 if (size != sizeof(value))
1678 return -EFAULT;
1679
1680 if (!value)
1681 return -EINVAL;
1682
1683 spin_lock_irq(&ctx->lock);
Peter Zijlstra0d486962009-06-02 19:22:16 +02001684 if (counter->attr.freq) {
Peter Zijlstra08247e32009-06-02 16:46:57 +02001685 if (value > sysctl_perf_counter_limit) {
1686 ret = -EINVAL;
1687 goto unlock;
1688 }
1689
Peter Zijlstra0d486962009-06-02 19:22:16 +02001690 counter->attr.sample_freq = value;
Peter Zijlstra08247e32009-06-02 16:46:57 +02001691 } else {
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001692 perf_log_period(counter, value);
1693
Peter Zijlstra0d486962009-06-02 19:22:16 +02001694 counter->attr.sample_period = value;
Peter Zijlstra08247e32009-06-02 16:46:57 +02001695 counter->hw.sample_period = value;
Peter Zijlstra08247e32009-06-02 16:46:57 +02001696 }
1697unlock:
1698 spin_unlock_irq(&ctx->lock);
1699
1700 return ret;
1701}
1702
Paul Mackerrasd859e292009-01-17 18:10:22 +11001703static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1704{
1705 struct perf_counter *counter = file->private_data;
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001706 void (*func)(struct perf_counter *);
1707 u32 flags = arg;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001708
1709 switch (cmd) {
1710 case PERF_COUNTER_IOC_ENABLE:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001711 func = perf_counter_enable;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001712 break;
1713 case PERF_COUNTER_IOC_DISABLE:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001714 func = perf_counter_disable;
Peter Zijlstra79f14642009-04-06 11:45:07 +02001715 break;
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001716 case PERF_COUNTER_IOC_RESET:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001717 func = perf_counter_reset;
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001718 break;
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001719
1720 case PERF_COUNTER_IOC_REFRESH:
1721 return perf_counter_refresh(counter, arg);
Peter Zijlstra08247e32009-06-02 16:46:57 +02001722
1723 case PERF_COUNTER_IOC_PERIOD:
1724 return perf_counter_period(counter, (u64 __user *)arg);
1725
Paul Mackerrasd859e292009-01-17 18:10:22 +11001726 default:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001727 return -ENOTTY;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001728 }
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001729
1730 if (flags & PERF_IOC_FLAG_GROUP)
1731 perf_counter_for_each(counter, func);
1732 else
1733 perf_counter_for_each_child(counter, func);
1734
1735 return 0;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001736}
1737
Peter Zijlstra771d7cd2009-05-25 14:45:26 +02001738int perf_counter_task_enable(void)
1739{
1740 struct perf_counter *counter;
1741
1742 mutex_lock(&current->perf_counter_mutex);
1743 list_for_each_entry(counter, &current->perf_counter_list, owner_entry)
1744 perf_counter_for_each_child(counter, perf_counter_enable);
1745 mutex_unlock(&current->perf_counter_mutex);
1746
1747 return 0;
1748}
1749
1750int perf_counter_task_disable(void)
1751{
1752 struct perf_counter *counter;
1753
1754 mutex_lock(&current->perf_counter_mutex);
1755 list_for_each_entry(counter, &current->perf_counter_list, owner_entry)
1756 perf_counter_for_each_child(counter, perf_counter_disable);
1757 mutex_unlock(&current->perf_counter_mutex);
1758
1759 return 0;
1760}
1761
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001762/*
1763 * Callers need to ensure there can be no nesting of this function, otherwise
1764 * the seqlock logic goes bad. We can not serialize this because the arch
1765 * code calls this from NMI context.
1766 */
1767void perf_counter_update_userpage(struct perf_counter *counter)
Paul Mackerras37d81822009-03-23 18:22:08 +01001768{
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001769 struct perf_counter_mmap_page *userpg;
Ingo Molnar22a4f652009-06-01 10:13:37 +02001770 struct perf_mmap_data *data;
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001771
1772 rcu_read_lock();
1773 data = rcu_dereference(counter->data);
1774 if (!data)
1775 goto unlock;
1776
1777 userpg = data->user_page;
Paul Mackerras37d81822009-03-23 18:22:08 +01001778
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001779 /*
1780 * Disable preemption so as to not let the corresponding user-space
1781 * spin too long if we get preempted.
1782 */
1783 preempt_disable();
Paul Mackerras37d81822009-03-23 18:22:08 +01001784 ++userpg->lock;
Peter Zijlstra92f22a32009-04-02 11:12:04 +02001785 barrier();
Paul Mackerras37d81822009-03-23 18:22:08 +01001786 userpg->index = counter->hw.idx;
1787 userpg->offset = atomic64_read(&counter->count);
1788 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
1789 userpg->offset -= atomic64_read(&counter->hw.prev_count);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001790
Peter Zijlstra92f22a32009-04-02 11:12:04 +02001791 barrier();
Paul Mackerras37d81822009-03-23 18:22:08 +01001792 ++userpg->lock;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001793 preempt_enable();
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001794unlock:
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001795 rcu_read_unlock();
Paul Mackerras37d81822009-03-23 18:22:08 +01001796}
1797
1798static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1799{
1800 struct perf_counter *counter = vma->vm_file->private_data;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001801 struct perf_mmap_data *data;
1802 int ret = VM_FAULT_SIGBUS;
Paul Mackerras37d81822009-03-23 18:22:08 +01001803
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001804 rcu_read_lock();
1805 data = rcu_dereference(counter->data);
1806 if (!data)
1807 goto unlock;
Paul Mackerras37d81822009-03-23 18:22:08 +01001808
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001809 if (vmf->pgoff == 0) {
1810 vmf->page = virt_to_page(data->user_page);
1811 } else {
1812 int nr = vmf->pgoff - 1;
1813
1814 if ((unsigned)nr > data->nr_pages)
1815 goto unlock;
1816
1817 vmf->page = virt_to_page(data->data_pages[nr]);
1818 }
Paul Mackerras37d81822009-03-23 18:22:08 +01001819 get_page(vmf->page);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001820 ret = 0;
1821unlock:
1822 rcu_read_unlock();
1823
1824 return ret;
1825}
1826
1827static int perf_mmap_data_alloc(struct perf_counter *counter, int nr_pages)
1828{
1829 struct perf_mmap_data *data;
1830 unsigned long size;
1831 int i;
1832
1833 WARN_ON(atomic_read(&counter->mmap_count));
1834
1835 size = sizeof(struct perf_mmap_data);
1836 size += nr_pages * sizeof(void *);
1837
1838 data = kzalloc(size, GFP_KERNEL);
1839 if (!data)
1840 goto fail;
1841
1842 data->user_page = (void *)get_zeroed_page(GFP_KERNEL);
1843 if (!data->user_page)
1844 goto fail_user_page;
1845
1846 for (i = 0; i < nr_pages; i++) {
1847 data->data_pages[i] = (void *)get_zeroed_page(GFP_KERNEL);
1848 if (!data->data_pages[i])
1849 goto fail_data_pages;
1850 }
1851
1852 data->nr_pages = nr_pages;
Peter Zijlstra22c15582009-05-05 17:50:25 +02001853 atomic_set(&data->lock, -1);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001854
1855 rcu_assign_pointer(counter->data, data);
1856
Paul Mackerras37d81822009-03-23 18:22:08 +01001857 return 0;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001858
1859fail_data_pages:
1860 for (i--; i >= 0; i--)
1861 free_page((unsigned long)data->data_pages[i]);
1862
1863 free_page((unsigned long)data->user_page);
1864
1865fail_user_page:
1866 kfree(data);
1867
1868fail:
1869 return -ENOMEM;
1870}
1871
1872static void __perf_mmap_data_free(struct rcu_head *rcu_head)
1873{
Ingo Molnar22a4f652009-06-01 10:13:37 +02001874 struct perf_mmap_data *data;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001875 int i;
1876
Ingo Molnar22a4f652009-06-01 10:13:37 +02001877 data = container_of(rcu_head, struct perf_mmap_data, rcu_head);
1878
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001879 free_page((unsigned long)data->user_page);
1880 for (i = 0; i < data->nr_pages; i++)
1881 free_page((unsigned long)data->data_pages[i]);
1882 kfree(data);
1883}
1884
1885static void perf_mmap_data_free(struct perf_counter *counter)
1886{
1887 struct perf_mmap_data *data = counter->data;
1888
1889 WARN_ON(atomic_read(&counter->mmap_count));
1890
1891 rcu_assign_pointer(counter->data, NULL);
1892 call_rcu(&data->rcu_head, __perf_mmap_data_free);
1893}
1894
1895static void perf_mmap_open(struct vm_area_struct *vma)
1896{
1897 struct perf_counter *counter = vma->vm_file->private_data;
1898
1899 atomic_inc(&counter->mmap_count);
1900}
1901
1902static void perf_mmap_close(struct vm_area_struct *vma)
1903{
1904 struct perf_counter *counter = vma->vm_file->private_data;
1905
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001906 WARN_ON_ONCE(counter->ctx->parent_ctx);
Ingo Molnar22a4f652009-06-01 10:13:37 +02001907 if (atomic_dec_and_mutex_lock(&counter->mmap_count, &counter->mmap_mutex)) {
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001908 struct user_struct *user = current_user();
1909
1910 atomic_long_sub(counter->data->nr_pages + 1, &user->locked_vm);
Peter Zijlstrac5078f72009-05-05 17:50:24 +02001911 vma->vm_mm->locked_vm -= counter->data->nr_locked;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001912 perf_mmap_data_free(counter);
1913 mutex_unlock(&counter->mmap_mutex);
1914 }
Paul Mackerras37d81822009-03-23 18:22:08 +01001915}
1916
1917static struct vm_operations_struct perf_mmap_vmops = {
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001918 .open = perf_mmap_open,
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001919 .close = perf_mmap_close,
Paul Mackerras37d81822009-03-23 18:22:08 +01001920 .fault = perf_mmap_fault,
1921};
1922
1923static int perf_mmap(struct file *file, struct vm_area_struct *vma)
1924{
1925 struct perf_counter *counter = file->private_data;
Ingo Molnar22a4f652009-06-01 10:13:37 +02001926 unsigned long user_locked, user_lock_limit;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001927 struct user_struct *user = current_user();
Ingo Molnar22a4f652009-06-01 10:13:37 +02001928 unsigned long locked, lock_limit;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001929 unsigned long vma_size;
1930 unsigned long nr_pages;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001931 long user_extra, extra;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001932 int ret = 0;
Paul Mackerras37d81822009-03-23 18:22:08 +01001933
1934 if (!(vma->vm_flags & VM_SHARED) || (vma->vm_flags & VM_WRITE))
1935 return -EINVAL;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001936
1937 vma_size = vma->vm_end - vma->vm_start;
1938 nr_pages = (vma_size / PAGE_SIZE) - 1;
1939
Peter Zijlstra7730d862009-03-25 12:48:31 +01001940 /*
1941 * If we have data pages ensure they're a power-of-two number, so we
1942 * can do bitmasks instead of modulo.
1943 */
1944 if (nr_pages != 0 && !is_power_of_2(nr_pages))
Paul Mackerras37d81822009-03-23 18:22:08 +01001945 return -EINVAL;
1946
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001947 if (vma_size != PAGE_SIZE * (1 + nr_pages))
Paul Mackerras37d81822009-03-23 18:22:08 +01001948 return -EINVAL;
1949
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001950 if (vma->vm_pgoff != 0)
1951 return -EINVAL;
Paul Mackerras37d81822009-03-23 18:22:08 +01001952
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001953 WARN_ON_ONCE(counter->ctx->parent_ctx);
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001954 mutex_lock(&counter->mmap_mutex);
1955 if (atomic_inc_not_zero(&counter->mmap_count)) {
1956 if (nr_pages != counter->data->nr_pages)
1957 ret = -EINVAL;
1958 goto unlock;
1959 }
1960
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001961 user_extra = nr_pages + 1;
1962 user_lock_limit = sysctl_perf_counter_mlock >> (PAGE_SHIFT - 10);
Ingo Molnara3862d32009-05-24 09:02:37 +02001963
1964 /*
1965 * Increase the limit linearly with more CPUs:
1966 */
1967 user_lock_limit *= num_online_cpus();
1968
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001969 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
Peter Zijlstrac5078f72009-05-05 17:50:24 +02001970
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001971 extra = 0;
1972 if (user_locked > user_lock_limit)
1973 extra = user_locked - user_lock_limit;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001974
1975 lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
1976 lock_limit >>= PAGE_SHIFT;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001977 locked = vma->vm_mm->locked_vm + extra;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001978
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001979 if ((locked > lock_limit) && !capable(CAP_IPC_LOCK)) {
1980 ret = -EPERM;
1981 goto unlock;
1982 }
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001983
1984 WARN_ON(counter->data);
1985 ret = perf_mmap_data_alloc(counter, nr_pages);
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001986 if (ret)
1987 goto unlock;
1988
1989 atomic_set(&counter->mmap_count, 1);
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001990 atomic_long_add(user_extra, &user->locked_vm);
Peter Zijlstrac5078f72009-05-05 17:50:24 +02001991 vma->vm_mm->locked_vm += extra;
1992 counter->data->nr_locked = extra;
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001993unlock:
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001994 mutex_unlock(&counter->mmap_mutex);
Paul Mackerras37d81822009-03-23 18:22:08 +01001995
1996 vma->vm_flags &= ~VM_MAYWRITE;
1997 vma->vm_flags |= VM_RESERVED;
1998 vma->vm_ops = &perf_mmap_vmops;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001999
2000 return ret;
Paul Mackerras37d81822009-03-23 18:22:08 +01002001}
2002
Peter Zijlstra3c446b3d2009-04-06 11:45:01 +02002003static int perf_fasync(int fd, struct file *filp, int on)
2004{
Peter Zijlstra3c446b3d2009-04-06 11:45:01 +02002005 struct inode *inode = filp->f_path.dentry->d_inode;
Ingo Molnar22a4f652009-06-01 10:13:37 +02002006 struct perf_counter *counter = filp->private_data;
Peter Zijlstra3c446b3d2009-04-06 11:45:01 +02002007 int retval;
2008
2009 mutex_lock(&inode->i_mutex);
2010 retval = fasync_helper(fd, filp, on, &counter->fasync);
2011 mutex_unlock(&inode->i_mutex);
2012
2013 if (retval < 0)
2014 return retval;
2015
2016 return 0;
2017}
2018
Thomas Gleixner0793a612008-12-04 20:12:29 +01002019static const struct file_operations perf_fops = {
2020 .release = perf_release,
2021 .read = perf_read,
2022 .poll = perf_poll,
Paul Mackerrasd859e292009-01-17 18:10:22 +11002023 .unlocked_ioctl = perf_ioctl,
2024 .compat_ioctl = perf_ioctl,
Paul Mackerras37d81822009-03-23 18:22:08 +01002025 .mmap = perf_mmap,
Peter Zijlstra3c446b3d2009-04-06 11:45:01 +02002026 .fasync = perf_fasync,
Thomas Gleixner0793a612008-12-04 20:12:29 +01002027};
2028
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002029/*
Peter Zijlstra925d5192009-03-30 19:07:02 +02002030 * Perf counter wakeup
2031 *
2032 * If there's data, ensure we set the poll() state and publish everything
2033 * to user-space before waking everybody up.
2034 */
2035
2036void perf_counter_wakeup(struct perf_counter *counter)
2037{
Peter Zijlstra925d5192009-03-30 19:07:02 +02002038 wake_up_all(&counter->waitq);
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002039
2040 if (counter->pending_kill) {
2041 kill_fasync(&counter->fasync, SIGIO, counter->pending_kill);
2042 counter->pending_kill = 0;
2043 }
Peter Zijlstra925d5192009-03-30 19:07:02 +02002044}
2045
2046/*
2047 * Pending wakeups
2048 *
2049 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
2050 *
2051 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
2052 * single linked list and use cmpxchg() to add entries lockless.
2053 */
2054
Peter Zijlstra79f14642009-04-06 11:45:07 +02002055static void perf_pending_counter(struct perf_pending_entry *entry)
2056{
2057 struct perf_counter *counter = container_of(entry,
2058 struct perf_counter, pending);
2059
2060 if (counter->pending_disable) {
2061 counter->pending_disable = 0;
2062 perf_counter_disable(counter);
2063 }
2064
2065 if (counter->pending_wakeup) {
2066 counter->pending_wakeup = 0;
2067 perf_counter_wakeup(counter);
2068 }
2069}
2070
Peter Zijlstra671dec52009-04-06 11:45:02 +02002071#define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
Peter Zijlstra925d5192009-03-30 19:07:02 +02002072
Peter Zijlstra671dec52009-04-06 11:45:02 +02002073static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = {
Peter Zijlstra925d5192009-03-30 19:07:02 +02002074 PENDING_TAIL,
2075};
2076
Peter Zijlstra671dec52009-04-06 11:45:02 +02002077static void perf_pending_queue(struct perf_pending_entry *entry,
2078 void (*func)(struct perf_pending_entry *))
Peter Zijlstra925d5192009-03-30 19:07:02 +02002079{
Peter Zijlstra671dec52009-04-06 11:45:02 +02002080 struct perf_pending_entry **head;
Peter Zijlstra925d5192009-03-30 19:07:02 +02002081
Peter Zijlstra671dec52009-04-06 11:45:02 +02002082 if (cmpxchg(&entry->next, NULL, PENDING_TAIL) != NULL)
Peter Zijlstra925d5192009-03-30 19:07:02 +02002083 return;
2084
Peter Zijlstra671dec52009-04-06 11:45:02 +02002085 entry->func = func;
2086
2087 head = &get_cpu_var(perf_pending_head);
Peter Zijlstra925d5192009-03-30 19:07:02 +02002088
2089 do {
Peter Zijlstra671dec52009-04-06 11:45:02 +02002090 entry->next = *head;
2091 } while (cmpxchg(head, entry->next, entry) != entry->next);
Peter Zijlstra925d5192009-03-30 19:07:02 +02002092
2093 set_perf_counter_pending();
2094
Peter Zijlstra671dec52009-04-06 11:45:02 +02002095 put_cpu_var(perf_pending_head);
Peter Zijlstra925d5192009-03-30 19:07:02 +02002096}
2097
2098static int __perf_pending_run(void)
2099{
Peter Zijlstra671dec52009-04-06 11:45:02 +02002100 struct perf_pending_entry *list;
Peter Zijlstra925d5192009-03-30 19:07:02 +02002101 int nr = 0;
2102
Peter Zijlstra671dec52009-04-06 11:45:02 +02002103 list = xchg(&__get_cpu_var(perf_pending_head), PENDING_TAIL);
Peter Zijlstra925d5192009-03-30 19:07:02 +02002104 while (list != PENDING_TAIL) {
Peter Zijlstra671dec52009-04-06 11:45:02 +02002105 void (*func)(struct perf_pending_entry *);
2106 struct perf_pending_entry *entry = list;
Peter Zijlstra925d5192009-03-30 19:07:02 +02002107
2108 list = list->next;
2109
Peter Zijlstra671dec52009-04-06 11:45:02 +02002110 func = entry->func;
2111 entry->next = NULL;
Peter Zijlstra925d5192009-03-30 19:07:02 +02002112 /*
2113 * Ensure we observe the unqueue before we issue the wakeup,
2114 * so that we won't be waiting forever.
2115 * -- see perf_not_pending().
2116 */
2117 smp_wmb();
2118
Peter Zijlstra671dec52009-04-06 11:45:02 +02002119 func(entry);
Peter Zijlstra925d5192009-03-30 19:07:02 +02002120 nr++;
2121 }
2122
2123 return nr;
2124}
2125
2126static inline int perf_not_pending(struct perf_counter *counter)
2127{
2128 /*
2129 * If we flush on whatever cpu we run, there is a chance we don't
2130 * need to wait.
2131 */
2132 get_cpu();
2133 __perf_pending_run();
2134 put_cpu();
2135
2136 /*
2137 * Ensure we see the proper queue state before going to sleep
2138 * so that we do not miss the wakeup. -- see perf_pending_handle()
2139 */
2140 smp_rmb();
Peter Zijlstra671dec52009-04-06 11:45:02 +02002141 return counter->pending.next == NULL;
Peter Zijlstra925d5192009-03-30 19:07:02 +02002142}
2143
2144static void perf_pending_sync(struct perf_counter *counter)
2145{
2146 wait_event(counter->waitq, perf_not_pending(counter));
2147}
2148
2149void perf_counter_do_pending(void)
2150{
2151 __perf_pending_run();
2152}
2153
2154/*
Peter Zijlstra394ee072009-03-30 19:07:14 +02002155 * Callchain support -- arch specific
2156 */
2157
Peter Zijlstra9c03d882009-04-06 11:45:00 +02002158__weak struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
Peter Zijlstra394ee072009-03-30 19:07:14 +02002159{
2160 return NULL;
2161}
2162
2163/*
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002164 * Output
2165 */
2166
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002167struct perf_output_handle {
2168 struct perf_counter *counter;
2169 struct perf_mmap_data *data;
Peter Zijlstra8e3747c2009-06-02 16:16:02 +02002170 unsigned long head;
2171 unsigned long offset;
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002172 int nmi;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002173 int overflow;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002174 int locked;
2175 unsigned long flags;
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002176};
2177
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002178static void perf_output_wakeup(struct perf_output_handle *handle)
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002179{
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002180 atomic_set(&handle->data->poll, POLL_IN);
2181
Peter Zijlstra671dec52009-04-06 11:45:02 +02002182 if (handle->nmi) {
Peter Zijlstra79f14642009-04-06 11:45:07 +02002183 handle->counter->pending_wakeup = 1;
Peter Zijlstra671dec52009-04-06 11:45:02 +02002184 perf_pending_queue(&handle->counter->pending,
Peter Zijlstra79f14642009-04-06 11:45:07 +02002185 perf_pending_counter);
Peter Zijlstra671dec52009-04-06 11:45:02 +02002186 } else
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002187 perf_counter_wakeup(handle->counter);
2188}
2189
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002190/*
2191 * Curious locking construct.
2192 *
2193 * We need to ensure a later event doesn't publish a head when a former
2194 * event isn't done writing. However since we need to deal with NMIs we
2195 * cannot fully serialize things.
2196 *
2197 * What we do is serialize between CPUs so we only have to deal with NMI
2198 * nesting on a single CPU.
2199 *
2200 * We only publish the head (and generate a wakeup) when the outer-most
2201 * event completes.
2202 */
2203static void perf_output_lock(struct perf_output_handle *handle)
2204{
2205 struct perf_mmap_data *data = handle->data;
2206 int cpu;
2207
2208 handle->locked = 0;
2209
2210 local_irq_save(handle->flags);
2211 cpu = smp_processor_id();
2212
2213 if (in_nmi() && atomic_read(&data->lock) == cpu)
2214 return;
2215
Peter Zijlstra22c15582009-05-05 17:50:25 +02002216 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002217 cpu_relax();
2218
2219 handle->locked = 1;
2220}
2221
2222static void perf_output_unlock(struct perf_output_handle *handle)
2223{
2224 struct perf_mmap_data *data = handle->data;
Peter Zijlstra8e3747c2009-06-02 16:16:02 +02002225 unsigned long head;
2226 int cpu;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002227
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002228 data->done_head = data->head;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002229
2230 if (!handle->locked)
2231 goto out;
2232
2233again:
2234 /*
2235 * The xchg implies a full barrier that ensures all writes are done
2236 * before we publish the new head, matched by a rmb() in userspace when
2237 * reading this position.
2238 */
Peter Zijlstra8e3747c2009-06-02 16:16:02 +02002239 while ((head = atomic_long_xchg(&data->done_head, 0)))
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002240 data->user_page->data_head = head;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002241
2242 /*
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002243 * NMI can happen here, which means we can miss a done_head update.
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002244 */
2245
Peter Zijlstra22c15582009-05-05 17:50:25 +02002246 cpu = atomic_xchg(&data->lock, -1);
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002247 WARN_ON_ONCE(cpu != smp_processor_id());
2248
2249 /*
2250 * Therefore we have to validate we did not indeed do so.
2251 */
Peter Zijlstra8e3747c2009-06-02 16:16:02 +02002252 if (unlikely(atomic_long_read(&data->done_head))) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002253 /*
2254 * Since we had it locked, we can lock it again.
2255 */
Peter Zijlstra22c15582009-05-05 17:50:25 +02002256 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002257 cpu_relax();
2258
2259 goto again;
2260 }
2261
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002262 if (atomic_xchg(&data->wakeup, 0))
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002263 perf_output_wakeup(handle);
2264out:
2265 local_irq_restore(handle->flags);
2266}
2267
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002268static int perf_output_begin(struct perf_output_handle *handle,
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002269 struct perf_counter *counter, unsigned int size,
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002270 int nmi, int overflow)
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002271{
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002272 struct perf_mmap_data *data;
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002273 unsigned int offset, head;
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002274
Peter Zijlstra2023b352009-05-05 17:50:26 +02002275 /*
2276 * For inherited counters we send all the output towards the parent.
2277 */
2278 if (counter->parent)
2279 counter = counter->parent;
2280
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002281 rcu_read_lock();
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002282 data = rcu_dereference(counter->data);
2283 if (!data)
2284 goto out;
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002285
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002286 handle->data = data;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002287 handle->counter = counter;
2288 handle->nmi = nmi;
2289 handle->overflow = overflow;
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002290
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002291 if (!data->nr_pages)
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002292 goto fail;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002293
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002294 perf_output_lock(handle);
2295
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002296 do {
Paul Mackerras6dc5f2a2009-06-05 12:36:28 +10002297 offset = head = atomic_long_read(&data->head);
Peter Zijlstrac7138f32009-03-24 13:18:16 +01002298 head += size;
Peter Zijlstra8e3747c2009-06-02 16:16:02 +02002299 } while (atomic_long_cmpxchg(&data->head, offset, head) != offset);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002300
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002301 handle->offset = offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01002302 handle->head = head;
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002303
2304 if ((offset >> PAGE_SHIFT) != (head >> PAGE_SHIFT))
2305 atomic_set(&data->wakeup, 1);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002306
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002307 return 0;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002308
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002309fail:
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002310 perf_output_wakeup(handle);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002311out:
2312 rcu_read_unlock();
2313
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002314 return -ENOSPC;
2315}
2316
2317static void perf_output_copy(struct perf_output_handle *handle,
Peter Zijlstra089dd792009-06-05 14:04:55 +02002318 const void *buf, unsigned int len)
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002319{
2320 unsigned int pages_mask;
2321 unsigned int offset;
2322 unsigned int size;
2323 void **pages;
2324
2325 offset = handle->offset;
2326 pages_mask = handle->data->nr_pages - 1;
2327 pages = handle->data->data_pages;
2328
2329 do {
2330 unsigned int page_offset;
2331 int nr;
2332
2333 nr = (offset >> PAGE_SHIFT) & pages_mask;
2334 page_offset = offset & (PAGE_SIZE - 1);
2335 size = min_t(unsigned int, PAGE_SIZE - page_offset, len);
2336
2337 memcpy(pages[nr] + page_offset, buf, size);
2338
2339 len -= size;
2340 buf += size;
2341 offset += size;
2342 } while (len);
2343
2344 handle->offset = offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01002345
Peter Zijlstra53020fe2009-05-13 21:26:19 +02002346 /*
2347 * Check we didn't copy past our reservation window, taking the
2348 * possible unsigned int wrap into account.
2349 */
Peter Zijlstra8e3747c2009-06-02 16:16:02 +02002350 WARN_ON_ONCE(((long)(handle->head - handle->offset)) < 0);
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002351}
2352
Peter Zijlstra5c148192009-03-25 12:30:23 +01002353#define perf_output_put(handle, x) \
2354 perf_output_copy((handle), &(x), sizeof(x))
2355
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002356static void perf_output_end(struct perf_output_handle *handle)
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002357{
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002358 struct perf_counter *counter = handle->counter;
2359 struct perf_mmap_data *data = handle->data;
2360
Peter Zijlstra0d486962009-06-02 19:22:16 +02002361 int wakeup_events = counter->attr.wakeup_events;
Peter Zijlstrac4578102009-04-02 11:12:01 +02002362
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002363 if (handle->overflow && wakeup_events) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002364 int events = atomic_inc_return(&data->events);
Peter Zijlstrac4578102009-04-02 11:12:01 +02002365 if (events >= wakeup_events) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002366 atomic_sub(wakeup_events, &data->events);
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002367 atomic_set(&data->wakeup, 1);
Peter Zijlstrac4578102009-04-02 11:12:01 +02002368 }
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002369 }
2370
2371 perf_output_unlock(handle);
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002372 rcu_read_unlock();
2373}
2374
Peter Zijlstra709e50c2009-06-02 14:13:15 +02002375static u32 perf_counter_pid(struct perf_counter *counter, struct task_struct *p)
2376{
2377 /*
2378 * only top level counters have the pid namespace they were created in
2379 */
2380 if (counter->parent)
2381 counter = counter->parent;
2382
2383 return task_tgid_nr_ns(p, counter->ns);
2384}
2385
2386static u32 perf_counter_tid(struct perf_counter *counter, struct task_struct *p)
2387{
2388 /*
2389 * only top level counters have the pid namespace they were created in
2390 */
2391 if (counter->parent)
2392 counter = counter->parent;
2393
2394 return task_pid_nr_ns(p, counter->ns);
2395}
2396
Peter Zijlstradf1a1322009-06-10 21:02:22 +02002397static void perf_counter_output(struct perf_counter *counter, int nmi,
2398 struct perf_sample_data *data)
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002399{
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002400 int ret;
Peter Zijlstra0d486962009-06-02 19:22:16 +02002401 u64 sample_type = counter->attr.sample_type;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002402 struct perf_output_handle handle;
2403 struct perf_event_header header;
2404 u64 ip;
Peter Zijlstra5c148192009-03-25 12:30:23 +01002405 struct {
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002406 u32 pid, tid;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002407 } tid_entry;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002408 struct {
Peter Zijlstra8e5799b2009-06-02 15:08:15 +02002409 u64 id;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002410 u64 counter;
2411 } group_entry;
Peter Zijlstra394ee072009-03-30 19:07:14 +02002412 struct perf_callchain_entry *callchain = NULL;
2413 int callchain_size = 0;
Peter Zijlstra339f7c92009-04-06 11:45:06 +02002414 u64 time;
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002415 struct {
2416 u32 cpu, reserved;
2417 } cpu_entry;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002418
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02002419 header.type = 0;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002420 header.size = sizeof(header);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002421
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02002422 header.misc = PERF_EVENT_MISC_OVERFLOW;
Peter Zijlstradf1a1322009-06-10 21:02:22 +02002423 header.misc |= perf_misc_flags(data->regs);
Peter Zijlstra6fab0192009-04-08 15:01:26 +02002424
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002425 if (sample_type & PERF_SAMPLE_IP) {
Peter Zijlstradf1a1322009-06-10 21:02:22 +02002426 ip = perf_instruction_pointer(data->regs);
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002427 header.type |= PERF_SAMPLE_IP;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002428 header.size += sizeof(ip);
2429 }
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002430
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002431 if (sample_type & PERF_SAMPLE_TID) {
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002432 /* namespace issues */
Peter Zijlstra709e50c2009-06-02 14:13:15 +02002433 tid_entry.pid = perf_counter_pid(counter, current);
2434 tid_entry.tid = perf_counter_tid(counter, current);
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002435
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002436 header.type |= PERF_SAMPLE_TID;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002437 header.size += sizeof(tid_entry);
2438 }
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002439
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002440 if (sample_type & PERF_SAMPLE_TIME) {
Peter Zijlstra4d855452009-04-08 15:01:32 +02002441 /*
2442 * Maybe do better on x86 and provide cpu_clock_nmi()
2443 */
2444 time = sched_clock();
2445
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002446 header.type |= PERF_SAMPLE_TIME;
Peter Zijlstra4d855452009-04-08 15:01:32 +02002447 header.size += sizeof(u64);
2448 }
2449
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002450 if (sample_type & PERF_SAMPLE_ADDR) {
2451 header.type |= PERF_SAMPLE_ADDR;
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002452 header.size += sizeof(u64);
2453 }
2454
Peter Zijlstraac4bcf82009-06-05 14:44:52 +02002455 if (sample_type & PERF_SAMPLE_ID) {
2456 header.type |= PERF_SAMPLE_ID;
Peter Zijlstraa85f61a2009-05-08 18:52:23 +02002457 header.size += sizeof(u64);
2458 }
2459
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002460 if (sample_type & PERF_SAMPLE_CPU) {
2461 header.type |= PERF_SAMPLE_CPU;
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002462 header.size += sizeof(cpu_entry);
2463
2464 cpu_entry.cpu = raw_smp_processor_id();
2465 }
2466
Peter Zijlstra689802b2009-06-05 15:05:43 +02002467 if (sample_type & PERF_SAMPLE_PERIOD) {
2468 header.type |= PERF_SAMPLE_PERIOD;
2469 header.size += sizeof(u64);
2470 }
2471
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002472 if (sample_type & PERF_SAMPLE_GROUP) {
2473 header.type |= PERF_SAMPLE_GROUP;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002474 header.size += sizeof(u64) +
2475 counter->nr_siblings * sizeof(group_entry);
2476 }
2477
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002478 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
Peter Zijlstradf1a1322009-06-10 21:02:22 +02002479 callchain = perf_callchain(data->regs);
Peter Zijlstra394ee072009-03-30 19:07:14 +02002480
2481 if (callchain) {
Peter Zijlstra9c03d882009-04-06 11:45:00 +02002482 callchain_size = (1 + callchain->nr) * sizeof(u64);
Peter Zijlstra394ee072009-03-30 19:07:14 +02002483
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002484 header.type |= PERF_SAMPLE_CALLCHAIN;
Peter Zijlstra394ee072009-03-30 19:07:14 +02002485 header.size += callchain_size;
2486 }
2487 }
2488
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002489 ret = perf_output_begin(&handle, counter, header.size, nmi, 1);
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002490 if (ret)
2491 return;
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002492
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002493 perf_output_put(&handle, header);
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002494
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002495 if (sample_type & PERF_SAMPLE_IP)
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002496 perf_output_put(&handle, ip);
2497
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002498 if (sample_type & PERF_SAMPLE_TID)
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002499 perf_output_put(&handle, tid_entry);
2500
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002501 if (sample_type & PERF_SAMPLE_TIME)
Peter Zijlstra4d855452009-04-08 15:01:32 +02002502 perf_output_put(&handle, time);
2503
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002504 if (sample_type & PERF_SAMPLE_ADDR)
Peter Zijlstradf1a1322009-06-10 21:02:22 +02002505 perf_output_put(&handle, data->addr);
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002506
Peter Zijlstraac4bcf82009-06-05 14:44:52 +02002507 if (sample_type & PERF_SAMPLE_ID)
2508 perf_output_put(&handle, counter->id);
Peter Zijlstraa85f61a2009-05-08 18:52:23 +02002509
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002510 if (sample_type & PERF_SAMPLE_CPU)
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002511 perf_output_put(&handle, cpu_entry);
2512
Peter Zijlstra689802b2009-06-05 15:05:43 +02002513 if (sample_type & PERF_SAMPLE_PERIOD)
Peter Zijlstra9e350de2009-06-10 21:34:59 +02002514 perf_output_put(&handle, data->period);
Peter Zijlstra689802b2009-06-05 15:05:43 +02002515
Peter Zijlstra2023b352009-05-05 17:50:26 +02002516 /*
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002517 * XXX PERF_SAMPLE_GROUP vs inherited counters seems difficult.
Peter Zijlstra2023b352009-05-05 17:50:26 +02002518 */
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002519 if (sample_type & PERF_SAMPLE_GROUP) {
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002520 struct perf_counter *leader, *sub;
2521 u64 nr = counter->nr_siblings;
2522
2523 perf_output_put(&handle, nr);
2524
2525 leader = counter->group_leader;
2526 list_for_each_entry(sub, &leader->sibling_list, list_entry) {
2527 if (sub != counter)
Robert Richter4aeb0b42009-04-29 12:47:03 +02002528 sub->pmu->read(sub);
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002529
Peter Zijlstra8e5799b2009-06-02 15:08:15 +02002530 group_entry.id = sub->id;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002531 group_entry.counter = atomic64_read(&sub->count);
2532
2533 perf_output_put(&handle, group_entry);
2534 }
2535 }
2536
Peter Zijlstra394ee072009-03-30 19:07:14 +02002537 if (callchain)
2538 perf_output_copy(&handle, callchain, callchain_size);
2539
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002540 perf_output_end(&handle);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002541}
2542
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002543/*
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002544 * fork tracking
2545 */
2546
2547struct perf_fork_event {
2548 struct task_struct *task;
2549
2550 struct {
2551 struct perf_event_header header;
2552
2553 u32 pid;
2554 u32 ppid;
2555 } event;
2556};
2557
2558static void perf_counter_fork_output(struct perf_counter *counter,
2559 struct perf_fork_event *fork_event)
2560{
2561 struct perf_output_handle handle;
2562 int size = fork_event->event.header.size;
2563 struct task_struct *task = fork_event->task;
2564 int ret = perf_output_begin(&handle, counter, size, 0, 0);
2565
2566 if (ret)
2567 return;
2568
2569 fork_event->event.pid = perf_counter_pid(counter, task);
2570 fork_event->event.ppid = perf_counter_pid(counter, task->real_parent);
2571
2572 perf_output_put(&handle, fork_event->event);
2573 perf_output_end(&handle);
2574}
2575
2576static int perf_counter_fork_match(struct perf_counter *counter)
2577{
Peter Zijlstrad99e9442009-06-04 17:08:58 +02002578 if (counter->attr.comm || counter->attr.mmap)
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002579 return 1;
2580
2581 return 0;
2582}
2583
2584static void perf_counter_fork_ctx(struct perf_counter_context *ctx,
2585 struct perf_fork_event *fork_event)
2586{
2587 struct perf_counter *counter;
2588
2589 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2590 return;
2591
2592 rcu_read_lock();
2593 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2594 if (perf_counter_fork_match(counter))
2595 perf_counter_fork_output(counter, fork_event);
2596 }
2597 rcu_read_unlock();
2598}
2599
2600static void perf_counter_fork_event(struct perf_fork_event *fork_event)
2601{
2602 struct perf_cpu_context *cpuctx;
2603 struct perf_counter_context *ctx;
2604
2605 cpuctx = &get_cpu_var(perf_cpu_context);
2606 perf_counter_fork_ctx(&cpuctx->ctx, fork_event);
2607 put_cpu_var(perf_cpu_context);
2608
2609 rcu_read_lock();
2610 /*
2611 * doesn't really matter which of the child contexts the
2612 * events ends up in.
2613 */
2614 ctx = rcu_dereference(current->perf_counter_ctxp);
2615 if (ctx)
2616 perf_counter_fork_ctx(ctx, fork_event);
2617 rcu_read_unlock();
2618}
2619
2620void perf_counter_fork(struct task_struct *task)
2621{
2622 struct perf_fork_event fork_event;
2623
2624 if (!atomic_read(&nr_comm_counters) &&
Peter Zijlstrad99e9442009-06-04 17:08:58 +02002625 !atomic_read(&nr_mmap_counters))
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002626 return;
2627
2628 fork_event = (struct perf_fork_event){
2629 .task = task,
2630 .event = {
2631 .header = {
2632 .type = PERF_EVENT_FORK,
2633 .size = sizeof(fork_event.event),
2634 },
2635 },
2636 };
2637
2638 perf_counter_fork_event(&fork_event);
2639}
2640
2641/*
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002642 * comm tracking
2643 */
2644
2645struct perf_comm_event {
Ingo Molnar22a4f652009-06-01 10:13:37 +02002646 struct task_struct *task;
2647 char *comm;
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002648 int comm_size;
2649
2650 struct {
2651 struct perf_event_header header;
2652
2653 u32 pid;
2654 u32 tid;
2655 } event;
2656};
2657
2658static void perf_counter_comm_output(struct perf_counter *counter,
2659 struct perf_comm_event *comm_event)
2660{
2661 struct perf_output_handle handle;
2662 int size = comm_event->event.header.size;
2663 int ret = perf_output_begin(&handle, counter, size, 0, 0);
2664
2665 if (ret)
2666 return;
2667
Peter Zijlstra709e50c2009-06-02 14:13:15 +02002668 comm_event->event.pid = perf_counter_pid(counter, comm_event->task);
2669 comm_event->event.tid = perf_counter_tid(counter, comm_event->task);
2670
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002671 perf_output_put(&handle, comm_event->event);
2672 perf_output_copy(&handle, comm_event->comm,
2673 comm_event->comm_size);
2674 perf_output_end(&handle);
2675}
2676
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002677static int perf_counter_comm_match(struct perf_counter *counter)
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002678{
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002679 if (counter->attr.comm)
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002680 return 1;
2681
2682 return 0;
2683}
2684
2685static void perf_counter_comm_ctx(struct perf_counter_context *ctx,
2686 struct perf_comm_event *comm_event)
2687{
2688 struct perf_counter *counter;
2689
2690 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2691 return;
2692
2693 rcu_read_lock();
2694 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002695 if (perf_counter_comm_match(counter))
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002696 perf_counter_comm_output(counter, comm_event);
2697 }
2698 rcu_read_unlock();
2699}
2700
2701static void perf_counter_comm_event(struct perf_comm_event *comm_event)
2702{
2703 struct perf_cpu_context *cpuctx;
Peter Zijlstra665c2142009-05-29 14:51:57 +02002704 struct perf_counter_context *ctx;
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002705 unsigned int size;
2706 char *comm = comm_event->task->comm;
2707
Ingo Molnar888fcee2009-04-09 09:48:22 +02002708 size = ALIGN(strlen(comm)+1, sizeof(u64));
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002709
2710 comm_event->comm = comm;
2711 comm_event->comm_size = size;
2712
2713 comm_event->event.header.size = sizeof(comm_event->event) + size;
2714
2715 cpuctx = &get_cpu_var(perf_cpu_context);
2716 perf_counter_comm_ctx(&cpuctx->ctx, comm_event);
2717 put_cpu_var(perf_cpu_context);
Peter Zijlstra665c2142009-05-29 14:51:57 +02002718
2719 rcu_read_lock();
2720 /*
2721 * doesn't really matter which of the child contexts the
2722 * events ends up in.
2723 */
2724 ctx = rcu_dereference(current->perf_counter_ctxp);
2725 if (ctx)
2726 perf_counter_comm_ctx(ctx, comm_event);
2727 rcu_read_unlock();
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002728}
2729
2730void perf_counter_comm(struct task_struct *task)
2731{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002732 struct perf_comm_event comm_event;
2733
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002734 if (!atomic_read(&nr_comm_counters))
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002735 return;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10002736
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002737 comm_event = (struct perf_comm_event){
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002738 .task = task,
2739 .event = {
2740 .header = { .type = PERF_EVENT_COMM, },
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002741 },
2742 };
2743
2744 perf_counter_comm_event(&comm_event);
2745}
2746
2747/*
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002748 * mmap tracking
2749 */
2750
2751struct perf_mmap_event {
Peter Zijlstra089dd792009-06-05 14:04:55 +02002752 struct vm_area_struct *vma;
2753
2754 const char *file_name;
2755 int file_size;
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002756
2757 struct {
2758 struct perf_event_header header;
2759
2760 u32 pid;
2761 u32 tid;
2762 u64 start;
2763 u64 len;
2764 u64 pgoff;
2765 } event;
2766};
2767
2768static void perf_counter_mmap_output(struct perf_counter *counter,
2769 struct perf_mmap_event *mmap_event)
2770{
2771 struct perf_output_handle handle;
2772 int size = mmap_event->event.header.size;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002773 int ret = perf_output_begin(&handle, counter, size, 0, 0);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002774
2775 if (ret)
2776 return;
2777
Peter Zijlstra709e50c2009-06-02 14:13:15 +02002778 mmap_event->event.pid = perf_counter_pid(counter, current);
2779 mmap_event->event.tid = perf_counter_tid(counter, current);
2780
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002781 perf_output_put(&handle, mmap_event->event);
2782 perf_output_copy(&handle, mmap_event->file_name,
2783 mmap_event->file_size);
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002784 perf_output_end(&handle);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002785}
2786
2787static int perf_counter_mmap_match(struct perf_counter *counter,
2788 struct perf_mmap_event *mmap_event)
2789{
Peter Zijlstrad99e9442009-06-04 17:08:58 +02002790 if (counter->attr.mmap)
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002791 return 1;
2792
2793 return 0;
2794}
2795
2796static void perf_counter_mmap_ctx(struct perf_counter_context *ctx,
2797 struct perf_mmap_event *mmap_event)
2798{
2799 struct perf_counter *counter;
2800
2801 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2802 return;
2803
2804 rcu_read_lock();
2805 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2806 if (perf_counter_mmap_match(counter, mmap_event))
2807 perf_counter_mmap_output(counter, mmap_event);
2808 }
2809 rcu_read_unlock();
2810}
2811
2812static void perf_counter_mmap_event(struct perf_mmap_event *mmap_event)
2813{
2814 struct perf_cpu_context *cpuctx;
Peter Zijlstra665c2142009-05-29 14:51:57 +02002815 struct perf_counter_context *ctx;
Peter Zijlstra089dd792009-06-05 14:04:55 +02002816 struct vm_area_struct *vma = mmap_event->vma;
2817 struct file *file = vma->vm_file;
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002818 unsigned int size;
2819 char tmp[16];
2820 char *buf = NULL;
Peter Zijlstra089dd792009-06-05 14:04:55 +02002821 const char *name;
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002822
2823 if (file) {
2824 buf = kzalloc(PATH_MAX, GFP_KERNEL);
2825 if (!buf) {
2826 name = strncpy(tmp, "//enomem", sizeof(tmp));
2827 goto got_name;
2828 }
Peter Zijlstrad3d21c42009-04-09 10:53:46 +02002829 name = d_path(&file->f_path, buf, PATH_MAX);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002830 if (IS_ERR(name)) {
2831 name = strncpy(tmp, "//toolong", sizeof(tmp));
2832 goto got_name;
2833 }
2834 } else {
Peter Zijlstra089dd792009-06-05 14:04:55 +02002835 name = arch_vma_name(mmap_event->vma);
2836 if (name)
2837 goto got_name;
2838
2839 if (!vma->vm_mm) {
2840 name = strncpy(tmp, "[vdso]", sizeof(tmp));
2841 goto got_name;
2842 }
2843
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002844 name = strncpy(tmp, "//anon", sizeof(tmp));
2845 goto got_name;
2846 }
2847
2848got_name:
Ingo Molnar888fcee2009-04-09 09:48:22 +02002849 size = ALIGN(strlen(name)+1, sizeof(u64));
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002850
2851 mmap_event->file_name = name;
2852 mmap_event->file_size = size;
2853
2854 mmap_event->event.header.size = sizeof(mmap_event->event) + size;
2855
2856 cpuctx = &get_cpu_var(perf_cpu_context);
2857 perf_counter_mmap_ctx(&cpuctx->ctx, mmap_event);
2858 put_cpu_var(perf_cpu_context);
2859
Peter Zijlstra665c2142009-05-29 14:51:57 +02002860 rcu_read_lock();
2861 /*
2862 * doesn't really matter which of the child contexts the
2863 * events ends up in.
2864 */
2865 ctx = rcu_dereference(current->perf_counter_ctxp);
2866 if (ctx)
2867 perf_counter_mmap_ctx(ctx, mmap_event);
2868 rcu_read_unlock();
2869
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002870 kfree(buf);
2871}
2872
Peter Zijlstra089dd792009-06-05 14:04:55 +02002873void __perf_counter_mmap(struct vm_area_struct *vma)
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002874{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002875 struct perf_mmap_event mmap_event;
2876
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002877 if (!atomic_read(&nr_mmap_counters))
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002878 return;
2879
2880 mmap_event = (struct perf_mmap_event){
Peter Zijlstra089dd792009-06-05 14:04:55 +02002881 .vma = vma,
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002882 .event = {
2883 .header = { .type = PERF_EVENT_MMAP, },
Peter Zijlstra089dd792009-06-05 14:04:55 +02002884 .start = vma->vm_start,
2885 .len = vma->vm_end - vma->vm_start,
2886 .pgoff = vma->vm_pgoff,
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002887 },
2888 };
2889
2890 perf_counter_mmap_event(&mmap_event);
2891}
2892
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002893/*
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002894 * Log sample_period changes so that analyzing tools can re-normalize the
Peter Zijlstrae220d2d2009-05-23 18:28:55 +02002895 * event flow.
Peter Zijlstra26b119b2009-05-20 12:21:20 +02002896 */
2897
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02002898struct freq_event {
2899 struct perf_event_header header;
2900 u64 time;
2901 u64 id;
2902 u64 period;
2903};
2904
Peter Zijlstra26b119b2009-05-20 12:21:20 +02002905static void perf_log_period(struct perf_counter *counter, u64 period)
2906{
2907 struct perf_output_handle handle;
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02002908 struct freq_event event;
Peter Zijlstra26b119b2009-05-20 12:21:20 +02002909 int ret;
2910
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02002911 if (counter->hw.sample_period == period)
2912 return;
2913
2914 if (counter->attr.sample_type & PERF_SAMPLE_PERIOD)
2915 return;
2916
2917 event = (struct freq_event) {
Peter Zijlstra26b119b2009-05-20 12:21:20 +02002918 .header = {
2919 .type = PERF_EVENT_PERIOD,
2920 .misc = 0,
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02002921 .size = sizeof(event),
Peter Zijlstra26b119b2009-05-20 12:21:20 +02002922 },
2923 .time = sched_clock(),
Peter Zijlstra689802b2009-06-05 15:05:43 +02002924 .id = counter->id,
Peter Zijlstra26b119b2009-05-20 12:21:20 +02002925 .period = period,
2926 };
2927
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02002928 ret = perf_output_begin(&handle, counter, sizeof(event), 1, 0);
Peter Zijlstra26b119b2009-05-20 12:21:20 +02002929 if (ret)
2930 return;
2931
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02002932 perf_output_put(&handle, event);
Peter Zijlstra26b119b2009-05-20 12:21:20 +02002933 perf_output_end(&handle);
2934}
2935
2936/*
Peter Zijlstraa78ac322009-05-25 17:39:05 +02002937 * IRQ throttle logging
2938 */
2939
2940static void perf_log_throttle(struct perf_counter *counter, int enable)
2941{
2942 struct perf_output_handle handle;
2943 int ret;
2944
2945 struct {
2946 struct perf_event_header header;
2947 u64 time;
2948 } throttle_event = {
2949 .header = {
2950 .type = PERF_EVENT_THROTTLE + 1,
2951 .misc = 0,
2952 .size = sizeof(throttle_event),
2953 },
2954 .time = sched_clock(),
2955 };
2956
Ingo Molnar0127c3e2009-05-25 22:03:26 +02002957 ret = perf_output_begin(&handle, counter, sizeof(throttle_event), 1, 0);
Peter Zijlstraa78ac322009-05-25 17:39:05 +02002958 if (ret)
2959 return;
2960
2961 perf_output_put(&handle, throttle_event);
2962 perf_output_end(&handle);
2963}
2964
2965/*
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002966 * Generic counter overflow handling.
2967 */
2968
Peter Zijlstradf1a1322009-06-10 21:02:22 +02002969int perf_counter_overflow(struct perf_counter *counter, int nmi,
2970 struct perf_sample_data *data)
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002971{
Peter Zijlstra79f14642009-04-06 11:45:07 +02002972 int events = atomic_read(&counter->event_limit);
Peter Zijlstraa78ac322009-05-25 17:39:05 +02002973 int throttle = counter->pmu->unthrottle != NULL;
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02002974 struct hw_perf_counter *hwc = &counter->hw;
Peter Zijlstra79f14642009-04-06 11:45:07 +02002975 int ret = 0;
2976
Peter Zijlstraa78ac322009-05-25 17:39:05 +02002977 if (!throttle) {
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02002978 hwc->interrupts++;
Ingo Molnar128f0482009-06-03 22:19:36 +02002979 } else {
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02002980 if (hwc->interrupts != MAX_INTERRUPTS) {
2981 hwc->interrupts++;
2982 if (HZ * hwc->interrupts > (u64)sysctl_perf_counter_limit) {
2983 hwc->interrupts = MAX_INTERRUPTS;
Ingo Molnar128f0482009-06-03 22:19:36 +02002984 perf_log_throttle(counter, 0);
2985 ret = 1;
2986 }
2987 } else {
2988 /*
2989 * Keep re-disabling counters even though on the previous
2990 * pass we disabled it - just in case we raced with a
2991 * sched-in and the counter got enabled again:
2992 */
Peter Zijlstraa78ac322009-05-25 17:39:05 +02002993 ret = 1;
2994 }
2995 }
Peter Zijlstra60db5e02009-05-15 15:19:28 +02002996
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02002997 if (counter->attr.freq) {
2998 u64 now = sched_clock();
2999 s64 delta = now - hwc->freq_stamp;
3000
3001 hwc->freq_stamp = now;
3002
3003 if (delta > 0 && delta < TICK_NSEC)
3004 perf_adjust_period(counter, NSEC_PER_SEC / (int)delta);
3005 }
3006
Peter Zijlstra2023b352009-05-05 17:50:26 +02003007 /*
3008 * XXX event_limit might not quite work as expected on inherited
3009 * counters
3010 */
3011
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02003012 counter->pending_kill = POLL_IN;
Peter Zijlstra79f14642009-04-06 11:45:07 +02003013 if (events && atomic_dec_and_test(&counter->event_limit)) {
3014 ret = 1;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02003015 counter->pending_kill = POLL_HUP;
Peter Zijlstra79f14642009-04-06 11:45:07 +02003016 if (nmi) {
3017 counter->pending_disable = 1;
3018 perf_pending_queue(&counter->pending,
3019 perf_pending_counter);
3020 } else
3021 perf_counter_disable(counter);
3022 }
3023
Peter Zijlstradf1a1322009-06-10 21:02:22 +02003024 perf_counter_output(counter, nmi, data);
Peter Zijlstra79f14642009-04-06 11:45:07 +02003025 return ret;
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02003026}
3027
3028/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003029 * Generic software counter infrastructure
3030 */
3031
3032static void perf_swcounter_update(struct perf_counter *counter)
3033{
3034 struct hw_perf_counter *hwc = &counter->hw;
3035 u64 prev, now;
3036 s64 delta;
3037
3038again:
3039 prev = atomic64_read(&hwc->prev_count);
3040 now = atomic64_read(&hwc->count);
3041 if (atomic64_cmpxchg(&hwc->prev_count, prev, now) != prev)
3042 goto again;
3043
3044 delta = now - prev;
3045
3046 atomic64_add(delta, &counter->count);
3047 atomic64_sub(delta, &hwc->period_left);
3048}
3049
3050static void perf_swcounter_set_period(struct perf_counter *counter)
3051{
3052 struct hw_perf_counter *hwc = &counter->hw;
3053 s64 left = atomic64_read(&hwc->period_left);
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003054 s64 period = hwc->sample_period;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003055
3056 if (unlikely(left <= -period)) {
3057 left = period;
3058 atomic64_set(&hwc->period_left, left);
Peter Zijlstra9e350de2009-06-10 21:34:59 +02003059 hwc->last_period = period;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003060 }
3061
3062 if (unlikely(left <= 0)) {
3063 left += period;
3064 atomic64_add(period, &hwc->period_left);
Peter Zijlstra9e350de2009-06-10 21:34:59 +02003065 hwc->last_period = period;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003066 }
3067
3068 atomic64_set(&hwc->prev_count, -left);
3069 atomic64_set(&hwc->count, -left);
3070}
3071
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003072static enum hrtimer_restart perf_swcounter_hrtimer(struct hrtimer *hrtimer)
3073{
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02003074 enum hrtimer_restart ret = HRTIMER_RESTART;
Peter Zijlstradf1a1322009-06-10 21:02:22 +02003075 struct perf_sample_data data;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003076 struct perf_counter *counter;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003077 u64 period;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003078
3079 counter = container_of(hrtimer, struct perf_counter, hw.hrtimer);
Robert Richter4aeb0b42009-04-29 12:47:03 +02003080 counter->pmu->read(counter);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003081
Peter Zijlstradf1a1322009-06-10 21:02:22 +02003082 data.addr = 0;
3083 data.regs = get_irq_regs();
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003084 /*
3085 * In case we exclude kernel IPs or are somehow not in interrupt
3086 * context, provide the next best thing, the user IP.
3087 */
Peter Zijlstradf1a1322009-06-10 21:02:22 +02003088 if ((counter->attr.exclude_kernel || !data.regs) &&
Peter Zijlstra0d486962009-06-02 19:22:16 +02003089 !counter->attr.exclude_user)
Peter Zijlstradf1a1322009-06-10 21:02:22 +02003090 data.regs = task_pt_regs(current);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003091
Peter Zijlstradf1a1322009-06-10 21:02:22 +02003092 if (data.regs) {
3093 if (perf_counter_overflow(counter, 0, &data))
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02003094 ret = HRTIMER_NORESTART;
3095 }
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003096
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003097 period = max_t(u64, 10000, counter->hw.sample_period);
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003098 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003099
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02003100 return ret;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003101}
3102
3103static void perf_swcounter_overflow(struct perf_counter *counter,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003104 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003105{
Peter Zijlstradf1a1322009-06-10 21:02:22 +02003106 struct perf_sample_data data = {
Peter Zijlstra9e350de2009-06-10 21:34:59 +02003107 .regs = regs,
3108 .addr = addr,
3109 .period = counter->hw.last_period,
Peter Zijlstradf1a1322009-06-10 21:02:22 +02003110 };
3111
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003112 perf_swcounter_update(counter);
3113 perf_swcounter_set_period(counter);
Peter Zijlstradf1a1322009-06-10 21:02:22 +02003114 if (perf_counter_overflow(counter, nmi, &data))
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02003115 /* soft-disable the counter */
3116 ;
3117
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003118}
3119
Paul Mackerras880ca152009-06-01 17:49:14 +10003120static int perf_swcounter_is_counting(struct perf_counter *counter)
3121{
3122 struct perf_counter_context *ctx;
3123 unsigned long flags;
3124 int count;
3125
3126 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
3127 return 1;
3128
3129 if (counter->state != PERF_COUNTER_STATE_INACTIVE)
3130 return 0;
3131
3132 /*
3133 * If the counter is inactive, it could be just because
3134 * its task is scheduled out, or because it's in a group
3135 * which could not go on the PMU. We want to count in
3136 * the first case but not the second. If the context is
3137 * currently active then an inactive software counter must
3138 * be the second case. If it's not currently active then
3139 * we need to know whether the counter was active when the
3140 * context was last active, which we can determine by
3141 * comparing counter->tstamp_stopped with ctx->time.
3142 *
3143 * We are within an RCU read-side critical section,
3144 * which protects the existence of *ctx.
3145 */
3146 ctx = counter->ctx;
3147 spin_lock_irqsave(&ctx->lock, flags);
3148 count = 1;
3149 /* Re-check state now we have the lock */
3150 if (counter->state < PERF_COUNTER_STATE_INACTIVE ||
3151 counter->ctx->is_active ||
3152 counter->tstamp_stopped < ctx->time)
3153 count = 0;
3154 spin_unlock_irqrestore(&ctx->lock, flags);
3155 return count;
3156}
3157
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003158static int perf_swcounter_match(struct perf_counter *counter,
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003159 enum perf_event_types type,
3160 u32 event, struct pt_regs *regs)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003161{
Paul Mackerras880ca152009-06-01 17:49:14 +10003162 if (!perf_swcounter_is_counting(counter))
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003163 return 0;
3164
Ingo Molnara21ca2c2009-06-06 09:58:57 +02003165 if (counter->attr.type != type)
3166 return 0;
3167 if (counter->attr.config != event)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003168 return 0;
3169
Paul Mackerras3f731ca2009-06-01 17:52:30 +10003170 if (regs) {
Peter Zijlstra0d486962009-06-02 19:22:16 +02003171 if (counter->attr.exclude_user && user_mode(regs))
Paul Mackerras3f731ca2009-06-01 17:52:30 +10003172 return 0;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003173
Peter Zijlstra0d486962009-06-02 19:22:16 +02003174 if (counter->attr.exclude_kernel && !user_mode(regs))
Paul Mackerras3f731ca2009-06-01 17:52:30 +10003175 return 0;
3176 }
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003177
3178 return 1;
3179}
3180
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003181static void perf_swcounter_add(struct perf_counter *counter, u64 nr,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003182 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003183{
3184 int neg = atomic64_add_negative(nr, &counter->hw.count);
Ingo Molnar22a4f652009-06-01 10:13:37 +02003185
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003186 if (counter->hw.sample_period && !neg && regs)
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003187 perf_swcounter_overflow(counter, nmi, regs, addr);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003188}
3189
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003190static void perf_swcounter_ctx_event(struct perf_counter_context *ctx,
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003191 enum perf_event_types type, u32 event,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003192 u64 nr, int nmi, struct pt_regs *regs,
3193 u64 addr)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003194{
3195 struct perf_counter *counter;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003196
Peter Zijlstra01ef09d2009-03-19 20:26:11 +01003197 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003198 return;
3199
Peter Zijlstra592903c2009-03-13 12:21:36 +01003200 rcu_read_lock();
3201 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003202 if (perf_swcounter_match(counter, type, event, regs))
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003203 perf_swcounter_add(counter, nr, nmi, regs, addr);
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003204 }
Peter Zijlstra592903c2009-03-13 12:21:36 +01003205 rcu_read_unlock();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003206}
3207
Peter Zijlstra96f6d442009-03-23 18:22:07 +01003208static int *perf_swcounter_recursion_context(struct perf_cpu_context *cpuctx)
3209{
3210 if (in_nmi())
3211 return &cpuctx->recursion[3];
3212
3213 if (in_irq())
3214 return &cpuctx->recursion[2];
3215
3216 if (in_softirq())
3217 return &cpuctx->recursion[1];
3218
3219 return &cpuctx->recursion[0];
3220}
3221
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003222static void __perf_swcounter_event(enum perf_event_types type, u32 event,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003223 u64 nr, int nmi, struct pt_regs *regs,
3224 u64 addr)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003225{
3226 struct perf_cpu_context *cpuctx = &get_cpu_var(perf_cpu_context);
Peter Zijlstra96f6d442009-03-23 18:22:07 +01003227 int *recursion = perf_swcounter_recursion_context(cpuctx);
Peter Zijlstra665c2142009-05-29 14:51:57 +02003228 struct perf_counter_context *ctx;
Peter Zijlstra96f6d442009-03-23 18:22:07 +01003229
3230 if (*recursion)
3231 goto out;
3232
3233 (*recursion)++;
3234 barrier();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003235
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003236 perf_swcounter_ctx_event(&cpuctx->ctx, type, event,
3237 nr, nmi, regs, addr);
Peter Zijlstra665c2142009-05-29 14:51:57 +02003238 rcu_read_lock();
3239 /*
3240 * doesn't really matter which of the child contexts the
3241 * events ends up in.
3242 */
3243 ctx = rcu_dereference(current->perf_counter_ctxp);
3244 if (ctx)
3245 perf_swcounter_ctx_event(ctx, type, event, nr, nmi, regs, addr);
3246 rcu_read_unlock();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003247
Peter Zijlstra96f6d442009-03-23 18:22:07 +01003248 barrier();
3249 (*recursion)--;
3250
3251out:
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003252 put_cpu_var(perf_cpu_context);
3253}
3254
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003255void
3256perf_swcounter_event(u32 event, u64 nr, int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003257{
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003258 __perf_swcounter_event(PERF_TYPE_SOFTWARE, event, nr, nmi, regs, addr);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003259}
3260
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003261static void perf_swcounter_read(struct perf_counter *counter)
3262{
3263 perf_swcounter_update(counter);
3264}
3265
3266static int perf_swcounter_enable(struct perf_counter *counter)
3267{
3268 perf_swcounter_set_period(counter);
3269 return 0;
3270}
3271
3272static void perf_swcounter_disable(struct perf_counter *counter)
3273{
3274 perf_swcounter_update(counter);
3275}
3276
Robert Richter4aeb0b42009-04-29 12:47:03 +02003277static const struct pmu perf_ops_generic = {
Peter Zijlstraac17dc82009-03-13 12:21:34 +01003278 .enable = perf_swcounter_enable,
3279 .disable = perf_swcounter_disable,
3280 .read = perf_swcounter_read,
3281};
3282
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003283/*
3284 * Software counter: cpu wall time clock
3285 */
3286
Paul Mackerras9abf8a02009-01-09 16:26:43 +11003287static void cpu_clock_perf_counter_update(struct perf_counter *counter)
3288{
3289 int cpu = raw_smp_processor_id();
3290 s64 prev;
3291 u64 now;
3292
3293 now = cpu_clock(cpu);
3294 prev = atomic64_read(&counter->hw.prev_count);
3295 atomic64_set(&counter->hw.prev_count, now);
3296 atomic64_add(now - prev, &counter->count);
3297}
3298
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003299static int cpu_clock_perf_counter_enable(struct perf_counter *counter)
3300{
3301 struct hw_perf_counter *hwc = &counter->hw;
3302 int cpu = raw_smp_processor_id();
3303
3304 atomic64_set(&hwc->prev_count, cpu_clock(cpu));
Peter Zijlstra039fc912009-03-13 16:43:47 +01003305 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
3306 hwc->hrtimer.function = perf_swcounter_hrtimer;
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003307 if (hwc->sample_period) {
3308 u64 period = max_t(u64, 10000, hwc->sample_period);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003309 __hrtimer_start_range_ns(&hwc->hrtimer,
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003310 ns_to_ktime(period), 0,
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003311 HRTIMER_MODE_REL, 0);
3312 }
3313
3314 return 0;
3315}
3316
Ingo Molnar5c92d122008-12-11 13:21:10 +01003317static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
3318{
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003319 if (counter->hw.sample_period)
Peter Zijlstrab986d7e2009-05-20 12:21:21 +02003320 hrtimer_cancel(&counter->hw.hrtimer);
Paul Mackerras9abf8a02009-01-09 16:26:43 +11003321 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01003322}
3323
3324static void cpu_clock_perf_counter_read(struct perf_counter *counter)
3325{
Paul Mackerras9abf8a02009-01-09 16:26:43 +11003326 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01003327}
3328
Robert Richter4aeb0b42009-04-29 12:47:03 +02003329static const struct pmu perf_ops_cpu_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +01003330 .enable = cpu_clock_perf_counter_enable,
3331 .disable = cpu_clock_perf_counter_disable,
3332 .read = cpu_clock_perf_counter_read,
Ingo Molnar5c92d122008-12-11 13:21:10 +01003333};
3334
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01003335/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003336 * Software counter: task time clock
3337 */
3338
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02003339static void task_clock_perf_counter_update(struct perf_counter *counter, u64 now)
Ingo Molnarbae43c92008-12-11 14:03:20 +01003340{
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02003341 u64 prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003342 s64 delta;
Ingo Molnarbae43c92008-12-11 14:03:20 +01003343
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02003344 prev = atomic64_xchg(&counter->hw.prev_count, now);
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003345 delta = now - prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003346 atomic64_add(delta, &counter->count);
Ingo Molnarbae43c92008-12-11 14:03:20 +01003347}
3348
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01003349static int task_clock_perf_counter_enable(struct perf_counter *counter)
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003350{
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003351 struct hw_perf_counter *hwc = &counter->hw;
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02003352 u64 now;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003353
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02003354 now = counter->ctx->time;
3355
3356 atomic64_set(&hwc->prev_count, now);
Peter Zijlstra039fc912009-03-13 16:43:47 +01003357 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
3358 hwc->hrtimer.function = perf_swcounter_hrtimer;
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003359 if (hwc->sample_period) {
3360 u64 period = max_t(u64, 10000, hwc->sample_period);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003361 __hrtimer_start_range_ns(&hwc->hrtimer,
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003362 ns_to_ktime(period), 0,
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003363 HRTIMER_MODE_REL, 0);
3364 }
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01003365
3366 return 0;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003367}
3368
3369static void task_clock_perf_counter_disable(struct perf_counter *counter)
3370{
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003371 if (counter->hw.sample_period)
Peter Zijlstrab986d7e2009-05-20 12:21:21 +02003372 hrtimer_cancel(&counter->hw.hrtimer);
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02003373 task_clock_perf_counter_update(counter, counter->ctx->time);
3374
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003375}
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01003376
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003377static void task_clock_perf_counter_read(struct perf_counter *counter)
3378{
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02003379 u64 time;
3380
3381 if (!in_nmi()) {
3382 update_context_time(counter->ctx);
3383 time = counter->ctx->time;
3384 } else {
3385 u64 now = perf_clock();
3386 u64 delta = now - counter->ctx->timestamp;
3387 time = counter->ctx->time + delta;
3388 }
3389
3390 task_clock_perf_counter_update(counter, time);
Ingo Molnarbae43c92008-12-11 14:03:20 +01003391}
3392
Robert Richter4aeb0b42009-04-29 12:47:03 +02003393static const struct pmu perf_ops_task_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +01003394 .enable = task_clock_perf_counter_enable,
3395 .disable = task_clock_perf_counter_disable,
3396 .read = task_clock_perf_counter_read,
Ingo Molnarbae43c92008-12-11 14:03:20 +01003397};
3398
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003399/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003400 * Software counter: cpu migrations
3401 */
Paul Mackerras3f731ca2009-06-01 17:52:30 +10003402void perf_counter_task_migration(struct task_struct *task, int cpu)
Ingo Molnar6c594c22008-12-14 12:34:15 +01003403{
Paul Mackerras3f731ca2009-06-01 17:52:30 +10003404 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
3405 struct perf_counter_context *ctx;
Paul Mackerras23a185c2009-02-09 22:42:47 +11003406
Paul Mackerras3f731ca2009-06-01 17:52:30 +10003407 perf_swcounter_ctx_event(&cpuctx->ctx, PERF_TYPE_SOFTWARE,
3408 PERF_COUNT_CPU_MIGRATIONS,
3409 1, 1, NULL, 0);
3410
3411 ctx = perf_pin_task_context(task);
3412 if (ctx) {
3413 perf_swcounter_ctx_event(ctx, PERF_TYPE_SOFTWARE,
3414 PERF_COUNT_CPU_MIGRATIONS,
3415 1, 1, NULL, 0);
3416 perf_unpin_context(ctx);
3417 }
Ingo Molnar6c594c22008-12-14 12:34:15 +01003418}
3419
Peter Zijlstrae077df42009-03-19 20:26:17 +01003420#ifdef CONFIG_EVENT_PROFILE
3421void perf_tpcounter_event(int event_id)
3422{
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003423 struct pt_regs *regs = get_irq_regs();
3424
3425 if (!regs)
3426 regs = task_pt_regs(current);
3427
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003428 __perf_swcounter_event(PERF_TYPE_TRACEPOINT, event_id, 1, 1, regs, 0);
Peter Zijlstrae077df42009-03-19 20:26:17 +01003429}
Steven Whitehouseff7b1b42009-04-15 16:55:05 +01003430EXPORT_SYMBOL_GPL(perf_tpcounter_event);
Peter Zijlstrae077df42009-03-19 20:26:17 +01003431
3432extern int ftrace_profile_enable(int);
3433extern void ftrace_profile_disable(int);
3434
3435static void tp_perf_counter_destroy(struct perf_counter *counter)
3436{
Peter Zijlstra0d486962009-06-02 19:22:16 +02003437 ftrace_profile_disable(perf_event_id(&counter->attr));
Peter Zijlstrae077df42009-03-19 20:26:17 +01003438}
3439
Robert Richter4aeb0b42009-04-29 12:47:03 +02003440static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
Peter Zijlstrae077df42009-03-19 20:26:17 +01003441{
Peter Zijlstra0d486962009-06-02 19:22:16 +02003442 int event_id = perf_event_id(&counter->attr);
Peter Zijlstrae077df42009-03-19 20:26:17 +01003443 int ret;
3444
3445 ret = ftrace_profile_enable(event_id);
3446 if (ret)
3447 return NULL;
3448
3449 counter->destroy = tp_perf_counter_destroy;
3450
3451 return &perf_ops_generic;
3452}
3453#else
Robert Richter4aeb0b42009-04-29 12:47:03 +02003454static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
Peter Zijlstrae077df42009-03-19 20:26:17 +01003455{
3456 return NULL;
3457}
3458#endif
3459
Robert Richter4aeb0b42009-04-29 12:47:03 +02003460static const struct pmu *sw_perf_counter_init(struct perf_counter *counter)
Ingo Molnar5c92d122008-12-11 13:21:10 +01003461{
Robert Richter4aeb0b42009-04-29 12:47:03 +02003462 const struct pmu *pmu = NULL;
Ingo Molnar5c92d122008-12-11 13:21:10 +01003463
Paul Mackerras0475f9e2009-02-11 14:35:35 +11003464 /*
3465 * Software counters (currently) can't in general distinguish
3466 * between user, kernel and hypervisor events.
3467 * However, context switches and cpu migrations are considered
3468 * to be kernel events, and page faults are never hypervisor
3469 * events.
3470 */
Ingo Molnara21ca2c2009-06-06 09:58:57 +02003471 switch (counter->attr.config) {
Ingo Molnar5c92d122008-12-11 13:21:10 +01003472 case PERF_COUNT_CPU_CLOCK:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003473 pmu = &perf_ops_cpu_clock;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003474
Ingo Molnar5c92d122008-12-11 13:21:10 +01003475 break;
Ingo Molnarbae43c92008-12-11 14:03:20 +01003476 case PERF_COUNT_TASK_CLOCK:
Paul Mackerras23a185c2009-02-09 22:42:47 +11003477 /*
3478 * If the user instantiates this as a per-cpu counter,
3479 * use the cpu_clock counter instead.
3480 */
3481 if (counter->ctx->task)
Robert Richter4aeb0b42009-04-29 12:47:03 +02003482 pmu = &perf_ops_task_clock;
Paul Mackerras23a185c2009-02-09 22:42:47 +11003483 else
Robert Richter4aeb0b42009-04-29 12:47:03 +02003484 pmu = &perf_ops_cpu_clock;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003485
Ingo Molnarbae43c92008-12-11 14:03:20 +01003486 break;
Ingo Molnare06c61a2008-12-14 14:44:31 +01003487 case PERF_COUNT_PAGE_FAULTS:
Peter Zijlstraac17dc82009-03-13 12:21:34 +01003488 case PERF_COUNT_PAGE_FAULTS_MIN:
3489 case PERF_COUNT_PAGE_FAULTS_MAJ:
Ingo Molnar5d6a27d2008-12-14 12:28:33 +01003490 case PERF_COUNT_CONTEXT_SWITCHES:
Ingo Molnar6c594c22008-12-14 12:34:15 +01003491 case PERF_COUNT_CPU_MIGRATIONS:
Paul Mackerras3f731ca2009-06-01 17:52:30 +10003492 pmu = &perf_ops_generic;
Ingo Molnar6c594c22008-12-14 12:34:15 +01003493 break;
Ingo Molnar5c92d122008-12-11 13:21:10 +01003494 }
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003495
Robert Richter4aeb0b42009-04-29 12:47:03 +02003496 return pmu;
Ingo Molnar5c92d122008-12-11 13:21:10 +01003497}
3498
Thomas Gleixner0793a612008-12-04 20:12:29 +01003499/*
3500 * Allocate and initialize a counter structure
3501 */
3502static struct perf_counter *
Peter Zijlstra0d486962009-06-02 19:22:16 +02003503perf_counter_alloc(struct perf_counter_attr *attr,
Ingo Molnar04289bb2008-12-11 08:38:42 +01003504 int cpu,
Paul Mackerras23a185c2009-02-09 22:42:47 +11003505 struct perf_counter_context *ctx,
Ingo Molnar9b51f662008-12-12 13:49:45 +01003506 struct perf_counter *group_leader,
3507 gfp_t gfpflags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003508{
Robert Richter4aeb0b42009-04-29 12:47:03 +02003509 const struct pmu *pmu;
Ingo Molnar621a01e2008-12-11 12:46:46 +01003510 struct perf_counter *counter;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003511 struct hw_perf_counter *hwc;
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003512 long err;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003513
Ingo Molnar9b51f662008-12-12 13:49:45 +01003514 counter = kzalloc(sizeof(*counter), gfpflags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003515 if (!counter)
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003516 return ERR_PTR(-ENOMEM);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003517
Ingo Molnar04289bb2008-12-11 08:38:42 +01003518 /*
3519 * Single counters are their own group leaders, with an
3520 * empty sibling list:
3521 */
3522 if (!group_leader)
3523 group_leader = counter;
3524
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003525 mutex_init(&counter->child_mutex);
3526 INIT_LIST_HEAD(&counter->child_list);
3527
Ingo Molnar04289bb2008-12-11 08:38:42 +01003528 INIT_LIST_HEAD(&counter->list_entry);
Peter Zijlstra592903c2009-03-13 12:21:36 +01003529 INIT_LIST_HEAD(&counter->event_entry);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003530 INIT_LIST_HEAD(&counter->sibling_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003531 init_waitqueue_head(&counter->waitq);
3532
Peter Zijlstra7b732a72009-03-23 18:22:10 +01003533 mutex_init(&counter->mmap_mutex);
3534
Peter Zijlstraa96bbc12009-06-03 14:01:36 +02003535 counter->cpu = cpu;
Peter Zijlstra0d486962009-06-02 19:22:16 +02003536 counter->attr = *attr;
Peter Zijlstraa96bbc12009-06-03 14:01:36 +02003537 counter->group_leader = group_leader;
3538 counter->pmu = NULL;
3539 counter->ctx = ctx;
3540 counter->oncpu = -1;
Ingo Molnar329d8762009-05-26 08:10:00 +02003541
Peter Zijlstraa96bbc12009-06-03 14:01:36 +02003542 counter->ns = get_pid_ns(current->nsproxy->pid_ns);
3543 counter->id = atomic64_inc_return(&perf_counter_id);
3544
3545 counter->state = PERF_COUNTER_STATE_INACTIVE;
3546
Peter Zijlstra0d486962009-06-02 19:22:16 +02003547 if (attr->disabled)
Ingo Molnara86ed502008-12-17 00:43:10 +01003548 counter->state = PERF_COUNTER_STATE_OFF;
3549
Robert Richter4aeb0b42009-04-29 12:47:03 +02003550 pmu = NULL;
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003551
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003552 hwc = &counter->hw;
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02003553 hwc->sample_period = attr->sample_period;
Peter Zijlstra0d486962009-06-02 19:22:16 +02003554 if (attr->freq && attr->sample_freq)
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02003555 hwc->sample_period = 1;
3556
3557 atomic64_set(&hwc->period_left, hwc->sample_period);
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003558
Peter Zijlstra2023b352009-05-05 17:50:26 +02003559 /*
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003560 * we currently do not support PERF_SAMPLE_GROUP on inherited counters
Peter Zijlstra2023b352009-05-05 17:50:26 +02003561 */
Peter Zijlstra0d486962009-06-02 19:22:16 +02003562 if (attr->inherit && (attr->sample_type & PERF_SAMPLE_GROUP))
Peter Zijlstra2023b352009-05-05 17:50:26 +02003563 goto done;
3564
Ingo Molnara21ca2c2009-06-06 09:58:57 +02003565 if (attr->type == PERF_TYPE_RAW) {
Robert Richter4aeb0b42009-04-29 12:47:03 +02003566 pmu = hw_perf_counter_init(counter);
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01003567 goto done;
3568 }
3569
Ingo Molnara21ca2c2009-06-06 09:58:57 +02003570 switch (attr->type) {
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003571 case PERF_TYPE_HARDWARE:
Ingo Molnar8326f442009-06-05 20:22:46 +02003572 case PERF_TYPE_HW_CACHE:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003573 pmu = hw_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003574 break;
3575
3576 case PERF_TYPE_SOFTWARE:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003577 pmu = sw_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003578 break;
3579
3580 case PERF_TYPE_TRACEPOINT:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003581 pmu = tp_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003582 break;
3583 }
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01003584done:
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003585 err = 0;
Robert Richter4aeb0b42009-04-29 12:47:03 +02003586 if (!pmu)
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003587 err = -EINVAL;
Robert Richter4aeb0b42009-04-29 12:47:03 +02003588 else if (IS_ERR(pmu))
3589 err = PTR_ERR(pmu);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003590
3591 if (err) {
Peter Zijlstraa96bbc12009-06-03 14:01:36 +02003592 if (counter->ns)
3593 put_pid_ns(counter->ns);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003594 kfree(counter);
3595 return ERR_PTR(err);
3596 }
3597
Robert Richter4aeb0b42009-04-29 12:47:03 +02003598 counter->pmu = pmu;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003599
Peter Zijlstra7fc23a52009-05-08 18:52:21 +02003600 atomic_inc(&nr_counters);
Peter Zijlstra0d486962009-06-02 19:22:16 +02003601 if (counter->attr.mmap)
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003602 atomic_inc(&nr_mmap_counters);
Peter Zijlstra0d486962009-06-02 19:22:16 +02003603 if (counter->attr.comm)
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003604 atomic_inc(&nr_comm_counters);
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02003605
Thomas Gleixner0793a612008-12-04 20:12:29 +01003606 return counter;
3607}
3608
3609/**
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003610 * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
Ingo Molnar9f66a382008-12-10 12:33:23 +01003611 *
Peter Zijlstra0d486962009-06-02 19:22:16 +02003612 * @attr_uptr: event type attributes for monitoring/sampling
Thomas Gleixner0793a612008-12-04 20:12:29 +01003613 * @pid: target pid
Ingo Molnar9f66a382008-12-10 12:33:23 +01003614 * @cpu: target cpu
3615 * @group_fd: group leader counter fd
Thomas Gleixner0793a612008-12-04 20:12:29 +01003616 */
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003617SYSCALL_DEFINE5(perf_counter_open,
Peter Zijlstra0d486962009-06-02 19:22:16 +02003618 const struct perf_counter_attr __user *, attr_uptr,
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003619 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003620{
Ingo Molnar04289bb2008-12-11 08:38:42 +01003621 struct perf_counter *counter, *group_leader;
Peter Zijlstra0d486962009-06-02 19:22:16 +02003622 struct perf_counter_attr attr;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003623 struct perf_counter_context *ctx;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003624 struct file *counter_file = NULL;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003625 struct file *group_file = NULL;
3626 int fput_needed = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003627 int fput_needed2 = 0;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003628 int ret;
3629
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003630 /* for future expandability... */
3631 if (flags)
3632 return -EINVAL;
3633
Peter Zijlstra0d486962009-06-02 19:22:16 +02003634 if (copy_from_user(&attr, attr_uptr, sizeof(attr)) != 0)
Thomas Gleixnereab656a2008-12-08 19:26:59 +01003635 return -EFAULT;
3636
Peter Zijlstra07647712009-06-11 11:18:36 +02003637 if (!attr.exclude_kernel) {
3638 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
3639 return -EACCES;
3640 }
3641
Ingo Molnar04289bb2008-12-11 08:38:42 +01003642 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01003643 * Get the target context (task or percpu):
3644 */
3645 ctx = find_get_context(pid, cpu);
3646 if (IS_ERR(ctx))
3647 return PTR_ERR(ctx);
3648
3649 /*
3650 * Look up the group leader (we will attach this counter to it):
Ingo Molnar04289bb2008-12-11 08:38:42 +01003651 */
3652 group_leader = NULL;
3653 if (group_fd != -1) {
3654 ret = -EINVAL;
3655 group_file = fget_light(group_fd, &fput_needed);
3656 if (!group_file)
Ingo Molnarccff2862008-12-11 11:26:29 +01003657 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003658 if (group_file->f_op != &perf_fops)
Ingo Molnarccff2862008-12-11 11:26:29 +01003659 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003660
3661 group_leader = group_file->private_data;
3662 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01003663 * Do not allow a recursive hierarchy (this new sibling
3664 * becoming part of another group-sibling):
Ingo Molnar04289bb2008-12-11 08:38:42 +01003665 */
Ingo Molnarccff2862008-12-11 11:26:29 +01003666 if (group_leader->group_leader != group_leader)
3667 goto err_put_context;
3668 /*
3669 * Do not allow to attach to a group in a different
3670 * task or CPU context:
3671 */
3672 if (group_leader->ctx != ctx)
3673 goto err_put_context;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11003674 /*
3675 * Only a group leader can be exclusive or pinned
3676 */
Peter Zijlstra0d486962009-06-02 19:22:16 +02003677 if (attr.exclusive || attr.pinned)
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11003678 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003679 }
3680
Peter Zijlstra0d486962009-06-02 19:22:16 +02003681 counter = perf_counter_alloc(&attr, cpu, ctx, group_leader,
Paul Mackerras23a185c2009-02-09 22:42:47 +11003682 GFP_KERNEL);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003683 ret = PTR_ERR(counter);
3684 if (IS_ERR(counter))
Thomas Gleixner0793a612008-12-04 20:12:29 +01003685 goto err_put_context;
3686
Thomas Gleixner0793a612008-12-04 20:12:29 +01003687 ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
3688 if (ret < 0)
Ingo Molnar9b51f662008-12-12 13:49:45 +01003689 goto err_free_put_context;
3690
3691 counter_file = fget_light(ret, &fput_needed2);
3692 if (!counter_file)
3693 goto err_free_put_context;
3694
3695 counter->filp = counter_file;
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003696 WARN_ON_ONCE(ctx->parent_ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003697 mutex_lock(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003698 perf_install_in_context(ctx, counter, cpu);
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003699 ++ctx->generation;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003700 mutex_unlock(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003701
Peter Zijlstra082ff5a2009-05-23 18:29:00 +02003702 counter->owner = current;
3703 get_task_struct(current);
3704 mutex_lock(&current->perf_counter_mutex);
3705 list_add_tail(&counter->owner_entry, &current->perf_counter_list);
3706 mutex_unlock(&current->perf_counter_mutex);
3707
Ingo Molnar9b51f662008-12-12 13:49:45 +01003708 fput_light(counter_file, fput_needed2);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003709
Ingo Molnar04289bb2008-12-11 08:38:42 +01003710out_fput:
3711 fput_light(group_file, fput_needed);
3712
Thomas Gleixner0793a612008-12-04 20:12:29 +01003713 return ret;
3714
Ingo Molnar9b51f662008-12-12 13:49:45 +01003715err_free_put_context:
Thomas Gleixner0793a612008-12-04 20:12:29 +01003716 kfree(counter);
3717
3718err_put_context:
Paul Mackerrasc93f7662009-05-28 22:18:17 +10003719 put_ctx(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003720
Ingo Molnar04289bb2008-12-11 08:38:42 +01003721 goto out_fput;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003722}
3723
Ingo Molnar9b51f662008-12-12 13:49:45 +01003724/*
Ingo Molnar9b51f662008-12-12 13:49:45 +01003725 * inherit a counter from parent task to child task:
3726 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11003727static struct perf_counter *
Ingo Molnar9b51f662008-12-12 13:49:45 +01003728inherit_counter(struct perf_counter *parent_counter,
3729 struct task_struct *parent,
3730 struct perf_counter_context *parent_ctx,
3731 struct task_struct *child,
Paul Mackerrasd859e292009-01-17 18:10:22 +11003732 struct perf_counter *group_leader,
Ingo Molnar9b51f662008-12-12 13:49:45 +01003733 struct perf_counter_context *child_ctx)
3734{
3735 struct perf_counter *child_counter;
3736
Paul Mackerrasd859e292009-01-17 18:10:22 +11003737 /*
3738 * Instead of creating recursive hierarchies of counters,
3739 * we link inherited counters back to the original parent,
3740 * which has a filp for sure, which we use as the reference
3741 * count:
3742 */
3743 if (parent_counter->parent)
3744 parent_counter = parent_counter->parent;
3745
Peter Zijlstra0d486962009-06-02 19:22:16 +02003746 child_counter = perf_counter_alloc(&parent_counter->attr,
Paul Mackerras23a185c2009-02-09 22:42:47 +11003747 parent_counter->cpu, child_ctx,
3748 group_leader, GFP_KERNEL);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003749 if (IS_ERR(child_counter))
3750 return child_counter;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10003751 get_ctx(child_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003752
3753 /*
Paul Mackerras564c2b22009-05-22 14:27:22 +10003754 * Make the child state follow the state of the parent counter,
Peter Zijlstra0d486962009-06-02 19:22:16 +02003755 * not its attr.disabled bit. We hold the parent's mutex,
Ingo Molnar22a4f652009-06-01 10:13:37 +02003756 * so we won't race with perf_counter_{en, dis}able_family.
Paul Mackerras564c2b22009-05-22 14:27:22 +10003757 */
3758 if (parent_counter->state >= PERF_COUNTER_STATE_INACTIVE)
3759 child_counter->state = PERF_COUNTER_STATE_INACTIVE;
3760 else
3761 child_counter->state = PERF_COUNTER_STATE_OFF;
3762
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02003763 if (parent_counter->attr.freq)
3764 child_counter->hw.sample_period = parent_counter->hw.sample_period;
3765
Paul Mackerras564c2b22009-05-22 14:27:22 +10003766 /*
Ingo Molnar9b51f662008-12-12 13:49:45 +01003767 * Link it up in the child's context:
3768 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +11003769 add_counter_to_ctx(child_counter, child_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003770
3771 child_counter->parent = parent_counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003772 /*
3773 * inherit into child's child as well:
3774 */
Peter Zijlstra0d486962009-06-02 19:22:16 +02003775 child_counter->attr.inherit = 1;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003776
3777 /*
3778 * Get a reference to the parent filp - we will fput it
3779 * when the child counter exits. This is safe to do because
3780 * we are in the parent and we know that the filp still
3781 * exists and has a nonzero count:
3782 */
3783 atomic_long_inc(&parent_counter->filp->f_count);
3784
Paul Mackerrasd859e292009-01-17 18:10:22 +11003785 /*
3786 * Link this into the parent counter's child list
3787 */
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003788 WARN_ON_ONCE(parent_counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003789 mutex_lock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003790 list_add_tail(&child_counter->child_list, &parent_counter->child_list);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003791 mutex_unlock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003792
3793 return child_counter;
3794}
3795
3796static int inherit_group(struct perf_counter *parent_counter,
3797 struct task_struct *parent,
3798 struct perf_counter_context *parent_ctx,
3799 struct task_struct *child,
3800 struct perf_counter_context *child_ctx)
3801{
3802 struct perf_counter *leader;
3803 struct perf_counter *sub;
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003804 struct perf_counter *child_ctr;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003805
3806 leader = inherit_counter(parent_counter, parent, parent_ctx,
3807 child, NULL, child_ctx);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003808 if (IS_ERR(leader))
3809 return PTR_ERR(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003810 list_for_each_entry(sub, &parent_counter->sibling_list, list_entry) {
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003811 child_ctr = inherit_counter(sub, parent, parent_ctx,
3812 child, leader, child_ctx);
3813 if (IS_ERR(child_ctr))
3814 return PTR_ERR(child_ctr);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003815 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01003816 return 0;
3817}
3818
Paul Mackerrasd859e292009-01-17 18:10:22 +11003819static void sync_child_counter(struct perf_counter *child_counter,
3820 struct perf_counter *parent_counter)
3821{
Peter Zijlstra8bc20952009-05-15 20:45:59 +02003822 u64 child_val;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003823
Paul Mackerrasd859e292009-01-17 18:10:22 +11003824 child_val = atomic64_read(&child_counter->count);
3825
3826 /*
3827 * Add back the child's count to the parent's count:
3828 */
3829 atomic64_add(child_val, &parent_counter->count);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11003830 atomic64_add(child_counter->total_time_enabled,
3831 &parent_counter->child_total_time_enabled);
3832 atomic64_add(child_counter->total_time_running,
3833 &parent_counter->child_total_time_running);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003834
3835 /*
3836 * Remove this counter from the parent's list
3837 */
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003838 WARN_ON_ONCE(parent_counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003839 mutex_lock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003840 list_del_init(&child_counter->child_list);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003841 mutex_unlock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003842
3843 /*
3844 * Release the parent counter, if this was the last
3845 * reference to it.
3846 */
3847 fput(parent_counter->filp);
3848}
3849
Ingo Molnar9b51f662008-12-12 13:49:45 +01003850static void
Peter Zijlstrabbbee902009-05-29 14:25:58 +02003851__perf_counter_exit_task(struct perf_counter *child_counter,
Ingo Molnar9b51f662008-12-12 13:49:45 +01003852 struct perf_counter_context *child_ctx)
3853{
3854 struct perf_counter *parent_counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003855
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003856 update_counter_times(child_counter);
Peter Zijlstraaa9c67f2009-05-23 18:28:59 +02003857 perf_counter_remove_from_context(child_counter);
Ingo Molnar0cc0c022008-12-14 23:20:36 +01003858
Ingo Molnar9b51f662008-12-12 13:49:45 +01003859 parent_counter = child_counter->parent;
3860 /*
3861 * It can happen that parent exits first, and has counters
3862 * that are still around due to the child reference. These
3863 * counters need to be zapped - but otherwise linger.
3864 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11003865 if (parent_counter) {
3866 sync_child_counter(child_counter, parent_counter);
Peter Zijlstraf1600952009-03-19 20:26:16 +01003867 free_counter(child_counter);
Paul Mackerras4bcf3492009-02-11 13:53:19 +01003868 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01003869}
3870
3871/*
Paul Mackerrasd859e292009-01-17 18:10:22 +11003872 * When a child task exits, feed back counter values to parent counters.
Ingo Molnar9b51f662008-12-12 13:49:45 +01003873 */
3874void perf_counter_exit_task(struct task_struct *child)
3875{
3876 struct perf_counter *child_counter, *tmp;
3877 struct perf_counter_context *child_ctx;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003878 unsigned long flags;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003879
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003880 if (likely(!child->perf_counter_ctxp))
Ingo Molnar9b51f662008-12-12 13:49:45 +01003881 return;
3882
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003883 local_irq_save(flags);
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003884 /*
3885 * We can't reschedule here because interrupts are disabled,
3886 * and either child is current or it is a task that can't be
3887 * scheduled, so we are now safe from rescheduling changing
3888 * our context.
3889 */
3890 child_ctx = child->perf_counter_ctxp;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003891 __perf_counter_task_sched_out(child_ctx);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10003892
3893 /*
3894 * Take the context lock here so that if find_get_context is
3895 * reading child->perf_counter_ctxp, we wait until it has
3896 * incremented the context's refcount before we do put_ctx below.
3897 */
3898 spin_lock(&child_ctx->lock);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003899 child->perf_counter_ctxp = NULL;
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003900 if (child_ctx->parent_ctx) {
3901 /*
3902 * This context is a clone; unclone it so it can't get
3903 * swapped to another process while we're removing all
3904 * the counters from it.
3905 */
3906 put_ctx(child_ctx->parent_ctx);
3907 child_ctx->parent_ctx = NULL;
3908 }
Paul Mackerrasc93f7662009-05-28 22:18:17 +10003909 spin_unlock(&child_ctx->lock);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003910 local_irq_restore(flags);
3911
Peter Zijlstra66fff222009-06-10 22:53:37 +02003912 /*
3913 * We can recurse on the same lock type through:
3914 *
3915 * __perf_counter_exit_task()
3916 * sync_child_counter()
3917 * fput(parent_counter->filp)
3918 * perf_release()
3919 * mutex_lock(&ctx->mutex)
3920 *
3921 * But since its the parent context it won't be the same instance.
3922 */
3923 mutex_lock_nested(&child_ctx->mutex, SINGLE_DEPTH_NESTING);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003924
Peter Zijlstra8bc20952009-05-15 20:45:59 +02003925again:
Ingo Molnar9b51f662008-12-12 13:49:45 +01003926 list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list,
3927 list_entry)
Peter Zijlstrabbbee902009-05-29 14:25:58 +02003928 __perf_counter_exit_task(child_counter, child_ctx);
Peter Zijlstra8bc20952009-05-15 20:45:59 +02003929
3930 /*
3931 * If the last counter was a group counter, it will have appended all
3932 * its siblings to the list, but we obtained 'tmp' before that which
3933 * will still point to the list head terminating the iteration.
3934 */
3935 if (!list_empty(&child_ctx->counter_list))
3936 goto again;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003937
3938 mutex_unlock(&child_ctx->mutex);
3939
3940 put_ctx(child_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003941}
3942
3943/*
Peter Zijlstrabbbee902009-05-29 14:25:58 +02003944 * free an unexposed, unused context as created by inheritance by
3945 * init_task below, used by fork() in case of fail.
3946 */
3947void perf_counter_free_task(struct task_struct *task)
3948{
3949 struct perf_counter_context *ctx = task->perf_counter_ctxp;
3950 struct perf_counter *counter, *tmp;
3951
3952 if (!ctx)
3953 return;
3954
3955 mutex_lock(&ctx->mutex);
3956again:
3957 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry) {
3958 struct perf_counter *parent = counter->parent;
3959
3960 if (WARN_ON_ONCE(!parent))
3961 continue;
3962
3963 mutex_lock(&parent->child_mutex);
3964 list_del_init(&counter->child_list);
3965 mutex_unlock(&parent->child_mutex);
3966
3967 fput(parent->filp);
3968
3969 list_del_counter(counter, ctx);
3970 free_counter(counter);
3971 }
3972
3973 if (!list_empty(&ctx->counter_list))
3974 goto again;
3975
3976 mutex_unlock(&ctx->mutex);
3977
3978 put_ctx(ctx);
3979}
3980
3981/*
Ingo Molnar9b51f662008-12-12 13:49:45 +01003982 * Initialize the perf_counter context in task_struct
3983 */
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02003984int perf_counter_init_task(struct task_struct *child)
Ingo Molnar9b51f662008-12-12 13:49:45 +01003985{
3986 struct perf_counter_context *child_ctx, *parent_ctx;
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003987 struct perf_counter_context *cloned_ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003988 struct perf_counter *counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003989 struct task_struct *parent = current;
Paul Mackerras564c2b22009-05-22 14:27:22 +10003990 int inherited_all = 1;
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02003991 int ret = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003992
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003993 child->perf_counter_ctxp = NULL;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003994
Peter Zijlstra082ff5a2009-05-23 18:29:00 +02003995 mutex_init(&child->perf_counter_mutex);
3996 INIT_LIST_HEAD(&child->perf_counter_list);
3997
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003998 if (likely(!parent->perf_counter_ctxp))
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02003999 return 0;
4000
Ingo Molnar9b51f662008-12-12 13:49:45 +01004001 /*
4002 * This is executed from the parent task context, so inherit
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004003 * counters that have been marked for cloning.
4004 * First allocate and initialize a context for the child.
Ingo Molnar9b51f662008-12-12 13:49:45 +01004005 */
4006
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004007 child_ctx = kmalloc(sizeof(struct perf_counter_context), GFP_KERNEL);
4008 if (!child_ctx)
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02004009 return -ENOMEM;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004010
4011 __perf_counter_init_context(child_ctx, child);
4012 child->perf_counter_ctxp = child_ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10004013 get_task_struct(child);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004014
Ingo Molnar9b51f662008-12-12 13:49:45 +01004015 /*
Paul Mackerras25346b932009-06-01 17:48:12 +10004016 * If the parent's context is a clone, pin it so it won't get
4017 * swapped under us.
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004018 */
Paul Mackerras25346b932009-06-01 17:48:12 +10004019 parent_ctx = perf_pin_task_context(parent);
4020
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004021 /*
4022 * No need to check if parent_ctx != NULL here; since we saw
4023 * it non-NULL earlier, the only reason for it to become NULL
4024 * is if we exit, and since we're currently in the middle of
4025 * a fork we can't be exiting at the same time.
4026 */
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004027
4028 /*
Ingo Molnar9b51f662008-12-12 13:49:45 +01004029 * Lock the parent list. No need to lock the child - not PID
4030 * hashed yet and not running, so nobody can access it.
4031 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11004032 mutex_lock(&parent_ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01004033
4034 /*
4035 * We dont have to disable NMIs - we are only looking at
4036 * the list, not manipulating it:
4037 */
Peter Zijlstrad7b629a2009-05-20 12:21:19 +02004038 list_for_each_entry_rcu(counter, &parent_ctx->event_list, event_entry) {
4039 if (counter != counter->group_leader)
4040 continue;
4041
Peter Zijlstra0d486962009-06-02 19:22:16 +02004042 if (!counter->attr.inherit) {
Paul Mackerras564c2b22009-05-22 14:27:22 +10004043 inherited_all = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004044 continue;
Paul Mackerras564c2b22009-05-22 14:27:22 +10004045 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01004046
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02004047 ret = inherit_group(counter, parent, parent_ctx,
4048 child, child_ctx);
4049 if (ret) {
Paul Mackerras564c2b22009-05-22 14:27:22 +10004050 inherited_all = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004051 break;
Paul Mackerras564c2b22009-05-22 14:27:22 +10004052 }
4053 }
4054
4055 if (inherited_all) {
4056 /*
4057 * Mark the child context as a clone of the parent
4058 * context, or of whatever the parent is a clone of.
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004059 * Note that if the parent is a clone, it could get
4060 * uncloned at any point, but that doesn't matter
4061 * because the list of counters and the generation
4062 * count can't have changed since we took the mutex.
Paul Mackerras564c2b22009-05-22 14:27:22 +10004063 */
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004064 cloned_ctx = rcu_dereference(parent_ctx->parent_ctx);
4065 if (cloned_ctx) {
4066 child_ctx->parent_ctx = cloned_ctx;
Paul Mackerras25346b932009-06-01 17:48:12 +10004067 child_ctx->parent_gen = parent_ctx->parent_gen;
Paul Mackerras564c2b22009-05-22 14:27:22 +10004068 } else {
4069 child_ctx->parent_ctx = parent_ctx;
4070 child_ctx->parent_gen = parent_ctx->generation;
4071 }
4072 get_ctx(child_ctx->parent_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01004073 }
4074
Paul Mackerrasd859e292009-01-17 18:10:22 +11004075 mutex_unlock(&parent_ctx->mutex);
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02004076
Paul Mackerras25346b932009-06-01 17:48:12 +10004077 perf_unpin_context(parent_ctx);
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004078
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02004079 return ret;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004080}
4081
Ingo Molnar04289bb2008-12-11 08:38:42 +01004082static void __cpuinit perf_counter_init_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01004083{
Ingo Molnar04289bb2008-12-11 08:38:42 +01004084 struct perf_cpu_context *cpuctx;
Thomas Gleixner0793a612008-12-04 20:12:29 +01004085
Ingo Molnar04289bb2008-12-11 08:38:42 +01004086 cpuctx = &per_cpu(perf_cpu_context, cpu);
4087 __perf_counter_init_context(&cpuctx->ctx, NULL);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004088
Ingo Molnar1dce8d92009-05-04 19:23:18 +02004089 spin_lock(&perf_resource_lock);
Ingo Molnar04289bb2008-12-11 08:38:42 +01004090 cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu;
Ingo Molnar1dce8d92009-05-04 19:23:18 +02004091 spin_unlock(&perf_resource_lock);
Ingo Molnar04289bb2008-12-11 08:38:42 +01004092
Paul Mackerras01d02872009-01-14 13:44:19 +11004093 hw_perf_counter_setup(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004094}
4095
4096#ifdef CONFIG_HOTPLUG_CPU
Ingo Molnar04289bb2008-12-11 08:38:42 +01004097static void __perf_counter_exit_cpu(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01004098{
4099 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
4100 struct perf_counter_context *ctx = &cpuctx->ctx;
4101 struct perf_counter *counter, *tmp;
4102
Ingo Molnar04289bb2008-12-11 08:38:42 +01004103 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry)
4104 __perf_counter_remove_from_context(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004105}
Ingo Molnar04289bb2008-12-11 08:38:42 +01004106static void perf_counter_exit_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01004107{
Paul Mackerrasd859e292009-01-17 18:10:22 +11004108 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
4109 struct perf_counter_context *ctx = &cpuctx->ctx;
4110
4111 mutex_lock(&ctx->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01004112 smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1);
Paul Mackerrasd859e292009-01-17 18:10:22 +11004113 mutex_unlock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004114}
4115#else
Ingo Molnar04289bb2008-12-11 08:38:42 +01004116static inline void perf_counter_exit_cpu(int cpu) { }
Thomas Gleixner0793a612008-12-04 20:12:29 +01004117#endif
4118
4119static int __cpuinit
4120perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
4121{
4122 unsigned int cpu = (long)hcpu;
4123
4124 switch (action) {
4125
4126 case CPU_UP_PREPARE:
4127 case CPU_UP_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01004128 perf_counter_init_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004129 break;
4130
4131 case CPU_DOWN_PREPARE:
4132 case CPU_DOWN_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01004133 perf_counter_exit_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004134 break;
4135
4136 default:
4137 break;
4138 }
4139
4140 return NOTIFY_OK;
4141}
4142
Paul Mackerrasf38b0822009-06-02 21:05:16 +10004143/*
4144 * This has to have a higher priority than migration_notifier in sched.c.
4145 */
Thomas Gleixner0793a612008-12-04 20:12:29 +01004146static struct notifier_block __cpuinitdata perf_cpu_nb = {
4147 .notifier_call = perf_cpu_notify,
Paul Mackerrasf38b0822009-06-02 21:05:16 +10004148 .priority = 20,
Thomas Gleixner0793a612008-12-04 20:12:29 +01004149};
4150
Ingo Molnar0d905bc2009-05-04 19:13:30 +02004151void __init perf_counter_init(void)
Thomas Gleixner0793a612008-12-04 20:12:29 +01004152{
4153 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
4154 (void *)(long)smp_processor_id());
4155 register_cpu_notifier(&perf_cpu_nb);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004156}
Thomas Gleixner0793a612008-12-04 20:12:29 +01004157
4158static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
4159{
4160 return sprintf(buf, "%d\n", perf_reserved_percpu);
4161}
4162
4163static ssize_t
4164perf_set_reserve_percpu(struct sysdev_class *class,
4165 const char *buf,
4166 size_t count)
4167{
4168 struct perf_cpu_context *cpuctx;
4169 unsigned long val;
4170 int err, cpu, mpt;
4171
4172 err = strict_strtoul(buf, 10, &val);
4173 if (err)
4174 return err;
4175 if (val > perf_max_counters)
4176 return -EINVAL;
4177
Ingo Molnar1dce8d92009-05-04 19:23:18 +02004178 spin_lock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004179 perf_reserved_percpu = val;
4180 for_each_online_cpu(cpu) {
4181 cpuctx = &per_cpu(perf_cpu_context, cpu);
4182 spin_lock_irq(&cpuctx->ctx.lock);
4183 mpt = min(perf_max_counters - cpuctx->ctx.nr_counters,
4184 perf_max_counters - perf_reserved_percpu);
4185 cpuctx->max_pertask = mpt;
4186 spin_unlock_irq(&cpuctx->ctx.lock);
4187 }
Ingo Molnar1dce8d92009-05-04 19:23:18 +02004188 spin_unlock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004189
4190 return count;
4191}
4192
4193static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
4194{
4195 return sprintf(buf, "%d\n", perf_overcommit);
4196}
4197
4198static ssize_t
4199perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
4200{
4201 unsigned long val;
4202 int err;
4203
4204 err = strict_strtoul(buf, 10, &val);
4205 if (err)
4206 return err;
4207 if (val > 1)
4208 return -EINVAL;
4209
Ingo Molnar1dce8d92009-05-04 19:23:18 +02004210 spin_lock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004211 perf_overcommit = val;
Ingo Molnar1dce8d92009-05-04 19:23:18 +02004212 spin_unlock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004213
4214 return count;
4215}
4216
4217static SYSDEV_CLASS_ATTR(
4218 reserve_percpu,
4219 0644,
4220 perf_show_reserve_percpu,
4221 perf_set_reserve_percpu
4222 );
4223
4224static SYSDEV_CLASS_ATTR(
4225 overcommit,
4226 0644,
4227 perf_show_overcommit,
4228 perf_set_overcommit
4229 );
4230
4231static struct attribute *perfclass_attrs[] = {
4232 &attr_reserve_percpu.attr,
4233 &attr_overcommit.attr,
4234 NULL
4235};
4236
4237static struct attribute_group perfclass_attr_group = {
4238 .attrs = perfclass_attrs,
4239 .name = "perf_counters",
4240};
4241
4242static int __init perf_counter_sysfs_init(void)
4243{
4244 return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
4245 &perfclass_attr_group);
4246}
4247device_initcall(perf_counter_sysfs_init);