blob: ff8b4636f8451896e604999b53f1acc82aca0879 [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>
19#include <linux/ptrace.h>
20#include <linux/percpu.h>
Peter Zijlstrab9cacc72009-03-25 12:30:22 +010021#include <linux/vmstat.h>
22#include <linux/hardirq.h>
23#include <linux/rculist.h>
Thomas Gleixner0793a612008-12-04 20:12:29 +010024#include <linux/uaccess.h>
25#include <linux/syscalls.h>
26#include <linux/anon_inodes.h>
Ingo Molnaraa9c4c02008-12-17 14:10:57 +010027#include <linux/kernel_stat.h>
Thomas Gleixner0793a612008-12-04 20:12:29 +010028#include <linux/perf_counter.h>
Peter Zijlstra0a4a9392009-03-30 19:07:05 +020029#include <linux/dcache.h>
Thomas Gleixner0793a612008-12-04 20:12:29 +010030
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(); }
Paul Mackerras3cbed422009-01-09 16:43:42 +110068int __weak hw_perf_group_sched_in(struct perf_counter *group_leader,
69 struct perf_cpu_context *cpuctx,
70 struct perf_counter_context *ctx, int cpu)
71{
72 return 0;
73}
Thomas Gleixner0793a612008-12-04 20:12:29 +010074
Paul Mackerras4eb96fc2009-01-09 17:24:34 +110075void __weak perf_counter_print_debug(void) { }
76
Peter Zijlstra9e35ad32009-05-13 16:21:38 +020077static DEFINE_PER_CPU(int, disable_count);
78
79void __perf_disable(void)
80{
81 __get_cpu_var(disable_count)++;
82}
83
84bool __perf_enable(void)
85{
86 return !--__get_cpu_var(disable_count);
87}
88
89void perf_disable(void)
90{
91 __perf_disable();
92 hw_perf_disable();
93}
Peter Zijlstra9e35ad32009-05-13 16:21:38 +020094
95void perf_enable(void)
96{
97 if (__perf_enable())
98 hw_perf_enable();
99}
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200100
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000101static void get_ctx(struct perf_counter_context *ctx)
102{
103 atomic_inc(&ctx->refcount);
104}
105
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000106static void free_ctx(struct rcu_head *head)
107{
108 struct perf_counter_context *ctx;
109
110 ctx = container_of(head, struct perf_counter_context, rcu_head);
111 kfree(ctx);
112}
113
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000114static void put_ctx(struct perf_counter_context *ctx)
115{
Paul Mackerras564c2b22009-05-22 14:27:22 +1000116 if (atomic_dec_and_test(&ctx->refcount)) {
117 if (ctx->parent_ctx)
118 put_ctx(ctx->parent_ctx);
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000119 if (ctx->task)
120 put_task_struct(ctx->task);
121 call_rcu(&ctx->rcu_head, free_ctx);
Paul Mackerras564c2b22009-05-22 14:27:22 +1000122 }
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000123}
124
Peter Zijlstrafccc7142009-05-23 18:28:56 +0200125/*
Paul Mackerras25346b932009-06-01 17:48:12 +1000126 * Get the perf_counter_context for a task and lock it.
127 * This has to cope with with the fact that until it is locked,
128 * the context could get moved to another task.
129 */
130static struct perf_counter_context *perf_lock_task_context(
131 struct task_struct *task, unsigned long *flags)
132{
133 struct perf_counter_context *ctx;
134
135 rcu_read_lock();
136 retry:
137 ctx = rcu_dereference(task->perf_counter_ctxp);
138 if (ctx) {
139 /*
140 * If this context is a clone of another, it might
141 * get swapped for another underneath us by
142 * perf_counter_task_sched_out, though the
143 * rcu_read_lock() protects us from any context
144 * getting freed. Lock the context and check if it
145 * got swapped before we could get the lock, and retry
146 * if so. If we locked the right context, then it
147 * can't get swapped on us any more.
148 */
149 spin_lock_irqsave(&ctx->lock, *flags);
150 if (ctx != rcu_dereference(task->perf_counter_ctxp)) {
151 spin_unlock_irqrestore(&ctx->lock, *flags);
152 goto retry;
153 }
154 }
155 rcu_read_unlock();
156 return ctx;
157}
158
159/*
160 * Get the context for a task and increment its pin_count so it
161 * can't get swapped to another task. This also increments its
162 * reference count so that the context can't get freed.
163 */
164static struct perf_counter_context *perf_pin_task_context(struct task_struct *task)
165{
166 struct perf_counter_context *ctx;
167 unsigned long flags;
168
169 ctx = perf_lock_task_context(task, &flags);
170 if (ctx) {
171 ++ctx->pin_count;
172 get_ctx(ctx);
173 spin_unlock_irqrestore(&ctx->lock, flags);
174 }
175 return ctx;
176}
177
178static void perf_unpin_context(struct perf_counter_context *ctx)
179{
180 unsigned long flags;
181
182 spin_lock_irqsave(&ctx->lock, flags);
183 --ctx->pin_count;
184 spin_unlock_irqrestore(&ctx->lock, flags);
185 put_ctx(ctx);
186}
187
188/*
Peter Zijlstrafccc7142009-05-23 18:28:56 +0200189 * Add a counter from the lists for its context.
190 * Must be called with ctx->mutex and ctx->lock held.
191 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100192static void
193list_add_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
194{
195 struct perf_counter *group_leader = counter->group_leader;
196
197 /*
198 * Depending on whether it is a standalone or sibling counter,
199 * add it straight to the context's counter list, or to the group
200 * leader's sibling list:
201 */
Peter Zijlstra3df5eda2009-05-08 18:52:22 +0200202 if (group_leader == counter)
Ingo Molnar04289bb2008-12-11 08:38:42 +0100203 list_add_tail(&counter->list_entry, &ctx->counter_list);
Peter Zijlstra5c148192009-03-25 12:30:23 +0100204 else {
Ingo Molnar04289bb2008-12-11 08:38:42 +0100205 list_add_tail(&counter->list_entry, &group_leader->sibling_list);
Peter Zijlstra5c148192009-03-25 12:30:23 +0100206 group_leader->nr_siblings++;
207 }
Peter Zijlstra592903c2009-03-13 12:21:36 +0100208
209 list_add_rcu(&counter->event_entry, &ctx->event_list);
Peter Zijlstra8bc20952009-05-15 20:45:59 +0200210 ctx->nr_counters++;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100211}
212
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000213/*
214 * Remove a counter from the lists for its context.
Peter Zijlstrafccc7142009-05-23 18:28:56 +0200215 * Must be called with ctx->mutex and ctx->lock held.
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000216 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100217static void
218list_del_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
219{
220 struct perf_counter *sibling, *tmp;
221
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000222 if (list_empty(&counter->list_entry))
223 return;
Peter Zijlstra8bc20952009-05-15 20:45:59 +0200224 ctx->nr_counters--;
225
Ingo Molnar04289bb2008-12-11 08:38:42 +0100226 list_del_init(&counter->list_entry);
Peter Zijlstra592903c2009-03-13 12:21:36 +0100227 list_del_rcu(&counter->event_entry);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100228
Peter Zijlstra5c148192009-03-25 12:30:23 +0100229 if (counter->group_leader != counter)
230 counter->group_leader->nr_siblings--;
231
Ingo Molnar04289bb2008-12-11 08:38:42 +0100232 /*
233 * If this was a group counter with sibling counters then
234 * upgrade the siblings to singleton counters by adding them
235 * to the context list directly:
236 */
237 list_for_each_entry_safe(sibling, tmp,
238 &counter->sibling_list, list_entry) {
239
Peter Zijlstra75564232009-03-13 12:21:29 +0100240 list_move_tail(&sibling->list_entry, &ctx->counter_list);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100241 sibling->group_leader = sibling;
242 }
243}
244
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100245static void
246counter_sched_out(struct perf_counter *counter,
247 struct perf_cpu_context *cpuctx,
248 struct perf_counter_context *ctx)
249{
250 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
251 return;
252
253 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200254 counter->tstamp_stopped = ctx->time;
Robert Richter4aeb0b42009-04-29 12:47:03 +0200255 counter->pmu->disable(counter);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100256 counter->oncpu = -1;
257
258 if (!is_software_counter(counter))
259 cpuctx->active_oncpu--;
260 ctx->nr_active--;
261 if (counter->hw_event.exclusive || !cpuctx->active_oncpu)
262 cpuctx->exclusive = 0;
263}
264
Paul Mackerrasd859e292009-01-17 18:10:22 +1100265static void
266group_sched_out(struct perf_counter *group_counter,
267 struct perf_cpu_context *cpuctx,
268 struct perf_counter_context *ctx)
269{
270 struct perf_counter *counter;
271
272 if (group_counter->state != PERF_COUNTER_STATE_ACTIVE)
273 return;
274
275 counter_sched_out(group_counter, cpuctx, ctx);
276
277 /*
278 * Schedule out siblings (if any):
279 */
280 list_for_each_entry(counter, &group_counter->sibling_list, list_entry)
281 counter_sched_out(counter, cpuctx, ctx);
282
283 if (group_counter->hw_event.exclusive)
284 cpuctx->exclusive = 0;
285}
286
Thomas Gleixner0793a612008-12-04 20:12:29 +0100287/*
288 * Cross CPU call to remove a performance counter
289 *
290 * We disable the counter on the hardware level first. After that we
291 * remove it from the context list.
292 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100293static void __perf_counter_remove_from_context(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100294{
295 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
296 struct perf_counter *counter = info;
297 struct perf_counter_context *ctx = counter->ctx;
298
299 /*
300 * If this is a task context, we need to check whether it is
301 * the current task context of this cpu. If not it has been
302 * scheduled out before the smp call arrived.
303 */
Peter Zijlstra665c2142009-05-29 14:51:57 +0200304 if (ctx->task && cpuctx->task_ctx != ctx)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100305 return;
306
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200307 spin_lock(&ctx->lock);
Ingo Molnar34adc802009-05-20 20:13:28 +0200308 /*
309 * Protect the list operation against NMI by disabling the
310 * counters on a global level.
311 */
312 perf_disable();
Thomas Gleixner0793a612008-12-04 20:12:29 +0100313
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100314 counter_sched_out(counter, cpuctx, ctx);
315
Ingo Molnar04289bb2008-12-11 08:38:42 +0100316 list_del_counter(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100317
318 if (!ctx->task) {
319 /*
320 * Allow more per task counters with respect to the
321 * reservation:
322 */
323 cpuctx->max_pertask =
324 min(perf_max_counters - ctx->nr_counters,
325 perf_max_counters - perf_reserved_percpu);
326 }
327
Ingo Molnar34adc802009-05-20 20:13:28 +0200328 perf_enable();
Peter Zijlstra665c2142009-05-29 14:51:57 +0200329 spin_unlock(&ctx->lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100330}
331
332
333/*
334 * Remove the counter from a task's (or a CPU's) list of counters.
335 *
Peter Zijlstrafccc7142009-05-23 18:28:56 +0200336 * Must be called with ctx->mutex held.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100337 *
338 * CPU counters are removed with a smp call. For task counters we only
339 * call when the task is on a CPU.
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000340 *
341 * If counter->ctx is a cloned context, callers must make sure that
342 * every task struct that counter->ctx->task could possibly point to
343 * remains valid. This is OK when called from perf_release since
344 * that only calls us on the top-level context, which can't be a clone.
345 * When called from perf_counter_exit_task, it's OK because the
346 * context has been detached from its task.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100347 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100348static void perf_counter_remove_from_context(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100349{
350 struct perf_counter_context *ctx = counter->ctx;
351 struct task_struct *task = ctx->task;
352
353 if (!task) {
354 /*
355 * Per cpu counters are removed via an smp call and
356 * the removal is always sucessful.
357 */
358 smp_call_function_single(counter->cpu,
Ingo Molnar04289bb2008-12-11 08:38:42 +0100359 __perf_counter_remove_from_context,
Thomas Gleixner0793a612008-12-04 20:12:29 +0100360 counter, 1);
361 return;
362 }
363
364retry:
Ingo Molnar04289bb2008-12-11 08:38:42 +0100365 task_oncpu_function_call(task, __perf_counter_remove_from_context,
Thomas Gleixner0793a612008-12-04 20:12:29 +0100366 counter);
367
368 spin_lock_irq(&ctx->lock);
369 /*
370 * If the context is active we need to retry the smp call.
371 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100372 if (ctx->nr_active && !list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100373 spin_unlock_irq(&ctx->lock);
374 goto retry;
375 }
376
377 /*
378 * The lock prevents that this context is scheduled in so we
Ingo Molnar04289bb2008-12-11 08:38:42 +0100379 * can remove the counter safely, if the call above did not
Thomas Gleixner0793a612008-12-04 20:12:29 +0100380 * succeed.
381 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100382 if (!list_empty(&counter->list_entry)) {
Ingo Molnar04289bb2008-12-11 08:38:42 +0100383 list_del_counter(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100384 }
385 spin_unlock_irq(&ctx->lock);
386}
387
Peter Zijlstra4af49982009-04-06 11:45:10 +0200388static inline u64 perf_clock(void)
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100389{
Peter Zijlstra4af49982009-04-06 11:45:10 +0200390 return cpu_clock(smp_processor_id());
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100391}
392
393/*
394 * Update the record of the current time in a context.
395 */
Peter Zijlstra4af49982009-04-06 11:45:10 +0200396static void update_context_time(struct perf_counter_context *ctx)
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100397{
Peter Zijlstra4af49982009-04-06 11:45:10 +0200398 u64 now = perf_clock();
399
400 ctx->time += now - ctx->timestamp;
401 ctx->timestamp = now;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100402}
403
404/*
405 * Update the total_time_enabled and total_time_running fields for a counter.
406 */
407static void update_counter_times(struct perf_counter *counter)
408{
409 struct perf_counter_context *ctx = counter->ctx;
410 u64 run_end;
411
Peter Zijlstra4af49982009-04-06 11:45:10 +0200412 if (counter->state < PERF_COUNTER_STATE_INACTIVE)
413 return;
414
415 counter->total_time_enabled = ctx->time - counter->tstamp_enabled;
416
417 if (counter->state == PERF_COUNTER_STATE_INACTIVE)
418 run_end = counter->tstamp_stopped;
419 else
420 run_end = ctx->time;
421
422 counter->total_time_running = run_end - counter->tstamp_running;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100423}
424
425/*
426 * Update total_time_enabled and total_time_running for all counters in a group.
427 */
428static void update_group_times(struct perf_counter *leader)
429{
430 struct perf_counter *counter;
431
432 update_counter_times(leader);
433 list_for_each_entry(counter, &leader->sibling_list, list_entry)
434 update_counter_times(counter);
435}
436
437/*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100438 * Cross CPU call to disable a performance counter
439 */
440static void __perf_counter_disable(void *info)
441{
442 struct perf_counter *counter = info;
443 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
444 struct perf_counter_context *ctx = counter->ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100445
446 /*
447 * If this is a per-task counter, need to check whether this
448 * counter's task is the current task on this cpu.
449 */
Peter Zijlstra665c2142009-05-29 14:51:57 +0200450 if (ctx->task && cpuctx->task_ctx != ctx)
Paul Mackerrasd859e292009-01-17 18:10:22 +1100451 return;
452
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200453 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100454
455 /*
456 * If the counter is on, turn it off.
457 * If it is in error state, leave it in error state.
458 */
459 if (counter->state >= PERF_COUNTER_STATE_INACTIVE) {
Peter Zijlstra4af49982009-04-06 11:45:10 +0200460 update_context_time(ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100461 update_counter_times(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100462 if (counter == counter->group_leader)
463 group_sched_out(counter, cpuctx, ctx);
464 else
465 counter_sched_out(counter, cpuctx, ctx);
466 counter->state = PERF_COUNTER_STATE_OFF;
467 }
468
Peter Zijlstra665c2142009-05-29 14:51:57 +0200469 spin_unlock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100470}
471
472/*
473 * Disable a counter.
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000474 *
475 * If counter->ctx is a cloned context, callers must make sure that
476 * every task struct that counter->ctx->task could possibly point to
477 * remains valid. This condition is satisifed when called through
478 * perf_counter_for_each_child or perf_counter_for_each because they
479 * hold the top-level counter's child_mutex, so any descendant that
480 * goes to exit will block in sync_child_counter.
481 * When called from perf_pending_counter it's OK because counter->ctx
482 * is the current context on this CPU and preemption is disabled,
483 * hence we can't get into perf_counter_task_sched_out for this context.
Paul Mackerrasd859e292009-01-17 18:10:22 +1100484 */
485static void perf_counter_disable(struct perf_counter *counter)
486{
487 struct perf_counter_context *ctx = counter->ctx;
488 struct task_struct *task = ctx->task;
489
490 if (!task) {
491 /*
492 * Disable the counter on the cpu that it's on
493 */
494 smp_call_function_single(counter->cpu, __perf_counter_disable,
495 counter, 1);
496 return;
497 }
498
499 retry:
500 task_oncpu_function_call(task, __perf_counter_disable, counter);
501
502 spin_lock_irq(&ctx->lock);
503 /*
504 * If the counter is still active, we need to retry the cross-call.
505 */
506 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
507 spin_unlock_irq(&ctx->lock);
508 goto retry;
509 }
510
511 /*
512 * Since we have the lock this context can't be scheduled
513 * in, so we can change the state safely.
514 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100515 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
516 update_counter_times(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100517 counter->state = PERF_COUNTER_STATE_OFF;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100518 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100519
520 spin_unlock_irq(&ctx->lock);
521}
522
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100523static int
524counter_sched_in(struct perf_counter *counter,
525 struct perf_cpu_context *cpuctx,
526 struct perf_counter_context *ctx,
527 int cpu)
528{
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100529 if (counter->state <= PERF_COUNTER_STATE_OFF)
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100530 return 0;
531
532 counter->state = PERF_COUNTER_STATE_ACTIVE;
533 counter->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */
534 /*
535 * The new state must be visible before we turn it on in the hardware:
536 */
537 smp_wmb();
538
Robert Richter4aeb0b42009-04-29 12:47:03 +0200539 if (counter->pmu->enable(counter)) {
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100540 counter->state = PERF_COUNTER_STATE_INACTIVE;
541 counter->oncpu = -1;
542 return -EAGAIN;
543 }
544
Peter Zijlstra4af49982009-04-06 11:45:10 +0200545 counter->tstamp_running += ctx->time - counter->tstamp_stopped;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100546
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100547 if (!is_software_counter(counter))
548 cpuctx->active_oncpu++;
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100549 ctx->nr_active++;
550
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100551 if (counter->hw_event.exclusive)
552 cpuctx->exclusive = 1;
553
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100554 return 0;
555}
556
Paul Mackerras6751b712009-05-11 12:08:02 +1000557static int
558group_sched_in(struct perf_counter *group_counter,
559 struct perf_cpu_context *cpuctx,
560 struct perf_counter_context *ctx,
561 int cpu)
562{
563 struct perf_counter *counter, *partial_group;
564 int ret;
565
566 if (group_counter->state == PERF_COUNTER_STATE_OFF)
567 return 0;
568
569 ret = hw_perf_group_sched_in(group_counter, cpuctx, ctx, cpu);
570 if (ret)
571 return ret < 0 ? ret : 0;
572
573 group_counter->prev_state = group_counter->state;
574 if (counter_sched_in(group_counter, cpuctx, ctx, cpu))
575 return -EAGAIN;
576
577 /*
578 * Schedule in siblings as one group (if any):
579 */
580 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
581 counter->prev_state = counter->state;
582 if (counter_sched_in(counter, cpuctx, ctx, cpu)) {
583 partial_group = counter;
584 goto group_error;
585 }
586 }
587
588 return 0;
589
590group_error:
591 /*
592 * Groups can be scheduled in as one unit only, so undo any
593 * partial group before returning:
594 */
595 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
596 if (counter == partial_group)
597 break;
598 counter_sched_out(counter, cpuctx, ctx);
599 }
600 counter_sched_out(group_counter, cpuctx, ctx);
601
602 return -EAGAIN;
603}
604
Thomas Gleixner0793a612008-12-04 20:12:29 +0100605/*
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100606 * Return 1 for a group consisting entirely of software counters,
607 * 0 if the group contains any hardware counters.
608 */
609static int is_software_only_group(struct perf_counter *leader)
610{
611 struct perf_counter *counter;
612
613 if (!is_software_counter(leader))
614 return 0;
Peter Zijlstra5c148192009-03-25 12:30:23 +0100615
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100616 list_for_each_entry(counter, &leader->sibling_list, list_entry)
617 if (!is_software_counter(counter))
618 return 0;
Peter Zijlstra5c148192009-03-25 12:30:23 +0100619
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100620 return 1;
621}
622
623/*
624 * Work out whether we can put this counter group on the CPU now.
625 */
626static int group_can_go_on(struct perf_counter *counter,
627 struct perf_cpu_context *cpuctx,
628 int can_add_hw)
629{
630 /*
631 * Groups consisting entirely of software counters can always go on.
632 */
633 if (is_software_only_group(counter))
634 return 1;
635 /*
636 * If an exclusive group is already on, no other hardware
637 * counters can go on.
638 */
639 if (cpuctx->exclusive)
640 return 0;
641 /*
642 * If this group is exclusive and there are already
643 * counters on the CPU, it can't go on.
644 */
645 if (counter->hw_event.exclusive && cpuctx->active_oncpu)
646 return 0;
647 /*
648 * Otherwise, try to add it if all previous groups were able
649 * to go on.
650 */
651 return can_add_hw;
652}
653
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100654static void add_counter_to_ctx(struct perf_counter *counter,
655 struct perf_counter_context *ctx)
656{
657 list_add_counter(counter, ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100658 counter->prev_state = PERF_COUNTER_STATE_OFF;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200659 counter->tstamp_enabled = ctx->time;
660 counter->tstamp_running = ctx->time;
661 counter->tstamp_stopped = ctx->time;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100662}
663
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100664/*
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100665 * Cross CPU call to install and enable a performance counter
Peter Zijlstra682076a2009-05-23 18:28:57 +0200666 *
667 * Must be called with ctx->mutex held
Thomas Gleixner0793a612008-12-04 20:12:29 +0100668 */
669static void __perf_install_in_context(void *info)
670{
671 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
672 struct perf_counter *counter = info;
673 struct perf_counter_context *ctx = counter->ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100674 struct perf_counter *leader = counter->group_leader;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100675 int cpu = smp_processor_id();
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100676 int err;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100677
678 /*
679 * If this is a task context, we need to check whether it is
680 * the current task context of this cpu. If not it has been
681 * scheduled out before the smp call arrived.
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000682 * Or possibly this is the right context but it isn't
683 * on this cpu because it had no counters.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100684 */
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000685 if (ctx->task && cpuctx->task_ctx != ctx) {
Peter Zijlstra665c2142009-05-29 14:51:57 +0200686 if (cpuctx->task_ctx || ctx->task != current)
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000687 return;
688 cpuctx->task_ctx = ctx;
689 }
Thomas Gleixner0793a612008-12-04 20:12:29 +0100690
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200691 spin_lock(&ctx->lock);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000692 ctx->is_active = 1;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200693 update_context_time(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100694
695 /*
696 * Protect the list operation against NMI by disabling the
697 * counters on a global level. NOP for non NMI based counters.
698 */
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200699 perf_disable();
Thomas Gleixner0793a612008-12-04 20:12:29 +0100700
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100701 add_counter_to_ctx(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100702
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100703 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100704 * Don't put the counter on if it is disabled or if
705 * it is in a group and the group isn't on.
706 */
707 if (counter->state != PERF_COUNTER_STATE_INACTIVE ||
708 (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE))
709 goto unlock;
710
711 /*
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100712 * An exclusive counter can't go on if there are already active
713 * hardware counters, and no hardware counter can go on if there
714 * is already an exclusive counter on.
715 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100716 if (!group_can_go_on(counter, cpuctx, 1))
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100717 err = -EEXIST;
718 else
719 err = counter_sched_in(counter, cpuctx, ctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100720
Paul Mackerrasd859e292009-01-17 18:10:22 +1100721 if (err) {
722 /*
723 * This counter couldn't go on. If it is in a group
724 * then we have to pull the whole group off.
725 * If the counter group is pinned then put it in error state.
726 */
727 if (leader != counter)
728 group_sched_out(leader, cpuctx, ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100729 if (leader->hw_event.pinned) {
730 update_group_times(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100731 leader->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100732 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100733 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100734
735 if (!err && !ctx->task && cpuctx->max_pertask)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100736 cpuctx->max_pertask--;
737
Paul Mackerrasd859e292009-01-17 18:10:22 +1100738 unlock:
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200739 perf_enable();
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100740
Peter Zijlstra665c2142009-05-29 14:51:57 +0200741 spin_unlock(&ctx->lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100742}
743
744/*
745 * Attach a performance counter to a context
746 *
747 * First we add the counter to the list with the hardware enable bit
748 * in counter->hw_config cleared.
749 *
750 * If the counter is attached to a task which is on a CPU we use a smp
751 * call to enable it in the task context. The task might have been
752 * scheduled away, but we check this in the smp call again.
Paul Mackerrasd859e292009-01-17 18:10:22 +1100753 *
754 * Must be called with ctx->mutex held.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100755 */
756static void
757perf_install_in_context(struct perf_counter_context *ctx,
758 struct perf_counter *counter,
759 int cpu)
760{
761 struct task_struct *task = ctx->task;
762
Thomas Gleixner0793a612008-12-04 20:12:29 +0100763 if (!task) {
764 /*
765 * Per cpu counters are installed via an smp call and
766 * the install is always sucessful.
767 */
768 smp_call_function_single(cpu, __perf_install_in_context,
769 counter, 1);
770 return;
771 }
772
Thomas Gleixner0793a612008-12-04 20:12:29 +0100773retry:
774 task_oncpu_function_call(task, __perf_install_in_context,
775 counter);
776
777 spin_lock_irq(&ctx->lock);
778 /*
Thomas Gleixner0793a612008-12-04 20:12:29 +0100779 * we need to retry the smp call.
780 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100781 if (ctx->is_active && list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100782 spin_unlock_irq(&ctx->lock);
783 goto retry;
784 }
785
786 /*
787 * The lock prevents that this context is scheduled in so we
788 * can add the counter safely, if it the call above did not
789 * succeed.
790 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100791 if (list_empty(&counter->list_entry))
792 add_counter_to_ctx(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100793 spin_unlock_irq(&ctx->lock);
794}
795
Paul Mackerrasd859e292009-01-17 18:10:22 +1100796/*
797 * Cross CPU call to enable a performance counter
798 */
799static void __perf_counter_enable(void *info)
Ingo Molnar04289bb2008-12-11 08:38:42 +0100800{
Paul Mackerrasd859e292009-01-17 18:10:22 +1100801 struct perf_counter *counter = info;
802 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
803 struct perf_counter_context *ctx = counter->ctx;
804 struct perf_counter *leader = counter->group_leader;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100805 int err;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100806
807 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100808 * If this is a per-task counter, need to check whether this
809 * counter's task is the current task on this cpu.
Ingo Molnar04289bb2008-12-11 08:38:42 +0100810 */
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000811 if (ctx->task && cpuctx->task_ctx != ctx) {
Peter Zijlstra665c2142009-05-29 14:51:57 +0200812 if (cpuctx->task_ctx || ctx->task != current)
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000813 return;
814 cpuctx->task_ctx = ctx;
815 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100816
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200817 spin_lock(&ctx->lock);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000818 ctx->is_active = 1;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200819 update_context_time(ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100820
Paul Mackerrasc07c99b2009-02-13 22:10:34 +1100821 counter->prev_state = counter->state;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100822 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
823 goto unlock;
824 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200825 counter->tstamp_enabled = ctx->time - counter->total_time_enabled;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100826
827 /*
828 * If the counter is in a group and isn't the group leader,
829 * then don't put it on unless the group is on.
830 */
831 if (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE)
832 goto unlock;
833
Paul Mackerrase758a332009-05-12 21:59:01 +1000834 if (!group_can_go_on(counter, cpuctx, 1)) {
Paul Mackerrasd859e292009-01-17 18:10:22 +1100835 err = -EEXIST;
Paul Mackerrase758a332009-05-12 21:59:01 +1000836 } else {
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200837 perf_disable();
Paul Mackerrase758a332009-05-12 21:59:01 +1000838 if (counter == leader)
839 err = group_sched_in(counter, cpuctx, ctx,
840 smp_processor_id());
841 else
842 err = counter_sched_in(counter, cpuctx, ctx,
843 smp_processor_id());
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200844 perf_enable();
Paul Mackerrase758a332009-05-12 21:59:01 +1000845 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100846
847 if (err) {
848 /*
849 * If this counter can't go on and it's part of a
850 * group, then the whole group has to come off.
851 */
852 if (leader != counter)
853 group_sched_out(leader, cpuctx, ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100854 if (leader->hw_event.pinned) {
855 update_group_times(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100856 leader->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100857 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100858 }
859
860 unlock:
Peter Zijlstra665c2142009-05-29 14:51:57 +0200861 spin_unlock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100862}
863
864/*
865 * Enable a counter.
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000866 *
867 * If counter->ctx is a cloned context, callers must make sure that
868 * every task struct that counter->ctx->task could possibly point to
869 * remains valid. This condition is satisfied when called through
870 * perf_counter_for_each_child or perf_counter_for_each as described
871 * for perf_counter_disable.
Paul Mackerrasd859e292009-01-17 18:10:22 +1100872 */
873static void perf_counter_enable(struct perf_counter *counter)
874{
875 struct perf_counter_context *ctx = counter->ctx;
876 struct task_struct *task = ctx->task;
877
878 if (!task) {
879 /*
880 * Enable the counter on the cpu that it's on
881 */
882 smp_call_function_single(counter->cpu, __perf_counter_enable,
883 counter, 1);
884 return;
885 }
886
887 spin_lock_irq(&ctx->lock);
888 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
889 goto out;
890
891 /*
892 * If the counter is in error state, clear that first.
893 * That way, if we see the counter in error state below, we
894 * know that it has gone back into error state, as distinct
895 * from the task having been scheduled away before the
896 * cross-call arrived.
897 */
898 if (counter->state == PERF_COUNTER_STATE_ERROR)
899 counter->state = PERF_COUNTER_STATE_OFF;
900
901 retry:
902 spin_unlock_irq(&ctx->lock);
903 task_oncpu_function_call(task, __perf_counter_enable, counter);
904
905 spin_lock_irq(&ctx->lock);
906
907 /*
908 * If the context is active and the counter is still off,
909 * we need to retry the cross-call.
910 */
911 if (ctx->is_active && counter->state == PERF_COUNTER_STATE_OFF)
912 goto retry;
913
914 /*
915 * Since we have the lock this context can't be scheduled
916 * in, so we can change the state safely.
917 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100918 if (counter->state == PERF_COUNTER_STATE_OFF) {
Paul Mackerrasd859e292009-01-17 18:10:22 +1100919 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200920 counter->tstamp_enabled =
921 ctx->time - counter->total_time_enabled;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100922 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100923 out:
924 spin_unlock_irq(&ctx->lock);
925}
926
Peter Zijlstra2023b352009-05-05 17:50:26 +0200927static int perf_counter_refresh(struct perf_counter *counter, int refresh)
Peter Zijlstra79f14642009-04-06 11:45:07 +0200928{
Peter Zijlstra2023b352009-05-05 17:50:26 +0200929 /*
930 * not supported on inherited counters
931 */
932 if (counter->hw_event.inherit)
933 return -EINVAL;
934
Peter Zijlstra79f14642009-04-06 11:45:07 +0200935 atomic_add(refresh, &counter->event_limit);
936 perf_counter_enable(counter);
Peter Zijlstra2023b352009-05-05 17:50:26 +0200937
938 return 0;
Peter Zijlstra79f14642009-04-06 11:45:07 +0200939}
940
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100941void __perf_counter_sched_out(struct perf_counter_context *ctx,
942 struct perf_cpu_context *cpuctx)
943{
944 struct perf_counter *counter;
945
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100946 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100947 ctx->is_active = 0;
948 if (likely(!ctx->nr_counters))
949 goto out;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200950 update_context_time(ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100951
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200952 perf_disable();
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100953 if (ctx->nr_active) {
Peter Zijlstraafedadf2009-05-20 12:21:22 +0200954 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
955 if (counter != counter->group_leader)
956 counter_sched_out(counter, cpuctx, ctx);
957 else
958 group_sched_out(counter, cpuctx, ctx);
959 }
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100960 }
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200961 perf_enable();
Paul Mackerrasd859e292009-01-17 18:10:22 +1100962 out:
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100963 spin_unlock(&ctx->lock);
964}
965
Thomas Gleixner0793a612008-12-04 20:12:29 +0100966/*
Paul Mackerras564c2b22009-05-22 14:27:22 +1000967 * Test whether two contexts are equivalent, i.e. whether they
968 * have both been cloned from the same version of the same context
969 * and they both have the same number of enabled counters.
970 * If the number of enabled counters is the same, then the set
971 * of enabled counters should be the same, because these are both
972 * inherited contexts, therefore we can't access individual counters
973 * in them directly with an fd; we can only enable/disable all
974 * counters via prctl, or enable/disable all counters in a family
975 * via ioctl, which will have the same effect on both contexts.
976 */
977static int context_equiv(struct perf_counter_context *ctx1,
978 struct perf_counter_context *ctx2)
979{
980 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
Paul Mackerrasad3a37d2009-05-29 16:06:20 +1000981 && ctx1->parent_gen == ctx2->parent_gen
Paul Mackerras25346b932009-06-01 17:48:12 +1000982 && !ctx1->pin_count && !ctx2->pin_count;
Paul Mackerras564c2b22009-05-22 14:27:22 +1000983}
984
985/*
Thomas Gleixner0793a612008-12-04 20:12:29 +0100986 * Called from scheduler to remove the counters of the current task,
987 * with interrupts disabled.
988 *
989 * We stop each counter and update the counter value in counter->count.
990 *
Ingo Molnar76715812008-12-17 14:20:28 +0100991 * This does not protect us against NMI, but disable()
Thomas Gleixner0793a612008-12-04 20:12:29 +0100992 * sets the disabled bit in the control field of counter _before_
993 * accessing the counter control register. If a NMI hits, then it will
994 * not restart the counter.
995 */
Paul Mackerras564c2b22009-05-22 14:27:22 +1000996void perf_counter_task_sched_out(struct task_struct *task,
997 struct task_struct *next, int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100998{
999 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001000 struct perf_counter_context *ctx = task->perf_counter_ctxp;
Paul Mackerras564c2b22009-05-22 14:27:22 +10001001 struct perf_counter_context *next_ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001002 struct perf_counter_context *parent;
Peter Zijlstra4a0deca2009-03-19 20:26:12 +01001003 struct pt_regs *regs;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001004 int do_switch = 1;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001005
Peter Zijlstra10989fb2009-05-25 14:45:28 +02001006 regs = task_pt_regs(task);
1007 perf_swcounter_event(PERF_COUNT_CONTEXT_SWITCHES, 1, 1, regs, 0);
1008
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001009 if (likely(!ctx || !cpuctx->task_ctx))
Thomas Gleixner0793a612008-12-04 20:12:29 +01001010 return;
1011
Peter Zijlstrabce379b2009-04-06 11:45:13 +02001012 update_context_time(ctx);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001013
1014 rcu_read_lock();
1015 parent = rcu_dereference(ctx->parent_ctx);
Paul Mackerras564c2b22009-05-22 14:27:22 +10001016 next_ctx = next->perf_counter_ctxp;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001017 if (parent && next_ctx &&
1018 rcu_dereference(next_ctx->parent_ctx) == parent) {
1019 /*
1020 * Looks like the two contexts are clones, so we might be
1021 * able to optimize the context switch. We lock both
1022 * contexts and check that they are clones under the
1023 * lock (including re-checking that neither has been
1024 * uncloned in the meantime). It doesn't matter which
1025 * order we take the locks because no other cpu could
1026 * be trying to lock both of these tasks.
1027 */
1028 spin_lock(&ctx->lock);
1029 spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
1030 if (context_equiv(ctx, next_ctx)) {
Peter Zijlstra665c2142009-05-29 14:51:57 +02001031 /*
1032 * XXX do we need a memory barrier of sorts
1033 * wrt to rcu_dereference() of perf_counter_ctxp
1034 */
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001035 task->perf_counter_ctxp = next_ctx;
1036 next->perf_counter_ctxp = ctx;
1037 ctx->task = next;
1038 next_ctx->task = task;
1039 do_switch = 0;
1040 }
1041 spin_unlock(&next_ctx->lock);
1042 spin_unlock(&ctx->lock);
Paul Mackerras564c2b22009-05-22 14:27:22 +10001043 }
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001044 rcu_read_unlock();
Paul Mackerras564c2b22009-05-22 14:27:22 +10001045
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001046 if (do_switch) {
1047 __perf_counter_sched_out(ctx, cpuctx);
1048 cpuctx->task_ctx = NULL;
1049 }
Thomas Gleixner0793a612008-12-04 20:12:29 +01001050}
1051
Peter Zijlstra665c2142009-05-29 14:51:57 +02001052/*
1053 * Called with IRQs disabled
1054 */
Paul Mackerrasa08b1592009-05-11 15:46:10 +10001055static void __perf_counter_task_sched_out(struct perf_counter_context *ctx)
1056{
1057 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1058
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001059 if (!cpuctx->task_ctx)
1060 return;
Ingo Molnar012b84d2009-05-17 11:08:41 +02001061
1062 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
1063 return;
1064
Paul Mackerrasa08b1592009-05-11 15:46:10 +10001065 __perf_counter_sched_out(ctx, cpuctx);
1066 cpuctx->task_ctx = NULL;
1067}
1068
Peter Zijlstra665c2142009-05-29 14:51:57 +02001069/*
1070 * Called with IRQs disabled
1071 */
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001072static void perf_counter_cpu_sched_out(struct perf_cpu_context *cpuctx)
Ingo Molnar04289bb2008-12-11 08:38:42 +01001073{
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001074 __perf_counter_sched_out(&cpuctx->ctx, cpuctx);
Ingo Molnar04289bb2008-12-11 08:38:42 +01001075}
1076
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001077static void
1078__perf_counter_sched_in(struct perf_counter_context *ctx,
1079 struct perf_cpu_context *cpuctx, int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001080{
Thomas Gleixner0793a612008-12-04 20:12:29 +01001081 struct perf_counter *counter;
Paul Mackerrasdd0e6ba2009-01-12 15:11:00 +11001082 int can_add_hw = 1;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001083
Thomas Gleixner0793a612008-12-04 20:12:29 +01001084 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001085 ctx->is_active = 1;
1086 if (likely(!ctx->nr_counters))
1087 goto out;
1088
Peter Zijlstra4af49982009-04-06 11:45:10 +02001089 ctx->timestamp = perf_clock();
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001090
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001091 perf_disable();
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001092
1093 /*
1094 * First go through the list and put on any pinned groups
1095 * in order to give them the best chance of going on.
1096 */
Ingo Molnar04289bb2008-12-11 08:38:42 +01001097 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001098 if (counter->state <= PERF_COUNTER_STATE_OFF ||
1099 !counter->hw_event.pinned)
1100 continue;
1101 if (counter->cpu != -1 && counter->cpu != cpu)
1102 continue;
1103
Peter Zijlstraafedadf2009-05-20 12:21:22 +02001104 if (counter != counter->group_leader)
1105 counter_sched_in(counter, cpuctx, ctx, cpu);
1106 else {
1107 if (group_can_go_on(counter, cpuctx, 1))
1108 group_sched_in(counter, cpuctx, ctx, cpu);
1109 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001110
1111 /*
1112 * If this pinned group hasn't been scheduled,
1113 * put it in error state.
1114 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001115 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
1116 update_group_times(counter);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001117 counter->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001118 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001119 }
1120
1121 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1122 /*
1123 * Ignore counters in OFF or ERROR state, and
1124 * ignore pinned counters since we did them already.
1125 */
1126 if (counter->state <= PERF_COUNTER_STATE_OFF ||
1127 counter->hw_event.pinned)
1128 continue;
1129
Ingo Molnar04289bb2008-12-11 08:38:42 +01001130 /*
1131 * Listen to the 'cpu' scheduling filter constraint
1132 * of counters:
1133 */
Thomas Gleixner0793a612008-12-04 20:12:29 +01001134 if (counter->cpu != -1 && counter->cpu != cpu)
1135 continue;
1136
Peter Zijlstraafedadf2009-05-20 12:21:22 +02001137 if (counter != counter->group_leader) {
1138 if (counter_sched_in(counter, cpuctx, ctx, cpu))
Paul Mackerrasdd0e6ba2009-01-12 15:11:00 +11001139 can_add_hw = 0;
Peter Zijlstraafedadf2009-05-20 12:21:22 +02001140 } else {
1141 if (group_can_go_on(counter, cpuctx, can_add_hw)) {
1142 if (group_sched_in(counter, cpuctx, ctx, cpu))
1143 can_add_hw = 0;
1144 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001145 }
Thomas Gleixner0793a612008-12-04 20:12:29 +01001146 }
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001147 perf_enable();
Paul Mackerrasd859e292009-01-17 18:10:22 +11001148 out:
Thomas Gleixner0793a612008-12-04 20:12:29 +01001149 spin_unlock(&ctx->lock);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001150}
Ingo Molnar04289bb2008-12-11 08:38:42 +01001151
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001152/*
1153 * Called from scheduler to add the counters of the current task
1154 * with interrupts disabled.
1155 *
1156 * We restore the counter value and then enable it.
1157 *
1158 * This does not protect us against NMI, but enable()
1159 * sets the enabled bit in the control field of counter _before_
1160 * accessing the counter control register. If a NMI hits, then it will
1161 * keep the counter running.
1162 */
1163void perf_counter_task_sched_in(struct task_struct *task, int cpu)
1164{
1165 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001166 struct perf_counter_context *ctx = task->perf_counter_ctxp;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001167
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001168 if (likely(!ctx))
1169 return;
Paul Mackerras564c2b22009-05-22 14:27:22 +10001170 if (cpuctx->task_ctx == ctx)
1171 return;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001172 __perf_counter_sched_in(ctx, cpuctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001173 cpuctx->task_ctx = ctx;
1174}
1175
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001176static void perf_counter_cpu_sched_in(struct perf_cpu_context *cpuctx, int cpu)
1177{
1178 struct perf_counter_context *ctx = &cpuctx->ctx;
1179
1180 __perf_counter_sched_in(ctx, cpuctx, cpu);
1181}
1182
Peter Zijlstraa78ac322009-05-25 17:39:05 +02001183#define MAX_INTERRUPTS (~0ULL)
1184
1185static void perf_log_throttle(struct perf_counter *counter, int enable);
Peter Zijlstra26b119b2009-05-20 12:21:20 +02001186static void perf_log_period(struct perf_counter *counter, u64 period);
1187
1188static void perf_adjust_freq(struct perf_counter_context *ctx)
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001189{
1190 struct perf_counter *counter;
Peter Zijlstraa78ac322009-05-25 17:39:05 +02001191 u64 interrupts, irq_period;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001192 u64 events, period;
1193 s64 delta;
1194
1195 spin_lock(&ctx->lock);
1196 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1197 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
1198 continue;
1199
Peter Zijlstraa78ac322009-05-25 17:39:05 +02001200 interrupts = counter->hw.interrupts;
1201 counter->hw.interrupts = 0;
1202
1203 if (interrupts == MAX_INTERRUPTS) {
1204 perf_log_throttle(counter, 1);
1205 counter->pmu->unthrottle(counter);
1206 interrupts = 2*sysctl_perf_counter_limit/HZ;
1207 }
1208
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001209 if (!counter->hw_event.freq || !counter->hw_event.irq_freq)
1210 continue;
1211
Peter Zijlstraa78ac322009-05-25 17:39:05 +02001212 events = HZ * interrupts * counter->hw.irq_period;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001213 period = div64_u64(events, counter->hw_event.irq_freq);
1214
1215 delta = (s64)(1 + period - counter->hw.irq_period);
1216 delta >>= 1;
1217
1218 irq_period = counter->hw.irq_period + delta;
1219
1220 if (!irq_period)
1221 irq_period = 1;
1222
Peter Zijlstra26b119b2009-05-20 12:21:20 +02001223 perf_log_period(counter, irq_period);
1224
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001225 counter->hw.irq_period = irq_period;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001226 }
1227 spin_unlock(&ctx->lock);
1228}
1229
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001230/*
1231 * Round-robin a context's counters:
1232 */
1233static void rotate_ctx(struct perf_counter_context *ctx)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001234{
Thomas Gleixner0793a612008-12-04 20:12:29 +01001235 struct perf_counter *counter;
1236
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001237 if (!ctx->nr_counters)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001238 return;
1239
Thomas Gleixner0793a612008-12-04 20:12:29 +01001240 spin_lock(&ctx->lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001241 /*
Ingo Molnar04289bb2008-12-11 08:38:42 +01001242 * Rotate the first entry last (works just fine for group counters too):
Thomas Gleixner0793a612008-12-04 20:12:29 +01001243 */
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001244 perf_disable();
Ingo Molnar04289bb2008-12-11 08:38:42 +01001245 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Peter Zijlstra75564232009-03-13 12:21:29 +01001246 list_move_tail(&counter->list_entry, &ctx->counter_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001247 break;
1248 }
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001249 perf_enable();
Thomas Gleixner0793a612008-12-04 20:12:29 +01001250
1251 spin_unlock(&ctx->lock);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001252}
Thomas Gleixner0793a612008-12-04 20:12:29 +01001253
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001254void perf_counter_task_tick(struct task_struct *curr, int cpu)
1255{
Peter Zijlstra7fc23a52009-05-08 18:52:21 +02001256 struct perf_cpu_context *cpuctx;
1257 struct perf_counter_context *ctx;
1258
1259 if (!atomic_read(&nr_counters))
1260 return;
1261
1262 cpuctx = &per_cpu(perf_cpu_context, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001263 ctx = curr->perf_counter_ctxp;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001264
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001265 perf_adjust_freq(&cpuctx->ctx);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001266 if (ctx)
1267 perf_adjust_freq(ctx);
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001268
Ingo Molnarb82914c2009-05-04 18:54:32 +02001269 perf_counter_cpu_sched_out(cpuctx);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001270 if (ctx)
1271 __perf_counter_task_sched_out(ctx);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001272
Ingo Molnarb82914c2009-05-04 18:54:32 +02001273 rotate_ctx(&cpuctx->ctx);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001274 if (ctx)
1275 rotate_ctx(ctx);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001276
Ingo Molnarb82914c2009-05-04 18:54:32 +02001277 perf_counter_cpu_sched_in(cpuctx, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001278 if (ctx)
1279 perf_counter_task_sched_in(curr, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001280}
1281
1282/*
Thomas Gleixner0793a612008-12-04 20:12:29 +01001283 * Cross CPU call to read the hardware counter
1284 */
Ingo Molnar76715812008-12-17 14:20:28 +01001285static void __read(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001286{
Ingo Molnar621a01e2008-12-11 12:46:46 +01001287 struct perf_counter *counter = info;
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001288 struct perf_counter_context *ctx = counter->ctx;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001289 unsigned long flags;
Ingo Molnar621a01e2008-12-11 12:46:46 +01001290
Peter Zijlstra849691a2009-04-06 11:45:12 +02001291 local_irq_save(flags);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001292 if (ctx->is_active)
Peter Zijlstra4af49982009-04-06 11:45:10 +02001293 update_context_time(ctx);
Robert Richter4aeb0b42009-04-29 12:47:03 +02001294 counter->pmu->read(counter);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001295 update_counter_times(counter);
Peter Zijlstra849691a2009-04-06 11:45:12 +02001296 local_irq_restore(flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001297}
1298
Ingo Molnar04289bb2008-12-11 08:38:42 +01001299static u64 perf_counter_read(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001300{
1301 /*
1302 * If counter is enabled and currently active on a CPU, update the
1303 * value in the counter structure:
1304 */
Ingo Molnar6a930702008-12-11 15:17:03 +01001305 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
Thomas Gleixner0793a612008-12-04 20:12:29 +01001306 smp_call_function_single(counter->oncpu,
Ingo Molnar76715812008-12-17 14:20:28 +01001307 __read, counter, 1);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001308 } else if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
1309 update_counter_times(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001310 }
1311
Ingo Molnaree060942008-12-13 09:00:03 +01001312 return atomic64_read(&counter->count);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001313}
1314
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001315/*
1316 * Initialize the perf_counter context in a task_struct:
1317 */
1318static void
1319__perf_counter_init_context(struct perf_counter_context *ctx,
1320 struct task_struct *task)
1321{
1322 memset(ctx, 0, sizeof(*ctx));
1323 spin_lock_init(&ctx->lock);
1324 mutex_init(&ctx->mutex);
1325 INIT_LIST_HEAD(&ctx->counter_list);
1326 INIT_LIST_HEAD(&ctx->event_list);
1327 atomic_set(&ctx->refcount, 1);
1328 ctx->task = task;
1329}
1330
Thomas Gleixner0793a612008-12-04 20:12:29 +01001331static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
1332{
1333 struct perf_cpu_context *cpuctx;
1334 struct perf_counter_context *ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001335 struct perf_counter_context *parent_ctx;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001336 struct task_struct *task;
Paul Mackerras25346b932009-06-01 17:48:12 +10001337 unsigned long flags;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001338 int err;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001339
1340 /*
1341 * If cpu is not a wildcard then this is a percpu counter:
1342 */
1343 if (cpu != -1) {
1344 /* Must be root to operate on a CPU counter: */
Peter Zijlstra1ccd1542009-04-09 10:53:45 +02001345 if (sysctl_perf_counter_priv && !capable(CAP_SYS_ADMIN))
Thomas Gleixner0793a612008-12-04 20:12:29 +01001346 return ERR_PTR(-EACCES);
1347
1348 if (cpu < 0 || cpu > num_possible_cpus())
1349 return ERR_PTR(-EINVAL);
1350
1351 /*
1352 * We could be clever and allow to attach a counter to an
1353 * offline CPU and activate it when the CPU comes up, but
1354 * that's for later.
1355 */
1356 if (!cpu_isset(cpu, cpu_online_map))
1357 return ERR_PTR(-ENODEV);
1358
1359 cpuctx = &per_cpu(perf_cpu_context, cpu);
1360 ctx = &cpuctx->ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001361 get_ctx(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001362
Thomas Gleixner0793a612008-12-04 20:12:29 +01001363 return ctx;
1364 }
1365
1366 rcu_read_lock();
1367 if (!pid)
1368 task = current;
1369 else
1370 task = find_task_by_vpid(pid);
1371 if (task)
1372 get_task_struct(task);
1373 rcu_read_unlock();
1374
1375 if (!task)
1376 return ERR_PTR(-ESRCH);
1377
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001378 /*
1379 * Can't attach counters to a dying task.
1380 */
1381 err = -ESRCH;
1382 if (task->flags & PF_EXITING)
1383 goto errout;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001384
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001385 /* Reuse ptrace permission checks for now. */
1386 err = -EACCES;
1387 if (!ptrace_may_access(task, PTRACE_MODE_READ))
1388 goto errout;
1389
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001390 retry:
Paul Mackerras25346b932009-06-01 17:48:12 +10001391 ctx = perf_lock_task_context(task, &flags);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001392 if (ctx) {
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001393 parent_ctx = ctx->parent_ctx;
1394 if (parent_ctx) {
1395 put_ctx(parent_ctx);
1396 ctx->parent_ctx = NULL; /* no longer a clone */
1397 }
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001398 /*
1399 * Get an extra reference before dropping the lock so that
1400 * this context won't get freed if the task exits.
1401 */
1402 get_ctx(ctx);
Paul Mackerras25346b932009-06-01 17:48:12 +10001403 spin_unlock_irqrestore(&ctx->lock, flags);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001404 }
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001405
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001406 if (!ctx) {
1407 ctx = kmalloc(sizeof(struct perf_counter_context), GFP_KERNEL);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001408 err = -ENOMEM;
1409 if (!ctx)
1410 goto errout;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001411 __perf_counter_init_context(ctx, task);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001412 get_ctx(ctx);
1413 if (cmpxchg(&task->perf_counter_ctxp, NULL, ctx)) {
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001414 /*
1415 * We raced with some other task; use
1416 * the context they set.
1417 */
1418 kfree(ctx);
Paul Mackerras25346b932009-06-01 17:48:12 +10001419 goto retry;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001420 }
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001421 get_task_struct(task);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001422 }
1423
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001424 put_task_struct(task);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001425 return ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001426
1427 errout:
1428 put_task_struct(task);
1429 return ERR_PTR(err);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001430}
1431
Peter Zijlstra592903c2009-03-13 12:21:36 +01001432static void free_counter_rcu(struct rcu_head *head)
1433{
1434 struct perf_counter *counter;
1435
1436 counter = container_of(head, struct perf_counter, rcu_head);
1437 kfree(counter);
1438}
1439
Peter Zijlstra925d5192009-03-30 19:07:02 +02001440static void perf_pending_sync(struct perf_counter *counter);
1441
Peter Zijlstraf1600952009-03-19 20:26:16 +01001442static void free_counter(struct perf_counter *counter)
1443{
Peter Zijlstra925d5192009-03-30 19:07:02 +02001444 perf_pending_sync(counter);
1445
Peter Zijlstra7fc23a52009-05-08 18:52:21 +02001446 atomic_dec(&nr_counters);
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02001447 if (counter->hw_event.mmap)
1448 atomic_dec(&nr_mmap_tracking);
1449 if (counter->hw_event.munmap)
1450 atomic_dec(&nr_munmap_tracking);
1451 if (counter->hw_event.comm)
1452 atomic_dec(&nr_comm_tracking);
1453
Peter Zijlstrae077df42009-03-19 20:26:17 +01001454 if (counter->destroy)
1455 counter->destroy(counter);
1456
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001457 put_ctx(counter->ctx);
Peter Zijlstraf1600952009-03-19 20:26:16 +01001458 call_rcu(&counter->rcu_head, free_counter_rcu);
1459}
1460
Thomas Gleixner0793a612008-12-04 20:12:29 +01001461/*
1462 * Called when the last reference to the file is gone.
1463 */
1464static int perf_release(struct inode *inode, struct file *file)
1465{
1466 struct perf_counter *counter = file->private_data;
1467 struct perf_counter_context *ctx = counter->ctx;
1468
1469 file->private_data = NULL;
1470
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001471 WARN_ON_ONCE(ctx->parent_ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001472 mutex_lock(&ctx->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01001473 perf_counter_remove_from_context(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001474 mutex_unlock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001475
Peter Zijlstra082ff5a2009-05-23 18:29:00 +02001476 mutex_lock(&counter->owner->perf_counter_mutex);
1477 list_del_init(&counter->owner_entry);
1478 mutex_unlock(&counter->owner->perf_counter_mutex);
1479 put_task_struct(counter->owner);
1480
Peter Zijlstraf1600952009-03-19 20:26:16 +01001481 free_counter(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001482
1483 return 0;
1484}
1485
1486/*
1487 * Read the performance counter - simple non blocking version for now
1488 */
1489static ssize_t
1490perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
1491{
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001492 u64 values[3];
1493 int n;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001494
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001495 /*
1496 * Return end-of-file for a read on a counter that is in
1497 * error state (i.e. because it was pinned but it couldn't be
1498 * scheduled on to the CPU at some point).
1499 */
1500 if (counter->state == PERF_COUNTER_STATE_ERROR)
1501 return 0;
1502
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001503 WARN_ON_ONCE(counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001504 mutex_lock(&counter->child_mutex);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001505 values[0] = perf_counter_read(counter);
1506 n = 1;
1507 if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1508 values[n++] = counter->total_time_enabled +
1509 atomic64_read(&counter->child_total_time_enabled);
1510 if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1511 values[n++] = counter->total_time_running +
1512 atomic64_read(&counter->child_total_time_running);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001513 mutex_unlock(&counter->child_mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001514
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001515 if (count < n * sizeof(u64))
1516 return -EINVAL;
1517 count = n * sizeof(u64);
1518
1519 if (copy_to_user(buf, values, count))
1520 return -EFAULT;
1521
1522 return count;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001523}
1524
1525static ssize_t
Thomas Gleixner0793a612008-12-04 20:12:29 +01001526perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
1527{
1528 struct perf_counter *counter = file->private_data;
1529
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001530 return perf_read_hw(counter, buf, count);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001531}
1532
1533static unsigned int perf_poll(struct file *file, poll_table *wait)
1534{
1535 struct perf_counter *counter = file->private_data;
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001536 struct perf_mmap_data *data;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001537 unsigned int events = POLL_HUP;
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001538
1539 rcu_read_lock();
1540 data = rcu_dereference(counter->data);
1541 if (data)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001542 events = atomic_xchg(&data->poll, 0);
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001543 rcu_read_unlock();
Thomas Gleixner0793a612008-12-04 20:12:29 +01001544
1545 poll_wait(file, &counter->waitq, wait);
1546
Thomas Gleixner0793a612008-12-04 20:12:29 +01001547 return events;
1548}
1549
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001550static void perf_counter_reset(struct perf_counter *counter)
1551{
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001552 (void)perf_counter_read(counter);
Paul Mackerras615a3f12009-05-11 15:50:21 +10001553 atomic64_set(&counter->count, 0);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001554 perf_counter_update_userpage(counter);
1555}
1556
1557static void perf_counter_for_each_sibling(struct perf_counter *counter,
1558 void (*func)(struct perf_counter *))
1559{
1560 struct perf_counter_context *ctx = counter->ctx;
1561 struct perf_counter *sibling;
1562
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001563 WARN_ON_ONCE(ctx->parent_ctx);
Peter Zijlstra682076a2009-05-23 18:28:57 +02001564 mutex_lock(&ctx->mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001565 counter = counter->group_leader;
1566
1567 func(counter);
1568 list_for_each_entry(sibling, &counter->sibling_list, list_entry)
1569 func(sibling);
Peter Zijlstra682076a2009-05-23 18:28:57 +02001570 mutex_unlock(&ctx->mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001571}
1572
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001573/*
1574 * Holding the top-level counter's child_mutex means that any
1575 * descendant process that has inherited this counter will block
1576 * in sync_child_counter if it goes to exit, thus satisfying the
1577 * task existence requirements of perf_counter_enable/disable.
1578 */
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001579static void perf_counter_for_each_child(struct perf_counter *counter,
1580 void (*func)(struct perf_counter *))
1581{
1582 struct perf_counter *child;
1583
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001584 WARN_ON_ONCE(counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001585 mutex_lock(&counter->child_mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001586 func(counter);
1587 list_for_each_entry(child, &counter->child_list, child_list)
1588 func(child);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001589 mutex_unlock(&counter->child_mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001590}
1591
1592static void perf_counter_for_each(struct perf_counter *counter,
1593 void (*func)(struct perf_counter *))
1594{
1595 struct perf_counter *child;
1596
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001597 WARN_ON_ONCE(counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001598 mutex_lock(&counter->child_mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001599 perf_counter_for_each_sibling(counter, func);
1600 list_for_each_entry(child, &counter->child_list, child_list)
1601 perf_counter_for_each_sibling(child, func);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001602 mutex_unlock(&counter->child_mutex);
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001603}
1604
Paul Mackerrasd859e292009-01-17 18:10:22 +11001605static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1606{
1607 struct perf_counter *counter = file->private_data;
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001608 void (*func)(struct perf_counter *);
1609 u32 flags = arg;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001610
1611 switch (cmd) {
1612 case PERF_COUNTER_IOC_ENABLE:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001613 func = perf_counter_enable;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001614 break;
1615 case PERF_COUNTER_IOC_DISABLE:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001616 func = perf_counter_disable;
Peter Zijlstra79f14642009-04-06 11:45:07 +02001617 break;
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001618 case PERF_COUNTER_IOC_RESET:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001619 func = perf_counter_reset;
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001620 break;
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001621
1622 case PERF_COUNTER_IOC_REFRESH:
1623 return perf_counter_refresh(counter, arg);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001624 default:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001625 return -ENOTTY;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001626 }
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001627
1628 if (flags & PERF_IOC_FLAG_GROUP)
1629 perf_counter_for_each(counter, func);
1630 else
1631 perf_counter_for_each_child(counter, func);
1632
1633 return 0;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001634}
1635
Peter Zijlstra771d7cd2009-05-25 14:45:26 +02001636int perf_counter_task_enable(void)
1637{
1638 struct perf_counter *counter;
1639
1640 mutex_lock(&current->perf_counter_mutex);
1641 list_for_each_entry(counter, &current->perf_counter_list, owner_entry)
1642 perf_counter_for_each_child(counter, perf_counter_enable);
1643 mutex_unlock(&current->perf_counter_mutex);
1644
1645 return 0;
1646}
1647
1648int perf_counter_task_disable(void)
1649{
1650 struct perf_counter *counter;
1651
1652 mutex_lock(&current->perf_counter_mutex);
1653 list_for_each_entry(counter, &current->perf_counter_list, owner_entry)
1654 perf_counter_for_each_child(counter, perf_counter_disable);
1655 mutex_unlock(&current->perf_counter_mutex);
1656
1657 return 0;
1658}
1659
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001660/*
1661 * Callers need to ensure there can be no nesting of this function, otherwise
1662 * the seqlock logic goes bad. We can not serialize this because the arch
1663 * code calls this from NMI context.
1664 */
1665void perf_counter_update_userpage(struct perf_counter *counter)
Paul Mackerras37d81822009-03-23 18:22:08 +01001666{
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001667 struct perf_mmap_data *data;
1668 struct perf_counter_mmap_page *userpg;
1669
1670 rcu_read_lock();
1671 data = rcu_dereference(counter->data);
1672 if (!data)
1673 goto unlock;
1674
1675 userpg = data->user_page;
Paul Mackerras37d81822009-03-23 18:22:08 +01001676
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001677 /*
1678 * Disable preemption so as to not let the corresponding user-space
1679 * spin too long if we get preempted.
1680 */
1681 preempt_disable();
Paul Mackerras37d81822009-03-23 18:22:08 +01001682 ++userpg->lock;
Peter Zijlstra92f22a32009-04-02 11:12:04 +02001683 barrier();
Paul Mackerras37d81822009-03-23 18:22:08 +01001684 userpg->index = counter->hw.idx;
1685 userpg->offset = atomic64_read(&counter->count);
1686 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
1687 userpg->offset -= atomic64_read(&counter->hw.prev_count);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001688
Peter Zijlstra92f22a32009-04-02 11:12:04 +02001689 barrier();
Paul Mackerras37d81822009-03-23 18:22:08 +01001690 ++userpg->lock;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001691 preempt_enable();
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001692unlock:
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001693 rcu_read_unlock();
Paul Mackerras37d81822009-03-23 18:22:08 +01001694}
1695
1696static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1697{
1698 struct perf_counter *counter = vma->vm_file->private_data;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001699 struct perf_mmap_data *data;
1700 int ret = VM_FAULT_SIGBUS;
Paul Mackerras37d81822009-03-23 18:22:08 +01001701
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001702 rcu_read_lock();
1703 data = rcu_dereference(counter->data);
1704 if (!data)
1705 goto unlock;
Paul Mackerras37d81822009-03-23 18:22:08 +01001706
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001707 if (vmf->pgoff == 0) {
1708 vmf->page = virt_to_page(data->user_page);
1709 } else {
1710 int nr = vmf->pgoff - 1;
1711
1712 if ((unsigned)nr > data->nr_pages)
1713 goto unlock;
1714
1715 vmf->page = virt_to_page(data->data_pages[nr]);
1716 }
Paul Mackerras37d81822009-03-23 18:22:08 +01001717 get_page(vmf->page);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001718 ret = 0;
1719unlock:
1720 rcu_read_unlock();
1721
1722 return ret;
1723}
1724
1725static int perf_mmap_data_alloc(struct perf_counter *counter, int nr_pages)
1726{
1727 struct perf_mmap_data *data;
1728 unsigned long size;
1729 int i;
1730
1731 WARN_ON(atomic_read(&counter->mmap_count));
1732
1733 size = sizeof(struct perf_mmap_data);
1734 size += nr_pages * sizeof(void *);
1735
1736 data = kzalloc(size, GFP_KERNEL);
1737 if (!data)
1738 goto fail;
1739
1740 data->user_page = (void *)get_zeroed_page(GFP_KERNEL);
1741 if (!data->user_page)
1742 goto fail_user_page;
1743
1744 for (i = 0; i < nr_pages; i++) {
1745 data->data_pages[i] = (void *)get_zeroed_page(GFP_KERNEL);
1746 if (!data->data_pages[i])
1747 goto fail_data_pages;
1748 }
1749
1750 data->nr_pages = nr_pages;
Peter Zijlstra22c15582009-05-05 17:50:25 +02001751 atomic_set(&data->lock, -1);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001752
1753 rcu_assign_pointer(counter->data, data);
1754
Paul Mackerras37d81822009-03-23 18:22:08 +01001755 return 0;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001756
1757fail_data_pages:
1758 for (i--; i >= 0; i--)
1759 free_page((unsigned long)data->data_pages[i]);
1760
1761 free_page((unsigned long)data->user_page);
1762
1763fail_user_page:
1764 kfree(data);
1765
1766fail:
1767 return -ENOMEM;
1768}
1769
1770static void __perf_mmap_data_free(struct rcu_head *rcu_head)
1771{
1772 struct perf_mmap_data *data = container_of(rcu_head,
1773 struct perf_mmap_data, rcu_head);
1774 int i;
1775
1776 free_page((unsigned long)data->user_page);
1777 for (i = 0; i < data->nr_pages; i++)
1778 free_page((unsigned long)data->data_pages[i]);
1779 kfree(data);
1780}
1781
1782static void perf_mmap_data_free(struct perf_counter *counter)
1783{
1784 struct perf_mmap_data *data = counter->data;
1785
1786 WARN_ON(atomic_read(&counter->mmap_count));
1787
1788 rcu_assign_pointer(counter->data, NULL);
1789 call_rcu(&data->rcu_head, __perf_mmap_data_free);
1790}
1791
1792static void perf_mmap_open(struct vm_area_struct *vma)
1793{
1794 struct perf_counter *counter = vma->vm_file->private_data;
1795
1796 atomic_inc(&counter->mmap_count);
1797}
1798
1799static void perf_mmap_close(struct vm_area_struct *vma)
1800{
1801 struct perf_counter *counter = vma->vm_file->private_data;
1802
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001803 WARN_ON_ONCE(counter->ctx->parent_ctx);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001804 if (atomic_dec_and_mutex_lock(&counter->mmap_count,
1805 &counter->mmap_mutex)) {
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001806 struct user_struct *user = current_user();
1807
1808 atomic_long_sub(counter->data->nr_pages + 1, &user->locked_vm);
Peter Zijlstrac5078f72009-05-05 17:50:24 +02001809 vma->vm_mm->locked_vm -= counter->data->nr_locked;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001810 perf_mmap_data_free(counter);
1811 mutex_unlock(&counter->mmap_mutex);
1812 }
Paul Mackerras37d81822009-03-23 18:22:08 +01001813}
1814
1815static struct vm_operations_struct perf_mmap_vmops = {
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001816 .open = perf_mmap_open,
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001817 .close = perf_mmap_close,
Paul Mackerras37d81822009-03-23 18:22:08 +01001818 .fault = perf_mmap_fault,
1819};
1820
1821static int perf_mmap(struct file *file, struct vm_area_struct *vma)
1822{
1823 struct perf_counter *counter = file->private_data;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001824 struct user_struct *user = current_user();
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001825 unsigned long vma_size;
1826 unsigned long nr_pages;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001827 unsigned long user_locked, user_lock_limit;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001828 unsigned long locked, lock_limit;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001829 long user_extra, extra;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001830 int ret = 0;
Paul Mackerras37d81822009-03-23 18:22:08 +01001831
1832 if (!(vma->vm_flags & VM_SHARED) || (vma->vm_flags & VM_WRITE))
1833 return -EINVAL;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001834
1835 vma_size = vma->vm_end - vma->vm_start;
1836 nr_pages = (vma_size / PAGE_SIZE) - 1;
1837
Peter Zijlstra7730d862009-03-25 12:48:31 +01001838 /*
1839 * If we have data pages ensure they're a power-of-two number, so we
1840 * can do bitmasks instead of modulo.
1841 */
1842 if (nr_pages != 0 && !is_power_of_2(nr_pages))
Paul Mackerras37d81822009-03-23 18:22:08 +01001843 return -EINVAL;
1844
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001845 if (vma_size != PAGE_SIZE * (1 + nr_pages))
Paul Mackerras37d81822009-03-23 18:22:08 +01001846 return -EINVAL;
1847
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001848 if (vma->vm_pgoff != 0)
1849 return -EINVAL;
Paul Mackerras37d81822009-03-23 18:22:08 +01001850
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001851 WARN_ON_ONCE(counter->ctx->parent_ctx);
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001852 mutex_lock(&counter->mmap_mutex);
1853 if (atomic_inc_not_zero(&counter->mmap_count)) {
1854 if (nr_pages != counter->data->nr_pages)
1855 ret = -EINVAL;
1856 goto unlock;
1857 }
1858
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001859 user_extra = nr_pages + 1;
1860 user_lock_limit = sysctl_perf_counter_mlock >> (PAGE_SHIFT - 10);
Ingo Molnara3862d32009-05-24 09:02:37 +02001861
1862 /*
1863 * Increase the limit linearly with more CPUs:
1864 */
1865 user_lock_limit *= num_online_cpus();
1866
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001867 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
Peter Zijlstrac5078f72009-05-05 17:50:24 +02001868
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001869 extra = 0;
1870 if (user_locked > user_lock_limit)
1871 extra = user_locked - user_lock_limit;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001872
1873 lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
1874 lock_limit >>= PAGE_SHIFT;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001875 locked = vma->vm_mm->locked_vm + extra;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001876
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001877 if ((locked > lock_limit) && !capable(CAP_IPC_LOCK)) {
1878 ret = -EPERM;
1879 goto unlock;
1880 }
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001881
1882 WARN_ON(counter->data);
1883 ret = perf_mmap_data_alloc(counter, nr_pages);
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001884 if (ret)
1885 goto unlock;
1886
1887 atomic_set(&counter->mmap_count, 1);
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001888 atomic_long_add(user_extra, &user->locked_vm);
Peter Zijlstrac5078f72009-05-05 17:50:24 +02001889 vma->vm_mm->locked_vm += extra;
1890 counter->data->nr_locked = extra;
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001891unlock:
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001892 mutex_unlock(&counter->mmap_mutex);
Paul Mackerras37d81822009-03-23 18:22:08 +01001893
1894 vma->vm_flags &= ~VM_MAYWRITE;
1895 vma->vm_flags |= VM_RESERVED;
1896 vma->vm_ops = &perf_mmap_vmops;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001897
1898 return ret;
Paul Mackerras37d81822009-03-23 18:22:08 +01001899}
1900
Peter Zijlstra3c446b3d2009-04-06 11:45:01 +02001901static int perf_fasync(int fd, struct file *filp, int on)
1902{
1903 struct perf_counter *counter = filp->private_data;
1904 struct inode *inode = filp->f_path.dentry->d_inode;
1905 int retval;
1906
1907 mutex_lock(&inode->i_mutex);
1908 retval = fasync_helper(fd, filp, on, &counter->fasync);
1909 mutex_unlock(&inode->i_mutex);
1910
1911 if (retval < 0)
1912 return retval;
1913
1914 return 0;
1915}
1916
Thomas Gleixner0793a612008-12-04 20:12:29 +01001917static const struct file_operations perf_fops = {
1918 .release = perf_release,
1919 .read = perf_read,
1920 .poll = perf_poll,
Paul Mackerrasd859e292009-01-17 18:10:22 +11001921 .unlocked_ioctl = perf_ioctl,
1922 .compat_ioctl = perf_ioctl,
Paul Mackerras37d81822009-03-23 18:22:08 +01001923 .mmap = perf_mmap,
Peter Zijlstra3c446b3d2009-04-06 11:45:01 +02001924 .fasync = perf_fasync,
Thomas Gleixner0793a612008-12-04 20:12:29 +01001925};
1926
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001927/*
Peter Zijlstra925d5192009-03-30 19:07:02 +02001928 * Perf counter wakeup
1929 *
1930 * If there's data, ensure we set the poll() state and publish everything
1931 * to user-space before waking everybody up.
1932 */
1933
1934void perf_counter_wakeup(struct perf_counter *counter)
1935{
Peter Zijlstra925d5192009-03-30 19:07:02 +02001936 wake_up_all(&counter->waitq);
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02001937
1938 if (counter->pending_kill) {
1939 kill_fasync(&counter->fasync, SIGIO, counter->pending_kill);
1940 counter->pending_kill = 0;
1941 }
Peter Zijlstra925d5192009-03-30 19:07:02 +02001942}
1943
1944/*
1945 * Pending wakeups
1946 *
1947 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
1948 *
1949 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
1950 * single linked list and use cmpxchg() to add entries lockless.
1951 */
1952
Peter Zijlstra79f14642009-04-06 11:45:07 +02001953static void perf_pending_counter(struct perf_pending_entry *entry)
1954{
1955 struct perf_counter *counter = container_of(entry,
1956 struct perf_counter, pending);
1957
1958 if (counter->pending_disable) {
1959 counter->pending_disable = 0;
1960 perf_counter_disable(counter);
1961 }
1962
1963 if (counter->pending_wakeup) {
1964 counter->pending_wakeup = 0;
1965 perf_counter_wakeup(counter);
1966 }
1967}
1968
Peter Zijlstra671dec52009-04-06 11:45:02 +02001969#define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
Peter Zijlstra925d5192009-03-30 19:07:02 +02001970
Peter Zijlstra671dec52009-04-06 11:45:02 +02001971static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = {
Peter Zijlstra925d5192009-03-30 19:07:02 +02001972 PENDING_TAIL,
1973};
1974
Peter Zijlstra671dec52009-04-06 11:45:02 +02001975static void perf_pending_queue(struct perf_pending_entry *entry,
1976 void (*func)(struct perf_pending_entry *))
Peter Zijlstra925d5192009-03-30 19:07:02 +02001977{
Peter Zijlstra671dec52009-04-06 11:45:02 +02001978 struct perf_pending_entry **head;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001979
Peter Zijlstra671dec52009-04-06 11:45:02 +02001980 if (cmpxchg(&entry->next, NULL, PENDING_TAIL) != NULL)
Peter Zijlstra925d5192009-03-30 19:07:02 +02001981 return;
1982
Peter Zijlstra671dec52009-04-06 11:45:02 +02001983 entry->func = func;
1984
1985 head = &get_cpu_var(perf_pending_head);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001986
1987 do {
Peter Zijlstra671dec52009-04-06 11:45:02 +02001988 entry->next = *head;
1989 } while (cmpxchg(head, entry->next, entry) != entry->next);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001990
1991 set_perf_counter_pending();
1992
Peter Zijlstra671dec52009-04-06 11:45:02 +02001993 put_cpu_var(perf_pending_head);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001994}
1995
1996static int __perf_pending_run(void)
1997{
Peter Zijlstra671dec52009-04-06 11:45:02 +02001998 struct perf_pending_entry *list;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001999 int nr = 0;
2000
Peter Zijlstra671dec52009-04-06 11:45:02 +02002001 list = xchg(&__get_cpu_var(perf_pending_head), PENDING_TAIL);
Peter Zijlstra925d5192009-03-30 19:07:02 +02002002 while (list != PENDING_TAIL) {
Peter Zijlstra671dec52009-04-06 11:45:02 +02002003 void (*func)(struct perf_pending_entry *);
2004 struct perf_pending_entry *entry = list;
Peter Zijlstra925d5192009-03-30 19:07:02 +02002005
2006 list = list->next;
2007
Peter Zijlstra671dec52009-04-06 11:45:02 +02002008 func = entry->func;
2009 entry->next = NULL;
Peter Zijlstra925d5192009-03-30 19:07:02 +02002010 /*
2011 * Ensure we observe the unqueue before we issue the wakeup,
2012 * so that we won't be waiting forever.
2013 * -- see perf_not_pending().
2014 */
2015 smp_wmb();
2016
Peter Zijlstra671dec52009-04-06 11:45:02 +02002017 func(entry);
Peter Zijlstra925d5192009-03-30 19:07:02 +02002018 nr++;
2019 }
2020
2021 return nr;
2022}
2023
2024static inline int perf_not_pending(struct perf_counter *counter)
2025{
2026 /*
2027 * If we flush on whatever cpu we run, there is a chance we don't
2028 * need to wait.
2029 */
2030 get_cpu();
2031 __perf_pending_run();
2032 put_cpu();
2033
2034 /*
2035 * Ensure we see the proper queue state before going to sleep
2036 * so that we do not miss the wakeup. -- see perf_pending_handle()
2037 */
2038 smp_rmb();
Peter Zijlstra671dec52009-04-06 11:45:02 +02002039 return counter->pending.next == NULL;
Peter Zijlstra925d5192009-03-30 19:07:02 +02002040}
2041
2042static void perf_pending_sync(struct perf_counter *counter)
2043{
2044 wait_event(counter->waitq, perf_not_pending(counter));
2045}
2046
2047void perf_counter_do_pending(void)
2048{
2049 __perf_pending_run();
2050}
2051
2052/*
Peter Zijlstra394ee072009-03-30 19:07:14 +02002053 * Callchain support -- arch specific
2054 */
2055
Peter Zijlstra9c03d882009-04-06 11:45:00 +02002056__weak struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
Peter Zijlstra394ee072009-03-30 19:07:14 +02002057{
2058 return NULL;
2059}
2060
2061/*
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002062 * Output
2063 */
2064
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002065struct perf_output_handle {
2066 struct perf_counter *counter;
2067 struct perf_mmap_data *data;
2068 unsigned int offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01002069 unsigned int head;
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002070 int nmi;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002071 int overflow;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002072 int locked;
2073 unsigned long flags;
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002074};
2075
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002076static void perf_output_wakeup(struct perf_output_handle *handle)
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002077{
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002078 atomic_set(&handle->data->poll, POLL_IN);
2079
Peter Zijlstra671dec52009-04-06 11:45:02 +02002080 if (handle->nmi) {
Peter Zijlstra79f14642009-04-06 11:45:07 +02002081 handle->counter->pending_wakeup = 1;
Peter Zijlstra671dec52009-04-06 11:45:02 +02002082 perf_pending_queue(&handle->counter->pending,
Peter Zijlstra79f14642009-04-06 11:45:07 +02002083 perf_pending_counter);
Peter Zijlstra671dec52009-04-06 11:45:02 +02002084 } else
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002085 perf_counter_wakeup(handle->counter);
2086}
2087
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002088/*
2089 * Curious locking construct.
2090 *
2091 * We need to ensure a later event doesn't publish a head when a former
2092 * event isn't done writing. However since we need to deal with NMIs we
2093 * cannot fully serialize things.
2094 *
2095 * What we do is serialize between CPUs so we only have to deal with NMI
2096 * nesting on a single CPU.
2097 *
2098 * We only publish the head (and generate a wakeup) when the outer-most
2099 * event completes.
2100 */
2101static void perf_output_lock(struct perf_output_handle *handle)
2102{
2103 struct perf_mmap_data *data = handle->data;
2104 int cpu;
2105
2106 handle->locked = 0;
2107
2108 local_irq_save(handle->flags);
2109 cpu = smp_processor_id();
2110
2111 if (in_nmi() && atomic_read(&data->lock) == cpu)
2112 return;
2113
Peter Zijlstra22c15582009-05-05 17:50:25 +02002114 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002115 cpu_relax();
2116
2117 handle->locked = 1;
2118}
2119
2120static void perf_output_unlock(struct perf_output_handle *handle)
2121{
2122 struct perf_mmap_data *data = handle->data;
2123 int head, cpu;
2124
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002125 data->done_head = data->head;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002126
2127 if (!handle->locked)
2128 goto out;
2129
2130again:
2131 /*
2132 * The xchg implies a full barrier that ensures all writes are done
2133 * before we publish the new head, matched by a rmb() in userspace when
2134 * reading this position.
2135 */
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002136 while ((head = atomic_xchg(&data->done_head, 0)))
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002137 data->user_page->data_head = head;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002138
2139 /*
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002140 * NMI can happen here, which means we can miss a done_head update.
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002141 */
2142
Peter Zijlstra22c15582009-05-05 17:50:25 +02002143 cpu = atomic_xchg(&data->lock, -1);
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002144 WARN_ON_ONCE(cpu != smp_processor_id());
2145
2146 /*
2147 * Therefore we have to validate we did not indeed do so.
2148 */
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002149 if (unlikely(atomic_read(&data->done_head))) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002150 /*
2151 * Since we had it locked, we can lock it again.
2152 */
Peter Zijlstra22c15582009-05-05 17:50:25 +02002153 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002154 cpu_relax();
2155
2156 goto again;
2157 }
2158
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002159 if (atomic_xchg(&data->wakeup, 0))
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002160 perf_output_wakeup(handle);
2161out:
2162 local_irq_restore(handle->flags);
2163}
2164
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002165static int perf_output_begin(struct perf_output_handle *handle,
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002166 struct perf_counter *counter, unsigned int size,
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002167 int nmi, int overflow)
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002168{
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002169 struct perf_mmap_data *data;
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002170 unsigned int offset, head;
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002171
Peter Zijlstra2023b352009-05-05 17:50:26 +02002172 /*
2173 * For inherited counters we send all the output towards the parent.
2174 */
2175 if (counter->parent)
2176 counter = counter->parent;
2177
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002178 rcu_read_lock();
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002179 data = rcu_dereference(counter->data);
2180 if (!data)
2181 goto out;
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002182
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002183 handle->data = data;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002184 handle->counter = counter;
2185 handle->nmi = nmi;
2186 handle->overflow = overflow;
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002187
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002188 if (!data->nr_pages)
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002189 goto fail;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002190
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002191 perf_output_lock(handle);
2192
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002193 do {
2194 offset = head = atomic_read(&data->head);
Peter Zijlstrac7138f32009-03-24 13:18:16 +01002195 head += size;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002196 } while (atomic_cmpxchg(&data->head, offset, head) != offset);
2197
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002198 handle->offset = offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01002199 handle->head = head;
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002200
2201 if ((offset >> PAGE_SHIFT) != (head >> PAGE_SHIFT))
2202 atomic_set(&data->wakeup, 1);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002203
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002204 return 0;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002205
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002206fail:
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002207 perf_output_wakeup(handle);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002208out:
2209 rcu_read_unlock();
2210
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002211 return -ENOSPC;
2212}
2213
2214static void perf_output_copy(struct perf_output_handle *handle,
2215 void *buf, unsigned int len)
2216{
2217 unsigned int pages_mask;
2218 unsigned int offset;
2219 unsigned int size;
2220 void **pages;
2221
2222 offset = handle->offset;
2223 pages_mask = handle->data->nr_pages - 1;
2224 pages = handle->data->data_pages;
2225
2226 do {
2227 unsigned int page_offset;
2228 int nr;
2229
2230 nr = (offset >> PAGE_SHIFT) & pages_mask;
2231 page_offset = offset & (PAGE_SIZE - 1);
2232 size = min_t(unsigned int, PAGE_SIZE - page_offset, len);
2233
2234 memcpy(pages[nr] + page_offset, buf, size);
2235
2236 len -= size;
2237 buf += size;
2238 offset += size;
2239 } while (len);
2240
2241 handle->offset = offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01002242
Peter Zijlstra53020fe2009-05-13 21:26:19 +02002243 /*
2244 * Check we didn't copy past our reservation window, taking the
2245 * possible unsigned int wrap into account.
2246 */
2247 WARN_ON_ONCE(((int)(handle->head - handle->offset)) < 0);
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002248}
2249
Peter Zijlstra5c148192009-03-25 12:30:23 +01002250#define perf_output_put(handle, x) \
2251 perf_output_copy((handle), &(x), sizeof(x))
2252
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002253static void perf_output_end(struct perf_output_handle *handle)
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002254{
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002255 struct perf_counter *counter = handle->counter;
2256 struct perf_mmap_data *data = handle->data;
2257
2258 int wakeup_events = counter->hw_event.wakeup_events;
Peter Zijlstrac4578102009-04-02 11:12:01 +02002259
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002260 if (handle->overflow && wakeup_events) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002261 int events = atomic_inc_return(&data->events);
Peter Zijlstrac4578102009-04-02 11:12:01 +02002262 if (events >= wakeup_events) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002263 atomic_sub(wakeup_events, &data->events);
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002264 atomic_set(&data->wakeup, 1);
Peter Zijlstrac4578102009-04-02 11:12:01 +02002265 }
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002266 }
2267
2268 perf_output_unlock(handle);
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002269 rcu_read_unlock();
2270}
2271
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002272static void perf_counter_output(struct perf_counter *counter,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002273 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002274{
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002275 int ret;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002276 u64 record_type = counter->hw_event.record_type;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002277 struct perf_output_handle handle;
2278 struct perf_event_header header;
2279 u64 ip;
Peter Zijlstra5c148192009-03-25 12:30:23 +01002280 struct {
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002281 u32 pid, tid;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002282 } tid_entry;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002283 struct {
2284 u64 event;
2285 u64 counter;
2286 } group_entry;
Peter Zijlstra394ee072009-03-30 19:07:14 +02002287 struct perf_callchain_entry *callchain = NULL;
2288 int callchain_size = 0;
Peter Zijlstra339f7c92009-04-06 11:45:06 +02002289 u64 time;
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002290 struct {
2291 u32 cpu, reserved;
2292 } cpu_entry;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002293
Peter Zijlstra6b6e54862009-04-08 15:01:27 +02002294 header.type = 0;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002295 header.size = sizeof(header);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002296
Peter Zijlstra6b6e54862009-04-08 15:01:27 +02002297 header.misc = PERF_EVENT_MISC_OVERFLOW;
Paul Mackerras9d23a902009-05-14 21:48:08 +10002298 header.misc |= perf_misc_flags(regs);
Peter Zijlstra6fab0192009-04-08 15:01:26 +02002299
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002300 if (record_type & PERF_RECORD_IP) {
Paul Mackerras9d23a902009-05-14 21:48:08 +10002301 ip = perf_instruction_pointer(regs);
Peter Zijlstra6b6e54862009-04-08 15:01:27 +02002302 header.type |= PERF_RECORD_IP;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002303 header.size += sizeof(ip);
2304 }
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002305
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002306 if (record_type & PERF_RECORD_TID) {
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002307 /* namespace issues */
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002308 tid_entry.pid = current->group_leader->pid;
2309 tid_entry.tid = current->pid;
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002310
Peter Zijlstra6b6e54862009-04-08 15:01:27 +02002311 header.type |= PERF_RECORD_TID;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002312 header.size += sizeof(tid_entry);
2313 }
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002314
Peter Zijlstra4d855452009-04-08 15:01:32 +02002315 if (record_type & PERF_RECORD_TIME) {
2316 /*
2317 * Maybe do better on x86 and provide cpu_clock_nmi()
2318 */
2319 time = sched_clock();
2320
2321 header.type |= PERF_RECORD_TIME;
2322 header.size += sizeof(u64);
2323 }
2324
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002325 if (record_type & PERF_RECORD_ADDR) {
2326 header.type |= PERF_RECORD_ADDR;
2327 header.size += sizeof(u64);
2328 }
2329
Peter Zijlstraa85f61a2009-05-08 18:52:23 +02002330 if (record_type & PERF_RECORD_CONFIG) {
2331 header.type |= PERF_RECORD_CONFIG;
2332 header.size += sizeof(u64);
2333 }
2334
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002335 if (record_type & PERF_RECORD_CPU) {
2336 header.type |= PERF_RECORD_CPU;
2337 header.size += sizeof(cpu_entry);
2338
2339 cpu_entry.cpu = raw_smp_processor_id();
2340 }
2341
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002342 if (record_type & PERF_RECORD_GROUP) {
Peter Zijlstra6b6e54862009-04-08 15:01:27 +02002343 header.type |= PERF_RECORD_GROUP;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002344 header.size += sizeof(u64) +
2345 counter->nr_siblings * sizeof(group_entry);
2346 }
2347
2348 if (record_type & PERF_RECORD_CALLCHAIN) {
Peter Zijlstra394ee072009-03-30 19:07:14 +02002349 callchain = perf_callchain(regs);
2350
2351 if (callchain) {
Peter Zijlstra9c03d882009-04-06 11:45:00 +02002352 callchain_size = (1 + callchain->nr) * sizeof(u64);
Peter Zijlstra394ee072009-03-30 19:07:14 +02002353
Peter Zijlstra6b6e54862009-04-08 15:01:27 +02002354 header.type |= PERF_RECORD_CALLCHAIN;
Peter Zijlstra394ee072009-03-30 19:07:14 +02002355 header.size += callchain_size;
2356 }
2357 }
2358
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002359 ret = perf_output_begin(&handle, counter, header.size, nmi, 1);
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002360 if (ret)
2361 return;
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002362
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002363 perf_output_put(&handle, header);
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002364
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002365 if (record_type & PERF_RECORD_IP)
2366 perf_output_put(&handle, ip);
2367
2368 if (record_type & PERF_RECORD_TID)
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002369 perf_output_put(&handle, tid_entry);
2370
Peter Zijlstra4d855452009-04-08 15:01:32 +02002371 if (record_type & PERF_RECORD_TIME)
2372 perf_output_put(&handle, time);
2373
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002374 if (record_type & PERF_RECORD_ADDR)
2375 perf_output_put(&handle, addr);
2376
Peter Zijlstraa85f61a2009-05-08 18:52:23 +02002377 if (record_type & PERF_RECORD_CONFIG)
2378 perf_output_put(&handle, counter->hw_event.config);
2379
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002380 if (record_type & PERF_RECORD_CPU)
2381 perf_output_put(&handle, cpu_entry);
2382
Peter Zijlstra2023b352009-05-05 17:50:26 +02002383 /*
2384 * XXX PERF_RECORD_GROUP vs inherited counters seems difficult.
2385 */
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002386 if (record_type & PERF_RECORD_GROUP) {
2387 struct perf_counter *leader, *sub;
2388 u64 nr = counter->nr_siblings;
2389
2390 perf_output_put(&handle, nr);
2391
2392 leader = counter->group_leader;
2393 list_for_each_entry(sub, &leader->sibling_list, list_entry) {
2394 if (sub != counter)
Robert Richter4aeb0b42009-04-29 12:47:03 +02002395 sub->pmu->read(sub);
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002396
2397 group_entry.event = sub->hw_event.config;
2398 group_entry.counter = atomic64_read(&sub->count);
2399
2400 perf_output_put(&handle, group_entry);
2401 }
2402 }
2403
Peter Zijlstra394ee072009-03-30 19:07:14 +02002404 if (callchain)
2405 perf_output_copy(&handle, callchain, callchain_size);
2406
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002407 perf_output_end(&handle);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002408}
2409
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002410/*
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002411 * comm tracking
2412 */
2413
2414struct perf_comm_event {
2415 struct task_struct *task;
2416 char *comm;
2417 int comm_size;
2418
2419 struct {
2420 struct perf_event_header header;
2421
2422 u32 pid;
2423 u32 tid;
2424 } event;
2425};
2426
2427static void perf_counter_comm_output(struct perf_counter *counter,
2428 struct perf_comm_event *comm_event)
2429{
2430 struct perf_output_handle handle;
2431 int size = comm_event->event.header.size;
2432 int ret = perf_output_begin(&handle, counter, size, 0, 0);
2433
2434 if (ret)
2435 return;
2436
2437 perf_output_put(&handle, comm_event->event);
2438 perf_output_copy(&handle, comm_event->comm,
2439 comm_event->comm_size);
2440 perf_output_end(&handle);
2441}
2442
2443static int perf_counter_comm_match(struct perf_counter *counter,
2444 struct perf_comm_event *comm_event)
2445{
2446 if (counter->hw_event.comm &&
2447 comm_event->event.header.type == PERF_EVENT_COMM)
2448 return 1;
2449
2450 return 0;
2451}
2452
2453static void perf_counter_comm_ctx(struct perf_counter_context *ctx,
2454 struct perf_comm_event *comm_event)
2455{
2456 struct perf_counter *counter;
2457
2458 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2459 return;
2460
2461 rcu_read_lock();
2462 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2463 if (perf_counter_comm_match(counter, comm_event))
2464 perf_counter_comm_output(counter, comm_event);
2465 }
2466 rcu_read_unlock();
2467}
2468
2469static void perf_counter_comm_event(struct perf_comm_event *comm_event)
2470{
2471 struct perf_cpu_context *cpuctx;
Peter Zijlstra665c2142009-05-29 14:51:57 +02002472 struct perf_counter_context *ctx;
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002473 unsigned int size;
2474 char *comm = comm_event->task->comm;
2475
Ingo Molnar888fcee2009-04-09 09:48:22 +02002476 size = ALIGN(strlen(comm)+1, sizeof(u64));
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002477
2478 comm_event->comm = comm;
2479 comm_event->comm_size = size;
2480
2481 comm_event->event.header.size = sizeof(comm_event->event) + size;
2482
2483 cpuctx = &get_cpu_var(perf_cpu_context);
2484 perf_counter_comm_ctx(&cpuctx->ctx, comm_event);
2485 put_cpu_var(perf_cpu_context);
Peter Zijlstra665c2142009-05-29 14:51:57 +02002486
2487 rcu_read_lock();
2488 /*
2489 * doesn't really matter which of the child contexts the
2490 * events ends up in.
2491 */
2492 ctx = rcu_dereference(current->perf_counter_ctxp);
2493 if (ctx)
2494 perf_counter_comm_ctx(ctx, comm_event);
2495 rcu_read_unlock();
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002496}
2497
2498void perf_counter_comm(struct task_struct *task)
2499{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002500 struct perf_comm_event comm_event;
2501
2502 if (!atomic_read(&nr_comm_tracking))
2503 return;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10002504
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002505 comm_event = (struct perf_comm_event){
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002506 .task = task,
2507 .event = {
2508 .header = { .type = PERF_EVENT_COMM, },
2509 .pid = task->group_leader->pid,
2510 .tid = task->pid,
2511 },
2512 };
2513
2514 perf_counter_comm_event(&comm_event);
2515}
2516
2517/*
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002518 * mmap tracking
2519 */
2520
2521struct perf_mmap_event {
2522 struct file *file;
2523 char *file_name;
2524 int file_size;
2525
2526 struct {
2527 struct perf_event_header header;
2528
2529 u32 pid;
2530 u32 tid;
2531 u64 start;
2532 u64 len;
2533 u64 pgoff;
2534 } event;
2535};
2536
2537static void perf_counter_mmap_output(struct perf_counter *counter,
2538 struct perf_mmap_event *mmap_event)
2539{
2540 struct perf_output_handle handle;
2541 int size = mmap_event->event.header.size;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002542 int ret = perf_output_begin(&handle, counter, size, 0, 0);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002543
2544 if (ret)
2545 return;
2546
2547 perf_output_put(&handle, mmap_event->event);
2548 perf_output_copy(&handle, mmap_event->file_name,
2549 mmap_event->file_size);
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002550 perf_output_end(&handle);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002551}
2552
2553static int perf_counter_mmap_match(struct perf_counter *counter,
2554 struct perf_mmap_event *mmap_event)
2555{
2556 if (counter->hw_event.mmap &&
2557 mmap_event->event.header.type == PERF_EVENT_MMAP)
2558 return 1;
2559
2560 if (counter->hw_event.munmap &&
2561 mmap_event->event.header.type == PERF_EVENT_MUNMAP)
2562 return 1;
2563
2564 return 0;
2565}
2566
2567static void perf_counter_mmap_ctx(struct perf_counter_context *ctx,
2568 struct perf_mmap_event *mmap_event)
2569{
2570 struct perf_counter *counter;
2571
2572 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2573 return;
2574
2575 rcu_read_lock();
2576 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2577 if (perf_counter_mmap_match(counter, mmap_event))
2578 perf_counter_mmap_output(counter, mmap_event);
2579 }
2580 rcu_read_unlock();
2581}
2582
2583static void perf_counter_mmap_event(struct perf_mmap_event *mmap_event)
2584{
2585 struct perf_cpu_context *cpuctx;
Peter Zijlstra665c2142009-05-29 14:51:57 +02002586 struct perf_counter_context *ctx;
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002587 struct file *file = mmap_event->file;
2588 unsigned int size;
2589 char tmp[16];
2590 char *buf = NULL;
2591 char *name;
2592
2593 if (file) {
2594 buf = kzalloc(PATH_MAX, GFP_KERNEL);
2595 if (!buf) {
2596 name = strncpy(tmp, "//enomem", sizeof(tmp));
2597 goto got_name;
2598 }
Peter Zijlstrad3d21c42009-04-09 10:53:46 +02002599 name = d_path(&file->f_path, buf, PATH_MAX);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002600 if (IS_ERR(name)) {
2601 name = strncpy(tmp, "//toolong", sizeof(tmp));
2602 goto got_name;
2603 }
2604 } else {
2605 name = strncpy(tmp, "//anon", sizeof(tmp));
2606 goto got_name;
2607 }
2608
2609got_name:
Ingo Molnar888fcee2009-04-09 09:48:22 +02002610 size = ALIGN(strlen(name)+1, sizeof(u64));
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002611
2612 mmap_event->file_name = name;
2613 mmap_event->file_size = size;
2614
2615 mmap_event->event.header.size = sizeof(mmap_event->event) + size;
2616
2617 cpuctx = &get_cpu_var(perf_cpu_context);
2618 perf_counter_mmap_ctx(&cpuctx->ctx, mmap_event);
2619 put_cpu_var(perf_cpu_context);
2620
Peter Zijlstra665c2142009-05-29 14:51:57 +02002621 rcu_read_lock();
2622 /*
2623 * doesn't really matter which of the child contexts the
2624 * events ends up in.
2625 */
2626 ctx = rcu_dereference(current->perf_counter_ctxp);
2627 if (ctx)
2628 perf_counter_mmap_ctx(ctx, mmap_event);
2629 rcu_read_unlock();
2630
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002631 kfree(buf);
2632}
2633
2634void perf_counter_mmap(unsigned long addr, unsigned long len,
2635 unsigned long pgoff, struct file *file)
2636{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002637 struct perf_mmap_event mmap_event;
2638
2639 if (!atomic_read(&nr_mmap_tracking))
2640 return;
2641
2642 mmap_event = (struct perf_mmap_event){
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002643 .file = file,
2644 .event = {
2645 .header = { .type = PERF_EVENT_MMAP, },
2646 .pid = current->group_leader->pid,
2647 .tid = current->pid,
2648 .start = addr,
2649 .len = len,
2650 .pgoff = pgoff,
2651 },
2652 };
2653
2654 perf_counter_mmap_event(&mmap_event);
2655}
2656
2657void perf_counter_munmap(unsigned long addr, unsigned long len,
2658 unsigned long pgoff, struct file *file)
2659{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002660 struct perf_mmap_event mmap_event;
2661
2662 if (!atomic_read(&nr_munmap_tracking))
2663 return;
2664
2665 mmap_event = (struct perf_mmap_event){
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002666 .file = file,
2667 .event = {
2668 .header = { .type = PERF_EVENT_MUNMAP, },
2669 .pid = current->group_leader->pid,
2670 .tid = current->pid,
2671 .start = addr,
2672 .len = len,
2673 .pgoff = pgoff,
2674 },
2675 };
2676
2677 perf_counter_mmap_event(&mmap_event);
2678}
2679
2680/*
Peter Zijlstrae220d2d2009-05-23 18:28:55 +02002681 * Log irq_period changes so that analyzing tools can re-normalize the
2682 * event flow.
Peter Zijlstra26b119b2009-05-20 12:21:20 +02002683 */
2684
2685static void perf_log_period(struct perf_counter *counter, u64 period)
2686{
2687 struct perf_output_handle handle;
2688 int ret;
2689
2690 struct {
2691 struct perf_event_header header;
2692 u64 time;
2693 u64 period;
2694 } freq_event = {
2695 .header = {
2696 .type = PERF_EVENT_PERIOD,
2697 .misc = 0,
2698 .size = sizeof(freq_event),
2699 },
2700 .time = sched_clock(),
2701 .period = period,
2702 };
2703
2704 if (counter->hw.irq_period == period)
2705 return;
2706
2707 ret = perf_output_begin(&handle, counter, sizeof(freq_event), 0, 0);
2708 if (ret)
2709 return;
2710
2711 perf_output_put(&handle, freq_event);
2712 perf_output_end(&handle);
2713}
2714
2715/*
Peter Zijlstraa78ac322009-05-25 17:39:05 +02002716 * IRQ throttle logging
2717 */
2718
2719static void perf_log_throttle(struct perf_counter *counter, int enable)
2720{
2721 struct perf_output_handle handle;
2722 int ret;
2723
2724 struct {
2725 struct perf_event_header header;
2726 u64 time;
2727 } throttle_event = {
2728 .header = {
2729 .type = PERF_EVENT_THROTTLE + 1,
2730 .misc = 0,
2731 .size = sizeof(throttle_event),
2732 },
2733 .time = sched_clock(),
2734 };
2735
Ingo Molnar0127c3e2009-05-25 22:03:26 +02002736 ret = perf_output_begin(&handle, counter, sizeof(throttle_event), 1, 0);
Peter Zijlstraa78ac322009-05-25 17:39:05 +02002737 if (ret)
2738 return;
2739
2740 perf_output_put(&handle, throttle_event);
2741 perf_output_end(&handle);
2742}
2743
2744/*
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002745 * Generic counter overflow handling.
2746 */
2747
2748int perf_counter_overflow(struct perf_counter *counter,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002749 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002750{
Peter Zijlstra79f14642009-04-06 11:45:07 +02002751 int events = atomic_read(&counter->event_limit);
Peter Zijlstraa78ac322009-05-25 17:39:05 +02002752 int throttle = counter->pmu->unthrottle != NULL;
Peter Zijlstra79f14642009-04-06 11:45:07 +02002753 int ret = 0;
2754
Peter Zijlstraa78ac322009-05-25 17:39:05 +02002755 if (!throttle) {
2756 counter->hw.interrupts++;
2757 } else if (counter->hw.interrupts != MAX_INTERRUPTS) {
2758 counter->hw.interrupts++;
2759 if (HZ*counter->hw.interrupts > (u64)sysctl_perf_counter_limit) {
2760 counter->hw.interrupts = MAX_INTERRUPTS;
2761 perf_log_throttle(counter, 0);
2762 ret = 1;
2763 }
2764 }
Peter Zijlstra60db5e02009-05-15 15:19:28 +02002765
Peter Zijlstra2023b352009-05-05 17:50:26 +02002766 /*
2767 * XXX event_limit might not quite work as expected on inherited
2768 * counters
2769 */
2770
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002771 counter->pending_kill = POLL_IN;
Peter Zijlstra79f14642009-04-06 11:45:07 +02002772 if (events && atomic_dec_and_test(&counter->event_limit)) {
2773 ret = 1;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002774 counter->pending_kill = POLL_HUP;
Peter Zijlstra79f14642009-04-06 11:45:07 +02002775 if (nmi) {
2776 counter->pending_disable = 1;
2777 perf_pending_queue(&counter->pending,
2778 perf_pending_counter);
2779 } else
2780 perf_counter_disable(counter);
2781 }
2782
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002783 perf_counter_output(counter, nmi, regs, addr);
Peter Zijlstra79f14642009-04-06 11:45:07 +02002784 return ret;
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002785}
2786
2787/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002788 * Generic software counter infrastructure
2789 */
2790
2791static void perf_swcounter_update(struct perf_counter *counter)
2792{
2793 struct hw_perf_counter *hwc = &counter->hw;
2794 u64 prev, now;
2795 s64 delta;
2796
2797again:
2798 prev = atomic64_read(&hwc->prev_count);
2799 now = atomic64_read(&hwc->count);
2800 if (atomic64_cmpxchg(&hwc->prev_count, prev, now) != prev)
2801 goto again;
2802
2803 delta = now - prev;
2804
2805 atomic64_add(delta, &counter->count);
2806 atomic64_sub(delta, &hwc->period_left);
2807}
2808
2809static void perf_swcounter_set_period(struct perf_counter *counter)
2810{
2811 struct hw_perf_counter *hwc = &counter->hw;
2812 s64 left = atomic64_read(&hwc->period_left);
2813 s64 period = hwc->irq_period;
2814
2815 if (unlikely(left <= -period)) {
2816 left = period;
2817 atomic64_set(&hwc->period_left, left);
2818 }
2819
2820 if (unlikely(left <= 0)) {
2821 left += period;
2822 atomic64_add(period, &hwc->period_left);
2823 }
2824
2825 atomic64_set(&hwc->prev_count, -left);
2826 atomic64_set(&hwc->count, -left);
2827}
2828
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002829static enum hrtimer_restart perf_swcounter_hrtimer(struct hrtimer *hrtimer)
2830{
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002831 enum hrtimer_restart ret = HRTIMER_RESTART;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002832 struct perf_counter *counter;
2833 struct pt_regs *regs;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02002834 u64 period;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002835
2836 counter = container_of(hrtimer, struct perf_counter, hw.hrtimer);
Robert Richter4aeb0b42009-04-29 12:47:03 +02002837 counter->pmu->read(counter);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002838
2839 regs = get_irq_regs();
2840 /*
2841 * In case we exclude kernel IPs or are somehow not in interrupt
2842 * context, provide the next best thing, the user IP.
2843 */
2844 if ((counter->hw_event.exclude_kernel || !regs) &&
2845 !counter->hw_event.exclude_user)
2846 regs = task_pt_regs(current);
2847
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002848 if (regs) {
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002849 if (perf_counter_overflow(counter, 0, regs, 0))
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002850 ret = HRTIMER_NORESTART;
2851 }
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002852
Peter Zijlstra60db5e02009-05-15 15:19:28 +02002853 period = max_t(u64, 10000, counter->hw.irq_period);
2854 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002855
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002856 return ret;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002857}
2858
2859static void perf_swcounter_overflow(struct perf_counter *counter,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002860 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002861{
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002862 perf_swcounter_update(counter);
2863 perf_swcounter_set_period(counter);
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002864 if (perf_counter_overflow(counter, nmi, regs, addr))
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002865 /* soft-disable the counter */
2866 ;
2867
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002868}
2869
Paul Mackerras880ca152009-06-01 17:49:14 +10002870static int perf_swcounter_is_counting(struct perf_counter *counter)
2871{
2872 struct perf_counter_context *ctx;
2873 unsigned long flags;
2874 int count;
2875
2876 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
2877 return 1;
2878
2879 if (counter->state != PERF_COUNTER_STATE_INACTIVE)
2880 return 0;
2881
2882 /*
2883 * If the counter is inactive, it could be just because
2884 * its task is scheduled out, or because it's in a group
2885 * which could not go on the PMU. We want to count in
2886 * the first case but not the second. If the context is
2887 * currently active then an inactive software counter must
2888 * be the second case. If it's not currently active then
2889 * we need to know whether the counter was active when the
2890 * context was last active, which we can determine by
2891 * comparing counter->tstamp_stopped with ctx->time.
2892 *
2893 * We are within an RCU read-side critical section,
2894 * which protects the existence of *ctx.
2895 */
2896 ctx = counter->ctx;
2897 spin_lock_irqsave(&ctx->lock, flags);
2898 count = 1;
2899 /* Re-check state now we have the lock */
2900 if (counter->state < PERF_COUNTER_STATE_INACTIVE ||
2901 counter->ctx->is_active ||
2902 counter->tstamp_stopped < ctx->time)
2903 count = 0;
2904 spin_unlock_irqrestore(&ctx->lock, flags);
2905 return count;
2906}
2907
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002908static int perf_swcounter_match(struct perf_counter *counter,
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002909 enum perf_event_types type,
2910 u32 event, struct pt_regs *regs)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002911{
Paul Mackerras880ca152009-06-01 17:49:14 +10002912 u64 event_config;
2913
2914 event_config = ((u64) type << PERF_COUNTER_TYPE_SHIFT) | event;
2915
2916 if (!perf_swcounter_is_counting(counter))
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002917 return 0;
2918
Paul Mackerras880ca152009-06-01 17:49:14 +10002919 if (counter->hw_event.config != event_config)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002920 return 0;
2921
2922 if (counter->hw_event.exclude_user && user_mode(regs))
2923 return 0;
2924
2925 if (counter->hw_event.exclude_kernel && !user_mode(regs))
2926 return 0;
2927
2928 return 1;
2929}
2930
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002931static void perf_swcounter_add(struct perf_counter *counter, u64 nr,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002932 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002933{
2934 int neg = atomic64_add_negative(nr, &counter->hw.count);
2935 if (counter->hw.irq_period && !neg)
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002936 perf_swcounter_overflow(counter, nmi, regs, addr);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002937}
2938
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002939static void perf_swcounter_ctx_event(struct perf_counter_context *ctx,
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002940 enum perf_event_types type, u32 event,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002941 u64 nr, int nmi, struct pt_regs *regs,
2942 u64 addr)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002943{
2944 struct perf_counter *counter;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002945
Peter Zijlstra01ef09d2009-03-19 20:26:11 +01002946 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002947 return;
2948
Peter Zijlstra592903c2009-03-13 12:21:36 +01002949 rcu_read_lock();
2950 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002951 if (perf_swcounter_match(counter, type, event, regs))
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002952 perf_swcounter_add(counter, nr, nmi, regs, addr);
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002953 }
Peter Zijlstra592903c2009-03-13 12:21:36 +01002954 rcu_read_unlock();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002955}
2956
Peter Zijlstra96f6d442009-03-23 18:22:07 +01002957static int *perf_swcounter_recursion_context(struct perf_cpu_context *cpuctx)
2958{
2959 if (in_nmi())
2960 return &cpuctx->recursion[3];
2961
2962 if (in_irq())
2963 return &cpuctx->recursion[2];
2964
2965 if (in_softirq())
2966 return &cpuctx->recursion[1];
2967
2968 return &cpuctx->recursion[0];
2969}
2970
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002971static void __perf_swcounter_event(enum perf_event_types type, u32 event,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002972 u64 nr, int nmi, struct pt_regs *regs,
2973 u64 addr)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002974{
2975 struct perf_cpu_context *cpuctx = &get_cpu_var(perf_cpu_context);
Peter Zijlstra96f6d442009-03-23 18:22:07 +01002976 int *recursion = perf_swcounter_recursion_context(cpuctx);
Peter Zijlstra665c2142009-05-29 14:51:57 +02002977 struct perf_counter_context *ctx;
Peter Zijlstra96f6d442009-03-23 18:22:07 +01002978
2979 if (*recursion)
2980 goto out;
2981
2982 (*recursion)++;
2983 barrier();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002984
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002985 perf_swcounter_ctx_event(&cpuctx->ctx, type, event,
2986 nr, nmi, regs, addr);
Peter Zijlstra665c2142009-05-29 14:51:57 +02002987 rcu_read_lock();
2988 /*
2989 * doesn't really matter which of the child contexts the
2990 * events ends up in.
2991 */
2992 ctx = rcu_dereference(current->perf_counter_ctxp);
2993 if (ctx)
2994 perf_swcounter_ctx_event(ctx, type, event, nr, nmi, regs, addr);
2995 rcu_read_unlock();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002996
Peter Zijlstra96f6d442009-03-23 18:22:07 +01002997 barrier();
2998 (*recursion)--;
2999
3000out:
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003001 put_cpu_var(perf_cpu_context);
3002}
3003
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003004void
3005perf_swcounter_event(u32 event, u64 nr, int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003006{
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003007 __perf_swcounter_event(PERF_TYPE_SOFTWARE, event, nr, nmi, regs, addr);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003008}
3009
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003010static void perf_swcounter_read(struct perf_counter *counter)
3011{
3012 perf_swcounter_update(counter);
3013}
3014
3015static int perf_swcounter_enable(struct perf_counter *counter)
3016{
3017 perf_swcounter_set_period(counter);
3018 return 0;
3019}
3020
3021static void perf_swcounter_disable(struct perf_counter *counter)
3022{
3023 perf_swcounter_update(counter);
3024}
3025
Robert Richter4aeb0b42009-04-29 12:47:03 +02003026static const struct pmu perf_ops_generic = {
Peter Zijlstraac17dc82009-03-13 12:21:34 +01003027 .enable = perf_swcounter_enable,
3028 .disable = perf_swcounter_disable,
3029 .read = perf_swcounter_read,
3030};
3031
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003032/*
3033 * Software counter: cpu wall time clock
3034 */
3035
Paul Mackerras9abf8a02009-01-09 16:26:43 +11003036static void cpu_clock_perf_counter_update(struct perf_counter *counter)
3037{
3038 int cpu = raw_smp_processor_id();
3039 s64 prev;
3040 u64 now;
3041
3042 now = cpu_clock(cpu);
3043 prev = atomic64_read(&counter->hw.prev_count);
3044 atomic64_set(&counter->hw.prev_count, now);
3045 atomic64_add(now - prev, &counter->count);
3046}
3047
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003048static int cpu_clock_perf_counter_enable(struct perf_counter *counter)
3049{
3050 struct hw_perf_counter *hwc = &counter->hw;
3051 int cpu = raw_smp_processor_id();
3052
3053 atomic64_set(&hwc->prev_count, cpu_clock(cpu));
Peter Zijlstra039fc912009-03-13 16:43:47 +01003054 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
3055 hwc->hrtimer.function = perf_swcounter_hrtimer;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003056 if (hwc->irq_period) {
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003057 u64 period = max_t(u64, 10000, hwc->irq_period);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003058 __hrtimer_start_range_ns(&hwc->hrtimer,
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003059 ns_to_ktime(period), 0,
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003060 HRTIMER_MODE_REL, 0);
3061 }
3062
3063 return 0;
3064}
3065
Ingo Molnar5c92d122008-12-11 13:21:10 +01003066static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
3067{
Peter Zijlstrab986d7e2009-05-20 12:21:21 +02003068 if (counter->hw.irq_period)
3069 hrtimer_cancel(&counter->hw.hrtimer);
Paul Mackerras9abf8a02009-01-09 16:26:43 +11003070 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01003071}
3072
3073static void cpu_clock_perf_counter_read(struct perf_counter *counter)
3074{
Paul Mackerras9abf8a02009-01-09 16:26:43 +11003075 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01003076}
3077
Robert Richter4aeb0b42009-04-29 12:47:03 +02003078static const struct pmu perf_ops_cpu_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +01003079 .enable = cpu_clock_perf_counter_enable,
3080 .disable = cpu_clock_perf_counter_disable,
3081 .read = cpu_clock_perf_counter_read,
Ingo Molnar5c92d122008-12-11 13:21:10 +01003082};
3083
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01003084/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003085 * Software counter: task time clock
3086 */
3087
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02003088static void task_clock_perf_counter_update(struct perf_counter *counter, u64 now)
Ingo Molnarbae43c92008-12-11 14:03:20 +01003089{
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02003090 u64 prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003091 s64 delta;
Ingo Molnarbae43c92008-12-11 14:03:20 +01003092
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02003093 prev = atomic64_xchg(&counter->hw.prev_count, now);
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003094 delta = now - prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003095 atomic64_add(delta, &counter->count);
Ingo Molnarbae43c92008-12-11 14:03:20 +01003096}
3097
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01003098static int task_clock_perf_counter_enable(struct perf_counter *counter)
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003099{
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003100 struct hw_perf_counter *hwc = &counter->hw;
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02003101 u64 now;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003102
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02003103 now = counter->ctx->time;
3104
3105 atomic64_set(&hwc->prev_count, now);
Peter Zijlstra039fc912009-03-13 16:43:47 +01003106 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
3107 hwc->hrtimer.function = perf_swcounter_hrtimer;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003108 if (hwc->irq_period) {
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003109 u64 period = max_t(u64, 10000, hwc->irq_period);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003110 __hrtimer_start_range_ns(&hwc->hrtimer,
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003111 ns_to_ktime(period), 0,
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003112 HRTIMER_MODE_REL, 0);
3113 }
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01003114
3115 return 0;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003116}
3117
3118static void task_clock_perf_counter_disable(struct perf_counter *counter)
3119{
Peter Zijlstrab986d7e2009-05-20 12:21:21 +02003120 if (counter->hw.irq_period)
3121 hrtimer_cancel(&counter->hw.hrtimer);
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02003122 task_clock_perf_counter_update(counter, counter->ctx->time);
3123
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003124}
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01003125
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003126static void task_clock_perf_counter_read(struct perf_counter *counter)
3127{
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02003128 u64 time;
3129
3130 if (!in_nmi()) {
3131 update_context_time(counter->ctx);
3132 time = counter->ctx->time;
3133 } else {
3134 u64 now = perf_clock();
3135 u64 delta = now - counter->ctx->timestamp;
3136 time = counter->ctx->time + delta;
3137 }
3138
3139 task_clock_perf_counter_update(counter, time);
Ingo Molnarbae43c92008-12-11 14:03:20 +01003140}
3141
Robert Richter4aeb0b42009-04-29 12:47:03 +02003142static const struct pmu perf_ops_task_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +01003143 .enable = task_clock_perf_counter_enable,
3144 .disable = task_clock_perf_counter_disable,
3145 .read = task_clock_perf_counter_read,
Ingo Molnarbae43c92008-12-11 14:03:20 +01003146};
3147
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003148/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003149 * Software counter: cpu migrations
3150 */
3151
Paul Mackerras23a185c2009-02-09 22:42:47 +11003152static inline u64 get_cpu_migrations(struct perf_counter *counter)
Ingo Molnar6c594c22008-12-14 12:34:15 +01003153{
Paul Mackerras23a185c2009-02-09 22:42:47 +11003154 struct task_struct *curr = counter->ctx->task;
3155
3156 if (curr)
3157 return curr->se.nr_migrations;
3158 return cpu_nr_migrations(smp_processor_id());
Ingo Molnar6c594c22008-12-14 12:34:15 +01003159}
3160
3161static void cpu_migrations_perf_counter_update(struct perf_counter *counter)
3162{
3163 u64 prev, now;
3164 s64 delta;
3165
3166 prev = atomic64_read(&counter->hw.prev_count);
Paul Mackerras23a185c2009-02-09 22:42:47 +11003167 now = get_cpu_migrations(counter);
Ingo Molnar6c594c22008-12-14 12:34:15 +01003168
3169 atomic64_set(&counter->hw.prev_count, now);
3170
3171 delta = now - prev;
Ingo Molnar6c594c22008-12-14 12:34:15 +01003172
3173 atomic64_add(delta, &counter->count);
3174}
3175
3176static void cpu_migrations_perf_counter_read(struct perf_counter *counter)
3177{
3178 cpu_migrations_perf_counter_update(counter);
3179}
3180
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01003181static int cpu_migrations_perf_counter_enable(struct perf_counter *counter)
Ingo Molnar6c594c22008-12-14 12:34:15 +01003182{
Paul Mackerrasc07c99b2009-02-13 22:10:34 +11003183 if (counter->prev_state <= PERF_COUNTER_STATE_OFF)
3184 atomic64_set(&counter->hw.prev_count,
3185 get_cpu_migrations(counter));
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01003186 return 0;
Ingo Molnar6c594c22008-12-14 12:34:15 +01003187}
3188
3189static void cpu_migrations_perf_counter_disable(struct perf_counter *counter)
3190{
3191 cpu_migrations_perf_counter_update(counter);
3192}
3193
Robert Richter4aeb0b42009-04-29 12:47:03 +02003194static const struct pmu perf_ops_cpu_migrations = {
Ingo Molnar76715812008-12-17 14:20:28 +01003195 .enable = cpu_migrations_perf_counter_enable,
3196 .disable = cpu_migrations_perf_counter_disable,
3197 .read = cpu_migrations_perf_counter_read,
Ingo Molnar6c594c22008-12-14 12:34:15 +01003198};
3199
Peter Zijlstrae077df42009-03-19 20:26:17 +01003200#ifdef CONFIG_EVENT_PROFILE
3201void perf_tpcounter_event(int event_id)
3202{
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003203 struct pt_regs *regs = get_irq_regs();
3204
3205 if (!regs)
3206 regs = task_pt_regs(current);
3207
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003208 __perf_swcounter_event(PERF_TYPE_TRACEPOINT, event_id, 1, 1, regs, 0);
Peter Zijlstrae077df42009-03-19 20:26:17 +01003209}
Steven Whitehouseff7b1b42009-04-15 16:55:05 +01003210EXPORT_SYMBOL_GPL(perf_tpcounter_event);
Peter Zijlstrae077df42009-03-19 20:26:17 +01003211
3212extern int ftrace_profile_enable(int);
3213extern void ftrace_profile_disable(int);
3214
3215static void tp_perf_counter_destroy(struct perf_counter *counter)
3216{
Peter Zijlstraf4a2deb42009-03-23 18:22:06 +01003217 ftrace_profile_disable(perf_event_id(&counter->hw_event));
Peter Zijlstrae077df42009-03-19 20:26:17 +01003218}
3219
Robert Richter4aeb0b42009-04-29 12:47:03 +02003220static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
Peter Zijlstrae077df42009-03-19 20:26:17 +01003221{
Peter Zijlstraf4a2deb42009-03-23 18:22:06 +01003222 int event_id = perf_event_id(&counter->hw_event);
Peter Zijlstrae077df42009-03-19 20:26:17 +01003223 int ret;
3224
3225 ret = ftrace_profile_enable(event_id);
3226 if (ret)
3227 return NULL;
3228
3229 counter->destroy = tp_perf_counter_destroy;
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003230 counter->hw.irq_period = counter->hw_event.irq_period;
Peter Zijlstrae077df42009-03-19 20:26:17 +01003231
3232 return &perf_ops_generic;
3233}
3234#else
Robert Richter4aeb0b42009-04-29 12:47:03 +02003235static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
Peter Zijlstrae077df42009-03-19 20:26:17 +01003236{
3237 return NULL;
3238}
3239#endif
3240
Robert Richter4aeb0b42009-04-29 12:47:03 +02003241static const struct pmu *sw_perf_counter_init(struct perf_counter *counter)
Ingo Molnar5c92d122008-12-11 13:21:10 +01003242{
Robert Richter4aeb0b42009-04-29 12:47:03 +02003243 const struct pmu *pmu = NULL;
Ingo Molnar5c92d122008-12-11 13:21:10 +01003244
Paul Mackerras0475f9e2009-02-11 14:35:35 +11003245 /*
3246 * Software counters (currently) can't in general distinguish
3247 * between user, kernel and hypervisor events.
3248 * However, context switches and cpu migrations are considered
3249 * to be kernel events, and page faults are never hypervisor
3250 * events.
3251 */
Peter Zijlstraf4a2deb42009-03-23 18:22:06 +01003252 switch (perf_event_id(&counter->hw_event)) {
Ingo Molnar5c92d122008-12-11 13:21:10 +01003253 case PERF_COUNT_CPU_CLOCK:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003254 pmu = &perf_ops_cpu_clock;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003255
Ingo Molnar5c92d122008-12-11 13:21:10 +01003256 break;
Ingo Molnarbae43c92008-12-11 14:03:20 +01003257 case PERF_COUNT_TASK_CLOCK:
Paul Mackerras23a185c2009-02-09 22:42:47 +11003258 /*
3259 * If the user instantiates this as a per-cpu counter,
3260 * use the cpu_clock counter instead.
3261 */
3262 if (counter->ctx->task)
Robert Richter4aeb0b42009-04-29 12:47:03 +02003263 pmu = &perf_ops_task_clock;
Paul Mackerras23a185c2009-02-09 22:42:47 +11003264 else
Robert Richter4aeb0b42009-04-29 12:47:03 +02003265 pmu = &perf_ops_cpu_clock;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003266
Ingo Molnarbae43c92008-12-11 14:03:20 +01003267 break;
Ingo Molnare06c61a2008-12-14 14:44:31 +01003268 case PERF_COUNT_PAGE_FAULTS:
Peter Zijlstraac17dc82009-03-13 12:21:34 +01003269 case PERF_COUNT_PAGE_FAULTS_MIN:
3270 case PERF_COUNT_PAGE_FAULTS_MAJ:
Ingo Molnar5d6a27d2008-12-14 12:28:33 +01003271 case PERF_COUNT_CONTEXT_SWITCHES:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003272 pmu = &perf_ops_generic;
Ingo Molnar5d6a27d2008-12-14 12:28:33 +01003273 break;
Ingo Molnar6c594c22008-12-14 12:34:15 +01003274 case PERF_COUNT_CPU_MIGRATIONS:
Paul Mackerras0475f9e2009-02-11 14:35:35 +11003275 if (!counter->hw_event.exclude_kernel)
Robert Richter4aeb0b42009-04-29 12:47:03 +02003276 pmu = &perf_ops_cpu_migrations;
Ingo Molnar6c594c22008-12-14 12:34:15 +01003277 break;
Ingo Molnar5c92d122008-12-11 13:21:10 +01003278 }
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003279
Robert Richter4aeb0b42009-04-29 12:47:03 +02003280 return pmu;
Ingo Molnar5c92d122008-12-11 13:21:10 +01003281}
3282
Thomas Gleixner0793a612008-12-04 20:12:29 +01003283/*
3284 * Allocate and initialize a counter structure
3285 */
3286static struct perf_counter *
Ingo Molnar04289bb2008-12-11 08:38:42 +01003287perf_counter_alloc(struct perf_counter_hw_event *hw_event,
3288 int cpu,
Paul Mackerras23a185c2009-02-09 22:42:47 +11003289 struct perf_counter_context *ctx,
Ingo Molnar9b51f662008-12-12 13:49:45 +01003290 struct perf_counter *group_leader,
3291 gfp_t gfpflags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003292{
Robert Richter4aeb0b42009-04-29 12:47:03 +02003293 const struct pmu *pmu;
Ingo Molnar621a01e2008-12-11 12:46:46 +01003294 struct perf_counter *counter;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003295 struct hw_perf_counter *hwc;
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003296 long err;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003297
Ingo Molnar9b51f662008-12-12 13:49:45 +01003298 counter = kzalloc(sizeof(*counter), gfpflags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003299 if (!counter)
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003300 return ERR_PTR(-ENOMEM);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003301
Ingo Molnar04289bb2008-12-11 08:38:42 +01003302 /*
3303 * Single counters are their own group leaders, with an
3304 * empty sibling list:
3305 */
3306 if (!group_leader)
3307 group_leader = counter;
3308
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003309 mutex_init(&counter->child_mutex);
3310 INIT_LIST_HEAD(&counter->child_list);
3311
Ingo Molnar04289bb2008-12-11 08:38:42 +01003312 INIT_LIST_HEAD(&counter->list_entry);
Peter Zijlstra592903c2009-03-13 12:21:36 +01003313 INIT_LIST_HEAD(&counter->event_entry);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003314 INIT_LIST_HEAD(&counter->sibling_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003315 init_waitqueue_head(&counter->waitq);
3316
Peter Zijlstra7b732a72009-03-23 18:22:10 +01003317 mutex_init(&counter->mmap_mutex);
3318
Ingo Molnar9f66a382008-12-10 12:33:23 +01003319 counter->cpu = cpu;
3320 counter->hw_event = *hw_event;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003321 counter->group_leader = group_leader;
Robert Richter4aeb0b42009-04-29 12:47:03 +02003322 counter->pmu = NULL;
Paul Mackerras23a185c2009-02-09 22:42:47 +11003323 counter->ctx = ctx;
Ingo Molnar329d8762009-05-26 08:10:00 +02003324 counter->oncpu = -1;
3325
Ingo Molnar235c7fc2008-12-21 14:43:25 +01003326 counter->state = PERF_COUNTER_STATE_INACTIVE;
Ingo Molnara86ed502008-12-17 00:43:10 +01003327 if (hw_event->disabled)
3328 counter->state = PERF_COUNTER_STATE_OFF;
3329
Robert Richter4aeb0b42009-04-29 12:47:03 +02003330 pmu = NULL;
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003331
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003332 hwc = &counter->hw;
3333 if (hw_event->freq && hw_event->irq_freq)
Peter Zijlstra2e569d32009-05-15 15:37:47 +02003334 hwc->irq_period = div64_u64(TICK_NSEC, hw_event->irq_freq);
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003335 else
3336 hwc->irq_period = hw_event->irq_period;
3337
Peter Zijlstra2023b352009-05-05 17:50:26 +02003338 /*
3339 * we currently do not support PERF_RECORD_GROUP on inherited counters
3340 */
3341 if (hw_event->inherit && (hw_event->record_type & PERF_RECORD_GROUP))
3342 goto done;
3343
Peter Zijlstraf4a2deb42009-03-23 18:22:06 +01003344 if (perf_event_raw(hw_event)) {
Robert Richter4aeb0b42009-04-29 12:47:03 +02003345 pmu = hw_perf_counter_init(counter);
Peter Zijlstraf4a2deb42009-03-23 18:22:06 +01003346 goto done;
3347 }
3348
3349 switch (perf_event_type(hw_event)) {
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003350 case PERF_TYPE_HARDWARE:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003351 pmu = hw_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003352 break;
3353
3354 case PERF_TYPE_SOFTWARE:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003355 pmu = sw_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003356 break;
3357
3358 case PERF_TYPE_TRACEPOINT:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003359 pmu = tp_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003360 break;
3361 }
Peter Zijlstraf4a2deb42009-03-23 18:22:06 +01003362done:
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003363 err = 0;
Robert Richter4aeb0b42009-04-29 12:47:03 +02003364 if (!pmu)
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003365 err = -EINVAL;
Robert Richter4aeb0b42009-04-29 12:47:03 +02003366 else if (IS_ERR(pmu))
3367 err = PTR_ERR(pmu);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003368
3369 if (err) {
3370 kfree(counter);
3371 return ERR_PTR(err);
3372 }
3373
Robert Richter4aeb0b42009-04-29 12:47:03 +02003374 counter->pmu = pmu;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003375
Peter Zijlstra7fc23a52009-05-08 18:52:21 +02003376 atomic_inc(&nr_counters);
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02003377 if (counter->hw_event.mmap)
3378 atomic_inc(&nr_mmap_tracking);
3379 if (counter->hw_event.munmap)
3380 atomic_inc(&nr_munmap_tracking);
3381 if (counter->hw_event.comm)
3382 atomic_inc(&nr_comm_tracking);
3383
Thomas Gleixner0793a612008-12-04 20:12:29 +01003384 return counter;
3385}
3386
3387/**
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003388 * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
Ingo Molnar9f66a382008-12-10 12:33:23 +01003389 *
3390 * @hw_event_uptr: event type attributes for monitoring/sampling
Thomas Gleixner0793a612008-12-04 20:12:29 +01003391 * @pid: target pid
Ingo Molnar9f66a382008-12-10 12:33:23 +01003392 * @cpu: target cpu
3393 * @group_fd: group leader counter fd
Thomas Gleixner0793a612008-12-04 20:12:29 +01003394 */
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003395SYSCALL_DEFINE5(perf_counter_open,
Paul Mackerrasf3dfd262009-02-26 22:43:46 +11003396 const struct perf_counter_hw_event __user *, hw_event_uptr,
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003397 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003398{
Ingo Molnar04289bb2008-12-11 08:38:42 +01003399 struct perf_counter *counter, *group_leader;
Ingo Molnar9f66a382008-12-10 12:33:23 +01003400 struct perf_counter_hw_event hw_event;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003401 struct perf_counter_context *ctx;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003402 struct file *counter_file = NULL;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003403 struct file *group_file = NULL;
3404 int fput_needed = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003405 int fput_needed2 = 0;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003406 int ret;
3407
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003408 /* for future expandability... */
3409 if (flags)
3410 return -EINVAL;
3411
Ingo Molnar9f66a382008-12-10 12:33:23 +01003412 if (copy_from_user(&hw_event, hw_event_uptr, sizeof(hw_event)) != 0)
Thomas Gleixnereab656a2008-12-08 19:26:59 +01003413 return -EFAULT;
3414
Ingo Molnar04289bb2008-12-11 08:38:42 +01003415 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01003416 * Get the target context (task or percpu):
3417 */
3418 ctx = find_get_context(pid, cpu);
3419 if (IS_ERR(ctx))
3420 return PTR_ERR(ctx);
3421
3422 /*
3423 * Look up the group leader (we will attach this counter to it):
Ingo Molnar04289bb2008-12-11 08:38:42 +01003424 */
3425 group_leader = NULL;
3426 if (group_fd != -1) {
3427 ret = -EINVAL;
3428 group_file = fget_light(group_fd, &fput_needed);
3429 if (!group_file)
Ingo Molnarccff2862008-12-11 11:26:29 +01003430 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003431 if (group_file->f_op != &perf_fops)
Ingo Molnarccff2862008-12-11 11:26:29 +01003432 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003433
3434 group_leader = group_file->private_data;
3435 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01003436 * Do not allow a recursive hierarchy (this new sibling
3437 * becoming part of another group-sibling):
Ingo Molnar04289bb2008-12-11 08:38:42 +01003438 */
Ingo Molnarccff2862008-12-11 11:26:29 +01003439 if (group_leader->group_leader != group_leader)
3440 goto err_put_context;
3441 /*
3442 * Do not allow to attach to a group in a different
3443 * task or CPU context:
3444 */
3445 if (group_leader->ctx != ctx)
3446 goto err_put_context;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11003447 /*
3448 * Only a group leader can be exclusive or pinned
3449 */
3450 if (hw_event.exclusive || hw_event.pinned)
3451 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003452 }
3453
Paul Mackerras23a185c2009-02-09 22:42:47 +11003454 counter = perf_counter_alloc(&hw_event, cpu, ctx, group_leader,
3455 GFP_KERNEL);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003456 ret = PTR_ERR(counter);
3457 if (IS_ERR(counter))
Thomas Gleixner0793a612008-12-04 20:12:29 +01003458 goto err_put_context;
3459
Thomas Gleixner0793a612008-12-04 20:12:29 +01003460 ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
3461 if (ret < 0)
Ingo Molnar9b51f662008-12-12 13:49:45 +01003462 goto err_free_put_context;
3463
3464 counter_file = fget_light(ret, &fput_needed2);
3465 if (!counter_file)
3466 goto err_free_put_context;
3467
3468 counter->filp = counter_file;
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003469 WARN_ON_ONCE(ctx->parent_ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003470 mutex_lock(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003471 perf_install_in_context(ctx, counter, cpu);
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003472 ++ctx->generation;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003473 mutex_unlock(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003474
Peter Zijlstra082ff5a2009-05-23 18:29:00 +02003475 counter->owner = current;
3476 get_task_struct(current);
3477 mutex_lock(&current->perf_counter_mutex);
3478 list_add_tail(&counter->owner_entry, &current->perf_counter_list);
3479 mutex_unlock(&current->perf_counter_mutex);
3480
Ingo Molnar9b51f662008-12-12 13:49:45 +01003481 fput_light(counter_file, fput_needed2);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003482
Ingo Molnar04289bb2008-12-11 08:38:42 +01003483out_fput:
3484 fput_light(group_file, fput_needed);
3485
Thomas Gleixner0793a612008-12-04 20:12:29 +01003486 return ret;
3487
Ingo Molnar9b51f662008-12-12 13:49:45 +01003488err_free_put_context:
Thomas Gleixner0793a612008-12-04 20:12:29 +01003489 kfree(counter);
3490
3491err_put_context:
Paul Mackerrasc93f7662009-05-28 22:18:17 +10003492 put_ctx(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003493
Ingo Molnar04289bb2008-12-11 08:38:42 +01003494 goto out_fput;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003495}
3496
Ingo Molnar9b51f662008-12-12 13:49:45 +01003497/*
Ingo Molnar9b51f662008-12-12 13:49:45 +01003498 * inherit a counter from parent task to child task:
3499 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11003500static struct perf_counter *
Ingo Molnar9b51f662008-12-12 13:49:45 +01003501inherit_counter(struct perf_counter *parent_counter,
3502 struct task_struct *parent,
3503 struct perf_counter_context *parent_ctx,
3504 struct task_struct *child,
Paul Mackerrasd859e292009-01-17 18:10:22 +11003505 struct perf_counter *group_leader,
Ingo Molnar9b51f662008-12-12 13:49:45 +01003506 struct perf_counter_context *child_ctx)
3507{
3508 struct perf_counter *child_counter;
3509
Paul Mackerrasd859e292009-01-17 18:10:22 +11003510 /*
3511 * Instead of creating recursive hierarchies of counters,
3512 * we link inherited counters back to the original parent,
3513 * which has a filp for sure, which we use as the reference
3514 * count:
3515 */
3516 if (parent_counter->parent)
3517 parent_counter = parent_counter->parent;
3518
Ingo Molnar9b51f662008-12-12 13:49:45 +01003519 child_counter = perf_counter_alloc(&parent_counter->hw_event,
Paul Mackerras23a185c2009-02-09 22:42:47 +11003520 parent_counter->cpu, child_ctx,
3521 group_leader, GFP_KERNEL);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003522 if (IS_ERR(child_counter))
3523 return child_counter;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10003524 get_ctx(child_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003525
3526 /*
Paul Mackerras564c2b22009-05-22 14:27:22 +10003527 * Make the child state follow the state of the parent counter,
3528 * not its hw_event.disabled bit. We hold the parent's mutex,
3529 * so we won't race with perf_counter_{en,dis}able_family.
3530 */
3531 if (parent_counter->state >= PERF_COUNTER_STATE_INACTIVE)
3532 child_counter->state = PERF_COUNTER_STATE_INACTIVE;
3533 else
3534 child_counter->state = PERF_COUNTER_STATE_OFF;
3535
3536 /*
Ingo Molnar9b51f662008-12-12 13:49:45 +01003537 * Link it up in the child's context:
3538 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +11003539 add_counter_to_ctx(child_counter, child_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003540
3541 child_counter->parent = parent_counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003542 /*
3543 * inherit into child's child as well:
3544 */
3545 child_counter->hw_event.inherit = 1;
3546
3547 /*
3548 * Get a reference to the parent filp - we will fput it
3549 * when the child counter exits. This is safe to do because
3550 * we are in the parent and we know that the filp still
3551 * exists and has a nonzero count:
3552 */
3553 atomic_long_inc(&parent_counter->filp->f_count);
3554
Paul Mackerrasd859e292009-01-17 18:10:22 +11003555 /*
3556 * Link this into the parent counter's child list
3557 */
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003558 WARN_ON_ONCE(parent_counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003559 mutex_lock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003560 list_add_tail(&child_counter->child_list, &parent_counter->child_list);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003561 mutex_unlock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003562
3563 return child_counter;
3564}
3565
3566static int inherit_group(struct perf_counter *parent_counter,
3567 struct task_struct *parent,
3568 struct perf_counter_context *parent_ctx,
3569 struct task_struct *child,
3570 struct perf_counter_context *child_ctx)
3571{
3572 struct perf_counter *leader;
3573 struct perf_counter *sub;
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003574 struct perf_counter *child_ctr;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003575
3576 leader = inherit_counter(parent_counter, parent, parent_ctx,
3577 child, NULL, child_ctx);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003578 if (IS_ERR(leader))
3579 return PTR_ERR(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003580 list_for_each_entry(sub, &parent_counter->sibling_list, list_entry) {
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003581 child_ctr = inherit_counter(sub, parent, parent_ctx,
3582 child, leader, child_ctx);
3583 if (IS_ERR(child_ctr))
3584 return PTR_ERR(child_ctr);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003585 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01003586 return 0;
3587}
3588
Paul Mackerrasd859e292009-01-17 18:10:22 +11003589static void sync_child_counter(struct perf_counter *child_counter,
3590 struct perf_counter *parent_counter)
3591{
Peter Zijlstra8bc20952009-05-15 20:45:59 +02003592 u64 child_val;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003593
Paul Mackerrasd859e292009-01-17 18:10:22 +11003594 child_val = atomic64_read(&child_counter->count);
3595
3596 /*
3597 * Add back the child's count to the parent's count:
3598 */
3599 atomic64_add(child_val, &parent_counter->count);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11003600 atomic64_add(child_counter->total_time_enabled,
3601 &parent_counter->child_total_time_enabled);
3602 atomic64_add(child_counter->total_time_running,
3603 &parent_counter->child_total_time_running);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003604
3605 /*
3606 * Remove this counter from the parent's list
3607 */
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003608 WARN_ON_ONCE(parent_counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003609 mutex_lock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003610 list_del_init(&child_counter->child_list);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003611 mutex_unlock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003612
3613 /*
3614 * Release the parent counter, if this was the last
3615 * reference to it.
3616 */
3617 fput(parent_counter->filp);
3618}
3619
Ingo Molnar9b51f662008-12-12 13:49:45 +01003620static void
Peter Zijlstrabbbee902009-05-29 14:25:58 +02003621__perf_counter_exit_task(struct perf_counter *child_counter,
Ingo Molnar9b51f662008-12-12 13:49:45 +01003622 struct perf_counter_context *child_ctx)
3623{
3624 struct perf_counter *parent_counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003625
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003626 update_counter_times(child_counter);
Peter Zijlstraaa9c67f2009-05-23 18:28:59 +02003627 perf_counter_remove_from_context(child_counter);
Ingo Molnar0cc0c022008-12-14 23:20:36 +01003628
Ingo Molnar9b51f662008-12-12 13:49:45 +01003629 parent_counter = child_counter->parent;
3630 /*
3631 * It can happen that parent exits first, and has counters
3632 * that are still around due to the child reference. These
3633 * counters need to be zapped - but otherwise linger.
3634 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11003635 if (parent_counter) {
3636 sync_child_counter(child_counter, parent_counter);
Peter Zijlstraf1600952009-03-19 20:26:16 +01003637 free_counter(child_counter);
Paul Mackerras4bcf3492009-02-11 13:53:19 +01003638 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01003639}
3640
3641/*
Paul Mackerrasd859e292009-01-17 18:10:22 +11003642 * When a child task exits, feed back counter values to parent counters.
Ingo Molnar9b51f662008-12-12 13:49:45 +01003643 */
3644void perf_counter_exit_task(struct task_struct *child)
3645{
3646 struct perf_counter *child_counter, *tmp;
3647 struct perf_counter_context *child_ctx;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003648 unsigned long flags;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003649
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003650 if (likely(!child->perf_counter_ctxp))
Ingo Molnar9b51f662008-12-12 13:49:45 +01003651 return;
3652
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003653 local_irq_save(flags);
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003654 /*
3655 * We can't reschedule here because interrupts are disabled,
3656 * and either child is current or it is a task that can't be
3657 * scheduled, so we are now safe from rescheduling changing
3658 * our context.
3659 */
3660 child_ctx = child->perf_counter_ctxp;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003661 __perf_counter_task_sched_out(child_ctx);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10003662
3663 /*
3664 * Take the context lock here so that if find_get_context is
3665 * reading child->perf_counter_ctxp, we wait until it has
3666 * incremented the context's refcount before we do put_ctx below.
3667 */
3668 spin_lock(&child_ctx->lock);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003669 child->perf_counter_ctxp = NULL;
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003670 if (child_ctx->parent_ctx) {
3671 /*
3672 * This context is a clone; unclone it so it can't get
3673 * swapped to another process while we're removing all
3674 * the counters from it.
3675 */
3676 put_ctx(child_ctx->parent_ctx);
3677 child_ctx->parent_ctx = NULL;
3678 }
Paul Mackerrasc93f7662009-05-28 22:18:17 +10003679 spin_unlock(&child_ctx->lock);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003680 local_irq_restore(flags);
3681
3682 mutex_lock(&child_ctx->mutex);
3683
Peter Zijlstra8bc20952009-05-15 20:45:59 +02003684again:
Ingo Molnar9b51f662008-12-12 13:49:45 +01003685 list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list,
3686 list_entry)
Peter Zijlstrabbbee902009-05-29 14:25:58 +02003687 __perf_counter_exit_task(child_counter, child_ctx);
Peter Zijlstra8bc20952009-05-15 20:45:59 +02003688
3689 /*
3690 * If the last counter was a group counter, it will have appended all
3691 * its siblings to the list, but we obtained 'tmp' before that which
3692 * will still point to the list head terminating the iteration.
3693 */
3694 if (!list_empty(&child_ctx->counter_list))
3695 goto again;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003696
3697 mutex_unlock(&child_ctx->mutex);
3698
3699 put_ctx(child_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003700}
3701
3702/*
Peter Zijlstrabbbee902009-05-29 14:25:58 +02003703 * free an unexposed, unused context as created by inheritance by
3704 * init_task below, used by fork() in case of fail.
3705 */
3706void perf_counter_free_task(struct task_struct *task)
3707{
3708 struct perf_counter_context *ctx = task->perf_counter_ctxp;
3709 struct perf_counter *counter, *tmp;
3710
3711 if (!ctx)
3712 return;
3713
3714 mutex_lock(&ctx->mutex);
3715again:
3716 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry) {
3717 struct perf_counter *parent = counter->parent;
3718
3719 if (WARN_ON_ONCE(!parent))
3720 continue;
3721
3722 mutex_lock(&parent->child_mutex);
3723 list_del_init(&counter->child_list);
3724 mutex_unlock(&parent->child_mutex);
3725
3726 fput(parent->filp);
3727
3728 list_del_counter(counter, ctx);
3729 free_counter(counter);
3730 }
3731
3732 if (!list_empty(&ctx->counter_list))
3733 goto again;
3734
3735 mutex_unlock(&ctx->mutex);
3736
3737 put_ctx(ctx);
3738}
3739
3740/*
Ingo Molnar9b51f662008-12-12 13:49:45 +01003741 * Initialize the perf_counter context in task_struct
3742 */
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02003743int perf_counter_init_task(struct task_struct *child)
Ingo Molnar9b51f662008-12-12 13:49:45 +01003744{
3745 struct perf_counter_context *child_ctx, *parent_ctx;
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003746 struct perf_counter_context *cloned_ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003747 struct perf_counter *counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003748 struct task_struct *parent = current;
Paul Mackerras564c2b22009-05-22 14:27:22 +10003749 int inherited_all = 1;
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02003750 int ret = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003751
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003752 child->perf_counter_ctxp = NULL;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003753
Peter Zijlstra082ff5a2009-05-23 18:29:00 +02003754 mutex_init(&child->perf_counter_mutex);
3755 INIT_LIST_HEAD(&child->perf_counter_list);
3756
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003757 if (likely(!parent->perf_counter_ctxp))
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02003758 return 0;
3759
Ingo Molnar9b51f662008-12-12 13:49:45 +01003760 /*
3761 * This is executed from the parent task context, so inherit
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003762 * counters that have been marked for cloning.
3763 * First allocate and initialize a context for the child.
Ingo Molnar9b51f662008-12-12 13:49:45 +01003764 */
3765
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003766 child_ctx = kmalloc(sizeof(struct perf_counter_context), GFP_KERNEL);
3767 if (!child_ctx)
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02003768 return -ENOMEM;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003769
3770 __perf_counter_init_context(child_ctx, child);
3771 child->perf_counter_ctxp = child_ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10003772 get_task_struct(child);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003773
Ingo Molnar9b51f662008-12-12 13:49:45 +01003774 /*
Paul Mackerras25346b932009-06-01 17:48:12 +10003775 * If the parent's context is a clone, pin it so it won't get
3776 * swapped under us.
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003777 */
Paul Mackerras25346b932009-06-01 17:48:12 +10003778 parent_ctx = perf_pin_task_context(parent);
3779
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003780 /*
3781 * No need to check if parent_ctx != NULL here; since we saw
3782 * it non-NULL earlier, the only reason for it to become NULL
3783 * is if we exit, and since we're currently in the middle of
3784 * a fork we can't be exiting at the same time.
3785 */
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003786
3787 /*
Ingo Molnar9b51f662008-12-12 13:49:45 +01003788 * Lock the parent list. No need to lock the child - not PID
3789 * hashed yet and not running, so nobody can access it.
3790 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11003791 mutex_lock(&parent_ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003792
3793 /*
3794 * We dont have to disable NMIs - we are only looking at
3795 * the list, not manipulating it:
3796 */
Peter Zijlstrad7b629a2009-05-20 12:21:19 +02003797 list_for_each_entry_rcu(counter, &parent_ctx->event_list, event_entry) {
3798 if (counter != counter->group_leader)
3799 continue;
3800
Paul Mackerras564c2b22009-05-22 14:27:22 +10003801 if (!counter->hw_event.inherit) {
3802 inherited_all = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003803 continue;
Paul Mackerras564c2b22009-05-22 14:27:22 +10003804 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01003805
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02003806 ret = inherit_group(counter, parent, parent_ctx,
3807 child, child_ctx);
3808 if (ret) {
Paul Mackerras564c2b22009-05-22 14:27:22 +10003809 inherited_all = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003810 break;
Paul Mackerras564c2b22009-05-22 14:27:22 +10003811 }
3812 }
3813
3814 if (inherited_all) {
3815 /*
3816 * Mark the child context as a clone of the parent
3817 * context, or of whatever the parent is a clone of.
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003818 * Note that if the parent is a clone, it could get
3819 * uncloned at any point, but that doesn't matter
3820 * because the list of counters and the generation
3821 * count can't have changed since we took the mutex.
Paul Mackerras564c2b22009-05-22 14:27:22 +10003822 */
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003823 cloned_ctx = rcu_dereference(parent_ctx->parent_ctx);
3824 if (cloned_ctx) {
3825 child_ctx->parent_ctx = cloned_ctx;
Paul Mackerras25346b932009-06-01 17:48:12 +10003826 child_ctx->parent_gen = parent_ctx->parent_gen;
Paul Mackerras564c2b22009-05-22 14:27:22 +10003827 } else {
3828 child_ctx->parent_ctx = parent_ctx;
3829 child_ctx->parent_gen = parent_ctx->generation;
3830 }
3831 get_ctx(child_ctx->parent_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003832 }
3833
Paul Mackerrasd859e292009-01-17 18:10:22 +11003834 mutex_unlock(&parent_ctx->mutex);
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02003835
Paul Mackerras25346b932009-06-01 17:48:12 +10003836 perf_unpin_context(parent_ctx);
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003837
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02003838 return ret;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003839}
3840
Ingo Molnar04289bb2008-12-11 08:38:42 +01003841static void __cpuinit perf_counter_init_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003842{
Ingo Molnar04289bb2008-12-11 08:38:42 +01003843 struct perf_cpu_context *cpuctx;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003844
Ingo Molnar04289bb2008-12-11 08:38:42 +01003845 cpuctx = &per_cpu(perf_cpu_context, cpu);
3846 __perf_counter_init_context(&cpuctx->ctx, NULL);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003847
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003848 spin_lock(&perf_resource_lock);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003849 cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu;
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003850 spin_unlock(&perf_resource_lock);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003851
Paul Mackerras01d02872009-01-14 13:44:19 +11003852 hw_perf_counter_setup(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003853}
3854
3855#ifdef CONFIG_HOTPLUG_CPU
Ingo Molnar04289bb2008-12-11 08:38:42 +01003856static void __perf_counter_exit_cpu(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003857{
3858 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
3859 struct perf_counter_context *ctx = &cpuctx->ctx;
3860 struct perf_counter *counter, *tmp;
3861
Ingo Molnar04289bb2008-12-11 08:38:42 +01003862 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry)
3863 __perf_counter_remove_from_context(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003864}
Ingo Molnar04289bb2008-12-11 08:38:42 +01003865static void perf_counter_exit_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003866{
Paul Mackerrasd859e292009-01-17 18:10:22 +11003867 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
3868 struct perf_counter_context *ctx = &cpuctx->ctx;
3869
3870 mutex_lock(&ctx->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003871 smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003872 mutex_unlock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003873}
3874#else
Ingo Molnar04289bb2008-12-11 08:38:42 +01003875static inline void perf_counter_exit_cpu(int cpu) { }
Thomas Gleixner0793a612008-12-04 20:12:29 +01003876#endif
3877
3878static int __cpuinit
3879perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
3880{
3881 unsigned int cpu = (long)hcpu;
3882
3883 switch (action) {
3884
3885 case CPU_UP_PREPARE:
3886 case CPU_UP_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01003887 perf_counter_init_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003888 break;
3889
3890 case CPU_DOWN_PREPARE:
3891 case CPU_DOWN_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01003892 perf_counter_exit_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003893 break;
3894
3895 default:
3896 break;
3897 }
3898
3899 return NOTIFY_OK;
3900}
3901
3902static struct notifier_block __cpuinitdata perf_cpu_nb = {
3903 .notifier_call = perf_cpu_notify,
3904};
3905
Ingo Molnar0d905bc2009-05-04 19:13:30 +02003906void __init perf_counter_init(void)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003907{
3908 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
3909 (void *)(long)smp_processor_id());
3910 register_cpu_notifier(&perf_cpu_nb);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003911}
Thomas Gleixner0793a612008-12-04 20:12:29 +01003912
3913static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
3914{
3915 return sprintf(buf, "%d\n", perf_reserved_percpu);
3916}
3917
3918static ssize_t
3919perf_set_reserve_percpu(struct sysdev_class *class,
3920 const char *buf,
3921 size_t count)
3922{
3923 struct perf_cpu_context *cpuctx;
3924 unsigned long val;
3925 int err, cpu, mpt;
3926
3927 err = strict_strtoul(buf, 10, &val);
3928 if (err)
3929 return err;
3930 if (val > perf_max_counters)
3931 return -EINVAL;
3932
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003933 spin_lock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003934 perf_reserved_percpu = val;
3935 for_each_online_cpu(cpu) {
3936 cpuctx = &per_cpu(perf_cpu_context, cpu);
3937 spin_lock_irq(&cpuctx->ctx.lock);
3938 mpt = min(perf_max_counters - cpuctx->ctx.nr_counters,
3939 perf_max_counters - perf_reserved_percpu);
3940 cpuctx->max_pertask = mpt;
3941 spin_unlock_irq(&cpuctx->ctx.lock);
3942 }
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003943 spin_unlock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003944
3945 return count;
3946}
3947
3948static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
3949{
3950 return sprintf(buf, "%d\n", perf_overcommit);
3951}
3952
3953static ssize_t
3954perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
3955{
3956 unsigned long val;
3957 int err;
3958
3959 err = strict_strtoul(buf, 10, &val);
3960 if (err)
3961 return err;
3962 if (val > 1)
3963 return -EINVAL;
3964
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003965 spin_lock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003966 perf_overcommit = val;
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003967 spin_unlock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003968
3969 return count;
3970}
3971
3972static SYSDEV_CLASS_ATTR(
3973 reserve_percpu,
3974 0644,
3975 perf_show_reserve_percpu,
3976 perf_set_reserve_percpu
3977 );
3978
3979static SYSDEV_CLASS_ATTR(
3980 overcommit,
3981 0644,
3982 perf_show_overcommit,
3983 perf_set_overcommit
3984 );
3985
3986static struct attribute *perfclass_attrs[] = {
3987 &attr_reserve_percpu.attr,
3988 &attr_overcommit.attr,
3989 NULL
3990};
3991
3992static struct attribute_group perfclass_attr_group = {
3993 .attrs = perfclass_attrs,
3994 .name = "perf_counters",
3995};
3996
3997static int __init perf_counter_sysfs_init(void)
3998{
3999 return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
4000 &perfclass_attr_group);
4001}
4002device_initcall(perf_counter_sysfs_init);