blob: 582108addefab7c5d66ac746310aedc2245186ea [file] [log] [blame]
Thomas Gleixner0793a612008-12-04 20:12:29 +01001/*
2 * Performance counter core code
3 *
4 * Copyright(C) 2008 Thomas Gleixner <tglx@linutronix.de>
5 * Copyright(C) 2008 Red Hat, Inc., Ingo Molnar
6 *
Peter Zijlstra7b732a72009-03-23 18:22:10 +01007 *
8 * For licensing details see kernel-base/COPYING
Thomas Gleixner0793a612008-12-04 20:12:29 +01009 */
10
11#include <linux/fs.h>
Peter Zijlstrab9cacc72009-03-25 12:30:22 +010012#include <linux/mm.h>
Thomas Gleixner0793a612008-12-04 20:12:29 +010013#include <linux/cpu.h>
14#include <linux/smp.h>
Ingo Molnar04289bb2008-12-11 08:38:42 +010015#include <linux/file.h>
Thomas Gleixner0793a612008-12-04 20:12:29 +010016#include <linux/poll.h>
17#include <linux/sysfs.h>
18#include <linux/ptrace.h>
19#include <linux/percpu.h>
Peter Zijlstrab9cacc72009-03-25 12:30:22 +010020#include <linux/vmstat.h>
21#include <linux/hardirq.h>
22#include <linux/rculist.h>
Thomas Gleixner0793a612008-12-04 20:12:29 +010023#include <linux/uaccess.h>
24#include <linux/syscalls.h>
25#include <linux/anon_inodes.h>
Ingo Molnaraa9c4c02008-12-17 14:10:57 +010026#include <linux/kernel_stat.h>
Thomas Gleixner0793a612008-12-04 20:12:29 +010027#include <linux/perf_counter.h>
Peter Zijlstra0a4a9392009-03-30 19:07:05 +020028#include <linux/dcache.h>
Thomas Gleixner0793a612008-12-04 20:12:29 +010029
Tim Blechmann4e193bd2009-03-14 14:29:25 +010030#include <asm/irq_regs.h>
31
Thomas Gleixner0793a612008-12-04 20:12:29 +010032/*
33 * Each CPU has a list of per CPU counters:
34 */
35DEFINE_PER_CPU(struct perf_cpu_context, perf_cpu_context);
36
Ingo Molnar088e2852008-12-14 20:21:00 +010037int perf_max_counters __read_mostly = 1;
Thomas Gleixner0793a612008-12-04 20:12:29 +010038static int perf_reserved_percpu __read_mostly;
39static int perf_overcommit __read_mostly = 1;
40
Peter Zijlstra9ee318a2009-04-09 10:53:44 +020041static atomic_t nr_mmap_tracking __read_mostly;
42static atomic_t nr_munmap_tracking __read_mostly;
43static atomic_t nr_comm_tracking __read_mostly;
44
Peter Zijlstra1ccd1542009-04-09 10:53:45 +020045int sysctl_perf_counter_priv __read_mostly; /* do we need to be privileged */
46
Thomas Gleixner0793a612008-12-04 20:12:29 +010047/*
48 * Mutex for (sysadmin-configurable) counter reservations:
49 */
50static DEFINE_MUTEX(perf_resource_mutex);
51
52/*
53 * Architecture provided APIs - weak aliases:
54 */
Robert Richter4aeb0b42009-04-29 12:47:03 +020055extern __weak const struct pmu *hw_perf_counter_init(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +010056{
Paul Mackerrasff6f0542009-01-09 16:19:25 +110057 return NULL;
Thomas Gleixner0793a612008-12-04 20:12:29 +010058}
59
Ingo Molnar01b28382008-12-11 13:45:51 +010060u64 __weak hw_perf_save_disable(void) { return 0; }
Yinghai Lu01ea1cc2008-12-26 21:05:06 -080061void __weak hw_perf_restore(u64 ctrl) { barrier(); }
Paul Mackerras01d02872009-01-14 13:44:19 +110062void __weak hw_perf_counter_setup(int cpu) { barrier(); }
Paul Mackerras3cbed422009-01-09 16:43:42 +110063int __weak hw_perf_group_sched_in(struct perf_counter *group_leader,
64 struct perf_cpu_context *cpuctx,
65 struct perf_counter_context *ctx, int cpu)
66{
67 return 0;
68}
Thomas Gleixner0793a612008-12-04 20:12:29 +010069
Paul Mackerras4eb96fc2009-01-09 17:24:34 +110070void __weak perf_counter_print_debug(void) { }
71
Ingo Molnar04289bb2008-12-11 08:38:42 +010072static void
73list_add_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
74{
75 struct perf_counter *group_leader = counter->group_leader;
76
77 /*
78 * Depending on whether it is a standalone or sibling counter,
79 * add it straight to the context's counter list, or to the group
80 * leader's sibling list:
81 */
82 if (counter->group_leader == counter)
83 list_add_tail(&counter->list_entry, &ctx->counter_list);
Peter Zijlstra5c148192009-03-25 12:30:23 +010084 else {
Ingo Molnar04289bb2008-12-11 08:38:42 +010085 list_add_tail(&counter->list_entry, &group_leader->sibling_list);
Peter Zijlstra5c148192009-03-25 12:30:23 +010086 group_leader->nr_siblings++;
87 }
Peter Zijlstra592903c2009-03-13 12:21:36 +010088
89 list_add_rcu(&counter->event_entry, &ctx->event_list);
Ingo Molnar04289bb2008-12-11 08:38:42 +010090}
91
92static void
93list_del_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
94{
95 struct perf_counter *sibling, *tmp;
96
97 list_del_init(&counter->list_entry);
Peter Zijlstra592903c2009-03-13 12:21:36 +010098 list_del_rcu(&counter->event_entry);
Ingo Molnar04289bb2008-12-11 08:38:42 +010099
Peter Zijlstra5c148192009-03-25 12:30:23 +0100100 if (counter->group_leader != counter)
101 counter->group_leader->nr_siblings--;
102
Ingo Molnar04289bb2008-12-11 08:38:42 +0100103 /*
104 * If this was a group counter with sibling counters then
105 * upgrade the siblings to singleton counters by adding them
106 * to the context list directly:
107 */
108 list_for_each_entry_safe(sibling, tmp,
109 &counter->sibling_list, list_entry) {
110
Peter Zijlstra75564232009-03-13 12:21:29 +0100111 list_move_tail(&sibling->list_entry, &ctx->counter_list);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100112 sibling->group_leader = sibling;
113 }
114}
115
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100116static void
117counter_sched_out(struct perf_counter *counter,
118 struct perf_cpu_context *cpuctx,
119 struct perf_counter_context *ctx)
120{
121 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
122 return;
123
124 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200125 counter->tstamp_stopped = ctx->time;
Robert Richter4aeb0b42009-04-29 12:47:03 +0200126 counter->pmu->disable(counter);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100127 counter->oncpu = -1;
128
129 if (!is_software_counter(counter))
130 cpuctx->active_oncpu--;
131 ctx->nr_active--;
132 if (counter->hw_event.exclusive || !cpuctx->active_oncpu)
133 cpuctx->exclusive = 0;
134}
135
Paul Mackerrasd859e292009-01-17 18:10:22 +1100136static void
137group_sched_out(struct perf_counter *group_counter,
138 struct perf_cpu_context *cpuctx,
139 struct perf_counter_context *ctx)
140{
141 struct perf_counter *counter;
142
143 if (group_counter->state != PERF_COUNTER_STATE_ACTIVE)
144 return;
145
146 counter_sched_out(group_counter, cpuctx, ctx);
147
148 /*
149 * Schedule out siblings (if any):
150 */
151 list_for_each_entry(counter, &group_counter->sibling_list, list_entry)
152 counter_sched_out(counter, cpuctx, ctx);
153
154 if (group_counter->hw_event.exclusive)
155 cpuctx->exclusive = 0;
156}
157
Thomas Gleixner0793a612008-12-04 20:12:29 +0100158/*
159 * Cross CPU call to remove a performance counter
160 *
161 * We disable the counter on the hardware level first. After that we
162 * remove it from the context list.
163 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100164static void __perf_counter_remove_from_context(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100165{
166 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
167 struct perf_counter *counter = info;
168 struct perf_counter_context *ctx = counter->ctx;
Ingo Molnar9b51f662008-12-12 13:49:45 +0100169 unsigned long flags;
Ingo Molnar5c92d122008-12-11 13:21:10 +0100170 u64 perf_flags;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100171
172 /*
173 * If this is a task context, we need to check whether it is
174 * the current task context of this cpu. If not it has been
175 * scheduled out before the smp call arrived.
176 */
177 if (ctx->task && cpuctx->task_ctx != ctx)
178 return;
179
Peter Zijlstra849691a2009-04-06 11:45:12 +0200180 spin_lock_irqsave(&ctx->lock, flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100181
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100182 counter_sched_out(counter, cpuctx, ctx);
183
184 counter->task = NULL;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100185 ctx->nr_counters--;
186
187 /*
188 * Protect the list operation against NMI by disabling the
189 * counters on a global level. NOP for non NMI based counters.
190 */
Ingo Molnar01b28382008-12-11 13:45:51 +0100191 perf_flags = hw_perf_save_disable();
Ingo Molnar04289bb2008-12-11 08:38:42 +0100192 list_del_counter(counter, ctx);
Ingo Molnar01b28382008-12-11 13:45:51 +0100193 hw_perf_restore(perf_flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100194
195 if (!ctx->task) {
196 /*
197 * Allow more per task counters with respect to the
198 * reservation:
199 */
200 cpuctx->max_pertask =
201 min(perf_max_counters - ctx->nr_counters,
202 perf_max_counters - perf_reserved_percpu);
203 }
204
Peter Zijlstra849691a2009-04-06 11:45:12 +0200205 spin_unlock_irqrestore(&ctx->lock, flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100206}
207
208
209/*
210 * Remove the counter from a task's (or a CPU's) list of counters.
211 *
Paul Mackerrasd859e292009-01-17 18:10:22 +1100212 * Must be called with counter->mutex and ctx->mutex held.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100213 *
214 * CPU counters are removed with a smp call. For task counters we only
215 * call when the task is on a CPU.
216 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100217static void perf_counter_remove_from_context(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100218{
219 struct perf_counter_context *ctx = counter->ctx;
220 struct task_struct *task = ctx->task;
221
222 if (!task) {
223 /*
224 * Per cpu counters are removed via an smp call and
225 * the removal is always sucessful.
226 */
227 smp_call_function_single(counter->cpu,
Ingo Molnar04289bb2008-12-11 08:38:42 +0100228 __perf_counter_remove_from_context,
Thomas Gleixner0793a612008-12-04 20:12:29 +0100229 counter, 1);
230 return;
231 }
232
233retry:
Ingo Molnar04289bb2008-12-11 08:38:42 +0100234 task_oncpu_function_call(task, __perf_counter_remove_from_context,
Thomas Gleixner0793a612008-12-04 20:12:29 +0100235 counter);
236
237 spin_lock_irq(&ctx->lock);
238 /*
239 * If the context is active we need to retry the smp call.
240 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100241 if (ctx->nr_active && !list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100242 spin_unlock_irq(&ctx->lock);
243 goto retry;
244 }
245
246 /*
247 * The lock prevents that this context is scheduled in so we
Ingo Molnar04289bb2008-12-11 08:38:42 +0100248 * can remove the counter safely, if the call above did not
Thomas Gleixner0793a612008-12-04 20:12:29 +0100249 * succeed.
250 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100251 if (!list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100252 ctx->nr_counters--;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100253 list_del_counter(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100254 counter->task = NULL;
255 }
256 spin_unlock_irq(&ctx->lock);
257}
258
Peter Zijlstra4af49982009-04-06 11:45:10 +0200259static inline u64 perf_clock(void)
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100260{
Peter Zijlstra4af49982009-04-06 11:45:10 +0200261 return cpu_clock(smp_processor_id());
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100262}
263
264/*
265 * Update the record of the current time in a context.
266 */
Peter Zijlstra4af49982009-04-06 11:45:10 +0200267static void update_context_time(struct perf_counter_context *ctx)
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100268{
Peter Zijlstra4af49982009-04-06 11:45:10 +0200269 u64 now = perf_clock();
270
271 ctx->time += now - ctx->timestamp;
272 ctx->timestamp = now;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100273}
274
275/*
276 * Update the total_time_enabled and total_time_running fields for a counter.
277 */
278static void update_counter_times(struct perf_counter *counter)
279{
280 struct perf_counter_context *ctx = counter->ctx;
281 u64 run_end;
282
Peter Zijlstra4af49982009-04-06 11:45:10 +0200283 if (counter->state < PERF_COUNTER_STATE_INACTIVE)
284 return;
285
286 counter->total_time_enabled = ctx->time - counter->tstamp_enabled;
287
288 if (counter->state == PERF_COUNTER_STATE_INACTIVE)
289 run_end = counter->tstamp_stopped;
290 else
291 run_end = ctx->time;
292
293 counter->total_time_running = run_end - counter->tstamp_running;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100294}
295
296/*
297 * Update total_time_enabled and total_time_running for all counters in a group.
298 */
299static void update_group_times(struct perf_counter *leader)
300{
301 struct perf_counter *counter;
302
303 update_counter_times(leader);
304 list_for_each_entry(counter, &leader->sibling_list, list_entry)
305 update_counter_times(counter);
306}
307
308/*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100309 * Cross CPU call to disable a performance counter
310 */
311static void __perf_counter_disable(void *info)
312{
313 struct perf_counter *counter = info;
314 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
315 struct perf_counter_context *ctx = counter->ctx;
316 unsigned long flags;
317
318 /*
319 * If this is a per-task counter, need to check whether this
320 * counter's task is the current task on this cpu.
321 */
322 if (ctx->task && cpuctx->task_ctx != ctx)
323 return;
324
Peter Zijlstra849691a2009-04-06 11:45:12 +0200325 spin_lock_irqsave(&ctx->lock, flags);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100326
327 /*
328 * If the counter is on, turn it off.
329 * If it is in error state, leave it in error state.
330 */
331 if (counter->state >= PERF_COUNTER_STATE_INACTIVE) {
Peter Zijlstra4af49982009-04-06 11:45:10 +0200332 update_context_time(ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100333 update_counter_times(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100334 if (counter == counter->group_leader)
335 group_sched_out(counter, cpuctx, ctx);
336 else
337 counter_sched_out(counter, cpuctx, ctx);
338 counter->state = PERF_COUNTER_STATE_OFF;
339 }
340
Peter Zijlstra849691a2009-04-06 11:45:12 +0200341 spin_unlock_irqrestore(&ctx->lock, flags);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100342}
343
344/*
345 * Disable a counter.
346 */
347static void perf_counter_disable(struct perf_counter *counter)
348{
349 struct perf_counter_context *ctx = counter->ctx;
350 struct task_struct *task = ctx->task;
351
352 if (!task) {
353 /*
354 * Disable the counter on the cpu that it's on
355 */
356 smp_call_function_single(counter->cpu, __perf_counter_disable,
357 counter, 1);
358 return;
359 }
360
361 retry:
362 task_oncpu_function_call(task, __perf_counter_disable, counter);
363
364 spin_lock_irq(&ctx->lock);
365 /*
366 * If the counter is still active, we need to retry the cross-call.
367 */
368 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
369 spin_unlock_irq(&ctx->lock);
370 goto retry;
371 }
372
373 /*
374 * Since we have the lock this context can't be scheduled
375 * in, so we can change the state safely.
376 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100377 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
378 update_counter_times(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100379 counter->state = PERF_COUNTER_STATE_OFF;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100380 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100381
382 spin_unlock_irq(&ctx->lock);
383}
384
385/*
386 * Disable a counter and all its children.
387 */
388static void perf_counter_disable_family(struct perf_counter *counter)
389{
390 struct perf_counter *child;
391
392 perf_counter_disable(counter);
393
394 /*
395 * Lock the mutex to protect the list of children
396 */
397 mutex_lock(&counter->mutex);
398 list_for_each_entry(child, &counter->child_list, child_list)
399 perf_counter_disable(child);
400 mutex_unlock(&counter->mutex);
401}
402
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100403static int
404counter_sched_in(struct perf_counter *counter,
405 struct perf_cpu_context *cpuctx,
406 struct perf_counter_context *ctx,
407 int cpu)
408{
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100409 if (counter->state <= PERF_COUNTER_STATE_OFF)
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100410 return 0;
411
412 counter->state = PERF_COUNTER_STATE_ACTIVE;
413 counter->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */
414 /*
415 * The new state must be visible before we turn it on in the hardware:
416 */
417 smp_wmb();
418
Robert Richter4aeb0b42009-04-29 12:47:03 +0200419 if (counter->pmu->enable(counter)) {
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100420 counter->state = PERF_COUNTER_STATE_INACTIVE;
421 counter->oncpu = -1;
422 return -EAGAIN;
423 }
424
Peter Zijlstra4af49982009-04-06 11:45:10 +0200425 counter->tstamp_running += ctx->time - counter->tstamp_stopped;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100426
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100427 if (!is_software_counter(counter))
428 cpuctx->active_oncpu++;
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100429 ctx->nr_active++;
430
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100431 if (counter->hw_event.exclusive)
432 cpuctx->exclusive = 1;
433
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100434 return 0;
435}
436
Thomas Gleixner0793a612008-12-04 20:12:29 +0100437/*
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100438 * Return 1 for a group consisting entirely of software counters,
439 * 0 if the group contains any hardware counters.
440 */
441static int is_software_only_group(struct perf_counter *leader)
442{
443 struct perf_counter *counter;
444
445 if (!is_software_counter(leader))
446 return 0;
Peter Zijlstra5c148192009-03-25 12:30:23 +0100447
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100448 list_for_each_entry(counter, &leader->sibling_list, list_entry)
449 if (!is_software_counter(counter))
450 return 0;
Peter Zijlstra5c148192009-03-25 12:30:23 +0100451
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100452 return 1;
453}
454
455/*
456 * Work out whether we can put this counter group on the CPU now.
457 */
458static int group_can_go_on(struct perf_counter *counter,
459 struct perf_cpu_context *cpuctx,
460 int can_add_hw)
461{
462 /*
463 * Groups consisting entirely of software counters can always go on.
464 */
465 if (is_software_only_group(counter))
466 return 1;
467 /*
468 * If an exclusive group is already on, no other hardware
469 * counters can go on.
470 */
471 if (cpuctx->exclusive)
472 return 0;
473 /*
474 * If this group is exclusive and there are already
475 * counters on the CPU, it can't go on.
476 */
477 if (counter->hw_event.exclusive && cpuctx->active_oncpu)
478 return 0;
479 /*
480 * Otherwise, try to add it if all previous groups were able
481 * to go on.
482 */
483 return can_add_hw;
484}
485
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100486static void add_counter_to_ctx(struct perf_counter *counter,
487 struct perf_counter_context *ctx)
488{
489 list_add_counter(counter, ctx);
490 ctx->nr_counters++;
491 counter->prev_state = PERF_COUNTER_STATE_OFF;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200492 counter->tstamp_enabled = ctx->time;
493 counter->tstamp_running = ctx->time;
494 counter->tstamp_stopped = ctx->time;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100495}
496
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100497/*
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100498 * Cross CPU call to install and enable a performance counter
Thomas Gleixner0793a612008-12-04 20:12:29 +0100499 */
500static void __perf_install_in_context(void *info)
501{
502 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
503 struct perf_counter *counter = info;
504 struct perf_counter_context *ctx = counter->ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100505 struct perf_counter *leader = counter->group_leader;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100506 int cpu = smp_processor_id();
Ingo Molnar9b51f662008-12-12 13:49:45 +0100507 unsigned long flags;
Ingo Molnar5c92d122008-12-11 13:21:10 +0100508 u64 perf_flags;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100509 int err;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100510
511 /*
512 * If this is a task context, we need to check whether it is
513 * the current task context of this cpu. If not it has been
514 * scheduled out before the smp call arrived.
515 */
516 if (ctx->task && cpuctx->task_ctx != ctx)
517 return;
518
Peter Zijlstra849691a2009-04-06 11:45:12 +0200519 spin_lock_irqsave(&ctx->lock, flags);
Peter Zijlstra4af49982009-04-06 11:45:10 +0200520 update_context_time(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100521
522 /*
523 * Protect the list operation against NMI by disabling the
524 * counters on a global level. NOP for non NMI based counters.
525 */
Ingo Molnar01b28382008-12-11 13:45:51 +0100526 perf_flags = hw_perf_save_disable();
Thomas Gleixner0793a612008-12-04 20:12:29 +0100527
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100528 add_counter_to_ctx(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100529
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100530 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100531 * Don't put the counter on if it is disabled or if
532 * it is in a group and the group isn't on.
533 */
534 if (counter->state != PERF_COUNTER_STATE_INACTIVE ||
535 (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE))
536 goto unlock;
537
538 /*
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100539 * An exclusive counter can't go on if there are already active
540 * hardware counters, and no hardware counter can go on if there
541 * is already an exclusive counter on.
542 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100543 if (!group_can_go_on(counter, cpuctx, 1))
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100544 err = -EEXIST;
545 else
546 err = counter_sched_in(counter, cpuctx, ctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100547
Paul Mackerrasd859e292009-01-17 18:10:22 +1100548 if (err) {
549 /*
550 * This counter couldn't go on. If it is in a group
551 * then we have to pull the whole group off.
552 * If the counter group is pinned then put it in error state.
553 */
554 if (leader != counter)
555 group_sched_out(leader, cpuctx, ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100556 if (leader->hw_event.pinned) {
557 update_group_times(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100558 leader->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100559 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100560 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100561
562 if (!err && !ctx->task && cpuctx->max_pertask)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100563 cpuctx->max_pertask--;
564
Paul Mackerrasd859e292009-01-17 18:10:22 +1100565 unlock:
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100566 hw_perf_restore(perf_flags);
567
Peter Zijlstra849691a2009-04-06 11:45:12 +0200568 spin_unlock_irqrestore(&ctx->lock, flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100569}
570
571/*
572 * Attach a performance counter to a context
573 *
574 * First we add the counter to the list with the hardware enable bit
575 * in counter->hw_config cleared.
576 *
577 * If the counter is attached to a task which is on a CPU we use a smp
578 * call to enable it in the task context. The task might have been
579 * scheduled away, but we check this in the smp call again.
Paul Mackerrasd859e292009-01-17 18:10:22 +1100580 *
581 * Must be called with ctx->mutex held.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100582 */
583static void
584perf_install_in_context(struct perf_counter_context *ctx,
585 struct perf_counter *counter,
586 int cpu)
587{
588 struct task_struct *task = ctx->task;
589
Thomas Gleixner0793a612008-12-04 20:12:29 +0100590 if (!task) {
591 /*
592 * Per cpu counters are installed via an smp call and
593 * the install is always sucessful.
594 */
595 smp_call_function_single(cpu, __perf_install_in_context,
596 counter, 1);
597 return;
598 }
599
600 counter->task = task;
601retry:
602 task_oncpu_function_call(task, __perf_install_in_context,
603 counter);
604
605 spin_lock_irq(&ctx->lock);
606 /*
Thomas Gleixner0793a612008-12-04 20:12:29 +0100607 * we need to retry the smp call.
608 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100609 if (ctx->is_active && list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100610 spin_unlock_irq(&ctx->lock);
611 goto retry;
612 }
613
614 /*
615 * The lock prevents that this context is scheduled in so we
616 * can add the counter safely, if it the call above did not
617 * succeed.
618 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100619 if (list_empty(&counter->list_entry))
620 add_counter_to_ctx(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100621 spin_unlock_irq(&ctx->lock);
622}
623
Paul Mackerrasd859e292009-01-17 18:10:22 +1100624/*
625 * Cross CPU call to enable a performance counter
626 */
627static void __perf_counter_enable(void *info)
Ingo Molnar04289bb2008-12-11 08:38:42 +0100628{
Paul Mackerrasd859e292009-01-17 18:10:22 +1100629 struct perf_counter *counter = info;
630 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
631 struct perf_counter_context *ctx = counter->ctx;
632 struct perf_counter *leader = counter->group_leader;
633 unsigned long flags;
634 int err;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100635
636 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100637 * If this is a per-task counter, need to check whether this
638 * counter's task is the current task on this cpu.
Ingo Molnar04289bb2008-12-11 08:38:42 +0100639 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100640 if (ctx->task && cpuctx->task_ctx != ctx)
641 return;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100642
Peter Zijlstra849691a2009-04-06 11:45:12 +0200643 spin_lock_irqsave(&ctx->lock, flags);
Peter Zijlstra4af49982009-04-06 11:45:10 +0200644 update_context_time(ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100645
Paul Mackerrasc07c99b2009-02-13 22:10:34 +1100646 counter->prev_state = counter->state;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100647 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
648 goto unlock;
649 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200650 counter->tstamp_enabled = ctx->time - counter->total_time_enabled;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100651
652 /*
653 * If the counter is in a group and isn't the group leader,
654 * then don't put it on unless the group is on.
655 */
656 if (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE)
657 goto unlock;
658
659 if (!group_can_go_on(counter, cpuctx, 1))
660 err = -EEXIST;
661 else
662 err = counter_sched_in(counter, cpuctx, ctx,
663 smp_processor_id());
664
665 if (err) {
666 /*
667 * If this counter can't go on and it's part of a
668 * group, then the whole group has to come off.
669 */
670 if (leader != counter)
671 group_sched_out(leader, cpuctx, ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100672 if (leader->hw_event.pinned) {
673 update_group_times(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100674 leader->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100675 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100676 }
677
678 unlock:
Peter Zijlstra849691a2009-04-06 11:45:12 +0200679 spin_unlock_irqrestore(&ctx->lock, flags);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100680}
681
682/*
683 * Enable a counter.
684 */
685static void perf_counter_enable(struct perf_counter *counter)
686{
687 struct perf_counter_context *ctx = counter->ctx;
688 struct task_struct *task = ctx->task;
689
690 if (!task) {
691 /*
692 * Enable the counter on the cpu that it's on
693 */
694 smp_call_function_single(counter->cpu, __perf_counter_enable,
695 counter, 1);
696 return;
697 }
698
699 spin_lock_irq(&ctx->lock);
700 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
701 goto out;
702
703 /*
704 * If the counter is in error state, clear that first.
705 * That way, if we see the counter in error state below, we
706 * know that it has gone back into error state, as distinct
707 * from the task having been scheduled away before the
708 * cross-call arrived.
709 */
710 if (counter->state == PERF_COUNTER_STATE_ERROR)
711 counter->state = PERF_COUNTER_STATE_OFF;
712
713 retry:
714 spin_unlock_irq(&ctx->lock);
715 task_oncpu_function_call(task, __perf_counter_enable, counter);
716
717 spin_lock_irq(&ctx->lock);
718
719 /*
720 * If the context is active and the counter is still off,
721 * we need to retry the cross-call.
722 */
723 if (ctx->is_active && counter->state == PERF_COUNTER_STATE_OFF)
724 goto retry;
725
726 /*
727 * Since we have the lock this context can't be scheduled
728 * in, so we can change the state safely.
729 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100730 if (counter->state == PERF_COUNTER_STATE_OFF) {
Paul Mackerrasd859e292009-01-17 18:10:22 +1100731 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200732 counter->tstamp_enabled =
733 ctx->time - counter->total_time_enabled;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100734 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100735 out:
736 spin_unlock_irq(&ctx->lock);
737}
738
Peter Zijlstra79f14642009-04-06 11:45:07 +0200739static void perf_counter_refresh(struct perf_counter *counter, int refresh)
740{
741 atomic_add(refresh, &counter->event_limit);
742 perf_counter_enable(counter);
743}
744
Paul Mackerrasd859e292009-01-17 18:10:22 +1100745/*
746 * Enable a counter and all its children.
747 */
748static void perf_counter_enable_family(struct perf_counter *counter)
749{
750 struct perf_counter *child;
751
752 perf_counter_enable(counter);
753
754 /*
755 * Lock the mutex to protect the list of children
756 */
757 mutex_lock(&counter->mutex);
758 list_for_each_entry(child, &counter->child_list, child_list)
759 perf_counter_enable(child);
760 mutex_unlock(&counter->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100761}
762
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100763void __perf_counter_sched_out(struct perf_counter_context *ctx,
764 struct perf_cpu_context *cpuctx)
765{
766 struct perf_counter *counter;
Paul Mackerras3cbed422009-01-09 16:43:42 +1100767 u64 flags;
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100768
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100769 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100770 ctx->is_active = 0;
771 if (likely(!ctx->nr_counters))
772 goto out;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200773 update_context_time(ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100774
Paul Mackerras3cbed422009-01-09 16:43:42 +1100775 flags = hw_perf_save_disable();
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100776 if (ctx->nr_active) {
777 list_for_each_entry(counter, &ctx->counter_list, list_entry)
778 group_sched_out(counter, cpuctx, ctx);
779 }
Paul Mackerras3cbed422009-01-09 16:43:42 +1100780 hw_perf_restore(flags);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100781 out:
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100782 spin_unlock(&ctx->lock);
783}
784
Thomas Gleixner0793a612008-12-04 20:12:29 +0100785/*
786 * Called from scheduler to remove the counters of the current task,
787 * with interrupts disabled.
788 *
789 * We stop each counter and update the counter value in counter->count.
790 *
Ingo Molnar76715812008-12-17 14:20:28 +0100791 * This does not protect us against NMI, but disable()
Thomas Gleixner0793a612008-12-04 20:12:29 +0100792 * sets the disabled bit in the control field of counter _before_
793 * accessing the counter control register. If a NMI hits, then it will
794 * not restart the counter.
795 */
796void perf_counter_task_sched_out(struct task_struct *task, int cpu)
797{
798 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
799 struct perf_counter_context *ctx = &task->perf_counter_ctx;
Peter Zijlstra4a0deca2009-03-19 20:26:12 +0100800 struct pt_regs *regs;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100801
802 if (likely(!cpuctx->task_ctx))
803 return;
804
Peter Zijlstrabce379b2009-04-06 11:45:13 +0200805 update_context_time(ctx);
806
Peter Zijlstra4a0deca2009-03-19 20:26:12 +0100807 regs = task_pt_regs(task);
Peter Zijlstra78f13e92009-04-08 15:01:33 +0200808 perf_swcounter_event(PERF_COUNT_CONTEXT_SWITCHES, 1, 1, regs, 0);
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100809 __perf_counter_sched_out(ctx, cpuctx);
810
Thomas Gleixner0793a612008-12-04 20:12:29 +0100811 cpuctx->task_ctx = NULL;
812}
813
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100814static void perf_counter_cpu_sched_out(struct perf_cpu_context *cpuctx)
Ingo Molnar04289bb2008-12-11 08:38:42 +0100815{
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100816 __perf_counter_sched_out(&cpuctx->ctx, cpuctx);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100817}
818
Ingo Molnar79958882008-12-17 08:54:56 +0100819static int
Ingo Molnar04289bb2008-12-11 08:38:42 +0100820group_sched_in(struct perf_counter *group_counter,
821 struct perf_cpu_context *cpuctx,
822 struct perf_counter_context *ctx,
823 int cpu)
824{
Ingo Molnar95cdd2e2008-12-21 13:50:42 +0100825 struct perf_counter *counter, *partial_group;
Paul Mackerras3cbed422009-01-09 16:43:42 +1100826 int ret;
827
828 if (group_counter->state == PERF_COUNTER_STATE_OFF)
829 return 0;
830
831 ret = hw_perf_group_sched_in(group_counter, cpuctx, ctx, cpu);
832 if (ret)
833 return ret < 0 ? ret : 0;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100834
Paul Mackerrasc07c99b2009-02-13 22:10:34 +1100835 group_counter->prev_state = group_counter->state;
Ingo Molnar95cdd2e2008-12-21 13:50:42 +0100836 if (counter_sched_in(group_counter, cpuctx, ctx, cpu))
837 return -EAGAIN;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100838
839 /*
840 * Schedule in siblings as one group (if any):
841 */
Ingo Molnar79958882008-12-17 08:54:56 +0100842 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
Paul Mackerrasc07c99b2009-02-13 22:10:34 +1100843 counter->prev_state = counter->state;
Ingo Molnar95cdd2e2008-12-21 13:50:42 +0100844 if (counter_sched_in(counter, cpuctx, ctx, cpu)) {
845 partial_group = counter;
846 goto group_error;
847 }
Ingo Molnar79958882008-12-17 08:54:56 +0100848 }
849
Paul Mackerras3cbed422009-01-09 16:43:42 +1100850 return 0;
Ingo Molnar95cdd2e2008-12-21 13:50:42 +0100851
852group_error:
853 /*
854 * Groups can be scheduled in as one unit only, so undo any
855 * partial group before returning:
856 */
857 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
858 if (counter == partial_group)
859 break;
860 counter_sched_out(counter, cpuctx, ctx);
861 }
862 counter_sched_out(group_counter, cpuctx, ctx);
863
864 return -EAGAIN;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100865}
866
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100867static void
868__perf_counter_sched_in(struct perf_counter_context *ctx,
869 struct perf_cpu_context *cpuctx, int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100870{
Thomas Gleixner0793a612008-12-04 20:12:29 +0100871 struct perf_counter *counter;
Paul Mackerras3cbed422009-01-09 16:43:42 +1100872 u64 flags;
Paul Mackerrasdd0e6ba2009-01-12 15:11:00 +1100873 int can_add_hw = 1;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100874
Thomas Gleixner0793a612008-12-04 20:12:29 +0100875 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100876 ctx->is_active = 1;
877 if (likely(!ctx->nr_counters))
878 goto out;
879
Peter Zijlstra4af49982009-04-06 11:45:10 +0200880 ctx->timestamp = perf_clock();
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100881
Paul Mackerras3cbed422009-01-09 16:43:42 +1100882 flags = hw_perf_save_disable();
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100883
884 /*
885 * First go through the list and put on any pinned groups
886 * in order to give them the best chance of going on.
887 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100888 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100889 if (counter->state <= PERF_COUNTER_STATE_OFF ||
890 !counter->hw_event.pinned)
891 continue;
892 if (counter->cpu != -1 && counter->cpu != cpu)
893 continue;
894
895 if (group_can_go_on(counter, cpuctx, 1))
896 group_sched_in(counter, cpuctx, ctx, cpu);
897
898 /*
899 * If this pinned group hasn't been scheduled,
900 * put it in error state.
901 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100902 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
903 update_group_times(counter);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100904 counter->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100905 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100906 }
907
908 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
909 /*
910 * Ignore counters in OFF or ERROR state, and
911 * ignore pinned counters since we did them already.
912 */
913 if (counter->state <= PERF_COUNTER_STATE_OFF ||
914 counter->hw_event.pinned)
915 continue;
916
Ingo Molnar04289bb2008-12-11 08:38:42 +0100917 /*
918 * Listen to the 'cpu' scheduling filter constraint
919 * of counters:
920 */
Thomas Gleixner0793a612008-12-04 20:12:29 +0100921 if (counter->cpu != -1 && counter->cpu != cpu)
922 continue;
923
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100924 if (group_can_go_on(counter, cpuctx, can_add_hw)) {
Paul Mackerrasdd0e6ba2009-01-12 15:11:00 +1100925 if (group_sched_in(counter, cpuctx, ctx, cpu))
926 can_add_hw = 0;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100927 }
Thomas Gleixner0793a612008-12-04 20:12:29 +0100928 }
Paul Mackerras3cbed422009-01-09 16:43:42 +1100929 hw_perf_restore(flags);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100930 out:
Thomas Gleixner0793a612008-12-04 20:12:29 +0100931 spin_unlock(&ctx->lock);
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100932}
Ingo Molnar04289bb2008-12-11 08:38:42 +0100933
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100934/*
935 * Called from scheduler to add the counters of the current task
936 * with interrupts disabled.
937 *
938 * We restore the counter value and then enable it.
939 *
940 * This does not protect us against NMI, but enable()
941 * sets the enabled bit in the control field of counter _before_
942 * accessing the counter control register. If a NMI hits, then it will
943 * keep the counter running.
944 */
945void perf_counter_task_sched_in(struct task_struct *task, int cpu)
946{
947 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
948 struct perf_counter_context *ctx = &task->perf_counter_ctx;
949
950 __perf_counter_sched_in(ctx, cpuctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100951 cpuctx->task_ctx = ctx;
952}
953
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100954static void perf_counter_cpu_sched_in(struct perf_cpu_context *cpuctx, int cpu)
955{
956 struct perf_counter_context *ctx = &cpuctx->ctx;
957
958 __perf_counter_sched_in(ctx, cpuctx, cpu);
959}
960
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100961int perf_counter_task_disable(void)
962{
963 struct task_struct *curr = current;
964 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
965 struct perf_counter *counter;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100966 unsigned long flags;
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100967 u64 perf_flags;
968 int cpu;
969
970 if (likely(!ctx->nr_counters))
971 return 0;
972
Peter Zijlstra849691a2009-04-06 11:45:12 +0200973 local_irq_save(flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100974 cpu = smp_processor_id();
975
976 perf_counter_task_sched_out(curr, cpu);
977
978 spin_lock(&ctx->lock);
979
980 /*
981 * Disable all the counters:
982 */
983 perf_flags = hw_perf_save_disable();
984
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100985 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100986 if (counter->state != PERF_COUNTER_STATE_ERROR) {
987 update_group_times(counter);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100988 counter->state = PERF_COUNTER_STATE_OFF;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100989 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100990 }
Ingo Molnar9b51f662008-12-12 13:49:45 +0100991
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100992 hw_perf_restore(perf_flags);
993
Peter Zijlstra849691a2009-04-06 11:45:12 +0200994 spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100995
996 return 0;
997}
998
999int perf_counter_task_enable(void)
1000{
1001 struct task_struct *curr = current;
1002 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
1003 struct perf_counter *counter;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001004 unsigned long flags;
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001005 u64 perf_flags;
1006 int cpu;
1007
1008 if (likely(!ctx->nr_counters))
1009 return 0;
1010
Peter Zijlstra849691a2009-04-06 11:45:12 +02001011 local_irq_save(flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001012 cpu = smp_processor_id();
1013
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001014 perf_counter_task_sched_out(curr, cpu);
1015
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001016 spin_lock(&ctx->lock);
1017
1018 /*
1019 * Disable all the counters:
1020 */
1021 perf_flags = hw_perf_save_disable();
1022
1023 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001024 if (counter->state > PERF_COUNTER_STATE_OFF)
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001025 continue;
Ingo Molnar6a930702008-12-11 15:17:03 +01001026 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +02001027 counter->tstamp_enabled =
1028 ctx->time - counter->total_time_enabled;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001029 counter->hw_event.disabled = 0;
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001030 }
1031 hw_perf_restore(perf_flags);
1032
1033 spin_unlock(&ctx->lock);
1034
1035 perf_counter_task_sched_in(curr, cpu);
1036
Peter Zijlstra849691a2009-04-06 11:45:12 +02001037 local_irq_restore(flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001038
1039 return 0;
1040}
1041
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001042/*
1043 * Round-robin a context's counters:
1044 */
1045static void rotate_ctx(struct perf_counter_context *ctx)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001046{
Thomas Gleixner0793a612008-12-04 20:12:29 +01001047 struct perf_counter *counter;
Ingo Molnar5c92d122008-12-11 13:21:10 +01001048 u64 perf_flags;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001049
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001050 if (!ctx->nr_counters)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001051 return;
1052
Thomas Gleixner0793a612008-12-04 20:12:29 +01001053 spin_lock(&ctx->lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001054 /*
Ingo Molnar04289bb2008-12-11 08:38:42 +01001055 * Rotate the first entry last (works just fine for group counters too):
Thomas Gleixner0793a612008-12-04 20:12:29 +01001056 */
Ingo Molnar01b28382008-12-11 13:45:51 +01001057 perf_flags = hw_perf_save_disable();
Ingo Molnar04289bb2008-12-11 08:38:42 +01001058 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Peter Zijlstra75564232009-03-13 12:21:29 +01001059 list_move_tail(&counter->list_entry, &ctx->counter_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001060 break;
1061 }
Ingo Molnar01b28382008-12-11 13:45:51 +01001062 hw_perf_restore(perf_flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001063
1064 spin_unlock(&ctx->lock);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001065}
Thomas Gleixner0793a612008-12-04 20:12:29 +01001066
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001067void perf_counter_task_tick(struct task_struct *curr, int cpu)
1068{
1069 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
1070 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
1071 const int rotate_percpu = 0;
1072
1073 if (rotate_percpu)
1074 perf_counter_cpu_sched_out(cpuctx);
1075 perf_counter_task_sched_out(curr, cpu);
1076
1077 if (rotate_percpu)
1078 rotate_ctx(&cpuctx->ctx);
1079 rotate_ctx(ctx);
1080
1081 if (rotate_percpu)
1082 perf_counter_cpu_sched_in(cpuctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001083 perf_counter_task_sched_in(curr, cpu);
1084}
1085
1086/*
Thomas Gleixner0793a612008-12-04 20:12:29 +01001087 * Cross CPU call to read the hardware counter
1088 */
Ingo Molnar76715812008-12-17 14:20:28 +01001089static void __read(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001090{
Ingo Molnar621a01e2008-12-11 12:46:46 +01001091 struct perf_counter *counter = info;
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001092 struct perf_counter_context *ctx = counter->ctx;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001093 unsigned long flags;
Ingo Molnar621a01e2008-12-11 12:46:46 +01001094
Peter Zijlstra849691a2009-04-06 11:45:12 +02001095 local_irq_save(flags);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001096 if (ctx->is_active)
Peter Zijlstra4af49982009-04-06 11:45:10 +02001097 update_context_time(ctx);
Robert Richter4aeb0b42009-04-29 12:47:03 +02001098 counter->pmu->read(counter);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001099 update_counter_times(counter);
Peter Zijlstra849691a2009-04-06 11:45:12 +02001100 local_irq_restore(flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001101}
1102
Ingo Molnar04289bb2008-12-11 08:38:42 +01001103static u64 perf_counter_read(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001104{
1105 /*
1106 * If counter is enabled and currently active on a CPU, update the
1107 * value in the counter structure:
1108 */
Ingo Molnar6a930702008-12-11 15:17:03 +01001109 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
Thomas Gleixner0793a612008-12-04 20:12:29 +01001110 smp_call_function_single(counter->oncpu,
Ingo Molnar76715812008-12-17 14:20:28 +01001111 __read, counter, 1);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001112 } else if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
1113 update_counter_times(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001114 }
1115
Ingo Molnaree060942008-12-13 09:00:03 +01001116 return atomic64_read(&counter->count);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001117}
1118
Thomas Gleixner0793a612008-12-04 20:12:29 +01001119static void put_context(struct perf_counter_context *ctx)
1120{
1121 if (ctx->task)
1122 put_task_struct(ctx->task);
1123}
1124
1125static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
1126{
1127 struct perf_cpu_context *cpuctx;
1128 struct perf_counter_context *ctx;
1129 struct task_struct *task;
1130
1131 /*
1132 * If cpu is not a wildcard then this is a percpu counter:
1133 */
1134 if (cpu != -1) {
1135 /* Must be root to operate on a CPU counter: */
Peter Zijlstra1ccd1542009-04-09 10:53:45 +02001136 if (sysctl_perf_counter_priv && !capable(CAP_SYS_ADMIN))
Thomas Gleixner0793a612008-12-04 20:12:29 +01001137 return ERR_PTR(-EACCES);
1138
1139 if (cpu < 0 || cpu > num_possible_cpus())
1140 return ERR_PTR(-EINVAL);
1141
1142 /*
1143 * We could be clever and allow to attach a counter to an
1144 * offline CPU and activate it when the CPU comes up, but
1145 * that's for later.
1146 */
1147 if (!cpu_isset(cpu, cpu_online_map))
1148 return ERR_PTR(-ENODEV);
1149
1150 cpuctx = &per_cpu(perf_cpu_context, cpu);
1151 ctx = &cpuctx->ctx;
1152
Thomas Gleixner0793a612008-12-04 20:12:29 +01001153 return ctx;
1154 }
1155
1156 rcu_read_lock();
1157 if (!pid)
1158 task = current;
1159 else
1160 task = find_task_by_vpid(pid);
1161 if (task)
1162 get_task_struct(task);
1163 rcu_read_unlock();
1164
1165 if (!task)
1166 return ERR_PTR(-ESRCH);
1167
1168 ctx = &task->perf_counter_ctx;
1169 ctx->task = task;
1170
1171 /* Reuse ptrace permission checks for now. */
1172 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
1173 put_context(ctx);
1174 return ERR_PTR(-EACCES);
1175 }
1176
1177 return ctx;
1178}
1179
Peter Zijlstra592903c2009-03-13 12:21:36 +01001180static void free_counter_rcu(struct rcu_head *head)
1181{
1182 struct perf_counter *counter;
1183
1184 counter = container_of(head, struct perf_counter, rcu_head);
1185 kfree(counter);
1186}
1187
Peter Zijlstra925d5192009-03-30 19:07:02 +02001188static void perf_pending_sync(struct perf_counter *counter);
1189
Peter Zijlstraf1600952009-03-19 20:26:16 +01001190static void free_counter(struct perf_counter *counter)
1191{
Peter Zijlstra925d5192009-03-30 19:07:02 +02001192 perf_pending_sync(counter);
1193
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02001194 if (counter->hw_event.mmap)
1195 atomic_dec(&nr_mmap_tracking);
1196 if (counter->hw_event.munmap)
1197 atomic_dec(&nr_munmap_tracking);
1198 if (counter->hw_event.comm)
1199 atomic_dec(&nr_comm_tracking);
1200
Peter Zijlstrae077df42009-03-19 20:26:17 +01001201 if (counter->destroy)
1202 counter->destroy(counter);
1203
Peter Zijlstraf1600952009-03-19 20:26:16 +01001204 call_rcu(&counter->rcu_head, free_counter_rcu);
1205}
1206
Thomas Gleixner0793a612008-12-04 20:12:29 +01001207/*
1208 * Called when the last reference to the file is gone.
1209 */
1210static int perf_release(struct inode *inode, struct file *file)
1211{
1212 struct perf_counter *counter = file->private_data;
1213 struct perf_counter_context *ctx = counter->ctx;
1214
1215 file->private_data = NULL;
1216
Paul Mackerrasd859e292009-01-17 18:10:22 +11001217 mutex_lock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001218 mutex_lock(&counter->mutex);
1219
Ingo Molnar04289bb2008-12-11 08:38:42 +01001220 perf_counter_remove_from_context(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001221
1222 mutex_unlock(&counter->mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001223 mutex_unlock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001224
Peter Zijlstraf1600952009-03-19 20:26:16 +01001225 free_counter(counter);
Mike Galbraith5af75912009-02-11 10:53:37 +01001226 put_context(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001227
1228 return 0;
1229}
1230
1231/*
1232 * Read the performance counter - simple non blocking version for now
1233 */
1234static ssize_t
1235perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
1236{
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001237 u64 values[3];
1238 int n;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001239
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001240 /*
1241 * Return end-of-file for a read on a counter that is in
1242 * error state (i.e. because it was pinned but it couldn't be
1243 * scheduled on to the CPU at some point).
1244 */
1245 if (counter->state == PERF_COUNTER_STATE_ERROR)
1246 return 0;
1247
Thomas Gleixner0793a612008-12-04 20:12:29 +01001248 mutex_lock(&counter->mutex);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001249 values[0] = perf_counter_read(counter);
1250 n = 1;
1251 if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1252 values[n++] = counter->total_time_enabled +
1253 atomic64_read(&counter->child_total_time_enabled);
1254 if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1255 values[n++] = counter->total_time_running +
1256 atomic64_read(&counter->child_total_time_running);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001257 mutex_unlock(&counter->mutex);
1258
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001259 if (count < n * sizeof(u64))
1260 return -EINVAL;
1261 count = n * sizeof(u64);
1262
1263 if (copy_to_user(buf, values, count))
1264 return -EFAULT;
1265
1266 return count;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001267}
1268
1269static ssize_t
Thomas Gleixner0793a612008-12-04 20:12:29 +01001270perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
1271{
1272 struct perf_counter *counter = file->private_data;
1273
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001274 return perf_read_hw(counter, buf, count);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001275}
1276
1277static unsigned int perf_poll(struct file *file, poll_table *wait)
1278{
1279 struct perf_counter *counter = file->private_data;
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001280 struct perf_mmap_data *data;
1281 unsigned int events;
1282
1283 rcu_read_lock();
1284 data = rcu_dereference(counter->data);
1285 if (data)
1286 events = atomic_xchg(&data->wakeup, 0);
1287 else
1288 events = POLL_HUP;
1289 rcu_read_unlock();
Thomas Gleixner0793a612008-12-04 20:12:29 +01001290
1291 poll_wait(file, &counter->waitq, wait);
1292
Thomas Gleixner0793a612008-12-04 20:12:29 +01001293 return events;
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;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001311 default:
1312 err = -ENOTTY;
1313 }
1314 return err;
1315}
1316
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001317/*
1318 * Callers need to ensure there can be no nesting of this function, otherwise
1319 * the seqlock logic goes bad. We can not serialize this because the arch
1320 * code calls this from NMI context.
1321 */
1322void perf_counter_update_userpage(struct perf_counter *counter)
Paul Mackerras37d81822009-03-23 18:22:08 +01001323{
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001324 struct perf_mmap_data *data;
1325 struct perf_counter_mmap_page *userpg;
1326
1327 rcu_read_lock();
1328 data = rcu_dereference(counter->data);
1329 if (!data)
1330 goto unlock;
1331
1332 userpg = data->user_page;
Paul Mackerras37d81822009-03-23 18:22:08 +01001333
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001334 /*
1335 * Disable preemption so as to not let the corresponding user-space
1336 * spin too long if we get preempted.
1337 */
1338 preempt_disable();
Paul Mackerras37d81822009-03-23 18:22:08 +01001339 ++userpg->lock;
Peter Zijlstra92f22a32009-04-02 11:12:04 +02001340 barrier();
Paul Mackerras37d81822009-03-23 18:22:08 +01001341 userpg->index = counter->hw.idx;
1342 userpg->offset = atomic64_read(&counter->count);
1343 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
1344 userpg->offset -= atomic64_read(&counter->hw.prev_count);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001345
Peter Zijlstra92f22a32009-04-02 11:12:04 +02001346 barrier();
Paul Mackerras37d81822009-03-23 18:22:08 +01001347 ++userpg->lock;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001348 preempt_enable();
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001349unlock:
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001350 rcu_read_unlock();
Paul Mackerras37d81822009-03-23 18:22:08 +01001351}
1352
1353static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1354{
1355 struct perf_counter *counter = vma->vm_file->private_data;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001356 struct perf_mmap_data *data;
1357 int ret = VM_FAULT_SIGBUS;
Paul Mackerras37d81822009-03-23 18:22:08 +01001358
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001359 rcu_read_lock();
1360 data = rcu_dereference(counter->data);
1361 if (!data)
1362 goto unlock;
Paul Mackerras37d81822009-03-23 18:22:08 +01001363
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001364 if (vmf->pgoff == 0) {
1365 vmf->page = virt_to_page(data->user_page);
1366 } else {
1367 int nr = vmf->pgoff - 1;
1368
1369 if ((unsigned)nr > data->nr_pages)
1370 goto unlock;
1371
1372 vmf->page = virt_to_page(data->data_pages[nr]);
1373 }
Paul Mackerras37d81822009-03-23 18:22:08 +01001374 get_page(vmf->page);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001375 ret = 0;
1376unlock:
1377 rcu_read_unlock();
1378
1379 return ret;
1380}
1381
1382static int perf_mmap_data_alloc(struct perf_counter *counter, int nr_pages)
1383{
1384 struct perf_mmap_data *data;
1385 unsigned long size;
1386 int i;
1387
1388 WARN_ON(atomic_read(&counter->mmap_count));
1389
1390 size = sizeof(struct perf_mmap_data);
1391 size += nr_pages * sizeof(void *);
1392
1393 data = kzalloc(size, GFP_KERNEL);
1394 if (!data)
1395 goto fail;
1396
1397 data->user_page = (void *)get_zeroed_page(GFP_KERNEL);
1398 if (!data->user_page)
1399 goto fail_user_page;
1400
1401 for (i = 0; i < nr_pages; i++) {
1402 data->data_pages[i] = (void *)get_zeroed_page(GFP_KERNEL);
1403 if (!data->data_pages[i])
1404 goto fail_data_pages;
1405 }
1406
1407 data->nr_pages = nr_pages;
1408
1409 rcu_assign_pointer(counter->data, data);
1410
Paul Mackerras37d81822009-03-23 18:22:08 +01001411 return 0;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001412
1413fail_data_pages:
1414 for (i--; i >= 0; i--)
1415 free_page((unsigned long)data->data_pages[i]);
1416
1417 free_page((unsigned long)data->user_page);
1418
1419fail_user_page:
1420 kfree(data);
1421
1422fail:
1423 return -ENOMEM;
1424}
1425
1426static void __perf_mmap_data_free(struct rcu_head *rcu_head)
1427{
1428 struct perf_mmap_data *data = container_of(rcu_head,
1429 struct perf_mmap_data, rcu_head);
1430 int i;
1431
1432 free_page((unsigned long)data->user_page);
1433 for (i = 0; i < data->nr_pages; i++)
1434 free_page((unsigned long)data->data_pages[i]);
1435 kfree(data);
1436}
1437
1438static void perf_mmap_data_free(struct perf_counter *counter)
1439{
1440 struct perf_mmap_data *data = counter->data;
1441
1442 WARN_ON(atomic_read(&counter->mmap_count));
1443
1444 rcu_assign_pointer(counter->data, NULL);
1445 call_rcu(&data->rcu_head, __perf_mmap_data_free);
1446}
1447
1448static void perf_mmap_open(struct vm_area_struct *vma)
1449{
1450 struct perf_counter *counter = vma->vm_file->private_data;
1451
1452 atomic_inc(&counter->mmap_count);
1453}
1454
1455static void perf_mmap_close(struct vm_area_struct *vma)
1456{
1457 struct perf_counter *counter = vma->vm_file->private_data;
1458
1459 if (atomic_dec_and_mutex_lock(&counter->mmap_count,
1460 &counter->mmap_mutex)) {
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001461 vma->vm_mm->locked_vm -= counter->data->nr_pages + 1;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001462 perf_mmap_data_free(counter);
1463 mutex_unlock(&counter->mmap_mutex);
1464 }
Paul Mackerras37d81822009-03-23 18:22:08 +01001465}
1466
1467static struct vm_operations_struct perf_mmap_vmops = {
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001468 .open = perf_mmap_open,
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001469 .close = perf_mmap_close,
Paul Mackerras37d81822009-03-23 18:22:08 +01001470 .fault = perf_mmap_fault,
1471};
1472
1473static int perf_mmap(struct file *file, struct vm_area_struct *vma)
1474{
1475 struct perf_counter *counter = file->private_data;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001476 unsigned long vma_size;
1477 unsigned long nr_pages;
1478 unsigned long locked, lock_limit;
1479 int ret = 0;
Paul Mackerras37d81822009-03-23 18:22:08 +01001480
1481 if (!(vma->vm_flags & VM_SHARED) || (vma->vm_flags & VM_WRITE))
1482 return -EINVAL;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001483
1484 vma_size = vma->vm_end - vma->vm_start;
1485 nr_pages = (vma_size / PAGE_SIZE) - 1;
1486
Peter Zijlstra7730d862009-03-25 12:48:31 +01001487 /*
1488 * If we have data pages ensure they're a power-of-two number, so we
1489 * can do bitmasks instead of modulo.
1490 */
1491 if (nr_pages != 0 && !is_power_of_2(nr_pages))
Paul Mackerras37d81822009-03-23 18:22:08 +01001492 return -EINVAL;
1493
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001494 if (vma_size != PAGE_SIZE * (1 + nr_pages))
Paul Mackerras37d81822009-03-23 18:22:08 +01001495 return -EINVAL;
1496
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001497 if (vma->vm_pgoff != 0)
1498 return -EINVAL;
Paul Mackerras37d81822009-03-23 18:22:08 +01001499
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001500 mutex_lock(&counter->mmap_mutex);
1501 if (atomic_inc_not_zero(&counter->mmap_count)) {
1502 if (nr_pages != counter->data->nr_pages)
1503 ret = -EINVAL;
1504 goto unlock;
1505 }
1506
1507 locked = vma->vm_mm->locked_vm;
1508 locked += nr_pages + 1;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001509
1510 lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
1511 lock_limit >>= PAGE_SHIFT;
1512
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001513 if ((locked > lock_limit) && !capable(CAP_IPC_LOCK)) {
1514 ret = -EPERM;
1515 goto unlock;
1516 }
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001517
1518 WARN_ON(counter->data);
1519 ret = perf_mmap_data_alloc(counter, nr_pages);
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001520 if (ret)
1521 goto unlock;
1522
1523 atomic_set(&counter->mmap_count, 1);
1524 vma->vm_mm->locked_vm += nr_pages + 1;
1525unlock:
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001526 mutex_unlock(&counter->mmap_mutex);
Paul Mackerras37d81822009-03-23 18:22:08 +01001527
1528 vma->vm_flags &= ~VM_MAYWRITE;
1529 vma->vm_flags |= VM_RESERVED;
1530 vma->vm_ops = &perf_mmap_vmops;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001531
1532 return ret;
Paul Mackerras37d81822009-03-23 18:22:08 +01001533}
1534
Peter Zijlstra3c446b32009-04-06 11:45:01 +02001535static int perf_fasync(int fd, struct file *filp, int on)
1536{
1537 struct perf_counter *counter = filp->private_data;
1538 struct inode *inode = filp->f_path.dentry->d_inode;
1539 int retval;
1540
1541 mutex_lock(&inode->i_mutex);
1542 retval = fasync_helper(fd, filp, on, &counter->fasync);
1543 mutex_unlock(&inode->i_mutex);
1544
1545 if (retval < 0)
1546 return retval;
1547
1548 return 0;
1549}
1550
Thomas Gleixner0793a612008-12-04 20:12:29 +01001551static const struct file_operations perf_fops = {
1552 .release = perf_release,
1553 .read = perf_read,
1554 .poll = perf_poll,
Paul Mackerrasd859e292009-01-17 18:10:22 +11001555 .unlocked_ioctl = perf_ioctl,
1556 .compat_ioctl = perf_ioctl,
Paul Mackerras37d81822009-03-23 18:22:08 +01001557 .mmap = perf_mmap,
Peter Zijlstra3c446b32009-04-06 11:45:01 +02001558 .fasync = perf_fasync,
Thomas Gleixner0793a612008-12-04 20:12:29 +01001559};
1560
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001561/*
Peter Zijlstra925d5192009-03-30 19:07:02 +02001562 * Perf counter wakeup
1563 *
1564 * If there's data, ensure we set the poll() state and publish everything
1565 * to user-space before waking everybody up.
1566 */
1567
1568void perf_counter_wakeup(struct perf_counter *counter)
1569{
1570 struct perf_mmap_data *data;
1571
1572 rcu_read_lock();
1573 data = rcu_dereference(counter->data);
1574 if (data) {
Peter Zijlstra3c446b32009-04-06 11:45:01 +02001575 atomic_set(&data->wakeup, POLL_IN);
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001576 /*
1577 * Ensure all data writes are issued before updating the
1578 * user-space data head information. The matching rmb()
1579 * will be in userspace after reading this value.
1580 */
1581 smp_wmb();
1582 data->user_page->data_head = atomic_read(&data->head);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001583 }
1584 rcu_read_unlock();
1585
1586 wake_up_all(&counter->waitq);
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02001587
1588 if (counter->pending_kill) {
1589 kill_fasync(&counter->fasync, SIGIO, counter->pending_kill);
1590 counter->pending_kill = 0;
1591 }
Peter Zijlstra925d5192009-03-30 19:07:02 +02001592}
1593
1594/*
1595 * Pending wakeups
1596 *
1597 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
1598 *
1599 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
1600 * single linked list and use cmpxchg() to add entries lockless.
1601 */
1602
Peter Zijlstra79f14642009-04-06 11:45:07 +02001603static void perf_pending_counter(struct perf_pending_entry *entry)
1604{
1605 struct perf_counter *counter = container_of(entry,
1606 struct perf_counter, pending);
1607
1608 if (counter->pending_disable) {
1609 counter->pending_disable = 0;
1610 perf_counter_disable(counter);
1611 }
1612
1613 if (counter->pending_wakeup) {
1614 counter->pending_wakeup = 0;
1615 perf_counter_wakeup(counter);
1616 }
1617}
1618
Peter Zijlstra671dec52009-04-06 11:45:02 +02001619#define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
Peter Zijlstra925d5192009-03-30 19:07:02 +02001620
Peter Zijlstra671dec52009-04-06 11:45:02 +02001621static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = {
Peter Zijlstra925d5192009-03-30 19:07:02 +02001622 PENDING_TAIL,
1623};
1624
Peter Zijlstra671dec52009-04-06 11:45:02 +02001625static void perf_pending_queue(struct perf_pending_entry *entry,
1626 void (*func)(struct perf_pending_entry *))
Peter Zijlstra925d5192009-03-30 19:07:02 +02001627{
Peter Zijlstra671dec52009-04-06 11:45:02 +02001628 struct perf_pending_entry **head;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001629
Peter Zijlstra671dec52009-04-06 11:45:02 +02001630 if (cmpxchg(&entry->next, NULL, PENDING_TAIL) != NULL)
Peter Zijlstra925d5192009-03-30 19:07:02 +02001631 return;
1632
Peter Zijlstra671dec52009-04-06 11:45:02 +02001633 entry->func = func;
1634
1635 head = &get_cpu_var(perf_pending_head);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001636
1637 do {
Peter Zijlstra671dec52009-04-06 11:45:02 +02001638 entry->next = *head;
1639 } while (cmpxchg(head, entry->next, entry) != entry->next);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001640
1641 set_perf_counter_pending();
1642
Peter Zijlstra671dec52009-04-06 11:45:02 +02001643 put_cpu_var(perf_pending_head);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001644}
1645
1646static int __perf_pending_run(void)
1647{
Peter Zijlstra671dec52009-04-06 11:45:02 +02001648 struct perf_pending_entry *list;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001649 int nr = 0;
1650
Peter Zijlstra671dec52009-04-06 11:45:02 +02001651 list = xchg(&__get_cpu_var(perf_pending_head), PENDING_TAIL);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001652 while (list != PENDING_TAIL) {
Peter Zijlstra671dec52009-04-06 11:45:02 +02001653 void (*func)(struct perf_pending_entry *);
1654 struct perf_pending_entry *entry = list;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001655
1656 list = list->next;
1657
Peter Zijlstra671dec52009-04-06 11:45:02 +02001658 func = entry->func;
1659 entry->next = NULL;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001660 /*
1661 * Ensure we observe the unqueue before we issue the wakeup,
1662 * so that we won't be waiting forever.
1663 * -- see perf_not_pending().
1664 */
1665 smp_wmb();
1666
Peter Zijlstra671dec52009-04-06 11:45:02 +02001667 func(entry);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001668 nr++;
1669 }
1670
1671 return nr;
1672}
1673
1674static inline int perf_not_pending(struct perf_counter *counter)
1675{
1676 /*
1677 * If we flush on whatever cpu we run, there is a chance we don't
1678 * need to wait.
1679 */
1680 get_cpu();
1681 __perf_pending_run();
1682 put_cpu();
1683
1684 /*
1685 * Ensure we see the proper queue state before going to sleep
1686 * so that we do not miss the wakeup. -- see perf_pending_handle()
1687 */
1688 smp_rmb();
Peter Zijlstra671dec52009-04-06 11:45:02 +02001689 return counter->pending.next == NULL;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001690}
1691
1692static void perf_pending_sync(struct perf_counter *counter)
1693{
1694 wait_event(counter->waitq, perf_not_pending(counter));
1695}
1696
1697void perf_counter_do_pending(void)
1698{
1699 __perf_pending_run();
1700}
1701
1702/*
Peter Zijlstra394ee072009-03-30 19:07:14 +02001703 * Callchain support -- arch specific
1704 */
1705
Peter Zijlstra9c03d882009-04-06 11:45:00 +02001706__weak struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
Peter Zijlstra394ee072009-03-30 19:07:14 +02001707{
1708 return NULL;
1709}
1710
1711/*
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001712 * Output
1713 */
1714
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001715struct perf_output_handle {
1716 struct perf_counter *counter;
1717 struct perf_mmap_data *data;
1718 unsigned int offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01001719 unsigned int head;
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001720 int wakeup;
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001721 int nmi;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02001722 int overflow;
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001723};
1724
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001725static inline void __perf_output_wakeup(struct perf_output_handle *handle)
1726{
Peter Zijlstra671dec52009-04-06 11:45:02 +02001727 if (handle->nmi) {
Peter Zijlstra79f14642009-04-06 11:45:07 +02001728 handle->counter->pending_wakeup = 1;
Peter Zijlstra671dec52009-04-06 11:45:02 +02001729 perf_pending_queue(&handle->counter->pending,
Peter Zijlstra79f14642009-04-06 11:45:07 +02001730 perf_pending_counter);
Peter Zijlstra671dec52009-04-06 11:45:02 +02001731 } else
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001732 perf_counter_wakeup(handle->counter);
1733}
1734
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001735static int perf_output_begin(struct perf_output_handle *handle,
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001736 struct perf_counter *counter, unsigned int size,
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02001737 int nmi, int overflow)
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001738{
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001739 struct perf_mmap_data *data;
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001740 unsigned int offset, head;
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001741
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001742 rcu_read_lock();
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001743 data = rcu_dereference(counter->data);
1744 if (!data)
1745 goto out;
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001746
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02001747 handle->counter = counter;
1748 handle->nmi = nmi;
1749 handle->overflow = overflow;
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001750
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001751 if (!data->nr_pages)
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001752 goto fail;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001753
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001754 do {
1755 offset = head = atomic_read(&data->head);
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001756 head += size;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001757 } while (atomic_cmpxchg(&data->head, offset, head) != offset);
1758
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001759 handle->data = data;
1760 handle->offset = offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01001761 handle->head = head;
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001762 handle->wakeup = (offset >> PAGE_SHIFT) != (head >> PAGE_SHIFT);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001763
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001764 return 0;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001765
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001766fail:
1767 __perf_output_wakeup(handle);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001768out:
1769 rcu_read_unlock();
1770
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001771 return -ENOSPC;
1772}
1773
1774static void perf_output_copy(struct perf_output_handle *handle,
1775 void *buf, unsigned int len)
1776{
1777 unsigned int pages_mask;
1778 unsigned int offset;
1779 unsigned int size;
1780 void **pages;
1781
1782 offset = handle->offset;
1783 pages_mask = handle->data->nr_pages - 1;
1784 pages = handle->data->data_pages;
1785
1786 do {
1787 unsigned int page_offset;
1788 int nr;
1789
1790 nr = (offset >> PAGE_SHIFT) & pages_mask;
1791 page_offset = offset & (PAGE_SIZE - 1);
1792 size = min_t(unsigned int, PAGE_SIZE - page_offset, len);
1793
1794 memcpy(pages[nr] + page_offset, buf, size);
1795
1796 len -= size;
1797 buf += size;
1798 offset += size;
1799 } while (len);
1800
1801 handle->offset = offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01001802
1803 WARN_ON_ONCE(handle->offset > handle->head);
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001804}
1805
Peter Zijlstra5c148192009-03-25 12:30:23 +01001806#define perf_output_put(handle, x) \
1807 perf_output_copy((handle), &(x), sizeof(x))
1808
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001809static void perf_output_end(struct perf_output_handle *handle)
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001810{
Peter Zijlstrac4578102009-04-02 11:12:01 +02001811 int wakeup_events = handle->counter->hw_event.wakeup_events;
1812
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02001813 if (handle->overflow && wakeup_events) {
Peter Zijlstrac4578102009-04-02 11:12:01 +02001814 int events = atomic_inc_return(&handle->data->events);
1815 if (events >= wakeup_events) {
1816 atomic_sub(wakeup_events, &handle->data->events);
1817 __perf_output_wakeup(handle);
1818 }
1819 } else if (handle->wakeup)
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001820 __perf_output_wakeup(handle);
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001821 rcu_read_unlock();
1822}
1823
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02001824static void perf_counter_output(struct perf_counter *counter,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02001825 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001826{
Peter Zijlstra5ed00412009-03-30 19:07:12 +02001827 int ret;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02001828 u64 record_type = counter->hw_event.record_type;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02001829 struct perf_output_handle handle;
1830 struct perf_event_header header;
1831 u64 ip;
Peter Zijlstra5c148192009-03-25 12:30:23 +01001832 struct {
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01001833 u32 pid, tid;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02001834 } tid_entry;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02001835 struct {
1836 u64 event;
1837 u64 counter;
1838 } group_entry;
Peter Zijlstra394ee072009-03-30 19:07:14 +02001839 struct perf_callchain_entry *callchain = NULL;
1840 int callchain_size = 0;
Peter Zijlstra339f7c92009-04-06 11:45:06 +02001841 u64 time;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001842
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02001843 header.type = 0;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02001844 header.size = sizeof(header);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001845
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02001846 header.misc = PERF_EVENT_MISC_OVERFLOW;
1847 header.misc |= user_mode(regs) ?
Peter Zijlstra6fab0192009-04-08 15:01:26 +02001848 PERF_EVENT_MISC_USER : PERF_EVENT_MISC_KERNEL;
1849
Peter Zijlstra8a057d82009-04-02 11:11:59 +02001850 if (record_type & PERF_RECORD_IP) {
1851 ip = instruction_pointer(regs);
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02001852 header.type |= PERF_RECORD_IP;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02001853 header.size += sizeof(ip);
1854 }
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01001855
Peter Zijlstra8a057d82009-04-02 11:11:59 +02001856 if (record_type & PERF_RECORD_TID) {
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01001857 /* namespace issues */
Peter Zijlstra5ed00412009-03-30 19:07:12 +02001858 tid_entry.pid = current->group_leader->pid;
1859 tid_entry.tid = current->pid;
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01001860
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02001861 header.type |= PERF_RECORD_TID;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02001862 header.size += sizeof(tid_entry);
1863 }
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01001864
Peter Zijlstra4d855452009-04-08 15:01:32 +02001865 if (record_type & PERF_RECORD_TIME) {
1866 /*
1867 * Maybe do better on x86 and provide cpu_clock_nmi()
1868 */
1869 time = sched_clock();
1870
1871 header.type |= PERF_RECORD_TIME;
1872 header.size += sizeof(u64);
1873 }
1874
Peter Zijlstra78f13e92009-04-08 15:01:33 +02001875 if (record_type & PERF_RECORD_ADDR) {
1876 header.type |= PERF_RECORD_ADDR;
1877 header.size += sizeof(u64);
1878 }
1879
Peter Zijlstra8a057d82009-04-02 11:11:59 +02001880 if (record_type & PERF_RECORD_GROUP) {
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02001881 header.type |= PERF_RECORD_GROUP;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02001882 header.size += sizeof(u64) +
1883 counter->nr_siblings * sizeof(group_entry);
1884 }
1885
1886 if (record_type & PERF_RECORD_CALLCHAIN) {
Peter Zijlstra394ee072009-03-30 19:07:14 +02001887 callchain = perf_callchain(regs);
1888
1889 if (callchain) {
Peter Zijlstra9c03d882009-04-06 11:45:00 +02001890 callchain_size = (1 + callchain->nr) * sizeof(u64);
Peter Zijlstra394ee072009-03-30 19:07:14 +02001891
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02001892 header.type |= PERF_RECORD_CALLCHAIN;
Peter Zijlstra394ee072009-03-30 19:07:14 +02001893 header.size += callchain_size;
1894 }
1895 }
1896
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02001897 ret = perf_output_begin(&handle, counter, header.size, nmi, 1);
Peter Zijlstra5ed00412009-03-30 19:07:12 +02001898 if (ret)
1899 return;
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01001900
Peter Zijlstra5ed00412009-03-30 19:07:12 +02001901 perf_output_put(&handle, header);
Peter Zijlstra5ed00412009-03-30 19:07:12 +02001902
Peter Zijlstra8a057d82009-04-02 11:11:59 +02001903 if (record_type & PERF_RECORD_IP)
1904 perf_output_put(&handle, ip);
1905
1906 if (record_type & PERF_RECORD_TID)
Peter Zijlstra5ed00412009-03-30 19:07:12 +02001907 perf_output_put(&handle, tid_entry);
1908
Peter Zijlstra4d855452009-04-08 15:01:32 +02001909 if (record_type & PERF_RECORD_TIME)
1910 perf_output_put(&handle, time);
1911
Peter Zijlstra78f13e92009-04-08 15:01:33 +02001912 if (record_type & PERF_RECORD_ADDR)
1913 perf_output_put(&handle, addr);
1914
Peter Zijlstra8a057d82009-04-02 11:11:59 +02001915 if (record_type & PERF_RECORD_GROUP) {
1916 struct perf_counter *leader, *sub;
1917 u64 nr = counter->nr_siblings;
1918
1919 perf_output_put(&handle, nr);
1920
1921 leader = counter->group_leader;
1922 list_for_each_entry(sub, &leader->sibling_list, list_entry) {
1923 if (sub != counter)
Robert Richter4aeb0b42009-04-29 12:47:03 +02001924 sub->pmu->read(sub);
Peter Zijlstra8a057d82009-04-02 11:11:59 +02001925
1926 group_entry.event = sub->hw_event.config;
1927 group_entry.counter = atomic64_read(&sub->count);
1928
1929 perf_output_put(&handle, group_entry);
1930 }
1931 }
1932
Peter Zijlstra394ee072009-03-30 19:07:14 +02001933 if (callchain)
1934 perf_output_copy(&handle, callchain, callchain_size);
1935
Peter Zijlstra5ed00412009-03-30 19:07:12 +02001936 perf_output_end(&handle);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001937}
1938
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001939/*
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02001940 * comm tracking
1941 */
1942
1943struct perf_comm_event {
1944 struct task_struct *task;
1945 char *comm;
1946 int comm_size;
1947
1948 struct {
1949 struct perf_event_header header;
1950
1951 u32 pid;
1952 u32 tid;
1953 } event;
1954};
1955
1956static void perf_counter_comm_output(struct perf_counter *counter,
1957 struct perf_comm_event *comm_event)
1958{
1959 struct perf_output_handle handle;
1960 int size = comm_event->event.header.size;
1961 int ret = perf_output_begin(&handle, counter, size, 0, 0);
1962
1963 if (ret)
1964 return;
1965
1966 perf_output_put(&handle, comm_event->event);
1967 perf_output_copy(&handle, comm_event->comm,
1968 comm_event->comm_size);
1969 perf_output_end(&handle);
1970}
1971
1972static int perf_counter_comm_match(struct perf_counter *counter,
1973 struct perf_comm_event *comm_event)
1974{
1975 if (counter->hw_event.comm &&
1976 comm_event->event.header.type == PERF_EVENT_COMM)
1977 return 1;
1978
1979 return 0;
1980}
1981
1982static void perf_counter_comm_ctx(struct perf_counter_context *ctx,
1983 struct perf_comm_event *comm_event)
1984{
1985 struct perf_counter *counter;
1986
1987 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
1988 return;
1989
1990 rcu_read_lock();
1991 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
1992 if (perf_counter_comm_match(counter, comm_event))
1993 perf_counter_comm_output(counter, comm_event);
1994 }
1995 rcu_read_unlock();
1996}
1997
1998static void perf_counter_comm_event(struct perf_comm_event *comm_event)
1999{
2000 struct perf_cpu_context *cpuctx;
2001 unsigned int size;
2002 char *comm = comm_event->task->comm;
2003
Ingo Molnar888fcee2009-04-09 09:48:22 +02002004 size = ALIGN(strlen(comm)+1, sizeof(u64));
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002005
2006 comm_event->comm = comm;
2007 comm_event->comm_size = size;
2008
2009 comm_event->event.header.size = sizeof(comm_event->event) + size;
2010
2011 cpuctx = &get_cpu_var(perf_cpu_context);
2012 perf_counter_comm_ctx(&cpuctx->ctx, comm_event);
2013 put_cpu_var(perf_cpu_context);
2014
2015 perf_counter_comm_ctx(&current->perf_counter_ctx, comm_event);
2016}
2017
2018void perf_counter_comm(struct task_struct *task)
2019{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002020 struct perf_comm_event comm_event;
2021
2022 if (!atomic_read(&nr_comm_tracking))
2023 return;
2024
2025 comm_event = (struct perf_comm_event){
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002026 .task = task,
2027 .event = {
2028 .header = { .type = PERF_EVENT_COMM, },
2029 .pid = task->group_leader->pid,
2030 .tid = task->pid,
2031 },
2032 };
2033
2034 perf_counter_comm_event(&comm_event);
2035}
2036
2037/*
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002038 * mmap tracking
2039 */
2040
2041struct perf_mmap_event {
2042 struct file *file;
2043 char *file_name;
2044 int file_size;
2045
2046 struct {
2047 struct perf_event_header header;
2048
2049 u32 pid;
2050 u32 tid;
2051 u64 start;
2052 u64 len;
2053 u64 pgoff;
2054 } event;
2055};
2056
2057static void perf_counter_mmap_output(struct perf_counter *counter,
2058 struct perf_mmap_event *mmap_event)
2059{
2060 struct perf_output_handle handle;
2061 int size = mmap_event->event.header.size;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002062 int ret = perf_output_begin(&handle, counter, size, 0, 0);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002063
2064 if (ret)
2065 return;
2066
2067 perf_output_put(&handle, mmap_event->event);
2068 perf_output_copy(&handle, mmap_event->file_name,
2069 mmap_event->file_size);
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002070 perf_output_end(&handle);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002071}
2072
2073static int perf_counter_mmap_match(struct perf_counter *counter,
2074 struct perf_mmap_event *mmap_event)
2075{
2076 if (counter->hw_event.mmap &&
2077 mmap_event->event.header.type == PERF_EVENT_MMAP)
2078 return 1;
2079
2080 if (counter->hw_event.munmap &&
2081 mmap_event->event.header.type == PERF_EVENT_MUNMAP)
2082 return 1;
2083
2084 return 0;
2085}
2086
2087static void perf_counter_mmap_ctx(struct perf_counter_context *ctx,
2088 struct perf_mmap_event *mmap_event)
2089{
2090 struct perf_counter *counter;
2091
2092 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2093 return;
2094
2095 rcu_read_lock();
2096 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2097 if (perf_counter_mmap_match(counter, mmap_event))
2098 perf_counter_mmap_output(counter, mmap_event);
2099 }
2100 rcu_read_unlock();
2101}
2102
2103static void perf_counter_mmap_event(struct perf_mmap_event *mmap_event)
2104{
2105 struct perf_cpu_context *cpuctx;
2106 struct file *file = mmap_event->file;
2107 unsigned int size;
2108 char tmp[16];
2109 char *buf = NULL;
2110 char *name;
2111
2112 if (file) {
2113 buf = kzalloc(PATH_MAX, GFP_KERNEL);
2114 if (!buf) {
2115 name = strncpy(tmp, "//enomem", sizeof(tmp));
2116 goto got_name;
2117 }
Peter Zijlstrad3d21c42009-04-09 10:53:46 +02002118 name = d_path(&file->f_path, buf, PATH_MAX);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002119 if (IS_ERR(name)) {
2120 name = strncpy(tmp, "//toolong", sizeof(tmp));
2121 goto got_name;
2122 }
2123 } else {
2124 name = strncpy(tmp, "//anon", sizeof(tmp));
2125 goto got_name;
2126 }
2127
2128got_name:
Ingo Molnar888fcee2009-04-09 09:48:22 +02002129 size = ALIGN(strlen(name)+1, sizeof(u64));
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002130
2131 mmap_event->file_name = name;
2132 mmap_event->file_size = size;
2133
2134 mmap_event->event.header.size = sizeof(mmap_event->event) + size;
2135
2136 cpuctx = &get_cpu_var(perf_cpu_context);
2137 perf_counter_mmap_ctx(&cpuctx->ctx, mmap_event);
2138 put_cpu_var(perf_cpu_context);
2139
2140 perf_counter_mmap_ctx(&current->perf_counter_ctx, mmap_event);
2141
2142 kfree(buf);
2143}
2144
2145void perf_counter_mmap(unsigned long addr, unsigned long len,
2146 unsigned long pgoff, struct file *file)
2147{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002148 struct perf_mmap_event mmap_event;
2149
2150 if (!atomic_read(&nr_mmap_tracking))
2151 return;
2152
2153 mmap_event = (struct perf_mmap_event){
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002154 .file = file,
2155 .event = {
2156 .header = { .type = PERF_EVENT_MMAP, },
2157 .pid = current->group_leader->pid,
2158 .tid = current->pid,
2159 .start = addr,
2160 .len = len,
2161 .pgoff = pgoff,
2162 },
2163 };
2164
2165 perf_counter_mmap_event(&mmap_event);
2166}
2167
2168void perf_counter_munmap(unsigned long addr, unsigned long len,
2169 unsigned long pgoff, struct file *file)
2170{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002171 struct perf_mmap_event mmap_event;
2172
2173 if (!atomic_read(&nr_munmap_tracking))
2174 return;
2175
2176 mmap_event = (struct perf_mmap_event){
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002177 .file = file,
2178 .event = {
2179 .header = { .type = PERF_EVENT_MUNMAP, },
2180 .pid = current->group_leader->pid,
2181 .tid = current->pid,
2182 .start = addr,
2183 .len = len,
2184 .pgoff = pgoff,
2185 },
2186 };
2187
2188 perf_counter_mmap_event(&mmap_event);
2189}
2190
2191/*
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002192 * Generic counter overflow handling.
2193 */
2194
2195int perf_counter_overflow(struct perf_counter *counter,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002196 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002197{
Peter Zijlstra79f14642009-04-06 11:45:07 +02002198 int events = atomic_read(&counter->event_limit);
2199 int ret = 0;
2200
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002201 counter->pending_kill = POLL_IN;
Peter Zijlstra79f14642009-04-06 11:45:07 +02002202 if (events && atomic_dec_and_test(&counter->event_limit)) {
2203 ret = 1;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002204 counter->pending_kill = POLL_HUP;
Peter Zijlstra79f14642009-04-06 11:45:07 +02002205 if (nmi) {
2206 counter->pending_disable = 1;
2207 perf_pending_queue(&counter->pending,
2208 perf_pending_counter);
2209 } else
2210 perf_counter_disable(counter);
2211 }
2212
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002213 perf_counter_output(counter, nmi, regs, addr);
Peter Zijlstra79f14642009-04-06 11:45:07 +02002214 return ret;
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002215}
2216
2217/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002218 * Generic software counter infrastructure
2219 */
2220
2221static void perf_swcounter_update(struct perf_counter *counter)
2222{
2223 struct hw_perf_counter *hwc = &counter->hw;
2224 u64 prev, now;
2225 s64 delta;
2226
2227again:
2228 prev = atomic64_read(&hwc->prev_count);
2229 now = atomic64_read(&hwc->count);
2230 if (atomic64_cmpxchg(&hwc->prev_count, prev, now) != prev)
2231 goto again;
2232
2233 delta = now - prev;
2234
2235 atomic64_add(delta, &counter->count);
2236 atomic64_sub(delta, &hwc->period_left);
2237}
2238
2239static void perf_swcounter_set_period(struct perf_counter *counter)
2240{
2241 struct hw_perf_counter *hwc = &counter->hw;
2242 s64 left = atomic64_read(&hwc->period_left);
2243 s64 period = hwc->irq_period;
2244
2245 if (unlikely(left <= -period)) {
2246 left = period;
2247 atomic64_set(&hwc->period_left, left);
2248 }
2249
2250 if (unlikely(left <= 0)) {
2251 left += period;
2252 atomic64_add(period, &hwc->period_left);
2253 }
2254
2255 atomic64_set(&hwc->prev_count, -left);
2256 atomic64_set(&hwc->count, -left);
2257}
2258
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002259static enum hrtimer_restart perf_swcounter_hrtimer(struct hrtimer *hrtimer)
2260{
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002261 enum hrtimer_restart ret = HRTIMER_RESTART;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002262 struct perf_counter *counter;
2263 struct pt_regs *regs;
2264
2265 counter = container_of(hrtimer, struct perf_counter, hw.hrtimer);
Robert Richter4aeb0b42009-04-29 12:47:03 +02002266 counter->pmu->read(counter);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002267
2268 regs = get_irq_regs();
2269 /*
2270 * In case we exclude kernel IPs or are somehow not in interrupt
2271 * context, provide the next best thing, the user IP.
2272 */
2273 if ((counter->hw_event.exclude_kernel || !regs) &&
2274 !counter->hw_event.exclude_user)
2275 regs = task_pt_regs(current);
2276
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002277 if (regs) {
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002278 if (perf_counter_overflow(counter, 0, regs, 0))
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002279 ret = HRTIMER_NORESTART;
2280 }
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002281
2282 hrtimer_forward_now(hrtimer, ns_to_ktime(counter->hw.irq_period));
2283
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002284 return ret;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002285}
2286
2287static void perf_swcounter_overflow(struct perf_counter *counter,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002288 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002289{
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002290 perf_swcounter_update(counter);
2291 perf_swcounter_set_period(counter);
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002292 if (perf_counter_overflow(counter, nmi, regs, addr))
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002293 /* soft-disable the counter */
2294 ;
2295
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002296}
2297
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002298static int perf_swcounter_match(struct perf_counter *counter,
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002299 enum perf_event_types type,
2300 u32 event, struct pt_regs *regs)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002301{
2302 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
2303 return 0;
2304
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002305 if (perf_event_raw(&counter->hw_event))
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002306 return 0;
2307
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002308 if (perf_event_type(&counter->hw_event) != type)
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002309 return 0;
2310
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002311 if (perf_event_id(&counter->hw_event) != event)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002312 return 0;
2313
2314 if (counter->hw_event.exclude_user && user_mode(regs))
2315 return 0;
2316
2317 if (counter->hw_event.exclude_kernel && !user_mode(regs))
2318 return 0;
2319
2320 return 1;
2321}
2322
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002323static void perf_swcounter_add(struct perf_counter *counter, u64 nr,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002324 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002325{
2326 int neg = atomic64_add_negative(nr, &counter->hw.count);
2327 if (counter->hw.irq_period && !neg)
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002328 perf_swcounter_overflow(counter, nmi, regs, addr);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002329}
2330
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002331static void perf_swcounter_ctx_event(struct perf_counter_context *ctx,
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002332 enum perf_event_types type, u32 event,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002333 u64 nr, int nmi, struct pt_regs *regs,
2334 u64 addr)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002335{
2336 struct perf_counter *counter;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002337
Peter Zijlstra01ef09d2009-03-19 20:26:11 +01002338 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002339 return;
2340
Peter Zijlstra592903c2009-03-13 12:21:36 +01002341 rcu_read_lock();
2342 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002343 if (perf_swcounter_match(counter, type, event, regs))
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002344 perf_swcounter_add(counter, nr, nmi, regs, addr);
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002345 }
Peter Zijlstra592903c2009-03-13 12:21:36 +01002346 rcu_read_unlock();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002347}
2348
Peter Zijlstra96f6d442009-03-23 18:22:07 +01002349static int *perf_swcounter_recursion_context(struct perf_cpu_context *cpuctx)
2350{
2351 if (in_nmi())
2352 return &cpuctx->recursion[3];
2353
2354 if (in_irq())
2355 return &cpuctx->recursion[2];
2356
2357 if (in_softirq())
2358 return &cpuctx->recursion[1];
2359
2360 return &cpuctx->recursion[0];
2361}
2362
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002363static void __perf_swcounter_event(enum perf_event_types type, u32 event,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002364 u64 nr, int nmi, struct pt_regs *regs,
2365 u64 addr)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002366{
2367 struct perf_cpu_context *cpuctx = &get_cpu_var(perf_cpu_context);
Peter Zijlstra96f6d442009-03-23 18:22:07 +01002368 int *recursion = perf_swcounter_recursion_context(cpuctx);
2369
2370 if (*recursion)
2371 goto out;
2372
2373 (*recursion)++;
2374 barrier();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002375
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002376 perf_swcounter_ctx_event(&cpuctx->ctx, type, event,
2377 nr, nmi, regs, addr);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002378 if (cpuctx->task_ctx) {
2379 perf_swcounter_ctx_event(cpuctx->task_ctx, type, event,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002380 nr, nmi, regs, addr);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002381 }
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002382
Peter Zijlstra96f6d442009-03-23 18:22:07 +01002383 barrier();
2384 (*recursion)--;
2385
2386out:
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002387 put_cpu_var(perf_cpu_context);
2388}
2389
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002390void
2391perf_swcounter_event(u32 event, u64 nr, int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002392{
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002393 __perf_swcounter_event(PERF_TYPE_SOFTWARE, event, nr, nmi, regs, addr);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002394}
2395
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002396static void perf_swcounter_read(struct perf_counter *counter)
2397{
2398 perf_swcounter_update(counter);
2399}
2400
2401static int perf_swcounter_enable(struct perf_counter *counter)
2402{
2403 perf_swcounter_set_period(counter);
2404 return 0;
2405}
2406
2407static void perf_swcounter_disable(struct perf_counter *counter)
2408{
2409 perf_swcounter_update(counter);
2410}
2411
Robert Richter4aeb0b42009-04-29 12:47:03 +02002412static const struct pmu perf_ops_generic = {
Peter Zijlstraac17dc82009-03-13 12:21:34 +01002413 .enable = perf_swcounter_enable,
2414 .disable = perf_swcounter_disable,
2415 .read = perf_swcounter_read,
2416};
2417
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002418/*
2419 * Software counter: cpu wall time clock
2420 */
2421
Paul Mackerras9abf8a02009-01-09 16:26:43 +11002422static void cpu_clock_perf_counter_update(struct perf_counter *counter)
2423{
2424 int cpu = raw_smp_processor_id();
2425 s64 prev;
2426 u64 now;
2427
2428 now = cpu_clock(cpu);
2429 prev = atomic64_read(&counter->hw.prev_count);
2430 atomic64_set(&counter->hw.prev_count, now);
2431 atomic64_add(now - prev, &counter->count);
2432}
2433
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002434static int cpu_clock_perf_counter_enable(struct perf_counter *counter)
2435{
2436 struct hw_perf_counter *hwc = &counter->hw;
2437 int cpu = raw_smp_processor_id();
2438
2439 atomic64_set(&hwc->prev_count, cpu_clock(cpu));
Peter Zijlstra039fc912009-03-13 16:43:47 +01002440 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2441 hwc->hrtimer.function = perf_swcounter_hrtimer;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002442 if (hwc->irq_period) {
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002443 __hrtimer_start_range_ns(&hwc->hrtimer,
2444 ns_to_ktime(hwc->irq_period), 0,
2445 HRTIMER_MODE_REL, 0);
2446 }
2447
2448 return 0;
2449}
2450
Ingo Molnar5c92d122008-12-11 13:21:10 +01002451static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
2452{
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002453 hrtimer_cancel(&counter->hw.hrtimer);
Paul Mackerras9abf8a02009-01-09 16:26:43 +11002454 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01002455}
2456
2457static void cpu_clock_perf_counter_read(struct perf_counter *counter)
2458{
Paul Mackerras9abf8a02009-01-09 16:26:43 +11002459 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01002460}
2461
Robert Richter4aeb0b42009-04-29 12:47:03 +02002462static const struct pmu perf_ops_cpu_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +01002463 .enable = cpu_clock_perf_counter_enable,
2464 .disable = cpu_clock_perf_counter_disable,
2465 .read = cpu_clock_perf_counter_read,
Ingo Molnar5c92d122008-12-11 13:21:10 +01002466};
2467
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01002468/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002469 * Software counter: task time clock
2470 */
2471
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02002472static void task_clock_perf_counter_update(struct perf_counter *counter, u64 now)
Ingo Molnarbae43c92008-12-11 14:03:20 +01002473{
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02002474 u64 prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002475 s64 delta;
Ingo Molnarbae43c92008-12-11 14:03:20 +01002476
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02002477 prev = atomic64_xchg(&counter->hw.prev_count, now);
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002478 delta = now - prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002479 atomic64_add(delta, &counter->count);
Ingo Molnarbae43c92008-12-11 14:03:20 +01002480}
2481
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01002482static int task_clock_perf_counter_enable(struct perf_counter *counter)
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002483{
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002484 struct hw_perf_counter *hwc = &counter->hw;
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02002485 u64 now;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002486
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02002487 now = counter->ctx->time;
2488
2489 atomic64_set(&hwc->prev_count, now);
Peter Zijlstra039fc912009-03-13 16:43:47 +01002490 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2491 hwc->hrtimer.function = perf_swcounter_hrtimer;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002492 if (hwc->irq_period) {
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002493 __hrtimer_start_range_ns(&hwc->hrtimer,
2494 ns_to_ktime(hwc->irq_period), 0,
2495 HRTIMER_MODE_REL, 0);
2496 }
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01002497
2498 return 0;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002499}
2500
2501static void task_clock_perf_counter_disable(struct perf_counter *counter)
2502{
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002503 hrtimer_cancel(&counter->hw.hrtimer);
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02002504 task_clock_perf_counter_update(counter, counter->ctx->time);
2505
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002506}
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01002507
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002508static void task_clock_perf_counter_read(struct perf_counter *counter)
2509{
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02002510 u64 time;
2511
2512 if (!in_nmi()) {
2513 update_context_time(counter->ctx);
2514 time = counter->ctx->time;
2515 } else {
2516 u64 now = perf_clock();
2517 u64 delta = now - counter->ctx->timestamp;
2518 time = counter->ctx->time + delta;
2519 }
2520
2521 task_clock_perf_counter_update(counter, time);
Ingo Molnarbae43c92008-12-11 14:03:20 +01002522}
2523
Robert Richter4aeb0b42009-04-29 12:47:03 +02002524static const struct pmu perf_ops_task_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +01002525 .enable = task_clock_perf_counter_enable,
2526 .disable = task_clock_perf_counter_disable,
2527 .read = task_clock_perf_counter_read,
Ingo Molnarbae43c92008-12-11 14:03:20 +01002528};
2529
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002530/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002531 * Software counter: cpu migrations
2532 */
2533
Paul Mackerras23a185c2009-02-09 22:42:47 +11002534static inline u64 get_cpu_migrations(struct perf_counter *counter)
Ingo Molnar6c594c22008-12-14 12:34:15 +01002535{
Paul Mackerras23a185c2009-02-09 22:42:47 +11002536 struct task_struct *curr = counter->ctx->task;
2537
2538 if (curr)
2539 return curr->se.nr_migrations;
2540 return cpu_nr_migrations(smp_processor_id());
Ingo Molnar6c594c22008-12-14 12:34:15 +01002541}
2542
2543static void cpu_migrations_perf_counter_update(struct perf_counter *counter)
2544{
2545 u64 prev, now;
2546 s64 delta;
2547
2548 prev = atomic64_read(&counter->hw.prev_count);
Paul Mackerras23a185c2009-02-09 22:42:47 +11002549 now = get_cpu_migrations(counter);
Ingo Molnar6c594c22008-12-14 12:34:15 +01002550
2551 atomic64_set(&counter->hw.prev_count, now);
2552
2553 delta = now - prev;
Ingo Molnar6c594c22008-12-14 12:34:15 +01002554
2555 atomic64_add(delta, &counter->count);
2556}
2557
2558static void cpu_migrations_perf_counter_read(struct perf_counter *counter)
2559{
2560 cpu_migrations_perf_counter_update(counter);
2561}
2562
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01002563static int cpu_migrations_perf_counter_enable(struct perf_counter *counter)
Ingo Molnar6c594c22008-12-14 12:34:15 +01002564{
Paul Mackerrasc07c99b2009-02-13 22:10:34 +11002565 if (counter->prev_state <= PERF_COUNTER_STATE_OFF)
2566 atomic64_set(&counter->hw.prev_count,
2567 get_cpu_migrations(counter));
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01002568 return 0;
Ingo Molnar6c594c22008-12-14 12:34:15 +01002569}
2570
2571static void cpu_migrations_perf_counter_disable(struct perf_counter *counter)
2572{
2573 cpu_migrations_perf_counter_update(counter);
2574}
2575
Robert Richter4aeb0b42009-04-29 12:47:03 +02002576static const struct pmu perf_ops_cpu_migrations = {
Ingo Molnar76715812008-12-17 14:20:28 +01002577 .enable = cpu_migrations_perf_counter_enable,
2578 .disable = cpu_migrations_perf_counter_disable,
2579 .read = cpu_migrations_perf_counter_read,
Ingo Molnar6c594c22008-12-14 12:34:15 +01002580};
2581
Peter Zijlstrae077df42009-03-19 20:26:17 +01002582#ifdef CONFIG_EVENT_PROFILE
2583void perf_tpcounter_event(int event_id)
2584{
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002585 struct pt_regs *regs = get_irq_regs();
2586
2587 if (!regs)
2588 regs = task_pt_regs(current);
2589
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002590 __perf_swcounter_event(PERF_TYPE_TRACEPOINT, event_id, 1, 1, regs, 0);
Peter Zijlstrae077df42009-03-19 20:26:17 +01002591}
Steven Whitehouseff7b1b42009-04-15 16:55:05 +01002592EXPORT_SYMBOL_GPL(perf_tpcounter_event);
Peter Zijlstrae077df42009-03-19 20:26:17 +01002593
2594extern int ftrace_profile_enable(int);
2595extern void ftrace_profile_disable(int);
2596
2597static void tp_perf_counter_destroy(struct perf_counter *counter)
2598{
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002599 ftrace_profile_disable(perf_event_id(&counter->hw_event));
Peter Zijlstrae077df42009-03-19 20:26:17 +01002600}
2601
Robert Richter4aeb0b42009-04-29 12:47:03 +02002602static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
Peter Zijlstrae077df42009-03-19 20:26:17 +01002603{
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002604 int event_id = perf_event_id(&counter->hw_event);
Peter Zijlstrae077df42009-03-19 20:26:17 +01002605 int ret;
2606
2607 ret = ftrace_profile_enable(event_id);
2608 if (ret)
2609 return NULL;
2610
2611 counter->destroy = tp_perf_counter_destroy;
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002612 counter->hw.irq_period = counter->hw_event.irq_period;
Peter Zijlstrae077df42009-03-19 20:26:17 +01002613
2614 return &perf_ops_generic;
2615}
2616#else
Robert Richter4aeb0b42009-04-29 12:47:03 +02002617static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
Peter Zijlstrae077df42009-03-19 20:26:17 +01002618{
2619 return NULL;
2620}
2621#endif
2622
Robert Richter4aeb0b42009-04-29 12:47:03 +02002623static const struct pmu *sw_perf_counter_init(struct perf_counter *counter)
Ingo Molnar5c92d122008-12-11 13:21:10 +01002624{
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002625 struct perf_counter_hw_event *hw_event = &counter->hw_event;
Robert Richter4aeb0b42009-04-29 12:47:03 +02002626 const struct pmu *pmu = NULL;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002627 struct hw_perf_counter *hwc = &counter->hw;
Ingo Molnar5c92d122008-12-11 13:21:10 +01002628
Paul Mackerras0475f9e2009-02-11 14:35:35 +11002629 /*
2630 * Software counters (currently) can't in general distinguish
2631 * between user, kernel and hypervisor events.
2632 * However, context switches and cpu migrations are considered
2633 * to be kernel events, and page faults are never hypervisor
2634 * events.
2635 */
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002636 switch (perf_event_id(&counter->hw_event)) {
Ingo Molnar5c92d122008-12-11 13:21:10 +01002637 case PERF_COUNT_CPU_CLOCK:
Robert Richter4aeb0b42009-04-29 12:47:03 +02002638 pmu = &perf_ops_cpu_clock;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002639
2640 if (hw_event->irq_period && hw_event->irq_period < 10000)
2641 hw_event->irq_period = 10000;
Ingo Molnar5c92d122008-12-11 13:21:10 +01002642 break;
Ingo Molnarbae43c92008-12-11 14:03:20 +01002643 case PERF_COUNT_TASK_CLOCK:
Paul Mackerras23a185c2009-02-09 22:42:47 +11002644 /*
2645 * If the user instantiates this as a per-cpu counter,
2646 * use the cpu_clock counter instead.
2647 */
2648 if (counter->ctx->task)
Robert Richter4aeb0b42009-04-29 12:47:03 +02002649 pmu = &perf_ops_task_clock;
Paul Mackerras23a185c2009-02-09 22:42:47 +11002650 else
Robert Richter4aeb0b42009-04-29 12:47:03 +02002651 pmu = &perf_ops_cpu_clock;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002652
2653 if (hw_event->irq_period && hw_event->irq_period < 10000)
2654 hw_event->irq_period = 10000;
Ingo Molnarbae43c92008-12-11 14:03:20 +01002655 break;
Ingo Molnare06c61a2008-12-14 14:44:31 +01002656 case PERF_COUNT_PAGE_FAULTS:
Peter Zijlstraac17dc82009-03-13 12:21:34 +01002657 case PERF_COUNT_PAGE_FAULTS_MIN:
2658 case PERF_COUNT_PAGE_FAULTS_MAJ:
Ingo Molnar5d6a27d2008-12-14 12:28:33 +01002659 case PERF_COUNT_CONTEXT_SWITCHES:
Robert Richter4aeb0b42009-04-29 12:47:03 +02002660 pmu = &perf_ops_generic;
Ingo Molnar5d6a27d2008-12-14 12:28:33 +01002661 break;
Ingo Molnar6c594c22008-12-14 12:34:15 +01002662 case PERF_COUNT_CPU_MIGRATIONS:
Paul Mackerras0475f9e2009-02-11 14:35:35 +11002663 if (!counter->hw_event.exclude_kernel)
Robert Richter4aeb0b42009-04-29 12:47:03 +02002664 pmu = &perf_ops_cpu_migrations;
Ingo Molnar6c594c22008-12-14 12:34:15 +01002665 break;
Ingo Molnar5c92d122008-12-11 13:21:10 +01002666 }
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002667
Robert Richter4aeb0b42009-04-29 12:47:03 +02002668 if (pmu)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002669 hwc->irq_period = hw_event->irq_period;
2670
Robert Richter4aeb0b42009-04-29 12:47:03 +02002671 return pmu;
Ingo Molnar5c92d122008-12-11 13:21:10 +01002672}
2673
Thomas Gleixner0793a612008-12-04 20:12:29 +01002674/*
2675 * Allocate and initialize a counter structure
2676 */
2677static struct perf_counter *
Ingo Molnar04289bb2008-12-11 08:38:42 +01002678perf_counter_alloc(struct perf_counter_hw_event *hw_event,
2679 int cpu,
Paul Mackerras23a185c2009-02-09 22:42:47 +11002680 struct perf_counter_context *ctx,
Ingo Molnar9b51f662008-12-12 13:49:45 +01002681 struct perf_counter *group_leader,
2682 gfp_t gfpflags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01002683{
Robert Richter4aeb0b42009-04-29 12:47:03 +02002684 const struct pmu *pmu;
Ingo Molnar621a01e2008-12-11 12:46:46 +01002685 struct perf_counter *counter;
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02002686 long err;
Thomas Gleixner0793a612008-12-04 20:12:29 +01002687
Ingo Molnar9b51f662008-12-12 13:49:45 +01002688 counter = kzalloc(sizeof(*counter), gfpflags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002689 if (!counter)
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02002690 return ERR_PTR(-ENOMEM);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002691
Ingo Molnar04289bb2008-12-11 08:38:42 +01002692 /*
2693 * Single counters are their own group leaders, with an
2694 * empty sibling list:
2695 */
2696 if (!group_leader)
2697 group_leader = counter;
2698
Thomas Gleixner0793a612008-12-04 20:12:29 +01002699 mutex_init(&counter->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01002700 INIT_LIST_HEAD(&counter->list_entry);
Peter Zijlstra592903c2009-03-13 12:21:36 +01002701 INIT_LIST_HEAD(&counter->event_entry);
Ingo Molnar04289bb2008-12-11 08:38:42 +01002702 INIT_LIST_HEAD(&counter->sibling_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002703 init_waitqueue_head(&counter->waitq);
2704
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002705 mutex_init(&counter->mmap_mutex);
2706
Paul Mackerrasd859e292009-01-17 18:10:22 +11002707 INIT_LIST_HEAD(&counter->child_list);
2708
Ingo Molnar9f66a382008-12-10 12:33:23 +01002709 counter->cpu = cpu;
2710 counter->hw_event = *hw_event;
Ingo Molnar04289bb2008-12-11 08:38:42 +01002711 counter->group_leader = group_leader;
Robert Richter4aeb0b42009-04-29 12:47:03 +02002712 counter->pmu = NULL;
Paul Mackerras23a185c2009-02-09 22:42:47 +11002713 counter->ctx = ctx;
Ingo Molnar621a01e2008-12-11 12:46:46 +01002714
Ingo Molnar235c7fc2008-12-21 14:43:25 +01002715 counter->state = PERF_COUNTER_STATE_INACTIVE;
Ingo Molnara86ed502008-12-17 00:43:10 +01002716 if (hw_event->disabled)
2717 counter->state = PERF_COUNTER_STATE_OFF;
2718
Robert Richter4aeb0b42009-04-29 12:47:03 +02002719 pmu = NULL;
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002720
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002721 if (perf_event_raw(hw_event)) {
Robert Richter4aeb0b42009-04-29 12:47:03 +02002722 pmu = hw_perf_counter_init(counter);
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002723 goto done;
2724 }
2725
2726 switch (perf_event_type(hw_event)) {
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002727 case PERF_TYPE_HARDWARE:
Robert Richter4aeb0b42009-04-29 12:47:03 +02002728 pmu = hw_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002729 break;
2730
2731 case PERF_TYPE_SOFTWARE:
Robert Richter4aeb0b42009-04-29 12:47:03 +02002732 pmu = sw_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002733 break;
2734
2735 case PERF_TYPE_TRACEPOINT:
Robert Richter4aeb0b42009-04-29 12:47:03 +02002736 pmu = tp_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002737 break;
2738 }
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002739done:
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02002740 err = 0;
Robert Richter4aeb0b42009-04-29 12:47:03 +02002741 if (!pmu)
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02002742 err = -EINVAL;
Robert Richter4aeb0b42009-04-29 12:47:03 +02002743 else if (IS_ERR(pmu))
2744 err = PTR_ERR(pmu);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02002745
2746 if (err) {
2747 kfree(counter);
2748 return ERR_PTR(err);
2749 }
2750
Robert Richter4aeb0b42009-04-29 12:47:03 +02002751 counter->pmu = pmu;
Thomas Gleixner0793a612008-12-04 20:12:29 +01002752
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002753 if (counter->hw_event.mmap)
2754 atomic_inc(&nr_mmap_tracking);
2755 if (counter->hw_event.munmap)
2756 atomic_inc(&nr_munmap_tracking);
2757 if (counter->hw_event.comm)
2758 atomic_inc(&nr_comm_tracking);
2759
Thomas Gleixner0793a612008-12-04 20:12:29 +01002760 return counter;
2761}
2762
2763/**
Paul Mackerras2743a5b2009-03-04 20:36:51 +11002764 * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
Ingo Molnar9f66a382008-12-10 12:33:23 +01002765 *
2766 * @hw_event_uptr: event type attributes for monitoring/sampling
Thomas Gleixner0793a612008-12-04 20:12:29 +01002767 * @pid: target pid
Ingo Molnar9f66a382008-12-10 12:33:23 +01002768 * @cpu: target cpu
2769 * @group_fd: group leader counter fd
Thomas Gleixner0793a612008-12-04 20:12:29 +01002770 */
Paul Mackerras2743a5b2009-03-04 20:36:51 +11002771SYSCALL_DEFINE5(perf_counter_open,
Paul Mackerrasf3dfd262009-02-26 22:43:46 +11002772 const struct perf_counter_hw_event __user *, hw_event_uptr,
Paul Mackerras2743a5b2009-03-04 20:36:51 +11002773 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01002774{
Ingo Molnar04289bb2008-12-11 08:38:42 +01002775 struct perf_counter *counter, *group_leader;
Ingo Molnar9f66a382008-12-10 12:33:23 +01002776 struct perf_counter_hw_event hw_event;
Ingo Molnar04289bb2008-12-11 08:38:42 +01002777 struct perf_counter_context *ctx;
Ingo Molnar9b51f662008-12-12 13:49:45 +01002778 struct file *counter_file = NULL;
Ingo Molnar04289bb2008-12-11 08:38:42 +01002779 struct file *group_file = NULL;
2780 int fput_needed = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01002781 int fput_needed2 = 0;
Thomas Gleixner0793a612008-12-04 20:12:29 +01002782 int ret;
2783
Paul Mackerras2743a5b2009-03-04 20:36:51 +11002784 /* for future expandability... */
2785 if (flags)
2786 return -EINVAL;
2787
Ingo Molnar9f66a382008-12-10 12:33:23 +01002788 if (copy_from_user(&hw_event, hw_event_uptr, sizeof(hw_event)) != 0)
Thomas Gleixnereab656a2008-12-08 19:26:59 +01002789 return -EFAULT;
2790
Ingo Molnar04289bb2008-12-11 08:38:42 +01002791 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01002792 * Get the target context (task or percpu):
2793 */
2794 ctx = find_get_context(pid, cpu);
2795 if (IS_ERR(ctx))
2796 return PTR_ERR(ctx);
2797
2798 /*
2799 * Look up the group leader (we will attach this counter to it):
Ingo Molnar04289bb2008-12-11 08:38:42 +01002800 */
2801 group_leader = NULL;
2802 if (group_fd != -1) {
2803 ret = -EINVAL;
2804 group_file = fget_light(group_fd, &fput_needed);
2805 if (!group_file)
Ingo Molnarccff2862008-12-11 11:26:29 +01002806 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01002807 if (group_file->f_op != &perf_fops)
Ingo Molnarccff2862008-12-11 11:26:29 +01002808 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01002809
2810 group_leader = group_file->private_data;
2811 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01002812 * Do not allow a recursive hierarchy (this new sibling
2813 * becoming part of another group-sibling):
Ingo Molnar04289bb2008-12-11 08:38:42 +01002814 */
Ingo Molnarccff2862008-12-11 11:26:29 +01002815 if (group_leader->group_leader != group_leader)
2816 goto err_put_context;
2817 /*
2818 * Do not allow to attach to a group in a different
2819 * task or CPU context:
2820 */
2821 if (group_leader->ctx != ctx)
2822 goto err_put_context;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11002823 /*
2824 * Only a group leader can be exclusive or pinned
2825 */
2826 if (hw_event.exclusive || hw_event.pinned)
2827 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01002828 }
2829
Paul Mackerras23a185c2009-02-09 22:42:47 +11002830 counter = perf_counter_alloc(&hw_event, cpu, ctx, group_leader,
2831 GFP_KERNEL);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02002832 ret = PTR_ERR(counter);
2833 if (IS_ERR(counter))
Thomas Gleixner0793a612008-12-04 20:12:29 +01002834 goto err_put_context;
2835
Thomas Gleixner0793a612008-12-04 20:12:29 +01002836 ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
2837 if (ret < 0)
Ingo Molnar9b51f662008-12-12 13:49:45 +01002838 goto err_free_put_context;
2839
2840 counter_file = fget_light(ret, &fput_needed2);
2841 if (!counter_file)
2842 goto err_free_put_context;
2843
2844 counter->filp = counter_file;
Paul Mackerrasd859e292009-01-17 18:10:22 +11002845 mutex_lock(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01002846 perf_install_in_context(ctx, counter, cpu);
Paul Mackerrasd859e292009-01-17 18:10:22 +11002847 mutex_unlock(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01002848
2849 fput_light(counter_file, fput_needed2);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002850
Ingo Molnar04289bb2008-12-11 08:38:42 +01002851out_fput:
2852 fput_light(group_file, fput_needed);
2853
Thomas Gleixner0793a612008-12-04 20:12:29 +01002854 return ret;
2855
Ingo Molnar9b51f662008-12-12 13:49:45 +01002856err_free_put_context:
Thomas Gleixner0793a612008-12-04 20:12:29 +01002857 kfree(counter);
2858
2859err_put_context:
2860 put_context(ctx);
2861
Ingo Molnar04289bb2008-12-11 08:38:42 +01002862 goto out_fput;
Thomas Gleixner0793a612008-12-04 20:12:29 +01002863}
2864
Ingo Molnar9b51f662008-12-12 13:49:45 +01002865/*
2866 * Initialize the perf_counter context in a task_struct:
2867 */
2868static void
2869__perf_counter_init_context(struct perf_counter_context *ctx,
2870 struct task_struct *task)
2871{
2872 memset(ctx, 0, sizeof(*ctx));
2873 spin_lock_init(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +11002874 mutex_init(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01002875 INIT_LIST_HEAD(&ctx->counter_list);
Peter Zijlstra592903c2009-03-13 12:21:36 +01002876 INIT_LIST_HEAD(&ctx->event_list);
Ingo Molnar9b51f662008-12-12 13:49:45 +01002877 ctx->task = task;
2878}
2879
2880/*
2881 * inherit a counter from parent task to child task:
2882 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11002883static struct perf_counter *
Ingo Molnar9b51f662008-12-12 13:49:45 +01002884inherit_counter(struct perf_counter *parent_counter,
2885 struct task_struct *parent,
2886 struct perf_counter_context *parent_ctx,
2887 struct task_struct *child,
Paul Mackerrasd859e292009-01-17 18:10:22 +11002888 struct perf_counter *group_leader,
Ingo Molnar9b51f662008-12-12 13:49:45 +01002889 struct perf_counter_context *child_ctx)
2890{
2891 struct perf_counter *child_counter;
2892
Paul Mackerrasd859e292009-01-17 18:10:22 +11002893 /*
2894 * Instead of creating recursive hierarchies of counters,
2895 * we link inherited counters back to the original parent,
2896 * which has a filp for sure, which we use as the reference
2897 * count:
2898 */
2899 if (parent_counter->parent)
2900 parent_counter = parent_counter->parent;
2901
Ingo Molnar9b51f662008-12-12 13:49:45 +01002902 child_counter = perf_counter_alloc(&parent_counter->hw_event,
Paul Mackerras23a185c2009-02-09 22:42:47 +11002903 parent_counter->cpu, child_ctx,
2904 group_leader, GFP_KERNEL);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02002905 if (IS_ERR(child_counter))
2906 return child_counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01002907
2908 /*
2909 * Link it up in the child's context:
2910 */
Ingo Molnar9b51f662008-12-12 13:49:45 +01002911 child_counter->task = child;
Paul Mackerras53cfbf52009-03-25 22:46:58 +11002912 add_counter_to_ctx(child_counter, child_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01002913
2914 child_counter->parent = parent_counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01002915 /*
2916 * inherit into child's child as well:
2917 */
2918 child_counter->hw_event.inherit = 1;
2919
2920 /*
2921 * Get a reference to the parent filp - we will fput it
2922 * when the child counter exits. This is safe to do because
2923 * we are in the parent and we know that the filp still
2924 * exists and has a nonzero count:
2925 */
2926 atomic_long_inc(&parent_counter->filp->f_count);
2927
Paul Mackerrasd859e292009-01-17 18:10:22 +11002928 /*
2929 * Link this into the parent counter's child list
2930 */
2931 mutex_lock(&parent_counter->mutex);
2932 list_add_tail(&child_counter->child_list, &parent_counter->child_list);
2933
2934 /*
2935 * Make the child state follow the state of the parent counter,
2936 * not its hw_event.disabled bit. We hold the parent's mutex,
2937 * so we won't race with perf_counter_{en,dis}able_family.
2938 */
2939 if (parent_counter->state >= PERF_COUNTER_STATE_INACTIVE)
2940 child_counter->state = PERF_COUNTER_STATE_INACTIVE;
2941 else
2942 child_counter->state = PERF_COUNTER_STATE_OFF;
2943
2944 mutex_unlock(&parent_counter->mutex);
2945
2946 return child_counter;
2947}
2948
2949static int inherit_group(struct perf_counter *parent_counter,
2950 struct task_struct *parent,
2951 struct perf_counter_context *parent_ctx,
2952 struct task_struct *child,
2953 struct perf_counter_context *child_ctx)
2954{
2955 struct perf_counter *leader;
2956 struct perf_counter *sub;
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02002957 struct perf_counter *child_ctr;
Paul Mackerrasd859e292009-01-17 18:10:22 +11002958
2959 leader = inherit_counter(parent_counter, parent, parent_ctx,
2960 child, NULL, child_ctx);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02002961 if (IS_ERR(leader))
2962 return PTR_ERR(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +11002963 list_for_each_entry(sub, &parent_counter->sibling_list, list_entry) {
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02002964 child_ctr = inherit_counter(sub, parent, parent_ctx,
2965 child, leader, child_ctx);
2966 if (IS_ERR(child_ctr))
2967 return PTR_ERR(child_ctr);
Paul Mackerrasd859e292009-01-17 18:10:22 +11002968 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01002969 return 0;
2970}
2971
Paul Mackerrasd859e292009-01-17 18:10:22 +11002972static void sync_child_counter(struct perf_counter *child_counter,
2973 struct perf_counter *parent_counter)
2974{
2975 u64 parent_val, child_val;
2976
2977 parent_val = atomic64_read(&parent_counter->count);
2978 child_val = atomic64_read(&child_counter->count);
2979
2980 /*
2981 * Add back the child's count to the parent's count:
2982 */
2983 atomic64_add(child_val, &parent_counter->count);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11002984 atomic64_add(child_counter->total_time_enabled,
2985 &parent_counter->child_total_time_enabled);
2986 atomic64_add(child_counter->total_time_running,
2987 &parent_counter->child_total_time_running);
Paul Mackerrasd859e292009-01-17 18:10:22 +11002988
2989 /*
2990 * Remove this counter from the parent's list
2991 */
2992 mutex_lock(&parent_counter->mutex);
2993 list_del_init(&child_counter->child_list);
2994 mutex_unlock(&parent_counter->mutex);
2995
2996 /*
2997 * Release the parent counter, if this was the last
2998 * reference to it.
2999 */
3000 fput(parent_counter->filp);
3001}
3002
Ingo Molnar9b51f662008-12-12 13:49:45 +01003003static void
3004__perf_counter_exit_task(struct task_struct *child,
3005 struct perf_counter *child_counter,
3006 struct perf_counter_context *child_ctx)
3007{
3008 struct perf_counter *parent_counter;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003009 struct perf_counter *sub, *tmp;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003010
3011 /*
Ingo Molnar235c7fc2008-12-21 14:43:25 +01003012 * If we do not self-reap then we have to wait for the
3013 * child task to unschedule (it will happen for sure),
3014 * so that its counter is at its final count. (This
3015 * condition triggers rarely - child tasks usually get
3016 * off their CPU before the parent has a chance to
3017 * get this far into the reaping action)
Ingo Molnar9b51f662008-12-12 13:49:45 +01003018 */
Ingo Molnar235c7fc2008-12-21 14:43:25 +01003019 if (child != current) {
3020 wait_task_inactive(child, 0);
3021 list_del_init(&child_counter->list_entry);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11003022 update_counter_times(child_counter);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01003023 } else {
Ingo Molnar0cc0c022008-12-14 23:20:36 +01003024 struct perf_cpu_context *cpuctx;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01003025 unsigned long flags;
3026 u64 perf_flags;
3027
3028 /*
3029 * Disable and unlink this counter.
3030 *
3031 * Be careful about zapping the list - IRQ/NMI context
3032 * could still be processing it:
3033 */
Peter Zijlstra849691a2009-04-06 11:45:12 +02003034 local_irq_save(flags);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01003035 perf_flags = hw_perf_save_disable();
Ingo Molnar0cc0c022008-12-14 23:20:36 +01003036
3037 cpuctx = &__get_cpu_var(perf_cpu_context);
3038
Paul Mackerrasd859e292009-01-17 18:10:22 +11003039 group_sched_out(child_counter, cpuctx, child_ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11003040 update_counter_times(child_counter);
Ingo Molnar0cc0c022008-12-14 23:20:36 +01003041
Ingo Molnar235c7fc2008-12-21 14:43:25 +01003042 list_del_init(&child_counter->list_entry);
3043
3044 child_ctx->nr_counters--;
3045
3046 hw_perf_restore(perf_flags);
Peter Zijlstra849691a2009-04-06 11:45:12 +02003047 local_irq_restore(flags);
Ingo Molnar0cc0c022008-12-14 23:20:36 +01003048 }
3049
Ingo Molnar9b51f662008-12-12 13:49:45 +01003050 parent_counter = child_counter->parent;
3051 /*
3052 * It can happen that parent exits first, and has counters
3053 * that are still around due to the child reference. These
3054 * counters need to be zapped - but otherwise linger.
3055 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11003056 if (parent_counter) {
3057 sync_child_counter(child_counter, parent_counter);
3058 list_for_each_entry_safe(sub, tmp, &child_counter->sibling_list,
3059 list_entry) {
Paul Mackerras4bcf3492009-02-11 13:53:19 +01003060 if (sub->parent) {
Paul Mackerrasd859e292009-01-17 18:10:22 +11003061 sync_child_counter(sub, sub->parent);
Peter Zijlstraf1600952009-03-19 20:26:16 +01003062 free_counter(sub);
Paul Mackerras4bcf3492009-02-11 13:53:19 +01003063 }
Paul Mackerrasd859e292009-01-17 18:10:22 +11003064 }
Peter Zijlstraf1600952009-03-19 20:26:16 +01003065 free_counter(child_counter);
Paul Mackerras4bcf3492009-02-11 13:53:19 +01003066 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01003067}
3068
3069/*
Paul Mackerrasd859e292009-01-17 18:10:22 +11003070 * When a child task exits, feed back counter values to parent counters.
Ingo Molnar9b51f662008-12-12 13:49:45 +01003071 *
Paul Mackerrasd859e292009-01-17 18:10:22 +11003072 * Note: we may be running in child context, but the PID is not hashed
Ingo Molnar9b51f662008-12-12 13:49:45 +01003073 * anymore so new counters will not be added.
3074 */
3075void perf_counter_exit_task(struct task_struct *child)
3076{
3077 struct perf_counter *child_counter, *tmp;
3078 struct perf_counter_context *child_ctx;
3079
3080 child_ctx = &child->perf_counter_ctx;
3081
3082 if (likely(!child_ctx->nr_counters))
3083 return;
3084
3085 list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list,
3086 list_entry)
3087 __perf_counter_exit_task(child, child_counter, child_ctx);
3088}
3089
3090/*
3091 * Initialize the perf_counter context in task_struct
3092 */
3093void perf_counter_init_task(struct task_struct *child)
3094{
3095 struct perf_counter_context *child_ctx, *parent_ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003096 struct perf_counter *counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003097 struct task_struct *parent = current;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003098
3099 child_ctx = &child->perf_counter_ctx;
3100 parent_ctx = &parent->perf_counter_ctx;
3101
3102 __perf_counter_init_context(child_ctx, child);
3103
3104 /*
3105 * This is executed from the parent task context, so inherit
3106 * counters that have been marked for cloning:
3107 */
3108
3109 if (likely(!parent_ctx->nr_counters))
3110 return;
3111
3112 /*
3113 * Lock the parent list. No need to lock the child - not PID
3114 * hashed yet and not running, so nobody can access it.
3115 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11003116 mutex_lock(&parent_ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003117
3118 /*
3119 * We dont have to disable NMIs - we are only looking at
3120 * the list, not manipulating it:
3121 */
3122 list_for_each_entry(counter, &parent_ctx->counter_list, list_entry) {
Paul Mackerrasd859e292009-01-17 18:10:22 +11003123 if (!counter->hw_event.inherit)
Ingo Molnar9b51f662008-12-12 13:49:45 +01003124 continue;
3125
Paul Mackerrasd859e292009-01-17 18:10:22 +11003126 if (inherit_group(counter, parent,
Ingo Molnar9b51f662008-12-12 13:49:45 +01003127 parent_ctx, child, child_ctx))
3128 break;
3129 }
3130
Paul Mackerrasd859e292009-01-17 18:10:22 +11003131 mutex_unlock(&parent_ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003132}
3133
Ingo Molnar04289bb2008-12-11 08:38:42 +01003134static void __cpuinit perf_counter_init_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003135{
Ingo Molnar04289bb2008-12-11 08:38:42 +01003136 struct perf_cpu_context *cpuctx;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003137
Ingo Molnar04289bb2008-12-11 08:38:42 +01003138 cpuctx = &per_cpu(perf_cpu_context, cpu);
3139 __perf_counter_init_context(&cpuctx->ctx, NULL);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003140
3141 mutex_lock(&perf_resource_mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003142 cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003143 mutex_unlock(&perf_resource_mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003144
Paul Mackerras01d02872009-01-14 13:44:19 +11003145 hw_perf_counter_setup(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003146}
3147
3148#ifdef CONFIG_HOTPLUG_CPU
Ingo Molnar04289bb2008-12-11 08:38:42 +01003149static void __perf_counter_exit_cpu(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003150{
3151 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
3152 struct perf_counter_context *ctx = &cpuctx->ctx;
3153 struct perf_counter *counter, *tmp;
3154
Ingo Molnar04289bb2008-12-11 08:38:42 +01003155 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry)
3156 __perf_counter_remove_from_context(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003157}
Ingo Molnar04289bb2008-12-11 08:38:42 +01003158static void perf_counter_exit_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003159{
Paul Mackerrasd859e292009-01-17 18:10:22 +11003160 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
3161 struct perf_counter_context *ctx = &cpuctx->ctx;
3162
3163 mutex_lock(&ctx->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003164 smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003165 mutex_unlock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003166}
3167#else
Ingo Molnar04289bb2008-12-11 08:38:42 +01003168static inline void perf_counter_exit_cpu(int cpu) { }
Thomas Gleixner0793a612008-12-04 20:12:29 +01003169#endif
3170
3171static int __cpuinit
3172perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
3173{
3174 unsigned int cpu = (long)hcpu;
3175
3176 switch (action) {
3177
3178 case CPU_UP_PREPARE:
3179 case CPU_UP_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01003180 perf_counter_init_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003181 break;
3182
3183 case CPU_DOWN_PREPARE:
3184 case CPU_DOWN_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01003185 perf_counter_exit_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003186 break;
3187
3188 default:
3189 break;
3190 }
3191
3192 return NOTIFY_OK;
3193}
3194
3195static struct notifier_block __cpuinitdata perf_cpu_nb = {
3196 .notifier_call = perf_cpu_notify,
3197};
3198
3199static int __init perf_counter_init(void)
3200{
3201 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
3202 (void *)(long)smp_processor_id());
3203 register_cpu_notifier(&perf_cpu_nb);
3204
3205 return 0;
3206}
3207early_initcall(perf_counter_init);
3208
3209static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
3210{
3211 return sprintf(buf, "%d\n", perf_reserved_percpu);
3212}
3213
3214static ssize_t
3215perf_set_reserve_percpu(struct sysdev_class *class,
3216 const char *buf,
3217 size_t count)
3218{
3219 struct perf_cpu_context *cpuctx;
3220 unsigned long val;
3221 int err, cpu, mpt;
3222
3223 err = strict_strtoul(buf, 10, &val);
3224 if (err)
3225 return err;
3226 if (val > perf_max_counters)
3227 return -EINVAL;
3228
3229 mutex_lock(&perf_resource_mutex);
3230 perf_reserved_percpu = val;
3231 for_each_online_cpu(cpu) {
3232 cpuctx = &per_cpu(perf_cpu_context, cpu);
3233 spin_lock_irq(&cpuctx->ctx.lock);
3234 mpt = min(perf_max_counters - cpuctx->ctx.nr_counters,
3235 perf_max_counters - perf_reserved_percpu);
3236 cpuctx->max_pertask = mpt;
3237 spin_unlock_irq(&cpuctx->ctx.lock);
3238 }
3239 mutex_unlock(&perf_resource_mutex);
3240
3241 return count;
3242}
3243
3244static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
3245{
3246 return sprintf(buf, "%d\n", perf_overcommit);
3247}
3248
3249static ssize_t
3250perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
3251{
3252 unsigned long val;
3253 int err;
3254
3255 err = strict_strtoul(buf, 10, &val);
3256 if (err)
3257 return err;
3258 if (val > 1)
3259 return -EINVAL;
3260
3261 mutex_lock(&perf_resource_mutex);
3262 perf_overcommit = val;
3263 mutex_unlock(&perf_resource_mutex);
3264
3265 return count;
3266}
3267
3268static SYSDEV_CLASS_ATTR(
3269 reserve_percpu,
3270 0644,
3271 perf_show_reserve_percpu,
3272 perf_set_reserve_percpu
3273 );
3274
3275static SYSDEV_CLASS_ATTR(
3276 overcommit,
3277 0644,
3278 perf_show_overcommit,
3279 perf_set_overcommit
3280 );
3281
3282static struct attribute *perfclass_attrs[] = {
3283 &attr_reserve_percpu.attr,
3284 &attr_overcommit.attr,
3285 NULL
3286};
3287
3288static struct attribute_group perfclass_attr_group = {
3289 .attrs = perfclass_attrs,
3290 .name = "perf_counters",
3291};
3292
3293static int __init perf_counter_sysfs_init(void)
3294{
3295 return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
3296 &perfclass_attr_group);
3297}
3298device_initcall(perf_counter_sysfs_init);