blob: 6e6834e0587e01238cc950015d635b632038e911 [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 Zijlstra9ee318a2009-04-09 10:53:44 +020042static atomic_t nr_mmap_tracking __read_mostly;
43static atomic_t nr_munmap_tracking __read_mostly;
44static atomic_t nr_comm_tracking __read_mostly;
45
Peter Zijlstra1ccd1542009-04-09 10:53:45 +020046int sysctl_perf_counter_priv __read_mostly; /* do we need to be privileged */
47
Thomas Gleixner0793a612008-12-04 20:12:29 +010048/*
Ingo Molnar1dce8d92009-05-04 19:23:18 +020049 * Lock for (sysadmin-configurable) counter reservations:
Thomas Gleixner0793a612008-12-04 20:12:29 +010050 */
Ingo Molnar1dce8d92009-05-04 19:23:18 +020051static DEFINE_SPINLOCK(perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +010052
53/*
54 * Architecture provided APIs - weak aliases:
55 */
Robert Richter4aeb0b42009-04-29 12:47:03 +020056extern __weak const struct pmu *hw_perf_counter_init(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +010057{
Paul Mackerrasff6f0542009-01-09 16:19:25 +110058 return NULL;
Thomas Gleixner0793a612008-12-04 20:12:29 +010059}
60
Ingo Molnar01b28382008-12-11 13:45:51 +010061u64 __weak hw_perf_save_disable(void) { return 0; }
Yinghai Lu01ea1cc2008-12-26 21:05:06 -080062void __weak hw_perf_restore(u64 ctrl) { barrier(); }
Paul Mackerras01d02872009-01-14 13:44:19 +110063void __weak hw_perf_counter_setup(int cpu) { barrier(); }
Paul Mackerras3cbed422009-01-09 16:43:42 +110064int __weak hw_perf_group_sched_in(struct perf_counter *group_leader,
65 struct perf_cpu_context *cpuctx,
66 struct perf_counter_context *ctx, int cpu)
67{
68 return 0;
69}
Thomas Gleixner0793a612008-12-04 20:12:29 +010070
Paul Mackerras4eb96fc2009-01-09 17:24:34 +110071void __weak perf_counter_print_debug(void) { }
72
Ingo Molnar04289bb2008-12-11 08:38:42 +010073static void
74list_add_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
75{
76 struct perf_counter *group_leader = counter->group_leader;
77
78 /*
79 * Depending on whether it is a standalone or sibling counter,
80 * add it straight to the context's counter list, or to the group
81 * leader's sibling list:
82 */
83 if (counter->group_leader == counter)
84 list_add_tail(&counter->list_entry, &ctx->counter_list);
Peter Zijlstra5c148192009-03-25 12:30:23 +010085 else {
Ingo Molnar04289bb2008-12-11 08:38:42 +010086 list_add_tail(&counter->list_entry, &group_leader->sibling_list);
Peter Zijlstra5c148192009-03-25 12:30:23 +010087 group_leader->nr_siblings++;
88 }
Peter Zijlstra592903c2009-03-13 12:21:36 +010089
90 list_add_rcu(&counter->event_entry, &ctx->event_list);
Ingo Molnar04289bb2008-12-11 08:38:42 +010091}
92
93static void
94list_del_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
95{
96 struct perf_counter *sibling, *tmp;
97
98 list_del_init(&counter->list_entry);
Peter Zijlstra592903c2009-03-13 12:21:36 +010099 list_del_rcu(&counter->event_entry);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100100
Peter Zijlstra5c148192009-03-25 12:30:23 +0100101 if (counter->group_leader != counter)
102 counter->group_leader->nr_siblings--;
103
Ingo Molnar04289bb2008-12-11 08:38:42 +0100104 /*
105 * If this was a group counter with sibling counters then
106 * upgrade the siblings to singleton counters by adding them
107 * to the context list directly:
108 */
109 list_for_each_entry_safe(sibling, tmp,
110 &counter->sibling_list, list_entry) {
111
Peter Zijlstra75564232009-03-13 12:21:29 +0100112 list_move_tail(&sibling->list_entry, &ctx->counter_list);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100113 sibling->group_leader = sibling;
114 }
115}
116
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100117static void
118counter_sched_out(struct perf_counter *counter,
119 struct perf_cpu_context *cpuctx,
120 struct perf_counter_context *ctx)
121{
122 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
123 return;
124
125 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200126 counter->tstamp_stopped = ctx->time;
Robert Richter4aeb0b42009-04-29 12:47:03 +0200127 counter->pmu->disable(counter);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100128 counter->oncpu = -1;
129
130 if (!is_software_counter(counter))
131 cpuctx->active_oncpu--;
132 ctx->nr_active--;
133 if (counter->hw_event.exclusive || !cpuctx->active_oncpu)
134 cpuctx->exclusive = 0;
135}
136
Paul Mackerrasd859e292009-01-17 18:10:22 +1100137static void
138group_sched_out(struct perf_counter *group_counter,
139 struct perf_cpu_context *cpuctx,
140 struct perf_counter_context *ctx)
141{
142 struct perf_counter *counter;
143
144 if (group_counter->state != PERF_COUNTER_STATE_ACTIVE)
145 return;
146
147 counter_sched_out(group_counter, cpuctx, ctx);
148
149 /*
150 * Schedule out siblings (if any):
151 */
152 list_for_each_entry(counter, &group_counter->sibling_list, list_entry)
153 counter_sched_out(counter, cpuctx, ctx);
154
155 if (group_counter->hw_event.exclusive)
156 cpuctx->exclusive = 0;
157}
158
Thomas Gleixner0793a612008-12-04 20:12:29 +0100159/*
160 * Cross CPU call to remove a performance counter
161 *
162 * We disable the counter on the hardware level first. After that we
163 * remove it from the context list.
164 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100165static void __perf_counter_remove_from_context(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100166{
167 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
168 struct perf_counter *counter = info;
169 struct perf_counter_context *ctx = counter->ctx;
Ingo Molnar9b51f662008-12-12 13:49:45 +0100170 unsigned long flags;
Ingo Molnar5c92d122008-12-11 13:21:10 +0100171 u64 perf_flags;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100172
173 /*
174 * If this is a task context, we need to check whether it is
175 * the current task context of this cpu. If not it has been
176 * scheduled out before the smp call arrived.
177 */
178 if (ctx->task && cpuctx->task_ctx != ctx)
179 return;
180
Peter Zijlstra849691a2009-04-06 11:45:12 +0200181 spin_lock_irqsave(&ctx->lock, flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100182
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100183 counter_sched_out(counter, cpuctx, ctx);
184
185 counter->task = NULL;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100186 ctx->nr_counters--;
187
188 /*
189 * Protect the list operation against NMI by disabling the
190 * counters on a global level. NOP for non NMI based counters.
191 */
Ingo Molnar01b28382008-12-11 13:45:51 +0100192 perf_flags = hw_perf_save_disable();
Ingo Molnar04289bb2008-12-11 08:38:42 +0100193 list_del_counter(counter, ctx);
Ingo Molnar01b28382008-12-11 13:45:51 +0100194 hw_perf_restore(perf_flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100195
196 if (!ctx->task) {
197 /*
198 * Allow more per task counters with respect to the
199 * reservation:
200 */
201 cpuctx->max_pertask =
202 min(perf_max_counters - ctx->nr_counters,
203 perf_max_counters - perf_reserved_percpu);
204 }
205
Peter Zijlstra849691a2009-04-06 11:45:12 +0200206 spin_unlock_irqrestore(&ctx->lock, flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100207}
208
209
210/*
211 * Remove the counter from a task's (or a CPU's) list of counters.
212 *
Paul Mackerrasd859e292009-01-17 18:10:22 +1100213 * Must be called with counter->mutex and ctx->mutex held.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100214 *
215 * CPU counters are removed with a smp call. For task counters we only
216 * call when the task is on a CPU.
217 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100218static void perf_counter_remove_from_context(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100219{
220 struct perf_counter_context *ctx = counter->ctx;
221 struct task_struct *task = ctx->task;
222
223 if (!task) {
224 /*
225 * Per cpu counters are removed via an smp call and
226 * the removal is always sucessful.
227 */
228 smp_call_function_single(counter->cpu,
Ingo Molnar04289bb2008-12-11 08:38:42 +0100229 __perf_counter_remove_from_context,
Thomas Gleixner0793a612008-12-04 20:12:29 +0100230 counter, 1);
231 return;
232 }
233
234retry:
Ingo Molnar04289bb2008-12-11 08:38:42 +0100235 task_oncpu_function_call(task, __perf_counter_remove_from_context,
Thomas Gleixner0793a612008-12-04 20:12:29 +0100236 counter);
237
238 spin_lock_irq(&ctx->lock);
239 /*
240 * If the context is active we need to retry the smp call.
241 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100242 if (ctx->nr_active && !list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100243 spin_unlock_irq(&ctx->lock);
244 goto retry;
245 }
246
247 /*
248 * The lock prevents that this context is scheduled in so we
Ingo Molnar04289bb2008-12-11 08:38:42 +0100249 * can remove the counter safely, if the call above did not
Thomas Gleixner0793a612008-12-04 20:12:29 +0100250 * succeed.
251 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100252 if (!list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100253 ctx->nr_counters--;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100254 list_del_counter(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100255 counter->task = NULL;
256 }
257 spin_unlock_irq(&ctx->lock);
258}
259
Peter Zijlstra4af49982009-04-06 11:45:10 +0200260static inline u64 perf_clock(void)
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100261{
Peter Zijlstra4af49982009-04-06 11:45:10 +0200262 return cpu_clock(smp_processor_id());
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100263}
264
265/*
266 * Update the record of the current time in a context.
267 */
Peter Zijlstra4af49982009-04-06 11:45:10 +0200268static void update_context_time(struct perf_counter_context *ctx)
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100269{
Peter Zijlstra4af49982009-04-06 11:45:10 +0200270 u64 now = perf_clock();
271
272 ctx->time += now - ctx->timestamp;
273 ctx->timestamp = now;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100274}
275
276/*
277 * Update the total_time_enabled and total_time_running fields for a counter.
278 */
279static void update_counter_times(struct perf_counter *counter)
280{
281 struct perf_counter_context *ctx = counter->ctx;
282 u64 run_end;
283
Peter Zijlstra4af49982009-04-06 11:45:10 +0200284 if (counter->state < PERF_COUNTER_STATE_INACTIVE)
285 return;
286
287 counter->total_time_enabled = ctx->time - counter->tstamp_enabled;
288
289 if (counter->state == PERF_COUNTER_STATE_INACTIVE)
290 run_end = counter->tstamp_stopped;
291 else
292 run_end = ctx->time;
293
294 counter->total_time_running = run_end - counter->tstamp_running;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100295}
296
297/*
298 * Update total_time_enabled and total_time_running for all counters in a group.
299 */
300static void update_group_times(struct perf_counter *leader)
301{
302 struct perf_counter *counter;
303
304 update_counter_times(leader);
305 list_for_each_entry(counter, &leader->sibling_list, list_entry)
306 update_counter_times(counter);
307}
308
309/*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100310 * Cross CPU call to disable a performance counter
311 */
312static void __perf_counter_disable(void *info)
313{
314 struct perf_counter *counter = info;
315 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
316 struct perf_counter_context *ctx = counter->ctx;
317 unsigned long flags;
318
319 /*
320 * If this is a per-task counter, need to check whether this
321 * counter's task is the current task on this cpu.
322 */
323 if (ctx->task && cpuctx->task_ctx != ctx)
324 return;
325
Peter Zijlstra849691a2009-04-06 11:45:12 +0200326 spin_lock_irqsave(&ctx->lock, flags);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100327
328 /*
329 * If the counter is on, turn it off.
330 * If it is in error state, leave it in error state.
331 */
332 if (counter->state >= PERF_COUNTER_STATE_INACTIVE) {
Peter Zijlstra4af49982009-04-06 11:45:10 +0200333 update_context_time(ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100334 update_counter_times(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100335 if (counter == counter->group_leader)
336 group_sched_out(counter, cpuctx, ctx);
337 else
338 counter_sched_out(counter, cpuctx, ctx);
339 counter->state = PERF_COUNTER_STATE_OFF;
340 }
341
Peter Zijlstra849691a2009-04-06 11:45:12 +0200342 spin_unlock_irqrestore(&ctx->lock, flags);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100343}
344
345/*
346 * Disable a counter.
347 */
348static void perf_counter_disable(struct perf_counter *counter)
349{
350 struct perf_counter_context *ctx = counter->ctx;
351 struct task_struct *task = ctx->task;
352
353 if (!task) {
354 /*
355 * Disable the counter on the cpu that it's on
356 */
357 smp_call_function_single(counter->cpu, __perf_counter_disable,
358 counter, 1);
359 return;
360 }
361
362 retry:
363 task_oncpu_function_call(task, __perf_counter_disable, counter);
364
365 spin_lock_irq(&ctx->lock);
366 /*
367 * If the counter is still active, we need to retry the cross-call.
368 */
369 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
370 spin_unlock_irq(&ctx->lock);
371 goto retry;
372 }
373
374 /*
375 * Since we have the lock this context can't be scheduled
376 * in, so we can change the state safely.
377 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100378 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
379 update_counter_times(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100380 counter->state = PERF_COUNTER_STATE_OFF;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100381 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100382
383 spin_unlock_irq(&ctx->lock);
384}
385
386/*
387 * Disable a counter and all its children.
388 */
389static void perf_counter_disable_family(struct perf_counter *counter)
390{
391 struct perf_counter *child;
392
393 perf_counter_disable(counter);
394
395 /*
396 * Lock the mutex to protect the list of children
397 */
398 mutex_lock(&counter->mutex);
399 list_for_each_entry(child, &counter->child_list, child_list)
400 perf_counter_disable(child);
401 mutex_unlock(&counter->mutex);
402}
403
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100404static int
405counter_sched_in(struct perf_counter *counter,
406 struct perf_cpu_context *cpuctx,
407 struct perf_counter_context *ctx,
408 int cpu)
409{
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100410 if (counter->state <= PERF_COUNTER_STATE_OFF)
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100411 return 0;
412
413 counter->state = PERF_COUNTER_STATE_ACTIVE;
414 counter->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */
415 /*
416 * The new state must be visible before we turn it on in the hardware:
417 */
418 smp_wmb();
419
Robert Richter4aeb0b42009-04-29 12:47:03 +0200420 if (counter->pmu->enable(counter)) {
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100421 counter->state = PERF_COUNTER_STATE_INACTIVE;
422 counter->oncpu = -1;
423 return -EAGAIN;
424 }
425
Peter Zijlstra4af49982009-04-06 11:45:10 +0200426 counter->tstamp_running += ctx->time - counter->tstamp_stopped;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100427
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100428 if (!is_software_counter(counter))
429 cpuctx->active_oncpu++;
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100430 ctx->nr_active++;
431
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100432 if (counter->hw_event.exclusive)
433 cpuctx->exclusive = 1;
434
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100435 return 0;
436}
437
Thomas Gleixner0793a612008-12-04 20:12:29 +0100438/*
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100439 * Return 1 for a group consisting entirely of software counters,
440 * 0 if the group contains any hardware counters.
441 */
442static int is_software_only_group(struct perf_counter *leader)
443{
444 struct perf_counter *counter;
445
446 if (!is_software_counter(leader))
447 return 0;
Peter Zijlstra5c148192009-03-25 12:30:23 +0100448
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100449 list_for_each_entry(counter, &leader->sibling_list, list_entry)
450 if (!is_software_counter(counter))
451 return 0;
Peter Zijlstra5c148192009-03-25 12:30:23 +0100452
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100453 return 1;
454}
455
456/*
457 * Work out whether we can put this counter group on the CPU now.
458 */
459static int group_can_go_on(struct perf_counter *counter,
460 struct perf_cpu_context *cpuctx,
461 int can_add_hw)
462{
463 /*
464 * Groups consisting entirely of software counters can always go on.
465 */
466 if (is_software_only_group(counter))
467 return 1;
468 /*
469 * If an exclusive group is already on, no other hardware
470 * counters can go on.
471 */
472 if (cpuctx->exclusive)
473 return 0;
474 /*
475 * If this group is exclusive and there are already
476 * counters on the CPU, it can't go on.
477 */
478 if (counter->hw_event.exclusive && cpuctx->active_oncpu)
479 return 0;
480 /*
481 * Otherwise, try to add it if all previous groups were able
482 * to go on.
483 */
484 return can_add_hw;
485}
486
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100487static void add_counter_to_ctx(struct perf_counter *counter,
488 struct perf_counter_context *ctx)
489{
490 list_add_counter(counter, ctx);
491 ctx->nr_counters++;
492 counter->prev_state = PERF_COUNTER_STATE_OFF;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200493 counter->tstamp_enabled = ctx->time;
494 counter->tstamp_running = ctx->time;
495 counter->tstamp_stopped = ctx->time;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100496}
497
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100498/*
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100499 * Cross CPU call to install and enable a performance counter
Thomas Gleixner0793a612008-12-04 20:12:29 +0100500 */
501static void __perf_install_in_context(void *info)
502{
503 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
504 struct perf_counter *counter = info;
505 struct perf_counter_context *ctx = counter->ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100506 struct perf_counter *leader = counter->group_leader;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100507 int cpu = smp_processor_id();
Ingo Molnar9b51f662008-12-12 13:49:45 +0100508 unsigned long flags;
Ingo Molnar5c92d122008-12-11 13:21:10 +0100509 u64 perf_flags;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100510 int err;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100511
512 /*
513 * If this is a task context, we need to check whether it is
514 * the current task context of this cpu. If not it has been
515 * scheduled out before the smp call arrived.
516 */
517 if (ctx->task && cpuctx->task_ctx != ctx)
518 return;
519
Peter Zijlstra849691a2009-04-06 11:45:12 +0200520 spin_lock_irqsave(&ctx->lock, flags);
Peter Zijlstra4af49982009-04-06 11:45:10 +0200521 update_context_time(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100522
523 /*
524 * Protect the list operation against NMI by disabling the
525 * counters on a global level. NOP for non NMI based counters.
526 */
Ingo Molnar01b28382008-12-11 13:45:51 +0100527 perf_flags = hw_perf_save_disable();
Thomas Gleixner0793a612008-12-04 20:12:29 +0100528
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100529 add_counter_to_ctx(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100530
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100531 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100532 * Don't put the counter on if it is disabled or if
533 * it is in a group and the group isn't on.
534 */
535 if (counter->state != PERF_COUNTER_STATE_INACTIVE ||
536 (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE))
537 goto unlock;
538
539 /*
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100540 * An exclusive counter can't go on if there are already active
541 * hardware counters, and no hardware counter can go on if there
542 * is already an exclusive counter on.
543 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100544 if (!group_can_go_on(counter, cpuctx, 1))
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100545 err = -EEXIST;
546 else
547 err = counter_sched_in(counter, cpuctx, ctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100548
Paul Mackerrasd859e292009-01-17 18:10:22 +1100549 if (err) {
550 /*
551 * This counter couldn't go on. If it is in a group
552 * then we have to pull the whole group off.
553 * If the counter group is pinned then put it in error state.
554 */
555 if (leader != counter)
556 group_sched_out(leader, cpuctx, ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100557 if (leader->hw_event.pinned) {
558 update_group_times(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100559 leader->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100560 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100561 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100562
563 if (!err && !ctx->task && cpuctx->max_pertask)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100564 cpuctx->max_pertask--;
565
Paul Mackerrasd859e292009-01-17 18:10:22 +1100566 unlock:
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100567 hw_perf_restore(perf_flags);
568
Peter Zijlstra849691a2009-04-06 11:45:12 +0200569 spin_unlock_irqrestore(&ctx->lock, flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100570}
571
572/*
573 * Attach a performance counter to a context
574 *
575 * First we add the counter to the list with the hardware enable bit
576 * in counter->hw_config cleared.
577 *
578 * If the counter is attached to a task which is on a CPU we use a smp
579 * call to enable it in the task context. The task might have been
580 * scheduled away, but we check this in the smp call again.
Paul Mackerrasd859e292009-01-17 18:10:22 +1100581 *
582 * Must be called with ctx->mutex held.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100583 */
584static void
585perf_install_in_context(struct perf_counter_context *ctx,
586 struct perf_counter *counter,
587 int cpu)
588{
589 struct task_struct *task = ctx->task;
590
Thomas Gleixner0793a612008-12-04 20:12:29 +0100591 if (!task) {
592 /*
593 * Per cpu counters are installed via an smp call and
594 * the install is always sucessful.
595 */
596 smp_call_function_single(cpu, __perf_install_in_context,
597 counter, 1);
598 return;
599 }
600
601 counter->task = task;
602retry:
603 task_oncpu_function_call(task, __perf_install_in_context,
604 counter);
605
606 spin_lock_irq(&ctx->lock);
607 /*
Thomas Gleixner0793a612008-12-04 20:12:29 +0100608 * we need to retry the smp call.
609 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100610 if (ctx->is_active && list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100611 spin_unlock_irq(&ctx->lock);
612 goto retry;
613 }
614
615 /*
616 * The lock prevents that this context is scheduled in so we
617 * can add the counter safely, if it the call above did not
618 * succeed.
619 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100620 if (list_empty(&counter->list_entry))
621 add_counter_to_ctx(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100622 spin_unlock_irq(&ctx->lock);
623}
624
Paul Mackerrasd859e292009-01-17 18:10:22 +1100625/*
626 * Cross CPU call to enable a performance counter
627 */
628static void __perf_counter_enable(void *info)
Ingo Molnar04289bb2008-12-11 08:38:42 +0100629{
Paul Mackerrasd859e292009-01-17 18:10:22 +1100630 struct perf_counter *counter = info;
631 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
632 struct perf_counter_context *ctx = counter->ctx;
633 struct perf_counter *leader = counter->group_leader;
634 unsigned long flags;
635 int err;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100636
637 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100638 * If this is a per-task counter, need to check whether this
639 * counter's task is the current task on this cpu.
Ingo Molnar04289bb2008-12-11 08:38:42 +0100640 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100641 if (ctx->task && cpuctx->task_ctx != ctx)
642 return;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100643
Peter Zijlstra849691a2009-04-06 11:45:12 +0200644 spin_lock_irqsave(&ctx->lock, flags);
Peter Zijlstra4af49982009-04-06 11:45:10 +0200645 update_context_time(ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100646
Paul Mackerrasc07c99b2009-02-13 22:10:34 +1100647 counter->prev_state = counter->state;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100648 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
649 goto unlock;
650 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200651 counter->tstamp_enabled = ctx->time - counter->total_time_enabled;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100652
653 /*
654 * If the counter is in a group and isn't the group leader,
655 * then don't put it on unless the group is on.
656 */
657 if (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE)
658 goto unlock;
659
660 if (!group_can_go_on(counter, cpuctx, 1))
661 err = -EEXIST;
662 else
663 err = counter_sched_in(counter, cpuctx, ctx,
664 smp_processor_id());
665
666 if (err) {
667 /*
668 * If this counter can't go on and it's part of a
669 * group, then the whole group has to come off.
670 */
671 if (leader != counter)
672 group_sched_out(leader, cpuctx, ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100673 if (leader->hw_event.pinned) {
674 update_group_times(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100675 leader->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100676 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100677 }
678
679 unlock:
Peter Zijlstra849691a2009-04-06 11:45:12 +0200680 spin_unlock_irqrestore(&ctx->lock, flags);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100681}
682
683/*
684 * Enable a counter.
685 */
686static void perf_counter_enable(struct perf_counter *counter)
687{
688 struct perf_counter_context *ctx = counter->ctx;
689 struct task_struct *task = ctx->task;
690
691 if (!task) {
692 /*
693 * Enable the counter on the cpu that it's on
694 */
695 smp_call_function_single(counter->cpu, __perf_counter_enable,
696 counter, 1);
697 return;
698 }
699
700 spin_lock_irq(&ctx->lock);
701 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
702 goto out;
703
704 /*
705 * If the counter is in error state, clear that first.
706 * That way, if we see the counter in error state below, we
707 * know that it has gone back into error state, as distinct
708 * from the task having been scheduled away before the
709 * cross-call arrived.
710 */
711 if (counter->state == PERF_COUNTER_STATE_ERROR)
712 counter->state = PERF_COUNTER_STATE_OFF;
713
714 retry:
715 spin_unlock_irq(&ctx->lock);
716 task_oncpu_function_call(task, __perf_counter_enable, counter);
717
718 spin_lock_irq(&ctx->lock);
719
720 /*
721 * If the context is active and the counter is still off,
722 * we need to retry the cross-call.
723 */
724 if (ctx->is_active && counter->state == PERF_COUNTER_STATE_OFF)
725 goto retry;
726
727 /*
728 * Since we have the lock this context can't be scheduled
729 * in, so we can change the state safely.
730 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100731 if (counter->state == PERF_COUNTER_STATE_OFF) {
Paul Mackerrasd859e292009-01-17 18:10:22 +1100732 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200733 counter->tstamp_enabled =
734 ctx->time - counter->total_time_enabled;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100735 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100736 out:
737 spin_unlock_irq(&ctx->lock);
738}
739
Peter Zijlstra79f14642009-04-06 11:45:07 +0200740static void perf_counter_refresh(struct perf_counter *counter, int refresh)
741{
742 atomic_add(refresh, &counter->event_limit);
743 perf_counter_enable(counter);
744}
745
Paul Mackerrasd859e292009-01-17 18:10:22 +1100746/*
747 * Enable a counter and all its children.
748 */
749static void perf_counter_enable_family(struct perf_counter *counter)
750{
751 struct perf_counter *child;
752
753 perf_counter_enable(counter);
754
755 /*
756 * Lock the mutex to protect the list of children
757 */
758 mutex_lock(&counter->mutex);
759 list_for_each_entry(child, &counter->child_list, child_list)
760 perf_counter_enable(child);
761 mutex_unlock(&counter->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100762}
763
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100764void __perf_counter_sched_out(struct perf_counter_context *ctx,
765 struct perf_cpu_context *cpuctx)
766{
767 struct perf_counter *counter;
Paul Mackerras3cbed422009-01-09 16:43:42 +1100768 u64 flags;
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100769
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100770 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100771 ctx->is_active = 0;
772 if (likely(!ctx->nr_counters))
773 goto out;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200774 update_context_time(ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100775
Paul Mackerras3cbed422009-01-09 16:43:42 +1100776 flags = hw_perf_save_disable();
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100777 if (ctx->nr_active) {
778 list_for_each_entry(counter, &ctx->counter_list, list_entry)
779 group_sched_out(counter, cpuctx, ctx);
780 }
Paul Mackerras3cbed422009-01-09 16:43:42 +1100781 hw_perf_restore(flags);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100782 out:
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100783 spin_unlock(&ctx->lock);
784}
785
Thomas Gleixner0793a612008-12-04 20:12:29 +0100786/*
787 * Called from scheduler to remove the counters of the current task,
788 * with interrupts disabled.
789 *
790 * We stop each counter and update the counter value in counter->count.
791 *
Ingo Molnar76715812008-12-17 14:20:28 +0100792 * This does not protect us against NMI, but disable()
Thomas Gleixner0793a612008-12-04 20:12:29 +0100793 * sets the disabled bit in the control field of counter _before_
794 * accessing the counter control register. If a NMI hits, then it will
795 * not restart the counter.
796 */
797void perf_counter_task_sched_out(struct task_struct *task, int cpu)
798{
799 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
800 struct perf_counter_context *ctx = &task->perf_counter_ctx;
Peter Zijlstra4a0deca2009-03-19 20:26:12 +0100801 struct pt_regs *regs;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100802
803 if (likely(!cpuctx->task_ctx))
804 return;
805
Peter Zijlstrabce379b2009-04-06 11:45:13 +0200806 update_context_time(ctx);
807
Peter Zijlstra4a0deca2009-03-19 20:26:12 +0100808 regs = task_pt_regs(task);
Peter Zijlstra78f13e92009-04-08 15:01:33 +0200809 perf_swcounter_event(PERF_COUNT_CONTEXT_SWITCHES, 1, 1, regs, 0);
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100810 __perf_counter_sched_out(ctx, cpuctx);
811
Thomas Gleixner0793a612008-12-04 20:12:29 +0100812 cpuctx->task_ctx = NULL;
813}
814
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100815static void perf_counter_cpu_sched_out(struct perf_cpu_context *cpuctx)
Ingo Molnar04289bb2008-12-11 08:38:42 +0100816{
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100817 __perf_counter_sched_out(&cpuctx->ctx, cpuctx);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100818}
819
Ingo Molnar79958882008-12-17 08:54:56 +0100820static int
Ingo Molnar04289bb2008-12-11 08:38:42 +0100821group_sched_in(struct perf_counter *group_counter,
822 struct perf_cpu_context *cpuctx,
823 struct perf_counter_context *ctx,
824 int cpu)
825{
Ingo Molnar95cdd2e2008-12-21 13:50:42 +0100826 struct perf_counter *counter, *partial_group;
Paul Mackerras3cbed422009-01-09 16:43:42 +1100827 int ret;
828
829 if (group_counter->state == PERF_COUNTER_STATE_OFF)
830 return 0;
831
832 ret = hw_perf_group_sched_in(group_counter, cpuctx, ctx, cpu);
833 if (ret)
834 return ret < 0 ? ret : 0;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100835
Paul Mackerrasc07c99b2009-02-13 22:10:34 +1100836 group_counter->prev_state = group_counter->state;
Ingo Molnar95cdd2e2008-12-21 13:50:42 +0100837 if (counter_sched_in(group_counter, cpuctx, ctx, cpu))
838 return -EAGAIN;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100839
840 /*
841 * Schedule in siblings as one group (if any):
842 */
Ingo Molnar79958882008-12-17 08:54:56 +0100843 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
Paul Mackerrasc07c99b2009-02-13 22:10:34 +1100844 counter->prev_state = counter->state;
Ingo Molnar95cdd2e2008-12-21 13:50:42 +0100845 if (counter_sched_in(counter, cpuctx, ctx, cpu)) {
846 partial_group = counter;
847 goto group_error;
848 }
Ingo Molnar79958882008-12-17 08:54:56 +0100849 }
850
Paul Mackerras3cbed422009-01-09 16:43:42 +1100851 return 0;
Ingo Molnar95cdd2e2008-12-21 13:50:42 +0100852
853group_error:
854 /*
855 * Groups can be scheduled in as one unit only, so undo any
856 * partial group before returning:
857 */
858 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
859 if (counter == partial_group)
860 break;
861 counter_sched_out(counter, cpuctx, ctx);
862 }
863 counter_sched_out(group_counter, cpuctx, ctx);
864
865 return -EAGAIN;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100866}
867
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100868static void
869__perf_counter_sched_in(struct perf_counter_context *ctx,
870 struct perf_cpu_context *cpuctx, int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100871{
Thomas Gleixner0793a612008-12-04 20:12:29 +0100872 struct perf_counter *counter;
Paul Mackerras3cbed422009-01-09 16:43:42 +1100873 u64 flags;
Paul Mackerrasdd0e6ba2009-01-12 15:11:00 +1100874 int can_add_hw = 1;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100875
Thomas Gleixner0793a612008-12-04 20:12:29 +0100876 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100877 ctx->is_active = 1;
878 if (likely(!ctx->nr_counters))
879 goto out;
880
Peter Zijlstra4af49982009-04-06 11:45:10 +0200881 ctx->timestamp = perf_clock();
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100882
Paul Mackerras3cbed422009-01-09 16:43:42 +1100883 flags = hw_perf_save_disable();
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100884
885 /*
886 * First go through the list and put on any pinned groups
887 * in order to give them the best chance of going on.
888 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100889 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100890 if (counter->state <= PERF_COUNTER_STATE_OFF ||
891 !counter->hw_event.pinned)
892 continue;
893 if (counter->cpu != -1 && counter->cpu != cpu)
894 continue;
895
896 if (group_can_go_on(counter, cpuctx, 1))
897 group_sched_in(counter, cpuctx, ctx, cpu);
898
899 /*
900 * If this pinned group hasn't been scheduled,
901 * put it in error state.
902 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100903 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
904 update_group_times(counter);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100905 counter->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100906 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100907 }
908
909 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
910 /*
911 * Ignore counters in OFF or ERROR state, and
912 * ignore pinned counters since we did them already.
913 */
914 if (counter->state <= PERF_COUNTER_STATE_OFF ||
915 counter->hw_event.pinned)
916 continue;
917
Ingo Molnar04289bb2008-12-11 08:38:42 +0100918 /*
919 * Listen to the 'cpu' scheduling filter constraint
920 * of counters:
921 */
Thomas Gleixner0793a612008-12-04 20:12:29 +0100922 if (counter->cpu != -1 && counter->cpu != cpu)
923 continue;
924
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100925 if (group_can_go_on(counter, cpuctx, can_add_hw)) {
Paul Mackerrasdd0e6ba2009-01-12 15:11:00 +1100926 if (group_sched_in(counter, cpuctx, ctx, cpu))
927 can_add_hw = 0;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100928 }
Thomas Gleixner0793a612008-12-04 20:12:29 +0100929 }
Paul Mackerras3cbed422009-01-09 16:43:42 +1100930 hw_perf_restore(flags);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100931 out:
Thomas Gleixner0793a612008-12-04 20:12:29 +0100932 spin_unlock(&ctx->lock);
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100933}
Ingo Molnar04289bb2008-12-11 08:38:42 +0100934
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100935/*
936 * Called from scheduler to add the counters of the current task
937 * with interrupts disabled.
938 *
939 * We restore the counter value and then enable it.
940 *
941 * This does not protect us against NMI, but enable()
942 * sets the enabled bit in the control field of counter _before_
943 * accessing the counter control register. If a NMI hits, then it will
944 * keep the counter running.
945 */
946void perf_counter_task_sched_in(struct task_struct *task, int cpu)
947{
948 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
949 struct perf_counter_context *ctx = &task->perf_counter_ctx;
950
951 __perf_counter_sched_in(ctx, cpuctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100952 cpuctx->task_ctx = ctx;
953}
954
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100955static void perf_counter_cpu_sched_in(struct perf_cpu_context *cpuctx, int cpu)
956{
957 struct perf_counter_context *ctx = &cpuctx->ctx;
958
959 __perf_counter_sched_in(ctx, cpuctx, cpu);
960}
961
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100962int perf_counter_task_disable(void)
963{
964 struct task_struct *curr = current;
965 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
966 struct perf_counter *counter;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100967 unsigned long flags;
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100968 u64 perf_flags;
969 int cpu;
970
971 if (likely(!ctx->nr_counters))
972 return 0;
973
Peter Zijlstra849691a2009-04-06 11:45:12 +0200974 local_irq_save(flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100975 cpu = smp_processor_id();
976
977 perf_counter_task_sched_out(curr, cpu);
978
979 spin_lock(&ctx->lock);
980
981 /*
982 * Disable all the counters:
983 */
984 perf_flags = hw_perf_save_disable();
985
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100986 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100987 if (counter->state != PERF_COUNTER_STATE_ERROR) {
988 update_group_times(counter);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100989 counter->state = PERF_COUNTER_STATE_OFF;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100990 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100991 }
Ingo Molnar9b51f662008-12-12 13:49:45 +0100992
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100993 hw_perf_restore(perf_flags);
994
Peter Zijlstra849691a2009-04-06 11:45:12 +0200995 spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100996
997 return 0;
998}
999
1000int perf_counter_task_enable(void)
1001{
1002 struct task_struct *curr = current;
1003 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
1004 struct perf_counter *counter;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001005 unsigned long flags;
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001006 u64 perf_flags;
1007 int cpu;
1008
1009 if (likely(!ctx->nr_counters))
1010 return 0;
1011
Peter Zijlstra849691a2009-04-06 11:45:12 +02001012 local_irq_save(flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001013 cpu = smp_processor_id();
1014
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001015 perf_counter_task_sched_out(curr, cpu);
1016
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001017 spin_lock(&ctx->lock);
1018
1019 /*
1020 * Disable all the counters:
1021 */
1022 perf_flags = hw_perf_save_disable();
1023
1024 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001025 if (counter->state > PERF_COUNTER_STATE_OFF)
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001026 continue;
Ingo Molnar6a930702008-12-11 15:17:03 +01001027 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +02001028 counter->tstamp_enabled =
1029 ctx->time - counter->total_time_enabled;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001030 counter->hw_event.disabled = 0;
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001031 }
1032 hw_perf_restore(perf_flags);
1033
1034 spin_unlock(&ctx->lock);
1035
1036 perf_counter_task_sched_in(curr, cpu);
1037
Peter Zijlstra849691a2009-04-06 11:45:12 +02001038 local_irq_restore(flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001039
1040 return 0;
1041}
1042
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001043/*
1044 * Round-robin a context's counters:
1045 */
1046static void rotate_ctx(struct perf_counter_context *ctx)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001047{
Thomas Gleixner0793a612008-12-04 20:12:29 +01001048 struct perf_counter *counter;
Ingo Molnar5c92d122008-12-11 13:21:10 +01001049 u64 perf_flags;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001050
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001051 if (!ctx->nr_counters)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001052 return;
1053
Thomas Gleixner0793a612008-12-04 20:12:29 +01001054 spin_lock(&ctx->lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001055 /*
Ingo Molnar04289bb2008-12-11 08:38:42 +01001056 * Rotate the first entry last (works just fine for group counters too):
Thomas Gleixner0793a612008-12-04 20:12:29 +01001057 */
Ingo Molnar01b28382008-12-11 13:45:51 +01001058 perf_flags = hw_perf_save_disable();
Ingo Molnar04289bb2008-12-11 08:38:42 +01001059 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Peter Zijlstra75564232009-03-13 12:21:29 +01001060 list_move_tail(&counter->list_entry, &ctx->counter_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001061 break;
1062 }
Ingo Molnar01b28382008-12-11 13:45:51 +01001063 hw_perf_restore(perf_flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001064
1065 spin_unlock(&ctx->lock);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001066}
Thomas Gleixner0793a612008-12-04 20:12:29 +01001067
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001068void perf_counter_task_tick(struct task_struct *curr, int cpu)
1069{
1070 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
1071 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001072
Ingo Molnarb82914c2009-05-04 18:54:32 +02001073 perf_counter_cpu_sched_out(cpuctx);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001074 perf_counter_task_sched_out(curr, cpu);
1075
Ingo Molnarb82914c2009-05-04 18:54:32 +02001076 rotate_ctx(&cpuctx->ctx);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001077 rotate_ctx(ctx);
1078
Ingo Molnarb82914c2009-05-04 18:54:32 +02001079 perf_counter_cpu_sched_in(cpuctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001080 perf_counter_task_sched_in(curr, cpu);
1081}
1082
1083/*
Thomas Gleixner0793a612008-12-04 20:12:29 +01001084 * Cross CPU call to read the hardware counter
1085 */
Ingo Molnar76715812008-12-17 14:20:28 +01001086static void __read(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001087{
Ingo Molnar621a01e2008-12-11 12:46:46 +01001088 struct perf_counter *counter = info;
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001089 struct perf_counter_context *ctx = counter->ctx;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001090 unsigned long flags;
Ingo Molnar621a01e2008-12-11 12:46:46 +01001091
Peter Zijlstra849691a2009-04-06 11:45:12 +02001092 local_irq_save(flags);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001093 if (ctx->is_active)
Peter Zijlstra4af49982009-04-06 11:45:10 +02001094 update_context_time(ctx);
Robert Richter4aeb0b42009-04-29 12:47:03 +02001095 counter->pmu->read(counter);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001096 update_counter_times(counter);
Peter Zijlstra849691a2009-04-06 11:45:12 +02001097 local_irq_restore(flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001098}
1099
Ingo Molnar04289bb2008-12-11 08:38:42 +01001100static u64 perf_counter_read(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001101{
1102 /*
1103 * If counter is enabled and currently active on a CPU, update the
1104 * value in the counter structure:
1105 */
Ingo Molnar6a930702008-12-11 15:17:03 +01001106 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
Thomas Gleixner0793a612008-12-04 20:12:29 +01001107 smp_call_function_single(counter->oncpu,
Ingo Molnar76715812008-12-17 14:20:28 +01001108 __read, counter, 1);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001109 } else if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
1110 update_counter_times(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001111 }
1112
Ingo Molnaree060942008-12-13 09:00:03 +01001113 return atomic64_read(&counter->count);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001114}
1115
Thomas Gleixner0793a612008-12-04 20:12:29 +01001116static void put_context(struct perf_counter_context *ctx)
1117{
1118 if (ctx->task)
1119 put_task_struct(ctx->task);
1120}
1121
1122static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
1123{
1124 struct perf_cpu_context *cpuctx;
1125 struct perf_counter_context *ctx;
1126 struct task_struct *task;
1127
1128 /*
1129 * If cpu is not a wildcard then this is a percpu counter:
1130 */
1131 if (cpu != -1) {
1132 /* Must be root to operate on a CPU counter: */
Peter Zijlstra1ccd1542009-04-09 10:53:45 +02001133 if (sysctl_perf_counter_priv && !capable(CAP_SYS_ADMIN))
Thomas Gleixner0793a612008-12-04 20:12:29 +01001134 return ERR_PTR(-EACCES);
1135
1136 if (cpu < 0 || cpu > num_possible_cpus())
1137 return ERR_PTR(-EINVAL);
1138
1139 /*
1140 * We could be clever and allow to attach a counter to an
1141 * offline CPU and activate it when the CPU comes up, but
1142 * that's for later.
1143 */
1144 if (!cpu_isset(cpu, cpu_online_map))
1145 return ERR_PTR(-ENODEV);
1146
1147 cpuctx = &per_cpu(perf_cpu_context, cpu);
1148 ctx = &cpuctx->ctx;
1149
Thomas Gleixner0793a612008-12-04 20:12:29 +01001150 return ctx;
1151 }
1152
1153 rcu_read_lock();
1154 if (!pid)
1155 task = current;
1156 else
1157 task = find_task_by_vpid(pid);
1158 if (task)
1159 get_task_struct(task);
1160 rcu_read_unlock();
1161
1162 if (!task)
1163 return ERR_PTR(-ESRCH);
1164
1165 ctx = &task->perf_counter_ctx;
1166 ctx->task = task;
1167
1168 /* Reuse ptrace permission checks for now. */
1169 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
1170 put_context(ctx);
1171 return ERR_PTR(-EACCES);
1172 }
1173
1174 return ctx;
1175}
1176
Peter Zijlstra592903c2009-03-13 12:21:36 +01001177static void free_counter_rcu(struct rcu_head *head)
1178{
1179 struct perf_counter *counter;
1180
1181 counter = container_of(head, struct perf_counter, rcu_head);
1182 kfree(counter);
1183}
1184
Peter Zijlstra925d5192009-03-30 19:07:02 +02001185static void perf_pending_sync(struct perf_counter *counter);
1186
Peter Zijlstraf1600952009-03-19 20:26:16 +01001187static void free_counter(struct perf_counter *counter)
1188{
Peter Zijlstra925d5192009-03-30 19:07:02 +02001189 perf_pending_sync(counter);
1190
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02001191 if (counter->hw_event.mmap)
1192 atomic_dec(&nr_mmap_tracking);
1193 if (counter->hw_event.munmap)
1194 atomic_dec(&nr_munmap_tracking);
1195 if (counter->hw_event.comm)
1196 atomic_dec(&nr_comm_tracking);
1197
Peter Zijlstrae077df42009-03-19 20:26:17 +01001198 if (counter->destroy)
1199 counter->destroy(counter);
1200
Peter Zijlstraf1600952009-03-19 20:26:16 +01001201 call_rcu(&counter->rcu_head, free_counter_rcu);
1202}
1203
Thomas Gleixner0793a612008-12-04 20:12:29 +01001204/*
1205 * Called when the last reference to the file is gone.
1206 */
1207static int perf_release(struct inode *inode, struct file *file)
1208{
1209 struct perf_counter *counter = file->private_data;
1210 struct perf_counter_context *ctx = counter->ctx;
1211
1212 file->private_data = NULL;
1213
Paul Mackerrasd859e292009-01-17 18:10:22 +11001214 mutex_lock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001215 mutex_lock(&counter->mutex);
1216
Ingo Molnar04289bb2008-12-11 08:38:42 +01001217 perf_counter_remove_from_context(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001218
1219 mutex_unlock(&counter->mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001220 mutex_unlock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001221
Peter Zijlstraf1600952009-03-19 20:26:16 +01001222 free_counter(counter);
Mike Galbraith5af75912009-02-11 10:53:37 +01001223 put_context(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001224
1225 return 0;
1226}
1227
1228/*
1229 * Read the performance counter - simple non blocking version for now
1230 */
1231static ssize_t
1232perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
1233{
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001234 u64 values[3];
1235 int n;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001236
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001237 /*
1238 * Return end-of-file for a read on a counter that is in
1239 * error state (i.e. because it was pinned but it couldn't be
1240 * scheduled on to the CPU at some point).
1241 */
1242 if (counter->state == PERF_COUNTER_STATE_ERROR)
1243 return 0;
1244
Thomas Gleixner0793a612008-12-04 20:12:29 +01001245 mutex_lock(&counter->mutex);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001246 values[0] = perf_counter_read(counter);
1247 n = 1;
1248 if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1249 values[n++] = counter->total_time_enabled +
1250 atomic64_read(&counter->child_total_time_enabled);
1251 if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1252 values[n++] = counter->total_time_running +
1253 atomic64_read(&counter->child_total_time_running);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001254 mutex_unlock(&counter->mutex);
1255
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001256 if (count < n * sizeof(u64))
1257 return -EINVAL;
1258 count = n * sizeof(u64);
1259
1260 if (copy_to_user(buf, values, count))
1261 return -EFAULT;
1262
1263 return count;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001264}
1265
1266static ssize_t
Thomas Gleixner0793a612008-12-04 20:12:29 +01001267perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
1268{
1269 struct perf_counter *counter = file->private_data;
1270
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001271 return perf_read_hw(counter, buf, count);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001272}
1273
1274static unsigned int perf_poll(struct file *file, poll_table *wait)
1275{
1276 struct perf_counter *counter = file->private_data;
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001277 struct perf_mmap_data *data;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001278 unsigned int events = POLL_HUP;
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001279
1280 rcu_read_lock();
1281 data = rcu_dereference(counter->data);
1282 if (data)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001283 events = atomic_xchg(&data->poll, 0);
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001284 rcu_read_unlock();
Thomas Gleixner0793a612008-12-04 20:12:29 +01001285
1286 poll_wait(file, &counter->waitq, wait);
1287
Thomas Gleixner0793a612008-12-04 20:12:29 +01001288 return events;
1289}
1290
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001291static void perf_counter_reset(struct perf_counter *counter)
1292{
1293 atomic_set(&counter->count, 0);
1294}
1295
Paul Mackerrasd859e292009-01-17 18:10:22 +11001296static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1297{
1298 struct perf_counter *counter = file->private_data;
1299 int err = 0;
1300
1301 switch (cmd) {
1302 case PERF_COUNTER_IOC_ENABLE:
1303 perf_counter_enable_family(counter);
1304 break;
1305 case PERF_COUNTER_IOC_DISABLE:
1306 perf_counter_disable_family(counter);
1307 break;
Peter Zijlstra79f14642009-04-06 11:45:07 +02001308 case PERF_COUNTER_IOC_REFRESH:
1309 perf_counter_refresh(counter, arg);
1310 break;
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001311 case PERF_COUNTER_IOC_RESET:
1312 perf_counter_reset(counter);
1313 break;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001314 default:
1315 err = -ENOTTY;
1316 }
1317 return err;
1318}
1319
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001320/*
1321 * Callers need to ensure there can be no nesting of this function, otherwise
1322 * the seqlock logic goes bad. We can not serialize this because the arch
1323 * code calls this from NMI context.
1324 */
1325void perf_counter_update_userpage(struct perf_counter *counter)
Paul Mackerras37d81822009-03-23 18:22:08 +01001326{
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001327 struct perf_mmap_data *data;
1328 struct perf_counter_mmap_page *userpg;
1329
1330 rcu_read_lock();
1331 data = rcu_dereference(counter->data);
1332 if (!data)
1333 goto unlock;
1334
1335 userpg = data->user_page;
Paul Mackerras37d81822009-03-23 18:22:08 +01001336
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001337 /*
1338 * Disable preemption so as to not let the corresponding user-space
1339 * spin too long if we get preempted.
1340 */
1341 preempt_disable();
Paul Mackerras37d81822009-03-23 18:22:08 +01001342 ++userpg->lock;
Peter Zijlstra92f22a32009-04-02 11:12:04 +02001343 barrier();
Paul Mackerras37d81822009-03-23 18:22:08 +01001344 userpg->index = counter->hw.idx;
1345 userpg->offset = atomic64_read(&counter->count);
1346 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
1347 userpg->offset -= atomic64_read(&counter->hw.prev_count);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001348
Peter Zijlstra92f22a32009-04-02 11:12:04 +02001349 barrier();
Paul Mackerras37d81822009-03-23 18:22:08 +01001350 ++userpg->lock;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001351 preempt_enable();
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001352unlock:
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001353 rcu_read_unlock();
Paul Mackerras37d81822009-03-23 18:22:08 +01001354}
1355
1356static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1357{
1358 struct perf_counter *counter = vma->vm_file->private_data;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001359 struct perf_mmap_data *data;
1360 int ret = VM_FAULT_SIGBUS;
Paul Mackerras37d81822009-03-23 18:22:08 +01001361
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001362 rcu_read_lock();
1363 data = rcu_dereference(counter->data);
1364 if (!data)
1365 goto unlock;
Paul Mackerras37d81822009-03-23 18:22:08 +01001366
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001367 if (vmf->pgoff == 0) {
1368 vmf->page = virt_to_page(data->user_page);
1369 } else {
1370 int nr = vmf->pgoff - 1;
1371
1372 if ((unsigned)nr > data->nr_pages)
1373 goto unlock;
1374
1375 vmf->page = virt_to_page(data->data_pages[nr]);
1376 }
Paul Mackerras37d81822009-03-23 18:22:08 +01001377 get_page(vmf->page);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001378 ret = 0;
1379unlock:
1380 rcu_read_unlock();
1381
1382 return ret;
1383}
1384
1385static int perf_mmap_data_alloc(struct perf_counter *counter, int nr_pages)
1386{
1387 struct perf_mmap_data *data;
1388 unsigned long size;
1389 int i;
1390
1391 WARN_ON(atomic_read(&counter->mmap_count));
1392
1393 size = sizeof(struct perf_mmap_data);
1394 size += nr_pages * sizeof(void *);
1395
1396 data = kzalloc(size, GFP_KERNEL);
1397 if (!data)
1398 goto fail;
1399
1400 data->user_page = (void *)get_zeroed_page(GFP_KERNEL);
1401 if (!data->user_page)
1402 goto fail_user_page;
1403
1404 for (i = 0; i < nr_pages; i++) {
1405 data->data_pages[i] = (void *)get_zeroed_page(GFP_KERNEL);
1406 if (!data->data_pages[i])
1407 goto fail_data_pages;
1408 }
1409
1410 data->nr_pages = nr_pages;
1411
1412 rcu_assign_pointer(counter->data, data);
1413
Paul Mackerras37d81822009-03-23 18:22:08 +01001414 return 0;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001415
1416fail_data_pages:
1417 for (i--; i >= 0; i--)
1418 free_page((unsigned long)data->data_pages[i]);
1419
1420 free_page((unsigned long)data->user_page);
1421
1422fail_user_page:
1423 kfree(data);
1424
1425fail:
1426 return -ENOMEM;
1427}
1428
1429static void __perf_mmap_data_free(struct rcu_head *rcu_head)
1430{
1431 struct perf_mmap_data *data = container_of(rcu_head,
1432 struct perf_mmap_data, rcu_head);
1433 int i;
1434
1435 free_page((unsigned long)data->user_page);
1436 for (i = 0; i < data->nr_pages; i++)
1437 free_page((unsigned long)data->data_pages[i]);
1438 kfree(data);
1439}
1440
1441static void perf_mmap_data_free(struct perf_counter *counter)
1442{
1443 struct perf_mmap_data *data = counter->data;
1444
1445 WARN_ON(atomic_read(&counter->mmap_count));
1446
1447 rcu_assign_pointer(counter->data, NULL);
1448 call_rcu(&data->rcu_head, __perf_mmap_data_free);
1449}
1450
1451static void perf_mmap_open(struct vm_area_struct *vma)
1452{
1453 struct perf_counter *counter = vma->vm_file->private_data;
1454
1455 atomic_inc(&counter->mmap_count);
1456}
1457
1458static void perf_mmap_close(struct vm_area_struct *vma)
1459{
1460 struct perf_counter *counter = vma->vm_file->private_data;
1461
1462 if (atomic_dec_and_mutex_lock(&counter->mmap_count,
1463 &counter->mmap_mutex)) {
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001464 vma->vm_mm->locked_vm -= counter->data->nr_pages + 1;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001465 perf_mmap_data_free(counter);
1466 mutex_unlock(&counter->mmap_mutex);
1467 }
Paul Mackerras37d81822009-03-23 18:22:08 +01001468}
1469
1470static struct vm_operations_struct perf_mmap_vmops = {
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001471 .open = perf_mmap_open,
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001472 .close = perf_mmap_close,
Paul Mackerras37d81822009-03-23 18:22:08 +01001473 .fault = perf_mmap_fault,
1474};
1475
1476static int perf_mmap(struct file *file, struct vm_area_struct *vma)
1477{
1478 struct perf_counter *counter = file->private_data;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001479 unsigned long vma_size;
1480 unsigned long nr_pages;
1481 unsigned long locked, lock_limit;
1482 int ret = 0;
Paul Mackerras37d81822009-03-23 18:22:08 +01001483
1484 if (!(vma->vm_flags & VM_SHARED) || (vma->vm_flags & VM_WRITE))
1485 return -EINVAL;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001486
1487 vma_size = vma->vm_end - vma->vm_start;
1488 nr_pages = (vma_size / PAGE_SIZE) - 1;
1489
Peter Zijlstra7730d862009-03-25 12:48:31 +01001490 /*
1491 * If we have data pages ensure they're a power-of-two number, so we
1492 * can do bitmasks instead of modulo.
1493 */
1494 if (nr_pages != 0 && !is_power_of_2(nr_pages))
Paul Mackerras37d81822009-03-23 18:22:08 +01001495 return -EINVAL;
1496
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001497 if (vma_size != PAGE_SIZE * (1 + nr_pages))
Paul Mackerras37d81822009-03-23 18:22:08 +01001498 return -EINVAL;
1499
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001500 if (vma->vm_pgoff != 0)
1501 return -EINVAL;
Paul Mackerras37d81822009-03-23 18:22:08 +01001502
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001503 mutex_lock(&counter->mmap_mutex);
1504 if (atomic_inc_not_zero(&counter->mmap_count)) {
1505 if (nr_pages != counter->data->nr_pages)
1506 ret = -EINVAL;
1507 goto unlock;
1508 }
1509
1510 locked = vma->vm_mm->locked_vm;
1511 locked += nr_pages + 1;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001512
1513 lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
1514 lock_limit >>= PAGE_SHIFT;
1515
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001516 if ((locked > lock_limit) && !capable(CAP_IPC_LOCK)) {
1517 ret = -EPERM;
1518 goto unlock;
1519 }
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001520
1521 WARN_ON(counter->data);
1522 ret = perf_mmap_data_alloc(counter, nr_pages);
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001523 if (ret)
1524 goto unlock;
1525
1526 atomic_set(&counter->mmap_count, 1);
1527 vma->vm_mm->locked_vm += nr_pages + 1;
1528unlock:
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001529 mutex_unlock(&counter->mmap_mutex);
Paul Mackerras37d81822009-03-23 18:22:08 +01001530
1531 vma->vm_flags &= ~VM_MAYWRITE;
1532 vma->vm_flags |= VM_RESERVED;
1533 vma->vm_ops = &perf_mmap_vmops;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001534
1535 return ret;
Paul Mackerras37d81822009-03-23 18:22:08 +01001536}
1537
Peter Zijlstra3c446b3d2009-04-06 11:45:01 +02001538static int perf_fasync(int fd, struct file *filp, int on)
1539{
1540 struct perf_counter *counter = filp->private_data;
1541 struct inode *inode = filp->f_path.dentry->d_inode;
1542 int retval;
1543
1544 mutex_lock(&inode->i_mutex);
1545 retval = fasync_helper(fd, filp, on, &counter->fasync);
1546 mutex_unlock(&inode->i_mutex);
1547
1548 if (retval < 0)
1549 return retval;
1550
1551 return 0;
1552}
1553
Thomas Gleixner0793a612008-12-04 20:12:29 +01001554static const struct file_operations perf_fops = {
1555 .release = perf_release,
1556 .read = perf_read,
1557 .poll = perf_poll,
Paul Mackerrasd859e292009-01-17 18:10:22 +11001558 .unlocked_ioctl = perf_ioctl,
1559 .compat_ioctl = perf_ioctl,
Paul Mackerras37d81822009-03-23 18:22:08 +01001560 .mmap = perf_mmap,
Peter Zijlstra3c446b3d2009-04-06 11:45:01 +02001561 .fasync = perf_fasync,
Thomas Gleixner0793a612008-12-04 20:12:29 +01001562};
1563
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001564/*
Peter Zijlstra925d5192009-03-30 19:07:02 +02001565 * Perf counter wakeup
1566 *
1567 * If there's data, ensure we set the poll() state and publish everything
1568 * to user-space before waking everybody up.
1569 */
1570
1571void perf_counter_wakeup(struct perf_counter *counter)
1572{
Peter Zijlstra925d5192009-03-30 19:07:02 +02001573 wake_up_all(&counter->waitq);
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02001574
1575 if (counter->pending_kill) {
1576 kill_fasync(&counter->fasync, SIGIO, counter->pending_kill);
1577 counter->pending_kill = 0;
1578 }
Peter Zijlstra925d5192009-03-30 19:07:02 +02001579}
1580
1581/*
1582 * Pending wakeups
1583 *
1584 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
1585 *
1586 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
1587 * single linked list and use cmpxchg() to add entries lockless.
1588 */
1589
Peter Zijlstra79f14642009-04-06 11:45:07 +02001590static void perf_pending_counter(struct perf_pending_entry *entry)
1591{
1592 struct perf_counter *counter = container_of(entry,
1593 struct perf_counter, pending);
1594
1595 if (counter->pending_disable) {
1596 counter->pending_disable = 0;
1597 perf_counter_disable(counter);
1598 }
1599
1600 if (counter->pending_wakeup) {
1601 counter->pending_wakeup = 0;
1602 perf_counter_wakeup(counter);
1603 }
1604}
1605
Peter Zijlstra671dec52009-04-06 11:45:02 +02001606#define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
Peter Zijlstra925d5192009-03-30 19:07:02 +02001607
Peter Zijlstra671dec52009-04-06 11:45:02 +02001608static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = {
Peter Zijlstra925d5192009-03-30 19:07:02 +02001609 PENDING_TAIL,
1610};
1611
Peter Zijlstra671dec52009-04-06 11:45:02 +02001612static void perf_pending_queue(struct perf_pending_entry *entry,
1613 void (*func)(struct perf_pending_entry *))
Peter Zijlstra925d5192009-03-30 19:07:02 +02001614{
Peter Zijlstra671dec52009-04-06 11:45:02 +02001615 struct perf_pending_entry **head;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001616
Peter Zijlstra671dec52009-04-06 11:45:02 +02001617 if (cmpxchg(&entry->next, NULL, PENDING_TAIL) != NULL)
Peter Zijlstra925d5192009-03-30 19:07:02 +02001618 return;
1619
Peter Zijlstra671dec52009-04-06 11:45:02 +02001620 entry->func = func;
1621
1622 head = &get_cpu_var(perf_pending_head);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001623
1624 do {
Peter Zijlstra671dec52009-04-06 11:45:02 +02001625 entry->next = *head;
1626 } while (cmpxchg(head, entry->next, entry) != entry->next);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001627
1628 set_perf_counter_pending();
1629
Peter Zijlstra671dec52009-04-06 11:45:02 +02001630 put_cpu_var(perf_pending_head);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001631}
1632
1633static int __perf_pending_run(void)
1634{
Peter Zijlstra671dec52009-04-06 11:45:02 +02001635 struct perf_pending_entry *list;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001636 int nr = 0;
1637
Peter Zijlstra671dec52009-04-06 11:45:02 +02001638 list = xchg(&__get_cpu_var(perf_pending_head), PENDING_TAIL);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001639 while (list != PENDING_TAIL) {
Peter Zijlstra671dec52009-04-06 11:45:02 +02001640 void (*func)(struct perf_pending_entry *);
1641 struct perf_pending_entry *entry = list;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001642
1643 list = list->next;
1644
Peter Zijlstra671dec52009-04-06 11:45:02 +02001645 func = entry->func;
1646 entry->next = NULL;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001647 /*
1648 * Ensure we observe the unqueue before we issue the wakeup,
1649 * so that we won't be waiting forever.
1650 * -- see perf_not_pending().
1651 */
1652 smp_wmb();
1653
Peter Zijlstra671dec52009-04-06 11:45:02 +02001654 func(entry);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001655 nr++;
1656 }
1657
1658 return nr;
1659}
1660
1661static inline int perf_not_pending(struct perf_counter *counter)
1662{
1663 /*
1664 * If we flush on whatever cpu we run, there is a chance we don't
1665 * need to wait.
1666 */
1667 get_cpu();
1668 __perf_pending_run();
1669 put_cpu();
1670
1671 /*
1672 * Ensure we see the proper queue state before going to sleep
1673 * so that we do not miss the wakeup. -- see perf_pending_handle()
1674 */
1675 smp_rmb();
Peter Zijlstra671dec52009-04-06 11:45:02 +02001676 return counter->pending.next == NULL;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001677}
1678
1679static void perf_pending_sync(struct perf_counter *counter)
1680{
1681 wait_event(counter->waitq, perf_not_pending(counter));
1682}
1683
1684void perf_counter_do_pending(void)
1685{
1686 __perf_pending_run();
1687}
1688
1689/*
Peter Zijlstra394ee072009-03-30 19:07:14 +02001690 * Callchain support -- arch specific
1691 */
1692
Peter Zijlstra9c03d882009-04-06 11:45:00 +02001693__weak struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
Peter Zijlstra394ee072009-03-30 19:07:14 +02001694{
1695 return NULL;
1696}
1697
1698/*
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001699 * Output
1700 */
1701
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001702struct perf_output_handle {
1703 struct perf_counter *counter;
1704 struct perf_mmap_data *data;
1705 unsigned int offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01001706 unsigned int head;
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001707 int nmi;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02001708 int overflow;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001709 int locked;
1710 unsigned long flags;
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001711};
1712
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001713static void perf_output_wakeup(struct perf_output_handle *handle)
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001714{
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001715 atomic_set(&handle->data->poll, POLL_IN);
1716
Peter Zijlstra671dec52009-04-06 11:45:02 +02001717 if (handle->nmi) {
Peter Zijlstra79f14642009-04-06 11:45:07 +02001718 handle->counter->pending_wakeup = 1;
Peter Zijlstra671dec52009-04-06 11:45:02 +02001719 perf_pending_queue(&handle->counter->pending,
Peter Zijlstra79f14642009-04-06 11:45:07 +02001720 perf_pending_counter);
Peter Zijlstra671dec52009-04-06 11:45:02 +02001721 } else
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001722 perf_counter_wakeup(handle->counter);
1723}
1724
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001725/*
1726 * Curious locking construct.
1727 *
1728 * We need to ensure a later event doesn't publish a head when a former
1729 * event isn't done writing. However since we need to deal with NMIs we
1730 * cannot fully serialize things.
1731 *
1732 * What we do is serialize between CPUs so we only have to deal with NMI
1733 * nesting on a single CPU.
1734 *
1735 * We only publish the head (and generate a wakeup) when the outer-most
1736 * event completes.
1737 */
1738static void perf_output_lock(struct perf_output_handle *handle)
1739{
1740 struct perf_mmap_data *data = handle->data;
1741 int cpu;
1742
1743 handle->locked = 0;
1744
1745 local_irq_save(handle->flags);
1746 cpu = smp_processor_id();
1747
1748 if (in_nmi() && atomic_read(&data->lock) == cpu)
1749 return;
1750
1751 while (atomic_cmpxchg(&data->lock, 0, cpu) != 0)
1752 cpu_relax();
1753
1754 handle->locked = 1;
1755}
1756
1757static void perf_output_unlock(struct perf_output_handle *handle)
1758{
1759 struct perf_mmap_data *data = handle->data;
1760 int head, cpu;
1761
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02001762 data->done_head = data->head;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001763
1764 if (!handle->locked)
1765 goto out;
1766
1767again:
1768 /*
1769 * The xchg implies a full barrier that ensures all writes are done
1770 * before we publish the new head, matched by a rmb() in userspace when
1771 * reading this position.
1772 */
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02001773 while ((head = atomic_xchg(&data->done_head, 0)))
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001774 data->user_page->data_head = head;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001775
1776 /*
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02001777 * NMI can happen here, which means we can miss a done_head update.
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001778 */
1779
1780 cpu = atomic_xchg(&data->lock, 0);
1781 WARN_ON_ONCE(cpu != smp_processor_id());
1782
1783 /*
1784 * Therefore we have to validate we did not indeed do so.
1785 */
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02001786 if (unlikely(atomic_read(&data->done_head))) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001787 /*
1788 * Since we had it locked, we can lock it again.
1789 */
1790 while (atomic_cmpxchg(&data->lock, 0, cpu) != 0)
1791 cpu_relax();
1792
1793 goto again;
1794 }
1795
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02001796 if (atomic_xchg(&data->wakeup, 0))
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001797 perf_output_wakeup(handle);
1798out:
1799 local_irq_restore(handle->flags);
1800}
1801
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001802static int perf_output_begin(struct perf_output_handle *handle,
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001803 struct perf_counter *counter, unsigned int size,
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02001804 int nmi, int overflow)
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001805{
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001806 struct perf_mmap_data *data;
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001807 unsigned int offset, head;
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001808
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001809 rcu_read_lock();
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001810 data = rcu_dereference(counter->data);
1811 if (!data)
1812 goto out;
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001813
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001814 handle->data = data;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02001815 handle->counter = counter;
1816 handle->nmi = nmi;
1817 handle->overflow = overflow;
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001818
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001819 if (!data->nr_pages)
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001820 goto fail;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001821
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001822 perf_output_lock(handle);
1823
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001824 do {
1825 offset = head = atomic_read(&data->head);
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001826 head += size;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001827 } while (atomic_cmpxchg(&data->head, offset, head) != offset);
1828
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001829 handle->offset = offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01001830 handle->head = head;
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02001831
1832 if ((offset >> PAGE_SHIFT) != (head >> PAGE_SHIFT))
1833 atomic_set(&data->wakeup, 1);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001834
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001835 return 0;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001836
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001837fail:
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001838 perf_output_wakeup(handle);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001839out:
1840 rcu_read_unlock();
1841
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001842 return -ENOSPC;
1843}
1844
1845static void perf_output_copy(struct perf_output_handle *handle,
1846 void *buf, unsigned int len)
1847{
1848 unsigned int pages_mask;
1849 unsigned int offset;
1850 unsigned int size;
1851 void **pages;
1852
1853 offset = handle->offset;
1854 pages_mask = handle->data->nr_pages - 1;
1855 pages = handle->data->data_pages;
1856
1857 do {
1858 unsigned int page_offset;
1859 int nr;
1860
1861 nr = (offset >> PAGE_SHIFT) & pages_mask;
1862 page_offset = offset & (PAGE_SIZE - 1);
1863 size = min_t(unsigned int, PAGE_SIZE - page_offset, len);
1864
1865 memcpy(pages[nr] + page_offset, buf, size);
1866
1867 len -= size;
1868 buf += size;
1869 offset += size;
1870 } while (len);
1871
1872 handle->offset = offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01001873
1874 WARN_ON_ONCE(handle->offset > handle->head);
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001875}
1876
Peter Zijlstra5c148192009-03-25 12:30:23 +01001877#define perf_output_put(handle, x) \
1878 perf_output_copy((handle), &(x), sizeof(x))
1879
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001880static void perf_output_end(struct perf_output_handle *handle)
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001881{
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001882 struct perf_counter *counter = handle->counter;
1883 struct perf_mmap_data *data = handle->data;
1884
1885 int wakeup_events = counter->hw_event.wakeup_events;
Peter Zijlstrac4578102009-04-02 11:12:01 +02001886
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02001887 if (handle->overflow && wakeup_events) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001888 int events = atomic_inc_return(&data->events);
Peter Zijlstrac4578102009-04-02 11:12:01 +02001889 if (events >= wakeup_events) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001890 atomic_sub(wakeup_events, &data->events);
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02001891 atomic_set(&data->wakeup, 1);
Peter Zijlstrac4578102009-04-02 11:12:01 +02001892 }
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001893 }
1894
1895 perf_output_unlock(handle);
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001896 rcu_read_unlock();
1897}
1898
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02001899static void perf_counter_output(struct perf_counter *counter,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02001900 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001901{
Peter Zijlstra5ed00412009-03-30 19:07:12 +02001902 int ret;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02001903 u64 record_type = counter->hw_event.record_type;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02001904 struct perf_output_handle handle;
1905 struct perf_event_header header;
1906 u64 ip;
Peter Zijlstra5c148192009-03-25 12:30:23 +01001907 struct {
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01001908 u32 pid, tid;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02001909 } tid_entry;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02001910 struct {
1911 u64 event;
1912 u64 counter;
1913 } group_entry;
Peter Zijlstra394ee072009-03-30 19:07:14 +02001914 struct perf_callchain_entry *callchain = NULL;
1915 int callchain_size = 0;
Peter Zijlstra339f7c92009-04-06 11:45:06 +02001916 u64 time;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001917
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02001918 header.type = 0;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02001919 header.size = sizeof(header);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001920
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02001921 header.misc = PERF_EVENT_MISC_OVERFLOW;
1922 header.misc |= user_mode(regs) ?
Peter Zijlstra6fab0192009-04-08 15:01:26 +02001923 PERF_EVENT_MISC_USER : PERF_EVENT_MISC_KERNEL;
1924
Peter Zijlstra8a057d82009-04-02 11:11:59 +02001925 if (record_type & PERF_RECORD_IP) {
1926 ip = instruction_pointer(regs);
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02001927 header.type |= PERF_RECORD_IP;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02001928 header.size += sizeof(ip);
1929 }
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01001930
Peter Zijlstra8a057d82009-04-02 11:11:59 +02001931 if (record_type & PERF_RECORD_TID) {
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01001932 /* namespace issues */
Peter Zijlstra5ed00412009-03-30 19:07:12 +02001933 tid_entry.pid = current->group_leader->pid;
1934 tid_entry.tid = current->pid;
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01001935
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02001936 header.type |= PERF_RECORD_TID;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02001937 header.size += sizeof(tid_entry);
1938 }
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01001939
Peter Zijlstra4d855452009-04-08 15:01:32 +02001940 if (record_type & PERF_RECORD_TIME) {
1941 /*
1942 * Maybe do better on x86 and provide cpu_clock_nmi()
1943 */
1944 time = sched_clock();
1945
1946 header.type |= PERF_RECORD_TIME;
1947 header.size += sizeof(u64);
1948 }
1949
Peter Zijlstra78f13e92009-04-08 15:01:33 +02001950 if (record_type & PERF_RECORD_ADDR) {
1951 header.type |= PERF_RECORD_ADDR;
1952 header.size += sizeof(u64);
1953 }
1954
Peter Zijlstra8a057d82009-04-02 11:11:59 +02001955 if (record_type & PERF_RECORD_GROUP) {
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02001956 header.type |= PERF_RECORD_GROUP;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02001957 header.size += sizeof(u64) +
1958 counter->nr_siblings * sizeof(group_entry);
1959 }
1960
1961 if (record_type & PERF_RECORD_CALLCHAIN) {
Peter Zijlstra394ee072009-03-30 19:07:14 +02001962 callchain = perf_callchain(regs);
1963
1964 if (callchain) {
Peter Zijlstra9c03d882009-04-06 11:45:00 +02001965 callchain_size = (1 + callchain->nr) * sizeof(u64);
Peter Zijlstra394ee072009-03-30 19:07:14 +02001966
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02001967 header.type |= PERF_RECORD_CALLCHAIN;
Peter Zijlstra394ee072009-03-30 19:07:14 +02001968 header.size += callchain_size;
1969 }
1970 }
1971
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02001972 ret = perf_output_begin(&handle, counter, header.size, nmi, 1);
Peter Zijlstra5ed00412009-03-30 19:07:12 +02001973 if (ret)
1974 return;
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01001975
Peter Zijlstra5ed00412009-03-30 19:07:12 +02001976 perf_output_put(&handle, header);
Peter Zijlstra5ed00412009-03-30 19:07:12 +02001977
Peter Zijlstra8a057d82009-04-02 11:11:59 +02001978 if (record_type & PERF_RECORD_IP)
1979 perf_output_put(&handle, ip);
1980
1981 if (record_type & PERF_RECORD_TID)
Peter Zijlstra5ed00412009-03-30 19:07:12 +02001982 perf_output_put(&handle, tid_entry);
1983
Peter Zijlstra4d855452009-04-08 15:01:32 +02001984 if (record_type & PERF_RECORD_TIME)
1985 perf_output_put(&handle, time);
1986
Peter Zijlstra78f13e92009-04-08 15:01:33 +02001987 if (record_type & PERF_RECORD_ADDR)
1988 perf_output_put(&handle, addr);
1989
Peter Zijlstra8a057d82009-04-02 11:11:59 +02001990 if (record_type & PERF_RECORD_GROUP) {
1991 struct perf_counter *leader, *sub;
1992 u64 nr = counter->nr_siblings;
1993
1994 perf_output_put(&handle, nr);
1995
1996 leader = counter->group_leader;
1997 list_for_each_entry(sub, &leader->sibling_list, list_entry) {
1998 if (sub != counter)
Robert Richter4aeb0b42009-04-29 12:47:03 +02001999 sub->pmu->read(sub);
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002000
2001 group_entry.event = sub->hw_event.config;
2002 group_entry.counter = atomic64_read(&sub->count);
2003
2004 perf_output_put(&handle, group_entry);
2005 }
2006 }
2007
Peter Zijlstra394ee072009-03-30 19:07:14 +02002008 if (callchain)
2009 perf_output_copy(&handle, callchain, callchain_size);
2010
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002011 perf_output_end(&handle);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002012}
2013
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002014/*
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002015 * comm tracking
2016 */
2017
2018struct perf_comm_event {
2019 struct task_struct *task;
2020 char *comm;
2021 int comm_size;
2022
2023 struct {
2024 struct perf_event_header header;
2025
2026 u32 pid;
2027 u32 tid;
2028 } event;
2029};
2030
2031static void perf_counter_comm_output(struct perf_counter *counter,
2032 struct perf_comm_event *comm_event)
2033{
2034 struct perf_output_handle handle;
2035 int size = comm_event->event.header.size;
2036 int ret = perf_output_begin(&handle, counter, size, 0, 0);
2037
2038 if (ret)
2039 return;
2040
2041 perf_output_put(&handle, comm_event->event);
2042 perf_output_copy(&handle, comm_event->comm,
2043 comm_event->comm_size);
2044 perf_output_end(&handle);
2045}
2046
2047static int perf_counter_comm_match(struct perf_counter *counter,
2048 struct perf_comm_event *comm_event)
2049{
2050 if (counter->hw_event.comm &&
2051 comm_event->event.header.type == PERF_EVENT_COMM)
2052 return 1;
2053
2054 return 0;
2055}
2056
2057static void perf_counter_comm_ctx(struct perf_counter_context *ctx,
2058 struct perf_comm_event *comm_event)
2059{
2060 struct perf_counter *counter;
2061
2062 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2063 return;
2064
2065 rcu_read_lock();
2066 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2067 if (perf_counter_comm_match(counter, comm_event))
2068 perf_counter_comm_output(counter, comm_event);
2069 }
2070 rcu_read_unlock();
2071}
2072
2073static void perf_counter_comm_event(struct perf_comm_event *comm_event)
2074{
2075 struct perf_cpu_context *cpuctx;
2076 unsigned int size;
2077 char *comm = comm_event->task->comm;
2078
Ingo Molnar888fcee2009-04-09 09:48:22 +02002079 size = ALIGN(strlen(comm)+1, sizeof(u64));
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002080
2081 comm_event->comm = comm;
2082 comm_event->comm_size = size;
2083
2084 comm_event->event.header.size = sizeof(comm_event->event) + size;
2085
2086 cpuctx = &get_cpu_var(perf_cpu_context);
2087 perf_counter_comm_ctx(&cpuctx->ctx, comm_event);
2088 put_cpu_var(perf_cpu_context);
2089
2090 perf_counter_comm_ctx(&current->perf_counter_ctx, comm_event);
2091}
2092
2093void perf_counter_comm(struct task_struct *task)
2094{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002095 struct perf_comm_event comm_event;
2096
2097 if (!atomic_read(&nr_comm_tracking))
2098 return;
2099
2100 comm_event = (struct perf_comm_event){
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002101 .task = task,
2102 .event = {
2103 .header = { .type = PERF_EVENT_COMM, },
2104 .pid = task->group_leader->pid,
2105 .tid = task->pid,
2106 },
2107 };
2108
2109 perf_counter_comm_event(&comm_event);
2110}
2111
2112/*
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002113 * mmap tracking
2114 */
2115
2116struct perf_mmap_event {
2117 struct file *file;
2118 char *file_name;
2119 int file_size;
2120
2121 struct {
2122 struct perf_event_header header;
2123
2124 u32 pid;
2125 u32 tid;
2126 u64 start;
2127 u64 len;
2128 u64 pgoff;
2129 } event;
2130};
2131
2132static void perf_counter_mmap_output(struct perf_counter *counter,
2133 struct perf_mmap_event *mmap_event)
2134{
2135 struct perf_output_handle handle;
2136 int size = mmap_event->event.header.size;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002137 int ret = perf_output_begin(&handle, counter, size, 0, 0);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002138
2139 if (ret)
2140 return;
2141
2142 perf_output_put(&handle, mmap_event->event);
2143 perf_output_copy(&handle, mmap_event->file_name,
2144 mmap_event->file_size);
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002145 perf_output_end(&handle);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002146}
2147
2148static int perf_counter_mmap_match(struct perf_counter *counter,
2149 struct perf_mmap_event *mmap_event)
2150{
2151 if (counter->hw_event.mmap &&
2152 mmap_event->event.header.type == PERF_EVENT_MMAP)
2153 return 1;
2154
2155 if (counter->hw_event.munmap &&
2156 mmap_event->event.header.type == PERF_EVENT_MUNMAP)
2157 return 1;
2158
2159 return 0;
2160}
2161
2162static void perf_counter_mmap_ctx(struct perf_counter_context *ctx,
2163 struct perf_mmap_event *mmap_event)
2164{
2165 struct perf_counter *counter;
2166
2167 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2168 return;
2169
2170 rcu_read_lock();
2171 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2172 if (perf_counter_mmap_match(counter, mmap_event))
2173 perf_counter_mmap_output(counter, mmap_event);
2174 }
2175 rcu_read_unlock();
2176}
2177
2178static void perf_counter_mmap_event(struct perf_mmap_event *mmap_event)
2179{
2180 struct perf_cpu_context *cpuctx;
2181 struct file *file = mmap_event->file;
2182 unsigned int size;
2183 char tmp[16];
2184 char *buf = NULL;
2185 char *name;
2186
2187 if (file) {
2188 buf = kzalloc(PATH_MAX, GFP_KERNEL);
2189 if (!buf) {
2190 name = strncpy(tmp, "//enomem", sizeof(tmp));
2191 goto got_name;
2192 }
Peter Zijlstrad3d21c42009-04-09 10:53:46 +02002193 name = d_path(&file->f_path, buf, PATH_MAX);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002194 if (IS_ERR(name)) {
2195 name = strncpy(tmp, "//toolong", sizeof(tmp));
2196 goto got_name;
2197 }
2198 } else {
2199 name = strncpy(tmp, "//anon", sizeof(tmp));
2200 goto got_name;
2201 }
2202
2203got_name:
Ingo Molnar888fcee2009-04-09 09:48:22 +02002204 size = ALIGN(strlen(name)+1, sizeof(u64));
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002205
2206 mmap_event->file_name = name;
2207 mmap_event->file_size = size;
2208
2209 mmap_event->event.header.size = sizeof(mmap_event->event) + size;
2210
2211 cpuctx = &get_cpu_var(perf_cpu_context);
2212 perf_counter_mmap_ctx(&cpuctx->ctx, mmap_event);
2213 put_cpu_var(perf_cpu_context);
2214
2215 perf_counter_mmap_ctx(&current->perf_counter_ctx, mmap_event);
2216
2217 kfree(buf);
2218}
2219
2220void perf_counter_mmap(unsigned long addr, unsigned long len,
2221 unsigned long pgoff, struct file *file)
2222{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002223 struct perf_mmap_event mmap_event;
2224
2225 if (!atomic_read(&nr_mmap_tracking))
2226 return;
2227
2228 mmap_event = (struct perf_mmap_event){
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002229 .file = file,
2230 .event = {
2231 .header = { .type = PERF_EVENT_MMAP, },
2232 .pid = current->group_leader->pid,
2233 .tid = current->pid,
2234 .start = addr,
2235 .len = len,
2236 .pgoff = pgoff,
2237 },
2238 };
2239
2240 perf_counter_mmap_event(&mmap_event);
2241}
2242
2243void perf_counter_munmap(unsigned long addr, unsigned long len,
2244 unsigned long pgoff, struct file *file)
2245{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002246 struct perf_mmap_event mmap_event;
2247
2248 if (!atomic_read(&nr_munmap_tracking))
2249 return;
2250
2251 mmap_event = (struct perf_mmap_event){
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002252 .file = file,
2253 .event = {
2254 .header = { .type = PERF_EVENT_MUNMAP, },
2255 .pid = current->group_leader->pid,
2256 .tid = current->pid,
2257 .start = addr,
2258 .len = len,
2259 .pgoff = pgoff,
2260 },
2261 };
2262
2263 perf_counter_mmap_event(&mmap_event);
2264}
2265
2266/*
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002267 * Generic counter overflow handling.
2268 */
2269
2270int perf_counter_overflow(struct perf_counter *counter,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002271 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002272{
Peter Zijlstra79f14642009-04-06 11:45:07 +02002273 int events = atomic_read(&counter->event_limit);
2274 int ret = 0;
2275
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002276 counter->pending_kill = POLL_IN;
Peter Zijlstra79f14642009-04-06 11:45:07 +02002277 if (events && atomic_dec_and_test(&counter->event_limit)) {
2278 ret = 1;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002279 counter->pending_kill = POLL_HUP;
Peter Zijlstra79f14642009-04-06 11:45:07 +02002280 if (nmi) {
2281 counter->pending_disable = 1;
2282 perf_pending_queue(&counter->pending,
2283 perf_pending_counter);
2284 } else
2285 perf_counter_disable(counter);
2286 }
2287
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002288 perf_counter_output(counter, nmi, regs, addr);
Peter Zijlstra79f14642009-04-06 11:45:07 +02002289 return ret;
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002290}
2291
2292/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002293 * Generic software counter infrastructure
2294 */
2295
2296static void perf_swcounter_update(struct perf_counter *counter)
2297{
2298 struct hw_perf_counter *hwc = &counter->hw;
2299 u64 prev, now;
2300 s64 delta;
2301
2302again:
2303 prev = atomic64_read(&hwc->prev_count);
2304 now = atomic64_read(&hwc->count);
2305 if (atomic64_cmpxchg(&hwc->prev_count, prev, now) != prev)
2306 goto again;
2307
2308 delta = now - prev;
2309
2310 atomic64_add(delta, &counter->count);
2311 atomic64_sub(delta, &hwc->period_left);
2312}
2313
2314static void perf_swcounter_set_period(struct perf_counter *counter)
2315{
2316 struct hw_perf_counter *hwc = &counter->hw;
2317 s64 left = atomic64_read(&hwc->period_left);
2318 s64 period = hwc->irq_period;
2319
2320 if (unlikely(left <= -period)) {
2321 left = period;
2322 atomic64_set(&hwc->period_left, left);
2323 }
2324
2325 if (unlikely(left <= 0)) {
2326 left += period;
2327 atomic64_add(period, &hwc->period_left);
2328 }
2329
2330 atomic64_set(&hwc->prev_count, -left);
2331 atomic64_set(&hwc->count, -left);
2332}
2333
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002334static enum hrtimer_restart perf_swcounter_hrtimer(struct hrtimer *hrtimer)
2335{
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002336 enum hrtimer_restart ret = HRTIMER_RESTART;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002337 struct perf_counter *counter;
2338 struct pt_regs *regs;
2339
2340 counter = container_of(hrtimer, struct perf_counter, hw.hrtimer);
Robert Richter4aeb0b42009-04-29 12:47:03 +02002341 counter->pmu->read(counter);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002342
2343 regs = get_irq_regs();
2344 /*
2345 * In case we exclude kernel IPs or are somehow not in interrupt
2346 * context, provide the next best thing, the user IP.
2347 */
2348 if ((counter->hw_event.exclude_kernel || !regs) &&
2349 !counter->hw_event.exclude_user)
2350 regs = task_pt_regs(current);
2351
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002352 if (regs) {
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002353 if (perf_counter_overflow(counter, 0, regs, 0))
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002354 ret = HRTIMER_NORESTART;
2355 }
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002356
2357 hrtimer_forward_now(hrtimer, ns_to_ktime(counter->hw.irq_period));
2358
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002359 return ret;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002360}
2361
2362static void perf_swcounter_overflow(struct perf_counter *counter,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002363 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002364{
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002365 perf_swcounter_update(counter);
2366 perf_swcounter_set_period(counter);
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002367 if (perf_counter_overflow(counter, nmi, regs, addr))
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002368 /* soft-disable the counter */
2369 ;
2370
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002371}
2372
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002373static int perf_swcounter_match(struct perf_counter *counter,
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002374 enum perf_event_types type,
2375 u32 event, struct pt_regs *regs)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002376{
2377 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
2378 return 0;
2379
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002380 if (perf_event_raw(&counter->hw_event))
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002381 return 0;
2382
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002383 if (perf_event_type(&counter->hw_event) != type)
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002384 return 0;
2385
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002386 if (perf_event_id(&counter->hw_event) != event)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002387 return 0;
2388
2389 if (counter->hw_event.exclude_user && user_mode(regs))
2390 return 0;
2391
2392 if (counter->hw_event.exclude_kernel && !user_mode(regs))
2393 return 0;
2394
2395 return 1;
2396}
2397
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002398static void perf_swcounter_add(struct perf_counter *counter, u64 nr,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002399 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002400{
2401 int neg = atomic64_add_negative(nr, &counter->hw.count);
2402 if (counter->hw.irq_period && !neg)
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002403 perf_swcounter_overflow(counter, nmi, regs, addr);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002404}
2405
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002406static void perf_swcounter_ctx_event(struct perf_counter_context *ctx,
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002407 enum perf_event_types type, u32 event,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002408 u64 nr, int nmi, struct pt_regs *regs,
2409 u64 addr)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002410{
2411 struct perf_counter *counter;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002412
Peter Zijlstra01ef09d2009-03-19 20:26:11 +01002413 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002414 return;
2415
Peter Zijlstra592903c2009-03-13 12:21:36 +01002416 rcu_read_lock();
2417 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002418 if (perf_swcounter_match(counter, type, event, regs))
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002419 perf_swcounter_add(counter, nr, nmi, regs, addr);
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002420 }
Peter Zijlstra592903c2009-03-13 12:21:36 +01002421 rcu_read_unlock();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002422}
2423
Peter Zijlstra96f6d442009-03-23 18:22:07 +01002424static int *perf_swcounter_recursion_context(struct perf_cpu_context *cpuctx)
2425{
2426 if (in_nmi())
2427 return &cpuctx->recursion[3];
2428
2429 if (in_irq())
2430 return &cpuctx->recursion[2];
2431
2432 if (in_softirq())
2433 return &cpuctx->recursion[1];
2434
2435 return &cpuctx->recursion[0];
2436}
2437
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002438static void __perf_swcounter_event(enum perf_event_types type, u32 event,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002439 u64 nr, int nmi, struct pt_regs *regs,
2440 u64 addr)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002441{
2442 struct perf_cpu_context *cpuctx = &get_cpu_var(perf_cpu_context);
Peter Zijlstra96f6d442009-03-23 18:22:07 +01002443 int *recursion = perf_swcounter_recursion_context(cpuctx);
2444
2445 if (*recursion)
2446 goto out;
2447
2448 (*recursion)++;
2449 barrier();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002450
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002451 perf_swcounter_ctx_event(&cpuctx->ctx, type, event,
2452 nr, nmi, regs, addr);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002453 if (cpuctx->task_ctx) {
2454 perf_swcounter_ctx_event(cpuctx->task_ctx, type, event,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002455 nr, nmi, regs, addr);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002456 }
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002457
Peter Zijlstra96f6d442009-03-23 18:22:07 +01002458 barrier();
2459 (*recursion)--;
2460
2461out:
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002462 put_cpu_var(perf_cpu_context);
2463}
2464
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002465void
2466perf_swcounter_event(u32 event, u64 nr, int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002467{
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002468 __perf_swcounter_event(PERF_TYPE_SOFTWARE, event, nr, nmi, regs, addr);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002469}
2470
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002471static void perf_swcounter_read(struct perf_counter *counter)
2472{
2473 perf_swcounter_update(counter);
2474}
2475
2476static int perf_swcounter_enable(struct perf_counter *counter)
2477{
2478 perf_swcounter_set_period(counter);
2479 return 0;
2480}
2481
2482static void perf_swcounter_disable(struct perf_counter *counter)
2483{
2484 perf_swcounter_update(counter);
2485}
2486
Robert Richter4aeb0b42009-04-29 12:47:03 +02002487static const struct pmu perf_ops_generic = {
Peter Zijlstraac17dc82009-03-13 12:21:34 +01002488 .enable = perf_swcounter_enable,
2489 .disable = perf_swcounter_disable,
2490 .read = perf_swcounter_read,
2491};
2492
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002493/*
2494 * Software counter: cpu wall time clock
2495 */
2496
Paul Mackerras9abf8a02009-01-09 16:26:43 +11002497static void cpu_clock_perf_counter_update(struct perf_counter *counter)
2498{
2499 int cpu = raw_smp_processor_id();
2500 s64 prev;
2501 u64 now;
2502
2503 now = cpu_clock(cpu);
2504 prev = atomic64_read(&counter->hw.prev_count);
2505 atomic64_set(&counter->hw.prev_count, now);
2506 atomic64_add(now - prev, &counter->count);
2507}
2508
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002509static int cpu_clock_perf_counter_enable(struct perf_counter *counter)
2510{
2511 struct hw_perf_counter *hwc = &counter->hw;
2512 int cpu = raw_smp_processor_id();
2513
2514 atomic64_set(&hwc->prev_count, cpu_clock(cpu));
Peter Zijlstra039fc912009-03-13 16:43:47 +01002515 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2516 hwc->hrtimer.function = perf_swcounter_hrtimer;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002517 if (hwc->irq_period) {
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002518 __hrtimer_start_range_ns(&hwc->hrtimer,
2519 ns_to_ktime(hwc->irq_period), 0,
2520 HRTIMER_MODE_REL, 0);
2521 }
2522
2523 return 0;
2524}
2525
Ingo Molnar5c92d122008-12-11 13:21:10 +01002526static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
2527{
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002528 hrtimer_cancel(&counter->hw.hrtimer);
Paul Mackerras9abf8a02009-01-09 16:26:43 +11002529 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01002530}
2531
2532static void cpu_clock_perf_counter_read(struct perf_counter *counter)
2533{
Paul Mackerras9abf8a02009-01-09 16:26:43 +11002534 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01002535}
2536
Robert Richter4aeb0b42009-04-29 12:47:03 +02002537static const struct pmu perf_ops_cpu_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +01002538 .enable = cpu_clock_perf_counter_enable,
2539 .disable = cpu_clock_perf_counter_disable,
2540 .read = cpu_clock_perf_counter_read,
Ingo Molnar5c92d122008-12-11 13:21:10 +01002541};
2542
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01002543/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002544 * Software counter: task time clock
2545 */
2546
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02002547static void task_clock_perf_counter_update(struct perf_counter *counter, u64 now)
Ingo Molnarbae43c92008-12-11 14:03:20 +01002548{
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02002549 u64 prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002550 s64 delta;
Ingo Molnarbae43c92008-12-11 14:03:20 +01002551
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02002552 prev = atomic64_xchg(&counter->hw.prev_count, now);
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002553 delta = now - prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002554 atomic64_add(delta, &counter->count);
Ingo Molnarbae43c92008-12-11 14:03:20 +01002555}
2556
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01002557static int task_clock_perf_counter_enable(struct perf_counter *counter)
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002558{
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002559 struct hw_perf_counter *hwc = &counter->hw;
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02002560 u64 now;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002561
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02002562 now = counter->ctx->time;
2563
2564 atomic64_set(&hwc->prev_count, now);
Peter Zijlstra039fc912009-03-13 16:43:47 +01002565 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2566 hwc->hrtimer.function = perf_swcounter_hrtimer;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002567 if (hwc->irq_period) {
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002568 __hrtimer_start_range_ns(&hwc->hrtimer,
2569 ns_to_ktime(hwc->irq_period), 0,
2570 HRTIMER_MODE_REL, 0);
2571 }
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01002572
2573 return 0;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002574}
2575
2576static void task_clock_perf_counter_disable(struct perf_counter *counter)
2577{
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002578 hrtimer_cancel(&counter->hw.hrtimer);
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02002579 task_clock_perf_counter_update(counter, counter->ctx->time);
2580
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002581}
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01002582
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002583static void task_clock_perf_counter_read(struct perf_counter *counter)
2584{
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02002585 u64 time;
2586
2587 if (!in_nmi()) {
2588 update_context_time(counter->ctx);
2589 time = counter->ctx->time;
2590 } else {
2591 u64 now = perf_clock();
2592 u64 delta = now - counter->ctx->timestamp;
2593 time = counter->ctx->time + delta;
2594 }
2595
2596 task_clock_perf_counter_update(counter, time);
Ingo Molnarbae43c92008-12-11 14:03:20 +01002597}
2598
Robert Richter4aeb0b42009-04-29 12:47:03 +02002599static const struct pmu perf_ops_task_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +01002600 .enable = task_clock_perf_counter_enable,
2601 .disable = task_clock_perf_counter_disable,
2602 .read = task_clock_perf_counter_read,
Ingo Molnarbae43c92008-12-11 14:03:20 +01002603};
2604
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002605/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002606 * Software counter: cpu migrations
2607 */
2608
Paul Mackerras23a185c2009-02-09 22:42:47 +11002609static inline u64 get_cpu_migrations(struct perf_counter *counter)
Ingo Molnar6c594c22008-12-14 12:34:15 +01002610{
Paul Mackerras23a185c2009-02-09 22:42:47 +11002611 struct task_struct *curr = counter->ctx->task;
2612
2613 if (curr)
2614 return curr->se.nr_migrations;
2615 return cpu_nr_migrations(smp_processor_id());
Ingo Molnar6c594c22008-12-14 12:34:15 +01002616}
2617
2618static void cpu_migrations_perf_counter_update(struct perf_counter *counter)
2619{
2620 u64 prev, now;
2621 s64 delta;
2622
2623 prev = atomic64_read(&counter->hw.prev_count);
Paul Mackerras23a185c2009-02-09 22:42:47 +11002624 now = get_cpu_migrations(counter);
Ingo Molnar6c594c22008-12-14 12:34:15 +01002625
2626 atomic64_set(&counter->hw.prev_count, now);
2627
2628 delta = now - prev;
Ingo Molnar6c594c22008-12-14 12:34:15 +01002629
2630 atomic64_add(delta, &counter->count);
2631}
2632
2633static void cpu_migrations_perf_counter_read(struct perf_counter *counter)
2634{
2635 cpu_migrations_perf_counter_update(counter);
2636}
2637
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01002638static int cpu_migrations_perf_counter_enable(struct perf_counter *counter)
Ingo Molnar6c594c22008-12-14 12:34:15 +01002639{
Paul Mackerrasc07c99b2009-02-13 22:10:34 +11002640 if (counter->prev_state <= PERF_COUNTER_STATE_OFF)
2641 atomic64_set(&counter->hw.prev_count,
2642 get_cpu_migrations(counter));
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01002643 return 0;
Ingo Molnar6c594c22008-12-14 12:34:15 +01002644}
2645
2646static void cpu_migrations_perf_counter_disable(struct perf_counter *counter)
2647{
2648 cpu_migrations_perf_counter_update(counter);
2649}
2650
Robert Richter4aeb0b42009-04-29 12:47:03 +02002651static const struct pmu perf_ops_cpu_migrations = {
Ingo Molnar76715812008-12-17 14:20:28 +01002652 .enable = cpu_migrations_perf_counter_enable,
2653 .disable = cpu_migrations_perf_counter_disable,
2654 .read = cpu_migrations_perf_counter_read,
Ingo Molnar6c594c22008-12-14 12:34:15 +01002655};
2656
Peter Zijlstrae077df42009-03-19 20:26:17 +01002657#ifdef CONFIG_EVENT_PROFILE
2658void perf_tpcounter_event(int event_id)
2659{
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002660 struct pt_regs *regs = get_irq_regs();
2661
2662 if (!regs)
2663 regs = task_pt_regs(current);
2664
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002665 __perf_swcounter_event(PERF_TYPE_TRACEPOINT, event_id, 1, 1, regs, 0);
Peter Zijlstrae077df42009-03-19 20:26:17 +01002666}
Steven Whitehouseff7b1b42009-04-15 16:55:05 +01002667EXPORT_SYMBOL_GPL(perf_tpcounter_event);
Peter Zijlstrae077df42009-03-19 20:26:17 +01002668
2669extern int ftrace_profile_enable(int);
2670extern void ftrace_profile_disable(int);
2671
2672static void tp_perf_counter_destroy(struct perf_counter *counter)
2673{
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002674 ftrace_profile_disable(perf_event_id(&counter->hw_event));
Peter Zijlstrae077df42009-03-19 20:26:17 +01002675}
2676
Robert Richter4aeb0b42009-04-29 12:47:03 +02002677static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
Peter Zijlstrae077df42009-03-19 20:26:17 +01002678{
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002679 int event_id = perf_event_id(&counter->hw_event);
Peter Zijlstrae077df42009-03-19 20:26:17 +01002680 int ret;
2681
2682 ret = ftrace_profile_enable(event_id);
2683 if (ret)
2684 return NULL;
2685
2686 counter->destroy = tp_perf_counter_destroy;
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002687 counter->hw.irq_period = counter->hw_event.irq_period;
Peter Zijlstrae077df42009-03-19 20:26:17 +01002688
2689 return &perf_ops_generic;
2690}
2691#else
Robert Richter4aeb0b42009-04-29 12:47:03 +02002692static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
Peter Zijlstrae077df42009-03-19 20:26:17 +01002693{
2694 return NULL;
2695}
2696#endif
2697
Robert Richter4aeb0b42009-04-29 12:47:03 +02002698static const struct pmu *sw_perf_counter_init(struct perf_counter *counter)
Ingo Molnar5c92d122008-12-11 13:21:10 +01002699{
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002700 struct perf_counter_hw_event *hw_event = &counter->hw_event;
Robert Richter4aeb0b42009-04-29 12:47:03 +02002701 const struct pmu *pmu = NULL;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002702 struct hw_perf_counter *hwc = &counter->hw;
Ingo Molnar5c92d122008-12-11 13:21:10 +01002703
Paul Mackerras0475f9e2009-02-11 14:35:35 +11002704 /*
2705 * Software counters (currently) can't in general distinguish
2706 * between user, kernel and hypervisor events.
2707 * However, context switches and cpu migrations are considered
2708 * to be kernel events, and page faults are never hypervisor
2709 * events.
2710 */
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002711 switch (perf_event_id(&counter->hw_event)) {
Ingo Molnar5c92d122008-12-11 13:21:10 +01002712 case PERF_COUNT_CPU_CLOCK:
Robert Richter4aeb0b42009-04-29 12:47:03 +02002713 pmu = &perf_ops_cpu_clock;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002714
2715 if (hw_event->irq_period && hw_event->irq_period < 10000)
2716 hw_event->irq_period = 10000;
Ingo Molnar5c92d122008-12-11 13:21:10 +01002717 break;
Ingo Molnarbae43c92008-12-11 14:03:20 +01002718 case PERF_COUNT_TASK_CLOCK:
Paul Mackerras23a185c2009-02-09 22:42:47 +11002719 /*
2720 * If the user instantiates this as a per-cpu counter,
2721 * use the cpu_clock counter instead.
2722 */
2723 if (counter->ctx->task)
Robert Richter4aeb0b42009-04-29 12:47:03 +02002724 pmu = &perf_ops_task_clock;
Paul Mackerras23a185c2009-02-09 22:42:47 +11002725 else
Robert Richter4aeb0b42009-04-29 12:47:03 +02002726 pmu = &perf_ops_cpu_clock;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002727
2728 if (hw_event->irq_period && hw_event->irq_period < 10000)
2729 hw_event->irq_period = 10000;
Ingo Molnarbae43c92008-12-11 14:03:20 +01002730 break;
Ingo Molnare06c61a2008-12-14 14:44:31 +01002731 case PERF_COUNT_PAGE_FAULTS:
Peter Zijlstraac17dc82009-03-13 12:21:34 +01002732 case PERF_COUNT_PAGE_FAULTS_MIN:
2733 case PERF_COUNT_PAGE_FAULTS_MAJ:
Ingo Molnar5d6a27d2008-12-14 12:28:33 +01002734 case PERF_COUNT_CONTEXT_SWITCHES:
Robert Richter4aeb0b42009-04-29 12:47:03 +02002735 pmu = &perf_ops_generic;
Ingo Molnar5d6a27d2008-12-14 12:28:33 +01002736 break;
Ingo Molnar6c594c22008-12-14 12:34:15 +01002737 case PERF_COUNT_CPU_MIGRATIONS:
Paul Mackerras0475f9e2009-02-11 14:35:35 +11002738 if (!counter->hw_event.exclude_kernel)
Robert Richter4aeb0b42009-04-29 12:47:03 +02002739 pmu = &perf_ops_cpu_migrations;
Ingo Molnar6c594c22008-12-14 12:34:15 +01002740 break;
Ingo Molnar5c92d122008-12-11 13:21:10 +01002741 }
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002742
Robert Richter4aeb0b42009-04-29 12:47:03 +02002743 if (pmu)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002744 hwc->irq_period = hw_event->irq_period;
2745
Robert Richter4aeb0b42009-04-29 12:47:03 +02002746 return pmu;
Ingo Molnar5c92d122008-12-11 13:21:10 +01002747}
2748
Thomas Gleixner0793a612008-12-04 20:12:29 +01002749/*
2750 * Allocate and initialize a counter structure
2751 */
2752static struct perf_counter *
Ingo Molnar04289bb2008-12-11 08:38:42 +01002753perf_counter_alloc(struct perf_counter_hw_event *hw_event,
2754 int cpu,
Paul Mackerras23a185c2009-02-09 22:42:47 +11002755 struct perf_counter_context *ctx,
Ingo Molnar9b51f662008-12-12 13:49:45 +01002756 struct perf_counter *group_leader,
2757 gfp_t gfpflags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01002758{
Robert Richter4aeb0b42009-04-29 12:47:03 +02002759 const struct pmu *pmu;
Ingo Molnar621a01e2008-12-11 12:46:46 +01002760 struct perf_counter *counter;
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02002761 long err;
Thomas Gleixner0793a612008-12-04 20:12:29 +01002762
Ingo Molnar9b51f662008-12-12 13:49:45 +01002763 counter = kzalloc(sizeof(*counter), gfpflags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002764 if (!counter)
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02002765 return ERR_PTR(-ENOMEM);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002766
Ingo Molnar04289bb2008-12-11 08:38:42 +01002767 /*
2768 * Single counters are their own group leaders, with an
2769 * empty sibling list:
2770 */
2771 if (!group_leader)
2772 group_leader = counter;
2773
Thomas Gleixner0793a612008-12-04 20:12:29 +01002774 mutex_init(&counter->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01002775 INIT_LIST_HEAD(&counter->list_entry);
Peter Zijlstra592903c2009-03-13 12:21:36 +01002776 INIT_LIST_HEAD(&counter->event_entry);
Ingo Molnar04289bb2008-12-11 08:38:42 +01002777 INIT_LIST_HEAD(&counter->sibling_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002778 init_waitqueue_head(&counter->waitq);
2779
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002780 mutex_init(&counter->mmap_mutex);
2781
Paul Mackerrasd859e292009-01-17 18:10:22 +11002782 INIT_LIST_HEAD(&counter->child_list);
2783
Ingo Molnar9f66a382008-12-10 12:33:23 +01002784 counter->cpu = cpu;
2785 counter->hw_event = *hw_event;
Ingo Molnar04289bb2008-12-11 08:38:42 +01002786 counter->group_leader = group_leader;
Robert Richter4aeb0b42009-04-29 12:47:03 +02002787 counter->pmu = NULL;
Paul Mackerras23a185c2009-02-09 22:42:47 +11002788 counter->ctx = ctx;
Ingo Molnar621a01e2008-12-11 12:46:46 +01002789
Ingo Molnar235c7fc2008-12-21 14:43:25 +01002790 counter->state = PERF_COUNTER_STATE_INACTIVE;
Ingo Molnara86ed502008-12-17 00:43:10 +01002791 if (hw_event->disabled)
2792 counter->state = PERF_COUNTER_STATE_OFF;
2793
Robert Richter4aeb0b42009-04-29 12:47:03 +02002794 pmu = NULL;
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002795
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002796 if (perf_event_raw(hw_event)) {
Robert Richter4aeb0b42009-04-29 12:47:03 +02002797 pmu = hw_perf_counter_init(counter);
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002798 goto done;
2799 }
2800
2801 switch (perf_event_type(hw_event)) {
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002802 case PERF_TYPE_HARDWARE:
Robert Richter4aeb0b42009-04-29 12:47:03 +02002803 pmu = hw_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002804 break;
2805
2806 case PERF_TYPE_SOFTWARE:
Robert Richter4aeb0b42009-04-29 12:47:03 +02002807 pmu = sw_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002808 break;
2809
2810 case PERF_TYPE_TRACEPOINT:
Robert Richter4aeb0b42009-04-29 12:47:03 +02002811 pmu = tp_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002812 break;
2813 }
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002814done:
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02002815 err = 0;
Robert Richter4aeb0b42009-04-29 12:47:03 +02002816 if (!pmu)
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02002817 err = -EINVAL;
Robert Richter4aeb0b42009-04-29 12:47:03 +02002818 else if (IS_ERR(pmu))
2819 err = PTR_ERR(pmu);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02002820
2821 if (err) {
2822 kfree(counter);
2823 return ERR_PTR(err);
2824 }
2825
Robert Richter4aeb0b42009-04-29 12:47:03 +02002826 counter->pmu = pmu;
Thomas Gleixner0793a612008-12-04 20:12:29 +01002827
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002828 if (counter->hw_event.mmap)
2829 atomic_inc(&nr_mmap_tracking);
2830 if (counter->hw_event.munmap)
2831 atomic_inc(&nr_munmap_tracking);
2832 if (counter->hw_event.comm)
2833 atomic_inc(&nr_comm_tracking);
2834
Thomas Gleixner0793a612008-12-04 20:12:29 +01002835 return counter;
2836}
2837
2838/**
Paul Mackerras2743a5b2009-03-04 20:36:51 +11002839 * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
Ingo Molnar9f66a382008-12-10 12:33:23 +01002840 *
2841 * @hw_event_uptr: event type attributes for monitoring/sampling
Thomas Gleixner0793a612008-12-04 20:12:29 +01002842 * @pid: target pid
Ingo Molnar9f66a382008-12-10 12:33:23 +01002843 * @cpu: target cpu
2844 * @group_fd: group leader counter fd
Thomas Gleixner0793a612008-12-04 20:12:29 +01002845 */
Paul Mackerras2743a5b2009-03-04 20:36:51 +11002846SYSCALL_DEFINE5(perf_counter_open,
Paul Mackerrasf3dfd262009-02-26 22:43:46 +11002847 const struct perf_counter_hw_event __user *, hw_event_uptr,
Paul Mackerras2743a5b2009-03-04 20:36:51 +11002848 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01002849{
Ingo Molnar04289bb2008-12-11 08:38:42 +01002850 struct perf_counter *counter, *group_leader;
Ingo Molnar9f66a382008-12-10 12:33:23 +01002851 struct perf_counter_hw_event hw_event;
Ingo Molnar04289bb2008-12-11 08:38:42 +01002852 struct perf_counter_context *ctx;
Ingo Molnar9b51f662008-12-12 13:49:45 +01002853 struct file *counter_file = NULL;
Ingo Molnar04289bb2008-12-11 08:38:42 +01002854 struct file *group_file = NULL;
2855 int fput_needed = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01002856 int fput_needed2 = 0;
Thomas Gleixner0793a612008-12-04 20:12:29 +01002857 int ret;
2858
Paul Mackerras2743a5b2009-03-04 20:36:51 +11002859 /* for future expandability... */
2860 if (flags)
2861 return -EINVAL;
2862
Ingo Molnar9f66a382008-12-10 12:33:23 +01002863 if (copy_from_user(&hw_event, hw_event_uptr, sizeof(hw_event)) != 0)
Thomas Gleixnereab656a2008-12-08 19:26:59 +01002864 return -EFAULT;
2865
Ingo Molnar04289bb2008-12-11 08:38:42 +01002866 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01002867 * Get the target context (task or percpu):
2868 */
2869 ctx = find_get_context(pid, cpu);
2870 if (IS_ERR(ctx))
2871 return PTR_ERR(ctx);
2872
2873 /*
2874 * Look up the group leader (we will attach this counter to it):
Ingo Molnar04289bb2008-12-11 08:38:42 +01002875 */
2876 group_leader = NULL;
2877 if (group_fd != -1) {
2878 ret = -EINVAL;
2879 group_file = fget_light(group_fd, &fput_needed);
2880 if (!group_file)
Ingo Molnarccff2862008-12-11 11:26:29 +01002881 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01002882 if (group_file->f_op != &perf_fops)
Ingo Molnarccff2862008-12-11 11:26:29 +01002883 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01002884
2885 group_leader = group_file->private_data;
2886 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01002887 * Do not allow a recursive hierarchy (this new sibling
2888 * becoming part of another group-sibling):
Ingo Molnar04289bb2008-12-11 08:38:42 +01002889 */
Ingo Molnarccff2862008-12-11 11:26:29 +01002890 if (group_leader->group_leader != group_leader)
2891 goto err_put_context;
2892 /*
2893 * Do not allow to attach to a group in a different
2894 * task or CPU context:
2895 */
2896 if (group_leader->ctx != ctx)
2897 goto err_put_context;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11002898 /*
2899 * Only a group leader can be exclusive or pinned
2900 */
2901 if (hw_event.exclusive || hw_event.pinned)
2902 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01002903 }
2904
Paul Mackerras23a185c2009-02-09 22:42:47 +11002905 counter = perf_counter_alloc(&hw_event, cpu, ctx, group_leader,
2906 GFP_KERNEL);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02002907 ret = PTR_ERR(counter);
2908 if (IS_ERR(counter))
Thomas Gleixner0793a612008-12-04 20:12:29 +01002909 goto err_put_context;
2910
Thomas Gleixner0793a612008-12-04 20:12:29 +01002911 ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
2912 if (ret < 0)
Ingo Molnar9b51f662008-12-12 13:49:45 +01002913 goto err_free_put_context;
2914
2915 counter_file = fget_light(ret, &fput_needed2);
2916 if (!counter_file)
2917 goto err_free_put_context;
2918
2919 counter->filp = counter_file;
Paul Mackerrasd859e292009-01-17 18:10:22 +11002920 mutex_lock(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01002921 perf_install_in_context(ctx, counter, cpu);
Paul Mackerrasd859e292009-01-17 18:10:22 +11002922 mutex_unlock(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01002923
2924 fput_light(counter_file, fput_needed2);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002925
Ingo Molnar04289bb2008-12-11 08:38:42 +01002926out_fput:
2927 fput_light(group_file, fput_needed);
2928
Thomas Gleixner0793a612008-12-04 20:12:29 +01002929 return ret;
2930
Ingo Molnar9b51f662008-12-12 13:49:45 +01002931err_free_put_context:
Thomas Gleixner0793a612008-12-04 20:12:29 +01002932 kfree(counter);
2933
2934err_put_context:
2935 put_context(ctx);
2936
Ingo Molnar04289bb2008-12-11 08:38:42 +01002937 goto out_fput;
Thomas Gleixner0793a612008-12-04 20:12:29 +01002938}
2939
Ingo Molnar9b51f662008-12-12 13:49:45 +01002940/*
2941 * Initialize the perf_counter context in a task_struct:
2942 */
2943static void
2944__perf_counter_init_context(struct perf_counter_context *ctx,
2945 struct task_struct *task)
2946{
2947 memset(ctx, 0, sizeof(*ctx));
2948 spin_lock_init(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +11002949 mutex_init(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01002950 INIT_LIST_HEAD(&ctx->counter_list);
Peter Zijlstra592903c2009-03-13 12:21:36 +01002951 INIT_LIST_HEAD(&ctx->event_list);
Ingo Molnar9b51f662008-12-12 13:49:45 +01002952 ctx->task = task;
2953}
2954
2955/*
2956 * inherit a counter from parent task to child task:
2957 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11002958static struct perf_counter *
Ingo Molnar9b51f662008-12-12 13:49:45 +01002959inherit_counter(struct perf_counter *parent_counter,
2960 struct task_struct *parent,
2961 struct perf_counter_context *parent_ctx,
2962 struct task_struct *child,
Paul Mackerrasd859e292009-01-17 18:10:22 +11002963 struct perf_counter *group_leader,
Ingo Molnar9b51f662008-12-12 13:49:45 +01002964 struct perf_counter_context *child_ctx)
2965{
2966 struct perf_counter *child_counter;
2967
Paul Mackerrasd859e292009-01-17 18:10:22 +11002968 /*
2969 * Instead of creating recursive hierarchies of counters,
2970 * we link inherited counters back to the original parent,
2971 * which has a filp for sure, which we use as the reference
2972 * count:
2973 */
2974 if (parent_counter->parent)
2975 parent_counter = parent_counter->parent;
2976
Ingo Molnar9b51f662008-12-12 13:49:45 +01002977 child_counter = perf_counter_alloc(&parent_counter->hw_event,
Paul Mackerras23a185c2009-02-09 22:42:47 +11002978 parent_counter->cpu, child_ctx,
2979 group_leader, GFP_KERNEL);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02002980 if (IS_ERR(child_counter))
2981 return child_counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01002982
2983 /*
2984 * Link it up in the child's context:
2985 */
Ingo Molnar9b51f662008-12-12 13:49:45 +01002986 child_counter->task = child;
Paul Mackerras53cfbf52009-03-25 22:46:58 +11002987 add_counter_to_ctx(child_counter, child_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01002988
2989 child_counter->parent = parent_counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01002990 /*
2991 * inherit into child's child as well:
2992 */
2993 child_counter->hw_event.inherit = 1;
2994
2995 /*
2996 * Get a reference to the parent filp - we will fput it
2997 * when the child counter exits. This is safe to do because
2998 * we are in the parent and we know that the filp still
2999 * exists and has a nonzero count:
3000 */
3001 atomic_long_inc(&parent_counter->filp->f_count);
3002
Paul Mackerrasd859e292009-01-17 18:10:22 +11003003 /*
3004 * Link this into the parent counter's child list
3005 */
3006 mutex_lock(&parent_counter->mutex);
3007 list_add_tail(&child_counter->child_list, &parent_counter->child_list);
3008
3009 /*
3010 * Make the child state follow the state of the parent counter,
3011 * not its hw_event.disabled bit. We hold the parent's mutex,
3012 * so we won't race with perf_counter_{en,dis}able_family.
3013 */
3014 if (parent_counter->state >= PERF_COUNTER_STATE_INACTIVE)
3015 child_counter->state = PERF_COUNTER_STATE_INACTIVE;
3016 else
3017 child_counter->state = PERF_COUNTER_STATE_OFF;
3018
3019 mutex_unlock(&parent_counter->mutex);
3020
3021 return child_counter;
3022}
3023
3024static int inherit_group(struct perf_counter *parent_counter,
3025 struct task_struct *parent,
3026 struct perf_counter_context *parent_ctx,
3027 struct task_struct *child,
3028 struct perf_counter_context *child_ctx)
3029{
3030 struct perf_counter *leader;
3031 struct perf_counter *sub;
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003032 struct perf_counter *child_ctr;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003033
3034 leader = inherit_counter(parent_counter, parent, parent_ctx,
3035 child, NULL, child_ctx);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003036 if (IS_ERR(leader))
3037 return PTR_ERR(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003038 list_for_each_entry(sub, &parent_counter->sibling_list, list_entry) {
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003039 child_ctr = inherit_counter(sub, parent, parent_ctx,
3040 child, leader, child_ctx);
3041 if (IS_ERR(child_ctr))
3042 return PTR_ERR(child_ctr);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003043 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01003044 return 0;
3045}
3046
Paul Mackerrasd859e292009-01-17 18:10:22 +11003047static void sync_child_counter(struct perf_counter *child_counter,
3048 struct perf_counter *parent_counter)
3049{
3050 u64 parent_val, child_val;
3051
3052 parent_val = atomic64_read(&parent_counter->count);
3053 child_val = atomic64_read(&child_counter->count);
3054
3055 /*
3056 * Add back the child's count to the parent's count:
3057 */
3058 atomic64_add(child_val, &parent_counter->count);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11003059 atomic64_add(child_counter->total_time_enabled,
3060 &parent_counter->child_total_time_enabled);
3061 atomic64_add(child_counter->total_time_running,
3062 &parent_counter->child_total_time_running);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003063
3064 /*
3065 * Remove this counter from the parent's list
3066 */
3067 mutex_lock(&parent_counter->mutex);
3068 list_del_init(&child_counter->child_list);
3069 mutex_unlock(&parent_counter->mutex);
3070
3071 /*
3072 * Release the parent counter, if this was the last
3073 * reference to it.
3074 */
3075 fput(parent_counter->filp);
3076}
3077
Ingo Molnar9b51f662008-12-12 13:49:45 +01003078static void
3079__perf_counter_exit_task(struct task_struct *child,
3080 struct perf_counter *child_counter,
3081 struct perf_counter_context *child_ctx)
3082{
3083 struct perf_counter *parent_counter;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003084 struct perf_counter *sub, *tmp;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003085
3086 /*
Ingo Molnar235c7fc2008-12-21 14:43:25 +01003087 * If we do not self-reap then we have to wait for the
3088 * child task to unschedule (it will happen for sure),
3089 * so that its counter is at its final count. (This
3090 * condition triggers rarely - child tasks usually get
3091 * off their CPU before the parent has a chance to
3092 * get this far into the reaping action)
Ingo Molnar9b51f662008-12-12 13:49:45 +01003093 */
Ingo Molnar235c7fc2008-12-21 14:43:25 +01003094 if (child != current) {
3095 wait_task_inactive(child, 0);
3096 list_del_init(&child_counter->list_entry);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11003097 update_counter_times(child_counter);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01003098 } else {
Ingo Molnar0cc0c022008-12-14 23:20:36 +01003099 struct perf_cpu_context *cpuctx;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01003100 unsigned long flags;
3101 u64 perf_flags;
3102
3103 /*
3104 * Disable and unlink this counter.
3105 *
3106 * Be careful about zapping the list - IRQ/NMI context
3107 * could still be processing it:
3108 */
Peter Zijlstra849691a2009-04-06 11:45:12 +02003109 local_irq_save(flags);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01003110 perf_flags = hw_perf_save_disable();
Ingo Molnar0cc0c022008-12-14 23:20:36 +01003111
3112 cpuctx = &__get_cpu_var(perf_cpu_context);
3113
Paul Mackerrasd859e292009-01-17 18:10:22 +11003114 group_sched_out(child_counter, cpuctx, child_ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11003115 update_counter_times(child_counter);
Ingo Molnar0cc0c022008-12-14 23:20:36 +01003116
Ingo Molnar235c7fc2008-12-21 14:43:25 +01003117 list_del_init(&child_counter->list_entry);
3118
3119 child_ctx->nr_counters--;
3120
3121 hw_perf_restore(perf_flags);
Peter Zijlstra849691a2009-04-06 11:45:12 +02003122 local_irq_restore(flags);
Ingo Molnar0cc0c022008-12-14 23:20:36 +01003123 }
3124
Ingo Molnar9b51f662008-12-12 13:49:45 +01003125 parent_counter = child_counter->parent;
3126 /*
3127 * It can happen that parent exits first, and has counters
3128 * that are still around due to the child reference. These
3129 * counters need to be zapped - but otherwise linger.
3130 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11003131 if (parent_counter) {
3132 sync_child_counter(child_counter, parent_counter);
3133 list_for_each_entry_safe(sub, tmp, &child_counter->sibling_list,
3134 list_entry) {
Paul Mackerras4bcf3492009-02-11 13:53:19 +01003135 if (sub->parent) {
Paul Mackerrasd859e292009-01-17 18:10:22 +11003136 sync_child_counter(sub, sub->parent);
Peter Zijlstraf1600952009-03-19 20:26:16 +01003137 free_counter(sub);
Paul Mackerras4bcf3492009-02-11 13:53:19 +01003138 }
Paul Mackerrasd859e292009-01-17 18:10:22 +11003139 }
Peter Zijlstraf1600952009-03-19 20:26:16 +01003140 free_counter(child_counter);
Paul Mackerras4bcf3492009-02-11 13:53:19 +01003141 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01003142}
3143
3144/*
Paul Mackerrasd859e292009-01-17 18:10:22 +11003145 * When a child task exits, feed back counter values to parent counters.
Ingo Molnar9b51f662008-12-12 13:49:45 +01003146 *
Paul Mackerrasd859e292009-01-17 18:10:22 +11003147 * Note: we may be running in child context, but the PID is not hashed
Ingo Molnar9b51f662008-12-12 13:49:45 +01003148 * anymore so new counters will not be added.
3149 */
3150void perf_counter_exit_task(struct task_struct *child)
3151{
3152 struct perf_counter *child_counter, *tmp;
3153 struct perf_counter_context *child_ctx;
3154
3155 child_ctx = &child->perf_counter_ctx;
3156
3157 if (likely(!child_ctx->nr_counters))
3158 return;
3159
3160 list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list,
3161 list_entry)
3162 __perf_counter_exit_task(child, child_counter, child_ctx);
3163}
3164
3165/*
3166 * Initialize the perf_counter context in task_struct
3167 */
3168void perf_counter_init_task(struct task_struct *child)
3169{
3170 struct perf_counter_context *child_ctx, *parent_ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003171 struct perf_counter *counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003172 struct task_struct *parent = current;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003173
3174 child_ctx = &child->perf_counter_ctx;
3175 parent_ctx = &parent->perf_counter_ctx;
3176
3177 __perf_counter_init_context(child_ctx, child);
3178
3179 /*
3180 * This is executed from the parent task context, so inherit
3181 * counters that have been marked for cloning:
3182 */
3183
3184 if (likely(!parent_ctx->nr_counters))
3185 return;
3186
3187 /*
3188 * Lock the parent list. No need to lock the child - not PID
3189 * hashed yet and not running, so nobody can access it.
3190 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11003191 mutex_lock(&parent_ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003192
3193 /*
3194 * We dont have to disable NMIs - we are only looking at
3195 * the list, not manipulating it:
3196 */
3197 list_for_each_entry(counter, &parent_ctx->counter_list, list_entry) {
Paul Mackerrasd859e292009-01-17 18:10:22 +11003198 if (!counter->hw_event.inherit)
Ingo Molnar9b51f662008-12-12 13:49:45 +01003199 continue;
3200
Paul Mackerrasd859e292009-01-17 18:10:22 +11003201 if (inherit_group(counter, parent,
Ingo Molnar9b51f662008-12-12 13:49:45 +01003202 parent_ctx, child, child_ctx))
3203 break;
3204 }
3205
Paul Mackerrasd859e292009-01-17 18:10:22 +11003206 mutex_unlock(&parent_ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003207}
3208
Ingo Molnar04289bb2008-12-11 08:38:42 +01003209static void __cpuinit perf_counter_init_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003210{
Ingo Molnar04289bb2008-12-11 08:38:42 +01003211 struct perf_cpu_context *cpuctx;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003212
Ingo Molnar04289bb2008-12-11 08:38:42 +01003213 cpuctx = &per_cpu(perf_cpu_context, cpu);
3214 __perf_counter_init_context(&cpuctx->ctx, NULL);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003215
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003216 spin_lock(&perf_resource_lock);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003217 cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu;
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003218 spin_unlock(&perf_resource_lock);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003219
Paul Mackerras01d02872009-01-14 13:44:19 +11003220 hw_perf_counter_setup(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003221}
3222
3223#ifdef CONFIG_HOTPLUG_CPU
Ingo Molnar04289bb2008-12-11 08:38:42 +01003224static void __perf_counter_exit_cpu(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003225{
3226 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
3227 struct perf_counter_context *ctx = &cpuctx->ctx;
3228 struct perf_counter *counter, *tmp;
3229
Ingo Molnar04289bb2008-12-11 08:38:42 +01003230 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry)
3231 __perf_counter_remove_from_context(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003232}
Ingo Molnar04289bb2008-12-11 08:38:42 +01003233static void perf_counter_exit_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003234{
Paul Mackerrasd859e292009-01-17 18:10:22 +11003235 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
3236 struct perf_counter_context *ctx = &cpuctx->ctx;
3237
3238 mutex_lock(&ctx->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003239 smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003240 mutex_unlock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003241}
3242#else
Ingo Molnar04289bb2008-12-11 08:38:42 +01003243static inline void perf_counter_exit_cpu(int cpu) { }
Thomas Gleixner0793a612008-12-04 20:12:29 +01003244#endif
3245
3246static int __cpuinit
3247perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
3248{
3249 unsigned int cpu = (long)hcpu;
3250
3251 switch (action) {
3252
3253 case CPU_UP_PREPARE:
3254 case CPU_UP_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01003255 perf_counter_init_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003256 break;
3257
3258 case CPU_DOWN_PREPARE:
3259 case CPU_DOWN_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01003260 perf_counter_exit_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003261 break;
3262
3263 default:
3264 break;
3265 }
3266
3267 return NOTIFY_OK;
3268}
3269
3270static struct notifier_block __cpuinitdata perf_cpu_nb = {
3271 .notifier_call = perf_cpu_notify,
3272};
3273
Ingo Molnar0d905bc2009-05-04 19:13:30 +02003274void __init perf_counter_init(void)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003275{
3276 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
3277 (void *)(long)smp_processor_id());
3278 register_cpu_notifier(&perf_cpu_nb);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003279}
Thomas Gleixner0793a612008-12-04 20:12:29 +01003280
3281static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
3282{
3283 return sprintf(buf, "%d\n", perf_reserved_percpu);
3284}
3285
3286static ssize_t
3287perf_set_reserve_percpu(struct sysdev_class *class,
3288 const char *buf,
3289 size_t count)
3290{
3291 struct perf_cpu_context *cpuctx;
3292 unsigned long val;
3293 int err, cpu, mpt;
3294
3295 err = strict_strtoul(buf, 10, &val);
3296 if (err)
3297 return err;
3298 if (val > perf_max_counters)
3299 return -EINVAL;
3300
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003301 spin_lock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003302 perf_reserved_percpu = val;
3303 for_each_online_cpu(cpu) {
3304 cpuctx = &per_cpu(perf_cpu_context, cpu);
3305 spin_lock_irq(&cpuctx->ctx.lock);
3306 mpt = min(perf_max_counters - cpuctx->ctx.nr_counters,
3307 perf_max_counters - perf_reserved_percpu);
3308 cpuctx->max_pertask = mpt;
3309 spin_unlock_irq(&cpuctx->ctx.lock);
3310 }
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003311 spin_unlock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003312
3313 return count;
3314}
3315
3316static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
3317{
3318 return sprintf(buf, "%d\n", perf_overcommit);
3319}
3320
3321static ssize_t
3322perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
3323{
3324 unsigned long val;
3325 int err;
3326
3327 err = strict_strtoul(buf, 10, &val);
3328 if (err)
3329 return err;
3330 if (val > 1)
3331 return -EINVAL;
3332
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003333 spin_lock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003334 perf_overcommit = val;
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003335 spin_unlock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003336
3337 return count;
3338}
3339
3340static SYSDEV_CLASS_ATTR(
3341 reserve_percpu,
3342 0644,
3343 perf_show_reserve_percpu,
3344 perf_set_reserve_percpu
3345 );
3346
3347static SYSDEV_CLASS_ATTR(
3348 overcommit,
3349 0644,
3350 perf_show_overcommit,
3351 perf_set_overcommit
3352 );
3353
3354static struct attribute *perfclass_attrs[] = {
3355 &attr_reserve_percpu.attr,
3356 &attr_overcommit.attr,
3357 NULL
3358};
3359
3360static struct attribute_group perfclass_attr_group = {
3361 .attrs = perfclass_attrs,
3362 .name = "perf_counters",
3363};
3364
3365static int __init perf_counter_sysfs_init(void)
3366{
3367 return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
3368 &perfclass_attr_group);
3369}
3370device_initcall(perf_counter_sysfs_init);