blob: f4f7596f78414944bd2ea0676a09a8ac4041e6fd [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
41/*
42 * Mutex for (sysadmin-configurable) counter reservations:
43 */
44static DEFINE_MUTEX(perf_resource_mutex);
45
46/*
47 * Architecture provided APIs - weak aliases:
48 */
Ingo Molnar5c92d122008-12-11 13:21:10 +010049extern __weak const struct hw_perf_counter_ops *
Ingo Molnar621a01e2008-12-11 12:46:46 +010050hw_perf_counter_init(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +010051{
Paul Mackerrasff6f0542009-01-09 16:19:25 +110052 return NULL;
Thomas Gleixner0793a612008-12-04 20:12:29 +010053}
54
Ingo Molnar01b28382008-12-11 13:45:51 +010055u64 __weak hw_perf_save_disable(void) { return 0; }
Yinghai Lu01ea1cc2008-12-26 21:05:06 -080056void __weak hw_perf_restore(u64 ctrl) { barrier(); }
Paul Mackerras01d02872009-01-14 13:44:19 +110057void __weak hw_perf_counter_setup(int cpu) { barrier(); }
Paul Mackerras3cbed422009-01-09 16:43:42 +110058int __weak hw_perf_group_sched_in(struct perf_counter *group_leader,
59 struct perf_cpu_context *cpuctx,
60 struct perf_counter_context *ctx, int cpu)
61{
62 return 0;
63}
Thomas Gleixner0793a612008-12-04 20:12:29 +010064
Paul Mackerras4eb96fc2009-01-09 17:24:34 +110065void __weak perf_counter_print_debug(void) { }
66
Ingo Molnar04289bb2008-12-11 08:38:42 +010067static void
68list_add_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
69{
70 struct perf_counter *group_leader = counter->group_leader;
71
72 /*
73 * Depending on whether it is a standalone or sibling counter,
74 * add it straight to the context's counter list, or to the group
75 * leader's sibling list:
76 */
77 if (counter->group_leader == counter)
78 list_add_tail(&counter->list_entry, &ctx->counter_list);
Peter Zijlstra5c148192009-03-25 12:30:23 +010079 else {
Ingo Molnar04289bb2008-12-11 08:38:42 +010080 list_add_tail(&counter->list_entry, &group_leader->sibling_list);
Peter Zijlstra5c148192009-03-25 12:30:23 +010081 group_leader->nr_siblings++;
82 }
Peter Zijlstra592903c2009-03-13 12:21:36 +010083
84 list_add_rcu(&counter->event_entry, &ctx->event_list);
Ingo Molnar04289bb2008-12-11 08:38:42 +010085}
86
87static void
88list_del_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
89{
90 struct perf_counter *sibling, *tmp;
91
92 list_del_init(&counter->list_entry);
Peter Zijlstra592903c2009-03-13 12:21:36 +010093 list_del_rcu(&counter->event_entry);
Ingo Molnar04289bb2008-12-11 08:38:42 +010094
Peter Zijlstra5c148192009-03-25 12:30:23 +010095 if (counter->group_leader != counter)
96 counter->group_leader->nr_siblings--;
97
Ingo Molnar04289bb2008-12-11 08:38:42 +010098 /*
99 * If this was a group counter with sibling counters then
100 * upgrade the siblings to singleton counters by adding them
101 * to the context list directly:
102 */
103 list_for_each_entry_safe(sibling, tmp,
104 &counter->sibling_list, list_entry) {
105
Peter Zijlstra75564232009-03-13 12:21:29 +0100106 list_move_tail(&sibling->list_entry, &ctx->counter_list);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100107 sibling->group_leader = sibling;
108 }
109}
110
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100111static void
112counter_sched_out(struct perf_counter *counter,
113 struct perf_cpu_context *cpuctx,
114 struct perf_counter_context *ctx)
115{
116 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
117 return;
118
119 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200120 counter->tstamp_stopped = ctx->time;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100121 counter->hw_ops->disable(counter);
122 counter->oncpu = -1;
123
124 if (!is_software_counter(counter))
125 cpuctx->active_oncpu--;
126 ctx->nr_active--;
127 if (counter->hw_event.exclusive || !cpuctx->active_oncpu)
128 cpuctx->exclusive = 0;
129}
130
Paul Mackerrasd859e292009-01-17 18:10:22 +1100131static void
132group_sched_out(struct perf_counter *group_counter,
133 struct perf_cpu_context *cpuctx,
134 struct perf_counter_context *ctx)
135{
136 struct perf_counter *counter;
137
138 if (group_counter->state != PERF_COUNTER_STATE_ACTIVE)
139 return;
140
141 counter_sched_out(group_counter, cpuctx, ctx);
142
143 /*
144 * Schedule out siblings (if any):
145 */
146 list_for_each_entry(counter, &group_counter->sibling_list, list_entry)
147 counter_sched_out(counter, cpuctx, ctx);
148
149 if (group_counter->hw_event.exclusive)
150 cpuctx->exclusive = 0;
151}
152
Thomas Gleixner0793a612008-12-04 20:12:29 +0100153/*
154 * Cross CPU call to remove a performance counter
155 *
156 * We disable the counter on the hardware level first. After that we
157 * remove it from the context list.
158 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100159static void __perf_counter_remove_from_context(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100160{
161 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
162 struct perf_counter *counter = info;
163 struct perf_counter_context *ctx = counter->ctx;
Ingo Molnar9b51f662008-12-12 13:49:45 +0100164 unsigned long flags;
Ingo Molnar5c92d122008-12-11 13:21:10 +0100165 u64 perf_flags;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100166
167 /*
168 * If this is a task context, we need to check whether it is
169 * the current task context of this cpu. If not it has been
170 * scheduled out before the smp call arrived.
171 */
172 if (ctx->task && cpuctx->task_ctx != ctx)
173 return;
174
Peter Zijlstra849691a2009-04-06 11:45:12 +0200175 spin_lock_irqsave(&ctx->lock, flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100176
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100177 counter_sched_out(counter, cpuctx, ctx);
178
179 counter->task = NULL;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100180 ctx->nr_counters--;
181
182 /*
183 * Protect the list operation against NMI by disabling the
184 * counters on a global level. NOP for non NMI based counters.
185 */
Ingo Molnar01b28382008-12-11 13:45:51 +0100186 perf_flags = hw_perf_save_disable();
Ingo Molnar04289bb2008-12-11 08:38:42 +0100187 list_del_counter(counter, ctx);
Ingo Molnar01b28382008-12-11 13:45:51 +0100188 hw_perf_restore(perf_flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100189
190 if (!ctx->task) {
191 /*
192 * Allow more per task counters with respect to the
193 * reservation:
194 */
195 cpuctx->max_pertask =
196 min(perf_max_counters - ctx->nr_counters,
197 perf_max_counters - perf_reserved_percpu);
198 }
199
Peter Zijlstra849691a2009-04-06 11:45:12 +0200200 spin_unlock_irqrestore(&ctx->lock, flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100201}
202
203
204/*
205 * Remove the counter from a task's (or a CPU's) list of counters.
206 *
Paul Mackerrasd859e292009-01-17 18:10:22 +1100207 * Must be called with counter->mutex and ctx->mutex held.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100208 *
209 * CPU counters are removed with a smp call. For task counters we only
210 * call when the task is on a CPU.
211 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100212static void perf_counter_remove_from_context(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100213{
214 struct perf_counter_context *ctx = counter->ctx;
215 struct task_struct *task = ctx->task;
216
217 if (!task) {
218 /*
219 * Per cpu counters are removed via an smp call and
220 * the removal is always sucessful.
221 */
222 smp_call_function_single(counter->cpu,
Ingo Molnar04289bb2008-12-11 08:38:42 +0100223 __perf_counter_remove_from_context,
Thomas Gleixner0793a612008-12-04 20:12:29 +0100224 counter, 1);
225 return;
226 }
227
228retry:
Ingo Molnar04289bb2008-12-11 08:38:42 +0100229 task_oncpu_function_call(task, __perf_counter_remove_from_context,
Thomas Gleixner0793a612008-12-04 20:12:29 +0100230 counter);
231
232 spin_lock_irq(&ctx->lock);
233 /*
234 * If the context is active we need to retry the smp call.
235 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100236 if (ctx->nr_active && !list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100237 spin_unlock_irq(&ctx->lock);
238 goto retry;
239 }
240
241 /*
242 * The lock prevents that this context is scheduled in so we
Ingo Molnar04289bb2008-12-11 08:38:42 +0100243 * can remove the counter safely, if the call above did not
Thomas Gleixner0793a612008-12-04 20:12:29 +0100244 * succeed.
245 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100246 if (!list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100247 ctx->nr_counters--;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100248 list_del_counter(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100249 counter->task = NULL;
250 }
251 spin_unlock_irq(&ctx->lock);
252}
253
Peter Zijlstra4af49982009-04-06 11:45:10 +0200254static inline u64 perf_clock(void)
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100255{
Peter Zijlstra4af49982009-04-06 11:45:10 +0200256 return cpu_clock(smp_processor_id());
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100257}
258
259/*
260 * Update the record of the current time in a context.
261 */
Peter Zijlstra4af49982009-04-06 11:45:10 +0200262static void update_context_time(struct perf_counter_context *ctx)
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100263{
Peter Zijlstra4af49982009-04-06 11:45:10 +0200264 u64 now = perf_clock();
265
266 ctx->time += now - ctx->timestamp;
267 ctx->timestamp = now;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100268}
269
270/*
271 * Update the total_time_enabled and total_time_running fields for a counter.
272 */
273static void update_counter_times(struct perf_counter *counter)
274{
275 struct perf_counter_context *ctx = counter->ctx;
276 u64 run_end;
277
Peter Zijlstra4af49982009-04-06 11:45:10 +0200278 if (counter->state < PERF_COUNTER_STATE_INACTIVE)
279 return;
280
281 counter->total_time_enabled = ctx->time - counter->tstamp_enabled;
282
283 if (counter->state == PERF_COUNTER_STATE_INACTIVE)
284 run_end = counter->tstamp_stopped;
285 else
286 run_end = ctx->time;
287
288 counter->total_time_running = run_end - counter->tstamp_running;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100289}
290
291/*
292 * Update total_time_enabled and total_time_running for all counters in a group.
293 */
294static void update_group_times(struct perf_counter *leader)
295{
296 struct perf_counter *counter;
297
298 update_counter_times(leader);
299 list_for_each_entry(counter, &leader->sibling_list, list_entry)
300 update_counter_times(counter);
301}
302
303/*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100304 * Cross CPU call to disable a performance counter
305 */
306static void __perf_counter_disable(void *info)
307{
308 struct perf_counter *counter = info;
309 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
310 struct perf_counter_context *ctx = counter->ctx;
311 unsigned long flags;
312
313 /*
314 * If this is a per-task counter, need to check whether this
315 * counter's task is the current task on this cpu.
316 */
317 if (ctx->task && cpuctx->task_ctx != ctx)
318 return;
319
Peter Zijlstra849691a2009-04-06 11:45:12 +0200320 spin_lock_irqsave(&ctx->lock, flags);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100321
322 /*
323 * If the counter is on, turn it off.
324 * If it is in error state, leave it in error state.
325 */
326 if (counter->state >= PERF_COUNTER_STATE_INACTIVE) {
Peter Zijlstra4af49982009-04-06 11:45:10 +0200327 update_context_time(ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100328 update_counter_times(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100329 if (counter == counter->group_leader)
330 group_sched_out(counter, cpuctx, ctx);
331 else
332 counter_sched_out(counter, cpuctx, ctx);
333 counter->state = PERF_COUNTER_STATE_OFF;
334 }
335
Peter Zijlstra849691a2009-04-06 11:45:12 +0200336 spin_unlock_irqrestore(&ctx->lock, flags);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100337}
338
339/*
340 * Disable a counter.
341 */
342static void perf_counter_disable(struct perf_counter *counter)
343{
344 struct perf_counter_context *ctx = counter->ctx;
345 struct task_struct *task = ctx->task;
346
347 if (!task) {
348 /*
349 * Disable the counter on the cpu that it's on
350 */
351 smp_call_function_single(counter->cpu, __perf_counter_disable,
352 counter, 1);
353 return;
354 }
355
356 retry:
357 task_oncpu_function_call(task, __perf_counter_disable, counter);
358
359 spin_lock_irq(&ctx->lock);
360 /*
361 * If the counter is still active, we need to retry the cross-call.
362 */
363 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
364 spin_unlock_irq(&ctx->lock);
365 goto retry;
366 }
367
368 /*
369 * Since we have the lock this context can't be scheduled
370 * in, so we can change the state safely.
371 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100372 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
373 update_counter_times(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100374 counter->state = PERF_COUNTER_STATE_OFF;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100375 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100376
377 spin_unlock_irq(&ctx->lock);
378}
379
380/*
381 * Disable a counter and all its children.
382 */
383static void perf_counter_disable_family(struct perf_counter *counter)
384{
385 struct perf_counter *child;
386
387 perf_counter_disable(counter);
388
389 /*
390 * Lock the mutex to protect the list of children
391 */
392 mutex_lock(&counter->mutex);
393 list_for_each_entry(child, &counter->child_list, child_list)
394 perf_counter_disable(child);
395 mutex_unlock(&counter->mutex);
396}
397
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100398static int
399counter_sched_in(struct perf_counter *counter,
400 struct perf_cpu_context *cpuctx,
401 struct perf_counter_context *ctx,
402 int cpu)
403{
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100404 if (counter->state <= PERF_COUNTER_STATE_OFF)
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100405 return 0;
406
407 counter->state = PERF_COUNTER_STATE_ACTIVE;
408 counter->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */
409 /*
410 * The new state must be visible before we turn it on in the hardware:
411 */
412 smp_wmb();
413
414 if (counter->hw_ops->enable(counter)) {
415 counter->state = PERF_COUNTER_STATE_INACTIVE;
416 counter->oncpu = -1;
417 return -EAGAIN;
418 }
419
Peter Zijlstra4af49982009-04-06 11:45:10 +0200420 counter->tstamp_running += ctx->time - counter->tstamp_stopped;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100421
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100422 if (!is_software_counter(counter))
423 cpuctx->active_oncpu++;
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100424 ctx->nr_active++;
425
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100426 if (counter->hw_event.exclusive)
427 cpuctx->exclusive = 1;
428
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100429 return 0;
430}
431
Thomas Gleixner0793a612008-12-04 20:12:29 +0100432/*
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100433 * Return 1 for a group consisting entirely of software counters,
434 * 0 if the group contains any hardware counters.
435 */
436static int is_software_only_group(struct perf_counter *leader)
437{
438 struct perf_counter *counter;
439
440 if (!is_software_counter(leader))
441 return 0;
Peter Zijlstra5c148192009-03-25 12:30:23 +0100442
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100443 list_for_each_entry(counter, &leader->sibling_list, list_entry)
444 if (!is_software_counter(counter))
445 return 0;
Peter Zijlstra5c148192009-03-25 12:30:23 +0100446
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100447 return 1;
448}
449
450/*
451 * Work out whether we can put this counter group on the CPU now.
452 */
453static int group_can_go_on(struct perf_counter *counter,
454 struct perf_cpu_context *cpuctx,
455 int can_add_hw)
456{
457 /*
458 * Groups consisting entirely of software counters can always go on.
459 */
460 if (is_software_only_group(counter))
461 return 1;
462 /*
463 * If an exclusive group is already on, no other hardware
464 * counters can go on.
465 */
466 if (cpuctx->exclusive)
467 return 0;
468 /*
469 * If this group is exclusive and there are already
470 * counters on the CPU, it can't go on.
471 */
472 if (counter->hw_event.exclusive && cpuctx->active_oncpu)
473 return 0;
474 /*
475 * Otherwise, try to add it if all previous groups were able
476 * to go on.
477 */
478 return can_add_hw;
479}
480
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100481static void add_counter_to_ctx(struct perf_counter *counter,
482 struct perf_counter_context *ctx)
483{
484 list_add_counter(counter, ctx);
485 ctx->nr_counters++;
486 counter->prev_state = PERF_COUNTER_STATE_OFF;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200487 counter->tstamp_enabled = ctx->time;
488 counter->tstamp_running = ctx->time;
489 counter->tstamp_stopped = ctx->time;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100490}
491
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100492/*
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100493 * Cross CPU call to install and enable a performance counter
Thomas Gleixner0793a612008-12-04 20:12:29 +0100494 */
495static void __perf_install_in_context(void *info)
496{
497 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
498 struct perf_counter *counter = info;
499 struct perf_counter_context *ctx = counter->ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100500 struct perf_counter *leader = counter->group_leader;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100501 int cpu = smp_processor_id();
Ingo Molnar9b51f662008-12-12 13:49:45 +0100502 unsigned long flags;
Ingo Molnar5c92d122008-12-11 13:21:10 +0100503 u64 perf_flags;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100504 int err;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100505
506 /*
507 * If this is a task context, we need to check whether it is
508 * the current task context of this cpu. If not it has been
509 * scheduled out before the smp call arrived.
510 */
511 if (ctx->task && cpuctx->task_ctx != ctx)
512 return;
513
Peter Zijlstra849691a2009-04-06 11:45:12 +0200514 spin_lock_irqsave(&ctx->lock, flags);
Peter Zijlstra4af49982009-04-06 11:45:10 +0200515 update_context_time(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100516
517 /*
518 * Protect the list operation against NMI by disabling the
519 * counters on a global level. NOP for non NMI based counters.
520 */
Ingo Molnar01b28382008-12-11 13:45:51 +0100521 perf_flags = hw_perf_save_disable();
Thomas Gleixner0793a612008-12-04 20:12:29 +0100522
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100523 add_counter_to_ctx(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100524
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100525 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100526 * Don't put the counter on if it is disabled or if
527 * it is in a group and the group isn't on.
528 */
529 if (counter->state != PERF_COUNTER_STATE_INACTIVE ||
530 (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE))
531 goto unlock;
532
533 /*
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100534 * An exclusive counter can't go on if there are already active
535 * hardware counters, and no hardware counter can go on if there
536 * is already an exclusive counter on.
537 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100538 if (!group_can_go_on(counter, cpuctx, 1))
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100539 err = -EEXIST;
540 else
541 err = counter_sched_in(counter, cpuctx, ctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100542
Paul Mackerrasd859e292009-01-17 18:10:22 +1100543 if (err) {
544 /*
545 * This counter couldn't go on. If it is in a group
546 * then we have to pull the whole group off.
547 * If the counter group is pinned then put it in error state.
548 */
549 if (leader != counter)
550 group_sched_out(leader, cpuctx, ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100551 if (leader->hw_event.pinned) {
552 update_group_times(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100553 leader->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100554 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100555 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100556
557 if (!err && !ctx->task && cpuctx->max_pertask)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100558 cpuctx->max_pertask--;
559
Paul Mackerrasd859e292009-01-17 18:10:22 +1100560 unlock:
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100561 hw_perf_restore(perf_flags);
562
Peter Zijlstra849691a2009-04-06 11:45:12 +0200563 spin_unlock_irqrestore(&ctx->lock, flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100564}
565
566/*
567 * Attach a performance counter to a context
568 *
569 * First we add the counter to the list with the hardware enable bit
570 * in counter->hw_config cleared.
571 *
572 * If the counter is attached to a task which is on a CPU we use a smp
573 * call to enable it in the task context. The task might have been
574 * scheduled away, but we check this in the smp call again.
Paul Mackerrasd859e292009-01-17 18:10:22 +1100575 *
576 * Must be called with ctx->mutex held.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100577 */
578static void
579perf_install_in_context(struct perf_counter_context *ctx,
580 struct perf_counter *counter,
581 int cpu)
582{
583 struct task_struct *task = ctx->task;
584
Thomas Gleixner0793a612008-12-04 20:12:29 +0100585 if (!task) {
586 /*
587 * Per cpu counters are installed via an smp call and
588 * the install is always sucessful.
589 */
590 smp_call_function_single(cpu, __perf_install_in_context,
591 counter, 1);
592 return;
593 }
594
595 counter->task = task;
596retry:
597 task_oncpu_function_call(task, __perf_install_in_context,
598 counter);
599
600 spin_lock_irq(&ctx->lock);
601 /*
Thomas Gleixner0793a612008-12-04 20:12:29 +0100602 * we need to retry the smp call.
603 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100604 if (ctx->is_active && list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100605 spin_unlock_irq(&ctx->lock);
606 goto retry;
607 }
608
609 /*
610 * The lock prevents that this context is scheduled in so we
611 * can add the counter safely, if it the call above did not
612 * succeed.
613 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100614 if (list_empty(&counter->list_entry))
615 add_counter_to_ctx(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100616 spin_unlock_irq(&ctx->lock);
617}
618
Paul Mackerrasd859e292009-01-17 18:10:22 +1100619/*
620 * Cross CPU call to enable a performance counter
621 */
622static void __perf_counter_enable(void *info)
Ingo Molnar04289bb2008-12-11 08:38:42 +0100623{
Paul Mackerrasd859e292009-01-17 18:10:22 +1100624 struct perf_counter *counter = info;
625 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
626 struct perf_counter_context *ctx = counter->ctx;
627 struct perf_counter *leader = counter->group_leader;
628 unsigned long flags;
629 int err;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100630
631 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100632 * If this is a per-task counter, need to check whether this
633 * counter's task is the current task on this cpu.
Ingo Molnar04289bb2008-12-11 08:38:42 +0100634 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100635 if (ctx->task && cpuctx->task_ctx != ctx)
636 return;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100637
Peter Zijlstra849691a2009-04-06 11:45:12 +0200638 spin_lock_irqsave(&ctx->lock, flags);
Peter Zijlstra4af49982009-04-06 11:45:10 +0200639 update_context_time(ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100640
Paul Mackerrasc07c99b2009-02-13 22:10:34 +1100641 counter->prev_state = counter->state;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100642 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
643 goto unlock;
644 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200645 counter->tstamp_enabled = ctx->time - counter->total_time_enabled;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100646
647 /*
648 * If the counter is in a group and isn't the group leader,
649 * then don't put it on unless the group is on.
650 */
651 if (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE)
652 goto unlock;
653
654 if (!group_can_go_on(counter, cpuctx, 1))
655 err = -EEXIST;
656 else
657 err = counter_sched_in(counter, cpuctx, ctx,
658 smp_processor_id());
659
660 if (err) {
661 /*
662 * If this counter can't go on and it's part of a
663 * group, then the whole group has to come off.
664 */
665 if (leader != counter)
666 group_sched_out(leader, cpuctx, ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100667 if (leader->hw_event.pinned) {
668 update_group_times(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100669 leader->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100670 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100671 }
672
673 unlock:
Peter Zijlstra849691a2009-04-06 11:45:12 +0200674 spin_unlock_irqrestore(&ctx->lock, flags);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100675}
676
677/*
678 * Enable a counter.
679 */
680static void perf_counter_enable(struct perf_counter *counter)
681{
682 struct perf_counter_context *ctx = counter->ctx;
683 struct task_struct *task = ctx->task;
684
685 if (!task) {
686 /*
687 * Enable the counter on the cpu that it's on
688 */
689 smp_call_function_single(counter->cpu, __perf_counter_enable,
690 counter, 1);
691 return;
692 }
693
694 spin_lock_irq(&ctx->lock);
695 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
696 goto out;
697
698 /*
699 * If the counter is in error state, clear that first.
700 * That way, if we see the counter in error state below, we
701 * know that it has gone back into error state, as distinct
702 * from the task having been scheduled away before the
703 * cross-call arrived.
704 */
705 if (counter->state == PERF_COUNTER_STATE_ERROR)
706 counter->state = PERF_COUNTER_STATE_OFF;
707
708 retry:
709 spin_unlock_irq(&ctx->lock);
710 task_oncpu_function_call(task, __perf_counter_enable, counter);
711
712 spin_lock_irq(&ctx->lock);
713
714 /*
715 * If the context is active and the counter is still off,
716 * we need to retry the cross-call.
717 */
718 if (ctx->is_active && counter->state == PERF_COUNTER_STATE_OFF)
719 goto retry;
720
721 /*
722 * Since we have the lock this context can't be scheduled
723 * in, so we can change the state safely.
724 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100725 if (counter->state == PERF_COUNTER_STATE_OFF) {
Paul Mackerrasd859e292009-01-17 18:10:22 +1100726 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200727 counter->tstamp_enabled =
728 ctx->time - counter->total_time_enabled;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100729 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100730 out:
731 spin_unlock_irq(&ctx->lock);
732}
733
Peter Zijlstra79f14642009-04-06 11:45:07 +0200734static void perf_counter_refresh(struct perf_counter *counter, int refresh)
735{
736 atomic_add(refresh, &counter->event_limit);
737 perf_counter_enable(counter);
738}
739
Paul Mackerrasd859e292009-01-17 18:10:22 +1100740/*
741 * Enable a counter and all its children.
742 */
743static void perf_counter_enable_family(struct perf_counter *counter)
744{
745 struct perf_counter *child;
746
747 perf_counter_enable(counter);
748
749 /*
750 * Lock the mutex to protect the list of children
751 */
752 mutex_lock(&counter->mutex);
753 list_for_each_entry(child, &counter->child_list, child_list)
754 perf_counter_enable(child);
755 mutex_unlock(&counter->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100756}
757
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100758void __perf_counter_sched_out(struct perf_counter_context *ctx,
759 struct perf_cpu_context *cpuctx)
760{
761 struct perf_counter *counter;
Paul Mackerras3cbed422009-01-09 16:43:42 +1100762 u64 flags;
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100763
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100764 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100765 ctx->is_active = 0;
766 if (likely(!ctx->nr_counters))
767 goto out;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200768 update_context_time(ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100769
Paul Mackerras3cbed422009-01-09 16:43:42 +1100770 flags = hw_perf_save_disable();
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100771 if (ctx->nr_active) {
772 list_for_each_entry(counter, &ctx->counter_list, list_entry)
773 group_sched_out(counter, cpuctx, ctx);
774 }
Paul Mackerras3cbed422009-01-09 16:43:42 +1100775 hw_perf_restore(flags);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100776 out:
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100777 spin_unlock(&ctx->lock);
778}
779
Thomas Gleixner0793a612008-12-04 20:12:29 +0100780/*
781 * Called from scheduler to remove the counters of the current task,
782 * with interrupts disabled.
783 *
784 * We stop each counter and update the counter value in counter->count.
785 *
Ingo Molnar76715812008-12-17 14:20:28 +0100786 * This does not protect us against NMI, but disable()
Thomas Gleixner0793a612008-12-04 20:12:29 +0100787 * sets the disabled bit in the control field of counter _before_
788 * accessing the counter control register. If a NMI hits, then it will
789 * not restart the counter.
790 */
791void perf_counter_task_sched_out(struct task_struct *task, int cpu)
792{
793 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
794 struct perf_counter_context *ctx = &task->perf_counter_ctx;
Peter Zijlstra4a0deca2009-03-19 20:26:12 +0100795 struct pt_regs *regs;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100796
797 if (likely(!cpuctx->task_ctx))
798 return;
799
Peter Zijlstra4a0deca2009-03-19 20:26:12 +0100800 regs = task_pt_regs(task);
801 perf_swcounter_event(PERF_COUNT_CONTEXT_SWITCHES, 1, 1, regs);
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100802 __perf_counter_sched_out(ctx, cpuctx);
803
Thomas Gleixner0793a612008-12-04 20:12:29 +0100804 cpuctx->task_ctx = NULL;
805}
806
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100807static void perf_counter_cpu_sched_out(struct perf_cpu_context *cpuctx)
Ingo Molnar04289bb2008-12-11 08:38:42 +0100808{
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100809 __perf_counter_sched_out(&cpuctx->ctx, cpuctx);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100810}
811
Ingo Molnar79958882008-12-17 08:54:56 +0100812static int
Ingo Molnar04289bb2008-12-11 08:38:42 +0100813group_sched_in(struct perf_counter *group_counter,
814 struct perf_cpu_context *cpuctx,
815 struct perf_counter_context *ctx,
816 int cpu)
817{
Ingo Molnar95cdd2e2008-12-21 13:50:42 +0100818 struct perf_counter *counter, *partial_group;
Paul Mackerras3cbed422009-01-09 16:43:42 +1100819 int ret;
820
821 if (group_counter->state == PERF_COUNTER_STATE_OFF)
822 return 0;
823
824 ret = hw_perf_group_sched_in(group_counter, cpuctx, ctx, cpu);
825 if (ret)
826 return ret < 0 ? ret : 0;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100827
Paul Mackerrasc07c99b2009-02-13 22:10:34 +1100828 group_counter->prev_state = group_counter->state;
Ingo Molnar95cdd2e2008-12-21 13:50:42 +0100829 if (counter_sched_in(group_counter, cpuctx, ctx, cpu))
830 return -EAGAIN;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100831
832 /*
833 * Schedule in siblings as one group (if any):
834 */
Ingo Molnar79958882008-12-17 08:54:56 +0100835 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
Paul Mackerrasc07c99b2009-02-13 22:10:34 +1100836 counter->prev_state = counter->state;
Ingo Molnar95cdd2e2008-12-21 13:50:42 +0100837 if (counter_sched_in(counter, cpuctx, ctx, cpu)) {
838 partial_group = counter;
839 goto group_error;
840 }
Ingo Molnar79958882008-12-17 08:54:56 +0100841 }
842
Paul Mackerras3cbed422009-01-09 16:43:42 +1100843 return 0;
Ingo Molnar95cdd2e2008-12-21 13:50:42 +0100844
845group_error:
846 /*
847 * Groups can be scheduled in as one unit only, so undo any
848 * partial group before returning:
849 */
850 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
851 if (counter == partial_group)
852 break;
853 counter_sched_out(counter, cpuctx, ctx);
854 }
855 counter_sched_out(group_counter, cpuctx, ctx);
856
857 return -EAGAIN;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100858}
859
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100860static void
861__perf_counter_sched_in(struct perf_counter_context *ctx,
862 struct perf_cpu_context *cpuctx, int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100863{
Thomas Gleixner0793a612008-12-04 20:12:29 +0100864 struct perf_counter *counter;
Paul Mackerras3cbed422009-01-09 16:43:42 +1100865 u64 flags;
Paul Mackerrasdd0e6ba2009-01-12 15:11:00 +1100866 int can_add_hw = 1;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100867
Thomas Gleixner0793a612008-12-04 20:12:29 +0100868 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100869 ctx->is_active = 1;
870 if (likely(!ctx->nr_counters))
871 goto out;
872
Peter Zijlstra4af49982009-04-06 11:45:10 +0200873 ctx->timestamp = perf_clock();
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100874
Paul Mackerras3cbed422009-01-09 16:43:42 +1100875 flags = hw_perf_save_disable();
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100876
877 /*
878 * First go through the list and put on any pinned groups
879 * in order to give them the best chance of going on.
880 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100881 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100882 if (counter->state <= PERF_COUNTER_STATE_OFF ||
883 !counter->hw_event.pinned)
884 continue;
885 if (counter->cpu != -1 && counter->cpu != cpu)
886 continue;
887
888 if (group_can_go_on(counter, cpuctx, 1))
889 group_sched_in(counter, cpuctx, ctx, cpu);
890
891 /*
892 * If this pinned group hasn't been scheduled,
893 * put it in error state.
894 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100895 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
896 update_group_times(counter);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100897 counter->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100898 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100899 }
900
901 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
902 /*
903 * Ignore counters in OFF or ERROR state, and
904 * ignore pinned counters since we did them already.
905 */
906 if (counter->state <= PERF_COUNTER_STATE_OFF ||
907 counter->hw_event.pinned)
908 continue;
909
Ingo Molnar04289bb2008-12-11 08:38:42 +0100910 /*
911 * Listen to the 'cpu' scheduling filter constraint
912 * of counters:
913 */
Thomas Gleixner0793a612008-12-04 20:12:29 +0100914 if (counter->cpu != -1 && counter->cpu != cpu)
915 continue;
916
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100917 if (group_can_go_on(counter, cpuctx, can_add_hw)) {
Paul Mackerrasdd0e6ba2009-01-12 15:11:00 +1100918 if (group_sched_in(counter, cpuctx, ctx, cpu))
919 can_add_hw = 0;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100920 }
Thomas Gleixner0793a612008-12-04 20:12:29 +0100921 }
Paul Mackerras3cbed422009-01-09 16:43:42 +1100922 hw_perf_restore(flags);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100923 out:
Thomas Gleixner0793a612008-12-04 20:12:29 +0100924 spin_unlock(&ctx->lock);
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100925}
Ingo Molnar04289bb2008-12-11 08:38:42 +0100926
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100927/*
928 * Called from scheduler to add the counters of the current task
929 * with interrupts disabled.
930 *
931 * We restore the counter value and then enable it.
932 *
933 * This does not protect us against NMI, but enable()
934 * sets the enabled bit in the control field of counter _before_
935 * accessing the counter control register. If a NMI hits, then it will
936 * keep the counter running.
937 */
938void perf_counter_task_sched_in(struct task_struct *task, int cpu)
939{
940 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
941 struct perf_counter_context *ctx = &task->perf_counter_ctx;
942
943 __perf_counter_sched_in(ctx, cpuctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100944 cpuctx->task_ctx = ctx;
945}
946
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100947static void perf_counter_cpu_sched_in(struct perf_cpu_context *cpuctx, int cpu)
948{
949 struct perf_counter_context *ctx = &cpuctx->ctx;
950
951 __perf_counter_sched_in(ctx, cpuctx, cpu);
952}
953
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100954int perf_counter_task_disable(void)
955{
956 struct task_struct *curr = current;
957 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
958 struct perf_counter *counter;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100959 unsigned long flags;
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100960 u64 perf_flags;
961 int cpu;
962
963 if (likely(!ctx->nr_counters))
964 return 0;
965
Peter Zijlstra849691a2009-04-06 11:45:12 +0200966 local_irq_save(flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100967 cpu = smp_processor_id();
968
969 perf_counter_task_sched_out(curr, cpu);
970
971 spin_lock(&ctx->lock);
972
973 /*
974 * Disable all the counters:
975 */
976 perf_flags = hw_perf_save_disable();
977
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100978 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100979 if (counter->state != PERF_COUNTER_STATE_ERROR) {
980 update_group_times(counter);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100981 counter->state = PERF_COUNTER_STATE_OFF;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100982 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100983 }
Ingo Molnar9b51f662008-12-12 13:49:45 +0100984
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100985 hw_perf_restore(perf_flags);
986
Peter Zijlstra849691a2009-04-06 11:45:12 +0200987 spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100988
989 return 0;
990}
991
992int perf_counter_task_enable(void)
993{
994 struct task_struct *curr = current;
995 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
996 struct perf_counter *counter;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100997 unsigned long flags;
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100998 u64 perf_flags;
999 int cpu;
1000
1001 if (likely(!ctx->nr_counters))
1002 return 0;
1003
Peter Zijlstra849691a2009-04-06 11:45:12 +02001004 local_irq_save(flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001005 cpu = smp_processor_id();
1006
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001007 perf_counter_task_sched_out(curr, cpu);
1008
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001009 spin_lock(&ctx->lock);
1010
1011 /*
1012 * Disable all the counters:
1013 */
1014 perf_flags = hw_perf_save_disable();
1015
1016 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001017 if (counter->state > PERF_COUNTER_STATE_OFF)
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001018 continue;
Ingo Molnar6a930702008-12-11 15:17:03 +01001019 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +02001020 counter->tstamp_enabled =
1021 ctx->time - counter->total_time_enabled;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001022 counter->hw_event.disabled = 0;
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001023 }
1024 hw_perf_restore(perf_flags);
1025
1026 spin_unlock(&ctx->lock);
1027
1028 perf_counter_task_sched_in(curr, cpu);
1029
Peter Zijlstra849691a2009-04-06 11:45:12 +02001030 local_irq_restore(flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001031
1032 return 0;
1033}
1034
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001035/*
1036 * Round-robin a context's counters:
1037 */
1038static void rotate_ctx(struct perf_counter_context *ctx)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001039{
Thomas Gleixner0793a612008-12-04 20:12:29 +01001040 struct perf_counter *counter;
Ingo Molnar5c92d122008-12-11 13:21:10 +01001041 u64 perf_flags;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001042
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001043 if (!ctx->nr_counters)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001044 return;
1045
Thomas Gleixner0793a612008-12-04 20:12:29 +01001046 spin_lock(&ctx->lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001047 /*
Ingo Molnar04289bb2008-12-11 08:38:42 +01001048 * Rotate the first entry last (works just fine for group counters too):
Thomas Gleixner0793a612008-12-04 20:12:29 +01001049 */
Ingo Molnar01b28382008-12-11 13:45:51 +01001050 perf_flags = hw_perf_save_disable();
Ingo Molnar04289bb2008-12-11 08:38:42 +01001051 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Peter Zijlstra75564232009-03-13 12:21:29 +01001052 list_move_tail(&counter->list_entry, &ctx->counter_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001053 break;
1054 }
Ingo Molnar01b28382008-12-11 13:45:51 +01001055 hw_perf_restore(perf_flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001056
1057 spin_unlock(&ctx->lock);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001058}
Thomas Gleixner0793a612008-12-04 20:12:29 +01001059
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001060void perf_counter_task_tick(struct task_struct *curr, int cpu)
1061{
1062 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
1063 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
1064 const int rotate_percpu = 0;
1065
1066 if (rotate_percpu)
1067 perf_counter_cpu_sched_out(cpuctx);
1068 perf_counter_task_sched_out(curr, cpu);
1069
1070 if (rotate_percpu)
1071 rotate_ctx(&cpuctx->ctx);
1072 rotate_ctx(ctx);
1073
1074 if (rotate_percpu)
1075 perf_counter_cpu_sched_in(cpuctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001076 perf_counter_task_sched_in(curr, cpu);
1077}
1078
1079/*
Thomas Gleixner0793a612008-12-04 20:12:29 +01001080 * Cross CPU call to read the hardware counter
1081 */
Ingo Molnar76715812008-12-17 14:20:28 +01001082static void __read(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001083{
Ingo Molnar621a01e2008-12-11 12:46:46 +01001084 struct perf_counter *counter = info;
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001085 struct perf_counter_context *ctx = counter->ctx;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001086 unsigned long flags;
Ingo Molnar621a01e2008-12-11 12:46:46 +01001087
Peter Zijlstra849691a2009-04-06 11:45:12 +02001088 local_irq_save(flags);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001089 if (ctx->is_active)
Peter Zijlstra4af49982009-04-06 11:45:10 +02001090 update_context_time(ctx);
Ingo Molnar76715812008-12-17 14:20:28 +01001091 counter->hw_ops->read(counter);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001092 update_counter_times(counter);
Peter Zijlstra849691a2009-04-06 11:45:12 +02001093 local_irq_restore(flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001094}
1095
Ingo Molnar04289bb2008-12-11 08:38:42 +01001096static u64 perf_counter_read(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001097{
1098 /*
1099 * If counter is enabled and currently active on a CPU, update the
1100 * value in the counter structure:
1101 */
Ingo Molnar6a930702008-12-11 15:17:03 +01001102 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
Thomas Gleixner0793a612008-12-04 20:12:29 +01001103 smp_call_function_single(counter->oncpu,
Ingo Molnar76715812008-12-17 14:20:28 +01001104 __read, counter, 1);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001105 } else if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
1106 update_counter_times(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001107 }
1108
Ingo Molnaree060942008-12-13 09:00:03 +01001109 return atomic64_read(&counter->count);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001110}
1111
Thomas Gleixner0793a612008-12-04 20:12:29 +01001112static void put_context(struct perf_counter_context *ctx)
1113{
1114 if (ctx->task)
1115 put_task_struct(ctx->task);
1116}
1117
1118static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
1119{
1120 struct perf_cpu_context *cpuctx;
1121 struct perf_counter_context *ctx;
1122 struct task_struct *task;
1123
1124 /*
1125 * If cpu is not a wildcard then this is a percpu counter:
1126 */
1127 if (cpu != -1) {
1128 /* Must be root to operate on a CPU counter: */
1129 if (!capable(CAP_SYS_ADMIN))
1130 return ERR_PTR(-EACCES);
1131
1132 if (cpu < 0 || cpu > num_possible_cpus())
1133 return ERR_PTR(-EINVAL);
1134
1135 /*
1136 * We could be clever and allow to attach a counter to an
1137 * offline CPU and activate it when the CPU comes up, but
1138 * that's for later.
1139 */
1140 if (!cpu_isset(cpu, cpu_online_map))
1141 return ERR_PTR(-ENODEV);
1142
1143 cpuctx = &per_cpu(perf_cpu_context, cpu);
1144 ctx = &cpuctx->ctx;
1145
Thomas Gleixner0793a612008-12-04 20:12:29 +01001146 return ctx;
1147 }
1148
1149 rcu_read_lock();
1150 if (!pid)
1151 task = current;
1152 else
1153 task = find_task_by_vpid(pid);
1154 if (task)
1155 get_task_struct(task);
1156 rcu_read_unlock();
1157
1158 if (!task)
1159 return ERR_PTR(-ESRCH);
1160
1161 ctx = &task->perf_counter_ctx;
1162 ctx->task = task;
1163
1164 /* Reuse ptrace permission checks for now. */
1165 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
1166 put_context(ctx);
1167 return ERR_PTR(-EACCES);
1168 }
1169
1170 return ctx;
1171}
1172
Peter Zijlstra592903c2009-03-13 12:21:36 +01001173static void free_counter_rcu(struct rcu_head *head)
1174{
1175 struct perf_counter *counter;
1176
1177 counter = container_of(head, struct perf_counter, rcu_head);
1178 kfree(counter);
1179}
1180
Peter Zijlstra925d5192009-03-30 19:07:02 +02001181static void perf_pending_sync(struct perf_counter *counter);
1182
Peter Zijlstraf1600952009-03-19 20:26:16 +01001183static void free_counter(struct perf_counter *counter)
1184{
Peter Zijlstra925d5192009-03-30 19:07:02 +02001185 perf_pending_sync(counter);
1186
Peter Zijlstrae077df42009-03-19 20:26:17 +01001187 if (counter->destroy)
1188 counter->destroy(counter);
1189
Peter Zijlstraf1600952009-03-19 20:26:16 +01001190 call_rcu(&counter->rcu_head, free_counter_rcu);
1191}
1192
Thomas Gleixner0793a612008-12-04 20:12:29 +01001193/*
1194 * Called when the last reference to the file is gone.
1195 */
1196static int perf_release(struct inode *inode, struct file *file)
1197{
1198 struct perf_counter *counter = file->private_data;
1199 struct perf_counter_context *ctx = counter->ctx;
1200
1201 file->private_data = NULL;
1202
Paul Mackerrasd859e292009-01-17 18:10:22 +11001203 mutex_lock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001204 mutex_lock(&counter->mutex);
1205
Ingo Molnar04289bb2008-12-11 08:38:42 +01001206 perf_counter_remove_from_context(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001207
1208 mutex_unlock(&counter->mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001209 mutex_unlock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001210
Peter Zijlstraf1600952009-03-19 20:26:16 +01001211 free_counter(counter);
Mike Galbraith5af75912009-02-11 10:53:37 +01001212 put_context(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001213
1214 return 0;
1215}
1216
1217/*
1218 * Read the performance counter - simple non blocking version for now
1219 */
1220static ssize_t
1221perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
1222{
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001223 u64 values[3];
1224 int n;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001225
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001226 /*
1227 * Return end-of-file for a read on a counter that is in
1228 * error state (i.e. because it was pinned but it couldn't be
1229 * scheduled on to the CPU at some point).
1230 */
1231 if (counter->state == PERF_COUNTER_STATE_ERROR)
1232 return 0;
1233
Thomas Gleixner0793a612008-12-04 20:12:29 +01001234 mutex_lock(&counter->mutex);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001235 values[0] = perf_counter_read(counter);
1236 n = 1;
1237 if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1238 values[n++] = counter->total_time_enabled +
1239 atomic64_read(&counter->child_total_time_enabled);
1240 if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1241 values[n++] = counter->total_time_running +
1242 atomic64_read(&counter->child_total_time_running);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001243 mutex_unlock(&counter->mutex);
1244
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001245 if (count < n * sizeof(u64))
1246 return -EINVAL;
1247 count = n * sizeof(u64);
1248
1249 if (copy_to_user(buf, values, count))
1250 return -EFAULT;
1251
1252 return count;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001253}
1254
1255static ssize_t
Thomas Gleixner0793a612008-12-04 20:12:29 +01001256perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
1257{
1258 struct perf_counter *counter = file->private_data;
1259
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001260 return perf_read_hw(counter, buf, count);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001261}
1262
1263static unsigned int perf_poll(struct file *file, poll_table *wait)
1264{
1265 struct perf_counter *counter = file->private_data;
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001266 struct perf_mmap_data *data;
1267 unsigned int events;
1268
1269 rcu_read_lock();
1270 data = rcu_dereference(counter->data);
1271 if (data)
1272 events = atomic_xchg(&data->wakeup, 0);
1273 else
1274 events = POLL_HUP;
1275 rcu_read_unlock();
Thomas Gleixner0793a612008-12-04 20:12:29 +01001276
1277 poll_wait(file, &counter->waitq, wait);
1278
Thomas Gleixner0793a612008-12-04 20:12:29 +01001279 return events;
1280}
1281
Paul Mackerrasd859e292009-01-17 18:10:22 +11001282static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1283{
1284 struct perf_counter *counter = file->private_data;
1285 int err = 0;
1286
1287 switch (cmd) {
1288 case PERF_COUNTER_IOC_ENABLE:
1289 perf_counter_enable_family(counter);
1290 break;
1291 case PERF_COUNTER_IOC_DISABLE:
1292 perf_counter_disable_family(counter);
1293 break;
Peter Zijlstra79f14642009-04-06 11:45:07 +02001294 case PERF_COUNTER_IOC_REFRESH:
1295 perf_counter_refresh(counter, arg);
1296 break;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001297 default:
1298 err = -ENOTTY;
1299 }
1300 return err;
1301}
1302
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001303/*
1304 * Callers need to ensure there can be no nesting of this function, otherwise
1305 * the seqlock logic goes bad. We can not serialize this because the arch
1306 * code calls this from NMI context.
1307 */
1308void perf_counter_update_userpage(struct perf_counter *counter)
Paul Mackerras37d81822009-03-23 18:22:08 +01001309{
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001310 struct perf_mmap_data *data;
1311 struct perf_counter_mmap_page *userpg;
1312
1313 rcu_read_lock();
1314 data = rcu_dereference(counter->data);
1315 if (!data)
1316 goto unlock;
1317
1318 userpg = data->user_page;
Paul Mackerras37d81822009-03-23 18:22:08 +01001319
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001320 /*
1321 * Disable preemption so as to not let the corresponding user-space
1322 * spin too long if we get preempted.
1323 */
1324 preempt_disable();
Paul Mackerras37d81822009-03-23 18:22:08 +01001325 ++userpg->lock;
Peter Zijlstra92f22a32009-04-02 11:12:04 +02001326 barrier();
Paul Mackerras37d81822009-03-23 18:22:08 +01001327 userpg->index = counter->hw.idx;
1328 userpg->offset = atomic64_read(&counter->count);
1329 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
1330 userpg->offset -= atomic64_read(&counter->hw.prev_count);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001331
Peter Zijlstra92f22a32009-04-02 11:12:04 +02001332 barrier();
Paul Mackerras37d81822009-03-23 18:22:08 +01001333 ++userpg->lock;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001334 preempt_enable();
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001335unlock:
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001336 rcu_read_unlock();
Paul Mackerras37d81822009-03-23 18:22:08 +01001337}
1338
1339static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1340{
1341 struct perf_counter *counter = vma->vm_file->private_data;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001342 struct perf_mmap_data *data;
1343 int ret = VM_FAULT_SIGBUS;
Paul Mackerras37d81822009-03-23 18:22:08 +01001344
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001345 rcu_read_lock();
1346 data = rcu_dereference(counter->data);
1347 if (!data)
1348 goto unlock;
Paul Mackerras37d81822009-03-23 18:22:08 +01001349
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001350 if (vmf->pgoff == 0) {
1351 vmf->page = virt_to_page(data->user_page);
1352 } else {
1353 int nr = vmf->pgoff - 1;
1354
1355 if ((unsigned)nr > data->nr_pages)
1356 goto unlock;
1357
1358 vmf->page = virt_to_page(data->data_pages[nr]);
1359 }
Paul Mackerras37d81822009-03-23 18:22:08 +01001360 get_page(vmf->page);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001361 ret = 0;
1362unlock:
1363 rcu_read_unlock();
1364
1365 return ret;
1366}
1367
1368static int perf_mmap_data_alloc(struct perf_counter *counter, int nr_pages)
1369{
1370 struct perf_mmap_data *data;
1371 unsigned long size;
1372 int i;
1373
1374 WARN_ON(atomic_read(&counter->mmap_count));
1375
1376 size = sizeof(struct perf_mmap_data);
1377 size += nr_pages * sizeof(void *);
1378
1379 data = kzalloc(size, GFP_KERNEL);
1380 if (!data)
1381 goto fail;
1382
1383 data->user_page = (void *)get_zeroed_page(GFP_KERNEL);
1384 if (!data->user_page)
1385 goto fail_user_page;
1386
1387 for (i = 0; i < nr_pages; i++) {
1388 data->data_pages[i] = (void *)get_zeroed_page(GFP_KERNEL);
1389 if (!data->data_pages[i])
1390 goto fail_data_pages;
1391 }
1392
1393 data->nr_pages = nr_pages;
1394
1395 rcu_assign_pointer(counter->data, data);
1396
Paul Mackerras37d81822009-03-23 18:22:08 +01001397 return 0;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001398
1399fail_data_pages:
1400 for (i--; i >= 0; i--)
1401 free_page((unsigned long)data->data_pages[i]);
1402
1403 free_page((unsigned long)data->user_page);
1404
1405fail_user_page:
1406 kfree(data);
1407
1408fail:
1409 return -ENOMEM;
1410}
1411
1412static void __perf_mmap_data_free(struct rcu_head *rcu_head)
1413{
1414 struct perf_mmap_data *data = container_of(rcu_head,
1415 struct perf_mmap_data, rcu_head);
1416 int i;
1417
1418 free_page((unsigned long)data->user_page);
1419 for (i = 0; i < data->nr_pages; i++)
1420 free_page((unsigned long)data->data_pages[i]);
1421 kfree(data);
1422}
1423
1424static void perf_mmap_data_free(struct perf_counter *counter)
1425{
1426 struct perf_mmap_data *data = counter->data;
1427
1428 WARN_ON(atomic_read(&counter->mmap_count));
1429
1430 rcu_assign_pointer(counter->data, NULL);
1431 call_rcu(&data->rcu_head, __perf_mmap_data_free);
1432}
1433
1434static void perf_mmap_open(struct vm_area_struct *vma)
1435{
1436 struct perf_counter *counter = vma->vm_file->private_data;
1437
1438 atomic_inc(&counter->mmap_count);
1439}
1440
1441static void perf_mmap_close(struct vm_area_struct *vma)
1442{
1443 struct perf_counter *counter = vma->vm_file->private_data;
1444
1445 if (atomic_dec_and_mutex_lock(&counter->mmap_count,
1446 &counter->mmap_mutex)) {
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001447 vma->vm_mm->locked_vm -= counter->data->nr_pages + 1;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001448 perf_mmap_data_free(counter);
1449 mutex_unlock(&counter->mmap_mutex);
1450 }
Paul Mackerras37d81822009-03-23 18:22:08 +01001451}
1452
1453static struct vm_operations_struct perf_mmap_vmops = {
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001454 .open = perf_mmap_open,
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001455 .close = perf_mmap_close,
Paul Mackerras37d81822009-03-23 18:22:08 +01001456 .fault = perf_mmap_fault,
1457};
1458
1459static int perf_mmap(struct file *file, struct vm_area_struct *vma)
1460{
1461 struct perf_counter *counter = file->private_data;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001462 unsigned long vma_size;
1463 unsigned long nr_pages;
1464 unsigned long locked, lock_limit;
1465 int ret = 0;
Paul Mackerras37d81822009-03-23 18:22:08 +01001466
1467 if (!(vma->vm_flags & VM_SHARED) || (vma->vm_flags & VM_WRITE))
1468 return -EINVAL;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001469
1470 vma_size = vma->vm_end - vma->vm_start;
1471 nr_pages = (vma_size / PAGE_SIZE) - 1;
1472
Peter Zijlstra7730d862009-03-25 12:48:31 +01001473 /*
1474 * If we have data pages ensure they're a power-of-two number, so we
1475 * can do bitmasks instead of modulo.
1476 */
1477 if (nr_pages != 0 && !is_power_of_2(nr_pages))
Paul Mackerras37d81822009-03-23 18:22:08 +01001478 return -EINVAL;
1479
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001480 if (vma_size != PAGE_SIZE * (1 + nr_pages))
Paul Mackerras37d81822009-03-23 18:22:08 +01001481 return -EINVAL;
1482
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001483 if (vma->vm_pgoff != 0)
1484 return -EINVAL;
Paul Mackerras37d81822009-03-23 18:22:08 +01001485
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001486 mutex_lock(&counter->mmap_mutex);
1487 if (atomic_inc_not_zero(&counter->mmap_count)) {
1488 if (nr_pages != counter->data->nr_pages)
1489 ret = -EINVAL;
1490 goto unlock;
1491 }
1492
1493 locked = vma->vm_mm->locked_vm;
1494 locked += nr_pages + 1;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001495
1496 lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
1497 lock_limit >>= PAGE_SHIFT;
1498
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001499 if ((locked > lock_limit) && !capable(CAP_IPC_LOCK)) {
1500 ret = -EPERM;
1501 goto unlock;
1502 }
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001503
1504 WARN_ON(counter->data);
1505 ret = perf_mmap_data_alloc(counter, nr_pages);
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001506 if (ret)
1507 goto unlock;
1508
1509 atomic_set(&counter->mmap_count, 1);
1510 vma->vm_mm->locked_vm += nr_pages + 1;
1511unlock:
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001512 mutex_unlock(&counter->mmap_mutex);
Paul Mackerras37d81822009-03-23 18:22:08 +01001513
1514 vma->vm_flags &= ~VM_MAYWRITE;
1515 vma->vm_flags |= VM_RESERVED;
1516 vma->vm_ops = &perf_mmap_vmops;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001517
1518 return ret;
Paul Mackerras37d81822009-03-23 18:22:08 +01001519}
1520
Peter Zijlstra3c446b32009-04-06 11:45:01 +02001521static int perf_fasync(int fd, struct file *filp, int on)
1522{
1523 struct perf_counter *counter = filp->private_data;
1524 struct inode *inode = filp->f_path.dentry->d_inode;
1525 int retval;
1526
1527 mutex_lock(&inode->i_mutex);
1528 retval = fasync_helper(fd, filp, on, &counter->fasync);
1529 mutex_unlock(&inode->i_mutex);
1530
1531 if (retval < 0)
1532 return retval;
1533
1534 return 0;
1535}
1536
Thomas Gleixner0793a612008-12-04 20:12:29 +01001537static const struct file_operations perf_fops = {
1538 .release = perf_release,
1539 .read = perf_read,
1540 .poll = perf_poll,
Paul Mackerrasd859e292009-01-17 18:10:22 +11001541 .unlocked_ioctl = perf_ioctl,
1542 .compat_ioctl = perf_ioctl,
Paul Mackerras37d81822009-03-23 18:22:08 +01001543 .mmap = perf_mmap,
Peter Zijlstra3c446b32009-04-06 11:45:01 +02001544 .fasync = perf_fasync,
Thomas Gleixner0793a612008-12-04 20:12:29 +01001545};
1546
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001547/*
Peter Zijlstra925d5192009-03-30 19:07:02 +02001548 * Perf counter wakeup
1549 *
1550 * If there's data, ensure we set the poll() state and publish everything
1551 * to user-space before waking everybody up.
1552 */
1553
1554void perf_counter_wakeup(struct perf_counter *counter)
1555{
1556 struct perf_mmap_data *data;
1557
1558 rcu_read_lock();
1559 data = rcu_dereference(counter->data);
1560 if (data) {
Peter Zijlstra3c446b32009-04-06 11:45:01 +02001561 atomic_set(&data->wakeup, POLL_IN);
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001562 /*
1563 * Ensure all data writes are issued before updating the
1564 * user-space data head information. The matching rmb()
1565 * will be in userspace after reading this value.
1566 */
1567 smp_wmb();
1568 data->user_page->data_head = atomic_read(&data->head);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001569 }
1570 rcu_read_unlock();
1571
1572 wake_up_all(&counter->waitq);
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02001573
1574 if (counter->pending_kill) {
1575 kill_fasync(&counter->fasync, SIGIO, counter->pending_kill);
1576 counter->pending_kill = 0;
1577 }
Peter Zijlstra925d5192009-03-30 19:07:02 +02001578}
1579
1580/*
1581 * Pending wakeups
1582 *
1583 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
1584 *
1585 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
1586 * single linked list and use cmpxchg() to add entries lockless.
1587 */
1588
Peter Zijlstra79f14642009-04-06 11:45:07 +02001589static void perf_pending_counter(struct perf_pending_entry *entry)
1590{
1591 struct perf_counter *counter = container_of(entry,
1592 struct perf_counter, pending);
1593
1594 if (counter->pending_disable) {
1595 counter->pending_disable = 0;
1596 perf_counter_disable(counter);
1597 }
1598
1599 if (counter->pending_wakeup) {
1600 counter->pending_wakeup = 0;
1601 perf_counter_wakeup(counter);
1602 }
1603}
1604
Peter Zijlstra671dec52009-04-06 11:45:02 +02001605#define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
Peter Zijlstra925d5192009-03-30 19:07:02 +02001606
Peter Zijlstra671dec52009-04-06 11:45:02 +02001607static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = {
Peter Zijlstra925d5192009-03-30 19:07:02 +02001608 PENDING_TAIL,
1609};
1610
Peter Zijlstra671dec52009-04-06 11:45:02 +02001611static void perf_pending_queue(struct perf_pending_entry *entry,
1612 void (*func)(struct perf_pending_entry *))
Peter Zijlstra925d5192009-03-30 19:07:02 +02001613{
Peter Zijlstra671dec52009-04-06 11:45:02 +02001614 struct perf_pending_entry **head;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001615
Peter Zijlstra671dec52009-04-06 11:45:02 +02001616 if (cmpxchg(&entry->next, NULL, PENDING_TAIL) != NULL)
Peter Zijlstra925d5192009-03-30 19:07:02 +02001617 return;
1618
Peter Zijlstra671dec52009-04-06 11:45:02 +02001619 entry->func = func;
1620
1621 head = &get_cpu_var(perf_pending_head);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001622
1623 do {
Peter Zijlstra671dec52009-04-06 11:45:02 +02001624 entry->next = *head;
1625 } while (cmpxchg(head, entry->next, entry) != entry->next);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001626
1627 set_perf_counter_pending();
1628
Peter Zijlstra671dec52009-04-06 11:45:02 +02001629 put_cpu_var(perf_pending_head);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001630}
1631
1632static int __perf_pending_run(void)
1633{
Peter Zijlstra671dec52009-04-06 11:45:02 +02001634 struct perf_pending_entry *list;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001635 int nr = 0;
1636
Peter Zijlstra671dec52009-04-06 11:45:02 +02001637 list = xchg(&__get_cpu_var(perf_pending_head), PENDING_TAIL);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001638 while (list != PENDING_TAIL) {
Peter Zijlstra671dec52009-04-06 11:45:02 +02001639 void (*func)(struct perf_pending_entry *);
1640 struct perf_pending_entry *entry = list;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001641
1642 list = list->next;
1643
Peter Zijlstra671dec52009-04-06 11:45:02 +02001644 func = entry->func;
1645 entry->next = NULL;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001646 /*
1647 * Ensure we observe the unqueue before we issue the wakeup,
1648 * so that we won't be waiting forever.
1649 * -- see perf_not_pending().
1650 */
1651 smp_wmb();
1652
Peter Zijlstra671dec52009-04-06 11:45:02 +02001653 func(entry);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001654 nr++;
1655 }
1656
1657 return nr;
1658}
1659
1660static inline int perf_not_pending(struct perf_counter *counter)
1661{
1662 /*
1663 * If we flush on whatever cpu we run, there is a chance we don't
1664 * need to wait.
1665 */
1666 get_cpu();
1667 __perf_pending_run();
1668 put_cpu();
1669
1670 /*
1671 * Ensure we see the proper queue state before going to sleep
1672 * so that we do not miss the wakeup. -- see perf_pending_handle()
1673 */
1674 smp_rmb();
Peter Zijlstra671dec52009-04-06 11:45:02 +02001675 return counter->pending.next == NULL;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001676}
1677
1678static void perf_pending_sync(struct perf_counter *counter)
1679{
1680 wait_event(counter->waitq, perf_not_pending(counter));
1681}
1682
1683void perf_counter_do_pending(void)
1684{
1685 __perf_pending_run();
1686}
1687
1688/*
Peter Zijlstra394ee072009-03-30 19:07:14 +02001689 * Callchain support -- arch specific
1690 */
1691
Peter Zijlstra9c03d882009-04-06 11:45:00 +02001692__weak struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
Peter Zijlstra394ee072009-03-30 19:07:14 +02001693{
1694 return NULL;
1695}
1696
1697/*
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001698 * Output
1699 */
1700
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001701struct perf_output_handle {
1702 struct perf_counter *counter;
1703 struct perf_mmap_data *data;
1704 unsigned int offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01001705 unsigned int head;
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001706 int wakeup;
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001707 int nmi;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02001708 int overflow;
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001709};
1710
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001711static inline void __perf_output_wakeup(struct perf_output_handle *handle)
1712{
Peter Zijlstra671dec52009-04-06 11:45:02 +02001713 if (handle->nmi) {
Peter Zijlstra79f14642009-04-06 11:45:07 +02001714 handle->counter->pending_wakeup = 1;
Peter Zijlstra671dec52009-04-06 11:45:02 +02001715 perf_pending_queue(&handle->counter->pending,
Peter Zijlstra79f14642009-04-06 11:45:07 +02001716 perf_pending_counter);
Peter Zijlstra671dec52009-04-06 11:45:02 +02001717 } else
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001718 perf_counter_wakeup(handle->counter);
1719}
1720
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001721static int perf_output_begin(struct perf_output_handle *handle,
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001722 struct perf_counter *counter, unsigned int size,
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02001723 int nmi, int overflow)
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001724{
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001725 struct perf_mmap_data *data;
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001726 unsigned int offset, head;
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001727
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001728 rcu_read_lock();
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001729 data = rcu_dereference(counter->data);
1730 if (!data)
1731 goto out;
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001732
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02001733 handle->counter = counter;
1734 handle->nmi = nmi;
1735 handle->overflow = overflow;
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001736
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001737 if (!data->nr_pages)
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001738 goto fail;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001739
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001740 do {
1741 offset = head = atomic_read(&data->head);
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001742 head += size;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001743 } while (atomic_cmpxchg(&data->head, offset, head) != offset);
1744
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001745 handle->data = data;
1746 handle->offset = offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01001747 handle->head = head;
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001748 handle->wakeup = (offset >> PAGE_SHIFT) != (head >> PAGE_SHIFT);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001749
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001750 return 0;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001751
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001752fail:
1753 __perf_output_wakeup(handle);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001754out:
1755 rcu_read_unlock();
1756
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001757 return -ENOSPC;
1758}
1759
1760static void perf_output_copy(struct perf_output_handle *handle,
1761 void *buf, unsigned int len)
1762{
1763 unsigned int pages_mask;
1764 unsigned int offset;
1765 unsigned int size;
1766 void **pages;
1767
1768 offset = handle->offset;
1769 pages_mask = handle->data->nr_pages - 1;
1770 pages = handle->data->data_pages;
1771
1772 do {
1773 unsigned int page_offset;
1774 int nr;
1775
1776 nr = (offset >> PAGE_SHIFT) & pages_mask;
1777 page_offset = offset & (PAGE_SIZE - 1);
1778 size = min_t(unsigned int, PAGE_SIZE - page_offset, len);
1779
1780 memcpy(pages[nr] + page_offset, buf, size);
1781
1782 len -= size;
1783 buf += size;
1784 offset += size;
1785 } while (len);
1786
1787 handle->offset = offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01001788
1789 WARN_ON_ONCE(handle->offset > handle->head);
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001790}
1791
Peter Zijlstra5c148192009-03-25 12:30:23 +01001792#define perf_output_put(handle, x) \
1793 perf_output_copy((handle), &(x), sizeof(x))
1794
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001795static void perf_output_end(struct perf_output_handle *handle)
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001796{
Peter Zijlstrac4578102009-04-02 11:12:01 +02001797 int wakeup_events = handle->counter->hw_event.wakeup_events;
1798
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02001799 if (handle->overflow && wakeup_events) {
Peter Zijlstrac4578102009-04-02 11:12:01 +02001800 int events = atomic_inc_return(&handle->data->events);
1801 if (events >= wakeup_events) {
1802 atomic_sub(wakeup_events, &handle->data->events);
1803 __perf_output_wakeup(handle);
1804 }
1805 } else if (handle->wakeup)
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001806 __perf_output_wakeup(handle);
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001807 rcu_read_unlock();
1808}
1809
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02001810static void perf_counter_output(struct perf_counter *counter,
1811 int nmi, struct pt_regs *regs)
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001812{
Peter Zijlstra5ed00412009-03-30 19:07:12 +02001813 int ret;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02001814 u64 record_type = counter->hw_event.record_type;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02001815 struct perf_output_handle handle;
1816 struct perf_event_header header;
1817 u64 ip;
Peter Zijlstra5c148192009-03-25 12:30:23 +01001818 struct {
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01001819 u32 pid, tid;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02001820 } tid_entry;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02001821 struct {
1822 u64 event;
1823 u64 counter;
1824 } group_entry;
Peter Zijlstra394ee072009-03-30 19:07:14 +02001825 struct perf_callchain_entry *callchain = NULL;
1826 int callchain_size = 0;
Peter Zijlstra339f7c92009-04-06 11:45:06 +02001827 u64 time;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001828
Peter Zijlstra8a057d82009-04-02 11:11:59 +02001829 header.type = PERF_EVENT_COUNTER_OVERFLOW;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02001830 header.size = sizeof(header);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001831
Peter Zijlstra8a057d82009-04-02 11:11:59 +02001832 if (record_type & PERF_RECORD_IP) {
1833 ip = instruction_pointer(regs);
1834 header.type |= __PERF_EVENT_IP;
1835 header.size += sizeof(ip);
1836 }
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01001837
Peter Zijlstra8a057d82009-04-02 11:11:59 +02001838 if (record_type & PERF_RECORD_TID) {
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01001839 /* namespace issues */
Peter Zijlstra5ed00412009-03-30 19:07:12 +02001840 tid_entry.pid = current->group_leader->pid;
1841 tid_entry.tid = current->pid;
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01001842
Peter Zijlstra5ed00412009-03-30 19:07:12 +02001843 header.type |= __PERF_EVENT_TID;
1844 header.size += sizeof(tid_entry);
1845 }
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01001846
Peter Zijlstra8a057d82009-04-02 11:11:59 +02001847 if (record_type & PERF_RECORD_GROUP) {
1848 header.type |= __PERF_EVENT_GROUP;
1849 header.size += sizeof(u64) +
1850 counter->nr_siblings * sizeof(group_entry);
1851 }
1852
1853 if (record_type & PERF_RECORD_CALLCHAIN) {
Peter Zijlstra394ee072009-03-30 19:07:14 +02001854 callchain = perf_callchain(regs);
1855
1856 if (callchain) {
Peter Zijlstra9c03d882009-04-06 11:45:00 +02001857 callchain_size = (1 + callchain->nr) * sizeof(u64);
Peter Zijlstra394ee072009-03-30 19:07:14 +02001858
1859 header.type |= __PERF_EVENT_CALLCHAIN;
1860 header.size += callchain_size;
1861 }
1862 }
1863
Peter Zijlstra339f7c92009-04-06 11:45:06 +02001864 if (record_type & PERF_RECORD_TIME) {
1865 /*
1866 * Maybe do better on x86 and provide cpu_clock_nmi()
1867 */
1868 time = sched_clock();
1869
1870 header.type |= __PERF_EVENT_TIME;
1871 header.size += sizeof(u64);
1872 }
1873
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02001874 ret = perf_output_begin(&handle, counter, header.size, nmi, 1);
Peter Zijlstra5ed00412009-03-30 19:07:12 +02001875 if (ret)
1876 return;
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01001877
Peter Zijlstra5ed00412009-03-30 19:07:12 +02001878 perf_output_put(&handle, header);
Peter Zijlstra5ed00412009-03-30 19:07:12 +02001879
Peter Zijlstra8a057d82009-04-02 11:11:59 +02001880 if (record_type & PERF_RECORD_IP)
1881 perf_output_put(&handle, ip);
1882
1883 if (record_type & PERF_RECORD_TID)
Peter Zijlstra5ed00412009-03-30 19:07:12 +02001884 perf_output_put(&handle, tid_entry);
1885
Peter Zijlstra8a057d82009-04-02 11:11:59 +02001886 if (record_type & PERF_RECORD_GROUP) {
1887 struct perf_counter *leader, *sub;
1888 u64 nr = counter->nr_siblings;
1889
1890 perf_output_put(&handle, nr);
1891
1892 leader = counter->group_leader;
1893 list_for_each_entry(sub, &leader->sibling_list, list_entry) {
1894 if (sub != counter)
1895 sub->hw_ops->read(sub);
1896
1897 group_entry.event = sub->hw_event.config;
1898 group_entry.counter = atomic64_read(&sub->count);
1899
1900 perf_output_put(&handle, group_entry);
1901 }
1902 }
1903
Peter Zijlstra394ee072009-03-30 19:07:14 +02001904 if (callchain)
1905 perf_output_copy(&handle, callchain, callchain_size);
1906
Peter Zijlstra339f7c92009-04-06 11:45:06 +02001907 if (record_type & PERF_RECORD_TIME)
1908 perf_output_put(&handle, time);
1909
Peter Zijlstra5ed00412009-03-30 19:07:12 +02001910 perf_output_end(&handle);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001911}
1912
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001913/*
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02001914 * mmap tracking
1915 */
1916
1917struct perf_mmap_event {
1918 struct file *file;
1919 char *file_name;
1920 int file_size;
1921
1922 struct {
1923 struct perf_event_header header;
1924
1925 u32 pid;
1926 u32 tid;
1927 u64 start;
1928 u64 len;
1929 u64 pgoff;
1930 } event;
1931};
1932
1933static void perf_counter_mmap_output(struct perf_counter *counter,
1934 struct perf_mmap_event *mmap_event)
1935{
1936 struct perf_output_handle handle;
1937 int size = mmap_event->event.header.size;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02001938 int ret = perf_output_begin(&handle, counter, size, 0, 0);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02001939
1940 if (ret)
1941 return;
1942
1943 perf_output_put(&handle, mmap_event->event);
1944 perf_output_copy(&handle, mmap_event->file_name,
1945 mmap_event->file_size);
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001946 perf_output_end(&handle);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02001947}
1948
1949static int perf_counter_mmap_match(struct perf_counter *counter,
1950 struct perf_mmap_event *mmap_event)
1951{
1952 if (counter->hw_event.mmap &&
1953 mmap_event->event.header.type == PERF_EVENT_MMAP)
1954 return 1;
1955
1956 if (counter->hw_event.munmap &&
1957 mmap_event->event.header.type == PERF_EVENT_MUNMAP)
1958 return 1;
1959
1960 return 0;
1961}
1962
1963static void perf_counter_mmap_ctx(struct perf_counter_context *ctx,
1964 struct perf_mmap_event *mmap_event)
1965{
1966 struct perf_counter *counter;
1967
1968 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
1969 return;
1970
1971 rcu_read_lock();
1972 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
1973 if (perf_counter_mmap_match(counter, mmap_event))
1974 perf_counter_mmap_output(counter, mmap_event);
1975 }
1976 rcu_read_unlock();
1977}
1978
1979static void perf_counter_mmap_event(struct perf_mmap_event *mmap_event)
1980{
1981 struct perf_cpu_context *cpuctx;
1982 struct file *file = mmap_event->file;
1983 unsigned int size;
1984 char tmp[16];
1985 char *buf = NULL;
1986 char *name;
1987
1988 if (file) {
1989 buf = kzalloc(PATH_MAX, GFP_KERNEL);
1990 if (!buf) {
1991 name = strncpy(tmp, "//enomem", sizeof(tmp));
1992 goto got_name;
1993 }
1994 name = dentry_path(file->f_dentry, buf, PATH_MAX);
1995 if (IS_ERR(name)) {
1996 name = strncpy(tmp, "//toolong", sizeof(tmp));
1997 goto got_name;
1998 }
1999 } else {
2000 name = strncpy(tmp, "//anon", sizeof(tmp));
2001 goto got_name;
2002 }
2003
2004got_name:
2005 size = ALIGN(strlen(name), sizeof(u64));
2006
2007 mmap_event->file_name = name;
2008 mmap_event->file_size = size;
2009
2010 mmap_event->event.header.size = sizeof(mmap_event->event) + size;
2011
2012 cpuctx = &get_cpu_var(perf_cpu_context);
2013 perf_counter_mmap_ctx(&cpuctx->ctx, mmap_event);
2014 put_cpu_var(perf_cpu_context);
2015
2016 perf_counter_mmap_ctx(&current->perf_counter_ctx, mmap_event);
2017
2018 kfree(buf);
2019}
2020
2021void perf_counter_mmap(unsigned long addr, unsigned long len,
2022 unsigned long pgoff, struct file *file)
2023{
2024 struct perf_mmap_event mmap_event = {
2025 .file = file,
2026 .event = {
2027 .header = { .type = PERF_EVENT_MMAP, },
2028 .pid = current->group_leader->pid,
2029 .tid = current->pid,
2030 .start = addr,
2031 .len = len,
2032 .pgoff = pgoff,
2033 },
2034 };
2035
2036 perf_counter_mmap_event(&mmap_event);
2037}
2038
2039void perf_counter_munmap(unsigned long addr, unsigned long len,
2040 unsigned long pgoff, struct file *file)
2041{
2042 struct perf_mmap_event mmap_event = {
2043 .file = file,
2044 .event = {
2045 .header = { .type = PERF_EVENT_MUNMAP, },
2046 .pid = current->group_leader->pid,
2047 .tid = current->pid,
2048 .start = addr,
2049 .len = len,
2050 .pgoff = pgoff,
2051 },
2052 };
2053
2054 perf_counter_mmap_event(&mmap_event);
2055}
2056
2057/*
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002058 * Generic counter overflow handling.
2059 */
2060
2061int perf_counter_overflow(struct perf_counter *counter,
2062 int nmi, struct pt_regs *regs)
2063{
Peter Zijlstra79f14642009-04-06 11:45:07 +02002064 int events = atomic_read(&counter->event_limit);
2065 int ret = 0;
2066
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002067 counter->pending_kill = POLL_IN;
Peter Zijlstra79f14642009-04-06 11:45:07 +02002068 if (events && atomic_dec_and_test(&counter->event_limit)) {
2069 ret = 1;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002070 counter->pending_kill = POLL_HUP;
Peter Zijlstra79f14642009-04-06 11:45:07 +02002071 if (nmi) {
2072 counter->pending_disable = 1;
2073 perf_pending_queue(&counter->pending,
2074 perf_pending_counter);
2075 } else
2076 perf_counter_disable(counter);
2077 }
2078
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002079 perf_counter_output(counter, nmi, regs);
Peter Zijlstra79f14642009-04-06 11:45:07 +02002080 return ret;
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002081}
2082
2083/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002084 * Generic software counter infrastructure
2085 */
2086
2087static void perf_swcounter_update(struct perf_counter *counter)
2088{
2089 struct hw_perf_counter *hwc = &counter->hw;
2090 u64 prev, now;
2091 s64 delta;
2092
2093again:
2094 prev = atomic64_read(&hwc->prev_count);
2095 now = atomic64_read(&hwc->count);
2096 if (atomic64_cmpxchg(&hwc->prev_count, prev, now) != prev)
2097 goto again;
2098
2099 delta = now - prev;
2100
2101 atomic64_add(delta, &counter->count);
2102 atomic64_sub(delta, &hwc->period_left);
2103}
2104
2105static void perf_swcounter_set_period(struct perf_counter *counter)
2106{
2107 struct hw_perf_counter *hwc = &counter->hw;
2108 s64 left = atomic64_read(&hwc->period_left);
2109 s64 period = hwc->irq_period;
2110
2111 if (unlikely(left <= -period)) {
2112 left = period;
2113 atomic64_set(&hwc->period_left, left);
2114 }
2115
2116 if (unlikely(left <= 0)) {
2117 left += period;
2118 atomic64_add(period, &hwc->period_left);
2119 }
2120
2121 atomic64_set(&hwc->prev_count, -left);
2122 atomic64_set(&hwc->count, -left);
2123}
2124
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002125static enum hrtimer_restart perf_swcounter_hrtimer(struct hrtimer *hrtimer)
2126{
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002127 enum hrtimer_restart ret = HRTIMER_RESTART;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002128 struct perf_counter *counter;
2129 struct pt_regs *regs;
2130
2131 counter = container_of(hrtimer, struct perf_counter, hw.hrtimer);
2132 counter->hw_ops->read(counter);
2133
2134 regs = get_irq_regs();
2135 /*
2136 * In case we exclude kernel IPs or are somehow not in interrupt
2137 * context, provide the next best thing, the user IP.
2138 */
2139 if ((counter->hw_event.exclude_kernel || !regs) &&
2140 !counter->hw_event.exclude_user)
2141 regs = task_pt_regs(current);
2142
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002143 if (regs) {
2144 if (perf_counter_overflow(counter, 0, regs))
2145 ret = HRTIMER_NORESTART;
2146 }
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002147
2148 hrtimer_forward_now(hrtimer, ns_to_ktime(counter->hw.irq_period));
2149
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002150 return ret;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002151}
2152
2153static void perf_swcounter_overflow(struct perf_counter *counter,
2154 int nmi, struct pt_regs *regs)
2155{
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002156 perf_swcounter_update(counter);
2157 perf_swcounter_set_period(counter);
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002158 if (perf_counter_overflow(counter, nmi, regs))
2159 /* soft-disable the counter */
2160 ;
2161
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002162}
2163
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002164static int perf_swcounter_match(struct perf_counter *counter,
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002165 enum perf_event_types type,
2166 u32 event, struct pt_regs *regs)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002167{
2168 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
2169 return 0;
2170
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002171 if (perf_event_raw(&counter->hw_event))
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002172 return 0;
2173
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002174 if (perf_event_type(&counter->hw_event) != type)
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002175 return 0;
2176
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002177 if (perf_event_id(&counter->hw_event) != event)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002178 return 0;
2179
2180 if (counter->hw_event.exclude_user && user_mode(regs))
2181 return 0;
2182
2183 if (counter->hw_event.exclude_kernel && !user_mode(regs))
2184 return 0;
2185
2186 return 1;
2187}
2188
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002189static void perf_swcounter_add(struct perf_counter *counter, u64 nr,
2190 int nmi, struct pt_regs *regs)
2191{
2192 int neg = atomic64_add_negative(nr, &counter->hw.count);
2193 if (counter->hw.irq_period && !neg)
2194 perf_swcounter_overflow(counter, nmi, regs);
2195}
2196
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002197static void perf_swcounter_ctx_event(struct perf_counter_context *ctx,
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002198 enum perf_event_types type, u32 event,
2199 u64 nr, int nmi, struct pt_regs *regs)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002200{
2201 struct perf_counter *counter;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002202
Peter Zijlstra01ef09d2009-03-19 20:26:11 +01002203 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002204 return;
2205
Peter Zijlstra592903c2009-03-13 12:21:36 +01002206 rcu_read_lock();
2207 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002208 if (perf_swcounter_match(counter, type, event, regs))
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002209 perf_swcounter_add(counter, nr, nmi, regs);
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002210 }
Peter Zijlstra592903c2009-03-13 12:21:36 +01002211 rcu_read_unlock();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002212}
2213
Peter Zijlstra96f6d442009-03-23 18:22:07 +01002214static int *perf_swcounter_recursion_context(struct perf_cpu_context *cpuctx)
2215{
2216 if (in_nmi())
2217 return &cpuctx->recursion[3];
2218
2219 if (in_irq())
2220 return &cpuctx->recursion[2];
2221
2222 if (in_softirq())
2223 return &cpuctx->recursion[1];
2224
2225 return &cpuctx->recursion[0];
2226}
2227
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002228static void __perf_swcounter_event(enum perf_event_types type, u32 event,
2229 u64 nr, int nmi, struct pt_regs *regs)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002230{
2231 struct perf_cpu_context *cpuctx = &get_cpu_var(perf_cpu_context);
Peter Zijlstra96f6d442009-03-23 18:22:07 +01002232 int *recursion = perf_swcounter_recursion_context(cpuctx);
2233
2234 if (*recursion)
2235 goto out;
2236
2237 (*recursion)++;
2238 barrier();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002239
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002240 perf_swcounter_ctx_event(&cpuctx->ctx, type, event, nr, nmi, regs);
2241 if (cpuctx->task_ctx) {
2242 perf_swcounter_ctx_event(cpuctx->task_ctx, type, event,
2243 nr, nmi, regs);
2244 }
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002245
Peter Zijlstra96f6d442009-03-23 18:22:07 +01002246 barrier();
2247 (*recursion)--;
2248
2249out:
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002250 put_cpu_var(perf_cpu_context);
2251}
2252
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002253void perf_swcounter_event(u32 event, u64 nr, int nmi, struct pt_regs *regs)
2254{
2255 __perf_swcounter_event(PERF_TYPE_SOFTWARE, event, nr, nmi, regs);
2256}
2257
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002258static void perf_swcounter_read(struct perf_counter *counter)
2259{
2260 perf_swcounter_update(counter);
2261}
2262
2263static int perf_swcounter_enable(struct perf_counter *counter)
2264{
2265 perf_swcounter_set_period(counter);
2266 return 0;
2267}
2268
2269static void perf_swcounter_disable(struct perf_counter *counter)
2270{
2271 perf_swcounter_update(counter);
2272}
2273
Peter Zijlstraac17dc82009-03-13 12:21:34 +01002274static const struct hw_perf_counter_ops perf_ops_generic = {
2275 .enable = perf_swcounter_enable,
2276 .disable = perf_swcounter_disable,
2277 .read = perf_swcounter_read,
2278};
2279
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002280/*
2281 * Software counter: cpu wall time clock
2282 */
2283
Paul Mackerras9abf8a02009-01-09 16:26:43 +11002284static void cpu_clock_perf_counter_update(struct perf_counter *counter)
2285{
2286 int cpu = raw_smp_processor_id();
2287 s64 prev;
2288 u64 now;
2289
2290 now = cpu_clock(cpu);
2291 prev = atomic64_read(&counter->hw.prev_count);
2292 atomic64_set(&counter->hw.prev_count, now);
2293 atomic64_add(now - prev, &counter->count);
2294}
2295
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002296static int cpu_clock_perf_counter_enable(struct perf_counter *counter)
2297{
2298 struct hw_perf_counter *hwc = &counter->hw;
2299 int cpu = raw_smp_processor_id();
2300
2301 atomic64_set(&hwc->prev_count, cpu_clock(cpu));
Peter Zijlstra039fc912009-03-13 16:43:47 +01002302 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2303 hwc->hrtimer.function = perf_swcounter_hrtimer;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002304 if (hwc->irq_period) {
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002305 __hrtimer_start_range_ns(&hwc->hrtimer,
2306 ns_to_ktime(hwc->irq_period), 0,
2307 HRTIMER_MODE_REL, 0);
2308 }
2309
2310 return 0;
2311}
2312
Ingo Molnar5c92d122008-12-11 13:21:10 +01002313static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
2314{
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002315 hrtimer_cancel(&counter->hw.hrtimer);
Paul Mackerras9abf8a02009-01-09 16:26:43 +11002316 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01002317}
2318
2319static void cpu_clock_perf_counter_read(struct perf_counter *counter)
2320{
Paul Mackerras9abf8a02009-01-09 16:26:43 +11002321 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01002322}
2323
2324static const struct hw_perf_counter_ops perf_ops_cpu_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +01002325 .enable = cpu_clock_perf_counter_enable,
2326 .disable = cpu_clock_perf_counter_disable,
2327 .read = cpu_clock_perf_counter_read,
Ingo Molnar5c92d122008-12-11 13:21:10 +01002328};
2329
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01002330/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002331 * Software counter: task time clock
2332 */
2333
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02002334static void task_clock_perf_counter_update(struct perf_counter *counter)
Ingo Molnarbae43c92008-12-11 14:03:20 +01002335{
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02002336 u64 prev, now;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002337 s64 delta;
Ingo Molnarbae43c92008-12-11 14:03:20 +01002338
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02002339 update_context_time(counter->ctx);
2340 now = counter->ctx->time;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002341
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02002342 prev = atomic64_xchg(&counter->hw.prev_count, now);
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002343 delta = now - prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002344 atomic64_add(delta, &counter->count);
Ingo Molnarbae43c92008-12-11 14:03:20 +01002345}
2346
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01002347static int task_clock_perf_counter_enable(struct perf_counter *counter)
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002348{
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002349 struct hw_perf_counter *hwc = &counter->hw;
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02002350 u64 now;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002351
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02002352 update_context_time(counter->ctx);
2353 now = counter->ctx->time;
2354
2355 atomic64_set(&hwc->prev_count, now);
Peter Zijlstra039fc912009-03-13 16:43:47 +01002356 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2357 hwc->hrtimer.function = perf_swcounter_hrtimer;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002358 if (hwc->irq_period) {
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002359 __hrtimer_start_range_ns(&hwc->hrtimer,
2360 ns_to_ktime(hwc->irq_period), 0,
2361 HRTIMER_MODE_REL, 0);
2362 }
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01002363
2364 return 0;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002365}
2366
2367static void task_clock_perf_counter_disable(struct perf_counter *counter)
2368{
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002369 hrtimer_cancel(&counter->hw.hrtimer);
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02002370 task_clock_perf_counter_update(counter);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002371}
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01002372
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002373static void task_clock_perf_counter_read(struct perf_counter *counter)
2374{
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02002375 task_clock_perf_counter_update(counter);
Ingo Molnarbae43c92008-12-11 14:03:20 +01002376}
2377
2378static const struct hw_perf_counter_ops perf_ops_task_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +01002379 .enable = task_clock_perf_counter_enable,
2380 .disable = task_clock_perf_counter_disable,
2381 .read = task_clock_perf_counter_read,
Ingo Molnarbae43c92008-12-11 14:03:20 +01002382};
2383
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002384/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002385 * Software counter: cpu migrations
2386 */
2387
Paul Mackerras23a185c2009-02-09 22:42:47 +11002388static inline u64 get_cpu_migrations(struct perf_counter *counter)
Ingo Molnar6c594c22008-12-14 12:34:15 +01002389{
Paul Mackerras23a185c2009-02-09 22:42:47 +11002390 struct task_struct *curr = counter->ctx->task;
2391
2392 if (curr)
2393 return curr->se.nr_migrations;
2394 return cpu_nr_migrations(smp_processor_id());
Ingo Molnar6c594c22008-12-14 12:34:15 +01002395}
2396
2397static void cpu_migrations_perf_counter_update(struct perf_counter *counter)
2398{
2399 u64 prev, now;
2400 s64 delta;
2401
2402 prev = atomic64_read(&counter->hw.prev_count);
Paul Mackerras23a185c2009-02-09 22:42:47 +11002403 now = get_cpu_migrations(counter);
Ingo Molnar6c594c22008-12-14 12:34:15 +01002404
2405 atomic64_set(&counter->hw.prev_count, now);
2406
2407 delta = now - prev;
Ingo Molnar6c594c22008-12-14 12:34:15 +01002408
2409 atomic64_add(delta, &counter->count);
2410}
2411
2412static void cpu_migrations_perf_counter_read(struct perf_counter *counter)
2413{
2414 cpu_migrations_perf_counter_update(counter);
2415}
2416
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01002417static int cpu_migrations_perf_counter_enable(struct perf_counter *counter)
Ingo Molnar6c594c22008-12-14 12:34:15 +01002418{
Paul Mackerrasc07c99b2009-02-13 22:10:34 +11002419 if (counter->prev_state <= PERF_COUNTER_STATE_OFF)
2420 atomic64_set(&counter->hw.prev_count,
2421 get_cpu_migrations(counter));
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01002422 return 0;
Ingo Molnar6c594c22008-12-14 12:34:15 +01002423}
2424
2425static void cpu_migrations_perf_counter_disable(struct perf_counter *counter)
2426{
2427 cpu_migrations_perf_counter_update(counter);
2428}
2429
2430static const struct hw_perf_counter_ops perf_ops_cpu_migrations = {
Ingo Molnar76715812008-12-17 14:20:28 +01002431 .enable = cpu_migrations_perf_counter_enable,
2432 .disable = cpu_migrations_perf_counter_disable,
2433 .read = cpu_migrations_perf_counter_read,
Ingo Molnar6c594c22008-12-14 12:34:15 +01002434};
2435
Peter Zijlstrae077df42009-03-19 20:26:17 +01002436#ifdef CONFIG_EVENT_PROFILE
2437void perf_tpcounter_event(int event_id)
2438{
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002439 struct pt_regs *regs = get_irq_regs();
2440
2441 if (!regs)
2442 regs = task_pt_regs(current);
2443
2444 __perf_swcounter_event(PERF_TYPE_TRACEPOINT, event_id, 1, 1, regs);
Peter Zijlstrae077df42009-03-19 20:26:17 +01002445}
2446
2447extern int ftrace_profile_enable(int);
2448extern void ftrace_profile_disable(int);
2449
2450static void tp_perf_counter_destroy(struct perf_counter *counter)
2451{
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002452 ftrace_profile_disable(perf_event_id(&counter->hw_event));
Peter Zijlstrae077df42009-03-19 20:26:17 +01002453}
2454
2455static const struct hw_perf_counter_ops *
2456tp_perf_counter_init(struct perf_counter *counter)
2457{
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002458 int event_id = perf_event_id(&counter->hw_event);
Peter Zijlstrae077df42009-03-19 20:26:17 +01002459 int ret;
2460
2461 ret = ftrace_profile_enable(event_id);
2462 if (ret)
2463 return NULL;
2464
2465 counter->destroy = tp_perf_counter_destroy;
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002466 counter->hw.irq_period = counter->hw_event.irq_period;
Peter Zijlstrae077df42009-03-19 20:26:17 +01002467
2468 return &perf_ops_generic;
2469}
2470#else
2471static const struct hw_perf_counter_ops *
2472tp_perf_counter_init(struct perf_counter *counter)
2473{
2474 return NULL;
2475}
2476#endif
2477
Ingo Molnar5c92d122008-12-11 13:21:10 +01002478static const struct hw_perf_counter_ops *
2479sw_perf_counter_init(struct perf_counter *counter)
2480{
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002481 struct perf_counter_hw_event *hw_event = &counter->hw_event;
Ingo Molnar5c92d122008-12-11 13:21:10 +01002482 const struct hw_perf_counter_ops *hw_ops = NULL;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002483 struct hw_perf_counter *hwc = &counter->hw;
Ingo Molnar5c92d122008-12-11 13:21:10 +01002484
Paul Mackerras0475f9e2009-02-11 14:35:35 +11002485 /*
2486 * Software counters (currently) can't in general distinguish
2487 * between user, kernel and hypervisor events.
2488 * However, context switches and cpu migrations are considered
2489 * to be kernel events, and page faults are never hypervisor
2490 * events.
2491 */
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002492 switch (perf_event_id(&counter->hw_event)) {
Ingo Molnar5c92d122008-12-11 13:21:10 +01002493 case PERF_COUNT_CPU_CLOCK:
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002494 hw_ops = &perf_ops_cpu_clock;
2495
2496 if (hw_event->irq_period && hw_event->irq_period < 10000)
2497 hw_event->irq_period = 10000;
Ingo Molnar5c92d122008-12-11 13:21:10 +01002498 break;
Ingo Molnarbae43c92008-12-11 14:03:20 +01002499 case PERF_COUNT_TASK_CLOCK:
Paul Mackerras23a185c2009-02-09 22:42:47 +11002500 /*
2501 * If the user instantiates this as a per-cpu counter,
2502 * use the cpu_clock counter instead.
2503 */
2504 if (counter->ctx->task)
2505 hw_ops = &perf_ops_task_clock;
2506 else
2507 hw_ops = &perf_ops_cpu_clock;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002508
2509 if (hw_event->irq_period && hw_event->irq_period < 10000)
2510 hw_event->irq_period = 10000;
Ingo Molnarbae43c92008-12-11 14:03:20 +01002511 break;
Ingo Molnare06c61a2008-12-14 14:44:31 +01002512 case PERF_COUNT_PAGE_FAULTS:
Peter Zijlstraac17dc82009-03-13 12:21:34 +01002513 case PERF_COUNT_PAGE_FAULTS_MIN:
2514 case PERF_COUNT_PAGE_FAULTS_MAJ:
Ingo Molnar5d6a27d2008-12-14 12:28:33 +01002515 case PERF_COUNT_CONTEXT_SWITCHES:
Peter Zijlstra4a0deca2009-03-19 20:26:12 +01002516 hw_ops = &perf_ops_generic;
Ingo Molnar5d6a27d2008-12-14 12:28:33 +01002517 break;
Ingo Molnar6c594c22008-12-14 12:34:15 +01002518 case PERF_COUNT_CPU_MIGRATIONS:
Paul Mackerras0475f9e2009-02-11 14:35:35 +11002519 if (!counter->hw_event.exclude_kernel)
2520 hw_ops = &perf_ops_cpu_migrations;
Ingo Molnar6c594c22008-12-14 12:34:15 +01002521 break;
Ingo Molnar5c92d122008-12-11 13:21:10 +01002522 }
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002523
2524 if (hw_ops)
2525 hwc->irq_period = hw_event->irq_period;
2526
Ingo Molnar5c92d122008-12-11 13:21:10 +01002527 return hw_ops;
2528}
2529
Thomas Gleixner0793a612008-12-04 20:12:29 +01002530/*
2531 * Allocate and initialize a counter structure
2532 */
2533static struct perf_counter *
Ingo Molnar04289bb2008-12-11 08:38:42 +01002534perf_counter_alloc(struct perf_counter_hw_event *hw_event,
2535 int cpu,
Paul Mackerras23a185c2009-02-09 22:42:47 +11002536 struct perf_counter_context *ctx,
Ingo Molnar9b51f662008-12-12 13:49:45 +01002537 struct perf_counter *group_leader,
2538 gfp_t gfpflags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01002539{
Ingo Molnar5c92d122008-12-11 13:21:10 +01002540 const struct hw_perf_counter_ops *hw_ops;
Ingo Molnar621a01e2008-12-11 12:46:46 +01002541 struct perf_counter *counter;
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02002542 long err;
Thomas Gleixner0793a612008-12-04 20:12:29 +01002543
Ingo Molnar9b51f662008-12-12 13:49:45 +01002544 counter = kzalloc(sizeof(*counter), gfpflags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002545 if (!counter)
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02002546 return ERR_PTR(-ENOMEM);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002547
Ingo Molnar04289bb2008-12-11 08:38:42 +01002548 /*
2549 * Single counters are their own group leaders, with an
2550 * empty sibling list:
2551 */
2552 if (!group_leader)
2553 group_leader = counter;
2554
Thomas Gleixner0793a612008-12-04 20:12:29 +01002555 mutex_init(&counter->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01002556 INIT_LIST_HEAD(&counter->list_entry);
Peter Zijlstra592903c2009-03-13 12:21:36 +01002557 INIT_LIST_HEAD(&counter->event_entry);
Ingo Molnar04289bb2008-12-11 08:38:42 +01002558 INIT_LIST_HEAD(&counter->sibling_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002559 init_waitqueue_head(&counter->waitq);
2560
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002561 mutex_init(&counter->mmap_mutex);
2562
Paul Mackerrasd859e292009-01-17 18:10:22 +11002563 INIT_LIST_HEAD(&counter->child_list);
2564
Ingo Molnar9f66a382008-12-10 12:33:23 +01002565 counter->cpu = cpu;
2566 counter->hw_event = *hw_event;
Ingo Molnar04289bb2008-12-11 08:38:42 +01002567 counter->group_leader = group_leader;
Ingo Molnar621a01e2008-12-11 12:46:46 +01002568 counter->hw_ops = NULL;
Paul Mackerras23a185c2009-02-09 22:42:47 +11002569 counter->ctx = ctx;
Ingo Molnar621a01e2008-12-11 12:46:46 +01002570
Ingo Molnar235c7fc2008-12-21 14:43:25 +01002571 counter->state = PERF_COUNTER_STATE_INACTIVE;
Ingo Molnara86ed502008-12-17 00:43:10 +01002572 if (hw_event->disabled)
2573 counter->state = PERF_COUNTER_STATE_OFF;
2574
Ingo Molnar5c92d122008-12-11 13:21:10 +01002575 hw_ops = NULL;
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002576
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002577 if (perf_event_raw(hw_event)) {
Ingo Molnar5c92d122008-12-11 13:21:10 +01002578 hw_ops = hw_perf_counter_init(counter);
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002579 goto done;
2580 }
2581
2582 switch (perf_event_type(hw_event)) {
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002583 case PERF_TYPE_HARDWARE:
2584 hw_ops = hw_perf_counter_init(counter);
2585 break;
2586
2587 case PERF_TYPE_SOFTWARE:
2588 hw_ops = sw_perf_counter_init(counter);
2589 break;
2590
2591 case PERF_TYPE_TRACEPOINT:
2592 hw_ops = tp_perf_counter_init(counter);
2593 break;
2594 }
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002595done:
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02002596 err = 0;
2597 if (!hw_ops)
2598 err = -EINVAL;
2599 else if (IS_ERR(hw_ops))
2600 err = PTR_ERR(hw_ops);
2601
2602 if (err) {
2603 kfree(counter);
2604 return ERR_PTR(err);
2605 }
2606
Ingo Molnar621a01e2008-12-11 12:46:46 +01002607 counter->hw_ops = hw_ops;
Thomas Gleixner0793a612008-12-04 20:12:29 +01002608
2609 return counter;
2610}
2611
2612/**
Paul Mackerras2743a5b2009-03-04 20:36:51 +11002613 * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
Ingo Molnar9f66a382008-12-10 12:33:23 +01002614 *
2615 * @hw_event_uptr: event type attributes for monitoring/sampling
Thomas Gleixner0793a612008-12-04 20:12:29 +01002616 * @pid: target pid
Ingo Molnar9f66a382008-12-10 12:33:23 +01002617 * @cpu: target cpu
2618 * @group_fd: group leader counter fd
Thomas Gleixner0793a612008-12-04 20:12:29 +01002619 */
Paul Mackerras2743a5b2009-03-04 20:36:51 +11002620SYSCALL_DEFINE5(perf_counter_open,
Paul Mackerrasf3dfd262009-02-26 22:43:46 +11002621 const struct perf_counter_hw_event __user *, hw_event_uptr,
Paul Mackerras2743a5b2009-03-04 20:36:51 +11002622 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01002623{
Ingo Molnar04289bb2008-12-11 08:38:42 +01002624 struct perf_counter *counter, *group_leader;
Ingo Molnar9f66a382008-12-10 12:33:23 +01002625 struct perf_counter_hw_event hw_event;
Ingo Molnar04289bb2008-12-11 08:38:42 +01002626 struct perf_counter_context *ctx;
Ingo Molnar9b51f662008-12-12 13:49:45 +01002627 struct file *counter_file = NULL;
Ingo Molnar04289bb2008-12-11 08:38:42 +01002628 struct file *group_file = NULL;
2629 int fput_needed = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01002630 int fput_needed2 = 0;
Thomas Gleixner0793a612008-12-04 20:12:29 +01002631 int ret;
2632
Paul Mackerras2743a5b2009-03-04 20:36:51 +11002633 /* for future expandability... */
2634 if (flags)
2635 return -EINVAL;
2636
Ingo Molnar9f66a382008-12-10 12:33:23 +01002637 if (copy_from_user(&hw_event, hw_event_uptr, sizeof(hw_event)) != 0)
Thomas Gleixnereab656a2008-12-08 19:26:59 +01002638 return -EFAULT;
2639
Ingo Molnar04289bb2008-12-11 08:38:42 +01002640 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01002641 * Get the target context (task or percpu):
2642 */
2643 ctx = find_get_context(pid, cpu);
2644 if (IS_ERR(ctx))
2645 return PTR_ERR(ctx);
2646
2647 /*
2648 * Look up the group leader (we will attach this counter to it):
Ingo Molnar04289bb2008-12-11 08:38:42 +01002649 */
2650 group_leader = NULL;
2651 if (group_fd != -1) {
2652 ret = -EINVAL;
2653 group_file = fget_light(group_fd, &fput_needed);
2654 if (!group_file)
Ingo Molnarccff2862008-12-11 11:26:29 +01002655 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01002656 if (group_file->f_op != &perf_fops)
Ingo Molnarccff2862008-12-11 11:26:29 +01002657 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01002658
2659 group_leader = group_file->private_data;
2660 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01002661 * Do not allow a recursive hierarchy (this new sibling
2662 * becoming part of another group-sibling):
Ingo Molnar04289bb2008-12-11 08:38:42 +01002663 */
Ingo Molnarccff2862008-12-11 11:26:29 +01002664 if (group_leader->group_leader != group_leader)
2665 goto err_put_context;
2666 /*
2667 * Do not allow to attach to a group in a different
2668 * task or CPU context:
2669 */
2670 if (group_leader->ctx != ctx)
2671 goto err_put_context;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11002672 /*
2673 * Only a group leader can be exclusive or pinned
2674 */
2675 if (hw_event.exclusive || hw_event.pinned)
2676 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01002677 }
2678
Paul Mackerras23a185c2009-02-09 22:42:47 +11002679 counter = perf_counter_alloc(&hw_event, cpu, ctx, group_leader,
2680 GFP_KERNEL);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02002681 ret = PTR_ERR(counter);
2682 if (IS_ERR(counter))
Thomas Gleixner0793a612008-12-04 20:12:29 +01002683 goto err_put_context;
2684
Thomas Gleixner0793a612008-12-04 20:12:29 +01002685 ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
2686 if (ret < 0)
Ingo Molnar9b51f662008-12-12 13:49:45 +01002687 goto err_free_put_context;
2688
2689 counter_file = fget_light(ret, &fput_needed2);
2690 if (!counter_file)
2691 goto err_free_put_context;
2692
2693 counter->filp = counter_file;
Paul Mackerrasd859e292009-01-17 18:10:22 +11002694 mutex_lock(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01002695 perf_install_in_context(ctx, counter, cpu);
Paul Mackerrasd859e292009-01-17 18:10:22 +11002696 mutex_unlock(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01002697
2698 fput_light(counter_file, fput_needed2);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002699
Ingo Molnar04289bb2008-12-11 08:38:42 +01002700out_fput:
2701 fput_light(group_file, fput_needed);
2702
Thomas Gleixner0793a612008-12-04 20:12:29 +01002703 return ret;
2704
Ingo Molnar9b51f662008-12-12 13:49:45 +01002705err_free_put_context:
Thomas Gleixner0793a612008-12-04 20:12:29 +01002706 kfree(counter);
2707
2708err_put_context:
2709 put_context(ctx);
2710
Ingo Molnar04289bb2008-12-11 08:38:42 +01002711 goto out_fput;
Thomas Gleixner0793a612008-12-04 20:12:29 +01002712}
2713
Ingo Molnar9b51f662008-12-12 13:49:45 +01002714/*
2715 * Initialize the perf_counter context in a task_struct:
2716 */
2717static void
2718__perf_counter_init_context(struct perf_counter_context *ctx,
2719 struct task_struct *task)
2720{
2721 memset(ctx, 0, sizeof(*ctx));
2722 spin_lock_init(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +11002723 mutex_init(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01002724 INIT_LIST_HEAD(&ctx->counter_list);
Peter Zijlstra592903c2009-03-13 12:21:36 +01002725 INIT_LIST_HEAD(&ctx->event_list);
Ingo Molnar9b51f662008-12-12 13:49:45 +01002726 ctx->task = task;
2727}
2728
2729/*
2730 * inherit a counter from parent task to child task:
2731 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11002732static struct perf_counter *
Ingo Molnar9b51f662008-12-12 13:49:45 +01002733inherit_counter(struct perf_counter *parent_counter,
2734 struct task_struct *parent,
2735 struct perf_counter_context *parent_ctx,
2736 struct task_struct *child,
Paul Mackerrasd859e292009-01-17 18:10:22 +11002737 struct perf_counter *group_leader,
Ingo Molnar9b51f662008-12-12 13:49:45 +01002738 struct perf_counter_context *child_ctx)
2739{
2740 struct perf_counter *child_counter;
2741
Paul Mackerrasd859e292009-01-17 18:10:22 +11002742 /*
2743 * Instead of creating recursive hierarchies of counters,
2744 * we link inherited counters back to the original parent,
2745 * which has a filp for sure, which we use as the reference
2746 * count:
2747 */
2748 if (parent_counter->parent)
2749 parent_counter = parent_counter->parent;
2750
Ingo Molnar9b51f662008-12-12 13:49:45 +01002751 child_counter = perf_counter_alloc(&parent_counter->hw_event,
Paul Mackerras23a185c2009-02-09 22:42:47 +11002752 parent_counter->cpu, child_ctx,
2753 group_leader, GFP_KERNEL);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02002754 if (IS_ERR(child_counter))
2755 return child_counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01002756
2757 /*
2758 * Link it up in the child's context:
2759 */
Ingo Molnar9b51f662008-12-12 13:49:45 +01002760 child_counter->task = child;
Paul Mackerras53cfbf52009-03-25 22:46:58 +11002761 add_counter_to_ctx(child_counter, child_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01002762
2763 child_counter->parent = parent_counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01002764 /*
2765 * inherit into child's child as well:
2766 */
2767 child_counter->hw_event.inherit = 1;
2768
2769 /*
2770 * Get a reference to the parent filp - we will fput it
2771 * when the child counter exits. This is safe to do because
2772 * we are in the parent and we know that the filp still
2773 * exists and has a nonzero count:
2774 */
2775 atomic_long_inc(&parent_counter->filp->f_count);
2776
Paul Mackerrasd859e292009-01-17 18:10:22 +11002777 /*
2778 * Link this into the parent counter's child list
2779 */
2780 mutex_lock(&parent_counter->mutex);
2781 list_add_tail(&child_counter->child_list, &parent_counter->child_list);
2782
2783 /*
2784 * Make the child state follow the state of the parent counter,
2785 * not its hw_event.disabled bit. We hold the parent's mutex,
2786 * so we won't race with perf_counter_{en,dis}able_family.
2787 */
2788 if (parent_counter->state >= PERF_COUNTER_STATE_INACTIVE)
2789 child_counter->state = PERF_COUNTER_STATE_INACTIVE;
2790 else
2791 child_counter->state = PERF_COUNTER_STATE_OFF;
2792
2793 mutex_unlock(&parent_counter->mutex);
2794
2795 return child_counter;
2796}
2797
2798static int inherit_group(struct perf_counter *parent_counter,
2799 struct task_struct *parent,
2800 struct perf_counter_context *parent_ctx,
2801 struct task_struct *child,
2802 struct perf_counter_context *child_ctx)
2803{
2804 struct perf_counter *leader;
2805 struct perf_counter *sub;
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02002806 struct perf_counter *child_ctr;
Paul Mackerrasd859e292009-01-17 18:10:22 +11002807
2808 leader = inherit_counter(parent_counter, parent, parent_ctx,
2809 child, NULL, child_ctx);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02002810 if (IS_ERR(leader))
2811 return PTR_ERR(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +11002812 list_for_each_entry(sub, &parent_counter->sibling_list, list_entry) {
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02002813 child_ctr = inherit_counter(sub, parent, parent_ctx,
2814 child, leader, child_ctx);
2815 if (IS_ERR(child_ctr))
2816 return PTR_ERR(child_ctr);
Paul Mackerrasd859e292009-01-17 18:10:22 +11002817 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01002818 return 0;
2819}
2820
Paul Mackerrasd859e292009-01-17 18:10:22 +11002821static void sync_child_counter(struct perf_counter *child_counter,
2822 struct perf_counter *parent_counter)
2823{
2824 u64 parent_val, child_val;
2825
2826 parent_val = atomic64_read(&parent_counter->count);
2827 child_val = atomic64_read(&child_counter->count);
2828
2829 /*
2830 * Add back the child's count to the parent's count:
2831 */
2832 atomic64_add(child_val, &parent_counter->count);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11002833 atomic64_add(child_counter->total_time_enabled,
2834 &parent_counter->child_total_time_enabled);
2835 atomic64_add(child_counter->total_time_running,
2836 &parent_counter->child_total_time_running);
Paul Mackerrasd859e292009-01-17 18:10:22 +11002837
2838 /*
2839 * Remove this counter from the parent's list
2840 */
2841 mutex_lock(&parent_counter->mutex);
2842 list_del_init(&child_counter->child_list);
2843 mutex_unlock(&parent_counter->mutex);
2844
2845 /*
2846 * Release the parent counter, if this was the last
2847 * reference to it.
2848 */
2849 fput(parent_counter->filp);
2850}
2851
Ingo Molnar9b51f662008-12-12 13:49:45 +01002852static void
2853__perf_counter_exit_task(struct task_struct *child,
2854 struct perf_counter *child_counter,
2855 struct perf_counter_context *child_ctx)
2856{
2857 struct perf_counter *parent_counter;
Paul Mackerrasd859e292009-01-17 18:10:22 +11002858 struct perf_counter *sub, *tmp;
Ingo Molnar9b51f662008-12-12 13:49:45 +01002859
2860 /*
Ingo Molnar235c7fc2008-12-21 14:43:25 +01002861 * If we do not self-reap then we have to wait for the
2862 * child task to unschedule (it will happen for sure),
2863 * so that its counter is at its final count. (This
2864 * condition triggers rarely - child tasks usually get
2865 * off their CPU before the parent has a chance to
2866 * get this far into the reaping action)
Ingo Molnar9b51f662008-12-12 13:49:45 +01002867 */
Ingo Molnar235c7fc2008-12-21 14:43:25 +01002868 if (child != current) {
2869 wait_task_inactive(child, 0);
2870 list_del_init(&child_counter->list_entry);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11002871 update_counter_times(child_counter);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01002872 } else {
Ingo Molnar0cc0c022008-12-14 23:20:36 +01002873 struct perf_cpu_context *cpuctx;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01002874 unsigned long flags;
2875 u64 perf_flags;
2876
2877 /*
2878 * Disable and unlink this counter.
2879 *
2880 * Be careful about zapping the list - IRQ/NMI context
2881 * could still be processing it:
2882 */
Peter Zijlstra849691a2009-04-06 11:45:12 +02002883 local_irq_save(flags);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01002884 perf_flags = hw_perf_save_disable();
Ingo Molnar0cc0c022008-12-14 23:20:36 +01002885
2886 cpuctx = &__get_cpu_var(perf_cpu_context);
2887
Paul Mackerrasd859e292009-01-17 18:10:22 +11002888 group_sched_out(child_counter, cpuctx, child_ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11002889 update_counter_times(child_counter);
Ingo Molnar0cc0c022008-12-14 23:20:36 +01002890
Ingo Molnar235c7fc2008-12-21 14:43:25 +01002891 list_del_init(&child_counter->list_entry);
2892
2893 child_ctx->nr_counters--;
2894
2895 hw_perf_restore(perf_flags);
Peter Zijlstra849691a2009-04-06 11:45:12 +02002896 local_irq_restore(flags);
Ingo Molnar0cc0c022008-12-14 23:20:36 +01002897 }
2898
Ingo Molnar9b51f662008-12-12 13:49:45 +01002899 parent_counter = child_counter->parent;
2900 /*
2901 * It can happen that parent exits first, and has counters
2902 * that are still around due to the child reference. These
2903 * counters need to be zapped - but otherwise linger.
2904 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11002905 if (parent_counter) {
2906 sync_child_counter(child_counter, parent_counter);
2907 list_for_each_entry_safe(sub, tmp, &child_counter->sibling_list,
2908 list_entry) {
Paul Mackerras4bcf3492009-02-11 13:53:19 +01002909 if (sub->parent) {
Paul Mackerrasd859e292009-01-17 18:10:22 +11002910 sync_child_counter(sub, sub->parent);
Peter Zijlstraf1600952009-03-19 20:26:16 +01002911 free_counter(sub);
Paul Mackerras4bcf3492009-02-11 13:53:19 +01002912 }
Paul Mackerrasd859e292009-01-17 18:10:22 +11002913 }
Peter Zijlstraf1600952009-03-19 20:26:16 +01002914 free_counter(child_counter);
Paul Mackerras4bcf3492009-02-11 13:53:19 +01002915 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01002916}
2917
2918/*
Paul Mackerrasd859e292009-01-17 18:10:22 +11002919 * When a child task exits, feed back counter values to parent counters.
Ingo Molnar9b51f662008-12-12 13:49:45 +01002920 *
Paul Mackerrasd859e292009-01-17 18:10:22 +11002921 * Note: we may be running in child context, but the PID is not hashed
Ingo Molnar9b51f662008-12-12 13:49:45 +01002922 * anymore so new counters will not be added.
2923 */
2924void perf_counter_exit_task(struct task_struct *child)
2925{
2926 struct perf_counter *child_counter, *tmp;
2927 struct perf_counter_context *child_ctx;
2928
2929 child_ctx = &child->perf_counter_ctx;
2930
2931 if (likely(!child_ctx->nr_counters))
2932 return;
2933
2934 list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list,
2935 list_entry)
2936 __perf_counter_exit_task(child, child_counter, child_ctx);
2937}
2938
2939/*
2940 * Initialize the perf_counter context in task_struct
2941 */
2942void perf_counter_init_task(struct task_struct *child)
2943{
2944 struct perf_counter_context *child_ctx, *parent_ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +11002945 struct perf_counter *counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01002946 struct task_struct *parent = current;
Ingo Molnar9b51f662008-12-12 13:49:45 +01002947
2948 child_ctx = &child->perf_counter_ctx;
2949 parent_ctx = &parent->perf_counter_ctx;
2950
2951 __perf_counter_init_context(child_ctx, child);
2952
2953 /*
2954 * This is executed from the parent task context, so inherit
2955 * counters that have been marked for cloning:
2956 */
2957
2958 if (likely(!parent_ctx->nr_counters))
2959 return;
2960
2961 /*
2962 * Lock the parent list. No need to lock the child - not PID
2963 * hashed yet and not running, so nobody can access it.
2964 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11002965 mutex_lock(&parent_ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01002966
2967 /*
2968 * We dont have to disable NMIs - we are only looking at
2969 * the list, not manipulating it:
2970 */
2971 list_for_each_entry(counter, &parent_ctx->counter_list, list_entry) {
Paul Mackerrasd859e292009-01-17 18:10:22 +11002972 if (!counter->hw_event.inherit)
Ingo Molnar9b51f662008-12-12 13:49:45 +01002973 continue;
2974
Paul Mackerrasd859e292009-01-17 18:10:22 +11002975 if (inherit_group(counter, parent,
Ingo Molnar9b51f662008-12-12 13:49:45 +01002976 parent_ctx, child, child_ctx))
2977 break;
2978 }
2979
Paul Mackerrasd859e292009-01-17 18:10:22 +11002980 mutex_unlock(&parent_ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01002981}
2982
Ingo Molnar04289bb2008-12-11 08:38:42 +01002983static void __cpuinit perf_counter_init_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01002984{
Ingo Molnar04289bb2008-12-11 08:38:42 +01002985 struct perf_cpu_context *cpuctx;
Thomas Gleixner0793a612008-12-04 20:12:29 +01002986
Ingo Molnar04289bb2008-12-11 08:38:42 +01002987 cpuctx = &per_cpu(perf_cpu_context, cpu);
2988 __perf_counter_init_context(&cpuctx->ctx, NULL);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002989
2990 mutex_lock(&perf_resource_mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01002991 cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu;
Thomas Gleixner0793a612008-12-04 20:12:29 +01002992 mutex_unlock(&perf_resource_mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01002993
Paul Mackerras01d02872009-01-14 13:44:19 +11002994 hw_perf_counter_setup(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002995}
2996
2997#ifdef CONFIG_HOTPLUG_CPU
Ingo Molnar04289bb2008-12-11 08:38:42 +01002998static void __perf_counter_exit_cpu(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01002999{
3000 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
3001 struct perf_counter_context *ctx = &cpuctx->ctx;
3002 struct perf_counter *counter, *tmp;
3003
Ingo Molnar04289bb2008-12-11 08:38:42 +01003004 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry)
3005 __perf_counter_remove_from_context(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003006}
Ingo Molnar04289bb2008-12-11 08:38:42 +01003007static void perf_counter_exit_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003008{
Paul Mackerrasd859e292009-01-17 18:10:22 +11003009 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
3010 struct perf_counter_context *ctx = &cpuctx->ctx;
3011
3012 mutex_lock(&ctx->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003013 smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003014 mutex_unlock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003015}
3016#else
Ingo Molnar04289bb2008-12-11 08:38:42 +01003017static inline void perf_counter_exit_cpu(int cpu) { }
Thomas Gleixner0793a612008-12-04 20:12:29 +01003018#endif
3019
3020static int __cpuinit
3021perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
3022{
3023 unsigned int cpu = (long)hcpu;
3024
3025 switch (action) {
3026
3027 case CPU_UP_PREPARE:
3028 case CPU_UP_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01003029 perf_counter_init_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003030 break;
3031
3032 case CPU_DOWN_PREPARE:
3033 case CPU_DOWN_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01003034 perf_counter_exit_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003035 break;
3036
3037 default:
3038 break;
3039 }
3040
3041 return NOTIFY_OK;
3042}
3043
3044static struct notifier_block __cpuinitdata perf_cpu_nb = {
3045 .notifier_call = perf_cpu_notify,
3046};
3047
3048static int __init perf_counter_init(void)
3049{
3050 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
3051 (void *)(long)smp_processor_id());
3052 register_cpu_notifier(&perf_cpu_nb);
3053
3054 return 0;
3055}
3056early_initcall(perf_counter_init);
3057
3058static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
3059{
3060 return sprintf(buf, "%d\n", perf_reserved_percpu);
3061}
3062
3063static ssize_t
3064perf_set_reserve_percpu(struct sysdev_class *class,
3065 const char *buf,
3066 size_t count)
3067{
3068 struct perf_cpu_context *cpuctx;
3069 unsigned long val;
3070 int err, cpu, mpt;
3071
3072 err = strict_strtoul(buf, 10, &val);
3073 if (err)
3074 return err;
3075 if (val > perf_max_counters)
3076 return -EINVAL;
3077
3078 mutex_lock(&perf_resource_mutex);
3079 perf_reserved_percpu = val;
3080 for_each_online_cpu(cpu) {
3081 cpuctx = &per_cpu(perf_cpu_context, cpu);
3082 spin_lock_irq(&cpuctx->ctx.lock);
3083 mpt = min(perf_max_counters - cpuctx->ctx.nr_counters,
3084 perf_max_counters - perf_reserved_percpu);
3085 cpuctx->max_pertask = mpt;
3086 spin_unlock_irq(&cpuctx->ctx.lock);
3087 }
3088 mutex_unlock(&perf_resource_mutex);
3089
3090 return count;
3091}
3092
3093static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
3094{
3095 return sprintf(buf, "%d\n", perf_overcommit);
3096}
3097
3098static ssize_t
3099perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
3100{
3101 unsigned long val;
3102 int err;
3103
3104 err = strict_strtoul(buf, 10, &val);
3105 if (err)
3106 return err;
3107 if (val > 1)
3108 return -EINVAL;
3109
3110 mutex_lock(&perf_resource_mutex);
3111 perf_overcommit = val;
3112 mutex_unlock(&perf_resource_mutex);
3113
3114 return count;
3115}
3116
3117static SYSDEV_CLASS_ATTR(
3118 reserve_percpu,
3119 0644,
3120 perf_show_reserve_percpu,
3121 perf_set_reserve_percpu
3122 );
3123
3124static SYSDEV_CLASS_ATTR(
3125 overcommit,
3126 0644,
3127 perf_show_overcommit,
3128 perf_set_overcommit
3129 );
3130
3131static struct attribute *perfclass_attrs[] = {
3132 &attr_reserve_percpu.attr,
3133 &attr_overcommit.attr,
3134 NULL
3135};
3136
3137static struct attribute_group perfclass_attr_group = {
3138 .attrs = perfclass_attrs,
3139 .name = "perf_counters",
3140};
3141
3142static int __init perf_counter_sysfs_init(void)
3143{
3144 return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
3145 &perfclass_attr_group);
3146}
3147device_initcall(perf_counter_sysfs_init);