blob: f3e1b27bc1b8559126bea89d6f701bfd4061ac61 [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>
28
Tim Blechmann4e193bd2009-03-14 14:29:25 +010029#include <asm/irq_regs.h>
30
Thomas Gleixner0793a612008-12-04 20:12:29 +010031/*
32 * Each CPU has a list of per CPU counters:
33 */
34DEFINE_PER_CPU(struct perf_cpu_context, perf_cpu_context);
35
Ingo Molnar088e2852008-12-14 20:21:00 +010036int perf_max_counters __read_mostly = 1;
Thomas Gleixner0793a612008-12-04 20:12:29 +010037static int perf_reserved_percpu __read_mostly;
38static int perf_overcommit __read_mostly = 1;
39
40/*
41 * Mutex for (sysadmin-configurable) counter reservations:
42 */
43static DEFINE_MUTEX(perf_resource_mutex);
44
45/*
46 * Architecture provided APIs - weak aliases:
47 */
Ingo Molnar5c92d122008-12-11 13:21:10 +010048extern __weak const struct hw_perf_counter_ops *
Ingo Molnar621a01e2008-12-11 12:46:46 +010049hw_perf_counter_init(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +010050{
Paul Mackerrasff6f0542009-01-09 16:19:25 +110051 return NULL;
Thomas Gleixner0793a612008-12-04 20:12:29 +010052}
53
Ingo Molnar01b28382008-12-11 13:45:51 +010054u64 __weak hw_perf_save_disable(void) { return 0; }
Yinghai Lu01ea1cc2008-12-26 21:05:06 -080055void __weak hw_perf_restore(u64 ctrl) { barrier(); }
Paul Mackerras01d02872009-01-14 13:44:19 +110056void __weak hw_perf_counter_setup(int cpu) { barrier(); }
Paul Mackerras3cbed422009-01-09 16:43:42 +110057int __weak hw_perf_group_sched_in(struct perf_counter *group_leader,
58 struct perf_cpu_context *cpuctx,
59 struct perf_counter_context *ctx, int cpu)
60{
61 return 0;
62}
Thomas Gleixner0793a612008-12-04 20:12:29 +010063
Paul Mackerras4eb96fc2009-01-09 17:24:34 +110064void __weak perf_counter_print_debug(void) { }
65
Ingo Molnar04289bb2008-12-11 08:38:42 +010066static void
67list_add_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
68{
69 struct perf_counter *group_leader = counter->group_leader;
70
71 /*
72 * Depending on whether it is a standalone or sibling counter,
73 * add it straight to the context's counter list, or to the group
74 * leader's sibling list:
75 */
76 if (counter->group_leader == counter)
77 list_add_tail(&counter->list_entry, &ctx->counter_list);
Peter Zijlstra5c148192009-03-25 12:30:23 +010078 else {
Ingo Molnar04289bb2008-12-11 08:38:42 +010079 list_add_tail(&counter->list_entry, &group_leader->sibling_list);
Peter Zijlstra5c148192009-03-25 12:30:23 +010080 group_leader->nr_siblings++;
81 }
Peter Zijlstra592903c2009-03-13 12:21:36 +010082
83 list_add_rcu(&counter->event_entry, &ctx->event_list);
Ingo Molnar04289bb2008-12-11 08:38:42 +010084}
85
86static void
87list_del_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
88{
89 struct perf_counter *sibling, *tmp;
90
91 list_del_init(&counter->list_entry);
Peter Zijlstra592903c2009-03-13 12:21:36 +010092 list_del_rcu(&counter->event_entry);
Ingo Molnar04289bb2008-12-11 08:38:42 +010093
Peter Zijlstra5c148192009-03-25 12:30:23 +010094 if (counter->group_leader != counter)
95 counter->group_leader->nr_siblings--;
96
Ingo Molnar04289bb2008-12-11 08:38:42 +010097 /*
98 * If this was a group counter with sibling counters then
99 * upgrade the siblings to singleton counters by adding them
100 * to the context list directly:
101 */
102 list_for_each_entry_safe(sibling, tmp,
103 &counter->sibling_list, list_entry) {
104
Peter Zijlstra75564232009-03-13 12:21:29 +0100105 list_move_tail(&sibling->list_entry, &ctx->counter_list);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100106 sibling->group_leader = sibling;
107 }
108}
109
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100110static void
111counter_sched_out(struct perf_counter *counter,
112 struct perf_cpu_context *cpuctx,
113 struct perf_counter_context *ctx)
114{
115 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
116 return;
117
118 counter->state = PERF_COUNTER_STATE_INACTIVE;
119 counter->hw_ops->disable(counter);
120 counter->oncpu = -1;
121
122 if (!is_software_counter(counter))
123 cpuctx->active_oncpu--;
124 ctx->nr_active--;
125 if (counter->hw_event.exclusive || !cpuctx->active_oncpu)
126 cpuctx->exclusive = 0;
127}
128
Paul Mackerrasd859e292009-01-17 18:10:22 +1100129static void
130group_sched_out(struct perf_counter *group_counter,
131 struct perf_cpu_context *cpuctx,
132 struct perf_counter_context *ctx)
133{
134 struct perf_counter *counter;
135
136 if (group_counter->state != PERF_COUNTER_STATE_ACTIVE)
137 return;
138
139 counter_sched_out(group_counter, cpuctx, ctx);
140
141 /*
142 * Schedule out siblings (if any):
143 */
144 list_for_each_entry(counter, &group_counter->sibling_list, list_entry)
145 counter_sched_out(counter, cpuctx, ctx);
146
147 if (group_counter->hw_event.exclusive)
148 cpuctx->exclusive = 0;
149}
150
Thomas Gleixner0793a612008-12-04 20:12:29 +0100151/*
152 * Cross CPU call to remove a performance counter
153 *
154 * We disable the counter on the hardware level first. After that we
155 * remove it from the context list.
156 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100157static void __perf_counter_remove_from_context(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100158{
159 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
160 struct perf_counter *counter = info;
161 struct perf_counter_context *ctx = counter->ctx;
Ingo Molnar9b51f662008-12-12 13:49:45 +0100162 unsigned long flags;
Ingo Molnar5c92d122008-12-11 13:21:10 +0100163 u64 perf_flags;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100164
165 /*
166 * If this is a task context, we need to check whether it is
167 * the current task context of this cpu. If not it has been
168 * scheduled out before the smp call arrived.
169 */
170 if (ctx->task && cpuctx->task_ctx != ctx)
171 return;
172
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100173 curr_rq_lock_irq_save(&flags);
174 spin_lock(&ctx->lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100175
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100176 counter_sched_out(counter, cpuctx, ctx);
177
178 counter->task = NULL;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100179 ctx->nr_counters--;
180
181 /*
182 * Protect the list operation against NMI by disabling the
183 * counters on a global level. NOP for non NMI based counters.
184 */
Ingo Molnar01b28382008-12-11 13:45:51 +0100185 perf_flags = hw_perf_save_disable();
Ingo Molnar04289bb2008-12-11 08:38:42 +0100186 list_del_counter(counter, ctx);
Ingo Molnar01b28382008-12-11 13:45:51 +0100187 hw_perf_restore(perf_flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100188
189 if (!ctx->task) {
190 /*
191 * Allow more per task counters with respect to the
192 * reservation:
193 */
194 cpuctx->max_pertask =
195 min(perf_max_counters - ctx->nr_counters,
196 perf_max_counters - perf_reserved_percpu);
197 }
198
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100199 spin_unlock(&ctx->lock);
200 curr_rq_unlock_irq_restore(&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
Paul Mackerrasd859e292009-01-17 18:10:22 +1100254/*
255 * Cross CPU call to disable a performance counter
256 */
257static void __perf_counter_disable(void *info)
258{
259 struct perf_counter *counter = info;
260 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
261 struct perf_counter_context *ctx = counter->ctx;
262 unsigned long flags;
263
264 /*
265 * If this is a per-task counter, need to check whether this
266 * counter's task is the current task on this cpu.
267 */
268 if (ctx->task && cpuctx->task_ctx != ctx)
269 return;
270
271 curr_rq_lock_irq_save(&flags);
272 spin_lock(&ctx->lock);
273
274 /*
275 * If the counter is on, turn it off.
276 * If it is in error state, leave it in error state.
277 */
278 if (counter->state >= PERF_COUNTER_STATE_INACTIVE) {
279 if (counter == counter->group_leader)
280 group_sched_out(counter, cpuctx, ctx);
281 else
282 counter_sched_out(counter, cpuctx, ctx);
283 counter->state = PERF_COUNTER_STATE_OFF;
284 }
285
286 spin_unlock(&ctx->lock);
287 curr_rq_unlock_irq_restore(&flags);
288}
289
290/*
291 * Disable a counter.
292 */
293static void perf_counter_disable(struct perf_counter *counter)
294{
295 struct perf_counter_context *ctx = counter->ctx;
296 struct task_struct *task = ctx->task;
297
298 if (!task) {
299 /*
300 * Disable the counter on the cpu that it's on
301 */
302 smp_call_function_single(counter->cpu, __perf_counter_disable,
303 counter, 1);
304 return;
305 }
306
307 retry:
308 task_oncpu_function_call(task, __perf_counter_disable, counter);
309
310 spin_lock_irq(&ctx->lock);
311 /*
312 * If the counter is still active, we need to retry the cross-call.
313 */
314 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
315 spin_unlock_irq(&ctx->lock);
316 goto retry;
317 }
318
319 /*
320 * Since we have the lock this context can't be scheduled
321 * in, so we can change the state safely.
322 */
323 if (counter->state == PERF_COUNTER_STATE_INACTIVE)
324 counter->state = PERF_COUNTER_STATE_OFF;
325
326 spin_unlock_irq(&ctx->lock);
327}
328
329/*
330 * Disable a counter and all its children.
331 */
332static void perf_counter_disable_family(struct perf_counter *counter)
333{
334 struct perf_counter *child;
335
336 perf_counter_disable(counter);
337
338 /*
339 * Lock the mutex to protect the list of children
340 */
341 mutex_lock(&counter->mutex);
342 list_for_each_entry(child, &counter->child_list, child_list)
343 perf_counter_disable(child);
344 mutex_unlock(&counter->mutex);
345}
346
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100347static int
348counter_sched_in(struct perf_counter *counter,
349 struct perf_cpu_context *cpuctx,
350 struct perf_counter_context *ctx,
351 int cpu)
352{
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100353 if (counter->state <= PERF_COUNTER_STATE_OFF)
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100354 return 0;
355
356 counter->state = PERF_COUNTER_STATE_ACTIVE;
357 counter->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */
358 /*
359 * The new state must be visible before we turn it on in the hardware:
360 */
361 smp_wmb();
362
363 if (counter->hw_ops->enable(counter)) {
364 counter->state = PERF_COUNTER_STATE_INACTIVE;
365 counter->oncpu = -1;
366 return -EAGAIN;
367 }
368
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100369 if (!is_software_counter(counter))
370 cpuctx->active_oncpu++;
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100371 ctx->nr_active++;
372
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100373 if (counter->hw_event.exclusive)
374 cpuctx->exclusive = 1;
375
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100376 return 0;
377}
378
Thomas Gleixner0793a612008-12-04 20:12:29 +0100379/*
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100380 * Return 1 for a group consisting entirely of software counters,
381 * 0 if the group contains any hardware counters.
382 */
383static int is_software_only_group(struct perf_counter *leader)
384{
385 struct perf_counter *counter;
386
387 if (!is_software_counter(leader))
388 return 0;
Peter Zijlstra5c148192009-03-25 12:30:23 +0100389
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100390 list_for_each_entry(counter, &leader->sibling_list, list_entry)
391 if (!is_software_counter(counter))
392 return 0;
Peter Zijlstra5c148192009-03-25 12:30:23 +0100393
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100394 return 1;
395}
396
397/*
398 * Work out whether we can put this counter group on the CPU now.
399 */
400static int group_can_go_on(struct perf_counter *counter,
401 struct perf_cpu_context *cpuctx,
402 int can_add_hw)
403{
404 /*
405 * Groups consisting entirely of software counters can always go on.
406 */
407 if (is_software_only_group(counter))
408 return 1;
409 /*
410 * If an exclusive group is already on, no other hardware
411 * counters can go on.
412 */
413 if (cpuctx->exclusive)
414 return 0;
415 /*
416 * If this group is exclusive and there are already
417 * counters on the CPU, it can't go on.
418 */
419 if (counter->hw_event.exclusive && cpuctx->active_oncpu)
420 return 0;
421 /*
422 * Otherwise, try to add it if all previous groups were able
423 * to go on.
424 */
425 return can_add_hw;
426}
427
428/*
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100429 * Cross CPU call to install and enable a performance counter
Thomas Gleixner0793a612008-12-04 20:12:29 +0100430 */
431static void __perf_install_in_context(void *info)
432{
433 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
434 struct perf_counter *counter = info;
435 struct perf_counter_context *ctx = counter->ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100436 struct perf_counter *leader = counter->group_leader;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100437 int cpu = smp_processor_id();
Ingo Molnar9b51f662008-12-12 13:49:45 +0100438 unsigned long flags;
Ingo Molnar5c92d122008-12-11 13:21:10 +0100439 u64 perf_flags;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100440 int err;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100441
442 /*
443 * If this is a task context, we need to check whether it is
444 * the current task context of this cpu. If not it has been
445 * scheduled out before the smp call arrived.
446 */
447 if (ctx->task && cpuctx->task_ctx != ctx)
448 return;
449
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100450 curr_rq_lock_irq_save(&flags);
451 spin_lock(&ctx->lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100452
453 /*
454 * Protect the list operation against NMI by disabling the
455 * counters on a global level. NOP for non NMI based counters.
456 */
Ingo Molnar01b28382008-12-11 13:45:51 +0100457 perf_flags = hw_perf_save_disable();
Thomas Gleixner0793a612008-12-04 20:12:29 +0100458
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100459 list_add_counter(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100460 ctx->nr_counters++;
Paul Mackerrasc07c99b2009-02-13 22:10:34 +1100461 counter->prev_state = PERF_COUNTER_STATE_OFF;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100462
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100463 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100464 * Don't put the counter on if it is disabled or if
465 * it is in a group and the group isn't on.
466 */
467 if (counter->state != PERF_COUNTER_STATE_INACTIVE ||
468 (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE))
469 goto unlock;
470
471 /*
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100472 * An exclusive counter can't go on if there are already active
473 * hardware counters, and no hardware counter can go on if there
474 * is already an exclusive counter on.
475 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100476 if (!group_can_go_on(counter, cpuctx, 1))
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100477 err = -EEXIST;
478 else
479 err = counter_sched_in(counter, cpuctx, ctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100480
Paul Mackerrasd859e292009-01-17 18:10:22 +1100481 if (err) {
482 /*
483 * This counter couldn't go on. If it is in a group
484 * then we have to pull the whole group off.
485 * If the counter group is pinned then put it in error state.
486 */
487 if (leader != counter)
488 group_sched_out(leader, cpuctx, ctx);
489 if (leader->hw_event.pinned)
490 leader->state = PERF_COUNTER_STATE_ERROR;
491 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100492
493 if (!err && !ctx->task && cpuctx->max_pertask)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100494 cpuctx->max_pertask--;
495
Paul Mackerrasd859e292009-01-17 18:10:22 +1100496 unlock:
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100497 hw_perf_restore(perf_flags);
498
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100499 spin_unlock(&ctx->lock);
500 curr_rq_unlock_irq_restore(&flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100501}
502
503/*
504 * Attach a performance counter to a context
505 *
506 * First we add the counter to the list with the hardware enable bit
507 * in counter->hw_config cleared.
508 *
509 * If the counter is attached to a task which is on a CPU we use a smp
510 * call to enable it in the task context. The task might have been
511 * scheduled away, but we check this in the smp call again.
Paul Mackerrasd859e292009-01-17 18:10:22 +1100512 *
513 * Must be called with ctx->mutex held.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100514 */
515static void
516perf_install_in_context(struct perf_counter_context *ctx,
517 struct perf_counter *counter,
518 int cpu)
519{
520 struct task_struct *task = ctx->task;
521
Thomas Gleixner0793a612008-12-04 20:12:29 +0100522 if (!task) {
523 /*
524 * Per cpu counters are installed via an smp call and
525 * the install is always sucessful.
526 */
527 smp_call_function_single(cpu, __perf_install_in_context,
528 counter, 1);
529 return;
530 }
531
532 counter->task = task;
533retry:
534 task_oncpu_function_call(task, __perf_install_in_context,
535 counter);
536
537 spin_lock_irq(&ctx->lock);
538 /*
Thomas Gleixner0793a612008-12-04 20:12:29 +0100539 * we need to retry the smp call.
540 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100541 if (ctx->is_active && list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100542 spin_unlock_irq(&ctx->lock);
543 goto retry;
544 }
545
546 /*
547 * The lock prevents that this context is scheduled in so we
548 * can add the counter safely, if it the call above did not
549 * succeed.
550 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100551 if (list_empty(&counter->list_entry)) {
552 list_add_counter(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100553 ctx->nr_counters++;
554 }
555 spin_unlock_irq(&ctx->lock);
556}
557
Paul Mackerrasd859e292009-01-17 18:10:22 +1100558/*
559 * Cross CPU call to enable a performance counter
560 */
561static void __perf_counter_enable(void *info)
Ingo Molnar04289bb2008-12-11 08:38:42 +0100562{
Paul Mackerrasd859e292009-01-17 18:10:22 +1100563 struct perf_counter *counter = info;
564 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
565 struct perf_counter_context *ctx = counter->ctx;
566 struct perf_counter *leader = counter->group_leader;
567 unsigned long flags;
568 int err;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100569
570 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100571 * If this is a per-task counter, need to check whether this
572 * counter's task is the current task on this cpu.
Ingo Molnar04289bb2008-12-11 08:38:42 +0100573 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100574 if (ctx->task && cpuctx->task_ctx != ctx)
575 return;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100576
Paul Mackerrasd859e292009-01-17 18:10:22 +1100577 curr_rq_lock_irq_save(&flags);
578 spin_lock(&ctx->lock);
579
Paul Mackerrasc07c99b2009-02-13 22:10:34 +1100580 counter->prev_state = counter->state;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100581 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
582 goto unlock;
583 counter->state = PERF_COUNTER_STATE_INACTIVE;
584
585 /*
586 * If the counter is in a group and isn't the group leader,
587 * then don't put it on unless the group is on.
588 */
589 if (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE)
590 goto unlock;
591
592 if (!group_can_go_on(counter, cpuctx, 1))
593 err = -EEXIST;
594 else
595 err = counter_sched_in(counter, cpuctx, ctx,
596 smp_processor_id());
597
598 if (err) {
599 /*
600 * If this counter can't go on and it's part of a
601 * group, then the whole group has to come off.
602 */
603 if (leader != counter)
604 group_sched_out(leader, cpuctx, ctx);
605 if (leader->hw_event.pinned)
606 leader->state = PERF_COUNTER_STATE_ERROR;
607 }
608
609 unlock:
610 spin_unlock(&ctx->lock);
611 curr_rq_unlock_irq_restore(&flags);
612}
613
614/*
615 * Enable a counter.
616 */
617static void perf_counter_enable(struct perf_counter *counter)
618{
619 struct perf_counter_context *ctx = counter->ctx;
620 struct task_struct *task = ctx->task;
621
622 if (!task) {
623 /*
624 * Enable the counter on the cpu that it's on
625 */
626 smp_call_function_single(counter->cpu, __perf_counter_enable,
627 counter, 1);
628 return;
629 }
630
631 spin_lock_irq(&ctx->lock);
632 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
633 goto out;
634
635 /*
636 * If the counter is in error state, clear that first.
637 * That way, if we see the counter in error state below, we
638 * know that it has gone back into error state, as distinct
639 * from the task having been scheduled away before the
640 * cross-call arrived.
641 */
642 if (counter->state == PERF_COUNTER_STATE_ERROR)
643 counter->state = PERF_COUNTER_STATE_OFF;
644
645 retry:
646 spin_unlock_irq(&ctx->lock);
647 task_oncpu_function_call(task, __perf_counter_enable, counter);
648
649 spin_lock_irq(&ctx->lock);
650
651 /*
652 * If the context is active and the counter is still off,
653 * we need to retry the cross-call.
654 */
655 if (ctx->is_active && counter->state == PERF_COUNTER_STATE_OFF)
656 goto retry;
657
658 /*
659 * Since we have the lock this context can't be scheduled
660 * in, so we can change the state safely.
661 */
662 if (counter->state == PERF_COUNTER_STATE_OFF)
663 counter->state = PERF_COUNTER_STATE_INACTIVE;
664 out:
665 spin_unlock_irq(&ctx->lock);
666}
667
668/*
669 * Enable a counter and all its children.
670 */
671static void perf_counter_enable_family(struct perf_counter *counter)
672{
673 struct perf_counter *child;
674
675 perf_counter_enable(counter);
676
677 /*
678 * Lock the mutex to protect the list of children
679 */
680 mutex_lock(&counter->mutex);
681 list_for_each_entry(child, &counter->child_list, child_list)
682 perf_counter_enable(child);
683 mutex_unlock(&counter->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100684}
685
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100686void __perf_counter_sched_out(struct perf_counter_context *ctx,
687 struct perf_cpu_context *cpuctx)
688{
689 struct perf_counter *counter;
Paul Mackerras3cbed422009-01-09 16:43:42 +1100690 u64 flags;
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100691
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100692 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100693 ctx->is_active = 0;
694 if (likely(!ctx->nr_counters))
695 goto out;
696
Paul Mackerras3cbed422009-01-09 16:43:42 +1100697 flags = hw_perf_save_disable();
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100698 if (ctx->nr_active) {
699 list_for_each_entry(counter, &ctx->counter_list, list_entry)
700 group_sched_out(counter, cpuctx, ctx);
701 }
Paul Mackerras3cbed422009-01-09 16:43:42 +1100702 hw_perf_restore(flags);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100703 out:
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100704 spin_unlock(&ctx->lock);
705}
706
Thomas Gleixner0793a612008-12-04 20:12:29 +0100707/*
708 * Called from scheduler to remove the counters of the current task,
709 * with interrupts disabled.
710 *
711 * We stop each counter and update the counter value in counter->count.
712 *
Ingo Molnar76715812008-12-17 14:20:28 +0100713 * This does not protect us against NMI, but disable()
Thomas Gleixner0793a612008-12-04 20:12:29 +0100714 * sets the disabled bit in the control field of counter _before_
715 * accessing the counter control register. If a NMI hits, then it will
716 * not restart the counter.
717 */
718void perf_counter_task_sched_out(struct task_struct *task, int cpu)
719{
720 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
721 struct perf_counter_context *ctx = &task->perf_counter_ctx;
Peter Zijlstra4a0deca2009-03-19 20:26:12 +0100722 struct pt_regs *regs;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100723
724 if (likely(!cpuctx->task_ctx))
725 return;
726
Peter Zijlstra4a0deca2009-03-19 20:26:12 +0100727 regs = task_pt_regs(task);
728 perf_swcounter_event(PERF_COUNT_CONTEXT_SWITCHES, 1, 1, regs);
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100729 __perf_counter_sched_out(ctx, cpuctx);
730
Thomas Gleixner0793a612008-12-04 20:12:29 +0100731 cpuctx->task_ctx = NULL;
732}
733
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100734static void perf_counter_cpu_sched_out(struct perf_cpu_context *cpuctx)
Ingo Molnar04289bb2008-12-11 08:38:42 +0100735{
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100736 __perf_counter_sched_out(&cpuctx->ctx, cpuctx);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100737}
738
Ingo Molnar79958882008-12-17 08:54:56 +0100739static int
Ingo Molnar04289bb2008-12-11 08:38:42 +0100740group_sched_in(struct perf_counter *group_counter,
741 struct perf_cpu_context *cpuctx,
742 struct perf_counter_context *ctx,
743 int cpu)
744{
Ingo Molnar95cdd2e2008-12-21 13:50:42 +0100745 struct perf_counter *counter, *partial_group;
Paul Mackerras3cbed422009-01-09 16:43:42 +1100746 int ret;
747
748 if (group_counter->state == PERF_COUNTER_STATE_OFF)
749 return 0;
750
751 ret = hw_perf_group_sched_in(group_counter, cpuctx, ctx, cpu);
752 if (ret)
753 return ret < 0 ? ret : 0;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100754
Paul Mackerrasc07c99b2009-02-13 22:10:34 +1100755 group_counter->prev_state = group_counter->state;
Ingo Molnar95cdd2e2008-12-21 13:50:42 +0100756 if (counter_sched_in(group_counter, cpuctx, ctx, cpu))
757 return -EAGAIN;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100758
759 /*
760 * Schedule in siblings as one group (if any):
761 */
Ingo Molnar79958882008-12-17 08:54:56 +0100762 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
Paul Mackerrasc07c99b2009-02-13 22:10:34 +1100763 counter->prev_state = counter->state;
Ingo Molnar95cdd2e2008-12-21 13:50:42 +0100764 if (counter_sched_in(counter, cpuctx, ctx, cpu)) {
765 partial_group = counter;
766 goto group_error;
767 }
Ingo Molnar79958882008-12-17 08:54:56 +0100768 }
769
Paul Mackerras3cbed422009-01-09 16:43:42 +1100770 return 0;
Ingo Molnar95cdd2e2008-12-21 13:50:42 +0100771
772group_error:
773 /*
774 * Groups can be scheduled in as one unit only, so undo any
775 * partial group before returning:
776 */
777 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
778 if (counter == partial_group)
779 break;
780 counter_sched_out(counter, cpuctx, ctx);
781 }
782 counter_sched_out(group_counter, cpuctx, ctx);
783
784 return -EAGAIN;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100785}
786
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100787static void
788__perf_counter_sched_in(struct perf_counter_context *ctx,
789 struct perf_cpu_context *cpuctx, int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100790{
Thomas Gleixner0793a612008-12-04 20:12:29 +0100791 struct perf_counter *counter;
Paul Mackerras3cbed422009-01-09 16:43:42 +1100792 u64 flags;
Paul Mackerrasdd0e6ba2009-01-12 15:11:00 +1100793 int can_add_hw = 1;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100794
Thomas Gleixner0793a612008-12-04 20:12:29 +0100795 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100796 ctx->is_active = 1;
797 if (likely(!ctx->nr_counters))
798 goto out;
799
Paul Mackerras3cbed422009-01-09 16:43:42 +1100800 flags = hw_perf_save_disable();
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100801
802 /*
803 * First go through the list and put on any pinned groups
804 * in order to give them the best chance of going on.
805 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100806 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100807 if (counter->state <= PERF_COUNTER_STATE_OFF ||
808 !counter->hw_event.pinned)
809 continue;
810 if (counter->cpu != -1 && counter->cpu != cpu)
811 continue;
812
813 if (group_can_go_on(counter, cpuctx, 1))
814 group_sched_in(counter, cpuctx, ctx, cpu);
815
816 /*
817 * If this pinned group hasn't been scheduled,
818 * put it in error state.
819 */
820 if (counter->state == PERF_COUNTER_STATE_INACTIVE)
821 counter->state = PERF_COUNTER_STATE_ERROR;
822 }
823
824 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
825 /*
826 * Ignore counters in OFF or ERROR state, and
827 * ignore pinned counters since we did them already.
828 */
829 if (counter->state <= PERF_COUNTER_STATE_OFF ||
830 counter->hw_event.pinned)
831 continue;
832
Ingo Molnar04289bb2008-12-11 08:38:42 +0100833 /*
834 * Listen to the 'cpu' scheduling filter constraint
835 * of counters:
836 */
Thomas Gleixner0793a612008-12-04 20:12:29 +0100837 if (counter->cpu != -1 && counter->cpu != cpu)
838 continue;
839
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100840 if (group_can_go_on(counter, cpuctx, can_add_hw)) {
Paul Mackerrasdd0e6ba2009-01-12 15:11:00 +1100841 if (group_sched_in(counter, cpuctx, ctx, cpu))
842 can_add_hw = 0;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100843 }
Thomas Gleixner0793a612008-12-04 20:12:29 +0100844 }
Paul Mackerras3cbed422009-01-09 16:43:42 +1100845 hw_perf_restore(flags);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100846 out:
Thomas Gleixner0793a612008-12-04 20:12:29 +0100847 spin_unlock(&ctx->lock);
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100848}
Ingo Molnar04289bb2008-12-11 08:38:42 +0100849
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100850/*
851 * Called from scheduler to add the counters of the current task
852 * with interrupts disabled.
853 *
854 * We restore the counter value and then enable it.
855 *
856 * This does not protect us against NMI, but enable()
857 * sets the enabled bit in the control field of counter _before_
858 * accessing the counter control register. If a NMI hits, then it will
859 * keep the counter running.
860 */
861void perf_counter_task_sched_in(struct task_struct *task, int cpu)
862{
863 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
864 struct perf_counter_context *ctx = &task->perf_counter_ctx;
865
866 __perf_counter_sched_in(ctx, cpuctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100867 cpuctx->task_ctx = ctx;
868}
869
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100870static void perf_counter_cpu_sched_in(struct perf_cpu_context *cpuctx, int cpu)
871{
872 struct perf_counter_context *ctx = &cpuctx->ctx;
873
874 __perf_counter_sched_in(ctx, cpuctx, cpu);
875}
876
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100877int perf_counter_task_disable(void)
878{
879 struct task_struct *curr = current;
880 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
881 struct perf_counter *counter;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100882 unsigned long flags;
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100883 u64 perf_flags;
884 int cpu;
885
886 if (likely(!ctx->nr_counters))
887 return 0;
888
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100889 curr_rq_lock_irq_save(&flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100890 cpu = smp_processor_id();
891
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100892 /* force the update of the task clock: */
893 __task_delta_exec(curr, 1);
894
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100895 perf_counter_task_sched_out(curr, cpu);
896
897 spin_lock(&ctx->lock);
898
899 /*
900 * Disable all the counters:
901 */
902 perf_flags = hw_perf_save_disable();
903
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100904 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
905 if (counter->state != PERF_COUNTER_STATE_ERROR)
906 counter->state = PERF_COUNTER_STATE_OFF;
907 }
Ingo Molnar9b51f662008-12-12 13:49:45 +0100908
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100909 hw_perf_restore(perf_flags);
910
911 spin_unlock(&ctx->lock);
912
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100913 curr_rq_unlock_irq_restore(&flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100914
915 return 0;
916}
917
918int perf_counter_task_enable(void)
919{
920 struct task_struct *curr = current;
921 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
922 struct perf_counter *counter;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100923 unsigned long flags;
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100924 u64 perf_flags;
925 int cpu;
926
927 if (likely(!ctx->nr_counters))
928 return 0;
929
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100930 curr_rq_lock_irq_save(&flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100931 cpu = smp_processor_id();
932
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100933 /* force the update of the task clock: */
934 __task_delta_exec(curr, 1);
935
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100936 perf_counter_task_sched_out(curr, cpu);
937
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100938 spin_lock(&ctx->lock);
939
940 /*
941 * Disable all the counters:
942 */
943 perf_flags = hw_perf_save_disable();
944
945 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100946 if (counter->state > PERF_COUNTER_STATE_OFF)
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100947 continue;
Ingo Molnar6a930702008-12-11 15:17:03 +0100948 counter->state = PERF_COUNTER_STATE_INACTIVE;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100949 counter->hw_event.disabled = 0;
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100950 }
951 hw_perf_restore(perf_flags);
952
953 spin_unlock(&ctx->lock);
954
955 perf_counter_task_sched_in(curr, cpu);
956
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100957 curr_rq_unlock_irq_restore(&flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100958
959 return 0;
960}
961
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100962/*
963 * Round-robin a context's counters:
964 */
965static void rotate_ctx(struct perf_counter_context *ctx)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100966{
Thomas Gleixner0793a612008-12-04 20:12:29 +0100967 struct perf_counter *counter;
Ingo Molnar5c92d122008-12-11 13:21:10 +0100968 u64 perf_flags;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100969
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100970 if (!ctx->nr_counters)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100971 return;
972
Thomas Gleixner0793a612008-12-04 20:12:29 +0100973 spin_lock(&ctx->lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100974 /*
Ingo Molnar04289bb2008-12-11 08:38:42 +0100975 * Rotate the first entry last (works just fine for group counters too):
Thomas Gleixner0793a612008-12-04 20:12:29 +0100976 */
Ingo Molnar01b28382008-12-11 13:45:51 +0100977 perf_flags = hw_perf_save_disable();
Ingo Molnar04289bb2008-12-11 08:38:42 +0100978 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Peter Zijlstra75564232009-03-13 12:21:29 +0100979 list_move_tail(&counter->list_entry, &ctx->counter_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100980 break;
981 }
Ingo Molnar01b28382008-12-11 13:45:51 +0100982 hw_perf_restore(perf_flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100983
984 spin_unlock(&ctx->lock);
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100985}
Thomas Gleixner0793a612008-12-04 20:12:29 +0100986
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100987void perf_counter_task_tick(struct task_struct *curr, int cpu)
988{
989 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
990 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
991 const int rotate_percpu = 0;
992
993 if (rotate_percpu)
994 perf_counter_cpu_sched_out(cpuctx);
995 perf_counter_task_sched_out(curr, cpu);
996
997 if (rotate_percpu)
998 rotate_ctx(&cpuctx->ctx);
999 rotate_ctx(ctx);
1000
1001 if (rotate_percpu)
1002 perf_counter_cpu_sched_in(cpuctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001003 perf_counter_task_sched_in(curr, cpu);
1004}
1005
1006/*
Thomas Gleixner0793a612008-12-04 20:12:29 +01001007 * Cross CPU call to read the hardware counter
1008 */
Ingo Molnar76715812008-12-17 14:20:28 +01001009static void __read(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001010{
Ingo Molnar621a01e2008-12-11 12:46:46 +01001011 struct perf_counter *counter = info;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001012 unsigned long flags;
Ingo Molnar621a01e2008-12-11 12:46:46 +01001013
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001014 curr_rq_lock_irq_save(&flags);
Ingo Molnar76715812008-12-17 14:20:28 +01001015 counter->hw_ops->read(counter);
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001016 curr_rq_unlock_irq_restore(&flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001017}
1018
Ingo Molnar04289bb2008-12-11 08:38:42 +01001019static u64 perf_counter_read(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001020{
1021 /*
1022 * If counter is enabled and currently active on a CPU, update the
1023 * value in the counter structure:
1024 */
Ingo Molnar6a930702008-12-11 15:17:03 +01001025 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
Thomas Gleixner0793a612008-12-04 20:12:29 +01001026 smp_call_function_single(counter->oncpu,
Ingo Molnar76715812008-12-17 14:20:28 +01001027 __read, counter, 1);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001028 }
1029
Ingo Molnaree060942008-12-13 09:00:03 +01001030 return atomic64_read(&counter->count);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001031}
1032
Thomas Gleixner0793a612008-12-04 20:12:29 +01001033static void put_context(struct perf_counter_context *ctx)
1034{
1035 if (ctx->task)
1036 put_task_struct(ctx->task);
1037}
1038
1039static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
1040{
1041 struct perf_cpu_context *cpuctx;
1042 struct perf_counter_context *ctx;
1043 struct task_struct *task;
1044
1045 /*
1046 * If cpu is not a wildcard then this is a percpu counter:
1047 */
1048 if (cpu != -1) {
1049 /* Must be root to operate on a CPU counter: */
1050 if (!capable(CAP_SYS_ADMIN))
1051 return ERR_PTR(-EACCES);
1052
1053 if (cpu < 0 || cpu > num_possible_cpus())
1054 return ERR_PTR(-EINVAL);
1055
1056 /*
1057 * We could be clever and allow to attach a counter to an
1058 * offline CPU and activate it when the CPU comes up, but
1059 * that's for later.
1060 */
1061 if (!cpu_isset(cpu, cpu_online_map))
1062 return ERR_PTR(-ENODEV);
1063
1064 cpuctx = &per_cpu(perf_cpu_context, cpu);
1065 ctx = &cpuctx->ctx;
1066
Thomas Gleixner0793a612008-12-04 20:12:29 +01001067 return ctx;
1068 }
1069
1070 rcu_read_lock();
1071 if (!pid)
1072 task = current;
1073 else
1074 task = find_task_by_vpid(pid);
1075 if (task)
1076 get_task_struct(task);
1077 rcu_read_unlock();
1078
1079 if (!task)
1080 return ERR_PTR(-ESRCH);
1081
1082 ctx = &task->perf_counter_ctx;
1083 ctx->task = task;
1084
1085 /* Reuse ptrace permission checks for now. */
1086 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
1087 put_context(ctx);
1088 return ERR_PTR(-EACCES);
1089 }
1090
1091 return ctx;
1092}
1093
Peter Zijlstra592903c2009-03-13 12:21:36 +01001094static void free_counter_rcu(struct rcu_head *head)
1095{
1096 struct perf_counter *counter;
1097
1098 counter = container_of(head, struct perf_counter, rcu_head);
1099 kfree(counter);
1100}
1101
Peter Zijlstraf1600952009-03-19 20:26:16 +01001102static void free_counter(struct perf_counter *counter)
1103{
Peter Zijlstrae077df42009-03-19 20:26:17 +01001104 if (counter->destroy)
1105 counter->destroy(counter);
1106
Peter Zijlstraf1600952009-03-19 20:26:16 +01001107 call_rcu(&counter->rcu_head, free_counter_rcu);
1108}
1109
Thomas Gleixner0793a612008-12-04 20:12:29 +01001110/*
1111 * Called when the last reference to the file is gone.
1112 */
1113static int perf_release(struct inode *inode, struct file *file)
1114{
1115 struct perf_counter *counter = file->private_data;
1116 struct perf_counter_context *ctx = counter->ctx;
1117
1118 file->private_data = NULL;
1119
Paul Mackerrasd859e292009-01-17 18:10:22 +11001120 mutex_lock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001121 mutex_lock(&counter->mutex);
1122
Ingo Molnar04289bb2008-12-11 08:38:42 +01001123 perf_counter_remove_from_context(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001124
1125 mutex_unlock(&counter->mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001126 mutex_unlock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001127
Peter Zijlstraf1600952009-03-19 20:26:16 +01001128 free_counter(counter);
Mike Galbraith5af75912009-02-11 10:53:37 +01001129 put_context(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001130
1131 return 0;
1132}
1133
1134/*
1135 * Read the performance counter - simple non blocking version for now
1136 */
1137static ssize_t
1138perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
1139{
1140 u64 cntval;
1141
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001142 if (count < sizeof(cntval))
Thomas Gleixner0793a612008-12-04 20:12:29 +01001143 return -EINVAL;
1144
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001145 /*
1146 * Return end-of-file for a read on a counter that is in
1147 * error state (i.e. because it was pinned but it couldn't be
1148 * scheduled on to the CPU at some point).
1149 */
1150 if (counter->state == PERF_COUNTER_STATE_ERROR)
1151 return 0;
1152
Thomas Gleixner0793a612008-12-04 20:12:29 +01001153 mutex_lock(&counter->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01001154 cntval = perf_counter_read(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001155 mutex_unlock(&counter->mutex);
1156
1157 return put_user(cntval, (u64 __user *) buf) ? -EFAULT : sizeof(cntval);
1158}
1159
1160static ssize_t
Thomas Gleixner0793a612008-12-04 20:12:29 +01001161perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
1162{
1163 struct perf_counter *counter = file->private_data;
1164
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001165 return perf_read_hw(counter, buf, count);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001166}
1167
1168static unsigned int perf_poll(struct file *file, poll_table *wait)
1169{
1170 struct perf_counter *counter = file->private_data;
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001171 struct perf_mmap_data *data;
1172 unsigned int events;
1173
1174 rcu_read_lock();
1175 data = rcu_dereference(counter->data);
1176 if (data)
1177 events = atomic_xchg(&data->wakeup, 0);
1178 else
1179 events = POLL_HUP;
1180 rcu_read_unlock();
Thomas Gleixner0793a612008-12-04 20:12:29 +01001181
1182 poll_wait(file, &counter->waitq, wait);
1183
Thomas Gleixner0793a612008-12-04 20:12:29 +01001184 return events;
1185}
1186
Paul Mackerrasd859e292009-01-17 18:10:22 +11001187static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1188{
1189 struct perf_counter *counter = file->private_data;
1190 int err = 0;
1191
1192 switch (cmd) {
1193 case PERF_COUNTER_IOC_ENABLE:
1194 perf_counter_enable_family(counter);
1195 break;
1196 case PERF_COUNTER_IOC_DISABLE:
1197 perf_counter_disable_family(counter);
1198 break;
1199 default:
1200 err = -ENOTTY;
1201 }
1202 return err;
1203}
1204
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001205static void __perf_counter_update_userpage(struct perf_counter *counter,
1206 struct perf_mmap_data *data)
Paul Mackerras37d81822009-03-23 18:22:08 +01001207{
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001208 struct perf_counter_mmap_page *userpg = data->user_page;
Paul Mackerras37d81822009-03-23 18:22:08 +01001209
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001210 /*
1211 * Disable preemption so as to not let the corresponding user-space
1212 * spin too long if we get preempted.
1213 */
1214 preempt_disable();
Paul Mackerras37d81822009-03-23 18:22:08 +01001215 ++userpg->lock;
1216 smp_wmb();
1217 userpg->index = counter->hw.idx;
1218 userpg->offset = atomic64_read(&counter->count);
1219 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
1220 userpg->offset -= atomic64_read(&counter->hw.prev_count);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001221
1222 userpg->data_head = atomic_read(&data->head);
Paul Mackerras37d81822009-03-23 18:22:08 +01001223 smp_wmb();
1224 ++userpg->lock;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001225 preempt_enable();
1226}
1227
1228void perf_counter_update_userpage(struct perf_counter *counter)
1229{
1230 struct perf_mmap_data *data;
1231
1232 rcu_read_lock();
1233 data = rcu_dereference(counter->data);
1234 if (data)
1235 __perf_counter_update_userpage(counter, data);
1236 rcu_read_unlock();
Paul Mackerras37d81822009-03-23 18:22:08 +01001237}
1238
1239static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1240{
1241 struct perf_counter *counter = vma->vm_file->private_data;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001242 struct perf_mmap_data *data;
1243 int ret = VM_FAULT_SIGBUS;
Paul Mackerras37d81822009-03-23 18:22:08 +01001244
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001245 rcu_read_lock();
1246 data = rcu_dereference(counter->data);
1247 if (!data)
1248 goto unlock;
Paul Mackerras37d81822009-03-23 18:22:08 +01001249
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001250 if (vmf->pgoff == 0) {
1251 vmf->page = virt_to_page(data->user_page);
1252 } else {
1253 int nr = vmf->pgoff - 1;
1254
1255 if ((unsigned)nr > data->nr_pages)
1256 goto unlock;
1257
1258 vmf->page = virt_to_page(data->data_pages[nr]);
1259 }
Paul Mackerras37d81822009-03-23 18:22:08 +01001260 get_page(vmf->page);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001261 ret = 0;
1262unlock:
1263 rcu_read_unlock();
1264
1265 return ret;
1266}
1267
1268static int perf_mmap_data_alloc(struct perf_counter *counter, int nr_pages)
1269{
1270 struct perf_mmap_data *data;
1271 unsigned long size;
1272 int i;
1273
1274 WARN_ON(atomic_read(&counter->mmap_count));
1275
1276 size = sizeof(struct perf_mmap_data);
1277 size += nr_pages * sizeof(void *);
1278
1279 data = kzalloc(size, GFP_KERNEL);
1280 if (!data)
1281 goto fail;
1282
1283 data->user_page = (void *)get_zeroed_page(GFP_KERNEL);
1284 if (!data->user_page)
1285 goto fail_user_page;
1286
1287 for (i = 0; i < nr_pages; i++) {
1288 data->data_pages[i] = (void *)get_zeroed_page(GFP_KERNEL);
1289 if (!data->data_pages[i])
1290 goto fail_data_pages;
1291 }
1292
1293 data->nr_pages = nr_pages;
1294
1295 rcu_assign_pointer(counter->data, data);
1296
Paul Mackerras37d81822009-03-23 18:22:08 +01001297 return 0;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001298
1299fail_data_pages:
1300 for (i--; i >= 0; i--)
1301 free_page((unsigned long)data->data_pages[i]);
1302
1303 free_page((unsigned long)data->user_page);
1304
1305fail_user_page:
1306 kfree(data);
1307
1308fail:
1309 return -ENOMEM;
1310}
1311
1312static void __perf_mmap_data_free(struct rcu_head *rcu_head)
1313{
1314 struct perf_mmap_data *data = container_of(rcu_head,
1315 struct perf_mmap_data, rcu_head);
1316 int i;
1317
1318 free_page((unsigned long)data->user_page);
1319 for (i = 0; i < data->nr_pages; i++)
1320 free_page((unsigned long)data->data_pages[i]);
1321 kfree(data);
1322}
1323
1324static void perf_mmap_data_free(struct perf_counter *counter)
1325{
1326 struct perf_mmap_data *data = counter->data;
1327
1328 WARN_ON(atomic_read(&counter->mmap_count));
1329
1330 rcu_assign_pointer(counter->data, NULL);
1331 call_rcu(&data->rcu_head, __perf_mmap_data_free);
1332}
1333
1334static void perf_mmap_open(struct vm_area_struct *vma)
1335{
1336 struct perf_counter *counter = vma->vm_file->private_data;
1337
1338 atomic_inc(&counter->mmap_count);
1339}
1340
1341static void perf_mmap_close(struct vm_area_struct *vma)
1342{
1343 struct perf_counter *counter = vma->vm_file->private_data;
1344
1345 if (atomic_dec_and_mutex_lock(&counter->mmap_count,
1346 &counter->mmap_mutex)) {
1347 perf_mmap_data_free(counter);
1348 mutex_unlock(&counter->mmap_mutex);
1349 }
Paul Mackerras37d81822009-03-23 18:22:08 +01001350}
1351
1352static struct vm_operations_struct perf_mmap_vmops = {
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001353 .open = perf_mmap_open,
1354 .close = perf_mmap_close,
Paul Mackerras37d81822009-03-23 18:22:08 +01001355 .fault = perf_mmap_fault,
1356};
1357
1358static int perf_mmap(struct file *file, struct vm_area_struct *vma)
1359{
1360 struct perf_counter *counter = file->private_data;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001361 unsigned long vma_size;
1362 unsigned long nr_pages;
1363 unsigned long locked, lock_limit;
1364 int ret = 0;
Paul Mackerras37d81822009-03-23 18:22:08 +01001365
1366 if (!(vma->vm_flags & VM_SHARED) || (vma->vm_flags & VM_WRITE))
1367 return -EINVAL;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001368
1369 vma_size = vma->vm_end - vma->vm_start;
1370 nr_pages = (vma_size / PAGE_SIZE) - 1;
1371
1372 if (nr_pages == 0 || !is_power_of_2(nr_pages))
Paul Mackerras37d81822009-03-23 18:22:08 +01001373 return -EINVAL;
1374
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001375 if (vma_size != PAGE_SIZE * (1 + nr_pages))
Paul Mackerras37d81822009-03-23 18:22:08 +01001376 return -EINVAL;
1377
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001378 if (vma->vm_pgoff != 0)
1379 return -EINVAL;
Paul Mackerras37d81822009-03-23 18:22:08 +01001380
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001381 locked = vma_size >> PAGE_SHIFT;
1382 locked += vma->vm_mm->locked_vm;
1383
1384 lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
1385 lock_limit >>= PAGE_SHIFT;
1386
1387 if ((locked > lock_limit) && !capable(CAP_IPC_LOCK))
1388 return -EPERM;
1389
1390 mutex_lock(&counter->mmap_mutex);
1391 if (atomic_inc_not_zero(&counter->mmap_count))
1392 goto out;
1393
1394 WARN_ON(counter->data);
1395 ret = perf_mmap_data_alloc(counter, nr_pages);
1396 if (!ret)
1397 atomic_set(&counter->mmap_count, 1);
1398out:
1399 mutex_unlock(&counter->mmap_mutex);
Paul Mackerras37d81822009-03-23 18:22:08 +01001400
1401 vma->vm_flags &= ~VM_MAYWRITE;
1402 vma->vm_flags |= VM_RESERVED;
1403 vma->vm_ops = &perf_mmap_vmops;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001404
1405 return ret;
Paul Mackerras37d81822009-03-23 18:22:08 +01001406}
1407
Thomas Gleixner0793a612008-12-04 20:12:29 +01001408static const struct file_operations perf_fops = {
1409 .release = perf_release,
1410 .read = perf_read,
1411 .poll = perf_poll,
Paul Mackerrasd859e292009-01-17 18:10:22 +11001412 .unlocked_ioctl = perf_ioctl,
1413 .compat_ioctl = perf_ioctl,
Paul Mackerras37d81822009-03-23 18:22:08 +01001414 .mmap = perf_mmap,
Thomas Gleixner0793a612008-12-04 20:12:29 +01001415};
1416
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001417/*
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001418 * Output
1419 */
1420
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001421struct perf_output_handle {
1422 struct perf_counter *counter;
1423 struct perf_mmap_data *data;
1424 unsigned int offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01001425 unsigned int head;
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001426 int wakeup;
1427};
1428
1429static int perf_output_begin(struct perf_output_handle *handle,
1430 struct perf_counter *counter, unsigned int size)
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001431{
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001432 struct perf_mmap_data *data;
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001433 unsigned int offset, head;
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001434
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001435 rcu_read_lock();
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001436 data = rcu_dereference(counter->data);
1437 if (!data)
1438 goto out;
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001439
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001440 if (!data->nr_pages)
1441 goto out;
1442
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001443 do {
1444 offset = head = atomic_read(&data->head);
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001445 head += size;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001446 } while (atomic_cmpxchg(&data->head, offset, head) != offset);
1447
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001448 handle->counter = counter;
1449 handle->data = data;
1450 handle->offset = offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01001451 handle->head = head;
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001452 handle->wakeup = (offset >> PAGE_SHIFT) != (head >> PAGE_SHIFT);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001453
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001454 return 0;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001455
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001456out:
1457 rcu_read_unlock();
1458
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001459 return -ENOSPC;
1460}
1461
1462static void perf_output_copy(struct perf_output_handle *handle,
1463 void *buf, unsigned int len)
1464{
1465 unsigned int pages_mask;
1466 unsigned int offset;
1467 unsigned int size;
1468 void **pages;
1469
1470 offset = handle->offset;
1471 pages_mask = handle->data->nr_pages - 1;
1472 pages = handle->data->data_pages;
1473
1474 do {
1475 unsigned int page_offset;
1476 int nr;
1477
1478 nr = (offset >> PAGE_SHIFT) & pages_mask;
1479 page_offset = offset & (PAGE_SIZE - 1);
1480 size = min_t(unsigned int, PAGE_SIZE - page_offset, len);
1481
1482 memcpy(pages[nr] + page_offset, buf, size);
1483
1484 len -= size;
1485 buf += size;
1486 offset += size;
1487 } while (len);
1488
1489 handle->offset = offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01001490
1491 WARN_ON_ONCE(handle->offset > handle->head);
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001492}
1493
Peter Zijlstra5c148192009-03-25 12:30:23 +01001494#define perf_output_put(handle, x) \
1495 perf_output_copy((handle), &(x), sizeof(x))
1496
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001497static void perf_output_end(struct perf_output_handle *handle, int nmi)
1498{
1499 if (handle->wakeup) {
1500 (void)atomic_xchg(&handle->data->wakeup, POLL_IN);
1501 __perf_counter_update_userpage(handle->counter, handle->data);
1502 if (nmi) {
1503 handle->counter->wakeup_pending = 1;
1504 set_perf_counter_pending();
1505 } else
1506 wake_up(&handle->counter->waitq);
1507 }
1508 rcu_read_unlock();
1509}
1510
1511static int perf_output_write(struct perf_counter *counter, int nmi,
1512 void *buf, ssize_t size)
1513{
1514 struct perf_output_handle handle;
1515 int ret;
1516
1517 ret = perf_output_begin(&handle, counter, size);
1518 if (ret)
1519 goto out;
1520
1521 perf_output_copy(&handle, buf, size);
1522 perf_output_end(&handle, nmi);
1523
1524out:
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001525 return ret;
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001526}
1527
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001528static void perf_output_simple(struct perf_counter *counter,
1529 int nmi, struct pt_regs *regs)
1530{
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01001531 unsigned int size;
Peter Zijlstra5c148192009-03-25 12:30:23 +01001532 struct {
1533 struct perf_event_header header;
1534 u64 ip;
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01001535 u32 pid, tid;
Peter Zijlstra5c148192009-03-25 12:30:23 +01001536 } event;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001537
Peter Zijlstra5c148192009-03-25 12:30:23 +01001538 event.header.type = PERF_EVENT_IP;
Peter Zijlstra5c148192009-03-25 12:30:23 +01001539 event.ip = instruction_pointer(regs);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001540
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01001541 size = sizeof(event);
1542
1543 if (counter->hw_event.include_tid) {
1544 /* namespace issues */
1545 event.pid = current->group_leader->pid;
1546 event.tid = current->pid;
1547
1548 event.header.type |= __PERF_EVENT_TID;
1549 } else
1550 size -= sizeof(u64);
1551
1552 event.header.size = size;
1553
1554 perf_output_write(counter, nmi, &event, size);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001555}
1556
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001557static void perf_output_group(struct perf_counter *counter, int nmi)
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001558{
Peter Zijlstra5c148192009-03-25 12:30:23 +01001559 struct perf_output_handle handle;
1560 struct perf_event_header header;
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001561 struct perf_counter *leader, *sub;
Peter Zijlstra5c148192009-03-25 12:30:23 +01001562 unsigned int size;
1563 struct {
1564 u64 event;
1565 u64 counter;
1566 } entry;
1567 int ret;
1568
1569 size = sizeof(header) + counter->nr_siblings * sizeof(entry);
1570
1571 ret = perf_output_begin(&handle, counter, size);
1572 if (ret)
1573 return;
1574
1575 header.type = PERF_EVENT_GROUP;
1576 header.size = size;
1577
1578 perf_output_put(&handle, header);
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001579
1580 leader = counter->group_leader;
1581 list_for_each_entry(sub, &leader->sibling_list, list_entry) {
1582 if (sub != counter)
1583 sub->hw_ops->read(sub);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001584
1585 entry.event = sub->hw_event.config;
1586 entry.counter = atomic64_read(&sub->count);
1587
Peter Zijlstra5c148192009-03-25 12:30:23 +01001588 perf_output_put(&handle, entry);
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001589 }
Peter Zijlstra5c148192009-03-25 12:30:23 +01001590
1591 perf_output_end(&handle, nmi);
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001592}
1593
1594void perf_counter_output(struct perf_counter *counter,
1595 int nmi, struct pt_regs *regs)
1596{
1597 switch (counter->hw_event.record_type) {
1598 case PERF_RECORD_SIMPLE:
1599 return;
1600
1601 case PERF_RECORD_IRQ:
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001602 perf_output_simple(counter, nmi, regs);
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001603 break;
1604
1605 case PERF_RECORD_GROUP:
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001606 perf_output_group(counter, nmi);
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001607 break;
1608 }
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001609}
1610
1611/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001612 * Generic software counter infrastructure
1613 */
1614
1615static void perf_swcounter_update(struct perf_counter *counter)
1616{
1617 struct hw_perf_counter *hwc = &counter->hw;
1618 u64 prev, now;
1619 s64 delta;
1620
1621again:
1622 prev = atomic64_read(&hwc->prev_count);
1623 now = atomic64_read(&hwc->count);
1624 if (atomic64_cmpxchg(&hwc->prev_count, prev, now) != prev)
1625 goto again;
1626
1627 delta = now - prev;
1628
1629 atomic64_add(delta, &counter->count);
1630 atomic64_sub(delta, &hwc->period_left);
1631}
1632
1633static void perf_swcounter_set_period(struct perf_counter *counter)
1634{
1635 struct hw_perf_counter *hwc = &counter->hw;
1636 s64 left = atomic64_read(&hwc->period_left);
1637 s64 period = hwc->irq_period;
1638
1639 if (unlikely(left <= -period)) {
1640 left = period;
1641 atomic64_set(&hwc->period_left, left);
1642 }
1643
1644 if (unlikely(left <= 0)) {
1645 left += period;
1646 atomic64_add(period, &hwc->period_left);
1647 }
1648
1649 atomic64_set(&hwc->prev_count, -left);
1650 atomic64_set(&hwc->count, -left);
1651}
1652
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01001653static enum hrtimer_restart perf_swcounter_hrtimer(struct hrtimer *hrtimer)
1654{
1655 struct perf_counter *counter;
1656 struct pt_regs *regs;
1657
1658 counter = container_of(hrtimer, struct perf_counter, hw.hrtimer);
1659 counter->hw_ops->read(counter);
1660
1661 regs = get_irq_regs();
1662 /*
1663 * In case we exclude kernel IPs or are somehow not in interrupt
1664 * context, provide the next best thing, the user IP.
1665 */
1666 if ((counter->hw_event.exclude_kernel || !regs) &&
1667 !counter->hw_event.exclude_user)
1668 regs = task_pt_regs(current);
1669
1670 if (regs)
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001671 perf_counter_output(counter, 0, regs);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01001672
1673 hrtimer_forward_now(hrtimer, ns_to_ktime(counter->hw.irq_period));
1674
1675 return HRTIMER_RESTART;
1676}
1677
1678static void perf_swcounter_overflow(struct perf_counter *counter,
1679 int nmi, struct pt_regs *regs)
1680{
Peter Zijlstrab8e83512009-03-19 20:26:18 +01001681 perf_swcounter_update(counter);
1682 perf_swcounter_set_period(counter);
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001683 perf_counter_output(counter, nmi, regs);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01001684}
1685
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001686static int perf_swcounter_match(struct perf_counter *counter,
Peter Zijlstrab8e83512009-03-19 20:26:18 +01001687 enum perf_event_types type,
1688 u32 event, struct pt_regs *regs)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001689{
1690 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
1691 return 0;
1692
Peter Zijlstraf4a2deb42009-03-23 18:22:06 +01001693 if (perf_event_raw(&counter->hw_event))
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001694 return 0;
1695
Peter Zijlstraf4a2deb42009-03-23 18:22:06 +01001696 if (perf_event_type(&counter->hw_event) != type)
Peter Zijlstrab8e83512009-03-19 20:26:18 +01001697 return 0;
1698
Peter Zijlstraf4a2deb42009-03-23 18:22:06 +01001699 if (perf_event_id(&counter->hw_event) != event)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001700 return 0;
1701
1702 if (counter->hw_event.exclude_user && user_mode(regs))
1703 return 0;
1704
1705 if (counter->hw_event.exclude_kernel && !user_mode(regs))
1706 return 0;
1707
1708 return 1;
1709}
1710
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01001711static void perf_swcounter_add(struct perf_counter *counter, u64 nr,
1712 int nmi, struct pt_regs *regs)
1713{
1714 int neg = atomic64_add_negative(nr, &counter->hw.count);
1715 if (counter->hw.irq_period && !neg)
1716 perf_swcounter_overflow(counter, nmi, regs);
1717}
1718
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001719static void perf_swcounter_ctx_event(struct perf_counter_context *ctx,
Peter Zijlstrab8e83512009-03-19 20:26:18 +01001720 enum perf_event_types type, u32 event,
1721 u64 nr, int nmi, struct pt_regs *regs)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001722{
1723 struct perf_counter *counter;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001724
Peter Zijlstra01ef09d2009-03-19 20:26:11 +01001725 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001726 return;
1727
Peter Zijlstra592903c2009-03-13 12:21:36 +01001728 rcu_read_lock();
1729 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
Peter Zijlstrab8e83512009-03-19 20:26:18 +01001730 if (perf_swcounter_match(counter, type, event, regs))
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01001731 perf_swcounter_add(counter, nr, nmi, regs);
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001732 }
Peter Zijlstra592903c2009-03-13 12:21:36 +01001733 rcu_read_unlock();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001734}
1735
Peter Zijlstra96f6d442009-03-23 18:22:07 +01001736static int *perf_swcounter_recursion_context(struct perf_cpu_context *cpuctx)
1737{
1738 if (in_nmi())
1739 return &cpuctx->recursion[3];
1740
1741 if (in_irq())
1742 return &cpuctx->recursion[2];
1743
1744 if (in_softirq())
1745 return &cpuctx->recursion[1];
1746
1747 return &cpuctx->recursion[0];
1748}
1749
Peter Zijlstrab8e83512009-03-19 20:26:18 +01001750static void __perf_swcounter_event(enum perf_event_types type, u32 event,
1751 u64 nr, int nmi, struct pt_regs *regs)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001752{
1753 struct perf_cpu_context *cpuctx = &get_cpu_var(perf_cpu_context);
Peter Zijlstra96f6d442009-03-23 18:22:07 +01001754 int *recursion = perf_swcounter_recursion_context(cpuctx);
1755
1756 if (*recursion)
1757 goto out;
1758
1759 (*recursion)++;
1760 barrier();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001761
Peter Zijlstrab8e83512009-03-19 20:26:18 +01001762 perf_swcounter_ctx_event(&cpuctx->ctx, type, event, nr, nmi, regs);
1763 if (cpuctx->task_ctx) {
1764 perf_swcounter_ctx_event(cpuctx->task_ctx, type, event,
1765 nr, nmi, regs);
1766 }
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001767
Peter Zijlstra96f6d442009-03-23 18:22:07 +01001768 barrier();
1769 (*recursion)--;
1770
1771out:
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001772 put_cpu_var(perf_cpu_context);
1773}
1774
Peter Zijlstrab8e83512009-03-19 20:26:18 +01001775void perf_swcounter_event(u32 event, u64 nr, int nmi, struct pt_regs *regs)
1776{
1777 __perf_swcounter_event(PERF_TYPE_SOFTWARE, event, nr, nmi, regs);
1778}
1779
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001780static void perf_swcounter_read(struct perf_counter *counter)
1781{
1782 perf_swcounter_update(counter);
1783}
1784
1785static int perf_swcounter_enable(struct perf_counter *counter)
1786{
1787 perf_swcounter_set_period(counter);
1788 return 0;
1789}
1790
1791static void perf_swcounter_disable(struct perf_counter *counter)
1792{
1793 perf_swcounter_update(counter);
1794}
1795
Peter Zijlstraac17dc82009-03-13 12:21:34 +01001796static const struct hw_perf_counter_ops perf_ops_generic = {
1797 .enable = perf_swcounter_enable,
1798 .disable = perf_swcounter_disable,
1799 .read = perf_swcounter_read,
1800};
1801
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001802/*
1803 * Software counter: cpu wall time clock
1804 */
1805
Paul Mackerras9abf8a02009-01-09 16:26:43 +11001806static void cpu_clock_perf_counter_update(struct perf_counter *counter)
1807{
1808 int cpu = raw_smp_processor_id();
1809 s64 prev;
1810 u64 now;
1811
1812 now = cpu_clock(cpu);
1813 prev = atomic64_read(&counter->hw.prev_count);
1814 atomic64_set(&counter->hw.prev_count, now);
1815 atomic64_add(now - prev, &counter->count);
1816}
1817
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01001818static int cpu_clock_perf_counter_enable(struct perf_counter *counter)
1819{
1820 struct hw_perf_counter *hwc = &counter->hw;
1821 int cpu = raw_smp_processor_id();
1822
1823 atomic64_set(&hwc->prev_count, cpu_clock(cpu));
Peter Zijlstra039fc912009-03-13 16:43:47 +01001824 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1825 hwc->hrtimer.function = perf_swcounter_hrtimer;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01001826 if (hwc->irq_period) {
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01001827 __hrtimer_start_range_ns(&hwc->hrtimer,
1828 ns_to_ktime(hwc->irq_period), 0,
1829 HRTIMER_MODE_REL, 0);
1830 }
1831
1832 return 0;
1833}
1834
Ingo Molnar5c92d122008-12-11 13:21:10 +01001835static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
1836{
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01001837 hrtimer_cancel(&counter->hw.hrtimer);
Paul Mackerras9abf8a02009-01-09 16:26:43 +11001838 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01001839}
1840
1841static void cpu_clock_perf_counter_read(struct perf_counter *counter)
1842{
Paul Mackerras9abf8a02009-01-09 16:26:43 +11001843 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01001844}
1845
1846static const struct hw_perf_counter_ops perf_ops_cpu_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +01001847 .enable = cpu_clock_perf_counter_enable,
1848 .disable = cpu_clock_perf_counter_disable,
1849 .read = cpu_clock_perf_counter_read,
Ingo Molnar5c92d122008-12-11 13:21:10 +01001850};
1851
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001852/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001853 * Software counter: task time clock
1854 */
1855
1856/*
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001857 * Called from within the scheduler:
1858 */
1859static u64 task_clock_perf_counter_val(struct perf_counter *counter, int update)
Ingo Molnarbae43c92008-12-11 14:03:20 +01001860{
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001861 struct task_struct *curr = counter->task;
1862 u64 delta;
1863
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001864 delta = __task_delta_exec(curr, update);
1865
1866 return curr->se.sum_exec_runtime + delta;
1867}
1868
1869static void task_clock_perf_counter_update(struct perf_counter *counter, u64 now)
1870{
1871 u64 prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01001872 s64 delta;
Ingo Molnarbae43c92008-12-11 14:03:20 +01001873
Ingo Molnar8cb391e2008-12-14 12:22:31 +01001874 prev = atomic64_read(&counter->hw.prev_count);
Ingo Molnar8cb391e2008-12-14 12:22:31 +01001875
1876 atomic64_set(&counter->hw.prev_count, now);
1877
1878 delta = now - prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01001879
1880 atomic64_add(delta, &counter->count);
Ingo Molnarbae43c92008-12-11 14:03:20 +01001881}
1882
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01001883static int task_clock_perf_counter_enable(struct perf_counter *counter)
Ingo Molnar8cb391e2008-12-14 12:22:31 +01001884{
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01001885 struct hw_perf_counter *hwc = &counter->hw;
1886
1887 atomic64_set(&hwc->prev_count, task_clock_perf_counter_val(counter, 0));
Peter Zijlstra039fc912009-03-13 16:43:47 +01001888 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1889 hwc->hrtimer.function = perf_swcounter_hrtimer;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01001890 if (hwc->irq_period) {
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01001891 __hrtimer_start_range_ns(&hwc->hrtimer,
1892 ns_to_ktime(hwc->irq_period), 0,
1893 HRTIMER_MODE_REL, 0);
1894 }
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01001895
1896 return 0;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01001897}
1898
1899static void task_clock_perf_counter_disable(struct perf_counter *counter)
1900{
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01001901 hrtimer_cancel(&counter->hw.hrtimer);
1902 task_clock_perf_counter_update(counter,
1903 task_clock_perf_counter_val(counter, 0));
1904}
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001905
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01001906static void task_clock_perf_counter_read(struct perf_counter *counter)
1907{
1908 task_clock_perf_counter_update(counter,
1909 task_clock_perf_counter_val(counter, 1));
Ingo Molnarbae43c92008-12-11 14:03:20 +01001910}
1911
1912static const struct hw_perf_counter_ops perf_ops_task_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +01001913 .enable = task_clock_perf_counter_enable,
1914 .disable = task_clock_perf_counter_disable,
1915 .read = task_clock_perf_counter_read,
Ingo Molnarbae43c92008-12-11 14:03:20 +01001916};
1917
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001918/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001919 * Software counter: cpu migrations
1920 */
1921
Paul Mackerras23a185c2009-02-09 22:42:47 +11001922static inline u64 get_cpu_migrations(struct perf_counter *counter)
Ingo Molnar6c594c22008-12-14 12:34:15 +01001923{
Paul Mackerras23a185c2009-02-09 22:42:47 +11001924 struct task_struct *curr = counter->ctx->task;
1925
1926 if (curr)
1927 return curr->se.nr_migrations;
1928 return cpu_nr_migrations(smp_processor_id());
Ingo Molnar6c594c22008-12-14 12:34:15 +01001929}
1930
1931static void cpu_migrations_perf_counter_update(struct perf_counter *counter)
1932{
1933 u64 prev, now;
1934 s64 delta;
1935
1936 prev = atomic64_read(&counter->hw.prev_count);
Paul Mackerras23a185c2009-02-09 22:42:47 +11001937 now = get_cpu_migrations(counter);
Ingo Molnar6c594c22008-12-14 12:34:15 +01001938
1939 atomic64_set(&counter->hw.prev_count, now);
1940
1941 delta = now - prev;
Ingo Molnar6c594c22008-12-14 12:34:15 +01001942
1943 atomic64_add(delta, &counter->count);
1944}
1945
1946static void cpu_migrations_perf_counter_read(struct perf_counter *counter)
1947{
1948 cpu_migrations_perf_counter_update(counter);
1949}
1950
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01001951static int cpu_migrations_perf_counter_enable(struct perf_counter *counter)
Ingo Molnar6c594c22008-12-14 12:34:15 +01001952{
Paul Mackerrasc07c99b2009-02-13 22:10:34 +11001953 if (counter->prev_state <= PERF_COUNTER_STATE_OFF)
1954 atomic64_set(&counter->hw.prev_count,
1955 get_cpu_migrations(counter));
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01001956 return 0;
Ingo Molnar6c594c22008-12-14 12:34:15 +01001957}
1958
1959static void cpu_migrations_perf_counter_disable(struct perf_counter *counter)
1960{
1961 cpu_migrations_perf_counter_update(counter);
1962}
1963
1964static const struct hw_perf_counter_ops perf_ops_cpu_migrations = {
Ingo Molnar76715812008-12-17 14:20:28 +01001965 .enable = cpu_migrations_perf_counter_enable,
1966 .disable = cpu_migrations_perf_counter_disable,
1967 .read = cpu_migrations_perf_counter_read,
Ingo Molnar6c594c22008-12-14 12:34:15 +01001968};
1969
Peter Zijlstrae077df42009-03-19 20:26:17 +01001970#ifdef CONFIG_EVENT_PROFILE
1971void perf_tpcounter_event(int event_id)
1972{
Peter Zijlstrab8e83512009-03-19 20:26:18 +01001973 struct pt_regs *regs = get_irq_regs();
1974
1975 if (!regs)
1976 regs = task_pt_regs(current);
1977
1978 __perf_swcounter_event(PERF_TYPE_TRACEPOINT, event_id, 1, 1, regs);
Peter Zijlstrae077df42009-03-19 20:26:17 +01001979}
1980
1981extern int ftrace_profile_enable(int);
1982extern void ftrace_profile_disable(int);
1983
1984static void tp_perf_counter_destroy(struct perf_counter *counter)
1985{
Peter Zijlstraf4a2deb42009-03-23 18:22:06 +01001986 ftrace_profile_disable(perf_event_id(&counter->hw_event));
Peter Zijlstrae077df42009-03-19 20:26:17 +01001987}
1988
1989static const struct hw_perf_counter_ops *
1990tp_perf_counter_init(struct perf_counter *counter)
1991{
Peter Zijlstraf4a2deb42009-03-23 18:22:06 +01001992 int event_id = perf_event_id(&counter->hw_event);
Peter Zijlstrae077df42009-03-19 20:26:17 +01001993 int ret;
1994
1995 ret = ftrace_profile_enable(event_id);
1996 if (ret)
1997 return NULL;
1998
1999 counter->destroy = tp_perf_counter_destroy;
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002000 counter->hw.irq_period = counter->hw_event.irq_period;
Peter Zijlstrae077df42009-03-19 20:26:17 +01002001
2002 return &perf_ops_generic;
2003}
2004#else
2005static const struct hw_perf_counter_ops *
2006tp_perf_counter_init(struct perf_counter *counter)
2007{
2008 return NULL;
2009}
2010#endif
2011
Ingo Molnar5c92d122008-12-11 13:21:10 +01002012static const struct hw_perf_counter_ops *
2013sw_perf_counter_init(struct perf_counter *counter)
2014{
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002015 struct perf_counter_hw_event *hw_event = &counter->hw_event;
Ingo Molnar5c92d122008-12-11 13:21:10 +01002016 const struct hw_perf_counter_ops *hw_ops = NULL;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002017 struct hw_perf_counter *hwc = &counter->hw;
Ingo Molnar5c92d122008-12-11 13:21:10 +01002018
Paul Mackerras0475f9e2009-02-11 14:35:35 +11002019 /*
2020 * Software counters (currently) can't in general distinguish
2021 * between user, kernel and hypervisor events.
2022 * However, context switches and cpu migrations are considered
2023 * to be kernel events, and page faults are never hypervisor
2024 * events.
2025 */
Peter Zijlstraf4a2deb42009-03-23 18:22:06 +01002026 switch (perf_event_id(&counter->hw_event)) {
Ingo Molnar5c92d122008-12-11 13:21:10 +01002027 case PERF_COUNT_CPU_CLOCK:
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002028 hw_ops = &perf_ops_cpu_clock;
2029
2030 if (hw_event->irq_period && hw_event->irq_period < 10000)
2031 hw_event->irq_period = 10000;
Ingo Molnar5c92d122008-12-11 13:21:10 +01002032 break;
Ingo Molnarbae43c92008-12-11 14:03:20 +01002033 case PERF_COUNT_TASK_CLOCK:
Paul Mackerras23a185c2009-02-09 22:42:47 +11002034 /*
2035 * If the user instantiates this as a per-cpu counter,
2036 * use the cpu_clock counter instead.
2037 */
2038 if (counter->ctx->task)
2039 hw_ops = &perf_ops_task_clock;
2040 else
2041 hw_ops = &perf_ops_cpu_clock;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002042
2043 if (hw_event->irq_period && hw_event->irq_period < 10000)
2044 hw_event->irq_period = 10000;
Ingo Molnarbae43c92008-12-11 14:03:20 +01002045 break;
Ingo Molnare06c61a2008-12-14 14:44:31 +01002046 case PERF_COUNT_PAGE_FAULTS:
Peter Zijlstraac17dc82009-03-13 12:21:34 +01002047 case PERF_COUNT_PAGE_FAULTS_MIN:
2048 case PERF_COUNT_PAGE_FAULTS_MAJ:
Ingo Molnar5d6a27d2008-12-14 12:28:33 +01002049 case PERF_COUNT_CONTEXT_SWITCHES:
Peter Zijlstra4a0deca2009-03-19 20:26:12 +01002050 hw_ops = &perf_ops_generic;
Ingo Molnar5d6a27d2008-12-14 12:28:33 +01002051 break;
Ingo Molnar6c594c22008-12-14 12:34:15 +01002052 case PERF_COUNT_CPU_MIGRATIONS:
Paul Mackerras0475f9e2009-02-11 14:35:35 +11002053 if (!counter->hw_event.exclude_kernel)
2054 hw_ops = &perf_ops_cpu_migrations;
Ingo Molnar6c594c22008-12-14 12:34:15 +01002055 break;
Ingo Molnar5c92d122008-12-11 13:21:10 +01002056 }
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002057
2058 if (hw_ops)
2059 hwc->irq_period = hw_event->irq_period;
2060
Ingo Molnar5c92d122008-12-11 13:21:10 +01002061 return hw_ops;
2062}
2063
Thomas Gleixner0793a612008-12-04 20:12:29 +01002064/*
2065 * Allocate and initialize a counter structure
2066 */
2067static struct perf_counter *
Ingo Molnar04289bb2008-12-11 08:38:42 +01002068perf_counter_alloc(struct perf_counter_hw_event *hw_event,
2069 int cpu,
Paul Mackerras23a185c2009-02-09 22:42:47 +11002070 struct perf_counter_context *ctx,
Ingo Molnar9b51f662008-12-12 13:49:45 +01002071 struct perf_counter *group_leader,
2072 gfp_t gfpflags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01002073{
Ingo Molnar5c92d122008-12-11 13:21:10 +01002074 const struct hw_perf_counter_ops *hw_ops;
Ingo Molnar621a01e2008-12-11 12:46:46 +01002075 struct perf_counter *counter;
Thomas Gleixner0793a612008-12-04 20:12:29 +01002076
Ingo Molnar9b51f662008-12-12 13:49:45 +01002077 counter = kzalloc(sizeof(*counter), gfpflags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002078 if (!counter)
2079 return NULL;
2080
Ingo Molnar04289bb2008-12-11 08:38:42 +01002081 /*
2082 * Single counters are their own group leaders, with an
2083 * empty sibling list:
2084 */
2085 if (!group_leader)
2086 group_leader = counter;
2087
Thomas Gleixner0793a612008-12-04 20:12:29 +01002088 mutex_init(&counter->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01002089 INIT_LIST_HEAD(&counter->list_entry);
Peter Zijlstra592903c2009-03-13 12:21:36 +01002090 INIT_LIST_HEAD(&counter->event_entry);
Ingo Molnar04289bb2008-12-11 08:38:42 +01002091 INIT_LIST_HEAD(&counter->sibling_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002092 init_waitqueue_head(&counter->waitq);
2093
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002094 mutex_init(&counter->mmap_mutex);
2095
Paul Mackerrasd859e292009-01-17 18:10:22 +11002096 INIT_LIST_HEAD(&counter->child_list);
2097
Ingo Molnar9f66a382008-12-10 12:33:23 +01002098 counter->cpu = cpu;
2099 counter->hw_event = *hw_event;
2100 counter->wakeup_pending = 0;
Ingo Molnar04289bb2008-12-11 08:38:42 +01002101 counter->group_leader = group_leader;
Ingo Molnar621a01e2008-12-11 12:46:46 +01002102 counter->hw_ops = NULL;
Paul Mackerras23a185c2009-02-09 22:42:47 +11002103 counter->ctx = ctx;
Ingo Molnar621a01e2008-12-11 12:46:46 +01002104
Ingo Molnar235c7fc2008-12-21 14:43:25 +01002105 counter->state = PERF_COUNTER_STATE_INACTIVE;
Ingo Molnara86ed502008-12-17 00:43:10 +01002106 if (hw_event->disabled)
2107 counter->state = PERF_COUNTER_STATE_OFF;
2108
Ingo Molnar5c92d122008-12-11 13:21:10 +01002109 hw_ops = NULL;
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002110
Peter Zijlstraf4a2deb42009-03-23 18:22:06 +01002111 if (perf_event_raw(hw_event)) {
Ingo Molnar5c92d122008-12-11 13:21:10 +01002112 hw_ops = hw_perf_counter_init(counter);
Peter Zijlstraf4a2deb42009-03-23 18:22:06 +01002113 goto done;
2114 }
2115
2116 switch (perf_event_type(hw_event)) {
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002117 case PERF_TYPE_HARDWARE:
2118 hw_ops = hw_perf_counter_init(counter);
2119 break;
2120
2121 case PERF_TYPE_SOFTWARE:
2122 hw_ops = sw_perf_counter_init(counter);
2123 break;
2124
2125 case PERF_TYPE_TRACEPOINT:
2126 hw_ops = tp_perf_counter_init(counter);
2127 break;
2128 }
Ingo Molnar5c92d122008-12-11 13:21:10 +01002129
Ingo Molnar621a01e2008-12-11 12:46:46 +01002130 if (!hw_ops) {
2131 kfree(counter);
2132 return NULL;
2133 }
Peter Zijlstraf4a2deb42009-03-23 18:22:06 +01002134done:
Ingo Molnar621a01e2008-12-11 12:46:46 +01002135 counter->hw_ops = hw_ops;
Thomas Gleixner0793a612008-12-04 20:12:29 +01002136
2137 return counter;
2138}
2139
2140/**
Paul Mackerras2743a5b2009-03-04 20:36:51 +11002141 * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
Ingo Molnar9f66a382008-12-10 12:33:23 +01002142 *
2143 * @hw_event_uptr: event type attributes for monitoring/sampling
Thomas Gleixner0793a612008-12-04 20:12:29 +01002144 * @pid: target pid
Ingo Molnar9f66a382008-12-10 12:33:23 +01002145 * @cpu: target cpu
2146 * @group_fd: group leader counter fd
Thomas Gleixner0793a612008-12-04 20:12:29 +01002147 */
Paul Mackerras2743a5b2009-03-04 20:36:51 +11002148SYSCALL_DEFINE5(perf_counter_open,
Paul Mackerrasf3dfd262009-02-26 22:43:46 +11002149 const struct perf_counter_hw_event __user *, hw_event_uptr,
Paul Mackerras2743a5b2009-03-04 20:36:51 +11002150 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01002151{
Ingo Molnar04289bb2008-12-11 08:38:42 +01002152 struct perf_counter *counter, *group_leader;
Ingo Molnar9f66a382008-12-10 12:33:23 +01002153 struct perf_counter_hw_event hw_event;
Ingo Molnar04289bb2008-12-11 08:38:42 +01002154 struct perf_counter_context *ctx;
Ingo Molnar9b51f662008-12-12 13:49:45 +01002155 struct file *counter_file = NULL;
Ingo Molnar04289bb2008-12-11 08:38:42 +01002156 struct file *group_file = NULL;
2157 int fput_needed = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01002158 int fput_needed2 = 0;
Thomas Gleixner0793a612008-12-04 20:12:29 +01002159 int ret;
2160
Paul Mackerras2743a5b2009-03-04 20:36:51 +11002161 /* for future expandability... */
2162 if (flags)
2163 return -EINVAL;
2164
Ingo Molnar9f66a382008-12-10 12:33:23 +01002165 if (copy_from_user(&hw_event, hw_event_uptr, sizeof(hw_event)) != 0)
Thomas Gleixnereab656a2008-12-08 19:26:59 +01002166 return -EFAULT;
2167
Ingo Molnar04289bb2008-12-11 08:38:42 +01002168 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01002169 * Get the target context (task or percpu):
2170 */
2171 ctx = find_get_context(pid, cpu);
2172 if (IS_ERR(ctx))
2173 return PTR_ERR(ctx);
2174
2175 /*
2176 * Look up the group leader (we will attach this counter to it):
Ingo Molnar04289bb2008-12-11 08:38:42 +01002177 */
2178 group_leader = NULL;
2179 if (group_fd != -1) {
2180 ret = -EINVAL;
2181 group_file = fget_light(group_fd, &fput_needed);
2182 if (!group_file)
Ingo Molnarccff2862008-12-11 11:26:29 +01002183 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01002184 if (group_file->f_op != &perf_fops)
Ingo Molnarccff2862008-12-11 11:26:29 +01002185 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01002186
2187 group_leader = group_file->private_data;
2188 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01002189 * Do not allow a recursive hierarchy (this new sibling
2190 * becoming part of another group-sibling):
Ingo Molnar04289bb2008-12-11 08:38:42 +01002191 */
Ingo Molnarccff2862008-12-11 11:26:29 +01002192 if (group_leader->group_leader != group_leader)
2193 goto err_put_context;
2194 /*
2195 * Do not allow to attach to a group in a different
2196 * task or CPU context:
2197 */
2198 if (group_leader->ctx != ctx)
2199 goto err_put_context;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11002200 /*
2201 * Only a group leader can be exclusive or pinned
2202 */
2203 if (hw_event.exclusive || hw_event.pinned)
2204 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01002205 }
2206
Ingo Molnar5c92d122008-12-11 13:21:10 +01002207 ret = -EINVAL;
Paul Mackerras23a185c2009-02-09 22:42:47 +11002208 counter = perf_counter_alloc(&hw_event, cpu, ctx, group_leader,
2209 GFP_KERNEL);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002210 if (!counter)
2211 goto err_put_context;
2212
Thomas Gleixner0793a612008-12-04 20:12:29 +01002213 ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
2214 if (ret < 0)
Ingo Molnar9b51f662008-12-12 13:49:45 +01002215 goto err_free_put_context;
2216
2217 counter_file = fget_light(ret, &fput_needed2);
2218 if (!counter_file)
2219 goto err_free_put_context;
2220
2221 counter->filp = counter_file;
Paul Mackerrasd859e292009-01-17 18:10:22 +11002222 mutex_lock(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01002223 perf_install_in_context(ctx, counter, cpu);
Paul Mackerrasd859e292009-01-17 18:10:22 +11002224 mutex_unlock(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01002225
2226 fput_light(counter_file, fput_needed2);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002227
Ingo Molnar04289bb2008-12-11 08:38:42 +01002228out_fput:
2229 fput_light(group_file, fput_needed);
2230
Thomas Gleixner0793a612008-12-04 20:12:29 +01002231 return ret;
2232
Ingo Molnar9b51f662008-12-12 13:49:45 +01002233err_free_put_context:
Thomas Gleixner0793a612008-12-04 20:12:29 +01002234 kfree(counter);
2235
2236err_put_context:
2237 put_context(ctx);
2238
Ingo Molnar04289bb2008-12-11 08:38:42 +01002239 goto out_fput;
Thomas Gleixner0793a612008-12-04 20:12:29 +01002240}
2241
Ingo Molnar9b51f662008-12-12 13:49:45 +01002242/*
2243 * Initialize the perf_counter context in a task_struct:
2244 */
2245static void
2246__perf_counter_init_context(struct perf_counter_context *ctx,
2247 struct task_struct *task)
2248{
2249 memset(ctx, 0, sizeof(*ctx));
2250 spin_lock_init(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +11002251 mutex_init(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01002252 INIT_LIST_HEAD(&ctx->counter_list);
Peter Zijlstra592903c2009-03-13 12:21:36 +01002253 INIT_LIST_HEAD(&ctx->event_list);
Ingo Molnar9b51f662008-12-12 13:49:45 +01002254 ctx->task = task;
2255}
2256
2257/*
2258 * inherit a counter from parent task to child task:
2259 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11002260static struct perf_counter *
Ingo Molnar9b51f662008-12-12 13:49:45 +01002261inherit_counter(struct perf_counter *parent_counter,
2262 struct task_struct *parent,
2263 struct perf_counter_context *parent_ctx,
2264 struct task_struct *child,
Paul Mackerrasd859e292009-01-17 18:10:22 +11002265 struct perf_counter *group_leader,
Ingo Molnar9b51f662008-12-12 13:49:45 +01002266 struct perf_counter_context *child_ctx)
2267{
2268 struct perf_counter *child_counter;
2269
Paul Mackerrasd859e292009-01-17 18:10:22 +11002270 /*
2271 * Instead of creating recursive hierarchies of counters,
2272 * we link inherited counters back to the original parent,
2273 * which has a filp for sure, which we use as the reference
2274 * count:
2275 */
2276 if (parent_counter->parent)
2277 parent_counter = parent_counter->parent;
2278
Ingo Molnar9b51f662008-12-12 13:49:45 +01002279 child_counter = perf_counter_alloc(&parent_counter->hw_event,
Paul Mackerras23a185c2009-02-09 22:42:47 +11002280 parent_counter->cpu, child_ctx,
2281 group_leader, GFP_KERNEL);
Ingo Molnar9b51f662008-12-12 13:49:45 +01002282 if (!child_counter)
Paul Mackerrasd859e292009-01-17 18:10:22 +11002283 return NULL;
Ingo Molnar9b51f662008-12-12 13:49:45 +01002284
2285 /*
2286 * Link it up in the child's context:
2287 */
Ingo Molnar9b51f662008-12-12 13:49:45 +01002288 child_counter->task = child;
2289 list_add_counter(child_counter, child_ctx);
2290 child_ctx->nr_counters++;
2291
2292 child_counter->parent = parent_counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01002293 /*
2294 * inherit into child's child as well:
2295 */
2296 child_counter->hw_event.inherit = 1;
2297
2298 /*
2299 * Get a reference to the parent filp - we will fput it
2300 * when the child counter exits. This is safe to do because
2301 * we are in the parent and we know that the filp still
2302 * exists and has a nonzero count:
2303 */
2304 atomic_long_inc(&parent_counter->filp->f_count);
2305
Paul Mackerrasd859e292009-01-17 18:10:22 +11002306 /*
2307 * Link this into the parent counter's child list
2308 */
2309 mutex_lock(&parent_counter->mutex);
2310 list_add_tail(&child_counter->child_list, &parent_counter->child_list);
2311
2312 /*
2313 * Make the child state follow the state of the parent counter,
2314 * not its hw_event.disabled bit. We hold the parent's mutex,
2315 * so we won't race with perf_counter_{en,dis}able_family.
2316 */
2317 if (parent_counter->state >= PERF_COUNTER_STATE_INACTIVE)
2318 child_counter->state = PERF_COUNTER_STATE_INACTIVE;
2319 else
2320 child_counter->state = PERF_COUNTER_STATE_OFF;
2321
2322 mutex_unlock(&parent_counter->mutex);
2323
2324 return child_counter;
2325}
2326
2327static int inherit_group(struct perf_counter *parent_counter,
2328 struct task_struct *parent,
2329 struct perf_counter_context *parent_ctx,
2330 struct task_struct *child,
2331 struct perf_counter_context *child_ctx)
2332{
2333 struct perf_counter *leader;
2334 struct perf_counter *sub;
2335
2336 leader = inherit_counter(parent_counter, parent, parent_ctx,
2337 child, NULL, child_ctx);
2338 if (!leader)
2339 return -ENOMEM;
2340 list_for_each_entry(sub, &parent_counter->sibling_list, list_entry) {
2341 if (!inherit_counter(sub, parent, parent_ctx,
2342 child, leader, child_ctx))
2343 return -ENOMEM;
2344 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01002345 return 0;
2346}
2347
Paul Mackerrasd859e292009-01-17 18:10:22 +11002348static void sync_child_counter(struct perf_counter *child_counter,
2349 struct perf_counter *parent_counter)
2350{
2351 u64 parent_val, child_val;
2352
2353 parent_val = atomic64_read(&parent_counter->count);
2354 child_val = atomic64_read(&child_counter->count);
2355
2356 /*
2357 * Add back the child's count to the parent's count:
2358 */
2359 atomic64_add(child_val, &parent_counter->count);
2360
2361 /*
2362 * Remove this counter from the parent's list
2363 */
2364 mutex_lock(&parent_counter->mutex);
2365 list_del_init(&child_counter->child_list);
2366 mutex_unlock(&parent_counter->mutex);
2367
2368 /*
2369 * Release the parent counter, if this was the last
2370 * reference to it.
2371 */
2372 fput(parent_counter->filp);
2373}
2374
Ingo Molnar9b51f662008-12-12 13:49:45 +01002375static void
2376__perf_counter_exit_task(struct task_struct *child,
2377 struct perf_counter *child_counter,
2378 struct perf_counter_context *child_ctx)
2379{
2380 struct perf_counter *parent_counter;
Paul Mackerrasd859e292009-01-17 18:10:22 +11002381 struct perf_counter *sub, *tmp;
Ingo Molnar9b51f662008-12-12 13:49:45 +01002382
2383 /*
Ingo Molnar235c7fc2008-12-21 14:43:25 +01002384 * If we do not self-reap then we have to wait for the
2385 * child task to unschedule (it will happen for sure),
2386 * so that its counter is at its final count. (This
2387 * condition triggers rarely - child tasks usually get
2388 * off their CPU before the parent has a chance to
2389 * get this far into the reaping action)
Ingo Molnar9b51f662008-12-12 13:49:45 +01002390 */
Ingo Molnar235c7fc2008-12-21 14:43:25 +01002391 if (child != current) {
2392 wait_task_inactive(child, 0);
2393 list_del_init(&child_counter->list_entry);
2394 } else {
Ingo Molnar0cc0c022008-12-14 23:20:36 +01002395 struct perf_cpu_context *cpuctx;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01002396 unsigned long flags;
2397 u64 perf_flags;
2398
2399 /*
2400 * Disable and unlink this counter.
2401 *
2402 * Be careful about zapping the list - IRQ/NMI context
2403 * could still be processing it:
2404 */
2405 curr_rq_lock_irq_save(&flags);
2406 perf_flags = hw_perf_save_disable();
Ingo Molnar0cc0c022008-12-14 23:20:36 +01002407
2408 cpuctx = &__get_cpu_var(perf_cpu_context);
2409
Paul Mackerrasd859e292009-01-17 18:10:22 +11002410 group_sched_out(child_counter, cpuctx, child_ctx);
Ingo Molnar0cc0c022008-12-14 23:20:36 +01002411
Ingo Molnar235c7fc2008-12-21 14:43:25 +01002412 list_del_init(&child_counter->list_entry);
2413
2414 child_ctx->nr_counters--;
2415
2416 hw_perf_restore(perf_flags);
2417 curr_rq_unlock_irq_restore(&flags);
Ingo Molnar0cc0c022008-12-14 23:20:36 +01002418 }
2419
Ingo Molnar9b51f662008-12-12 13:49:45 +01002420 parent_counter = child_counter->parent;
2421 /*
2422 * It can happen that parent exits first, and has counters
2423 * that are still around due to the child reference. These
2424 * counters need to be zapped - but otherwise linger.
2425 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11002426 if (parent_counter) {
2427 sync_child_counter(child_counter, parent_counter);
2428 list_for_each_entry_safe(sub, tmp, &child_counter->sibling_list,
2429 list_entry) {
Paul Mackerras4bcf3492009-02-11 13:53:19 +01002430 if (sub->parent) {
Paul Mackerrasd859e292009-01-17 18:10:22 +11002431 sync_child_counter(sub, sub->parent);
Peter Zijlstraf1600952009-03-19 20:26:16 +01002432 free_counter(sub);
Paul Mackerras4bcf3492009-02-11 13:53:19 +01002433 }
Paul Mackerrasd859e292009-01-17 18:10:22 +11002434 }
Peter Zijlstraf1600952009-03-19 20:26:16 +01002435 free_counter(child_counter);
Paul Mackerras4bcf3492009-02-11 13:53:19 +01002436 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01002437}
2438
2439/*
Paul Mackerrasd859e292009-01-17 18:10:22 +11002440 * When a child task exits, feed back counter values to parent counters.
Ingo Molnar9b51f662008-12-12 13:49:45 +01002441 *
Paul Mackerrasd859e292009-01-17 18:10:22 +11002442 * Note: we may be running in child context, but the PID is not hashed
Ingo Molnar9b51f662008-12-12 13:49:45 +01002443 * anymore so new counters will not be added.
2444 */
2445void perf_counter_exit_task(struct task_struct *child)
2446{
2447 struct perf_counter *child_counter, *tmp;
2448 struct perf_counter_context *child_ctx;
2449
2450 child_ctx = &child->perf_counter_ctx;
2451
2452 if (likely(!child_ctx->nr_counters))
2453 return;
2454
2455 list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list,
2456 list_entry)
2457 __perf_counter_exit_task(child, child_counter, child_ctx);
2458}
2459
2460/*
2461 * Initialize the perf_counter context in task_struct
2462 */
2463void perf_counter_init_task(struct task_struct *child)
2464{
2465 struct perf_counter_context *child_ctx, *parent_ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +11002466 struct perf_counter *counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01002467 struct task_struct *parent = current;
Ingo Molnar9b51f662008-12-12 13:49:45 +01002468
2469 child_ctx = &child->perf_counter_ctx;
2470 parent_ctx = &parent->perf_counter_ctx;
2471
2472 __perf_counter_init_context(child_ctx, child);
2473
2474 /*
2475 * This is executed from the parent task context, so inherit
2476 * counters that have been marked for cloning:
2477 */
2478
2479 if (likely(!parent_ctx->nr_counters))
2480 return;
2481
2482 /*
2483 * Lock the parent list. No need to lock the child - not PID
2484 * hashed yet and not running, so nobody can access it.
2485 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11002486 mutex_lock(&parent_ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01002487
2488 /*
2489 * We dont have to disable NMIs - we are only looking at
2490 * the list, not manipulating it:
2491 */
2492 list_for_each_entry(counter, &parent_ctx->counter_list, list_entry) {
Paul Mackerrasd859e292009-01-17 18:10:22 +11002493 if (!counter->hw_event.inherit)
Ingo Molnar9b51f662008-12-12 13:49:45 +01002494 continue;
2495
Paul Mackerrasd859e292009-01-17 18:10:22 +11002496 if (inherit_group(counter, parent,
Ingo Molnar9b51f662008-12-12 13:49:45 +01002497 parent_ctx, child, child_ctx))
2498 break;
2499 }
2500
Paul Mackerrasd859e292009-01-17 18:10:22 +11002501 mutex_unlock(&parent_ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01002502}
2503
Ingo Molnar04289bb2008-12-11 08:38:42 +01002504static void __cpuinit perf_counter_init_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01002505{
Ingo Molnar04289bb2008-12-11 08:38:42 +01002506 struct perf_cpu_context *cpuctx;
Thomas Gleixner0793a612008-12-04 20:12:29 +01002507
Ingo Molnar04289bb2008-12-11 08:38:42 +01002508 cpuctx = &per_cpu(perf_cpu_context, cpu);
2509 __perf_counter_init_context(&cpuctx->ctx, NULL);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002510
2511 mutex_lock(&perf_resource_mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01002512 cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu;
Thomas Gleixner0793a612008-12-04 20:12:29 +01002513 mutex_unlock(&perf_resource_mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01002514
Paul Mackerras01d02872009-01-14 13:44:19 +11002515 hw_perf_counter_setup(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002516}
2517
2518#ifdef CONFIG_HOTPLUG_CPU
Ingo Molnar04289bb2008-12-11 08:38:42 +01002519static void __perf_counter_exit_cpu(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01002520{
2521 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
2522 struct perf_counter_context *ctx = &cpuctx->ctx;
2523 struct perf_counter *counter, *tmp;
2524
Ingo Molnar04289bb2008-12-11 08:38:42 +01002525 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry)
2526 __perf_counter_remove_from_context(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002527}
Ingo Molnar04289bb2008-12-11 08:38:42 +01002528static void perf_counter_exit_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01002529{
Paul Mackerrasd859e292009-01-17 18:10:22 +11002530 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
2531 struct perf_counter_context *ctx = &cpuctx->ctx;
2532
2533 mutex_lock(&ctx->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01002534 smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1);
Paul Mackerrasd859e292009-01-17 18:10:22 +11002535 mutex_unlock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002536}
2537#else
Ingo Molnar04289bb2008-12-11 08:38:42 +01002538static inline void perf_counter_exit_cpu(int cpu) { }
Thomas Gleixner0793a612008-12-04 20:12:29 +01002539#endif
2540
2541static int __cpuinit
2542perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
2543{
2544 unsigned int cpu = (long)hcpu;
2545
2546 switch (action) {
2547
2548 case CPU_UP_PREPARE:
2549 case CPU_UP_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01002550 perf_counter_init_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002551 break;
2552
2553 case CPU_DOWN_PREPARE:
2554 case CPU_DOWN_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01002555 perf_counter_exit_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002556 break;
2557
2558 default:
2559 break;
2560 }
2561
2562 return NOTIFY_OK;
2563}
2564
2565static struct notifier_block __cpuinitdata perf_cpu_nb = {
2566 .notifier_call = perf_cpu_notify,
2567};
2568
2569static int __init perf_counter_init(void)
2570{
2571 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
2572 (void *)(long)smp_processor_id());
2573 register_cpu_notifier(&perf_cpu_nb);
2574
2575 return 0;
2576}
2577early_initcall(perf_counter_init);
2578
2579static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
2580{
2581 return sprintf(buf, "%d\n", perf_reserved_percpu);
2582}
2583
2584static ssize_t
2585perf_set_reserve_percpu(struct sysdev_class *class,
2586 const char *buf,
2587 size_t count)
2588{
2589 struct perf_cpu_context *cpuctx;
2590 unsigned long val;
2591 int err, cpu, mpt;
2592
2593 err = strict_strtoul(buf, 10, &val);
2594 if (err)
2595 return err;
2596 if (val > perf_max_counters)
2597 return -EINVAL;
2598
2599 mutex_lock(&perf_resource_mutex);
2600 perf_reserved_percpu = val;
2601 for_each_online_cpu(cpu) {
2602 cpuctx = &per_cpu(perf_cpu_context, cpu);
2603 spin_lock_irq(&cpuctx->ctx.lock);
2604 mpt = min(perf_max_counters - cpuctx->ctx.nr_counters,
2605 perf_max_counters - perf_reserved_percpu);
2606 cpuctx->max_pertask = mpt;
2607 spin_unlock_irq(&cpuctx->ctx.lock);
2608 }
2609 mutex_unlock(&perf_resource_mutex);
2610
2611 return count;
2612}
2613
2614static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
2615{
2616 return sprintf(buf, "%d\n", perf_overcommit);
2617}
2618
2619static ssize_t
2620perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
2621{
2622 unsigned long val;
2623 int err;
2624
2625 err = strict_strtoul(buf, 10, &val);
2626 if (err)
2627 return err;
2628 if (val > 1)
2629 return -EINVAL;
2630
2631 mutex_lock(&perf_resource_mutex);
2632 perf_overcommit = val;
2633 mutex_unlock(&perf_resource_mutex);
2634
2635 return count;
2636}
2637
2638static SYSDEV_CLASS_ATTR(
2639 reserve_percpu,
2640 0644,
2641 perf_show_reserve_percpu,
2642 perf_set_reserve_percpu
2643 );
2644
2645static SYSDEV_CLASS_ATTR(
2646 overcommit,
2647 0644,
2648 perf_show_overcommit,
2649 perf_set_overcommit
2650 );
2651
2652static struct attribute *perfclass_attrs[] = {
2653 &attr_reserve_percpu.attr,
2654 &attr_overcommit.attr,
2655 NULL
2656};
2657
2658static struct attribute_group perfclass_attr_group = {
2659 .attrs = perfclass_attrs,
2660 .name = "perf_counters",
2661};
2662
2663static int __init perf_counter_sysfs_init(void)
2664{
2665 return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
2666 &perfclass_attr_group);
2667}
2668device_initcall(perf_counter_sysfs_init);