blob: 0e97f8961333637c4fbb7e52d2a2ca93a26fb6c8 [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 Zijlstra1ccd1542009-04-09 10:53:45 +020049
Thomas Gleixner0793a612008-12-04 20:12:29 +010050/*
Ingo Molnar1dce8d92009-05-04 19:23:18 +020051 * Lock for (sysadmin-configurable) counter reservations:
Thomas Gleixner0793a612008-12-04 20:12:29 +010052 */
Ingo Molnar1dce8d92009-05-04 19:23:18 +020053static DEFINE_SPINLOCK(perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +010054
55/*
56 * Architecture provided APIs - weak aliases:
57 */
Robert Richter4aeb0b42009-04-29 12:47:03 +020058extern __weak const struct pmu *hw_perf_counter_init(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +010059{
Paul Mackerrasff6f0542009-01-09 16:19:25 +110060 return NULL;
Thomas Gleixner0793a612008-12-04 20:12:29 +010061}
62
Peter Zijlstra9e35ad32009-05-13 16:21:38 +020063void __weak hw_perf_disable(void) { barrier(); }
64void __weak hw_perf_enable(void) { barrier(); }
65
Paul Mackerras01d02872009-01-14 13:44:19 +110066void __weak hw_perf_counter_setup(int cpu) { barrier(); }
Paul Mackerras3cbed422009-01-09 16:43:42 +110067int __weak hw_perf_group_sched_in(struct perf_counter *group_leader,
68 struct perf_cpu_context *cpuctx,
69 struct perf_counter_context *ctx, int cpu)
70{
71 return 0;
72}
Thomas Gleixner0793a612008-12-04 20:12:29 +010073
Paul Mackerras4eb96fc2009-01-09 17:24:34 +110074void __weak perf_counter_print_debug(void) { }
75
Peter Zijlstra9e35ad32009-05-13 16:21:38 +020076static DEFINE_PER_CPU(int, disable_count);
77
78void __perf_disable(void)
79{
80 __get_cpu_var(disable_count)++;
81}
82
83bool __perf_enable(void)
84{
85 return !--__get_cpu_var(disable_count);
86}
87
88void perf_disable(void)
89{
90 __perf_disable();
91 hw_perf_disable();
92}
Peter Zijlstra9e35ad32009-05-13 16:21:38 +020093
94void perf_enable(void)
95{
96 if (__perf_enable())
97 hw_perf_enable();
98}
Peter Zijlstra9e35ad32009-05-13 16:21:38 +020099
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000100static void get_ctx(struct perf_counter_context *ctx)
101{
102 atomic_inc(&ctx->refcount);
103}
104
105static void put_ctx(struct perf_counter_context *ctx)
106{
Paul Mackerras564c2b22009-05-22 14:27:22 +1000107 if (atomic_dec_and_test(&ctx->refcount)) {
108 if (ctx->parent_ctx)
109 put_ctx(ctx->parent_ctx);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000110 kfree(ctx);
Paul Mackerras564c2b22009-05-22 14:27:22 +1000111 }
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000112}
113
Peter Zijlstrafccc7142009-05-23 18:28:56 +0200114/*
115 * Add a counter from the lists for its context.
116 * Must be called with ctx->mutex and ctx->lock held.
117 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100118static void
119list_add_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
120{
121 struct perf_counter *group_leader = counter->group_leader;
122
123 /*
124 * Depending on whether it is a standalone or sibling counter,
125 * add it straight to the context's counter list, or to the group
126 * leader's sibling list:
127 */
Peter Zijlstra3df5eda2009-05-08 18:52:22 +0200128 if (group_leader == counter)
Ingo Molnar04289bb2008-12-11 08:38:42 +0100129 list_add_tail(&counter->list_entry, &ctx->counter_list);
Peter Zijlstra5c148192009-03-25 12:30:23 +0100130 else {
Ingo Molnar04289bb2008-12-11 08:38:42 +0100131 list_add_tail(&counter->list_entry, &group_leader->sibling_list);
Peter Zijlstra5c148192009-03-25 12:30:23 +0100132 group_leader->nr_siblings++;
133 }
Peter Zijlstra592903c2009-03-13 12:21:36 +0100134
135 list_add_rcu(&counter->event_entry, &ctx->event_list);
Peter Zijlstra8bc20952009-05-15 20:45:59 +0200136 ctx->nr_counters++;
Paul Mackerras564c2b22009-05-22 14:27:22 +1000137 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
138 ctx->nr_enabled++;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100139}
140
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000141/*
142 * Remove a counter from the lists for its context.
Peter Zijlstrafccc7142009-05-23 18:28:56 +0200143 * Must be called with ctx->mutex and ctx->lock held.
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000144 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100145static void
146list_del_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
147{
148 struct perf_counter *sibling, *tmp;
149
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000150 if (list_empty(&counter->list_entry))
151 return;
Peter Zijlstra8bc20952009-05-15 20:45:59 +0200152 ctx->nr_counters--;
Paul Mackerras564c2b22009-05-22 14:27:22 +1000153 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
154 ctx->nr_enabled--;
Peter Zijlstra8bc20952009-05-15 20:45:59 +0200155
Ingo Molnar04289bb2008-12-11 08:38:42 +0100156 list_del_init(&counter->list_entry);
Peter Zijlstra592903c2009-03-13 12:21:36 +0100157 list_del_rcu(&counter->event_entry);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100158
Peter Zijlstra5c148192009-03-25 12:30:23 +0100159 if (counter->group_leader != counter)
160 counter->group_leader->nr_siblings--;
161
Ingo Molnar04289bb2008-12-11 08:38:42 +0100162 /*
163 * If this was a group counter with sibling counters then
164 * upgrade the siblings to singleton counters by adding them
165 * to the context list directly:
166 */
167 list_for_each_entry_safe(sibling, tmp,
168 &counter->sibling_list, list_entry) {
169
Peter Zijlstra75564232009-03-13 12:21:29 +0100170 list_move_tail(&sibling->list_entry, &ctx->counter_list);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100171 sibling->group_leader = sibling;
172 }
173}
174
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100175static void
176counter_sched_out(struct perf_counter *counter,
177 struct perf_cpu_context *cpuctx,
178 struct perf_counter_context *ctx)
179{
180 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
181 return;
182
183 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200184 counter->tstamp_stopped = ctx->time;
Robert Richter4aeb0b42009-04-29 12:47:03 +0200185 counter->pmu->disable(counter);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100186 counter->oncpu = -1;
187
188 if (!is_software_counter(counter))
189 cpuctx->active_oncpu--;
190 ctx->nr_active--;
191 if (counter->hw_event.exclusive || !cpuctx->active_oncpu)
192 cpuctx->exclusive = 0;
193}
194
Paul Mackerrasd859e292009-01-17 18:10:22 +1100195static void
196group_sched_out(struct perf_counter *group_counter,
197 struct perf_cpu_context *cpuctx,
198 struct perf_counter_context *ctx)
199{
200 struct perf_counter *counter;
201
202 if (group_counter->state != PERF_COUNTER_STATE_ACTIVE)
203 return;
204
205 counter_sched_out(group_counter, cpuctx, ctx);
206
207 /*
208 * Schedule out siblings (if any):
209 */
210 list_for_each_entry(counter, &group_counter->sibling_list, list_entry)
211 counter_sched_out(counter, cpuctx, ctx);
212
213 if (group_counter->hw_event.exclusive)
214 cpuctx->exclusive = 0;
215}
216
Thomas Gleixner0793a612008-12-04 20:12:29 +0100217/*
Paul Mackerras564c2b22009-05-22 14:27:22 +1000218 * Mark this context as not being a clone of another.
219 * Called when counters are added to or removed from this context.
220 * We also increment our generation number so that anything that
221 * was cloned from this context before this will not match anything
222 * cloned from this context after this.
223 */
224static void unclone_ctx(struct perf_counter_context *ctx)
225{
226 ++ctx->generation;
227 if (!ctx->parent_ctx)
228 return;
229 put_ctx(ctx->parent_ctx);
230 ctx->parent_ctx = NULL;
231}
232
233/*
Thomas Gleixner0793a612008-12-04 20:12:29 +0100234 * Cross CPU call to remove a performance counter
235 *
236 * We disable the counter on the hardware level first. After that we
237 * remove it from the context list.
238 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100239static void __perf_counter_remove_from_context(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100240{
241 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
242 struct perf_counter *counter = info;
243 struct perf_counter_context *ctx = counter->ctx;
Ingo Molnar9b51f662008-12-12 13:49:45 +0100244 unsigned long flags;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100245
246 /*
247 * If this is a task context, we need to check whether it is
248 * the current task context of this cpu. If not it has been
249 * scheduled out before the smp call arrived.
250 */
251 if (ctx->task && cpuctx->task_ctx != ctx)
252 return;
253
Peter Zijlstra849691a2009-04-06 11:45:12 +0200254 spin_lock_irqsave(&ctx->lock, flags);
Ingo Molnar34adc802009-05-20 20:13:28 +0200255 /*
256 * Protect the list operation against NMI by disabling the
257 * counters on a global level.
258 */
259 perf_disable();
Thomas Gleixner0793a612008-12-04 20:12:29 +0100260
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100261 counter_sched_out(counter, cpuctx, ctx);
262
Ingo Molnar04289bb2008-12-11 08:38:42 +0100263 list_del_counter(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100264
265 if (!ctx->task) {
266 /*
267 * Allow more per task counters with respect to the
268 * reservation:
269 */
270 cpuctx->max_pertask =
271 min(perf_max_counters - ctx->nr_counters,
272 perf_max_counters - perf_reserved_percpu);
273 }
274
Ingo Molnar34adc802009-05-20 20:13:28 +0200275 perf_enable();
Peter Zijlstra849691a2009-04-06 11:45:12 +0200276 spin_unlock_irqrestore(&ctx->lock, flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100277}
278
279
280/*
281 * Remove the counter from a task's (or a CPU's) list of counters.
282 *
Peter Zijlstrafccc7142009-05-23 18:28:56 +0200283 * Must be called with ctx->mutex held.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100284 *
285 * CPU counters are removed with a smp call. For task counters we only
286 * call when the task is on a CPU.
287 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100288static void perf_counter_remove_from_context(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100289{
290 struct perf_counter_context *ctx = counter->ctx;
291 struct task_struct *task = ctx->task;
292
Paul Mackerras564c2b22009-05-22 14:27:22 +1000293 unclone_ctx(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100294 if (!task) {
295 /*
296 * Per cpu counters are removed via an smp call and
297 * the removal is always sucessful.
298 */
299 smp_call_function_single(counter->cpu,
Ingo Molnar04289bb2008-12-11 08:38:42 +0100300 __perf_counter_remove_from_context,
Thomas Gleixner0793a612008-12-04 20:12:29 +0100301 counter, 1);
302 return;
303 }
304
305retry:
Ingo Molnar04289bb2008-12-11 08:38:42 +0100306 task_oncpu_function_call(task, __perf_counter_remove_from_context,
Thomas Gleixner0793a612008-12-04 20:12:29 +0100307 counter);
308
309 spin_lock_irq(&ctx->lock);
310 /*
311 * If the context is active we need to retry the smp call.
312 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100313 if (ctx->nr_active && !list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100314 spin_unlock_irq(&ctx->lock);
315 goto retry;
316 }
317
318 /*
319 * The lock prevents that this context is scheduled in so we
Ingo Molnar04289bb2008-12-11 08:38:42 +0100320 * can remove the counter safely, if the call above did not
Thomas Gleixner0793a612008-12-04 20:12:29 +0100321 * succeed.
322 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100323 if (!list_empty(&counter->list_entry)) {
Ingo Molnar04289bb2008-12-11 08:38:42 +0100324 list_del_counter(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100325 }
326 spin_unlock_irq(&ctx->lock);
327}
328
Peter Zijlstra4af49982009-04-06 11:45:10 +0200329static inline u64 perf_clock(void)
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100330{
Peter Zijlstra4af49982009-04-06 11:45:10 +0200331 return cpu_clock(smp_processor_id());
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100332}
333
334/*
335 * Update the record of the current time in a context.
336 */
Peter Zijlstra4af49982009-04-06 11:45:10 +0200337static void update_context_time(struct perf_counter_context *ctx)
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100338{
Peter Zijlstra4af49982009-04-06 11:45:10 +0200339 u64 now = perf_clock();
340
341 ctx->time += now - ctx->timestamp;
342 ctx->timestamp = now;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100343}
344
345/*
346 * Update the total_time_enabled and total_time_running fields for a counter.
347 */
348static void update_counter_times(struct perf_counter *counter)
349{
350 struct perf_counter_context *ctx = counter->ctx;
351 u64 run_end;
352
Peter Zijlstra4af49982009-04-06 11:45:10 +0200353 if (counter->state < PERF_COUNTER_STATE_INACTIVE)
354 return;
355
356 counter->total_time_enabled = ctx->time - counter->tstamp_enabled;
357
358 if (counter->state == PERF_COUNTER_STATE_INACTIVE)
359 run_end = counter->tstamp_stopped;
360 else
361 run_end = ctx->time;
362
363 counter->total_time_running = run_end - counter->tstamp_running;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100364}
365
366/*
367 * Update total_time_enabled and total_time_running for all counters in a group.
368 */
369static void update_group_times(struct perf_counter *leader)
370{
371 struct perf_counter *counter;
372
373 update_counter_times(leader);
374 list_for_each_entry(counter, &leader->sibling_list, list_entry)
375 update_counter_times(counter);
376}
377
378/*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100379 * Cross CPU call to disable a performance counter
380 */
381static void __perf_counter_disable(void *info)
382{
383 struct perf_counter *counter = info;
384 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
385 struct perf_counter_context *ctx = counter->ctx;
386 unsigned long flags;
387
388 /*
389 * If this is a per-task counter, need to check whether this
390 * counter's task is the current task on this cpu.
391 */
392 if (ctx->task && cpuctx->task_ctx != ctx)
393 return;
394
Peter Zijlstra849691a2009-04-06 11:45:12 +0200395 spin_lock_irqsave(&ctx->lock, flags);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100396
397 /*
398 * If the counter is on, turn it off.
399 * If it is in error state, leave it in error state.
400 */
401 if (counter->state >= PERF_COUNTER_STATE_INACTIVE) {
Peter Zijlstra4af49982009-04-06 11:45:10 +0200402 update_context_time(ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100403 update_counter_times(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100404 if (counter == counter->group_leader)
405 group_sched_out(counter, cpuctx, ctx);
406 else
407 counter_sched_out(counter, cpuctx, ctx);
408 counter->state = PERF_COUNTER_STATE_OFF;
Paul Mackerras564c2b22009-05-22 14:27:22 +1000409 ctx->nr_enabled--;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100410 }
411
Peter Zijlstra849691a2009-04-06 11:45:12 +0200412 spin_unlock_irqrestore(&ctx->lock, flags);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100413}
414
415/*
416 * Disable a counter.
417 */
418static void perf_counter_disable(struct perf_counter *counter)
419{
420 struct perf_counter_context *ctx = counter->ctx;
421 struct task_struct *task = ctx->task;
422
423 if (!task) {
424 /*
425 * Disable the counter on the cpu that it's on
426 */
427 smp_call_function_single(counter->cpu, __perf_counter_disable,
428 counter, 1);
429 return;
430 }
431
432 retry:
433 task_oncpu_function_call(task, __perf_counter_disable, counter);
434
435 spin_lock_irq(&ctx->lock);
436 /*
437 * If the counter is still active, we need to retry the cross-call.
438 */
439 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
440 spin_unlock_irq(&ctx->lock);
441 goto retry;
442 }
443
444 /*
445 * Since we have the lock this context can't be scheduled
446 * in, so we can change the state safely.
447 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100448 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
449 update_counter_times(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100450 counter->state = PERF_COUNTER_STATE_OFF;
Paul Mackerras564c2b22009-05-22 14:27:22 +1000451 ctx->nr_enabled--;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100452 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100453
454 spin_unlock_irq(&ctx->lock);
455}
456
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100457static int
458counter_sched_in(struct perf_counter *counter,
459 struct perf_cpu_context *cpuctx,
460 struct perf_counter_context *ctx,
461 int cpu)
462{
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100463 if (counter->state <= PERF_COUNTER_STATE_OFF)
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100464 return 0;
465
466 counter->state = PERF_COUNTER_STATE_ACTIVE;
467 counter->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */
468 /*
469 * The new state must be visible before we turn it on in the hardware:
470 */
471 smp_wmb();
472
Robert Richter4aeb0b42009-04-29 12:47:03 +0200473 if (counter->pmu->enable(counter)) {
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100474 counter->state = PERF_COUNTER_STATE_INACTIVE;
475 counter->oncpu = -1;
476 return -EAGAIN;
477 }
478
Peter Zijlstra4af49982009-04-06 11:45:10 +0200479 counter->tstamp_running += ctx->time - counter->tstamp_stopped;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100480
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100481 if (!is_software_counter(counter))
482 cpuctx->active_oncpu++;
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100483 ctx->nr_active++;
484
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100485 if (counter->hw_event.exclusive)
486 cpuctx->exclusive = 1;
487
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100488 return 0;
489}
490
Paul Mackerras6751b712009-05-11 12:08:02 +1000491static int
492group_sched_in(struct perf_counter *group_counter,
493 struct perf_cpu_context *cpuctx,
494 struct perf_counter_context *ctx,
495 int cpu)
496{
497 struct perf_counter *counter, *partial_group;
498 int ret;
499
500 if (group_counter->state == PERF_COUNTER_STATE_OFF)
501 return 0;
502
503 ret = hw_perf_group_sched_in(group_counter, cpuctx, ctx, cpu);
504 if (ret)
505 return ret < 0 ? ret : 0;
506
507 group_counter->prev_state = group_counter->state;
508 if (counter_sched_in(group_counter, cpuctx, ctx, cpu))
509 return -EAGAIN;
510
511 /*
512 * Schedule in siblings as one group (if any):
513 */
514 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
515 counter->prev_state = counter->state;
516 if (counter_sched_in(counter, cpuctx, ctx, cpu)) {
517 partial_group = counter;
518 goto group_error;
519 }
520 }
521
522 return 0;
523
524group_error:
525 /*
526 * Groups can be scheduled in as one unit only, so undo any
527 * partial group before returning:
528 */
529 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
530 if (counter == partial_group)
531 break;
532 counter_sched_out(counter, cpuctx, ctx);
533 }
534 counter_sched_out(group_counter, cpuctx, ctx);
535
536 return -EAGAIN;
537}
538
Thomas Gleixner0793a612008-12-04 20:12:29 +0100539/*
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100540 * Return 1 for a group consisting entirely of software counters,
541 * 0 if the group contains any hardware counters.
542 */
543static int is_software_only_group(struct perf_counter *leader)
544{
545 struct perf_counter *counter;
546
547 if (!is_software_counter(leader))
548 return 0;
Peter Zijlstra5c148192009-03-25 12:30:23 +0100549
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100550 list_for_each_entry(counter, &leader->sibling_list, list_entry)
551 if (!is_software_counter(counter))
552 return 0;
Peter Zijlstra5c148192009-03-25 12:30:23 +0100553
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100554 return 1;
555}
556
557/*
558 * Work out whether we can put this counter group on the CPU now.
559 */
560static int group_can_go_on(struct perf_counter *counter,
561 struct perf_cpu_context *cpuctx,
562 int can_add_hw)
563{
564 /*
565 * Groups consisting entirely of software counters can always go on.
566 */
567 if (is_software_only_group(counter))
568 return 1;
569 /*
570 * If an exclusive group is already on, no other hardware
571 * counters can go on.
572 */
573 if (cpuctx->exclusive)
574 return 0;
575 /*
576 * If this group is exclusive and there are already
577 * counters on the CPU, it can't go on.
578 */
579 if (counter->hw_event.exclusive && cpuctx->active_oncpu)
580 return 0;
581 /*
582 * Otherwise, try to add it if all previous groups were able
583 * to go on.
584 */
585 return can_add_hw;
586}
587
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100588static void add_counter_to_ctx(struct perf_counter *counter,
589 struct perf_counter_context *ctx)
590{
591 list_add_counter(counter, ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100592 counter->prev_state = PERF_COUNTER_STATE_OFF;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200593 counter->tstamp_enabled = ctx->time;
594 counter->tstamp_running = ctx->time;
595 counter->tstamp_stopped = ctx->time;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100596}
597
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100598/*
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100599 * Cross CPU call to install and enable a performance counter
Peter Zijlstra682076a2009-05-23 18:28:57 +0200600 *
601 * Must be called with ctx->mutex held
Thomas Gleixner0793a612008-12-04 20:12:29 +0100602 */
603static void __perf_install_in_context(void *info)
604{
605 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
606 struct perf_counter *counter = info;
607 struct perf_counter_context *ctx = counter->ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100608 struct perf_counter *leader = counter->group_leader;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100609 int cpu = smp_processor_id();
Ingo Molnar9b51f662008-12-12 13:49:45 +0100610 unsigned long flags;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100611 int err;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100612
613 /*
614 * If this is a task context, we need to check whether it is
615 * the current task context of this cpu. If not it has been
616 * scheduled out before the smp call arrived.
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000617 * Or possibly this is the right context but it isn't
618 * on this cpu because it had no counters.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100619 */
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000620 if (ctx->task && cpuctx->task_ctx != ctx) {
621 if (cpuctx->task_ctx || ctx->task != current)
622 return;
623 cpuctx->task_ctx = ctx;
624 }
Thomas Gleixner0793a612008-12-04 20:12:29 +0100625
Peter Zijlstra849691a2009-04-06 11:45:12 +0200626 spin_lock_irqsave(&ctx->lock, flags);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000627 ctx->is_active = 1;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200628 update_context_time(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100629
630 /*
631 * Protect the list operation against NMI by disabling the
632 * counters on a global level. NOP for non NMI based counters.
633 */
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200634 perf_disable();
Thomas Gleixner0793a612008-12-04 20:12:29 +0100635
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100636 add_counter_to_ctx(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100637
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100638 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100639 * Don't put the counter on if it is disabled or if
640 * it is in a group and the group isn't on.
641 */
642 if (counter->state != PERF_COUNTER_STATE_INACTIVE ||
643 (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE))
644 goto unlock;
645
646 /*
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100647 * An exclusive counter can't go on if there are already active
648 * hardware counters, and no hardware counter can go on if there
649 * is already an exclusive counter on.
650 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100651 if (!group_can_go_on(counter, cpuctx, 1))
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100652 err = -EEXIST;
653 else
654 err = counter_sched_in(counter, cpuctx, ctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100655
Paul Mackerrasd859e292009-01-17 18:10:22 +1100656 if (err) {
657 /*
658 * This counter couldn't go on. If it is in a group
659 * then we have to pull the whole group off.
660 * If the counter group is pinned then put it in error state.
661 */
662 if (leader != counter)
663 group_sched_out(leader, cpuctx, ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100664 if (leader->hw_event.pinned) {
665 update_group_times(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100666 leader->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100667 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100668 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100669
670 if (!err && !ctx->task && cpuctx->max_pertask)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100671 cpuctx->max_pertask--;
672
Paul Mackerrasd859e292009-01-17 18:10:22 +1100673 unlock:
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200674 perf_enable();
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100675
Peter Zijlstra849691a2009-04-06 11:45:12 +0200676 spin_unlock_irqrestore(&ctx->lock, flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100677}
678
679/*
680 * Attach a performance counter to a context
681 *
682 * First we add the counter to the list with the hardware enable bit
683 * in counter->hw_config cleared.
684 *
685 * If the counter is attached to a task which is on a CPU we use a smp
686 * call to enable it in the task context. The task might have been
687 * scheduled away, but we check this in the smp call again.
Paul Mackerrasd859e292009-01-17 18:10:22 +1100688 *
689 * Must be called with ctx->mutex held.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100690 */
691static void
692perf_install_in_context(struct perf_counter_context *ctx,
693 struct perf_counter *counter,
694 int cpu)
695{
696 struct task_struct *task = ctx->task;
697
Thomas Gleixner0793a612008-12-04 20:12:29 +0100698 if (!task) {
699 /*
700 * Per cpu counters are installed via an smp call and
701 * the install is always sucessful.
702 */
703 smp_call_function_single(cpu, __perf_install_in_context,
704 counter, 1);
705 return;
706 }
707
Thomas Gleixner0793a612008-12-04 20:12:29 +0100708retry:
709 task_oncpu_function_call(task, __perf_install_in_context,
710 counter);
711
712 spin_lock_irq(&ctx->lock);
713 /*
Thomas Gleixner0793a612008-12-04 20:12:29 +0100714 * we need to retry the smp call.
715 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100716 if (ctx->is_active && list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100717 spin_unlock_irq(&ctx->lock);
718 goto retry;
719 }
720
721 /*
722 * The lock prevents that this context is scheduled in so we
723 * can add the counter safely, if it the call above did not
724 * succeed.
725 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100726 if (list_empty(&counter->list_entry))
727 add_counter_to_ctx(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100728 spin_unlock_irq(&ctx->lock);
729}
730
Paul Mackerrasd859e292009-01-17 18:10:22 +1100731/*
732 * Cross CPU call to enable a performance counter
733 */
734static void __perf_counter_enable(void *info)
Ingo Molnar04289bb2008-12-11 08:38:42 +0100735{
Paul Mackerrasd859e292009-01-17 18:10:22 +1100736 struct perf_counter *counter = info;
737 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
738 struct perf_counter_context *ctx = counter->ctx;
739 struct perf_counter *leader = counter->group_leader;
740 unsigned long flags;
741 int err;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100742
743 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100744 * If this is a per-task counter, need to check whether this
745 * counter's task is the current task on this cpu.
Ingo Molnar04289bb2008-12-11 08:38:42 +0100746 */
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000747 if (ctx->task && cpuctx->task_ctx != ctx) {
748 if (cpuctx->task_ctx || ctx->task != current)
749 return;
750 cpuctx->task_ctx = ctx;
751 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100752
Peter Zijlstra849691a2009-04-06 11:45:12 +0200753 spin_lock_irqsave(&ctx->lock, flags);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000754 ctx->is_active = 1;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200755 update_context_time(ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100756
Paul Mackerrasc07c99b2009-02-13 22:10:34 +1100757 counter->prev_state = counter->state;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100758 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
759 goto unlock;
760 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200761 counter->tstamp_enabled = ctx->time - counter->total_time_enabled;
Paul Mackerras564c2b22009-05-22 14:27:22 +1000762 ctx->nr_enabled++;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100763
764 /*
765 * If the counter is in a group and isn't the group leader,
766 * then don't put it on unless the group is on.
767 */
768 if (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE)
769 goto unlock;
770
Paul Mackerrase758a332009-05-12 21:59:01 +1000771 if (!group_can_go_on(counter, cpuctx, 1)) {
Paul Mackerrasd859e292009-01-17 18:10:22 +1100772 err = -EEXIST;
Paul Mackerrase758a332009-05-12 21:59:01 +1000773 } else {
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200774 perf_disable();
Paul Mackerrase758a332009-05-12 21:59:01 +1000775 if (counter == leader)
776 err = group_sched_in(counter, cpuctx, ctx,
777 smp_processor_id());
778 else
779 err = counter_sched_in(counter, cpuctx, ctx,
780 smp_processor_id());
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200781 perf_enable();
Paul Mackerrase758a332009-05-12 21:59:01 +1000782 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100783
784 if (err) {
785 /*
786 * If this counter can't go on and it's part of a
787 * group, then the whole group has to come off.
788 */
789 if (leader != counter)
790 group_sched_out(leader, cpuctx, ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100791 if (leader->hw_event.pinned) {
792 update_group_times(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100793 leader->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100794 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100795 }
796
797 unlock:
Peter Zijlstra849691a2009-04-06 11:45:12 +0200798 spin_unlock_irqrestore(&ctx->lock, flags);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100799}
800
801/*
802 * Enable a counter.
803 */
804static void perf_counter_enable(struct perf_counter *counter)
805{
806 struct perf_counter_context *ctx = counter->ctx;
807 struct task_struct *task = ctx->task;
808
809 if (!task) {
810 /*
811 * Enable the counter on the cpu that it's on
812 */
813 smp_call_function_single(counter->cpu, __perf_counter_enable,
814 counter, 1);
815 return;
816 }
817
818 spin_lock_irq(&ctx->lock);
819 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
820 goto out;
821
822 /*
823 * If the counter is in error state, clear that first.
824 * That way, if we see the counter in error state below, we
825 * know that it has gone back into error state, as distinct
826 * from the task having been scheduled away before the
827 * cross-call arrived.
828 */
829 if (counter->state == PERF_COUNTER_STATE_ERROR)
830 counter->state = PERF_COUNTER_STATE_OFF;
831
832 retry:
833 spin_unlock_irq(&ctx->lock);
834 task_oncpu_function_call(task, __perf_counter_enable, counter);
835
836 spin_lock_irq(&ctx->lock);
837
838 /*
839 * If the context is active and the counter is still off,
840 * we need to retry the cross-call.
841 */
842 if (ctx->is_active && counter->state == PERF_COUNTER_STATE_OFF)
843 goto retry;
844
845 /*
846 * Since we have the lock this context can't be scheduled
847 * in, so we can change the state safely.
848 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100849 if (counter->state == PERF_COUNTER_STATE_OFF) {
Paul Mackerrasd859e292009-01-17 18:10:22 +1100850 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200851 counter->tstamp_enabled =
852 ctx->time - counter->total_time_enabled;
Paul Mackerras564c2b22009-05-22 14:27:22 +1000853 ctx->nr_enabled++;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100854 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100855 out:
856 spin_unlock_irq(&ctx->lock);
857}
858
Peter Zijlstra2023b352009-05-05 17:50:26 +0200859static int perf_counter_refresh(struct perf_counter *counter, int refresh)
Peter Zijlstra79f14642009-04-06 11:45:07 +0200860{
Peter Zijlstra2023b352009-05-05 17:50:26 +0200861 /*
862 * not supported on inherited counters
863 */
864 if (counter->hw_event.inherit)
865 return -EINVAL;
866
Peter Zijlstra79f14642009-04-06 11:45:07 +0200867 atomic_add(refresh, &counter->event_limit);
868 perf_counter_enable(counter);
Peter Zijlstra2023b352009-05-05 17:50:26 +0200869
870 return 0;
Peter Zijlstra79f14642009-04-06 11:45:07 +0200871}
872
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100873void __perf_counter_sched_out(struct perf_counter_context *ctx,
874 struct perf_cpu_context *cpuctx)
875{
876 struct perf_counter *counter;
877
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100878 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100879 ctx->is_active = 0;
880 if (likely(!ctx->nr_counters))
881 goto out;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200882 update_context_time(ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100883
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200884 perf_disable();
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100885 if (ctx->nr_active) {
Peter Zijlstraafedadf2009-05-20 12:21:22 +0200886 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
887 if (counter != counter->group_leader)
888 counter_sched_out(counter, cpuctx, ctx);
889 else
890 group_sched_out(counter, cpuctx, ctx);
891 }
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100892 }
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200893 perf_enable();
Paul Mackerrasd859e292009-01-17 18:10:22 +1100894 out:
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100895 spin_unlock(&ctx->lock);
896}
897
Thomas Gleixner0793a612008-12-04 20:12:29 +0100898/*
Paul Mackerras564c2b22009-05-22 14:27:22 +1000899 * Test whether two contexts are equivalent, i.e. whether they
900 * have both been cloned from the same version of the same context
901 * and they both have the same number of enabled counters.
902 * If the number of enabled counters is the same, then the set
903 * of enabled counters should be the same, because these are both
904 * inherited contexts, therefore we can't access individual counters
905 * in them directly with an fd; we can only enable/disable all
906 * counters via prctl, or enable/disable all counters in a family
907 * via ioctl, which will have the same effect on both contexts.
908 */
909static int context_equiv(struct perf_counter_context *ctx1,
910 struct perf_counter_context *ctx2)
911{
912 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
913 && ctx1->parent_gen == ctx2->parent_gen
914 && ctx1->nr_enabled == ctx2->nr_enabled;
915}
916
917/*
Thomas Gleixner0793a612008-12-04 20:12:29 +0100918 * Called from scheduler to remove the counters of the current task,
919 * with interrupts disabled.
920 *
921 * We stop each counter and update the counter value in counter->count.
922 *
Ingo Molnar76715812008-12-17 14:20:28 +0100923 * This does not protect us against NMI, but disable()
Thomas Gleixner0793a612008-12-04 20:12:29 +0100924 * sets the disabled bit in the control field of counter _before_
925 * accessing the counter control register. If a NMI hits, then it will
926 * not restart the counter.
927 */
Paul Mackerras564c2b22009-05-22 14:27:22 +1000928void perf_counter_task_sched_out(struct task_struct *task,
929 struct task_struct *next, int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100930{
931 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000932 struct perf_counter_context *ctx = task->perf_counter_ctxp;
Paul Mackerras564c2b22009-05-22 14:27:22 +1000933 struct perf_counter_context *next_ctx;
Peter Zijlstra4a0deca2009-03-19 20:26:12 +0100934 struct pt_regs *regs;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100935
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000936 if (likely(!ctx || !cpuctx->task_ctx))
Thomas Gleixner0793a612008-12-04 20:12:29 +0100937 return;
938
Peter Zijlstrabce379b2009-04-06 11:45:13 +0200939 update_context_time(ctx);
940
Peter Zijlstra4a0deca2009-03-19 20:26:12 +0100941 regs = task_pt_regs(task);
Peter Zijlstra78f13e92009-04-08 15:01:33 +0200942 perf_swcounter_event(PERF_COUNT_CONTEXT_SWITCHES, 1, 1, regs, 0);
Paul Mackerras564c2b22009-05-22 14:27:22 +1000943
944 next_ctx = next->perf_counter_ctxp;
945 if (next_ctx && context_equiv(ctx, next_ctx)) {
946 task->perf_counter_ctxp = next_ctx;
947 next->perf_counter_ctxp = ctx;
948 ctx->task = next;
949 next_ctx->task = task;
950 return;
951 }
952
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100953 __perf_counter_sched_out(ctx, cpuctx);
954
Thomas Gleixner0793a612008-12-04 20:12:29 +0100955 cpuctx->task_ctx = NULL;
956}
957
Paul Mackerrasa08b1592009-05-11 15:46:10 +1000958static void __perf_counter_task_sched_out(struct perf_counter_context *ctx)
959{
960 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
961
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000962 if (!cpuctx->task_ctx)
963 return;
Paul Mackerrasa08b1592009-05-11 15:46:10 +1000964 __perf_counter_sched_out(ctx, cpuctx);
965 cpuctx->task_ctx = NULL;
966}
967
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100968static void perf_counter_cpu_sched_out(struct perf_cpu_context *cpuctx)
Ingo Molnar04289bb2008-12-11 08:38:42 +0100969{
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100970 __perf_counter_sched_out(&cpuctx->ctx, cpuctx);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100971}
972
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100973static void
974__perf_counter_sched_in(struct perf_counter_context *ctx,
975 struct perf_cpu_context *cpuctx, int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100976{
Thomas Gleixner0793a612008-12-04 20:12:29 +0100977 struct perf_counter *counter;
Paul Mackerrasdd0e6ba2009-01-12 15:11:00 +1100978 int can_add_hw = 1;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100979
Thomas Gleixner0793a612008-12-04 20:12:29 +0100980 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100981 ctx->is_active = 1;
982 if (likely(!ctx->nr_counters))
983 goto out;
984
Peter Zijlstra4af49982009-04-06 11:45:10 +0200985 ctx->timestamp = perf_clock();
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100986
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200987 perf_disable();
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100988
989 /*
990 * First go through the list and put on any pinned groups
991 * in order to give them the best chance of going on.
992 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100993 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100994 if (counter->state <= PERF_COUNTER_STATE_OFF ||
995 !counter->hw_event.pinned)
996 continue;
997 if (counter->cpu != -1 && counter->cpu != cpu)
998 continue;
999
Peter Zijlstraafedadf2009-05-20 12:21:22 +02001000 if (counter != counter->group_leader)
1001 counter_sched_in(counter, cpuctx, ctx, cpu);
1002 else {
1003 if (group_can_go_on(counter, cpuctx, 1))
1004 group_sched_in(counter, cpuctx, ctx, cpu);
1005 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001006
1007 /*
1008 * If this pinned group hasn't been scheduled,
1009 * put it in error state.
1010 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001011 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
1012 update_group_times(counter);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001013 counter->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001014 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001015 }
1016
1017 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1018 /*
1019 * Ignore counters in OFF or ERROR state, and
1020 * ignore pinned counters since we did them already.
1021 */
1022 if (counter->state <= PERF_COUNTER_STATE_OFF ||
1023 counter->hw_event.pinned)
1024 continue;
1025
Ingo Molnar04289bb2008-12-11 08:38:42 +01001026 /*
1027 * Listen to the 'cpu' scheduling filter constraint
1028 * of counters:
1029 */
Thomas Gleixner0793a612008-12-04 20:12:29 +01001030 if (counter->cpu != -1 && counter->cpu != cpu)
1031 continue;
1032
Peter Zijlstraafedadf2009-05-20 12:21:22 +02001033 if (counter != counter->group_leader) {
1034 if (counter_sched_in(counter, cpuctx, ctx, cpu))
Paul Mackerrasdd0e6ba2009-01-12 15:11:00 +11001035 can_add_hw = 0;
Peter Zijlstraafedadf2009-05-20 12:21:22 +02001036 } else {
1037 if (group_can_go_on(counter, cpuctx, can_add_hw)) {
1038 if (group_sched_in(counter, cpuctx, ctx, cpu))
1039 can_add_hw = 0;
1040 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001041 }
Thomas Gleixner0793a612008-12-04 20:12:29 +01001042 }
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001043 perf_enable();
Paul Mackerrasd859e292009-01-17 18:10:22 +11001044 out:
Thomas Gleixner0793a612008-12-04 20:12:29 +01001045 spin_unlock(&ctx->lock);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001046}
Ingo Molnar04289bb2008-12-11 08:38:42 +01001047
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001048/*
1049 * Called from scheduler to add the counters of the current task
1050 * with interrupts disabled.
1051 *
1052 * We restore the counter value and then enable it.
1053 *
1054 * This does not protect us against NMI, but enable()
1055 * sets the enabled bit in the control field of counter _before_
1056 * accessing the counter control register. If a NMI hits, then it will
1057 * keep the counter running.
1058 */
1059void perf_counter_task_sched_in(struct task_struct *task, int cpu)
1060{
1061 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001062 struct perf_counter_context *ctx = task->perf_counter_ctxp;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001063
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001064 if (likely(!ctx))
1065 return;
Paul Mackerras564c2b22009-05-22 14:27:22 +10001066 if (cpuctx->task_ctx == ctx)
1067 return;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001068 __perf_counter_sched_in(ctx, cpuctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001069 cpuctx->task_ctx = ctx;
1070}
1071
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001072static void perf_counter_cpu_sched_in(struct perf_cpu_context *cpuctx, int cpu)
1073{
1074 struct perf_counter_context *ctx = &cpuctx->ctx;
1075
1076 __perf_counter_sched_in(ctx, cpuctx, cpu);
1077}
1078
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001079int perf_counter_task_disable(void)
1080{
1081 struct task_struct *curr = current;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001082 struct perf_counter_context *ctx = curr->perf_counter_ctxp;
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001083 struct perf_counter *counter;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001084 unsigned long flags;
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001085
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001086 if (!ctx || !ctx->nr_counters)
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001087 return 0;
1088
Peter Zijlstra849691a2009-04-06 11:45:12 +02001089 local_irq_save(flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001090
Paul Mackerrasa08b1592009-05-11 15:46:10 +10001091 __perf_counter_task_sched_out(ctx);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001092
1093 spin_lock(&ctx->lock);
1094
1095 /*
1096 * Disable all the counters:
1097 */
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001098 perf_disable();
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001099
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001100 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001101 if (counter->state != PERF_COUNTER_STATE_ERROR) {
1102 update_group_times(counter);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001103 counter->state = PERF_COUNTER_STATE_OFF;
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001104 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001105 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01001106
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001107 perf_enable();
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001108
Peter Zijlstra849691a2009-04-06 11:45:12 +02001109 spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001110
1111 return 0;
1112}
1113
1114int perf_counter_task_enable(void)
1115{
1116 struct task_struct *curr = current;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001117 struct perf_counter_context *ctx = curr->perf_counter_ctxp;
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001118 struct perf_counter *counter;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001119 unsigned long flags;
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001120 int cpu;
1121
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001122 if (!ctx || !ctx->nr_counters)
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001123 return 0;
1124
Peter Zijlstra849691a2009-04-06 11:45:12 +02001125 local_irq_save(flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001126 cpu = smp_processor_id();
1127
Paul Mackerrasa08b1592009-05-11 15:46:10 +10001128 __perf_counter_task_sched_out(ctx);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001129
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001130 spin_lock(&ctx->lock);
1131
1132 /*
1133 * Disable all the counters:
1134 */
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001135 perf_disable();
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001136
1137 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001138 if (counter->state > PERF_COUNTER_STATE_OFF)
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001139 continue;
Ingo Molnar6a930702008-12-11 15:17:03 +01001140 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +02001141 counter->tstamp_enabled =
1142 ctx->time - counter->total_time_enabled;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001143 counter->hw_event.disabled = 0;
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001144 }
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001145 perf_enable();
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001146
1147 spin_unlock(&ctx->lock);
1148
1149 perf_counter_task_sched_in(curr, cpu);
1150
Peter Zijlstra849691a2009-04-06 11:45:12 +02001151 local_irq_restore(flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001152
1153 return 0;
1154}
1155
Peter Zijlstra26b119b2009-05-20 12:21:20 +02001156static void perf_log_period(struct perf_counter *counter, u64 period);
1157
1158static void perf_adjust_freq(struct perf_counter_context *ctx)
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001159{
1160 struct perf_counter *counter;
1161 u64 irq_period;
1162 u64 events, period;
1163 s64 delta;
1164
1165 spin_lock(&ctx->lock);
1166 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1167 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
1168 continue;
1169
1170 if (!counter->hw_event.freq || !counter->hw_event.irq_freq)
1171 continue;
1172
1173 events = HZ * counter->hw.interrupts * counter->hw.irq_period;
1174 period = div64_u64(events, counter->hw_event.irq_freq);
1175
1176 delta = (s64)(1 + period - counter->hw.irq_period);
1177 delta >>= 1;
1178
1179 irq_period = counter->hw.irq_period + delta;
1180
1181 if (!irq_period)
1182 irq_period = 1;
1183
Peter Zijlstra26b119b2009-05-20 12:21:20 +02001184 perf_log_period(counter, irq_period);
1185
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001186 counter->hw.irq_period = irq_period;
1187 counter->hw.interrupts = 0;
1188 }
1189 spin_unlock(&ctx->lock);
1190}
1191
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001192/*
1193 * Round-robin a context's counters:
1194 */
1195static void rotate_ctx(struct perf_counter_context *ctx)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001196{
Thomas Gleixner0793a612008-12-04 20:12:29 +01001197 struct perf_counter *counter;
1198
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001199 if (!ctx->nr_counters)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001200 return;
1201
Thomas Gleixner0793a612008-12-04 20:12:29 +01001202 spin_lock(&ctx->lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001203 /*
Ingo Molnar04289bb2008-12-11 08:38:42 +01001204 * Rotate the first entry last (works just fine for group counters too):
Thomas Gleixner0793a612008-12-04 20:12:29 +01001205 */
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001206 perf_disable();
Ingo Molnar04289bb2008-12-11 08:38:42 +01001207 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Peter Zijlstra75564232009-03-13 12:21:29 +01001208 list_move_tail(&counter->list_entry, &ctx->counter_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001209 break;
1210 }
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001211 perf_enable();
Thomas Gleixner0793a612008-12-04 20:12:29 +01001212
1213 spin_unlock(&ctx->lock);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001214}
Thomas Gleixner0793a612008-12-04 20:12:29 +01001215
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001216void perf_counter_task_tick(struct task_struct *curr, int cpu)
1217{
Peter Zijlstra7fc23a52009-05-08 18:52:21 +02001218 struct perf_cpu_context *cpuctx;
1219 struct perf_counter_context *ctx;
1220
1221 if (!atomic_read(&nr_counters))
1222 return;
1223
1224 cpuctx = &per_cpu(perf_cpu_context, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001225 ctx = curr->perf_counter_ctxp;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001226
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001227 perf_adjust_freq(&cpuctx->ctx);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001228 if (ctx)
1229 perf_adjust_freq(ctx);
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001230
Ingo Molnarb82914c2009-05-04 18:54:32 +02001231 perf_counter_cpu_sched_out(cpuctx);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001232 if (ctx)
1233 __perf_counter_task_sched_out(ctx);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001234
Ingo Molnarb82914c2009-05-04 18:54:32 +02001235 rotate_ctx(&cpuctx->ctx);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001236 if (ctx)
1237 rotate_ctx(ctx);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001238
Ingo Molnarb82914c2009-05-04 18:54:32 +02001239 perf_counter_cpu_sched_in(cpuctx, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001240 if (ctx)
1241 perf_counter_task_sched_in(curr, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001242}
1243
1244/*
Thomas Gleixner0793a612008-12-04 20:12:29 +01001245 * Cross CPU call to read the hardware counter
1246 */
Ingo Molnar76715812008-12-17 14:20:28 +01001247static void __read(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001248{
Ingo Molnar621a01e2008-12-11 12:46:46 +01001249 struct perf_counter *counter = info;
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001250 struct perf_counter_context *ctx = counter->ctx;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001251 unsigned long flags;
Ingo Molnar621a01e2008-12-11 12:46:46 +01001252
Peter Zijlstra849691a2009-04-06 11:45:12 +02001253 local_irq_save(flags);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001254 if (ctx->is_active)
Peter Zijlstra4af49982009-04-06 11:45:10 +02001255 update_context_time(ctx);
Robert Richter4aeb0b42009-04-29 12:47:03 +02001256 counter->pmu->read(counter);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001257 update_counter_times(counter);
Peter Zijlstra849691a2009-04-06 11:45:12 +02001258 local_irq_restore(flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001259}
1260
Ingo Molnar04289bb2008-12-11 08:38:42 +01001261static u64 perf_counter_read(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001262{
1263 /*
1264 * If counter is enabled and currently active on a CPU, update the
1265 * value in the counter structure:
1266 */
Ingo Molnar6a930702008-12-11 15:17:03 +01001267 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
Thomas Gleixner0793a612008-12-04 20:12:29 +01001268 smp_call_function_single(counter->oncpu,
Ingo Molnar76715812008-12-17 14:20:28 +01001269 __read, counter, 1);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001270 } else if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
1271 update_counter_times(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001272 }
1273
Ingo Molnaree060942008-12-13 09:00:03 +01001274 return atomic64_read(&counter->count);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001275}
1276
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001277/*
1278 * Initialize the perf_counter context in a task_struct:
1279 */
1280static void
1281__perf_counter_init_context(struct perf_counter_context *ctx,
1282 struct task_struct *task)
1283{
1284 memset(ctx, 0, sizeof(*ctx));
1285 spin_lock_init(&ctx->lock);
1286 mutex_init(&ctx->mutex);
1287 INIT_LIST_HEAD(&ctx->counter_list);
1288 INIT_LIST_HEAD(&ctx->event_list);
1289 atomic_set(&ctx->refcount, 1);
1290 ctx->task = task;
1291}
1292
Thomas Gleixner0793a612008-12-04 20:12:29 +01001293static void put_context(struct perf_counter_context *ctx)
1294{
1295 if (ctx->task)
1296 put_task_struct(ctx->task);
1297}
1298
1299static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
1300{
1301 struct perf_cpu_context *cpuctx;
1302 struct perf_counter_context *ctx;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001303 struct perf_counter_context *tctx;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001304 struct task_struct *task;
1305
1306 /*
1307 * If cpu is not a wildcard then this is a percpu counter:
1308 */
1309 if (cpu != -1) {
1310 /* Must be root to operate on a CPU counter: */
Peter Zijlstra1ccd1542009-04-09 10:53:45 +02001311 if (sysctl_perf_counter_priv && !capable(CAP_SYS_ADMIN))
Thomas Gleixner0793a612008-12-04 20:12:29 +01001312 return ERR_PTR(-EACCES);
1313
1314 if (cpu < 0 || cpu > num_possible_cpus())
1315 return ERR_PTR(-EINVAL);
1316
1317 /*
1318 * We could be clever and allow to attach a counter to an
1319 * offline CPU and activate it when the CPU comes up, but
1320 * that's for later.
1321 */
1322 if (!cpu_isset(cpu, cpu_online_map))
1323 return ERR_PTR(-ENODEV);
1324
1325 cpuctx = &per_cpu(perf_cpu_context, cpu);
1326 ctx = &cpuctx->ctx;
1327
Thomas Gleixner0793a612008-12-04 20:12:29 +01001328 return ctx;
1329 }
1330
1331 rcu_read_lock();
1332 if (!pid)
1333 task = current;
1334 else
1335 task = find_task_by_vpid(pid);
1336 if (task)
1337 get_task_struct(task);
1338 rcu_read_unlock();
1339
1340 if (!task)
1341 return ERR_PTR(-ESRCH);
1342
Thomas Gleixner0793a612008-12-04 20:12:29 +01001343 /* Reuse ptrace permission checks for now. */
1344 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001345 put_task_struct(task);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001346 return ERR_PTR(-EACCES);
1347 }
1348
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001349 ctx = task->perf_counter_ctxp;
1350 if (!ctx) {
1351 ctx = kmalloc(sizeof(struct perf_counter_context), GFP_KERNEL);
1352 if (!ctx) {
1353 put_task_struct(task);
1354 return ERR_PTR(-ENOMEM);
1355 }
1356 __perf_counter_init_context(ctx, task);
1357 /*
1358 * Make sure other cpus see correct values for *ctx
1359 * once task->perf_counter_ctxp is visible to them.
1360 */
1361 smp_wmb();
1362 tctx = cmpxchg(&task->perf_counter_ctxp, NULL, ctx);
1363 if (tctx) {
1364 /*
1365 * We raced with some other task; use
1366 * the context they set.
1367 */
1368 kfree(ctx);
1369 ctx = tctx;
1370 }
1371 }
1372
Thomas Gleixner0793a612008-12-04 20:12:29 +01001373 return ctx;
1374}
1375
Peter Zijlstra592903c2009-03-13 12:21:36 +01001376static void free_counter_rcu(struct rcu_head *head)
1377{
1378 struct perf_counter *counter;
1379
1380 counter = container_of(head, struct perf_counter, rcu_head);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001381 put_ctx(counter->ctx);
Peter Zijlstra592903c2009-03-13 12:21:36 +01001382 kfree(counter);
1383}
1384
Peter Zijlstra925d5192009-03-30 19:07:02 +02001385static void perf_pending_sync(struct perf_counter *counter);
1386
Peter Zijlstraf1600952009-03-19 20:26:16 +01001387static void free_counter(struct perf_counter *counter)
1388{
Peter Zijlstra925d5192009-03-30 19:07:02 +02001389 perf_pending_sync(counter);
1390
Peter Zijlstra7fc23a52009-05-08 18:52:21 +02001391 atomic_dec(&nr_counters);
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02001392 if (counter->hw_event.mmap)
1393 atomic_dec(&nr_mmap_tracking);
1394 if (counter->hw_event.munmap)
1395 atomic_dec(&nr_munmap_tracking);
1396 if (counter->hw_event.comm)
1397 atomic_dec(&nr_comm_tracking);
1398
Peter Zijlstrae077df42009-03-19 20:26:17 +01001399 if (counter->destroy)
1400 counter->destroy(counter);
1401
Peter Zijlstraf1600952009-03-19 20:26:16 +01001402 call_rcu(&counter->rcu_head, free_counter_rcu);
1403}
1404
Thomas Gleixner0793a612008-12-04 20:12:29 +01001405/*
1406 * Called when the last reference to the file is gone.
1407 */
1408static int perf_release(struct inode *inode, struct file *file)
1409{
1410 struct perf_counter *counter = file->private_data;
1411 struct perf_counter_context *ctx = counter->ctx;
1412
1413 file->private_data = NULL;
1414
Paul Mackerrasd859e292009-01-17 18:10:22 +11001415 mutex_lock(&ctx->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01001416 perf_counter_remove_from_context(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001417 mutex_unlock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001418
Peter Zijlstraf1600952009-03-19 20:26:16 +01001419 free_counter(counter);
Mike Galbraith5af75912009-02-11 10:53:37 +01001420 put_context(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001421
1422 return 0;
1423}
1424
1425/*
1426 * Read the performance counter - simple non blocking version for now
1427 */
1428static ssize_t
1429perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
1430{
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001431 u64 values[3];
1432 int n;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001433
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001434 /*
1435 * Return end-of-file for a read on a counter that is in
1436 * error state (i.e. because it was pinned but it couldn't be
1437 * scheduled on to the CPU at some point).
1438 */
1439 if (counter->state == PERF_COUNTER_STATE_ERROR)
1440 return 0;
1441
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001442 mutex_lock(&counter->child_mutex);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001443 values[0] = perf_counter_read(counter);
1444 n = 1;
1445 if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1446 values[n++] = counter->total_time_enabled +
1447 atomic64_read(&counter->child_total_time_enabled);
1448 if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1449 values[n++] = counter->total_time_running +
1450 atomic64_read(&counter->child_total_time_running);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001451 mutex_unlock(&counter->child_mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001452
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001453 if (count < n * sizeof(u64))
1454 return -EINVAL;
1455 count = n * sizeof(u64);
1456
1457 if (copy_to_user(buf, values, count))
1458 return -EFAULT;
1459
1460 return count;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001461}
1462
1463static ssize_t
Thomas Gleixner0793a612008-12-04 20:12:29 +01001464perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
1465{
1466 struct perf_counter *counter = file->private_data;
1467
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001468 return perf_read_hw(counter, buf, count);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001469}
1470
1471static unsigned int perf_poll(struct file *file, poll_table *wait)
1472{
1473 struct perf_counter *counter = file->private_data;
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001474 struct perf_mmap_data *data;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001475 unsigned int events = POLL_HUP;
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001476
1477 rcu_read_lock();
1478 data = rcu_dereference(counter->data);
1479 if (data)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001480 events = atomic_xchg(&data->poll, 0);
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001481 rcu_read_unlock();
Thomas Gleixner0793a612008-12-04 20:12:29 +01001482
1483 poll_wait(file, &counter->waitq, wait);
1484
Thomas Gleixner0793a612008-12-04 20:12:29 +01001485 return events;
1486}
1487
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001488static void perf_counter_reset(struct perf_counter *counter)
1489{
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001490 (void)perf_counter_read(counter);
Paul Mackerras615a3f12009-05-11 15:50:21 +10001491 atomic64_set(&counter->count, 0);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001492 perf_counter_update_userpage(counter);
1493}
1494
1495static void perf_counter_for_each_sibling(struct perf_counter *counter,
1496 void (*func)(struct perf_counter *))
1497{
1498 struct perf_counter_context *ctx = counter->ctx;
1499 struct perf_counter *sibling;
1500
Peter Zijlstra682076a2009-05-23 18:28:57 +02001501 mutex_lock(&ctx->mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001502 counter = counter->group_leader;
1503
1504 func(counter);
1505 list_for_each_entry(sibling, &counter->sibling_list, list_entry)
1506 func(sibling);
Peter Zijlstra682076a2009-05-23 18:28:57 +02001507 mutex_unlock(&ctx->mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001508}
1509
1510static void perf_counter_for_each_child(struct perf_counter *counter,
1511 void (*func)(struct perf_counter *))
1512{
1513 struct perf_counter *child;
1514
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001515 mutex_lock(&counter->child_mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001516 func(counter);
1517 list_for_each_entry(child, &counter->child_list, child_list)
1518 func(child);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001519 mutex_unlock(&counter->child_mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001520}
1521
1522static void perf_counter_for_each(struct perf_counter *counter,
1523 void (*func)(struct perf_counter *))
1524{
1525 struct perf_counter *child;
1526
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001527 mutex_lock(&counter->child_mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001528 perf_counter_for_each_sibling(counter, func);
1529 list_for_each_entry(child, &counter->child_list, child_list)
1530 perf_counter_for_each_sibling(child, func);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001531 mutex_unlock(&counter->child_mutex);
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001532}
1533
Paul Mackerrasd859e292009-01-17 18:10:22 +11001534static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1535{
1536 struct perf_counter *counter = file->private_data;
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001537 void (*func)(struct perf_counter *);
1538 u32 flags = arg;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001539
1540 switch (cmd) {
1541 case PERF_COUNTER_IOC_ENABLE:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001542 func = perf_counter_enable;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001543 break;
1544 case PERF_COUNTER_IOC_DISABLE:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001545 func = perf_counter_disable;
Peter Zijlstra79f14642009-04-06 11:45:07 +02001546 break;
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001547 case PERF_COUNTER_IOC_RESET:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001548 func = perf_counter_reset;
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001549 break;
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001550
1551 case PERF_COUNTER_IOC_REFRESH:
1552 return perf_counter_refresh(counter, arg);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001553 default:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001554 return -ENOTTY;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001555 }
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001556
1557 if (flags & PERF_IOC_FLAG_GROUP)
1558 perf_counter_for_each(counter, func);
1559 else
1560 perf_counter_for_each_child(counter, func);
1561
1562 return 0;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001563}
1564
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001565/*
1566 * Callers need to ensure there can be no nesting of this function, otherwise
1567 * the seqlock logic goes bad. We can not serialize this because the arch
1568 * code calls this from NMI context.
1569 */
1570void perf_counter_update_userpage(struct perf_counter *counter)
Paul Mackerras37d81822009-03-23 18:22:08 +01001571{
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001572 struct perf_mmap_data *data;
1573 struct perf_counter_mmap_page *userpg;
1574
1575 rcu_read_lock();
1576 data = rcu_dereference(counter->data);
1577 if (!data)
1578 goto unlock;
1579
1580 userpg = data->user_page;
Paul Mackerras37d81822009-03-23 18:22:08 +01001581
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001582 /*
1583 * Disable preemption so as to not let the corresponding user-space
1584 * spin too long if we get preempted.
1585 */
1586 preempt_disable();
Paul Mackerras37d81822009-03-23 18:22:08 +01001587 ++userpg->lock;
Peter Zijlstra92f22a32009-04-02 11:12:04 +02001588 barrier();
Paul Mackerras37d81822009-03-23 18:22:08 +01001589 userpg->index = counter->hw.idx;
1590 userpg->offset = atomic64_read(&counter->count);
1591 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
1592 userpg->offset -= atomic64_read(&counter->hw.prev_count);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001593
Peter Zijlstra92f22a32009-04-02 11:12:04 +02001594 barrier();
Paul Mackerras37d81822009-03-23 18:22:08 +01001595 ++userpg->lock;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001596 preempt_enable();
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001597unlock:
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001598 rcu_read_unlock();
Paul Mackerras37d81822009-03-23 18:22:08 +01001599}
1600
1601static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1602{
1603 struct perf_counter *counter = vma->vm_file->private_data;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001604 struct perf_mmap_data *data;
1605 int ret = VM_FAULT_SIGBUS;
Paul Mackerras37d81822009-03-23 18:22:08 +01001606
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001607 rcu_read_lock();
1608 data = rcu_dereference(counter->data);
1609 if (!data)
1610 goto unlock;
Paul Mackerras37d81822009-03-23 18:22:08 +01001611
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001612 if (vmf->pgoff == 0) {
1613 vmf->page = virt_to_page(data->user_page);
1614 } else {
1615 int nr = vmf->pgoff - 1;
1616
1617 if ((unsigned)nr > data->nr_pages)
1618 goto unlock;
1619
1620 vmf->page = virt_to_page(data->data_pages[nr]);
1621 }
Paul Mackerras37d81822009-03-23 18:22:08 +01001622 get_page(vmf->page);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001623 ret = 0;
1624unlock:
1625 rcu_read_unlock();
1626
1627 return ret;
1628}
1629
1630static int perf_mmap_data_alloc(struct perf_counter *counter, int nr_pages)
1631{
1632 struct perf_mmap_data *data;
1633 unsigned long size;
1634 int i;
1635
1636 WARN_ON(atomic_read(&counter->mmap_count));
1637
1638 size = sizeof(struct perf_mmap_data);
1639 size += nr_pages * sizeof(void *);
1640
1641 data = kzalloc(size, GFP_KERNEL);
1642 if (!data)
1643 goto fail;
1644
1645 data->user_page = (void *)get_zeroed_page(GFP_KERNEL);
1646 if (!data->user_page)
1647 goto fail_user_page;
1648
1649 for (i = 0; i < nr_pages; i++) {
1650 data->data_pages[i] = (void *)get_zeroed_page(GFP_KERNEL);
1651 if (!data->data_pages[i])
1652 goto fail_data_pages;
1653 }
1654
1655 data->nr_pages = nr_pages;
Peter Zijlstra22c15582009-05-05 17:50:25 +02001656 atomic_set(&data->lock, -1);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001657
1658 rcu_assign_pointer(counter->data, data);
1659
Paul Mackerras37d81822009-03-23 18:22:08 +01001660 return 0;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001661
1662fail_data_pages:
1663 for (i--; i >= 0; i--)
1664 free_page((unsigned long)data->data_pages[i]);
1665
1666 free_page((unsigned long)data->user_page);
1667
1668fail_user_page:
1669 kfree(data);
1670
1671fail:
1672 return -ENOMEM;
1673}
1674
1675static void __perf_mmap_data_free(struct rcu_head *rcu_head)
1676{
1677 struct perf_mmap_data *data = container_of(rcu_head,
1678 struct perf_mmap_data, rcu_head);
1679 int i;
1680
1681 free_page((unsigned long)data->user_page);
1682 for (i = 0; i < data->nr_pages; i++)
1683 free_page((unsigned long)data->data_pages[i]);
1684 kfree(data);
1685}
1686
1687static void perf_mmap_data_free(struct perf_counter *counter)
1688{
1689 struct perf_mmap_data *data = counter->data;
1690
1691 WARN_ON(atomic_read(&counter->mmap_count));
1692
1693 rcu_assign_pointer(counter->data, NULL);
1694 call_rcu(&data->rcu_head, __perf_mmap_data_free);
1695}
1696
1697static void perf_mmap_open(struct vm_area_struct *vma)
1698{
1699 struct perf_counter *counter = vma->vm_file->private_data;
1700
1701 atomic_inc(&counter->mmap_count);
1702}
1703
1704static void perf_mmap_close(struct vm_area_struct *vma)
1705{
1706 struct perf_counter *counter = vma->vm_file->private_data;
1707
1708 if (atomic_dec_and_mutex_lock(&counter->mmap_count,
1709 &counter->mmap_mutex)) {
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001710 struct user_struct *user = current_user();
1711
1712 atomic_long_sub(counter->data->nr_pages + 1, &user->locked_vm);
Peter Zijlstrac5078f72009-05-05 17:50:24 +02001713 vma->vm_mm->locked_vm -= counter->data->nr_locked;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001714 perf_mmap_data_free(counter);
1715 mutex_unlock(&counter->mmap_mutex);
1716 }
Paul Mackerras37d81822009-03-23 18:22:08 +01001717}
1718
1719static struct vm_operations_struct perf_mmap_vmops = {
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001720 .open = perf_mmap_open,
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001721 .close = perf_mmap_close,
Paul Mackerras37d81822009-03-23 18:22:08 +01001722 .fault = perf_mmap_fault,
1723};
1724
1725static int perf_mmap(struct file *file, struct vm_area_struct *vma)
1726{
1727 struct perf_counter *counter = file->private_data;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001728 struct user_struct *user = current_user();
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001729 unsigned long vma_size;
1730 unsigned long nr_pages;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001731 unsigned long user_locked, user_lock_limit;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001732 unsigned long locked, lock_limit;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001733 long user_extra, extra;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001734 int ret = 0;
Paul Mackerras37d81822009-03-23 18:22:08 +01001735
1736 if (!(vma->vm_flags & VM_SHARED) || (vma->vm_flags & VM_WRITE))
1737 return -EINVAL;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001738
1739 vma_size = vma->vm_end - vma->vm_start;
1740 nr_pages = (vma_size / PAGE_SIZE) - 1;
1741
Peter Zijlstra7730d862009-03-25 12:48:31 +01001742 /*
1743 * If we have data pages ensure they're a power-of-two number, so we
1744 * can do bitmasks instead of modulo.
1745 */
1746 if (nr_pages != 0 && !is_power_of_2(nr_pages))
Paul Mackerras37d81822009-03-23 18:22:08 +01001747 return -EINVAL;
1748
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001749 if (vma_size != PAGE_SIZE * (1 + nr_pages))
Paul Mackerras37d81822009-03-23 18:22:08 +01001750 return -EINVAL;
1751
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001752 if (vma->vm_pgoff != 0)
1753 return -EINVAL;
Paul Mackerras37d81822009-03-23 18:22:08 +01001754
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001755 mutex_lock(&counter->mmap_mutex);
1756 if (atomic_inc_not_zero(&counter->mmap_count)) {
1757 if (nr_pages != counter->data->nr_pages)
1758 ret = -EINVAL;
1759 goto unlock;
1760 }
1761
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001762 user_extra = nr_pages + 1;
1763 user_lock_limit = sysctl_perf_counter_mlock >> (PAGE_SHIFT - 10);
1764 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
Peter Zijlstrac5078f72009-05-05 17:50:24 +02001765
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001766 extra = 0;
1767 if (user_locked > user_lock_limit)
1768 extra = user_locked - user_lock_limit;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001769
1770 lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
1771 lock_limit >>= PAGE_SHIFT;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001772 locked = vma->vm_mm->locked_vm + extra;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001773
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001774 if ((locked > lock_limit) && !capable(CAP_IPC_LOCK)) {
1775 ret = -EPERM;
1776 goto unlock;
1777 }
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001778
1779 WARN_ON(counter->data);
1780 ret = perf_mmap_data_alloc(counter, nr_pages);
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001781 if (ret)
1782 goto unlock;
1783
1784 atomic_set(&counter->mmap_count, 1);
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001785 atomic_long_add(user_extra, &user->locked_vm);
Peter Zijlstrac5078f72009-05-05 17:50:24 +02001786 vma->vm_mm->locked_vm += extra;
1787 counter->data->nr_locked = extra;
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001788unlock:
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001789 mutex_unlock(&counter->mmap_mutex);
Paul Mackerras37d81822009-03-23 18:22:08 +01001790
1791 vma->vm_flags &= ~VM_MAYWRITE;
1792 vma->vm_flags |= VM_RESERVED;
1793 vma->vm_ops = &perf_mmap_vmops;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001794
1795 return ret;
Paul Mackerras37d81822009-03-23 18:22:08 +01001796}
1797
Peter Zijlstra3c446b3d2009-04-06 11:45:01 +02001798static int perf_fasync(int fd, struct file *filp, int on)
1799{
1800 struct perf_counter *counter = filp->private_data;
1801 struct inode *inode = filp->f_path.dentry->d_inode;
1802 int retval;
1803
1804 mutex_lock(&inode->i_mutex);
1805 retval = fasync_helper(fd, filp, on, &counter->fasync);
1806 mutex_unlock(&inode->i_mutex);
1807
1808 if (retval < 0)
1809 return retval;
1810
1811 return 0;
1812}
1813
Thomas Gleixner0793a612008-12-04 20:12:29 +01001814static const struct file_operations perf_fops = {
1815 .release = perf_release,
1816 .read = perf_read,
1817 .poll = perf_poll,
Paul Mackerrasd859e292009-01-17 18:10:22 +11001818 .unlocked_ioctl = perf_ioctl,
1819 .compat_ioctl = perf_ioctl,
Paul Mackerras37d81822009-03-23 18:22:08 +01001820 .mmap = perf_mmap,
Peter Zijlstra3c446b3d2009-04-06 11:45:01 +02001821 .fasync = perf_fasync,
Thomas Gleixner0793a612008-12-04 20:12:29 +01001822};
1823
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001824/*
Peter Zijlstra925d5192009-03-30 19:07:02 +02001825 * Perf counter wakeup
1826 *
1827 * If there's data, ensure we set the poll() state and publish everything
1828 * to user-space before waking everybody up.
1829 */
1830
1831void perf_counter_wakeup(struct perf_counter *counter)
1832{
Peter Zijlstra925d5192009-03-30 19:07:02 +02001833 wake_up_all(&counter->waitq);
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02001834
1835 if (counter->pending_kill) {
1836 kill_fasync(&counter->fasync, SIGIO, counter->pending_kill);
1837 counter->pending_kill = 0;
1838 }
Peter Zijlstra925d5192009-03-30 19:07:02 +02001839}
1840
1841/*
1842 * Pending wakeups
1843 *
1844 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
1845 *
1846 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
1847 * single linked list and use cmpxchg() to add entries lockless.
1848 */
1849
Peter Zijlstra79f14642009-04-06 11:45:07 +02001850static void perf_pending_counter(struct perf_pending_entry *entry)
1851{
1852 struct perf_counter *counter = container_of(entry,
1853 struct perf_counter, pending);
1854
1855 if (counter->pending_disable) {
1856 counter->pending_disable = 0;
1857 perf_counter_disable(counter);
1858 }
1859
1860 if (counter->pending_wakeup) {
1861 counter->pending_wakeup = 0;
1862 perf_counter_wakeup(counter);
1863 }
1864}
1865
Peter Zijlstra671dec52009-04-06 11:45:02 +02001866#define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
Peter Zijlstra925d5192009-03-30 19:07:02 +02001867
Peter Zijlstra671dec52009-04-06 11:45:02 +02001868static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = {
Peter Zijlstra925d5192009-03-30 19:07:02 +02001869 PENDING_TAIL,
1870};
1871
Peter Zijlstra671dec52009-04-06 11:45:02 +02001872static void perf_pending_queue(struct perf_pending_entry *entry,
1873 void (*func)(struct perf_pending_entry *))
Peter Zijlstra925d5192009-03-30 19:07:02 +02001874{
Peter Zijlstra671dec52009-04-06 11:45:02 +02001875 struct perf_pending_entry **head;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001876
Peter Zijlstra671dec52009-04-06 11:45:02 +02001877 if (cmpxchg(&entry->next, NULL, PENDING_TAIL) != NULL)
Peter Zijlstra925d5192009-03-30 19:07:02 +02001878 return;
1879
Peter Zijlstra671dec52009-04-06 11:45:02 +02001880 entry->func = func;
1881
1882 head = &get_cpu_var(perf_pending_head);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001883
1884 do {
Peter Zijlstra671dec52009-04-06 11:45:02 +02001885 entry->next = *head;
1886 } while (cmpxchg(head, entry->next, entry) != entry->next);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001887
1888 set_perf_counter_pending();
1889
Peter Zijlstra671dec52009-04-06 11:45:02 +02001890 put_cpu_var(perf_pending_head);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001891}
1892
1893static int __perf_pending_run(void)
1894{
Peter Zijlstra671dec52009-04-06 11:45:02 +02001895 struct perf_pending_entry *list;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001896 int nr = 0;
1897
Peter Zijlstra671dec52009-04-06 11:45:02 +02001898 list = xchg(&__get_cpu_var(perf_pending_head), PENDING_TAIL);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001899 while (list != PENDING_TAIL) {
Peter Zijlstra671dec52009-04-06 11:45:02 +02001900 void (*func)(struct perf_pending_entry *);
1901 struct perf_pending_entry *entry = list;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001902
1903 list = list->next;
1904
Peter Zijlstra671dec52009-04-06 11:45:02 +02001905 func = entry->func;
1906 entry->next = NULL;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001907 /*
1908 * Ensure we observe the unqueue before we issue the wakeup,
1909 * so that we won't be waiting forever.
1910 * -- see perf_not_pending().
1911 */
1912 smp_wmb();
1913
Peter Zijlstra671dec52009-04-06 11:45:02 +02001914 func(entry);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001915 nr++;
1916 }
1917
1918 return nr;
1919}
1920
1921static inline int perf_not_pending(struct perf_counter *counter)
1922{
1923 /*
1924 * If we flush on whatever cpu we run, there is a chance we don't
1925 * need to wait.
1926 */
1927 get_cpu();
1928 __perf_pending_run();
1929 put_cpu();
1930
1931 /*
1932 * Ensure we see the proper queue state before going to sleep
1933 * so that we do not miss the wakeup. -- see perf_pending_handle()
1934 */
1935 smp_rmb();
Peter Zijlstra671dec52009-04-06 11:45:02 +02001936 return counter->pending.next == NULL;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001937}
1938
1939static void perf_pending_sync(struct perf_counter *counter)
1940{
1941 wait_event(counter->waitq, perf_not_pending(counter));
1942}
1943
1944void perf_counter_do_pending(void)
1945{
1946 __perf_pending_run();
1947}
1948
1949/*
Peter Zijlstra394ee072009-03-30 19:07:14 +02001950 * Callchain support -- arch specific
1951 */
1952
Peter Zijlstra9c03d882009-04-06 11:45:00 +02001953__weak struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
Peter Zijlstra394ee072009-03-30 19:07:14 +02001954{
1955 return NULL;
1956}
1957
1958/*
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001959 * Output
1960 */
1961
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001962struct perf_output_handle {
1963 struct perf_counter *counter;
1964 struct perf_mmap_data *data;
1965 unsigned int offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01001966 unsigned int head;
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001967 int nmi;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02001968 int overflow;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001969 int locked;
1970 unsigned long flags;
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001971};
1972
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001973static void perf_output_wakeup(struct perf_output_handle *handle)
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001974{
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001975 atomic_set(&handle->data->poll, POLL_IN);
1976
Peter Zijlstra671dec52009-04-06 11:45:02 +02001977 if (handle->nmi) {
Peter Zijlstra79f14642009-04-06 11:45:07 +02001978 handle->counter->pending_wakeup = 1;
Peter Zijlstra671dec52009-04-06 11:45:02 +02001979 perf_pending_queue(&handle->counter->pending,
Peter Zijlstra79f14642009-04-06 11:45:07 +02001980 perf_pending_counter);
Peter Zijlstra671dec52009-04-06 11:45:02 +02001981 } else
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001982 perf_counter_wakeup(handle->counter);
1983}
1984
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001985/*
1986 * Curious locking construct.
1987 *
1988 * We need to ensure a later event doesn't publish a head when a former
1989 * event isn't done writing. However since we need to deal with NMIs we
1990 * cannot fully serialize things.
1991 *
1992 * What we do is serialize between CPUs so we only have to deal with NMI
1993 * nesting on a single CPU.
1994 *
1995 * We only publish the head (and generate a wakeup) when the outer-most
1996 * event completes.
1997 */
1998static void perf_output_lock(struct perf_output_handle *handle)
1999{
2000 struct perf_mmap_data *data = handle->data;
2001 int cpu;
2002
2003 handle->locked = 0;
2004
2005 local_irq_save(handle->flags);
2006 cpu = smp_processor_id();
2007
2008 if (in_nmi() && atomic_read(&data->lock) == cpu)
2009 return;
2010
Peter Zijlstra22c15582009-05-05 17:50:25 +02002011 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002012 cpu_relax();
2013
2014 handle->locked = 1;
2015}
2016
2017static void perf_output_unlock(struct perf_output_handle *handle)
2018{
2019 struct perf_mmap_data *data = handle->data;
2020 int head, cpu;
2021
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002022 data->done_head = data->head;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002023
2024 if (!handle->locked)
2025 goto out;
2026
2027again:
2028 /*
2029 * The xchg implies a full barrier that ensures all writes are done
2030 * before we publish the new head, matched by a rmb() in userspace when
2031 * reading this position.
2032 */
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002033 while ((head = atomic_xchg(&data->done_head, 0)))
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002034 data->user_page->data_head = head;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002035
2036 /*
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002037 * NMI can happen here, which means we can miss a done_head update.
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002038 */
2039
Peter Zijlstra22c15582009-05-05 17:50:25 +02002040 cpu = atomic_xchg(&data->lock, -1);
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002041 WARN_ON_ONCE(cpu != smp_processor_id());
2042
2043 /*
2044 * Therefore we have to validate we did not indeed do so.
2045 */
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002046 if (unlikely(atomic_read(&data->done_head))) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002047 /*
2048 * Since we had it locked, we can lock it again.
2049 */
Peter Zijlstra22c15582009-05-05 17:50:25 +02002050 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002051 cpu_relax();
2052
2053 goto again;
2054 }
2055
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002056 if (atomic_xchg(&data->wakeup, 0))
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002057 perf_output_wakeup(handle);
2058out:
2059 local_irq_restore(handle->flags);
2060}
2061
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002062static int perf_output_begin(struct perf_output_handle *handle,
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002063 struct perf_counter *counter, unsigned int size,
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002064 int nmi, int overflow)
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002065{
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002066 struct perf_mmap_data *data;
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002067 unsigned int offset, head;
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002068
Peter Zijlstra2023b352009-05-05 17:50:26 +02002069 /*
2070 * For inherited counters we send all the output towards the parent.
2071 */
2072 if (counter->parent)
2073 counter = counter->parent;
2074
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002075 rcu_read_lock();
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002076 data = rcu_dereference(counter->data);
2077 if (!data)
2078 goto out;
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002079
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002080 handle->data = data;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002081 handle->counter = counter;
2082 handle->nmi = nmi;
2083 handle->overflow = overflow;
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002084
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002085 if (!data->nr_pages)
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002086 goto fail;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002087
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002088 perf_output_lock(handle);
2089
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002090 do {
2091 offset = head = atomic_read(&data->head);
Peter Zijlstrac7138f32009-03-24 13:18:16 +01002092 head += size;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002093 } while (atomic_cmpxchg(&data->head, offset, head) != offset);
2094
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002095 handle->offset = offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01002096 handle->head = head;
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002097
2098 if ((offset >> PAGE_SHIFT) != (head >> PAGE_SHIFT))
2099 atomic_set(&data->wakeup, 1);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002100
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002101 return 0;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002102
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002103fail:
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002104 perf_output_wakeup(handle);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002105out:
2106 rcu_read_unlock();
2107
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002108 return -ENOSPC;
2109}
2110
2111static void perf_output_copy(struct perf_output_handle *handle,
2112 void *buf, unsigned int len)
2113{
2114 unsigned int pages_mask;
2115 unsigned int offset;
2116 unsigned int size;
2117 void **pages;
2118
2119 offset = handle->offset;
2120 pages_mask = handle->data->nr_pages - 1;
2121 pages = handle->data->data_pages;
2122
2123 do {
2124 unsigned int page_offset;
2125 int nr;
2126
2127 nr = (offset >> PAGE_SHIFT) & pages_mask;
2128 page_offset = offset & (PAGE_SIZE - 1);
2129 size = min_t(unsigned int, PAGE_SIZE - page_offset, len);
2130
2131 memcpy(pages[nr] + page_offset, buf, size);
2132
2133 len -= size;
2134 buf += size;
2135 offset += size;
2136 } while (len);
2137
2138 handle->offset = offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01002139
Peter Zijlstra53020fe2009-05-13 21:26:19 +02002140 /*
2141 * Check we didn't copy past our reservation window, taking the
2142 * possible unsigned int wrap into account.
2143 */
2144 WARN_ON_ONCE(((int)(handle->head - handle->offset)) < 0);
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002145}
2146
Peter Zijlstra5c148192009-03-25 12:30:23 +01002147#define perf_output_put(handle, x) \
2148 perf_output_copy((handle), &(x), sizeof(x))
2149
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002150static void perf_output_end(struct perf_output_handle *handle)
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002151{
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002152 struct perf_counter *counter = handle->counter;
2153 struct perf_mmap_data *data = handle->data;
2154
2155 int wakeup_events = counter->hw_event.wakeup_events;
Peter Zijlstrac4578102009-04-02 11:12:01 +02002156
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002157 if (handle->overflow && wakeup_events) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002158 int events = atomic_inc_return(&data->events);
Peter Zijlstrac4578102009-04-02 11:12:01 +02002159 if (events >= wakeup_events) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002160 atomic_sub(wakeup_events, &data->events);
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002161 atomic_set(&data->wakeup, 1);
Peter Zijlstrac4578102009-04-02 11:12:01 +02002162 }
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002163 }
2164
2165 perf_output_unlock(handle);
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002166 rcu_read_unlock();
2167}
2168
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002169static void perf_counter_output(struct perf_counter *counter,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002170 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002171{
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002172 int ret;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002173 u64 record_type = counter->hw_event.record_type;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002174 struct perf_output_handle handle;
2175 struct perf_event_header header;
2176 u64 ip;
Peter Zijlstra5c148192009-03-25 12:30:23 +01002177 struct {
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002178 u32 pid, tid;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002179 } tid_entry;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002180 struct {
2181 u64 event;
2182 u64 counter;
2183 } group_entry;
Peter Zijlstra394ee072009-03-30 19:07:14 +02002184 struct perf_callchain_entry *callchain = NULL;
2185 int callchain_size = 0;
Peter Zijlstra339f7c92009-04-06 11:45:06 +02002186 u64 time;
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002187 struct {
2188 u32 cpu, reserved;
2189 } cpu_entry;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002190
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02002191 header.type = 0;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002192 header.size = sizeof(header);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002193
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02002194 header.misc = PERF_EVENT_MISC_OVERFLOW;
Paul Mackerras9d23a902009-05-14 21:48:08 +10002195 header.misc |= perf_misc_flags(regs);
Peter Zijlstra6fab0192009-04-08 15:01:26 +02002196
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002197 if (record_type & PERF_RECORD_IP) {
Paul Mackerras9d23a902009-05-14 21:48:08 +10002198 ip = perf_instruction_pointer(regs);
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02002199 header.type |= PERF_RECORD_IP;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002200 header.size += sizeof(ip);
2201 }
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002202
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002203 if (record_type & PERF_RECORD_TID) {
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002204 /* namespace issues */
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002205 tid_entry.pid = current->group_leader->pid;
2206 tid_entry.tid = current->pid;
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002207
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02002208 header.type |= PERF_RECORD_TID;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002209 header.size += sizeof(tid_entry);
2210 }
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002211
Peter Zijlstra4d855452009-04-08 15:01:32 +02002212 if (record_type & PERF_RECORD_TIME) {
2213 /*
2214 * Maybe do better on x86 and provide cpu_clock_nmi()
2215 */
2216 time = sched_clock();
2217
2218 header.type |= PERF_RECORD_TIME;
2219 header.size += sizeof(u64);
2220 }
2221
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002222 if (record_type & PERF_RECORD_ADDR) {
2223 header.type |= PERF_RECORD_ADDR;
2224 header.size += sizeof(u64);
2225 }
2226
Peter Zijlstraa85f61a2009-05-08 18:52:23 +02002227 if (record_type & PERF_RECORD_CONFIG) {
2228 header.type |= PERF_RECORD_CONFIG;
2229 header.size += sizeof(u64);
2230 }
2231
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002232 if (record_type & PERF_RECORD_CPU) {
2233 header.type |= PERF_RECORD_CPU;
2234 header.size += sizeof(cpu_entry);
2235
2236 cpu_entry.cpu = raw_smp_processor_id();
2237 }
2238
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002239 if (record_type & PERF_RECORD_GROUP) {
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02002240 header.type |= PERF_RECORD_GROUP;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002241 header.size += sizeof(u64) +
2242 counter->nr_siblings * sizeof(group_entry);
2243 }
2244
2245 if (record_type & PERF_RECORD_CALLCHAIN) {
Peter Zijlstra394ee072009-03-30 19:07:14 +02002246 callchain = perf_callchain(regs);
2247
2248 if (callchain) {
Peter Zijlstra9c03d882009-04-06 11:45:00 +02002249 callchain_size = (1 + callchain->nr) * sizeof(u64);
Peter Zijlstra394ee072009-03-30 19:07:14 +02002250
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02002251 header.type |= PERF_RECORD_CALLCHAIN;
Peter Zijlstra394ee072009-03-30 19:07:14 +02002252 header.size += callchain_size;
2253 }
2254 }
2255
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002256 ret = perf_output_begin(&handle, counter, header.size, nmi, 1);
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002257 if (ret)
2258 return;
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002259
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002260 perf_output_put(&handle, header);
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002261
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002262 if (record_type & PERF_RECORD_IP)
2263 perf_output_put(&handle, ip);
2264
2265 if (record_type & PERF_RECORD_TID)
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002266 perf_output_put(&handle, tid_entry);
2267
Peter Zijlstra4d855452009-04-08 15:01:32 +02002268 if (record_type & PERF_RECORD_TIME)
2269 perf_output_put(&handle, time);
2270
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002271 if (record_type & PERF_RECORD_ADDR)
2272 perf_output_put(&handle, addr);
2273
Peter Zijlstraa85f61a2009-05-08 18:52:23 +02002274 if (record_type & PERF_RECORD_CONFIG)
2275 perf_output_put(&handle, counter->hw_event.config);
2276
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002277 if (record_type & PERF_RECORD_CPU)
2278 perf_output_put(&handle, cpu_entry);
2279
Peter Zijlstra2023b352009-05-05 17:50:26 +02002280 /*
2281 * XXX PERF_RECORD_GROUP vs inherited counters seems difficult.
2282 */
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002283 if (record_type & PERF_RECORD_GROUP) {
2284 struct perf_counter *leader, *sub;
2285 u64 nr = counter->nr_siblings;
2286
2287 perf_output_put(&handle, nr);
2288
2289 leader = counter->group_leader;
2290 list_for_each_entry(sub, &leader->sibling_list, list_entry) {
2291 if (sub != counter)
Robert Richter4aeb0b42009-04-29 12:47:03 +02002292 sub->pmu->read(sub);
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002293
2294 group_entry.event = sub->hw_event.config;
2295 group_entry.counter = atomic64_read(&sub->count);
2296
2297 perf_output_put(&handle, group_entry);
2298 }
2299 }
2300
Peter Zijlstra394ee072009-03-30 19:07:14 +02002301 if (callchain)
2302 perf_output_copy(&handle, callchain, callchain_size);
2303
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002304 perf_output_end(&handle);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002305}
2306
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002307/*
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002308 * comm tracking
2309 */
2310
2311struct perf_comm_event {
2312 struct task_struct *task;
2313 char *comm;
2314 int comm_size;
2315
2316 struct {
2317 struct perf_event_header header;
2318
2319 u32 pid;
2320 u32 tid;
2321 } event;
2322};
2323
2324static void perf_counter_comm_output(struct perf_counter *counter,
2325 struct perf_comm_event *comm_event)
2326{
2327 struct perf_output_handle handle;
2328 int size = comm_event->event.header.size;
2329 int ret = perf_output_begin(&handle, counter, size, 0, 0);
2330
2331 if (ret)
2332 return;
2333
2334 perf_output_put(&handle, comm_event->event);
2335 perf_output_copy(&handle, comm_event->comm,
2336 comm_event->comm_size);
2337 perf_output_end(&handle);
2338}
2339
2340static int perf_counter_comm_match(struct perf_counter *counter,
2341 struct perf_comm_event *comm_event)
2342{
2343 if (counter->hw_event.comm &&
2344 comm_event->event.header.type == PERF_EVENT_COMM)
2345 return 1;
2346
2347 return 0;
2348}
2349
2350static void perf_counter_comm_ctx(struct perf_counter_context *ctx,
2351 struct perf_comm_event *comm_event)
2352{
2353 struct perf_counter *counter;
2354
2355 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2356 return;
2357
2358 rcu_read_lock();
2359 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2360 if (perf_counter_comm_match(counter, comm_event))
2361 perf_counter_comm_output(counter, comm_event);
2362 }
2363 rcu_read_unlock();
2364}
2365
2366static void perf_counter_comm_event(struct perf_comm_event *comm_event)
2367{
2368 struct perf_cpu_context *cpuctx;
2369 unsigned int size;
2370 char *comm = comm_event->task->comm;
2371
Ingo Molnar888fcee2009-04-09 09:48:22 +02002372 size = ALIGN(strlen(comm)+1, sizeof(u64));
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002373
2374 comm_event->comm = comm;
2375 comm_event->comm_size = size;
2376
2377 comm_event->event.header.size = sizeof(comm_event->event) + size;
2378
2379 cpuctx = &get_cpu_var(perf_cpu_context);
2380 perf_counter_comm_ctx(&cpuctx->ctx, comm_event);
2381 put_cpu_var(perf_cpu_context);
2382
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10002383 perf_counter_comm_ctx(current->perf_counter_ctxp, comm_event);
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002384}
2385
2386void perf_counter_comm(struct task_struct *task)
2387{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002388 struct perf_comm_event comm_event;
2389
2390 if (!atomic_read(&nr_comm_tracking))
2391 return;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10002392 if (!current->perf_counter_ctxp)
2393 return;
2394
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002395 comm_event = (struct perf_comm_event){
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002396 .task = task,
2397 .event = {
2398 .header = { .type = PERF_EVENT_COMM, },
2399 .pid = task->group_leader->pid,
2400 .tid = task->pid,
2401 },
2402 };
2403
2404 perf_counter_comm_event(&comm_event);
2405}
2406
2407/*
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002408 * mmap tracking
2409 */
2410
2411struct perf_mmap_event {
2412 struct file *file;
2413 char *file_name;
2414 int file_size;
2415
2416 struct {
2417 struct perf_event_header header;
2418
2419 u32 pid;
2420 u32 tid;
2421 u64 start;
2422 u64 len;
2423 u64 pgoff;
2424 } event;
2425};
2426
2427static void perf_counter_mmap_output(struct perf_counter *counter,
2428 struct perf_mmap_event *mmap_event)
2429{
2430 struct perf_output_handle handle;
2431 int size = mmap_event->event.header.size;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002432 int ret = perf_output_begin(&handle, counter, size, 0, 0);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002433
2434 if (ret)
2435 return;
2436
2437 perf_output_put(&handle, mmap_event->event);
2438 perf_output_copy(&handle, mmap_event->file_name,
2439 mmap_event->file_size);
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002440 perf_output_end(&handle);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002441}
2442
2443static int perf_counter_mmap_match(struct perf_counter *counter,
2444 struct perf_mmap_event *mmap_event)
2445{
2446 if (counter->hw_event.mmap &&
2447 mmap_event->event.header.type == PERF_EVENT_MMAP)
2448 return 1;
2449
2450 if (counter->hw_event.munmap &&
2451 mmap_event->event.header.type == PERF_EVENT_MUNMAP)
2452 return 1;
2453
2454 return 0;
2455}
2456
2457static void perf_counter_mmap_ctx(struct perf_counter_context *ctx,
2458 struct perf_mmap_event *mmap_event)
2459{
2460 struct perf_counter *counter;
2461
2462 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2463 return;
2464
2465 rcu_read_lock();
2466 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2467 if (perf_counter_mmap_match(counter, mmap_event))
2468 perf_counter_mmap_output(counter, mmap_event);
2469 }
2470 rcu_read_unlock();
2471}
2472
2473static void perf_counter_mmap_event(struct perf_mmap_event *mmap_event)
2474{
2475 struct perf_cpu_context *cpuctx;
2476 struct file *file = mmap_event->file;
2477 unsigned int size;
2478 char tmp[16];
2479 char *buf = NULL;
2480 char *name;
2481
2482 if (file) {
2483 buf = kzalloc(PATH_MAX, GFP_KERNEL);
2484 if (!buf) {
2485 name = strncpy(tmp, "//enomem", sizeof(tmp));
2486 goto got_name;
2487 }
Peter Zijlstrad3d21c42009-04-09 10:53:46 +02002488 name = d_path(&file->f_path, buf, PATH_MAX);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002489 if (IS_ERR(name)) {
2490 name = strncpy(tmp, "//toolong", sizeof(tmp));
2491 goto got_name;
2492 }
2493 } else {
2494 name = strncpy(tmp, "//anon", sizeof(tmp));
2495 goto got_name;
2496 }
2497
2498got_name:
Ingo Molnar888fcee2009-04-09 09:48:22 +02002499 size = ALIGN(strlen(name)+1, sizeof(u64));
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002500
2501 mmap_event->file_name = name;
2502 mmap_event->file_size = size;
2503
2504 mmap_event->event.header.size = sizeof(mmap_event->event) + size;
2505
2506 cpuctx = &get_cpu_var(perf_cpu_context);
2507 perf_counter_mmap_ctx(&cpuctx->ctx, mmap_event);
2508 put_cpu_var(perf_cpu_context);
2509
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10002510 perf_counter_mmap_ctx(current->perf_counter_ctxp, mmap_event);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002511
2512 kfree(buf);
2513}
2514
2515void perf_counter_mmap(unsigned long addr, unsigned long len,
2516 unsigned long pgoff, struct file *file)
2517{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002518 struct perf_mmap_event mmap_event;
2519
2520 if (!atomic_read(&nr_mmap_tracking))
2521 return;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10002522 if (!current->perf_counter_ctxp)
2523 return;
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002524
2525 mmap_event = (struct perf_mmap_event){
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002526 .file = file,
2527 .event = {
2528 .header = { .type = PERF_EVENT_MMAP, },
2529 .pid = current->group_leader->pid,
2530 .tid = current->pid,
2531 .start = addr,
2532 .len = len,
2533 .pgoff = pgoff,
2534 },
2535 };
2536
2537 perf_counter_mmap_event(&mmap_event);
2538}
2539
2540void perf_counter_munmap(unsigned long addr, unsigned long len,
2541 unsigned long pgoff, struct file *file)
2542{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002543 struct perf_mmap_event mmap_event;
2544
2545 if (!atomic_read(&nr_munmap_tracking))
2546 return;
2547
2548 mmap_event = (struct perf_mmap_event){
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002549 .file = file,
2550 .event = {
2551 .header = { .type = PERF_EVENT_MUNMAP, },
2552 .pid = current->group_leader->pid,
2553 .tid = current->pid,
2554 .start = addr,
2555 .len = len,
2556 .pgoff = pgoff,
2557 },
2558 };
2559
2560 perf_counter_mmap_event(&mmap_event);
2561}
2562
2563/*
Peter Zijlstrae220d2d2009-05-23 18:28:55 +02002564 * Log irq_period changes so that analyzing tools can re-normalize the
2565 * event flow.
Peter Zijlstra26b119b2009-05-20 12:21:20 +02002566 */
2567
2568static void perf_log_period(struct perf_counter *counter, u64 period)
2569{
2570 struct perf_output_handle handle;
2571 int ret;
2572
2573 struct {
2574 struct perf_event_header header;
2575 u64 time;
2576 u64 period;
2577 } freq_event = {
2578 .header = {
2579 .type = PERF_EVENT_PERIOD,
2580 .misc = 0,
2581 .size = sizeof(freq_event),
2582 },
2583 .time = sched_clock(),
2584 .period = period,
2585 };
2586
2587 if (counter->hw.irq_period == period)
2588 return;
2589
2590 ret = perf_output_begin(&handle, counter, sizeof(freq_event), 0, 0);
2591 if (ret)
2592 return;
2593
2594 perf_output_put(&handle, freq_event);
2595 perf_output_end(&handle);
2596}
2597
2598/*
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002599 * Generic counter overflow handling.
2600 */
2601
2602int perf_counter_overflow(struct perf_counter *counter,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002603 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002604{
Peter Zijlstra79f14642009-04-06 11:45:07 +02002605 int events = atomic_read(&counter->event_limit);
2606 int ret = 0;
2607
Peter Zijlstra60db5e02009-05-15 15:19:28 +02002608 counter->hw.interrupts++;
2609
Peter Zijlstra2023b352009-05-05 17:50:26 +02002610 /*
2611 * XXX event_limit might not quite work as expected on inherited
2612 * counters
2613 */
2614
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002615 counter->pending_kill = POLL_IN;
Peter Zijlstra79f14642009-04-06 11:45:07 +02002616 if (events && atomic_dec_and_test(&counter->event_limit)) {
2617 ret = 1;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002618 counter->pending_kill = POLL_HUP;
Peter Zijlstra79f14642009-04-06 11:45:07 +02002619 if (nmi) {
2620 counter->pending_disable = 1;
2621 perf_pending_queue(&counter->pending,
2622 perf_pending_counter);
2623 } else
2624 perf_counter_disable(counter);
2625 }
2626
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002627 perf_counter_output(counter, nmi, regs, addr);
Peter Zijlstra79f14642009-04-06 11:45:07 +02002628 return ret;
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002629}
2630
2631/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002632 * Generic software counter infrastructure
2633 */
2634
2635static void perf_swcounter_update(struct perf_counter *counter)
2636{
2637 struct hw_perf_counter *hwc = &counter->hw;
2638 u64 prev, now;
2639 s64 delta;
2640
2641again:
2642 prev = atomic64_read(&hwc->prev_count);
2643 now = atomic64_read(&hwc->count);
2644 if (atomic64_cmpxchg(&hwc->prev_count, prev, now) != prev)
2645 goto again;
2646
2647 delta = now - prev;
2648
2649 atomic64_add(delta, &counter->count);
2650 atomic64_sub(delta, &hwc->period_left);
2651}
2652
2653static void perf_swcounter_set_period(struct perf_counter *counter)
2654{
2655 struct hw_perf_counter *hwc = &counter->hw;
2656 s64 left = atomic64_read(&hwc->period_left);
2657 s64 period = hwc->irq_period;
2658
2659 if (unlikely(left <= -period)) {
2660 left = period;
2661 atomic64_set(&hwc->period_left, left);
2662 }
2663
2664 if (unlikely(left <= 0)) {
2665 left += period;
2666 atomic64_add(period, &hwc->period_left);
2667 }
2668
2669 atomic64_set(&hwc->prev_count, -left);
2670 atomic64_set(&hwc->count, -left);
2671}
2672
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002673static enum hrtimer_restart perf_swcounter_hrtimer(struct hrtimer *hrtimer)
2674{
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002675 enum hrtimer_restart ret = HRTIMER_RESTART;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002676 struct perf_counter *counter;
2677 struct pt_regs *regs;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02002678 u64 period;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002679
2680 counter = container_of(hrtimer, struct perf_counter, hw.hrtimer);
Robert Richter4aeb0b42009-04-29 12:47:03 +02002681 counter->pmu->read(counter);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002682
2683 regs = get_irq_regs();
2684 /*
2685 * In case we exclude kernel IPs or are somehow not in interrupt
2686 * context, provide the next best thing, the user IP.
2687 */
2688 if ((counter->hw_event.exclude_kernel || !regs) &&
2689 !counter->hw_event.exclude_user)
2690 regs = task_pt_regs(current);
2691
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002692 if (regs) {
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002693 if (perf_counter_overflow(counter, 0, regs, 0))
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002694 ret = HRTIMER_NORESTART;
2695 }
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002696
Peter Zijlstra60db5e02009-05-15 15:19:28 +02002697 period = max_t(u64, 10000, counter->hw.irq_period);
2698 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002699
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002700 return ret;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002701}
2702
2703static void perf_swcounter_overflow(struct perf_counter *counter,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002704 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002705{
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002706 perf_swcounter_update(counter);
2707 perf_swcounter_set_period(counter);
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002708 if (perf_counter_overflow(counter, nmi, regs, addr))
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002709 /* soft-disable the counter */
2710 ;
2711
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002712}
2713
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002714static int perf_swcounter_match(struct perf_counter *counter,
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002715 enum perf_event_types type,
2716 u32 event, struct pt_regs *regs)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002717{
2718 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
2719 return 0;
2720
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002721 if (perf_event_raw(&counter->hw_event))
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002722 return 0;
2723
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002724 if (perf_event_type(&counter->hw_event) != type)
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002725 return 0;
2726
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002727 if (perf_event_id(&counter->hw_event) != event)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002728 return 0;
2729
2730 if (counter->hw_event.exclude_user && user_mode(regs))
2731 return 0;
2732
2733 if (counter->hw_event.exclude_kernel && !user_mode(regs))
2734 return 0;
2735
2736 return 1;
2737}
2738
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002739static void perf_swcounter_add(struct perf_counter *counter, u64 nr,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002740 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002741{
2742 int neg = atomic64_add_negative(nr, &counter->hw.count);
2743 if (counter->hw.irq_period && !neg)
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002744 perf_swcounter_overflow(counter, nmi, regs, addr);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002745}
2746
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002747static void perf_swcounter_ctx_event(struct perf_counter_context *ctx,
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002748 enum perf_event_types type, u32 event,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002749 u64 nr, int nmi, struct pt_regs *regs,
2750 u64 addr)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002751{
2752 struct perf_counter *counter;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002753
Peter Zijlstra01ef09d2009-03-19 20:26:11 +01002754 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002755 return;
2756
Peter Zijlstra592903c2009-03-13 12:21:36 +01002757 rcu_read_lock();
2758 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002759 if (perf_swcounter_match(counter, type, event, regs))
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002760 perf_swcounter_add(counter, nr, nmi, regs, addr);
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002761 }
Peter Zijlstra592903c2009-03-13 12:21:36 +01002762 rcu_read_unlock();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002763}
2764
Peter Zijlstra96f6d442009-03-23 18:22:07 +01002765static int *perf_swcounter_recursion_context(struct perf_cpu_context *cpuctx)
2766{
2767 if (in_nmi())
2768 return &cpuctx->recursion[3];
2769
2770 if (in_irq())
2771 return &cpuctx->recursion[2];
2772
2773 if (in_softirq())
2774 return &cpuctx->recursion[1];
2775
2776 return &cpuctx->recursion[0];
2777}
2778
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002779static void __perf_swcounter_event(enum perf_event_types type, u32 event,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002780 u64 nr, int nmi, struct pt_regs *regs,
2781 u64 addr)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002782{
2783 struct perf_cpu_context *cpuctx = &get_cpu_var(perf_cpu_context);
Peter Zijlstra96f6d442009-03-23 18:22:07 +01002784 int *recursion = perf_swcounter_recursion_context(cpuctx);
2785
2786 if (*recursion)
2787 goto out;
2788
2789 (*recursion)++;
2790 barrier();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002791
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002792 perf_swcounter_ctx_event(&cpuctx->ctx, type, event,
2793 nr, nmi, regs, addr);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002794 if (cpuctx->task_ctx) {
2795 perf_swcounter_ctx_event(cpuctx->task_ctx, type, event,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002796 nr, nmi, regs, addr);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002797 }
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002798
Peter Zijlstra96f6d442009-03-23 18:22:07 +01002799 barrier();
2800 (*recursion)--;
2801
2802out:
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002803 put_cpu_var(perf_cpu_context);
2804}
2805
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002806void
2807perf_swcounter_event(u32 event, u64 nr, int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002808{
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002809 __perf_swcounter_event(PERF_TYPE_SOFTWARE, event, nr, nmi, regs, addr);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002810}
2811
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002812static void perf_swcounter_read(struct perf_counter *counter)
2813{
2814 perf_swcounter_update(counter);
2815}
2816
2817static int perf_swcounter_enable(struct perf_counter *counter)
2818{
2819 perf_swcounter_set_period(counter);
2820 return 0;
2821}
2822
2823static void perf_swcounter_disable(struct perf_counter *counter)
2824{
2825 perf_swcounter_update(counter);
2826}
2827
Robert Richter4aeb0b42009-04-29 12:47:03 +02002828static const struct pmu perf_ops_generic = {
Peter Zijlstraac17dc82009-03-13 12:21:34 +01002829 .enable = perf_swcounter_enable,
2830 .disable = perf_swcounter_disable,
2831 .read = perf_swcounter_read,
2832};
2833
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002834/*
2835 * Software counter: cpu wall time clock
2836 */
2837
Paul Mackerras9abf8a02009-01-09 16:26:43 +11002838static void cpu_clock_perf_counter_update(struct perf_counter *counter)
2839{
2840 int cpu = raw_smp_processor_id();
2841 s64 prev;
2842 u64 now;
2843
2844 now = cpu_clock(cpu);
2845 prev = atomic64_read(&counter->hw.prev_count);
2846 atomic64_set(&counter->hw.prev_count, now);
2847 atomic64_add(now - prev, &counter->count);
2848}
2849
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002850static int cpu_clock_perf_counter_enable(struct perf_counter *counter)
2851{
2852 struct hw_perf_counter *hwc = &counter->hw;
2853 int cpu = raw_smp_processor_id();
2854
2855 atomic64_set(&hwc->prev_count, cpu_clock(cpu));
Peter Zijlstra039fc912009-03-13 16:43:47 +01002856 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2857 hwc->hrtimer.function = perf_swcounter_hrtimer;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002858 if (hwc->irq_period) {
Peter Zijlstra60db5e02009-05-15 15:19:28 +02002859 u64 period = max_t(u64, 10000, hwc->irq_period);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002860 __hrtimer_start_range_ns(&hwc->hrtimer,
Peter Zijlstra60db5e02009-05-15 15:19:28 +02002861 ns_to_ktime(period), 0,
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002862 HRTIMER_MODE_REL, 0);
2863 }
2864
2865 return 0;
2866}
2867
Ingo Molnar5c92d122008-12-11 13:21:10 +01002868static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
2869{
Peter Zijlstrab986d7e2009-05-20 12:21:21 +02002870 if (counter->hw.irq_period)
2871 hrtimer_cancel(&counter->hw.hrtimer);
Paul Mackerras9abf8a02009-01-09 16:26:43 +11002872 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01002873}
2874
2875static void cpu_clock_perf_counter_read(struct perf_counter *counter)
2876{
Paul Mackerras9abf8a02009-01-09 16:26:43 +11002877 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01002878}
2879
Robert Richter4aeb0b42009-04-29 12:47:03 +02002880static const struct pmu perf_ops_cpu_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +01002881 .enable = cpu_clock_perf_counter_enable,
2882 .disable = cpu_clock_perf_counter_disable,
2883 .read = cpu_clock_perf_counter_read,
Ingo Molnar5c92d122008-12-11 13:21:10 +01002884};
2885
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01002886/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002887 * Software counter: task time clock
2888 */
2889
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02002890static void task_clock_perf_counter_update(struct perf_counter *counter, u64 now)
Ingo Molnarbae43c92008-12-11 14:03:20 +01002891{
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02002892 u64 prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002893 s64 delta;
Ingo Molnarbae43c92008-12-11 14:03:20 +01002894
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02002895 prev = atomic64_xchg(&counter->hw.prev_count, now);
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002896 delta = now - prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002897 atomic64_add(delta, &counter->count);
Ingo Molnarbae43c92008-12-11 14:03:20 +01002898}
2899
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01002900static int task_clock_perf_counter_enable(struct perf_counter *counter)
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002901{
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002902 struct hw_perf_counter *hwc = &counter->hw;
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02002903 u64 now;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002904
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02002905 now = counter->ctx->time;
2906
2907 atomic64_set(&hwc->prev_count, now);
Peter Zijlstra039fc912009-03-13 16:43:47 +01002908 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2909 hwc->hrtimer.function = perf_swcounter_hrtimer;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002910 if (hwc->irq_period) {
Peter Zijlstra60db5e02009-05-15 15:19:28 +02002911 u64 period = max_t(u64, 10000, hwc->irq_period);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002912 __hrtimer_start_range_ns(&hwc->hrtimer,
Peter Zijlstra60db5e02009-05-15 15:19:28 +02002913 ns_to_ktime(period), 0,
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002914 HRTIMER_MODE_REL, 0);
2915 }
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01002916
2917 return 0;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002918}
2919
2920static void task_clock_perf_counter_disable(struct perf_counter *counter)
2921{
Peter Zijlstrab986d7e2009-05-20 12:21:21 +02002922 if (counter->hw.irq_period)
2923 hrtimer_cancel(&counter->hw.hrtimer);
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02002924 task_clock_perf_counter_update(counter, counter->ctx->time);
2925
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002926}
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01002927
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002928static void task_clock_perf_counter_read(struct perf_counter *counter)
2929{
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02002930 u64 time;
2931
2932 if (!in_nmi()) {
2933 update_context_time(counter->ctx);
2934 time = counter->ctx->time;
2935 } else {
2936 u64 now = perf_clock();
2937 u64 delta = now - counter->ctx->timestamp;
2938 time = counter->ctx->time + delta;
2939 }
2940
2941 task_clock_perf_counter_update(counter, time);
Ingo Molnarbae43c92008-12-11 14:03:20 +01002942}
2943
Robert Richter4aeb0b42009-04-29 12:47:03 +02002944static const struct pmu perf_ops_task_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +01002945 .enable = task_clock_perf_counter_enable,
2946 .disable = task_clock_perf_counter_disable,
2947 .read = task_clock_perf_counter_read,
Ingo Molnarbae43c92008-12-11 14:03:20 +01002948};
2949
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002950/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002951 * Software counter: cpu migrations
2952 */
2953
Paul Mackerras23a185c2009-02-09 22:42:47 +11002954static inline u64 get_cpu_migrations(struct perf_counter *counter)
Ingo Molnar6c594c22008-12-14 12:34:15 +01002955{
Paul Mackerras23a185c2009-02-09 22:42:47 +11002956 struct task_struct *curr = counter->ctx->task;
2957
2958 if (curr)
2959 return curr->se.nr_migrations;
2960 return cpu_nr_migrations(smp_processor_id());
Ingo Molnar6c594c22008-12-14 12:34:15 +01002961}
2962
2963static void cpu_migrations_perf_counter_update(struct perf_counter *counter)
2964{
2965 u64 prev, now;
2966 s64 delta;
2967
2968 prev = atomic64_read(&counter->hw.prev_count);
Paul Mackerras23a185c2009-02-09 22:42:47 +11002969 now = get_cpu_migrations(counter);
Ingo Molnar6c594c22008-12-14 12:34:15 +01002970
2971 atomic64_set(&counter->hw.prev_count, now);
2972
2973 delta = now - prev;
Ingo Molnar6c594c22008-12-14 12:34:15 +01002974
2975 atomic64_add(delta, &counter->count);
2976}
2977
2978static void cpu_migrations_perf_counter_read(struct perf_counter *counter)
2979{
2980 cpu_migrations_perf_counter_update(counter);
2981}
2982
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01002983static int cpu_migrations_perf_counter_enable(struct perf_counter *counter)
Ingo Molnar6c594c22008-12-14 12:34:15 +01002984{
Paul Mackerrasc07c99b2009-02-13 22:10:34 +11002985 if (counter->prev_state <= PERF_COUNTER_STATE_OFF)
2986 atomic64_set(&counter->hw.prev_count,
2987 get_cpu_migrations(counter));
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01002988 return 0;
Ingo Molnar6c594c22008-12-14 12:34:15 +01002989}
2990
2991static void cpu_migrations_perf_counter_disable(struct perf_counter *counter)
2992{
2993 cpu_migrations_perf_counter_update(counter);
2994}
2995
Robert Richter4aeb0b42009-04-29 12:47:03 +02002996static const struct pmu perf_ops_cpu_migrations = {
Ingo Molnar76715812008-12-17 14:20:28 +01002997 .enable = cpu_migrations_perf_counter_enable,
2998 .disable = cpu_migrations_perf_counter_disable,
2999 .read = cpu_migrations_perf_counter_read,
Ingo Molnar6c594c22008-12-14 12:34:15 +01003000};
3001
Peter Zijlstrae077df42009-03-19 20:26:17 +01003002#ifdef CONFIG_EVENT_PROFILE
3003void perf_tpcounter_event(int event_id)
3004{
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003005 struct pt_regs *regs = get_irq_regs();
3006
3007 if (!regs)
3008 regs = task_pt_regs(current);
3009
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003010 __perf_swcounter_event(PERF_TYPE_TRACEPOINT, event_id, 1, 1, regs, 0);
Peter Zijlstrae077df42009-03-19 20:26:17 +01003011}
Steven Whitehouseff7b1b42009-04-15 16:55:05 +01003012EXPORT_SYMBOL_GPL(perf_tpcounter_event);
Peter Zijlstrae077df42009-03-19 20:26:17 +01003013
3014extern int ftrace_profile_enable(int);
3015extern void ftrace_profile_disable(int);
3016
3017static void tp_perf_counter_destroy(struct perf_counter *counter)
3018{
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01003019 ftrace_profile_disable(perf_event_id(&counter->hw_event));
Peter Zijlstrae077df42009-03-19 20:26:17 +01003020}
3021
Robert Richter4aeb0b42009-04-29 12:47:03 +02003022static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
Peter Zijlstrae077df42009-03-19 20:26:17 +01003023{
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01003024 int event_id = perf_event_id(&counter->hw_event);
Peter Zijlstrae077df42009-03-19 20:26:17 +01003025 int ret;
3026
3027 ret = ftrace_profile_enable(event_id);
3028 if (ret)
3029 return NULL;
3030
3031 counter->destroy = tp_perf_counter_destroy;
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003032 counter->hw.irq_period = counter->hw_event.irq_period;
Peter Zijlstrae077df42009-03-19 20:26:17 +01003033
3034 return &perf_ops_generic;
3035}
3036#else
Robert Richter4aeb0b42009-04-29 12:47:03 +02003037static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
Peter Zijlstrae077df42009-03-19 20:26:17 +01003038{
3039 return NULL;
3040}
3041#endif
3042
Robert Richter4aeb0b42009-04-29 12:47:03 +02003043static const struct pmu *sw_perf_counter_init(struct perf_counter *counter)
Ingo Molnar5c92d122008-12-11 13:21:10 +01003044{
Robert Richter4aeb0b42009-04-29 12:47:03 +02003045 const struct pmu *pmu = NULL;
Ingo Molnar5c92d122008-12-11 13:21:10 +01003046
Paul Mackerras0475f9e2009-02-11 14:35:35 +11003047 /*
3048 * Software counters (currently) can't in general distinguish
3049 * between user, kernel and hypervisor events.
3050 * However, context switches and cpu migrations are considered
3051 * to be kernel events, and page faults are never hypervisor
3052 * events.
3053 */
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01003054 switch (perf_event_id(&counter->hw_event)) {
Ingo Molnar5c92d122008-12-11 13:21:10 +01003055 case PERF_COUNT_CPU_CLOCK:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003056 pmu = &perf_ops_cpu_clock;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003057
Ingo Molnar5c92d122008-12-11 13:21:10 +01003058 break;
Ingo Molnarbae43c92008-12-11 14:03:20 +01003059 case PERF_COUNT_TASK_CLOCK:
Paul Mackerras23a185c2009-02-09 22:42:47 +11003060 /*
3061 * If the user instantiates this as a per-cpu counter,
3062 * use the cpu_clock counter instead.
3063 */
3064 if (counter->ctx->task)
Robert Richter4aeb0b42009-04-29 12:47:03 +02003065 pmu = &perf_ops_task_clock;
Paul Mackerras23a185c2009-02-09 22:42:47 +11003066 else
Robert Richter4aeb0b42009-04-29 12:47:03 +02003067 pmu = &perf_ops_cpu_clock;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003068
Ingo Molnarbae43c92008-12-11 14:03:20 +01003069 break;
Ingo Molnare06c61a2008-12-14 14:44:31 +01003070 case PERF_COUNT_PAGE_FAULTS:
Peter Zijlstraac17dc82009-03-13 12:21:34 +01003071 case PERF_COUNT_PAGE_FAULTS_MIN:
3072 case PERF_COUNT_PAGE_FAULTS_MAJ:
Ingo Molnar5d6a27d2008-12-14 12:28:33 +01003073 case PERF_COUNT_CONTEXT_SWITCHES:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003074 pmu = &perf_ops_generic;
Ingo Molnar5d6a27d2008-12-14 12:28:33 +01003075 break;
Ingo Molnar6c594c22008-12-14 12:34:15 +01003076 case PERF_COUNT_CPU_MIGRATIONS:
Paul Mackerras0475f9e2009-02-11 14:35:35 +11003077 if (!counter->hw_event.exclude_kernel)
Robert Richter4aeb0b42009-04-29 12:47:03 +02003078 pmu = &perf_ops_cpu_migrations;
Ingo Molnar6c594c22008-12-14 12:34:15 +01003079 break;
Ingo Molnar5c92d122008-12-11 13:21:10 +01003080 }
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003081
Robert Richter4aeb0b42009-04-29 12:47:03 +02003082 return pmu;
Ingo Molnar5c92d122008-12-11 13:21:10 +01003083}
3084
Thomas Gleixner0793a612008-12-04 20:12:29 +01003085/*
3086 * Allocate and initialize a counter structure
3087 */
3088static struct perf_counter *
Ingo Molnar04289bb2008-12-11 08:38:42 +01003089perf_counter_alloc(struct perf_counter_hw_event *hw_event,
3090 int cpu,
Paul Mackerras23a185c2009-02-09 22:42:47 +11003091 struct perf_counter_context *ctx,
Ingo Molnar9b51f662008-12-12 13:49:45 +01003092 struct perf_counter *group_leader,
3093 gfp_t gfpflags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003094{
Robert Richter4aeb0b42009-04-29 12:47:03 +02003095 const struct pmu *pmu;
Ingo Molnar621a01e2008-12-11 12:46:46 +01003096 struct perf_counter *counter;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003097 struct hw_perf_counter *hwc;
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003098 long err;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003099
Ingo Molnar9b51f662008-12-12 13:49:45 +01003100 counter = kzalloc(sizeof(*counter), gfpflags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003101 if (!counter)
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003102 return ERR_PTR(-ENOMEM);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003103
Ingo Molnar04289bb2008-12-11 08:38:42 +01003104 /*
3105 * Single counters are their own group leaders, with an
3106 * empty sibling list:
3107 */
3108 if (!group_leader)
3109 group_leader = counter;
3110
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003111 mutex_init(&counter->child_mutex);
3112 INIT_LIST_HEAD(&counter->child_list);
3113
Ingo Molnar04289bb2008-12-11 08:38:42 +01003114 INIT_LIST_HEAD(&counter->list_entry);
Peter Zijlstra592903c2009-03-13 12:21:36 +01003115 INIT_LIST_HEAD(&counter->event_entry);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003116 INIT_LIST_HEAD(&counter->sibling_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003117 init_waitqueue_head(&counter->waitq);
3118
Peter Zijlstra7b732a72009-03-23 18:22:10 +01003119 mutex_init(&counter->mmap_mutex);
3120
Ingo Molnar9f66a382008-12-10 12:33:23 +01003121 counter->cpu = cpu;
3122 counter->hw_event = *hw_event;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003123 counter->group_leader = group_leader;
Robert Richter4aeb0b42009-04-29 12:47:03 +02003124 counter->pmu = NULL;
Paul Mackerras23a185c2009-02-09 22:42:47 +11003125 counter->ctx = ctx;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003126 get_ctx(ctx);
Ingo Molnar621a01e2008-12-11 12:46:46 +01003127
Ingo Molnar235c7fc2008-12-21 14:43:25 +01003128 counter->state = PERF_COUNTER_STATE_INACTIVE;
Ingo Molnara86ed502008-12-17 00:43:10 +01003129 if (hw_event->disabled)
3130 counter->state = PERF_COUNTER_STATE_OFF;
3131
Robert Richter4aeb0b42009-04-29 12:47:03 +02003132 pmu = NULL;
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003133
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003134 hwc = &counter->hw;
3135 if (hw_event->freq && hw_event->irq_freq)
Peter Zijlstra2e569d32009-05-15 15:37:47 +02003136 hwc->irq_period = div64_u64(TICK_NSEC, hw_event->irq_freq);
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003137 else
3138 hwc->irq_period = hw_event->irq_period;
3139
Peter Zijlstra2023b352009-05-05 17:50:26 +02003140 /*
3141 * we currently do not support PERF_RECORD_GROUP on inherited counters
3142 */
3143 if (hw_event->inherit && (hw_event->record_type & PERF_RECORD_GROUP))
3144 goto done;
3145
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01003146 if (perf_event_raw(hw_event)) {
Robert Richter4aeb0b42009-04-29 12:47:03 +02003147 pmu = hw_perf_counter_init(counter);
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01003148 goto done;
3149 }
3150
3151 switch (perf_event_type(hw_event)) {
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003152 case PERF_TYPE_HARDWARE:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003153 pmu = hw_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003154 break;
3155
3156 case PERF_TYPE_SOFTWARE:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003157 pmu = sw_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003158 break;
3159
3160 case PERF_TYPE_TRACEPOINT:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003161 pmu = tp_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003162 break;
3163 }
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01003164done:
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003165 err = 0;
Robert Richter4aeb0b42009-04-29 12:47:03 +02003166 if (!pmu)
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003167 err = -EINVAL;
Robert Richter4aeb0b42009-04-29 12:47:03 +02003168 else if (IS_ERR(pmu))
3169 err = PTR_ERR(pmu);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003170
3171 if (err) {
3172 kfree(counter);
3173 return ERR_PTR(err);
3174 }
3175
Robert Richter4aeb0b42009-04-29 12:47:03 +02003176 counter->pmu = pmu;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003177
Peter Zijlstra7fc23a52009-05-08 18:52:21 +02003178 atomic_inc(&nr_counters);
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02003179 if (counter->hw_event.mmap)
3180 atomic_inc(&nr_mmap_tracking);
3181 if (counter->hw_event.munmap)
3182 atomic_inc(&nr_munmap_tracking);
3183 if (counter->hw_event.comm)
3184 atomic_inc(&nr_comm_tracking);
3185
Thomas Gleixner0793a612008-12-04 20:12:29 +01003186 return counter;
3187}
3188
3189/**
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003190 * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
Ingo Molnar9f66a382008-12-10 12:33:23 +01003191 *
3192 * @hw_event_uptr: event type attributes for monitoring/sampling
Thomas Gleixner0793a612008-12-04 20:12:29 +01003193 * @pid: target pid
Ingo Molnar9f66a382008-12-10 12:33:23 +01003194 * @cpu: target cpu
3195 * @group_fd: group leader counter fd
Thomas Gleixner0793a612008-12-04 20:12:29 +01003196 */
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003197SYSCALL_DEFINE5(perf_counter_open,
Paul Mackerrasf3dfd262009-02-26 22:43:46 +11003198 const struct perf_counter_hw_event __user *, hw_event_uptr,
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003199 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003200{
Ingo Molnar04289bb2008-12-11 08:38:42 +01003201 struct perf_counter *counter, *group_leader;
Ingo Molnar9f66a382008-12-10 12:33:23 +01003202 struct perf_counter_hw_event hw_event;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003203 struct perf_counter_context *ctx;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003204 struct file *counter_file = NULL;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003205 struct file *group_file = NULL;
3206 int fput_needed = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003207 int fput_needed2 = 0;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003208 int ret;
3209
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003210 /* for future expandability... */
3211 if (flags)
3212 return -EINVAL;
3213
Ingo Molnar9f66a382008-12-10 12:33:23 +01003214 if (copy_from_user(&hw_event, hw_event_uptr, sizeof(hw_event)) != 0)
Thomas Gleixnereab656a2008-12-08 19:26:59 +01003215 return -EFAULT;
3216
Ingo Molnar04289bb2008-12-11 08:38:42 +01003217 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01003218 * Get the target context (task or percpu):
3219 */
3220 ctx = find_get_context(pid, cpu);
3221 if (IS_ERR(ctx))
3222 return PTR_ERR(ctx);
3223
3224 /*
3225 * Look up the group leader (we will attach this counter to it):
Ingo Molnar04289bb2008-12-11 08:38:42 +01003226 */
3227 group_leader = NULL;
3228 if (group_fd != -1) {
3229 ret = -EINVAL;
3230 group_file = fget_light(group_fd, &fput_needed);
3231 if (!group_file)
Ingo Molnarccff2862008-12-11 11:26:29 +01003232 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003233 if (group_file->f_op != &perf_fops)
Ingo Molnarccff2862008-12-11 11:26:29 +01003234 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003235
3236 group_leader = group_file->private_data;
3237 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01003238 * Do not allow a recursive hierarchy (this new sibling
3239 * becoming part of another group-sibling):
Ingo Molnar04289bb2008-12-11 08:38:42 +01003240 */
Ingo Molnarccff2862008-12-11 11:26:29 +01003241 if (group_leader->group_leader != group_leader)
3242 goto err_put_context;
3243 /*
3244 * Do not allow to attach to a group in a different
3245 * task or CPU context:
3246 */
3247 if (group_leader->ctx != ctx)
3248 goto err_put_context;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11003249 /*
3250 * Only a group leader can be exclusive or pinned
3251 */
3252 if (hw_event.exclusive || hw_event.pinned)
3253 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003254 }
3255
Paul Mackerras23a185c2009-02-09 22:42:47 +11003256 counter = perf_counter_alloc(&hw_event, cpu, ctx, group_leader,
3257 GFP_KERNEL);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003258 ret = PTR_ERR(counter);
3259 if (IS_ERR(counter))
Thomas Gleixner0793a612008-12-04 20:12:29 +01003260 goto err_put_context;
3261
Thomas Gleixner0793a612008-12-04 20:12:29 +01003262 ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
3263 if (ret < 0)
Ingo Molnar9b51f662008-12-12 13:49:45 +01003264 goto err_free_put_context;
3265
3266 counter_file = fget_light(ret, &fput_needed2);
3267 if (!counter_file)
3268 goto err_free_put_context;
3269
3270 counter->filp = counter_file;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003271 mutex_lock(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003272 perf_install_in_context(ctx, counter, cpu);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003273 mutex_unlock(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003274
3275 fput_light(counter_file, fput_needed2);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003276
Ingo Molnar04289bb2008-12-11 08:38:42 +01003277out_fput:
3278 fput_light(group_file, fput_needed);
3279
Thomas Gleixner0793a612008-12-04 20:12:29 +01003280 return ret;
3281
Ingo Molnar9b51f662008-12-12 13:49:45 +01003282err_free_put_context:
Thomas Gleixner0793a612008-12-04 20:12:29 +01003283 kfree(counter);
3284
3285err_put_context:
3286 put_context(ctx);
3287
Ingo Molnar04289bb2008-12-11 08:38:42 +01003288 goto out_fput;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003289}
3290
Ingo Molnar9b51f662008-12-12 13:49:45 +01003291/*
Ingo Molnar9b51f662008-12-12 13:49:45 +01003292 * inherit a counter from parent task to child task:
3293 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11003294static struct perf_counter *
Ingo Molnar9b51f662008-12-12 13:49:45 +01003295inherit_counter(struct perf_counter *parent_counter,
3296 struct task_struct *parent,
3297 struct perf_counter_context *parent_ctx,
3298 struct task_struct *child,
Paul Mackerrasd859e292009-01-17 18:10:22 +11003299 struct perf_counter *group_leader,
Ingo Molnar9b51f662008-12-12 13:49:45 +01003300 struct perf_counter_context *child_ctx)
3301{
3302 struct perf_counter *child_counter;
3303
Paul Mackerrasd859e292009-01-17 18:10:22 +11003304 /*
3305 * Instead of creating recursive hierarchies of counters,
3306 * we link inherited counters back to the original parent,
3307 * which has a filp for sure, which we use as the reference
3308 * count:
3309 */
3310 if (parent_counter->parent)
3311 parent_counter = parent_counter->parent;
3312
Ingo Molnar9b51f662008-12-12 13:49:45 +01003313 child_counter = perf_counter_alloc(&parent_counter->hw_event,
Paul Mackerras23a185c2009-02-09 22:42:47 +11003314 parent_counter->cpu, child_ctx,
3315 group_leader, GFP_KERNEL);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003316 if (IS_ERR(child_counter))
3317 return child_counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003318
3319 /*
Paul Mackerras564c2b22009-05-22 14:27:22 +10003320 * Make the child state follow the state of the parent counter,
3321 * not its hw_event.disabled bit. We hold the parent's mutex,
3322 * so we won't race with perf_counter_{en,dis}able_family.
3323 */
3324 if (parent_counter->state >= PERF_COUNTER_STATE_INACTIVE)
3325 child_counter->state = PERF_COUNTER_STATE_INACTIVE;
3326 else
3327 child_counter->state = PERF_COUNTER_STATE_OFF;
3328
3329 /*
Ingo Molnar9b51f662008-12-12 13:49:45 +01003330 * Link it up in the child's context:
3331 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +11003332 add_counter_to_ctx(child_counter, child_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003333
3334 child_counter->parent = parent_counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003335 /*
3336 * inherit into child's child as well:
3337 */
3338 child_counter->hw_event.inherit = 1;
3339
3340 /*
3341 * Get a reference to the parent filp - we will fput it
3342 * when the child counter exits. This is safe to do because
3343 * we are in the parent and we know that the filp still
3344 * exists and has a nonzero count:
3345 */
3346 atomic_long_inc(&parent_counter->filp->f_count);
3347
Paul Mackerrasd859e292009-01-17 18:10:22 +11003348 /*
3349 * Link this into the parent counter's child list
3350 */
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003351 mutex_lock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003352 list_add_tail(&child_counter->child_list, &parent_counter->child_list);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003353 mutex_unlock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003354
3355 return child_counter;
3356}
3357
3358static int inherit_group(struct perf_counter *parent_counter,
3359 struct task_struct *parent,
3360 struct perf_counter_context *parent_ctx,
3361 struct task_struct *child,
3362 struct perf_counter_context *child_ctx)
3363{
3364 struct perf_counter *leader;
3365 struct perf_counter *sub;
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003366 struct perf_counter *child_ctr;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003367
3368 leader = inherit_counter(parent_counter, parent, parent_ctx,
3369 child, NULL, child_ctx);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003370 if (IS_ERR(leader))
3371 return PTR_ERR(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003372 list_for_each_entry(sub, &parent_counter->sibling_list, list_entry) {
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003373 child_ctr = inherit_counter(sub, parent, parent_ctx,
3374 child, leader, child_ctx);
3375 if (IS_ERR(child_ctr))
3376 return PTR_ERR(child_ctr);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003377 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01003378 return 0;
3379}
3380
Paul Mackerrasd859e292009-01-17 18:10:22 +11003381static void sync_child_counter(struct perf_counter *child_counter,
3382 struct perf_counter *parent_counter)
3383{
Peter Zijlstra8bc20952009-05-15 20:45:59 +02003384 u64 child_val;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003385
Paul Mackerrasd859e292009-01-17 18:10:22 +11003386 child_val = atomic64_read(&child_counter->count);
3387
3388 /*
3389 * Add back the child's count to the parent's count:
3390 */
3391 atomic64_add(child_val, &parent_counter->count);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11003392 atomic64_add(child_counter->total_time_enabled,
3393 &parent_counter->child_total_time_enabled);
3394 atomic64_add(child_counter->total_time_running,
3395 &parent_counter->child_total_time_running);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003396
3397 /*
3398 * Remove this counter from the parent's list
3399 */
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003400 mutex_lock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003401 list_del_init(&child_counter->child_list);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003402 mutex_unlock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003403
3404 /*
3405 * Release the parent counter, if this was the last
3406 * reference to it.
3407 */
3408 fput(parent_counter->filp);
3409}
3410
Ingo Molnar9b51f662008-12-12 13:49:45 +01003411static void
3412__perf_counter_exit_task(struct task_struct *child,
3413 struct perf_counter *child_counter,
3414 struct perf_counter_context *child_ctx)
3415{
3416 struct perf_counter *parent_counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003417
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003418 update_counter_times(child_counter);
Peter Zijlstraaa9c67f2009-05-23 18:28:59 +02003419 perf_counter_remove_from_context(child_counter);
Ingo Molnar0cc0c022008-12-14 23:20:36 +01003420
Ingo Molnar9b51f662008-12-12 13:49:45 +01003421 parent_counter = child_counter->parent;
3422 /*
3423 * It can happen that parent exits first, and has counters
3424 * that are still around due to the child reference. These
3425 * counters need to be zapped - but otherwise linger.
3426 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11003427 if (parent_counter) {
3428 sync_child_counter(child_counter, parent_counter);
Peter Zijlstraf1600952009-03-19 20:26:16 +01003429 free_counter(child_counter);
Paul Mackerras4bcf3492009-02-11 13:53:19 +01003430 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01003431}
3432
3433/*
Paul Mackerrasd859e292009-01-17 18:10:22 +11003434 * When a child task exits, feed back counter values to parent counters.
Ingo Molnar9b51f662008-12-12 13:49:45 +01003435 *
Paul Mackerrasd859e292009-01-17 18:10:22 +11003436 * Note: we may be running in child context, but the PID is not hashed
Ingo Molnar9b51f662008-12-12 13:49:45 +01003437 * anymore so new counters will not be added.
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003438 * (XXX not sure that is true when we get called from flush_old_exec.
3439 * -- paulus)
Ingo Molnar9b51f662008-12-12 13:49:45 +01003440 */
3441void perf_counter_exit_task(struct task_struct *child)
3442{
3443 struct perf_counter *child_counter, *tmp;
3444 struct perf_counter_context *child_ctx;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003445 unsigned long flags;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003446
Ingo Molnar33b2fb32009-05-17 11:08:41 +02003447 WARN_ON_ONCE(child != current);
3448
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003449 child_ctx = child->perf_counter_ctxp;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003450
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003451 if (likely(!child_ctx))
Ingo Molnar9b51f662008-12-12 13:49:45 +01003452 return;
3453
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003454 local_irq_save(flags);
3455 __perf_counter_task_sched_out(child_ctx);
3456 child->perf_counter_ctxp = NULL;
3457 local_irq_restore(flags);
3458
3459 mutex_lock(&child_ctx->mutex);
3460
Peter Zijlstra8bc20952009-05-15 20:45:59 +02003461again:
Ingo Molnar9b51f662008-12-12 13:49:45 +01003462 list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list,
3463 list_entry)
3464 __perf_counter_exit_task(child, child_counter, child_ctx);
Peter Zijlstra8bc20952009-05-15 20:45:59 +02003465
3466 /*
3467 * If the last counter was a group counter, it will have appended all
3468 * its siblings to the list, but we obtained 'tmp' before that which
3469 * will still point to the list head terminating the iteration.
3470 */
3471 if (!list_empty(&child_ctx->counter_list))
3472 goto again;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003473
3474 mutex_unlock(&child_ctx->mutex);
3475
3476 put_ctx(child_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003477}
3478
3479/*
3480 * Initialize the perf_counter context in task_struct
3481 */
3482void perf_counter_init_task(struct task_struct *child)
3483{
3484 struct perf_counter_context *child_ctx, *parent_ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003485 struct perf_counter *counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003486 struct task_struct *parent = current;
Paul Mackerras564c2b22009-05-22 14:27:22 +10003487 int inherited_all = 1;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003488
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003489 child->perf_counter_ctxp = NULL;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003490
3491 /*
3492 * This is executed from the parent task context, so inherit
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003493 * counters that have been marked for cloning.
3494 * First allocate and initialize a context for the child.
Ingo Molnar9b51f662008-12-12 13:49:45 +01003495 */
3496
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003497 child_ctx = kmalloc(sizeof(struct perf_counter_context), GFP_KERNEL);
3498 if (!child_ctx)
Ingo Molnar9b51f662008-12-12 13:49:45 +01003499 return;
3500
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003501 parent_ctx = parent->perf_counter_ctxp;
3502 if (likely(!parent_ctx || !parent_ctx->nr_counters))
3503 return;
3504
3505 __perf_counter_init_context(child_ctx, child);
3506 child->perf_counter_ctxp = child_ctx;
3507
Ingo Molnar9b51f662008-12-12 13:49:45 +01003508 /*
3509 * Lock the parent list. No need to lock the child - not PID
3510 * hashed yet and not running, so nobody can access it.
3511 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11003512 mutex_lock(&parent_ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003513
3514 /*
3515 * We dont have to disable NMIs - we are only looking at
3516 * the list, not manipulating it:
3517 */
Peter Zijlstrad7b629a2009-05-20 12:21:19 +02003518 list_for_each_entry_rcu(counter, &parent_ctx->event_list, event_entry) {
3519 if (counter != counter->group_leader)
3520 continue;
3521
Paul Mackerras564c2b22009-05-22 14:27:22 +10003522 if (!counter->hw_event.inherit) {
3523 inherited_all = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003524 continue;
Paul Mackerras564c2b22009-05-22 14:27:22 +10003525 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01003526
Paul Mackerrasd859e292009-01-17 18:10:22 +11003527 if (inherit_group(counter, parent,
Paul Mackerras564c2b22009-05-22 14:27:22 +10003528 parent_ctx, child, child_ctx)) {
3529 inherited_all = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003530 break;
Paul Mackerras564c2b22009-05-22 14:27:22 +10003531 }
3532 }
3533
3534 if (inherited_all) {
3535 /*
3536 * Mark the child context as a clone of the parent
3537 * context, or of whatever the parent is a clone of.
3538 */
3539 if (parent_ctx->parent_ctx) {
3540 child_ctx->parent_ctx = parent_ctx->parent_ctx;
3541 child_ctx->parent_gen = parent_ctx->parent_gen;
3542 } else {
3543 child_ctx->parent_ctx = parent_ctx;
3544 child_ctx->parent_gen = parent_ctx->generation;
3545 }
3546 get_ctx(child_ctx->parent_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003547 }
3548
Paul Mackerrasd859e292009-01-17 18:10:22 +11003549 mutex_unlock(&parent_ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003550}
3551
Ingo Molnar04289bb2008-12-11 08:38:42 +01003552static void __cpuinit perf_counter_init_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003553{
Ingo Molnar04289bb2008-12-11 08:38:42 +01003554 struct perf_cpu_context *cpuctx;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003555
Ingo Molnar04289bb2008-12-11 08:38:42 +01003556 cpuctx = &per_cpu(perf_cpu_context, cpu);
3557 __perf_counter_init_context(&cpuctx->ctx, NULL);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003558
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003559 spin_lock(&perf_resource_lock);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003560 cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu;
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003561 spin_unlock(&perf_resource_lock);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003562
Paul Mackerras01d02872009-01-14 13:44:19 +11003563 hw_perf_counter_setup(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003564}
3565
3566#ifdef CONFIG_HOTPLUG_CPU
Ingo Molnar04289bb2008-12-11 08:38:42 +01003567static void __perf_counter_exit_cpu(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003568{
3569 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
3570 struct perf_counter_context *ctx = &cpuctx->ctx;
3571 struct perf_counter *counter, *tmp;
3572
Ingo Molnar04289bb2008-12-11 08:38:42 +01003573 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry)
3574 __perf_counter_remove_from_context(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003575}
Ingo Molnar04289bb2008-12-11 08:38:42 +01003576static void perf_counter_exit_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003577{
Paul Mackerrasd859e292009-01-17 18:10:22 +11003578 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
3579 struct perf_counter_context *ctx = &cpuctx->ctx;
3580
3581 mutex_lock(&ctx->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003582 smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003583 mutex_unlock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003584}
3585#else
Ingo Molnar04289bb2008-12-11 08:38:42 +01003586static inline void perf_counter_exit_cpu(int cpu) { }
Thomas Gleixner0793a612008-12-04 20:12:29 +01003587#endif
3588
3589static int __cpuinit
3590perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
3591{
3592 unsigned int cpu = (long)hcpu;
3593
3594 switch (action) {
3595
3596 case CPU_UP_PREPARE:
3597 case CPU_UP_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01003598 perf_counter_init_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003599 break;
3600
3601 case CPU_DOWN_PREPARE:
3602 case CPU_DOWN_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01003603 perf_counter_exit_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003604 break;
3605
3606 default:
3607 break;
3608 }
3609
3610 return NOTIFY_OK;
3611}
3612
3613static struct notifier_block __cpuinitdata perf_cpu_nb = {
3614 .notifier_call = perf_cpu_notify,
3615};
3616
Ingo Molnar0d905bc2009-05-04 19:13:30 +02003617void __init perf_counter_init(void)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003618{
3619 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
3620 (void *)(long)smp_processor_id());
3621 register_cpu_notifier(&perf_cpu_nb);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003622}
Thomas Gleixner0793a612008-12-04 20:12:29 +01003623
3624static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
3625{
3626 return sprintf(buf, "%d\n", perf_reserved_percpu);
3627}
3628
3629static ssize_t
3630perf_set_reserve_percpu(struct sysdev_class *class,
3631 const char *buf,
3632 size_t count)
3633{
3634 struct perf_cpu_context *cpuctx;
3635 unsigned long val;
3636 int err, cpu, mpt;
3637
3638 err = strict_strtoul(buf, 10, &val);
3639 if (err)
3640 return err;
3641 if (val > perf_max_counters)
3642 return -EINVAL;
3643
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003644 spin_lock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003645 perf_reserved_percpu = val;
3646 for_each_online_cpu(cpu) {
3647 cpuctx = &per_cpu(perf_cpu_context, cpu);
3648 spin_lock_irq(&cpuctx->ctx.lock);
3649 mpt = min(perf_max_counters - cpuctx->ctx.nr_counters,
3650 perf_max_counters - perf_reserved_percpu);
3651 cpuctx->max_pertask = mpt;
3652 spin_unlock_irq(&cpuctx->ctx.lock);
3653 }
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003654 spin_unlock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003655
3656 return count;
3657}
3658
3659static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
3660{
3661 return sprintf(buf, "%d\n", perf_overcommit);
3662}
3663
3664static ssize_t
3665perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
3666{
3667 unsigned long val;
3668 int err;
3669
3670 err = strict_strtoul(buf, 10, &val);
3671 if (err)
3672 return err;
3673 if (val > 1)
3674 return -EINVAL;
3675
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003676 spin_lock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003677 perf_overcommit = val;
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003678 spin_unlock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003679
3680 return count;
3681}
3682
3683static SYSDEV_CLASS_ATTR(
3684 reserve_percpu,
3685 0644,
3686 perf_show_reserve_percpu,
3687 perf_set_reserve_percpu
3688 );
3689
3690static SYSDEV_CLASS_ATTR(
3691 overcommit,
3692 0644,
3693 perf_show_overcommit,
3694 perf_set_overcommit
3695 );
3696
3697static struct attribute *perfclass_attrs[] = {
3698 &attr_reserve_percpu.attr,
3699 &attr_overcommit.attr,
3700 NULL
3701};
3702
3703static struct attribute_group perfclass_attr_group = {
3704 .attrs = perfclass_attrs,
3705 .name = "perf_counters",
3706};
3707
3708static int __init perf_counter_sysfs_init(void)
3709{
3710 return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
3711 &perfclass_attr_group);
3712}
3713device_initcall(perf_counter_sysfs_init);