blob: 679c3b5bb7d4c2d786cebe7da3eff2214c04e726 [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
Thomas Gleixner0793a612008-12-04 20:12:29 +0100600 */
601static void __perf_install_in_context(void *info)
602{
603 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
604 struct perf_counter *counter = info;
605 struct perf_counter_context *ctx = counter->ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100606 struct perf_counter *leader = counter->group_leader;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100607 int cpu = smp_processor_id();
Ingo Molnar9b51f662008-12-12 13:49:45 +0100608 unsigned long flags;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100609 int err;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100610
611 /*
612 * If this is a task context, we need to check whether it is
613 * the current task context of this cpu. If not it has been
614 * scheduled out before the smp call arrived.
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000615 * Or possibly this is the right context but it isn't
616 * on this cpu because it had no counters.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100617 */
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000618 if (ctx->task && cpuctx->task_ctx != ctx) {
619 if (cpuctx->task_ctx || ctx->task != current)
620 return;
621 cpuctx->task_ctx = ctx;
622 }
Thomas Gleixner0793a612008-12-04 20:12:29 +0100623
Peter Zijlstra849691a2009-04-06 11:45:12 +0200624 spin_lock_irqsave(&ctx->lock, flags);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000625 ctx->is_active = 1;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200626 update_context_time(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100627
628 /*
629 * Protect the list operation against NMI by disabling the
630 * counters on a global level. NOP for non NMI based counters.
631 */
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200632 perf_disable();
Thomas Gleixner0793a612008-12-04 20:12:29 +0100633
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100634 add_counter_to_ctx(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100635
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100636 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100637 * Don't put the counter on if it is disabled or if
638 * it is in a group and the group isn't on.
639 */
640 if (counter->state != PERF_COUNTER_STATE_INACTIVE ||
641 (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE))
642 goto unlock;
643
644 /*
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100645 * An exclusive counter can't go on if there are already active
646 * hardware counters, and no hardware counter can go on if there
647 * is already an exclusive counter on.
648 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100649 if (!group_can_go_on(counter, cpuctx, 1))
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100650 err = -EEXIST;
651 else
652 err = counter_sched_in(counter, cpuctx, ctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100653
Paul Mackerrasd859e292009-01-17 18:10:22 +1100654 if (err) {
655 /*
656 * This counter couldn't go on. If it is in a group
657 * then we have to pull the whole group off.
658 * If the counter group is pinned then put it in error state.
659 */
660 if (leader != counter)
661 group_sched_out(leader, cpuctx, ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100662 if (leader->hw_event.pinned) {
663 update_group_times(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100664 leader->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100665 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100666 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100667
668 if (!err && !ctx->task && cpuctx->max_pertask)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100669 cpuctx->max_pertask--;
670
Paul Mackerrasd859e292009-01-17 18:10:22 +1100671 unlock:
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200672 perf_enable();
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100673
Peter Zijlstra849691a2009-04-06 11:45:12 +0200674 spin_unlock_irqrestore(&ctx->lock, flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100675}
676
677/*
678 * Attach a performance counter to a context
679 *
680 * First we add the counter to the list with the hardware enable bit
681 * in counter->hw_config cleared.
682 *
683 * If the counter is attached to a task which is on a CPU we use a smp
684 * call to enable it in the task context. The task might have been
685 * scheduled away, but we check this in the smp call again.
Paul Mackerrasd859e292009-01-17 18:10:22 +1100686 *
687 * Must be called with ctx->mutex held.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100688 */
689static void
690perf_install_in_context(struct perf_counter_context *ctx,
691 struct perf_counter *counter,
692 int cpu)
693{
694 struct task_struct *task = ctx->task;
695
Thomas Gleixner0793a612008-12-04 20:12:29 +0100696 if (!task) {
697 /*
698 * Per cpu counters are installed via an smp call and
699 * the install is always sucessful.
700 */
701 smp_call_function_single(cpu, __perf_install_in_context,
702 counter, 1);
703 return;
704 }
705
Thomas Gleixner0793a612008-12-04 20:12:29 +0100706retry:
707 task_oncpu_function_call(task, __perf_install_in_context,
708 counter);
709
710 spin_lock_irq(&ctx->lock);
711 /*
Thomas Gleixner0793a612008-12-04 20:12:29 +0100712 * we need to retry the smp call.
713 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100714 if (ctx->is_active && list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100715 spin_unlock_irq(&ctx->lock);
716 goto retry;
717 }
718
719 /*
720 * The lock prevents that this context is scheduled in so we
721 * can add the counter safely, if it the call above did not
722 * succeed.
723 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100724 if (list_empty(&counter->list_entry))
725 add_counter_to_ctx(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100726 spin_unlock_irq(&ctx->lock);
727}
728
Paul Mackerrasd859e292009-01-17 18:10:22 +1100729/*
730 * Cross CPU call to enable a performance counter
731 */
732static void __perf_counter_enable(void *info)
Ingo Molnar04289bb2008-12-11 08:38:42 +0100733{
Paul Mackerrasd859e292009-01-17 18:10:22 +1100734 struct perf_counter *counter = info;
735 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
736 struct perf_counter_context *ctx = counter->ctx;
737 struct perf_counter *leader = counter->group_leader;
738 unsigned long flags;
739 int err;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100740
741 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100742 * If this is a per-task counter, need to check whether this
743 * counter's task is the current task on this cpu.
Ingo Molnar04289bb2008-12-11 08:38:42 +0100744 */
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000745 if (ctx->task && cpuctx->task_ctx != ctx) {
746 if (cpuctx->task_ctx || ctx->task != current)
747 return;
748 cpuctx->task_ctx = ctx;
749 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100750
Peter Zijlstra849691a2009-04-06 11:45:12 +0200751 spin_lock_irqsave(&ctx->lock, flags);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000752 ctx->is_active = 1;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200753 update_context_time(ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100754
Paul Mackerrasc07c99b2009-02-13 22:10:34 +1100755 counter->prev_state = counter->state;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100756 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
757 goto unlock;
758 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200759 counter->tstamp_enabled = ctx->time - counter->total_time_enabled;
Paul Mackerras564c2b22009-05-22 14:27:22 +1000760 ctx->nr_enabled++;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100761
762 /*
763 * If the counter is in a group and isn't the group leader,
764 * then don't put it on unless the group is on.
765 */
766 if (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE)
767 goto unlock;
768
Paul Mackerrase758a332009-05-12 21:59:01 +1000769 if (!group_can_go_on(counter, cpuctx, 1)) {
Paul Mackerrasd859e292009-01-17 18:10:22 +1100770 err = -EEXIST;
Paul Mackerrase758a332009-05-12 21:59:01 +1000771 } else {
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200772 perf_disable();
Paul Mackerrase758a332009-05-12 21:59:01 +1000773 if (counter == leader)
774 err = group_sched_in(counter, cpuctx, ctx,
775 smp_processor_id());
776 else
777 err = counter_sched_in(counter, cpuctx, ctx,
778 smp_processor_id());
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200779 perf_enable();
Paul Mackerrase758a332009-05-12 21:59:01 +1000780 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100781
782 if (err) {
783 /*
784 * If this counter can't go on and it's part of a
785 * group, then the whole group has to come off.
786 */
787 if (leader != counter)
788 group_sched_out(leader, cpuctx, ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100789 if (leader->hw_event.pinned) {
790 update_group_times(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100791 leader->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100792 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100793 }
794
795 unlock:
Peter Zijlstra849691a2009-04-06 11:45:12 +0200796 spin_unlock_irqrestore(&ctx->lock, flags);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100797}
798
799/*
800 * Enable a counter.
801 */
802static void perf_counter_enable(struct perf_counter *counter)
803{
804 struct perf_counter_context *ctx = counter->ctx;
805 struct task_struct *task = ctx->task;
806
807 if (!task) {
808 /*
809 * Enable the counter on the cpu that it's on
810 */
811 smp_call_function_single(counter->cpu, __perf_counter_enable,
812 counter, 1);
813 return;
814 }
815
816 spin_lock_irq(&ctx->lock);
817 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
818 goto out;
819
820 /*
821 * If the counter is in error state, clear that first.
822 * That way, if we see the counter in error state below, we
823 * know that it has gone back into error state, as distinct
824 * from the task having been scheduled away before the
825 * cross-call arrived.
826 */
827 if (counter->state == PERF_COUNTER_STATE_ERROR)
828 counter->state = PERF_COUNTER_STATE_OFF;
829
830 retry:
831 spin_unlock_irq(&ctx->lock);
832 task_oncpu_function_call(task, __perf_counter_enable, counter);
833
834 spin_lock_irq(&ctx->lock);
835
836 /*
837 * If the context is active and the counter is still off,
838 * we need to retry the cross-call.
839 */
840 if (ctx->is_active && counter->state == PERF_COUNTER_STATE_OFF)
841 goto retry;
842
843 /*
844 * Since we have the lock this context can't be scheduled
845 * in, so we can change the state safely.
846 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100847 if (counter->state == PERF_COUNTER_STATE_OFF) {
Paul Mackerrasd859e292009-01-17 18:10:22 +1100848 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200849 counter->tstamp_enabled =
850 ctx->time - counter->total_time_enabled;
Paul Mackerras564c2b22009-05-22 14:27:22 +1000851 ctx->nr_enabled++;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100852 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100853 out:
854 spin_unlock_irq(&ctx->lock);
855}
856
Peter Zijlstra2023b352009-05-05 17:50:26 +0200857static int perf_counter_refresh(struct perf_counter *counter, int refresh)
Peter Zijlstra79f14642009-04-06 11:45:07 +0200858{
Peter Zijlstra2023b352009-05-05 17:50:26 +0200859 /*
860 * not supported on inherited counters
861 */
862 if (counter->hw_event.inherit)
863 return -EINVAL;
864
Peter Zijlstra79f14642009-04-06 11:45:07 +0200865 atomic_add(refresh, &counter->event_limit);
866 perf_counter_enable(counter);
Peter Zijlstra2023b352009-05-05 17:50:26 +0200867
868 return 0;
Peter Zijlstra79f14642009-04-06 11:45:07 +0200869}
870
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100871void __perf_counter_sched_out(struct perf_counter_context *ctx,
872 struct perf_cpu_context *cpuctx)
873{
874 struct perf_counter *counter;
875
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100876 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100877 ctx->is_active = 0;
878 if (likely(!ctx->nr_counters))
879 goto out;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200880 update_context_time(ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100881
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200882 perf_disable();
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100883 if (ctx->nr_active) {
Peter Zijlstraafedadf2009-05-20 12:21:22 +0200884 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
885 if (counter != counter->group_leader)
886 counter_sched_out(counter, cpuctx, ctx);
887 else
888 group_sched_out(counter, cpuctx, ctx);
889 }
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100890 }
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200891 perf_enable();
Paul Mackerrasd859e292009-01-17 18:10:22 +1100892 out:
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100893 spin_unlock(&ctx->lock);
894}
895
Thomas Gleixner0793a612008-12-04 20:12:29 +0100896/*
Paul Mackerras564c2b22009-05-22 14:27:22 +1000897 * Test whether two contexts are equivalent, i.e. whether they
898 * have both been cloned from the same version of the same context
899 * and they both have the same number of enabled counters.
900 * If the number of enabled counters is the same, then the set
901 * of enabled counters should be the same, because these are both
902 * inherited contexts, therefore we can't access individual counters
903 * in them directly with an fd; we can only enable/disable all
904 * counters via prctl, or enable/disable all counters in a family
905 * via ioctl, which will have the same effect on both contexts.
906 */
907static int context_equiv(struct perf_counter_context *ctx1,
908 struct perf_counter_context *ctx2)
909{
910 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
911 && ctx1->parent_gen == ctx2->parent_gen
912 && ctx1->nr_enabled == ctx2->nr_enabled;
913}
914
915/*
Thomas Gleixner0793a612008-12-04 20:12:29 +0100916 * Called from scheduler to remove the counters of the current task,
917 * with interrupts disabled.
918 *
919 * We stop each counter and update the counter value in counter->count.
920 *
Ingo Molnar76715812008-12-17 14:20:28 +0100921 * This does not protect us against NMI, but disable()
Thomas Gleixner0793a612008-12-04 20:12:29 +0100922 * sets the disabled bit in the control field of counter _before_
923 * accessing the counter control register. If a NMI hits, then it will
924 * not restart the counter.
925 */
Paul Mackerras564c2b22009-05-22 14:27:22 +1000926void perf_counter_task_sched_out(struct task_struct *task,
927 struct task_struct *next, int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100928{
929 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000930 struct perf_counter_context *ctx = task->perf_counter_ctxp;
Paul Mackerras564c2b22009-05-22 14:27:22 +1000931 struct perf_counter_context *next_ctx;
Peter Zijlstra4a0deca2009-03-19 20:26:12 +0100932 struct pt_regs *regs;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100933
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000934 if (likely(!ctx || !cpuctx->task_ctx))
Thomas Gleixner0793a612008-12-04 20:12:29 +0100935 return;
936
Peter Zijlstrabce379b2009-04-06 11:45:13 +0200937 update_context_time(ctx);
938
Peter Zijlstra4a0deca2009-03-19 20:26:12 +0100939 regs = task_pt_regs(task);
Peter Zijlstra78f13e92009-04-08 15:01:33 +0200940 perf_swcounter_event(PERF_COUNT_CONTEXT_SWITCHES, 1, 1, regs, 0);
Paul Mackerras564c2b22009-05-22 14:27:22 +1000941
942 next_ctx = next->perf_counter_ctxp;
943 if (next_ctx && context_equiv(ctx, next_ctx)) {
944 task->perf_counter_ctxp = next_ctx;
945 next->perf_counter_ctxp = ctx;
946 ctx->task = next;
947 next_ctx->task = task;
948 return;
949 }
950
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100951 __perf_counter_sched_out(ctx, cpuctx);
952
Thomas Gleixner0793a612008-12-04 20:12:29 +0100953 cpuctx->task_ctx = NULL;
954}
955
Paul Mackerrasa08b1592009-05-11 15:46:10 +1000956static void __perf_counter_task_sched_out(struct perf_counter_context *ctx)
957{
958 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
959
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000960 if (!cpuctx->task_ctx)
961 return;
Paul Mackerrasa08b1592009-05-11 15:46:10 +1000962 __perf_counter_sched_out(ctx, cpuctx);
963 cpuctx->task_ctx = NULL;
964}
965
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100966static void perf_counter_cpu_sched_out(struct perf_cpu_context *cpuctx)
Ingo Molnar04289bb2008-12-11 08:38:42 +0100967{
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100968 __perf_counter_sched_out(&cpuctx->ctx, cpuctx);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100969}
970
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100971static void
972__perf_counter_sched_in(struct perf_counter_context *ctx,
973 struct perf_cpu_context *cpuctx, int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100974{
Thomas Gleixner0793a612008-12-04 20:12:29 +0100975 struct perf_counter *counter;
Paul Mackerrasdd0e6ba2009-01-12 15:11:00 +1100976 int can_add_hw = 1;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100977
Thomas Gleixner0793a612008-12-04 20:12:29 +0100978 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100979 ctx->is_active = 1;
980 if (likely(!ctx->nr_counters))
981 goto out;
982
Peter Zijlstra4af49982009-04-06 11:45:10 +0200983 ctx->timestamp = perf_clock();
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100984
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200985 perf_disable();
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100986
987 /*
988 * First go through the list and put on any pinned groups
989 * in order to give them the best chance of going on.
990 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100991 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100992 if (counter->state <= PERF_COUNTER_STATE_OFF ||
993 !counter->hw_event.pinned)
994 continue;
995 if (counter->cpu != -1 && counter->cpu != cpu)
996 continue;
997
Peter Zijlstraafedadf2009-05-20 12:21:22 +0200998 if (counter != counter->group_leader)
999 counter_sched_in(counter, cpuctx, ctx, cpu);
1000 else {
1001 if (group_can_go_on(counter, cpuctx, 1))
1002 group_sched_in(counter, cpuctx, ctx, cpu);
1003 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001004
1005 /*
1006 * If this pinned group hasn't been scheduled,
1007 * put it in error state.
1008 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001009 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
1010 update_group_times(counter);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001011 counter->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001012 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001013 }
1014
1015 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1016 /*
1017 * Ignore counters in OFF or ERROR state, and
1018 * ignore pinned counters since we did them already.
1019 */
1020 if (counter->state <= PERF_COUNTER_STATE_OFF ||
1021 counter->hw_event.pinned)
1022 continue;
1023
Ingo Molnar04289bb2008-12-11 08:38:42 +01001024 /*
1025 * Listen to the 'cpu' scheduling filter constraint
1026 * of counters:
1027 */
Thomas Gleixner0793a612008-12-04 20:12:29 +01001028 if (counter->cpu != -1 && counter->cpu != cpu)
1029 continue;
1030
Peter Zijlstraafedadf2009-05-20 12:21:22 +02001031 if (counter != counter->group_leader) {
1032 if (counter_sched_in(counter, cpuctx, ctx, cpu))
Paul Mackerrasdd0e6ba2009-01-12 15:11:00 +11001033 can_add_hw = 0;
Peter Zijlstraafedadf2009-05-20 12:21:22 +02001034 } else {
1035 if (group_can_go_on(counter, cpuctx, can_add_hw)) {
1036 if (group_sched_in(counter, cpuctx, ctx, cpu))
1037 can_add_hw = 0;
1038 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001039 }
Thomas Gleixner0793a612008-12-04 20:12:29 +01001040 }
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001041 perf_enable();
Paul Mackerrasd859e292009-01-17 18:10:22 +11001042 out:
Thomas Gleixner0793a612008-12-04 20:12:29 +01001043 spin_unlock(&ctx->lock);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001044}
Ingo Molnar04289bb2008-12-11 08:38:42 +01001045
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001046/*
1047 * Called from scheduler to add the counters of the current task
1048 * with interrupts disabled.
1049 *
1050 * We restore the counter value and then enable it.
1051 *
1052 * This does not protect us against NMI, but enable()
1053 * sets the enabled bit in the control field of counter _before_
1054 * accessing the counter control register. If a NMI hits, then it will
1055 * keep the counter running.
1056 */
1057void perf_counter_task_sched_in(struct task_struct *task, int cpu)
1058{
1059 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001060 struct perf_counter_context *ctx = task->perf_counter_ctxp;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001061
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001062 if (likely(!ctx))
1063 return;
Paul Mackerras564c2b22009-05-22 14:27:22 +10001064 if (cpuctx->task_ctx == ctx)
1065 return;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001066 __perf_counter_sched_in(ctx, cpuctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001067 cpuctx->task_ctx = ctx;
1068}
1069
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001070static void perf_counter_cpu_sched_in(struct perf_cpu_context *cpuctx, int cpu)
1071{
1072 struct perf_counter_context *ctx = &cpuctx->ctx;
1073
1074 __perf_counter_sched_in(ctx, cpuctx, cpu);
1075}
1076
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001077int perf_counter_task_disable(void)
1078{
1079 struct task_struct *curr = current;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001080 struct perf_counter_context *ctx = curr->perf_counter_ctxp;
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001081 struct perf_counter *counter;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001082 unsigned long flags;
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001083
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001084 if (!ctx || !ctx->nr_counters)
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001085 return 0;
1086
Peter Zijlstra849691a2009-04-06 11:45:12 +02001087 local_irq_save(flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001088
Paul Mackerrasa08b1592009-05-11 15:46:10 +10001089 __perf_counter_task_sched_out(ctx);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001090
1091 spin_lock(&ctx->lock);
1092
1093 /*
1094 * Disable all the counters:
1095 */
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001096 perf_disable();
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001097
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001098 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001099 if (counter->state != PERF_COUNTER_STATE_ERROR) {
1100 update_group_times(counter);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001101 counter->state = PERF_COUNTER_STATE_OFF;
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001102 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001103 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01001104
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001105 perf_enable();
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001106
Peter Zijlstra849691a2009-04-06 11:45:12 +02001107 spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001108
1109 return 0;
1110}
1111
1112int perf_counter_task_enable(void)
1113{
1114 struct task_struct *curr = current;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001115 struct perf_counter_context *ctx = curr->perf_counter_ctxp;
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001116 struct perf_counter *counter;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001117 unsigned long flags;
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001118 int cpu;
1119
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001120 if (!ctx || !ctx->nr_counters)
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001121 return 0;
1122
Peter Zijlstra849691a2009-04-06 11:45:12 +02001123 local_irq_save(flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001124 cpu = smp_processor_id();
1125
Paul Mackerrasa08b1592009-05-11 15:46:10 +10001126 __perf_counter_task_sched_out(ctx);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001127
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001128 spin_lock(&ctx->lock);
1129
1130 /*
1131 * Disable all the counters:
1132 */
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001133 perf_disable();
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001134
1135 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001136 if (counter->state > PERF_COUNTER_STATE_OFF)
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001137 continue;
Ingo Molnar6a930702008-12-11 15:17:03 +01001138 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +02001139 counter->tstamp_enabled =
1140 ctx->time - counter->total_time_enabled;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001141 counter->hw_event.disabled = 0;
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001142 }
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001143 perf_enable();
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001144
1145 spin_unlock(&ctx->lock);
1146
1147 perf_counter_task_sched_in(curr, cpu);
1148
Peter Zijlstra849691a2009-04-06 11:45:12 +02001149 local_irq_restore(flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001150
1151 return 0;
1152}
1153
Peter Zijlstra26b119b2009-05-20 12:21:20 +02001154static void perf_log_period(struct perf_counter *counter, u64 period);
1155
1156static void perf_adjust_freq(struct perf_counter_context *ctx)
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001157{
1158 struct perf_counter *counter;
1159 u64 irq_period;
1160 u64 events, period;
1161 s64 delta;
1162
1163 spin_lock(&ctx->lock);
1164 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1165 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
1166 continue;
1167
1168 if (!counter->hw_event.freq || !counter->hw_event.irq_freq)
1169 continue;
1170
1171 events = HZ * counter->hw.interrupts * counter->hw.irq_period;
1172 period = div64_u64(events, counter->hw_event.irq_freq);
1173
1174 delta = (s64)(1 + period - counter->hw.irq_period);
1175 delta >>= 1;
1176
1177 irq_period = counter->hw.irq_period + delta;
1178
1179 if (!irq_period)
1180 irq_period = 1;
1181
Peter Zijlstra26b119b2009-05-20 12:21:20 +02001182 perf_log_period(counter, irq_period);
1183
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001184 counter->hw.irq_period = irq_period;
1185 counter->hw.interrupts = 0;
1186 }
1187 spin_unlock(&ctx->lock);
1188}
1189
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001190/*
1191 * Round-robin a context's counters:
1192 */
1193static void rotate_ctx(struct perf_counter_context *ctx)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001194{
Thomas Gleixner0793a612008-12-04 20:12:29 +01001195 struct perf_counter *counter;
1196
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001197 if (!ctx->nr_counters)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001198 return;
1199
Thomas Gleixner0793a612008-12-04 20:12:29 +01001200 spin_lock(&ctx->lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001201 /*
Ingo Molnar04289bb2008-12-11 08:38:42 +01001202 * Rotate the first entry last (works just fine for group counters too):
Thomas Gleixner0793a612008-12-04 20:12:29 +01001203 */
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001204 perf_disable();
Ingo Molnar04289bb2008-12-11 08:38:42 +01001205 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Peter Zijlstra75564232009-03-13 12:21:29 +01001206 list_move_tail(&counter->list_entry, &ctx->counter_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001207 break;
1208 }
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001209 perf_enable();
Thomas Gleixner0793a612008-12-04 20:12:29 +01001210
1211 spin_unlock(&ctx->lock);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001212}
Thomas Gleixner0793a612008-12-04 20:12:29 +01001213
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001214void perf_counter_task_tick(struct task_struct *curr, int cpu)
1215{
Peter Zijlstra7fc23a52009-05-08 18:52:21 +02001216 struct perf_cpu_context *cpuctx;
1217 struct perf_counter_context *ctx;
1218
1219 if (!atomic_read(&nr_counters))
1220 return;
1221
1222 cpuctx = &per_cpu(perf_cpu_context, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001223 ctx = curr->perf_counter_ctxp;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001224
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001225 perf_adjust_freq(&cpuctx->ctx);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001226 if (ctx)
1227 perf_adjust_freq(ctx);
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001228
Ingo Molnarb82914c2009-05-04 18:54:32 +02001229 perf_counter_cpu_sched_out(cpuctx);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001230 if (ctx)
1231 __perf_counter_task_sched_out(ctx);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001232
Ingo Molnarb82914c2009-05-04 18:54:32 +02001233 rotate_ctx(&cpuctx->ctx);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001234 if (ctx)
1235 rotate_ctx(ctx);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001236
Ingo Molnarb82914c2009-05-04 18:54:32 +02001237 perf_counter_cpu_sched_in(cpuctx, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001238 if (ctx)
1239 perf_counter_task_sched_in(curr, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001240}
1241
1242/*
Thomas Gleixner0793a612008-12-04 20:12:29 +01001243 * Cross CPU call to read the hardware counter
1244 */
Ingo Molnar76715812008-12-17 14:20:28 +01001245static void __read(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001246{
Ingo Molnar621a01e2008-12-11 12:46:46 +01001247 struct perf_counter *counter = info;
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001248 struct perf_counter_context *ctx = counter->ctx;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001249 unsigned long flags;
Ingo Molnar621a01e2008-12-11 12:46:46 +01001250
Peter Zijlstra849691a2009-04-06 11:45:12 +02001251 local_irq_save(flags);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001252 if (ctx->is_active)
Peter Zijlstra4af49982009-04-06 11:45:10 +02001253 update_context_time(ctx);
Robert Richter4aeb0b42009-04-29 12:47:03 +02001254 counter->pmu->read(counter);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001255 update_counter_times(counter);
Peter Zijlstra849691a2009-04-06 11:45:12 +02001256 local_irq_restore(flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001257}
1258
Ingo Molnar04289bb2008-12-11 08:38:42 +01001259static u64 perf_counter_read(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001260{
1261 /*
1262 * If counter is enabled and currently active on a CPU, update the
1263 * value in the counter structure:
1264 */
Ingo Molnar6a930702008-12-11 15:17:03 +01001265 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
Thomas Gleixner0793a612008-12-04 20:12:29 +01001266 smp_call_function_single(counter->oncpu,
Ingo Molnar76715812008-12-17 14:20:28 +01001267 __read, counter, 1);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001268 } else if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
1269 update_counter_times(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001270 }
1271
Ingo Molnaree060942008-12-13 09:00:03 +01001272 return atomic64_read(&counter->count);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001273}
1274
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001275/*
1276 * Initialize the perf_counter context in a task_struct:
1277 */
1278static void
1279__perf_counter_init_context(struct perf_counter_context *ctx,
1280 struct task_struct *task)
1281{
1282 memset(ctx, 0, sizeof(*ctx));
1283 spin_lock_init(&ctx->lock);
1284 mutex_init(&ctx->mutex);
1285 INIT_LIST_HEAD(&ctx->counter_list);
1286 INIT_LIST_HEAD(&ctx->event_list);
1287 atomic_set(&ctx->refcount, 1);
1288 ctx->task = task;
1289}
1290
Thomas Gleixner0793a612008-12-04 20:12:29 +01001291static void put_context(struct perf_counter_context *ctx)
1292{
1293 if (ctx->task)
1294 put_task_struct(ctx->task);
1295}
1296
1297static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
1298{
1299 struct perf_cpu_context *cpuctx;
1300 struct perf_counter_context *ctx;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001301 struct perf_counter_context *tctx;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001302 struct task_struct *task;
1303
1304 /*
1305 * If cpu is not a wildcard then this is a percpu counter:
1306 */
1307 if (cpu != -1) {
1308 /* Must be root to operate on a CPU counter: */
Peter Zijlstra1ccd1542009-04-09 10:53:45 +02001309 if (sysctl_perf_counter_priv && !capable(CAP_SYS_ADMIN))
Thomas Gleixner0793a612008-12-04 20:12:29 +01001310 return ERR_PTR(-EACCES);
1311
1312 if (cpu < 0 || cpu > num_possible_cpus())
1313 return ERR_PTR(-EINVAL);
1314
1315 /*
1316 * We could be clever and allow to attach a counter to an
1317 * offline CPU and activate it when the CPU comes up, but
1318 * that's for later.
1319 */
1320 if (!cpu_isset(cpu, cpu_online_map))
1321 return ERR_PTR(-ENODEV);
1322
1323 cpuctx = &per_cpu(perf_cpu_context, cpu);
1324 ctx = &cpuctx->ctx;
1325
Thomas Gleixner0793a612008-12-04 20:12:29 +01001326 return ctx;
1327 }
1328
1329 rcu_read_lock();
1330 if (!pid)
1331 task = current;
1332 else
1333 task = find_task_by_vpid(pid);
1334 if (task)
1335 get_task_struct(task);
1336 rcu_read_unlock();
1337
1338 if (!task)
1339 return ERR_PTR(-ESRCH);
1340
Thomas Gleixner0793a612008-12-04 20:12:29 +01001341 /* Reuse ptrace permission checks for now. */
1342 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001343 put_task_struct(task);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001344 return ERR_PTR(-EACCES);
1345 }
1346
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001347 ctx = task->perf_counter_ctxp;
1348 if (!ctx) {
1349 ctx = kmalloc(sizeof(struct perf_counter_context), GFP_KERNEL);
1350 if (!ctx) {
1351 put_task_struct(task);
1352 return ERR_PTR(-ENOMEM);
1353 }
1354 __perf_counter_init_context(ctx, task);
1355 /*
1356 * Make sure other cpus see correct values for *ctx
1357 * once task->perf_counter_ctxp is visible to them.
1358 */
1359 smp_wmb();
1360 tctx = cmpxchg(&task->perf_counter_ctxp, NULL, ctx);
1361 if (tctx) {
1362 /*
1363 * We raced with some other task; use
1364 * the context they set.
1365 */
1366 kfree(ctx);
1367 ctx = tctx;
1368 }
1369 }
1370
Thomas Gleixner0793a612008-12-04 20:12:29 +01001371 return ctx;
1372}
1373
Peter Zijlstra592903c2009-03-13 12:21:36 +01001374static void free_counter_rcu(struct rcu_head *head)
1375{
1376 struct perf_counter *counter;
1377
1378 counter = container_of(head, struct perf_counter, rcu_head);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001379 put_ctx(counter->ctx);
Peter Zijlstra592903c2009-03-13 12:21:36 +01001380 kfree(counter);
1381}
1382
Peter Zijlstra925d5192009-03-30 19:07:02 +02001383static void perf_pending_sync(struct perf_counter *counter);
1384
Peter Zijlstraf1600952009-03-19 20:26:16 +01001385static void free_counter(struct perf_counter *counter)
1386{
Peter Zijlstra925d5192009-03-30 19:07:02 +02001387 perf_pending_sync(counter);
1388
Peter Zijlstra7fc23a52009-05-08 18:52:21 +02001389 atomic_dec(&nr_counters);
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02001390 if (counter->hw_event.mmap)
1391 atomic_dec(&nr_mmap_tracking);
1392 if (counter->hw_event.munmap)
1393 atomic_dec(&nr_munmap_tracking);
1394 if (counter->hw_event.comm)
1395 atomic_dec(&nr_comm_tracking);
1396
Peter Zijlstrae077df42009-03-19 20:26:17 +01001397 if (counter->destroy)
1398 counter->destroy(counter);
1399
Peter Zijlstraf1600952009-03-19 20:26:16 +01001400 call_rcu(&counter->rcu_head, free_counter_rcu);
1401}
1402
Thomas Gleixner0793a612008-12-04 20:12:29 +01001403/*
1404 * Called when the last reference to the file is gone.
1405 */
1406static int perf_release(struct inode *inode, struct file *file)
1407{
1408 struct perf_counter *counter = file->private_data;
1409 struct perf_counter_context *ctx = counter->ctx;
1410
1411 file->private_data = NULL;
1412
Paul Mackerrasd859e292009-01-17 18:10:22 +11001413 mutex_lock(&ctx->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01001414 perf_counter_remove_from_context(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001415 mutex_unlock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001416
Peter Zijlstraf1600952009-03-19 20:26:16 +01001417 free_counter(counter);
Mike Galbraith5af75912009-02-11 10:53:37 +01001418 put_context(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001419
1420 return 0;
1421}
1422
1423/*
1424 * Read the performance counter - simple non blocking version for now
1425 */
1426static ssize_t
1427perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
1428{
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001429 u64 values[3];
1430 int n;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001431
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001432 /*
1433 * Return end-of-file for a read on a counter that is in
1434 * error state (i.e. because it was pinned but it couldn't be
1435 * scheduled on to the CPU at some point).
1436 */
1437 if (counter->state == PERF_COUNTER_STATE_ERROR)
1438 return 0;
1439
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001440 mutex_lock(&counter->child_mutex);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001441 values[0] = perf_counter_read(counter);
1442 n = 1;
1443 if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1444 values[n++] = counter->total_time_enabled +
1445 atomic64_read(&counter->child_total_time_enabled);
1446 if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1447 values[n++] = counter->total_time_running +
1448 atomic64_read(&counter->child_total_time_running);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001449 mutex_unlock(&counter->child_mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001450
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001451 if (count < n * sizeof(u64))
1452 return -EINVAL;
1453 count = n * sizeof(u64);
1454
1455 if (copy_to_user(buf, values, count))
1456 return -EFAULT;
1457
1458 return count;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001459}
1460
1461static ssize_t
Thomas Gleixner0793a612008-12-04 20:12:29 +01001462perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
1463{
1464 struct perf_counter *counter = file->private_data;
1465
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001466 return perf_read_hw(counter, buf, count);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001467}
1468
1469static unsigned int perf_poll(struct file *file, poll_table *wait)
1470{
1471 struct perf_counter *counter = file->private_data;
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001472 struct perf_mmap_data *data;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001473 unsigned int events = POLL_HUP;
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001474
1475 rcu_read_lock();
1476 data = rcu_dereference(counter->data);
1477 if (data)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001478 events = atomic_xchg(&data->poll, 0);
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001479 rcu_read_unlock();
Thomas Gleixner0793a612008-12-04 20:12:29 +01001480
1481 poll_wait(file, &counter->waitq, wait);
1482
Thomas Gleixner0793a612008-12-04 20:12:29 +01001483 return events;
1484}
1485
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001486static void perf_counter_reset(struct perf_counter *counter)
1487{
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001488 (void)perf_counter_read(counter);
Paul Mackerras615a3f12009-05-11 15:50:21 +10001489 atomic64_set(&counter->count, 0);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001490 perf_counter_update_userpage(counter);
1491}
1492
1493static void perf_counter_for_each_sibling(struct perf_counter *counter,
1494 void (*func)(struct perf_counter *))
1495{
1496 struct perf_counter_context *ctx = counter->ctx;
1497 struct perf_counter *sibling;
1498
1499 spin_lock_irq(&ctx->lock);
1500 counter = counter->group_leader;
1501
1502 func(counter);
1503 list_for_each_entry(sibling, &counter->sibling_list, list_entry)
1504 func(sibling);
1505 spin_unlock_irq(&ctx->lock);
1506}
1507
1508static void perf_counter_for_each_child(struct perf_counter *counter,
1509 void (*func)(struct perf_counter *))
1510{
1511 struct perf_counter *child;
1512
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001513 mutex_lock(&counter->child_mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001514 func(counter);
1515 list_for_each_entry(child, &counter->child_list, child_list)
1516 func(child);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001517 mutex_unlock(&counter->child_mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001518}
1519
1520static void perf_counter_for_each(struct perf_counter *counter,
1521 void (*func)(struct perf_counter *))
1522{
1523 struct perf_counter *child;
1524
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001525 mutex_lock(&counter->child_mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001526 perf_counter_for_each_sibling(counter, func);
1527 list_for_each_entry(child, &counter->child_list, child_list)
1528 perf_counter_for_each_sibling(child, func);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001529 mutex_unlock(&counter->child_mutex);
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001530}
1531
Paul Mackerrasd859e292009-01-17 18:10:22 +11001532static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1533{
1534 struct perf_counter *counter = file->private_data;
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001535 void (*func)(struct perf_counter *);
1536 u32 flags = arg;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001537
1538 switch (cmd) {
1539 case PERF_COUNTER_IOC_ENABLE:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001540 func = perf_counter_enable;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001541 break;
1542 case PERF_COUNTER_IOC_DISABLE:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001543 func = perf_counter_disable;
Peter Zijlstra79f14642009-04-06 11:45:07 +02001544 break;
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001545 case PERF_COUNTER_IOC_RESET:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001546 func = perf_counter_reset;
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001547 break;
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001548
1549 case PERF_COUNTER_IOC_REFRESH:
1550 return perf_counter_refresh(counter, arg);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001551 default:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001552 return -ENOTTY;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001553 }
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001554
1555 if (flags & PERF_IOC_FLAG_GROUP)
1556 perf_counter_for_each(counter, func);
1557 else
1558 perf_counter_for_each_child(counter, func);
1559
1560 return 0;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001561}
1562
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001563/*
1564 * Callers need to ensure there can be no nesting of this function, otherwise
1565 * the seqlock logic goes bad. We can not serialize this because the arch
1566 * code calls this from NMI context.
1567 */
1568void perf_counter_update_userpage(struct perf_counter *counter)
Paul Mackerras37d81822009-03-23 18:22:08 +01001569{
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001570 struct perf_mmap_data *data;
1571 struct perf_counter_mmap_page *userpg;
1572
1573 rcu_read_lock();
1574 data = rcu_dereference(counter->data);
1575 if (!data)
1576 goto unlock;
1577
1578 userpg = data->user_page;
Paul Mackerras37d81822009-03-23 18:22:08 +01001579
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001580 /*
1581 * Disable preemption so as to not let the corresponding user-space
1582 * spin too long if we get preempted.
1583 */
1584 preempt_disable();
Paul Mackerras37d81822009-03-23 18:22:08 +01001585 ++userpg->lock;
Peter Zijlstra92f22a32009-04-02 11:12:04 +02001586 barrier();
Paul Mackerras37d81822009-03-23 18:22:08 +01001587 userpg->index = counter->hw.idx;
1588 userpg->offset = atomic64_read(&counter->count);
1589 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
1590 userpg->offset -= atomic64_read(&counter->hw.prev_count);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001591
Peter Zijlstra92f22a32009-04-02 11:12:04 +02001592 barrier();
Paul Mackerras37d81822009-03-23 18:22:08 +01001593 ++userpg->lock;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001594 preempt_enable();
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001595unlock:
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001596 rcu_read_unlock();
Paul Mackerras37d81822009-03-23 18:22:08 +01001597}
1598
1599static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1600{
1601 struct perf_counter *counter = vma->vm_file->private_data;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001602 struct perf_mmap_data *data;
1603 int ret = VM_FAULT_SIGBUS;
Paul Mackerras37d81822009-03-23 18:22:08 +01001604
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001605 rcu_read_lock();
1606 data = rcu_dereference(counter->data);
1607 if (!data)
1608 goto unlock;
Paul Mackerras37d81822009-03-23 18:22:08 +01001609
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001610 if (vmf->pgoff == 0) {
1611 vmf->page = virt_to_page(data->user_page);
1612 } else {
1613 int nr = vmf->pgoff - 1;
1614
1615 if ((unsigned)nr > data->nr_pages)
1616 goto unlock;
1617
1618 vmf->page = virt_to_page(data->data_pages[nr]);
1619 }
Paul Mackerras37d81822009-03-23 18:22:08 +01001620 get_page(vmf->page);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001621 ret = 0;
1622unlock:
1623 rcu_read_unlock();
1624
1625 return ret;
1626}
1627
1628static int perf_mmap_data_alloc(struct perf_counter *counter, int nr_pages)
1629{
1630 struct perf_mmap_data *data;
1631 unsigned long size;
1632 int i;
1633
1634 WARN_ON(atomic_read(&counter->mmap_count));
1635
1636 size = sizeof(struct perf_mmap_data);
1637 size += nr_pages * sizeof(void *);
1638
1639 data = kzalloc(size, GFP_KERNEL);
1640 if (!data)
1641 goto fail;
1642
1643 data->user_page = (void *)get_zeroed_page(GFP_KERNEL);
1644 if (!data->user_page)
1645 goto fail_user_page;
1646
1647 for (i = 0; i < nr_pages; i++) {
1648 data->data_pages[i] = (void *)get_zeroed_page(GFP_KERNEL);
1649 if (!data->data_pages[i])
1650 goto fail_data_pages;
1651 }
1652
1653 data->nr_pages = nr_pages;
Peter Zijlstra22c15582009-05-05 17:50:25 +02001654 atomic_set(&data->lock, -1);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001655
1656 rcu_assign_pointer(counter->data, data);
1657
Paul Mackerras37d81822009-03-23 18:22:08 +01001658 return 0;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001659
1660fail_data_pages:
1661 for (i--; i >= 0; i--)
1662 free_page((unsigned long)data->data_pages[i]);
1663
1664 free_page((unsigned long)data->user_page);
1665
1666fail_user_page:
1667 kfree(data);
1668
1669fail:
1670 return -ENOMEM;
1671}
1672
1673static void __perf_mmap_data_free(struct rcu_head *rcu_head)
1674{
1675 struct perf_mmap_data *data = container_of(rcu_head,
1676 struct perf_mmap_data, rcu_head);
1677 int i;
1678
1679 free_page((unsigned long)data->user_page);
1680 for (i = 0; i < data->nr_pages; i++)
1681 free_page((unsigned long)data->data_pages[i]);
1682 kfree(data);
1683}
1684
1685static void perf_mmap_data_free(struct perf_counter *counter)
1686{
1687 struct perf_mmap_data *data = counter->data;
1688
1689 WARN_ON(atomic_read(&counter->mmap_count));
1690
1691 rcu_assign_pointer(counter->data, NULL);
1692 call_rcu(&data->rcu_head, __perf_mmap_data_free);
1693}
1694
1695static void perf_mmap_open(struct vm_area_struct *vma)
1696{
1697 struct perf_counter *counter = vma->vm_file->private_data;
1698
1699 atomic_inc(&counter->mmap_count);
1700}
1701
1702static void perf_mmap_close(struct vm_area_struct *vma)
1703{
1704 struct perf_counter *counter = vma->vm_file->private_data;
1705
1706 if (atomic_dec_and_mutex_lock(&counter->mmap_count,
1707 &counter->mmap_mutex)) {
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001708 struct user_struct *user = current_user();
1709
1710 atomic_long_sub(counter->data->nr_pages + 1, &user->locked_vm);
Peter Zijlstrac5078f72009-05-05 17:50:24 +02001711 vma->vm_mm->locked_vm -= counter->data->nr_locked;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001712 perf_mmap_data_free(counter);
1713 mutex_unlock(&counter->mmap_mutex);
1714 }
Paul Mackerras37d81822009-03-23 18:22:08 +01001715}
1716
1717static struct vm_operations_struct perf_mmap_vmops = {
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001718 .open = perf_mmap_open,
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001719 .close = perf_mmap_close,
Paul Mackerras37d81822009-03-23 18:22:08 +01001720 .fault = perf_mmap_fault,
1721};
1722
1723static int perf_mmap(struct file *file, struct vm_area_struct *vma)
1724{
1725 struct perf_counter *counter = file->private_data;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001726 struct user_struct *user = current_user();
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001727 unsigned long vma_size;
1728 unsigned long nr_pages;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001729 unsigned long user_locked, user_lock_limit;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001730 unsigned long locked, lock_limit;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001731 long user_extra, extra;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001732 int ret = 0;
Paul Mackerras37d81822009-03-23 18:22:08 +01001733
1734 if (!(vma->vm_flags & VM_SHARED) || (vma->vm_flags & VM_WRITE))
1735 return -EINVAL;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001736
1737 vma_size = vma->vm_end - vma->vm_start;
1738 nr_pages = (vma_size / PAGE_SIZE) - 1;
1739
Peter Zijlstra7730d862009-03-25 12:48:31 +01001740 /*
1741 * If we have data pages ensure they're a power-of-two number, so we
1742 * can do bitmasks instead of modulo.
1743 */
1744 if (nr_pages != 0 && !is_power_of_2(nr_pages))
Paul Mackerras37d81822009-03-23 18:22:08 +01001745 return -EINVAL;
1746
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001747 if (vma_size != PAGE_SIZE * (1 + nr_pages))
Paul Mackerras37d81822009-03-23 18:22:08 +01001748 return -EINVAL;
1749
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001750 if (vma->vm_pgoff != 0)
1751 return -EINVAL;
Paul Mackerras37d81822009-03-23 18:22:08 +01001752
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001753 mutex_lock(&counter->mmap_mutex);
1754 if (atomic_inc_not_zero(&counter->mmap_count)) {
1755 if (nr_pages != counter->data->nr_pages)
1756 ret = -EINVAL;
1757 goto unlock;
1758 }
1759
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001760 user_extra = nr_pages + 1;
1761 user_lock_limit = sysctl_perf_counter_mlock >> (PAGE_SHIFT - 10);
1762 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
Peter Zijlstrac5078f72009-05-05 17:50:24 +02001763
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001764 extra = 0;
1765 if (user_locked > user_lock_limit)
1766 extra = user_locked - user_lock_limit;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001767
1768 lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
1769 lock_limit >>= PAGE_SHIFT;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001770 locked = vma->vm_mm->locked_vm + extra;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001771
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001772 if ((locked > lock_limit) && !capable(CAP_IPC_LOCK)) {
1773 ret = -EPERM;
1774 goto unlock;
1775 }
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001776
1777 WARN_ON(counter->data);
1778 ret = perf_mmap_data_alloc(counter, nr_pages);
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001779 if (ret)
1780 goto unlock;
1781
1782 atomic_set(&counter->mmap_count, 1);
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001783 atomic_long_add(user_extra, &user->locked_vm);
Peter Zijlstrac5078f72009-05-05 17:50:24 +02001784 vma->vm_mm->locked_vm += extra;
1785 counter->data->nr_locked = extra;
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001786unlock:
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001787 mutex_unlock(&counter->mmap_mutex);
Paul Mackerras37d81822009-03-23 18:22:08 +01001788
1789 vma->vm_flags &= ~VM_MAYWRITE;
1790 vma->vm_flags |= VM_RESERVED;
1791 vma->vm_ops = &perf_mmap_vmops;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001792
1793 return ret;
Paul Mackerras37d81822009-03-23 18:22:08 +01001794}
1795
Peter Zijlstra3c446b32009-04-06 11:45:01 +02001796static int perf_fasync(int fd, struct file *filp, int on)
1797{
1798 struct perf_counter *counter = filp->private_data;
1799 struct inode *inode = filp->f_path.dentry->d_inode;
1800 int retval;
1801
1802 mutex_lock(&inode->i_mutex);
1803 retval = fasync_helper(fd, filp, on, &counter->fasync);
1804 mutex_unlock(&inode->i_mutex);
1805
1806 if (retval < 0)
1807 return retval;
1808
1809 return 0;
1810}
1811
Thomas Gleixner0793a612008-12-04 20:12:29 +01001812static const struct file_operations perf_fops = {
1813 .release = perf_release,
1814 .read = perf_read,
1815 .poll = perf_poll,
Paul Mackerrasd859e292009-01-17 18:10:22 +11001816 .unlocked_ioctl = perf_ioctl,
1817 .compat_ioctl = perf_ioctl,
Paul Mackerras37d81822009-03-23 18:22:08 +01001818 .mmap = perf_mmap,
Peter Zijlstra3c446b32009-04-06 11:45:01 +02001819 .fasync = perf_fasync,
Thomas Gleixner0793a612008-12-04 20:12:29 +01001820};
1821
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001822/*
Peter Zijlstra925d5192009-03-30 19:07:02 +02001823 * Perf counter wakeup
1824 *
1825 * If there's data, ensure we set the poll() state and publish everything
1826 * to user-space before waking everybody up.
1827 */
1828
1829void perf_counter_wakeup(struct perf_counter *counter)
1830{
Peter Zijlstra925d5192009-03-30 19:07:02 +02001831 wake_up_all(&counter->waitq);
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02001832
1833 if (counter->pending_kill) {
1834 kill_fasync(&counter->fasync, SIGIO, counter->pending_kill);
1835 counter->pending_kill = 0;
1836 }
Peter Zijlstra925d5192009-03-30 19:07:02 +02001837}
1838
1839/*
1840 * Pending wakeups
1841 *
1842 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
1843 *
1844 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
1845 * single linked list and use cmpxchg() to add entries lockless.
1846 */
1847
Peter Zijlstra79f14642009-04-06 11:45:07 +02001848static void perf_pending_counter(struct perf_pending_entry *entry)
1849{
1850 struct perf_counter *counter = container_of(entry,
1851 struct perf_counter, pending);
1852
1853 if (counter->pending_disable) {
1854 counter->pending_disable = 0;
1855 perf_counter_disable(counter);
1856 }
1857
1858 if (counter->pending_wakeup) {
1859 counter->pending_wakeup = 0;
1860 perf_counter_wakeup(counter);
1861 }
1862}
1863
Peter Zijlstra671dec52009-04-06 11:45:02 +02001864#define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
Peter Zijlstra925d5192009-03-30 19:07:02 +02001865
Peter Zijlstra671dec52009-04-06 11:45:02 +02001866static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = {
Peter Zijlstra925d5192009-03-30 19:07:02 +02001867 PENDING_TAIL,
1868};
1869
Peter Zijlstra671dec52009-04-06 11:45:02 +02001870static void perf_pending_queue(struct perf_pending_entry *entry,
1871 void (*func)(struct perf_pending_entry *))
Peter Zijlstra925d5192009-03-30 19:07:02 +02001872{
Peter Zijlstra671dec52009-04-06 11:45:02 +02001873 struct perf_pending_entry **head;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001874
Peter Zijlstra671dec52009-04-06 11:45:02 +02001875 if (cmpxchg(&entry->next, NULL, PENDING_TAIL) != NULL)
Peter Zijlstra925d5192009-03-30 19:07:02 +02001876 return;
1877
Peter Zijlstra671dec52009-04-06 11:45:02 +02001878 entry->func = func;
1879
1880 head = &get_cpu_var(perf_pending_head);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001881
1882 do {
Peter Zijlstra671dec52009-04-06 11:45:02 +02001883 entry->next = *head;
1884 } while (cmpxchg(head, entry->next, entry) != entry->next);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001885
1886 set_perf_counter_pending();
1887
Peter Zijlstra671dec52009-04-06 11:45:02 +02001888 put_cpu_var(perf_pending_head);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001889}
1890
1891static int __perf_pending_run(void)
1892{
Peter Zijlstra671dec52009-04-06 11:45:02 +02001893 struct perf_pending_entry *list;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001894 int nr = 0;
1895
Peter Zijlstra671dec52009-04-06 11:45:02 +02001896 list = xchg(&__get_cpu_var(perf_pending_head), PENDING_TAIL);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001897 while (list != PENDING_TAIL) {
Peter Zijlstra671dec52009-04-06 11:45:02 +02001898 void (*func)(struct perf_pending_entry *);
1899 struct perf_pending_entry *entry = list;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001900
1901 list = list->next;
1902
Peter Zijlstra671dec52009-04-06 11:45:02 +02001903 func = entry->func;
1904 entry->next = NULL;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001905 /*
1906 * Ensure we observe the unqueue before we issue the wakeup,
1907 * so that we won't be waiting forever.
1908 * -- see perf_not_pending().
1909 */
1910 smp_wmb();
1911
Peter Zijlstra671dec52009-04-06 11:45:02 +02001912 func(entry);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001913 nr++;
1914 }
1915
1916 return nr;
1917}
1918
1919static inline int perf_not_pending(struct perf_counter *counter)
1920{
1921 /*
1922 * If we flush on whatever cpu we run, there is a chance we don't
1923 * need to wait.
1924 */
1925 get_cpu();
1926 __perf_pending_run();
1927 put_cpu();
1928
1929 /*
1930 * Ensure we see the proper queue state before going to sleep
1931 * so that we do not miss the wakeup. -- see perf_pending_handle()
1932 */
1933 smp_rmb();
Peter Zijlstra671dec52009-04-06 11:45:02 +02001934 return counter->pending.next == NULL;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001935}
1936
1937static void perf_pending_sync(struct perf_counter *counter)
1938{
1939 wait_event(counter->waitq, perf_not_pending(counter));
1940}
1941
1942void perf_counter_do_pending(void)
1943{
1944 __perf_pending_run();
1945}
1946
1947/*
Peter Zijlstra394ee072009-03-30 19:07:14 +02001948 * Callchain support -- arch specific
1949 */
1950
Peter Zijlstra9c03d882009-04-06 11:45:00 +02001951__weak struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
Peter Zijlstra394ee072009-03-30 19:07:14 +02001952{
1953 return NULL;
1954}
1955
1956/*
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001957 * Output
1958 */
1959
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001960struct perf_output_handle {
1961 struct perf_counter *counter;
1962 struct perf_mmap_data *data;
1963 unsigned int offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01001964 unsigned int head;
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001965 int nmi;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02001966 int overflow;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001967 int locked;
1968 unsigned long flags;
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001969};
1970
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001971static void perf_output_wakeup(struct perf_output_handle *handle)
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001972{
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001973 atomic_set(&handle->data->poll, POLL_IN);
1974
Peter Zijlstra671dec52009-04-06 11:45:02 +02001975 if (handle->nmi) {
Peter Zijlstra79f14642009-04-06 11:45:07 +02001976 handle->counter->pending_wakeup = 1;
Peter Zijlstra671dec52009-04-06 11:45:02 +02001977 perf_pending_queue(&handle->counter->pending,
Peter Zijlstra79f14642009-04-06 11:45:07 +02001978 perf_pending_counter);
Peter Zijlstra671dec52009-04-06 11:45:02 +02001979 } else
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001980 perf_counter_wakeup(handle->counter);
1981}
1982
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001983/*
1984 * Curious locking construct.
1985 *
1986 * We need to ensure a later event doesn't publish a head when a former
1987 * event isn't done writing. However since we need to deal with NMIs we
1988 * cannot fully serialize things.
1989 *
1990 * What we do is serialize between CPUs so we only have to deal with NMI
1991 * nesting on a single CPU.
1992 *
1993 * We only publish the head (and generate a wakeup) when the outer-most
1994 * event completes.
1995 */
1996static void perf_output_lock(struct perf_output_handle *handle)
1997{
1998 struct perf_mmap_data *data = handle->data;
1999 int cpu;
2000
2001 handle->locked = 0;
2002
2003 local_irq_save(handle->flags);
2004 cpu = smp_processor_id();
2005
2006 if (in_nmi() && atomic_read(&data->lock) == cpu)
2007 return;
2008
Peter Zijlstra22c15582009-05-05 17:50:25 +02002009 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002010 cpu_relax();
2011
2012 handle->locked = 1;
2013}
2014
2015static void perf_output_unlock(struct perf_output_handle *handle)
2016{
2017 struct perf_mmap_data *data = handle->data;
2018 int head, cpu;
2019
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002020 data->done_head = data->head;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002021
2022 if (!handle->locked)
2023 goto out;
2024
2025again:
2026 /*
2027 * The xchg implies a full barrier that ensures all writes are done
2028 * before we publish the new head, matched by a rmb() in userspace when
2029 * reading this position.
2030 */
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002031 while ((head = atomic_xchg(&data->done_head, 0)))
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002032 data->user_page->data_head = head;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002033
2034 /*
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002035 * NMI can happen here, which means we can miss a done_head update.
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002036 */
2037
Peter Zijlstra22c15582009-05-05 17:50:25 +02002038 cpu = atomic_xchg(&data->lock, -1);
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002039 WARN_ON_ONCE(cpu != smp_processor_id());
2040
2041 /*
2042 * Therefore we have to validate we did not indeed do so.
2043 */
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002044 if (unlikely(atomic_read(&data->done_head))) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002045 /*
2046 * Since we had it locked, we can lock it again.
2047 */
Peter Zijlstra22c15582009-05-05 17:50:25 +02002048 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002049 cpu_relax();
2050
2051 goto again;
2052 }
2053
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002054 if (atomic_xchg(&data->wakeup, 0))
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002055 perf_output_wakeup(handle);
2056out:
2057 local_irq_restore(handle->flags);
2058}
2059
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002060static int perf_output_begin(struct perf_output_handle *handle,
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002061 struct perf_counter *counter, unsigned int size,
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002062 int nmi, int overflow)
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002063{
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002064 struct perf_mmap_data *data;
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002065 unsigned int offset, head;
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002066
Peter Zijlstra2023b352009-05-05 17:50:26 +02002067 /*
2068 * For inherited counters we send all the output towards the parent.
2069 */
2070 if (counter->parent)
2071 counter = counter->parent;
2072
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002073 rcu_read_lock();
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002074 data = rcu_dereference(counter->data);
2075 if (!data)
2076 goto out;
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002077
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002078 handle->data = data;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002079 handle->counter = counter;
2080 handle->nmi = nmi;
2081 handle->overflow = overflow;
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002082
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002083 if (!data->nr_pages)
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002084 goto fail;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002085
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002086 perf_output_lock(handle);
2087
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002088 do {
2089 offset = head = atomic_read(&data->head);
Peter Zijlstrac7138f32009-03-24 13:18:16 +01002090 head += size;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002091 } while (atomic_cmpxchg(&data->head, offset, head) != offset);
2092
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002093 handle->offset = offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01002094 handle->head = head;
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002095
2096 if ((offset >> PAGE_SHIFT) != (head >> PAGE_SHIFT))
2097 atomic_set(&data->wakeup, 1);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002098
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002099 return 0;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002100
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002101fail:
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002102 perf_output_wakeup(handle);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002103out:
2104 rcu_read_unlock();
2105
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002106 return -ENOSPC;
2107}
2108
2109static void perf_output_copy(struct perf_output_handle *handle,
2110 void *buf, unsigned int len)
2111{
2112 unsigned int pages_mask;
2113 unsigned int offset;
2114 unsigned int size;
2115 void **pages;
2116
2117 offset = handle->offset;
2118 pages_mask = handle->data->nr_pages - 1;
2119 pages = handle->data->data_pages;
2120
2121 do {
2122 unsigned int page_offset;
2123 int nr;
2124
2125 nr = (offset >> PAGE_SHIFT) & pages_mask;
2126 page_offset = offset & (PAGE_SIZE - 1);
2127 size = min_t(unsigned int, PAGE_SIZE - page_offset, len);
2128
2129 memcpy(pages[nr] + page_offset, buf, size);
2130
2131 len -= size;
2132 buf += size;
2133 offset += size;
2134 } while (len);
2135
2136 handle->offset = offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01002137
Peter Zijlstra53020fe2009-05-13 21:26:19 +02002138 /*
2139 * Check we didn't copy past our reservation window, taking the
2140 * possible unsigned int wrap into account.
2141 */
2142 WARN_ON_ONCE(((int)(handle->head - handle->offset)) < 0);
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002143}
2144
Peter Zijlstra5c148192009-03-25 12:30:23 +01002145#define perf_output_put(handle, x) \
2146 perf_output_copy((handle), &(x), sizeof(x))
2147
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002148static void perf_output_end(struct perf_output_handle *handle)
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002149{
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002150 struct perf_counter *counter = handle->counter;
2151 struct perf_mmap_data *data = handle->data;
2152
2153 int wakeup_events = counter->hw_event.wakeup_events;
Peter Zijlstrac4578102009-04-02 11:12:01 +02002154
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002155 if (handle->overflow && wakeup_events) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002156 int events = atomic_inc_return(&data->events);
Peter Zijlstrac4578102009-04-02 11:12:01 +02002157 if (events >= wakeup_events) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002158 atomic_sub(wakeup_events, &data->events);
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002159 atomic_set(&data->wakeup, 1);
Peter Zijlstrac4578102009-04-02 11:12:01 +02002160 }
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002161 }
2162
2163 perf_output_unlock(handle);
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002164 rcu_read_unlock();
2165}
2166
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002167static void perf_counter_output(struct perf_counter *counter,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002168 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002169{
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002170 int ret;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002171 u64 record_type = counter->hw_event.record_type;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002172 struct perf_output_handle handle;
2173 struct perf_event_header header;
2174 u64 ip;
Peter Zijlstra5c148192009-03-25 12:30:23 +01002175 struct {
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002176 u32 pid, tid;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002177 } tid_entry;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002178 struct {
2179 u64 event;
2180 u64 counter;
2181 } group_entry;
Peter Zijlstra394ee072009-03-30 19:07:14 +02002182 struct perf_callchain_entry *callchain = NULL;
2183 int callchain_size = 0;
Peter Zijlstra339f7c92009-04-06 11:45:06 +02002184 u64 time;
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002185 struct {
2186 u32 cpu, reserved;
2187 } cpu_entry;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002188
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02002189 header.type = 0;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002190 header.size = sizeof(header);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002191
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02002192 header.misc = PERF_EVENT_MISC_OVERFLOW;
Paul Mackerras9d23a902009-05-14 21:48:08 +10002193 header.misc |= perf_misc_flags(regs);
Peter Zijlstra6fab0192009-04-08 15:01:26 +02002194
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002195 if (record_type & PERF_RECORD_IP) {
Paul Mackerras9d23a902009-05-14 21:48:08 +10002196 ip = perf_instruction_pointer(regs);
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02002197 header.type |= PERF_RECORD_IP;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002198 header.size += sizeof(ip);
2199 }
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002200
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002201 if (record_type & PERF_RECORD_TID) {
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002202 /* namespace issues */
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002203 tid_entry.pid = current->group_leader->pid;
2204 tid_entry.tid = current->pid;
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002205
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02002206 header.type |= PERF_RECORD_TID;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002207 header.size += sizeof(tid_entry);
2208 }
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002209
Peter Zijlstra4d855452009-04-08 15:01:32 +02002210 if (record_type & PERF_RECORD_TIME) {
2211 /*
2212 * Maybe do better on x86 and provide cpu_clock_nmi()
2213 */
2214 time = sched_clock();
2215
2216 header.type |= PERF_RECORD_TIME;
2217 header.size += sizeof(u64);
2218 }
2219
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002220 if (record_type & PERF_RECORD_ADDR) {
2221 header.type |= PERF_RECORD_ADDR;
2222 header.size += sizeof(u64);
2223 }
2224
Peter Zijlstraa85f61a2009-05-08 18:52:23 +02002225 if (record_type & PERF_RECORD_CONFIG) {
2226 header.type |= PERF_RECORD_CONFIG;
2227 header.size += sizeof(u64);
2228 }
2229
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002230 if (record_type & PERF_RECORD_CPU) {
2231 header.type |= PERF_RECORD_CPU;
2232 header.size += sizeof(cpu_entry);
2233
2234 cpu_entry.cpu = raw_smp_processor_id();
2235 }
2236
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002237 if (record_type & PERF_RECORD_GROUP) {
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02002238 header.type |= PERF_RECORD_GROUP;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002239 header.size += sizeof(u64) +
2240 counter->nr_siblings * sizeof(group_entry);
2241 }
2242
2243 if (record_type & PERF_RECORD_CALLCHAIN) {
Peter Zijlstra394ee072009-03-30 19:07:14 +02002244 callchain = perf_callchain(regs);
2245
2246 if (callchain) {
Peter Zijlstra9c03d882009-04-06 11:45:00 +02002247 callchain_size = (1 + callchain->nr) * sizeof(u64);
Peter Zijlstra394ee072009-03-30 19:07:14 +02002248
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02002249 header.type |= PERF_RECORD_CALLCHAIN;
Peter Zijlstra394ee072009-03-30 19:07:14 +02002250 header.size += callchain_size;
2251 }
2252 }
2253
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002254 ret = perf_output_begin(&handle, counter, header.size, nmi, 1);
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002255 if (ret)
2256 return;
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002257
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002258 perf_output_put(&handle, header);
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002259
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002260 if (record_type & PERF_RECORD_IP)
2261 perf_output_put(&handle, ip);
2262
2263 if (record_type & PERF_RECORD_TID)
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002264 perf_output_put(&handle, tid_entry);
2265
Peter Zijlstra4d855452009-04-08 15:01:32 +02002266 if (record_type & PERF_RECORD_TIME)
2267 perf_output_put(&handle, time);
2268
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002269 if (record_type & PERF_RECORD_ADDR)
2270 perf_output_put(&handle, addr);
2271
Peter Zijlstraa85f61a2009-05-08 18:52:23 +02002272 if (record_type & PERF_RECORD_CONFIG)
2273 perf_output_put(&handle, counter->hw_event.config);
2274
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002275 if (record_type & PERF_RECORD_CPU)
2276 perf_output_put(&handle, cpu_entry);
2277
Peter Zijlstra2023b352009-05-05 17:50:26 +02002278 /*
2279 * XXX PERF_RECORD_GROUP vs inherited counters seems difficult.
2280 */
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002281 if (record_type & PERF_RECORD_GROUP) {
2282 struct perf_counter *leader, *sub;
2283 u64 nr = counter->nr_siblings;
2284
2285 perf_output_put(&handle, nr);
2286
2287 leader = counter->group_leader;
2288 list_for_each_entry(sub, &leader->sibling_list, list_entry) {
2289 if (sub != counter)
Robert Richter4aeb0b42009-04-29 12:47:03 +02002290 sub->pmu->read(sub);
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002291
2292 group_entry.event = sub->hw_event.config;
2293 group_entry.counter = atomic64_read(&sub->count);
2294
2295 perf_output_put(&handle, group_entry);
2296 }
2297 }
2298
Peter Zijlstra394ee072009-03-30 19:07:14 +02002299 if (callchain)
2300 perf_output_copy(&handle, callchain, callchain_size);
2301
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002302 perf_output_end(&handle);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002303}
2304
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002305/*
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002306 * comm tracking
2307 */
2308
2309struct perf_comm_event {
2310 struct task_struct *task;
2311 char *comm;
2312 int comm_size;
2313
2314 struct {
2315 struct perf_event_header header;
2316
2317 u32 pid;
2318 u32 tid;
2319 } event;
2320};
2321
2322static void perf_counter_comm_output(struct perf_counter *counter,
2323 struct perf_comm_event *comm_event)
2324{
2325 struct perf_output_handle handle;
2326 int size = comm_event->event.header.size;
2327 int ret = perf_output_begin(&handle, counter, size, 0, 0);
2328
2329 if (ret)
2330 return;
2331
2332 perf_output_put(&handle, comm_event->event);
2333 perf_output_copy(&handle, comm_event->comm,
2334 comm_event->comm_size);
2335 perf_output_end(&handle);
2336}
2337
2338static int perf_counter_comm_match(struct perf_counter *counter,
2339 struct perf_comm_event *comm_event)
2340{
2341 if (counter->hw_event.comm &&
2342 comm_event->event.header.type == PERF_EVENT_COMM)
2343 return 1;
2344
2345 return 0;
2346}
2347
2348static void perf_counter_comm_ctx(struct perf_counter_context *ctx,
2349 struct perf_comm_event *comm_event)
2350{
2351 struct perf_counter *counter;
2352
2353 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2354 return;
2355
2356 rcu_read_lock();
2357 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2358 if (perf_counter_comm_match(counter, comm_event))
2359 perf_counter_comm_output(counter, comm_event);
2360 }
2361 rcu_read_unlock();
2362}
2363
2364static void perf_counter_comm_event(struct perf_comm_event *comm_event)
2365{
2366 struct perf_cpu_context *cpuctx;
2367 unsigned int size;
2368 char *comm = comm_event->task->comm;
2369
Ingo Molnar888fcee2009-04-09 09:48:22 +02002370 size = ALIGN(strlen(comm)+1, sizeof(u64));
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002371
2372 comm_event->comm = comm;
2373 comm_event->comm_size = size;
2374
2375 comm_event->event.header.size = sizeof(comm_event->event) + size;
2376
2377 cpuctx = &get_cpu_var(perf_cpu_context);
2378 perf_counter_comm_ctx(&cpuctx->ctx, comm_event);
2379 put_cpu_var(perf_cpu_context);
2380
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10002381 perf_counter_comm_ctx(current->perf_counter_ctxp, comm_event);
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002382}
2383
2384void perf_counter_comm(struct task_struct *task)
2385{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002386 struct perf_comm_event comm_event;
2387
2388 if (!atomic_read(&nr_comm_tracking))
2389 return;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10002390 if (!current->perf_counter_ctxp)
2391 return;
2392
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002393 comm_event = (struct perf_comm_event){
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002394 .task = task,
2395 .event = {
2396 .header = { .type = PERF_EVENT_COMM, },
2397 .pid = task->group_leader->pid,
2398 .tid = task->pid,
2399 },
2400 };
2401
2402 perf_counter_comm_event(&comm_event);
2403}
2404
2405/*
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002406 * mmap tracking
2407 */
2408
2409struct perf_mmap_event {
2410 struct file *file;
2411 char *file_name;
2412 int file_size;
2413
2414 struct {
2415 struct perf_event_header header;
2416
2417 u32 pid;
2418 u32 tid;
2419 u64 start;
2420 u64 len;
2421 u64 pgoff;
2422 } event;
2423};
2424
2425static void perf_counter_mmap_output(struct perf_counter *counter,
2426 struct perf_mmap_event *mmap_event)
2427{
2428 struct perf_output_handle handle;
2429 int size = mmap_event->event.header.size;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002430 int ret = perf_output_begin(&handle, counter, size, 0, 0);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002431
2432 if (ret)
2433 return;
2434
2435 perf_output_put(&handle, mmap_event->event);
2436 perf_output_copy(&handle, mmap_event->file_name,
2437 mmap_event->file_size);
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002438 perf_output_end(&handle);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002439}
2440
2441static int perf_counter_mmap_match(struct perf_counter *counter,
2442 struct perf_mmap_event *mmap_event)
2443{
2444 if (counter->hw_event.mmap &&
2445 mmap_event->event.header.type == PERF_EVENT_MMAP)
2446 return 1;
2447
2448 if (counter->hw_event.munmap &&
2449 mmap_event->event.header.type == PERF_EVENT_MUNMAP)
2450 return 1;
2451
2452 return 0;
2453}
2454
2455static void perf_counter_mmap_ctx(struct perf_counter_context *ctx,
2456 struct perf_mmap_event *mmap_event)
2457{
2458 struct perf_counter *counter;
2459
2460 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2461 return;
2462
2463 rcu_read_lock();
2464 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2465 if (perf_counter_mmap_match(counter, mmap_event))
2466 perf_counter_mmap_output(counter, mmap_event);
2467 }
2468 rcu_read_unlock();
2469}
2470
2471static void perf_counter_mmap_event(struct perf_mmap_event *mmap_event)
2472{
2473 struct perf_cpu_context *cpuctx;
2474 struct file *file = mmap_event->file;
2475 unsigned int size;
2476 char tmp[16];
2477 char *buf = NULL;
2478 char *name;
2479
2480 if (file) {
2481 buf = kzalloc(PATH_MAX, GFP_KERNEL);
2482 if (!buf) {
2483 name = strncpy(tmp, "//enomem", sizeof(tmp));
2484 goto got_name;
2485 }
Peter Zijlstrad3d21c42009-04-09 10:53:46 +02002486 name = d_path(&file->f_path, buf, PATH_MAX);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002487 if (IS_ERR(name)) {
2488 name = strncpy(tmp, "//toolong", sizeof(tmp));
2489 goto got_name;
2490 }
2491 } else {
2492 name = strncpy(tmp, "//anon", sizeof(tmp));
2493 goto got_name;
2494 }
2495
2496got_name:
Ingo Molnar888fcee2009-04-09 09:48:22 +02002497 size = ALIGN(strlen(name)+1, sizeof(u64));
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002498
2499 mmap_event->file_name = name;
2500 mmap_event->file_size = size;
2501
2502 mmap_event->event.header.size = sizeof(mmap_event->event) + size;
2503
2504 cpuctx = &get_cpu_var(perf_cpu_context);
2505 perf_counter_mmap_ctx(&cpuctx->ctx, mmap_event);
2506 put_cpu_var(perf_cpu_context);
2507
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10002508 perf_counter_mmap_ctx(current->perf_counter_ctxp, mmap_event);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002509
2510 kfree(buf);
2511}
2512
2513void perf_counter_mmap(unsigned long addr, unsigned long len,
2514 unsigned long pgoff, struct file *file)
2515{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002516 struct perf_mmap_event mmap_event;
2517
2518 if (!atomic_read(&nr_mmap_tracking))
2519 return;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10002520 if (!current->perf_counter_ctxp)
2521 return;
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002522
2523 mmap_event = (struct perf_mmap_event){
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002524 .file = file,
2525 .event = {
2526 .header = { .type = PERF_EVENT_MMAP, },
2527 .pid = current->group_leader->pid,
2528 .tid = current->pid,
2529 .start = addr,
2530 .len = len,
2531 .pgoff = pgoff,
2532 },
2533 };
2534
2535 perf_counter_mmap_event(&mmap_event);
2536}
2537
2538void perf_counter_munmap(unsigned long addr, unsigned long len,
2539 unsigned long pgoff, struct file *file)
2540{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002541 struct perf_mmap_event mmap_event;
2542
2543 if (!atomic_read(&nr_munmap_tracking))
2544 return;
2545
2546 mmap_event = (struct perf_mmap_event){
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002547 .file = file,
2548 .event = {
2549 .header = { .type = PERF_EVENT_MUNMAP, },
2550 .pid = current->group_leader->pid,
2551 .tid = current->pid,
2552 .start = addr,
2553 .len = len,
2554 .pgoff = pgoff,
2555 },
2556 };
2557
2558 perf_counter_mmap_event(&mmap_event);
2559}
2560
2561/*
Peter Zijlstrae220d2d2009-05-23 18:28:55 +02002562 * Log irq_period changes so that analyzing tools can re-normalize the
2563 * event flow.
Peter Zijlstra26b119b2009-05-20 12:21:20 +02002564 */
2565
2566static void perf_log_period(struct perf_counter *counter, u64 period)
2567{
2568 struct perf_output_handle handle;
2569 int ret;
2570
2571 struct {
2572 struct perf_event_header header;
2573 u64 time;
2574 u64 period;
2575 } freq_event = {
2576 .header = {
2577 .type = PERF_EVENT_PERIOD,
2578 .misc = 0,
2579 .size = sizeof(freq_event),
2580 },
2581 .time = sched_clock(),
2582 .period = period,
2583 };
2584
2585 if (counter->hw.irq_period == period)
2586 return;
2587
2588 ret = perf_output_begin(&handle, counter, sizeof(freq_event), 0, 0);
2589 if (ret)
2590 return;
2591
2592 perf_output_put(&handle, freq_event);
2593 perf_output_end(&handle);
2594}
2595
2596/*
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002597 * Generic counter overflow handling.
2598 */
2599
2600int perf_counter_overflow(struct perf_counter *counter,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002601 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002602{
Peter Zijlstra79f14642009-04-06 11:45:07 +02002603 int events = atomic_read(&counter->event_limit);
2604 int ret = 0;
2605
Peter Zijlstra60db5e02009-05-15 15:19:28 +02002606 counter->hw.interrupts++;
2607
Peter Zijlstra2023b352009-05-05 17:50:26 +02002608 /*
2609 * XXX event_limit might not quite work as expected on inherited
2610 * counters
2611 */
2612
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002613 counter->pending_kill = POLL_IN;
Peter Zijlstra79f14642009-04-06 11:45:07 +02002614 if (events && atomic_dec_and_test(&counter->event_limit)) {
2615 ret = 1;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002616 counter->pending_kill = POLL_HUP;
Peter Zijlstra79f14642009-04-06 11:45:07 +02002617 if (nmi) {
2618 counter->pending_disable = 1;
2619 perf_pending_queue(&counter->pending,
2620 perf_pending_counter);
2621 } else
2622 perf_counter_disable(counter);
2623 }
2624
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002625 perf_counter_output(counter, nmi, regs, addr);
Peter Zijlstra79f14642009-04-06 11:45:07 +02002626 return ret;
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002627}
2628
2629/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002630 * Generic software counter infrastructure
2631 */
2632
2633static void perf_swcounter_update(struct perf_counter *counter)
2634{
2635 struct hw_perf_counter *hwc = &counter->hw;
2636 u64 prev, now;
2637 s64 delta;
2638
2639again:
2640 prev = atomic64_read(&hwc->prev_count);
2641 now = atomic64_read(&hwc->count);
2642 if (atomic64_cmpxchg(&hwc->prev_count, prev, now) != prev)
2643 goto again;
2644
2645 delta = now - prev;
2646
2647 atomic64_add(delta, &counter->count);
2648 atomic64_sub(delta, &hwc->period_left);
2649}
2650
2651static void perf_swcounter_set_period(struct perf_counter *counter)
2652{
2653 struct hw_perf_counter *hwc = &counter->hw;
2654 s64 left = atomic64_read(&hwc->period_left);
2655 s64 period = hwc->irq_period;
2656
2657 if (unlikely(left <= -period)) {
2658 left = period;
2659 atomic64_set(&hwc->period_left, left);
2660 }
2661
2662 if (unlikely(left <= 0)) {
2663 left += period;
2664 atomic64_add(period, &hwc->period_left);
2665 }
2666
2667 atomic64_set(&hwc->prev_count, -left);
2668 atomic64_set(&hwc->count, -left);
2669}
2670
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002671static enum hrtimer_restart perf_swcounter_hrtimer(struct hrtimer *hrtimer)
2672{
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002673 enum hrtimer_restart ret = HRTIMER_RESTART;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002674 struct perf_counter *counter;
2675 struct pt_regs *regs;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02002676 u64 period;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002677
2678 counter = container_of(hrtimer, struct perf_counter, hw.hrtimer);
Robert Richter4aeb0b42009-04-29 12:47:03 +02002679 counter->pmu->read(counter);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002680
2681 regs = get_irq_regs();
2682 /*
2683 * In case we exclude kernel IPs or are somehow not in interrupt
2684 * context, provide the next best thing, the user IP.
2685 */
2686 if ((counter->hw_event.exclude_kernel || !regs) &&
2687 !counter->hw_event.exclude_user)
2688 regs = task_pt_regs(current);
2689
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002690 if (regs) {
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002691 if (perf_counter_overflow(counter, 0, regs, 0))
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002692 ret = HRTIMER_NORESTART;
2693 }
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002694
Peter Zijlstra60db5e02009-05-15 15:19:28 +02002695 period = max_t(u64, 10000, counter->hw.irq_period);
2696 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002697
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002698 return ret;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002699}
2700
2701static void perf_swcounter_overflow(struct perf_counter *counter,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002702 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002703{
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002704 perf_swcounter_update(counter);
2705 perf_swcounter_set_period(counter);
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002706 if (perf_counter_overflow(counter, nmi, regs, addr))
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002707 /* soft-disable the counter */
2708 ;
2709
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002710}
2711
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002712static int perf_swcounter_match(struct perf_counter *counter,
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002713 enum perf_event_types type,
2714 u32 event, struct pt_regs *regs)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002715{
2716 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
2717 return 0;
2718
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002719 if (perf_event_raw(&counter->hw_event))
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002720 return 0;
2721
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002722 if (perf_event_type(&counter->hw_event) != type)
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002723 return 0;
2724
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002725 if (perf_event_id(&counter->hw_event) != event)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002726 return 0;
2727
2728 if (counter->hw_event.exclude_user && user_mode(regs))
2729 return 0;
2730
2731 if (counter->hw_event.exclude_kernel && !user_mode(regs))
2732 return 0;
2733
2734 return 1;
2735}
2736
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002737static void perf_swcounter_add(struct perf_counter *counter, u64 nr,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002738 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002739{
2740 int neg = atomic64_add_negative(nr, &counter->hw.count);
2741 if (counter->hw.irq_period && !neg)
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002742 perf_swcounter_overflow(counter, nmi, regs, addr);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002743}
2744
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002745static void perf_swcounter_ctx_event(struct perf_counter_context *ctx,
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002746 enum perf_event_types type, u32 event,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002747 u64 nr, int nmi, struct pt_regs *regs,
2748 u64 addr)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002749{
2750 struct perf_counter *counter;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002751
Peter Zijlstra01ef09d2009-03-19 20:26:11 +01002752 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002753 return;
2754
Peter Zijlstra592903c2009-03-13 12:21:36 +01002755 rcu_read_lock();
2756 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002757 if (perf_swcounter_match(counter, type, event, regs))
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002758 perf_swcounter_add(counter, nr, nmi, regs, addr);
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002759 }
Peter Zijlstra592903c2009-03-13 12:21:36 +01002760 rcu_read_unlock();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002761}
2762
Peter Zijlstra96f6d442009-03-23 18:22:07 +01002763static int *perf_swcounter_recursion_context(struct perf_cpu_context *cpuctx)
2764{
2765 if (in_nmi())
2766 return &cpuctx->recursion[3];
2767
2768 if (in_irq())
2769 return &cpuctx->recursion[2];
2770
2771 if (in_softirq())
2772 return &cpuctx->recursion[1];
2773
2774 return &cpuctx->recursion[0];
2775}
2776
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002777static void __perf_swcounter_event(enum perf_event_types type, u32 event,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002778 u64 nr, int nmi, struct pt_regs *regs,
2779 u64 addr)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002780{
2781 struct perf_cpu_context *cpuctx = &get_cpu_var(perf_cpu_context);
Peter Zijlstra96f6d442009-03-23 18:22:07 +01002782 int *recursion = perf_swcounter_recursion_context(cpuctx);
2783
2784 if (*recursion)
2785 goto out;
2786
2787 (*recursion)++;
2788 barrier();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002789
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002790 perf_swcounter_ctx_event(&cpuctx->ctx, type, event,
2791 nr, nmi, regs, addr);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002792 if (cpuctx->task_ctx) {
2793 perf_swcounter_ctx_event(cpuctx->task_ctx, type, event,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002794 nr, nmi, regs, addr);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002795 }
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002796
Peter Zijlstra96f6d442009-03-23 18:22:07 +01002797 barrier();
2798 (*recursion)--;
2799
2800out:
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002801 put_cpu_var(perf_cpu_context);
2802}
2803
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002804void
2805perf_swcounter_event(u32 event, u64 nr, int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002806{
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002807 __perf_swcounter_event(PERF_TYPE_SOFTWARE, event, nr, nmi, regs, addr);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002808}
2809
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002810static void perf_swcounter_read(struct perf_counter *counter)
2811{
2812 perf_swcounter_update(counter);
2813}
2814
2815static int perf_swcounter_enable(struct perf_counter *counter)
2816{
2817 perf_swcounter_set_period(counter);
2818 return 0;
2819}
2820
2821static void perf_swcounter_disable(struct perf_counter *counter)
2822{
2823 perf_swcounter_update(counter);
2824}
2825
Robert Richter4aeb0b42009-04-29 12:47:03 +02002826static const struct pmu perf_ops_generic = {
Peter Zijlstraac17dc82009-03-13 12:21:34 +01002827 .enable = perf_swcounter_enable,
2828 .disable = perf_swcounter_disable,
2829 .read = perf_swcounter_read,
2830};
2831
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002832/*
2833 * Software counter: cpu wall time clock
2834 */
2835
Paul Mackerras9abf8a02009-01-09 16:26:43 +11002836static void cpu_clock_perf_counter_update(struct perf_counter *counter)
2837{
2838 int cpu = raw_smp_processor_id();
2839 s64 prev;
2840 u64 now;
2841
2842 now = cpu_clock(cpu);
2843 prev = atomic64_read(&counter->hw.prev_count);
2844 atomic64_set(&counter->hw.prev_count, now);
2845 atomic64_add(now - prev, &counter->count);
2846}
2847
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002848static int cpu_clock_perf_counter_enable(struct perf_counter *counter)
2849{
2850 struct hw_perf_counter *hwc = &counter->hw;
2851 int cpu = raw_smp_processor_id();
2852
2853 atomic64_set(&hwc->prev_count, cpu_clock(cpu));
Peter Zijlstra039fc912009-03-13 16:43:47 +01002854 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2855 hwc->hrtimer.function = perf_swcounter_hrtimer;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002856 if (hwc->irq_period) {
Peter Zijlstra60db5e02009-05-15 15:19:28 +02002857 u64 period = max_t(u64, 10000, hwc->irq_period);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002858 __hrtimer_start_range_ns(&hwc->hrtimer,
Peter Zijlstra60db5e02009-05-15 15:19:28 +02002859 ns_to_ktime(period), 0,
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002860 HRTIMER_MODE_REL, 0);
2861 }
2862
2863 return 0;
2864}
2865
Ingo Molnar5c92d122008-12-11 13:21:10 +01002866static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
2867{
Peter Zijlstrab986d7e2009-05-20 12:21:21 +02002868 if (counter->hw.irq_period)
2869 hrtimer_cancel(&counter->hw.hrtimer);
Paul Mackerras9abf8a02009-01-09 16:26:43 +11002870 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01002871}
2872
2873static void cpu_clock_perf_counter_read(struct perf_counter *counter)
2874{
Paul Mackerras9abf8a02009-01-09 16:26:43 +11002875 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01002876}
2877
Robert Richter4aeb0b42009-04-29 12:47:03 +02002878static const struct pmu perf_ops_cpu_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +01002879 .enable = cpu_clock_perf_counter_enable,
2880 .disable = cpu_clock_perf_counter_disable,
2881 .read = cpu_clock_perf_counter_read,
Ingo Molnar5c92d122008-12-11 13:21:10 +01002882};
2883
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01002884/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002885 * Software counter: task time clock
2886 */
2887
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02002888static void task_clock_perf_counter_update(struct perf_counter *counter, u64 now)
Ingo Molnarbae43c92008-12-11 14:03:20 +01002889{
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02002890 u64 prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002891 s64 delta;
Ingo Molnarbae43c92008-12-11 14:03:20 +01002892
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02002893 prev = atomic64_xchg(&counter->hw.prev_count, now);
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002894 delta = now - prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002895 atomic64_add(delta, &counter->count);
Ingo Molnarbae43c92008-12-11 14:03:20 +01002896}
2897
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01002898static int task_clock_perf_counter_enable(struct perf_counter *counter)
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002899{
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002900 struct hw_perf_counter *hwc = &counter->hw;
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02002901 u64 now;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002902
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02002903 now = counter->ctx->time;
2904
2905 atomic64_set(&hwc->prev_count, now);
Peter Zijlstra039fc912009-03-13 16:43:47 +01002906 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2907 hwc->hrtimer.function = perf_swcounter_hrtimer;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002908 if (hwc->irq_period) {
Peter Zijlstra60db5e02009-05-15 15:19:28 +02002909 u64 period = max_t(u64, 10000, hwc->irq_period);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002910 __hrtimer_start_range_ns(&hwc->hrtimer,
Peter Zijlstra60db5e02009-05-15 15:19:28 +02002911 ns_to_ktime(period), 0,
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002912 HRTIMER_MODE_REL, 0);
2913 }
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01002914
2915 return 0;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002916}
2917
2918static void task_clock_perf_counter_disable(struct perf_counter *counter)
2919{
Peter Zijlstrab986d7e2009-05-20 12:21:21 +02002920 if (counter->hw.irq_period)
2921 hrtimer_cancel(&counter->hw.hrtimer);
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02002922 task_clock_perf_counter_update(counter, counter->ctx->time);
2923
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002924}
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01002925
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002926static void task_clock_perf_counter_read(struct perf_counter *counter)
2927{
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02002928 u64 time;
2929
2930 if (!in_nmi()) {
2931 update_context_time(counter->ctx);
2932 time = counter->ctx->time;
2933 } else {
2934 u64 now = perf_clock();
2935 u64 delta = now - counter->ctx->timestamp;
2936 time = counter->ctx->time + delta;
2937 }
2938
2939 task_clock_perf_counter_update(counter, time);
Ingo Molnarbae43c92008-12-11 14:03:20 +01002940}
2941
Robert Richter4aeb0b42009-04-29 12:47:03 +02002942static const struct pmu perf_ops_task_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +01002943 .enable = task_clock_perf_counter_enable,
2944 .disable = task_clock_perf_counter_disable,
2945 .read = task_clock_perf_counter_read,
Ingo Molnarbae43c92008-12-11 14:03:20 +01002946};
2947
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002948/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002949 * Software counter: cpu migrations
2950 */
2951
Paul Mackerras23a185c2009-02-09 22:42:47 +11002952static inline u64 get_cpu_migrations(struct perf_counter *counter)
Ingo Molnar6c594c22008-12-14 12:34:15 +01002953{
Paul Mackerras23a185c2009-02-09 22:42:47 +11002954 struct task_struct *curr = counter->ctx->task;
2955
2956 if (curr)
2957 return curr->se.nr_migrations;
2958 return cpu_nr_migrations(smp_processor_id());
Ingo Molnar6c594c22008-12-14 12:34:15 +01002959}
2960
2961static void cpu_migrations_perf_counter_update(struct perf_counter *counter)
2962{
2963 u64 prev, now;
2964 s64 delta;
2965
2966 prev = atomic64_read(&counter->hw.prev_count);
Paul Mackerras23a185c2009-02-09 22:42:47 +11002967 now = get_cpu_migrations(counter);
Ingo Molnar6c594c22008-12-14 12:34:15 +01002968
2969 atomic64_set(&counter->hw.prev_count, now);
2970
2971 delta = now - prev;
Ingo Molnar6c594c22008-12-14 12:34:15 +01002972
2973 atomic64_add(delta, &counter->count);
2974}
2975
2976static void cpu_migrations_perf_counter_read(struct perf_counter *counter)
2977{
2978 cpu_migrations_perf_counter_update(counter);
2979}
2980
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01002981static int cpu_migrations_perf_counter_enable(struct perf_counter *counter)
Ingo Molnar6c594c22008-12-14 12:34:15 +01002982{
Paul Mackerrasc07c99b2009-02-13 22:10:34 +11002983 if (counter->prev_state <= PERF_COUNTER_STATE_OFF)
2984 atomic64_set(&counter->hw.prev_count,
2985 get_cpu_migrations(counter));
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01002986 return 0;
Ingo Molnar6c594c22008-12-14 12:34:15 +01002987}
2988
2989static void cpu_migrations_perf_counter_disable(struct perf_counter *counter)
2990{
2991 cpu_migrations_perf_counter_update(counter);
2992}
2993
Robert Richter4aeb0b42009-04-29 12:47:03 +02002994static const struct pmu perf_ops_cpu_migrations = {
Ingo Molnar76715812008-12-17 14:20:28 +01002995 .enable = cpu_migrations_perf_counter_enable,
2996 .disable = cpu_migrations_perf_counter_disable,
2997 .read = cpu_migrations_perf_counter_read,
Ingo Molnar6c594c22008-12-14 12:34:15 +01002998};
2999
Peter Zijlstrae077df42009-03-19 20:26:17 +01003000#ifdef CONFIG_EVENT_PROFILE
3001void perf_tpcounter_event(int event_id)
3002{
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003003 struct pt_regs *regs = get_irq_regs();
3004
3005 if (!regs)
3006 regs = task_pt_regs(current);
3007
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003008 __perf_swcounter_event(PERF_TYPE_TRACEPOINT, event_id, 1, 1, regs, 0);
Peter Zijlstrae077df42009-03-19 20:26:17 +01003009}
Steven Whitehouseff7b1b42009-04-15 16:55:05 +01003010EXPORT_SYMBOL_GPL(perf_tpcounter_event);
Peter Zijlstrae077df42009-03-19 20:26:17 +01003011
3012extern int ftrace_profile_enable(int);
3013extern void ftrace_profile_disable(int);
3014
3015static void tp_perf_counter_destroy(struct perf_counter *counter)
3016{
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01003017 ftrace_profile_disable(perf_event_id(&counter->hw_event));
Peter Zijlstrae077df42009-03-19 20:26:17 +01003018}
3019
Robert Richter4aeb0b42009-04-29 12:47:03 +02003020static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
Peter Zijlstrae077df42009-03-19 20:26:17 +01003021{
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01003022 int event_id = perf_event_id(&counter->hw_event);
Peter Zijlstrae077df42009-03-19 20:26:17 +01003023 int ret;
3024
3025 ret = ftrace_profile_enable(event_id);
3026 if (ret)
3027 return NULL;
3028
3029 counter->destroy = tp_perf_counter_destroy;
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003030 counter->hw.irq_period = counter->hw_event.irq_period;
Peter Zijlstrae077df42009-03-19 20:26:17 +01003031
3032 return &perf_ops_generic;
3033}
3034#else
Robert Richter4aeb0b42009-04-29 12:47:03 +02003035static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
Peter Zijlstrae077df42009-03-19 20:26:17 +01003036{
3037 return NULL;
3038}
3039#endif
3040
Robert Richter4aeb0b42009-04-29 12:47:03 +02003041static const struct pmu *sw_perf_counter_init(struct perf_counter *counter)
Ingo Molnar5c92d122008-12-11 13:21:10 +01003042{
Robert Richter4aeb0b42009-04-29 12:47:03 +02003043 const struct pmu *pmu = NULL;
Ingo Molnar5c92d122008-12-11 13:21:10 +01003044
Paul Mackerras0475f9e2009-02-11 14:35:35 +11003045 /*
3046 * Software counters (currently) can't in general distinguish
3047 * between user, kernel and hypervisor events.
3048 * However, context switches and cpu migrations are considered
3049 * to be kernel events, and page faults are never hypervisor
3050 * events.
3051 */
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01003052 switch (perf_event_id(&counter->hw_event)) {
Ingo Molnar5c92d122008-12-11 13:21:10 +01003053 case PERF_COUNT_CPU_CLOCK:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003054 pmu = &perf_ops_cpu_clock;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003055
Ingo Molnar5c92d122008-12-11 13:21:10 +01003056 break;
Ingo Molnarbae43c92008-12-11 14:03:20 +01003057 case PERF_COUNT_TASK_CLOCK:
Paul Mackerras23a185c2009-02-09 22:42:47 +11003058 /*
3059 * If the user instantiates this as a per-cpu counter,
3060 * use the cpu_clock counter instead.
3061 */
3062 if (counter->ctx->task)
Robert Richter4aeb0b42009-04-29 12:47:03 +02003063 pmu = &perf_ops_task_clock;
Paul Mackerras23a185c2009-02-09 22:42:47 +11003064 else
Robert Richter4aeb0b42009-04-29 12:47:03 +02003065 pmu = &perf_ops_cpu_clock;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003066
Ingo Molnarbae43c92008-12-11 14:03:20 +01003067 break;
Ingo Molnare06c61a2008-12-14 14:44:31 +01003068 case PERF_COUNT_PAGE_FAULTS:
Peter Zijlstraac17dc82009-03-13 12:21:34 +01003069 case PERF_COUNT_PAGE_FAULTS_MIN:
3070 case PERF_COUNT_PAGE_FAULTS_MAJ:
Ingo Molnar5d6a27d2008-12-14 12:28:33 +01003071 case PERF_COUNT_CONTEXT_SWITCHES:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003072 pmu = &perf_ops_generic;
Ingo Molnar5d6a27d2008-12-14 12:28:33 +01003073 break;
Ingo Molnar6c594c22008-12-14 12:34:15 +01003074 case PERF_COUNT_CPU_MIGRATIONS:
Paul Mackerras0475f9e2009-02-11 14:35:35 +11003075 if (!counter->hw_event.exclude_kernel)
Robert Richter4aeb0b42009-04-29 12:47:03 +02003076 pmu = &perf_ops_cpu_migrations;
Ingo Molnar6c594c22008-12-14 12:34:15 +01003077 break;
Ingo Molnar5c92d122008-12-11 13:21:10 +01003078 }
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003079
Robert Richter4aeb0b42009-04-29 12:47:03 +02003080 return pmu;
Ingo Molnar5c92d122008-12-11 13:21:10 +01003081}
3082
Thomas Gleixner0793a612008-12-04 20:12:29 +01003083/*
3084 * Allocate and initialize a counter structure
3085 */
3086static struct perf_counter *
Ingo Molnar04289bb2008-12-11 08:38:42 +01003087perf_counter_alloc(struct perf_counter_hw_event *hw_event,
3088 int cpu,
Paul Mackerras23a185c2009-02-09 22:42:47 +11003089 struct perf_counter_context *ctx,
Ingo Molnar9b51f662008-12-12 13:49:45 +01003090 struct perf_counter *group_leader,
3091 gfp_t gfpflags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003092{
Robert Richter4aeb0b42009-04-29 12:47:03 +02003093 const struct pmu *pmu;
Ingo Molnar621a01e2008-12-11 12:46:46 +01003094 struct perf_counter *counter;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003095 struct hw_perf_counter *hwc;
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003096 long err;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003097
Ingo Molnar9b51f662008-12-12 13:49:45 +01003098 counter = kzalloc(sizeof(*counter), gfpflags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003099 if (!counter)
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003100 return ERR_PTR(-ENOMEM);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003101
Ingo Molnar04289bb2008-12-11 08:38:42 +01003102 /*
3103 * Single counters are their own group leaders, with an
3104 * empty sibling list:
3105 */
3106 if (!group_leader)
3107 group_leader = counter;
3108
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003109 mutex_init(&counter->child_mutex);
3110 INIT_LIST_HEAD(&counter->child_list);
3111
Ingo Molnar04289bb2008-12-11 08:38:42 +01003112 INIT_LIST_HEAD(&counter->list_entry);
Peter Zijlstra592903c2009-03-13 12:21:36 +01003113 INIT_LIST_HEAD(&counter->event_entry);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003114 INIT_LIST_HEAD(&counter->sibling_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003115 init_waitqueue_head(&counter->waitq);
3116
Peter Zijlstra7b732a72009-03-23 18:22:10 +01003117 mutex_init(&counter->mmap_mutex);
3118
Ingo Molnar9f66a382008-12-10 12:33:23 +01003119 counter->cpu = cpu;
3120 counter->hw_event = *hw_event;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003121 counter->group_leader = group_leader;
Robert Richter4aeb0b42009-04-29 12:47:03 +02003122 counter->pmu = NULL;
Paul Mackerras23a185c2009-02-09 22:42:47 +11003123 counter->ctx = ctx;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003124 get_ctx(ctx);
Ingo Molnar621a01e2008-12-11 12:46:46 +01003125
Ingo Molnar235c7fc2008-12-21 14:43:25 +01003126 counter->state = PERF_COUNTER_STATE_INACTIVE;
Ingo Molnara86ed502008-12-17 00:43:10 +01003127 if (hw_event->disabled)
3128 counter->state = PERF_COUNTER_STATE_OFF;
3129
Robert Richter4aeb0b42009-04-29 12:47:03 +02003130 pmu = NULL;
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003131
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003132 hwc = &counter->hw;
3133 if (hw_event->freq && hw_event->irq_freq)
Peter Zijlstra2e569d32009-05-15 15:37:47 +02003134 hwc->irq_period = div64_u64(TICK_NSEC, hw_event->irq_freq);
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003135 else
3136 hwc->irq_period = hw_event->irq_period;
3137
Peter Zijlstra2023b352009-05-05 17:50:26 +02003138 /*
3139 * we currently do not support PERF_RECORD_GROUP on inherited counters
3140 */
3141 if (hw_event->inherit && (hw_event->record_type & PERF_RECORD_GROUP))
3142 goto done;
3143
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01003144 if (perf_event_raw(hw_event)) {
Robert Richter4aeb0b42009-04-29 12:47:03 +02003145 pmu = hw_perf_counter_init(counter);
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01003146 goto done;
3147 }
3148
3149 switch (perf_event_type(hw_event)) {
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003150 case PERF_TYPE_HARDWARE:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003151 pmu = hw_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003152 break;
3153
3154 case PERF_TYPE_SOFTWARE:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003155 pmu = sw_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003156 break;
3157
3158 case PERF_TYPE_TRACEPOINT:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003159 pmu = tp_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003160 break;
3161 }
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01003162done:
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003163 err = 0;
Robert Richter4aeb0b42009-04-29 12:47:03 +02003164 if (!pmu)
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003165 err = -EINVAL;
Robert Richter4aeb0b42009-04-29 12:47:03 +02003166 else if (IS_ERR(pmu))
3167 err = PTR_ERR(pmu);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003168
3169 if (err) {
3170 kfree(counter);
3171 return ERR_PTR(err);
3172 }
3173
Robert Richter4aeb0b42009-04-29 12:47:03 +02003174 counter->pmu = pmu;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003175
Peter Zijlstra7fc23a52009-05-08 18:52:21 +02003176 atomic_inc(&nr_counters);
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02003177 if (counter->hw_event.mmap)
3178 atomic_inc(&nr_mmap_tracking);
3179 if (counter->hw_event.munmap)
3180 atomic_inc(&nr_munmap_tracking);
3181 if (counter->hw_event.comm)
3182 atomic_inc(&nr_comm_tracking);
3183
Thomas Gleixner0793a612008-12-04 20:12:29 +01003184 return counter;
3185}
3186
3187/**
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003188 * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
Ingo Molnar9f66a382008-12-10 12:33:23 +01003189 *
3190 * @hw_event_uptr: event type attributes for monitoring/sampling
Thomas Gleixner0793a612008-12-04 20:12:29 +01003191 * @pid: target pid
Ingo Molnar9f66a382008-12-10 12:33:23 +01003192 * @cpu: target cpu
3193 * @group_fd: group leader counter fd
Thomas Gleixner0793a612008-12-04 20:12:29 +01003194 */
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003195SYSCALL_DEFINE5(perf_counter_open,
Paul Mackerrasf3dfd262009-02-26 22:43:46 +11003196 const struct perf_counter_hw_event __user *, hw_event_uptr,
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003197 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003198{
Ingo Molnar04289bb2008-12-11 08:38:42 +01003199 struct perf_counter *counter, *group_leader;
Ingo Molnar9f66a382008-12-10 12:33:23 +01003200 struct perf_counter_hw_event hw_event;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003201 struct perf_counter_context *ctx;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003202 struct file *counter_file = NULL;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003203 struct file *group_file = NULL;
3204 int fput_needed = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003205 int fput_needed2 = 0;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003206 int ret;
3207
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003208 /* for future expandability... */
3209 if (flags)
3210 return -EINVAL;
3211
Ingo Molnar9f66a382008-12-10 12:33:23 +01003212 if (copy_from_user(&hw_event, hw_event_uptr, sizeof(hw_event)) != 0)
Thomas Gleixnereab656a2008-12-08 19:26:59 +01003213 return -EFAULT;
3214
Ingo Molnar04289bb2008-12-11 08:38:42 +01003215 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01003216 * Get the target context (task or percpu):
3217 */
3218 ctx = find_get_context(pid, cpu);
3219 if (IS_ERR(ctx))
3220 return PTR_ERR(ctx);
3221
3222 /*
3223 * Look up the group leader (we will attach this counter to it):
Ingo Molnar04289bb2008-12-11 08:38:42 +01003224 */
3225 group_leader = NULL;
3226 if (group_fd != -1) {
3227 ret = -EINVAL;
3228 group_file = fget_light(group_fd, &fput_needed);
3229 if (!group_file)
Ingo Molnarccff2862008-12-11 11:26:29 +01003230 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003231 if (group_file->f_op != &perf_fops)
Ingo Molnarccff2862008-12-11 11:26:29 +01003232 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003233
3234 group_leader = group_file->private_data;
3235 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01003236 * Do not allow a recursive hierarchy (this new sibling
3237 * becoming part of another group-sibling):
Ingo Molnar04289bb2008-12-11 08:38:42 +01003238 */
Ingo Molnarccff2862008-12-11 11:26:29 +01003239 if (group_leader->group_leader != group_leader)
3240 goto err_put_context;
3241 /*
3242 * Do not allow to attach to a group in a different
3243 * task or CPU context:
3244 */
3245 if (group_leader->ctx != ctx)
3246 goto err_put_context;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11003247 /*
3248 * Only a group leader can be exclusive or pinned
3249 */
3250 if (hw_event.exclusive || hw_event.pinned)
3251 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003252 }
3253
Paul Mackerras23a185c2009-02-09 22:42:47 +11003254 counter = perf_counter_alloc(&hw_event, cpu, ctx, group_leader,
3255 GFP_KERNEL);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003256 ret = PTR_ERR(counter);
3257 if (IS_ERR(counter))
Thomas Gleixner0793a612008-12-04 20:12:29 +01003258 goto err_put_context;
3259
Thomas Gleixner0793a612008-12-04 20:12:29 +01003260 ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
3261 if (ret < 0)
Ingo Molnar9b51f662008-12-12 13:49:45 +01003262 goto err_free_put_context;
3263
3264 counter_file = fget_light(ret, &fput_needed2);
3265 if (!counter_file)
3266 goto err_free_put_context;
3267
3268 counter->filp = counter_file;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003269 mutex_lock(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003270 perf_install_in_context(ctx, counter, cpu);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003271 mutex_unlock(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003272
3273 fput_light(counter_file, fput_needed2);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003274
Ingo Molnar04289bb2008-12-11 08:38:42 +01003275out_fput:
3276 fput_light(group_file, fput_needed);
3277
Thomas Gleixner0793a612008-12-04 20:12:29 +01003278 return ret;
3279
Ingo Molnar9b51f662008-12-12 13:49:45 +01003280err_free_put_context:
Thomas Gleixner0793a612008-12-04 20:12:29 +01003281 kfree(counter);
3282
3283err_put_context:
3284 put_context(ctx);
3285
Ingo Molnar04289bb2008-12-11 08:38:42 +01003286 goto out_fput;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003287}
3288
Ingo Molnar9b51f662008-12-12 13:49:45 +01003289/*
Ingo Molnar9b51f662008-12-12 13:49:45 +01003290 * inherit a counter from parent task to child task:
3291 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11003292static struct perf_counter *
Ingo Molnar9b51f662008-12-12 13:49:45 +01003293inherit_counter(struct perf_counter *parent_counter,
3294 struct task_struct *parent,
3295 struct perf_counter_context *parent_ctx,
3296 struct task_struct *child,
Paul Mackerrasd859e292009-01-17 18:10:22 +11003297 struct perf_counter *group_leader,
Ingo Molnar9b51f662008-12-12 13:49:45 +01003298 struct perf_counter_context *child_ctx)
3299{
3300 struct perf_counter *child_counter;
3301
Paul Mackerrasd859e292009-01-17 18:10:22 +11003302 /*
3303 * Instead of creating recursive hierarchies of counters,
3304 * we link inherited counters back to the original parent,
3305 * which has a filp for sure, which we use as the reference
3306 * count:
3307 */
3308 if (parent_counter->parent)
3309 parent_counter = parent_counter->parent;
3310
Ingo Molnar9b51f662008-12-12 13:49:45 +01003311 child_counter = perf_counter_alloc(&parent_counter->hw_event,
Paul Mackerras23a185c2009-02-09 22:42:47 +11003312 parent_counter->cpu, child_ctx,
3313 group_leader, GFP_KERNEL);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003314 if (IS_ERR(child_counter))
3315 return child_counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003316
3317 /*
Paul Mackerras564c2b22009-05-22 14:27:22 +10003318 * Make the child state follow the state of the parent counter,
3319 * not its hw_event.disabled bit. We hold the parent's mutex,
3320 * so we won't race with perf_counter_{en,dis}able_family.
3321 */
3322 if (parent_counter->state >= PERF_COUNTER_STATE_INACTIVE)
3323 child_counter->state = PERF_COUNTER_STATE_INACTIVE;
3324 else
3325 child_counter->state = PERF_COUNTER_STATE_OFF;
3326
3327 /*
Ingo Molnar9b51f662008-12-12 13:49:45 +01003328 * Link it up in the child's context:
3329 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +11003330 add_counter_to_ctx(child_counter, child_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003331
3332 child_counter->parent = parent_counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003333 /*
3334 * inherit into child's child as well:
3335 */
3336 child_counter->hw_event.inherit = 1;
3337
3338 /*
3339 * Get a reference to the parent filp - we will fput it
3340 * when the child counter exits. This is safe to do because
3341 * we are in the parent and we know that the filp still
3342 * exists and has a nonzero count:
3343 */
3344 atomic_long_inc(&parent_counter->filp->f_count);
3345
Paul Mackerrasd859e292009-01-17 18:10:22 +11003346 /*
3347 * Link this into the parent counter's child list
3348 */
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003349 mutex_lock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003350 list_add_tail(&child_counter->child_list, &parent_counter->child_list);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003351 mutex_unlock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003352
3353 return child_counter;
3354}
3355
3356static int inherit_group(struct perf_counter *parent_counter,
3357 struct task_struct *parent,
3358 struct perf_counter_context *parent_ctx,
3359 struct task_struct *child,
3360 struct perf_counter_context *child_ctx)
3361{
3362 struct perf_counter *leader;
3363 struct perf_counter *sub;
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003364 struct perf_counter *child_ctr;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003365
3366 leader = inherit_counter(parent_counter, parent, parent_ctx,
3367 child, NULL, child_ctx);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003368 if (IS_ERR(leader))
3369 return PTR_ERR(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003370 list_for_each_entry(sub, &parent_counter->sibling_list, list_entry) {
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003371 child_ctr = inherit_counter(sub, parent, parent_ctx,
3372 child, leader, child_ctx);
3373 if (IS_ERR(child_ctr))
3374 return PTR_ERR(child_ctr);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003375 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01003376 return 0;
3377}
3378
Paul Mackerrasd859e292009-01-17 18:10:22 +11003379static void sync_child_counter(struct perf_counter *child_counter,
3380 struct perf_counter *parent_counter)
3381{
Peter Zijlstra8bc20952009-05-15 20:45:59 +02003382 u64 child_val;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003383
Paul Mackerrasd859e292009-01-17 18:10:22 +11003384 child_val = atomic64_read(&child_counter->count);
3385
3386 /*
3387 * Add back the child's count to the parent's count:
3388 */
3389 atomic64_add(child_val, &parent_counter->count);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11003390 atomic64_add(child_counter->total_time_enabled,
3391 &parent_counter->child_total_time_enabled);
3392 atomic64_add(child_counter->total_time_running,
3393 &parent_counter->child_total_time_running);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003394
3395 /*
3396 * Remove this counter from the parent's list
3397 */
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003398 mutex_lock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003399 list_del_init(&child_counter->child_list);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003400 mutex_unlock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003401
3402 /*
3403 * Release the parent counter, if this was the last
3404 * reference to it.
3405 */
3406 fput(parent_counter->filp);
3407}
3408
Ingo Molnar9b51f662008-12-12 13:49:45 +01003409static void
3410__perf_counter_exit_task(struct task_struct *child,
3411 struct perf_counter *child_counter,
3412 struct perf_counter_context *child_ctx)
3413{
3414 struct perf_counter *parent_counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003415
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003416 update_counter_times(child_counter);
3417 list_del_counter(child_counter, child_ctx);
Ingo Molnar0cc0c022008-12-14 23:20:36 +01003418
Ingo Molnar9b51f662008-12-12 13:49:45 +01003419 parent_counter = child_counter->parent;
3420 /*
3421 * It can happen that parent exits first, and has counters
3422 * that are still around due to the child reference. These
3423 * counters need to be zapped - but otherwise linger.
3424 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11003425 if (parent_counter) {
3426 sync_child_counter(child_counter, parent_counter);
Peter Zijlstraf1600952009-03-19 20:26:16 +01003427 free_counter(child_counter);
Paul Mackerras4bcf3492009-02-11 13:53:19 +01003428 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01003429}
3430
3431/*
Paul Mackerrasd859e292009-01-17 18:10:22 +11003432 * When a child task exits, feed back counter values to parent counters.
Ingo Molnar9b51f662008-12-12 13:49:45 +01003433 *
Paul Mackerrasd859e292009-01-17 18:10:22 +11003434 * Note: we may be running in child context, but the PID is not hashed
Ingo Molnar9b51f662008-12-12 13:49:45 +01003435 * anymore so new counters will not be added.
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003436 * (XXX not sure that is true when we get called from flush_old_exec.
3437 * -- paulus)
Ingo Molnar9b51f662008-12-12 13:49:45 +01003438 */
3439void perf_counter_exit_task(struct task_struct *child)
3440{
3441 struct perf_counter *child_counter, *tmp;
3442 struct perf_counter_context *child_ctx;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003443 unsigned long flags;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003444
Ingo Molnar33b2fb32009-05-17 11:08:41 +02003445 WARN_ON_ONCE(child != current);
3446
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003447 child_ctx = child->perf_counter_ctxp;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003448
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003449 if (likely(!child_ctx))
Ingo Molnar9b51f662008-12-12 13:49:45 +01003450 return;
3451
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003452 local_irq_save(flags);
3453 __perf_counter_task_sched_out(child_ctx);
3454 child->perf_counter_ctxp = NULL;
3455 local_irq_restore(flags);
3456
3457 mutex_lock(&child_ctx->mutex);
3458
Peter Zijlstra8bc20952009-05-15 20:45:59 +02003459again:
Ingo Molnar9b51f662008-12-12 13:49:45 +01003460 list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list,
3461 list_entry)
3462 __perf_counter_exit_task(child, child_counter, child_ctx);
Peter Zijlstra8bc20952009-05-15 20:45:59 +02003463
3464 /*
3465 * If the last counter was a group counter, it will have appended all
3466 * its siblings to the list, but we obtained 'tmp' before that which
3467 * will still point to the list head terminating the iteration.
3468 */
3469 if (!list_empty(&child_ctx->counter_list))
3470 goto again;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003471
3472 mutex_unlock(&child_ctx->mutex);
3473
3474 put_ctx(child_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003475}
3476
3477/*
3478 * Initialize the perf_counter context in task_struct
3479 */
3480void perf_counter_init_task(struct task_struct *child)
3481{
3482 struct perf_counter_context *child_ctx, *parent_ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003483 struct perf_counter *counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003484 struct task_struct *parent = current;
Paul Mackerras564c2b22009-05-22 14:27:22 +10003485 int inherited_all = 1;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003486
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003487 child->perf_counter_ctxp = NULL;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003488
3489 /*
3490 * This is executed from the parent task context, so inherit
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003491 * counters that have been marked for cloning.
3492 * First allocate and initialize a context for the child.
Ingo Molnar9b51f662008-12-12 13:49:45 +01003493 */
3494
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003495 child_ctx = kmalloc(sizeof(struct perf_counter_context), GFP_KERNEL);
3496 if (!child_ctx)
Ingo Molnar9b51f662008-12-12 13:49:45 +01003497 return;
3498
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003499 parent_ctx = parent->perf_counter_ctxp;
3500 if (likely(!parent_ctx || !parent_ctx->nr_counters))
3501 return;
3502
3503 __perf_counter_init_context(child_ctx, child);
3504 child->perf_counter_ctxp = child_ctx;
3505
Ingo Molnar9b51f662008-12-12 13:49:45 +01003506 /*
3507 * Lock the parent list. No need to lock the child - not PID
3508 * hashed yet and not running, so nobody can access it.
3509 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11003510 mutex_lock(&parent_ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003511
3512 /*
3513 * We dont have to disable NMIs - we are only looking at
3514 * the list, not manipulating it:
3515 */
Peter Zijlstrad7b629a2009-05-20 12:21:19 +02003516 list_for_each_entry_rcu(counter, &parent_ctx->event_list, event_entry) {
3517 if (counter != counter->group_leader)
3518 continue;
3519
Paul Mackerras564c2b22009-05-22 14:27:22 +10003520 if (!counter->hw_event.inherit) {
3521 inherited_all = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003522 continue;
Paul Mackerras564c2b22009-05-22 14:27:22 +10003523 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01003524
Paul Mackerrasd859e292009-01-17 18:10:22 +11003525 if (inherit_group(counter, parent,
Paul Mackerras564c2b22009-05-22 14:27:22 +10003526 parent_ctx, child, child_ctx)) {
3527 inherited_all = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003528 break;
Paul Mackerras564c2b22009-05-22 14:27:22 +10003529 }
3530 }
3531
3532 if (inherited_all) {
3533 /*
3534 * Mark the child context as a clone of the parent
3535 * context, or of whatever the parent is a clone of.
3536 */
3537 if (parent_ctx->parent_ctx) {
3538 child_ctx->parent_ctx = parent_ctx->parent_ctx;
3539 child_ctx->parent_gen = parent_ctx->parent_gen;
3540 } else {
3541 child_ctx->parent_ctx = parent_ctx;
3542 child_ctx->parent_gen = parent_ctx->generation;
3543 }
3544 get_ctx(child_ctx->parent_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003545 }
3546
Paul Mackerrasd859e292009-01-17 18:10:22 +11003547 mutex_unlock(&parent_ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003548}
3549
Ingo Molnar04289bb2008-12-11 08:38:42 +01003550static void __cpuinit perf_counter_init_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003551{
Ingo Molnar04289bb2008-12-11 08:38:42 +01003552 struct perf_cpu_context *cpuctx;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003553
Ingo Molnar04289bb2008-12-11 08:38:42 +01003554 cpuctx = &per_cpu(perf_cpu_context, cpu);
3555 __perf_counter_init_context(&cpuctx->ctx, NULL);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003556
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003557 spin_lock(&perf_resource_lock);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003558 cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu;
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003559 spin_unlock(&perf_resource_lock);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003560
Paul Mackerras01d02872009-01-14 13:44:19 +11003561 hw_perf_counter_setup(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003562}
3563
3564#ifdef CONFIG_HOTPLUG_CPU
Ingo Molnar04289bb2008-12-11 08:38:42 +01003565static void __perf_counter_exit_cpu(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003566{
3567 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
3568 struct perf_counter_context *ctx = &cpuctx->ctx;
3569 struct perf_counter *counter, *tmp;
3570
Ingo Molnar04289bb2008-12-11 08:38:42 +01003571 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry)
3572 __perf_counter_remove_from_context(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003573}
Ingo Molnar04289bb2008-12-11 08:38:42 +01003574static void perf_counter_exit_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003575{
Paul Mackerrasd859e292009-01-17 18:10:22 +11003576 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
3577 struct perf_counter_context *ctx = &cpuctx->ctx;
3578
3579 mutex_lock(&ctx->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003580 smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003581 mutex_unlock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003582}
3583#else
Ingo Molnar04289bb2008-12-11 08:38:42 +01003584static inline void perf_counter_exit_cpu(int cpu) { }
Thomas Gleixner0793a612008-12-04 20:12:29 +01003585#endif
3586
3587static int __cpuinit
3588perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
3589{
3590 unsigned int cpu = (long)hcpu;
3591
3592 switch (action) {
3593
3594 case CPU_UP_PREPARE:
3595 case CPU_UP_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01003596 perf_counter_init_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003597 break;
3598
3599 case CPU_DOWN_PREPARE:
3600 case CPU_DOWN_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01003601 perf_counter_exit_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003602 break;
3603
3604 default:
3605 break;
3606 }
3607
3608 return NOTIFY_OK;
3609}
3610
3611static struct notifier_block __cpuinitdata perf_cpu_nb = {
3612 .notifier_call = perf_cpu_notify,
3613};
3614
Ingo Molnar0d905bc2009-05-04 19:13:30 +02003615void __init perf_counter_init(void)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003616{
3617 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
3618 (void *)(long)smp_processor_id());
3619 register_cpu_notifier(&perf_cpu_nb);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003620}
Thomas Gleixner0793a612008-12-04 20:12:29 +01003621
3622static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
3623{
3624 return sprintf(buf, "%d\n", perf_reserved_percpu);
3625}
3626
3627static ssize_t
3628perf_set_reserve_percpu(struct sysdev_class *class,
3629 const char *buf,
3630 size_t count)
3631{
3632 struct perf_cpu_context *cpuctx;
3633 unsigned long val;
3634 int err, cpu, mpt;
3635
3636 err = strict_strtoul(buf, 10, &val);
3637 if (err)
3638 return err;
3639 if (val > perf_max_counters)
3640 return -EINVAL;
3641
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003642 spin_lock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003643 perf_reserved_percpu = val;
3644 for_each_online_cpu(cpu) {
3645 cpuctx = &per_cpu(perf_cpu_context, cpu);
3646 spin_lock_irq(&cpuctx->ctx.lock);
3647 mpt = min(perf_max_counters - cpuctx->ctx.nr_counters,
3648 perf_max_counters - perf_reserved_percpu);
3649 cpuctx->max_pertask = mpt;
3650 spin_unlock_irq(&cpuctx->ctx.lock);
3651 }
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003652 spin_unlock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003653
3654 return count;
3655}
3656
3657static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
3658{
3659 return sprintf(buf, "%d\n", perf_overcommit);
3660}
3661
3662static ssize_t
3663perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
3664{
3665 unsigned long val;
3666 int err;
3667
3668 err = strict_strtoul(buf, 10, &val);
3669 if (err)
3670 return err;
3671 if (val > 1)
3672 return -EINVAL;
3673
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003674 spin_lock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003675 perf_overcommit = val;
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003676 spin_unlock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003677
3678 return count;
3679}
3680
3681static SYSDEV_CLASS_ATTR(
3682 reserve_percpu,
3683 0644,
3684 perf_show_reserve_percpu,
3685 perf_set_reserve_percpu
3686 );
3687
3688static SYSDEV_CLASS_ATTR(
3689 overcommit,
3690 0644,
3691 perf_show_overcommit,
3692 perf_set_overcommit
3693 );
3694
3695static struct attribute *perfclass_attrs[] = {
3696 &attr_reserve_percpu.attr,
3697 &attr_overcommit.attr,
3698 NULL
3699};
3700
3701static struct attribute_group perfclass_attr_group = {
3702 .attrs = perfclass_attrs,
3703 .name = "perf_counters",
3704};
3705
3706static int __init perf_counter_sysfs_init(void)
3707{
3708 return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
3709 &perfclass_attr_group);
3710}
3711device_initcall(perf_counter_sysfs_init);