blob: cd94cf3bf9e2a652dd39b5a95cb6f29b0c6bbcc5 [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 Zijlstra9ee318a2009-04-09 10:53:44 +020043static atomic_t nr_mmap_tracking __read_mostly;
44static atomic_t nr_munmap_tracking __read_mostly;
45static atomic_t nr_comm_tracking __read_mostly;
46
Peter Zijlstra1ccd1542009-04-09 10:53:45 +020047int sysctl_perf_counter_priv __read_mostly; /* do we need to be privileged */
Peter Zijlstra789f90f2009-05-15 15:19:27 +020048int sysctl_perf_counter_mlock __read_mostly = 512; /* 'free' kb per user */
Peter Zijlstraa78ac322009-05-25 17:39:05 +020049int sysctl_perf_counter_limit __read_mostly = 100000; /* max NMIs per second */
Peter Zijlstra1ccd1542009-04-09 10:53:45 +020050
Thomas Gleixner0793a612008-12-04 20:12:29 +010051/*
Ingo Molnar1dce8d92009-05-04 19:23:18 +020052 * Lock for (sysadmin-configurable) counter reservations:
Thomas Gleixner0793a612008-12-04 20:12:29 +010053 */
Ingo Molnar1dce8d92009-05-04 19:23:18 +020054static DEFINE_SPINLOCK(perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +010055
56/*
57 * Architecture provided APIs - weak aliases:
58 */
Robert Richter4aeb0b42009-04-29 12:47:03 +020059extern __weak const struct pmu *hw_perf_counter_init(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +010060{
Paul Mackerrasff6f0542009-01-09 16:19:25 +110061 return NULL;
Thomas Gleixner0793a612008-12-04 20:12:29 +010062}
63
Peter Zijlstra9e35ad32009-05-13 16:21:38 +020064void __weak hw_perf_disable(void) { barrier(); }
65void __weak hw_perf_enable(void) { barrier(); }
66
Paul Mackerras01d02872009-01-14 13:44:19 +110067void __weak hw_perf_counter_setup(int cpu) { barrier(); }
Ingo Molnar22a4f652009-06-01 10:13:37 +020068
69int __weak
70hw_perf_group_sched_in(struct perf_counter *group_leader,
Paul Mackerras3cbed422009-01-09 16:43:42 +110071 struct perf_cpu_context *cpuctx,
72 struct perf_counter_context *ctx, int cpu)
73{
74 return 0;
75}
Thomas Gleixner0793a612008-12-04 20:12:29 +010076
Paul Mackerras4eb96fc2009-01-09 17:24:34 +110077void __weak perf_counter_print_debug(void) { }
78
Peter Zijlstra9e35ad32009-05-13 16:21:38 +020079static DEFINE_PER_CPU(int, disable_count);
80
81void __perf_disable(void)
82{
83 __get_cpu_var(disable_count)++;
84}
85
86bool __perf_enable(void)
87{
88 return !--__get_cpu_var(disable_count);
89}
90
91void perf_disable(void)
92{
93 __perf_disable();
94 hw_perf_disable();
95}
Peter Zijlstra9e35ad32009-05-13 16:21:38 +020096
97void perf_enable(void)
98{
99 if (__perf_enable())
100 hw_perf_enable();
101}
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200102
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000103static void get_ctx(struct perf_counter_context *ctx)
104{
105 atomic_inc(&ctx->refcount);
106}
107
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000108static void free_ctx(struct rcu_head *head)
109{
110 struct perf_counter_context *ctx;
111
112 ctx = container_of(head, struct perf_counter_context, rcu_head);
113 kfree(ctx);
114}
115
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000116static void put_ctx(struct perf_counter_context *ctx)
117{
Paul Mackerras564c2b22009-05-22 14:27:22 +1000118 if (atomic_dec_and_test(&ctx->refcount)) {
119 if (ctx->parent_ctx)
120 put_ctx(ctx->parent_ctx);
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000121 if (ctx->task)
122 put_task_struct(ctx->task);
123 call_rcu(&ctx->rcu_head, free_ctx);
Paul Mackerras564c2b22009-05-22 14:27:22 +1000124 }
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000125}
126
Peter Zijlstrafccc7142009-05-23 18:28:56 +0200127/*
Paul Mackerras25346b932009-06-01 17:48:12 +1000128 * Get the perf_counter_context for a task and lock it.
129 * This has to cope with with the fact that until it is locked,
130 * the context could get moved to another task.
131 */
Ingo Molnar22a4f652009-06-01 10:13:37 +0200132static struct perf_counter_context *
133perf_lock_task_context(struct task_struct *task, unsigned long *flags)
Paul Mackerras25346b932009-06-01 17:48:12 +1000134{
135 struct perf_counter_context *ctx;
136
137 rcu_read_lock();
138 retry:
139 ctx = rcu_dereference(task->perf_counter_ctxp);
140 if (ctx) {
141 /*
142 * If this context is a clone of another, it might
143 * get swapped for another underneath us by
144 * perf_counter_task_sched_out, though the
145 * rcu_read_lock() protects us from any context
146 * getting freed. Lock the context and check if it
147 * got swapped before we could get the lock, and retry
148 * if so. If we locked the right context, then it
149 * can't get swapped on us any more.
150 */
151 spin_lock_irqsave(&ctx->lock, *flags);
152 if (ctx != rcu_dereference(task->perf_counter_ctxp)) {
153 spin_unlock_irqrestore(&ctx->lock, *flags);
154 goto retry;
155 }
156 }
157 rcu_read_unlock();
158 return ctx;
159}
160
161/*
162 * Get the context for a task and increment its pin_count so it
163 * can't get swapped to another task. This also increments its
164 * reference count so that the context can't get freed.
165 */
166static struct perf_counter_context *perf_pin_task_context(struct task_struct *task)
167{
168 struct perf_counter_context *ctx;
169 unsigned long flags;
170
171 ctx = perf_lock_task_context(task, &flags);
172 if (ctx) {
173 ++ctx->pin_count;
174 get_ctx(ctx);
175 spin_unlock_irqrestore(&ctx->lock, flags);
176 }
177 return ctx;
178}
179
180static void perf_unpin_context(struct perf_counter_context *ctx)
181{
182 unsigned long flags;
183
184 spin_lock_irqsave(&ctx->lock, flags);
185 --ctx->pin_count;
186 spin_unlock_irqrestore(&ctx->lock, flags);
187 put_ctx(ctx);
188}
189
190/*
Peter Zijlstrafccc7142009-05-23 18:28:56 +0200191 * Add a counter from the lists for its context.
192 * Must be called with ctx->mutex and ctx->lock held.
193 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100194static void
195list_add_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
196{
197 struct perf_counter *group_leader = counter->group_leader;
198
199 /*
200 * Depending on whether it is a standalone or sibling counter,
201 * add it straight to the context's counter list, or to the group
202 * leader's sibling list:
203 */
Peter Zijlstra3df5eda2009-05-08 18:52:22 +0200204 if (group_leader == counter)
Ingo Molnar04289bb2008-12-11 08:38:42 +0100205 list_add_tail(&counter->list_entry, &ctx->counter_list);
Peter Zijlstra5c148192009-03-25 12:30:23 +0100206 else {
Ingo Molnar04289bb2008-12-11 08:38:42 +0100207 list_add_tail(&counter->list_entry, &group_leader->sibling_list);
Peter Zijlstra5c148192009-03-25 12:30:23 +0100208 group_leader->nr_siblings++;
209 }
Peter Zijlstra592903c2009-03-13 12:21:36 +0100210
211 list_add_rcu(&counter->event_entry, &ctx->event_list);
Peter Zijlstra8bc20952009-05-15 20:45:59 +0200212 ctx->nr_counters++;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100213}
214
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000215/*
216 * Remove a counter from the lists for its context.
Peter Zijlstrafccc7142009-05-23 18:28:56 +0200217 * Must be called with ctx->mutex and ctx->lock held.
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000218 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100219static void
220list_del_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
221{
222 struct perf_counter *sibling, *tmp;
223
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000224 if (list_empty(&counter->list_entry))
225 return;
Peter Zijlstra8bc20952009-05-15 20:45:59 +0200226 ctx->nr_counters--;
227
Ingo Molnar04289bb2008-12-11 08:38:42 +0100228 list_del_init(&counter->list_entry);
Peter Zijlstra592903c2009-03-13 12:21:36 +0100229 list_del_rcu(&counter->event_entry);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100230
Peter Zijlstra5c148192009-03-25 12:30:23 +0100231 if (counter->group_leader != counter)
232 counter->group_leader->nr_siblings--;
233
Ingo Molnar04289bb2008-12-11 08:38:42 +0100234 /*
235 * If this was a group counter with sibling counters then
236 * upgrade the siblings to singleton counters by adding them
237 * to the context list directly:
238 */
239 list_for_each_entry_safe(sibling, tmp,
240 &counter->sibling_list, list_entry) {
241
Peter Zijlstra75564232009-03-13 12:21:29 +0100242 list_move_tail(&sibling->list_entry, &ctx->counter_list);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100243 sibling->group_leader = sibling;
244 }
245}
246
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100247static void
248counter_sched_out(struct perf_counter *counter,
249 struct perf_cpu_context *cpuctx,
250 struct perf_counter_context *ctx)
251{
252 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
253 return;
254
255 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200256 counter->tstamp_stopped = ctx->time;
Robert Richter4aeb0b42009-04-29 12:47:03 +0200257 counter->pmu->disable(counter);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100258 counter->oncpu = -1;
259
260 if (!is_software_counter(counter))
261 cpuctx->active_oncpu--;
262 ctx->nr_active--;
263 if (counter->hw_event.exclusive || !cpuctx->active_oncpu)
264 cpuctx->exclusive = 0;
265}
266
Paul Mackerrasd859e292009-01-17 18:10:22 +1100267static void
268group_sched_out(struct perf_counter *group_counter,
269 struct perf_cpu_context *cpuctx,
270 struct perf_counter_context *ctx)
271{
272 struct perf_counter *counter;
273
274 if (group_counter->state != PERF_COUNTER_STATE_ACTIVE)
275 return;
276
277 counter_sched_out(group_counter, cpuctx, ctx);
278
279 /*
280 * Schedule out siblings (if any):
281 */
282 list_for_each_entry(counter, &group_counter->sibling_list, list_entry)
283 counter_sched_out(counter, cpuctx, ctx);
284
285 if (group_counter->hw_event.exclusive)
286 cpuctx->exclusive = 0;
287}
288
Thomas Gleixner0793a612008-12-04 20:12:29 +0100289/*
290 * Cross CPU call to remove a performance counter
291 *
292 * We disable the counter on the hardware level first. After that we
293 * remove it from the context list.
294 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100295static void __perf_counter_remove_from_context(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100296{
297 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
298 struct perf_counter *counter = info;
299 struct perf_counter_context *ctx = counter->ctx;
300
301 /*
302 * If this is a task context, we need to check whether it is
303 * the current task context of this cpu. If not it has been
304 * scheduled out before the smp call arrived.
305 */
Peter Zijlstra665c2142009-05-29 14:51:57 +0200306 if (ctx->task && cpuctx->task_ctx != ctx)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100307 return;
308
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200309 spin_lock(&ctx->lock);
Ingo Molnar34adc802009-05-20 20:13:28 +0200310 /*
311 * Protect the list operation against NMI by disabling the
312 * counters on a global level.
313 */
314 perf_disable();
Thomas Gleixner0793a612008-12-04 20:12:29 +0100315
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100316 counter_sched_out(counter, cpuctx, ctx);
317
Ingo Molnar04289bb2008-12-11 08:38:42 +0100318 list_del_counter(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100319
320 if (!ctx->task) {
321 /*
322 * Allow more per task counters with respect to the
323 * reservation:
324 */
325 cpuctx->max_pertask =
326 min(perf_max_counters - ctx->nr_counters,
327 perf_max_counters - perf_reserved_percpu);
328 }
329
Ingo Molnar34adc802009-05-20 20:13:28 +0200330 perf_enable();
Peter Zijlstra665c2142009-05-29 14:51:57 +0200331 spin_unlock(&ctx->lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100332}
333
334
335/*
336 * Remove the counter from a task's (or a CPU's) list of counters.
337 *
Peter Zijlstrafccc7142009-05-23 18:28:56 +0200338 * Must be called with ctx->mutex held.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100339 *
340 * CPU counters are removed with a smp call. For task counters we only
341 * call when the task is on a CPU.
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000342 *
343 * If counter->ctx is a cloned context, callers must make sure that
344 * every task struct that counter->ctx->task could possibly point to
345 * remains valid. This is OK when called from perf_release since
346 * that only calls us on the top-level context, which can't be a clone.
347 * When called from perf_counter_exit_task, it's OK because the
348 * context has been detached from its task.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100349 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100350static void perf_counter_remove_from_context(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100351{
352 struct perf_counter_context *ctx = counter->ctx;
353 struct task_struct *task = ctx->task;
354
355 if (!task) {
356 /*
357 * Per cpu counters are removed via an smp call and
358 * the removal is always sucessful.
359 */
360 smp_call_function_single(counter->cpu,
Ingo Molnar04289bb2008-12-11 08:38:42 +0100361 __perf_counter_remove_from_context,
Thomas Gleixner0793a612008-12-04 20:12:29 +0100362 counter, 1);
363 return;
364 }
365
366retry:
Ingo Molnar04289bb2008-12-11 08:38:42 +0100367 task_oncpu_function_call(task, __perf_counter_remove_from_context,
Thomas Gleixner0793a612008-12-04 20:12:29 +0100368 counter);
369
370 spin_lock_irq(&ctx->lock);
371 /*
372 * If the context is active we need to retry the smp call.
373 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100374 if (ctx->nr_active && !list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100375 spin_unlock_irq(&ctx->lock);
376 goto retry;
377 }
378
379 /*
380 * The lock prevents that this context is scheduled in so we
Ingo Molnar04289bb2008-12-11 08:38:42 +0100381 * can remove the counter safely, if the call above did not
Thomas Gleixner0793a612008-12-04 20:12:29 +0100382 * succeed.
383 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100384 if (!list_empty(&counter->list_entry)) {
Ingo Molnar04289bb2008-12-11 08:38:42 +0100385 list_del_counter(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100386 }
387 spin_unlock_irq(&ctx->lock);
388}
389
Peter Zijlstra4af49982009-04-06 11:45:10 +0200390static inline u64 perf_clock(void)
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100391{
Peter Zijlstra4af49982009-04-06 11:45:10 +0200392 return cpu_clock(smp_processor_id());
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100393}
394
395/*
396 * Update the record of the current time in a context.
397 */
Peter Zijlstra4af49982009-04-06 11:45:10 +0200398static void update_context_time(struct perf_counter_context *ctx)
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100399{
Peter Zijlstra4af49982009-04-06 11:45:10 +0200400 u64 now = perf_clock();
401
402 ctx->time += now - ctx->timestamp;
403 ctx->timestamp = now;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100404}
405
406/*
407 * Update the total_time_enabled and total_time_running fields for a counter.
408 */
409static void update_counter_times(struct perf_counter *counter)
410{
411 struct perf_counter_context *ctx = counter->ctx;
412 u64 run_end;
413
Peter Zijlstra4af49982009-04-06 11:45:10 +0200414 if (counter->state < PERF_COUNTER_STATE_INACTIVE)
415 return;
416
417 counter->total_time_enabled = ctx->time - counter->tstamp_enabled;
418
419 if (counter->state == PERF_COUNTER_STATE_INACTIVE)
420 run_end = counter->tstamp_stopped;
421 else
422 run_end = ctx->time;
423
424 counter->total_time_running = run_end - counter->tstamp_running;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100425}
426
427/*
428 * Update total_time_enabled and total_time_running for all counters in a group.
429 */
430static void update_group_times(struct perf_counter *leader)
431{
432 struct perf_counter *counter;
433
434 update_counter_times(leader);
435 list_for_each_entry(counter, &leader->sibling_list, list_entry)
436 update_counter_times(counter);
437}
438
439/*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100440 * Cross CPU call to disable a performance counter
441 */
442static void __perf_counter_disable(void *info)
443{
444 struct perf_counter *counter = info;
445 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
446 struct perf_counter_context *ctx = counter->ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100447
448 /*
449 * If this is a per-task counter, need to check whether this
450 * counter's task is the current task on this cpu.
451 */
Peter Zijlstra665c2142009-05-29 14:51:57 +0200452 if (ctx->task && cpuctx->task_ctx != ctx)
Paul Mackerrasd859e292009-01-17 18:10:22 +1100453 return;
454
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200455 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100456
457 /*
458 * If the counter is on, turn it off.
459 * If it is in error state, leave it in error state.
460 */
461 if (counter->state >= PERF_COUNTER_STATE_INACTIVE) {
Peter Zijlstra4af49982009-04-06 11:45:10 +0200462 update_context_time(ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100463 update_counter_times(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100464 if (counter == counter->group_leader)
465 group_sched_out(counter, cpuctx, ctx);
466 else
467 counter_sched_out(counter, cpuctx, ctx);
468 counter->state = PERF_COUNTER_STATE_OFF;
469 }
470
Peter Zijlstra665c2142009-05-29 14:51:57 +0200471 spin_unlock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100472}
473
474/*
475 * Disable a counter.
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000476 *
477 * If counter->ctx is a cloned context, callers must make sure that
478 * every task struct that counter->ctx->task could possibly point to
479 * remains valid. This condition is satisifed when called through
480 * perf_counter_for_each_child or perf_counter_for_each because they
481 * hold the top-level counter's child_mutex, so any descendant that
482 * goes to exit will block in sync_child_counter.
483 * When called from perf_pending_counter it's OK because counter->ctx
484 * is the current context on this CPU and preemption is disabled,
485 * hence we can't get into perf_counter_task_sched_out for this context.
Paul Mackerrasd859e292009-01-17 18:10:22 +1100486 */
487static void perf_counter_disable(struct perf_counter *counter)
488{
489 struct perf_counter_context *ctx = counter->ctx;
490 struct task_struct *task = ctx->task;
491
492 if (!task) {
493 /*
494 * Disable the counter on the cpu that it's on
495 */
496 smp_call_function_single(counter->cpu, __perf_counter_disable,
497 counter, 1);
498 return;
499 }
500
501 retry:
502 task_oncpu_function_call(task, __perf_counter_disable, counter);
503
504 spin_lock_irq(&ctx->lock);
505 /*
506 * If the counter is still active, we need to retry the cross-call.
507 */
508 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
509 spin_unlock_irq(&ctx->lock);
510 goto retry;
511 }
512
513 /*
514 * Since we have the lock this context can't be scheduled
515 * in, so we can change the state safely.
516 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100517 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
518 update_counter_times(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100519 counter->state = PERF_COUNTER_STATE_OFF;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100520 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100521
522 spin_unlock_irq(&ctx->lock);
523}
524
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100525static int
526counter_sched_in(struct perf_counter *counter,
527 struct perf_cpu_context *cpuctx,
528 struct perf_counter_context *ctx,
529 int cpu)
530{
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100531 if (counter->state <= PERF_COUNTER_STATE_OFF)
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100532 return 0;
533
534 counter->state = PERF_COUNTER_STATE_ACTIVE;
535 counter->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */
536 /*
537 * The new state must be visible before we turn it on in the hardware:
538 */
539 smp_wmb();
540
Robert Richter4aeb0b42009-04-29 12:47:03 +0200541 if (counter->pmu->enable(counter)) {
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100542 counter->state = PERF_COUNTER_STATE_INACTIVE;
543 counter->oncpu = -1;
544 return -EAGAIN;
545 }
546
Peter Zijlstra4af49982009-04-06 11:45:10 +0200547 counter->tstamp_running += ctx->time - counter->tstamp_stopped;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100548
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100549 if (!is_software_counter(counter))
550 cpuctx->active_oncpu++;
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100551 ctx->nr_active++;
552
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100553 if (counter->hw_event.exclusive)
554 cpuctx->exclusive = 1;
555
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100556 return 0;
557}
558
Paul Mackerras6751b712009-05-11 12:08:02 +1000559static int
560group_sched_in(struct perf_counter *group_counter,
561 struct perf_cpu_context *cpuctx,
562 struct perf_counter_context *ctx,
563 int cpu)
564{
565 struct perf_counter *counter, *partial_group;
566 int ret;
567
568 if (group_counter->state == PERF_COUNTER_STATE_OFF)
569 return 0;
570
571 ret = hw_perf_group_sched_in(group_counter, cpuctx, ctx, cpu);
572 if (ret)
573 return ret < 0 ? ret : 0;
574
575 group_counter->prev_state = group_counter->state;
576 if (counter_sched_in(group_counter, cpuctx, ctx, cpu))
577 return -EAGAIN;
578
579 /*
580 * Schedule in siblings as one group (if any):
581 */
582 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
583 counter->prev_state = counter->state;
584 if (counter_sched_in(counter, cpuctx, ctx, cpu)) {
585 partial_group = counter;
586 goto group_error;
587 }
588 }
589
590 return 0;
591
592group_error:
593 /*
594 * Groups can be scheduled in as one unit only, so undo any
595 * partial group before returning:
596 */
597 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
598 if (counter == partial_group)
599 break;
600 counter_sched_out(counter, cpuctx, ctx);
601 }
602 counter_sched_out(group_counter, cpuctx, ctx);
603
604 return -EAGAIN;
605}
606
Thomas Gleixner0793a612008-12-04 20:12:29 +0100607/*
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100608 * Return 1 for a group consisting entirely of software counters,
609 * 0 if the group contains any hardware counters.
610 */
611static int is_software_only_group(struct perf_counter *leader)
612{
613 struct perf_counter *counter;
614
615 if (!is_software_counter(leader))
616 return 0;
Peter Zijlstra5c148192009-03-25 12:30:23 +0100617
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100618 list_for_each_entry(counter, &leader->sibling_list, list_entry)
619 if (!is_software_counter(counter))
620 return 0;
Peter Zijlstra5c148192009-03-25 12:30:23 +0100621
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100622 return 1;
623}
624
625/*
626 * Work out whether we can put this counter group on the CPU now.
627 */
628static int group_can_go_on(struct perf_counter *counter,
629 struct perf_cpu_context *cpuctx,
630 int can_add_hw)
631{
632 /*
633 * Groups consisting entirely of software counters can always go on.
634 */
635 if (is_software_only_group(counter))
636 return 1;
637 /*
638 * If an exclusive group is already on, no other hardware
639 * counters can go on.
640 */
641 if (cpuctx->exclusive)
642 return 0;
643 /*
644 * If this group is exclusive and there are already
645 * counters on the CPU, it can't go on.
646 */
647 if (counter->hw_event.exclusive && cpuctx->active_oncpu)
648 return 0;
649 /*
650 * Otherwise, try to add it if all previous groups were able
651 * to go on.
652 */
653 return can_add_hw;
654}
655
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100656static void add_counter_to_ctx(struct perf_counter *counter,
657 struct perf_counter_context *ctx)
658{
659 list_add_counter(counter, ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100660 counter->prev_state = PERF_COUNTER_STATE_OFF;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200661 counter->tstamp_enabled = ctx->time;
662 counter->tstamp_running = ctx->time;
663 counter->tstamp_stopped = ctx->time;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100664}
665
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100666/*
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100667 * Cross CPU call to install and enable a performance counter
Peter Zijlstra682076a2009-05-23 18:28:57 +0200668 *
669 * Must be called with ctx->mutex held
Thomas Gleixner0793a612008-12-04 20:12:29 +0100670 */
671static void __perf_install_in_context(void *info)
672{
673 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
674 struct perf_counter *counter = info;
675 struct perf_counter_context *ctx = counter->ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100676 struct perf_counter *leader = counter->group_leader;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100677 int cpu = smp_processor_id();
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100678 int err;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100679
680 /*
681 * If this is a task context, we need to check whether it is
682 * the current task context of this cpu. If not it has been
683 * scheduled out before the smp call arrived.
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000684 * Or possibly this is the right context but it isn't
685 * on this cpu because it had no counters.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100686 */
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000687 if (ctx->task && cpuctx->task_ctx != ctx) {
Peter Zijlstra665c2142009-05-29 14:51:57 +0200688 if (cpuctx->task_ctx || ctx->task != current)
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000689 return;
690 cpuctx->task_ctx = ctx;
691 }
Thomas Gleixner0793a612008-12-04 20:12:29 +0100692
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200693 spin_lock(&ctx->lock);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000694 ctx->is_active = 1;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200695 update_context_time(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100696
697 /*
698 * Protect the list operation against NMI by disabling the
699 * counters on a global level. NOP for non NMI based counters.
700 */
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200701 perf_disable();
Thomas Gleixner0793a612008-12-04 20:12:29 +0100702
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100703 add_counter_to_ctx(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100704
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100705 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100706 * Don't put the counter on if it is disabled or if
707 * it is in a group and the group isn't on.
708 */
709 if (counter->state != PERF_COUNTER_STATE_INACTIVE ||
710 (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE))
711 goto unlock;
712
713 /*
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100714 * An exclusive counter can't go on if there are already active
715 * hardware counters, and no hardware counter can go on if there
716 * is already an exclusive counter on.
717 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100718 if (!group_can_go_on(counter, cpuctx, 1))
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100719 err = -EEXIST;
720 else
721 err = counter_sched_in(counter, cpuctx, ctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100722
Paul Mackerrasd859e292009-01-17 18:10:22 +1100723 if (err) {
724 /*
725 * This counter couldn't go on. If it is in a group
726 * then we have to pull the whole group off.
727 * If the counter group is pinned then put it in error state.
728 */
729 if (leader != counter)
730 group_sched_out(leader, cpuctx, ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100731 if (leader->hw_event.pinned) {
732 update_group_times(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100733 leader->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100734 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100735 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100736
737 if (!err && !ctx->task && cpuctx->max_pertask)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100738 cpuctx->max_pertask--;
739
Paul Mackerrasd859e292009-01-17 18:10:22 +1100740 unlock:
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200741 perf_enable();
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100742
Peter Zijlstra665c2142009-05-29 14:51:57 +0200743 spin_unlock(&ctx->lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100744}
745
746/*
747 * Attach a performance counter to a context
748 *
749 * First we add the counter to the list with the hardware enable bit
750 * in counter->hw_config cleared.
751 *
752 * If the counter is attached to a task which is on a CPU we use a smp
753 * call to enable it in the task context. The task might have been
754 * scheduled away, but we check this in the smp call again.
Paul Mackerrasd859e292009-01-17 18:10:22 +1100755 *
756 * Must be called with ctx->mutex held.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100757 */
758static void
759perf_install_in_context(struct perf_counter_context *ctx,
760 struct perf_counter *counter,
761 int cpu)
762{
763 struct task_struct *task = ctx->task;
764
Thomas Gleixner0793a612008-12-04 20:12:29 +0100765 if (!task) {
766 /*
767 * Per cpu counters are installed via an smp call and
768 * the install is always sucessful.
769 */
770 smp_call_function_single(cpu, __perf_install_in_context,
771 counter, 1);
772 return;
773 }
774
Thomas Gleixner0793a612008-12-04 20:12:29 +0100775retry:
776 task_oncpu_function_call(task, __perf_install_in_context,
777 counter);
778
779 spin_lock_irq(&ctx->lock);
780 /*
Thomas Gleixner0793a612008-12-04 20:12:29 +0100781 * we need to retry the smp call.
782 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100783 if (ctx->is_active && list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100784 spin_unlock_irq(&ctx->lock);
785 goto retry;
786 }
787
788 /*
789 * The lock prevents that this context is scheduled in so we
790 * can add the counter safely, if it the call above did not
791 * succeed.
792 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100793 if (list_empty(&counter->list_entry))
794 add_counter_to_ctx(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100795 spin_unlock_irq(&ctx->lock);
796}
797
Paul Mackerrasd859e292009-01-17 18:10:22 +1100798/*
799 * Cross CPU call to enable a performance counter
800 */
801static void __perf_counter_enable(void *info)
Ingo Molnar04289bb2008-12-11 08:38:42 +0100802{
Paul Mackerrasd859e292009-01-17 18:10:22 +1100803 struct perf_counter *counter = info;
804 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
805 struct perf_counter_context *ctx = counter->ctx;
806 struct perf_counter *leader = counter->group_leader;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100807 int err;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100808
809 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100810 * If this is a per-task counter, need to check whether this
811 * counter's task is the current task on this cpu.
Ingo Molnar04289bb2008-12-11 08:38:42 +0100812 */
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000813 if (ctx->task && cpuctx->task_ctx != ctx) {
Peter Zijlstra665c2142009-05-29 14:51:57 +0200814 if (cpuctx->task_ctx || ctx->task != current)
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000815 return;
816 cpuctx->task_ctx = ctx;
817 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100818
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200819 spin_lock(&ctx->lock);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000820 ctx->is_active = 1;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200821 update_context_time(ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100822
Paul Mackerrasc07c99b2009-02-13 22:10:34 +1100823 counter->prev_state = counter->state;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100824 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
825 goto unlock;
826 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200827 counter->tstamp_enabled = ctx->time - counter->total_time_enabled;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100828
829 /*
830 * If the counter is in a group and isn't the group leader,
831 * then don't put it on unless the group is on.
832 */
833 if (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE)
834 goto unlock;
835
Paul Mackerrase758a332009-05-12 21:59:01 +1000836 if (!group_can_go_on(counter, cpuctx, 1)) {
Paul Mackerrasd859e292009-01-17 18:10:22 +1100837 err = -EEXIST;
Paul Mackerrase758a332009-05-12 21:59:01 +1000838 } else {
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200839 perf_disable();
Paul Mackerrase758a332009-05-12 21:59:01 +1000840 if (counter == leader)
841 err = group_sched_in(counter, cpuctx, ctx,
842 smp_processor_id());
843 else
844 err = counter_sched_in(counter, cpuctx, ctx,
845 smp_processor_id());
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200846 perf_enable();
Paul Mackerrase758a332009-05-12 21:59:01 +1000847 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100848
849 if (err) {
850 /*
851 * If this counter can't go on and it's part of a
852 * group, then the whole group has to come off.
853 */
854 if (leader != counter)
855 group_sched_out(leader, cpuctx, ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100856 if (leader->hw_event.pinned) {
857 update_group_times(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100858 leader->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100859 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100860 }
861
862 unlock:
Peter Zijlstra665c2142009-05-29 14:51:57 +0200863 spin_unlock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100864}
865
866/*
867 * Enable a counter.
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000868 *
869 * If counter->ctx is a cloned context, callers must make sure that
870 * every task struct that counter->ctx->task could possibly point to
871 * remains valid. This condition is satisfied when called through
872 * perf_counter_for_each_child or perf_counter_for_each as described
873 * for perf_counter_disable.
Paul Mackerrasd859e292009-01-17 18:10:22 +1100874 */
875static void perf_counter_enable(struct perf_counter *counter)
876{
877 struct perf_counter_context *ctx = counter->ctx;
878 struct task_struct *task = ctx->task;
879
880 if (!task) {
881 /*
882 * Enable the counter on the cpu that it's on
883 */
884 smp_call_function_single(counter->cpu, __perf_counter_enable,
885 counter, 1);
886 return;
887 }
888
889 spin_lock_irq(&ctx->lock);
890 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
891 goto out;
892
893 /*
894 * If the counter is in error state, clear that first.
895 * That way, if we see the counter in error state below, we
896 * know that it has gone back into error state, as distinct
897 * from the task having been scheduled away before the
898 * cross-call arrived.
899 */
900 if (counter->state == PERF_COUNTER_STATE_ERROR)
901 counter->state = PERF_COUNTER_STATE_OFF;
902
903 retry:
904 spin_unlock_irq(&ctx->lock);
905 task_oncpu_function_call(task, __perf_counter_enable, counter);
906
907 spin_lock_irq(&ctx->lock);
908
909 /*
910 * If the context is active and the counter is still off,
911 * we need to retry the cross-call.
912 */
913 if (ctx->is_active && counter->state == PERF_COUNTER_STATE_OFF)
914 goto retry;
915
916 /*
917 * Since we have the lock this context can't be scheduled
918 * in, so we can change the state safely.
919 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100920 if (counter->state == PERF_COUNTER_STATE_OFF) {
Paul Mackerrasd859e292009-01-17 18:10:22 +1100921 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200922 counter->tstamp_enabled =
923 ctx->time - counter->total_time_enabled;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100924 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100925 out:
926 spin_unlock_irq(&ctx->lock);
927}
928
Peter Zijlstra2023b352009-05-05 17:50:26 +0200929static int perf_counter_refresh(struct perf_counter *counter, int refresh)
Peter Zijlstra79f14642009-04-06 11:45:07 +0200930{
Peter Zijlstra2023b352009-05-05 17:50:26 +0200931 /*
932 * not supported on inherited counters
933 */
934 if (counter->hw_event.inherit)
935 return -EINVAL;
936
Peter Zijlstra79f14642009-04-06 11:45:07 +0200937 atomic_add(refresh, &counter->event_limit);
938 perf_counter_enable(counter);
Peter Zijlstra2023b352009-05-05 17:50:26 +0200939
940 return 0;
Peter Zijlstra79f14642009-04-06 11:45:07 +0200941}
942
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100943void __perf_counter_sched_out(struct perf_counter_context *ctx,
944 struct perf_cpu_context *cpuctx)
945{
946 struct perf_counter *counter;
947
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100948 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100949 ctx->is_active = 0;
950 if (likely(!ctx->nr_counters))
951 goto out;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200952 update_context_time(ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100953
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200954 perf_disable();
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100955 if (ctx->nr_active) {
Peter Zijlstraafedadf2009-05-20 12:21:22 +0200956 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
957 if (counter != counter->group_leader)
958 counter_sched_out(counter, cpuctx, ctx);
959 else
960 group_sched_out(counter, cpuctx, ctx);
961 }
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100962 }
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200963 perf_enable();
Paul Mackerrasd859e292009-01-17 18:10:22 +1100964 out:
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100965 spin_unlock(&ctx->lock);
966}
967
Thomas Gleixner0793a612008-12-04 20:12:29 +0100968/*
Paul Mackerras564c2b22009-05-22 14:27:22 +1000969 * Test whether two contexts are equivalent, i.e. whether they
970 * have both been cloned from the same version of the same context
971 * and they both have the same number of enabled counters.
972 * If the number of enabled counters is the same, then the set
973 * of enabled counters should be the same, because these are both
974 * inherited contexts, therefore we can't access individual counters
975 * in them directly with an fd; we can only enable/disable all
976 * counters via prctl, or enable/disable all counters in a family
977 * via ioctl, which will have the same effect on both contexts.
978 */
979static int context_equiv(struct perf_counter_context *ctx1,
980 struct perf_counter_context *ctx2)
981{
982 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
Paul Mackerrasad3a37d2009-05-29 16:06:20 +1000983 && ctx1->parent_gen == ctx2->parent_gen
Paul Mackerras25346b932009-06-01 17:48:12 +1000984 && !ctx1->pin_count && !ctx2->pin_count;
Paul Mackerras564c2b22009-05-22 14:27:22 +1000985}
986
987/*
Thomas Gleixner0793a612008-12-04 20:12:29 +0100988 * Called from scheduler to remove the counters of the current task,
989 * with interrupts disabled.
990 *
991 * We stop each counter and update the counter value in counter->count.
992 *
Ingo Molnar76715812008-12-17 14:20:28 +0100993 * This does not protect us against NMI, but disable()
Thomas Gleixner0793a612008-12-04 20:12:29 +0100994 * sets the disabled bit in the control field of counter _before_
995 * accessing the counter control register. If a NMI hits, then it will
996 * not restart the counter.
997 */
Paul Mackerras564c2b22009-05-22 14:27:22 +1000998void perf_counter_task_sched_out(struct task_struct *task,
999 struct task_struct *next, int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001000{
1001 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001002 struct perf_counter_context *ctx = task->perf_counter_ctxp;
Paul Mackerras564c2b22009-05-22 14:27:22 +10001003 struct perf_counter_context *next_ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001004 struct perf_counter_context *parent;
Peter Zijlstra4a0deca2009-03-19 20:26:12 +01001005 struct pt_regs *regs;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001006 int do_switch = 1;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001007
Peter Zijlstra10989fb2009-05-25 14:45:28 +02001008 regs = task_pt_regs(task);
1009 perf_swcounter_event(PERF_COUNT_CONTEXT_SWITCHES, 1, 1, regs, 0);
1010
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001011 if (likely(!ctx || !cpuctx->task_ctx))
Thomas Gleixner0793a612008-12-04 20:12:29 +01001012 return;
1013
Peter Zijlstrabce379b2009-04-06 11:45:13 +02001014 update_context_time(ctx);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001015
1016 rcu_read_lock();
1017 parent = rcu_dereference(ctx->parent_ctx);
Paul Mackerras564c2b22009-05-22 14:27:22 +10001018 next_ctx = next->perf_counter_ctxp;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001019 if (parent && next_ctx &&
1020 rcu_dereference(next_ctx->parent_ctx) == parent) {
1021 /*
1022 * Looks like the two contexts are clones, so we might be
1023 * able to optimize the context switch. We lock both
1024 * contexts and check that they are clones under the
1025 * lock (including re-checking that neither has been
1026 * uncloned in the meantime). It doesn't matter which
1027 * order we take the locks because no other cpu could
1028 * be trying to lock both of these tasks.
1029 */
1030 spin_lock(&ctx->lock);
1031 spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
1032 if (context_equiv(ctx, next_ctx)) {
Peter Zijlstra665c2142009-05-29 14:51:57 +02001033 /*
1034 * XXX do we need a memory barrier of sorts
1035 * wrt to rcu_dereference() of perf_counter_ctxp
1036 */
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001037 task->perf_counter_ctxp = next_ctx;
1038 next->perf_counter_ctxp = ctx;
1039 ctx->task = next;
1040 next_ctx->task = task;
1041 do_switch = 0;
1042 }
1043 spin_unlock(&next_ctx->lock);
1044 spin_unlock(&ctx->lock);
Paul Mackerras564c2b22009-05-22 14:27:22 +10001045 }
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001046 rcu_read_unlock();
Paul Mackerras564c2b22009-05-22 14:27:22 +10001047
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001048 if (do_switch) {
1049 __perf_counter_sched_out(ctx, cpuctx);
1050 cpuctx->task_ctx = NULL;
1051 }
Thomas Gleixner0793a612008-12-04 20:12:29 +01001052}
1053
Peter Zijlstra665c2142009-05-29 14:51:57 +02001054/*
1055 * Called with IRQs disabled
1056 */
Paul Mackerrasa08b1592009-05-11 15:46:10 +10001057static void __perf_counter_task_sched_out(struct perf_counter_context *ctx)
1058{
1059 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1060
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001061 if (!cpuctx->task_ctx)
1062 return;
Ingo Molnar012b84d2009-05-17 11:08:41 +02001063
1064 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
1065 return;
1066
Paul Mackerrasa08b1592009-05-11 15:46:10 +10001067 __perf_counter_sched_out(ctx, cpuctx);
1068 cpuctx->task_ctx = NULL;
1069}
1070
Peter Zijlstra665c2142009-05-29 14:51:57 +02001071/*
1072 * Called with IRQs disabled
1073 */
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001074static void perf_counter_cpu_sched_out(struct perf_cpu_context *cpuctx)
Ingo Molnar04289bb2008-12-11 08:38:42 +01001075{
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001076 __perf_counter_sched_out(&cpuctx->ctx, cpuctx);
Ingo Molnar04289bb2008-12-11 08:38:42 +01001077}
1078
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001079static void
1080__perf_counter_sched_in(struct perf_counter_context *ctx,
1081 struct perf_cpu_context *cpuctx, int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001082{
Thomas Gleixner0793a612008-12-04 20:12:29 +01001083 struct perf_counter *counter;
Paul Mackerrasdd0e6ba2009-01-12 15:11:00 +11001084 int can_add_hw = 1;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001085
Thomas Gleixner0793a612008-12-04 20:12:29 +01001086 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001087 ctx->is_active = 1;
1088 if (likely(!ctx->nr_counters))
1089 goto out;
1090
Peter Zijlstra4af49982009-04-06 11:45:10 +02001091 ctx->timestamp = perf_clock();
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001092
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001093 perf_disable();
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001094
1095 /*
1096 * First go through the list and put on any pinned groups
1097 * in order to give them the best chance of going on.
1098 */
Ingo Molnar04289bb2008-12-11 08:38:42 +01001099 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001100 if (counter->state <= PERF_COUNTER_STATE_OFF ||
1101 !counter->hw_event.pinned)
1102 continue;
1103 if (counter->cpu != -1 && counter->cpu != cpu)
1104 continue;
1105
Peter Zijlstraafedadf2009-05-20 12:21:22 +02001106 if (counter != counter->group_leader)
1107 counter_sched_in(counter, cpuctx, ctx, cpu);
1108 else {
1109 if (group_can_go_on(counter, cpuctx, 1))
1110 group_sched_in(counter, cpuctx, ctx, cpu);
1111 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001112
1113 /*
1114 * If this pinned group hasn't been scheduled,
1115 * put it in error state.
1116 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001117 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
1118 update_group_times(counter);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001119 counter->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001120 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001121 }
1122
1123 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1124 /*
1125 * Ignore counters in OFF or ERROR state, and
1126 * ignore pinned counters since we did them already.
1127 */
1128 if (counter->state <= PERF_COUNTER_STATE_OFF ||
1129 counter->hw_event.pinned)
1130 continue;
1131
Ingo Molnar04289bb2008-12-11 08:38:42 +01001132 /*
1133 * Listen to the 'cpu' scheduling filter constraint
1134 * of counters:
1135 */
Thomas Gleixner0793a612008-12-04 20:12:29 +01001136 if (counter->cpu != -1 && counter->cpu != cpu)
1137 continue;
1138
Peter Zijlstraafedadf2009-05-20 12:21:22 +02001139 if (counter != counter->group_leader) {
1140 if (counter_sched_in(counter, cpuctx, ctx, cpu))
Paul Mackerrasdd0e6ba2009-01-12 15:11:00 +11001141 can_add_hw = 0;
Peter Zijlstraafedadf2009-05-20 12:21:22 +02001142 } else {
1143 if (group_can_go_on(counter, cpuctx, can_add_hw)) {
1144 if (group_sched_in(counter, cpuctx, ctx, cpu))
1145 can_add_hw = 0;
1146 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001147 }
Thomas Gleixner0793a612008-12-04 20:12:29 +01001148 }
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001149 perf_enable();
Paul Mackerrasd859e292009-01-17 18:10:22 +11001150 out:
Thomas Gleixner0793a612008-12-04 20:12:29 +01001151 spin_unlock(&ctx->lock);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001152}
Ingo Molnar04289bb2008-12-11 08:38:42 +01001153
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001154/*
1155 * Called from scheduler to add the counters of the current task
1156 * with interrupts disabled.
1157 *
1158 * We restore the counter value and then enable it.
1159 *
1160 * This does not protect us against NMI, but enable()
1161 * sets the enabled bit in the control field of counter _before_
1162 * accessing the counter control register. If a NMI hits, then it will
1163 * keep the counter running.
1164 */
1165void perf_counter_task_sched_in(struct task_struct *task, int cpu)
1166{
1167 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001168 struct perf_counter_context *ctx = task->perf_counter_ctxp;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001169
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001170 if (likely(!ctx))
1171 return;
Paul Mackerras564c2b22009-05-22 14:27:22 +10001172 if (cpuctx->task_ctx == ctx)
1173 return;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001174 __perf_counter_sched_in(ctx, cpuctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001175 cpuctx->task_ctx = ctx;
1176}
1177
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001178static void perf_counter_cpu_sched_in(struct perf_cpu_context *cpuctx, int cpu)
1179{
1180 struct perf_counter_context *ctx = &cpuctx->ctx;
1181
1182 __perf_counter_sched_in(ctx, cpuctx, cpu);
1183}
1184
Peter Zijlstraa78ac322009-05-25 17:39:05 +02001185#define MAX_INTERRUPTS (~0ULL)
1186
1187static void perf_log_throttle(struct perf_counter *counter, int enable);
Peter Zijlstra26b119b2009-05-20 12:21:20 +02001188static void perf_log_period(struct perf_counter *counter, u64 period);
1189
1190static void perf_adjust_freq(struct perf_counter_context *ctx)
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001191{
1192 struct perf_counter *counter;
Peter Zijlstraa78ac322009-05-25 17:39:05 +02001193 u64 interrupts, irq_period;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001194 u64 events, period;
1195 s64 delta;
1196
1197 spin_lock(&ctx->lock);
1198 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1199 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
1200 continue;
1201
Peter Zijlstraa78ac322009-05-25 17:39:05 +02001202 interrupts = counter->hw.interrupts;
1203 counter->hw.interrupts = 0;
1204
1205 if (interrupts == MAX_INTERRUPTS) {
1206 perf_log_throttle(counter, 1);
1207 counter->pmu->unthrottle(counter);
1208 interrupts = 2*sysctl_perf_counter_limit/HZ;
1209 }
1210
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001211 if (!counter->hw_event.freq || !counter->hw_event.irq_freq)
1212 continue;
1213
Peter Zijlstraa78ac322009-05-25 17:39:05 +02001214 events = HZ * interrupts * counter->hw.irq_period;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001215 period = div64_u64(events, counter->hw_event.irq_freq);
1216
1217 delta = (s64)(1 + period - counter->hw.irq_period);
1218 delta >>= 1;
1219
1220 irq_period = counter->hw.irq_period + delta;
1221
1222 if (!irq_period)
1223 irq_period = 1;
1224
Peter Zijlstra26b119b2009-05-20 12:21:20 +02001225 perf_log_period(counter, irq_period);
1226
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001227 counter->hw.irq_period = irq_period;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001228 }
1229 spin_unlock(&ctx->lock);
1230}
1231
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001232/*
1233 * Round-robin a context's counters:
1234 */
1235static void rotate_ctx(struct perf_counter_context *ctx)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001236{
Thomas Gleixner0793a612008-12-04 20:12:29 +01001237 struct perf_counter *counter;
1238
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001239 if (!ctx->nr_counters)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001240 return;
1241
Thomas Gleixner0793a612008-12-04 20:12:29 +01001242 spin_lock(&ctx->lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001243 /*
Ingo Molnar04289bb2008-12-11 08:38:42 +01001244 * Rotate the first entry last (works just fine for group counters too):
Thomas Gleixner0793a612008-12-04 20:12:29 +01001245 */
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001246 perf_disable();
Ingo Molnar04289bb2008-12-11 08:38:42 +01001247 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Peter Zijlstra75564232009-03-13 12:21:29 +01001248 list_move_tail(&counter->list_entry, &ctx->counter_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001249 break;
1250 }
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001251 perf_enable();
Thomas Gleixner0793a612008-12-04 20:12:29 +01001252
1253 spin_unlock(&ctx->lock);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001254}
Thomas Gleixner0793a612008-12-04 20:12:29 +01001255
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001256void perf_counter_task_tick(struct task_struct *curr, int cpu)
1257{
Peter Zijlstra7fc23a52009-05-08 18:52:21 +02001258 struct perf_cpu_context *cpuctx;
1259 struct perf_counter_context *ctx;
1260
1261 if (!atomic_read(&nr_counters))
1262 return;
1263
1264 cpuctx = &per_cpu(perf_cpu_context, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001265 ctx = curr->perf_counter_ctxp;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001266
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001267 perf_adjust_freq(&cpuctx->ctx);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001268 if (ctx)
1269 perf_adjust_freq(ctx);
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001270
Ingo Molnarb82914c2009-05-04 18:54:32 +02001271 perf_counter_cpu_sched_out(cpuctx);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001272 if (ctx)
1273 __perf_counter_task_sched_out(ctx);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001274
Ingo Molnarb82914c2009-05-04 18:54:32 +02001275 rotate_ctx(&cpuctx->ctx);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001276 if (ctx)
1277 rotate_ctx(ctx);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001278
Ingo Molnarb82914c2009-05-04 18:54:32 +02001279 perf_counter_cpu_sched_in(cpuctx, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001280 if (ctx)
1281 perf_counter_task_sched_in(curr, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001282}
1283
1284/*
Thomas Gleixner0793a612008-12-04 20:12:29 +01001285 * Cross CPU call to read the hardware counter
1286 */
Ingo Molnar76715812008-12-17 14:20:28 +01001287static void __read(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001288{
Ingo Molnar621a01e2008-12-11 12:46:46 +01001289 struct perf_counter *counter = info;
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001290 struct perf_counter_context *ctx = counter->ctx;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001291 unsigned long flags;
Ingo Molnar621a01e2008-12-11 12:46:46 +01001292
Peter Zijlstra849691a2009-04-06 11:45:12 +02001293 local_irq_save(flags);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001294 if (ctx->is_active)
Peter Zijlstra4af49982009-04-06 11:45:10 +02001295 update_context_time(ctx);
Robert Richter4aeb0b42009-04-29 12:47:03 +02001296 counter->pmu->read(counter);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001297 update_counter_times(counter);
Peter Zijlstra849691a2009-04-06 11:45:12 +02001298 local_irq_restore(flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001299}
1300
Ingo Molnar04289bb2008-12-11 08:38:42 +01001301static u64 perf_counter_read(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001302{
1303 /*
1304 * If counter is enabled and currently active on a CPU, update the
1305 * value in the counter structure:
1306 */
Ingo Molnar6a930702008-12-11 15:17:03 +01001307 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
Thomas Gleixner0793a612008-12-04 20:12:29 +01001308 smp_call_function_single(counter->oncpu,
Ingo Molnar76715812008-12-17 14:20:28 +01001309 __read, counter, 1);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001310 } else if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
1311 update_counter_times(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001312 }
1313
Ingo Molnaree060942008-12-13 09:00:03 +01001314 return atomic64_read(&counter->count);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001315}
1316
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001317/*
1318 * Initialize the perf_counter context in a task_struct:
1319 */
1320static void
1321__perf_counter_init_context(struct perf_counter_context *ctx,
1322 struct task_struct *task)
1323{
1324 memset(ctx, 0, sizeof(*ctx));
1325 spin_lock_init(&ctx->lock);
1326 mutex_init(&ctx->mutex);
1327 INIT_LIST_HEAD(&ctx->counter_list);
1328 INIT_LIST_HEAD(&ctx->event_list);
1329 atomic_set(&ctx->refcount, 1);
1330 ctx->task = task;
1331}
1332
Thomas Gleixner0793a612008-12-04 20:12:29 +01001333static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
1334{
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001335 struct perf_counter_context *parent_ctx;
Ingo Molnar22a4f652009-06-01 10:13:37 +02001336 struct perf_counter_context *ctx;
1337 struct perf_cpu_context *cpuctx;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001338 struct task_struct *task;
Paul Mackerras25346b932009-06-01 17:48:12 +10001339 unsigned long flags;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001340 int err;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001341
1342 /*
1343 * If cpu is not a wildcard then this is a percpu counter:
1344 */
1345 if (cpu != -1) {
1346 /* Must be root to operate on a CPU counter: */
Peter Zijlstra1ccd1542009-04-09 10:53:45 +02001347 if (sysctl_perf_counter_priv && !capable(CAP_SYS_ADMIN))
Thomas Gleixner0793a612008-12-04 20:12:29 +01001348 return ERR_PTR(-EACCES);
1349
1350 if (cpu < 0 || cpu > num_possible_cpus())
1351 return ERR_PTR(-EINVAL);
1352
1353 /*
1354 * We could be clever and allow to attach a counter to an
1355 * offline CPU and activate it when the CPU comes up, but
1356 * that's for later.
1357 */
1358 if (!cpu_isset(cpu, cpu_online_map))
1359 return ERR_PTR(-ENODEV);
1360
1361 cpuctx = &per_cpu(perf_cpu_context, cpu);
1362 ctx = &cpuctx->ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001363 get_ctx(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001364
Thomas Gleixner0793a612008-12-04 20:12:29 +01001365 return ctx;
1366 }
1367
1368 rcu_read_lock();
1369 if (!pid)
1370 task = current;
1371 else
1372 task = find_task_by_vpid(pid);
1373 if (task)
1374 get_task_struct(task);
1375 rcu_read_unlock();
1376
1377 if (!task)
1378 return ERR_PTR(-ESRCH);
1379
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001380 /*
1381 * Can't attach counters to a dying task.
1382 */
1383 err = -ESRCH;
1384 if (task->flags & PF_EXITING)
1385 goto errout;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001386
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001387 /* Reuse ptrace permission checks for now. */
1388 err = -EACCES;
1389 if (!ptrace_may_access(task, PTRACE_MODE_READ))
1390 goto errout;
1391
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001392 retry:
Paul Mackerras25346b932009-06-01 17:48:12 +10001393 ctx = perf_lock_task_context(task, &flags);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001394 if (ctx) {
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001395 parent_ctx = ctx->parent_ctx;
1396 if (parent_ctx) {
1397 put_ctx(parent_ctx);
1398 ctx->parent_ctx = NULL; /* no longer a clone */
1399 }
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001400 /*
1401 * Get an extra reference before dropping the lock so that
1402 * this context won't get freed if the task exits.
1403 */
1404 get_ctx(ctx);
Paul Mackerras25346b932009-06-01 17:48:12 +10001405 spin_unlock_irqrestore(&ctx->lock, flags);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001406 }
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001407
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001408 if (!ctx) {
1409 ctx = kmalloc(sizeof(struct perf_counter_context), GFP_KERNEL);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001410 err = -ENOMEM;
1411 if (!ctx)
1412 goto errout;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001413 __perf_counter_init_context(ctx, task);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001414 get_ctx(ctx);
1415 if (cmpxchg(&task->perf_counter_ctxp, NULL, ctx)) {
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001416 /*
1417 * We raced with some other task; use
1418 * the context they set.
1419 */
1420 kfree(ctx);
Paul Mackerras25346b932009-06-01 17:48:12 +10001421 goto retry;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001422 }
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001423 get_task_struct(task);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001424 }
1425
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001426 put_task_struct(task);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001427 return ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001428
1429 errout:
1430 put_task_struct(task);
1431 return ERR_PTR(err);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001432}
1433
Peter Zijlstra592903c2009-03-13 12:21:36 +01001434static void free_counter_rcu(struct rcu_head *head)
1435{
1436 struct perf_counter *counter;
1437
1438 counter = container_of(head, struct perf_counter, rcu_head);
1439 kfree(counter);
1440}
1441
Peter Zijlstra925d5192009-03-30 19:07:02 +02001442static void perf_pending_sync(struct perf_counter *counter);
1443
Peter Zijlstraf1600952009-03-19 20:26:16 +01001444static void free_counter(struct perf_counter *counter)
1445{
Peter Zijlstra925d5192009-03-30 19:07:02 +02001446 perf_pending_sync(counter);
1447
Peter Zijlstra7fc23a52009-05-08 18:52:21 +02001448 atomic_dec(&nr_counters);
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02001449 if (counter->hw_event.mmap)
1450 atomic_dec(&nr_mmap_tracking);
1451 if (counter->hw_event.munmap)
1452 atomic_dec(&nr_munmap_tracking);
1453 if (counter->hw_event.comm)
1454 atomic_dec(&nr_comm_tracking);
1455
Peter Zijlstrae077df42009-03-19 20:26:17 +01001456 if (counter->destroy)
1457 counter->destroy(counter);
1458
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001459 put_ctx(counter->ctx);
Peter Zijlstraf1600952009-03-19 20:26:16 +01001460 call_rcu(&counter->rcu_head, free_counter_rcu);
1461}
1462
Thomas Gleixner0793a612008-12-04 20:12:29 +01001463/*
1464 * Called when the last reference to the file is gone.
1465 */
1466static int perf_release(struct inode *inode, struct file *file)
1467{
1468 struct perf_counter *counter = file->private_data;
1469 struct perf_counter_context *ctx = counter->ctx;
1470
1471 file->private_data = NULL;
1472
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001473 WARN_ON_ONCE(ctx->parent_ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001474 mutex_lock(&ctx->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01001475 perf_counter_remove_from_context(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001476 mutex_unlock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001477
Peter Zijlstra082ff5a2009-05-23 18:29:00 +02001478 mutex_lock(&counter->owner->perf_counter_mutex);
1479 list_del_init(&counter->owner_entry);
1480 mutex_unlock(&counter->owner->perf_counter_mutex);
1481 put_task_struct(counter->owner);
1482
Peter Zijlstraf1600952009-03-19 20:26:16 +01001483 free_counter(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001484
1485 return 0;
1486}
1487
1488/*
1489 * Read the performance counter - simple non blocking version for now
1490 */
1491static ssize_t
1492perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
1493{
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001494 u64 values[3];
1495 int n;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001496
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001497 /*
1498 * Return end-of-file for a read on a counter that is in
1499 * error state (i.e. because it was pinned but it couldn't be
1500 * scheduled on to the CPU at some point).
1501 */
1502 if (counter->state == PERF_COUNTER_STATE_ERROR)
1503 return 0;
1504
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001505 WARN_ON_ONCE(counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001506 mutex_lock(&counter->child_mutex);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001507 values[0] = perf_counter_read(counter);
1508 n = 1;
1509 if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1510 values[n++] = counter->total_time_enabled +
1511 atomic64_read(&counter->child_total_time_enabled);
1512 if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1513 values[n++] = counter->total_time_running +
1514 atomic64_read(&counter->child_total_time_running);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001515 mutex_unlock(&counter->child_mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001516
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001517 if (count < n * sizeof(u64))
1518 return -EINVAL;
1519 count = n * sizeof(u64);
1520
1521 if (copy_to_user(buf, values, count))
1522 return -EFAULT;
1523
1524 return count;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001525}
1526
1527static ssize_t
Thomas Gleixner0793a612008-12-04 20:12:29 +01001528perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
1529{
1530 struct perf_counter *counter = file->private_data;
1531
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001532 return perf_read_hw(counter, buf, count);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001533}
1534
1535static unsigned int perf_poll(struct file *file, poll_table *wait)
1536{
1537 struct perf_counter *counter = file->private_data;
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001538 struct perf_mmap_data *data;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001539 unsigned int events = POLL_HUP;
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001540
1541 rcu_read_lock();
1542 data = rcu_dereference(counter->data);
1543 if (data)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001544 events = atomic_xchg(&data->poll, 0);
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001545 rcu_read_unlock();
Thomas Gleixner0793a612008-12-04 20:12:29 +01001546
1547 poll_wait(file, &counter->waitq, wait);
1548
Thomas Gleixner0793a612008-12-04 20:12:29 +01001549 return events;
1550}
1551
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001552static void perf_counter_reset(struct perf_counter *counter)
1553{
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001554 (void)perf_counter_read(counter);
Paul Mackerras615a3f12009-05-11 15:50:21 +10001555 atomic64_set(&counter->count, 0);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001556 perf_counter_update_userpage(counter);
1557}
1558
1559static void perf_counter_for_each_sibling(struct perf_counter *counter,
1560 void (*func)(struct perf_counter *))
1561{
1562 struct perf_counter_context *ctx = counter->ctx;
1563 struct perf_counter *sibling;
1564
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001565 WARN_ON_ONCE(ctx->parent_ctx);
Peter Zijlstra682076a2009-05-23 18:28:57 +02001566 mutex_lock(&ctx->mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001567 counter = counter->group_leader;
1568
1569 func(counter);
1570 list_for_each_entry(sibling, &counter->sibling_list, list_entry)
1571 func(sibling);
Peter Zijlstra682076a2009-05-23 18:28:57 +02001572 mutex_unlock(&ctx->mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001573}
1574
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001575/*
1576 * Holding the top-level counter's child_mutex means that any
1577 * descendant process that has inherited this counter will block
1578 * in sync_child_counter if it goes to exit, thus satisfying the
1579 * task existence requirements of perf_counter_enable/disable.
1580 */
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001581static void perf_counter_for_each_child(struct perf_counter *counter,
1582 void (*func)(struct perf_counter *))
1583{
1584 struct perf_counter *child;
1585
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001586 WARN_ON_ONCE(counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001587 mutex_lock(&counter->child_mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001588 func(counter);
1589 list_for_each_entry(child, &counter->child_list, child_list)
1590 func(child);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001591 mutex_unlock(&counter->child_mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001592}
1593
1594static void perf_counter_for_each(struct perf_counter *counter,
1595 void (*func)(struct perf_counter *))
1596{
1597 struct perf_counter *child;
1598
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001599 WARN_ON_ONCE(counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001600 mutex_lock(&counter->child_mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001601 perf_counter_for_each_sibling(counter, func);
1602 list_for_each_entry(child, &counter->child_list, child_list)
1603 perf_counter_for_each_sibling(child, func);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001604 mutex_unlock(&counter->child_mutex);
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001605}
1606
Paul Mackerrasd859e292009-01-17 18:10:22 +11001607static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1608{
1609 struct perf_counter *counter = file->private_data;
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001610 void (*func)(struct perf_counter *);
1611 u32 flags = arg;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001612
1613 switch (cmd) {
1614 case PERF_COUNTER_IOC_ENABLE:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001615 func = perf_counter_enable;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001616 break;
1617 case PERF_COUNTER_IOC_DISABLE:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001618 func = perf_counter_disable;
Peter Zijlstra79f14642009-04-06 11:45:07 +02001619 break;
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001620 case PERF_COUNTER_IOC_RESET:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001621 func = perf_counter_reset;
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001622 break;
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001623
1624 case PERF_COUNTER_IOC_REFRESH:
1625 return perf_counter_refresh(counter, arg);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001626 default:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001627 return -ENOTTY;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001628 }
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001629
1630 if (flags & PERF_IOC_FLAG_GROUP)
1631 perf_counter_for_each(counter, func);
1632 else
1633 perf_counter_for_each_child(counter, func);
1634
1635 return 0;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001636}
1637
Peter Zijlstra771d7cd2009-05-25 14:45:26 +02001638int perf_counter_task_enable(void)
1639{
1640 struct perf_counter *counter;
1641
1642 mutex_lock(&current->perf_counter_mutex);
1643 list_for_each_entry(counter, &current->perf_counter_list, owner_entry)
1644 perf_counter_for_each_child(counter, perf_counter_enable);
1645 mutex_unlock(&current->perf_counter_mutex);
1646
1647 return 0;
1648}
1649
1650int perf_counter_task_disable(void)
1651{
1652 struct perf_counter *counter;
1653
1654 mutex_lock(&current->perf_counter_mutex);
1655 list_for_each_entry(counter, &current->perf_counter_list, owner_entry)
1656 perf_counter_for_each_child(counter, perf_counter_disable);
1657 mutex_unlock(&current->perf_counter_mutex);
1658
1659 return 0;
1660}
1661
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001662/*
1663 * Callers need to ensure there can be no nesting of this function, otherwise
1664 * the seqlock logic goes bad. We can not serialize this because the arch
1665 * code calls this from NMI context.
1666 */
1667void perf_counter_update_userpage(struct perf_counter *counter)
Paul Mackerras37d81822009-03-23 18:22:08 +01001668{
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001669 struct perf_counter_mmap_page *userpg;
Ingo Molnar22a4f652009-06-01 10:13:37 +02001670 struct perf_mmap_data *data;
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001671
1672 rcu_read_lock();
1673 data = rcu_dereference(counter->data);
1674 if (!data)
1675 goto unlock;
1676
1677 userpg = data->user_page;
Paul Mackerras37d81822009-03-23 18:22:08 +01001678
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001679 /*
1680 * Disable preemption so as to not let the corresponding user-space
1681 * spin too long if we get preempted.
1682 */
1683 preempt_disable();
Paul Mackerras37d81822009-03-23 18:22:08 +01001684 ++userpg->lock;
Peter Zijlstra92f22a32009-04-02 11:12:04 +02001685 barrier();
Paul Mackerras37d81822009-03-23 18:22:08 +01001686 userpg->index = counter->hw.idx;
1687 userpg->offset = atomic64_read(&counter->count);
1688 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
1689 userpg->offset -= atomic64_read(&counter->hw.prev_count);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001690
Peter Zijlstra92f22a32009-04-02 11:12:04 +02001691 barrier();
Paul Mackerras37d81822009-03-23 18:22:08 +01001692 ++userpg->lock;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001693 preempt_enable();
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001694unlock:
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001695 rcu_read_unlock();
Paul Mackerras37d81822009-03-23 18:22:08 +01001696}
1697
1698static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1699{
1700 struct perf_counter *counter = vma->vm_file->private_data;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001701 struct perf_mmap_data *data;
1702 int ret = VM_FAULT_SIGBUS;
Paul Mackerras37d81822009-03-23 18:22:08 +01001703
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001704 rcu_read_lock();
1705 data = rcu_dereference(counter->data);
1706 if (!data)
1707 goto unlock;
Paul Mackerras37d81822009-03-23 18:22:08 +01001708
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001709 if (vmf->pgoff == 0) {
1710 vmf->page = virt_to_page(data->user_page);
1711 } else {
1712 int nr = vmf->pgoff - 1;
1713
1714 if ((unsigned)nr > data->nr_pages)
1715 goto unlock;
1716
1717 vmf->page = virt_to_page(data->data_pages[nr]);
1718 }
Paul Mackerras37d81822009-03-23 18:22:08 +01001719 get_page(vmf->page);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001720 ret = 0;
1721unlock:
1722 rcu_read_unlock();
1723
1724 return ret;
1725}
1726
1727static int perf_mmap_data_alloc(struct perf_counter *counter, int nr_pages)
1728{
1729 struct perf_mmap_data *data;
1730 unsigned long size;
1731 int i;
1732
1733 WARN_ON(atomic_read(&counter->mmap_count));
1734
1735 size = sizeof(struct perf_mmap_data);
1736 size += nr_pages * sizeof(void *);
1737
1738 data = kzalloc(size, GFP_KERNEL);
1739 if (!data)
1740 goto fail;
1741
1742 data->user_page = (void *)get_zeroed_page(GFP_KERNEL);
1743 if (!data->user_page)
1744 goto fail_user_page;
1745
1746 for (i = 0; i < nr_pages; i++) {
1747 data->data_pages[i] = (void *)get_zeroed_page(GFP_KERNEL);
1748 if (!data->data_pages[i])
1749 goto fail_data_pages;
1750 }
1751
1752 data->nr_pages = nr_pages;
Peter Zijlstra22c15582009-05-05 17:50:25 +02001753 atomic_set(&data->lock, -1);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001754
1755 rcu_assign_pointer(counter->data, data);
1756
Paul Mackerras37d81822009-03-23 18:22:08 +01001757 return 0;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001758
1759fail_data_pages:
1760 for (i--; i >= 0; i--)
1761 free_page((unsigned long)data->data_pages[i]);
1762
1763 free_page((unsigned long)data->user_page);
1764
1765fail_user_page:
1766 kfree(data);
1767
1768fail:
1769 return -ENOMEM;
1770}
1771
1772static void __perf_mmap_data_free(struct rcu_head *rcu_head)
1773{
Ingo Molnar22a4f652009-06-01 10:13:37 +02001774 struct perf_mmap_data *data;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001775 int i;
1776
Ingo Molnar22a4f652009-06-01 10:13:37 +02001777 data = container_of(rcu_head, struct perf_mmap_data, rcu_head);
1778
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001779 free_page((unsigned long)data->user_page);
1780 for (i = 0; i < data->nr_pages; i++)
1781 free_page((unsigned long)data->data_pages[i]);
1782 kfree(data);
1783}
1784
1785static void perf_mmap_data_free(struct perf_counter *counter)
1786{
1787 struct perf_mmap_data *data = counter->data;
1788
1789 WARN_ON(atomic_read(&counter->mmap_count));
1790
1791 rcu_assign_pointer(counter->data, NULL);
1792 call_rcu(&data->rcu_head, __perf_mmap_data_free);
1793}
1794
1795static void perf_mmap_open(struct vm_area_struct *vma)
1796{
1797 struct perf_counter *counter = vma->vm_file->private_data;
1798
1799 atomic_inc(&counter->mmap_count);
1800}
1801
1802static void perf_mmap_close(struct vm_area_struct *vma)
1803{
1804 struct perf_counter *counter = vma->vm_file->private_data;
1805
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001806 WARN_ON_ONCE(counter->ctx->parent_ctx);
Ingo Molnar22a4f652009-06-01 10:13:37 +02001807 if (atomic_dec_and_mutex_lock(&counter->mmap_count, &counter->mmap_mutex)) {
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001808 struct user_struct *user = current_user();
1809
1810 atomic_long_sub(counter->data->nr_pages + 1, &user->locked_vm);
Peter Zijlstrac5078f72009-05-05 17:50:24 +02001811 vma->vm_mm->locked_vm -= counter->data->nr_locked;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001812 perf_mmap_data_free(counter);
1813 mutex_unlock(&counter->mmap_mutex);
1814 }
Paul Mackerras37d81822009-03-23 18:22:08 +01001815}
1816
1817static struct vm_operations_struct perf_mmap_vmops = {
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001818 .open = perf_mmap_open,
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001819 .close = perf_mmap_close,
Paul Mackerras37d81822009-03-23 18:22:08 +01001820 .fault = perf_mmap_fault,
1821};
1822
1823static int perf_mmap(struct file *file, struct vm_area_struct *vma)
1824{
1825 struct perf_counter *counter = file->private_data;
Ingo Molnar22a4f652009-06-01 10:13:37 +02001826 unsigned long user_locked, user_lock_limit;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001827 struct user_struct *user = current_user();
Ingo Molnar22a4f652009-06-01 10:13:37 +02001828 unsigned long locked, lock_limit;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001829 unsigned long vma_size;
1830 unsigned long nr_pages;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001831 long user_extra, extra;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001832 int ret = 0;
Paul Mackerras37d81822009-03-23 18:22:08 +01001833
1834 if (!(vma->vm_flags & VM_SHARED) || (vma->vm_flags & VM_WRITE))
1835 return -EINVAL;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001836
1837 vma_size = vma->vm_end - vma->vm_start;
1838 nr_pages = (vma_size / PAGE_SIZE) - 1;
1839
Peter Zijlstra7730d862009-03-25 12:48:31 +01001840 /*
1841 * If we have data pages ensure they're a power-of-two number, so we
1842 * can do bitmasks instead of modulo.
1843 */
1844 if (nr_pages != 0 && !is_power_of_2(nr_pages))
Paul Mackerras37d81822009-03-23 18:22:08 +01001845 return -EINVAL;
1846
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001847 if (vma_size != PAGE_SIZE * (1 + nr_pages))
Paul Mackerras37d81822009-03-23 18:22:08 +01001848 return -EINVAL;
1849
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001850 if (vma->vm_pgoff != 0)
1851 return -EINVAL;
Paul Mackerras37d81822009-03-23 18:22:08 +01001852
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001853 WARN_ON_ONCE(counter->ctx->parent_ctx);
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001854 mutex_lock(&counter->mmap_mutex);
1855 if (atomic_inc_not_zero(&counter->mmap_count)) {
1856 if (nr_pages != counter->data->nr_pages)
1857 ret = -EINVAL;
1858 goto unlock;
1859 }
1860
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001861 user_extra = nr_pages + 1;
1862 user_lock_limit = sysctl_perf_counter_mlock >> (PAGE_SHIFT - 10);
Ingo Molnara3862d32009-05-24 09:02:37 +02001863
1864 /*
1865 * Increase the limit linearly with more CPUs:
1866 */
1867 user_lock_limit *= num_online_cpus();
1868
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001869 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
Peter Zijlstrac5078f72009-05-05 17:50:24 +02001870
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001871 extra = 0;
1872 if (user_locked > user_lock_limit)
1873 extra = user_locked - user_lock_limit;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001874
1875 lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
1876 lock_limit >>= PAGE_SHIFT;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001877 locked = vma->vm_mm->locked_vm + extra;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001878
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001879 if ((locked > lock_limit) && !capable(CAP_IPC_LOCK)) {
1880 ret = -EPERM;
1881 goto unlock;
1882 }
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001883
1884 WARN_ON(counter->data);
1885 ret = perf_mmap_data_alloc(counter, nr_pages);
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001886 if (ret)
1887 goto unlock;
1888
1889 atomic_set(&counter->mmap_count, 1);
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001890 atomic_long_add(user_extra, &user->locked_vm);
Peter Zijlstrac5078f72009-05-05 17:50:24 +02001891 vma->vm_mm->locked_vm += extra;
1892 counter->data->nr_locked = extra;
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001893unlock:
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001894 mutex_unlock(&counter->mmap_mutex);
Paul Mackerras37d81822009-03-23 18:22:08 +01001895
1896 vma->vm_flags &= ~VM_MAYWRITE;
1897 vma->vm_flags |= VM_RESERVED;
1898 vma->vm_ops = &perf_mmap_vmops;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001899
1900 return ret;
Paul Mackerras37d81822009-03-23 18:22:08 +01001901}
1902
Peter Zijlstra3c446b3d2009-04-06 11:45:01 +02001903static int perf_fasync(int fd, struct file *filp, int on)
1904{
Peter Zijlstra3c446b3d2009-04-06 11:45:01 +02001905 struct inode *inode = filp->f_path.dentry->d_inode;
Ingo Molnar22a4f652009-06-01 10:13:37 +02001906 struct perf_counter *counter = filp->private_data;
Peter Zijlstra3c446b3d2009-04-06 11:45:01 +02001907 int retval;
1908
1909 mutex_lock(&inode->i_mutex);
1910 retval = fasync_helper(fd, filp, on, &counter->fasync);
1911 mutex_unlock(&inode->i_mutex);
1912
1913 if (retval < 0)
1914 return retval;
1915
1916 return 0;
1917}
1918
Thomas Gleixner0793a612008-12-04 20:12:29 +01001919static const struct file_operations perf_fops = {
1920 .release = perf_release,
1921 .read = perf_read,
1922 .poll = perf_poll,
Paul Mackerrasd859e292009-01-17 18:10:22 +11001923 .unlocked_ioctl = perf_ioctl,
1924 .compat_ioctl = perf_ioctl,
Paul Mackerras37d81822009-03-23 18:22:08 +01001925 .mmap = perf_mmap,
Peter Zijlstra3c446b3d2009-04-06 11:45:01 +02001926 .fasync = perf_fasync,
Thomas Gleixner0793a612008-12-04 20:12:29 +01001927};
1928
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001929/*
Peter Zijlstra925d5192009-03-30 19:07:02 +02001930 * Perf counter wakeup
1931 *
1932 * If there's data, ensure we set the poll() state and publish everything
1933 * to user-space before waking everybody up.
1934 */
1935
1936void perf_counter_wakeup(struct perf_counter *counter)
1937{
Peter Zijlstra925d5192009-03-30 19:07:02 +02001938 wake_up_all(&counter->waitq);
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02001939
1940 if (counter->pending_kill) {
1941 kill_fasync(&counter->fasync, SIGIO, counter->pending_kill);
1942 counter->pending_kill = 0;
1943 }
Peter Zijlstra925d5192009-03-30 19:07:02 +02001944}
1945
1946/*
1947 * Pending wakeups
1948 *
1949 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
1950 *
1951 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
1952 * single linked list and use cmpxchg() to add entries lockless.
1953 */
1954
Peter Zijlstra79f14642009-04-06 11:45:07 +02001955static void perf_pending_counter(struct perf_pending_entry *entry)
1956{
1957 struct perf_counter *counter = container_of(entry,
1958 struct perf_counter, pending);
1959
1960 if (counter->pending_disable) {
1961 counter->pending_disable = 0;
1962 perf_counter_disable(counter);
1963 }
1964
1965 if (counter->pending_wakeup) {
1966 counter->pending_wakeup = 0;
1967 perf_counter_wakeup(counter);
1968 }
1969}
1970
Peter Zijlstra671dec52009-04-06 11:45:02 +02001971#define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
Peter Zijlstra925d5192009-03-30 19:07:02 +02001972
Peter Zijlstra671dec52009-04-06 11:45:02 +02001973static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = {
Peter Zijlstra925d5192009-03-30 19:07:02 +02001974 PENDING_TAIL,
1975};
1976
Peter Zijlstra671dec52009-04-06 11:45:02 +02001977static void perf_pending_queue(struct perf_pending_entry *entry,
1978 void (*func)(struct perf_pending_entry *))
Peter Zijlstra925d5192009-03-30 19:07:02 +02001979{
Peter Zijlstra671dec52009-04-06 11:45:02 +02001980 struct perf_pending_entry **head;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001981
Peter Zijlstra671dec52009-04-06 11:45:02 +02001982 if (cmpxchg(&entry->next, NULL, PENDING_TAIL) != NULL)
Peter Zijlstra925d5192009-03-30 19:07:02 +02001983 return;
1984
Peter Zijlstra671dec52009-04-06 11:45:02 +02001985 entry->func = func;
1986
1987 head = &get_cpu_var(perf_pending_head);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001988
1989 do {
Peter Zijlstra671dec52009-04-06 11:45:02 +02001990 entry->next = *head;
1991 } while (cmpxchg(head, entry->next, entry) != entry->next);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001992
1993 set_perf_counter_pending();
1994
Peter Zijlstra671dec52009-04-06 11:45:02 +02001995 put_cpu_var(perf_pending_head);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001996}
1997
1998static int __perf_pending_run(void)
1999{
Peter Zijlstra671dec52009-04-06 11:45:02 +02002000 struct perf_pending_entry *list;
Peter Zijlstra925d5192009-03-30 19:07:02 +02002001 int nr = 0;
2002
Peter Zijlstra671dec52009-04-06 11:45:02 +02002003 list = xchg(&__get_cpu_var(perf_pending_head), PENDING_TAIL);
Peter Zijlstra925d5192009-03-30 19:07:02 +02002004 while (list != PENDING_TAIL) {
Peter Zijlstra671dec52009-04-06 11:45:02 +02002005 void (*func)(struct perf_pending_entry *);
2006 struct perf_pending_entry *entry = list;
Peter Zijlstra925d5192009-03-30 19:07:02 +02002007
2008 list = list->next;
2009
Peter Zijlstra671dec52009-04-06 11:45:02 +02002010 func = entry->func;
2011 entry->next = NULL;
Peter Zijlstra925d5192009-03-30 19:07:02 +02002012 /*
2013 * Ensure we observe the unqueue before we issue the wakeup,
2014 * so that we won't be waiting forever.
2015 * -- see perf_not_pending().
2016 */
2017 smp_wmb();
2018
Peter Zijlstra671dec52009-04-06 11:45:02 +02002019 func(entry);
Peter Zijlstra925d5192009-03-30 19:07:02 +02002020 nr++;
2021 }
2022
2023 return nr;
2024}
2025
2026static inline int perf_not_pending(struct perf_counter *counter)
2027{
2028 /*
2029 * If we flush on whatever cpu we run, there is a chance we don't
2030 * need to wait.
2031 */
2032 get_cpu();
2033 __perf_pending_run();
2034 put_cpu();
2035
2036 /*
2037 * Ensure we see the proper queue state before going to sleep
2038 * so that we do not miss the wakeup. -- see perf_pending_handle()
2039 */
2040 smp_rmb();
Peter Zijlstra671dec52009-04-06 11:45:02 +02002041 return counter->pending.next == NULL;
Peter Zijlstra925d5192009-03-30 19:07:02 +02002042}
2043
2044static void perf_pending_sync(struct perf_counter *counter)
2045{
2046 wait_event(counter->waitq, perf_not_pending(counter));
2047}
2048
2049void perf_counter_do_pending(void)
2050{
2051 __perf_pending_run();
2052}
2053
2054/*
Peter Zijlstra394ee072009-03-30 19:07:14 +02002055 * Callchain support -- arch specific
2056 */
2057
Peter Zijlstra9c03d882009-04-06 11:45:00 +02002058__weak struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
Peter Zijlstra394ee072009-03-30 19:07:14 +02002059{
2060 return NULL;
2061}
2062
2063/*
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002064 * Output
2065 */
2066
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002067struct perf_output_handle {
2068 struct perf_counter *counter;
2069 struct perf_mmap_data *data;
2070 unsigned int offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01002071 unsigned int head;
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002072 int nmi;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002073 int overflow;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002074 int locked;
2075 unsigned long flags;
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002076};
2077
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002078static void perf_output_wakeup(struct perf_output_handle *handle)
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002079{
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002080 atomic_set(&handle->data->poll, POLL_IN);
2081
Peter Zijlstra671dec52009-04-06 11:45:02 +02002082 if (handle->nmi) {
Peter Zijlstra79f14642009-04-06 11:45:07 +02002083 handle->counter->pending_wakeup = 1;
Peter Zijlstra671dec52009-04-06 11:45:02 +02002084 perf_pending_queue(&handle->counter->pending,
Peter Zijlstra79f14642009-04-06 11:45:07 +02002085 perf_pending_counter);
Peter Zijlstra671dec52009-04-06 11:45:02 +02002086 } else
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002087 perf_counter_wakeup(handle->counter);
2088}
2089
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002090/*
2091 * Curious locking construct.
2092 *
2093 * We need to ensure a later event doesn't publish a head when a former
2094 * event isn't done writing. However since we need to deal with NMIs we
2095 * cannot fully serialize things.
2096 *
2097 * What we do is serialize between CPUs so we only have to deal with NMI
2098 * nesting on a single CPU.
2099 *
2100 * We only publish the head (and generate a wakeup) when the outer-most
2101 * event completes.
2102 */
2103static void perf_output_lock(struct perf_output_handle *handle)
2104{
2105 struct perf_mmap_data *data = handle->data;
2106 int cpu;
2107
2108 handle->locked = 0;
2109
2110 local_irq_save(handle->flags);
2111 cpu = smp_processor_id();
2112
2113 if (in_nmi() && atomic_read(&data->lock) == cpu)
2114 return;
2115
Peter Zijlstra22c15582009-05-05 17:50:25 +02002116 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002117 cpu_relax();
2118
2119 handle->locked = 1;
2120}
2121
2122static void perf_output_unlock(struct perf_output_handle *handle)
2123{
2124 struct perf_mmap_data *data = handle->data;
2125 int head, cpu;
2126
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002127 data->done_head = data->head;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002128
2129 if (!handle->locked)
2130 goto out;
2131
2132again:
2133 /*
2134 * The xchg implies a full barrier that ensures all writes are done
2135 * before we publish the new head, matched by a rmb() in userspace when
2136 * reading this position.
2137 */
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002138 while ((head = atomic_xchg(&data->done_head, 0)))
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002139 data->user_page->data_head = head;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002140
2141 /*
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002142 * NMI can happen here, which means we can miss a done_head update.
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002143 */
2144
Peter Zijlstra22c15582009-05-05 17:50:25 +02002145 cpu = atomic_xchg(&data->lock, -1);
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002146 WARN_ON_ONCE(cpu != smp_processor_id());
2147
2148 /*
2149 * Therefore we have to validate we did not indeed do so.
2150 */
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002151 if (unlikely(atomic_read(&data->done_head))) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002152 /*
2153 * Since we had it locked, we can lock it again.
2154 */
Peter Zijlstra22c15582009-05-05 17:50:25 +02002155 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002156 cpu_relax();
2157
2158 goto again;
2159 }
2160
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002161 if (atomic_xchg(&data->wakeup, 0))
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002162 perf_output_wakeup(handle);
2163out:
2164 local_irq_restore(handle->flags);
2165}
2166
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002167static int perf_output_begin(struct perf_output_handle *handle,
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002168 struct perf_counter *counter, unsigned int size,
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002169 int nmi, int overflow)
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002170{
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002171 struct perf_mmap_data *data;
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002172 unsigned int offset, head;
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002173
Peter Zijlstra2023b352009-05-05 17:50:26 +02002174 /*
2175 * For inherited counters we send all the output towards the parent.
2176 */
2177 if (counter->parent)
2178 counter = counter->parent;
2179
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002180 rcu_read_lock();
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002181 data = rcu_dereference(counter->data);
2182 if (!data)
2183 goto out;
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002184
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002185 handle->data = data;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002186 handle->counter = counter;
2187 handle->nmi = nmi;
2188 handle->overflow = overflow;
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002189
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002190 if (!data->nr_pages)
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002191 goto fail;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002192
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002193 perf_output_lock(handle);
2194
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002195 do {
2196 offset = head = atomic_read(&data->head);
Peter Zijlstrac7138f32009-03-24 13:18:16 +01002197 head += size;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002198 } while (atomic_cmpxchg(&data->head, offset, head) != offset);
2199
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002200 handle->offset = offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01002201 handle->head = head;
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002202
2203 if ((offset >> PAGE_SHIFT) != (head >> PAGE_SHIFT))
2204 atomic_set(&data->wakeup, 1);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002205
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002206 return 0;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002207
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002208fail:
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002209 perf_output_wakeup(handle);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002210out:
2211 rcu_read_unlock();
2212
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002213 return -ENOSPC;
2214}
2215
2216static void perf_output_copy(struct perf_output_handle *handle,
2217 void *buf, unsigned int len)
2218{
2219 unsigned int pages_mask;
2220 unsigned int offset;
2221 unsigned int size;
2222 void **pages;
2223
2224 offset = handle->offset;
2225 pages_mask = handle->data->nr_pages - 1;
2226 pages = handle->data->data_pages;
2227
2228 do {
2229 unsigned int page_offset;
2230 int nr;
2231
2232 nr = (offset >> PAGE_SHIFT) & pages_mask;
2233 page_offset = offset & (PAGE_SIZE - 1);
2234 size = min_t(unsigned int, PAGE_SIZE - page_offset, len);
2235
2236 memcpy(pages[nr] + page_offset, buf, size);
2237
2238 len -= size;
2239 buf += size;
2240 offset += size;
2241 } while (len);
2242
2243 handle->offset = offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01002244
Peter Zijlstra53020fe2009-05-13 21:26:19 +02002245 /*
2246 * Check we didn't copy past our reservation window, taking the
2247 * possible unsigned int wrap into account.
2248 */
2249 WARN_ON_ONCE(((int)(handle->head - handle->offset)) < 0);
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002250}
2251
Peter Zijlstra5c148192009-03-25 12:30:23 +01002252#define perf_output_put(handle, x) \
2253 perf_output_copy((handle), &(x), sizeof(x))
2254
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002255static void perf_output_end(struct perf_output_handle *handle)
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002256{
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002257 struct perf_counter *counter = handle->counter;
2258 struct perf_mmap_data *data = handle->data;
2259
2260 int wakeup_events = counter->hw_event.wakeup_events;
Peter Zijlstrac4578102009-04-02 11:12:01 +02002261
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002262 if (handle->overflow && wakeup_events) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002263 int events = atomic_inc_return(&data->events);
Peter Zijlstrac4578102009-04-02 11:12:01 +02002264 if (events >= wakeup_events) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002265 atomic_sub(wakeup_events, &data->events);
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002266 atomic_set(&data->wakeup, 1);
Peter Zijlstrac4578102009-04-02 11:12:01 +02002267 }
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002268 }
2269
2270 perf_output_unlock(handle);
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002271 rcu_read_unlock();
2272}
2273
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002274static void perf_counter_output(struct perf_counter *counter,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002275 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002276{
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002277 int ret;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002278 u64 record_type = counter->hw_event.record_type;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002279 struct perf_output_handle handle;
2280 struct perf_event_header header;
2281 u64 ip;
Peter Zijlstra5c148192009-03-25 12:30:23 +01002282 struct {
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002283 u32 pid, tid;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002284 } tid_entry;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002285 struct {
2286 u64 event;
2287 u64 counter;
2288 } group_entry;
Peter Zijlstra394ee072009-03-30 19:07:14 +02002289 struct perf_callchain_entry *callchain = NULL;
2290 int callchain_size = 0;
Peter Zijlstra339f7c92009-04-06 11:45:06 +02002291 u64 time;
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002292 struct {
2293 u32 cpu, reserved;
2294 } cpu_entry;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002295
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02002296 header.type = 0;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002297 header.size = sizeof(header);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002298
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02002299 header.misc = PERF_EVENT_MISC_OVERFLOW;
Paul Mackerras9d23a902009-05-14 21:48:08 +10002300 header.misc |= perf_misc_flags(regs);
Peter Zijlstra6fab0192009-04-08 15:01:26 +02002301
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002302 if (record_type & PERF_RECORD_IP) {
Paul Mackerras9d23a902009-05-14 21:48:08 +10002303 ip = perf_instruction_pointer(regs);
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02002304 header.type |= PERF_RECORD_IP;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002305 header.size += sizeof(ip);
2306 }
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002307
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002308 if (record_type & PERF_RECORD_TID) {
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002309 /* namespace issues */
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002310 tid_entry.pid = current->group_leader->pid;
2311 tid_entry.tid = current->pid;
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002312
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02002313 header.type |= PERF_RECORD_TID;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002314 header.size += sizeof(tid_entry);
2315 }
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002316
Peter Zijlstra4d855452009-04-08 15:01:32 +02002317 if (record_type & PERF_RECORD_TIME) {
2318 /*
2319 * Maybe do better on x86 and provide cpu_clock_nmi()
2320 */
2321 time = sched_clock();
2322
2323 header.type |= PERF_RECORD_TIME;
2324 header.size += sizeof(u64);
2325 }
2326
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002327 if (record_type & PERF_RECORD_ADDR) {
2328 header.type |= PERF_RECORD_ADDR;
2329 header.size += sizeof(u64);
2330 }
2331
Peter Zijlstraa85f61a2009-05-08 18:52:23 +02002332 if (record_type & PERF_RECORD_CONFIG) {
2333 header.type |= PERF_RECORD_CONFIG;
2334 header.size += sizeof(u64);
2335 }
2336
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002337 if (record_type & PERF_RECORD_CPU) {
2338 header.type |= PERF_RECORD_CPU;
2339 header.size += sizeof(cpu_entry);
2340
2341 cpu_entry.cpu = raw_smp_processor_id();
2342 }
2343
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002344 if (record_type & PERF_RECORD_GROUP) {
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02002345 header.type |= PERF_RECORD_GROUP;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002346 header.size += sizeof(u64) +
2347 counter->nr_siblings * sizeof(group_entry);
2348 }
2349
2350 if (record_type & PERF_RECORD_CALLCHAIN) {
Peter Zijlstra394ee072009-03-30 19:07:14 +02002351 callchain = perf_callchain(regs);
2352
2353 if (callchain) {
Peter Zijlstra9c03d882009-04-06 11:45:00 +02002354 callchain_size = (1 + callchain->nr) * sizeof(u64);
Peter Zijlstra394ee072009-03-30 19:07:14 +02002355
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02002356 header.type |= PERF_RECORD_CALLCHAIN;
Peter Zijlstra394ee072009-03-30 19:07:14 +02002357 header.size += callchain_size;
2358 }
2359 }
2360
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002361 ret = perf_output_begin(&handle, counter, header.size, nmi, 1);
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002362 if (ret)
2363 return;
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002364
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002365 perf_output_put(&handle, header);
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002366
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002367 if (record_type & PERF_RECORD_IP)
2368 perf_output_put(&handle, ip);
2369
2370 if (record_type & PERF_RECORD_TID)
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002371 perf_output_put(&handle, tid_entry);
2372
Peter Zijlstra4d855452009-04-08 15:01:32 +02002373 if (record_type & PERF_RECORD_TIME)
2374 perf_output_put(&handle, time);
2375
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002376 if (record_type & PERF_RECORD_ADDR)
2377 perf_output_put(&handle, addr);
2378
Peter Zijlstraa85f61a2009-05-08 18:52:23 +02002379 if (record_type & PERF_RECORD_CONFIG)
2380 perf_output_put(&handle, counter->hw_event.config);
2381
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002382 if (record_type & PERF_RECORD_CPU)
2383 perf_output_put(&handle, cpu_entry);
2384
Peter Zijlstra2023b352009-05-05 17:50:26 +02002385 /*
2386 * XXX PERF_RECORD_GROUP vs inherited counters seems difficult.
2387 */
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002388 if (record_type & PERF_RECORD_GROUP) {
2389 struct perf_counter *leader, *sub;
2390 u64 nr = counter->nr_siblings;
2391
2392 perf_output_put(&handle, nr);
2393
2394 leader = counter->group_leader;
2395 list_for_each_entry(sub, &leader->sibling_list, list_entry) {
2396 if (sub != counter)
Robert Richter4aeb0b42009-04-29 12:47:03 +02002397 sub->pmu->read(sub);
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002398
2399 group_entry.event = sub->hw_event.config;
2400 group_entry.counter = atomic64_read(&sub->count);
2401
2402 perf_output_put(&handle, group_entry);
2403 }
2404 }
2405
Peter Zijlstra394ee072009-03-30 19:07:14 +02002406 if (callchain)
2407 perf_output_copy(&handle, callchain, callchain_size);
2408
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002409 perf_output_end(&handle);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002410}
2411
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002412/*
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002413 * comm tracking
2414 */
2415
2416struct perf_comm_event {
Ingo Molnar22a4f652009-06-01 10:13:37 +02002417 struct task_struct *task;
2418 char *comm;
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002419 int comm_size;
2420
2421 struct {
2422 struct perf_event_header header;
2423
2424 u32 pid;
2425 u32 tid;
2426 } event;
2427};
2428
2429static void perf_counter_comm_output(struct perf_counter *counter,
2430 struct perf_comm_event *comm_event)
2431{
2432 struct perf_output_handle handle;
2433 int size = comm_event->event.header.size;
2434 int ret = perf_output_begin(&handle, counter, size, 0, 0);
2435
2436 if (ret)
2437 return;
2438
2439 perf_output_put(&handle, comm_event->event);
2440 perf_output_copy(&handle, comm_event->comm,
2441 comm_event->comm_size);
2442 perf_output_end(&handle);
2443}
2444
2445static int perf_counter_comm_match(struct perf_counter *counter,
2446 struct perf_comm_event *comm_event)
2447{
2448 if (counter->hw_event.comm &&
2449 comm_event->event.header.type == PERF_EVENT_COMM)
2450 return 1;
2451
2452 return 0;
2453}
2454
2455static void perf_counter_comm_ctx(struct perf_counter_context *ctx,
2456 struct perf_comm_event *comm_event)
2457{
2458 struct perf_counter *counter;
2459
2460 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2461 return;
2462
2463 rcu_read_lock();
2464 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2465 if (perf_counter_comm_match(counter, comm_event))
2466 perf_counter_comm_output(counter, comm_event);
2467 }
2468 rcu_read_unlock();
2469}
2470
2471static void perf_counter_comm_event(struct perf_comm_event *comm_event)
2472{
2473 struct perf_cpu_context *cpuctx;
Peter Zijlstra665c2142009-05-29 14:51:57 +02002474 struct perf_counter_context *ctx;
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002475 unsigned int size;
2476 char *comm = comm_event->task->comm;
2477
Ingo Molnar888fcee2009-04-09 09:48:22 +02002478 size = ALIGN(strlen(comm)+1, sizeof(u64));
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002479
2480 comm_event->comm = comm;
2481 comm_event->comm_size = size;
2482
2483 comm_event->event.header.size = sizeof(comm_event->event) + size;
2484
2485 cpuctx = &get_cpu_var(perf_cpu_context);
2486 perf_counter_comm_ctx(&cpuctx->ctx, comm_event);
2487 put_cpu_var(perf_cpu_context);
Peter Zijlstra665c2142009-05-29 14:51:57 +02002488
2489 rcu_read_lock();
2490 /*
2491 * doesn't really matter which of the child contexts the
2492 * events ends up in.
2493 */
2494 ctx = rcu_dereference(current->perf_counter_ctxp);
2495 if (ctx)
2496 perf_counter_comm_ctx(ctx, comm_event);
2497 rcu_read_unlock();
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002498}
2499
2500void perf_counter_comm(struct task_struct *task)
2501{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002502 struct perf_comm_event comm_event;
2503
2504 if (!atomic_read(&nr_comm_tracking))
2505 return;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10002506
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002507 comm_event = (struct perf_comm_event){
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002508 .task = task,
2509 .event = {
2510 .header = { .type = PERF_EVENT_COMM, },
2511 .pid = task->group_leader->pid,
2512 .tid = task->pid,
2513 },
2514 };
2515
2516 perf_counter_comm_event(&comm_event);
2517}
2518
2519/*
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002520 * mmap tracking
2521 */
2522
2523struct perf_mmap_event {
2524 struct file *file;
2525 char *file_name;
2526 int file_size;
2527
2528 struct {
2529 struct perf_event_header header;
2530
2531 u32 pid;
2532 u32 tid;
2533 u64 start;
2534 u64 len;
2535 u64 pgoff;
2536 } event;
2537};
2538
2539static void perf_counter_mmap_output(struct perf_counter *counter,
2540 struct perf_mmap_event *mmap_event)
2541{
2542 struct perf_output_handle handle;
2543 int size = mmap_event->event.header.size;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002544 int ret = perf_output_begin(&handle, counter, size, 0, 0);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002545
2546 if (ret)
2547 return;
2548
2549 perf_output_put(&handle, mmap_event->event);
2550 perf_output_copy(&handle, mmap_event->file_name,
2551 mmap_event->file_size);
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002552 perf_output_end(&handle);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002553}
2554
2555static int perf_counter_mmap_match(struct perf_counter *counter,
2556 struct perf_mmap_event *mmap_event)
2557{
2558 if (counter->hw_event.mmap &&
2559 mmap_event->event.header.type == PERF_EVENT_MMAP)
2560 return 1;
2561
2562 if (counter->hw_event.munmap &&
2563 mmap_event->event.header.type == PERF_EVENT_MUNMAP)
2564 return 1;
2565
2566 return 0;
2567}
2568
2569static void perf_counter_mmap_ctx(struct perf_counter_context *ctx,
2570 struct perf_mmap_event *mmap_event)
2571{
2572 struct perf_counter *counter;
2573
2574 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2575 return;
2576
2577 rcu_read_lock();
2578 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2579 if (perf_counter_mmap_match(counter, mmap_event))
2580 perf_counter_mmap_output(counter, mmap_event);
2581 }
2582 rcu_read_unlock();
2583}
2584
2585static void perf_counter_mmap_event(struct perf_mmap_event *mmap_event)
2586{
2587 struct perf_cpu_context *cpuctx;
Peter Zijlstra665c2142009-05-29 14:51:57 +02002588 struct perf_counter_context *ctx;
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002589 struct file *file = mmap_event->file;
2590 unsigned int size;
2591 char tmp[16];
2592 char *buf = NULL;
2593 char *name;
2594
2595 if (file) {
2596 buf = kzalloc(PATH_MAX, GFP_KERNEL);
2597 if (!buf) {
2598 name = strncpy(tmp, "//enomem", sizeof(tmp));
2599 goto got_name;
2600 }
Peter Zijlstrad3d21c42009-04-09 10:53:46 +02002601 name = d_path(&file->f_path, buf, PATH_MAX);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002602 if (IS_ERR(name)) {
2603 name = strncpy(tmp, "//toolong", sizeof(tmp));
2604 goto got_name;
2605 }
2606 } else {
2607 name = strncpy(tmp, "//anon", sizeof(tmp));
2608 goto got_name;
2609 }
2610
2611got_name:
Ingo Molnar888fcee2009-04-09 09:48:22 +02002612 size = ALIGN(strlen(name)+1, sizeof(u64));
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002613
2614 mmap_event->file_name = name;
2615 mmap_event->file_size = size;
2616
2617 mmap_event->event.header.size = sizeof(mmap_event->event) + size;
2618
2619 cpuctx = &get_cpu_var(perf_cpu_context);
2620 perf_counter_mmap_ctx(&cpuctx->ctx, mmap_event);
2621 put_cpu_var(perf_cpu_context);
2622
Peter Zijlstra665c2142009-05-29 14:51:57 +02002623 rcu_read_lock();
2624 /*
2625 * doesn't really matter which of the child contexts the
2626 * events ends up in.
2627 */
2628 ctx = rcu_dereference(current->perf_counter_ctxp);
2629 if (ctx)
2630 perf_counter_mmap_ctx(ctx, mmap_event);
2631 rcu_read_unlock();
2632
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002633 kfree(buf);
2634}
2635
2636void perf_counter_mmap(unsigned long addr, unsigned long len,
2637 unsigned long pgoff, struct file *file)
2638{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002639 struct perf_mmap_event mmap_event;
2640
2641 if (!atomic_read(&nr_mmap_tracking))
2642 return;
2643
2644 mmap_event = (struct perf_mmap_event){
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002645 .file = file,
2646 .event = {
2647 .header = { .type = PERF_EVENT_MMAP, },
2648 .pid = current->group_leader->pid,
2649 .tid = current->pid,
2650 .start = addr,
2651 .len = len,
2652 .pgoff = pgoff,
2653 },
2654 };
2655
2656 perf_counter_mmap_event(&mmap_event);
2657}
2658
2659void perf_counter_munmap(unsigned long addr, unsigned long len,
2660 unsigned long pgoff, struct file *file)
2661{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002662 struct perf_mmap_event mmap_event;
2663
2664 if (!atomic_read(&nr_munmap_tracking))
2665 return;
2666
2667 mmap_event = (struct perf_mmap_event){
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002668 .file = file,
2669 .event = {
2670 .header = { .type = PERF_EVENT_MUNMAP, },
2671 .pid = current->group_leader->pid,
2672 .tid = current->pid,
2673 .start = addr,
2674 .len = len,
2675 .pgoff = pgoff,
2676 },
2677 };
2678
2679 perf_counter_mmap_event(&mmap_event);
2680}
2681
2682/*
Peter Zijlstrae220d2d2009-05-23 18:28:55 +02002683 * Log irq_period changes so that analyzing tools can re-normalize the
2684 * event flow.
Peter Zijlstra26b119b2009-05-20 12:21:20 +02002685 */
2686
2687static void perf_log_period(struct perf_counter *counter, u64 period)
2688{
2689 struct perf_output_handle handle;
2690 int ret;
2691
2692 struct {
2693 struct perf_event_header header;
2694 u64 time;
2695 u64 period;
2696 } freq_event = {
2697 .header = {
2698 .type = PERF_EVENT_PERIOD,
2699 .misc = 0,
2700 .size = sizeof(freq_event),
2701 },
2702 .time = sched_clock(),
2703 .period = period,
2704 };
2705
2706 if (counter->hw.irq_period == period)
2707 return;
2708
2709 ret = perf_output_begin(&handle, counter, sizeof(freq_event), 0, 0);
2710 if (ret)
2711 return;
2712
2713 perf_output_put(&handle, freq_event);
2714 perf_output_end(&handle);
2715}
2716
2717/*
Peter Zijlstraa78ac322009-05-25 17:39:05 +02002718 * IRQ throttle logging
2719 */
2720
2721static void perf_log_throttle(struct perf_counter *counter, int enable)
2722{
2723 struct perf_output_handle handle;
2724 int ret;
2725
2726 struct {
2727 struct perf_event_header header;
2728 u64 time;
2729 } throttle_event = {
2730 .header = {
2731 .type = PERF_EVENT_THROTTLE + 1,
2732 .misc = 0,
2733 .size = sizeof(throttle_event),
2734 },
2735 .time = sched_clock(),
2736 };
2737
Ingo Molnar0127c3e2009-05-25 22:03:26 +02002738 ret = perf_output_begin(&handle, counter, sizeof(throttle_event), 1, 0);
Peter Zijlstraa78ac322009-05-25 17:39:05 +02002739 if (ret)
2740 return;
2741
2742 perf_output_put(&handle, throttle_event);
2743 perf_output_end(&handle);
2744}
2745
2746/*
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002747 * Generic counter overflow handling.
2748 */
2749
2750int perf_counter_overflow(struct perf_counter *counter,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002751 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002752{
Peter Zijlstra79f14642009-04-06 11:45:07 +02002753 int events = atomic_read(&counter->event_limit);
Peter Zijlstraa78ac322009-05-25 17:39:05 +02002754 int throttle = counter->pmu->unthrottle != NULL;
Peter Zijlstra79f14642009-04-06 11:45:07 +02002755 int ret = 0;
2756
Peter Zijlstraa78ac322009-05-25 17:39:05 +02002757 if (!throttle) {
2758 counter->hw.interrupts++;
2759 } else if (counter->hw.interrupts != MAX_INTERRUPTS) {
2760 counter->hw.interrupts++;
2761 if (HZ*counter->hw.interrupts > (u64)sysctl_perf_counter_limit) {
2762 counter->hw.interrupts = MAX_INTERRUPTS;
2763 perf_log_throttle(counter, 0);
2764 ret = 1;
2765 }
2766 }
Peter Zijlstra60db5e02009-05-15 15:19:28 +02002767
Peter Zijlstra2023b352009-05-05 17:50:26 +02002768 /*
2769 * XXX event_limit might not quite work as expected on inherited
2770 * counters
2771 */
2772
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002773 counter->pending_kill = POLL_IN;
Peter Zijlstra79f14642009-04-06 11:45:07 +02002774 if (events && atomic_dec_and_test(&counter->event_limit)) {
2775 ret = 1;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002776 counter->pending_kill = POLL_HUP;
Peter Zijlstra79f14642009-04-06 11:45:07 +02002777 if (nmi) {
2778 counter->pending_disable = 1;
2779 perf_pending_queue(&counter->pending,
2780 perf_pending_counter);
2781 } else
2782 perf_counter_disable(counter);
2783 }
2784
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002785 perf_counter_output(counter, nmi, regs, addr);
Peter Zijlstra79f14642009-04-06 11:45:07 +02002786 return ret;
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002787}
2788
2789/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002790 * Generic software counter infrastructure
2791 */
2792
2793static void perf_swcounter_update(struct perf_counter *counter)
2794{
2795 struct hw_perf_counter *hwc = &counter->hw;
2796 u64 prev, now;
2797 s64 delta;
2798
2799again:
2800 prev = atomic64_read(&hwc->prev_count);
2801 now = atomic64_read(&hwc->count);
2802 if (atomic64_cmpxchg(&hwc->prev_count, prev, now) != prev)
2803 goto again;
2804
2805 delta = now - prev;
2806
2807 atomic64_add(delta, &counter->count);
2808 atomic64_sub(delta, &hwc->period_left);
2809}
2810
2811static void perf_swcounter_set_period(struct perf_counter *counter)
2812{
2813 struct hw_perf_counter *hwc = &counter->hw;
2814 s64 left = atomic64_read(&hwc->period_left);
2815 s64 period = hwc->irq_period;
2816
2817 if (unlikely(left <= -period)) {
2818 left = period;
2819 atomic64_set(&hwc->period_left, left);
2820 }
2821
2822 if (unlikely(left <= 0)) {
2823 left += period;
2824 atomic64_add(period, &hwc->period_left);
2825 }
2826
2827 atomic64_set(&hwc->prev_count, -left);
2828 atomic64_set(&hwc->count, -left);
2829}
2830
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002831static enum hrtimer_restart perf_swcounter_hrtimer(struct hrtimer *hrtimer)
2832{
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002833 enum hrtimer_restart ret = HRTIMER_RESTART;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002834 struct perf_counter *counter;
2835 struct pt_regs *regs;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02002836 u64 period;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002837
2838 counter = container_of(hrtimer, struct perf_counter, hw.hrtimer);
Robert Richter4aeb0b42009-04-29 12:47:03 +02002839 counter->pmu->read(counter);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002840
2841 regs = get_irq_regs();
2842 /*
2843 * In case we exclude kernel IPs or are somehow not in interrupt
2844 * context, provide the next best thing, the user IP.
2845 */
2846 if ((counter->hw_event.exclude_kernel || !regs) &&
2847 !counter->hw_event.exclude_user)
2848 regs = task_pt_regs(current);
2849
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002850 if (regs) {
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002851 if (perf_counter_overflow(counter, 0, regs, 0))
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002852 ret = HRTIMER_NORESTART;
2853 }
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002854
Peter Zijlstra60db5e02009-05-15 15:19:28 +02002855 period = max_t(u64, 10000, counter->hw.irq_period);
2856 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002857
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002858 return ret;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002859}
2860
2861static void perf_swcounter_overflow(struct perf_counter *counter,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002862 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002863{
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002864 perf_swcounter_update(counter);
2865 perf_swcounter_set_period(counter);
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002866 if (perf_counter_overflow(counter, nmi, regs, addr))
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002867 /* soft-disable the counter */
2868 ;
2869
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002870}
2871
Paul Mackerras880ca152009-06-01 17:49:14 +10002872static int perf_swcounter_is_counting(struct perf_counter *counter)
2873{
2874 struct perf_counter_context *ctx;
2875 unsigned long flags;
2876 int count;
2877
2878 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
2879 return 1;
2880
2881 if (counter->state != PERF_COUNTER_STATE_INACTIVE)
2882 return 0;
2883
2884 /*
2885 * If the counter is inactive, it could be just because
2886 * its task is scheduled out, or because it's in a group
2887 * which could not go on the PMU. We want to count in
2888 * the first case but not the second. If the context is
2889 * currently active then an inactive software counter must
2890 * be the second case. If it's not currently active then
2891 * we need to know whether the counter was active when the
2892 * context was last active, which we can determine by
2893 * comparing counter->tstamp_stopped with ctx->time.
2894 *
2895 * We are within an RCU read-side critical section,
2896 * which protects the existence of *ctx.
2897 */
2898 ctx = counter->ctx;
2899 spin_lock_irqsave(&ctx->lock, flags);
2900 count = 1;
2901 /* Re-check state now we have the lock */
2902 if (counter->state < PERF_COUNTER_STATE_INACTIVE ||
2903 counter->ctx->is_active ||
2904 counter->tstamp_stopped < ctx->time)
2905 count = 0;
2906 spin_unlock_irqrestore(&ctx->lock, flags);
2907 return count;
2908}
2909
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002910static int perf_swcounter_match(struct perf_counter *counter,
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002911 enum perf_event_types type,
2912 u32 event, struct pt_regs *regs)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002913{
Paul Mackerras880ca152009-06-01 17:49:14 +10002914 u64 event_config;
2915
2916 event_config = ((u64) type << PERF_COUNTER_TYPE_SHIFT) | event;
2917
2918 if (!perf_swcounter_is_counting(counter))
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002919 return 0;
2920
Paul Mackerras880ca152009-06-01 17:49:14 +10002921 if (counter->hw_event.config != event_config)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002922 return 0;
2923
Paul Mackerras3f731ca2009-06-01 17:52:30 +10002924 if (regs) {
2925 if (counter->hw_event.exclude_user && user_mode(regs))
2926 return 0;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002927
Paul Mackerras3f731ca2009-06-01 17:52:30 +10002928 if (counter->hw_event.exclude_kernel && !user_mode(regs))
2929 return 0;
2930 }
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002931
2932 return 1;
2933}
2934
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002935static void perf_swcounter_add(struct perf_counter *counter, u64 nr,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002936 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002937{
2938 int neg = atomic64_add_negative(nr, &counter->hw.count);
Ingo Molnar22a4f652009-06-01 10:13:37 +02002939
Paul Mackerras3f731ca2009-06-01 17:52:30 +10002940 if (counter->hw.irq_period && !neg && regs)
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002941 perf_swcounter_overflow(counter, nmi, regs, addr);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002942}
2943
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002944static void perf_swcounter_ctx_event(struct perf_counter_context *ctx,
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002945 enum perf_event_types type, u32 event,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002946 u64 nr, int nmi, struct pt_regs *regs,
2947 u64 addr)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002948{
2949 struct perf_counter *counter;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002950
Peter Zijlstra01ef09d2009-03-19 20:26:11 +01002951 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002952 return;
2953
Peter Zijlstra592903c2009-03-13 12:21:36 +01002954 rcu_read_lock();
2955 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002956 if (perf_swcounter_match(counter, type, event, regs))
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002957 perf_swcounter_add(counter, nr, nmi, regs, addr);
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002958 }
Peter Zijlstra592903c2009-03-13 12:21:36 +01002959 rcu_read_unlock();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002960}
2961
Peter Zijlstra96f6d442009-03-23 18:22:07 +01002962static int *perf_swcounter_recursion_context(struct perf_cpu_context *cpuctx)
2963{
2964 if (in_nmi())
2965 return &cpuctx->recursion[3];
2966
2967 if (in_irq())
2968 return &cpuctx->recursion[2];
2969
2970 if (in_softirq())
2971 return &cpuctx->recursion[1];
2972
2973 return &cpuctx->recursion[0];
2974}
2975
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002976static void __perf_swcounter_event(enum perf_event_types type, u32 event,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002977 u64 nr, int nmi, struct pt_regs *regs,
2978 u64 addr)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002979{
2980 struct perf_cpu_context *cpuctx = &get_cpu_var(perf_cpu_context);
Peter Zijlstra96f6d442009-03-23 18:22:07 +01002981 int *recursion = perf_swcounter_recursion_context(cpuctx);
Peter Zijlstra665c2142009-05-29 14:51:57 +02002982 struct perf_counter_context *ctx;
Peter Zijlstra96f6d442009-03-23 18:22:07 +01002983
2984 if (*recursion)
2985 goto out;
2986
2987 (*recursion)++;
2988 barrier();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002989
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002990 perf_swcounter_ctx_event(&cpuctx->ctx, type, event,
2991 nr, nmi, regs, addr);
Peter Zijlstra665c2142009-05-29 14:51:57 +02002992 rcu_read_lock();
2993 /*
2994 * doesn't really matter which of the child contexts the
2995 * events ends up in.
2996 */
2997 ctx = rcu_dereference(current->perf_counter_ctxp);
2998 if (ctx)
2999 perf_swcounter_ctx_event(ctx, type, event, nr, nmi, regs, addr);
3000 rcu_read_unlock();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003001
Peter Zijlstra96f6d442009-03-23 18:22:07 +01003002 barrier();
3003 (*recursion)--;
3004
3005out:
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003006 put_cpu_var(perf_cpu_context);
3007}
3008
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003009void
3010perf_swcounter_event(u32 event, u64 nr, int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003011{
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003012 __perf_swcounter_event(PERF_TYPE_SOFTWARE, event, nr, nmi, regs, addr);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003013}
3014
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003015static void perf_swcounter_read(struct perf_counter *counter)
3016{
3017 perf_swcounter_update(counter);
3018}
3019
3020static int perf_swcounter_enable(struct perf_counter *counter)
3021{
3022 perf_swcounter_set_period(counter);
3023 return 0;
3024}
3025
3026static void perf_swcounter_disable(struct perf_counter *counter)
3027{
3028 perf_swcounter_update(counter);
3029}
3030
Robert Richter4aeb0b42009-04-29 12:47:03 +02003031static const struct pmu perf_ops_generic = {
Peter Zijlstraac17dc82009-03-13 12:21:34 +01003032 .enable = perf_swcounter_enable,
3033 .disable = perf_swcounter_disable,
3034 .read = perf_swcounter_read,
3035};
3036
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003037/*
3038 * Software counter: cpu wall time clock
3039 */
3040
Paul Mackerras9abf8a02009-01-09 16:26:43 +11003041static void cpu_clock_perf_counter_update(struct perf_counter *counter)
3042{
3043 int cpu = raw_smp_processor_id();
3044 s64 prev;
3045 u64 now;
3046
3047 now = cpu_clock(cpu);
3048 prev = atomic64_read(&counter->hw.prev_count);
3049 atomic64_set(&counter->hw.prev_count, now);
3050 atomic64_add(now - prev, &counter->count);
3051}
3052
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003053static int cpu_clock_perf_counter_enable(struct perf_counter *counter)
3054{
3055 struct hw_perf_counter *hwc = &counter->hw;
3056 int cpu = raw_smp_processor_id();
3057
3058 atomic64_set(&hwc->prev_count, cpu_clock(cpu));
Peter Zijlstra039fc912009-03-13 16:43:47 +01003059 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
3060 hwc->hrtimer.function = perf_swcounter_hrtimer;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003061 if (hwc->irq_period) {
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003062 u64 period = max_t(u64, 10000, hwc->irq_period);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003063 __hrtimer_start_range_ns(&hwc->hrtimer,
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003064 ns_to_ktime(period), 0,
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003065 HRTIMER_MODE_REL, 0);
3066 }
3067
3068 return 0;
3069}
3070
Ingo Molnar5c92d122008-12-11 13:21:10 +01003071static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
3072{
Peter Zijlstrab986d7e2009-05-20 12:21:21 +02003073 if (counter->hw.irq_period)
3074 hrtimer_cancel(&counter->hw.hrtimer);
Paul Mackerras9abf8a02009-01-09 16:26:43 +11003075 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01003076}
3077
3078static void cpu_clock_perf_counter_read(struct perf_counter *counter)
3079{
Paul Mackerras9abf8a02009-01-09 16:26:43 +11003080 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01003081}
3082
Robert Richter4aeb0b42009-04-29 12:47:03 +02003083static const struct pmu perf_ops_cpu_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +01003084 .enable = cpu_clock_perf_counter_enable,
3085 .disable = cpu_clock_perf_counter_disable,
3086 .read = cpu_clock_perf_counter_read,
Ingo Molnar5c92d122008-12-11 13:21:10 +01003087};
3088
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01003089/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003090 * Software counter: task time clock
3091 */
3092
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02003093static void task_clock_perf_counter_update(struct perf_counter *counter, u64 now)
Ingo Molnarbae43c92008-12-11 14:03:20 +01003094{
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02003095 u64 prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003096 s64 delta;
Ingo Molnarbae43c92008-12-11 14:03:20 +01003097
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02003098 prev = atomic64_xchg(&counter->hw.prev_count, now);
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003099 delta = now - prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003100 atomic64_add(delta, &counter->count);
Ingo Molnarbae43c92008-12-11 14:03:20 +01003101}
3102
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01003103static int task_clock_perf_counter_enable(struct perf_counter *counter)
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003104{
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003105 struct hw_perf_counter *hwc = &counter->hw;
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02003106 u64 now;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003107
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02003108 now = counter->ctx->time;
3109
3110 atomic64_set(&hwc->prev_count, now);
Peter Zijlstra039fc912009-03-13 16:43:47 +01003111 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
3112 hwc->hrtimer.function = perf_swcounter_hrtimer;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003113 if (hwc->irq_period) {
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003114 u64 period = max_t(u64, 10000, hwc->irq_period);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003115 __hrtimer_start_range_ns(&hwc->hrtimer,
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003116 ns_to_ktime(period), 0,
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003117 HRTIMER_MODE_REL, 0);
3118 }
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01003119
3120 return 0;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003121}
3122
3123static void task_clock_perf_counter_disable(struct perf_counter *counter)
3124{
Peter Zijlstrab986d7e2009-05-20 12:21:21 +02003125 if (counter->hw.irq_period)
3126 hrtimer_cancel(&counter->hw.hrtimer);
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02003127 task_clock_perf_counter_update(counter, counter->ctx->time);
3128
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003129}
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01003130
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003131static void task_clock_perf_counter_read(struct perf_counter *counter)
3132{
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02003133 u64 time;
3134
3135 if (!in_nmi()) {
3136 update_context_time(counter->ctx);
3137 time = counter->ctx->time;
3138 } else {
3139 u64 now = perf_clock();
3140 u64 delta = now - counter->ctx->timestamp;
3141 time = counter->ctx->time + delta;
3142 }
3143
3144 task_clock_perf_counter_update(counter, time);
Ingo Molnarbae43c92008-12-11 14:03:20 +01003145}
3146
Robert Richter4aeb0b42009-04-29 12:47:03 +02003147static const struct pmu perf_ops_task_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +01003148 .enable = task_clock_perf_counter_enable,
3149 .disable = task_clock_perf_counter_disable,
3150 .read = task_clock_perf_counter_read,
Ingo Molnarbae43c92008-12-11 14:03:20 +01003151};
3152
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003153/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003154 * Software counter: cpu migrations
3155 */
Paul Mackerras3f731ca2009-06-01 17:52:30 +10003156void perf_counter_task_migration(struct task_struct *task, int cpu)
Ingo Molnar6c594c22008-12-14 12:34:15 +01003157{
Paul Mackerras3f731ca2009-06-01 17:52:30 +10003158 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
3159 struct perf_counter_context *ctx;
Paul Mackerras23a185c2009-02-09 22:42:47 +11003160
Paul Mackerras3f731ca2009-06-01 17:52:30 +10003161 perf_swcounter_ctx_event(&cpuctx->ctx, PERF_TYPE_SOFTWARE,
3162 PERF_COUNT_CPU_MIGRATIONS,
3163 1, 1, NULL, 0);
3164
3165 ctx = perf_pin_task_context(task);
3166 if (ctx) {
3167 perf_swcounter_ctx_event(ctx, PERF_TYPE_SOFTWARE,
3168 PERF_COUNT_CPU_MIGRATIONS,
3169 1, 1, NULL, 0);
3170 perf_unpin_context(ctx);
3171 }
Ingo Molnar6c594c22008-12-14 12:34:15 +01003172}
3173
Peter Zijlstrae077df42009-03-19 20:26:17 +01003174#ifdef CONFIG_EVENT_PROFILE
3175void perf_tpcounter_event(int event_id)
3176{
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003177 struct pt_regs *regs = get_irq_regs();
3178
3179 if (!regs)
3180 regs = task_pt_regs(current);
3181
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003182 __perf_swcounter_event(PERF_TYPE_TRACEPOINT, event_id, 1, 1, regs, 0);
Peter Zijlstrae077df42009-03-19 20:26:17 +01003183}
Steven Whitehouseff7b1b42009-04-15 16:55:05 +01003184EXPORT_SYMBOL_GPL(perf_tpcounter_event);
Peter Zijlstrae077df42009-03-19 20:26:17 +01003185
3186extern int ftrace_profile_enable(int);
3187extern void ftrace_profile_disable(int);
3188
3189static void tp_perf_counter_destroy(struct perf_counter *counter)
3190{
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01003191 ftrace_profile_disable(perf_event_id(&counter->hw_event));
Peter Zijlstrae077df42009-03-19 20:26:17 +01003192}
3193
Robert Richter4aeb0b42009-04-29 12:47:03 +02003194static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
Peter Zijlstrae077df42009-03-19 20:26:17 +01003195{
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01003196 int event_id = perf_event_id(&counter->hw_event);
Peter Zijlstrae077df42009-03-19 20:26:17 +01003197 int ret;
3198
3199 ret = ftrace_profile_enable(event_id);
3200 if (ret)
3201 return NULL;
3202
3203 counter->destroy = tp_perf_counter_destroy;
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003204 counter->hw.irq_period = counter->hw_event.irq_period;
Peter Zijlstrae077df42009-03-19 20:26:17 +01003205
3206 return &perf_ops_generic;
3207}
3208#else
Robert Richter4aeb0b42009-04-29 12:47:03 +02003209static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
Peter Zijlstrae077df42009-03-19 20:26:17 +01003210{
3211 return NULL;
3212}
3213#endif
3214
Robert Richter4aeb0b42009-04-29 12:47:03 +02003215static const struct pmu *sw_perf_counter_init(struct perf_counter *counter)
Ingo Molnar5c92d122008-12-11 13:21:10 +01003216{
Robert Richter4aeb0b42009-04-29 12:47:03 +02003217 const struct pmu *pmu = NULL;
Ingo Molnar5c92d122008-12-11 13:21:10 +01003218
Paul Mackerras0475f9e2009-02-11 14:35:35 +11003219 /*
3220 * Software counters (currently) can't in general distinguish
3221 * between user, kernel and hypervisor events.
3222 * However, context switches and cpu migrations are considered
3223 * to be kernel events, and page faults are never hypervisor
3224 * events.
3225 */
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01003226 switch (perf_event_id(&counter->hw_event)) {
Ingo Molnar5c92d122008-12-11 13:21:10 +01003227 case PERF_COUNT_CPU_CLOCK:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003228 pmu = &perf_ops_cpu_clock;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003229
Ingo Molnar5c92d122008-12-11 13:21:10 +01003230 break;
Ingo Molnarbae43c92008-12-11 14:03:20 +01003231 case PERF_COUNT_TASK_CLOCK:
Paul Mackerras23a185c2009-02-09 22:42:47 +11003232 /*
3233 * If the user instantiates this as a per-cpu counter,
3234 * use the cpu_clock counter instead.
3235 */
3236 if (counter->ctx->task)
Robert Richter4aeb0b42009-04-29 12:47:03 +02003237 pmu = &perf_ops_task_clock;
Paul Mackerras23a185c2009-02-09 22:42:47 +11003238 else
Robert Richter4aeb0b42009-04-29 12:47:03 +02003239 pmu = &perf_ops_cpu_clock;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003240
Ingo Molnarbae43c92008-12-11 14:03:20 +01003241 break;
Ingo Molnare06c61a2008-12-14 14:44:31 +01003242 case PERF_COUNT_PAGE_FAULTS:
Peter Zijlstraac17dc82009-03-13 12:21:34 +01003243 case PERF_COUNT_PAGE_FAULTS_MIN:
3244 case PERF_COUNT_PAGE_FAULTS_MAJ:
Ingo Molnar5d6a27d2008-12-14 12:28:33 +01003245 case PERF_COUNT_CONTEXT_SWITCHES:
Ingo Molnar6c594c22008-12-14 12:34:15 +01003246 case PERF_COUNT_CPU_MIGRATIONS:
Paul Mackerras3f731ca2009-06-01 17:52:30 +10003247 pmu = &perf_ops_generic;
Ingo Molnar6c594c22008-12-14 12:34:15 +01003248 break;
Ingo Molnar5c92d122008-12-11 13:21:10 +01003249 }
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003250
Robert Richter4aeb0b42009-04-29 12:47:03 +02003251 return pmu;
Ingo Molnar5c92d122008-12-11 13:21:10 +01003252}
3253
Thomas Gleixner0793a612008-12-04 20:12:29 +01003254/*
3255 * Allocate and initialize a counter structure
3256 */
3257static struct perf_counter *
Ingo Molnar04289bb2008-12-11 08:38:42 +01003258perf_counter_alloc(struct perf_counter_hw_event *hw_event,
3259 int cpu,
Paul Mackerras23a185c2009-02-09 22:42:47 +11003260 struct perf_counter_context *ctx,
Ingo Molnar9b51f662008-12-12 13:49:45 +01003261 struct perf_counter *group_leader,
3262 gfp_t gfpflags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003263{
Robert Richter4aeb0b42009-04-29 12:47:03 +02003264 const struct pmu *pmu;
Ingo Molnar621a01e2008-12-11 12:46:46 +01003265 struct perf_counter *counter;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003266 struct hw_perf_counter *hwc;
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003267 long err;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003268
Ingo Molnar9b51f662008-12-12 13:49:45 +01003269 counter = kzalloc(sizeof(*counter), gfpflags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003270 if (!counter)
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003271 return ERR_PTR(-ENOMEM);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003272
Ingo Molnar04289bb2008-12-11 08:38:42 +01003273 /*
3274 * Single counters are their own group leaders, with an
3275 * empty sibling list:
3276 */
3277 if (!group_leader)
3278 group_leader = counter;
3279
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003280 mutex_init(&counter->child_mutex);
3281 INIT_LIST_HEAD(&counter->child_list);
3282
Ingo Molnar04289bb2008-12-11 08:38:42 +01003283 INIT_LIST_HEAD(&counter->list_entry);
Peter Zijlstra592903c2009-03-13 12:21:36 +01003284 INIT_LIST_HEAD(&counter->event_entry);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003285 INIT_LIST_HEAD(&counter->sibling_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003286 init_waitqueue_head(&counter->waitq);
3287
Peter Zijlstra7b732a72009-03-23 18:22:10 +01003288 mutex_init(&counter->mmap_mutex);
3289
Ingo Molnar9f66a382008-12-10 12:33:23 +01003290 counter->cpu = cpu;
3291 counter->hw_event = *hw_event;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003292 counter->group_leader = group_leader;
Robert Richter4aeb0b42009-04-29 12:47:03 +02003293 counter->pmu = NULL;
Paul Mackerras23a185c2009-02-09 22:42:47 +11003294 counter->ctx = ctx;
Ingo Molnar329d8762009-05-26 08:10:00 +02003295 counter->oncpu = -1;
3296
Ingo Molnar235c7fc2008-12-21 14:43:25 +01003297 counter->state = PERF_COUNTER_STATE_INACTIVE;
Ingo Molnara86ed502008-12-17 00:43:10 +01003298 if (hw_event->disabled)
3299 counter->state = PERF_COUNTER_STATE_OFF;
3300
Robert Richter4aeb0b42009-04-29 12:47:03 +02003301 pmu = NULL;
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003302
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003303 hwc = &counter->hw;
3304 if (hw_event->freq && hw_event->irq_freq)
Peter Zijlstra2e569d32009-05-15 15:37:47 +02003305 hwc->irq_period = div64_u64(TICK_NSEC, hw_event->irq_freq);
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003306 else
3307 hwc->irq_period = hw_event->irq_period;
3308
Peter Zijlstra2023b352009-05-05 17:50:26 +02003309 /*
3310 * we currently do not support PERF_RECORD_GROUP on inherited counters
3311 */
3312 if (hw_event->inherit && (hw_event->record_type & PERF_RECORD_GROUP))
3313 goto done;
3314
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01003315 if (perf_event_raw(hw_event)) {
Robert Richter4aeb0b42009-04-29 12:47:03 +02003316 pmu = hw_perf_counter_init(counter);
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01003317 goto done;
3318 }
3319
3320 switch (perf_event_type(hw_event)) {
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003321 case PERF_TYPE_HARDWARE:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003322 pmu = hw_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003323 break;
3324
3325 case PERF_TYPE_SOFTWARE:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003326 pmu = sw_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003327 break;
3328
3329 case PERF_TYPE_TRACEPOINT:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003330 pmu = tp_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003331 break;
3332 }
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01003333done:
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003334 err = 0;
Robert Richter4aeb0b42009-04-29 12:47:03 +02003335 if (!pmu)
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003336 err = -EINVAL;
Robert Richter4aeb0b42009-04-29 12:47:03 +02003337 else if (IS_ERR(pmu))
3338 err = PTR_ERR(pmu);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003339
3340 if (err) {
3341 kfree(counter);
3342 return ERR_PTR(err);
3343 }
3344
Robert Richter4aeb0b42009-04-29 12:47:03 +02003345 counter->pmu = pmu;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003346
Peter Zijlstra7fc23a52009-05-08 18:52:21 +02003347 atomic_inc(&nr_counters);
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02003348 if (counter->hw_event.mmap)
3349 atomic_inc(&nr_mmap_tracking);
3350 if (counter->hw_event.munmap)
3351 atomic_inc(&nr_munmap_tracking);
3352 if (counter->hw_event.comm)
3353 atomic_inc(&nr_comm_tracking);
3354
Thomas Gleixner0793a612008-12-04 20:12:29 +01003355 return counter;
3356}
3357
3358/**
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003359 * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
Ingo Molnar9f66a382008-12-10 12:33:23 +01003360 *
3361 * @hw_event_uptr: event type attributes for monitoring/sampling
Thomas Gleixner0793a612008-12-04 20:12:29 +01003362 * @pid: target pid
Ingo Molnar9f66a382008-12-10 12:33:23 +01003363 * @cpu: target cpu
3364 * @group_fd: group leader counter fd
Thomas Gleixner0793a612008-12-04 20:12:29 +01003365 */
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003366SYSCALL_DEFINE5(perf_counter_open,
Paul Mackerrasf3dfd262009-02-26 22:43:46 +11003367 const struct perf_counter_hw_event __user *, hw_event_uptr,
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003368 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003369{
Ingo Molnar04289bb2008-12-11 08:38:42 +01003370 struct perf_counter *counter, *group_leader;
Ingo Molnar9f66a382008-12-10 12:33:23 +01003371 struct perf_counter_hw_event hw_event;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003372 struct perf_counter_context *ctx;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003373 struct file *counter_file = NULL;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003374 struct file *group_file = NULL;
3375 int fput_needed = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003376 int fput_needed2 = 0;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003377 int ret;
3378
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003379 /* for future expandability... */
3380 if (flags)
3381 return -EINVAL;
3382
Ingo Molnar9f66a382008-12-10 12:33:23 +01003383 if (copy_from_user(&hw_event, hw_event_uptr, sizeof(hw_event)) != 0)
Thomas Gleixnereab656a2008-12-08 19:26:59 +01003384 return -EFAULT;
3385
Ingo Molnar04289bb2008-12-11 08:38:42 +01003386 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01003387 * Get the target context (task or percpu):
3388 */
3389 ctx = find_get_context(pid, cpu);
3390 if (IS_ERR(ctx))
3391 return PTR_ERR(ctx);
3392
3393 /*
3394 * Look up the group leader (we will attach this counter to it):
Ingo Molnar04289bb2008-12-11 08:38:42 +01003395 */
3396 group_leader = NULL;
3397 if (group_fd != -1) {
3398 ret = -EINVAL;
3399 group_file = fget_light(group_fd, &fput_needed);
3400 if (!group_file)
Ingo Molnarccff2862008-12-11 11:26:29 +01003401 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003402 if (group_file->f_op != &perf_fops)
Ingo Molnarccff2862008-12-11 11:26:29 +01003403 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003404
3405 group_leader = group_file->private_data;
3406 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01003407 * Do not allow a recursive hierarchy (this new sibling
3408 * becoming part of another group-sibling):
Ingo Molnar04289bb2008-12-11 08:38:42 +01003409 */
Ingo Molnarccff2862008-12-11 11:26:29 +01003410 if (group_leader->group_leader != group_leader)
3411 goto err_put_context;
3412 /*
3413 * Do not allow to attach to a group in a different
3414 * task or CPU context:
3415 */
3416 if (group_leader->ctx != ctx)
3417 goto err_put_context;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11003418 /*
3419 * Only a group leader can be exclusive or pinned
3420 */
3421 if (hw_event.exclusive || hw_event.pinned)
3422 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003423 }
3424
Paul Mackerras23a185c2009-02-09 22:42:47 +11003425 counter = perf_counter_alloc(&hw_event, cpu, ctx, group_leader,
3426 GFP_KERNEL);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003427 ret = PTR_ERR(counter);
3428 if (IS_ERR(counter))
Thomas Gleixner0793a612008-12-04 20:12:29 +01003429 goto err_put_context;
3430
Thomas Gleixner0793a612008-12-04 20:12:29 +01003431 ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
3432 if (ret < 0)
Ingo Molnar9b51f662008-12-12 13:49:45 +01003433 goto err_free_put_context;
3434
3435 counter_file = fget_light(ret, &fput_needed2);
3436 if (!counter_file)
3437 goto err_free_put_context;
3438
3439 counter->filp = counter_file;
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003440 WARN_ON_ONCE(ctx->parent_ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003441 mutex_lock(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003442 perf_install_in_context(ctx, counter, cpu);
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003443 ++ctx->generation;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003444 mutex_unlock(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003445
Peter Zijlstra082ff5a2009-05-23 18:29:00 +02003446 counter->owner = current;
3447 get_task_struct(current);
3448 mutex_lock(&current->perf_counter_mutex);
3449 list_add_tail(&counter->owner_entry, &current->perf_counter_list);
3450 mutex_unlock(&current->perf_counter_mutex);
3451
Ingo Molnar9b51f662008-12-12 13:49:45 +01003452 fput_light(counter_file, fput_needed2);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003453
Ingo Molnar04289bb2008-12-11 08:38:42 +01003454out_fput:
3455 fput_light(group_file, fput_needed);
3456
Thomas Gleixner0793a612008-12-04 20:12:29 +01003457 return ret;
3458
Ingo Molnar9b51f662008-12-12 13:49:45 +01003459err_free_put_context:
Thomas Gleixner0793a612008-12-04 20:12:29 +01003460 kfree(counter);
3461
3462err_put_context:
Paul Mackerrasc93f7662009-05-28 22:18:17 +10003463 put_ctx(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003464
Ingo Molnar04289bb2008-12-11 08:38:42 +01003465 goto out_fput;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003466}
3467
Ingo Molnar9b51f662008-12-12 13:49:45 +01003468/*
Ingo Molnar9b51f662008-12-12 13:49:45 +01003469 * inherit a counter from parent task to child task:
3470 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11003471static struct perf_counter *
Ingo Molnar9b51f662008-12-12 13:49:45 +01003472inherit_counter(struct perf_counter *parent_counter,
3473 struct task_struct *parent,
3474 struct perf_counter_context *parent_ctx,
3475 struct task_struct *child,
Paul Mackerrasd859e292009-01-17 18:10:22 +11003476 struct perf_counter *group_leader,
Ingo Molnar9b51f662008-12-12 13:49:45 +01003477 struct perf_counter_context *child_ctx)
3478{
3479 struct perf_counter *child_counter;
3480
Paul Mackerrasd859e292009-01-17 18:10:22 +11003481 /*
3482 * Instead of creating recursive hierarchies of counters,
3483 * we link inherited counters back to the original parent,
3484 * which has a filp for sure, which we use as the reference
3485 * count:
3486 */
3487 if (parent_counter->parent)
3488 parent_counter = parent_counter->parent;
3489
Ingo Molnar9b51f662008-12-12 13:49:45 +01003490 child_counter = perf_counter_alloc(&parent_counter->hw_event,
Paul Mackerras23a185c2009-02-09 22:42:47 +11003491 parent_counter->cpu, child_ctx,
3492 group_leader, GFP_KERNEL);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003493 if (IS_ERR(child_counter))
3494 return child_counter;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10003495 get_ctx(child_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003496
3497 /*
Paul Mackerras564c2b22009-05-22 14:27:22 +10003498 * Make the child state follow the state of the parent counter,
3499 * not its hw_event.disabled bit. We hold the parent's mutex,
Ingo Molnar22a4f652009-06-01 10:13:37 +02003500 * so we won't race with perf_counter_{en, dis}able_family.
Paul Mackerras564c2b22009-05-22 14:27:22 +10003501 */
3502 if (parent_counter->state >= PERF_COUNTER_STATE_INACTIVE)
3503 child_counter->state = PERF_COUNTER_STATE_INACTIVE;
3504 else
3505 child_counter->state = PERF_COUNTER_STATE_OFF;
3506
3507 /*
Ingo Molnar9b51f662008-12-12 13:49:45 +01003508 * Link it up in the child's context:
3509 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +11003510 add_counter_to_ctx(child_counter, child_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003511
3512 child_counter->parent = parent_counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003513 /*
3514 * inherit into child's child as well:
3515 */
3516 child_counter->hw_event.inherit = 1;
3517
3518 /*
3519 * Get a reference to the parent filp - we will fput it
3520 * when the child counter exits. This is safe to do because
3521 * we are in the parent and we know that the filp still
3522 * exists and has a nonzero count:
3523 */
3524 atomic_long_inc(&parent_counter->filp->f_count);
3525
Paul Mackerrasd859e292009-01-17 18:10:22 +11003526 /*
3527 * Link this into the parent counter's child list
3528 */
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003529 WARN_ON_ONCE(parent_counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003530 mutex_lock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003531 list_add_tail(&child_counter->child_list, &parent_counter->child_list);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003532 mutex_unlock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003533
3534 return child_counter;
3535}
3536
3537static int inherit_group(struct perf_counter *parent_counter,
3538 struct task_struct *parent,
3539 struct perf_counter_context *parent_ctx,
3540 struct task_struct *child,
3541 struct perf_counter_context *child_ctx)
3542{
3543 struct perf_counter *leader;
3544 struct perf_counter *sub;
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003545 struct perf_counter *child_ctr;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003546
3547 leader = inherit_counter(parent_counter, parent, parent_ctx,
3548 child, NULL, child_ctx);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003549 if (IS_ERR(leader))
3550 return PTR_ERR(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003551 list_for_each_entry(sub, &parent_counter->sibling_list, list_entry) {
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003552 child_ctr = inherit_counter(sub, parent, parent_ctx,
3553 child, leader, child_ctx);
3554 if (IS_ERR(child_ctr))
3555 return PTR_ERR(child_ctr);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003556 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01003557 return 0;
3558}
3559
Paul Mackerrasd859e292009-01-17 18:10:22 +11003560static void sync_child_counter(struct perf_counter *child_counter,
3561 struct perf_counter *parent_counter)
3562{
Peter Zijlstra8bc20952009-05-15 20:45:59 +02003563 u64 child_val;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003564
Paul Mackerrasd859e292009-01-17 18:10:22 +11003565 child_val = atomic64_read(&child_counter->count);
3566
3567 /*
3568 * Add back the child's count to the parent's count:
3569 */
3570 atomic64_add(child_val, &parent_counter->count);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11003571 atomic64_add(child_counter->total_time_enabled,
3572 &parent_counter->child_total_time_enabled);
3573 atomic64_add(child_counter->total_time_running,
3574 &parent_counter->child_total_time_running);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003575
3576 /*
3577 * Remove this counter from the parent's list
3578 */
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003579 WARN_ON_ONCE(parent_counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003580 mutex_lock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003581 list_del_init(&child_counter->child_list);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003582 mutex_unlock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003583
3584 /*
3585 * Release the parent counter, if this was the last
3586 * reference to it.
3587 */
3588 fput(parent_counter->filp);
3589}
3590
Ingo Molnar9b51f662008-12-12 13:49:45 +01003591static void
Peter Zijlstrabbbee902009-05-29 14:25:58 +02003592__perf_counter_exit_task(struct perf_counter *child_counter,
Ingo Molnar9b51f662008-12-12 13:49:45 +01003593 struct perf_counter_context *child_ctx)
3594{
3595 struct perf_counter *parent_counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003596
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003597 update_counter_times(child_counter);
Peter Zijlstraaa9c67f2009-05-23 18:28:59 +02003598 perf_counter_remove_from_context(child_counter);
Ingo Molnar0cc0c022008-12-14 23:20:36 +01003599
Ingo Molnar9b51f662008-12-12 13:49:45 +01003600 parent_counter = child_counter->parent;
3601 /*
3602 * It can happen that parent exits first, and has counters
3603 * that are still around due to the child reference. These
3604 * counters need to be zapped - but otherwise linger.
3605 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11003606 if (parent_counter) {
3607 sync_child_counter(child_counter, parent_counter);
Peter Zijlstraf1600952009-03-19 20:26:16 +01003608 free_counter(child_counter);
Paul Mackerras4bcf3492009-02-11 13:53:19 +01003609 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01003610}
3611
3612/*
Paul Mackerrasd859e292009-01-17 18:10:22 +11003613 * When a child task exits, feed back counter values to parent counters.
Ingo Molnar9b51f662008-12-12 13:49:45 +01003614 */
3615void perf_counter_exit_task(struct task_struct *child)
3616{
3617 struct perf_counter *child_counter, *tmp;
3618 struct perf_counter_context *child_ctx;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003619 unsigned long flags;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003620
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003621 if (likely(!child->perf_counter_ctxp))
Ingo Molnar9b51f662008-12-12 13:49:45 +01003622 return;
3623
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003624 local_irq_save(flags);
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003625 /*
3626 * We can't reschedule here because interrupts are disabled,
3627 * and either child is current or it is a task that can't be
3628 * scheduled, so we are now safe from rescheduling changing
3629 * our context.
3630 */
3631 child_ctx = child->perf_counter_ctxp;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003632 __perf_counter_task_sched_out(child_ctx);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10003633
3634 /*
3635 * Take the context lock here so that if find_get_context is
3636 * reading child->perf_counter_ctxp, we wait until it has
3637 * incremented the context's refcount before we do put_ctx below.
3638 */
3639 spin_lock(&child_ctx->lock);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003640 child->perf_counter_ctxp = NULL;
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003641 if (child_ctx->parent_ctx) {
3642 /*
3643 * This context is a clone; unclone it so it can't get
3644 * swapped to another process while we're removing all
3645 * the counters from it.
3646 */
3647 put_ctx(child_ctx->parent_ctx);
3648 child_ctx->parent_ctx = NULL;
3649 }
Paul Mackerrasc93f7662009-05-28 22:18:17 +10003650 spin_unlock(&child_ctx->lock);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003651 local_irq_restore(flags);
3652
3653 mutex_lock(&child_ctx->mutex);
3654
Peter Zijlstra8bc20952009-05-15 20:45:59 +02003655again:
Ingo Molnar9b51f662008-12-12 13:49:45 +01003656 list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list,
3657 list_entry)
Peter Zijlstrabbbee902009-05-29 14:25:58 +02003658 __perf_counter_exit_task(child_counter, child_ctx);
Peter Zijlstra8bc20952009-05-15 20:45:59 +02003659
3660 /*
3661 * If the last counter was a group counter, it will have appended all
3662 * its siblings to the list, but we obtained 'tmp' before that which
3663 * will still point to the list head terminating the iteration.
3664 */
3665 if (!list_empty(&child_ctx->counter_list))
3666 goto again;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003667
3668 mutex_unlock(&child_ctx->mutex);
3669
3670 put_ctx(child_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003671}
3672
3673/*
Peter Zijlstrabbbee902009-05-29 14:25:58 +02003674 * free an unexposed, unused context as created by inheritance by
3675 * init_task below, used by fork() in case of fail.
3676 */
3677void perf_counter_free_task(struct task_struct *task)
3678{
3679 struct perf_counter_context *ctx = task->perf_counter_ctxp;
3680 struct perf_counter *counter, *tmp;
3681
3682 if (!ctx)
3683 return;
3684
3685 mutex_lock(&ctx->mutex);
3686again:
3687 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry) {
3688 struct perf_counter *parent = counter->parent;
3689
3690 if (WARN_ON_ONCE(!parent))
3691 continue;
3692
3693 mutex_lock(&parent->child_mutex);
3694 list_del_init(&counter->child_list);
3695 mutex_unlock(&parent->child_mutex);
3696
3697 fput(parent->filp);
3698
3699 list_del_counter(counter, ctx);
3700 free_counter(counter);
3701 }
3702
3703 if (!list_empty(&ctx->counter_list))
3704 goto again;
3705
3706 mutex_unlock(&ctx->mutex);
3707
3708 put_ctx(ctx);
3709}
3710
3711/*
Ingo Molnar9b51f662008-12-12 13:49:45 +01003712 * Initialize the perf_counter context in task_struct
3713 */
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02003714int perf_counter_init_task(struct task_struct *child)
Ingo Molnar9b51f662008-12-12 13:49:45 +01003715{
3716 struct perf_counter_context *child_ctx, *parent_ctx;
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003717 struct perf_counter_context *cloned_ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003718 struct perf_counter *counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003719 struct task_struct *parent = current;
Paul Mackerras564c2b22009-05-22 14:27:22 +10003720 int inherited_all = 1;
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02003721 int ret = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003722
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003723 child->perf_counter_ctxp = NULL;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003724
Peter Zijlstra082ff5a2009-05-23 18:29:00 +02003725 mutex_init(&child->perf_counter_mutex);
3726 INIT_LIST_HEAD(&child->perf_counter_list);
3727
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003728 if (likely(!parent->perf_counter_ctxp))
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02003729 return 0;
3730
Ingo Molnar9b51f662008-12-12 13:49:45 +01003731 /*
3732 * This is executed from the parent task context, so inherit
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003733 * counters that have been marked for cloning.
3734 * First allocate and initialize a context for the child.
Ingo Molnar9b51f662008-12-12 13:49:45 +01003735 */
3736
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003737 child_ctx = kmalloc(sizeof(struct perf_counter_context), GFP_KERNEL);
3738 if (!child_ctx)
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02003739 return -ENOMEM;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003740
3741 __perf_counter_init_context(child_ctx, child);
3742 child->perf_counter_ctxp = child_ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10003743 get_task_struct(child);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003744
Ingo Molnar9b51f662008-12-12 13:49:45 +01003745 /*
Paul Mackerras25346b932009-06-01 17:48:12 +10003746 * If the parent's context is a clone, pin it so it won't get
3747 * swapped under us.
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003748 */
Paul Mackerras25346b932009-06-01 17:48:12 +10003749 parent_ctx = perf_pin_task_context(parent);
3750
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003751 /*
3752 * No need to check if parent_ctx != NULL here; since we saw
3753 * it non-NULL earlier, the only reason for it to become NULL
3754 * is if we exit, and since we're currently in the middle of
3755 * a fork we can't be exiting at the same time.
3756 */
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003757
3758 /*
Ingo Molnar9b51f662008-12-12 13:49:45 +01003759 * Lock the parent list. No need to lock the child - not PID
3760 * hashed yet and not running, so nobody can access it.
3761 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11003762 mutex_lock(&parent_ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003763
3764 /*
3765 * We dont have to disable NMIs - we are only looking at
3766 * the list, not manipulating it:
3767 */
Peter Zijlstrad7b629a2009-05-20 12:21:19 +02003768 list_for_each_entry_rcu(counter, &parent_ctx->event_list, event_entry) {
3769 if (counter != counter->group_leader)
3770 continue;
3771
Paul Mackerras564c2b22009-05-22 14:27:22 +10003772 if (!counter->hw_event.inherit) {
3773 inherited_all = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003774 continue;
Paul Mackerras564c2b22009-05-22 14:27:22 +10003775 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01003776
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02003777 ret = inherit_group(counter, parent, parent_ctx,
3778 child, child_ctx);
3779 if (ret) {
Paul Mackerras564c2b22009-05-22 14:27:22 +10003780 inherited_all = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003781 break;
Paul Mackerras564c2b22009-05-22 14:27:22 +10003782 }
3783 }
3784
3785 if (inherited_all) {
3786 /*
3787 * Mark the child context as a clone of the parent
3788 * context, or of whatever the parent is a clone of.
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003789 * Note that if the parent is a clone, it could get
3790 * uncloned at any point, but that doesn't matter
3791 * because the list of counters and the generation
3792 * count can't have changed since we took the mutex.
Paul Mackerras564c2b22009-05-22 14:27:22 +10003793 */
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003794 cloned_ctx = rcu_dereference(parent_ctx->parent_ctx);
3795 if (cloned_ctx) {
3796 child_ctx->parent_ctx = cloned_ctx;
Paul Mackerras25346b932009-06-01 17:48:12 +10003797 child_ctx->parent_gen = parent_ctx->parent_gen;
Paul Mackerras564c2b22009-05-22 14:27:22 +10003798 } else {
3799 child_ctx->parent_ctx = parent_ctx;
3800 child_ctx->parent_gen = parent_ctx->generation;
3801 }
3802 get_ctx(child_ctx->parent_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003803 }
3804
Paul Mackerrasd859e292009-01-17 18:10:22 +11003805 mutex_unlock(&parent_ctx->mutex);
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02003806
Paul Mackerras25346b932009-06-01 17:48:12 +10003807 perf_unpin_context(parent_ctx);
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003808
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02003809 return ret;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003810}
3811
Ingo Molnar04289bb2008-12-11 08:38:42 +01003812static void __cpuinit perf_counter_init_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003813{
Ingo Molnar04289bb2008-12-11 08:38:42 +01003814 struct perf_cpu_context *cpuctx;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003815
Ingo Molnar04289bb2008-12-11 08:38:42 +01003816 cpuctx = &per_cpu(perf_cpu_context, cpu);
3817 __perf_counter_init_context(&cpuctx->ctx, NULL);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003818
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003819 spin_lock(&perf_resource_lock);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003820 cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu;
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003821 spin_unlock(&perf_resource_lock);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003822
Paul Mackerras01d02872009-01-14 13:44:19 +11003823 hw_perf_counter_setup(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003824}
3825
3826#ifdef CONFIG_HOTPLUG_CPU
Ingo Molnar04289bb2008-12-11 08:38:42 +01003827static void __perf_counter_exit_cpu(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003828{
3829 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
3830 struct perf_counter_context *ctx = &cpuctx->ctx;
3831 struct perf_counter *counter, *tmp;
3832
Ingo Molnar04289bb2008-12-11 08:38:42 +01003833 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry)
3834 __perf_counter_remove_from_context(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003835}
Ingo Molnar04289bb2008-12-11 08:38:42 +01003836static void perf_counter_exit_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003837{
Paul Mackerrasd859e292009-01-17 18:10:22 +11003838 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
3839 struct perf_counter_context *ctx = &cpuctx->ctx;
3840
3841 mutex_lock(&ctx->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003842 smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003843 mutex_unlock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003844}
3845#else
Ingo Molnar04289bb2008-12-11 08:38:42 +01003846static inline void perf_counter_exit_cpu(int cpu) { }
Thomas Gleixner0793a612008-12-04 20:12:29 +01003847#endif
3848
3849static int __cpuinit
3850perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
3851{
3852 unsigned int cpu = (long)hcpu;
3853
3854 switch (action) {
3855
3856 case CPU_UP_PREPARE:
3857 case CPU_UP_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01003858 perf_counter_init_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003859 break;
3860
3861 case CPU_DOWN_PREPARE:
3862 case CPU_DOWN_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01003863 perf_counter_exit_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003864 break;
3865
3866 default:
3867 break;
3868 }
3869
3870 return NOTIFY_OK;
3871}
3872
Paul Mackerrasf38b0822009-06-02 21:05:16 +10003873/*
3874 * This has to have a higher priority than migration_notifier in sched.c.
3875 */
Thomas Gleixner0793a612008-12-04 20:12:29 +01003876static struct notifier_block __cpuinitdata perf_cpu_nb = {
3877 .notifier_call = perf_cpu_notify,
Paul Mackerrasf38b0822009-06-02 21:05:16 +10003878 .priority = 20,
Thomas Gleixner0793a612008-12-04 20:12:29 +01003879};
3880
Ingo Molnar0d905bc2009-05-04 19:13:30 +02003881void __init perf_counter_init(void)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003882{
3883 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
3884 (void *)(long)smp_processor_id());
3885 register_cpu_notifier(&perf_cpu_nb);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003886}
Thomas Gleixner0793a612008-12-04 20:12:29 +01003887
3888static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
3889{
3890 return sprintf(buf, "%d\n", perf_reserved_percpu);
3891}
3892
3893static ssize_t
3894perf_set_reserve_percpu(struct sysdev_class *class,
3895 const char *buf,
3896 size_t count)
3897{
3898 struct perf_cpu_context *cpuctx;
3899 unsigned long val;
3900 int err, cpu, mpt;
3901
3902 err = strict_strtoul(buf, 10, &val);
3903 if (err)
3904 return err;
3905 if (val > perf_max_counters)
3906 return -EINVAL;
3907
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003908 spin_lock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003909 perf_reserved_percpu = val;
3910 for_each_online_cpu(cpu) {
3911 cpuctx = &per_cpu(perf_cpu_context, cpu);
3912 spin_lock_irq(&cpuctx->ctx.lock);
3913 mpt = min(perf_max_counters - cpuctx->ctx.nr_counters,
3914 perf_max_counters - perf_reserved_percpu);
3915 cpuctx->max_pertask = mpt;
3916 spin_unlock_irq(&cpuctx->ctx.lock);
3917 }
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003918 spin_unlock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003919
3920 return count;
3921}
3922
3923static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
3924{
3925 return sprintf(buf, "%d\n", perf_overcommit);
3926}
3927
3928static ssize_t
3929perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
3930{
3931 unsigned long val;
3932 int err;
3933
3934 err = strict_strtoul(buf, 10, &val);
3935 if (err)
3936 return err;
3937 if (val > 1)
3938 return -EINVAL;
3939
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003940 spin_lock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003941 perf_overcommit = val;
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003942 spin_unlock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003943
3944 return count;
3945}
3946
3947static SYSDEV_CLASS_ATTR(
3948 reserve_percpu,
3949 0644,
3950 perf_show_reserve_percpu,
3951 perf_set_reserve_percpu
3952 );
3953
3954static SYSDEV_CLASS_ATTR(
3955 overcommit,
3956 0644,
3957 perf_show_overcommit,
3958 perf_set_overcommit
3959 );
3960
3961static struct attribute *perfclass_attrs[] = {
3962 &attr_reserve_percpu.attr,
3963 &attr_overcommit.attr,
3964 NULL
3965};
3966
3967static struct attribute_group perfclass_attr_group = {
3968 .attrs = perfclass_attrs,
3969 .name = "perf_counters",
3970};
3971
3972static int __init perf_counter_sysfs_init(void)
3973{
3974 return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
3975 &perfclass_attr_group);
3976}
3977device_initcall(perf_counter_sysfs_init);