blob: 08584c16049fc9cac1bb1902ac491f8ef38e5f4b [file] [log] [blame]
Thomas Gleixner0793a612008-12-04 20:12:29 +01001/*
2 * Performance counter core code
3 *
Ingo Molnar98144512009-04-29 14:52:50 +02004 * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
5 * Copyright (C) 2008-2009 Red Hat, Inc., Ingo Molnar
6 * Copyright (C) 2008-2009 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
Paul Mackerrasc5dd0162009-04-30 09:48:16 +10007 * Copyright © 2009 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
Peter Zijlstra7b732a72009-03-23 18:22:10 +01008 *
9 * For licensing details see kernel-base/COPYING
Thomas Gleixner0793a612008-12-04 20:12:29 +010010 */
11
12#include <linux/fs.h>
Peter Zijlstrab9cacc72009-03-25 12:30:22 +010013#include <linux/mm.h>
Thomas Gleixner0793a612008-12-04 20:12:29 +010014#include <linux/cpu.h>
15#include <linux/smp.h>
Ingo Molnar04289bb2008-12-11 08:38:42 +010016#include <linux/file.h>
Thomas Gleixner0793a612008-12-04 20:12:29 +010017#include <linux/poll.h>
18#include <linux/sysfs.h>
19#include <linux/ptrace.h>
20#include <linux/percpu.h>
Peter Zijlstrab9cacc72009-03-25 12:30:22 +010021#include <linux/vmstat.h>
22#include <linux/hardirq.h>
23#include <linux/rculist.h>
Thomas Gleixner0793a612008-12-04 20:12:29 +010024#include <linux/uaccess.h>
25#include <linux/syscalls.h>
26#include <linux/anon_inodes.h>
Ingo Molnaraa9c4c02008-12-17 14:10:57 +010027#include <linux/kernel_stat.h>
Thomas Gleixner0793a612008-12-04 20:12:29 +010028#include <linux/perf_counter.h>
Peter Zijlstra0a4a9392009-03-30 19:07:05 +020029#include <linux/dcache.h>
Thomas Gleixner0793a612008-12-04 20:12:29 +010030
Tim Blechmann4e193bd2009-03-14 14:29:25 +010031#include <asm/irq_regs.h>
32
Thomas Gleixner0793a612008-12-04 20:12:29 +010033/*
34 * Each CPU has a list of per CPU counters:
35 */
36DEFINE_PER_CPU(struct perf_cpu_context, perf_cpu_context);
37
Ingo Molnar088e2852008-12-14 20:21:00 +010038int perf_max_counters __read_mostly = 1;
Thomas Gleixner0793a612008-12-04 20:12:29 +010039static int perf_reserved_percpu __read_mostly;
40static int perf_overcommit __read_mostly = 1;
41
Peter Zijlstra7fc23a52009-05-08 18:52:21 +020042static atomic_t nr_counters __read_mostly;
Peter Zijlstra9ee318a2009-04-09 10:53:44 +020043static atomic_t nr_mmap_tracking __read_mostly;
44static atomic_t nr_munmap_tracking __read_mostly;
45static atomic_t nr_comm_tracking __read_mostly;
46
Peter Zijlstra1ccd1542009-04-09 10:53:45 +020047int sysctl_perf_counter_priv __read_mostly; /* do we need to be privileged */
Peter Zijlstra789f90f2009-05-15 15:19:27 +020048int sysctl_perf_counter_mlock __read_mostly = 512; /* 'free' kb per user */
Peter Zijlstra1ccd1542009-04-09 10:53:45 +020049
Thomas Gleixner0793a612008-12-04 20:12:29 +010050/*
Ingo Molnar1dce8d92009-05-04 19:23:18 +020051 * Lock for (sysadmin-configurable) counter reservations:
Thomas Gleixner0793a612008-12-04 20:12:29 +010052 */
Ingo Molnar1dce8d92009-05-04 19:23:18 +020053static DEFINE_SPINLOCK(perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +010054
55/*
56 * Architecture provided APIs - weak aliases:
57 */
Robert Richter4aeb0b42009-04-29 12:47:03 +020058extern __weak const struct pmu *hw_perf_counter_init(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +010059{
Paul Mackerrasff6f0542009-01-09 16:19:25 +110060 return NULL;
Thomas Gleixner0793a612008-12-04 20:12:29 +010061}
62
Peter Zijlstra9e35ad32009-05-13 16:21:38 +020063void __weak hw_perf_disable(void) { barrier(); }
64void __weak hw_perf_enable(void) { barrier(); }
65
Paul Mackerras01d02872009-01-14 13:44:19 +110066void __weak hw_perf_counter_setup(int cpu) { barrier(); }
Paul Mackerras3cbed422009-01-09 16:43:42 +110067int __weak hw_perf_group_sched_in(struct perf_counter *group_leader,
68 struct perf_cpu_context *cpuctx,
69 struct perf_counter_context *ctx, int cpu)
70{
71 return 0;
72}
Thomas Gleixner0793a612008-12-04 20:12:29 +010073
Paul Mackerras4eb96fc2009-01-09 17:24:34 +110074void __weak perf_counter_print_debug(void) { }
75
Peter Zijlstra9e35ad32009-05-13 16:21:38 +020076static DEFINE_PER_CPU(int, disable_count);
77
78void __perf_disable(void)
79{
80 __get_cpu_var(disable_count)++;
81}
82
83bool __perf_enable(void)
84{
85 return !--__get_cpu_var(disable_count);
86}
87
88void perf_disable(void)
89{
90 __perf_disable();
91 hw_perf_disable();
92}
Peter Zijlstra9e35ad32009-05-13 16:21:38 +020093
94void perf_enable(void)
95{
96 if (__perf_enable())
97 hw_perf_enable();
98}
Peter Zijlstra9e35ad32009-05-13 16:21:38 +020099
Ingo Molnar04289bb2008-12-11 08:38:42 +0100100static void
101list_add_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
102{
103 struct perf_counter *group_leader = counter->group_leader;
104
105 /*
106 * Depending on whether it is a standalone or sibling counter,
107 * add it straight to the context's counter list, or to the group
108 * leader's sibling list:
109 */
Peter Zijlstra3df5eda2009-05-08 18:52:22 +0200110 if (group_leader == counter)
Ingo Molnar04289bb2008-12-11 08:38:42 +0100111 list_add_tail(&counter->list_entry, &ctx->counter_list);
Peter Zijlstra5c148192009-03-25 12:30:23 +0100112 else {
Ingo Molnar04289bb2008-12-11 08:38:42 +0100113 list_add_tail(&counter->list_entry, &group_leader->sibling_list);
Peter Zijlstra5c148192009-03-25 12:30:23 +0100114 group_leader->nr_siblings++;
115 }
Peter Zijlstra592903c2009-03-13 12:21:36 +0100116
117 list_add_rcu(&counter->event_entry, &ctx->event_list);
Peter Zijlstra8bc20952009-05-15 20:45:59 +0200118 ctx->nr_counters++;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100119}
120
121static void
122list_del_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
123{
124 struct perf_counter *sibling, *tmp;
125
Peter Zijlstra8bc20952009-05-15 20:45:59 +0200126 ctx->nr_counters--;
127
Ingo Molnar04289bb2008-12-11 08:38:42 +0100128 list_del_init(&counter->list_entry);
Peter Zijlstra592903c2009-03-13 12:21:36 +0100129 list_del_rcu(&counter->event_entry);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100130
Peter Zijlstra5c148192009-03-25 12:30:23 +0100131 if (counter->group_leader != counter)
132 counter->group_leader->nr_siblings--;
133
Ingo Molnar04289bb2008-12-11 08:38:42 +0100134 /*
135 * If this was a group counter with sibling counters then
136 * upgrade the siblings to singleton counters by adding them
137 * to the context list directly:
138 */
139 list_for_each_entry_safe(sibling, tmp,
140 &counter->sibling_list, list_entry) {
141
Peter Zijlstra75564232009-03-13 12:21:29 +0100142 list_move_tail(&sibling->list_entry, &ctx->counter_list);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100143 sibling->group_leader = sibling;
144 }
145}
146
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100147static void
148counter_sched_out(struct perf_counter *counter,
149 struct perf_cpu_context *cpuctx,
150 struct perf_counter_context *ctx)
151{
152 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
153 return;
154
155 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200156 counter->tstamp_stopped = ctx->time;
Robert Richter4aeb0b42009-04-29 12:47:03 +0200157 counter->pmu->disable(counter);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100158 counter->oncpu = -1;
159
160 if (!is_software_counter(counter))
161 cpuctx->active_oncpu--;
162 ctx->nr_active--;
163 if (counter->hw_event.exclusive || !cpuctx->active_oncpu)
164 cpuctx->exclusive = 0;
165}
166
Paul Mackerrasd859e292009-01-17 18:10:22 +1100167static void
168group_sched_out(struct perf_counter *group_counter,
169 struct perf_cpu_context *cpuctx,
170 struct perf_counter_context *ctx)
171{
172 struct perf_counter *counter;
173
174 if (group_counter->state != PERF_COUNTER_STATE_ACTIVE)
175 return;
176
177 counter_sched_out(group_counter, cpuctx, ctx);
178
179 /*
180 * Schedule out siblings (if any):
181 */
182 list_for_each_entry(counter, &group_counter->sibling_list, list_entry)
183 counter_sched_out(counter, cpuctx, ctx);
184
185 if (group_counter->hw_event.exclusive)
186 cpuctx->exclusive = 0;
187}
188
Thomas Gleixner0793a612008-12-04 20:12:29 +0100189/*
190 * Cross CPU call to remove a performance counter
191 *
192 * We disable the counter on the hardware level first. After that we
193 * remove it from the context list.
194 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100195static void __perf_counter_remove_from_context(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100196{
197 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
198 struct perf_counter *counter = info;
199 struct perf_counter_context *ctx = counter->ctx;
Ingo Molnar9b51f662008-12-12 13:49:45 +0100200 unsigned long flags;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100201
202 /*
203 * If this is a task context, we need to check whether it is
204 * the current task context of this cpu. If not it has been
205 * scheduled out before the smp call arrived.
206 */
207 if (ctx->task && cpuctx->task_ctx != ctx)
208 return;
209
Peter Zijlstra849691a2009-04-06 11:45:12 +0200210 spin_lock_irqsave(&ctx->lock, flags);
Ingo Molnar34adc802009-05-20 20:13:28 +0200211 /*
212 * Protect the list operation against NMI by disabling the
213 * counters on a global level.
214 */
215 perf_disable();
Thomas Gleixner0793a612008-12-04 20:12:29 +0100216
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100217 counter_sched_out(counter, cpuctx, ctx);
218
219 counter->task = NULL;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100220
Ingo Molnar04289bb2008-12-11 08:38:42 +0100221 list_del_counter(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100222
223 if (!ctx->task) {
224 /*
225 * Allow more per task counters with respect to the
226 * reservation:
227 */
228 cpuctx->max_pertask =
229 min(perf_max_counters - ctx->nr_counters,
230 perf_max_counters - perf_reserved_percpu);
231 }
232
Ingo Molnar34adc802009-05-20 20:13:28 +0200233 perf_enable();
Peter Zijlstra849691a2009-04-06 11:45:12 +0200234 spin_unlock_irqrestore(&ctx->lock, flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100235}
236
237
238/*
239 * Remove the counter from a task's (or a CPU's) list of counters.
240 *
Paul Mackerrasd859e292009-01-17 18:10:22 +1100241 * Must be called with counter->mutex and ctx->mutex held.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100242 *
243 * CPU counters are removed with a smp call. For task counters we only
244 * call when the task is on a CPU.
245 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100246static void perf_counter_remove_from_context(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100247{
248 struct perf_counter_context *ctx = counter->ctx;
249 struct task_struct *task = ctx->task;
250
251 if (!task) {
252 /*
253 * Per cpu counters are removed via an smp call and
254 * the removal is always sucessful.
255 */
256 smp_call_function_single(counter->cpu,
Ingo Molnar04289bb2008-12-11 08:38:42 +0100257 __perf_counter_remove_from_context,
Thomas Gleixner0793a612008-12-04 20:12:29 +0100258 counter, 1);
259 return;
260 }
261
262retry:
Ingo Molnar04289bb2008-12-11 08:38:42 +0100263 task_oncpu_function_call(task, __perf_counter_remove_from_context,
Thomas Gleixner0793a612008-12-04 20:12:29 +0100264 counter);
265
266 spin_lock_irq(&ctx->lock);
267 /*
268 * If the context is active we need to retry the smp call.
269 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100270 if (ctx->nr_active && !list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100271 spin_unlock_irq(&ctx->lock);
272 goto retry;
273 }
274
275 /*
276 * The lock prevents that this context is scheduled in so we
Ingo Molnar04289bb2008-12-11 08:38:42 +0100277 * can remove the counter safely, if the call above did not
Thomas Gleixner0793a612008-12-04 20:12:29 +0100278 * succeed.
279 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100280 if (!list_empty(&counter->list_entry)) {
Ingo Molnar04289bb2008-12-11 08:38:42 +0100281 list_del_counter(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100282 counter->task = NULL;
283 }
284 spin_unlock_irq(&ctx->lock);
285}
286
Peter Zijlstra4af49982009-04-06 11:45:10 +0200287static inline u64 perf_clock(void)
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100288{
Peter Zijlstra4af49982009-04-06 11:45:10 +0200289 return cpu_clock(smp_processor_id());
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100290}
291
292/*
293 * Update the record of the current time in a context.
294 */
Peter Zijlstra4af49982009-04-06 11:45:10 +0200295static void update_context_time(struct perf_counter_context *ctx)
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100296{
Peter Zijlstra4af49982009-04-06 11:45:10 +0200297 u64 now = perf_clock();
298
299 ctx->time += now - ctx->timestamp;
300 ctx->timestamp = now;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100301}
302
303/*
304 * Update the total_time_enabled and total_time_running fields for a counter.
305 */
306static void update_counter_times(struct perf_counter *counter)
307{
308 struct perf_counter_context *ctx = counter->ctx;
309 u64 run_end;
310
Peter Zijlstra4af49982009-04-06 11:45:10 +0200311 if (counter->state < PERF_COUNTER_STATE_INACTIVE)
312 return;
313
314 counter->total_time_enabled = ctx->time - counter->tstamp_enabled;
315
316 if (counter->state == PERF_COUNTER_STATE_INACTIVE)
317 run_end = counter->tstamp_stopped;
318 else
319 run_end = ctx->time;
320
321 counter->total_time_running = run_end - counter->tstamp_running;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100322}
323
324/*
325 * Update total_time_enabled and total_time_running for all counters in a group.
326 */
327static void update_group_times(struct perf_counter *leader)
328{
329 struct perf_counter *counter;
330
331 update_counter_times(leader);
332 list_for_each_entry(counter, &leader->sibling_list, list_entry)
333 update_counter_times(counter);
334}
335
336/*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100337 * Cross CPU call to disable a performance counter
338 */
339static void __perf_counter_disable(void *info)
340{
341 struct perf_counter *counter = info;
342 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
343 struct perf_counter_context *ctx = counter->ctx;
344 unsigned long flags;
345
346 /*
347 * If this is a per-task counter, need to check whether this
348 * counter's task is the current task on this cpu.
349 */
350 if (ctx->task && cpuctx->task_ctx != ctx)
351 return;
352
Peter Zijlstra849691a2009-04-06 11:45:12 +0200353 spin_lock_irqsave(&ctx->lock, flags);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100354
355 /*
356 * If the counter is on, turn it off.
357 * If it is in error state, leave it in error state.
358 */
359 if (counter->state >= PERF_COUNTER_STATE_INACTIVE) {
Peter Zijlstra4af49982009-04-06 11:45:10 +0200360 update_context_time(ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100361 update_counter_times(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100362 if (counter == counter->group_leader)
363 group_sched_out(counter, cpuctx, ctx);
364 else
365 counter_sched_out(counter, cpuctx, ctx);
366 counter->state = PERF_COUNTER_STATE_OFF;
367 }
368
Peter Zijlstra849691a2009-04-06 11:45:12 +0200369 spin_unlock_irqrestore(&ctx->lock, flags);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100370}
371
372/*
373 * Disable a counter.
374 */
375static void perf_counter_disable(struct perf_counter *counter)
376{
377 struct perf_counter_context *ctx = counter->ctx;
378 struct task_struct *task = ctx->task;
379
380 if (!task) {
381 /*
382 * Disable the counter on the cpu that it's on
383 */
384 smp_call_function_single(counter->cpu, __perf_counter_disable,
385 counter, 1);
386 return;
387 }
388
389 retry:
390 task_oncpu_function_call(task, __perf_counter_disable, counter);
391
392 spin_lock_irq(&ctx->lock);
393 /*
394 * If the counter is still active, we need to retry the cross-call.
395 */
396 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
397 spin_unlock_irq(&ctx->lock);
398 goto retry;
399 }
400
401 /*
402 * Since we have the lock this context can't be scheduled
403 * in, so we can change the state safely.
404 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100405 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
406 update_counter_times(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100407 counter->state = PERF_COUNTER_STATE_OFF;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100408 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100409
410 spin_unlock_irq(&ctx->lock);
411}
412
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100413static int
414counter_sched_in(struct perf_counter *counter,
415 struct perf_cpu_context *cpuctx,
416 struct perf_counter_context *ctx,
417 int cpu)
418{
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100419 if (counter->state <= PERF_COUNTER_STATE_OFF)
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100420 return 0;
421
422 counter->state = PERF_COUNTER_STATE_ACTIVE;
423 counter->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */
424 /*
425 * The new state must be visible before we turn it on in the hardware:
426 */
427 smp_wmb();
428
Robert Richter4aeb0b42009-04-29 12:47:03 +0200429 if (counter->pmu->enable(counter)) {
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100430 counter->state = PERF_COUNTER_STATE_INACTIVE;
431 counter->oncpu = -1;
432 return -EAGAIN;
433 }
434
Peter Zijlstra4af49982009-04-06 11:45:10 +0200435 counter->tstamp_running += ctx->time - counter->tstamp_stopped;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100436
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100437 if (!is_software_counter(counter))
438 cpuctx->active_oncpu++;
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100439 ctx->nr_active++;
440
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100441 if (counter->hw_event.exclusive)
442 cpuctx->exclusive = 1;
443
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100444 return 0;
445}
446
Paul Mackerras6751b712009-05-11 12:08:02 +1000447static int
448group_sched_in(struct perf_counter *group_counter,
449 struct perf_cpu_context *cpuctx,
450 struct perf_counter_context *ctx,
451 int cpu)
452{
453 struct perf_counter *counter, *partial_group;
454 int ret;
455
456 if (group_counter->state == PERF_COUNTER_STATE_OFF)
457 return 0;
458
459 ret = hw_perf_group_sched_in(group_counter, cpuctx, ctx, cpu);
460 if (ret)
461 return ret < 0 ? ret : 0;
462
463 group_counter->prev_state = group_counter->state;
464 if (counter_sched_in(group_counter, cpuctx, ctx, cpu))
465 return -EAGAIN;
466
467 /*
468 * Schedule in siblings as one group (if any):
469 */
470 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
471 counter->prev_state = counter->state;
472 if (counter_sched_in(counter, cpuctx, ctx, cpu)) {
473 partial_group = counter;
474 goto group_error;
475 }
476 }
477
478 return 0;
479
480group_error:
481 /*
482 * Groups can be scheduled in as one unit only, so undo any
483 * partial group before returning:
484 */
485 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
486 if (counter == partial_group)
487 break;
488 counter_sched_out(counter, cpuctx, ctx);
489 }
490 counter_sched_out(group_counter, cpuctx, ctx);
491
492 return -EAGAIN;
493}
494
Thomas Gleixner0793a612008-12-04 20:12:29 +0100495/*
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100496 * Return 1 for a group consisting entirely of software counters,
497 * 0 if the group contains any hardware counters.
498 */
499static int is_software_only_group(struct perf_counter *leader)
500{
501 struct perf_counter *counter;
502
503 if (!is_software_counter(leader))
504 return 0;
Peter Zijlstra5c148192009-03-25 12:30:23 +0100505
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100506 list_for_each_entry(counter, &leader->sibling_list, list_entry)
507 if (!is_software_counter(counter))
508 return 0;
Peter Zijlstra5c148192009-03-25 12:30:23 +0100509
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100510 return 1;
511}
512
513/*
514 * Work out whether we can put this counter group on the CPU now.
515 */
516static int group_can_go_on(struct perf_counter *counter,
517 struct perf_cpu_context *cpuctx,
518 int can_add_hw)
519{
520 /*
521 * Groups consisting entirely of software counters can always go on.
522 */
523 if (is_software_only_group(counter))
524 return 1;
525 /*
526 * If an exclusive group is already on, no other hardware
527 * counters can go on.
528 */
529 if (cpuctx->exclusive)
530 return 0;
531 /*
532 * If this group is exclusive and there are already
533 * counters on the CPU, it can't go on.
534 */
535 if (counter->hw_event.exclusive && cpuctx->active_oncpu)
536 return 0;
537 /*
538 * Otherwise, try to add it if all previous groups were able
539 * to go on.
540 */
541 return can_add_hw;
542}
543
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100544static void add_counter_to_ctx(struct perf_counter *counter,
545 struct perf_counter_context *ctx)
546{
547 list_add_counter(counter, ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100548 counter->prev_state = PERF_COUNTER_STATE_OFF;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200549 counter->tstamp_enabled = ctx->time;
550 counter->tstamp_running = ctx->time;
551 counter->tstamp_stopped = ctx->time;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100552}
553
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100554/*
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100555 * Cross CPU call to install and enable a performance counter
Thomas Gleixner0793a612008-12-04 20:12:29 +0100556 */
557static void __perf_install_in_context(void *info)
558{
559 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
560 struct perf_counter *counter = info;
561 struct perf_counter_context *ctx = counter->ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100562 struct perf_counter *leader = counter->group_leader;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100563 int cpu = smp_processor_id();
Ingo Molnar9b51f662008-12-12 13:49:45 +0100564 unsigned long flags;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100565 int err;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100566
567 /*
568 * If this is a task context, we need to check whether it is
569 * the current task context of this cpu. If not it has been
570 * scheduled out before the smp call arrived.
571 */
572 if (ctx->task && cpuctx->task_ctx != ctx)
573 return;
574
Peter Zijlstra849691a2009-04-06 11:45:12 +0200575 spin_lock_irqsave(&ctx->lock, flags);
Peter Zijlstra4af49982009-04-06 11:45:10 +0200576 update_context_time(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100577
578 /*
579 * Protect the list operation against NMI by disabling the
580 * counters on a global level. NOP for non NMI based counters.
581 */
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200582 perf_disable();
Thomas Gleixner0793a612008-12-04 20:12:29 +0100583
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100584 add_counter_to_ctx(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100585
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100586 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100587 * Don't put the counter on if it is disabled or if
588 * it is in a group and the group isn't on.
589 */
590 if (counter->state != PERF_COUNTER_STATE_INACTIVE ||
591 (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE))
592 goto unlock;
593
594 /*
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100595 * An exclusive counter can't go on if there are already active
596 * hardware counters, and no hardware counter can go on if there
597 * is already an exclusive counter on.
598 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100599 if (!group_can_go_on(counter, cpuctx, 1))
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100600 err = -EEXIST;
601 else
602 err = counter_sched_in(counter, cpuctx, ctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100603
Paul Mackerrasd859e292009-01-17 18:10:22 +1100604 if (err) {
605 /*
606 * This counter couldn't go on. If it is in a group
607 * then we have to pull the whole group off.
608 * If the counter group is pinned then put it in error state.
609 */
610 if (leader != counter)
611 group_sched_out(leader, cpuctx, ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100612 if (leader->hw_event.pinned) {
613 update_group_times(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100614 leader->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100615 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100616 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100617
618 if (!err && !ctx->task && cpuctx->max_pertask)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100619 cpuctx->max_pertask--;
620
Paul Mackerrasd859e292009-01-17 18:10:22 +1100621 unlock:
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200622 perf_enable();
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100623
Peter Zijlstra849691a2009-04-06 11:45:12 +0200624 spin_unlock_irqrestore(&ctx->lock, flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100625}
626
627/*
628 * Attach a performance counter to a context
629 *
630 * First we add the counter to the list with the hardware enable bit
631 * in counter->hw_config cleared.
632 *
633 * If the counter is attached to a task which is on a CPU we use a smp
634 * call to enable it in the task context. The task might have been
635 * scheduled away, but we check this in the smp call again.
Paul Mackerrasd859e292009-01-17 18:10:22 +1100636 *
637 * Must be called with ctx->mutex held.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100638 */
639static void
640perf_install_in_context(struct perf_counter_context *ctx,
641 struct perf_counter *counter,
642 int cpu)
643{
644 struct task_struct *task = ctx->task;
645
Thomas Gleixner0793a612008-12-04 20:12:29 +0100646 if (!task) {
647 /*
648 * Per cpu counters are installed via an smp call and
649 * the install is always sucessful.
650 */
651 smp_call_function_single(cpu, __perf_install_in_context,
652 counter, 1);
653 return;
654 }
655
656 counter->task = task;
657retry:
658 task_oncpu_function_call(task, __perf_install_in_context,
659 counter);
660
661 spin_lock_irq(&ctx->lock);
662 /*
Thomas Gleixner0793a612008-12-04 20:12:29 +0100663 * we need to retry the smp call.
664 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100665 if (ctx->is_active && list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100666 spin_unlock_irq(&ctx->lock);
667 goto retry;
668 }
669
670 /*
671 * The lock prevents that this context is scheduled in so we
672 * can add the counter safely, if it the call above did not
673 * succeed.
674 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100675 if (list_empty(&counter->list_entry))
676 add_counter_to_ctx(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100677 spin_unlock_irq(&ctx->lock);
678}
679
Paul Mackerrasd859e292009-01-17 18:10:22 +1100680/*
681 * Cross CPU call to enable a performance counter
682 */
683static void __perf_counter_enable(void *info)
Ingo Molnar04289bb2008-12-11 08:38:42 +0100684{
Paul Mackerrasd859e292009-01-17 18:10:22 +1100685 struct perf_counter *counter = info;
686 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
687 struct perf_counter_context *ctx = counter->ctx;
688 struct perf_counter *leader = counter->group_leader;
689 unsigned long flags;
690 int err;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100691
692 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100693 * If this is a per-task counter, need to check whether this
694 * counter's task is the current task on this cpu.
Ingo Molnar04289bb2008-12-11 08:38:42 +0100695 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100696 if (ctx->task && cpuctx->task_ctx != ctx)
697 return;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100698
Peter Zijlstra849691a2009-04-06 11:45:12 +0200699 spin_lock_irqsave(&ctx->lock, flags);
Peter Zijlstra4af49982009-04-06 11:45:10 +0200700 update_context_time(ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100701
Paul Mackerrasc07c99b2009-02-13 22:10:34 +1100702 counter->prev_state = counter->state;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100703 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
704 goto unlock;
705 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200706 counter->tstamp_enabled = ctx->time - counter->total_time_enabled;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100707
708 /*
709 * If the counter is in a group and isn't the group leader,
710 * then don't put it on unless the group is on.
711 */
712 if (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE)
713 goto unlock;
714
Paul Mackerrase758a332009-05-12 21:59:01 +1000715 if (!group_can_go_on(counter, cpuctx, 1)) {
Paul Mackerrasd859e292009-01-17 18:10:22 +1100716 err = -EEXIST;
Paul Mackerrase758a332009-05-12 21:59:01 +1000717 } else {
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200718 perf_disable();
Paul Mackerrase758a332009-05-12 21:59:01 +1000719 if (counter == leader)
720 err = group_sched_in(counter, cpuctx, ctx,
721 smp_processor_id());
722 else
723 err = counter_sched_in(counter, cpuctx, ctx,
724 smp_processor_id());
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200725 perf_enable();
Paul Mackerrase758a332009-05-12 21:59:01 +1000726 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100727
728 if (err) {
729 /*
730 * If this counter can't go on and it's part of a
731 * group, then the whole group has to come off.
732 */
733 if (leader != counter)
734 group_sched_out(leader, cpuctx, ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100735 if (leader->hw_event.pinned) {
736 update_group_times(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100737 leader->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100738 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100739 }
740
741 unlock:
Peter Zijlstra849691a2009-04-06 11:45:12 +0200742 spin_unlock_irqrestore(&ctx->lock, flags);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100743}
744
745/*
746 * Enable a counter.
747 */
748static void perf_counter_enable(struct perf_counter *counter)
749{
750 struct perf_counter_context *ctx = counter->ctx;
751 struct task_struct *task = ctx->task;
752
753 if (!task) {
754 /*
755 * Enable the counter on the cpu that it's on
756 */
757 smp_call_function_single(counter->cpu, __perf_counter_enable,
758 counter, 1);
759 return;
760 }
761
762 spin_lock_irq(&ctx->lock);
763 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
764 goto out;
765
766 /*
767 * If the counter is in error state, clear that first.
768 * That way, if we see the counter in error state below, we
769 * know that it has gone back into error state, as distinct
770 * from the task having been scheduled away before the
771 * cross-call arrived.
772 */
773 if (counter->state == PERF_COUNTER_STATE_ERROR)
774 counter->state = PERF_COUNTER_STATE_OFF;
775
776 retry:
777 spin_unlock_irq(&ctx->lock);
778 task_oncpu_function_call(task, __perf_counter_enable, counter);
779
780 spin_lock_irq(&ctx->lock);
781
782 /*
783 * If the context is active and the counter is still off,
784 * we need to retry the cross-call.
785 */
786 if (ctx->is_active && counter->state == PERF_COUNTER_STATE_OFF)
787 goto retry;
788
789 /*
790 * Since we have the lock this context can't be scheduled
791 * in, so we can change the state safely.
792 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100793 if (counter->state == PERF_COUNTER_STATE_OFF) {
Paul Mackerrasd859e292009-01-17 18:10:22 +1100794 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200795 counter->tstamp_enabled =
796 ctx->time - counter->total_time_enabled;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100797 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100798 out:
799 spin_unlock_irq(&ctx->lock);
800}
801
Peter Zijlstra2023b352009-05-05 17:50:26 +0200802static int perf_counter_refresh(struct perf_counter *counter, int refresh)
Peter Zijlstra79f14642009-04-06 11:45:07 +0200803{
Peter Zijlstra2023b352009-05-05 17:50:26 +0200804 /*
805 * not supported on inherited counters
806 */
807 if (counter->hw_event.inherit)
808 return -EINVAL;
809
Peter Zijlstra79f14642009-04-06 11:45:07 +0200810 atomic_add(refresh, &counter->event_limit);
811 perf_counter_enable(counter);
Peter Zijlstra2023b352009-05-05 17:50:26 +0200812
813 return 0;
Peter Zijlstra79f14642009-04-06 11:45:07 +0200814}
815
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100816void __perf_counter_sched_out(struct perf_counter_context *ctx,
817 struct perf_cpu_context *cpuctx)
818{
819 struct perf_counter *counter;
820
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100821 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100822 ctx->is_active = 0;
823 if (likely(!ctx->nr_counters))
824 goto out;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200825 update_context_time(ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100826
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200827 perf_disable();
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100828 if (ctx->nr_active) {
Peter Zijlstraafedadf2009-05-20 12:21:22 +0200829 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
830 if (counter != counter->group_leader)
831 counter_sched_out(counter, cpuctx, ctx);
832 else
833 group_sched_out(counter, cpuctx, ctx);
834 }
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100835 }
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200836 perf_enable();
Paul Mackerrasd859e292009-01-17 18:10:22 +1100837 out:
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100838 spin_unlock(&ctx->lock);
839}
840
Thomas Gleixner0793a612008-12-04 20:12:29 +0100841/*
842 * Called from scheduler to remove the counters of the current task,
843 * with interrupts disabled.
844 *
845 * We stop each counter and update the counter value in counter->count.
846 *
Ingo Molnar76715812008-12-17 14:20:28 +0100847 * This does not protect us against NMI, but disable()
Thomas Gleixner0793a612008-12-04 20:12:29 +0100848 * sets the disabled bit in the control field of counter _before_
849 * accessing the counter control register. If a NMI hits, then it will
850 * not restart the counter.
851 */
852void perf_counter_task_sched_out(struct task_struct *task, int cpu)
853{
854 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
855 struct perf_counter_context *ctx = &task->perf_counter_ctx;
Peter Zijlstra4a0deca2009-03-19 20:26:12 +0100856 struct pt_regs *regs;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100857
858 if (likely(!cpuctx->task_ctx))
859 return;
860
Peter Zijlstrabce379b2009-04-06 11:45:13 +0200861 update_context_time(ctx);
862
Peter Zijlstra4a0deca2009-03-19 20:26:12 +0100863 regs = task_pt_regs(task);
Peter Zijlstra78f13e92009-04-08 15:01:33 +0200864 perf_swcounter_event(PERF_COUNT_CONTEXT_SWITCHES, 1, 1, regs, 0);
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100865 __perf_counter_sched_out(ctx, cpuctx);
866
Thomas Gleixner0793a612008-12-04 20:12:29 +0100867 cpuctx->task_ctx = NULL;
868}
869
Paul Mackerrasa08b1592009-05-11 15:46:10 +1000870static void __perf_counter_task_sched_out(struct perf_counter_context *ctx)
871{
872 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
873
874 __perf_counter_sched_out(ctx, cpuctx);
875 cpuctx->task_ctx = NULL;
876}
877
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100878static void perf_counter_cpu_sched_out(struct perf_cpu_context *cpuctx)
Ingo Molnar04289bb2008-12-11 08:38:42 +0100879{
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100880 __perf_counter_sched_out(&cpuctx->ctx, cpuctx);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100881}
882
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100883static void
884__perf_counter_sched_in(struct perf_counter_context *ctx,
885 struct perf_cpu_context *cpuctx, int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100886{
Thomas Gleixner0793a612008-12-04 20:12:29 +0100887 struct perf_counter *counter;
Paul Mackerrasdd0e6ba2009-01-12 15:11:00 +1100888 int can_add_hw = 1;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100889
Thomas Gleixner0793a612008-12-04 20:12:29 +0100890 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100891 ctx->is_active = 1;
892 if (likely(!ctx->nr_counters))
893 goto out;
894
Peter Zijlstra4af49982009-04-06 11:45:10 +0200895 ctx->timestamp = perf_clock();
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100896
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200897 perf_disable();
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100898
899 /*
900 * First go through the list and put on any pinned groups
901 * in order to give them the best chance of going on.
902 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100903 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100904 if (counter->state <= PERF_COUNTER_STATE_OFF ||
905 !counter->hw_event.pinned)
906 continue;
907 if (counter->cpu != -1 && counter->cpu != cpu)
908 continue;
909
Peter Zijlstraafedadf2009-05-20 12:21:22 +0200910 if (counter != counter->group_leader)
911 counter_sched_in(counter, cpuctx, ctx, cpu);
912 else {
913 if (group_can_go_on(counter, cpuctx, 1))
914 group_sched_in(counter, cpuctx, ctx, cpu);
915 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100916
917 /*
918 * If this pinned group hasn't been scheduled,
919 * put it in error state.
920 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100921 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
922 update_group_times(counter);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100923 counter->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100924 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100925 }
926
927 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
928 /*
929 * Ignore counters in OFF or ERROR state, and
930 * ignore pinned counters since we did them already.
931 */
932 if (counter->state <= PERF_COUNTER_STATE_OFF ||
933 counter->hw_event.pinned)
934 continue;
935
Ingo Molnar04289bb2008-12-11 08:38:42 +0100936 /*
937 * Listen to the 'cpu' scheduling filter constraint
938 * of counters:
939 */
Thomas Gleixner0793a612008-12-04 20:12:29 +0100940 if (counter->cpu != -1 && counter->cpu != cpu)
941 continue;
942
Peter Zijlstraafedadf2009-05-20 12:21:22 +0200943 if (counter != counter->group_leader) {
944 if (counter_sched_in(counter, cpuctx, ctx, cpu))
Paul Mackerrasdd0e6ba2009-01-12 15:11:00 +1100945 can_add_hw = 0;
Peter Zijlstraafedadf2009-05-20 12:21:22 +0200946 } else {
947 if (group_can_go_on(counter, cpuctx, can_add_hw)) {
948 if (group_sched_in(counter, cpuctx, ctx, cpu))
949 can_add_hw = 0;
950 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100951 }
Thomas Gleixner0793a612008-12-04 20:12:29 +0100952 }
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200953 perf_enable();
Paul Mackerrasd859e292009-01-17 18:10:22 +1100954 out:
Thomas Gleixner0793a612008-12-04 20:12:29 +0100955 spin_unlock(&ctx->lock);
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100956}
Ingo Molnar04289bb2008-12-11 08:38:42 +0100957
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100958/*
959 * Called from scheduler to add the counters of the current task
960 * with interrupts disabled.
961 *
962 * We restore the counter value and then enable it.
963 *
964 * This does not protect us against NMI, but enable()
965 * sets the enabled bit in the control field of counter _before_
966 * accessing the counter control register. If a NMI hits, then it will
967 * keep the counter running.
968 */
969void perf_counter_task_sched_in(struct task_struct *task, int cpu)
970{
971 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
972 struct perf_counter_context *ctx = &task->perf_counter_ctx;
973
974 __perf_counter_sched_in(ctx, cpuctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100975 cpuctx->task_ctx = ctx;
976}
977
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100978static void perf_counter_cpu_sched_in(struct perf_cpu_context *cpuctx, int cpu)
979{
980 struct perf_counter_context *ctx = &cpuctx->ctx;
981
982 __perf_counter_sched_in(ctx, cpuctx, cpu);
983}
984
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100985int perf_counter_task_disable(void)
986{
987 struct task_struct *curr = current;
988 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
989 struct perf_counter *counter;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100990 unsigned long flags;
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100991
992 if (likely(!ctx->nr_counters))
993 return 0;
994
Peter Zijlstra849691a2009-04-06 11:45:12 +0200995 local_irq_save(flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100996
Paul Mackerrasa08b1592009-05-11 15:46:10 +1000997 __perf_counter_task_sched_out(ctx);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100998
999 spin_lock(&ctx->lock);
1000
1001 /*
1002 * Disable all the counters:
1003 */
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001004 perf_disable();
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001005
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001006 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001007 if (counter->state != PERF_COUNTER_STATE_ERROR) {
1008 update_group_times(counter);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001009 counter->state = PERF_COUNTER_STATE_OFF;
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001010 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001011 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01001012
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001013 perf_enable();
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001014
Peter Zijlstra849691a2009-04-06 11:45:12 +02001015 spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001016
1017 return 0;
1018}
1019
1020int perf_counter_task_enable(void)
1021{
1022 struct task_struct *curr = current;
1023 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
1024 struct perf_counter *counter;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001025 unsigned long flags;
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001026 int cpu;
1027
1028 if (likely(!ctx->nr_counters))
1029 return 0;
1030
Peter Zijlstra849691a2009-04-06 11:45:12 +02001031 local_irq_save(flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001032 cpu = smp_processor_id();
1033
Paul Mackerrasa08b1592009-05-11 15:46:10 +10001034 __perf_counter_task_sched_out(ctx);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001035
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001036 spin_lock(&ctx->lock);
1037
1038 /*
1039 * Disable all the counters:
1040 */
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001041 perf_disable();
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001042
1043 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001044 if (counter->state > PERF_COUNTER_STATE_OFF)
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001045 continue;
Ingo Molnar6a930702008-12-11 15:17:03 +01001046 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +02001047 counter->tstamp_enabled =
1048 ctx->time - counter->total_time_enabled;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001049 counter->hw_event.disabled = 0;
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001050 }
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001051 perf_enable();
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001052
1053 spin_unlock(&ctx->lock);
1054
1055 perf_counter_task_sched_in(curr, cpu);
1056
Peter Zijlstra849691a2009-04-06 11:45:12 +02001057 local_irq_restore(flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001058
1059 return 0;
1060}
1061
Peter Zijlstra26b119b2009-05-20 12:21:20 +02001062static void perf_log_period(struct perf_counter *counter, u64 period);
1063
1064static void perf_adjust_freq(struct perf_counter_context *ctx)
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001065{
1066 struct perf_counter *counter;
1067 u64 irq_period;
1068 u64 events, period;
1069 s64 delta;
1070
1071 spin_lock(&ctx->lock);
1072 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1073 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
1074 continue;
1075
1076 if (!counter->hw_event.freq || !counter->hw_event.irq_freq)
1077 continue;
1078
1079 events = HZ * counter->hw.interrupts * counter->hw.irq_period;
1080 period = div64_u64(events, counter->hw_event.irq_freq);
1081
1082 delta = (s64)(1 + period - counter->hw.irq_period);
1083 delta >>= 1;
1084
1085 irq_period = counter->hw.irq_period + delta;
1086
1087 if (!irq_period)
1088 irq_period = 1;
1089
Peter Zijlstra26b119b2009-05-20 12:21:20 +02001090 perf_log_period(counter, irq_period);
1091
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001092 counter->hw.irq_period = irq_period;
1093 counter->hw.interrupts = 0;
1094 }
1095 spin_unlock(&ctx->lock);
1096}
1097
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001098/*
1099 * Round-robin a context's counters:
1100 */
1101static void rotate_ctx(struct perf_counter_context *ctx)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001102{
Thomas Gleixner0793a612008-12-04 20:12:29 +01001103 struct perf_counter *counter;
1104
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001105 if (!ctx->nr_counters)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001106 return;
1107
Thomas Gleixner0793a612008-12-04 20:12:29 +01001108 spin_lock(&ctx->lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001109 /*
Ingo Molnar04289bb2008-12-11 08:38:42 +01001110 * Rotate the first entry last (works just fine for group counters too):
Thomas Gleixner0793a612008-12-04 20:12:29 +01001111 */
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001112 perf_disable();
Ingo Molnar04289bb2008-12-11 08:38:42 +01001113 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Peter Zijlstra75564232009-03-13 12:21:29 +01001114 list_move_tail(&counter->list_entry, &ctx->counter_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001115 break;
1116 }
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001117 perf_enable();
Thomas Gleixner0793a612008-12-04 20:12:29 +01001118
1119 spin_unlock(&ctx->lock);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001120}
Thomas Gleixner0793a612008-12-04 20:12:29 +01001121
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001122void perf_counter_task_tick(struct task_struct *curr, int cpu)
1123{
Peter Zijlstra7fc23a52009-05-08 18:52:21 +02001124 struct perf_cpu_context *cpuctx;
1125 struct perf_counter_context *ctx;
1126
1127 if (!atomic_read(&nr_counters))
1128 return;
1129
1130 cpuctx = &per_cpu(perf_cpu_context, cpu);
1131 ctx = &curr->perf_counter_ctx;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001132
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001133 perf_adjust_freq(&cpuctx->ctx);
1134 perf_adjust_freq(ctx);
1135
Ingo Molnarb82914c2009-05-04 18:54:32 +02001136 perf_counter_cpu_sched_out(cpuctx);
Paul Mackerrasa08b1592009-05-11 15:46:10 +10001137 __perf_counter_task_sched_out(ctx);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001138
Ingo Molnarb82914c2009-05-04 18:54:32 +02001139 rotate_ctx(&cpuctx->ctx);
Peter Zijlstrad7b629a2009-05-20 12:21:19 +02001140 rotate_ctx(ctx);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001141
Ingo Molnarb82914c2009-05-04 18:54:32 +02001142 perf_counter_cpu_sched_in(cpuctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001143 perf_counter_task_sched_in(curr, cpu);
1144}
1145
1146/*
Thomas Gleixner0793a612008-12-04 20:12:29 +01001147 * Cross CPU call to read the hardware counter
1148 */
Ingo Molnar76715812008-12-17 14:20:28 +01001149static void __read(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001150{
Ingo Molnar621a01e2008-12-11 12:46:46 +01001151 struct perf_counter *counter = info;
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001152 struct perf_counter_context *ctx = counter->ctx;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001153 unsigned long flags;
Ingo Molnar621a01e2008-12-11 12:46:46 +01001154
Peter Zijlstra849691a2009-04-06 11:45:12 +02001155 local_irq_save(flags);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001156 if (ctx->is_active)
Peter Zijlstra4af49982009-04-06 11:45:10 +02001157 update_context_time(ctx);
Robert Richter4aeb0b42009-04-29 12:47:03 +02001158 counter->pmu->read(counter);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001159 update_counter_times(counter);
Peter Zijlstra849691a2009-04-06 11:45:12 +02001160 local_irq_restore(flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001161}
1162
Ingo Molnar04289bb2008-12-11 08:38:42 +01001163static u64 perf_counter_read(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001164{
1165 /*
1166 * If counter is enabled and currently active on a CPU, update the
1167 * value in the counter structure:
1168 */
Ingo Molnar6a930702008-12-11 15:17:03 +01001169 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
Thomas Gleixner0793a612008-12-04 20:12:29 +01001170 smp_call_function_single(counter->oncpu,
Ingo Molnar76715812008-12-17 14:20:28 +01001171 __read, counter, 1);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001172 } else if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
1173 update_counter_times(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001174 }
1175
Ingo Molnaree060942008-12-13 09:00:03 +01001176 return atomic64_read(&counter->count);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001177}
1178
Thomas Gleixner0793a612008-12-04 20:12:29 +01001179static void put_context(struct perf_counter_context *ctx)
1180{
1181 if (ctx->task)
1182 put_task_struct(ctx->task);
1183}
1184
1185static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
1186{
1187 struct perf_cpu_context *cpuctx;
1188 struct perf_counter_context *ctx;
1189 struct task_struct *task;
1190
1191 /*
1192 * If cpu is not a wildcard then this is a percpu counter:
1193 */
1194 if (cpu != -1) {
1195 /* Must be root to operate on a CPU counter: */
Peter Zijlstra1ccd1542009-04-09 10:53:45 +02001196 if (sysctl_perf_counter_priv && !capable(CAP_SYS_ADMIN))
Thomas Gleixner0793a612008-12-04 20:12:29 +01001197 return ERR_PTR(-EACCES);
1198
1199 if (cpu < 0 || cpu > num_possible_cpus())
1200 return ERR_PTR(-EINVAL);
1201
1202 /*
1203 * We could be clever and allow to attach a counter to an
1204 * offline CPU and activate it when the CPU comes up, but
1205 * that's for later.
1206 */
1207 if (!cpu_isset(cpu, cpu_online_map))
1208 return ERR_PTR(-ENODEV);
1209
1210 cpuctx = &per_cpu(perf_cpu_context, cpu);
1211 ctx = &cpuctx->ctx;
1212
Thomas Gleixner0793a612008-12-04 20:12:29 +01001213 return ctx;
1214 }
1215
1216 rcu_read_lock();
1217 if (!pid)
1218 task = current;
1219 else
1220 task = find_task_by_vpid(pid);
1221 if (task)
1222 get_task_struct(task);
1223 rcu_read_unlock();
1224
1225 if (!task)
1226 return ERR_PTR(-ESRCH);
1227
1228 ctx = &task->perf_counter_ctx;
1229 ctx->task = task;
1230
1231 /* Reuse ptrace permission checks for now. */
1232 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
1233 put_context(ctx);
1234 return ERR_PTR(-EACCES);
1235 }
1236
1237 return ctx;
1238}
1239
Peter Zijlstra592903c2009-03-13 12:21:36 +01001240static void free_counter_rcu(struct rcu_head *head)
1241{
1242 struct perf_counter *counter;
1243
1244 counter = container_of(head, struct perf_counter, rcu_head);
1245 kfree(counter);
1246}
1247
Peter Zijlstra925d5192009-03-30 19:07:02 +02001248static void perf_pending_sync(struct perf_counter *counter);
1249
Peter Zijlstraf1600952009-03-19 20:26:16 +01001250static void free_counter(struct perf_counter *counter)
1251{
Peter Zijlstra925d5192009-03-30 19:07:02 +02001252 perf_pending_sync(counter);
1253
Peter Zijlstra7fc23a52009-05-08 18:52:21 +02001254 atomic_dec(&nr_counters);
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02001255 if (counter->hw_event.mmap)
1256 atomic_dec(&nr_mmap_tracking);
1257 if (counter->hw_event.munmap)
1258 atomic_dec(&nr_munmap_tracking);
1259 if (counter->hw_event.comm)
1260 atomic_dec(&nr_comm_tracking);
1261
Peter Zijlstrae077df42009-03-19 20:26:17 +01001262 if (counter->destroy)
1263 counter->destroy(counter);
1264
Peter Zijlstraf1600952009-03-19 20:26:16 +01001265 call_rcu(&counter->rcu_head, free_counter_rcu);
1266}
1267
Thomas Gleixner0793a612008-12-04 20:12:29 +01001268/*
1269 * Called when the last reference to the file is gone.
1270 */
1271static int perf_release(struct inode *inode, struct file *file)
1272{
1273 struct perf_counter *counter = file->private_data;
1274 struct perf_counter_context *ctx = counter->ctx;
1275
1276 file->private_data = NULL;
1277
Paul Mackerrasd859e292009-01-17 18:10:22 +11001278 mutex_lock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001279 mutex_lock(&counter->mutex);
1280
Ingo Molnar04289bb2008-12-11 08:38:42 +01001281 perf_counter_remove_from_context(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001282
1283 mutex_unlock(&counter->mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001284 mutex_unlock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001285
Peter Zijlstraf1600952009-03-19 20:26:16 +01001286 free_counter(counter);
Mike Galbraith5af75912009-02-11 10:53:37 +01001287 put_context(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001288
1289 return 0;
1290}
1291
1292/*
1293 * Read the performance counter - simple non blocking version for now
1294 */
1295static ssize_t
1296perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
1297{
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001298 u64 values[3];
1299 int n;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001300
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001301 /*
1302 * Return end-of-file for a read on a counter that is in
1303 * error state (i.e. because it was pinned but it couldn't be
1304 * scheduled on to the CPU at some point).
1305 */
1306 if (counter->state == PERF_COUNTER_STATE_ERROR)
1307 return 0;
1308
Thomas Gleixner0793a612008-12-04 20:12:29 +01001309 mutex_lock(&counter->mutex);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001310 values[0] = perf_counter_read(counter);
1311 n = 1;
1312 if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1313 values[n++] = counter->total_time_enabled +
1314 atomic64_read(&counter->child_total_time_enabled);
1315 if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1316 values[n++] = counter->total_time_running +
1317 atomic64_read(&counter->child_total_time_running);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001318 mutex_unlock(&counter->mutex);
1319
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001320 if (count < n * sizeof(u64))
1321 return -EINVAL;
1322 count = n * sizeof(u64);
1323
1324 if (copy_to_user(buf, values, count))
1325 return -EFAULT;
1326
1327 return count;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001328}
1329
1330static ssize_t
Thomas Gleixner0793a612008-12-04 20:12:29 +01001331perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
1332{
1333 struct perf_counter *counter = file->private_data;
1334
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001335 return perf_read_hw(counter, buf, count);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001336}
1337
1338static unsigned int perf_poll(struct file *file, poll_table *wait)
1339{
1340 struct perf_counter *counter = file->private_data;
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001341 struct perf_mmap_data *data;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001342 unsigned int events = POLL_HUP;
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001343
1344 rcu_read_lock();
1345 data = rcu_dereference(counter->data);
1346 if (data)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001347 events = atomic_xchg(&data->poll, 0);
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001348 rcu_read_unlock();
Thomas Gleixner0793a612008-12-04 20:12:29 +01001349
1350 poll_wait(file, &counter->waitq, wait);
1351
Thomas Gleixner0793a612008-12-04 20:12:29 +01001352 return events;
1353}
1354
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001355static void perf_counter_reset(struct perf_counter *counter)
1356{
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001357 (void)perf_counter_read(counter);
Paul Mackerras615a3f12009-05-11 15:50:21 +10001358 atomic64_set(&counter->count, 0);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001359 perf_counter_update_userpage(counter);
1360}
1361
1362static void perf_counter_for_each_sibling(struct perf_counter *counter,
1363 void (*func)(struct perf_counter *))
1364{
1365 struct perf_counter_context *ctx = counter->ctx;
1366 struct perf_counter *sibling;
1367
1368 spin_lock_irq(&ctx->lock);
1369 counter = counter->group_leader;
1370
1371 func(counter);
1372 list_for_each_entry(sibling, &counter->sibling_list, list_entry)
1373 func(sibling);
1374 spin_unlock_irq(&ctx->lock);
1375}
1376
1377static void perf_counter_for_each_child(struct perf_counter *counter,
1378 void (*func)(struct perf_counter *))
1379{
1380 struct perf_counter *child;
1381
1382 mutex_lock(&counter->mutex);
1383 func(counter);
1384 list_for_each_entry(child, &counter->child_list, child_list)
1385 func(child);
1386 mutex_unlock(&counter->mutex);
1387}
1388
1389static void perf_counter_for_each(struct perf_counter *counter,
1390 void (*func)(struct perf_counter *))
1391{
1392 struct perf_counter *child;
1393
1394 mutex_lock(&counter->mutex);
1395 perf_counter_for_each_sibling(counter, func);
1396 list_for_each_entry(child, &counter->child_list, child_list)
1397 perf_counter_for_each_sibling(child, func);
1398 mutex_unlock(&counter->mutex);
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001399}
1400
Paul Mackerrasd859e292009-01-17 18:10:22 +11001401static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1402{
1403 struct perf_counter *counter = file->private_data;
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001404 void (*func)(struct perf_counter *);
1405 u32 flags = arg;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001406
1407 switch (cmd) {
1408 case PERF_COUNTER_IOC_ENABLE:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001409 func = perf_counter_enable;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001410 break;
1411 case PERF_COUNTER_IOC_DISABLE:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001412 func = perf_counter_disable;
Peter Zijlstra79f14642009-04-06 11:45:07 +02001413 break;
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001414 case PERF_COUNTER_IOC_RESET:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001415 func = perf_counter_reset;
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001416 break;
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001417
1418 case PERF_COUNTER_IOC_REFRESH:
1419 return perf_counter_refresh(counter, arg);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001420 default:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001421 return -ENOTTY;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001422 }
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001423
1424 if (flags & PERF_IOC_FLAG_GROUP)
1425 perf_counter_for_each(counter, func);
1426 else
1427 perf_counter_for_each_child(counter, func);
1428
1429 return 0;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001430}
1431
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001432/*
1433 * Callers need to ensure there can be no nesting of this function, otherwise
1434 * the seqlock logic goes bad. We can not serialize this because the arch
1435 * code calls this from NMI context.
1436 */
1437void perf_counter_update_userpage(struct perf_counter *counter)
Paul Mackerras37d81822009-03-23 18:22:08 +01001438{
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001439 struct perf_mmap_data *data;
1440 struct perf_counter_mmap_page *userpg;
1441
1442 rcu_read_lock();
1443 data = rcu_dereference(counter->data);
1444 if (!data)
1445 goto unlock;
1446
1447 userpg = data->user_page;
Paul Mackerras37d81822009-03-23 18:22:08 +01001448
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001449 /*
1450 * Disable preemption so as to not let the corresponding user-space
1451 * spin too long if we get preempted.
1452 */
1453 preempt_disable();
Paul Mackerras37d81822009-03-23 18:22:08 +01001454 ++userpg->lock;
Peter Zijlstra92f22a32009-04-02 11:12:04 +02001455 barrier();
Paul Mackerras37d81822009-03-23 18:22:08 +01001456 userpg->index = counter->hw.idx;
1457 userpg->offset = atomic64_read(&counter->count);
1458 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
1459 userpg->offset -= atomic64_read(&counter->hw.prev_count);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001460
Peter Zijlstra92f22a32009-04-02 11:12:04 +02001461 barrier();
Paul Mackerras37d81822009-03-23 18:22:08 +01001462 ++userpg->lock;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001463 preempt_enable();
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001464unlock:
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001465 rcu_read_unlock();
Paul Mackerras37d81822009-03-23 18:22:08 +01001466}
1467
1468static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1469{
1470 struct perf_counter *counter = vma->vm_file->private_data;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001471 struct perf_mmap_data *data;
1472 int ret = VM_FAULT_SIGBUS;
Paul Mackerras37d81822009-03-23 18:22:08 +01001473
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001474 rcu_read_lock();
1475 data = rcu_dereference(counter->data);
1476 if (!data)
1477 goto unlock;
Paul Mackerras37d81822009-03-23 18:22:08 +01001478
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001479 if (vmf->pgoff == 0) {
1480 vmf->page = virt_to_page(data->user_page);
1481 } else {
1482 int nr = vmf->pgoff - 1;
1483
1484 if ((unsigned)nr > data->nr_pages)
1485 goto unlock;
1486
1487 vmf->page = virt_to_page(data->data_pages[nr]);
1488 }
Paul Mackerras37d81822009-03-23 18:22:08 +01001489 get_page(vmf->page);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001490 ret = 0;
1491unlock:
1492 rcu_read_unlock();
1493
1494 return ret;
1495}
1496
1497static int perf_mmap_data_alloc(struct perf_counter *counter, int nr_pages)
1498{
1499 struct perf_mmap_data *data;
1500 unsigned long size;
1501 int i;
1502
1503 WARN_ON(atomic_read(&counter->mmap_count));
1504
1505 size = sizeof(struct perf_mmap_data);
1506 size += nr_pages * sizeof(void *);
1507
1508 data = kzalloc(size, GFP_KERNEL);
1509 if (!data)
1510 goto fail;
1511
1512 data->user_page = (void *)get_zeroed_page(GFP_KERNEL);
1513 if (!data->user_page)
1514 goto fail_user_page;
1515
1516 for (i = 0; i < nr_pages; i++) {
1517 data->data_pages[i] = (void *)get_zeroed_page(GFP_KERNEL);
1518 if (!data->data_pages[i])
1519 goto fail_data_pages;
1520 }
1521
1522 data->nr_pages = nr_pages;
Peter Zijlstra22c15582009-05-05 17:50:25 +02001523 atomic_set(&data->lock, -1);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001524
1525 rcu_assign_pointer(counter->data, data);
1526
Paul Mackerras37d81822009-03-23 18:22:08 +01001527 return 0;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001528
1529fail_data_pages:
1530 for (i--; i >= 0; i--)
1531 free_page((unsigned long)data->data_pages[i]);
1532
1533 free_page((unsigned long)data->user_page);
1534
1535fail_user_page:
1536 kfree(data);
1537
1538fail:
1539 return -ENOMEM;
1540}
1541
1542static void __perf_mmap_data_free(struct rcu_head *rcu_head)
1543{
1544 struct perf_mmap_data *data = container_of(rcu_head,
1545 struct perf_mmap_data, rcu_head);
1546 int i;
1547
1548 free_page((unsigned long)data->user_page);
1549 for (i = 0; i < data->nr_pages; i++)
1550 free_page((unsigned long)data->data_pages[i]);
1551 kfree(data);
1552}
1553
1554static void perf_mmap_data_free(struct perf_counter *counter)
1555{
1556 struct perf_mmap_data *data = counter->data;
1557
1558 WARN_ON(atomic_read(&counter->mmap_count));
1559
1560 rcu_assign_pointer(counter->data, NULL);
1561 call_rcu(&data->rcu_head, __perf_mmap_data_free);
1562}
1563
1564static void perf_mmap_open(struct vm_area_struct *vma)
1565{
1566 struct perf_counter *counter = vma->vm_file->private_data;
1567
1568 atomic_inc(&counter->mmap_count);
1569}
1570
1571static void perf_mmap_close(struct vm_area_struct *vma)
1572{
1573 struct perf_counter *counter = vma->vm_file->private_data;
1574
1575 if (atomic_dec_and_mutex_lock(&counter->mmap_count,
1576 &counter->mmap_mutex)) {
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001577 struct user_struct *user = current_user();
1578
1579 atomic_long_sub(counter->data->nr_pages + 1, &user->locked_vm);
Peter Zijlstrac5078f72009-05-05 17:50:24 +02001580 vma->vm_mm->locked_vm -= counter->data->nr_locked;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001581 perf_mmap_data_free(counter);
1582 mutex_unlock(&counter->mmap_mutex);
1583 }
Paul Mackerras37d81822009-03-23 18:22:08 +01001584}
1585
1586static struct vm_operations_struct perf_mmap_vmops = {
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001587 .open = perf_mmap_open,
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001588 .close = perf_mmap_close,
Paul Mackerras37d81822009-03-23 18:22:08 +01001589 .fault = perf_mmap_fault,
1590};
1591
1592static int perf_mmap(struct file *file, struct vm_area_struct *vma)
1593{
1594 struct perf_counter *counter = file->private_data;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001595 struct user_struct *user = current_user();
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001596 unsigned long vma_size;
1597 unsigned long nr_pages;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001598 unsigned long user_locked, user_lock_limit;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001599 unsigned long locked, lock_limit;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001600 long user_extra, extra;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001601 int ret = 0;
Paul Mackerras37d81822009-03-23 18:22:08 +01001602
1603 if (!(vma->vm_flags & VM_SHARED) || (vma->vm_flags & VM_WRITE))
1604 return -EINVAL;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001605
1606 vma_size = vma->vm_end - vma->vm_start;
1607 nr_pages = (vma_size / PAGE_SIZE) - 1;
1608
Peter Zijlstra7730d862009-03-25 12:48:31 +01001609 /*
1610 * If we have data pages ensure they're a power-of-two number, so we
1611 * can do bitmasks instead of modulo.
1612 */
1613 if (nr_pages != 0 && !is_power_of_2(nr_pages))
Paul Mackerras37d81822009-03-23 18:22:08 +01001614 return -EINVAL;
1615
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001616 if (vma_size != PAGE_SIZE * (1 + nr_pages))
Paul Mackerras37d81822009-03-23 18:22:08 +01001617 return -EINVAL;
1618
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001619 if (vma->vm_pgoff != 0)
1620 return -EINVAL;
Paul Mackerras37d81822009-03-23 18:22:08 +01001621
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001622 mutex_lock(&counter->mmap_mutex);
1623 if (atomic_inc_not_zero(&counter->mmap_count)) {
1624 if (nr_pages != counter->data->nr_pages)
1625 ret = -EINVAL;
1626 goto unlock;
1627 }
1628
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001629 user_extra = nr_pages + 1;
1630 user_lock_limit = sysctl_perf_counter_mlock >> (PAGE_SHIFT - 10);
1631 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
Peter Zijlstrac5078f72009-05-05 17:50:24 +02001632
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001633 extra = 0;
1634 if (user_locked > user_lock_limit)
1635 extra = user_locked - user_lock_limit;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001636
1637 lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
1638 lock_limit >>= PAGE_SHIFT;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001639 locked = vma->vm_mm->locked_vm + extra;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001640
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001641 if ((locked > lock_limit) && !capable(CAP_IPC_LOCK)) {
1642 ret = -EPERM;
1643 goto unlock;
1644 }
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001645
1646 WARN_ON(counter->data);
1647 ret = perf_mmap_data_alloc(counter, nr_pages);
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001648 if (ret)
1649 goto unlock;
1650
1651 atomic_set(&counter->mmap_count, 1);
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001652 atomic_long_add(user_extra, &user->locked_vm);
Peter Zijlstrac5078f72009-05-05 17:50:24 +02001653 vma->vm_mm->locked_vm += extra;
1654 counter->data->nr_locked = extra;
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001655unlock:
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001656 mutex_unlock(&counter->mmap_mutex);
Paul Mackerras37d81822009-03-23 18:22:08 +01001657
1658 vma->vm_flags &= ~VM_MAYWRITE;
1659 vma->vm_flags |= VM_RESERVED;
1660 vma->vm_ops = &perf_mmap_vmops;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001661
1662 return ret;
Paul Mackerras37d81822009-03-23 18:22:08 +01001663}
1664
Peter Zijlstra3c446b3d2009-04-06 11:45:01 +02001665static int perf_fasync(int fd, struct file *filp, int on)
1666{
1667 struct perf_counter *counter = filp->private_data;
1668 struct inode *inode = filp->f_path.dentry->d_inode;
1669 int retval;
1670
1671 mutex_lock(&inode->i_mutex);
1672 retval = fasync_helper(fd, filp, on, &counter->fasync);
1673 mutex_unlock(&inode->i_mutex);
1674
1675 if (retval < 0)
1676 return retval;
1677
1678 return 0;
1679}
1680
Thomas Gleixner0793a612008-12-04 20:12:29 +01001681static const struct file_operations perf_fops = {
1682 .release = perf_release,
1683 .read = perf_read,
1684 .poll = perf_poll,
Paul Mackerrasd859e292009-01-17 18:10:22 +11001685 .unlocked_ioctl = perf_ioctl,
1686 .compat_ioctl = perf_ioctl,
Paul Mackerras37d81822009-03-23 18:22:08 +01001687 .mmap = perf_mmap,
Peter Zijlstra3c446b3d2009-04-06 11:45:01 +02001688 .fasync = perf_fasync,
Thomas Gleixner0793a612008-12-04 20:12:29 +01001689};
1690
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001691/*
Peter Zijlstra925d5192009-03-30 19:07:02 +02001692 * Perf counter wakeup
1693 *
1694 * If there's data, ensure we set the poll() state and publish everything
1695 * to user-space before waking everybody up.
1696 */
1697
1698void perf_counter_wakeup(struct perf_counter *counter)
1699{
Peter Zijlstra925d5192009-03-30 19:07:02 +02001700 wake_up_all(&counter->waitq);
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02001701
1702 if (counter->pending_kill) {
1703 kill_fasync(&counter->fasync, SIGIO, counter->pending_kill);
1704 counter->pending_kill = 0;
1705 }
Peter Zijlstra925d5192009-03-30 19:07:02 +02001706}
1707
1708/*
1709 * Pending wakeups
1710 *
1711 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
1712 *
1713 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
1714 * single linked list and use cmpxchg() to add entries lockless.
1715 */
1716
Peter Zijlstra79f14642009-04-06 11:45:07 +02001717static void perf_pending_counter(struct perf_pending_entry *entry)
1718{
1719 struct perf_counter *counter = container_of(entry,
1720 struct perf_counter, pending);
1721
1722 if (counter->pending_disable) {
1723 counter->pending_disable = 0;
1724 perf_counter_disable(counter);
1725 }
1726
1727 if (counter->pending_wakeup) {
1728 counter->pending_wakeup = 0;
1729 perf_counter_wakeup(counter);
1730 }
1731}
1732
Peter Zijlstra671dec52009-04-06 11:45:02 +02001733#define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
Peter Zijlstra925d5192009-03-30 19:07:02 +02001734
Peter Zijlstra671dec52009-04-06 11:45:02 +02001735static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = {
Peter Zijlstra925d5192009-03-30 19:07:02 +02001736 PENDING_TAIL,
1737};
1738
Peter Zijlstra671dec52009-04-06 11:45:02 +02001739static void perf_pending_queue(struct perf_pending_entry *entry,
1740 void (*func)(struct perf_pending_entry *))
Peter Zijlstra925d5192009-03-30 19:07:02 +02001741{
Peter Zijlstra671dec52009-04-06 11:45:02 +02001742 struct perf_pending_entry **head;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001743
Peter Zijlstra671dec52009-04-06 11:45:02 +02001744 if (cmpxchg(&entry->next, NULL, PENDING_TAIL) != NULL)
Peter Zijlstra925d5192009-03-30 19:07:02 +02001745 return;
1746
Peter Zijlstra671dec52009-04-06 11:45:02 +02001747 entry->func = func;
1748
1749 head = &get_cpu_var(perf_pending_head);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001750
1751 do {
Peter Zijlstra671dec52009-04-06 11:45:02 +02001752 entry->next = *head;
1753 } while (cmpxchg(head, entry->next, entry) != entry->next);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001754
1755 set_perf_counter_pending();
1756
Peter Zijlstra671dec52009-04-06 11:45:02 +02001757 put_cpu_var(perf_pending_head);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001758}
1759
1760static int __perf_pending_run(void)
1761{
Peter Zijlstra671dec52009-04-06 11:45:02 +02001762 struct perf_pending_entry *list;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001763 int nr = 0;
1764
Peter Zijlstra671dec52009-04-06 11:45:02 +02001765 list = xchg(&__get_cpu_var(perf_pending_head), PENDING_TAIL);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001766 while (list != PENDING_TAIL) {
Peter Zijlstra671dec52009-04-06 11:45:02 +02001767 void (*func)(struct perf_pending_entry *);
1768 struct perf_pending_entry *entry = list;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001769
1770 list = list->next;
1771
Peter Zijlstra671dec52009-04-06 11:45:02 +02001772 func = entry->func;
1773 entry->next = NULL;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001774 /*
1775 * Ensure we observe the unqueue before we issue the wakeup,
1776 * so that we won't be waiting forever.
1777 * -- see perf_not_pending().
1778 */
1779 smp_wmb();
1780
Peter Zijlstra671dec52009-04-06 11:45:02 +02001781 func(entry);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001782 nr++;
1783 }
1784
1785 return nr;
1786}
1787
1788static inline int perf_not_pending(struct perf_counter *counter)
1789{
1790 /*
1791 * If we flush on whatever cpu we run, there is a chance we don't
1792 * need to wait.
1793 */
1794 get_cpu();
1795 __perf_pending_run();
1796 put_cpu();
1797
1798 /*
1799 * Ensure we see the proper queue state before going to sleep
1800 * so that we do not miss the wakeup. -- see perf_pending_handle()
1801 */
1802 smp_rmb();
Peter Zijlstra671dec52009-04-06 11:45:02 +02001803 return counter->pending.next == NULL;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001804}
1805
1806static void perf_pending_sync(struct perf_counter *counter)
1807{
1808 wait_event(counter->waitq, perf_not_pending(counter));
1809}
1810
1811void perf_counter_do_pending(void)
1812{
1813 __perf_pending_run();
1814}
1815
1816/*
Peter Zijlstra394ee072009-03-30 19:07:14 +02001817 * Callchain support -- arch specific
1818 */
1819
Peter Zijlstra9c03d882009-04-06 11:45:00 +02001820__weak struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
Peter Zijlstra394ee072009-03-30 19:07:14 +02001821{
1822 return NULL;
1823}
1824
1825/*
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001826 * Output
1827 */
1828
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001829struct perf_output_handle {
1830 struct perf_counter *counter;
1831 struct perf_mmap_data *data;
1832 unsigned int offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01001833 unsigned int head;
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001834 int nmi;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02001835 int overflow;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001836 int locked;
1837 unsigned long flags;
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001838};
1839
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001840static void perf_output_wakeup(struct perf_output_handle *handle)
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001841{
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001842 atomic_set(&handle->data->poll, POLL_IN);
1843
Peter Zijlstra671dec52009-04-06 11:45:02 +02001844 if (handle->nmi) {
Peter Zijlstra79f14642009-04-06 11:45:07 +02001845 handle->counter->pending_wakeup = 1;
Peter Zijlstra671dec52009-04-06 11:45:02 +02001846 perf_pending_queue(&handle->counter->pending,
Peter Zijlstra79f14642009-04-06 11:45:07 +02001847 perf_pending_counter);
Peter Zijlstra671dec52009-04-06 11:45:02 +02001848 } else
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001849 perf_counter_wakeup(handle->counter);
1850}
1851
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001852/*
1853 * Curious locking construct.
1854 *
1855 * We need to ensure a later event doesn't publish a head when a former
1856 * event isn't done writing. However since we need to deal with NMIs we
1857 * cannot fully serialize things.
1858 *
1859 * What we do is serialize between CPUs so we only have to deal with NMI
1860 * nesting on a single CPU.
1861 *
1862 * We only publish the head (and generate a wakeup) when the outer-most
1863 * event completes.
1864 */
1865static void perf_output_lock(struct perf_output_handle *handle)
1866{
1867 struct perf_mmap_data *data = handle->data;
1868 int cpu;
1869
1870 handle->locked = 0;
1871
1872 local_irq_save(handle->flags);
1873 cpu = smp_processor_id();
1874
1875 if (in_nmi() && atomic_read(&data->lock) == cpu)
1876 return;
1877
Peter Zijlstra22c15582009-05-05 17:50:25 +02001878 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001879 cpu_relax();
1880
1881 handle->locked = 1;
1882}
1883
1884static void perf_output_unlock(struct perf_output_handle *handle)
1885{
1886 struct perf_mmap_data *data = handle->data;
1887 int head, cpu;
1888
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02001889 data->done_head = data->head;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001890
1891 if (!handle->locked)
1892 goto out;
1893
1894again:
1895 /*
1896 * The xchg implies a full barrier that ensures all writes are done
1897 * before we publish the new head, matched by a rmb() in userspace when
1898 * reading this position.
1899 */
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02001900 while ((head = atomic_xchg(&data->done_head, 0)))
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001901 data->user_page->data_head = head;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001902
1903 /*
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02001904 * NMI can happen here, which means we can miss a done_head update.
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001905 */
1906
Peter Zijlstra22c15582009-05-05 17:50:25 +02001907 cpu = atomic_xchg(&data->lock, -1);
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001908 WARN_ON_ONCE(cpu != smp_processor_id());
1909
1910 /*
1911 * Therefore we have to validate we did not indeed do so.
1912 */
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02001913 if (unlikely(atomic_read(&data->done_head))) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001914 /*
1915 * Since we had it locked, we can lock it again.
1916 */
Peter Zijlstra22c15582009-05-05 17:50:25 +02001917 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001918 cpu_relax();
1919
1920 goto again;
1921 }
1922
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02001923 if (atomic_xchg(&data->wakeup, 0))
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001924 perf_output_wakeup(handle);
1925out:
1926 local_irq_restore(handle->flags);
1927}
1928
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001929static int perf_output_begin(struct perf_output_handle *handle,
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001930 struct perf_counter *counter, unsigned int size,
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02001931 int nmi, int overflow)
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001932{
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001933 struct perf_mmap_data *data;
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001934 unsigned int offset, head;
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001935
Peter Zijlstra2023b352009-05-05 17:50:26 +02001936 /*
1937 * For inherited counters we send all the output towards the parent.
1938 */
1939 if (counter->parent)
1940 counter = counter->parent;
1941
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001942 rcu_read_lock();
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001943 data = rcu_dereference(counter->data);
1944 if (!data)
1945 goto out;
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001946
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001947 handle->data = data;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02001948 handle->counter = counter;
1949 handle->nmi = nmi;
1950 handle->overflow = overflow;
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001951
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001952 if (!data->nr_pages)
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001953 goto fail;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001954
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001955 perf_output_lock(handle);
1956
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001957 do {
1958 offset = head = atomic_read(&data->head);
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001959 head += size;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001960 } while (atomic_cmpxchg(&data->head, offset, head) != offset);
1961
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001962 handle->offset = offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01001963 handle->head = head;
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02001964
1965 if ((offset >> PAGE_SHIFT) != (head >> PAGE_SHIFT))
1966 atomic_set(&data->wakeup, 1);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001967
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001968 return 0;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001969
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001970fail:
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001971 perf_output_wakeup(handle);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001972out:
1973 rcu_read_unlock();
1974
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001975 return -ENOSPC;
1976}
1977
1978static void perf_output_copy(struct perf_output_handle *handle,
1979 void *buf, unsigned int len)
1980{
1981 unsigned int pages_mask;
1982 unsigned int offset;
1983 unsigned int size;
1984 void **pages;
1985
1986 offset = handle->offset;
1987 pages_mask = handle->data->nr_pages - 1;
1988 pages = handle->data->data_pages;
1989
1990 do {
1991 unsigned int page_offset;
1992 int nr;
1993
1994 nr = (offset >> PAGE_SHIFT) & pages_mask;
1995 page_offset = offset & (PAGE_SIZE - 1);
1996 size = min_t(unsigned int, PAGE_SIZE - page_offset, len);
1997
1998 memcpy(pages[nr] + page_offset, buf, size);
1999
2000 len -= size;
2001 buf += size;
2002 offset += size;
2003 } while (len);
2004
2005 handle->offset = offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01002006
Peter Zijlstra53020fe2009-05-13 21:26:19 +02002007 /*
2008 * Check we didn't copy past our reservation window, taking the
2009 * possible unsigned int wrap into account.
2010 */
2011 WARN_ON_ONCE(((int)(handle->head - handle->offset)) < 0);
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002012}
2013
Peter Zijlstra5c148192009-03-25 12:30:23 +01002014#define perf_output_put(handle, x) \
2015 perf_output_copy((handle), &(x), sizeof(x))
2016
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002017static void perf_output_end(struct perf_output_handle *handle)
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002018{
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002019 struct perf_counter *counter = handle->counter;
2020 struct perf_mmap_data *data = handle->data;
2021
2022 int wakeup_events = counter->hw_event.wakeup_events;
Peter Zijlstrac4578102009-04-02 11:12:01 +02002023
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002024 if (handle->overflow && wakeup_events) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002025 int events = atomic_inc_return(&data->events);
Peter Zijlstrac4578102009-04-02 11:12:01 +02002026 if (events >= wakeup_events) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002027 atomic_sub(wakeup_events, &data->events);
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002028 atomic_set(&data->wakeup, 1);
Peter Zijlstrac4578102009-04-02 11:12:01 +02002029 }
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002030 }
2031
2032 perf_output_unlock(handle);
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002033 rcu_read_unlock();
2034}
2035
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002036static void perf_counter_output(struct perf_counter *counter,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002037 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002038{
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002039 int ret;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002040 u64 record_type = counter->hw_event.record_type;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002041 struct perf_output_handle handle;
2042 struct perf_event_header header;
2043 u64 ip;
Peter Zijlstra5c148192009-03-25 12:30:23 +01002044 struct {
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002045 u32 pid, tid;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002046 } tid_entry;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002047 struct {
2048 u64 event;
2049 u64 counter;
2050 } group_entry;
Peter Zijlstra394ee072009-03-30 19:07:14 +02002051 struct perf_callchain_entry *callchain = NULL;
2052 int callchain_size = 0;
Peter Zijlstra339f7c92009-04-06 11:45:06 +02002053 u64 time;
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002054 struct {
2055 u32 cpu, reserved;
2056 } cpu_entry;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002057
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02002058 header.type = 0;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002059 header.size = sizeof(header);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002060
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02002061 header.misc = PERF_EVENT_MISC_OVERFLOW;
Paul Mackerras9d23a902009-05-14 21:48:08 +10002062 header.misc |= perf_misc_flags(regs);
Peter Zijlstra6fab0192009-04-08 15:01:26 +02002063
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002064 if (record_type & PERF_RECORD_IP) {
Paul Mackerras9d23a902009-05-14 21:48:08 +10002065 ip = perf_instruction_pointer(regs);
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02002066 header.type |= PERF_RECORD_IP;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002067 header.size += sizeof(ip);
2068 }
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002069
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002070 if (record_type & PERF_RECORD_TID) {
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002071 /* namespace issues */
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002072 tid_entry.pid = current->group_leader->pid;
2073 tid_entry.tid = current->pid;
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002074
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02002075 header.type |= PERF_RECORD_TID;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002076 header.size += sizeof(tid_entry);
2077 }
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002078
Peter Zijlstra4d855452009-04-08 15:01:32 +02002079 if (record_type & PERF_RECORD_TIME) {
2080 /*
2081 * Maybe do better on x86 and provide cpu_clock_nmi()
2082 */
2083 time = sched_clock();
2084
2085 header.type |= PERF_RECORD_TIME;
2086 header.size += sizeof(u64);
2087 }
2088
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002089 if (record_type & PERF_RECORD_ADDR) {
2090 header.type |= PERF_RECORD_ADDR;
2091 header.size += sizeof(u64);
2092 }
2093
Peter Zijlstraa85f61a2009-05-08 18:52:23 +02002094 if (record_type & PERF_RECORD_CONFIG) {
2095 header.type |= PERF_RECORD_CONFIG;
2096 header.size += sizeof(u64);
2097 }
2098
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002099 if (record_type & PERF_RECORD_CPU) {
2100 header.type |= PERF_RECORD_CPU;
2101 header.size += sizeof(cpu_entry);
2102
2103 cpu_entry.cpu = raw_smp_processor_id();
2104 }
2105
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002106 if (record_type & PERF_RECORD_GROUP) {
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02002107 header.type |= PERF_RECORD_GROUP;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002108 header.size += sizeof(u64) +
2109 counter->nr_siblings * sizeof(group_entry);
2110 }
2111
2112 if (record_type & PERF_RECORD_CALLCHAIN) {
Peter Zijlstra394ee072009-03-30 19:07:14 +02002113 callchain = perf_callchain(regs);
2114
2115 if (callchain) {
Peter Zijlstra9c03d882009-04-06 11:45:00 +02002116 callchain_size = (1 + callchain->nr) * sizeof(u64);
Peter Zijlstra394ee072009-03-30 19:07:14 +02002117
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02002118 header.type |= PERF_RECORD_CALLCHAIN;
Peter Zijlstra394ee072009-03-30 19:07:14 +02002119 header.size += callchain_size;
2120 }
2121 }
2122
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002123 ret = perf_output_begin(&handle, counter, header.size, nmi, 1);
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002124 if (ret)
2125 return;
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002126
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002127 perf_output_put(&handle, header);
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002128
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002129 if (record_type & PERF_RECORD_IP)
2130 perf_output_put(&handle, ip);
2131
2132 if (record_type & PERF_RECORD_TID)
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002133 perf_output_put(&handle, tid_entry);
2134
Peter Zijlstra4d855452009-04-08 15:01:32 +02002135 if (record_type & PERF_RECORD_TIME)
2136 perf_output_put(&handle, time);
2137
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002138 if (record_type & PERF_RECORD_ADDR)
2139 perf_output_put(&handle, addr);
2140
Peter Zijlstraa85f61a2009-05-08 18:52:23 +02002141 if (record_type & PERF_RECORD_CONFIG)
2142 perf_output_put(&handle, counter->hw_event.config);
2143
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002144 if (record_type & PERF_RECORD_CPU)
2145 perf_output_put(&handle, cpu_entry);
2146
Peter Zijlstra2023b352009-05-05 17:50:26 +02002147 /*
2148 * XXX PERF_RECORD_GROUP vs inherited counters seems difficult.
2149 */
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002150 if (record_type & PERF_RECORD_GROUP) {
2151 struct perf_counter *leader, *sub;
2152 u64 nr = counter->nr_siblings;
2153
2154 perf_output_put(&handle, nr);
2155
2156 leader = counter->group_leader;
2157 list_for_each_entry(sub, &leader->sibling_list, list_entry) {
2158 if (sub != counter)
Robert Richter4aeb0b42009-04-29 12:47:03 +02002159 sub->pmu->read(sub);
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002160
2161 group_entry.event = sub->hw_event.config;
2162 group_entry.counter = atomic64_read(&sub->count);
2163
2164 perf_output_put(&handle, group_entry);
2165 }
2166 }
2167
Peter Zijlstra394ee072009-03-30 19:07:14 +02002168 if (callchain)
2169 perf_output_copy(&handle, callchain, callchain_size);
2170
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002171 perf_output_end(&handle);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002172}
2173
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002174/*
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002175 * comm tracking
2176 */
2177
2178struct perf_comm_event {
2179 struct task_struct *task;
2180 char *comm;
2181 int comm_size;
2182
2183 struct {
2184 struct perf_event_header header;
2185
2186 u32 pid;
2187 u32 tid;
2188 } event;
2189};
2190
2191static void perf_counter_comm_output(struct perf_counter *counter,
2192 struct perf_comm_event *comm_event)
2193{
2194 struct perf_output_handle handle;
2195 int size = comm_event->event.header.size;
2196 int ret = perf_output_begin(&handle, counter, size, 0, 0);
2197
2198 if (ret)
2199 return;
2200
2201 perf_output_put(&handle, comm_event->event);
2202 perf_output_copy(&handle, comm_event->comm,
2203 comm_event->comm_size);
2204 perf_output_end(&handle);
2205}
2206
2207static int perf_counter_comm_match(struct perf_counter *counter,
2208 struct perf_comm_event *comm_event)
2209{
2210 if (counter->hw_event.comm &&
2211 comm_event->event.header.type == PERF_EVENT_COMM)
2212 return 1;
2213
2214 return 0;
2215}
2216
2217static void perf_counter_comm_ctx(struct perf_counter_context *ctx,
2218 struct perf_comm_event *comm_event)
2219{
2220 struct perf_counter *counter;
2221
2222 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2223 return;
2224
2225 rcu_read_lock();
2226 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2227 if (perf_counter_comm_match(counter, comm_event))
2228 perf_counter_comm_output(counter, comm_event);
2229 }
2230 rcu_read_unlock();
2231}
2232
2233static void perf_counter_comm_event(struct perf_comm_event *comm_event)
2234{
2235 struct perf_cpu_context *cpuctx;
2236 unsigned int size;
2237 char *comm = comm_event->task->comm;
2238
Ingo Molnar888fcee2009-04-09 09:48:22 +02002239 size = ALIGN(strlen(comm)+1, sizeof(u64));
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002240
2241 comm_event->comm = comm;
2242 comm_event->comm_size = size;
2243
2244 comm_event->event.header.size = sizeof(comm_event->event) + size;
2245
2246 cpuctx = &get_cpu_var(perf_cpu_context);
2247 perf_counter_comm_ctx(&cpuctx->ctx, comm_event);
2248 put_cpu_var(perf_cpu_context);
2249
2250 perf_counter_comm_ctx(&current->perf_counter_ctx, comm_event);
2251}
2252
2253void perf_counter_comm(struct task_struct *task)
2254{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002255 struct perf_comm_event comm_event;
2256
2257 if (!atomic_read(&nr_comm_tracking))
2258 return;
2259
2260 comm_event = (struct perf_comm_event){
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002261 .task = task,
2262 .event = {
2263 .header = { .type = PERF_EVENT_COMM, },
2264 .pid = task->group_leader->pid,
2265 .tid = task->pid,
2266 },
2267 };
2268
2269 perf_counter_comm_event(&comm_event);
2270}
2271
2272/*
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002273 * mmap tracking
2274 */
2275
2276struct perf_mmap_event {
2277 struct file *file;
2278 char *file_name;
2279 int file_size;
2280
2281 struct {
2282 struct perf_event_header header;
2283
2284 u32 pid;
2285 u32 tid;
2286 u64 start;
2287 u64 len;
2288 u64 pgoff;
2289 } event;
2290};
2291
2292static void perf_counter_mmap_output(struct perf_counter *counter,
2293 struct perf_mmap_event *mmap_event)
2294{
2295 struct perf_output_handle handle;
2296 int size = mmap_event->event.header.size;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002297 int ret = perf_output_begin(&handle, counter, size, 0, 0);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002298
2299 if (ret)
2300 return;
2301
2302 perf_output_put(&handle, mmap_event->event);
2303 perf_output_copy(&handle, mmap_event->file_name,
2304 mmap_event->file_size);
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002305 perf_output_end(&handle);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002306}
2307
2308static int perf_counter_mmap_match(struct perf_counter *counter,
2309 struct perf_mmap_event *mmap_event)
2310{
2311 if (counter->hw_event.mmap &&
2312 mmap_event->event.header.type == PERF_EVENT_MMAP)
2313 return 1;
2314
2315 if (counter->hw_event.munmap &&
2316 mmap_event->event.header.type == PERF_EVENT_MUNMAP)
2317 return 1;
2318
2319 return 0;
2320}
2321
2322static void perf_counter_mmap_ctx(struct perf_counter_context *ctx,
2323 struct perf_mmap_event *mmap_event)
2324{
2325 struct perf_counter *counter;
2326
2327 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2328 return;
2329
2330 rcu_read_lock();
2331 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2332 if (perf_counter_mmap_match(counter, mmap_event))
2333 perf_counter_mmap_output(counter, mmap_event);
2334 }
2335 rcu_read_unlock();
2336}
2337
2338static void perf_counter_mmap_event(struct perf_mmap_event *mmap_event)
2339{
2340 struct perf_cpu_context *cpuctx;
2341 struct file *file = mmap_event->file;
2342 unsigned int size;
2343 char tmp[16];
2344 char *buf = NULL;
2345 char *name;
2346
2347 if (file) {
2348 buf = kzalloc(PATH_MAX, GFP_KERNEL);
2349 if (!buf) {
2350 name = strncpy(tmp, "//enomem", sizeof(tmp));
2351 goto got_name;
2352 }
Peter Zijlstrad3d21c42009-04-09 10:53:46 +02002353 name = d_path(&file->f_path, buf, PATH_MAX);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002354 if (IS_ERR(name)) {
2355 name = strncpy(tmp, "//toolong", sizeof(tmp));
2356 goto got_name;
2357 }
2358 } else {
2359 name = strncpy(tmp, "//anon", sizeof(tmp));
2360 goto got_name;
2361 }
2362
2363got_name:
Ingo Molnar888fcee2009-04-09 09:48:22 +02002364 size = ALIGN(strlen(name)+1, sizeof(u64));
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002365
2366 mmap_event->file_name = name;
2367 mmap_event->file_size = size;
2368
2369 mmap_event->event.header.size = sizeof(mmap_event->event) + size;
2370
2371 cpuctx = &get_cpu_var(perf_cpu_context);
2372 perf_counter_mmap_ctx(&cpuctx->ctx, mmap_event);
2373 put_cpu_var(perf_cpu_context);
2374
2375 perf_counter_mmap_ctx(&current->perf_counter_ctx, mmap_event);
2376
2377 kfree(buf);
2378}
2379
2380void perf_counter_mmap(unsigned long addr, unsigned long len,
2381 unsigned long pgoff, struct file *file)
2382{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002383 struct perf_mmap_event mmap_event;
2384
2385 if (!atomic_read(&nr_mmap_tracking))
2386 return;
2387
2388 mmap_event = (struct perf_mmap_event){
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002389 .file = file,
2390 .event = {
2391 .header = { .type = PERF_EVENT_MMAP, },
2392 .pid = current->group_leader->pid,
2393 .tid = current->pid,
2394 .start = addr,
2395 .len = len,
2396 .pgoff = pgoff,
2397 },
2398 };
2399
2400 perf_counter_mmap_event(&mmap_event);
2401}
2402
2403void perf_counter_munmap(unsigned long addr, unsigned long len,
2404 unsigned long pgoff, struct file *file)
2405{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002406 struct perf_mmap_event mmap_event;
2407
2408 if (!atomic_read(&nr_munmap_tracking))
2409 return;
2410
2411 mmap_event = (struct perf_mmap_event){
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002412 .file = file,
2413 .event = {
2414 .header = { .type = PERF_EVENT_MUNMAP, },
2415 .pid = current->group_leader->pid,
2416 .tid = current->pid,
2417 .start = addr,
2418 .len = len,
2419 .pgoff = pgoff,
2420 },
2421 };
2422
2423 perf_counter_mmap_event(&mmap_event);
2424}
2425
2426/*
Peter Zijlstra26b119b2009-05-20 12:21:20 +02002427 *
2428 */
2429
2430static void perf_log_period(struct perf_counter *counter, u64 period)
2431{
2432 struct perf_output_handle handle;
2433 int ret;
2434
2435 struct {
2436 struct perf_event_header header;
2437 u64 time;
2438 u64 period;
2439 } freq_event = {
2440 .header = {
2441 .type = PERF_EVENT_PERIOD,
2442 .misc = 0,
2443 .size = sizeof(freq_event),
2444 },
2445 .time = sched_clock(),
2446 .period = period,
2447 };
2448
2449 if (counter->hw.irq_period == period)
2450 return;
2451
2452 ret = perf_output_begin(&handle, counter, sizeof(freq_event), 0, 0);
2453 if (ret)
2454 return;
2455
2456 perf_output_put(&handle, freq_event);
2457 perf_output_end(&handle);
2458}
2459
2460/*
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002461 * Generic counter overflow handling.
2462 */
2463
2464int perf_counter_overflow(struct perf_counter *counter,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002465 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002466{
Peter Zijlstra79f14642009-04-06 11:45:07 +02002467 int events = atomic_read(&counter->event_limit);
2468 int ret = 0;
2469
Peter Zijlstra60db5e02009-05-15 15:19:28 +02002470 counter->hw.interrupts++;
2471
Peter Zijlstra2023b352009-05-05 17:50:26 +02002472 /*
2473 * XXX event_limit might not quite work as expected on inherited
2474 * counters
2475 */
2476
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002477 counter->pending_kill = POLL_IN;
Peter Zijlstra79f14642009-04-06 11:45:07 +02002478 if (events && atomic_dec_and_test(&counter->event_limit)) {
2479 ret = 1;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002480 counter->pending_kill = POLL_HUP;
Peter Zijlstra79f14642009-04-06 11:45:07 +02002481 if (nmi) {
2482 counter->pending_disable = 1;
2483 perf_pending_queue(&counter->pending,
2484 perf_pending_counter);
2485 } else
2486 perf_counter_disable(counter);
2487 }
2488
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002489 perf_counter_output(counter, nmi, regs, addr);
Peter Zijlstra79f14642009-04-06 11:45:07 +02002490 return ret;
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002491}
2492
2493/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002494 * Generic software counter infrastructure
2495 */
2496
2497static void perf_swcounter_update(struct perf_counter *counter)
2498{
2499 struct hw_perf_counter *hwc = &counter->hw;
2500 u64 prev, now;
2501 s64 delta;
2502
2503again:
2504 prev = atomic64_read(&hwc->prev_count);
2505 now = atomic64_read(&hwc->count);
2506 if (atomic64_cmpxchg(&hwc->prev_count, prev, now) != prev)
2507 goto again;
2508
2509 delta = now - prev;
2510
2511 atomic64_add(delta, &counter->count);
2512 atomic64_sub(delta, &hwc->period_left);
2513}
2514
2515static void perf_swcounter_set_period(struct perf_counter *counter)
2516{
2517 struct hw_perf_counter *hwc = &counter->hw;
2518 s64 left = atomic64_read(&hwc->period_left);
2519 s64 period = hwc->irq_period;
2520
2521 if (unlikely(left <= -period)) {
2522 left = period;
2523 atomic64_set(&hwc->period_left, left);
2524 }
2525
2526 if (unlikely(left <= 0)) {
2527 left += period;
2528 atomic64_add(period, &hwc->period_left);
2529 }
2530
2531 atomic64_set(&hwc->prev_count, -left);
2532 atomic64_set(&hwc->count, -left);
2533}
2534
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002535static enum hrtimer_restart perf_swcounter_hrtimer(struct hrtimer *hrtimer)
2536{
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002537 enum hrtimer_restart ret = HRTIMER_RESTART;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002538 struct perf_counter *counter;
2539 struct pt_regs *regs;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02002540 u64 period;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002541
2542 counter = container_of(hrtimer, struct perf_counter, hw.hrtimer);
Robert Richter4aeb0b42009-04-29 12:47:03 +02002543 counter->pmu->read(counter);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002544
2545 regs = get_irq_regs();
2546 /*
2547 * In case we exclude kernel IPs or are somehow not in interrupt
2548 * context, provide the next best thing, the user IP.
2549 */
2550 if ((counter->hw_event.exclude_kernel || !regs) &&
2551 !counter->hw_event.exclude_user)
2552 regs = task_pt_regs(current);
2553
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002554 if (regs) {
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002555 if (perf_counter_overflow(counter, 0, regs, 0))
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002556 ret = HRTIMER_NORESTART;
2557 }
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002558
Peter Zijlstra60db5e02009-05-15 15:19:28 +02002559 period = max_t(u64, 10000, counter->hw.irq_period);
2560 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002561
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002562 return ret;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002563}
2564
2565static void perf_swcounter_overflow(struct perf_counter *counter,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002566 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002567{
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002568 perf_swcounter_update(counter);
2569 perf_swcounter_set_period(counter);
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002570 if (perf_counter_overflow(counter, nmi, regs, addr))
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002571 /* soft-disable the counter */
2572 ;
2573
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002574}
2575
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002576static int perf_swcounter_match(struct perf_counter *counter,
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002577 enum perf_event_types type,
2578 u32 event, struct pt_regs *regs)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002579{
2580 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
2581 return 0;
2582
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002583 if (perf_event_raw(&counter->hw_event))
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002584 return 0;
2585
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002586 if (perf_event_type(&counter->hw_event) != type)
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002587 return 0;
2588
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002589 if (perf_event_id(&counter->hw_event) != event)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002590 return 0;
2591
2592 if (counter->hw_event.exclude_user && user_mode(regs))
2593 return 0;
2594
2595 if (counter->hw_event.exclude_kernel && !user_mode(regs))
2596 return 0;
2597
2598 return 1;
2599}
2600
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002601static void perf_swcounter_add(struct perf_counter *counter, u64 nr,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002602 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002603{
2604 int neg = atomic64_add_negative(nr, &counter->hw.count);
2605 if (counter->hw.irq_period && !neg)
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002606 perf_swcounter_overflow(counter, nmi, regs, addr);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002607}
2608
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002609static void perf_swcounter_ctx_event(struct perf_counter_context *ctx,
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002610 enum perf_event_types type, u32 event,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002611 u64 nr, int nmi, struct pt_regs *regs,
2612 u64 addr)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002613{
2614 struct perf_counter *counter;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002615
Peter Zijlstra01ef09d2009-03-19 20:26:11 +01002616 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002617 return;
2618
Peter Zijlstra592903c2009-03-13 12:21:36 +01002619 rcu_read_lock();
2620 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002621 if (perf_swcounter_match(counter, type, event, regs))
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002622 perf_swcounter_add(counter, nr, nmi, regs, addr);
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002623 }
Peter Zijlstra592903c2009-03-13 12:21:36 +01002624 rcu_read_unlock();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002625}
2626
Peter Zijlstra96f6d442009-03-23 18:22:07 +01002627static int *perf_swcounter_recursion_context(struct perf_cpu_context *cpuctx)
2628{
2629 if (in_nmi())
2630 return &cpuctx->recursion[3];
2631
2632 if (in_irq())
2633 return &cpuctx->recursion[2];
2634
2635 if (in_softirq())
2636 return &cpuctx->recursion[1];
2637
2638 return &cpuctx->recursion[0];
2639}
2640
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002641static void __perf_swcounter_event(enum perf_event_types type, u32 event,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002642 u64 nr, int nmi, struct pt_regs *regs,
2643 u64 addr)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002644{
2645 struct perf_cpu_context *cpuctx = &get_cpu_var(perf_cpu_context);
Peter Zijlstra96f6d442009-03-23 18:22:07 +01002646 int *recursion = perf_swcounter_recursion_context(cpuctx);
2647
2648 if (*recursion)
2649 goto out;
2650
2651 (*recursion)++;
2652 barrier();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002653
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002654 perf_swcounter_ctx_event(&cpuctx->ctx, type, event,
2655 nr, nmi, regs, addr);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002656 if (cpuctx->task_ctx) {
2657 perf_swcounter_ctx_event(cpuctx->task_ctx, type, event,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002658 nr, nmi, regs, addr);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002659 }
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002660
Peter Zijlstra96f6d442009-03-23 18:22:07 +01002661 barrier();
2662 (*recursion)--;
2663
2664out:
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002665 put_cpu_var(perf_cpu_context);
2666}
2667
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002668void
2669perf_swcounter_event(u32 event, u64 nr, int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002670{
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002671 __perf_swcounter_event(PERF_TYPE_SOFTWARE, event, nr, nmi, regs, addr);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002672}
2673
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002674static void perf_swcounter_read(struct perf_counter *counter)
2675{
2676 perf_swcounter_update(counter);
2677}
2678
2679static int perf_swcounter_enable(struct perf_counter *counter)
2680{
2681 perf_swcounter_set_period(counter);
2682 return 0;
2683}
2684
2685static void perf_swcounter_disable(struct perf_counter *counter)
2686{
2687 perf_swcounter_update(counter);
2688}
2689
Robert Richter4aeb0b42009-04-29 12:47:03 +02002690static const struct pmu perf_ops_generic = {
Peter Zijlstraac17dc82009-03-13 12:21:34 +01002691 .enable = perf_swcounter_enable,
2692 .disable = perf_swcounter_disable,
2693 .read = perf_swcounter_read,
2694};
2695
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002696/*
2697 * Software counter: cpu wall time clock
2698 */
2699
Paul Mackerras9abf8a02009-01-09 16:26:43 +11002700static void cpu_clock_perf_counter_update(struct perf_counter *counter)
2701{
2702 int cpu = raw_smp_processor_id();
2703 s64 prev;
2704 u64 now;
2705
2706 now = cpu_clock(cpu);
2707 prev = atomic64_read(&counter->hw.prev_count);
2708 atomic64_set(&counter->hw.prev_count, now);
2709 atomic64_add(now - prev, &counter->count);
2710}
2711
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002712static int cpu_clock_perf_counter_enable(struct perf_counter *counter)
2713{
2714 struct hw_perf_counter *hwc = &counter->hw;
2715 int cpu = raw_smp_processor_id();
2716
2717 atomic64_set(&hwc->prev_count, cpu_clock(cpu));
Peter Zijlstra039fc912009-03-13 16:43:47 +01002718 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2719 hwc->hrtimer.function = perf_swcounter_hrtimer;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002720 if (hwc->irq_period) {
Peter Zijlstra60db5e02009-05-15 15:19:28 +02002721 u64 period = max_t(u64, 10000, hwc->irq_period);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002722 __hrtimer_start_range_ns(&hwc->hrtimer,
Peter Zijlstra60db5e02009-05-15 15:19:28 +02002723 ns_to_ktime(period), 0,
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002724 HRTIMER_MODE_REL, 0);
2725 }
2726
2727 return 0;
2728}
2729
Ingo Molnar5c92d122008-12-11 13:21:10 +01002730static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
2731{
Peter Zijlstrab986d7e2009-05-20 12:21:21 +02002732 if (counter->hw.irq_period)
2733 hrtimer_cancel(&counter->hw.hrtimer);
Paul Mackerras9abf8a02009-01-09 16:26:43 +11002734 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01002735}
2736
2737static void cpu_clock_perf_counter_read(struct perf_counter *counter)
2738{
Paul Mackerras9abf8a02009-01-09 16:26:43 +11002739 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01002740}
2741
Robert Richter4aeb0b42009-04-29 12:47:03 +02002742static const struct pmu perf_ops_cpu_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +01002743 .enable = cpu_clock_perf_counter_enable,
2744 .disable = cpu_clock_perf_counter_disable,
2745 .read = cpu_clock_perf_counter_read,
Ingo Molnar5c92d122008-12-11 13:21:10 +01002746};
2747
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01002748/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002749 * Software counter: task time clock
2750 */
2751
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02002752static void task_clock_perf_counter_update(struct perf_counter *counter, u64 now)
Ingo Molnarbae43c92008-12-11 14:03:20 +01002753{
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02002754 u64 prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002755 s64 delta;
Ingo Molnarbae43c92008-12-11 14:03:20 +01002756
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02002757 prev = atomic64_xchg(&counter->hw.prev_count, now);
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002758 delta = now - prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002759 atomic64_add(delta, &counter->count);
Ingo Molnarbae43c92008-12-11 14:03:20 +01002760}
2761
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01002762static int task_clock_perf_counter_enable(struct perf_counter *counter)
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002763{
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002764 struct hw_perf_counter *hwc = &counter->hw;
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02002765 u64 now;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002766
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02002767 now = counter->ctx->time;
2768
2769 atomic64_set(&hwc->prev_count, now);
Peter Zijlstra039fc912009-03-13 16:43:47 +01002770 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2771 hwc->hrtimer.function = perf_swcounter_hrtimer;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002772 if (hwc->irq_period) {
Peter Zijlstra60db5e02009-05-15 15:19:28 +02002773 u64 period = max_t(u64, 10000, hwc->irq_period);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002774 __hrtimer_start_range_ns(&hwc->hrtimer,
Peter Zijlstra60db5e02009-05-15 15:19:28 +02002775 ns_to_ktime(period), 0,
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002776 HRTIMER_MODE_REL, 0);
2777 }
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01002778
2779 return 0;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002780}
2781
2782static void task_clock_perf_counter_disable(struct perf_counter *counter)
2783{
Peter Zijlstrab986d7e2009-05-20 12:21:21 +02002784 if (counter->hw.irq_period)
2785 hrtimer_cancel(&counter->hw.hrtimer);
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02002786 task_clock_perf_counter_update(counter, counter->ctx->time);
2787
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002788}
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01002789
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002790static void task_clock_perf_counter_read(struct perf_counter *counter)
2791{
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02002792 u64 time;
2793
2794 if (!in_nmi()) {
2795 update_context_time(counter->ctx);
2796 time = counter->ctx->time;
2797 } else {
2798 u64 now = perf_clock();
2799 u64 delta = now - counter->ctx->timestamp;
2800 time = counter->ctx->time + delta;
2801 }
2802
2803 task_clock_perf_counter_update(counter, time);
Ingo Molnarbae43c92008-12-11 14:03:20 +01002804}
2805
Robert Richter4aeb0b42009-04-29 12:47:03 +02002806static const struct pmu perf_ops_task_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +01002807 .enable = task_clock_perf_counter_enable,
2808 .disable = task_clock_perf_counter_disable,
2809 .read = task_clock_perf_counter_read,
Ingo Molnarbae43c92008-12-11 14:03:20 +01002810};
2811
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002812/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002813 * Software counter: cpu migrations
2814 */
2815
Paul Mackerras23a185c2009-02-09 22:42:47 +11002816static inline u64 get_cpu_migrations(struct perf_counter *counter)
Ingo Molnar6c594c22008-12-14 12:34:15 +01002817{
Paul Mackerras23a185c2009-02-09 22:42:47 +11002818 struct task_struct *curr = counter->ctx->task;
2819
2820 if (curr)
2821 return curr->se.nr_migrations;
2822 return cpu_nr_migrations(smp_processor_id());
Ingo Molnar6c594c22008-12-14 12:34:15 +01002823}
2824
2825static void cpu_migrations_perf_counter_update(struct perf_counter *counter)
2826{
2827 u64 prev, now;
2828 s64 delta;
2829
2830 prev = atomic64_read(&counter->hw.prev_count);
Paul Mackerras23a185c2009-02-09 22:42:47 +11002831 now = get_cpu_migrations(counter);
Ingo Molnar6c594c22008-12-14 12:34:15 +01002832
2833 atomic64_set(&counter->hw.prev_count, now);
2834
2835 delta = now - prev;
Ingo Molnar6c594c22008-12-14 12:34:15 +01002836
2837 atomic64_add(delta, &counter->count);
2838}
2839
2840static void cpu_migrations_perf_counter_read(struct perf_counter *counter)
2841{
2842 cpu_migrations_perf_counter_update(counter);
2843}
2844
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01002845static int cpu_migrations_perf_counter_enable(struct perf_counter *counter)
Ingo Molnar6c594c22008-12-14 12:34:15 +01002846{
Paul Mackerrasc07c99b2009-02-13 22:10:34 +11002847 if (counter->prev_state <= PERF_COUNTER_STATE_OFF)
2848 atomic64_set(&counter->hw.prev_count,
2849 get_cpu_migrations(counter));
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01002850 return 0;
Ingo Molnar6c594c22008-12-14 12:34:15 +01002851}
2852
2853static void cpu_migrations_perf_counter_disable(struct perf_counter *counter)
2854{
2855 cpu_migrations_perf_counter_update(counter);
2856}
2857
Robert Richter4aeb0b42009-04-29 12:47:03 +02002858static const struct pmu perf_ops_cpu_migrations = {
Ingo Molnar76715812008-12-17 14:20:28 +01002859 .enable = cpu_migrations_perf_counter_enable,
2860 .disable = cpu_migrations_perf_counter_disable,
2861 .read = cpu_migrations_perf_counter_read,
Ingo Molnar6c594c22008-12-14 12:34:15 +01002862};
2863
Peter Zijlstrae077df42009-03-19 20:26:17 +01002864#ifdef CONFIG_EVENT_PROFILE
2865void perf_tpcounter_event(int event_id)
2866{
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002867 struct pt_regs *regs = get_irq_regs();
2868
2869 if (!regs)
2870 regs = task_pt_regs(current);
2871
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002872 __perf_swcounter_event(PERF_TYPE_TRACEPOINT, event_id, 1, 1, regs, 0);
Peter Zijlstrae077df42009-03-19 20:26:17 +01002873}
Steven Whitehouseff7b1b42009-04-15 16:55:05 +01002874EXPORT_SYMBOL_GPL(perf_tpcounter_event);
Peter Zijlstrae077df42009-03-19 20:26:17 +01002875
2876extern int ftrace_profile_enable(int);
2877extern void ftrace_profile_disable(int);
2878
2879static void tp_perf_counter_destroy(struct perf_counter *counter)
2880{
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002881 ftrace_profile_disable(perf_event_id(&counter->hw_event));
Peter Zijlstrae077df42009-03-19 20:26:17 +01002882}
2883
Robert Richter4aeb0b42009-04-29 12:47:03 +02002884static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
Peter Zijlstrae077df42009-03-19 20:26:17 +01002885{
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002886 int event_id = perf_event_id(&counter->hw_event);
Peter Zijlstrae077df42009-03-19 20:26:17 +01002887 int ret;
2888
2889 ret = ftrace_profile_enable(event_id);
2890 if (ret)
2891 return NULL;
2892
2893 counter->destroy = tp_perf_counter_destroy;
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002894 counter->hw.irq_period = counter->hw_event.irq_period;
Peter Zijlstrae077df42009-03-19 20:26:17 +01002895
2896 return &perf_ops_generic;
2897}
2898#else
Robert Richter4aeb0b42009-04-29 12:47:03 +02002899static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
Peter Zijlstrae077df42009-03-19 20:26:17 +01002900{
2901 return NULL;
2902}
2903#endif
2904
Robert Richter4aeb0b42009-04-29 12:47:03 +02002905static const struct pmu *sw_perf_counter_init(struct perf_counter *counter)
Ingo Molnar5c92d122008-12-11 13:21:10 +01002906{
Robert Richter4aeb0b42009-04-29 12:47:03 +02002907 const struct pmu *pmu = NULL;
Ingo Molnar5c92d122008-12-11 13:21:10 +01002908
Paul Mackerras0475f9e2009-02-11 14:35:35 +11002909 /*
2910 * Software counters (currently) can't in general distinguish
2911 * between user, kernel and hypervisor events.
2912 * However, context switches and cpu migrations are considered
2913 * to be kernel events, and page faults are never hypervisor
2914 * events.
2915 */
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002916 switch (perf_event_id(&counter->hw_event)) {
Ingo Molnar5c92d122008-12-11 13:21:10 +01002917 case PERF_COUNT_CPU_CLOCK:
Robert Richter4aeb0b42009-04-29 12:47:03 +02002918 pmu = &perf_ops_cpu_clock;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002919
Ingo Molnar5c92d122008-12-11 13:21:10 +01002920 break;
Ingo Molnarbae43c92008-12-11 14:03:20 +01002921 case PERF_COUNT_TASK_CLOCK:
Paul Mackerras23a185c2009-02-09 22:42:47 +11002922 /*
2923 * If the user instantiates this as a per-cpu counter,
2924 * use the cpu_clock counter instead.
2925 */
2926 if (counter->ctx->task)
Robert Richter4aeb0b42009-04-29 12:47:03 +02002927 pmu = &perf_ops_task_clock;
Paul Mackerras23a185c2009-02-09 22:42:47 +11002928 else
Robert Richter4aeb0b42009-04-29 12:47:03 +02002929 pmu = &perf_ops_cpu_clock;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002930
Ingo Molnarbae43c92008-12-11 14:03:20 +01002931 break;
Ingo Molnare06c61a2008-12-14 14:44:31 +01002932 case PERF_COUNT_PAGE_FAULTS:
Peter Zijlstraac17dc82009-03-13 12:21:34 +01002933 case PERF_COUNT_PAGE_FAULTS_MIN:
2934 case PERF_COUNT_PAGE_FAULTS_MAJ:
Ingo Molnar5d6a27d2008-12-14 12:28:33 +01002935 case PERF_COUNT_CONTEXT_SWITCHES:
Robert Richter4aeb0b42009-04-29 12:47:03 +02002936 pmu = &perf_ops_generic;
Ingo Molnar5d6a27d2008-12-14 12:28:33 +01002937 break;
Ingo Molnar6c594c22008-12-14 12:34:15 +01002938 case PERF_COUNT_CPU_MIGRATIONS:
Paul Mackerras0475f9e2009-02-11 14:35:35 +11002939 if (!counter->hw_event.exclude_kernel)
Robert Richter4aeb0b42009-04-29 12:47:03 +02002940 pmu = &perf_ops_cpu_migrations;
Ingo Molnar6c594c22008-12-14 12:34:15 +01002941 break;
Ingo Molnar5c92d122008-12-11 13:21:10 +01002942 }
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002943
Robert Richter4aeb0b42009-04-29 12:47:03 +02002944 return pmu;
Ingo Molnar5c92d122008-12-11 13:21:10 +01002945}
2946
Thomas Gleixner0793a612008-12-04 20:12:29 +01002947/*
2948 * Allocate and initialize a counter structure
2949 */
2950static struct perf_counter *
Ingo Molnar04289bb2008-12-11 08:38:42 +01002951perf_counter_alloc(struct perf_counter_hw_event *hw_event,
2952 int cpu,
Paul Mackerras23a185c2009-02-09 22:42:47 +11002953 struct perf_counter_context *ctx,
Ingo Molnar9b51f662008-12-12 13:49:45 +01002954 struct perf_counter *group_leader,
2955 gfp_t gfpflags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01002956{
Robert Richter4aeb0b42009-04-29 12:47:03 +02002957 const struct pmu *pmu;
Ingo Molnar621a01e2008-12-11 12:46:46 +01002958 struct perf_counter *counter;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02002959 struct hw_perf_counter *hwc;
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02002960 long err;
Thomas Gleixner0793a612008-12-04 20:12:29 +01002961
Ingo Molnar9b51f662008-12-12 13:49:45 +01002962 counter = kzalloc(sizeof(*counter), gfpflags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002963 if (!counter)
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02002964 return ERR_PTR(-ENOMEM);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002965
Ingo Molnar04289bb2008-12-11 08:38:42 +01002966 /*
2967 * Single counters are their own group leaders, with an
2968 * empty sibling list:
2969 */
2970 if (!group_leader)
2971 group_leader = counter;
2972
Thomas Gleixner0793a612008-12-04 20:12:29 +01002973 mutex_init(&counter->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01002974 INIT_LIST_HEAD(&counter->list_entry);
Peter Zijlstra592903c2009-03-13 12:21:36 +01002975 INIT_LIST_HEAD(&counter->event_entry);
Ingo Molnar04289bb2008-12-11 08:38:42 +01002976 INIT_LIST_HEAD(&counter->sibling_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002977 init_waitqueue_head(&counter->waitq);
2978
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002979 mutex_init(&counter->mmap_mutex);
2980
Paul Mackerrasd859e292009-01-17 18:10:22 +11002981 INIT_LIST_HEAD(&counter->child_list);
2982
Ingo Molnar9f66a382008-12-10 12:33:23 +01002983 counter->cpu = cpu;
2984 counter->hw_event = *hw_event;
Ingo Molnar04289bb2008-12-11 08:38:42 +01002985 counter->group_leader = group_leader;
Robert Richter4aeb0b42009-04-29 12:47:03 +02002986 counter->pmu = NULL;
Paul Mackerras23a185c2009-02-09 22:42:47 +11002987 counter->ctx = ctx;
Ingo Molnar621a01e2008-12-11 12:46:46 +01002988
Ingo Molnar235c7fc2008-12-21 14:43:25 +01002989 counter->state = PERF_COUNTER_STATE_INACTIVE;
Ingo Molnara86ed502008-12-17 00:43:10 +01002990 if (hw_event->disabled)
2991 counter->state = PERF_COUNTER_STATE_OFF;
2992
Robert Richter4aeb0b42009-04-29 12:47:03 +02002993 pmu = NULL;
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002994
Peter Zijlstra60db5e02009-05-15 15:19:28 +02002995 hwc = &counter->hw;
2996 if (hw_event->freq && hw_event->irq_freq)
Peter Zijlstra2e569d32009-05-15 15:37:47 +02002997 hwc->irq_period = div64_u64(TICK_NSEC, hw_event->irq_freq);
Peter Zijlstra60db5e02009-05-15 15:19:28 +02002998 else
2999 hwc->irq_period = hw_event->irq_period;
3000
Peter Zijlstra2023b352009-05-05 17:50:26 +02003001 /*
3002 * we currently do not support PERF_RECORD_GROUP on inherited counters
3003 */
3004 if (hw_event->inherit && (hw_event->record_type & PERF_RECORD_GROUP))
3005 goto done;
3006
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01003007 if (perf_event_raw(hw_event)) {
Robert Richter4aeb0b42009-04-29 12:47:03 +02003008 pmu = hw_perf_counter_init(counter);
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01003009 goto done;
3010 }
3011
3012 switch (perf_event_type(hw_event)) {
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003013 case PERF_TYPE_HARDWARE:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003014 pmu = hw_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003015 break;
3016
3017 case PERF_TYPE_SOFTWARE:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003018 pmu = sw_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003019 break;
3020
3021 case PERF_TYPE_TRACEPOINT:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003022 pmu = tp_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003023 break;
3024 }
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01003025done:
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003026 err = 0;
Robert Richter4aeb0b42009-04-29 12:47:03 +02003027 if (!pmu)
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003028 err = -EINVAL;
Robert Richter4aeb0b42009-04-29 12:47:03 +02003029 else if (IS_ERR(pmu))
3030 err = PTR_ERR(pmu);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003031
3032 if (err) {
3033 kfree(counter);
3034 return ERR_PTR(err);
3035 }
3036
Robert Richter4aeb0b42009-04-29 12:47:03 +02003037 counter->pmu = pmu;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003038
Peter Zijlstra7fc23a52009-05-08 18:52:21 +02003039 atomic_inc(&nr_counters);
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02003040 if (counter->hw_event.mmap)
3041 atomic_inc(&nr_mmap_tracking);
3042 if (counter->hw_event.munmap)
3043 atomic_inc(&nr_munmap_tracking);
3044 if (counter->hw_event.comm)
3045 atomic_inc(&nr_comm_tracking);
3046
Thomas Gleixner0793a612008-12-04 20:12:29 +01003047 return counter;
3048}
3049
3050/**
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003051 * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
Ingo Molnar9f66a382008-12-10 12:33:23 +01003052 *
3053 * @hw_event_uptr: event type attributes for monitoring/sampling
Thomas Gleixner0793a612008-12-04 20:12:29 +01003054 * @pid: target pid
Ingo Molnar9f66a382008-12-10 12:33:23 +01003055 * @cpu: target cpu
3056 * @group_fd: group leader counter fd
Thomas Gleixner0793a612008-12-04 20:12:29 +01003057 */
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003058SYSCALL_DEFINE5(perf_counter_open,
Paul Mackerrasf3dfd262009-02-26 22:43:46 +11003059 const struct perf_counter_hw_event __user *, hw_event_uptr,
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003060 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003061{
Ingo Molnar04289bb2008-12-11 08:38:42 +01003062 struct perf_counter *counter, *group_leader;
Ingo Molnar9f66a382008-12-10 12:33:23 +01003063 struct perf_counter_hw_event hw_event;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003064 struct perf_counter_context *ctx;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003065 struct file *counter_file = NULL;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003066 struct file *group_file = NULL;
3067 int fput_needed = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003068 int fput_needed2 = 0;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003069 int ret;
3070
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003071 /* for future expandability... */
3072 if (flags)
3073 return -EINVAL;
3074
Ingo Molnar9f66a382008-12-10 12:33:23 +01003075 if (copy_from_user(&hw_event, hw_event_uptr, sizeof(hw_event)) != 0)
Thomas Gleixnereab656a2008-12-08 19:26:59 +01003076 return -EFAULT;
3077
Ingo Molnar04289bb2008-12-11 08:38:42 +01003078 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01003079 * Get the target context (task or percpu):
3080 */
3081 ctx = find_get_context(pid, cpu);
3082 if (IS_ERR(ctx))
3083 return PTR_ERR(ctx);
3084
3085 /*
3086 * Look up the group leader (we will attach this counter to it):
Ingo Molnar04289bb2008-12-11 08:38:42 +01003087 */
3088 group_leader = NULL;
3089 if (group_fd != -1) {
3090 ret = -EINVAL;
3091 group_file = fget_light(group_fd, &fput_needed);
3092 if (!group_file)
Ingo Molnarccff2862008-12-11 11:26:29 +01003093 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003094 if (group_file->f_op != &perf_fops)
Ingo Molnarccff2862008-12-11 11:26:29 +01003095 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003096
3097 group_leader = group_file->private_data;
3098 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01003099 * Do not allow a recursive hierarchy (this new sibling
3100 * becoming part of another group-sibling):
Ingo Molnar04289bb2008-12-11 08:38:42 +01003101 */
Ingo Molnarccff2862008-12-11 11:26:29 +01003102 if (group_leader->group_leader != group_leader)
3103 goto err_put_context;
3104 /*
3105 * Do not allow to attach to a group in a different
3106 * task or CPU context:
3107 */
3108 if (group_leader->ctx != ctx)
3109 goto err_put_context;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11003110 /*
3111 * Only a group leader can be exclusive or pinned
3112 */
3113 if (hw_event.exclusive || hw_event.pinned)
3114 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003115 }
3116
Paul Mackerras23a185c2009-02-09 22:42:47 +11003117 counter = perf_counter_alloc(&hw_event, cpu, ctx, group_leader,
3118 GFP_KERNEL);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003119 ret = PTR_ERR(counter);
3120 if (IS_ERR(counter))
Thomas Gleixner0793a612008-12-04 20:12:29 +01003121 goto err_put_context;
3122
Thomas Gleixner0793a612008-12-04 20:12:29 +01003123 ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
3124 if (ret < 0)
Ingo Molnar9b51f662008-12-12 13:49:45 +01003125 goto err_free_put_context;
3126
3127 counter_file = fget_light(ret, &fput_needed2);
3128 if (!counter_file)
3129 goto err_free_put_context;
3130
3131 counter->filp = counter_file;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003132 mutex_lock(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003133 perf_install_in_context(ctx, counter, cpu);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003134 mutex_unlock(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003135
3136 fput_light(counter_file, fput_needed2);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003137
Ingo Molnar04289bb2008-12-11 08:38:42 +01003138out_fput:
3139 fput_light(group_file, fput_needed);
3140
Thomas Gleixner0793a612008-12-04 20:12:29 +01003141 return ret;
3142
Ingo Molnar9b51f662008-12-12 13:49:45 +01003143err_free_put_context:
Thomas Gleixner0793a612008-12-04 20:12:29 +01003144 kfree(counter);
3145
3146err_put_context:
3147 put_context(ctx);
3148
Ingo Molnar04289bb2008-12-11 08:38:42 +01003149 goto out_fput;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003150}
3151
Ingo Molnar9b51f662008-12-12 13:49:45 +01003152/*
3153 * Initialize the perf_counter context in a task_struct:
3154 */
3155static void
3156__perf_counter_init_context(struct perf_counter_context *ctx,
3157 struct task_struct *task)
3158{
3159 memset(ctx, 0, sizeof(*ctx));
3160 spin_lock_init(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003161 mutex_init(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003162 INIT_LIST_HEAD(&ctx->counter_list);
Peter Zijlstra592903c2009-03-13 12:21:36 +01003163 INIT_LIST_HEAD(&ctx->event_list);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003164 ctx->task = task;
3165}
3166
3167/*
3168 * inherit a counter from parent task to child task:
3169 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11003170static struct perf_counter *
Ingo Molnar9b51f662008-12-12 13:49:45 +01003171inherit_counter(struct perf_counter *parent_counter,
3172 struct task_struct *parent,
3173 struct perf_counter_context *parent_ctx,
3174 struct task_struct *child,
Paul Mackerrasd859e292009-01-17 18:10:22 +11003175 struct perf_counter *group_leader,
Ingo Molnar9b51f662008-12-12 13:49:45 +01003176 struct perf_counter_context *child_ctx)
3177{
3178 struct perf_counter *child_counter;
3179
Paul Mackerrasd859e292009-01-17 18:10:22 +11003180 /*
3181 * Instead of creating recursive hierarchies of counters,
3182 * we link inherited counters back to the original parent,
3183 * which has a filp for sure, which we use as the reference
3184 * count:
3185 */
3186 if (parent_counter->parent)
3187 parent_counter = parent_counter->parent;
3188
Ingo Molnar9b51f662008-12-12 13:49:45 +01003189 child_counter = perf_counter_alloc(&parent_counter->hw_event,
Paul Mackerras23a185c2009-02-09 22:42:47 +11003190 parent_counter->cpu, child_ctx,
3191 group_leader, GFP_KERNEL);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003192 if (IS_ERR(child_counter))
3193 return child_counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003194
3195 /*
3196 * Link it up in the child's context:
3197 */
Ingo Molnar9b51f662008-12-12 13:49:45 +01003198 child_counter->task = child;
Paul Mackerras53cfbf52009-03-25 22:46:58 +11003199 add_counter_to_ctx(child_counter, child_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003200
3201 child_counter->parent = parent_counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003202 /*
3203 * inherit into child's child as well:
3204 */
3205 child_counter->hw_event.inherit = 1;
3206
3207 /*
3208 * Get a reference to the parent filp - we will fput it
3209 * when the child counter exits. This is safe to do because
3210 * we are in the parent and we know that the filp still
3211 * exists and has a nonzero count:
3212 */
3213 atomic_long_inc(&parent_counter->filp->f_count);
3214
Paul Mackerrasd859e292009-01-17 18:10:22 +11003215 /*
3216 * Link this into the parent counter's child list
3217 */
3218 mutex_lock(&parent_counter->mutex);
3219 list_add_tail(&child_counter->child_list, &parent_counter->child_list);
3220
3221 /*
3222 * Make the child state follow the state of the parent counter,
3223 * not its hw_event.disabled bit. We hold the parent's mutex,
3224 * so we won't race with perf_counter_{en,dis}able_family.
3225 */
3226 if (parent_counter->state >= PERF_COUNTER_STATE_INACTIVE)
3227 child_counter->state = PERF_COUNTER_STATE_INACTIVE;
3228 else
3229 child_counter->state = PERF_COUNTER_STATE_OFF;
3230
3231 mutex_unlock(&parent_counter->mutex);
3232
3233 return child_counter;
3234}
3235
3236static int inherit_group(struct perf_counter *parent_counter,
3237 struct task_struct *parent,
3238 struct perf_counter_context *parent_ctx,
3239 struct task_struct *child,
3240 struct perf_counter_context *child_ctx)
3241{
3242 struct perf_counter *leader;
3243 struct perf_counter *sub;
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003244 struct perf_counter *child_ctr;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003245
3246 leader = inherit_counter(parent_counter, parent, parent_ctx,
3247 child, NULL, child_ctx);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003248 if (IS_ERR(leader))
3249 return PTR_ERR(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003250 list_for_each_entry(sub, &parent_counter->sibling_list, list_entry) {
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003251 child_ctr = inherit_counter(sub, parent, parent_ctx,
3252 child, leader, child_ctx);
3253 if (IS_ERR(child_ctr))
3254 return PTR_ERR(child_ctr);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003255 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01003256 return 0;
3257}
3258
Paul Mackerrasd859e292009-01-17 18:10:22 +11003259static void sync_child_counter(struct perf_counter *child_counter,
3260 struct perf_counter *parent_counter)
3261{
Peter Zijlstra8bc20952009-05-15 20:45:59 +02003262 u64 child_val;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003263
Paul Mackerrasd859e292009-01-17 18:10:22 +11003264 child_val = atomic64_read(&child_counter->count);
3265
3266 /*
3267 * Add back the child's count to the parent's count:
3268 */
3269 atomic64_add(child_val, &parent_counter->count);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11003270 atomic64_add(child_counter->total_time_enabled,
3271 &parent_counter->child_total_time_enabled);
3272 atomic64_add(child_counter->total_time_running,
3273 &parent_counter->child_total_time_running);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003274
3275 /*
3276 * Remove this counter from the parent's list
3277 */
3278 mutex_lock(&parent_counter->mutex);
3279 list_del_init(&child_counter->child_list);
3280 mutex_unlock(&parent_counter->mutex);
3281
3282 /*
3283 * Release the parent counter, if this was the last
3284 * reference to it.
3285 */
3286 fput(parent_counter->filp);
3287}
3288
Ingo Molnar9b51f662008-12-12 13:49:45 +01003289static void
3290__perf_counter_exit_task(struct task_struct *child,
3291 struct perf_counter *child_counter,
3292 struct perf_counter_context *child_ctx)
3293{
3294 struct perf_counter *parent_counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003295
3296 /*
Ingo Molnar235c7fc2008-12-21 14:43:25 +01003297 * If we do not self-reap then we have to wait for the
3298 * child task to unschedule (it will happen for sure),
3299 * so that its counter is at its final count. (This
3300 * condition triggers rarely - child tasks usually get
3301 * off their CPU before the parent has a chance to
3302 * get this far into the reaping action)
Ingo Molnar9b51f662008-12-12 13:49:45 +01003303 */
Ingo Molnar235c7fc2008-12-21 14:43:25 +01003304 if (child != current) {
3305 wait_task_inactive(child, 0);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11003306 update_counter_times(child_counter);
Peter Zijlstra8bc20952009-05-15 20:45:59 +02003307 list_del_counter(child_counter, child_ctx);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01003308 } else {
Ingo Molnar0cc0c022008-12-14 23:20:36 +01003309 struct perf_cpu_context *cpuctx;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01003310 unsigned long flags;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01003311
3312 /*
3313 * Disable and unlink this counter.
3314 *
3315 * Be careful about zapping the list - IRQ/NMI context
3316 * could still be processing it:
3317 */
Peter Zijlstra849691a2009-04-06 11:45:12 +02003318 local_irq_save(flags);
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02003319 perf_disable();
Ingo Molnar0cc0c022008-12-14 23:20:36 +01003320
3321 cpuctx = &__get_cpu_var(perf_cpu_context);
3322
Paul Mackerrasd859e292009-01-17 18:10:22 +11003323 group_sched_out(child_counter, cpuctx, child_ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11003324 update_counter_times(child_counter);
Ingo Molnar0cc0c022008-12-14 23:20:36 +01003325
Peter Zijlstra8bc20952009-05-15 20:45:59 +02003326 list_del_counter(child_counter, child_ctx);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01003327
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02003328 perf_enable();
Peter Zijlstra849691a2009-04-06 11:45:12 +02003329 local_irq_restore(flags);
Ingo Molnar0cc0c022008-12-14 23:20:36 +01003330 }
3331
Ingo Molnar9b51f662008-12-12 13:49:45 +01003332 parent_counter = child_counter->parent;
3333 /*
3334 * It can happen that parent exits first, and has counters
3335 * that are still around due to the child reference. These
3336 * counters need to be zapped - but otherwise linger.
3337 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11003338 if (parent_counter) {
3339 sync_child_counter(child_counter, parent_counter);
Peter Zijlstraf1600952009-03-19 20:26:16 +01003340 free_counter(child_counter);
Paul Mackerras4bcf3492009-02-11 13:53:19 +01003341 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01003342}
3343
3344/*
Paul Mackerrasd859e292009-01-17 18:10:22 +11003345 * When a child task exits, feed back counter values to parent counters.
Ingo Molnar9b51f662008-12-12 13:49:45 +01003346 *
Paul Mackerrasd859e292009-01-17 18:10:22 +11003347 * Note: we may be running in child context, but the PID is not hashed
Ingo Molnar9b51f662008-12-12 13:49:45 +01003348 * anymore so new counters will not be added.
3349 */
3350void perf_counter_exit_task(struct task_struct *child)
3351{
3352 struct perf_counter *child_counter, *tmp;
3353 struct perf_counter_context *child_ctx;
3354
Ingo Molnar33b2fb32009-05-17 11:08:41 +02003355 WARN_ON_ONCE(child != current);
3356
Ingo Molnar9b51f662008-12-12 13:49:45 +01003357 child_ctx = &child->perf_counter_ctx;
3358
3359 if (likely(!child_ctx->nr_counters))
3360 return;
3361
Peter Zijlstra8bc20952009-05-15 20:45:59 +02003362again:
Ingo Molnar9b51f662008-12-12 13:49:45 +01003363 list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list,
3364 list_entry)
3365 __perf_counter_exit_task(child, child_counter, child_ctx);
Peter Zijlstra8bc20952009-05-15 20:45:59 +02003366
3367 /*
3368 * If the last counter was a group counter, it will have appended all
3369 * its siblings to the list, but we obtained 'tmp' before that which
3370 * will still point to the list head terminating the iteration.
3371 */
3372 if (!list_empty(&child_ctx->counter_list))
3373 goto again;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003374}
3375
3376/*
3377 * Initialize the perf_counter context in task_struct
3378 */
3379void perf_counter_init_task(struct task_struct *child)
3380{
3381 struct perf_counter_context *child_ctx, *parent_ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003382 struct perf_counter *counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003383 struct task_struct *parent = current;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003384
3385 child_ctx = &child->perf_counter_ctx;
3386 parent_ctx = &parent->perf_counter_ctx;
3387
3388 __perf_counter_init_context(child_ctx, child);
3389
3390 /*
3391 * This is executed from the parent task context, so inherit
3392 * counters that have been marked for cloning:
3393 */
3394
3395 if (likely(!parent_ctx->nr_counters))
3396 return;
3397
3398 /*
3399 * Lock the parent list. No need to lock the child - not PID
3400 * hashed yet and not running, so nobody can access it.
3401 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11003402 mutex_lock(&parent_ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003403
3404 /*
3405 * We dont have to disable NMIs - we are only looking at
3406 * the list, not manipulating it:
3407 */
Peter Zijlstrad7b629a2009-05-20 12:21:19 +02003408 list_for_each_entry_rcu(counter, &parent_ctx->event_list, event_entry) {
3409 if (counter != counter->group_leader)
3410 continue;
3411
Paul Mackerrasd859e292009-01-17 18:10:22 +11003412 if (!counter->hw_event.inherit)
Ingo Molnar9b51f662008-12-12 13:49:45 +01003413 continue;
3414
Paul Mackerrasd859e292009-01-17 18:10:22 +11003415 if (inherit_group(counter, parent,
Ingo Molnar9b51f662008-12-12 13:49:45 +01003416 parent_ctx, child, child_ctx))
3417 break;
3418 }
3419
Paul Mackerrasd859e292009-01-17 18:10:22 +11003420 mutex_unlock(&parent_ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003421}
3422
Ingo Molnar04289bb2008-12-11 08:38:42 +01003423static void __cpuinit perf_counter_init_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003424{
Ingo Molnar04289bb2008-12-11 08:38:42 +01003425 struct perf_cpu_context *cpuctx;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003426
Ingo Molnar04289bb2008-12-11 08:38:42 +01003427 cpuctx = &per_cpu(perf_cpu_context, cpu);
3428 __perf_counter_init_context(&cpuctx->ctx, NULL);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003429
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003430 spin_lock(&perf_resource_lock);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003431 cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu;
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003432 spin_unlock(&perf_resource_lock);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003433
Paul Mackerras01d02872009-01-14 13:44:19 +11003434 hw_perf_counter_setup(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003435}
3436
3437#ifdef CONFIG_HOTPLUG_CPU
Ingo Molnar04289bb2008-12-11 08:38:42 +01003438static void __perf_counter_exit_cpu(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003439{
3440 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
3441 struct perf_counter_context *ctx = &cpuctx->ctx;
3442 struct perf_counter *counter, *tmp;
3443
Ingo Molnar04289bb2008-12-11 08:38:42 +01003444 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry)
3445 __perf_counter_remove_from_context(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003446}
Ingo Molnar04289bb2008-12-11 08:38:42 +01003447static void perf_counter_exit_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003448{
Paul Mackerrasd859e292009-01-17 18:10:22 +11003449 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
3450 struct perf_counter_context *ctx = &cpuctx->ctx;
3451
3452 mutex_lock(&ctx->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003453 smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003454 mutex_unlock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003455}
3456#else
Ingo Molnar04289bb2008-12-11 08:38:42 +01003457static inline void perf_counter_exit_cpu(int cpu) { }
Thomas Gleixner0793a612008-12-04 20:12:29 +01003458#endif
3459
3460static int __cpuinit
3461perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
3462{
3463 unsigned int cpu = (long)hcpu;
3464
3465 switch (action) {
3466
3467 case CPU_UP_PREPARE:
3468 case CPU_UP_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01003469 perf_counter_init_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003470 break;
3471
3472 case CPU_DOWN_PREPARE:
3473 case CPU_DOWN_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01003474 perf_counter_exit_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003475 break;
3476
3477 default:
3478 break;
3479 }
3480
3481 return NOTIFY_OK;
3482}
3483
3484static struct notifier_block __cpuinitdata perf_cpu_nb = {
3485 .notifier_call = perf_cpu_notify,
3486};
3487
Ingo Molnar0d905bc2009-05-04 19:13:30 +02003488void __init perf_counter_init(void)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003489{
3490 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
3491 (void *)(long)smp_processor_id());
3492 register_cpu_notifier(&perf_cpu_nb);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003493}
Thomas Gleixner0793a612008-12-04 20:12:29 +01003494
3495static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
3496{
3497 return sprintf(buf, "%d\n", perf_reserved_percpu);
3498}
3499
3500static ssize_t
3501perf_set_reserve_percpu(struct sysdev_class *class,
3502 const char *buf,
3503 size_t count)
3504{
3505 struct perf_cpu_context *cpuctx;
3506 unsigned long val;
3507 int err, cpu, mpt;
3508
3509 err = strict_strtoul(buf, 10, &val);
3510 if (err)
3511 return err;
3512 if (val > perf_max_counters)
3513 return -EINVAL;
3514
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003515 spin_lock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003516 perf_reserved_percpu = val;
3517 for_each_online_cpu(cpu) {
3518 cpuctx = &per_cpu(perf_cpu_context, cpu);
3519 spin_lock_irq(&cpuctx->ctx.lock);
3520 mpt = min(perf_max_counters - cpuctx->ctx.nr_counters,
3521 perf_max_counters - perf_reserved_percpu);
3522 cpuctx->max_pertask = mpt;
3523 spin_unlock_irq(&cpuctx->ctx.lock);
3524 }
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003525 spin_unlock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003526
3527 return count;
3528}
3529
3530static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
3531{
3532 return sprintf(buf, "%d\n", perf_overcommit);
3533}
3534
3535static ssize_t
3536perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
3537{
3538 unsigned long val;
3539 int err;
3540
3541 err = strict_strtoul(buf, 10, &val);
3542 if (err)
3543 return err;
3544 if (val > 1)
3545 return -EINVAL;
3546
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003547 spin_lock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003548 perf_overcommit = val;
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003549 spin_unlock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003550
3551 return count;
3552}
3553
3554static SYSDEV_CLASS_ATTR(
3555 reserve_percpu,
3556 0644,
3557 perf_show_reserve_percpu,
3558 perf_set_reserve_percpu
3559 );
3560
3561static SYSDEV_CLASS_ATTR(
3562 overcommit,
3563 0644,
3564 perf_show_overcommit,
3565 perf_set_overcommit
3566 );
3567
3568static struct attribute *perfclass_attrs[] = {
3569 &attr_reserve_percpu.attr,
3570 &attr_overcommit.attr,
3571 NULL
3572};
3573
3574static struct attribute_group perfclass_attr_group = {
3575 .attrs = perfclass_attrs,
3576 .name = "perf_counters",
3577};
3578
3579static int __init perf_counter_sysfs_init(void)
3580{
3581 return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
3582 &perfclass_attr_group);
3583}
3584device_initcall(perf_counter_sysfs_init);