blob: fdb0d2421276351b5885ce1173e9c5141cd453f1 [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 Zijlstrac5078f72009-05-05 17:50:24 +020048int sysctl_perf_counter_mlock __read_mostly = 128; /* 'free' kb per counter */
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
Ingo Molnar01b28382008-12-11 13:45:51 +010063u64 __weak hw_perf_save_disable(void) { return 0; }
Yinghai Lu01ea1cc2008-12-26 21:05:06 -080064void __weak hw_perf_restore(u64 ctrl) { barrier(); }
Paul Mackerras01d02872009-01-14 13:44:19 +110065void __weak hw_perf_counter_setup(int cpu) { barrier(); }
Paul Mackerras3cbed422009-01-09 16:43:42 +110066int __weak hw_perf_group_sched_in(struct perf_counter *group_leader,
67 struct perf_cpu_context *cpuctx,
68 struct perf_counter_context *ctx, int cpu)
69{
70 return 0;
71}
Thomas Gleixner0793a612008-12-04 20:12:29 +010072
Paul Mackerras4eb96fc2009-01-09 17:24:34 +110073void __weak perf_counter_print_debug(void) { }
74
Ingo Molnar04289bb2008-12-11 08:38:42 +010075static void
76list_add_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
77{
78 struct perf_counter *group_leader = counter->group_leader;
79
80 /*
81 * Depending on whether it is a standalone or sibling counter,
82 * add it straight to the context's counter list, or to the group
83 * leader's sibling list:
84 */
85 if (counter->group_leader == counter)
86 list_add_tail(&counter->list_entry, &ctx->counter_list);
Peter Zijlstra5c148192009-03-25 12:30:23 +010087 else {
Ingo Molnar04289bb2008-12-11 08:38:42 +010088 list_add_tail(&counter->list_entry, &group_leader->sibling_list);
Peter Zijlstra5c148192009-03-25 12:30:23 +010089 group_leader->nr_siblings++;
90 }
Peter Zijlstra592903c2009-03-13 12:21:36 +010091
92 list_add_rcu(&counter->event_entry, &ctx->event_list);
Ingo Molnar04289bb2008-12-11 08:38:42 +010093}
94
95static void
96list_del_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
97{
98 struct perf_counter *sibling, *tmp;
99
100 list_del_init(&counter->list_entry);
Peter Zijlstra592903c2009-03-13 12:21:36 +0100101 list_del_rcu(&counter->event_entry);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100102
Peter Zijlstra5c148192009-03-25 12:30:23 +0100103 if (counter->group_leader != counter)
104 counter->group_leader->nr_siblings--;
105
Ingo Molnar04289bb2008-12-11 08:38:42 +0100106 /*
107 * If this was a group counter with sibling counters then
108 * upgrade the siblings to singleton counters by adding them
109 * to the context list directly:
110 */
111 list_for_each_entry_safe(sibling, tmp,
112 &counter->sibling_list, list_entry) {
113
Peter Zijlstra75564232009-03-13 12:21:29 +0100114 list_move_tail(&sibling->list_entry, &ctx->counter_list);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100115 sibling->group_leader = sibling;
116 }
117}
118
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100119static void
120counter_sched_out(struct perf_counter *counter,
121 struct perf_cpu_context *cpuctx,
122 struct perf_counter_context *ctx)
123{
124 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
125 return;
126
127 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200128 counter->tstamp_stopped = ctx->time;
Robert Richter4aeb0b42009-04-29 12:47:03 +0200129 counter->pmu->disable(counter);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100130 counter->oncpu = -1;
131
132 if (!is_software_counter(counter))
133 cpuctx->active_oncpu--;
134 ctx->nr_active--;
135 if (counter->hw_event.exclusive || !cpuctx->active_oncpu)
136 cpuctx->exclusive = 0;
137}
138
Paul Mackerrasd859e292009-01-17 18:10:22 +1100139static void
140group_sched_out(struct perf_counter *group_counter,
141 struct perf_cpu_context *cpuctx,
142 struct perf_counter_context *ctx)
143{
144 struct perf_counter *counter;
145
146 if (group_counter->state != PERF_COUNTER_STATE_ACTIVE)
147 return;
148
149 counter_sched_out(group_counter, cpuctx, ctx);
150
151 /*
152 * Schedule out siblings (if any):
153 */
154 list_for_each_entry(counter, &group_counter->sibling_list, list_entry)
155 counter_sched_out(counter, cpuctx, ctx);
156
157 if (group_counter->hw_event.exclusive)
158 cpuctx->exclusive = 0;
159}
160
Thomas Gleixner0793a612008-12-04 20:12:29 +0100161/*
162 * Cross CPU call to remove a performance counter
163 *
164 * We disable the counter on the hardware level first. After that we
165 * remove it from the context list.
166 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100167static void __perf_counter_remove_from_context(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100168{
169 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
170 struct perf_counter *counter = info;
171 struct perf_counter_context *ctx = counter->ctx;
Ingo Molnar9b51f662008-12-12 13:49:45 +0100172 unsigned long flags;
Ingo Molnar5c92d122008-12-11 13:21:10 +0100173 u64 perf_flags;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100174
175 /*
176 * If this is a task context, we need to check whether it is
177 * the current task context of this cpu. If not it has been
178 * scheduled out before the smp call arrived.
179 */
180 if (ctx->task && cpuctx->task_ctx != ctx)
181 return;
182
Peter Zijlstra849691a2009-04-06 11:45:12 +0200183 spin_lock_irqsave(&ctx->lock, flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100184
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100185 counter_sched_out(counter, cpuctx, ctx);
186
187 counter->task = NULL;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100188 ctx->nr_counters--;
189
190 /*
191 * Protect the list operation against NMI by disabling the
192 * counters on a global level. NOP for non NMI based counters.
193 */
Ingo Molnar01b28382008-12-11 13:45:51 +0100194 perf_flags = hw_perf_save_disable();
Ingo Molnar04289bb2008-12-11 08:38:42 +0100195 list_del_counter(counter, ctx);
Ingo Molnar01b28382008-12-11 13:45:51 +0100196 hw_perf_restore(perf_flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100197
198 if (!ctx->task) {
199 /*
200 * Allow more per task counters with respect to the
201 * reservation:
202 */
203 cpuctx->max_pertask =
204 min(perf_max_counters - ctx->nr_counters,
205 perf_max_counters - perf_reserved_percpu);
206 }
207
Peter Zijlstra849691a2009-04-06 11:45:12 +0200208 spin_unlock_irqrestore(&ctx->lock, flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100209}
210
211
212/*
213 * Remove the counter from a task's (or a CPU's) list of counters.
214 *
Paul Mackerrasd859e292009-01-17 18:10:22 +1100215 * Must be called with counter->mutex and ctx->mutex held.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100216 *
217 * CPU counters are removed with a smp call. For task counters we only
218 * call when the task is on a CPU.
219 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100220static void perf_counter_remove_from_context(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100221{
222 struct perf_counter_context *ctx = counter->ctx;
223 struct task_struct *task = ctx->task;
224
225 if (!task) {
226 /*
227 * Per cpu counters are removed via an smp call and
228 * the removal is always sucessful.
229 */
230 smp_call_function_single(counter->cpu,
Ingo Molnar04289bb2008-12-11 08:38:42 +0100231 __perf_counter_remove_from_context,
Thomas Gleixner0793a612008-12-04 20:12:29 +0100232 counter, 1);
233 return;
234 }
235
236retry:
Ingo Molnar04289bb2008-12-11 08:38:42 +0100237 task_oncpu_function_call(task, __perf_counter_remove_from_context,
Thomas Gleixner0793a612008-12-04 20:12:29 +0100238 counter);
239
240 spin_lock_irq(&ctx->lock);
241 /*
242 * If the context is active we need to retry the smp call.
243 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100244 if (ctx->nr_active && !list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100245 spin_unlock_irq(&ctx->lock);
246 goto retry;
247 }
248
249 /*
250 * The lock prevents that this context is scheduled in so we
Ingo Molnar04289bb2008-12-11 08:38:42 +0100251 * can remove the counter safely, if the call above did not
Thomas Gleixner0793a612008-12-04 20:12:29 +0100252 * succeed.
253 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100254 if (!list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100255 ctx->nr_counters--;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100256 list_del_counter(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100257 counter->task = NULL;
258 }
259 spin_unlock_irq(&ctx->lock);
260}
261
Peter Zijlstra4af49982009-04-06 11:45:10 +0200262static inline u64 perf_clock(void)
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100263{
Peter Zijlstra4af49982009-04-06 11:45:10 +0200264 return cpu_clock(smp_processor_id());
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100265}
266
267/*
268 * Update the record of the current time in a context.
269 */
Peter Zijlstra4af49982009-04-06 11:45:10 +0200270static void update_context_time(struct perf_counter_context *ctx)
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100271{
Peter Zijlstra4af49982009-04-06 11:45:10 +0200272 u64 now = perf_clock();
273
274 ctx->time += now - ctx->timestamp;
275 ctx->timestamp = now;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100276}
277
278/*
279 * Update the total_time_enabled and total_time_running fields for a counter.
280 */
281static void update_counter_times(struct perf_counter *counter)
282{
283 struct perf_counter_context *ctx = counter->ctx;
284 u64 run_end;
285
Peter Zijlstra4af49982009-04-06 11:45:10 +0200286 if (counter->state < PERF_COUNTER_STATE_INACTIVE)
287 return;
288
289 counter->total_time_enabled = ctx->time - counter->tstamp_enabled;
290
291 if (counter->state == PERF_COUNTER_STATE_INACTIVE)
292 run_end = counter->tstamp_stopped;
293 else
294 run_end = ctx->time;
295
296 counter->total_time_running = run_end - counter->tstamp_running;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100297}
298
299/*
300 * Update total_time_enabled and total_time_running for all counters in a group.
301 */
302static void update_group_times(struct perf_counter *leader)
303{
304 struct perf_counter *counter;
305
306 update_counter_times(leader);
307 list_for_each_entry(counter, &leader->sibling_list, list_entry)
308 update_counter_times(counter);
309}
310
311/*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100312 * Cross CPU call to disable a performance counter
313 */
314static void __perf_counter_disable(void *info)
315{
316 struct perf_counter *counter = info;
317 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
318 struct perf_counter_context *ctx = counter->ctx;
319 unsigned long flags;
320
321 /*
322 * If this is a per-task counter, need to check whether this
323 * counter's task is the current task on this cpu.
324 */
325 if (ctx->task && cpuctx->task_ctx != ctx)
326 return;
327
Peter Zijlstra849691a2009-04-06 11:45:12 +0200328 spin_lock_irqsave(&ctx->lock, flags);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100329
330 /*
331 * If the counter is on, turn it off.
332 * If it is in error state, leave it in error state.
333 */
334 if (counter->state >= PERF_COUNTER_STATE_INACTIVE) {
Peter Zijlstra4af49982009-04-06 11:45:10 +0200335 update_context_time(ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100336 update_counter_times(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100337 if (counter == counter->group_leader)
338 group_sched_out(counter, cpuctx, ctx);
339 else
340 counter_sched_out(counter, cpuctx, ctx);
341 counter->state = PERF_COUNTER_STATE_OFF;
342 }
343
Peter Zijlstra849691a2009-04-06 11:45:12 +0200344 spin_unlock_irqrestore(&ctx->lock, flags);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100345}
346
347/*
348 * Disable a counter.
349 */
350static void perf_counter_disable(struct perf_counter *counter)
351{
352 struct perf_counter_context *ctx = counter->ctx;
353 struct task_struct *task = ctx->task;
354
355 if (!task) {
356 /*
357 * Disable the counter on the cpu that it's on
358 */
359 smp_call_function_single(counter->cpu, __perf_counter_disable,
360 counter, 1);
361 return;
362 }
363
364 retry:
365 task_oncpu_function_call(task, __perf_counter_disable, counter);
366
367 spin_lock_irq(&ctx->lock);
368 /*
369 * If the counter is still active, we need to retry the cross-call.
370 */
371 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
372 spin_unlock_irq(&ctx->lock);
373 goto retry;
374 }
375
376 /*
377 * Since we have the lock this context can't be scheduled
378 * in, so we can change the state safely.
379 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100380 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
381 update_counter_times(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100382 counter->state = PERF_COUNTER_STATE_OFF;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100383 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100384
385 spin_unlock_irq(&ctx->lock);
386}
387
388/*
389 * Disable a counter and all its children.
390 */
391static void perf_counter_disable_family(struct perf_counter *counter)
392{
393 struct perf_counter *child;
394
395 perf_counter_disable(counter);
396
397 /*
398 * Lock the mutex to protect the list of children
399 */
400 mutex_lock(&counter->mutex);
401 list_for_each_entry(child, &counter->child_list, child_list)
402 perf_counter_disable(child);
403 mutex_unlock(&counter->mutex);
404}
405
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100406static int
407counter_sched_in(struct perf_counter *counter,
408 struct perf_cpu_context *cpuctx,
409 struct perf_counter_context *ctx,
410 int cpu)
411{
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100412 if (counter->state <= PERF_COUNTER_STATE_OFF)
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100413 return 0;
414
415 counter->state = PERF_COUNTER_STATE_ACTIVE;
416 counter->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */
417 /*
418 * The new state must be visible before we turn it on in the hardware:
419 */
420 smp_wmb();
421
Robert Richter4aeb0b42009-04-29 12:47:03 +0200422 if (counter->pmu->enable(counter)) {
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100423 counter->state = PERF_COUNTER_STATE_INACTIVE;
424 counter->oncpu = -1;
425 return -EAGAIN;
426 }
427
Peter Zijlstra4af49982009-04-06 11:45:10 +0200428 counter->tstamp_running += ctx->time - counter->tstamp_stopped;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100429
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100430 if (!is_software_counter(counter))
431 cpuctx->active_oncpu++;
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100432 ctx->nr_active++;
433
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100434 if (counter->hw_event.exclusive)
435 cpuctx->exclusive = 1;
436
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100437 return 0;
438}
439
Thomas Gleixner0793a612008-12-04 20:12:29 +0100440/*
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100441 * Return 1 for a group consisting entirely of software counters,
442 * 0 if the group contains any hardware counters.
443 */
444static int is_software_only_group(struct perf_counter *leader)
445{
446 struct perf_counter *counter;
447
448 if (!is_software_counter(leader))
449 return 0;
Peter Zijlstra5c148192009-03-25 12:30:23 +0100450
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100451 list_for_each_entry(counter, &leader->sibling_list, list_entry)
452 if (!is_software_counter(counter))
453 return 0;
Peter Zijlstra5c148192009-03-25 12:30:23 +0100454
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100455 return 1;
456}
457
458/*
459 * Work out whether we can put this counter group on the CPU now.
460 */
461static int group_can_go_on(struct perf_counter *counter,
462 struct perf_cpu_context *cpuctx,
463 int can_add_hw)
464{
465 /*
466 * Groups consisting entirely of software counters can always go on.
467 */
468 if (is_software_only_group(counter))
469 return 1;
470 /*
471 * If an exclusive group is already on, no other hardware
472 * counters can go on.
473 */
474 if (cpuctx->exclusive)
475 return 0;
476 /*
477 * If this group is exclusive and there are already
478 * counters on the CPU, it can't go on.
479 */
480 if (counter->hw_event.exclusive && cpuctx->active_oncpu)
481 return 0;
482 /*
483 * Otherwise, try to add it if all previous groups were able
484 * to go on.
485 */
486 return can_add_hw;
487}
488
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100489static void add_counter_to_ctx(struct perf_counter *counter,
490 struct perf_counter_context *ctx)
491{
492 list_add_counter(counter, ctx);
493 ctx->nr_counters++;
494 counter->prev_state = PERF_COUNTER_STATE_OFF;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200495 counter->tstamp_enabled = ctx->time;
496 counter->tstamp_running = ctx->time;
497 counter->tstamp_stopped = ctx->time;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100498}
499
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100500/*
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100501 * Cross CPU call to install and enable a performance counter
Thomas Gleixner0793a612008-12-04 20:12:29 +0100502 */
503static void __perf_install_in_context(void *info)
504{
505 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
506 struct perf_counter *counter = info;
507 struct perf_counter_context *ctx = counter->ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100508 struct perf_counter *leader = counter->group_leader;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100509 int cpu = smp_processor_id();
Ingo Molnar9b51f662008-12-12 13:49:45 +0100510 unsigned long flags;
Ingo Molnar5c92d122008-12-11 13:21:10 +0100511 u64 perf_flags;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100512 int err;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100513
514 /*
515 * If this is a task context, we need to check whether it is
516 * the current task context of this cpu. If not it has been
517 * scheduled out before the smp call arrived.
518 */
519 if (ctx->task && cpuctx->task_ctx != ctx)
520 return;
521
Peter Zijlstra849691a2009-04-06 11:45:12 +0200522 spin_lock_irqsave(&ctx->lock, flags);
Peter Zijlstra4af49982009-04-06 11:45:10 +0200523 update_context_time(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100524
525 /*
526 * Protect the list operation against NMI by disabling the
527 * counters on a global level. NOP for non NMI based counters.
528 */
Ingo Molnar01b28382008-12-11 13:45:51 +0100529 perf_flags = hw_perf_save_disable();
Thomas Gleixner0793a612008-12-04 20:12:29 +0100530
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100531 add_counter_to_ctx(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100532
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100533 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100534 * Don't put the counter on if it is disabled or if
535 * it is in a group and the group isn't on.
536 */
537 if (counter->state != PERF_COUNTER_STATE_INACTIVE ||
538 (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE))
539 goto unlock;
540
541 /*
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100542 * An exclusive counter can't go on if there are already active
543 * hardware counters, and no hardware counter can go on if there
544 * is already an exclusive counter on.
545 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100546 if (!group_can_go_on(counter, cpuctx, 1))
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100547 err = -EEXIST;
548 else
549 err = counter_sched_in(counter, cpuctx, ctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100550
Paul Mackerrasd859e292009-01-17 18:10:22 +1100551 if (err) {
552 /*
553 * This counter couldn't go on. If it is in a group
554 * then we have to pull the whole group off.
555 * If the counter group is pinned then put it in error state.
556 */
557 if (leader != counter)
558 group_sched_out(leader, cpuctx, ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100559 if (leader->hw_event.pinned) {
560 update_group_times(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100561 leader->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100562 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100563 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100564
565 if (!err && !ctx->task && cpuctx->max_pertask)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100566 cpuctx->max_pertask--;
567
Paul Mackerrasd859e292009-01-17 18:10:22 +1100568 unlock:
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100569 hw_perf_restore(perf_flags);
570
Peter Zijlstra849691a2009-04-06 11:45:12 +0200571 spin_unlock_irqrestore(&ctx->lock, flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100572}
573
574/*
575 * Attach a performance counter to a context
576 *
577 * First we add the counter to the list with the hardware enable bit
578 * in counter->hw_config cleared.
579 *
580 * If the counter is attached to a task which is on a CPU we use a smp
581 * call to enable it in the task context. The task might have been
582 * scheduled away, but we check this in the smp call again.
Paul Mackerrasd859e292009-01-17 18:10:22 +1100583 *
584 * Must be called with ctx->mutex held.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100585 */
586static void
587perf_install_in_context(struct perf_counter_context *ctx,
588 struct perf_counter *counter,
589 int cpu)
590{
591 struct task_struct *task = ctx->task;
592
Thomas Gleixner0793a612008-12-04 20:12:29 +0100593 if (!task) {
594 /*
595 * Per cpu counters are installed via an smp call and
596 * the install is always sucessful.
597 */
598 smp_call_function_single(cpu, __perf_install_in_context,
599 counter, 1);
600 return;
601 }
602
603 counter->task = task;
604retry:
605 task_oncpu_function_call(task, __perf_install_in_context,
606 counter);
607
608 spin_lock_irq(&ctx->lock);
609 /*
Thomas Gleixner0793a612008-12-04 20:12:29 +0100610 * we need to retry the smp call.
611 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100612 if (ctx->is_active && list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100613 spin_unlock_irq(&ctx->lock);
614 goto retry;
615 }
616
617 /*
618 * The lock prevents that this context is scheduled in so we
619 * can add the counter safely, if it the call above did not
620 * succeed.
621 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100622 if (list_empty(&counter->list_entry))
623 add_counter_to_ctx(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100624 spin_unlock_irq(&ctx->lock);
625}
626
Paul Mackerrasd859e292009-01-17 18:10:22 +1100627/*
628 * Cross CPU call to enable a performance counter
629 */
630static void __perf_counter_enable(void *info)
Ingo Molnar04289bb2008-12-11 08:38:42 +0100631{
Paul Mackerrasd859e292009-01-17 18:10:22 +1100632 struct perf_counter *counter = info;
633 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
634 struct perf_counter_context *ctx = counter->ctx;
635 struct perf_counter *leader = counter->group_leader;
636 unsigned long flags;
637 int err;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100638
639 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100640 * If this is a per-task counter, need to check whether this
641 * counter's task is the current task on this cpu.
Ingo Molnar04289bb2008-12-11 08:38:42 +0100642 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100643 if (ctx->task && cpuctx->task_ctx != ctx)
644 return;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100645
Peter Zijlstra849691a2009-04-06 11:45:12 +0200646 spin_lock_irqsave(&ctx->lock, flags);
Peter Zijlstra4af49982009-04-06 11:45:10 +0200647 update_context_time(ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100648
Paul Mackerrasc07c99b2009-02-13 22:10:34 +1100649 counter->prev_state = counter->state;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100650 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
651 goto unlock;
652 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200653 counter->tstamp_enabled = ctx->time - counter->total_time_enabled;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100654
655 /*
656 * If the counter is in a group and isn't the group leader,
657 * then don't put it on unless the group is on.
658 */
659 if (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE)
660 goto unlock;
661
662 if (!group_can_go_on(counter, cpuctx, 1))
663 err = -EEXIST;
664 else
665 err = counter_sched_in(counter, cpuctx, ctx,
666 smp_processor_id());
667
668 if (err) {
669 /*
670 * If this counter can't go on and it's part of a
671 * group, then the whole group has to come off.
672 */
673 if (leader != counter)
674 group_sched_out(leader, cpuctx, ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100675 if (leader->hw_event.pinned) {
676 update_group_times(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100677 leader->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100678 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100679 }
680
681 unlock:
Peter Zijlstra849691a2009-04-06 11:45:12 +0200682 spin_unlock_irqrestore(&ctx->lock, flags);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100683}
684
685/*
686 * Enable a counter.
687 */
688static void perf_counter_enable(struct perf_counter *counter)
689{
690 struct perf_counter_context *ctx = counter->ctx;
691 struct task_struct *task = ctx->task;
692
693 if (!task) {
694 /*
695 * Enable the counter on the cpu that it's on
696 */
697 smp_call_function_single(counter->cpu, __perf_counter_enable,
698 counter, 1);
699 return;
700 }
701
702 spin_lock_irq(&ctx->lock);
703 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
704 goto out;
705
706 /*
707 * If the counter is in error state, clear that first.
708 * That way, if we see the counter in error state below, we
709 * know that it has gone back into error state, as distinct
710 * from the task having been scheduled away before the
711 * cross-call arrived.
712 */
713 if (counter->state == PERF_COUNTER_STATE_ERROR)
714 counter->state = PERF_COUNTER_STATE_OFF;
715
716 retry:
717 spin_unlock_irq(&ctx->lock);
718 task_oncpu_function_call(task, __perf_counter_enable, counter);
719
720 spin_lock_irq(&ctx->lock);
721
722 /*
723 * If the context is active and the counter is still off,
724 * we need to retry the cross-call.
725 */
726 if (ctx->is_active && counter->state == PERF_COUNTER_STATE_OFF)
727 goto retry;
728
729 /*
730 * Since we have the lock this context can't be scheduled
731 * in, so we can change the state safely.
732 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100733 if (counter->state == PERF_COUNTER_STATE_OFF) {
Paul Mackerrasd859e292009-01-17 18:10:22 +1100734 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200735 counter->tstamp_enabled =
736 ctx->time - counter->total_time_enabled;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100737 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100738 out:
739 spin_unlock_irq(&ctx->lock);
740}
741
Peter Zijlstra2023b352009-05-05 17:50:26 +0200742static int perf_counter_refresh(struct perf_counter *counter, int refresh)
Peter Zijlstra79f14642009-04-06 11:45:07 +0200743{
Peter Zijlstra2023b352009-05-05 17:50:26 +0200744 /*
745 * not supported on inherited counters
746 */
747 if (counter->hw_event.inherit)
748 return -EINVAL;
749
Peter Zijlstra79f14642009-04-06 11:45:07 +0200750 atomic_add(refresh, &counter->event_limit);
751 perf_counter_enable(counter);
Peter Zijlstra2023b352009-05-05 17:50:26 +0200752
753 return 0;
Peter Zijlstra79f14642009-04-06 11:45:07 +0200754}
755
Paul Mackerrasd859e292009-01-17 18:10:22 +1100756/*
757 * Enable a counter and all its children.
758 */
759static void perf_counter_enable_family(struct perf_counter *counter)
760{
761 struct perf_counter *child;
762
763 perf_counter_enable(counter);
764
765 /*
766 * Lock the mutex to protect the list of children
767 */
768 mutex_lock(&counter->mutex);
769 list_for_each_entry(child, &counter->child_list, child_list)
770 perf_counter_enable(child);
771 mutex_unlock(&counter->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100772}
773
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100774void __perf_counter_sched_out(struct perf_counter_context *ctx,
775 struct perf_cpu_context *cpuctx)
776{
777 struct perf_counter *counter;
Paul Mackerras3cbed422009-01-09 16:43:42 +1100778 u64 flags;
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100779
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100780 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100781 ctx->is_active = 0;
782 if (likely(!ctx->nr_counters))
783 goto out;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200784 update_context_time(ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100785
Paul Mackerras3cbed422009-01-09 16:43:42 +1100786 flags = hw_perf_save_disable();
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100787 if (ctx->nr_active) {
788 list_for_each_entry(counter, &ctx->counter_list, list_entry)
789 group_sched_out(counter, cpuctx, ctx);
790 }
Paul Mackerras3cbed422009-01-09 16:43:42 +1100791 hw_perf_restore(flags);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100792 out:
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100793 spin_unlock(&ctx->lock);
794}
795
Thomas Gleixner0793a612008-12-04 20:12:29 +0100796/*
797 * Called from scheduler to remove the counters of the current task,
798 * with interrupts disabled.
799 *
800 * We stop each counter and update the counter value in counter->count.
801 *
Ingo Molnar76715812008-12-17 14:20:28 +0100802 * This does not protect us against NMI, but disable()
Thomas Gleixner0793a612008-12-04 20:12:29 +0100803 * sets the disabled bit in the control field of counter _before_
804 * accessing the counter control register. If a NMI hits, then it will
805 * not restart the counter.
806 */
807void perf_counter_task_sched_out(struct task_struct *task, int cpu)
808{
809 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
810 struct perf_counter_context *ctx = &task->perf_counter_ctx;
Peter Zijlstra4a0deca2009-03-19 20:26:12 +0100811 struct pt_regs *regs;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100812
813 if (likely(!cpuctx->task_ctx))
814 return;
815
Peter Zijlstrabce379b2009-04-06 11:45:13 +0200816 update_context_time(ctx);
817
Peter Zijlstra4a0deca2009-03-19 20:26:12 +0100818 regs = task_pt_regs(task);
Peter Zijlstra78f13e92009-04-08 15:01:33 +0200819 perf_swcounter_event(PERF_COUNT_CONTEXT_SWITCHES, 1, 1, regs, 0);
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100820 __perf_counter_sched_out(ctx, cpuctx);
821
Thomas Gleixner0793a612008-12-04 20:12:29 +0100822 cpuctx->task_ctx = NULL;
823}
824
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100825static void perf_counter_cpu_sched_out(struct perf_cpu_context *cpuctx)
Ingo Molnar04289bb2008-12-11 08:38:42 +0100826{
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100827 __perf_counter_sched_out(&cpuctx->ctx, cpuctx);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100828}
829
Ingo Molnar79958882008-12-17 08:54:56 +0100830static int
Ingo Molnar04289bb2008-12-11 08:38:42 +0100831group_sched_in(struct perf_counter *group_counter,
832 struct perf_cpu_context *cpuctx,
833 struct perf_counter_context *ctx,
834 int cpu)
835{
Ingo Molnar95cdd2e2008-12-21 13:50:42 +0100836 struct perf_counter *counter, *partial_group;
Paul Mackerras3cbed422009-01-09 16:43:42 +1100837 int ret;
838
839 if (group_counter->state == PERF_COUNTER_STATE_OFF)
840 return 0;
841
842 ret = hw_perf_group_sched_in(group_counter, cpuctx, ctx, cpu);
843 if (ret)
844 return ret < 0 ? ret : 0;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100845
Paul Mackerrasc07c99b2009-02-13 22:10:34 +1100846 group_counter->prev_state = group_counter->state;
Ingo Molnar95cdd2e2008-12-21 13:50:42 +0100847 if (counter_sched_in(group_counter, cpuctx, ctx, cpu))
848 return -EAGAIN;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100849
850 /*
851 * Schedule in siblings as one group (if any):
852 */
Ingo Molnar79958882008-12-17 08:54:56 +0100853 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
Paul Mackerrasc07c99b2009-02-13 22:10:34 +1100854 counter->prev_state = counter->state;
Ingo Molnar95cdd2e2008-12-21 13:50:42 +0100855 if (counter_sched_in(counter, cpuctx, ctx, cpu)) {
856 partial_group = counter;
857 goto group_error;
858 }
Ingo Molnar79958882008-12-17 08:54:56 +0100859 }
860
Paul Mackerras3cbed422009-01-09 16:43:42 +1100861 return 0;
Ingo Molnar95cdd2e2008-12-21 13:50:42 +0100862
863group_error:
864 /*
865 * Groups can be scheduled in as one unit only, so undo any
866 * partial group before returning:
867 */
868 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
869 if (counter == partial_group)
870 break;
871 counter_sched_out(counter, cpuctx, ctx);
872 }
873 counter_sched_out(group_counter, cpuctx, ctx);
874
875 return -EAGAIN;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100876}
877
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100878static void
879__perf_counter_sched_in(struct perf_counter_context *ctx,
880 struct perf_cpu_context *cpuctx, int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100881{
Thomas Gleixner0793a612008-12-04 20:12:29 +0100882 struct perf_counter *counter;
Paul Mackerras3cbed422009-01-09 16:43:42 +1100883 u64 flags;
Paul Mackerrasdd0e6ba2009-01-12 15:11:00 +1100884 int can_add_hw = 1;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100885
Thomas Gleixner0793a612008-12-04 20:12:29 +0100886 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100887 ctx->is_active = 1;
888 if (likely(!ctx->nr_counters))
889 goto out;
890
Peter Zijlstra4af49982009-04-06 11:45:10 +0200891 ctx->timestamp = perf_clock();
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100892
Paul Mackerras3cbed422009-01-09 16:43:42 +1100893 flags = hw_perf_save_disable();
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100894
895 /*
896 * First go through the list and put on any pinned groups
897 * in order to give them the best chance of going on.
898 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100899 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100900 if (counter->state <= PERF_COUNTER_STATE_OFF ||
901 !counter->hw_event.pinned)
902 continue;
903 if (counter->cpu != -1 && counter->cpu != cpu)
904 continue;
905
906 if (group_can_go_on(counter, cpuctx, 1))
907 group_sched_in(counter, cpuctx, ctx, cpu);
908
909 /*
910 * If this pinned group hasn't been scheduled,
911 * put it in error state.
912 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100913 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
914 update_group_times(counter);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100915 counter->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100916 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100917 }
918
919 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
920 /*
921 * Ignore counters in OFF or ERROR state, and
922 * ignore pinned counters since we did them already.
923 */
924 if (counter->state <= PERF_COUNTER_STATE_OFF ||
925 counter->hw_event.pinned)
926 continue;
927
Ingo Molnar04289bb2008-12-11 08:38:42 +0100928 /*
929 * Listen to the 'cpu' scheduling filter constraint
930 * of counters:
931 */
Thomas Gleixner0793a612008-12-04 20:12:29 +0100932 if (counter->cpu != -1 && counter->cpu != cpu)
933 continue;
934
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100935 if (group_can_go_on(counter, cpuctx, can_add_hw)) {
Paul Mackerrasdd0e6ba2009-01-12 15:11:00 +1100936 if (group_sched_in(counter, cpuctx, ctx, cpu))
937 can_add_hw = 0;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100938 }
Thomas Gleixner0793a612008-12-04 20:12:29 +0100939 }
Paul Mackerras3cbed422009-01-09 16:43:42 +1100940 hw_perf_restore(flags);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100941 out:
Thomas Gleixner0793a612008-12-04 20:12:29 +0100942 spin_unlock(&ctx->lock);
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100943}
Ingo Molnar04289bb2008-12-11 08:38:42 +0100944
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100945/*
946 * Called from scheduler to add the counters of the current task
947 * with interrupts disabled.
948 *
949 * We restore the counter value and then enable it.
950 *
951 * This does not protect us against NMI, but enable()
952 * sets the enabled bit in the control field of counter _before_
953 * accessing the counter control register. If a NMI hits, then it will
954 * keep the counter running.
955 */
956void perf_counter_task_sched_in(struct task_struct *task, int cpu)
957{
958 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
959 struct perf_counter_context *ctx = &task->perf_counter_ctx;
960
961 __perf_counter_sched_in(ctx, cpuctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100962 cpuctx->task_ctx = ctx;
963}
964
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100965static void perf_counter_cpu_sched_in(struct perf_cpu_context *cpuctx, int cpu)
966{
967 struct perf_counter_context *ctx = &cpuctx->ctx;
968
969 __perf_counter_sched_in(ctx, cpuctx, cpu);
970}
971
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100972int perf_counter_task_disable(void)
973{
974 struct task_struct *curr = current;
975 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
976 struct perf_counter *counter;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100977 unsigned long flags;
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100978 u64 perf_flags;
979 int cpu;
980
981 if (likely(!ctx->nr_counters))
982 return 0;
983
Peter Zijlstra849691a2009-04-06 11:45:12 +0200984 local_irq_save(flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100985 cpu = smp_processor_id();
986
987 perf_counter_task_sched_out(curr, cpu);
988
989 spin_lock(&ctx->lock);
990
991 /*
992 * Disable all the counters:
993 */
994 perf_flags = hw_perf_save_disable();
995
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100996 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100997 if (counter->state != PERF_COUNTER_STATE_ERROR) {
998 update_group_times(counter);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100999 counter->state = PERF_COUNTER_STATE_OFF;
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001000 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001001 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01001002
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001003 hw_perf_restore(perf_flags);
1004
Peter Zijlstra849691a2009-04-06 11:45:12 +02001005 spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001006
1007 return 0;
1008}
1009
1010int perf_counter_task_enable(void)
1011{
1012 struct task_struct *curr = current;
1013 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
1014 struct perf_counter *counter;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001015 unsigned long flags;
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001016 u64 perf_flags;
1017 int cpu;
1018
1019 if (likely(!ctx->nr_counters))
1020 return 0;
1021
Peter Zijlstra849691a2009-04-06 11:45:12 +02001022 local_irq_save(flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001023 cpu = smp_processor_id();
1024
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001025 perf_counter_task_sched_out(curr, cpu);
1026
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001027 spin_lock(&ctx->lock);
1028
1029 /*
1030 * Disable all the counters:
1031 */
1032 perf_flags = hw_perf_save_disable();
1033
1034 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001035 if (counter->state > PERF_COUNTER_STATE_OFF)
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001036 continue;
Ingo Molnar6a930702008-12-11 15:17:03 +01001037 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +02001038 counter->tstamp_enabled =
1039 ctx->time - counter->total_time_enabled;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001040 counter->hw_event.disabled = 0;
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001041 }
1042 hw_perf_restore(perf_flags);
1043
1044 spin_unlock(&ctx->lock);
1045
1046 perf_counter_task_sched_in(curr, cpu);
1047
Peter Zijlstra849691a2009-04-06 11:45:12 +02001048 local_irq_restore(flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001049
1050 return 0;
1051}
1052
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001053/*
1054 * Round-robin a context's counters:
1055 */
1056static void rotate_ctx(struct perf_counter_context *ctx)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001057{
Thomas Gleixner0793a612008-12-04 20:12:29 +01001058 struct perf_counter *counter;
Ingo Molnar5c92d122008-12-11 13:21:10 +01001059 u64 perf_flags;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001060
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001061 if (!ctx->nr_counters)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001062 return;
1063
Thomas Gleixner0793a612008-12-04 20:12:29 +01001064 spin_lock(&ctx->lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001065 /*
Ingo Molnar04289bb2008-12-11 08:38:42 +01001066 * Rotate the first entry last (works just fine for group counters too):
Thomas Gleixner0793a612008-12-04 20:12:29 +01001067 */
Ingo Molnar01b28382008-12-11 13:45:51 +01001068 perf_flags = hw_perf_save_disable();
Ingo Molnar04289bb2008-12-11 08:38:42 +01001069 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Peter Zijlstra75564232009-03-13 12:21:29 +01001070 list_move_tail(&counter->list_entry, &ctx->counter_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001071 break;
1072 }
Ingo Molnar01b28382008-12-11 13:45:51 +01001073 hw_perf_restore(perf_flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001074
1075 spin_unlock(&ctx->lock);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001076}
Thomas Gleixner0793a612008-12-04 20:12:29 +01001077
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001078void perf_counter_task_tick(struct task_struct *curr, int cpu)
1079{
Peter Zijlstra7fc23a52009-05-08 18:52:21 +02001080 struct perf_cpu_context *cpuctx;
1081 struct perf_counter_context *ctx;
1082
1083 if (!atomic_read(&nr_counters))
1084 return;
1085
1086 cpuctx = &per_cpu(perf_cpu_context, cpu);
1087 ctx = &curr->perf_counter_ctx;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001088
Ingo Molnarb82914c2009-05-04 18:54:32 +02001089 perf_counter_cpu_sched_out(cpuctx);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001090 perf_counter_task_sched_out(curr, cpu);
1091
Ingo Molnarb82914c2009-05-04 18:54:32 +02001092 rotate_ctx(&cpuctx->ctx);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001093 rotate_ctx(ctx);
1094
Ingo Molnarb82914c2009-05-04 18:54:32 +02001095 perf_counter_cpu_sched_in(cpuctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001096 perf_counter_task_sched_in(curr, cpu);
1097}
1098
1099/*
Thomas Gleixner0793a612008-12-04 20:12:29 +01001100 * Cross CPU call to read the hardware counter
1101 */
Ingo Molnar76715812008-12-17 14:20:28 +01001102static void __read(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001103{
Ingo Molnar621a01e2008-12-11 12:46:46 +01001104 struct perf_counter *counter = info;
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001105 struct perf_counter_context *ctx = counter->ctx;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001106 unsigned long flags;
Ingo Molnar621a01e2008-12-11 12:46:46 +01001107
Peter Zijlstra849691a2009-04-06 11:45:12 +02001108 local_irq_save(flags);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001109 if (ctx->is_active)
Peter Zijlstra4af49982009-04-06 11:45:10 +02001110 update_context_time(ctx);
Robert Richter4aeb0b42009-04-29 12:47:03 +02001111 counter->pmu->read(counter);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001112 update_counter_times(counter);
Peter Zijlstra849691a2009-04-06 11:45:12 +02001113 local_irq_restore(flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001114}
1115
Ingo Molnar04289bb2008-12-11 08:38:42 +01001116static u64 perf_counter_read(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001117{
1118 /*
1119 * If counter is enabled and currently active on a CPU, update the
1120 * value in the counter structure:
1121 */
Ingo Molnar6a930702008-12-11 15:17:03 +01001122 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
Thomas Gleixner0793a612008-12-04 20:12:29 +01001123 smp_call_function_single(counter->oncpu,
Ingo Molnar76715812008-12-17 14:20:28 +01001124 __read, counter, 1);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001125 } else if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
1126 update_counter_times(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001127 }
1128
Ingo Molnaree060942008-12-13 09:00:03 +01001129 return atomic64_read(&counter->count);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001130}
1131
Thomas Gleixner0793a612008-12-04 20:12:29 +01001132static void put_context(struct perf_counter_context *ctx)
1133{
1134 if (ctx->task)
1135 put_task_struct(ctx->task);
1136}
1137
1138static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
1139{
1140 struct perf_cpu_context *cpuctx;
1141 struct perf_counter_context *ctx;
1142 struct task_struct *task;
1143
1144 /*
1145 * If cpu is not a wildcard then this is a percpu counter:
1146 */
1147 if (cpu != -1) {
1148 /* Must be root to operate on a CPU counter: */
Peter Zijlstra1ccd1542009-04-09 10:53:45 +02001149 if (sysctl_perf_counter_priv && !capable(CAP_SYS_ADMIN))
Thomas Gleixner0793a612008-12-04 20:12:29 +01001150 return ERR_PTR(-EACCES);
1151
1152 if (cpu < 0 || cpu > num_possible_cpus())
1153 return ERR_PTR(-EINVAL);
1154
1155 /*
1156 * We could be clever and allow to attach a counter to an
1157 * offline CPU and activate it when the CPU comes up, but
1158 * that's for later.
1159 */
1160 if (!cpu_isset(cpu, cpu_online_map))
1161 return ERR_PTR(-ENODEV);
1162
1163 cpuctx = &per_cpu(perf_cpu_context, cpu);
1164 ctx = &cpuctx->ctx;
1165
Thomas Gleixner0793a612008-12-04 20:12:29 +01001166 return ctx;
1167 }
1168
1169 rcu_read_lock();
1170 if (!pid)
1171 task = current;
1172 else
1173 task = find_task_by_vpid(pid);
1174 if (task)
1175 get_task_struct(task);
1176 rcu_read_unlock();
1177
1178 if (!task)
1179 return ERR_PTR(-ESRCH);
1180
1181 ctx = &task->perf_counter_ctx;
1182 ctx->task = task;
1183
1184 /* Reuse ptrace permission checks for now. */
1185 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
1186 put_context(ctx);
1187 return ERR_PTR(-EACCES);
1188 }
1189
1190 return ctx;
1191}
1192
Peter Zijlstra592903c2009-03-13 12:21:36 +01001193static void free_counter_rcu(struct rcu_head *head)
1194{
1195 struct perf_counter *counter;
1196
1197 counter = container_of(head, struct perf_counter, rcu_head);
1198 kfree(counter);
1199}
1200
Peter Zijlstra925d5192009-03-30 19:07:02 +02001201static void perf_pending_sync(struct perf_counter *counter);
1202
Peter Zijlstraf1600952009-03-19 20:26:16 +01001203static void free_counter(struct perf_counter *counter)
1204{
Peter Zijlstra925d5192009-03-30 19:07:02 +02001205 perf_pending_sync(counter);
1206
Peter Zijlstra7fc23a52009-05-08 18:52:21 +02001207 atomic_dec(&nr_counters);
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02001208 if (counter->hw_event.mmap)
1209 atomic_dec(&nr_mmap_tracking);
1210 if (counter->hw_event.munmap)
1211 atomic_dec(&nr_munmap_tracking);
1212 if (counter->hw_event.comm)
1213 atomic_dec(&nr_comm_tracking);
1214
Peter Zijlstrae077df42009-03-19 20:26:17 +01001215 if (counter->destroy)
1216 counter->destroy(counter);
1217
Peter Zijlstraf1600952009-03-19 20:26:16 +01001218 call_rcu(&counter->rcu_head, free_counter_rcu);
1219}
1220
Thomas Gleixner0793a612008-12-04 20:12:29 +01001221/*
1222 * Called when the last reference to the file is gone.
1223 */
1224static int perf_release(struct inode *inode, struct file *file)
1225{
1226 struct perf_counter *counter = file->private_data;
1227 struct perf_counter_context *ctx = counter->ctx;
1228
1229 file->private_data = NULL;
1230
Paul Mackerrasd859e292009-01-17 18:10:22 +11001231 mutex_lock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001232 mutex_lock(&counter->mutex);
1233
Ingo Molnar04289bb2008-12-11 08:38:42 +01001234 perf_counter_remove_from_context(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001235
1236 mutex_unlock(&counter->mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001237 mutex_unlock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001238
Peter Zijlstraf1600952009-03-19 20:26:16 +01001239 free_counter(counter);
Mike Galbraith5af75912009-02-11 10:53:37 +01001240 put_context(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001241
1242 return 0;
1243}
1244
1245/*
1246 * Read the performance counter - simple non blocking version for now
1247 */
1248static ssize_t
1249perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
1250{
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001251 u64 values[3];
1252 int n;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001253
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001254 /*
1255 * Return end-of-file for a read on a counter that is in
1256 * error state (i.e. because it was pinned but it couldn't be
1257 * scheduled on to the CPU at some point).
1258 */
1259 if (counter->state == PERF_COUNTER_STATE_ERROR)
1260 return 0;
1261
Thomas Gleixner0793a612008-12-04 20:12:29 +01001262 mutex_lock(&counter->mutex);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001263 values[0] = perf_counter_read(counter);
1264 n = 1;
1265 if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1266 values[n++] = counter->total_time_enabled +
1267 atomic64_read(&counter->child_total_time_enabled);
1268 if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1269 values[n++] = counter->total_time_running +
1270 atomic64_read(&counter->child_total_time_running);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001271 mutex_unlock(&counter->mutex);
1272
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001273 if (count < n * sizeof(u64))
1274 return -EINVAL;
1275 count = n * sizeof(u64);
1276
1277 if (copy_to_user(buf, values, count))
1278 return -EFAULT;
1279
1280 return count;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001281}
1282
1283static ssize_t
Thomas Gleixner0793a612008-12-04 20:12:29 +01001284perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
1285{
1286 struct perf_counter *counter = file->private_data;
1287
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001288 return perf_read_hw(counter, buf, count);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001289}
1290
1291static unsigned int perf_poll(struct file *file, poll_table *wait)
1292{
1293 struct perf_counter *counter = file->private_data;
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001294 struct perf_mmap_data *data;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001295 unsigned int events = POLL_HUP;
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001296
1297 rcu_read_lock();
1298 data = rcu_dereference(counter->data);
1299 if (data)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001300 events = atomic_xchg(&data->poll, 0);
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001301 rcu_read_unlock();
Thomas Gleixner0793a612008-12-04 20:12:29 +01001302
1303 poll_wait(file, &counter->waitq, wait);
1304
Thomas Gleixner0793a612008-12-04 20:12:29 +01001305 return events;
1306}
1307
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001308static void perf_counter_reset(struct perf_counter *counter)
1309{
1310 atomic_set(&counter->count, 0);
1311}
1312
Paul Mackerrasd859e292009-01-17 18:10:22 +11001313static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1314{
1315 struct perf_counter *counter = file->private_data;
1316 int err = 0;
1317
1318 switch (cmd) {
1319 case PERF_COUNTER_IOC_ENABLE:
1320 perf_counter_enable_family(counter);
1321 break;
1322 case PERF_COUNTER_IOC_DISABLE:
1323 perf_counter_disable_family(counter);
1324 break;
Peter Zijlstra79f14642009-04-06 11:45:07 +02001325 case PERF_COUNTER_IOC_REFRESH:
Peter Zijlstra2023b352009-05-05 17:50:26 +02001326 err = perf_counter_refresh(counter, arg);
Peter Zijlstra79f14642009-04-06 11:45:07 +02001327 break;
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001328 case PERF_COUNTER_IOC_RESET:
1329 perf_counter_reset(counter);
1330 break;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001331 default:
1332 err = -ENOTTY;
1333 }
1334 return err;
1335}
1336
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001337/*
1338 * Callers need to ensure there can be no nesting of this function, otherwise
1339 * the seqlock logic goes bad. We can not serialize this because the arch
1340 * code calls this from NMI context.
1341 */
1342void perf_counter_update_userpage(struct perf_counter *counter)
Paul Mackerras37d81822009-03-23 18:22:08 +01001343{
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001344 struct perf_mmap_data *data;
1345 struct perf_counter_mmap_page *userpg;
1346
1347 rcu_read_lock();
1348 data = rcu_dereference(counter->data);
1349 if (!data)
1350 goto unlock;
1351
1352 userpg = data->user_page;
Paul Mackerras37d81822009-03-23 18:22:08 +01001353
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001354 /*
1355 * Disable preemption so as to not let the corresponding user-space
1356 * spin too long if we get preempted.
1357 */
1358 preempt_disable();
Paul Mackerras37d81822009-03-23 18:22:08 +01001359 ++userpg->lock;
Peter Zijlstra92f22a32009-04-02 11:12:04 +02001360 barrier();
Paul Mackerras37d81822009-03-23 18:22:08 +01001361 userpg->index = counter->hw.idx;
1362 userpg->offset = atomic64_read(&counter->count);
1363 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
1364 userpg->offset -= atomic64_read(&counter->hw.prev_count);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001365
Peter Zijlstra92f22a32009-04-02 11:12:04 +02001366 barrier();
Paul Mackerras37d81822009-03-23 18:22:08 +01001367 ++userpg->lock;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001368 preempt_enable();
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001369unlock:
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001370 rcu_read_unlock();
Paul Mackerras37d81822009-03-23 18:22:08 +01001371}
1372
1373static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1374{
1375 struct perf_counter *counter = vma->vm_file->private_data;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001376 struct perf_mmap_data *data;
1377 int ret = VM_FAULT_SIGBUS;
Paul Mackerras37d81822009-03-23 18:22:08 +01001378
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001379 rcu_read_lock();
1380 data = rcu_dereference(counter->data);
1381 if (!data)
1382 goto unlock;
Paul Mackerras37d81822009-03-23 18:22:08 +01001383
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001384 if (vmf->pgoff == 0) {
1385 vmf->page = virt_to_page(data->user_page);
1386 } else {
1387 int nr = vmf->pgoff - 1;
1388
1389 if ((unsigned)nr > data->nr_pages)
1390 goto unlock;
1391
1392 vmf->page = virt_to_page(data->data_pages[nr]);
1393 }
Paul Mackerras37d81822009-03-23 18:22:08 +01001394 get_page(vmf->page);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001395 ret = 0;
1396unlock:
1397 rcu_read_unlock();
1398
1399 return ret;
1400}
1401
1402static int perf_mmap_data_alloc(struct perf_counter *counter, int nr_pages)
1403{
1404 struct perf_mmap_data *data;
1405 unsigned long size;
1406 int i;
1407
1408 WARN_ON(atomic_read(&counter->mmap_count));
1409
1410 size = sizeof(struct perf_mmap_data);
1411 size += nr_pages * sizeof(void *);
1412
1413 data = kzalloc(size, GFP_KERNEL);
1414 if (!data)
1415 goto fail;
1416
1417 data->user_page = (void *)get_zeroed_page(GFP_KERNEL);
1418 if (!data->user_page)
1419 goto fail_user_page;
1420
1421 for (i = 0; i < nr_pages; i++) {
1422 data->data_pages[i] = (void *)get_zeroed_page(GFP_KERNEL);
1423 if (!data->data_pages[i])
1424 goto fail_data_pages;
1425 }
1426
1427 data->nr_pages = nr_pages;
Peter Zijlstra22c15582009-05-05 17:50:25 +02001428 atomic_set(&data->lock, -1);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001429
1430 rcu_assign_pointer(counter->data, data);
1431
Paul Mackerras37d81822009-03-23 18:22:08 +01001432 return 0;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001433
1434fail_data_pages:
1435 for (i--; i >= 0; i--)
1436 free_page((unsigned long)data->data_pages[i]);
1437
1438 free_page((unsigned long)data->user_page);
1439
1440fail_user_page:
1441 kfree(data);
1442
1443fail:
1444 return -ENOMEM;
1445}
1446
1447static void __perf_mmap_data_free(struct rcu_head *rcu_head)
1448{
1449 struct perf_mmap_data *data = container_of(rcu_head,
1450 struct perf_mmap_data, rcu_head);
1451 int i;
1452
1453 free_page((unsigned long)data->user_page);
1454 for (i = 0; i < data->nr_pages; i++)
1455 free_page((unsigned long)data->data_pages[i]);
1456 kfree(data);
1457}
1458
1459static void perf_mmap_data_free(struct perf_counter *counter)
1460{
1461 struct perf_mmap_data *data = counter->data;
1462
1463 WARN_ON(atomic_read(&counter->mmap_count));
1464
1465 rcu_assign_pointer(counter->data, NULL);
1466 call_rcu(&data->rcu_head, __perf_mmap_data_free);
1467}
1468
1469static void perf_mmap_open(struct vm_area_struct *vma)
1470{
1471 struct perf_counter *counter = vma->vm_file->private_data;
1472
1473 atomic_inc(&counter->mmap_count);
1474}
1475
1476static void perf_mmap_close(struct vm_area_struct *vma)
1477{
1478 struct perf_counter *counter = vma->vm_file->private_data;
1479
1480 if (atomic_dec_and_mutex_lock(&counter->mmap_count,
1481 &counter->mmap_mutex)) {
Peter Zijlstrac5078f72009-05-05 17:50:24 +02001482 vma->vm_mm->locked_vm -= counter->data->nr_locked;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001483 perf_mmap_data_free(counter);
1484 mutex_unlock(&counter->mmap_mutex);
1485 }
Paul Mackerras37d81822009-03-23 18:22:08 +01001486}
1487
1488static struct vm_operations_struct perf_mmap_vmops = {
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001489 .open = perf_mmap_open,
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001490 .close = perf_mmap_close,
Paul Mackerras37d81822009-03-23 18:22:08 +01001491 .fault = perf_mmap_fault,
1492};
1493
1494static int perf_mmap(struct file *file, struct vm_area_struct *vma)
1495{
1496 struct perf_counter *counter = file->private_data;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001497 unsigned long vma_size;
1498 unsigned long nr_pages;
1499 unsigned long locked, lock_limit;
1500 int ret = 0;
Peter Zijlstrac5078f72009-05-05 17:50:24 +02001501 long extra;
Paul Mackerras37d81822009-03-23 18:22:08 +01001502
1503 if (!(vma->vm_flags & VM_SHARED) || (vma->vm_flags & VM_WRITE))
1504 return -EINVAL;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001505
1506 vma_size = vma->vm_end - vma->vm_start;
1507 nr_pages = (vma_size / PAGE_SIZE) - 1;
1508
Peter Zijlstra7730d862009-03-25 12:48:31 +01001509 /*
1510 * If we have data pages ensure they're a power-of-two number, so we
1511 * can do bitmasks instead of modulo.
1512 */
1513 if (nr_pages != 0 && !is_power_of_2(nr_pages))
Paul Mackerras37d81822009-03-23 18:22:08 +01001514 return -EINVAL;
1515
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001516 if (vma_size != PAGE_SIZE * (1 + nr_pages))
Paul Mackerras37d81822009-03-23 18:22:08 +01001517 return -EINVAL;
1518
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001519 if (vma->vm_pgoff != 0)
1520 return -EINVAL;
Paul Mackerras37d81822009-03-23 18:22:08 +01001521
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001522 mutex_lock(&counter->mmap_mutex);
1523 if (atomic_inc_not_zero(&counter->mmap_count)) {
1524 if (nr_pages != counter->data->nr_pages)
1525 ret = -EINVAL;
1526 goto unlock;
1527 }
1528
Peter Zijlstrac5078f72009-05-05 17:50:24 +02001529 extra = nr_pages /* + 1 only account the data pages */;
1530 extra -= sysctl_perf_counter_mlock >> (PAGE_SHIFT - 10);
1531 if (extra < 0)
1532 extra = 0;
1533
1534 locked = vma->vm_mm->locked_vm + extra;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001535
1536 lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
1537 lock_limit >>= PAGE_SHIFT;
1538
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001539 if ((locked > lock_limit) && !capable(CAP_IPC_LOCK)) {
1540 ret = -EPERM;
1541 goto unlock;
1542 }
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001543
1544 WARN_ON(counter->data);
1545 ret = perf_mmap_data_alloc(counter, nr_pages);
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001546 if (ret)
1547 goto unlock;
1548
1549 atomic_set(&counter->mmap_count, 1);
Peter Zijlstrac5078f72009-05-05 17:50:24 +02001550 vma->vm_mm->locked_vm += extra;
1551 counter->data->nr_locked = extra;
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001552unlock:
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001553 mutex_unlock(&counter->mmap_mutex);
Paul Mackerras37d81822009-03-23 18:22:08 +01001554
1555 vma->vm_flags &= ~VM_MAYWRITE;
1556 vma->vm_flags |= VM_RESERVED;
1557 vma->vm_ops = &perf_mmap_vmops;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001558
1559 return ret;
Paul Mackerras37d81822009-03-23 18:22:08 +01001560}
1561
Peter Zijlstra3c446b3d2009-04-06 11:45:01 +02001562static int perf_fasync(int fd, struct file *filp, int on)
1563{
1564 struct perf_counter *counter = filp->private_data;
1565 struct inode *inode = filp->f_path.dentry->d_inode;
1566 int retval;
1567
1568 mutex_lock(&inode->i_mutex);
1569 retval = fasync_helper(fd, filp, on, &counter->fasync);
1570 mutex_unlock(&inode->i_mutex);
1571
1572 if (retval < 0)
1573 return retval;
1574
1575 return 0;
1576}
1577
Thomas Gleixner0793a612008-12-04 20:12:29 +01001578static const struct file_operations perf_fops = {
1579 .release = perf_release,
1580 .read = perf_read,
1581 .poll = perf_poll,
Paul Mackerrasd859e292009-01-17 18:10:22 +11001582 .unlocked_ioctl = perf_ioctl,
1583 .compat_ioctl = perf_ioctl,
Paul Mackerras37d81822009-03-23 18:22:08 +01001584 .mmap = perf_mmap,
Peter Zijlstra3c446b3d2009-04-06 11:45:01 +02001585 .fasync = perf_fasync,
Thomas Gleixner0793a612008-12-04 20:12:29 +01001586};
1587
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001588/*
Peter Zijlstra925d5192009-03-30 19:07:02 +02001589 * Perf counter wakeup
1590 *
1591 * If there's data, ensure we set the poll() state and publish everything
1592 * to user-space before waking everybody up.
1593 */
1594
1595void perf_counter_wakeup(struct perf_counter *counter)
1596{
Peter Zijlstra925d5192009-03-30 19:07:02 +02001597 wake_up_all(&counter->waitq);
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02001598
1599 if (counter->pending_kill) {
1600 kill_fasync(&counter->fasync, SIGIO, counter->pending_kill);
1601 counter->pending_kill = 0;
1602 }
Peter Zijlstra925d5192009-03-30 19:07:02 +02001603}
1604
1605/*
1606 * Pending wakeups
1607 *
1608 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
1609 *
1610 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
1611 * single linked list and use cmpxchg() to add entries lockless.
1612 */
1613
Peter Zijlstra79f14642009-04-06 11:45:07 +02001614static void perf_pending_counter(struct perf_pending_entry *entry)
1615{
1616 struct perf_counter *counter = container_of(entry,
1617 struct perf_counter, pending);
1618
1619 if (counter->pending_disable) {
1620 counter->pending_disable = 0;
1621 perf_counter_disable(counter);
1622 }
1623
1624 if (counter->pending_wakeup) {
1625 counter->pending_wakeup = 0;
1626 perf_counter_wakeup(counter);
1627 }
1628}
1629
Peter Zijlstra671dec52009-04-06 11:45:02 +02001630#define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
Peter Zijlstra925d5192009-03-30 19:07:02 +02001631
Peter Zijlstra671dec52009-04-06 11:45:02 +02001632static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = {
Peter Zijlstra925d5192009-03-30 19:07:02 +02001633 PENDING_TAIL,
1634};
1635
Peter Zijlstra671dec52009-04-06 11:45:02 +02001636static void perf_pending_queue(struct perf_pending_entry *entry,
1637 void (*func)(struct perf_pending_entry *))
Peter Zijlstra925d5192009-03-30 19:07:02 +02001638{
Peter Zijlstra671dec52009-04-06 11:45:02 +02001639 struct perf_pending_entry **head;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001640
Peter Zijlstra671dec52009-04-06 11:45:02 +02001641 if (cmpxchg(&entry->next, NULL, PENDING_TAIL) != NULL)
Peter Zijlstra925d5192009-03-30 19:07:02 +02001642 return;
1643
Peter Zijlstra671dec52009-04-06 11:45:02 +02001644 entry->func = func;
1645
1646 head = &get_cpu_var(perf_pending_head);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001647
1648 do {
Peter Zijlstra671dec52009-04-06 11:45:02 +02001649 entry->next = *head;
1650 } while (cmpxchg(head, entry->next, entry) != entry->next);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001651
1652 set_perf_counter_pending();
1653
Peter Zijlstra671dec52009-04-06 11:45:02 +02001654 put_cpu_var(perf_pending_head);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001655}
1656
1657static int __perf_pending_run(void)
1658{
Peter Zijlstra671dec52009-04-06 11:45:02 +02001659 struct perf_pending_entry *list;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001660 int nr = 0;
1661
Peter Zijlstra671dec52009-04-06 11:45:02 +02001662 list = xchg(&__get_cpu_var(perf_pending_head), PENDING_TAIL);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001663 while (list != PENDING_TAIL) {
Peter Zijlstra671dec52009-04-06 11:45:02 +02001664 void (*func)(struct perf_pending_entry *);
1665 struct perf_pending_entry *entry = list;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001666
1667 list = list->next;
1668
Peter Zijlstra671dec52009-04-06 11:45:02 +02001669 func = entry->func;
1670 entry->next = NULL;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001671 /*
1672 * Ensure we observe the unqueue before we issue the wakeup,
1673 * so that we won't be waiting forever.
1674 * -- see perf_not_pending().
1675 */
1676 smp_wmb();
1677
Peter Zijlstra671dec52009-04-06 11:45:02 +02001678 func(entry);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001679 nr++;
1680 }
1681
1682 return nr;
1683}
1684
1685static inline int perf_not_pending(struct perf_counter *counter)
1686{
1687 /*
1688 * If we flush on whatever cpu we run, there is a chance we don't
1689 * need to wait.
1690 */
1691 get_cpu();
1692 __perf_pending_run();
1693 put_cpu();
1694
1695 /*
1696 * Ensure we see the proper queue state before going to sleep
1697 * so that we do not miss the wakeup. -- see perf_pending_handle()
1698 */
1699 smp_rmb();
Peter Zijlstra671dec52009-04-06 11:45:02 +02001700 return counter->pending.next == NULL;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001701}
1702
1703static void perf_pending_sync(struct perf_counter *counter)
1704{
1705 wait_event(counter->waitq, perf_not_pending(counter));
1706}
1707
1708void perf_counter_do_pending(void)
1709{
1710 __perf_pending_run();
1711}
1712
1713/*
Peter Zijlstra394ee072009-03-30 19:07:14 +02001714 * Callchain support -- arch specific
1715 */
1716
Peter Zijlstra9c03d882009-04-06 11:45:00 +02001717__weak struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
Peter Zijlstra394ee072009-03-30 19:07:14 +02001718{
1719 return NULL;
1720}
1721
1722/*
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001723 * Output
1724 */
1725
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001726struct perf_output_handle {
1727 struct perf_counter *counter;
1728 struct perf_mmap_data *data;
1729 unsigned int offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01001730 unsigned int head;
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001731 int nmi;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02001732 int overflow;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001733 int locked;
1734 unsigned long flags;
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001735};
1736
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001737static void perf_output_wakeup(struct perf_output_handle *handle)
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001738{
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001739 atomic_set(&handle->data->poll, POLL_IN);
1740
Peter Zijlstra671dec52009-04-06 11:45:02 +02001741 if (handle->nmi) {
Peter Zijlstra79f14642009-04-06 11:45:07 +02001742 handle->counter->pending_wakeup = 1;
Peter Zijlstra671dec52009-04-06 11:45:02 +02001743 perf_pending_queue(&handle->counter->pending,
Peter Zijlstra79f14642009-04-06 11:45:07 +02001744 perf_pending_counter);
Peter Zijlstra671dec52009-04-06 11:45:02 +02001745 } else
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001746 perf_counter_wakeup(handle->counter);
1747}
1748
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001749/*
1750 * Curious locking construct.
1751 *
1752 * We need to ensure a later event doesn't publish a head when a former
1753 * event isn't done writing. However since we need to deal with NMIs we
1754 * cannot fully serialize things.
1755 *
1756 * What we do is serialize between CPUs so we only have to deal with NMI
1757 * nesting on a single CPU.
1758 *
1759 * We only publish the head (and generate a wakeup) when the outer-most
1760 * event completes.
1761 */
1762static void perf_output_lock(struct perf_output_handle *handle)
1763{
1764 struct perf_mmap_data *data = handle->data;
1765 int cpu;
1766
1767 handle->locked = 0;
1768
1769 local_irq_save(handle->flags);
1770 cpu = smp_processor_id();
1771
1772 if (in_nmi() && atomic_read(&data->lock) == cpu)
1773 return;
1774
Peter Zijlstra22c15582009-05-05 17:50:25 +02001775 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001776 cpu_relax();
1777
1778 handle->locked = 1;
1779}
1780
1781static void perf_output_unlock(struct perf_output_handle *handle)
1782{
1783 struct perf_mmap_data *data = handle->data;
1784 int head, cpu;
1785
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02001786 data->done_head = data->head;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001787
1788 if (!handle->locked)
1789 goto out;
1790
1791again:
1792 /*
1793 * The xchg implies a full barrier that ensures all writes are done
1794 * before we publish the new head, matched by a rmb() in userspace when
1795 * reading this position.
1796 */
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02001797 while ((head = atomic_xchg(&data->done_head, 0)))
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001798 data->user_page->data_head = head;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001799
1800 /*
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02001801 * NMI can happen here, which means we can miss a done_head update.
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001802 */
1803
Peter Zijlstra22c15582009-05-05 17:50:25 +02001804 cpu = atomic_xchg(&data->lock, -1);
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001805 WARN_ON_ONCE(cpu != smp_processor_id());
1806
1807 /*
1808 * Therefore we have to validate we did not indeed do so.
1809 */
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02001810 if (unlikely(atomic_read(&data->done_head))) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001811 /*
1812 * Since we had it locked, we can lock it again.
1813 */
Peter Zijlstra22c15582009-05-05 17:50:25 +02001814 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001815 cpu_relax();
1816
1817 goto again;
1818 }
1819
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02001820 if (atomic_xchg(&data->wakeup, 0))
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001821 perf_output_wakeup(handle);
1822out:
1823 local_irq_restore(handle->flags);
1824}
1825
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001826static int perf_output_begin(struct perf_output_handle *handle,
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001827 struct perf_counter *counter, unsigned int size,
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02001828 int nmi, int overflow)
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001829{
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001830 struct perf_mmap_data *data;
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001831 unsigned int offset, head;
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001832
Peter Zijlstra2023b352009-05-05 17:50:26 +02001833 /*
1834 * For inherited counters we send all the output towards the parent.
1835 */
1836 if (counter->parent)
1837 counter = counter->parent;
1838
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001839 rcu_read_lock();
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001840 data = rcu_dereference(counter->data);
1841 if (!data)
1842 goto out;
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001843
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001844 handle->data = data;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02001845 handle->counter = counter;
1846 handle->nmi = nmi;
1847 handle->overflow = overflow;
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001848
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001849 if (!data->nr_pages)
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001850 goto fail;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001851
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001852 perf_output_lock(handle);
1853
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001854 do {
1855 offset = head = atomic_read(&data->head);
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001856 head += size;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001857 } while (atomic_cmpxchg(&data->head, offset, head) != offset);
1858
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001859 handle->offset = offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01001860 handle->head = head;
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02001861
1862 if ((offset >> PAGE_SHIFT) != (head >> PAGE_SHIFT))
1863 atomic_set(&data->wakeup, 1);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001864
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001865 return 0;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001866
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001867fail:
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001868 perf_output_wakeup(handle);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001869out:
1870 rcu_read_unlock();
1871
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001872 return -ENOSPC;
1873}
1874
1875static void perf_output_copy(struct perf_output_handle *handle,
1876 void *buf, unsigned int len)
1877{
1878 unsigned int pages_mask;
1879 unsigned int offset;
1880 unsigned int size;
1881 void **pages;
1882
1883 offset = handle->offset;
1884 pages_mask = handle->data->nr_pages - 1;
1885 pages = handle->data->data_pages;
1886
1887 do {
1888 unsigned int page_offset;
1889 int nr;
1890
1891 nr = (offset >> PAGE_SHIFT) & pages_mask;
1892 page_offset = offset & (PAGE_SIZE - 1);
1893 size = min_t(unsigned int, PAGE_SIZE - page_offset, len);
1894
1895 memcpy(pages[nr] + page_offset, buf, size);
1896
1897 len -= size;
1898 buf += size;
1899 offset += size;
1900 } while (len);
1901
1902 handle->offset = offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01001903
1904 WARN_ON_ONCE(handle->offset > handle->head);
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001905}
1906
Peter Zijlstra5c148192009-03-25 12:30:23 +01001907#define perf_output_put(handle, x) \
1908 perf_output_copy((handle), &(x), sizeof(x))
1909
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001910static void perf_output_end(struct perf_output_handle *handle)
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001911{
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001912 struct perf_counter *counter = handle->counter;
1913 struct perf_mmap_data *data = handle->data;
1914
1915 int wakeup_events = counter->hw_event.wakeup_events;
Peter Zijlstrac4578102009-04-02 11:12:01 +02001916
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02001917 if (handle->overflow && wakeup_events) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001918 int events = atomic_inc_return(&data->events);
Peter Zijlstrac4578102009-04-02 11:12:01 +02001919 if (events >= wakeup_events) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001920 atomic_sub(wakeup_events, &data->events);
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02001921 atomic_set(&data->wakeup, 1);
Peter Zijlstrac4578102009-04-02 11:12:01 +02001922 }
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001923 }
1924
1925 perf_output_unlock(handle);
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001926 rcu_read_unlock();
1927}
1928
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02001929static void perf_counter_output(struct perf_counter *counter,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02001930 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001931{
Peter Zijlstra5ed00412009-03-30 19:07:12 +02001932 int ret;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02001933 u64 record_type = counter->hw_event.record_type;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02001934 struct perf_output_handle handle;
1935 struct perf_event_header header;
1936 u64 ip;
Peter Zijlstra5c148192009-03-25 12:30:23 +01001937 struct {
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01001938 u32 pid, tid;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02001939 } tid_entry;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02001940 struct {
1941 u64 event;
1942 u64 counter;
1943 } group_entry;
Peter Zijlstra394ee072009-03-30 19:07:14 +02001944 struct perf_callchain_entry *callchain = NULL;
1945 int callchain_size = 0;
Peter Zijlstra339f7c92009-04-06 11:45:06 +02001946 u64 time;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001947
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02001948 header.type = 0;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02001949 header.size = sizeof(header);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001950
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02001951 header.misc = PERF_EVENT_MISC_OVERFLOW;
1952 header.misc |= user_mode(regs) ?
Peter Zijlstra6fab0192009-04-08 15:01:26 +02001953 PERF_EVENT_MISC_USER : PERF_EVENT_MISC_KERNEL;
1954
Peter Zijlstra8a057d82009-04-02 11:11:59 +02001955 if (record_type & PERF_RECORD_IP) {
1956 ip = instruction_pointer(regs);
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02001957 header.type |= PERF_RECORD_IP;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02001958 header.size += sizeof(ip);
1959 }
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01001960
Peter Zijlstra8a057d82009-04-02 11:11:59 +02001961 if (record_type & PERF_RECORD_TID) {
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01001962 /* namespace issues */
Peter Zijlstra5ed00412009-03-30 19:07:12 +02001963 tid_entry.pid = current->group_leader->pid;
1964 tid_entry.tid = current->pid;
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01001965
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02001966 header.type |= PERF_RECORD_TID;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02001967 header.size += sizeof(tid_entry);
1968 }
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01001969
Peter Zijlstra4d855452009-04-08 15:01:32 +02001970 if (record_type & PERF_RECORD_TIME) {
1971 /*
1972 * Maybe do better on x86 and provide cpu_clock_nmi()
1973 */
1974 time = sched_clock();
1975
1976 header.type |= PERF_RECORD_TIME;
1977 header.size += sizeof(u64);
1978 }
1979
Peter Zijlstra78f13e92009-04-08 15:01:33 +02001980 if (record_type & PERF_RECORD_ADDR) {
1981 header.type |= PERF_RECORD_ADDR;
1982 header.size += sizeof(u64);
1983 }
1984
Peter Zijlstra8a057d82009-04-02 11:11:59 +02001985 if (record_type & PERF_RECORD_GROUP) {
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02001986 header.type |= PERF_RECORD_GROUP;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02001987 header.size += sizeof(u64) +
1988 counter->nr_siblings * sizeof(group_entry);
1989 }
1990
1991 if (record_type & PERF_RECORD_CALLCHAIN) {
Peter Zijlstra394ee072009-03-30 19:07:14 +02001992 callchain = perf_callchain(regs);
1993
1994 if (callchain) {
Peter Zijlstra9c03d882009-04-06 11:45:00 +02001995 callchain_size = (1 + callchain->nr) * sizeof(u64);
Peter Zijlstra394ee072009-03-30 19:07:14 +02001996
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02001997 header.type |= PERF_RECORD_CALLCHAIN;
Peter Zijlstra394ee072009-03-30 19:07:14 +02001998 header.size += callchain_size;
1999 }
2000 }
2001
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002002 ret = perf_output_begin(&handle, counter, header.size, nmi, 1);
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002003 if (ret)
2004 return;
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002005
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002006 perf_output_put(&handle, header);
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002007
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002008 if (record_type & PERF_RECORD_IP)
2009 perf_output_put(&handle, ip);
2010
2011 if (record_type & PERF_RECORD_TID)
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002012 perf_output_put(&handle, tid_entry);
2013
Peter Zijlstra4d855452009-04-08 15:01:32 +02002014 if (record_type & PERF_RECORD_TIME)
2015 perf_output_put(&handle, time);
2016
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002017 if (record_type & PERF_RECORD_ADDR)
2018 perf_output_put(&handle, addr);
2019
Peter Zijlstra2023b352009-05-05 17:50:26 +02002020 /*
2021 * XXX PERF_RECORD_GROUP vs inherited counters seems difficult.
2022 */
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002023 if (record_type & PERF_RECORD_GROUP) {
2024 struct perf_counter *leader, *sub;
2025 u64 nr = counter->nr_siblings;
2026
2027 perf_output_put(&handle, nr);
2028
2029 leader = counter->group_leader;
2030 list_for_each_entry(sub, &leader->sibling_list, list_entry) {
2031 if (sub != counter)
Robert Richter4aeb0b42009-04-29 12:47:03 +02002032 sub->pmu->read(sub);
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002033
2034 group_entry.event = sub->hw_event.config;
2035 group_entry.counter = atomic64_read(&sub->count);
2036
2037 perf_output_put(&handle, group_entry);
2038 }
2039 }
2040
Peter Zijlstra394ee072009-03-30 19:07:14 +02002041 if (callchain)
2042 perf_output_copy(&handle, callchain, callchain_size);
2043
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002044 perf_output_end(&handle);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002045}
2046
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002047/*
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002048 * comm tracking
2049 */
2050
2051struct perf_comm_event {
2052 struct task_struct *task;
2053 char *comm;
2054 int comm_size;
2055
2056 struct {
2057 struct perf_event_header header;
2058
2059 u32 pid;
2060 u32 tid;
2061 } event;
2062};
2063
2064static void perf_counter_comm_output(struct perf_counter *counter,
2065 struct perf_comm_event *comm_event)
2066{
2067 struct perf_output_handle handle;
2068 int size = comm_event->event.header.size;
2069 int ret = perf_output_begin(&handle, counter, size, 0, 0);
2070
2071 if (ret)
2072 return;
2073
2074 perf_output_put(&handle, comm_event->event);
2075 perf_output_copy(&handle, comm_event->comm,
2076 comm_event->comm_size);
2077 perf_output_end(&handle);
2078}
2079
2080static int perf_counter_comm_match(struct perf_counter *counter,
2081 struct perf_comm_event *comm_event)
2082{
2083 if (counter->hw_event.comm &&
2084 comm_event->event.header.type == PERF_EVENT_COMM)
2085 return 1;
2086
2087 return 0;
2088}
2089
2090static void perf_counter_comm_ctx(struct perf_counter_context *ctx,
2091 struct perf_comm_event *comm_event)
2092{
2093 struct perf_counter *counter;
2094
2095 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2096 return;
2097
2098 rcu_read_lock();
2099 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2100 if (perf_counter_comm_match(counter, comm_event))
2101 perf_counter_comm_output(counter, comm_event);
2102 }
2103 rcu_read_unlock();
2104}
2105
2106static void perf_counter_comm_event(struct perf_comm_event *comm_event)
2107{
2108 struct perf_cpu_context *cpuctx;
2109 unsigned int size;
2110 char *comm = comm_event->task->comm;
2111
Ingo Molnar888fcee2009-04-09 09:48:22 +02002112 size = ALIGN(strlen(comm)+1, sizeof(u64));
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002113
2114 comm_event->comm = comm;
2115 comm_event->comm_size = size;
2116
2117 comm_event->event.header.size = sizeof(comm_event->event) + size;
2118
2119 cpuctx = &get_cpu_var(perf_cpu_context);
2120 perf_counter_comm_ctx(&cpuctx->ctx, comm_event);
2121 put_cpu_var(perf_cpu_context);
2122
2123 perf_counter_comm_ctx(&current->perf_counter_ctx, comm_event);
2124}
2125
2126void perf_counter_comm(struct task_struct *task)
2127{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002128 struct perf_comm_event comm_event;
2129
2130 if (!atomic_read(&nr_comm_tracking))
2131 return;
2132
2133 comm_event = (struct perf_comm_event){
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002134 .task = task,
2135 .event = {
2136 .header = { .type = PERF_EVENT_COMM, },
2137 .pid = task->group_leader->pid,
2138 .tid = task->pid,
2139 },
2140 };
2141
2142 perf_counter_comm_event(&comm_event);
2143}
2144
2145/*
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002146 * mmap tracking
2147 */
2148
2149struct perf_mmap_event {
2150 struct file *file;
2151 char *file_name;
2152 int file_size;
2153
2154 struct {
2155 struct perf_event_header header;
2156
2157 u32 pid;
2158 u32 tid;
2159 u64 start;
2160 u64 len;
2161 u64 pgoff;
2162 } event;
2163};
2164
2165static void perf_counter_mmap_output(struct perf_counter *counter,
2166 struct perf_mmap_event *mmap_event)
2167{
2168 struct perf_output_handle handle;
2169 int size = mmap_event->event.header.size;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002170 int ret = perf_output_begin(&handle, counter, size, 0, 0);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002171
2172 if (ret)
2173 return;
2174
2175 perf_output_put(&handle, mmap_event->event);
2176 perf_output_copy(&handle, mmap_event->file_name,
2177 mmap_event->file_size);
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002178 perf_output_end(&handle);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002179}
2180
2181static int perf_counter_mmap_match(struct perf_counter *counter,
2182 struct perf_mmap_event *mmap_event)
2183{
2184 if (counter->hw_event.mmap &&
2185 mmap_event->event.header.type == PERF_EVENT_MMAP)
2186 return 1;
2187
2188 if (counter->hw_event.munmap &&
2189 mmap_event->event.header.type == PERF_EVENT_MUNMAP)
2190 return 1;
2191
2192 return 0;
2193}
2194
2195static void perf_counter_mmap_ctx(struct perf_counter_context *ctx,
2196 struct perf_mmap_event *mmap_event)
2197{
2198 struct perf_counter *counter;
2199
2200 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2201 return;
2202
2203 rcu_read_lock();
2204 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2205 if (perf_counter_mmap_match(counter, mmap_event))
2206 perf_counter_mmap_output(counter, mmap_event);
2207 }
2208 rcu_read_unlock();
2209}
2210
2211static void perf_counter_mmap_event(struct perf_mmap_event *mmap_event)
2212{
2213 struct perf_cpu_context *cpuctx;
2214 struct file *file = mmap_event->file;
2215 unsigned int size;
2216 char tmp[16];
2217 char *buf = NULL;
2218 char *name;
2219
2220 if (file) {
2221 buf = kzalloc(PATH_MAX, GFP_KERNEL);
2222 if (!buf) {
2223 name = strncpy(tmp, "//enomem", sizeof(tmp));
2224 goto got_name;
2225 }
Peter Zijlstrad3d21c42009-04-09 10:53:46 +02002226 name = d_path(&file->f_path, buf, PATH_MAX);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002227 if (IS_ERR(name)) {
2228 name = strncpy(tmp, "//toolong", sizeof(tmp));
2229 goto got_name;
2230 }
2231 } else {
2232 name = strncpy(tmp, "//anon", sizeof(tmp));
2233 goto got_name;
2234 }
2235
2236got_name:
Ingo Molnar888fcee2009-04-09 09:48:22 +02002237 size = ALIGN(strlen(name)+1, sizeof(u64));
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002238
2239 mmap_event->file_name = name;
2240 mmap_event->file_size = size;
2241
2242 mmap_event->event.header.size = sizeof(mmap_event->event) + size;
2243
2244 cpuctx = &get_cpu_var(perf_cpu_context);
2245 perf_counter_mmap_ctx(&cpuctx->ctx, mmap_event);
2246 put_cpu_var(perf_cpu_context);
2247
2248 perf_counter_mmap_ctx(&current->perf_counter_ctx, mmap_event);
2249
2250 kfree(buf);
2251}
2252
2253void perf_counter_mmap(unsigned long addr, unsigned long len,
2254 unsigned long pgoff, struct file *file)
2255{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002256 struct perf_mmap_event mmap_event;
2257
2258 if (!atomic_read(&nr_mmap_tracking))
2259 return;
2260
2261 mmap_event = (struct perf_mmap_event){
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002262 .file = file,
2263 .event = {
2264 .header = { .type = PERF_EVENT_MMAP, },
2265 .pid = current->group_leader->pid,
2266 .tid = current->pid,
2267 .start = addr,
2268 .len = len,
2269 .pgoff = pgoff,
2270 },
2271 };
2272
2273 perf_counter_mmap_event(&mmap_event);
2274}
2275
2276void perf_counter_munmap(unsigned long addr, unsigned long len,
2277 unsigned long pgoff, struct file *file)
2278{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002279 struct perf_mmap_event mmap_event;
2280
2281 if (!atomic_read(&nr_munmap_tracking))
2282 return;
2283
2284 mmap_event = (struct perf_mmap_event){
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002285 .file = file,
2286 .event = {
2287 .header = { .type = PERF_EVENT_MUNMAP, },
2288 .pid = current->group_leader->pid,
2289 .tid = current->pid,
2290 .start = addr,
2291 .len = len,
2292 .pgoff = pgoff,
2293 },
2294 };
2295
2296 perf_counter_mmap_event(&mmap_event);
2297}
2298
2299/*
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002300 * Generic counter overflow handling.
2301 */
2302
2303int perf_counter_overflow(struct perf_counter *counter,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002304 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002305{
Peter Zijlstra79f14642009-04-06 11:45:07 +02002306 int events = atomic_read(&counter->event_limit);
2307 int ret = 0;
2308
Peter Zijlstra2023b352009-05-05 17:50:26 +02002309 /*
2310 * XXX event_limit might not quite work as expected on inherited
2311 * counters
2312 */
2313
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002314 counter->pending_kill = POLL_IN;
Peter Zijlstra79f14642009-04-06 11:45:07 +02002315 if (events && atomic_dec_and_test(&counter->event_limit)) {
2316 ret = 1;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002317 counter->pending_kill = POLL_HUP;
Peter Zijlstra79f14642009-04-06 11:45:07 +02002318 if (nmi) {
2319 counter->pending_disable = 1;
2320 perf_pending_queue(&counter->pending,
2321 perf_pending_counter);
2322 } else
2323 perf_counter_disable(counter);
2324 }
2325
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002326 perf_counter_output(counter, nmi, regs, addr);
Peter Zijlstra79f14642009-04-06 11:45:07 +02002327 return ret;
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002328}
2329
2330/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002331 * Generic software counter infrastructure
2332 */
2333
2334static void perf_swcounter_update(struct perf_counter *counter)
2335{
2336 struct hw_perf_counter *hwc = &counter->hw;
2337 u64 prev, now;
2338 s64 delta;
2339
2340again:
2341 prev = atomic64_read(&hwc->prev_count);
2342 now = atomic64_read(&hwc->count);
2343 if (atomic64_cmpxchg(&hwc->prev_count, prev, now) != prev)
2344 goto again;
2345
2346 delta = now - prev;
2347
2348 atomic64_add(delta, &counter->count);
2349 atomic64_sub(delta, &hwc->period_left);
2350}
2351
2352static void perf_swcounter_set_period(struct perf_counter *counter)
2353{
2354 struct hw_perf_counter *hwc = &counter->hw;
2355 s64 left = atomic64_read(&hwc->period_left);
2356 s64 period = hwc->irq_period;
2357
2358 if (unlikely(left <= -period)) {
2359 left = period;
2360 atomic64_set(&hwc->period_left, left);
2361 }
2362
2363 if (unlikely(left <= 0)) {
2364 left += period;
2365 atomic64_add(period, &hwc->period_left);
2366 }
2367
2368 atomic64_set(&hwc->prev_count, -left);
2369 atomic64_set(&hwc->count, -left);
2370}
2371
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002372static enum hrtimer_restart perf_swcounter_hrtimer(struct hrtimer *hrtimer)
2373{
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002374 enum hrtimer_restart ret = HRTIMER_RESTART;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002375 struct perf_counter *counter;
2376 struct pt_regs *regs;
2377
2378 counter = container_of(hrtimer, struct perf_counter, hw.hrtimer);
Robert Richter4aeb0b42009-04-29 12:47:03 +02002379 counter->pmu->read(counter);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002380
2381 regs = get_irq_regs();
2382 /*
2383 * In case we exclude kernel IPs or are somehow not in interrupt
2384 * context, provide the next best thing, the user IP.
2385 */
2386 if ((counter->hw_event.exclude_kernel || !regs) &&
2387 !counter->hw_event.exclude_user)
2388 regs = task_pt_regs(current);
2389
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002390 if (regs) {
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002391 if (perf_counter_overflow(counter, 0, regs, 0))
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002392 ret = HRTIMER_NORESTART;
2393 }
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002394
2395 hrtimer_forward_now(hrtimer, ns_to_ktime(counter->hw.irq_period));
2396
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002397 return ret;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002398}
2399
2400static void perf_swcounter_overflow(struct perf_counter *counter,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002401 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002402{
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002403 perf_swcounter_update(counter);
2404 perf_swcounter_set_period(counter);
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002405 if (perf_counter_overflow(counter, nmi, regs, addr))
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002406 /* soft-disable the counter */
2407 ;
2408
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002409}
2410
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002411static int perf_swcounter_match(struct perf_counter *counter,
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002412 enum perf_event_types type,
2413 u32 event, struct pt_regs *regs)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002414{
2415 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
2416 return 0;
2417
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002418 if (perf_event_raw(&counter->hw_event))
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002419 return 0;
2420
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002421 if (perf_event_type(&counter->hw_event) != type)
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002422 return 0;
2423
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002424 if (perf_event_id(&counter->hw_event) != event)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002425 return 0;
2426
2427 if (counter->hw_event.exclude_user && user_mode(regs))
2428 return 0;
2429
2430 if (counter->hw_event.exclude_kernel && !user_mode(regs))
2431 return 0;
2432
2433 return 1;
2434}
2435
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002436static void perf_swcounter_add(struct perf_counter *counter, u64 nr,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002437 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002438{
2439 int neg = atomic64_add_negative(nr, &counter->hw.count);
2440 if (counter->hw.irq_period && !neg)
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002441 perf_swcounter_overflow(counter, nmi, regs, addr);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002442}
2443
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002444static void perf_swcounter_ctx_event(struct perf_counter_context *ctx,
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002445 enum perf_event_types type, u32 event,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002446 u64 nr, int nmi, struct pt_regs *regs,
2447 u64 addr)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002448{
2449 struct perf_counter *counter;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002450
Peter Zijlstra01ef09d2009-03-19 20:26:11 +01002451 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002452 return;
2453
Peter Zijlstra592903c2009-03-13 12:21:36 +01002454 rcu_read_lock();
2455 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002456 if (perf_swcounter_match(counter, type, event, regs))
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002457 perf_swcounter_add(counter, nr, nmi, regs, addr);
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002458 }
Peter Zijlstra592903c2009-03-13 12:21:36 +01002459 rcu_read_unlock();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002460}
2461
Peter Zijlstra96f6d442009-03-23 18:22:07 +01002462static int *perf_swcounter_recursion_context(struct perf_cpu_context *cpuctx)
2463{
2464 if (in_nmi())
2465 return &cpuctx->recursion[3];
2466
2467 if (in_irq())
2468 return &cpuctx->recursion[2];
2469
2470 if (in_softirq())
2471 return &cpuctx->recursion[1];
2472
2473 return &cpuctx->recursion[0];
2474}
2475
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002476static void __perf_swcounter_event(enum perf_event_types type, u32 event,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002477 u64 nr, int nmi, struct pt_regs *regs,
2478 u64 addr)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002479{
2480 struct perf_cpu_context *cpuctx = &get_cpu_var(perf_cpu_context);
Peter Zijlstra96f6d442009-03-23 18:22:07 +01002481 int *recursion = perf_swcounter_recursion_context(cpuctx);
2482
2483 if (*recursion)
2484 goto out;
2485
2486 (*recursion)++;
2487 barrier();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002488
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002489 perf_swcounter_ctx_event(&cpuctx->ctx, type, event,
2490 nr, nmi, regs, addr);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002491 if (cpuctx->task_ctx) {
2492 perf_swcounter_ctx_event(cpuctx->task_ctx, type, event,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002493 nr, nmi, regs, addr);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002494 }
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002495
Peter Zijlstra96f6d442009-03-23 18:22:07 +01002496 barrier();
2497 (*recursion)--;
2498
2499out:
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002500 put_cpu_var(perf_cpu_context);
2501}
2502
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002503void
2504perf_swcounter_event(u32 event, u64 nr, int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002505{
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002506 __perf_swcounter_event(PERF_TYPE_SOFTWARE, event, nr, nmi, regs, addr);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002507}
2508
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002509static void perf_swcounter_read(struct perf_counter *counter)
2510{
2511 perf_swcounter_update(counter);
2512}
2513
2514static int perf_swcounter_enable(struct perf_counter *counter)
2515{
2516 perf_swcounter_set_period(counter);
2517 return 0;
2518}
2519
2520static void perf_swcounter_disable(struct perf_counter *counter)
2521{
2522 perf_swcounter_update(counter);
2523}
2524
Robert Richter4aeb0b42009-04-29 12:47:03 +02002525static const struct pmu perf_ops_generic = {
Peter Zijlstraac17dc82009-03-13 12:21:34 +01002526 .enable = perf_swcounter_enable,
2527 .disable = perf_swcounter_disable,
2528 .read = perf_swcounter_read,
2529};
2530
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002531/*
2532 * Software counter: cpu wall time clock
2533 */
2534
Paul Mackerras9abf8a02009-01-09 16:26:43 +11002535static void cpu_clock_perf_counter_update(struct perf_counter *counter)
2536{
2537 int cpu = raw_smp_processor_id();
2538 s64 prev;
2539 u64 now;
2540
2541 now = cpu_clock(cpu);
2542 prev = atomic64_read(&counter->hw.prev_count);
2543 atomic64_set(&counter->hw.prev_count, now);
2544 atomic64_add(now - prev, &counter->count);
2545}
2546
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002547static int cpu_clock_perf_counter_enable(struct perf_counter *counter)
2548{
2549 struct hw_perf_counter *hwc = &counter->hw;
2550 int cpu = raw_smp_processor_id();
2551
2552 atomic64_set(&hwc->prev_count, cpu_clock(cpu));
Peter Zijlstra039fc912009-03-13 16:43:47 +01002553 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2554 hwc->hrtimer.function = perf_swcounter_hrtimer;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002555 if (hwc->irq_period) {
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002556 __hrtimer_start_range_ns(&hwc->hrtimer,
2557 ns_to_ktime(hwc->irq_period), 0,
2558 HRTIMER_MODE_REL, 0);
2559 }
2560
2561 return 0;
2562}
2563
Ingo Molnar5c92d122008-12-11 13:21:10 +01002564static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
2565{
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002566 hrtimer_cancel(&counter->hw.hrtimer);
Paul Mackerras9abf8a02009-01-09 16:26:43 +11002567 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01002568}
2569
2570static void cpu_clock_perf_counter_read(struct perf_counter *counter)
2571{
Paul Mackerras9abf8a02009-01-09 16:26:43 +11002572 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01002573}
2574
Robert Richter4aeb0b42009-04-29 12:47:03 +02002575static const struct pmu perf_ops_cpu_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +01002576 .enable = cpu_clock_perf_counter_enable,
2577 .disable = cpu_clock_perf_counter_disable,
2578 .read = cpu_clock_perf_counter_read,
Ingo Molnar5c92d122008-12-11 13:21:10 +01002579};
2580
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01002581/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002582 * Software counter: task time clock
2583 */
2584
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02002585static void task_clock_perf_counter_update(struct perf_counter *counter, u64 now)
Ingo Molnarbae43c92008-12-11 14:03:20 +01002586{
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02002587 u64 prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002588 s64 delta;
Ingo Molnarbae43c92008-12-11 14:03:20 +01002589
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02002590 prev = atomic64_xchg(&counter->hw.prev_count, now);
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002591 delta = now - prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002592 atomic64_add(delta, &counter->count);
Ingo Molnarbae43c92008-12-11 14:03:20 +01002593}
2594
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01002595static int task_clock_perf_counter_enable(struct perf_counter *counter)
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002596{
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002597 struct hw_perf_counter *hwc = &counter->hw;
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02002598 u64 now;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002599
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02002600 now = counter->ctx->time;
2601
2602 atomic64_set(&hwc->prev_count, now);
Peter Zijlstra039fc912009-03-13 16:43:47 +01002603 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2604 hwc->hrtimer.function = perf_swcounter_hrtimer;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002605 if (hwc->irq_period) {
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002606 __hrtimer_start_range_ns(&hwc->hrtimer,
2607 ns_to_ktime(hwc->irq_period), 0,
2608 HRTIMER_MODE_REL, 0);
2609 }
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01002610
2611 return 0;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002612}
2613
2614static void task_clock_perf_counter_disable(struct perf_counter *counter)
2615{
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002616 hrtimer_cancel(&counter->hw.hrtimer);
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02002617 task_clock_perf_counter_update(counter, counter->ctx->time);
2618
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002619}
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01002620
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002621static void task_clock_perf_counter_read(struct perf_counter *counter)
2622{
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02002623 u64 time;
2624
2625 if (!in_nmi()) {
2626 update_context_time(counter->ctx);
2627 time = counter->ctx->time;
2628 } else {
2629 u64 now = perf_clock();
2630 u64 delta = now - counter->ctx->timestamp;
2631 time = counter->ctx->time + delta;
2632 }
2633
2634 task_clock_perf_counter_update(counter, time);
Ingo Molnarbae43c92008-12-11 14:03:20 +01002635}
2636
Robert Richter4aeb0b42009-04-29 12:47:03 +02002637static const struct pmu perf_ops_task_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +01002638 .enable = task_clock_perf_counter_enable,
2639 .disable = task_clock_perf_counter_disable,
2640 .read = task_clock_perf_counter_read,
Ingo Molnarbae43c92008-12-11 14:03:20 +01002641};
2642
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002643/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002644 * Software counter: cpu migrations
2645 */
2646
Paul Mackerras23a185c2009-02-09 22:42:47 +11002647static inline u64 get_cpu_migrations(struct perf_counter *counter)
Ingo Molnar6c594c22008-12-14 12:34:15 +01002648{
Paul Mackerras23a185c2009-02-09 22:42:47 +11002649 struct task_struct *curr = counter->ctx->task;
2650
2651 if (curr)
2652 return curr->se.nr_migrations;
2653 return cpu_nr_migrations(smp_processor_id());
Ingo Molnar6c594c22008-12-14 12:34:15 +01002654}
2655
2656static void cpu_migrations_perf_counter_update(struct perf_counter *counter)
2657{
2658 u64 prev, now;
2659 s64 delta;
2660
2661 prev = atomic64_read(&counter->hw.prev_count);
Paul Mackerras23a185c2009-02-09 22:42:47 +11002662 now = get_cpu_migrations(counter);
Ingo Molnar6c594c22008-12-14 12:34:15 +01002663
2664 atomic64_set(&counter->hw.prev_count, now);
2665
2666 delta = now - prev;
Ingo Molnar6c594c22008-12-14 12:34:15 +01002667
2668 atomic64_add(delta, &counter->count);
2669}
2670
2671static void cpu_migrations_perf_counter_read(struct perf_counter *counter)
2672{
2673 cpu_migrations_perf_counter_update(counter);
2674}
2675
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01002676static int cpu_migrations_perf_counter_enable(struct perf_counter *counter)
Ingo Molnar6c594c22008-12-14 12:34:15 +01002677{
Paul Mackerrasc07c99b2009-02-13 22:10:34 +11002678 if (counter->prev_state <= PERF_COUNTER_STATE_OFF)
2679 atomic64_set(&counter->hw.prev_count,
2680 get_cpu_migrations(counter));
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01002681 return 0;
Ingo Molnar6c594c22008-12-14 12:34:15 +01002682}
2683
2684static void cpu_migrations_perf_counter_disable(struct perf_counter *counter)
2685{
2686 cpu_migrations_perf_counter_update(counter);
2687}
2688
Robert Richter4aeb0b42009-04-29 12:47:03 +02002689static const struct pmu perf_ops_cpu_migrations = {
Ingo Molnar76715812008-12-17 14:20:28 +01002690 .enable = cpu_migrations_perf_counter_enable,
2691 .disable = cpu_migrations_perf_counter_disable,
2692 .read = cpu_migrations_perf_counter_read,
Ingo Molnar6c594c22008-12-14 12:34:15 +01002693};
2694
Peter Zijlstrae077df42009-03-19 20:26:17 +01002695#ifdef CONFIG_EVENT_PROFILE
2696void perf_tpcounter_event(int event_id)
2697{
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002698 struct pt_regs *regs = get_irq_regs();
2699
2700 if (!regs)
2701 regs = task_pt_regs(current);
2702
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002703 __perf_swcounter_event(PERF_TYPE_TRACEPOINT, event_id, 1, 1, regs, 0);
Peter Zijlstrae077df42009-03-19 20:26:17 +01002704}
Steven Whitehouseff7b1b42009-04-15 16:55:05 +01002705EXPORT_SYMBOL_GPL(perf_tpcounter_event);
Peter Zijlstrae077df42009-03-19 20:26:17 +01002706
2707extern int ftrace_profile_enable(int);
2708extern void ftrace_profile_disable(int);
2709
2710static void tp_perf_counter_destroy(struct perf_counter *counter)
2711{
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002712 ftrace_profile_disable(perf_event_id(&counter->hw_event));
Peter Zijlstrae077df42009-03-19 20:26:17 +01002713}
2714
Robert Richter4aeb0b42009-04-29 12:47:03 +02002715static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
Peter Zijlstrae077df42009-03-19 20:26:17 +01002716{
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002717 int event_id = perf_event_id(&counter->hw_event);
Peter Zijlstrae077df42009-03-19 20:26:17 +01002718 int ret;
2719
2720 ret = ftrace_profile_enable(event_id);
2721 if (ret)
2722 return NULL;
2723
2724 counter->destroy = tp_perf_counter_destroy;
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002725 counter->hw.irq_period = counter->hw_event.irq_period;
Peter Zijlstrae077df42009-03-19 20:26:17 +01002726
2727 return &perf_ops_generic;
2728}
2729#else
Robert Richter4aeb0b42009-04-29 12:47:03 +02002730static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
Peter Zijlstrae077df42009-03-19 20:26:17 +01002731{
2732 return NULL;
2733}
2734#endif
2735
Robert Richter4aeb0b42009-04-29 12:47:03 +02002736static const struct pmu *sw_perf_counter_init(struct perf_counter *counter)
Ingo Molnar5c92d122008-12-11 13:21:10 +01002737{
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002738 struct perf_counter_hw_event *hw_event = &counter->hw_event;
Robert Richter4aeb0b42009-04-29 12:47:03 +02002739 const struct pmu *pmu = NULL;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002740 struct hw_perf_counter *hwc = &counter->hw;
Ingo Molnar5c92d122008-12-11 13:21:10 +01002741
Paul Mackerras0475f9e2009-02-11 14:35:35 +11002742 /*
2743 * Software counters (currently) can't in general distinguish
2744 * between user, kernel and hypervisor events.
2745 * However, context switches and cpu migrations are considered
2746 * to be kernel events, and page faults are never hypervisor
2747 * events.
2748 */
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002749 switch (perf_event_id(&counter->hw_event)) {
Ingo Molnar5c92d122008-12-11 13:21:10 +01002750 case PERF_COUNT_CPU_CLOCK:
Robert Richter4aeb0b42009-04-29 12:47:03 +02002751 pmu = &perf_ops_cpu_clock;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002752
2753 if (hw_event->irq_period && hw_event->irq_period < 10000)
2754 hw_event->irq_period = 10000;
Ingo Molnar5c92d122008-12-11 13:21:10 +01002755 break;
Ingo Molnarbae43c92008-12-11 14:03:20 +01002756 case PERF_COUNT_TASK_CLOCK:
Paul Mackerras23a185c2009-02-09 22:42:47 +11002757 /*
2758 * If the user instantiates this as a per-cpu counter,
2759 * use the cpu_clock counter instead.
2760 */
2761 if (counter->ctx->task)
Robert Richter4aeb0b42009-04-29 12:47:03 +02002762 pmu = &perf_ops_task_clock;
Paul Mackerras23a185c2009-02-09 22:42:47 +11002763 else
Robert Richter4aeb0b42009-04-29 12:47:03 +02002764 pmu = &perf_ops_cpu_clock;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002765
2766 if (hw_event->irq_period && hw_event->irq_period < 10000)
2767 hw_event->irq_period = 10000;
Ingo Molnarbae43c92008-12-11 14:03:20 +01002768 break;
Ingo Molnare06c61a2008-12-14 14:44:31 +01002769 case PERF_COUNT_PAGE_FAULTS:
Peter Zijlstraac17dc82009-03-13 12:21:34 +01002770 case PERF_COUNT_PAGE_FAULTS_MIN:
2771 case PERF_COUNT_PAGE_FAULTS_MAJ:
Ingo Molnar5d6a27d2008-12-14 12:28:33 +01002772 case PERF_COUNT_CONTEXT_SWITCHES:
Robert Richter4aeb0b42009-04-29 12:47:03 +02002773 pmu = &perf_ops_generic;
Ingo Molnar5d6a27d2008-12-14 12:28:33 +01002774 break;
Ingo Molnar6c594c22008-12-14 12:34:15 +01002775 case PERF_COUNT_CPU_MIGRATIONS:
Paul Mackerras0475f9e2009-02-11 14:35:35 +11002776 if (!counter->hw_event.exclude_kernel)
Robert Richter4aeb0b42009-04-29 12:47:03 +02002777 pmu = &perf_ops_cpu_migrations;
Ingo Molnar6c594c22008-12-14 12:34:15 +01002778 break;
Ingo Molnar5c92d122008-12-11 13:21:10 +01002779 }
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002780
Robert Richter4aeb0b42009-04-29 12:47:03 +02002781 if (pmu)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002782 hwc->irq_period = hw_event->irq_period;
2783
Robert Richter4aeb0b42009-04-29 12:47:03 +02002784 return pmu;
Ingo Molnar5c92d122008-12-11 13:21:10 +01002785}
2786
Thomas Gleixner0793a612008-12-04 20:12:29 +01002787/*
2788 * Allocate and initialize a counter structure
2789 */
2790static struct perf_counter *
Ingo Molnar04289bb2008-12-11 08:38:42 +01002791perf_counter_alloc(struct perf_counter_hw_event *hw_event,
2792 int cpu,
Paul Mackerras23a185c2009-02-09 22:42:47 +11002793 struct perf_counter_context *ctx,
Ingo Molnar9b51f662008-12-12 13:49:45 +01002794 struct perf_counter *group_leader,
2795 gfp_t gfpflags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01002796{
Robert Richter4aeb0b42009-04-29 12:47:03 +02002797 const struct pmu *pmu;
Ingo Molnar621a01e2008-12-11 12:46:46 +01002798 struct perf_counter *counter;
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02002799 long err;
Thomas Gleixner0793a612008-12-04 20:12:29 +01002800
Ingo Molnar9b51f662008-12-12 13:49:45 +01002801 counter = kzalloc(sizeof(*counter), gfpflags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002802 if (!counter)
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02002803 return ERR_PTR(-ENOMEM);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002804
Ingo Molnar04289bb2008-12-11 08:38:42 +01002805 /*
2806 * Single counters are their own group leaders, with an
2807 * empty sibling list:
2808 */
2809 if (!group_leader)
2810 group_leader = counter;
2811
Thomas Gleixner0793a612008-12-04 20:12:29 +01002812 mutex_init(&counter->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01002813 INIT_LIST_HEAD(&counter->list_entry);
Peter Zijlstra592903c2009-03-13 12:21:36 +01002814 INIT_LIST_HEAD(&counter->event_entry);
Ingo Molnar04289bb2008-12-11 08:38:42 +01002815 INIT_LIST_HEAD(&counter->sibling_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002816 init_waitqueue_head(&counter->waitq);
2817
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002818 mutex_init(&counter->mmap_mutex);
2819
Paul Mackerrasd859e292009-01-17 18:10:22 +11002820 INIT_LIST_HEAD(&counter->child_list);
2821
Ingo Molnar9f66a382008-12-10 12:33:23 +01002822 counter->cpu = cpu;
2823 counter->hw_event = *hw_event;
Ingo Molnar04289bb2008-12-11 08:38:42 +01002824 counter->group_leader = group_leader;
Robert Richter4aeb0b42009-04-29 12:47:03 +02002825 counter->pmu = NULL;
Paul Mackerras23a185c2009-02-09 22:42:47 +11002826 counter->ctx = ctx;
Ingo Molnar621a01e2008-12-11 12:46:46 +01002827
Ingo Molnar235c7fc2008-12-21 14:43:25 +01002828 counter->state = PERF_COUNTER_STATE_INACTIVE;
Ingo Molnara86ed502008-12-17 00:43:10 +01002829 if (hw_event->disabled)
2830 counter->state = PERF_COUNTER_STATE_OFF;
2831
Robert Richter4aeb0b42009-04-29 12:47:03 +02002832 pmu = NULL;
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002833
Peter Zijlstra2023b352009-05-05 17:50:26 +02002834 /*
2835 * we currently do not support PERF_RECORD_GROUP on inherited counters
2836 */
2837 if (hw_event->inherit && (hw_event->record_type & PERF_RECORD_GROUP))
2838 goto done;
2839
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002840 if (perf_event_raw(hw_event)) {
Robert Richter4aeb0b42009-04-29 12:47:03 +02002841 pmu = hw_perf_counter_init(counter);
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002842 goto done;
2843 }
2844
2845 switch (perf_event_type(hw_event)) {
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002846 case PERF_TYPE_HARDWARE:
Robert Richter4aeb0b42009-04-29 12:47:03 +02002847 pmu = hw_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002848 break;
2849
2850 case PERF_TYPE_SOFTWARE:
Robert Richter4aeb0b42009-04-29 12:47:03 +02002851 pmu = sw_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002852 break;
2853
2854 case PERF_TYPE_TRACEPOINT:
Robert Richter4aeb0b42009-04-29 12:47:03 +02002855 pmu = tp_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002856 break;
2857 }
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002858done:
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02002859 err = 0;
Robert Richter4aeb0b42009-04-29 12:47:03 +02002860 if (!pmu)
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02002861 err = -EINVAL;
Robert Richter4aeb0b42009-04-29 12:47:03 +02002862 else if (IS_ERR(pmu))
2863 err = PTR_ERR(pmu);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02002864
2865 if (err) {
2866 kfree(counter);
2867 return ERR_PTR(err);
2868 }
2869
Robert Richter4aeb0b42009-04-29 12:47:03 +02002870 counter->pmu = pmu;
Thomas Gleixner0793a612008-12-04 20:12:29 +01002871
Peter Zijlstra7fc23a52009-05-08 18:52:21 +02002872 atomic_inc(&nr_counters);
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002873 if (counter->hw_event.mmap)
2874 atomic_inc(&nr_mmap_tracking);
2875 if (counter->hw_event.munmap)
2876 atomic_inc(&nr_munmap_tracking);
2877 if (counter->hw_event.comm)
2878 atomic_inc(&nr_comm_tracking);
2879
Thomas Gleixner0793a612008-12-04 20:12:29 +01002880 return counter;
2881}
2882
2883/**
Paul Mackerras2743a5b2009-03-04 20:36:51 +11002884 * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
Ingo Molnar9f66a382008-12-10 12:33:23 +01002885 *
2886 * @hw_event_uptr: event type attributes for monitoring/sampling
Thomas Gleixner0793a612008-12-04 20:12:29 +01002887 * @pid: target pid
Ingo Molnar9f66a382008-12-10 12:33:23 +01002888 * @cpu: target cpu
2889 * @group_fd: group leader counter fd
Thomas Gleixner0793a612008-12-04 20:12:29 +01002890 */
Paul Mackerras2743a5b2009-03-04 20:36:51 +11002891SYSCALL_DEFINE5(perf_counter_open,
Paul Mackerrasf3dfd262009-02-26 22:43:46 +11002892 const struct perf_counter_hw_event __user *, hw_event_uptr,
Paul Mackerras2743a5b2009-03-04 20:36:51 +11002893 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01002894{
Ingo Molnar04289bb2008-12-11 08:38:42 +01002895 struct perf_counter *counter, *group_leader;
Ingo Molnar9f66a382008-12-10 12:33:23 +01002896 struct perf_counter_hw_event hw_event;
Ingo Molnar04289bb2008-12-11 08:38:42 +01002897 struct perf_counter_context *ctx;
Ingo Molnar9b51f662008-12-12 13:49:45 +01002898 struct file *counter_file = NULL;
Ingo Molnar04289bb2008-12-11 08:38:42 +01002899 struct file *group_file = NULL;
2900 int fput_needed = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01002901 int fput_needed2 = 0;
Thomas Gleixner0793a612008-12-04 20:12:29 +01002902 int ret;
2903
Paul Mackerras2743a5b2009-03-04 20:36:51 +11002904 /* for future expandability... */
2905 if (flags)
2906 return -EINVAL;
2907
Ingo Molnar9f66a382008-12-10 12:33:23 +01002908 if (copy_from_user(&hw_event, hw_event_uptr, sizeof(hw_event)) != 0)
Thomas Gleixnereab656a2008-12-08 19:26:59 +01002909 return -EFAULT;
2910
Ingo Molnar04289bb2008-12-11 08:38:42 +01002911 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01002912 * Get the target context (task or percpu):
2913 */
2914 ctx = find_get_context(pid, cpu);
2915 if (IS_ERR(ctx))
2916 return PTR_ERR(ctx);
2917
2918 /*
2919 * Look up the group leader (we will attach this counter to it):
Ingo Molnar04289bb2008-12-11 08:38:42 +01002920 */
2921 group_leader = NULL;
2922 if (group_fd != -1) {
2923 ret = -EINVAL;
2924 group_file = fget_light(group_fd, &fput_needed);
2925 if (!group_file)
Ingo Molnarccff2862008-12-11 11:26:29 +01002926 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01002927 if (group_file->f_op != &perf_fops)
Ingo Molnarccff2862008-12-11 11:26:29 +01002928 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01002929
2930 group_leader = group_file->private_data;
2931 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01002932 * Do not allow a recursive hierarchy (this new sibling
2933 * becoming part of another group-sibling):
Ingo Molnar04289bb2008-12-11 08:38:42 +01002934 */
Ingo Molnarccff2862008-12-11 11:26:29 +01002935 if (group_leader->group_leader != group_leader)
2936 goto err_put_context;
2937 /*
2938 * Do not allow to attach to a group in a different
2939 * task or CPU context:
2940 */
2941 if (group_leader->ctx != ctx)
2942 goto err_put_context;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11002943 /*
2944 * Only a group leader can be exclusive or pinned
2945 */
2946 if (hw_event.exclusive || hw_event.pinned)
2947 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01002948 }
2949
Paul Mackerras23a185c2009-02-09 22:42:47 +11002950 counter = perf_counter_alloc(&hw_event, cpu, ctx, group_leader,
2951 GFP_KERNEL);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02002952 ret = PTR_ERR(counter);
2953 if (IS_ERR(counter))
Thomas Gleixner0793a612008-12-04 20:12:29 +01002954 goto err_put_context;
2955
Thomas Gleixner0793a612008-12-04 20:12:29 +01002956 ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
2957 if (ret < 0)
Ingo Molnar9b51f662008-12-12 13:49:45 +01002958 goto err_free_put_context;
2959
2960 counter_file = fget_light(ret, &fput_needed2);
2961 if (!counter_file)
2962 goto err_free_put_context;
2963
2964 counter->filp = counter_file;
Paul Mackerrasd859e292009-01-17 18:10:22 +11002965 mutex_lock(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01002966 perf_install_in_context(ctx, counter, cpu);
Paul Mackerrasd859e292009-01-17 18:10:22 +11002967 mutex_unlock(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01002968
2969 fput_light(counter_file, fput_needed2);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002970
Ingo Molnar04289bb2008-12-11 08:38:42 +01002971out_fput:
2972 fput_light(group_file, fput_needed);
2973
Thomas Gleixner0793a612008-12-04 20:12:29 +01002974 return ret;
2975
Ingo Molnar9b51f662008-12-12 13:49:45 +01002976err_free_put_context:
Thomas Gleixner0793a612008-12-04 20:12:29 +01002977 kfree(counter);
2978
2979err_put_context:
2980 put_context(ctx);
2981
Ingo Molnar04289bb2008-12-11 08:38:42 +01002982 goto out_fput;
Thomas Gleixner0793a612008-12-04 20:12:29 +01002983}
2984
Ingo Molnar9b51f662008-12-12 13:49:45 +01002985/*
2986 * Initialize the perf_counter context in a task_struct:
2987 */
2988static void
2989__perf_counter_init_context(struct perf_counter_context *ctx,
2990 struct task_struct *task)
2991{
2992 memset(ctx, 0, sizeof(*ctx));
2993 spin_lock_init(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +11002994 mutex_init(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01002995 INIT_LIST_HEAD(&ctx->counter_list);
Peter Zijlstra592903c2009-03-13 12:21:36 +01002996 INIT_LIST_HEAD(&ctx->event_list);
Ingo Molnar9b51f662008-12-12 13:49:45 +01002997 ctx->task = task;
2998}
2999
3000/*
3001 * inherit a counter from parent task to child task:
3002 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11003003static struct perf_counter *
Ingo Molnar9b51f662008-12-12 13:49:45 +01003004inherit_counter(struct perf_counter *parent_counter,
3005 struct task_struct *parent,
3006 struct perf_counter_context *parent_ctx,
3007 struct task_struct *child,
Paul Mackerrasd859e292009-01-17 18:10:22 +11003008 struct perf_counter *group_leader,
Ingo Molnar9b51f662008-12-12 13:49:45 +01003009 struct perf_counter_context *child_ctx)
3010{
3011 struct perf_counter *child_counter;
3012
Paul Mackerrasd859e292009-01-17 18:10:22 +11003013 /*
3014 * Instead of creating recursive hierarchies of counters,
3015 * we link inherited counters back to the original parent,
3016 * which has a filp for sure, which we use as the reference
3017 * count:
3018 */
3019 if (parent_counter->parent)
3020 parent_counter = parent_counter->parent;
3021
Ingo Molnar9b51f662008-12-12 13:49:45 +01003022 child_counter = perf_counter_alloc(&parent_counter->hw_event,
Paul Mackerras23a185c2009-02-09 22:42:47 +11003023 parent_counter->cpu, child_ctx,
3024 group_leader, GFP_KERNEL);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003025 if (IS_ERR(child_counter))
3026 return child_counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003027
3028 /*
3029 * Link it up in the child's context:
3030 */
Ingo Molnar9b51f662008-12-12 13:49:45 +01003031 child_counter->task = child;
Paul Mackerras53cfbf52009-03-25 22:46:58 +11003032 add_counter_to_ctx(child_counter, child_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003033
3034 child_counter->parent = parent_counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003035 /*
3036 * inherit into child's child as well:
3037 */
3038 child_counter->hw_event.inherit = 1;
3039
3040 /*
3041 * Get a reference to the parent filp - we will fput it
3042 * when the child counter exits. This is safe to do because
3043 * we are in the parent and we know that the filp still
3044 * exists and has a nonzero count:
3045 */
3046 atomic_long_inc(&parent_counter->filp->f_count);
3047
Paul Mackerrasd859e292009-01-17 18:10:22 +11003048 /*
3049 * Link this into the parent counter's child list
3050 */
3051 mutex_lock(&parent_counter->mutex);
3052 list_add_tail(&child_counter->child_list, &parent_counter->child_list);
3053
3054 /*
3055 * Make the child state follow the state of the parent counter,
3056 * not its hw_event.disabled bit. We hold the parent's mutex,
3057 * so we won't race with perf_counter_{en,dis}able_family.
3058 */
3059 if (parent_counter->state >= PERF_COUNTER_STATE_INACTIVE)
3060 child_counter->state = PERF_COUNTER_STATE_INACTIVE;
3061 else
3062 child_counter->state = PERF_COUNTER_STATE_OFF;
3063
3064 mutex_unlock(&parent_counter->mutex);
3065
3066 return child_counter;
3067}
3068
3069static int inherit_group(struct perf_counter *parent_counter,
3070 struct task_struct *parent,
3071 struct perf_counter_context *parent_ctx,
3072 struct task_struct *child,
3073 struct perf_counter_context *child_ctx)
3074{
3075 struct perf_counter *leader;
3076 struct perf_counter *sub;
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003077 struct perf_counter *child_ctr;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003078
3079 leader = inherit_counter(parent_counter, parent, parent_ctx,
3080 child, NULL, child_ctx);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003081 if (IS_ERR(leader))
3082 return PTR_ERR(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003083 list_for_each_entry(sub, &parent_counter->sibling_list, list_entry) {
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003084 child_ctr = inherit_counter(sub, parent, parent_ctx,
3085 child, leader, child_ctx);
3086 if (IS_ERR(child_ctr))
3087 return PTR_ERR(child_ctr);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003088 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01003089 return 0;
3090}
3091
Paul Mackerrasd859e292009-01-17 18:10:22 +11003092static void sync_child_counter(struct perf_counter *child_counter,
3093 struct perf_counter *parent_counter)
3094{
3095 u64 parent_val, child_val;
3096
3097 parent_val = atomic64_read(&parent_counter->count);
3098 child_val = atomic64_read(&child_counter->count);
3099
3100 /*
3101 * Add back the child's count to the parent's count:
3102 */
3103 atomic64_add(child_val, &parent_counter->count);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11003104 atomic64_add(child_counter->total_time_enabled,
3105 &parent_counter->child_total_time_enabled);
3106 atomic64_add(child_counter->total_time_running,
3107 &parent_counter->child_total_time_running);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003108
3109 /*
3110 * Remove this counter from the parent's list
3111 */
3112 mutex_lock(&parent_counter->mutex);
3113 list_del_init(&child_counter->child_list);
3114 mutex_unlock(&parent_counter->mutex);
3115
3116 /*
3117 * Release the parent counter, if this was the last
3118 * reference to it.
3119 */
3120 fput(parent_counter->filp);
3121}
3122
Ingo Molnar9b51f662008-12-12 13:49:45 +01003123static void
3124__perf_counter_exit_task(struct task_struct *child,
3125 struct perf_counter *child_counter,
3126 struct perf_counter_context *child_ctx)
3127{
3128 struct perf_counter *parent_counter;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003129 struct perf_counter *sub, *tmp;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003130
3131 /*
Ingo Molnar235c7fc2008-12-21 14:43:25 +01003132 * If we do not self-reap then we have to wait for the
3133 * child task to unschedule (it will happen for sure),
3134 * so that its counter is at its final count. (This
3135 * condition triggers rarely - child tasks usually get
3136 * off their CPU before the parent has a chance to
3137 * get this far into the reaping action)
Ingo Molnar9b51f662008-12-12 13:49:45 +01003138 */
Ingo Molnar235c7fc2008-12-21 14:43:25 +01003139 if (child != current) {
3140 wait_task_inactive(child, 0);
3141 list_del_init(&child_counter->list_entry);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11003142 update_counter_times(child_counter);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01003143 } else {
Ingo Molnar0cc0c022008-12-14 23:20:36 +01003144 struct perf_cpu_context *cpuctx;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01003145 unsigned long flags;
3146 u64 perf_flags;
3147
3148 /*
3149 * Disable and unlink this counter.
3150 *
3151 * Be careful about zapping the list - IRQ/NMI context
3152 * could still be processing it:
3153 */
Peter Zijlstra849691a2009-04-06 11:45:12 +02003154 local_irq_save(flags);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01003155 perf_flags = hw_perf_save_disable();
Ingo Molnar0cc0c022008-12-14 23:20:36 +01003156
3157 cpuctx = &__get_cpu_var(perf_cpu_context);
3158
Paul Mackerrasd859e292009-01-17 18:10:22 +11003159 group_sched_out(child_counter, cpuctx, child_ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11003160 update_counter_times(child_counter);
Ingo Molnar0cc0c022008-12-14 23:20:36 +01003161
Ingo Molnar235c7fc2008-12-21 14:43:25 +01003162 list_del_init(&child_counter->list_entry);
3163
3164 child_ctx->nr_counters--;
3165
3166 hw_perf_restore(perf_flags);
Peter Zijlstra849691a2009-04-06 11:45:12 +02003167 local_irq_restore(flags);
Ingo Molnar0cc0c022008-12-14 23:20:36 +01003168 }
3169
Ingo Molnar9b51f662008-12-12 13:49:45 +01003170 parent_counter = child_counter->parent;
3171 /*
3172 * It can happen that parent exits first, and has counters
3173 * that are still around due to the child reference. These
3174 * counters need to be zapped - but otherwise linger.
3175 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11003176 if (parent_counter) {
3177 sync_child_counter(child_counter, parent_counter);
3178 list_for_each_entry_safe(sub, tmp, &child_counter->sibling_list,
3179 list_entry) {
Paul Mackerras4bcf3492009-02-11 13:53:19 +01003180 if (sub->parent) {
Paul Mackerrasd859e292009-01-17 18:10:22 +11003181 sync_child_counter(sub, sub->parent);
Peter Zijlstraf1600952009-03-19 20:26:16 +01003182 free_counter(sub);
Paul Mackerras4bcf3492009-02-11 13:53:19 +01003183 }
Paul Mackerrasd859e292009-01-17 18:10:22 +11003184 }
Peter Zijlstraf1600952009-03-19 20:26:16 +01003185 free_counter(child_counter);
Paul Mackerras4bcf3492009-02-11 13:53:19 +01003186 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01003187}
3188
3189/*
Paul Mackerrasd859e292009-01-17 18:10:22 +11003190 * When a child task exits, feed back counter values to parent counters.
Ingo Molnar9b51f662008-12-12 13:49:45 +01003191 *
Paul Mackerrasd859e292009-01-17 18:10:22 +11003192 * Note: we may be running in child context, but the PID is not hashed
Ingo Molnar9b51f662008-12-12 13:49:45 +01003193 * anymore so new counters will not be added.
3194 */
3195void perf_counter_exit_task(struct task_struct *child)
3196{
3197 struct perf_counter *child_counter, *tmp;
3198 struct perf_counter_context *child_ctx;
3199
3200 child_ctx = &child->perf_counter_ctx;
3201
3202 if (likely(!child_ctx->nr_counters))
3203 return;
3204
3205 list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list,
3206 list_entry)
3207 __perf_counter_exit_task(child, child_counter, child_ctx);
3208}
3209
3210/*
3211 * Initialize the perf_counter context in task_struct
3212 */
3213void perf_counter_init_task(struct task_struct *child)
3214{
3215 struct perf_counter_context *child_ctx, *parent_ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003216 struct perf_counter *counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003217 struct task_struct *parent = current;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003218
3219 child_ctx = &child->perf_counter_ctx;
3220 parent_ctx = &parent->perf_counter_ctx;
3221
3222 __perf_counter_init_context(child_ctx, child);
3223
3224 /*
3225 * This is executed from the parent task context, so inherit
3226 * counters that have been marked for cloning:
3227 */
3228
3229 if (likely(!parent_ctx->nr_counters))
3230 return;
3231
3232 /*
3233 * Lock the parent list. No need to lock the child - not PID
3234 * hashed yet and not running, so nobody can access it.
3235 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11003236 mutex_lock(&parent_ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003237
3238 /*
3239 * We dont have to disable NMIs - we are only looking at
3240 * the list, not manipulating it:
3241 */
3242 list_for_each_entry(counter, &parent_ctx->counter_list, list_entry) {
Paul Mackerrasd859e292009-01-17 18:10:22 +11003243 if (!counter->hw_event.inherit)
Ingo Molnar9b51f662008-12-12 13:49:45 +01003244 continue;
3245
Paul Mackerrasd859e292009-01-17 18:10:22 +11003246 if (inherit_group(counter, parent,
Ingo Molnar9b51f662008-12-12 13:49:45 +01003247 parent_ctx, child, child_ctx))
3248 break;
3249 }
3250
Paul Mackerrasd859e292009-01-17 18:10:22 +11003251 mutex_unlock(&parent_ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003252}
3253
Ingo Molnar04289bb2008-12-11 08:38:42 +01003254static void __cpuinit perf_counter_init_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003255{
Ingo Molnar04289bb2008-12-11 08:38:42 +01003256 struct perf_cpu_context *cpuctx;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003257
Ingo Molnar04289bb2008-12-11 08:38:42 +01003258 cpuctx = &per_cpu(perf_cpu_context, cpu);
3259 __perf_counter_init_context(&cpuctx->ctx, NULL);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003260
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003261 spin_lock(&perf_resource_lock);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003262 cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu;
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003263 spin_unlock(&perf_resource_lock);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003264
Paul Mackerras01d02872009-01-14 13:44:19 +11003265 hw_perf_counter_setup(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003266}
3267
3268#ifdef CONFIG_HOTPLUG_CPU
Ingo Molnar04289bb2008-12-11 08:38:42 +01003269static void __perf_counter_exit_cpu(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003270{
3271 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
3272 struct perf_counter_context *ctx = &cpuctx->ctx;
3273 struct perf_counter *counter, *tmp;
3274
Ingo Molnar04289bb2008-12-11 08:38:42 +01003275 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry)
3276 __perf_counter_remove_from_context(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003277}
Ingo Molnar04289bb2008-12-11 08:38:42 +01003278static void perf_counter_exit_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003279{
Paul Mackerrasd859e292009-01-17 18:10:22 +11003280 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
3281 struct perf_counter_context *ctx = &cpuctx->ctx;
3282
3283 mutex_lock(&ctx->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003284 smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003285 mutex_unlock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003286}
3287#else
Ingo Molnar04289bb2008-12-11 08:38:42 +01003288static inline void perf_counter_exit_cpu(int cpu) { }
Thomas Gleixner0793a612008-12-04 20:12:29 +01003289#endif
3290
3291static int __cpuinit
3292perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
3293{
3294 unsigned int cpu = (long)hcpu;
3295
3296 switch (action) {
3297
3298 case CPU_UP_PREPARE:
3299 case CPU_UP_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01003300 perf_counter_init_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003301 break;
3302
3303 case CPU_DOWN_PREPARE:
3304 case CPU_DOWN_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01003305 perf_counter_exit_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003306 break;
3307
3308 default:
3309 break;
3310 }
3311
3312 return NOTIFY_OK;
3313}
3314
3315static struct notifier_block __cpuinitdata perf_cpu_nb = {
3316 .notifier_call = perf_cpu_notify,
3317};
3318
Ingo Molnar0d905bc2009-05-04 19:13:30 +02003319void __init perf_counter_init(void)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003320{
3321 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
3322 (void *)(long)smp_processor_id());
3323 register_cpu_notifier(&perf_cpu_nb);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003324}
Thomas Gleixner0793a612008-12-04 20:12:29 +01003325
3326static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
3327{
3328 return sprintf(buf, "%d\n", perf_reserved_percpu);
3329}
3330
3331static ssize_t
3332perf_set_reserve_percpu(struct sysdev_class *class,
3333 const char *buf,
3334 size_t count)
3335{
3336 struct perf_cpu_context *cpuctx;
3337 unsigned long val;
3338 int err, cpu, mpt;
3339
3340 err = strict_strtoul(buf, 10, &val);
3341 if (err)
3342 return err;
3343 if (val > perf_max_counters)
3344 return -EINVAL;
3345
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003346 spin_lock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003347 perf_reserved_percpu = val;
3348 for_each_online_cpu(cpu) {
3349 cpuctx = &per_cpu(perf_cpu_context, cpu);
3350 spin_lock_irq(&cpuctx->ctx.lock);
3351 mpt = min(perf_max_counters - cpuctx->ctx.nr_counters,
3352 perf_max_counters - perf_reserved_percpu);
3353 cpuctx->max_pertask = mpt;
3354 spin_unlock_irq(&cpuctx->ctx.lock);
3355 }
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003356 spin_unlock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003357
3358 return count;
3359}
3360
3361static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
3362{
3363 return sprintf(buf, "%d\n", perf_overcommit);
3364}
3365
3366static ssize_t
3367perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
3368{
3369 unsigned long val;
3370 int err;
3371
3372 err = strict_strtoul(buf, 10, &val);
3373 if (err)
3374 return err;
3375 if (val > 1)
3376 return -EINVAL;
3377
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003378 spin_lock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003379 perf_overcommit = val;
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003380 spin_unlock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003381
3382 return count;
3383}
3384
3385static SYSDEV_CLASS_ATTR(
3386 reserve_percpu,
3387 0644,
3388 perf_show_reserve_percpu,
3389 perf_set_reserve_percpu
3390 );
3391
3392static SYSDEV_CLASS_ATTR(
3393 overcommit,
3394 0644,
3395 perf_show_overcommit,
3396 perf_set_overcommit
3397 );
3398
3399static struct attribute *perfclass_attrs[] = {
3400 &attr_reserve_percpu.attr,
3401 &attr_overcommit.attr,
3402 NULL
3403};
3404
3405static struct attribute_group perfclass_attr_group = {
3406 .attrs = perfclass_attrs,
3407 .name = "perf_counters",
3408};
3409
3410static int __init perf_counter_sysfs_init(void)
3411{
3412 return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
3413 &perfclass_attr_group);
3414}
3415device_initcall(perf_counter_sysfs_init);