blob: e814ff04d7ca0266c7f4fc44361149937d8d7df6 [file] [log] [blame]
Thomas Gleixner0793a612008-12-04 20:12:29 +01001/*
2 * Performance counter core code
3 *
Ingo Molnar98144512009-04-29 14:52:50 +02004 * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
5 * Copyright (C) 2008-2009 Red Hat, Inc., Ingo Molnar
6 * Copyright (C) 2008-2009 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
Paul Mackerrasc5dd0162009-04-30 09:48:16 +10007 * Copyright © 2009 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
Peter Zijlstra7b732a72009-03-23 18:22:10 +01008 *
9 * For licensing details see kernel-base/COPYING
Thomas Gleixner0793a612008-12-04 20:12:29 +010010 */
11
12#include <linux/fs.h>
Peter Zijlstrab9cacc72009-03-25 12:30:22 +010013#include <linux/mm.h>
Thomas Gleixner0793a612008-12-04 20:12:29 +010014#include <linux/cpu.h>
15#include <linux/smp.h>
Ingo Molnar04289bb2008-12-11 08:38:42 +010016#include <linux/file.h>
Thomas Gleixner0793a612008-12-04 20:12:29 +010017#include <linux/poll.h>
18#include <linux/sysfs.h>
19#include <linux/ptrace.h>
20#include <linux/percpu.h>
Peter Zijlstrab9cacc72009-03-25 12:30:22 +010021#include <linux/vmstat.h>
22#include <linux/hardirq.h>
23#include <linux/rculist.h>
Thomas Gleixner0793a612008-12-04 20:12:29 +010024#include <linux/uaccess.h>
25#include <linux/syscalls.h>
26#include <linux/anon_inodes.h>
Ingo Molnaraa9c4c02008-12-17 14:10:57 +010027#include <linux/kernel_stat.h>
Thomas Gleixner0793a612008-12-04 20:12:29 +010028#include <linux/perf_counter.h>
Peter Zijlstra0a4a9392009-03-30 19:07:05 +020029#include <linux/dcache.h>
Thomas Gleixner0793a612008-12-04 20:12:29 +010030
Tim Blechmann4e193bd2009-03-14 14:29:25 +010031#include <asm/irq_regs.h>
32
Thomas Gleixner0793a612008-12-04 20:12:29 +010033/*
34 * Each CPU has a list of per CPU counters:
35 */
36DEFINE_PER_CPU(struct perf_cpu_context, perf_cpu_context);
37
Ingo Molnar088e2852008-12-14 20:21:00 +010038int perf_max_counters __read_mostly = 1;
Thomas Gleixner0793a612008-12-04 20:12:29 +010039static int perf_reserved_percpu __read_mostly;
40static int perf_overcommit __read_mostly = 1;
41
Peter Zijlstra7fc23a52009-05-08 18:52:21 +020042static atomic_t nr_counters __read_mostly;
Peter Zijlstra9ee318a2009-04-09 10:53:44 +020043static atomic_t nr_mmap_tracking __read_mostly;
44static atomic_t nr_munmap_tracking __read_mostly;
45static atomic_t nr_comm_tracking __read_mostly;
46
Peter Zijlstra1ccd1542009-04-09 10:53:45 +020047int sysctl_perf_counter_priv __read_mostly; /* do we need to be privileged */
Peter Zijlstrac5078f72009-05-05 17:50:24 +020048int sysctl_perf_counter_mlock __read_mostly = 128; /* 'free' kb per counter */
Peter Zijlstra1ccd1542009-04-09 10:53:45 +020049
Thomas Gleixner0793a612008-12-04 20:12:29 +010050/*
Ingo Molnar1dce8d92009-05-04 19:23:18 +020051 * Lock for (sysadmin-configurable) counter reservations:
Thomas Gleixner0793a612008-12-04 20:12:29 +010052 */
Ingo Molnar1dce8d92009-05-04 19:23:18 +020053static DEFINE_SPINLOCK(perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +010054
55/*
56 * Architecture provided APIs - weak aliases:
57 */
Robert Richter4aeb0b42009-04-29 12:47:03 +020058extern __weak const struct pmu *hw_perf_counter_init(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +010059{
Paul Mackerrasff6f0542009-01-09 16:19:25 +110060 return NULL;
Thomas Gleixner0793a612008-12-04 20:12:29 +010061}
62
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}
93EXPORT_SYMBOL_GPL(perf_disable); /* ACPI idle */
94
95void perf_enable(void)
96{
97 if (__perf_enable())
98 hw_perf_enable();
99}
100EXPORT_SYMBOL_GPL(perf_enable); /* ACPI idle */
101
Ingo Molnar04289bb2008-12-11 08:38:42 +0100102static void
103list_add_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
104{
105 struct perf_counter *group_leader = counter->group_leader;
106
107 /*
108 * Depending on whether it is a standalone or sibling counter,
109 * add it straight to the context's counter list, or to the group
110 * leader's sibling list:
111 */
Peter Zijlstra3df5eda2009-05-08 18:52:22 +0200112 if (group_leader == counter)
Ingo Molnar04289bb2008-12-11 08:38:42 +0100113 list_add_tail(&counter->list_entry, &ctx->counter_list);
Peter Zijlstra5c148192009-03-25 12:30:23 +0100114 else {
Ingo Molnar04289bb2008-12-11 08:38:42 +0100115 list_add_tail(&counter->list_entry, &group_leader->sibling_list);
Peter Zijlstra5c148192009-03-25 12:30:23 +0100116 group_leader->nr_siblings++;
117 }
Peter Zijlstra592903c2009-03-13 12:21:36 +0100118
119 list_add_rcu(&counter->event_entry, &ctx->event_list);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100120}
121
122static void
123list_del_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
124{
125 struct perf_counter *sibling, *tmp;
126
127 list_del_init(&counter->list_entry);
Peter Zijlstra592903c2009-03-13 12:21:36 +0100128 list_del_rcu(&counter->event_entry);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100129
Peter Zijlstra5c148192009-03-25 12:30:23 +0100130 if (counter->group_leader != counter)
131 counter->group_leader->nr_siblings--;
132
Ingo Molnar04289bb2008-12-11 08:38:42 +0100133 /*
134 * If this was a group counter with sibling counters then
135 * upgrade the siblings to singleton counters by adding them
136 * to the context list directly:
137 */
138 list_for_each_entry_safe(sibling, tmp,
139 &counter->sibling_list, list_entry) {
140
Peter Zijlstra75564232009-03-13 12:21:29 +0100141 list_move_tail(&sibling->list_entry, &ctx->counter_list);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100142 sibling->group_leader = sibling;
143 }
144}
145
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100146static void
147counter_sched_out(struct perf_counter *counter,
148 struct perf_cpu_context *cpuctx,
149 struct perf_counter_context *ctx)
150{
151 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
152 return;
153
154 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200155 counter->tstamp_stopped = ctx->time;
Robert Richter4aeb0b42009-04-29 12:47:03 +0200156 counter->pmu->disable(counter);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100157 counter->oncpu = -1;
158
159 if (!is_software_counter(counter))
160 cpuctx->active_oncpu--;
161 ctx->nr_active--;
162 if (counter->hw_event.exclusive || !cpuctx->active_oncpu)
163 cpuctx->exclusive = 0;
164}
165
Paul Mackerrasd859e292009-01-17 18:10:22 +1100166static void
167group_sched_out(struct perf_counter *group_counter,
168 struct perf_cpu_context *cpuctx,
169 struct perf_counter_context *ctx)
170{
171 struct perf_counter *counter;
172
173 if (group_counter->state != PERF_COUNTER_STATE_ACTIVE)
174 return;
175
176 counter_sched_out(group_counter, cpuctx, ctx);
177
178 /*
179 * Schedule out siblings (if any):
180 */
181 list_for_each_entry(counter, &group_counter->sibling_list, list_entry)
182 counter_sched_out(counter, cpuctx, ctx);
183
184 if (group_counter->hw_event.exclusive)
185 cpuctx->exclusive = 0;
186}
187
Thomas Gleixner0793a612008-12-04 20:12:29 +0100188/*
189 * Cross CPU call to remove a performance counter
190 *
191 * We disable the counter on the hardware level first. After that we
192 * remove it from the context list.
193 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100194static void __perf_counter_remove_from_context(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100195{
196 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
197 struct perf_counter *counter = info;
198 struct perf_counter_context *ctx = counter->ctx;
Ingo Molnar9b51f662008-12-12 13:49:45 +0100199 unsigned long flags;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100200
201 /*
202 * If this is a task context, we need to check whether it is
203 * the current task context of this cpu. If not it has been
204 * scheduled out before the smp call arrived.
205 */
206 if (ctx->task && cpuctx->task_ctx != ctx)
207 return;
208
Peter Zijlstra849691a2009-04-06 11:45:12 +0200209 spin_lock_irqsave(&ctx->lock, flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100210
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100211 counter_sched_out(counter, cpuctx, ctx);
212
213 counter->task = NULL;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100214 ctx->nr_counters--;
215
216 /*
217 * Protect the list operation against NMI by disabling the
218 * counters on a global level. NOP for non NMI based counters.
219 */
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200220 perf_disable();
Ingo Molnar04289bb2008-12-11 08:38:42 +0100221 list_del_counter(counter, ctx);
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200222 perf_enable();
Thomas Gleixner0793a612008-12-04 20:12:29 +0100223
224 if (!ctx->task) {
225 /*
226 * Allow more per task counters with respect to the
227 * reservation:
228 */
229 cpuctx->max_pertask =
230 min(perf_max_counters - ctx->nr_counters,
231 perf_max_counters - perf_reserved_percpu);
232 }
233
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)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100281 ctx->nr_counters--;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100282 list_del_counter(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100283 counter->task = NULL;
284 }
285 spin_unlock_irq(&ctx->lock);
286}
287
Peter Zijlstra4af49982009-04-06 11:45:10 +0200288static inline u64 perf_clock(void)
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100289{
Peter Zijlstra4af49982009-04-06 11:45:10 +0200290 return cpu_clock(smp_processor_id());
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100291}
292
293/*
294 * Update the record of the current time in a context.
295 */
Peter Zijlstra4af49982009-04-06 11:45:10 +0200296static void update_context_time(struct perf_counter_context *ctx)
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100297{
Peter Zijlstra4af49982009-04-06 11:45:10 +0200298 u64 now = perf_clock();
299
300 ctx->time += now - ctx->timestamp;
301 ctx->timestamp = now;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100302}
303
304/*
305 * Update the total_time_enabled and total_time_running fields for a counter.
306 */
307static void update_counter_times(struct perf_counter *counter)
308{
309 struct perf_counter_context *ctx = counter->ctx;
310 u64 run_end;
311
Peter Zijlstra4af49982009-04-06 11:45:10 +0200312 if (counter->state < PERF_COUNTER_STATE_INACTIVE)
313 return;
314
315 counter->total_time_enabled = ctx->time - counter->tstamp_enabled;
316
317 if (counter->state == PERF_COUNTER_STATE_INACTIVE)
318 run_end = counter->tstamp_stopped;
319 else
320 run_end = ctx->time;
321
322 counter->total_time_running = run_end - counter->tstamp_running;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100323}
324
325/*
326 * Update total_time_enabled and total_time_running for all counters in a group.
327 */
328static void update_group_times(struct perf_counter *leader)
329{
330 struct perf_counter *counter;
331
332 update_counter_times(leader);
333 list_for_each_entry(counter, &leader->sibling_list, list_entry)
334 update_counter_times(counter);
335}
336
337/*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100338 * Cross CPU call to disable a performance counter
339 */
340static void __perf_counter_disable(void *info)
341{
342 struct perf_counter *counter = info;
343 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
344 struct perf_counter_context *ctx = counter->ctx;
345 unsigned long flags;
346
347 /*
348 * If this is a per-task counter, need to check whether this
349 * counter's task is the current task on this cpu.
350 */
351 if (ctx->task && cpuctx->task_ctx != ctx)
352 return;
353
Peter Zijlstra849691a2009-04-06 11:45:12 +0200354 spin_lock_irqsave(&ctx->lock, flags);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100355
356 /*
357 * If the counter is on, turn it off.
358 * If it is in error state, leave it in error state.
359 */
360 if (counter->state >= PERF_COUNTER_STATE_INACTIVE) {
Peter Zijlstra4af49982009-04-06 11:45:10 +0200361 update_context_time(ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100362 update_counter_times(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100363 if (counter == counter->group_leader)
364 group_sched_out(counter, cpuctx, ctx);
365 else
366 counter_sched_out(counter, cpuctx, ctx);
367 counter->state = PERF_COUNTER_STATE_OFF;
368 }
369
Peter Zijlstra849691a2009-04-06 11:45:12 +0200370 spin_unlock_irqrestore(&ctx->lock, flags);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100371}
372
373/*
374 * Disable a counter.
375 */
376static void perf_counter_disable(struct perf_counter *counter)
377{
378 struct perf_counter_context *ctx = counter->ctx;
379 struct task_struct *task = ctx->task;
380
381 if (!task) {
382 /*
383 * Disable the counter on the cpu that it's on
384 */
385 smp_call_function_single(counter->cpu, __perf_counter_disable,
386 counter, 1);
387 return;
388 }
389
390 retry:
391 task_oncpu_function_call(task, __perf_counter_disable, counter);
392
393 spin_lock_irq(&ctx->lock);
394 /*
395 * If the counter is still active, we need to retry the cross-call.
396 */
397 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
398 spin_unlock_irq(&ctx->lock);
399 goto retry;
400 }
401
402 /*
403 * Since we have the lock this context can't be scheduled
404 * in, so we can change the state safely.
405 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100406 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
407 update_counter_times(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100408 counter->state = PERF_COUNTER_STATE_OFF;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100409 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100410
411 spin_unlock_irq(&ctx->lock);
412}
413
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100414static int
415counter_sched_in(struct perf_counter *counter,
416 struct perf_cpu_context *cpuctx,
417 struct perf_counter_context *ctx,
418 int cpu)
419{
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100420 if (counter->state <= PERF_COUNTER_STATE_OFF)
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100421 return 0;
422
423 counter->state = PERF_COUNTER_STATE_ACTIVE;
424 counter->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */
425 /*
426 * The new state must be visible before we turn it on in the hardware:
427 */
428 smp_wmb();
429
Robert Richter4aeb0b42009-04-29 12:47:03 +0200430 if (counter->pmu->enable(counter)) {
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100431 counter->state = PERF_COUNTER_STATE_INACTIVE;
432 counter->oncpu = -1;
433 return -EAGAIN;
434 }
435
Peter Zijlstra4af49982009-04-06 11:45:10 +0200436 counter->tstamp_running += ctx->time - counter->tstamp_stopped;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100437
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100438 if (!is_software_counter(counter))
439 cpuctx->active_oncpu++;
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100440 ctx->nr_active++;
441
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100442 if (counter->hw_event.exclusive)
443 cpuctx->exclusive = 1;
444
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100445 return 0;
446}
447
Paul Mackerras6751b712009-05-11 12:08:02 +1000448static int
449group_sched_in(struct perf_counter *group_counter,
450 struct perf_cpu_context *cpuctx,
451 struct perf_counter_context *ctx,
452 int cpu)
453{
454 struct perf_counter *counter, *partial_group;
455 int ret;
456
457 if (group_counter->state == PERF_COUNTER_STATE_OFF)
458 return 0;
459
460 ret = hw_perf_group_sched_in(group_counter, cpuctx, ctx, cpu);
461 if (ret)
462 return ret < 0 ? ret : 0;
463
464 group_counter->prev_state = group_counter->state;
465 if (counter_sched_in(group_counter, cpuctx, ctx, cpu))
466 return -EAGAIN;
467
468 /*
469 * Schedule in siblings as one group (if any):
470 */
471 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
472 counter->prev_state = counter->state;
473 if (counter_sched_in(counter, cpuctx, ctx, cpu)) {
474 partial_group = counter;
475 goto group_error;
476 }
477 }
478
479 return 0;
480
481group_error:
482 /*
483 * Groups can be scheduled in as one unit only, so undo any
484 * partial group before returning:
485 */
486 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
487 if (counter == partial_group)
488 break;
489 counter_sched_out(counter, cpuctx, ctx);
490 }
491 counter_sched_out(group_counter, cpuctx, ctx);
492
493 return -EAGAIN;
494}
495
Thomas Gleixner0793a612008-12-04 20:12:29 +0100496/*
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100497 * Return 1 for a group consisting entirely of software counters,
498 * 0 if the group contains any hardware counters.
499 */
500static int is_software_only_group(struct perf_counter *leader)
501{
502 struct perf_counter *counter;
503
504 if (!is_software_counter(leader))
505 return 0;
Peter Zijlstra5c148192009-03-25 12:30:23 +0100506
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100507 list_for_each_entry(counter, &leader->sibling_list, list_entry)
508 if (!is_software_counter(counter))
509 return 0;
Peter Zijlstra5c148192009-03-25 12:30:23 +0100510
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100511 return 1;
512}
513
514/*
515 * Work out whether we can put this counter group on the CPU now.
516 */
517static int group_can_go_on(struct perf_counter *counter,
518 struct perf_cpu_context *cpuctx,
519 int can_add_hw)
520{
521 /*
522 * Groups consisting entirely of software counters can always go on.
523 */
524 if (is_software_only_group(counter))
525 return 1;
526 /*
527 * If an exclusive group is already on, no other hardware
528 * counters can go on.
529 */
530 if (cpuctx->exclusive)
531 return 0;
532 /*
533 * If this group is exclusive and there are already
534 * counters on the CPU, it can't go on.
535 */
536 if (counter->hw_event.exclusive && cpuctx->active_oncpu)
537 return 0;
538 /*
539 * Otherwise, try to add it if all previous groups were able
540 * to go on.
541 */
542 return can_add_hw;
543}
544
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100545static void add_counter_to_ctx(struct perf_counter *counter,
546 struct perf_counter_context *ctx)
547{
548 list_add_counter(counter, ctx);
549 ctx->nr_counters++;
550 counter->prev_state = PERF_COUNTER_STATE_OFF;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200551 counter->tstamp_enabled = ctx->time;
552 counter->tstamp_running = ctx->time;
553 counter->tstamp_stopped = ctx->time;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100554}
555
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100556/*
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100557 * Cross CPU call to install and enable a performance counter
Thomas Gleixner0793a612008-12-04 20:12:29 +0100558 */
559static void __perf_install_in_context(void *info)
560{
561 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
562 struct perf_counter *counter = info;
563 struct perf_counter_context *ctx = counter->ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100564 struct perf_counter *leader = counter->group_leader;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100565 int cpu = smp_processor_id();
Ingo Molnar9b51f662008-12-12 13:49:45 +0100566 unsigned long flags;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100567 int err;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100568
569 /*
570 * If this is a task context, we need to check whether it is
571 * the current task context of this cpu. If not it has been
572 * scheduled out before the smp call arrived.
573 */
574 if (ctx->task && cpuctx->task_ctx != ctx)
575 return;
576
Peter Zijlstra849691a2009-04-06 11:45:12 +0200577 spin_lock_irqsave(&ctx->lock, flags);
Peter Zijlstra4af49982009-04-06 11:45:10 +0200578 update_context_time(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100579
580 /*
581 * Protect the list operation against NMI by disabling the
582 * counters on a global level. NOP for non NMI based counters.
583 */
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200584 perf_disable();
Thomas Gleixner0793a612008-12-04 20:12:29 +0100585
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100586 add_counter_to_ctx(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100587
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100588 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100589 * Don't put the counter on if it is disabled or if
590 * it is in a group and the group isn't on.
591 */
592 if (counter->state != PERF_COUNTER_STATE_INACTIVE ||
593 (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE))
594 goto unlock;
595
596 /*
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100597 * An exclusive counter can't go on if there are already active
598 * hardware counters, and no hardware counter can go on if there
599 * is already an exclusive counter on.
600 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100601 if (!group_can_go_on(counter, cpuctx, 1))
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100602 err = -EEXIST;
603 else
604 err = counter_sched_in(counter, cpuctx, ctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100605
Paul Mackerrasd859e292009-01-17 18:10:22 +1100606 if (err) {
607 /*
608 * This counter couldn't go on. If it is in a group
609 * then we have to pull the whole group off.
610 * If the counter group is pinned then put it in error state.
611 */
612 if (leader != counter)
613 group_sched_out(leader, cpuctx, ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100614 if (leader->hw_event.pinned) {
615 update_group_times(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100616 leader->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100617 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100618 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100619
620 if (!err && !ctx->task && cpuctx->max_pertask)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100621 cpuctx->max_pertask--;
622
Paul Mackerrasd859e292009-01-17 18:10:22 +1100623 unlock:
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200624 perf_enable();
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100625
Peter Zijlstra849691a2009-04-06 11:45:12 +0200626 spin_unlock_irqrestore(&ctx->lock, flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100627}
628
629/*
630 * Attach a performance counter to a context
631 *
632 * First we add the counter to the list with the hardware enable bit
633 * in counter->hw_config cleared.
634 *
635 * If the counter is attached to a task which is on a CPU we use a smp
636 * call to enable it in the task context. The task might have been
637 * scheduled away, but we check this in the smp call again.
Paul Mackerrasd859e292009-01-17 18:10:22 +1100638 *
639 * Must be called with ctx->mutex held.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100640 */
641static void
642perf_install_in_context(struct perf_counter_context *ctx,
643 struct perf_counter *counter,
644 int cpu)
645{
646 struct task_struct *task = ctx->task;
647
Thomas Gleixner0793a612008-12-04 20:12:29 +0100648 if (!task) {
649 /*
650 * Per cpu counters are installed via an smp call and
651 * the install is always sucessful.
652 */
653 smp_call_function_single(cpu, __perf_install_in_context,
654 counter, 1);
655 return;
656 }
657
658 counter->task = task;
659retry:
660 task_oncpu_function_call(task, __perf_install_in_context,
661 counter);
662
663 spin_lock_irq(&ctx->lock);
664 /*
Thomas Gleixner0793a612008-12-04 20:12:29 +0100665 * we need to retry the smp call.
666 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100667 if (ctx->is_active && list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100668 spin_unlock_irq(&ctx->lock);
669 goto retry;
670 }
671
672 /*
673 * The lock prevents that this context is scheduled in so we
674 * can add the counter safely, if it the call above did not
675 * succeed.
676 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100677 if (list_empty(&counter->list_entry))
678 add_counter_to_ctx(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100679 spin_unlock_irq(&ctx->lock);
680}
681
Paul Mackerrasd859e292009-01-17 18:10:22 +1100682/*
683 * Cross CPU call to enable a performance counter
684 */
685static void __perf_counter_enable(void *info)
Ingo Molnar04289bb2008-12-11 08:38:42 +0100686{
Paul Mackerrasd859e292009-01-17 18:10:22 +1100687 struct perf_counter *counter = info;
688 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
689 struct perf_counter_context *ctx = counter->ctx;
690 struct perf_counter *leader = counter->group_leader;
691 unsigned long flags;
692 int err;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100693
694 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100695 * If this is a per-task counter, need to check whether this
696 * counter's task is the current task on this cpu.
Ingo Molnar04289bb2008-12-11 08:38:42 +0100697 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100698 if (ctx->task && cpuctx->task_ctx != ctx)
699 return;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100700
Peter Zijlstra849691a2009-04-06 11:45:12 +0200701 spin_lock_irqsave(&ctx->lock, flags);
Peter Zijlstra4af49982009-04-06 11:45:10 +0200702 update_context_time(ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100703
Paul Mackerrasc07c99b2009-02-13 22:10:34 +1100704 counter->prev_state = counter->state;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100705 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
706 goto unlock;
707 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200708 counter->tstamp_enabled = ctx->time - counter->total_time_enabled;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100709
710 /*
711 * If the counter is in a group and isn't the group leader,
712 * then don't put it on unless the group is on.
713 */
714 if (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE)
715 goto unlock;
716
Paul Mackerrase758a332009-05-12 21:59:01 +1000717 if (!group_can_go_on(counter, cpuctx, 1)) {
Paul Mackerrasd859e292009-01-17 18:10:22 +1100718 err = -EEXIST;
Paul Mackerrase758a332009-05-12 21:59:01 +1000719 } else {
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200720 perf_disable();
Paul Mackerrase758a332009-05-12 21:59:01 +1000721 if (counter == leader)
722 err = group_sched_in(counter, cpuctx, ctx,
723 smp_processor_id());
724 else
725 err = counter_sched_in(counter, cpuctx, ctx,
726 smp_processor_id());
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200727 perf_enable();
Paul Mackerrase758a332009-05-12 21:59:01 +1000728 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100729
730 if (err) {
731 /*
732 * If this counter can't go on and it's part of a
733 * group, then the whole group has to come off.
734 */
735 if (leader != counter)
736 group_sched_out(leader, cpuctx, ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100737 if (leader->hw_event.pinned) {
738 update_group_times(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100739 leader->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100740 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100741 }
742
743 unlock:
Peter Zijlstra849691a2009-04-06 11:45:12 +0200744 spin_unlock_irqrestore(&ctx->lock, flags);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100745}
746
747/*
748 * Enable a counter.
749 */
750static void perf_counter_enable(struct perf_counter *counter)
751{
752 struct perf_counter_context *ctx = counter->ctx;
753 struct task_struct *task = ctx->task;
754
755 if (!task) {
756 /*
757 * Enable the counter on the cpu that it's on
758 */
759 smp_call_function_single(counter->cpu, __perf_counter_enable,
760 counter, 1);
761 return;
762 }
763
764 spin_lock_irq(&ctx->lock);
765 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
766 goto out;
767
768 /*
769 * If the counter is in error state, clear that first.
770 * That way, if we see the counter in error state below, we
771 * know that it has gone back into error state, as distinct
772 * from the task having been scheduled away before the
773 * cross-call arrived.
774 */
775 if (counter->state == PERF_COUNTER_STATE_ERROR)
776 counter->state = PERF_COUNTER_STATE_OFF;
777
778 retry:
779 spin_unlock_irq(&ctx->lock);
780 task_oncpu_function_call(task, __perf_counter_enable, counter);
781
782 spin_lock_irq(&ctx->lock);
783
784 /*
785 * If the context is active and the counter is still off,
786 * we need to retry the cross-call.
787 */
788 if (ctx->is_active && counter->state == PERF_COUNTER_STATE_OFF)
789 goto retry;
790
791 /*
792 * Since we have the lock this context can't be scheduled
793 * in, so we can change the state safely.
794 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100795 if (counter->state == PERF_COUNTER_STATE_OFF) {
Paul Mackerrasd859e292009-01-17 18:10:22 +1100796 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200797 counter->tstamp_enabled =
798 ctx->time - counter->total_time_enabled;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100799 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100800 out:
801 spin_unlock_irq(&ctx->lock);
802}
803
Peter Zijlstra2023b352009-05-05 17:50:26 +0200804static int perf_counter_refresh(struct perf_counter *counter, int refresh)
Peter Zijlstra79f14642009-04-06 11:45:07 +0200805{
Peter Zijlstra2023b352009-05-05 17:50:26 +0200806 /*
807 * not supported on inherited counters
808 */
809 if (counter->hw_event.inherit)
810 return -EINVAL;
811
Peter Zijlstra79f14642009-04-06 11:45:07 +0200812 atomic_add(refresh, &counter->event_limit);
813 perf_counter_enable(counter);
Peter Zijlstra2023b352009-05-05 17:50:26 +0200814
815 return 0;
Peter Zijlstra79f14642009-04-06 11:45:07 +0200816}
817
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100818void __perf_counter_sched_out(struct perf_counter_context *ctx,
819 struct perf_cpu_context *cpuctx)
820{
821 struct perf_counter *counter;
822
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100823 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100824 ctx->is_active = 0;
825 if (likely(!ctx->nr_counters))
826 goto out;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200827 update_context_time(ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100828
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200829 perf_disable();
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100830 if (ctx->nr_active) {
831 list_for_each_entry(counter, &ctx->counter_list, list_entry)
832 group_sched_out(counter, cpuctx, ctx);
833 }
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200834 perf_enable();
Paul Mackerrasd859e292009-01-17 18:10:22 +1100835 out:
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100836 spin_unlock(&ctx->lock);
837}
838
Thomas Gleixner0793a612008-12-04 20:12:29 +0100839/*
840 * Called from scheduler to remove the counters of the current task,
841 * with interrupts disabled.
842 *
843 * We stop each counter and update the counter value in counter->count.
844 *
Ingo Molnar76715812008-12-17 14:20:28 +0100845 * This does not protect us against NMI, but disable()
Thomas Gleixner0793a612008-12-04 20:12:29 +0100846 * sets the disabled bit in the control field of counter _before_
847 * accessing the counter control register. If a NMI hits, then it will
848 * not restart the counter.
849 */
850void perf_counter_task_sched_out(struct task_struct *task, int cpu)
851{
852 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
853 struct perf_counter_context *ctx = &task->perf_counter_ctx;
Peter Zijlstra4a0deca2009-03-19 20:26:12 +0100854 struct pt_regs *regs;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100855
856 if (likely(!cpuctx->task_ctx))
857 return;
858
Peter Zijlstrabce379b2009-04-06 11:45:13 +0200859 update_context_time(ctx);
860
Peter Zijlstra4a0deca2009-03-19 20:26:12 +0100861 regs = task_pt_regs(task);
Peter Zijlstra78f13e92009-04-08 15:01:33 +0200862 perf_swcounter_event(PERF_COUNT_CONTEXT_SWITCHES, 1, 1, regs, 0);
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100863 __perf_counter_sched_out(ctx, cpuctx);
864
Thomas Gleixner0793a612008-12-04 20:12:29 +0100865 cpuctx->task_ctx = NULL;
866}
867
Paul Mackerrasa08b1592009-05-11 15:46:10 +1000868static void __perf_counter_task_sched_out(struct perf_counter_context *ctx)
869{
870 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
871
872 __perf_counter_sched_out(ctx, cpuctx);
873 cpuctx->task_ctx = NULL;
874}
875
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100876static void perf_counter_cpu_sched_out(struct perf_cpu_context *cpuctx)
Ingo Molnar04289bb2008-12-11 08:38:42 +0100877{
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100878 __perf_counter_sched_out(&cpuctx->ctx, cpuctx);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100879}
880
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100881static void
882__perf_counter_sched_in(struct perf_counter_context *ctx,
883 struct perf_cpu_context *cpuctx, int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100884{
Thomas Gleixner0793a612008-12-04 20:12:29 +0100885 struct perf_counter *counter;
Paul Mackerrasdd0e6ba2009-01-12 15:11:00 +1100886 int can_add_hw = 1;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100887
Thomas Gleixner0793a612008-12-04 20:12:29 +0100888 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100889 ctx->is_active = 1;
890 if (likely(!ctx->nr_counters))
891 goto out;
892
Peter Zijlstra4af49982009-04-06 11:45:10 +0200893 ctx->timestamp = perf_clock();
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100894
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200895 perf_disable();
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100896
897 /*
898 * First go through the list and put on any pinned groups
899 * in order to give them the best chance of going on.
900 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100901 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100902 if (counter->state <= PERF_COUNTER_STATE_OFF ||
903 !counter->hw_event.pinned)
904 continue;
905 if (counter->cpu != -1 && counter->cpu != cpu)
906 continue;
907
908 if (group_can_go_on(counter, cpuctx, 1))
909 group_sched_in(counter, cpuctx, ctx, cpu);
910
911 /*
912 * If this pinned group hasn't been scheduled,
913 * put it in error state.
914 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100915 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
916 update_group_times(counter);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100917 counter->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100918 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100919 }
920
921 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
922 /*
923 * Ignore counters in OFF or ERROR state, and
924 * ignore pinned counters since we did them already.
925 */
926 if (counter->state <= PERF_COUNTER_STATE_OFF ||
927 counter->hw_event.pinned)
928 continue;
929
Ingo Molnar04289bb2008-12-11 08:38:42 +0100930 /*
931 * Listen to the 'cpu' scheduling filter constraint
932 * of counters:
933 */
Thomas Gleixner0793a612008-12-04 20:12:29 +0100934 if (counter->cpu != -1 && counter->cpu != cpu)
935 continue;
936
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100937 if (group_can_go_on(counter, cpuctx, can_add_hw)) {
Paul Mackerrasdd0e6ba2009-01-12 15:11:00 +1100938 if (group_sched_in(counter, cpuctx, ctx, cpu))
939 can_add_hw = 0;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100940 }
Thomas Gleixner0793a612008-12-04 20:12:29 +0100941 }
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200942 perf_enable();
Paul Mackerrasd859e292009-01-17 18:10:22 +1100943 out:
Thomas Gleixner0793a612008-12-04 20:12:29 +0100944 spin_unlock(&ctx->lock);
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100945}
Ingo Molnar04289bb2008-12-11 08:38:42 +0100946
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100947/*
948 * Called from scheduler to add the counters of the current task
949 * with interrupts disabled.
950 *
951 * We restore the counter value and then enable it.
952 *
953 * This does not protect us against NMI, but enable()
954 * sets the enabled bit in the control field of counter _before_
955 * accessing the counter control register. If a NMI hits, then it will
956 * keep the counter running.
957 */
958void perf_counter_task_sched_in(struct task_struct *task, int cpu)
959{
960 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
961 struct perf_counter_context *ctx = &task->perf_counter_ctx;
962
963 __perf_counter_sched_in(ctx, cpuctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100964 cpuctx->task_ctx = ctx;
965}
966
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100967static void perf_counter_cpu_sched_in(struct perf_cpu_context *cpuctx, int cpu)
968{
969 struct perf_counter_context *ctx = &cpuctx->ctx;
970
971 __perf_counter_sched_in(ctx, cpuctx, cpu);
972}
973
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100974int perf_counter_task_disable(void)
975{
976 struct task_struct *curr = current;
977 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
978 struct perf_counter *counter;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100979 unsigned long flags;
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100980
981 if (likely(!ctx->nr_counters))
982 return 0;
983
Peter Zijlstra849691a2009-04-06 11:45:12 +0200984 local_irq_save(flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100985
Paul Mackerrasa08b1592009-05-11 15:46:10 +1000986 __perf_counter_task_sched_out(ctx);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100987
988 spin_lock(&ctx->lock);
989
990 /*
991 * Disable all the counters:
992 */
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200993 perf_disable();
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100994
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100995 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100996 if (counter->state != PERF_COUNTER_STATE_ERROR) {
997 update_group_times(counter);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100998 counter->state = PERF_COUNTER_STATE_OFF;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100999 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001000 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01001001
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001002 perf_enable();
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001003
Peter Zijlstra849691a2009-04-06 11:45:12 +02001004 spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001005
1006 return 0;
1007}
1008
1009int perf_counter_task_enable(void)
1010{
1011 struct task_struct *curr = current;
1012 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
1013 struct perf_counter *counter;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001014 unsigned long flags;
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001015 int cpu;
1016
1017 if (likely(!ctx->nr_counters))
1018 return 0;
1019
Peter Zijlstra849691a2009-04-06 11:45:12 +02001020 local_irq_save(flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001021 cpu = smp_processor_id();
1022
Paul Mackerrasa08b1592009-05-11 15:46:10 +10001023 __perf_counter_task_sched_out(ctx);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001024
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001025 spin_lock(&ctx->lock);
1026
1027 /*
1028 * Disable all the counters:
1029 */
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001030 perf_disable();
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001031
1032 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001033 if (counter->state > PERF_COUNTER_STATE_OFF)
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001034 continue;
Ingo Molnar6a930702008-12-11 15:17:03 +01001035 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +02001036 counter->tstamp_enabled =
1037 ctx->time - counter->total_time_enabled;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001038 counter->hw_event.disabled = 0;
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001039 }
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001040 perf_enable();
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001041
1042 spin_unlock(&ctx->lock);
1043
1044 perf_counter_task_sched_in(curr, cpu);
1045
Peter Zijlstra849691a2009-04-06 11:45:12 +02001046 local_irq_restore(flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001047
1048 return 0;
1049}
1050
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001051/*
1052 * Round-robin a context's counters:
1053 */
1054static void rotate_ctx(struct perf_counter_context *ctx)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001055{
Thomas Gleixner0793a612008-12-04 20:12:29 +01001056 struct perf_counter *counter;
1057
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001058 if (!ctx->nr_counters)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001059 return;
1060
Thomas Gleixner0793a612008-12-04 20:12:29 +01001061 spin_lock(&ctx->lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001062 /*
Ingo Molnar04289bb2008-12-11 08:38:42 +01001063 * Rotate the first entry last (works just fine for group counters too):
Thomas Gleixner0793a612008-12-04 20:12:29 +01001064 */
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001065 perf_disable();
Ingo Molnar04289bb2008-12-11 08:38:42 +01001066 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Peter Zijlstra75564232009-03-13 12:21:29 +01001067 list_move_tail(&counter->list_entry, &ctx->counter_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001068 break;
1069 }
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001070 perf_enable();
Thomas Gleixner0793a612008-12-04 20:12:29 +01001071
1072 spin_unlock(&ctx->lock);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001073}
Thomas Gleixner0793a612008-12-04 20:12:29 +01001074
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001075void perf_counter_task_tick(struct task_struct *curr, int cpu)
1076{
Peter Zijlstra7fc23a52009-05-08 18:52:21 +02001077 struct perf_cpu_context *cpuctx;
1078 struct perf_counter_context *ctx;
1079
1080 if (!atomic_read(&nr_counters))
1081 return;
1082
1083 cpuctx = &per_cpu(perf_cpu_context, cpu);
1084 ctx = &curr->perf_counter_ctx;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001085
Ingo Molnarb82914c2009-05-04 18:54:32 +02001086 perf_counter_cpu_sched_out(cpuctx);
Paul Mackerrasa08b1592009-05-11 15:46:10 +10001087 __perf_counter_task_sched_out(ctx);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001088
Ingo Molnarb82914c2009-05-04 18:54:32 +02001089 rotate_ctx(&cpuctx->ctx);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001090 rotate_ctx(ctx);
1091
Ingo Molnarb82914c2009-05-04 18:54:32 +02001092 perf_counter_cpu_sched_in(cpuctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001093 perf_counter_task_sched_in(curr, cpu);
1094}
1095
1096/*
Thomas Gleixner0793a612008-12-04 20:12:29 +01001097 * Cross CPU call to read the hardware counter
1098 */
Ingo Molnar76715812008-12-17 14:20:28 +01001099static void __read(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001100{
Ingo Molnar621a01e2008-12-11 12:46:46 +01001101 struct perf_counter *counter = info;
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001102 struct perf_counter_context *ctx = counter->ctx;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001103 unsigned long flags;
Ingo Molnar621a01e2008-12-11 12:46:46 +01001104
Peter Zijlstra849691a2009-04-06 11:45:12 +02001105 local_irq_save(flags);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001106 if (ctx->is_active)
Peter Zijlstra4af49982009-04-06 11:45:10 +02001107 update_context_time(ctx);
Robert Richter4aeb0b42009-04-29 12:47:03 +02001108 counter->pmu->read(counter);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001109 update_counter_times(counter);
Peter Zijlstra849691a2009-04-06 11:45:12 +02001110 local_irq_restore(flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001111}
1112
Ingo Molnar04289bb2008-12-11 08:38:42 +01001113static u64 perf_counter_read(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001114{
1115 /*
1116 * If counter is enabled and currently active on a CPU, update the
1117 * value in the counter structure:
1118 */
Ingo Molnar6a930702008-12-11 15:17:03 +01001119 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
Thomas Gleixner0793a612008-12-04 20:12:29 +01001120 smp_call_function_single(counter->oncpu,
Ingo Molnar76715812008-12-17 14:20:28 +01001121 __read, counter, 1);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001122 } else if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
1123 update_counter_times(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001124 }
1125
Ingo Molnaree060942008-12-13 09:00:03 +01001126 return atomic64_read(&counter->count);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001127}
1128
Thomas Gleixner0793a612008-12-04 20:12:29 +01001129static void put_context(struct perf_counter_context *ctx)
1130{
1131 if (ctx->task)
1132 put_task_struct(ctx->task);
1133}
1134
1135static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
1136{
1137 struct perf_cpu_context *cpuctx;
1138 struct perf_counter_context *ctx;
1139 struct task_struct *task;
1140
1141 /*
1142 * If cpu is not a wildcard then this is a percpu counter:
1143 */
1144 if (cpu != -1) {
1145 /* Must be root to operate on a CPU counter: */
Peter Zijlstra1ccd1542009-04-09 10:53:45 +02001146 if (sysctl_perf_counter_priv && !capable(CAP_SYS_ADMIN))
Thomas Gleixner0793a612008-12-04 20:12:29 +01001147 return ERR_PTR(-EACCES);
1148
1149 if (cpu < 0 || cpu > num_possible_cpus())
1150 return ERR_PTR(-EINVAL);
1151
1152 /*
1153 * We could be clever and allow to attach a counter to an
1154 * offline CPU and activate it when the CPU comes up, but
1155 * that's for later.
1156 */
1157 if (!cpu_isset(cpu, cpu_online_map))
1158 return ERR_PTR(-ENODEV);
1159
1160 cpuctx = &per_cpu(perf_cpu_context, cpu);
1161 ctx = &cpuctx->ctx;
1162
Thomas Gleixner0793a612008-12-04 20:12:29 +01001163 return ctx;
1164 }
1165
1166 rcu_read_lock();
1167 if (!pid)
1168 task = current;
1169 else
1170 task = find_task_by_vpid(pid);
1171 if (task)
1172 get_task_struct(task);
1173 rcu_read_unlock();
1174
1175 if (!task)
1176 return ERR_PTR(-ESRCH);
1177
1178 ctx = &task->perf_counter_ctx;
1179 ctx->task = task;
1180
1181 /* Reuse ptrace permission checks for now. */
1182 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
1183 put_context(ctx);
1184 return ERR_PTR(-EACCES);
1185 }
1186
1187 return ctx;
1188}
1189
Peter Zijlstra592903c2009-03-13 12:21:36 +01001190static void free_counter_rcu(struct rcu_head *head)
1191{
1192 struct perf_counter *counter;
1193
1194 counter = container_of(head, struct perf_counter, rcu_head);
1195 kfree(counter);
1196}
1197
Peter Zijlstra925d5192009-03-30 19:07:02 +02001198static void perf_pending_sync(struct perf_counter *counter);
1199
Peter Zijlstraf1600952009-03-19 20:26:16 +01001200static void free_counter(struct perf_counter *counter)
1201{
Peter Zijlstra925d5192009-03-30 19:07:02 +02001202 perf_pending_sync(counter);
1203
Peter Zijlstra7fc23a52009-05-08 18:52:21 +02001204 atomic_dec(&nr_counters);
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02001205 if (counter->hw_event.mmap)
1206 atomic_dec(&nr_mmap_tracking);
1207 if (counter->hw_event.munmap)
1208 atomic_dec(&nr_munmap_tracking);
1209 if (counter->hw_event.comm)
1210 atomic_dec(&nr_comm_tracking);
1211
Peter Zijlstrae077df42009-03-19 20:26:17 +01001212 if (counter->destroy)
1213 counter->destroy(counter);
1214
Peter Zijlstraf1600952009-03-19 20:26:16 +01001215 call_rcu(&counter->rcu_head, free_counter_rcu);
1216}
1217
Thomas Gleixner0793a612008-12-04 20:12:29 +01001218/*
1219 * Called when the last reference to the file is gone.
1220 */
1221static int perf_release(struct inode *inode, struct file *file)
1222{
1223 struct perf_counter *counter = file->private_data;
1224 struct perf_counter_context *ctx = counter->ctx;
1225
1226 file->private_data = NULL;
1227
Paul Mackerrasd859e292009-01-17 18:10:22 +11001228 mutex_lock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001229 mutex_lock(&counter->mutex);
1230
Ingo Molnar04289bb2008-12-11 08:38:42 +01001231 perf_counter_remove_from_context(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001232
1233 mutex_unlock(&counter->mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001234 mutex_unlock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001235
Peter Zijlstraf1600952009-03-19 20:26:16 +01001236 free_counter(counter);
Mike Galbraith5af75912009-02-11 10:53:37 +01001237 put_context(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001238
1239 return 0;
1240}
1241
1242/*
1243 * Read the performance counter - simple non blocking version for now
1244 */
1245static ssize_t
1246perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
1247{
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001248 u64 values[3];
1249 int n;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001250
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001251 /*
1252 * Return end-of-file for a read on a counter that is in
1253 * error state (i.e. because it was pinned but it couldn't be
1254 * scheduled on to the CPU at some point).
1255 */
1256 if (counter->state == PERF_COUNTER_STATE_ERROR)
1257 return 0;
1258
Thomas Gleixner0793a612008-12-04 20:12:29 +01001259 mutex_lock(&counter->mutex);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001260 values[0] = perf_counter_read(counter);
1261 n = 1;
1262 if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1263 values[n++] = counter->total_time_enabled +
1264 atomic64_read(&counter->child_total_time_enabled);
1265 if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1266 values[n++] = counter->total_time_running +
1267 atomic64_read(&counter->child_total_time_running);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001268 mutex_unlock(&counter->mutex);
1269
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001270 if (count < n * sizeof(u64))
1271 return -EINVAL;
1272 count = n * sizeof(u64);
1273
1274 if (copy_to_user(buf, values, count))
1275 return -EFAULT;
1276
1277 return count;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001278}
1279
1280static ssize_t
Thomas Gleixner0793a612008-12-04 20:12:29 +01001281perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
1282{
1283 struct perf_counter *counter = file->private_data;
1284
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001285 return perf_read_hw(counter, buf, count);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001286}
1287
1288static unsigned int perf_poll(struct file *file, poll_table *wait)
1289{
1290 struct perf_counter *counter = file->private_data;
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001291 struct perf_mmap_data *data;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001292 unsigned int events = POLL_HUP;
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001293
1294 rcu_read_lock();
1295 data = rcu_dereference(counter->data);
1296 if (data)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001297 events = atomic_xchg(&data->poll, 0);
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001298 rcu_read_unlock();
Thomas Gleixner0793a612008-12-04 20:12:29 +01001299
1300 poll_wait(file, &counter->waitq, wait);
1301
Thomas Gleixner0793a612008-12-04 20:12:29 +01001302 return events;
1303}
1304
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001305static void perf_counter_reset(struct perf_counter *counter)
1306{
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001307 (void)perf_counter_read(counter);
Paul Mackerras615a3f12009-05-11 15:50:21 +10001308 atomic64_set(&counter->count, 0);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001309 perf_counter_update_userpage(counter);
1310}
1311
1312static void perf_counter_for_each_sibling(struct perf_counter *counter,
1313 void (*func)(struct perf_counter *))
1314{
1315 struct perf_counter_context *ctx = counter->ctx;
1316 struct perf_counter *sibling;
1317
1318 spin_lock_irq(&ctx->lock);
1319 counter = counter->group_leader;
1320
1321 func(counter);
1322 list_for_each_entry(sibling, &counter->sibling_list, list_entry)
1323 func(sibling);
1324 spin_unlock_irq(&ctx->lock);
1325}
1326
1327static void perf_counter_for_each_child(struct perf_counter *counter,
1328 void (*func)(struct perf_counter *))
1329{
1330 struct perf_counter *child;
1331
1332 mutex_lock(&counter->mutex);
1333 func(counter);
1334 list_for_each_entry(child, &counter->child_list, child_list)
1335 func(child);
1336 mutex_unlock(&counter->mutex);
1337}
1338
1339static void perf_counter_for_each(struct perf_counter *counter,
1340 void (*func)(struct perf_counter *))
1341{
1342 struct perf_counter *child;
1343
1344 mutex_lock(&counter->mutex);
1345 perf_counter_for_each_sibling(counter, func);
1346 list_for_each_entry(child, &counter->child_list, child_list)
1347 perf_counter_for_each_sibling(child, func);
1348 mutex_unlock(&counter->mutex);
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001349}
1350
Paul Mackerrasd859e292009-01-17 18:10:22 +11001351static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1352{
1353 struct perf_counter *counter = file->private_data;
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001354 void (*func)(struct perf_counter *);
1355 u32 flags = arg;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001356
1357 switch (cmd) {
1358 case PERF_COUNTER_IOC_ENABLE:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001359 func = perf_counter_enable;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001360 break;
1361 case PERF_COUNTER_IOC_DISABLE:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001362 func = perf_counter_disable;
Peter Zijlstra79f14642009-04-06 11:45:07 +02001363 break;
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001364 case PERF_COUNTER_IOC_RESET:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001365 func = perf_counter_reset;
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001366 break;
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001367
1368 case PERF_COUNTER_IOC_REFRESH:
1369 return perf_counter_refresh(counter, arg);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001370 default:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001371 return -ENOTTY;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001372 }
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001373
1374 if (flags & PERF_IOC_FLAG_GROUP)
1375 perf_counter_for_each(counter, func);
1376 else
1377 perf_counter_for_each_child(counter, func);
1378
1379 return 0;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001380}
1381
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001382/*
1383 * Callers need to ensure there can be no nesting of this function, otherwise
1384 * the seqlock logic goes bad. We can not serialize this because the arch
1385 * code calls this from NMI context.
1386 */
1387void perf_counter_update_userpage(struct perf_counter *counter)
Paul Mackerras37d81822009-03-23 18:22:08 +01001388{
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001389 struct perf_mmap_data *data;
1390 struct perf_counter_mmap_page *userpg;
1391
1392 rcu_read_lock();
1393 data = rcu_dereference(counter->data);
1394 if (!data)
1395 goto unlock;
1396
1397 userpg = data->user_page;
Paul Mackerras37d81822009-03-23 18:22:08 +01001398
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001399 /*
1400 * Disable preemption so as to not let the corresponding user-space
1401 * spin too long if we get preempted.
1402 */
1403 preempt_disable();
Paul Mackerras37d81822009-03-23 18:22:08 +01001404 ++userpg->lock;
Peter Zijlstra92f22a32009-04-02 11:12:04 +02001405 barrier();
Paul Mackerras37d81822009-03-23 18:22:08 +01001406 userpg->index = counter->hw.idx;
1407 userpg->offset = atomic64_read(&counter->count);
1408 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
1409 userpg->offset -= atomic64_read(&counter->hw.prev_count);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001410
Peter Zijlstra92f22a32009-04-02 11:12:04 +02001411 barrier();
Paul Mackerras37d81822009-03-23 18:22:08 +01001412 ++userpg->lock;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001413 preempt_enable();
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001414unlock:
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001415 rcu_read_unlock();
Paul Mackerras37d81822009-03-23 18:22:08 +01001416}
1417
1418static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1419{
1420 struct perf_counter *counter = vma->vm_file->private_data;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001421 struct perf_mmap_data *data;
1422 int ret = VM_FAULT_SIGBUS;
Paul Mackerras37d81822009-03-23 18:22:08 +01001423
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001424 rcu_read_lock();
1425 data = rcu_dereference(counter->data);
1426 if (!data)
1427 goto unlock;
Paul Mackerras37d81822009-03-23 18:22:08 +01001428
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001429 if (vmf->pgoff == 0) {
1430 vmf->page = virt_to_page(data->user_page);
1431 } else {
1432 int nr = vmf->pgoff - 1;
1433
1434 if ((unsigned)nr > data->nr_pages)
1435 goto unlock;
1436
1437 vmf->page = virt_to_page(data->data_pages[nr]);
1438 }
Paul Mackerras37d81822009-03-23 18:22:08 +01001439 get_page(vmf->page);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001440 ret = 0;
1441unlock:
1442 rcu_read_unlock();
1443
1444 return ret;
1445}
1446
1447static int perf_mmap_data_alloc(struct perf_counter *counter, int nr_pages)
1448{
1449 struct perf_mmap_data *data;
1450 unsigned long size;
1451 int i;
1452
1453 WARN_ON(atomic_read(&counter->mmap_count));
1454
1455 size = sizeof(struct perf_mmap_data);
1456 size += nr_pages * sizeof(void *);
1457
1458 data = kzalloc(size, GFP_KERNEL);
1459 if (!data)
1460 goto fail;
1461
1462 data->user_page = (void *)get_zeroed_page(GFP_KERNEL);
1463 if (!data->user_page)
1464 goto fail_user_page;
1465
1466 for (i = 0; i < nr_pages; i++) {
1467 data->data_pages[i] = (void *)get_zeroed_page(GFP_KERNEL);
1468 if (!data->data_pages[i])
1469 goto fail_data_pages;
1470 }
1471
1472 data->nr_pages = nr_pages;
Peter Zijlstra22c15582009-05-05 17:50:25 +02001473 atomic_set(&data->lock, -1);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001474
1475 rcu_assign_pointer(counter->data, data);
1476
Paul Mackerras37d81822009-03-23 18:22:08 +01001477 return 0;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001478
1479fail_data_pages:
1480 for (i--; i >= 0; i--)
1481 free_page((unsigned long)data->data_pages[i]);
1482
1483 free_page((unsigned long)data->user_page);
1484
1485fail_user_page:
1486 kfree(data);
1487
1488fail:
1489 return -ENOMEM;
1490}
1491
1492static void __perf_mmap_data_free(struct rcu_head *rcu_head)
1493{
1494 struct perf_mmap_data *data = container_of(rcu_head,
1495 struct perf_mmap_data, rcu_head);
1496 int i;
1497
1498 free_page((unsigned long)data->user_page);
1499 for (i = 0; i < data->nr_pages; i++)
1500 free_page((unsigned long)data->data_pages[i]);
1501 kfree(data);
1502}
1503
1504static void perf_mmap_data_free(struct perf_counter *counter)
1505{
1506 struct perf_mmap_data *data = counter->data;
1507
1508 WARN_ON(atomic_read(&counter->mmap_count));
1509
1510 rcu_assign_pointer(counter->data, NULL);
1511 call_rcu(&data->rcu_head, __perf_mmap_data_free);
1512}
1513
1514static void perf_mmap_open(struct vm_area_struct *vma)
1515{
1516 struct perf_counter *counter = vma->vm_file->private_data;
1517
1518 atomic_inc(&counter->mmap_count);
1519}
1520
1521static void perf_mmap_close(struct vm_area_struct *vma)
1522{
1523 struct perf_counter *counter = vma->vm_file->private_data;
1524
1525 if (atomic_dec_and_mutex_lock(&counter->mmap_count,
1526 &counter->mmap_mutex)) {
Peter Zijlstrac5078f72009-05-05 17:50:24 +02001527 vma->vm_mm->locked_vm -= counter->data->nr_locked;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001528 perf_mmap_data_free(counter);
1529 mutex_unlock(&counter->mmap_mutex);
1530 }
Paul Mackerras37d81822009-03-23 18:22:08 +01001531}
1532
1533static struct vm_operations_struct perf_mmap_vmops = {
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001534 .open = perf_mmap_open,
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001535 .close = perf_mmap_close,
Paul Mackerras37d81822009-03-23 18:22:08 +01001536 .fault = perf_mmap_fault,
1537};
1538
1539static int perf_mmap(struct file *file, struct vm_area_struct *vma)
1540{
1541 struct perf_counter *counter = file->private_data;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001542 unsigned long vma_size;
1543 unsigned long nr_pages;
1544 unsigned long locked, lock_limit;
1545 int ret = 0;
Peter Zijlstrac5078f72009-05-05 17:50:24 +02001546 long extra;
Paul Mackerras37d81822009-03-23 18:22:08 +01001547
1548 if (!(vma->vm_flags & VM_SHARED) || (vma->vm_flags & VM_WRITE))
1549 return -EINVAL;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001550
1551 vma_size = vma->vm_end - vma->vm_start;
1552 nr_pages = (vma_size / PAGE_SIZE) - 1;
1553
Peter Zijlstra7730d862009-03-25 12:48:31 +01001554 /*
1555 * If we have data pages ensure they're a power-of-two number, so we
1556 * can do bitmasks instead of modulo.
1557 */
1558 if (nr_pages != 0 && !is_power_of_2(nr_pages))
Paul Mackerras37d81822009-03-23 18:22:08 +01001559 return -EINVAL;
1560
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001561 if (vma_size != PAGE_SIZE * (1 + nr_pages))
Paul Mackerras37d81822009-03-23 18:22:08 +01001562 return -EINVAL;
1563
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001564 if (vma->vm_pgoff != 0)
1565 return -EINVAL;
Paul Mackerras37d81822009-03-23 18:22:08 +01001566
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001567 mutex_lock(&counter->mmap_mutex);
1568 if (atomic_inc_not_zero(&counter->mmap_count)) {
1569 if (nr_pages != counter->data->nr_pages)
1570 ret = -EINVAL;
1571 goto unlock;
1572 }
1573
Peter Zijlstrac5078f72009-05-05 17:50:24 +02001574 extra = nr_pages /* + 1 only account the data pages */;
1575 extra -= sysctl_perf_counter_mlock >> (PAGE_SHIFT - 10);
1576 if (extra < 0)
1577 extra = 0;
1578
1579 locked = vma->vm_mm->locked_vm + extra;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001580
1581 lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
1582 lock_limit >>= PAGE_SHIFT;
1583
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001584 if ((locked > lock_limit) && !capable(CAP_IPC_LOCK)) {
1585 ret = -EPERM;
1586 goto unlock;
1587 }
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001588
1589 WARN_ON(counter->data);
1590 ret = perf_mmap_data_alloc(counter, nr_pages);
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001591 if (ret)
1592 goto unlock;
1593
1594 atomic_set(&counter->mmap_count, 1);
Peter Zijlstrac5078f72009-05-05 17:50:24 +02001595 vma->vm_mm->locked_vm += extra;
1596 counter->data->nr_locked = extra;
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001597unlock:
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001598 mutex_unlock(&counter->mmap_mutex);
Paul Mackerras37d81822009-03-23 18:22:08 +01001599
1600 vma->vm_flags &= ~VM_MAYWRITE;
1601 vma->vm_flags |= VM_RESERVED;
1602 vma->vm_ops = &perf_mmap_vmops;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001603
1604 return ret;
Paul Mackerras37d81822009-03-23 18:22:08 +01001605}
1606
Peter Zijlstra3c446b3d2009-04-06 11:45:01 +02001607static int perf_fasync(int fd, struct file *filp, int on)
1608{
1609 struct perf_counter *counter = filp->private_data;
1610 struct inode *inode = filp->f_path.dentry->d_inode;
1611 int retval;
1612
1613 mutex_lock(&inode->i_mutex);
1614 retval = fasync_helper(fd, filp, on, &counter->fasync);
1615 mutex_unlock(&inode->i_mutex);
1616
1617 if (retval < 0)
1618 return retval;
1619
1620 return 0;
1621}
1622
Thomas Gleixner0793a612008-12-04 20:12:29 +01001623static const struct file_operations perf_fops = {
1624 .release = perf_release,
1625 .read = perf_read,
1626 .poll = perf_poll,
Paul Mackerrasd859e292009-01-17 18:10:22 +11001627 .unlocked_ioctl = perf_ioctl,
1628 .compat_ioctl = perf_ioctl,
Paul Mackerras37d81822009-03-23 18:22:08 +01001629 .mmap = perf_mmap,
Peter Zijlstra3c446b3d2009-04-06 11:45:01 +02001630 .fasync = perf_fasync,
Thomas Gleixner0793a612008-12-04 20:12:29 +01001631};
1632
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001633/*
Peter Zijlstra925d5192009-03-30 19:07:02 +02001634 * Perf counter wakeup
1635 *
1636 * If there's data, ensure we set the poll() state and publish everything
1637 * to user-space before waking everybody up.
1638 */
1639
1640void perf_counter_wakeup(struct perf_counter *counter)
1641{
Peter Zijlstra925d5192009-03-30 19:07:02 +02001642 wake_up_all(&counter->waitq);
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02001643
1644 if (counter->pending_kill) {
1645 kill_fasync(&counter->fasync, SIGIO, counter->pending_kill);
1646 counter->pending_kill = 0;
1647 }
Peter Zijlstra925d5192009-03-30 19:07:02 +02001648}
1649
1650/*
1651 * Pending wakeups
1652 *
1653 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
1654 *
1655 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
1656 * single linked list and use cmpxchg() to add entries lockless.
1657 */
1658
Peter Zijlstra79f14642009-04-06 11:45:07 +02001659static void perf_pending_counter(struct perf_pending_entry *entry)
1660{
1661 struct perf_counter *counter = container_of(entry,
1662 struct perf_counter, pending);
1663
1664 if (counter->pending_disable) {
1665 counter->pending_disable = 0;
1666 perf_counter_disable(counter);
1667 }
1668
1669 if (counter->pending_wakeup) {
1670 counter->pending_wakeup = 0;
1671 perf_counter_wakeup(counter);
1672 }
1673}
1674
Peter Zijlstra671dec52009-04-06 11:45:02 +02001675#define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
Peter Zijlstra925d5192009-03-30 19:07:02 +02001676
Peter Zijlstra671dec52009-04-06 11:45:02 +02001677static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = {
Peter Zijlstra925d5192009-03-30 19:07:02 +02001678 PENDING_TAIL,
1679};
1680
Peter Zijlstra671dec52009-04-06 11:45:02 +02001681static void perf_pending_queue(struct perf_pending_entry *entry,
1682 void (*func)(struct perf_pending_entry *))
Peter Zijlstra925d5192009-03-30 19:07:02 +02001683{
Peter Zijlstra671dec52009-04-06 11:45:02 +02001684 struct perf_pending_entry **head;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001685
Peter Zijlstra671dec52009-04-06 11:45:02 +02001686 if (cmpxchg(&entry->next, NULL, PENDING_TAIL) != NULL)
Peter Zijlstra925d5192009-03-30 19:07:02 +02001687 return;
1688
Peter Zijlstra671dec52009-04-06 11:45:02 +02001689 entry->func = func;
1690
1691 head = &get_cpu_var(perf_pending_head);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001692
1693 do {
Peter Zijlstra671dec52009-04-06 11:45:02 +02001694 entry->next = *head;
1695 } while (cmpxchg(head, entry->next, entry) != entry->next);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001696
1697 set_perf_counter_pending();
1698
Peter Zijlstra671dec52009-04-06 11:45:02 +02001699 put_cpu_var(perf_pending_head);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001700}
1701
1702static int __perf_pending_run(void)
1703{
Peter Zijlstra671dec52009-04-06 11:45:02 +02001704 struct perf_pending_entry *list;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001705 int nr = 0;
1706
Peter Zijlstra671dec52009-04-06 11:45:02 +02001707 list = xchg(&__get_cpu_var(perf_pending_head), PENDING_TAIL);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001708 while (list != PENDING_TAIL) {
Peter Zijlstra671dec52009-04-06 11:45:02 +02001709 void (*func)(struct perf_pending_entry *);
1710 struct perf_pending_entry *entry = list;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001711
1712 list = list->next;
1713
Peter Zijlstra671dec52009-04-06 11:45:02 +02001714 func = entry->func;
1715 entry->next = NULL;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001716 /*
1717 * Ensure we observe the unqueue before we issue the wakeup,
1718 * so that we won't be waiting forever.
1719 * -- see perf_not_pending().
1720 */
1721 smp_wmb();
1722
Peter Zijlstra671dec52009-04-06 11:45:02 +02001723 func(entry);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001724 nr++;
1725 }
1726
1727 return nr;
1728}
1729
1730static inline int perf_not_pending(struct perf_counter *counter)
1731{
1732 /*
1733 * If we flush on whatever cpu we run, there is a chance we don't
1734 * need to wait.
1735 */
1736 get_cpu();
1737 __perf_pending_run();
1738 put_cpu();
1739
1740 /*
1741 * Ensure we see the proper queue state before going to sleep
1742 * so that we do not miss the wakeup. -- see perf_pending_handle()
1743 */
1744 smp_rmb();
Peter Zijlstra671dec52009-04-06 11:45:02 +02001745 return counter->pending.next == NULL;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001746}
1747
1748static void perf_pending_sync(struct perf_counter *counter)
1749{
1750 wait_event(counter->waitq, perf_not_pending(counter));
1751}
1752
1753void perf_counter_do_pending(void)
1754{
1755 __perf_pending_run();
1756}
1757
1758/*
Peter Zijlstra394ee072009-03-30 19:07:14 +02001759 * Callchain support -- arch specific
1760 */
1761
Peter Zijlstra9c03d882009-04-06 11:45:00 +02001762__weak struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
Peter Zijlstra394ee072009-03-30 19:07:14 +02001763{
1764 return NULL;
1765}
1766
1767/*
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001768 * Output
1769 */
1770
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001771struct perf_output_handle {
1772 struct perf_counter *counter;
1773 struct perf_mmap_data *data;
1774 unsigned int offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01001775 unsigned int head;
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001776 int nmi;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02001777 int overflow;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001778 int locked;
1779 unsigned long flags;
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001780};
1781
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001782static void perf_output_wakeup(struct perf_output_handle *handle)
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001783{
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001784 atomic_set(&handle->data->poll, POLL_IN);
1785
Peter Zijlstra671dec52009-04-06 11:45:02 +02001786 if (handle->nmi) {
Peter Zijlstra79f14642009-04-06 11:45:07 +02001787 handle->counter->pending_wakeup = 1;
Peter Zijlstra671dec52009-04-06 11:45:02 +02001788 perf_pending_queue(&handle->counter->pending,
Peter Zijlstra79f14642009-04-06 11:45:07 +02001789 perf_pending_counter);
Peter Zijlstra671dec52009-04-06 11:45:02 +02001790 } else
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001791 perf_counter_wakeup(handle->counter);
1792}
1793
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001794/*
1795 * Curious locking construct.
1796 *
1797 * We need to ensure a later event doesn't publish a head when a former
1798 * event isn't done writing. However since we need to deal with NMIs we
1799 * cannot fully serialize things.
1800 *
1801 * What we do is serialize between CPUs so we only have to deal with NMI
1802 * nesting on a single CPU.
1803 *
1804 * We only publish the head (and generate a wakeup) when the outer-most
1805 * event completes.
1806 */
1807static void perf_output_lock(struct perf_output_handle *handle)
1808{
1809 struct perf_mmap_data *data = handle->data;
1810 int cpu;
1811
1812 handle->locked = 0;
1813
1814 local_irq_save(handle->flags);
1815 cpu = smp_processor_id();
1816
1817 if (in_nmi() && atomic_read(&data->lock) == cpu)
1818 return;
1819
Peter Zijlstra22c15582009-05-05 17:50:25 +02001820 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001821 cpu_relax();
1822
1823 handle->locked = 1;
1824}
1825
1826static void perf_output_unlock(struct perf_output_handle *handle)
1827{
1828 struct perf_mmap_data *data = handle->data;
1829 int head, cpu;
1830
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02001831 data->done_head = data->head;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001832
1833 if (!handle->locked)
1834 goto out;
1835
1836again:
1837 /*
1838 * The xchg implies a full barrier that ensures all writes are done
1839 * before we publish the new head, matched by a rmb() in userspace when
1840 * reading this position.
1841 */
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02001842 while ((head = atomic_xchg(&data->done_head, 0)))
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001843 data->user_page->data_head = head;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001844
1845 /*
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02001846 * NMI can happen here, which means we can miss a done_head update.
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001847 */
1848
Peter Zijlstra22c15582009-05-05 17:50:25 +02001849 cpu = atomic_xchg(&data->lock, -1);
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001850 WARN_ON_ONCE(cpu != smp_processor_id());
1851
1852 /*
1853 * Therefore we have to validate we did not indeed do so.
1854 */
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02001855 if (unlikely(atomic_read(&data->done_head))) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001856 /*
1857 * Since we had it locked, we can lock it again.
1858 */
Peter Zijlstra22c15582009-05-05 17:50:25 +02001859 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001860 cpu_relax();
1861
1862 goto again;
1863 }
1864
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02001865 if (atomic_xchg(&data->wakeup, 0))
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001866 perf_output_wakeup(handle);
1867out:
1868 local_irq_restore(handle->flags);
1869}
1870
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001871static int perf_output_begin(struct perf_output_handle *handle,
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001872 struct perf_counter *counter, unsigned int size,
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02001873 int nmi, int overflow)
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001874{
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001875 struct perf_mmap_data *data;
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001876 unsigned int offset, head;
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001877
Peter Zijlstra2023b352009-05-05 17:50:26 +02001878 /*
1879 * For inherited counters we send all the output towards the parent.
1880 */
1881 if (counter->parent)
1882 counter = counter->parent;
1883
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001884 rcu_read_lock();
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001885 data = rcu_dereference(counter->data);
1886 if (!data)
1887 goto out;
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001888
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001889 handle->data = data;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02001890 handle->counter = counter;
1891 handle->nmi = nmi;
1892 handle->overflow = overflow;
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001893
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001894 if (!data->nr_pages)
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001895 goto fail;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001896
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001897 perf_output_lock(handle);
1898
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001899 do {
1900 offset = head = atomic_read(&data->head);
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001901 head += size;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001902 } while (atomic_cmpxchg(&data->head, offset, head) != offset);
1903
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001904 handle->offset = offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01001905 handle->head = head;
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02001906
1907 if ((offset >> PAGE_SHIFT) != (head >> PAGE_SHIFT))
1908 atomic_set(&data->wakeup, 1);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001909
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001910 return 0;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001911
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001912fail:
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001913 perf_output_wakeup(handle);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001914out:
1915 rcu_read_unlock();
1916
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001917 return -ENOSPC;
1918}
1919
1920static void perf_output_copy(struct perf_output_handle *handle,
1921 void *buf, unsigned int len)
1922{
1923 unsigned int pages_mask;
1924 unsigned int offset;
1925 unsigned int size;
1926 void **pages;
1927
1928 offset = handle->offset;
1929 pages_mask = handle->data->nr_pages - 1;
1930 pages = handle->data->data_pages;
1931
1932 do {
1933 unsigned int page_offset;
1934 int nr;
1935
1936 nr = (offset >> PAGE_SHIFT) & pages_mask;
1937 page_offset = offset & (PAGE_SIZE - 1);
1938 size = min_t(unsigned int, PAGE_SIZE - page_offset, len);
1939
1940 memcpy(pages[nr] + page_offset, buf, size);
1941
1942 len -= size;
1943 buf += size;
1944 offset += size;
1945 } while (len);
1946
1947 handle->offset = offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01001948
Peter Zijlstra53020fe2009-05-13 21:26:19 +02001949 /*
1950 * Check we didn't copy past our reservation window, taking the
1951 * possible unsigned int wrap into account.
1952 */
1953 WARN_ON_ONCE(((int)(handle->head - handle->offset)) < 0);
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001954}
1955
Peter Zijlstra5c148192009-03-25 12:30:23 +01001956#define perf_output_put(handle, x) \
1957 perf_output_copy((handle), &(x), sizeof(x))
1958
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001959static void perf_output_end(struct perf_output_handle *handle)
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001960{
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001961 struct perf_counter *counter = handle->counter;
1962 struct perf_mmap_data *data = handle->data;
1963
1964 int wakeup_events = counter->hw_event.wakeup_events;
Peter Zijlstrac4578102009-04-02 11:12:01 +02001965
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02001966 if (handle->overflow && wakeup_events) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001967 int events = atomic_inc_return(&data->events);
Peter Zijlstrac4578102009-04-02 11:12:01 +02001968 if (events >= wakeup_events) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001969 atomic_sub(wakeup_events, &data->events);
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02001970 atomic_set(&data->wakeup, 1);
Peter Zijlstrac4578102009-04-02 11:12:01 +02001971 }
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001972 }
1973
1974 perf_output_unlock(handle);
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001975 rcu_read_unlock();
1976}
1977
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02001978static void perf_counter_output(struct perf_counter *counter,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02001979 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001980{
Peter Zijlstra5ed00412009-03-30 19:07:12 +02001981 int ret;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02001982 u64 record_type = counter->hw_event.record_type;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02001983 struct perf_output_handle handle;
1984 struct perf_event_header header;
1985 u64 ip;
Peter Zijlstra5c148192009-03-25 12:30:23 +01001986 struct {
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01001987 u32 pid, tid;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02001988 } tid_entry;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02001989 struct {
1990 u64 event;
1991 u64 counter;
1992 } group_entry;
Peter Zijlstra394ee072009-03-30 19:07:14 +02001993 struct perf_callchain_entry *callchain = NULL;
1994 int callchain_size = 0;
Peter Zijlstra339f7c92009-04-06 11:45:06 +02001995 u64 time;
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02001996 struct {
1997 u32 cpu, reserved;
1998 } cpu_entry;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001999
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02002000 header.type = 0;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002001 header.size = sizeof(header);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002002
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02002003 header.misc = PERF_EVENT_MISC_OVERFLOW;
2004 header.misc |= user_mode(regs) ?
Peter Zijlstra6fab0192009-04-08 15:01:26 +02002005 PERF_EVENT_MISC_USER : PERF_EVENT_MISC_KERNEL;
2006
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002007 if (record_type & PERF_RECORD_IP) {
2008 ip = instruction_pointer(regs);
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02002009 header.type |= PERF_RECORD_IP;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002010 header.size += sizeof(ip);
2011 }
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002012
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002013 if (record_type & PERF_RECORD_TID) {
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002014 /* namespace issues */
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002015 tid_entry.pid = current->group_leader->pid;
2016 tid_entry.tid = current->pid;
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002017
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02002018 header.type |= PERF_RECORD_TID;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002019 header.size += sizeof(tid_entry);
2020 }
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002021
Peter Zijlstra4d855452009-04-08 15:01:32 +02002022 if (record_type & PERF_RECORD_TIME) {
2023 /*
2024 * Maybe do better on x86 and provide cpu_clock_nmi()
2025 */
2026 time = sched_clock();
2027
2028 header.type |= PERF_RECORD_TIME;
2029 header.size += sizeof(u64);
2030 }
2031
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002032 if (record_type & PERF_RECORD_ADDR) {
2033 header.type |= PERF_RECORD_ADDR;
2034 header.size += sizeof(u64);
2035 }
2036
Peter Zijlstraa85f61a2009-05-08 18:52:23 +02002037 if (record_type & PERF_RECORD_CONFIG) {
2038 header.type |= PERF_RECORD_CONFIG;
2039 header.size += sizeof(u64);
2040 }
2041
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002042 if (record_type & PERF_RECORD_CPU) {
2043 header.type |= PERF_RECORD_CPU;
2044 header.size += sizeof(cpu_entry);
2045
2046 cpu_entry.cpu = raw_smp_processor_id();
2047 }
2048
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002049 if (record_type & PERF_RECORD_GROUP) {
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02002050 header.type |= PERF_RECORD_GROUP;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002051 header.size += sizeof(u64) +
2052 counter->nr_siblings * sizeof(group_entry);
2053 }
2054
2055 if (record_type & PERF_RECORD_CALLCHAIN) {
Peter Zijlstra394ee072009-03-30 19:07:14 +02002056 callchain = perf_callchain(regs);
2057
2058 if (callchain) {
Peter Zijlstra9c03d882009-04-06 11:45:00 +02002059 callchain_size = (1 + callchain->nr) * sizeof(u64);
Peter Zijlstra394ee072009-03-30 19:07:14 +02002060
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02002061 header.type |= PERF_RECORD_CALLCHAIN;
Peter Zijlstra394ee072009-03-30 19:07:14 +02002062 header.size += callchain_size;
2063 }
2064 }
2065
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002066 ret = perf_output_begin(&handle, counter, header.size, nmi, 1);
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002067 if (ret)
2068 return;
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002069
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002070 perf_output_put(&handle, header);
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002071
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002072 if (record_type & PERF_RECORD_IP)
2073 perf_output_put(&handle, ip);
2074
2075 if (record_type & PERF_RECORD_TID)
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002076 perf_output_put(&handle, tid_entry);
2077
Peter Zijlstra4d855452009-04-08 15:01:32 +02002078 if (record_type & PERF_RECORD_TIME)
2079 perf_output_put(&handle, time);
2080
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002081 if (record_type & PERF_RECORD_ADDR)
2082 perf_output_put(&handle, addr);
2083
Peter Zijlstraa85f61a2009-05-08 18:52:23 +02002084 if (record_type & PERF_RECORD_CONFIG)
2085 perf_output_put(&handle, counter->hw_event.config);
2086
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002087 if (record_type & PERF_RECORD_CPU)
2088 perf_output_put(&handle, cpu_entry);
2089
Peter Zijlstra2023b352009-05-05 17:50:26 +02002090 /*
2091 * XXX PERF_RECORD_GROUP vs inherited counters seems difficult.
2092 */
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002093 if (record_type & PERF_RECORD_GROUP) {
2094 struct perf_counter *leader, *sub;
2095 u64 nr = counter->nr_siblings;
2096
2097 perf_output_put(&handle, nr);
2098
2099 leader = counter->group_leader;
2100 list_for_each_entry(sub, &leader->sibling_list, list_entry) {
2101 if (sub != counter)
Robert Richter4aeb0b42009-04-29 12:47:03 +02002102 sub->pmu->read(sub);
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002103
2104 group_entry.event = sub->hw_event.config;
2105 group_entry.counter = atomic64_read(&sub->count);
2106
2107 perf_output_put(&handle, group_entry);
2108 }
2109 }
2110
Peter Zijlstra394ee072009-03-30 19:07:14 +02002111 if (callchain)
2112 perf_output_copy(&handle, callchain, callchain_size);
2113
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002114 perf_output_end(&handle);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002115}
2116
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002117/*
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002118 * comm tracking
2119 */
2120
2121struct perf_comm_event {
2122 struct task_struct *task;
2123 char *comm;
2124 int comm_size;
2125
2126 struct {
2127 struct perf_event_header header;
2128
2129 u32 pid;
2130 u32 tid;
2131 } event;
2132};
2133
2134static void perf_counter_comm_output(struct perf_counter *counter,
2135 struct perf_comm_event *comm_event)
2136{
2137 struct perf_output_handle handle;
2138 int size = comm_event->event.header.size;
2139 int ret = perf_output_begin(&handle, counter, size, 0, 0);
2140
2141 if (ret)
2142 return;
2143
2144 perf_output_put(&handle, comm_event->event);
2145 perf_output_copy(&handle, comm_event->comm,
2146 comm_event->comm_size);
2147 perf_output_end(&handle);
2148}
2149
2150static int perf_counter_comm_match(struct perf_counter *counter,
2151 struct perf_comm_event *comm_event)
2152{
2153 if (counter->hw_event.comm &&
2154 comm_event->event.header.type == PERF_EVENT_COMM)
2155 return 1;
2156
2157 return 0;
2158}
2159
2160static void perf_counter_comm_ctx(struct perf_counter_context *ctx,
2161 struct perf_comm_event *comm_event)
2162{
2163 struct perf_counter *counter;
2164
2165 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2166 return;
2167
2168 rcu_read_lock();
2169 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2170 if (perf_counter_comm_match(counter, comm_event))
2171 perf_counter_comm_output(counter, comm_event);
2172 }
2173 rcu_read_unlock();
2174}
2175
2176static void perf_counter_comm_event(struct perf_comm_event *comm_event)
2177{
2178 struct perf_cpu_context *cpuctx;
2179 unsigned int size;
2180 char *comm = comm_event->task->comm;
2181
Ingo Molnar888fcee2009-04-09 09:48:22 +02002182 size = ALIGN(strlen(comm)+1, sizeof(u64));
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002183
2184 comm_event->comm = comm;
2185 comm_event->comm_size = size;
2186
2187 comm_event->event.header.size = sizeof(comm_event->event) + size;
2188
2189 cpuctx = &get_cpu_var(perf_cpu_context);
2190 perf_counter_comm_ctx(&cpuctx->ctx, comm_event);
2191 put_cpu_var(perf_cpu_context);
2192
2193 perf_counter_comm_ctx(&current->perf_counter_ctx, comm_event);
2194}
2195
2196void perf_counter_comm(struct task_struct *task)
2197{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002198 struct perf_comm_event comm_event;
2199
2200 if (!atomic_read(&nr_comm_tracking))
2201 return;
2202
2203 comm_event = (struct perf_comm_event){
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002204 .task = task,
2205 .event = {
2206 .header = { .type = PERF_EVENT_COMM, },
2207 .pid = task->group_leader->pid,
2208 .tid = task->pid,
2209 },
2210 };
2211
2212 perf_counter_comm_event(&comm_event);
2213}
2214
2215/*
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002216 * mmap tracking
2217 */
2218
2219struct perf_mmap_event {
2220 struct file *file;
2221 char *file_name;
2222 int file_size;
2223
2224 struct {
2225 struct perf_event_header header;
2226
2227 u32 pid;
2228 u32 tid;
2229 u64 start;
2230 u64 len;
2231 u64 pgoff;
2232 } event;
2233};
2234
2235static void perf_counter_mmap_output(struct perf_counter *counter,
2236 struct perf_mmap_event *mmap_event)
2237{
2238 struct perf_output_handle handle;
2239 int size = mmap_event->event.header.size;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002240 int ret = perf_output_begin(&handle, counter, size, 0, 0);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002241
2242 if (ret)
2243 return;
2244
2245 perf_output_put(&handle, mmap_event->event);
2246 perf_output_copy(&handle, mmap_event->file_name,
2247 mmap_event->file_size);
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002248 perf_output_end(&handle);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002249}
2250
2251static int perf_counter_mmap_match(struct perf_counter *counter,
2252 struct perf_mmap_event *mmap_event)
2253{
2254 if (counter->hw_event.mmap &&
2255 mmap_event->event.header.type == PERF_EVENT_MMAP)
2256 return 1;
2257
2258 if (counter->hw_event.munmap &&
2259 mmap_event->event.header.type == PERF_EVENT_MUNMAP)
2260 return 1;
2261
2262 return 0;
2263}
2264
2265static void perf_counter_mmap_ctx(struct perf_counter_context *ctx,
2266 struct perf_mmap_event *mmap_event)
2267{
2268 struct perf_counter *counter;
2269
2270 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2271 return;
2272
2273 rcu_read_lock();
2274 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2275 if (perf_counter_mmap_match(counter, mmap_event))
2276 perf_counter_mmap_output(counter, mmap_event);
2277 }
2278 rcu_read_unlock();
2279}
2280
2281static void perf_counter_mmap_event(struct perf_mmap_event *mmap_event)
2282{
2283 struct perf_cpu_context *cpuctx;
2284 struct file *file = mmap_event->file;
2285 unsigned int size;
2286 char tmp[16];
2287 char *buf = NULL;
2288 char *name;
2289
2290 if (file) {
2291 buf = kzalloc(PATH_MAX, GFP_KERNEL);
2292 if (!buf) {
2293 name = strncpy(tmp, "//enomem", sizeof(tmp));
2294 goto got_name;
2295 }
Peter Zijlstrad3d21c42009-04-09 10:53:46 +02002296 name = d_path(&file->f_path, buf, PATH_MAX);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002297 if (IS_ERR(name)) {
2298 name = strncpy(tmp, "//toolong", sizeof(tmp));
2299 goto got_name;
2300 }
2301 } else {
2302 name = strncpy(tmp, "//anon", sizeof(tmp));
2303 goto got_name;
2304 }
2305
2306got_name:
Ingo Molnar888fcee2009-04-09 09:48:22 +02002307 size = ALIGN(strlen(name)+1, sizeof(u64));
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002308
2309 mmap_event->file_name = name;
2310 mmap_event->file_size = size;
2311
2312 mmap_event->event.header.size = sizeof(mmap_event->event) + size;
2313
2314 cpuctx = &get_cpu_var(perf_cpu_context);
2315 perf_counter_mmap_ctx(&cpuctx->ctx, mmap_event);
2316 put_cpu_var(perf_cpu_context);
2317
2318 perf_counter_mmap_ctx(&current->perf_counter_ctx, mmap_event);
2319
2320 kfree(buf);
2321}
2322
2323void perf_counter_mmap(unsigned long addr, unsigned long len,
2324 unsigned long pgoff, struct file *file)
2325{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002326 struct perf_mmap_event mmap_event;
2327
2328 if (!atomic_read(&nr_mmap_tracking))
2329 return;
2330
2331 mmap_event = (struct perf_mmap_event){
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002332 .file = file,
2333 .event = {
2334 .header = { .type = PERF_EVENT_MMAP, },
2335 .pid = current->group_leader->pid,
2336 .tid = current->pid,
2337 .start = addr,
2338 .len = len,
2339 .pgoff = pgoff,
2340 },
2341 };
2342
2343 perf_counter_mmap_event(&mmap_event);
2344}
2345
2346void perf_counter_munmap(unsigned long addr, unsigned long len,
2347 unsigned long pgoff, struct file *file)
2348{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002349 struct perf_mmap_event mmap_event;
2350
2351 if (!atomic_read(&nr_munmap_tracking))
2352 return;
2353
2354 mmap_event = (struct perf_mmap_event){
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002355 .file = file,
2356 .event = {
2357 .header = { .type = PERF_EVENT_MUNMAP, },
2358 .pid = current->group_leader->pid,
2359 .tid = current->pid,
2360 .start = addr,
2361 .len = len,
2362 .pgoff = pgoff,
2363 },
2364 };
2365
2366 perf_counter_mmap_event(&mmap_event);
2367}
2368
2369/*
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002370 * Generic counter overflow handling.
2371 */
2372
2373int perf_counter_overflow(struct perf_counter *counter,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002374 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002375{
Peter Zijlstra79f14642009-04-06 11:45:07 +02002376 int events = atomic_read(&counter->event_limit);
2377 int ret = 0;
2378
Peter Zijlstra2023b352009-05-05 17:50:26 +02002379 /*
2380 * XXX event_limit might not quite work as expected on inherited
2381 * counters
2382 */
2383
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002384 counter->pending_kill = POLL_IN;
Peter Zijlstra79f14642009-04-06 11:45:07 +02002385 if (events && atomic_dec_and_test(&counter->event_limit)) {
2386 ret = 1;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002387 counter->pending_kill = POLL_HUP;
Peter Zijlstra79f14642009-04-06 11:45:07 +02002388 if (nmi) {
2389 counter->pending_disable = 1;
2390 perf_pending_queue(&counter->pending,
2391 perf_pending_counter);
2392 } else
2393 perf_counter_disable(counter);
2394 }
2395
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002396 perf_counter_output(counter, nmi, regs, addr);
Peter Zijlstra79f14642009-04-06 11:45:07 +02002397 return ret;
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002398}
2399
2400/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002401 * Generic software counter infrastructure
2402 */
2403
2404static void perf_swcounter_update(struct perf_counter *counter)
2405{
2406 struct hw_perf_counter *hwc = &counter->hw;
2407 u64 prev, now;
2408 s64 delta;
2409
2410again:
2411 prev = atomic64_read(&hwc->prev_count);
2412 now = atomic64_read(&hwc->count);
2413 if (atomic64_cmpxchg(&hwc->prev_count, prev, now) != prev)
2414 goto again;
2415
2416 delta = now - prev;
2417
2418 atomic64_add(delta, &counter->count);
2419 atomic64_sub(delta, &hwc->period_left);
2420}
2421
2422static void perf_swcounter_set_period(struct perf_counter *counter)
2423{
2424 struct hw_perf_counter *hwc = &counter->hw;
2425 s64 left = atomic64_read(&hwc->period_left);
2426 s64 period = hwc->irq_period;
2427
2428 if (unlikely(left <= -period)) {
2429 left = period;
2430 atomic64_set(&hwc->period_left, left);
2431 }
2432
2433 if (unlikely(left <= 0)) {
2434 left += period;
2435 atomic64_add(period, &hwc->period_left);
2436 }
2437
2438 atomic64_set(&hwc->prev_count, -left);
2439 atomic64_set(&hwc->count, -left);
2440}
2441
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002442static enum hrtimer_restart perf_swcounter_hrtimer(struct hrtimer *hrtimer)
2443{
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002444 enum hrtimer_restart ret = HRTIMER_RESTART;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002445 struct perf_counter *counter;
2446 struct pt_regs *regs;
2447
2448 counter = container_of(hrtimer, struct perf_counter, hw.hrtimer);
Robert Richter4aeb0b42009-04-29 12:47:03 +02002449 counter->pmu->read(counter);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002450
2451 regs = get_irq_regs();
2452 /*
2453 * In case we exclude kernel IPs or are somehow not in interrupt
2454 * context, provide the next best thing, the user IP.
2455 */
2456 if ((counter->hw_event.exclude_kernel || !regs) &&
2457 !counter->hw_event.exclude_user)
2458 regs = task_pt_regs(current);
2459
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002460 if (regs) {
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002461 if (perf_counter_overflow(counter, 0, regs, 0))
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002462 ret = HRTIMER_NORESTART;
2463 }
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002464
2465 hrtimer_forward_now(hrtimer, ns_to_ktime(counter->hw.irq_period));
2466
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002467 return ret;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002468}
2469
2470static void perf_swcounter_overflow(struct perf_counter *counter,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002471 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002472{
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002473 perf_swcounter_update(counter);
2474 perf_swcounter_set_period(counter);
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002475 if (perf_counter_overflow(counter, nmi, regs, addr))
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002476 /* soft-disable the counter */
2477 ;
2478
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002479}
2480
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002481static int perf_swcounter_match(struct perf_counter *counter,
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002482 enum perf_event_types type,
2483 u32 event, struct pt_regs *regs)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002484{
2485 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
2486 return 0;
2487
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002488 if (perf_event_raw(&counter->hw_event))
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002489 return 0;
2490
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002491 if (perf_event_type(&counter->hw_event) != type)
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002492 return 0;
2493
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002494 if (perf_event_id(&counter->hw_event) != event)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002495 return 0;
2496
2497 if (counter->hw_event.exclude_user && user_mode(regs))
2498 return 0;
2499
2500 if (counter->hw_event.exclude_kernel && !user_mode(regs))
2501 return 0;
2502
2503 return 1;
2504}
2505
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002506static void perf_swcounter_add(struct perf_counter *counter, u64 nr,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002507 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002508{
2509 int neg = atomic64_add_negative(nr, &counter->hw.count);
2510 if (counter->hw.irq_period && !neg)
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002511 perf_swcounter_overflow(counter, nmi, regs, addr);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002512}
2513
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002514static void perf_swcounter_ctx_event(struct perf_counter_context *ctx,
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002515 enum perf_event_types type, u32 event,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002516 u64 nr, int nmi, struct pt_regs *regs,
2517 u64 addr)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002518{
2519 struct perf_counter *counter;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002520
Peter Zijlstra01ef09d2009-03-19 20:26:11 +01002521 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002522 return;
2523
Peter Zijlstra592903c2009-03-13 12:21:36 +01002524 rcu_read_lock();
2525 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002526 if (perf_swcounter_match(counter, type, event, regs))
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002527 perf_swcounter_add(counter, nr, nmi, regs, addr);
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002528 }
Peter Zijlstra592903c2009-03-13 12:21:36 +01002529 rcu_read_unlock();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002530}
2531
Peter Zijlstra96f6d442009-03-23 18:22:07 +01002532static int *perf_swcounter_recursion_context(struct perf_cpu_context *cpuctx)
2533{
2534 if (in_nmi())
2535 return &cpuctx->recursion[3];
2536
2537 if (in_irq())
2538 return &cpuctx->recursion[2];
2539
2540 if (in_softirq())
2541 return &cpuctx->recursion[1];
2542
2543 return &cpuctx->recursion[0];
2544}
2545
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002546static void __perf_swcounter_event(enum perf_event_types type, u32 event,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002547 u64 nr, int nmi, struct pt_regs *regs,
2548 u64 addr)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002549{
2550 struct perf_cpu_context *cpuctx = &get_cpu_var(perf_cpu_context);
Peter Zijlstra96f6d442009-03-23 18:22:07 +01002551 int *recursion = perf_swcounter_recursion_context(cpuctx);
2552
2553 if (*recursion)
2554 goto out;
2555
2556 (*recursion)++;
2557 barrier();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002558
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002559 perf_swcounter_ctx_event(&cpuctx->ctx, type, event,
2560 nr, nmi, regs, addr);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002561 if (cpuctx->task_ctx) {
2562 perf_swcounter_ctx_event(cpuctx->task_ctx, type, event,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002563 nr, nmi, regs, addr);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002564 }
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002565
Peter Zijlstra96f6d442009-03-23 18:22:07 +01002566 barrier();
2567 (*recursion)--;
2568
2569out:
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002570 put_cpu_var(perf_cpu_context);
2571}
2572
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002573void
2574perf_swcounter_event(u32 event, u64 nr, int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002575{
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002576 __perf_swcounter_event(PERF_TYPE_SOFTWARE, event, nr, nmi, regs, addr);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002577}
2578
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002579static void perf_swcounter_read(struct perf_counter *counter)
2580{
2581 perf_swcounter_update(counter);
2582}
2583
2584static int perf_swcounter_enable(struct perf_counter *counter)
2585{
2586 perf_swcounter_set_period(counter);
2587 return 0;
2588}
2589
2590static void perf_swcounter_disable(struct perf_counter *counter)
2591{
2592 perf_swcounter_update(counter);
2593}
2594
Robert Richter4aeb0b42009-04-29 12:47:03 +02002595static const struct pmu perf_ops_generic = {
Peter Zijlstraac17dc82009-03-13 12:21:34 +01002596 .enable = perf_swcounter_enable,
2597 .disable = perf_swcounter_disable,
2598 .read = perf_swcounter_read,
2599};
2600
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002601/*
2602 * Software counter: cpu wall time clock
2603 */
2604
Paul Mackerras9abf8a02009-01-09 16:26:43 +11002605static void cpu_clock_perf_counter_update(struct perf_counter *counter)
2606{
2607 int cpu = raw_smp_processor_id();
2608 s64 prev;
2609 u64 now;
2610
2611 now = cpu_clock(cpu);
2612 prev = atomic64_read(&counter->hw.prev_count);
2613 atomic64_set(&counter->hw.prev_count, now);
2614 atomic64_add(now - prev, &counter->count);
2615}
2616
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002617static int cpu_clock_perf_counter_enable(struct perf_counter *counter)
2618{
2619 struct hw_perf_counter *hwc = &counter->hw;
2620 int cpu = raw_smp_processor_id();
2621
2622 atomic64_set(&hwc->prev_count, cpu_clock(cpu));
Peter Zijlstra039fc912009-03-13 16:43:47 +01002623 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2624 hwc->hrtimer.function = perf_swcounter_hrtimer;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002625 if (hwc->irq_period) {
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002626 __hrtimer_start_range_ns(&hwc->hrtimer,
2627 ns_to_ktime(hwc->irq_period), 0,
2628 HRTIMER_MODE_REL, 0);
2629 }
2630
2631 return 0;
2632}
2633
Ingo Molnar5c92d122008-12-11 13:21:10 +01002634static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
2635{
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002636 hrtimer_cancel(&counter->hw.hrtimer);
Paul Mackerras9abf8a02009-01-09 16:26:43 +11002637 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01002638}
2639
2640static void cpu_clock_perf_counter_read(struct perf_counter *counter)
2641{
Paul Mackerras9abf8a02009-01-09 16:26:43 +11002642 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01002643}
2644
Robert Richter4aeb0b42009-04-29 12:47:03 +02002645static const struct pmu perf_ops_cpu_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +01002646 .enable = cpu_clock_perf_counter_enable,
2647 .disable = cpu_clock_perf_counter_disable,
2648 .read = cpu_clock_perf_counter_read,
Ingo Molnar5c92d122008-12-11 13:21:10 +01002649};
2650
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01002651/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002652 * Software counter: task time clock
2653 */
2654
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02002655static void task_clock_perf_counter_update(struct perf_counter *counter, u64 now)
Ingo Molnarbae43c92008-12-11 14:03:20 +01002656{
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02002657 u64 prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002658 s64 delta;
Ingo Molnarbae43c92008-12-11 14:03:20 +01002659
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02002660 prev = atomic64_xchg(&counter->hw.prev_count, now);
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002661 delta = now - prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002662 atomic64_add(delta, &counter->count);
Ingo Molnarbae43c92008-12-11 14:03:20 +01002663}
2664
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01002665static int task_clock_perf_counter_enable(struct perf_counter *counter)
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002666{
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002667 struct hw_perf_counter *hwc = &counter->hw;
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02002668 u64 now;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002669
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02002670 now = counter->ctx->time;
2671
2672 atomic64_set(&hwc->prev_count, now);
Peter Zijlstra039fc912009-03-13 16:43:47 +01002673 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2674 hwc->hrtimer.function = perf_swcounter_hrtimer;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002675 if (hwc->irq_period) {
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002676 __hrtimer_start_range_ns(&hwc->hrtimer,
2677 ns_to_ktime(hwc->irq_period), 0,
2678 HRTIMER_MODE_REL, 0);
2679 }
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01002680
2681 return 0;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002682}
2683
2684static void task_clock_perf_counter_disable(struct perf_counter *counter)
2685{
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002686 hrtimer_cancel(&counter->hw.hrtimer);
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02002687 task_clock_perf_counter_update(counter, counter->ctx->time);
2688
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002689}
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01002690
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002691static void task_clock_perf_counter_read(struct perf_counter *counter)
2692{
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02002693 u64 time;
2694
2695 if (!in_nmi()) {
2696 update_context_time(counter->ctx);
2697 time = counter->ctx->time;
2698 } else {
2699 u64 now = perf_clock();
2700 u64 delta = now - counter->ctx->timestamp;
2701 time = counter->ctx->time + delta;
2702 }
2703
2704 task_clock_perf_counter_update(counter, time);
Ingo Molnarbae43c92008-12-11 14:03:20 +01002705}
2706
Robert Richter4aeb0b42009-04-29 12:47:03 +02002707static const struct pmu perf_ops_task_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +01002708 .enable = task_clock_perf_counter_enable,
2709 .disable = task_clock_perf_counter_disable,
2710 .read = task_clock_perf_counter_read,
Ingo Molnarbae43c92008-12-11 14:03:20 +01002711};
2712
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002713/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002714 * Software counter: cpu migrations
2715 */
2716
Paul Mackerras23a185c2009-02-09 22:42:47 +11002717static inline u64 get_cpu_migrations(struct perf_counter *counter)
Ingo Molnar6c594c22008-12-14 12:34:15 +01002718{
Paul Mackerras23a185c2009-02-09 22:42:47 +11002719 struct task_struct *curr = counter->ctx->task;
2720
2721 if (curr)
2722 return curr->se.nr_migrations;
2723 return cpu_nr_migrations(smp_processor_id());
Ingo Molnar6c594c22008-12-14 12:34:15 +01002724}
2725
2726static void cpu_migrations_perf_counter_update(struct perf_counter *counter)
2727{
2728 u64 prev, now;
2729 s64 delta;
2730
2731 prev = atomic64_read(&counter->hw.prev_count);
Paul Mackerras23a185c2009-02-09 22:42:47 +11002732 now = get_cpu_migrations(counter);
Ingo Molnar6c594c22008-12-14 12:34:15 +01002733
2734 atomic64_set(&counter->hw.prev_count, now);
2735
2736 delta = now - prev;
Ingo Molnar6c594c22008-12-14 12:34:15 +01002737
2738 atomic64_add(delta, &counter->count);
2739}
2740
2741static void cpu_migrations_perf_counter_read(struct perf_counter *counter)
2742{
2743 cpu_migrations_perf_counter_update(counter);
2744}
2745
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01002746static int cpu_migrations_perf_counter_enable(struct perf_counter *counter)
Ingo Molnar6c594c22008-12-14 12:34:15 +01002747{
Paul Mackerrasc07c99b2009-02-13 22:10:34 +11002748 if (counter->prev_state <= PERF_COUNTER_STATE_OFF)
2749 atomic64_set(&counter->hw.prev_count,
2750 get_cpu_migrations(counter));
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01002751 return 0;
Ingo Molnar6c594c22008-12-14 12:34:15 +01002752}
2753
2754static void cpu_migrations_perf_counter_disable(struct perf_counter *counter)
2755{
2756 cpu_migrations_perf_counter_update(counter);
2757}
2758
Robert Richter4aeb0b42009-04-29 12:47:03 +02002759static const struct pmu perf_ops_cpu_migrations = {
Ingo Molnar76715812008-12-17 14:20:28 +01002760 .enable = cpu_migrations_perf_counter_enable,
2761 .disable = cpu_migrations_perf_counter_disable,
2762 .read = cpu_migrations_perf_counter_read,
Ingo Molnar6c594c22008-12-14 12:34:15 +01002763};
2764
Peter Zijlstrae077df42009-03-19 20:26:17 +01002765#ifdef CONFIG_EVENT_PROFILE
2766void perf_tpcounter_event(int event_id)
2767{
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002768 struct pt_regs *regs = get_irq_regs();
2769
2770 if (!regs)
2771 regs = task_pt_regs(current);
2772
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002773 __perf_swcounter_event(PERF_TYPE_TRACEPOINT, event_id, 1, 1, regs, 0);
Peter Zijlstrae077df42009-03-19 20:26:17 +01002774}
Steven Whitehouseff7b1b42009-04-15 16:55:05 +01002775EXPORT_SYMBOL_GPL(perf_tpcounter_event);
Peter Zijlstrae077df42009-03-19 20:26:17 +01002776
2777extern int ftrace_profile_enable(int);
2778extern void ftrace_profile_disable(int);
2779
2780static void tp_perf_counter_destroy(struct perf_counter *counter)
2781{
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002782 ftrace_profile_disable(perf_event_id(&counter->hw_event));
Peter Zijlstrae077df42009-03-19 20:26:17 +01002783}
2784
Robert Richter4aeb0b42009-04-29 12:47:03 +02002785static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
Peter Zijlstrae077df42009-03-19 20:26:17 +01002786{
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002787 int event_id = perf_event_id(&counter->hw_event);
Peter Zijlstrae077df42009-03-19 20:26:17 +01002788 int ret;
2789
2790 ret = ftrace_profile_enable(event_id);
2791 if (ret)
2792 return NULL;
2793
2794 counter->destroy = tp_perf_counter_destroy;
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002795 counter->hw.irq_period = counter->hw_event.irq_period;
Peter Zijlstrae077df42009-03-19 20:26:17 +01002796
2797 return &perf_ops_generic;
2798}
2799#else
Robert Richter4aeb0b42009-04-29 12:47:03 +02002800static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
Peter Zijlstrae077df42009-03-19 20:26:17 +01002801{
2802 return NULL;
2803}
2804#endif
2805
Robert Richter4aeb0b42009-04-29 12:47:03 +02002806static const struct pmu *sw_perf_counter_init(struct perf_counter *counter)
Ingo Molnar5c92d122008-12-11 13:21:10 +01002807{
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002808 struct perf_counter_hw_event *hw_event = &counter->hw_event;
Robert Richter4aeb0b42009-04-29 12:47:03 +02002809 const struct pmu *pmu = NULL;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002810 struct hw_perf_counter *hwc = &counter->hw;
Ingo Molnar5c92d122008-12-11 13:21:10 +01002811
Paul Mackerras0475f9e2009-02-11 14:35:35 +11002812 /*
2813 * Software counters (currently) can't in general distinguish
2814 * between user, kernel and hypervisor events.
2815 * However, context switches and cpu migrations are considered
2816 * to be kernel events, and page faults are never hypervisor
2817 * events.
2818 */
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002819 switch (perf_event_id(&counter->hw_event)) {
Ingo Molnar5c92d122008-12-11 13:21:10 +01002820 case PERF_COUNT_CPU_CLOCK:
Robert Richter4aeb0b42009-04-29 12:47:03 +02002821 pmu = &perf_ops_cpu_clock;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002822
2823 if (hw_event->irq_period && hw_event->irq_period < 10000)
2824 hw_event->irq_period = 10000;
Ingo Molnar5c92d122008-12-11 13:21:10 +01002825 break;
Ingo Molnarbae43c92008-12-11 14:03:20 +01002826 case PERF_COUNT_TASK_CLOCK:
Paul Mackerras23a185c2009-02-09 22:42:47 +11002827 /*
2828 * If the user instantiates this as a per-cpu counter,
2829 * use the cpu_clock counter instead.
2830 */
2831 if (counter->ctx->task)
Robert Richter4aeb0b42009-04-29 12:47:03 +02002832 pmu = &perf_ops_task_clock;
Paul Mackerras23a185c2009-02-09 22:42:47 +11002833 else
Robert Richter4aeb0b42009-04-29 12:47:03 +02002834 pmu = &perf_ops_cpu_clock;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002835
2836 if (hw_event->irq_period && hw_event->irq_period < 10000)
2837 hw_event->irq_period = 10000;
Ingo Molnarbae43c92008-12-11 14:03:20 +01002838 break;
Ingo Molnare06c61a2008-12-14 14:44:31 +01002839 case PERF_COUNT_PAGE_FAULTS:
Peter Zijlstraac17dc82009-03-13 12:21:34 +01002840 case PERF_COUNT_PAGE_FAULTS_MIN:
2841 case PERF_COUNT_PAGE_FAULTS_MAJ:
Ingo Molnar5d6a27d2008-12-14 12:28:33 +01002842 case PERF_COUNT_CONTEXT_SWITCHES:
Robert Richter4aeb0b42009-04-29 12:47:03 +02002843 pmu = &perf_ops_generic;
Ingo Molnar5d6a27d2008-12-14 12:28:33 +01002844 break;
Ingo Molnar6c594c22008-12-14 12:34:15 +01002845 case PERF_COUNT_CPU_MIGRATIONS:
Paul Mackerras0475f9e2009-02-11 14:35:35 +11002846 if (!counter->hw_event.exclude_kernel)
Robert Richter4aeb0b42009-04-29 12:47:03 +02002847 pmu = &perf_ops_cpu_migrations;
Ingo Molnar6c594c22008-12-14 12:34:15 +01002848 break;
Ingo Molnar5c92d122008-12-11 13:21:10 +01002849 }
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002850
Robert Richter4aeb0b42009-04-29 12:47:03 +02002851 if (pmu)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002852 hwc->irq_period = hw_event->irq_period;
2853
Robert Richter4aeb0b42009-04-29 12:47:03 +02002854 return pmu;
Ingo Molnar5c92d122008-12-11 13:21:10 +01002855}
2856
Thomas Gleixner0793a612008-12-04 20:12:29 +01002857/*
2858 * Allocate and initialize a counter structure
2859 */
2860static struct perf_counter *
Ingo Molnar04289bb2008-12-11 08:38:42 +01002861perf_counter_alloc(struct perf_counter_hw_event *hw_event,
2862 int cpu,
Paul Mackerras23a185c2009-02-09 22:42:47 +11002863 struct perf_counter_context *ctx,
Ingo Molnar9b51f662008-12-12 13:49:45 +01002864 struct perf_counter *group_leader,
2865 gfp_t gfpflags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01002866{
Robert Richter4aeb0b42009-04-29 12:47:03 +02002867 const struct pmu *pmu;
Ingo Molnar621a01e2008-12-11 12:46:46 +01002868 struct perf_counter *counter;
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02002869 long err;
Thomas Gleixner0793a612008-12-04 20:12:29 +01002870
Ingo Molnar9b51f662008-12-12 13:49:45 +01002871 counter = kzalloc(sizeof(*counter), gfpflags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002872 if (!counter)
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02002873 return ERR_PTR(-ENOMEM);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002874
Ingo Molnar04289bb2008-12-11 08:38:42 +01002875 /*
2876 * Single counters are their own group leaders, with an
2877 * empty sibling list:
2878 */
2879 if (!group_leader)
2880 group_leader = counter;
2881
Thomas Gleixner0793a612008-12-04 20:12:29 +01002882 mutex_init(&counter->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01002883 INIT_LIST_HEAD(&counter->list_entry);
Peter Zijlstra592903c2009-03-13 12:21:36 +01002884 INIT_LIST_HEAD(&counter->event_entry);
Ingo Molnar04289bb2008-12-11 08:38:42 +01002885 INIT_LIST_HEAD(&counter->sibling_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002886 init_waitqueue_head(&counter->waitq);
2887
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002888 mutex_init(&counter->mmap_mutex);
2889
Paul Mackerrasd859e292009-01-17 18:10:22 +11002890 INIT_LIST_HEAD(&counter->child_list);
2891
Ingo Molnar9f66a382008-12-10 12:33:23 +01002892 counter->cpu = cpu;
2893 counter->hw_event = *hw_event;
Ingo Molnar04289bb2008-12-11 08:38:42 +01002894 counter->group_leader = group_leader;
Robert Richter4aeb0b42009-04-29 12:47:03 +02002895 counter->pmu = NULL;
Paul Mackerras23a185c2009-02-09 22:42:47 +11002896 counter->ctx = ctx;
Ingo Molnar621a01e2008-12-11 12:46:46 +01002897
Ingo Molnar235c7fc2008-12-21 14:43:25 +01002898 counter->state = PERF_COUNTER_STATE_INACTIVE;
Ingo Molnara86ed502008-12-17 00:43:10 +01002899 if (hw_event->disabled)
2900 counter->state = PERF_COUNTER_STATE_OFF;
2901
Robert Richter4aeb0b42009-04-29 12:47:03 +02002902 pmu = NULL;
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002903
Peter Zijlstra2023b352009-05-05 17:50:26 +02002904 /*
2905 * we currently do not support PERF_RECORD_GROUP on inherited counters
2906 */
2907 if (hw_event->inherit && (hw_event->record_type & PERF_RECORD_GROUP))
2908 goto done;
2909
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002910 if (perf_event_raw(hw_event)) {
Robert Richter4aeb0b42009-04-29 12:47:03 +02002911 pmu = hw_perf_counter_init(counter);
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002912 goto done;
2913 }
2914
2915 switch (perf_event_type(hw_event)) {
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002916 case PERF_TYPE_HARDWARE:
Robert Richter4aeb0b42009-04-29 12:47:03 +02002917 pmu = hw_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002918 break;
2919
2920 case PERF_TYPE_SOFTWARE:
Robert Richter4aeb0b42009-04-29 12:47:03 +02002921 pmu = sw_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002922 break;
2923
2924 case PERF_TYPE_TRACEPOINT:
Robert Richter4aeb0b42009-04-29 12:47:03 +02002925 pmu = tp_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002926 break;
2927 }
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002928done:
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02002929 err = 0;
Robert Richter4aeb0b42009-04-29 12:47:03 +02002930 if (!pmu)
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02002931 err = -EINVAL;
Robert Richter4aeb0b42009-04-29 12:47:03 +02002932 else if (IS_ERR(pmu))
2933 err = PTR_ERR(pmu);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02002934
2935 if (err) {
2936 kfree(counter);
2937 return ERR_PTR(err);
2938 }
2939
Robert Richter4aeb0b42009-04-29 12:47:03 +02002940 counter->pmu = pmu;
Thomas Gleixner0793a612008-12-04 20:12:29 +01002941
Peter Zijlstra7fc23a52009-05-08 18:52:21 +02002942 atomic_inc(&nr_counters);
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002943 if (counter->hw_event.mmap)
2944 atomic_inc(&nr_mmap_tracking);
2945 if (counter->hw_event.munmap)
2946 atomic_inc(&nr_munmap_tracking);
2947 if (counter->hw_event.comm)
2948 atomic_inc(&nr_comm_tracking);
2949
Thomas Gleixner0793a612008-12-04 20:12:29 +01002950 return counter;
2951}
2952
2953/**
Paul Mackerras2743a5b2009-03-04 20:36:51 +11002954 * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
Ingo Molnar9f66a382008-12-10 12:33:23 +01002955 *
2956 * @hw_event_uptr: event type attributes for monitoring/sampling
Thomas Gleixner0793a612008-12-04 20:12:29 +01002957 * @pid: target pid
Ingo Molnar9f66a382008-12-10 12:33:23 +01002958 * @cpu: target cpu
2959 * @group_fd: group leader counter fd
Thomas Gleixner0793a612008-12-04 20:12:29 +01002960 */
Paul Mackerras2743a5b2009-03-04 20:36:51 +11002961SYSCALL_DEFINE5(perf_counter_open,
Paul Mackerrasf3dfd262009-02-26 22:43:46 +11002962 const struct perf_counter_hw_event __user *, hw_event_uptr,
Paul Mackerras2743a5b2009-03-04 20:36:51 +11002963 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01002964{
Ingo Molnar04289bb2008-12-11 08:38:42 +01002965 struct perf_counter *counter, *group_leader;
Ingo Molnar9f66a382008-12-10 12:33:23 +01002966 struct perf_counter_hw_event hw_event;
Ingo Molnar04289bb2008-12-11 08:38:42 +01002967 struct perf_counter_context *ctx;
Ingo Molnar9b51f662008-12-12 13:49:45 +01002968 struct file *counter_file = NULL;
Ingo Molnar04289bb2008-12-11 08:38:42 +01002969 struct file *group_file = NULL;
2970 int fput_needed = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01002971 int fput_needed2 = 0;
Thomas Gleixner0793a612008-12-04 20:12:29 +01002972 int ret;
2973
Paul Mackerras2743a5b2009-03-04 20:36:51 +11002974 /* for future expandability... */
2975 if (flags)
2976 return -EINVAL;
2977
Ingo Molnar9f66a382008-12-10 12:33:23 +01002978 if (copy_from_user(&hw_event, hw_event_uptr, sizeof(hw_event)) != 0)
Thomas Gleixnereab656a2008-12-08 19:26:59 +01002979 return -EFAULT;
2980
Ingo Molnar04289bb2008-12-11 08:38:42 +01002981 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01002982 * Get the target context (task or percpu):
2983 */
2984 ctx = find_get_context(pid, cpu);
2985 if (IS_ERR(ctx))
2986 return PTR_ERR(ctx);
2987
2988 /*
2989 * Look up the group leader (we will attach this counter to it):
Ingo Molnar04289bb2008-12-11 08:38:42 +01002990 */
2991 group_leader = NULL;
2992 if (group_fd != -1) {
2993 ret = -EINVAL;
2994 group_file = fget_light(group_fd, &fput_needed);
2995 if (!group_file)
Ingo Molnarccff2862008-12-11 11:26:29 +01002996 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01002997 if (group_file->f_op != &perf_fops)
Ingo Molnarccff2862008-12-11 11:26:29 +01002998 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01002999
3000 group_leader = group_file->private_data;
3001 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01003002 * Do not allow a recursive hierarchy (this new sibling
3003 * becoming part of another group-sibling):
Ingo Molnar04289bb2008-12-11 08:38:42 +01003004 */
Ingo Molnarccff2862008-12-11 11:26:29 +01003005 if (group_leader->group_leader != group_leader)
3006 goto err_put_context;
3007 /*
3008 * Do not allow to attach to a group in a different
3009 * task or CPU context:
3010 */
3011 if (group_leader->ctx != ctx)
3012 goto err_put_context;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11003013 /*
3014 * Only a group leader can be exclusive or pinned
3015 */
3016 if (hw_event.exclusive || hw_event.pinned)
3017 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003018 }
3019
Paul Mackerras23a185c2009-02-09 22:42:47 +11003020 counter = perf_counter_alloc(&hw_event, cpu, ctx, group_leader,
3021 GFP_KERNEL);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003022 ret = PTR_ERR(counter);
3023 if (IS_ERR(counter))
Thomas Gleixner0793a612008-12-04 20:12:29 +01003024 goto err_put_context;
3025
Thomas Gleixner0793a612008-12-04 20:12:29 +01003026 ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
3027 if (ret < 0)
Ingo Molnar9b51f662008-12-12 13:49:45 +01003028 goto err_free_put_context;
3029
3030 counter_file = fget_light(ret, &fput_needed2);
3031 if (!counter_file)
3032 goto err_free_put_context;
3033
3034 counter->filp = counter_file;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003035 mutex_lock(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003036 perf_install_in_context(ctx, counter, cpu);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003037 mutex_unlock(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003038
3039 fput_light(counter_file, fput_needed2);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003040
Ingo Molnar04289bb2008-12-11 08:38:42 +01003041out_fput:
3042 fput_light(group_file, fput_needed);
3043
Thomas Gleixner0793a612008-12-04 20:12:29 +01003044 return ret;
3045
Ingo Molnar9b51f662008-12-12 13:49:45 +01003046err_free_put_context:
Thomas Gleixner0793a612008-12-04 20:12:29 +01003047 kfree(counter);
3048
3049err_put_context:
3050 put_context(ctx);
3051
Ingo Molnar04289bb2008-12-11 08:38:42 +01003052 goto out_fput;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003053}
3054
Ingo Molnar9b51f662008-12-12 13:49:45 +01003055/*
3056 * Initialize the perf_counter context in a task_struct:
3057 */
3058static void
3059__perf_counter_init_context(struct perf_counter_context *ctx,
3060 struct task_struct *task)
3061{
3062 memset(ctx, 0, sizeof(*ctx));
3063 spin_lock_init(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003064 mutex_init(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003065 INIT_LIST_HEAD(&ctx->counter_list);
Peter Zijlstra592903c2009-03-13 12:21:36 +01003066 INIT_LIST_HEAD(&ctx->event_list);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003067 ctx->task = task;
3068}
3069
3070/*
3071 * inherit a counter from parent task to child task:
3072 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11003073static struct perf_counter *
Ingo Molnar9b51f662008-12-12 13:49:45 +01003074inherit_counter(struct perf_counter *parent_counter,
3075 struct task_struct *parent,
3076 struct perf_counter_context *parent_ctx,
3077 struct task_struct *child,
Paul Mackerrasd859e292009-01-17 18:10:22 +11003078 struct perf_counter *group_leader,
Ingo Molnar9b51f662008-12-12 13:49:45 +01003079 struct perf_counter_context *child_ctx)
3080{
3081 struct perf_counter *child_counter;
3082
Paul Mackerrasd859e292009-01-17 18:10:22 +11003083 /*
3084 * Instead of creating recursive hierarchies of counters,
3085 * we link inherited counters back to the original parent,
3086 * which has a filp for sure, which we use as the reference
3087 * count:
3088 */
3089 if (parent_counter->parent)
3090 parent_counter = parent_counter->parent;
3091
Ingo Molnar9b51f662008-12-12 13:49:45 +01003092 child_counter = perf_counter_alloc(&parent_counter->hw_event,
Paul Mackerras23a185c2009-02-09 22:42:47 +11003093 parent_counter->cpu, child_ctx,
3094 group_leader, GFP_KERNEL);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003095 if (IS_ERR(child_counter))
3096 return child_counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003097
3098 /*
3099 * Link it up in the child's context:
3100 */
Ingo Molnar9b51f662008-12-12 13:49:45 +01003101 child_counter->task = child;
Paul Mackerras53cfbf52009-03-25 22:46:58 +11003102 add_counter_to_ctx(child_counter, child_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003103
3104 child_counter->parent = parent_counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003105 /*
3106 * inherit into child's child as well:
3107 */
3108 child_counter->hw_event.inherit = 1;
3109
3110 /*
3111 * Get a reference to the parent filp - we will fput it
3112 * when the child counter exits. This is safe to do because
3113 * we are in the parent and we know that the filp still
3114 * exists and has a nonzero count:
3115 */
3116 atomic_long_inc(&parent_counter->filp->f_count);
3117
Paul Mackerrasd859e292009-01-17 18:10:22 +11003118 /*
3119 * Link this into the parent counter's child list
3120 */
3121 mutex_lock(&parent_counter->mutex);
3122 list_add_tail(&child_counter->child_list, &parent_counter->child_list);
3123
3124 /*
3125 * Make the child state follow the state of the parent counter,
3126 * not its hw_event.disabled bit. We hold the parent's mutex,
3127 * so we won't race with perf_counter_{en,dis}able_family.
3128 */
3129 if (parent_counter->state >= PERF_COUNTER_STATE_INACTIVE)
3130 child_counter->state = PERF_COUNTER_STATE_INACTIVE;
3131 else
3132 child_counter->state = PERF_COUNTER_STATE_OFF;
3133
3134 mutex_unlock(&parent_counter->mutex);
3135
3136 return child_counter;
3137}
3138
3139static int inherit_group(struct perf_counter *parent_counter,
3140 struct task_struct *parent,
3141 struct perf_counter_context *parent_ctx,
3142 struct task_struct *child,
3143 struct perf_counter_context *child_ctx)
3144{
3145 struct perf_counter *leader;
3146 struct perf_counter *sub;
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003147 struct perf_counter *child_ctr;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003148
3149 leader = inherit_counter(parent_counter, parent, parent_ctx,
3150 child, NULL, child_ctx);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003151 if (IS_ERR(leader))
3152 return PTR_ERR(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003153 list_for_each_entry(sub, &parent_counter->sibling_list, list_entry) {
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003154 child_ctr = inherit_counter(sub, parent, parent_ctx,
3155 child, leader, child_ctx);
3156 if (IS_ERR(child_ctr))
3157 return PTR_ERR(child_ctr);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003158 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01003159 return 0;
3160}
3161
Paul Mackerrasd859e292009-01-17 18:10:22 +11003162static void sync_child_counter(struct perf_counter *child_counter,
3163 struct perf_counter *parent_counter)
3164{
3165 u64 parent_val, child_val;
3166
3167 parent_val = atomic64_read(&parent_counter->count);
3168 child_val = atomic64_read(&child_counter->count);
3169
3170 /*
3171 * Add back the child's count to the parent's count:
3172 */
3173 atomic64_add(child_val, &parent_counter->count);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11003174 atomic64_add(child_counter->total_time_enabled,
3175 &parent_counter->child_total_time_enabled);
3176 atomic64_add(child_counter->total_time_running,
3177 &parent_counter->child_total_time_running);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003178
3179 /*
3180 * Remove this counter from the parent's list
3181 */
3182 mutex_lock(&parent_counter->mutex);
3183 list_del_init(&child_counter->child_list);
3184 mutex_unlock(&parent_counter->mutex);
3185
3186 /*
3187 * Release the parent counter, if this was the last
3188 * reference to it.
3189 */
3190 fput(parent_counter->filp);
3191}
3192
Ingo Molnar9b51f662008-12-12 13:49:45 +01003193static void
3194__perf_counter_exit_task(struct task_struct *child,
3195 struct perf_counter *child_counter,
3196 struct perf_counter_context *child_ctx)
3197{
3198 struct perf_counter *parent_counter;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003199 struct perf_counter *sub, *tmp;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003200
3201 /*
Ingo Molnar235c7fc2008-12-21 14:43:25 +01003202 * If we do not self-reap then we have to wait for the
3203 * child task to unschedule (it will happen for sure),
3204 * so that its counter is at its final count. (This
3205 * condition triggers rarely - child tasks usually get
3206 * off their CPU before the parent has a chance to
3207 * get this far into the reaping action)
Ingo Molnar9b51f662008-12-12 13:49:45 +01003208 */
Ingo Molnar235c7fc2008-12-21 14:43:25 +01003209 if (child != current) {
3210 wait_task_inactive(child, 0);
3211 list_del_init(&child_counter->list_entry);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11003212 update_counter_times(child_counter);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01003213 } else {
Ingo Molnar0cc0c022008-12-14 23:20:36 +01003214 struct perf_cpu_context *cpuctx;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01003215 unsigned long flags;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01003216
3217 /*
3218 * Disable and unlink this counter.
3219 *
3220 * Be careful about zapping the list - IRQ/NMI context
3221 * could still be processing it:
3222 */
Peter Zijlstra849691a2009-04-06 11:45:12 +02003223 local_irq_save(flags);
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02003224 perf_disable();
Ingo Molnar0cc0c022008-12-14 23:20:36 +01003225
3226 cpuctx = &__get_cpu_var(perf_cpu_context);
3227
Paul Mackerrasd859e292009-01-17 18:10:22 +11003228 group_sched_out(child_counter, cpuctx, child_ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11003229 update_counter_times(child_counter);
Ingo Molnar0cc0c022008-12-14 23:20:36 +01003230
Ingo Molnar235c7fc2008-12-21 14:43:25 +01003231 list_del_init(&child_counter->list_entry);
3232
3233 child_ctx->nr_counters--;
3234
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02003235 perf_enable();
Peter Zijlstra849691a2009-04-06 11:45:12 +02003236 local_irq_restore(flags);
Ingo Molnar0cc0c022008-12-14 23:20:36 +01003237 }
3238
Ingo Molnar9b51f662008-12-12 13:49:45 +01003239 parent_counter = child_counter->parent;
3240 /*
3241 * It can happen that parent exits first, and has counters
3242 * that are still around due to the child reference. These
3243 * counters need to be zapped - but otherwise linger.
3244 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11003245 if (parent_counter) {
3246 sync_child_counter(child_counter, parent_counter);
3247 list_for_each_entry_safe(sub, tmp, &child_counter->sibling_list,
3248 list_entry) {
Paul Mackerras4bcf3492009-02-11 13:53:19 +01003249 if (sub->parent) {
Paul Mackerrasd859e292009-01-17 18:10:22 +11003250 sync_child_counter(sub, sub->parent);
Peter Zijlstraf1600952009-03-19 20:26:16 +01003251 free_counter(sub);
Paul Mackerras4bcf3492009-02-11 13:53:19 +01003252 }
Paul Mackerrasd859e292009-01-17 18:10:22 +11003253 }
Peter Zijlstraf1600952009-03-19 20:26:16 +01003254 free_counter(child_counter);
Paul Mackerras4bcf3492009-02-11 13:53:19 +01003255 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01003256}
3257
3258/*
Paul Mackerrasd859e292009-01-17 18:10:22 +11003259 * When a child task exits, feed back counter values to parent counters.
Ingo Molnar9b51f662008-12-12 13:49:45 +01003260 *
Paul Mackerrasd859e292009-01-17 18:10:22 +11003261 * Note: we may be running in child context, but the PID is not hashed
Ingo Molnar9b51f662008-12-12 13:49:45 +01003262 * anymore so new counters will not be added.
3263 */
3264void perf_counter_exit_task(struct task_struct *child)
3265{
3266 struct perf_counter *child_counter, *tmp;
3267 struct perf_counter_context *child_ctx;
3268
3269 child_ctx = &child->perf_counter_ctx;
3270
3271 if (likely(!child_ctx->nr_counters))
3272 return;
3273
3274 list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list,
3275 list_entry)
3276 __perf_counter_exit_task(child, child_counter, child_ctx);
3277}
3278
3279/*
3280 * Initialize the perf_counter context in task_struct
3281 */
3282void perf_counter_init_task(struct task_struct *child)
3283{
3284 struct perf_counter_context *child_ctx, *parent_ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003285 struct perf_counter *counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003286 struct task_struct *parent = current;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003287
3288 child_ctx = &child->perf_counter_ctx;
3289 parent_ctx = &parent->perf_counter_ctx;
3290
3291 __perf_counter_init_context(child_ctx, child);
3292
3293 /*
3294 * This is executed from the parent task context, so inherit
3295 * counters that have been marked for cloning:
3296 */
3297
3298 if (likely(!parent_ctx->nr_counters))
3299 return;
3300
3301 /*
3302 * Lock the parent list. No need to lock the child - not PID
3303 * hashed yet and not running, so nobody can access it.
3304 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11003305 mutex_lock(&parent_ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003306
3307 /*
3308 * We dont have to disable NMIs - we are only looking at
3309 * the list, not manipulating it:
3310 */
3311 list_for_each_entry(counter, &parent_ctx->counter_list, list_entry) {
Paul Mackerrasd859e292009-01-17 18:10:22 +11003312 if (!counter->hw_event.inherit)
Ingo Molnar9b51f662008-12-12 13:49:45 +01003313 continue;
3314
Paul Mackerrasd859e292009-01-17 18:10:22 +11003315 if (inherit_group(counter, parent,
Ingo Molnar9b51f662008-12-12 13:49:45 +01003316 parent_ctx, child, child_ctx))
3317 break;
3318 }
3319
Paul Mackerrasd859e292009-01-17 18:10:22 +11003320 mutex_unlock(&parent_ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003321}
3322
Ingo Molnar04289bb2008-12-11 08:38:42 +01003323static void __cpuinit perf_counter_init_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003324{
Ingo Molnar04289bb2008-12-11 08:38:42 +01003325 struct perf_cpu_context *cpuctx;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003326
Ingo Molnar04289bb2008-12-11 08:38:42 +01003327 cpuctx = &per_cpu(perf_cpu_context, cpu);
3328 __perf_counter_init_context(&cpuctx->ctx, NULL);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003329
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003330 spin_lock(&perf_resource_lock);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003331 cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu;
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003332 spin_unlock(&perf_resource_lock);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003333
Paul Mackerras01d02872009-01-14 13:44:19 +11003334 hw_perf_counter_setup(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003335}
3336
3337#ifdef CONFIG_HOTPLUG_CPU
Ingo Molnar04289bb2008-12-11 08:38:42 +01003338static void __perf_counter_exit_cpu(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003339{
3340 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
3341 struct perf_counter_context *ctx = &cpuctx->ctx;
3342 struct perf_counter *counter, *tmp;
3343
Ingo Molnar04289bb2008-12-11 08:38:42 +01003344 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry)
3345 __perf_counter_remove_from_context(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003346}
Ingo Molnar04289bb2008-12-11 08:38:42 +01003347static void perf_counter_exit_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003348{
Paul Mackerrasd859e292009-01-17 18:10:22 +11003349 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
3350 struct perf_counter_context *ctx = &cpuctx->ctx;
3351
3352 mutex_lock(&ctx->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003353 smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003354 mutex_unlock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003355}
3356#else
Ingo Molnar04289bb2008-12-11 08:38:42 +01003357static inline void perf_counter_exit_cpu(int cpu) { }
Thomas Gleixner0793a612008-12-04 20:12:29 +01003358#endif
3359
3360static int __cpuinit
3361perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
3362{
3363 unsigned int cpu = (long)hcpu;
3364
3365 switch (action) {
3366
3367 case CPU_UP_PREPARE:
3368 case CPU_UP_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01003369 perf_counter_init_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003370 break;
3371
3372 case CPU_DOWN_PREPARE:
3373 case CPU_DOWN_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01003374 perf_counter_exit_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003375 break;
3376
3377 default:
3378 break;
3379 }
3380
3381 return NOTIFY_OK;
3382}
3383
3384static struct notifier_block __cpuinitdata perf_cpu_nb = {
3385 .notifier_call = perf_cpu_notify,
3386};
3387
Ingo Molnar0d905bc2009-05-04 19:13:30 +02003388void __init perf_counter_init(void)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003389{
3390 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
3391 (void *)(long)smp_processor_id());
3392 register_cpu_notifier(&perf_cpu_nb);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003393}
Thomas Gleixner0793a612008-12-04 20:12:29 +01003394
3395static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
3396{
3397 return sprintf(buf, "%d\n", perf_reserved_percpu);
3398}
3399
3400static ssize_t
3401perf_set_reserve_percpu(struct sysdev_class *class,
3402 const char *buf,
3403 size_t count)
3404{
3405 struct perf_cpu_context *cpuctx;
3406 unsigned long val;
3407 int err, cpu, mpt;
3408
3409 err = strict_strtoul(buf, 10, &val);
3410 if (err)
3411 return err;
3412 if (val > perf_max_counters)
3413 return -EINVAL;
3414
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003415 spin_lock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003416 perf_reserved_percpu = val;
3417 for_each_online_cpu(cpu) {
3418 cpuctx = &per_cpu(perf_cpu_context, cpu);
3419 spin_lock_irq(&cpuctx->ctx.lock);
3420 mpt = min(perf_max_counters - cpuctx->ctx.nr_counters,
3421 perf_max_counters - perf_reserved_percpu);
3422 cpuctx->max_pertask = mpt;
3423 spin_unlock_irq(&cpuctx->ctx.lock);
3424 }
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003425 spin_unlock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003426
3427 return count;
3428}
3429
3430static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
3431{
3432 return sprintf(buf, "%d\n", perf_overcommit);
3433}
3434
3435static ssize_t
3436perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
3437{
3438 unsigned long val;
3439 int err;
3440
3441 err = strict_strtoul(buf, 10, &val);
3442 if (err)
3443 return err;
3444 if (val > 1)
3445 return -EINVAL;
3446
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003447 spin_lock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003448 perf_overcommit = val;
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003449 spin_unlock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003450
3451 return count;
3452}
3453
3454static SYSDEV_CLASS_ATTR(
3455 reserve_percpu,
3456 0644,
3457 perf_show_reserve_percpu,
3458 perf_set_reserve_percpu
3459 );
3460
3461static SYSDEV_CLASS_ATTR(
3462 overcommit,
3463 0644,
3464 perf_show_overcommit,
3465 perf_set_overcommit
3466 );
3467
3468static struct attribute *perfclass_attrs[] = {
3469 &attr_reserve_percpu.attr,
3470 &attr_overcommit.attr,
3471 NULL
3472};
3473
3474static struct attribute_group perfclass_attr_group = {
3475 .attrs = perfclass_attrs,
3476 .name = "perf_counters",
3477};
3478
3479static int __init perf_counter_sysfs_init(void)
3480{
3481 return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
3482 &perfclass_attr_group);
3483}
3484device_initcall(perf_counter_sysfs_init);