blob: c95e92329b9781c8de7c10599adb069d6c23a8ce [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;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100119 counter->tstamp_stopped = ctx->time_now;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100120 counter->hw_ops->disable(counter);
121 counter->oncpu = -1;
122
123 if (!is_software_counter(counter))
124 cpuctx->active_oncpu--;
125 ctx->nr_active--;
126 if (counter->hw_event.exclusive || !cpuctx->active_oncpu)
127 cpuctx->exclusive = 0;
128}
129
Paul Mackerrasd859e292009-01-17 18:10:22 +1100130static void
131group_sched_out(struct perf_counter *group_counter,
132 struct perf_cpu_context *cpuctx,
133 struct perf_counter_context *ctx)
134{
135 struct perf_counter *counter;
136
137 if (group_counter->state != PERF_COUNTER_STATE_ACTIVE)
138 return;
139
140 counter_sched_out(group_counter, cpuctx, ctx);
141
142 /*
143 * Schedule out siblings (if any):
144 */
145 list_for_each_entry(counter, &group_counter->sibling_list, list_entry)
146 counter_sched_out(counter, cpuctx, ctx);
147
148 if (group_counter->hw_event.exclusive)
149 cpuctx->exclusive = 0;
150}
151
Thomas Gleixner0793a612008-12-04 20:12:29 +0100152/*
153 * Cross CPU call to remove a performance counter
154 *
155 * We disable the counter on the hardware level first. After that we
156 * remove it from the context list.
157 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100158static void __perf_counter_remove_from_context(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100159{
160 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
161 struct perf_counter *counter = info;
162 struct perf_counter_context *ctx = counter->ctx;
Ingo Molnar9b51f662008-12-12 13:49:45 +0100163 unsigned long flags;
Ingo Molnar5c92d122008-12-11 13:21:10 +0100164 u64 perf_flags;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100165
166 /*
167 * If this is a task context, we need to check whether it is
168 * the current task context of this cpu. If not it has been
169 * scheduled out before the smp call arrived.
170 */
171 if (ctx->task && cpuctx->task_ctx != ctx)
172 return;
173
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100174 curr_rq_lock_irq_save(&flags);
175 spin_lock(&ctx->lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100176
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100177 counter_sched_out(counter, cpuctx, ctx);
178
179 counter->task = NULL;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100180 ctx->nr_counters--;
181
182 /*
183 * Protect the list operation against NMI by disabling the
184 * counters on a global level. NOP for non NMI based counters.
185 */
Ingo Molnar01b28382008-12-11 13:45:51 +0100186 perf_flags = hw_perf_save_disable();
Ingo Molnar04289bb2008-12-11 08:38:42 +0100187 list_del_counter(counter, ctx);
Ingo Molnar01b28382008-12-11 13:45:51 +0100188 hw_perf_restore(perf_flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100189
190 if (!ctx->task) {
191 /*
192 * Allow more per task counters with respect to the
193 * reservation:
194 */
195 cpuctx->max_pertask =
196 min(perf_max_counters - ctx->nr_counters,
197 perf_max_counters - perf_reserved_percpu);
198 }
199
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100200 spin_unlock(&ctx->lock);
201 curr_rq_unlock_irq_restore(&flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100202}
203
204
205/*
206 * Remove the counter from a task's (or a CPU's) list of counters.
207 *
Paul Mackerrasd859e292009-01-17 18:10:22 +1100208 * Must be called with counter->mutex and ctx->mutex held.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100209 *
210 * CPU counters are removed with a smp call. For task counters we only
211 * call when the task is on a CPU.
212 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100213static void perf_counter_remove_from_context(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100214{
215 struct perf_counter_context *ctx = counter->ctx;
216 struct task_struct *task = ctx->task;
217
218 if (!task) {
219 /*
220 * Per cpu counters are removed via an smp call and
221 * the removal is always sucessful.
222 */
223 smp_call_function_single(counter->cpu,
Ingo Molnar04289bb2008-12-11 08:38:42 +0100224 __perf_counter_remove_from_context,
Thomas Gleixner0793a612008-12-04 20:12:29 +0100225 counter, 1);
226 return;
227 }
228
229retry:
Ingo Molnar04289bb2008-12-11 08:38:42 +0100230 task_oncpu_function_call(task, __perf_counter_remove_from_context,
Thomas Gleixner0793a612008-12-04 20:12:29 +0100231 counter);
232
233 spin_lock_irq(&ctx->lock);
234 /*
235 * If the context is active we need to retry the smp call.
236 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100237 if (ctx->nr_active && !list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100238 spin_unlock_irq(&ctx->lock);
239 goto retry;
240 }
241
242 /*
243 * The lock prevents that this context is scheduled in so we
Ingo Molnar04289bb2008-12-11 08:38:42 +0100244 * can remove the counter safely, if the call above did not
Thomas Gleixner0793a612008-12-04 20:12:29 +0100245 * succeed.
246 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100247 if (!list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100248 ctx->nr_counters--;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100249 list_del_counter(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100250 counter->task = NULL;
251 }
252 spin_unlock_irq(&ctx->lock);
253}
254
Paul Mackerrasd859e292009-01-17 18:10:22 +1100255/*
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100256 * Get the current time for this context.
257 * If this is a task context, we use the task's task clock,
258 * or for a per-cpu context, we use the cpu clock.
259 */
260static u64 get_context_time(struct perf_counter_context *ctx, int update)
261{
262 struct task_struct *curr = ctx->task;
263
264 if (!curr)
265 return cpu_clock(smp_processor_id());
266
267 return __task_delta_exec(curr, update) + curr->se.sum_exec_runtime;
268}
269
270/*
271 * Update the record of the current time in a context.
272 */
273static void update_context_time(struct perf_counter_context *ctx, int update)
274{
275 ctx->time_now = get_context_time(ctx, update) - ctx->time_lost;
276}
277
278/*
279 * Update the total_time_enabled and total_time_running fields for a counter.
280 */
281static void update_counter_times(struct perf_counter *counter)
282{
283 struct perf_counter_context *ctx = counter->ctx;
284 u64 run_end;
285
286 if (counter->state >= PERF_COUNTER_STATE_INACTIVE) {
287 counter->total_time_enabled = ctx->time_now -
288 counter->tstamp_enabled;
289 if (counter->state == PERF_COUNTER_STATE_INACTIVE)
290 run_end = counter->tstamp_stopped;
291 else
292 run_end = ctx->time_now;
293 counter->total_time_running = run_end - counter->tstamp_running;
294 }
295}
296
297/*
298 * Update total_time_enabled and total_time_running for all counters in a group.
299 */
300static void update_group_times(struct perf_counter *leader)
301{
302 struct perf_counter *counter;
303
304 update_counter_times(leader);
305 list_for_each_entry(counter, &leader->sibling_list, list_entry)
306 update_counter_times(counter);
307}
308
309/*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100310 * Cross CPU call to disable a performance counter
311 */
312static void __perf_counter_disable(void *info)
313{
314 struct perf_counter *counter = info;
315 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
316 struct perf_counter_context *ctx = counter->ctx;
317 unsigned long flags;
318
319 /*
320 * If this is a per-task counter, need to check whether this
321 * counter's task is the current task on this cpu.
322 */
323 if (ctx->task && cpuctx->task_ctx != ctx)
324 return;
325
326 curr_rq_lock_irq_save(&flags);
327 spin_lock(&ctx->lock);
328
329 /*
330 * If the counter is on, turn it off.
331 * If it is in error state, leave it in error state.
332 */
333 if (counter->state >= PERF_COUNTER_STATE_INACTIVE) {
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100334 update_context_time(ctx, 1);
335 update_counter_times(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100336 if (counter == counter->group_leader)
337 group_sched_out(counter, cpuctx, ctx);
338 else
339 counter_sched_out(counter, cpuctx, ctx);
340 counter->state = PERF_COUNTER_STATE_OFF;
341 }
342
343 spin_unlock(&ctx->lock);
344 curr_rq_unlock_irq_restore(&flags);
345}
346
347/*
348 * Disable a counter.
349 */
350static void perf_counter_disable(struct perf_counter *counter)
351{
352 struct perf_counter_context *ctx = counter->ctx;
353 struct task_struct *task = ctx->task;
354
355 if (!task) {
356 /*
357 * Disable the counter on the cpu that it's on
358 */
359 smp_call_function_single(counter->cpu, __perf_counter_disable,
360 counter, 1);
361 return;
362 }
363
364 retry:
365 task_oncpu_function_call(task, __perf_counter_disable, counter);
366
367 spin_lock_irq(&ctx->lock);
368 /*
369 * If the counter is still active, we need to retry the cross-call.
370 */
371 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
372 spin_unlock_irq(&ctx->lock);
373 goto retry;
374 }
375
376 /*
377 * Since we have the lock this context can't be scheduled
378 * in, so we can change the state safely.
379 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100380 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
381 update_counter_times(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100382 counter->state = PERF_COUNTER_STATE_OFF;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100383 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100384
385 spin_unlock_irq(&ctx->lock);
386}
387
388/*
389 * Disable a counter and all its children.
390 */
391static void perf_counter_disable_family(struct perf_counter *counter)
392{
393 struct perf_counter *child;
394
395 perf_counter_disable(counter);
396
397 /*
398 * Lock the mutex to protect the list of children
399 */
400 mutex_lock(&counter->mutex);
401 list_for_each_entry(child, &counter->child_list, child_list)
402 perf_counter_disable(child);
403 mutex_unlock(&counter->mutex);
404}
405
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100406static int
407counter_sched_in(struct perf_counter *counter,
408 struct perf_cpu_context *cpuctx,
409 struct perf_counter_context *ctx,
410 int cpu)
411{
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100412 if (counter->state <= PERF_COUNTER_STATE_OFF)
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100413 return 0;
414
415 counter->state = PERF_COUNTER_STATE_ACTIVE;
416 counter->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */
417 /*
418 * The new state must be visible before we turn it on in the hardware:
419 */
420 smp_wmb();
421
422 if (counter->hw_ops->enable(counter)) {
423 counter->state = PERF_COUNTER_STATE_INACTIVE;
424 counter->oncpu = -1;
425 return -EAGAIN;
426 }
427
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100428 counter->tstamp_running += ctx->time_now - counter->tstamp_stopped;
429
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100430 if (!is_software_counter(counter))
431 cpuctx->active_oncpu++;
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100432 ctx->nr_active++;
433
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100434 if (counter->hw_event.exclusive)
435 cpuctx->exclusive = 1;
436
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100437 return 0;
438}
439
Thomas Gleixner0793a612008-12-04 20:12:29 +0100440/*
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100441 * Return 1 for a group consisting entirely of software counters,
442 * 0 if the group contains any hardware counters.
443 */
444static int is_software_only_group(struct perf_counter *leader)
445{
446 struct perf_counter *counter;
447
448 if (!is_software_counter(leader))
449 return 0;
Peter Zijlstra5c148192009-03-25 12:30:23 +0100450
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100451 list_for_each_entry(counter, &leader->sibling_list, list_entry)
452 if (!is_software_counter(counter))
453 return 0;
Peter Zijlstra5c148192009-03-25 12:30:23 +0100454
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100455 return 1;
456}
457
458/*
459 * Work out whether we can put this counter group on the CPU now.
460 */
461static int group_can_go_on(struct perf_counter *counter,
462 struct perf_cpu_context *cpuctx,
463 int can_add_hw)
464{
465 /*
466 * Groups consisting entirely of software counters can always go on.
467 */
468 if (is_software_only_group(counter))
469 return 1;
470 /*
471 * If an exclusive group is already on, no other hardware
472 * counters can go on.
473 */
474 if (cpuctx->exclusive)
475 return 0;
476 /*
477 * If this group is exclusive and there are already
478 * counters on the CPU, it can't go on.
479 */
480 if (counter->hw_event.exclusive && cpuctx->active_oncpu)
481 return 0;
482 /*
483 * Otherwise, try to add it if all previous groups were able
484 * to go on.
485 */
486 return can_add_hw;
487}
488
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100489static void add_counter_to_ctx(struct perf_counter *counter,
490 struct perf_counter_context *ctx)
491{
492 list_add_counter(counter, ctx);
493 ctx->nr_counters++;
494 counter->prev_state = PERF_COUNTER_STATE_OFF;
495 counter->tstamp_enabled = ctx->time_now;
496 counter->tstamp_running = ctx->time_now;
497 counter->tstamp_stopped = ctx->time_now;
498}
499
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100500/*
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100501 * Cross CPU call to install and enable a performance counter
Thomas Gleixner0793a612008-12-04 20:12:29 +0100502 */
503static void __perf_install_in_context(void *info)
504{
505 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
506 struct perf_counter *counter = info;
507 struct perf_counter_context *ctx = counter->ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100508 struct perf_counter *leader = counter->group_leader;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100509 int cpu = smp_processor_id();
Ingo Molnar9b51f662008-12-12 13:49:45 +0100510 unsigned long flags;
Ingo Molnar5c92d122008-12-11 13:21:10 +0100511 u64 perf_flags;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100512 int err;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100513
514 /*
515 * If this is a task context, we need to check whether it is
516 * the current task context of this cpu. If not it has been
517 * scheduled out before the smp call arrived.
518 */
519 if (ctx->task && cpuctx->task_ctx != ctx)
520 return;
521
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100522 curr_rq_lock_irq_save(&flags);
523 spin_lock(&ctx->lock);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100524 update_context_time(ctx, 1);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100525
526 /*
527 * Protect the list operation against NMI by disabling the
528 * counters on a global level. NOP for non NMI based counters.
529 */
Ingo Molnar01b28382008-12-11 13:45:51 +0100530 perf_flags = hw_perf_save_disable();
Thomas Gleixner0793a612008-12-04 20:12:29 +0100531
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100532 add_counter_to_ctx(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100533
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100534 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100535 * Don't put the counter on if it is disabled or if
536 * it is in a group and the group isn't on.
537 */
538 if (counter->state != PERF_COUNTER_STATE_INACTIVE ||
539 (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE))
540 goto unlock;
541
542 /*
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100543 * An exclusive counter can't go on if there are already active
544 * hardware counters, and no hardware counter can go on if there
545 * is already an exclusive counter on.
546 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100547 if (!group_can_go_on(counter, cpuctx, 1))
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100548 err = -EEXIST;
549 else
550 err = counter_sched_in(counter, cpuctx, ctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100551
Paul Mackerrasd859e292009-01-17 18:10:22 +1100552 if (err) {
553 /*
554 * This counter couldn't go on. If it is in a group
555 * then we have to pull the whole group off.
556 * If the counter group is pinned then put it in error state.
557 */
558 if (leader != counter)
559 group_sched_out(leader, cpuctx, ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100560 if (leader->hw_event.pinned) {
561 update_group_times(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100562 leader->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100563 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100564 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100565
566 if (!err && !ctx->task && cpuctx->max_pertask)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100567 cpuctx->max_pertask--;
568
Paul Mackerrasd859e292009-01-17 18:10:22 +1100569 unlock:
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100570 hw_perf_restore(perf_flags);
571
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100572 spin_unlock(&ctx->lock);
573 curr_rq_unlock_irq_restore(&flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100574}
575
576/*
577 * Attach a performance counter to a context
578 *
579 * First we add the counter to the list with the hardware enable bit
580 * in counter->hw_config cleared.
581 *
582 * If the counter is attached to a task which is on a CPU we use a smp
583 * call to enable it in the task context. The task might have been
584 * scheduled away, but we check this in the smp call again.
Paul Mackerrasd859e292009-01-17 18:10:22 +1100585 *
586 * Must be called with ctx->mutex held.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100587 */
588static void
589perf_install_in_context(struct perf_counter_context *ctx,
590 struct perf_counter *counter,
591 int cpu)
592{
593 struct task_struct *task = ctx->task;
594
Thomas Gleixner0793a612008-12-04 20:12:29 +0100595 if (!task) {
596 /*
597 * Per cpu counters are installed via an smp call and
598 * the install is always sucessful.
599 */
600 smp_call_function_single(cpu, __perf_install_in_context,
601 counter, 1);
602 return;
603 }
604
605 counter->task = task;
606retry:
607 task_oncpu_function_call(task, __perf_install_in_context,
608 counter);
609
610 spin_lock_irq(&ctx->lock);
611 /*
Thomas Gleixner0793a612008-12-04 20:12:29 +0100612 * we need to retry the smp call.
613 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100614 if (ctx->is_active && list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100615 spin_unlock_irq(&ctx->lock);
616 goto retry;
617 }
618
619 /*
620 * The lock prevents that this context is scheduled in so we
621 * can add the counter safely, if it the call above did not
622 * succeed.
623 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100624 if (list_empty(&counter->list_entry))
625 add_counter_to_ctx(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100626 spin_unlock_irq(&ctx->lock);
627}
628
Paul Mackerrasd859e292009-01-17 18:10:22 +1100629/*
630 * Cross CPU call to enable a performance counter
631 */
632static void __perf_counter_enable(void *info)
Ingo Molnar04289bb2008-12-11 08:38:42 +0100633{
Paul Mackerrasd859e292009-01-17 18:10:22 +1100634 struct perf_counter *counter = info;
635 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
636 struct perf_counter_context *ctx = counter->ctx;
637 struct perf_counter *leader = counter->group_leader;
638 unsigned long flags;
639 int err;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100640
641 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100642 * If this is a per-task counter, need to check whether this
643 * counter's task is the current task on this cpu.
Ingo Molnar04289bb2008-12-11 08:38:42 +0100644 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100645 if (ctx->task && cpuctx->task_ctx != ctx)
646 return;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100647
Paul Mackerrasd859e292009-01-17 18:10:22 +1100648 curr_rq_lock_irq_save(&flags);
649 spin_lock(&ctx->lock);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100650 update_context_time(ctx, 1);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100651
Paul Mackerrasc07c99b2009-02-13 22:10:34 +1100652 counter->prev_state = counter->state;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100653 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
654 goto unlock;
655 counter->state = PERF_COUNTER_STATE_INACTIVE;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100656 counter->tstamp_enabled = ctx->time_now - counter->total_time_enabled;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100657
658 /*
659 * If the counter is in a group and isn't the group leader,
660 * then don't put it on unless the group is on.
661 */
662 if (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE)
663 goto unlock;
664
665 if (!group_can_go_on(counter, cpuctx, 1))
666 err = -EEXIST;
667 else
668 err = counter_sched_in(counter, cpuctx, ctx,
669 smp_processor_id());
670
671 if (err) {
672 /*
673 * If this counter can't go on and it's part of a
674 * group, then the whole group has to come off.
675 */
676 if (leader != counter)
677 group_sched_out(leader, cpuctx, ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100678 if (leader->hw_event.pinned) {
679 update_group_times(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100680 leader->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100681 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100682 }
683
684 unlock:
685 spin_unlock(&ctx->lock);
686 curr_rq_unlock_irq_restore(&flags);
687}
688
689/*
690 * Enable a counter.
691 */
692static void perf_counter_enable(struct perf_counter *counter)
693{
694 struct perf_counter_context *ctx = counter->ctx;
695 struct task_struct *task = ctx->task;
696
697 if (!task) {
698 /*
699 * Enable the counter on the cpu that it's on
700 */
701 smp_call_function_single(counter->cpu, __perf_counter_enable,
702 counter, 1);
703 return;
704 }
705
706 spin_lock_irq(&ctx->lock);
707 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
708 goto out;
709
710 /*
711 * If the counter is in error state, clear that first.
712 * That way, if we see the counter in error state below, we
713 * know that it has gone back into error state, as distinct
714 * from the task having been scheduled away before the
715 * cross-call arrived.
716 */
717 if (counter->state == PERF_COUNTER_STATE_ERROR)
718 counter->state = PERF_COUNTER_STATE_OFF;
719
720 retry:
721 spin_unlock_irq(&ctx->lock);
722 task_oncpu_function_call(task, __perf_counter_enable, counter);
723
724 spin_lock_irq(&ctx->lock);
725
726 /*
727 * If the context is active and the counter is still off,
728 * we need to retry the cross-call.
729 */
730 if (ctx->is_active && counter->state == PERF_COUNTER_STATE_OFF)
731 goto retry;
732
733 /*
734 * Since we have the lock this context can't be scheduled
735 * in, so we can change the state safely.
736 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100737 if (counter->state == PERF_COUNTER_STATE_OFF) {
Paul Mackerrasd859e292009-01-17 18:10:22 +1100738 counter->state = PERF_COUNTER_STATE_INACTIVE;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100739 counter->tstamp_enabled = ctx->time_now -
740 counter->total_time_enabled;
741 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100742 out:
743 spin_unlock_irq(&ctx->lock);
744}
745
746/*
747 * Enable a counter and all its children.
748 */
749static void perf_counter_enable_family(struct perf_counter *counter)
750{
751 struct perf_counter *child;
752
753 perf_counter_enable(counter);
754
755 /*
756 * Lock the mutex to protect the list of children
757 */
758 mutex_lock(&counter->mutex);
759 list_for_each_entry(child, &counter->child_list, child_list)
760 perf_counter_enable(child);
761 mutex_unlock(&counter->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100762}
763
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100764void __perf_counter_sched_out(struct perf_counter_context *ctx,
765 struct perf_cpu_context *cpuctx)
766{
767 struct perf_counter *counter;
Paul Mackerras3cbed422009-01-09 16:43:42 +1100768 u64 flags;
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100769
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100770 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100771 ctx->is_active = 0;
772 if (likely(!ctx->nr_counters))
773 goto out;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100774 update_context_time(ctx, 0);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100775
Paul Mackerras3cbed422009-01-09 16:43:42 +1100776 flags = hw_perf_save_disable();
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100777 if (ctx->nr_active) {
778 list_for_each_entry(counter, &ctx->counter_list, list_entry)
779 group_sched_out(counter, cpuctx, ctx);
780 }
Paul Mackerras3cbed422009-01-09 16:43:42 +1100781 hw_perf_restore(flags);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100782 out:
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100783 spin_unlock(&ctx->lock);
784}
785
Thomas Gleixner0793a612008-12-04 20:12:29 +0100786/*
787 * Called from scheduler to remove the counters of the current task,
788 * with interrupts disabled.
789 *
790 * We stop each counter and update the counter value in counter->count.
791 *
Ingo Molnar76715812008-12-17 14:20:28 +0100792 * This does not protect us against NMI, but disable()
Thomas Gleixner0793a612008-12-04 20:12:29 +0100793 * sets the disabled bit in the control field of counter _before_
794 * accessing the counter control register. If a NMI hits, then it will
795 * not restart the counter.
796 */
797void perf_counter_task_sched_out(struct task_struct *task, int cpu)
798{
799 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
800 struct perf_counter_context *ctx = &task->perf_counter_ctx;
Peter Zijlstra4a0deca2009-03-19 20:26:12 +0100801 struct pt_regs *regs;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100802
803 if (likely(!cpuctx->task_ctx))
804 return;
805
Peter Zijlstra4a0deca2009-03-19 20:26:12 +0100806 regs = task_pt_regs(task);
807 perf_swcounter_event(PERF_COUNT_CONTEXT_SWITCHES, 1, 1, regs);
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100808 __perf_counter_sched_out(ctx, cpuctx);
809
Thomas Gleixner0793a612008-12-04 20:12:29 +0100810 cpuctx->task_ctx = NULL;
811}
812
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100813static void perf_counter_cpu_sched_out(struct perf_cpu_context *cpuctx)
Ingo Molnar04289bb2008-12-11 08:38:42 +0100814{
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100815 __perf_counter_sched_out(&cpuctx->ctx, cpuctx);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100816}
817
Ingo Molnar79958882008-12-17 08:54:56 +0100818static int
Ingo Molnar04289bb2008-12-11 08:38:42 +0100819group_sched_in(struct perf_counter *group_counter,
820 struct perf_cpu_context *cpuctx,
821 struct perf_counter_context *ctx,
822 int cpu)
823{
Ingo Molnar95cdd2e2008-12-21 13:50:42 +0100824 struct perf_counter *counter, *partial_group;
Paul Mackerras3cbed422009-01-09 16:43:42 +1100825 int ret;
826
827 if (group_counter->state == PERF_COUNTER_STATE_OFF)
828 return 0;
829
830 ret = hw_perf_group_sched_in(group_counter, cpuctx, ctx, cpu);
831 if (ret)
832 return ret < 0 ? ret : 0;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100833
Paul Mackerrasc07c99b2009-02-13 22:10:34 +1100834 group_counter->prev_state = group_counter->state;
Ingo Molnar95cdd2e2008-12-21 13:50:42 +0100835 if (counter_sched_in(group_counter, cpuctx, ctx, cpu))
836 return -EAGAIN;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100837
838 /*
839 * Schedule in siblings as one group (if any):
840 */
Ingo Molnar79958882008-12-17 08:54:56 +0100841 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
Paul Mackerrasc07c99b2009-02-13 22:10:34 +1100842 counter->prev_state = counter->state;
Ingo Molnar95cdd2e2008-12-21 13:50:42 +0100843 if (counter_sched_in(counter, cpuctx, ctx, cpu)) {
844 partial_group = counter;
845 goto group_error;
846 }
Ingo Molnar79958882008-12-17 08:54:56 +0100847 }
848
Paul Mackerras3cbed422009-01-09 16:43:42 +1100849 return 0;
Ingo Molnar95cdd2e2008-12-21 13:50:42 +0100850
851group_error:
852 /*
853 * Groups can be scheduled in as one unit only, so undo any
854 * partial group before returning:
855 */
856 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
857 if (counter == partial_group)
858 break;
859 counter_sched_out(counter, cpuctx, ctx);
860 }
861 counter_sched_out(group_counter, cpuctx, ctx);
862
863 return -EAGAIN;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100864}
865
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100866static void
867__perf_counter_sched_in(struct perf_counter_context *ctx,
868 struct perf_cpu_context *cpuctx, int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100869{
Thomas Gleixner0793a612008-12-04 20:12:29 +0100870 struct perf_counter *counter;
Paul Mackerras3cbed422009-01-09 16:43:42 +1100871 u64 flags;
Paul Mackerrasdd0e6ba2009-01-12 15:11:00 +1100872 int can_add_hw = 1;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100873
Thomas Gleixner0793a612008-12-04 20:12:29 +0100874 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100875 ctx->is_active = 1;
876 if (likely(!ctx->nr_counters))
877 goto out;
878
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100879 /*
880 * Add any time since the last sched_out to the lost time
881 * so it doesn't get included in the total_time_enabled and
882 * total_time_running measures for counters in the context.
883 */
884 ctx->time_lost = get_context_time(ctx, 0) - ctx->time_now;
885
Paul Mackerras3cbed422009-01-09 16:43:42 +1100886 flags = hw_perf_save_disable();
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100887
888 /*
889 * First go through the list and put on any pinned groups
890 * in order to give them the best chance of going on.
891 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100892 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100893 if (counter->state <= PERF_COUNTER_STATE_OFF ||
894 !counter->hw_event.pinned)
895 continue;
896 if (counter->cpu != -1 && counter->cpu != cpu)
897 continue;
898
899 if (group_can_go_on(counter, cpuctx, 1))
900 group_sched_in(counter, cpuctx, ctx, cpu);
901
902 /*
903 * If this pinned group hasn't been scheduled,
904 * put it in error state.
905 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100906 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
907 update_group_times(counter);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100908 counter->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100909 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100910 }
911
912 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
913 /*
914 * Ignore counters in OFF or ERROR state, and
915 * ignore pinned counters since we did them already.
916 */
917 if (counter->state <= PERF_COUNTER_STATE_OFF ||
918 counter->hw_event.pinned)
919 continue;
920
Ingo Molnar04289bb2008-12-11 08:38:42 +0100921 /*
922 * Listen to the 'cpu' scheduling filter constraint
923 * of counters:
924 */
Thomas Gleixner0793a612008-12-04 20:12:29 +0100925 if (counter->cpu != -1 && counter->cpu != cpu)
926 continue;
927
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100928 if (group_can_go_on(counter, cpuctx, can_add_hw)) {
Paul Mackerrasdd0e6ba2009-01-12 15:11:00 +1100929 if (group_sched_in(counter, cpuctx, ctx, cpu))
930 can_add_hw = 0;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100931 }
Thomas Gleixner0793a612008-12-04 20:12:29 +0100932 }
Paul Mackerras3cbed422009-01-09 16:43:42 +1100933 hw_perf_restore(flags);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100934 out:
Thomas Gleixner0793a612008-12-04 20:12:29 +0100935 spin_unlock(&ctx->lock);
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100936}
Ingo Molnar04289bb2008-12-11 08:38:42 +0100937
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100938/*
939 * Called from scheduler to add the counters of the current task
940 * with interrupts disabled.
941 *
942 * We restore the counter value and then enable it.
943 *
944 * This does not protect us against NMI, but enable()
945 * sets the enabled bit in the control field of counter _before_
946 * accessing the counter control register. If a NMI hits, then it will
947 * keep the counter running.
948 */
949void perf_counter_task_sched_in(struct task_struct *task, int cpu)
950{
951 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
952 struct perf_counter_context *ctx = &task->perf_counter_ctx;
953
954 __perf_counter_sched_in(ctx, cpuctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100955 cpuctx->task_ctx = ctx;
956}
957
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100958static void perf_counter_cpu_sched_in(struct perf_cpu_context *cpuctx, int cpu)
959{
960 struct perf_counter_context *ctx = &cpuctx->ctx;
961
962 __perf_counter_sched_in(ctx, cpuctx, cpu);
963}
964
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100965int perf_counter_task_disable(void)
966{
967 struct task_struct *curr = current;
968 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
969 struct perf_counter *counter;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100970 unsigned long flags;
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100971 u64 perf_flags;
972 int cpu;
973
974 if (likely(!ctx->nr_counters))
975 return 0;
976
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100977 curr_rq_lock_irq_save(&flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100978 cpu = smp_processor_id();
979
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100980 /* force the update of the task clock: */
981 __task_delta_exec(curr, 1);
982
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100983 perf_counter_task_sched_out(curr, cpu);
984
985 spin_lock(&ctx->lock);
986
987 /*
988 * Disable all the counters:
989 */
990 perf_flags = hw_perf_save_disable();
991
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100992 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100993 if (counter->state != PERF_COUNTER_STATE_ERROR) {
994 update_group_times(counter);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100995 counter->state = PERF_COUNTER_STATE_OFF;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100996 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100997 }
Ingo Molnar9b51f662008-12-12 13:49:45 +0100998
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100999 hw_perf_restore(perf_flags);
1000
1001 spin_unlock(&ctx->lock);
1002
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001003 curr_rq_unlock_irq_restore(&flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001004
1005 return 0;
1006}
1007
1008int perf_counter_task_enable(void)
1009{
1010 struct task_struct *curr = current;
1011 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
1012 struct perf_counter *counter;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001013 unsigned long flags;
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001014 u64 perf_flags;
1015 int cpu;
1016
1017 if (likely(!ctx->nr_counters))
1018 return 0;
1019
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001020 curr_rq_lock_irq_save(&flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001021 cpu = smp_processor_id();
1022
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001023 /* force the update of the task clock: */
1024 __task_delta_exec(curr, 1);
1025
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001026 perf_counter_task_sched_out(curr, cpu);
1027
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001028 spin_lock(&ctx->lock);
1029
1030 /*
1031 * Disable all the counters:
1032 */
1033 perf_flags = hw_perf_save_disable();
1034
1035 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001036 if (counter->state > PERF_COUNTER_STATE_OFF)
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001037 continue;
Ingo Molnar6a930702008-12-11 15:17:03 +01001038 counter->state = PERF_COUNTER_STATE_INACTIVE;
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001039 counter->tstamp_enabled = ctx->time_now -
1040 counter->total_time_enabled;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001041 counter->hw_event.disabled = 0;
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001042 }
1043 hw_perf_restore(perf_flags);
1044
1045 spin_unlock(&ctx->lock);
1046
1047 perf_counter_task_sched_in(curr, cpu);
1048
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001049 curr_rq_unlock_irq_restore(&flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001050
1051 return 0;
1052}
1053
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001054/*
1055 * Round-robin a context's counters:
1056 */
1057static void rotate_ctx(struct perf_counter_context *ctx)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001058{
Thomas Gleixner0793a612008-12-04 20:12:29 +01001059 struct perf_counter *counter;
Ingo Molnar5c92d122008-12-11 13:21:10 +01001060 u64 perf_flags;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001061
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001062 if (!ctx->nr_counters)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001063 return;
1064
Thomas Gleixner0793a612008-12-04 20:12:29 +01001065 spin_lock(&ctx->lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001066 /*
Ingo Molnar04289bb2008-12-11 08:38:42 +01001067 * Rotate the first entry last (works just fine for group counters too):
Thomas Gleixner0793a612008-12-04 20:12:29 +01001068 */
Ingo Molnar01b28382008-12-11 13:45:51 +01001069 perf_flags = hw_perf_save_disable();
Ingo Molnar04289bb2008-12-11 08:38:42 +01001070 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Peter Zijlstra75564232009-03-13 12:21:29 +01001071 list_move_tail(&counter->list_entry, &ctx->counter_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001072 break;
1073 }
Ingo Molnar01b28382008-12-11 13:45:51 +01001074 hw_perf_restore(perf_flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001075
1076 spin_unlock(&ctx->lock);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001077}
Thomas Gleixner0793a612008-12-04 20:12:29 +01001078
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001079void perf_counter_task_tick(struct task_struct *curr, int cpu)
1080{
1081 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
1082 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
1083 const int rotate_percpu = 0;
1084
1085 if (rotate_percpu)
1086 perf_counter_cpu_sched_out(cpuctx);
1087 perf_counter_task_sched_out(curr, cpu);
1088
1089 if (rotate_percpu)
1090 rotate_ctx(&cpuctx->ctx);
1091 rotate_ctx(ctx);
1092
1093 if (rotate_percpu)
1094 perf_counter_cpu_sched_in(cpuctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001095 perf_counter_task_sched_in(curr, cpu);
1096}
1097
1098/*
Thomas Gleixner0793a612008-12-04 20:12:29 +01001099 * Cross CPU call to read the hardware counter
1100 */
Ingo Molnar76715812008-12-17 14:20:28 +01001101static void __read(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001102{
Ingo Molnar621a01e2008-12-11 12:46:46 +01001103 struct perf_counter *counter = info;
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001104 struct perf_counter_context *ctx = counter->ctx;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001105 unsigned long flags;
Ingo Molnar621a01e2008-12-11 12:46:46 +01001106
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001107 curr_rq_lock_irq_save(&flags);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001108 if (ctx->is_active)
1109 update_context_time(ctx, 1);
Ingo Molnar76715812008-12-17 14:20:28 +01001110 counter->hw_ops->read(counter);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001111 update_counter_times(counter);
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001112 curr_rq_unlock_irq_restore(&flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001113}
1114
Ingo Molnar04289bb2008-12-11 08:38:42 +01001115static u64 perf_counter_read(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001116{
1117 /*
1118 * If counter is enabled and currently active on a CPU, update the
1119 * value in the counter structure:
1120 */
Ingo Molnar6a930702008-12-11 15:17:03 +01001121 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
Thomas Gleixner0793a612008-12-04 20:12:29 +01001122 smp_call_function_single(counter->oncpu,
Ingo Molnar76715812008-12-17 14:20:28 +01001123 __read, counter, 1);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001124 } else if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
1125 update_counter_times(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001126 }
1127
Ingo Molnaree060942008-12-13 09:00:03 +01001128 return atomic64_read(&counter->count);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001129}
1130
Thomas Gleixner0793a612008-12-04 20:12:29 +01001131static void put_context(struct perf_counter_context *ctx)
1132{
1133 if (ctx->task)
1134 put_task_struct(ctx->task);
1135}
1136
1137static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
1138{
1139 struct perf_cpu_context *cpuctx;
1140 struct perf_counter_context *ctx;
1141 struct task_struct *task;
1142
1143 /*
1144 * If cpu is not a wildcard then this is a percpu counter:
1145 */
1146 if (cpu != -1) {
1147 /* Must be root to operate on a CPU counter: */
1148 if (!capable(CAP_SYS_ADMIN))
1149 return ERR_PTR(-EACCES);
1150
1151 if (cpu < 0 || cpu > num_possible_cpus())
1152 return ERR_PTR(-EINVAL);
1153
1154 /*
1155 * We could be clever and allow to attach a counter to an
1156 * offline CPU and activate it when the CPU comes up, but
1157 * that's for later.
1158 */
1159 if (!cpu_isset(cpu, cpu_online_map))
1160 return ERR_PTR(-ENODEV);
1161
1162 cpuctx = &per_cpu(perf_cpu_context, cpu);
1163 ctx = &cpuctx->ctx;
1164
Thomas Gleixner0793a612008-12-04 20:12:29 +01001165 return ctx;
1166 }
1167
1168 rcu_read_lock();
1169 if (!pid)
1170 task = current;
1171 else
1172 task = find_task_by_vpid(pid);
1173 if (task)
1174 get_task_struct(task);
1175 rcu_read_unlock();
1176
1177 if (!task)
1178 return ERR_PTR(-ESRCH);
1179
1180 ctx = &task->perf_counter_ctx;
1181 ctx->task = task;
1182
1183 /* Reuse ptrace permission checks for now. */
1184 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
1185 put_context(ctx);
1186 return ERR_PTR(-EACCES);
1187 }
1188
1189 return ctx;
1190}
1191
Peter Zijlstra592903c2009-03-13 12:21:36 +01001192static void free_counter_rcu(struct rcu_head *head)
1193{
1194 struct perf_counter *counter;
1195
1196 counter = container_of(head, struct perf_counter, rcu_head);
1197 kfree(counter);
1198}
1199
Peter Zijlstra925d5192009-03-30 19:07:02 +02001200static void perf_pending_sync(struct perf_counter *counter);
1201
Peter Zijlstraf1600952009-03-19 20:26:16 +01001202static void free_counter(struct perf_counter *counter)
1203{
Peter Zijlstra925d5192009-03-30 19:07:02 +02001204 perf_pending_sync(counter);
1205
Peter Zijlstrae077df42009-03-19 20:26:17 +01001206 if (counter->destroy)
1207 counter->destroy(counter);
1208
Peter Zijlstraf1600952009-03-19 20:26:16 +01001209 call_rcu(&counter->rcu_head, free_counter_rcu);
1210}
1211
Thomas Gleixner0793a612008-12-04 20:12:29 +01001212/*
1213 * Called when the last reference to the file is gone.
1214 */
1215static int perf_release(struct inode *inode, struct file *file)
1216{
1217 struct perf_counter *counter = file->private_data;
1218 struct perf_counter_context *ctx = counter->ctx;
1219
1220 file->private_data = NULL;
1221
Paul Mackerrasd859e292009-01-17 18:10:22 +11001222 mutex_lock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001223 mutex_lock(&counter->mutex);
1224
Ingo Molnar04289bb2008-12-11 08:38:42 +01001225 perf_counter_remove_from_context(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001226
1227 mutex_unlock(&counter->mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001228 mutex_unlock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001229
Peter Zijlstraf1600952009-03-19 20:26:16 +01001230 free_counter(counter);
Mike Galbraith5af75912009-02-11 10:53:37 +01001231 put_context(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001232
1233 return 0;
1234}
1235
1236/*
1237 * Read the performance counter - simple non blocking version for now
1238 */
1239static ssize_t
1240perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
1241{
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001242 u64 values[3];
1243 int n;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001244
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001245 /*
1246 * Return end-of-file for a read on a counter that is in
1247 * error state (i.e. because it was pinned but it couldn't be
1248 * scheduled on to the CPU at some point).
1249 */
1250 if (counter->state == PERF_COUNTER_STATE_ERROR)
1251 return 0;
1252
Thomas Gleixner0793a612008-12-04 20:12:29 +01001253 mutex_lock(&counter->mutex);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001254 values[0] = perf_counter_read(counter);
1255 n = 1;
1256 if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1257 values[n++] = counter->total_time_enabled +
1258 atomic64_read(&counter->child_total_time_enabled);
1259 if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1260 values[n++] = counter->total_time_running +
1261 atomic64_read(&counter->child_total_time_running);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001262 mutex_unlock(&counter->mutex);
1263
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001264 if (count < n * sizeof(u64))
1265 return -EINVAL;
1266 count = n * sizeof(u64);
1267
1268 if (copy_to_user(buf, values, count))
1269 return -EFAULT;
1270
1271 return count;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001272}
1273
1274static ssize_t
Thomas Gleixner0793a612008-12-04 20:12:29 +01001275perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
1276{
1277 struct perf_counter *counter = file->private_data;
1278
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001279 return perf_read_hw(counter, buf, count);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001280}
1281
1282static unsigned int perf_poll(struct file *file, poll_table *wait)
1283{
1284 struct perf_counter *counter = file->private_data;
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001285 struct perf_mmap_data *data;
1286 unsigned int events;
1287
1288 rcu_read_lock();
1289 data = rcu_dereference(counter->data);
1290 if (data)
1291 events = atomic_xchg(&data->wakeup, 0);
1292 else
1293 events = POLL_HUP;
1294 rcu_read_unlock();
Thomas Gleixner0793a612008-12-04 20:12:29 +01001295
1296 poll_wait(file, &counter->waitq, wait);
1297
Thomas Gleixner0793a612008-12-04 20:12:29 +01001298 return events;
1299}
1300
Paul Mackerrasd859e292009-01-17 18:10:22 +11001301static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1302{
1303 struct perf_counter *counter = file->private_data;
1304 int err = 0;
1305
1306 switch (cmd) {
1307 case PERF_COUNTER_IOC_ENABLE:
1308 perf_counter_enable_family(counter);
1309 break;
1310 case PERF_COUNTER_IOC_DISABLE:
1311 perf_counter_disable_family(counter);
1312 break;
1313 default:
1314 err = -ENOTTY;
1315 }
1316 return err;
1317}
1318
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001319/*
1320 * Callers need to ensure there can be no nesting of this function, otherwise
1321 * the seqlock logic goes bad. We can not serialize this because the arch
1322 * code calls this from NMI context.
1323 */
1324void perf_counter_update_userpage(struct perf_counter *counter)
Paul Mackerras37d81822009-03-23 18:22:08 +01001325{
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001326 struct perf_mmap_data *data;
1327 struct perf_counter_mmap_page *userpg;
1328
1329 rcu_read_lock();
1330 data = rcu_dereference(counter->data);
1331 if (!data)
1332 goto unlock;
1333
1334 userpg = data->user_page;
Paul Mackerras37d81822009-03-23 18:22:08 +01001335
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001336 /*
1337 * Disable preemption so as to not let the corresponding user-space
1338 * spin too long if we get preempted.
1339 */
1340 preempt_disable();
Paul Mackerras37d81822009-03-23 18:22:08 +01001341 ++userpg->lock;
1342 smp_wmb();
1343 userpg->index = counter->hw.idx;
1344 userpg->offset = atomic64_read(&counter->count);
1345 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
1346 userpg->offset -= atomic64_read(&counter->hw.prev_count);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001347
Paul Mackerras37d81822009-03-23 18:22:08 +01001348 smp_wmb();
1349 ++userpg->lock;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001350 preempt_enable();
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001351unlock:
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001352 rcu_read_unlock();
Paul Mackerras37d81822009-03-23 18:22:08 +01001353}
1354
1355static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1356{
1357 struct perf_counter *counter = vma->vm_file->private_data;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001358 struct perf_mmap_data *data;
1359 int ret = VM_FAULT_SIGBUS;
Paul Mackerras37d81822009-03-23 18:22:08 +01001360
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001361 rcu_read_lock();
1362 data = rcu_dereference(counter->data);
1363 if (!data)
1364 goto unlock;
Paul Mackerras37d81822009-03-23 18:22:08 +01001365
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001366 if (vmf->pgoff == 0) {
1367 vmf->page = virt_to_page(data->user_page);
1368 } else {
1369 int nr = vmf->pgoff - 1;
1370
1371 if ((unsigned)nr > data->nr_pages)
1372 goto unlock;
1373
1374 vmf->page = virt_to_page(data->data_pages[nr]);
1375 }
Paul Mackerras37d81822009-03-23 18:22:08 +01001376 get_page(vmf->page);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001377 ret = 0;
1378unlock:
1379 rcu_read_unlock();
1380
1381 return ret;
1382}
1383
1384static int perf_mmap_data_alloc(struct perf_counter *counter, int nr_pages)
1385{
1386 struct perf_mmap_data *data;
1387 unsigned long size;
1388 int i;
1389
1390 WARN_ON(atomic_read(&counter->mmap_count));
1391
1392 size = sizeof(struct perf_mmap_data);
1393 size += nr_pages * sizeof(void *);
1394
1395 data = kzalloc(size, GFP_KERNEL);
1396 if (!data)
1397 goto fail;
1398
1399 data->user_page = (void *)get_zeroed_page(GFP_KERNEL);
1400 if (!data->user_page)
1401 goto fail_user_page;
1402
1403 for (i = 0; i < nr_pages; i++) {
1404 data->data_pages[i] = (void *)get_zeroed_page(GFP_KERNEL);
1405 if (!data->data_pages[i])
1406 goto fail_data_pages;
1407 }
1408
1409 data->nr_pages = nr_pages;
1410
1411 rcu_assign_pointer(counter->data, data);
1412
Paul Mackerras37d81822009-03-23 18:22:08 +01001413 return 0;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001414
1415fail_data_pages:
1416 for (i--; i >= 0; i--)
1417 free_page((unsigned long)data->data_pages[i]);
1418
1419 free_page((unsigned long)data->user_page);
1420
1421fail_user_page:
1422 kfree(data);
1423
1424fail:
1425 return -ENOMEM;
1426}
1427
1428static void __perf_mmap_data_free(struct rcu_head *rcu_head)
1429{
1430 struct perf_mmap_data *data = container_of(rcu_head,
1431 struct perf_mmap_data, rcu_head);
1432 int i;
1433
1434 free_page((unsigned long)data->user_page);
1435 for (i = 0; i < data->nr_pages; i++)
1436 free_page((unsigned long)data->data_pages[i]);
1437 kfree(data);
1438}
1439
1440static void perf_mmap_data_free(struct perf_counter *counter)
1441{
1442 struct perf_mmap_data *data = counter->data;
1443
1444 WARN_ON(atomic_read(&counter->mmap_count));
1445
1446 rcu_assign_pointer(counter->data, NULL);
1447 call_rcu(&data->rcu_head, __perf_mmap_data_free);
1448}
1449
1450static void perf_mmap_open(struct vm_area_struct *vma)
1451{
1452 struct perf_counter *counter = vma->vm_file->private_data;
1453
1454 atomic_inc(&counter->mmap_count);
1455}
1456
1457static void perf_mmap_close(struct vm_area_struct *vma)
1458{
1459 struct perf_counter *counter = vma->vm_file->private_data;
1460
1461 if (atomic_dec_and_mutex_lock(&counter->mmap_count,
1462 &counter->mmap_mutex)) {
1463 perf_mmap_data_free(counter);
1464 mutex_unlock(&counter->mmap_mutex);
1465 }
Paul Mackerras37d81822009-03-23 18:22:08 +01001466}
1467
1468static struct vm_operations_struct perf_mmap_vmops = {
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001469 .open = perf_mmap_open,
1470 .close = perf_mmap_close,
Paul Mackerras37d81822009-03-23 18:22:08 +01001471 .fault = perf_mmap_fault,
1472};
1473
1474static int perf_mmap(struct file *file, struct vm_area_struct *vma)
1475{
1476 struct perf_counter *counter = file->private_data;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001477 unsigned long vma_size;
1478 unsigned long nr_pages;
1479 unsigned long locked, lock_limit;
1480 int ret = 0;
Paul Mackerras37d81822009-03-23 18:22:08 +01001481
1482 if (!(vma->vm_flags & VM_SHARED) || (vma->vm_flags & VM_WRITE))
1483 return -EINVAL;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001484
1485 vma_size = vma->vm_end - vma->vm_start;
1486 nr_pages = (vma_size / PAGE_SIZE) - 1;
1487
Peter Zijlstra7730d862009-03-25 12:48:31 +01001488 /*
1489 * If we have data pages ensure they're a power-of-two number, so we
1490 * can do bitmasks instead of modulo.
1491 */
1492 if (nr_pages != 0 && !is_power_of_2(nr_pages))
Paul Mackerras37d81822009-03-23 18:22:08 +01001493 return -EINVAL;
1494
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001495 if (vma_size != PAGE_SIZE * (1 + nr_pages))
Paul Mackerras37d81822009-03-23 18:22:08 +01001496 return -EINVAL;
1497
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001498 if (vma->vm_pgoff != 0)
1499 return -EINVAL;
Paul Mackerras37d81822009-03-23 18:22:08 +01001500
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001501 locked = vma_size >> PAGE_SHIFT;
1502 locked += vma->vm_mm->locked_vm;
1503
1504 lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
1505 lock_limit >>= PAGE_SHIFT;
1506
1507 if ((locked > lock_limit) && !capable(CAP_IPC_LOCK))
1508 return -EPERM;
1509
1510 mutex_lock(&counter->mmap_mutex);
1511 if (atomic_inc_not_zero(&counter->mmap_count))
1512 goto out;
1513
1514 WARN_ON(counter->data);
1515 ret = perf_mmap_data_alloc(counter, nr_pages);
1516 if (!ret)
1517 atomic_set(&counter->mmap_count, 1);
1518out:
1519 mutex_unlock(&counter->mmap_mutex);
Paul Mackerras37d81822009-03-23 18:22:08 +01001520
1521 vma->vm_flags &= ~VM_MAYWRITE;
1522 vma->vm_flags |= VM_RESERVED;
1523 vma->vm_ops = &perf_mmap_vmops;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001524
1525 return ret;
Paul Mackerras37d81822009-03-23 18:22:08 +01001526}
1527
Thomas Gleixner0793a612008-12-04 20:12:29 +01001528static const struct file_operations perf_fops = {
1529 .release = perf_release,
1530 .read = perf_read,
1531 .poll = perf_poll,
Paul Mackerrasd859e292009-01-17 18:10:22 +11001532 .unlocked_ioctl = perf_ioctl,
1533 .compat_ioctl = perf_ioctl,
Paul Mackerras37d81822009-03-23 18:22:08 +01001534 .mmap = perf_mmap,
Thomas Gleixner0793a612008-12-04 20:12:29 +01001535};
1536
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001537/*
Peter Zijlstra925d5192009-03-30 19:07:02 +02001538 * Perf counter wakeup
1539 *
1540 * If there's data, ensure we set the poll() state and publish everything
1541 * to user-space before waking everybody up.
1542 */
1543
1544void perf_counter_wakeup(struct perf_counter *counter)
1545{
1546 struct perf_mmap_data *data;
1547
1548 rcu_read_lock();
1549 data = rcu_dereference(counter->data);
1550 if (data) {
1551 (void)atomic_xchg(&data->wakeup, POLL_IN);
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001552 /*
1553 * Ensure all data writes are issued before updating the
1554 * user-space data head information. The matching rmb()
1555 * will be in userspace after reading this value.
1556 */
1557 smp_wmb();
1558 data->user_page->data_head = atomic_read(&data->head);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001559 }
1560 rcu_read_unlock();
1561
1562 wake_up_all(&counter->waitq);
1563}
1564
1565/*
1566 * Pending wakeups
1567 *
1568 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
1569 *
1570 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
1571 * single linked list and use cmpxchg() to add entries lockless.
1572 */
1573
1574#define PENDING_TAIL ((struct perf_wakeup_entry *)-1UL)
1575
1576static DEFINE_PER_CPU(struct perf_wakeup_entry *, perf_wakeup_head) = {
1577 PENDING_TAIL,
1578};
1579
1580static void perf_pending_queue(struct perf_counter *counter)
1581{
1582 struct perf_wakeup_entry **head;
1583 struct perf_wakeup_entry *prev, *next;
1584
1585 if (cmpxchg(&counter->wakeup.next, NULL, PENDING_TAIL) != NULL)
1586 return;
1587
1588 head = &get_cpu_var(perf_wakeup_head);
1589
1590 do {
1591 prev = counter->wakeup.next = *head;
1592 next = &counter->wakeup;
1593 } while (cmpxchg(head, prev, next) != prev);
1594
1595 set_perf_counter_pending();
1596
1597 put_cpu_var(perf_wakeup_head);
1598}
1599
1600static int __perf_pending_run(void)
1601{
1602 struct perf_wakeup_entry *list;
1603 int nr = 0;
1604
1605 list = xchg(&__get_cpu_var(perf_wakeup_head), PENDING_TAIL);
1606 while (list != PENDING_TAIL) {
1607 struct perf_counter *counter = container_of(list,
1608 struct perf_counter, wakeup);
1609
1610 list = list->next;
1611
1612 counter->wakeup.next = NULL;
1613 /*
1614 * Ensure we observe the unqueue before we issue the wakeup,
1615 * so that we won't be waiting forever.
1616 * -- see perf_not_pending().
1617 */
1618 smp_wmb();
1619
1620 perf_counter_wakeup(counter);
1621 nr++;
1622 }
1623
1624 return nr;
1625}
1626
1627static inline int perf_not_pending(struct perf_counter *counter)
1628{
1629 /*
1630 * If we flush on whatever cpu we run, there is a chance we don't
1631 * need to wait.
1632 */
1633 get_cpu();
1634 __perf_pending_run();
1635 put_cpu();
1636
1637 /*
1638 * Ensure we see the proper queue state before going to sleep
1639 * so that we do not miss the wakeup. -- see perf_pending_handle()
1640 */
1641 smp_rmb();
1642 return counter->wakeup.next == NULL;
1643}
1644
1645static void perf_pending_sync(struct perf_counter *counter)
1646{
1647 wait_event(counter->waitq, perf_not_pending(counter));
1648}
1649
1650void perf_counter_do_pending(void)
1651{
1652 __perf_pending_run();
1653}
1654
1655/*
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001656 * Output
1657 */
1658
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001659struct perf_output_handle {
1660 struct perf_counter *counter;
1661 struct perf_mmap_data *data;
1662 unsigned int offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01001663 unsigned int head;
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001664 int wakeup;
1665};
1666
1667static int perf_output_begin(struct perf_output_handle *handle,
1668 struct perf_counter *counter, unsigned int size)
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001669{
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001670 struct perf_mmap_data *data;
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001671 unsigned int offset, head;
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001672
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001673 rcu_read_lock();
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001674 data = rcu_dereference(counter->data);
1675 if (!data)
1676 goto out;
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001677
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001678 if (!data->nr_pages)
1679 goto out;
1680
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001681 do {
1682 offset = head = atomic_read(&data->head);
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001683 head += size;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001684 } while (atomic_cmpxchg(&data->head, offset, head) != offset);
1685
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001686 handle->counter = counter;
1687 handle->data = data;
1688 handle->offset = offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01001689 handle->head = head;
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001690 handle->wakeup = (offset >> PAGE_SHIFT) != (head >> PAGE_SHIFT);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001691
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001692 return 0;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001693
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001694out:
1695 rcu_read_unlock();
1696
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001697 return -ENOSPC;
1698}
1699
1700static void perf_output_copy(struct perf_output_handle *handle,
1701 void *buf, unsigned int len)
1702{
1703 unsigned int pages_mask;
1704 unsigned int offset;
1705 unsigned int size;
1706 void **pages;
1707
1708 offset = handle->offset;
1709 pages_mask = handle->data->nr_pages - 1;
1710 pages = handle->data->data_pages;
1711
1712 do {
1713 unsigned int page_offset;
1714 int nr;
1715
1716 nr = (offset >> PAGE_SHIFT) & pages_mask;
1717 page_offset = offset & (PAGE_SIZE - 1);
1718 size = min_t(unsigned int, PAGE_SIZE - page_offset, len);
1719
1720 memcpy(pages[nr] + page_offset, buf, size);
1721
1722 len -= size;
1723 buf += size;
1724 offset += size;
1725 } while (len);
1726
1727 handle->offset = offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01001728
1729 WARN_ON_ONCE(handle->offset > handle->head);
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001730}
1731
Peter Zijlstra5c148192009-03-25 12:30:23 +01001732#define perf_output_put(handle, x) \
1733 perf_output_copy((handle), &(x), sizeof(x))
1734
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001735static void perf_output_end(struct perf_output_handle *handle, int nmi)
1736{
1737 if (handle->wakeup) {
Peter Zijlstra925d5192009-03-30 19:07:02 +02001738 if (nmi)
1739 perf_pending_queue(handle->counter);
1740 else
1741 perf_counter_wakeup(handle->counter);
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001742 }
1743 rcu_read_unlock();
1744}
1745
1746static int perf_output_write(struct perf_counter *counter, int nmi,
1747 void *buf, ssize_t size)
1748{
1749 struct perf_output_handle handle;
1750 int ret;
1751
1752 ret = perf_output_begin(&handle, counter, size);
1753 if (ret)
1754 goto out;
1755
1756 perf_output_copy(&handle, buf, size);
1757 perf_output_end(&handle, nmi);
1758
1759out:
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001760 return ret;
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001761}
1762
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001763static void perf_output_simple(struct perf_counter *counter,
1764 int nmi, struct pt_regs *regs)
1765{
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01001766 unsigned int size;
Peter Zijlstra5c148192009-03-25 12:30:23 +01001767 struct {
1768 struct perf_event_header header;
1769 u64 ip;
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01001770 u32 pid, tid;
Peter Zijlstra5c148192009-03-25 12:30:23 +01001771 } event;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001772
Peter Zijlstra5c148192009-03-25 12:30:23 +01001773 event.header.type = PERF_EVENT_IP;
Peter Zijlstra5c148192009-03-25 12:30:23 +01001774 event.ip = instruction_pointer(regs);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001775
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01001776 size = sizeof(event);
1777
1778 if (counter->hw_event.include_tid) {
1779 /* namespace issues */
1780 event.pid = current->group_leader->pid;
1781 event.tid = current->pid;
1782
1783 event.header.type |= __PERF_EVENT_TID;
1784 } else
1785 size -= sizeof(u64);
1786
1787 event.header.size = size;
1788
1789 perf_output_write(counter, nmi, &event, size);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001790}
1791
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001792static void perf_output_group(struct perf_counter *counter, int nmi)
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001793{
Peter Zijlstra5c148192009-03-25 12:30:23 +01001794 struct perf_output_handle handle;
1795 struct perf_event_header header;
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001796 struct perf_counter *leader, *sub;
Peter Zijlstra5c148192009-03-25 12:30:23 +01001797 unsigned int size;
1798 struct {
1799 u64 event;
1800 u64 counter;
1801 } entry;
1802 int ret;
1803
1804 size = sizeof(header) + counter->nr_siblings * sizeof(entry);
1805
1806 ret = perf_output_begin(&handle, counter, size);
1807 if (ret)
1808 return;
1809
1810 header.type = PERF_EVENT_GROUP;
1811 header.size = size;
1812
1813 perf_output_put(&handle, header);
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001814
1815 leader = counter->group_leader;
1816 list_for_each_entry(sub, &leader->sibling_list, list_entry) {
1817 if (sub != counter)
1818 sub->hw_ops->read(sub);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001819
1820 entry.event = sub->hw_event.config;
1821 entry.counter = atomic64_read(&sub->count);
1822
Peter Zijlstra5c148192009-03-25 12:30:23 +01001823 perf_output_put(&handle, entry);
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001824 }
Peter Zijlstra5c148192009-03-25 12:30:23 +01001825
1826 perf_output_end(&handle, nmi);
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001827}
1828
1829void perf_counter_output(struct perf_counter *counter,
1830 int nmi, struct pt_regs *regs)
1831{
1832 switch (counter->hw_event.record_type) {
1833 case PERF_RECORD_SIMPLE:
1834 return;
1835
1836 case PERF_RECORD_IRQ:
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001837 perf_output_simple(counter, nmi, regs);
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001838 break;
1839
1840 case PERF_RECORD_GROUP:
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001841 perf_output_group(counter, nmi);
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001842 break;
1843 }
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001844}
1845
1846/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001847 * Generic software counter infrastructure
1848 */
1849
1850static void perf_swcounter_update(struct perf_counter *counter)
1851{
1852 struct hw_perf_counter *hwc = &counter->hw;
1853 u64 prev, now;
1854 s64 delta;
1855
1856again:
1857 prev = atomic64_read(&hwc->prev_count);
1858 now = atomic64_read(&hwc->count);
1859 if (atomic64_cmpxchg(&hwc->prev_count, prev, now) != prev)
1860 goto again;
1861
1862 delta = now - prev;
1863
1864 atomic64_add(delta, &counter->count);
1865 atomic64_sub(delta, &hwc->period_left);
1866}
1867
1868static void perf_swcounter_set_period(struct perf_counter *counter)
1869{
1870 struct hw_perf_counter *hwc = &counter->hw;
1871 s64 left = atomic64_read(&hwc->period_left);
1872 s64 period = hwc->irq_period;
1873
1874 if (unlikely(left <= -period)) {
1875 left = period;
1876 atomic64_set(&hwc->period_left, left);
1877 }
1878
1879 if (unlikely(left <= 0)) {
1880 left += period;
1881 atomic64_add(period, &hwc->period_left);
1882 }
1883
1884 atomic64_set(&hwc->prev_count, -left);
1885 atomic64_set(&hwc->count, -left);
1886}
1887
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01001888static enum hrtimer_restart perf_swcounter_hrtimer(struct hrtimer *hrtimer)
1889{
1890 struct perf_counter *counter;
1891 struct pt_regs *regs;
1892
1893 counter = container_of(hrtimer, struct perf_counter, hw.hrtimer);
1894 counter->hw_ops->read(counter);
1895
1896 regs = get_irq_regs();
1897 /*
1898 * In case we exclude kernel IPs or are somehow not in interrupt
1899 * context, provide the next best thing, the user IP.
1900 */
1901 if ((counter->hw_event.exclude_kernel || !regs) &&
1902 !counter->hw_event.exclude_user)
1903 regs = task_pt_regs(current);
1904
1905 if (regs)
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001906 perf_counter_output(counter, 0, regs);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01001907
1908 hrtimer_forward_now(hrtimer, ns_to_ktime(counter->hw.irq_period));
1909
1910 return HRTIMER_RESTART;
1911}
1912
1913static void perf_swcounter_overflow(struct perf_counter *counter,
1914 int nmi, struct pt_regs *regs)
1915{
Peter Zijlstrab8e83512009-03-19 20:26:18 +01001916 perf_swcounter_update(counter);
1917 perf_swcounter_set_period(counter);
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001918 perf_counter_output(counter, nmi, regs);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01001919}
1920
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001921static int perf_swcounter_match(struct perf_counter *counter,
Peter Zijlstrab8e83512009-03-19 20:26:18 +01001922 enum perf_event_types type,
1923 u32 event, struct pt_regs *regs)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001924{
1925 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
1926 return 0;
1927
Peter Zijlstraf4a2deb42009-03-23 18:22:06 +01001928 if (perf_event_raw(&counter->hw_event))
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001929 return 0;
1930
Peter Zijlstraf4a2deb42009-03-23 18:22:06 +01001931 if (perf_event_type(&counter->hw_event) != type)
Peter Zijlstrab8e83512009-03-19 20:26:18 +01001932 return 0;
1933
Peter Zijlstraf4a2deb42009-03-23 18:22:06 +01001934 if (perf_event_id(&counter->hw_event) != event)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001935 return 0;
1936
1937 if (counter->hw_event.exclude_user && user_mode(regs))
1938 return 0;
1939
1940 if (counter->hw_event.exclude_kernel && !user_mode(regs))
1941 return 0;
1942
1943 return 1;
1944}
1945
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01001946static void perf_swcounter_add(struct perf_counter *counter, u64 nr,
1947 int nmi, struct pt_regs *regs)
1948{
1949 int neg = atomic64_add_negative(nr, &counter->hw.count);
1950 if (counter->hw.irq_period && !neg)
1951 perf_swcounter_overflow(counter, nmi, regs);
1952}
1953
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001954static void perf_swcounter_ctx_event(struct perf_counter_context *ctx,
Peter Zijlstrab8e83512009-03-19 20:26:18 +01001955 enum perf_event_types type, u32 event,
1956 u64 nr, int nmi, struct pt_regs *regs)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001957{
1958 struct perf_counter *counter;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001959
Peter Zijlstra01ef09d2009-03-19 20:26:11 +01001960 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001961 return;
1962
Peter Zijlstra592903c2009-03-13 12:21:36 +01001963 rcu_read_lock();
1964 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
Peter Zijlstrab8e83512009-03-19 20:26:18 +01001965 if (perf_swcounter_match(counter, type, event, regs))
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01001966 perf_swcounter_add(counter, nr, nmi, regs);
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001967 }
Peter Zijlstra592903c2009-03-13 12:21:36 +01001968 rcu_read_unlock();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001969}
1970
Peter Zijlstra96f6d442009-03-23 18:22:07 +01001971static int *perf_swcounter_recursion_context(struct perf_cpu_context *cpuctx)
1972{
1973 if (in_nmi())
1974 return &cpuctx->recursion[3];
1975
1976 if (in_irq())
1977 return &cpuctx->recursion[2];
1978
1979 if (in_softirq())
1980 return &cpuctx->recursion[1];
1981
1982 return &cpuctx->recursion[0];
1983}
1984
Peter Zijlstrab8e83512009-03-19 20:26:18 +01001985static void __perf_swcounter_event(enum perf_event_types type, u32 event,
1986 u64 nr, int nmi, struct pt_regs *regs)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001987{
1988 struct perf_cpu_context *cpuctx = &get_cpu_var(perf_cpu_context);
Peter Zijlstra96f6d442009-03-23 18:22:07 +01001989 int *recursion = perf_swcounter_recursion_context(cpuctx);
1990
1991 if (*recursion)
1992 goto out;
1993
1994 (*recursion)++;
1995 barrier();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001996
Peter Zijlstrab8e83512009-03-19 20:26:18 +01001997 perf_swcounter_ctx_event(&cpuctx->ctx, type, event, nr, nmi, regs);
1998 if (cpuctx->task_ctx) {
1999 perf_swcounter_ctx_event(cpuctx->task_ctx, type, event,
2000 nr, nmi, regs);
2001 }
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002002
Peter Zijlstra96f6d442009-03-23 18:22:07 +01002003 barrier();
2004 (*recursion)--;
2005
2006out:
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002007 put_cpu_var(perf_cpu_context);
2008}
2009
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002010void perf_swcounter_event(u32 event, u64 nr, int nmi, struct pt_regs *regs)
2011{
2012 __perf_swcounter_event(PERF_TYPE_SOFTWARE, event, nr, nmi, regs);
2013}
2014
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002015static void perf_swcounter_read(struct perf_counter *counter)
2016{
2017 perf_swcounter_update(counter);
2018}
2019
2020static int perf_swcounter_enable(struct perf_counter *counter)
2021{
2022 perf_swcounter_set_period(counter);
2023 return 0;
2024}
2025
2026static void perf_swcounter_disable(struct perf_counter *counter)
2027{
2028 perf_swcounter_update(counter);
2029}
2030
Peter Zijlstraac17dc82009-03-13 12:21:34 +01002031static const struct hw_perf_counter_ops perf_ops_generic = {
2032 .enable = perf_swcounter_enable,
2033 .disable = perf_swcounter_disable,
2034 .read = perf_swcounter_read,
2035};
2036
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002037/*
2038 * Software counter: cpu wall time clock
2039 */
2040
Paul Mackerras9abf8a02009-01-09 16:26:43 +11002041static void cpu_clock_perf_counter_update(struct perf_counter *counter)
2042{
2043 int cpu = raw_smp_processor_id();
2044 s64 prev;
2045 u64 now;
2046
2047 now = cpu_clock(cpu);
2048 prev = atomic64_read(&counter->hw.prev_count);
2049 atomic64_set(&counter->hw.prev_count, now);
2050 atomic64_add(now - prev, &counter->count);
2051}
2052
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002053static int cpu_clock_perf_counter_enable(struct perf_counter *counter)
2054{
2055 struct hw_perf_counter *hwc = &counter->hw;
2056 int cpu = raw_smp_processor_id();
2057
2058 atomic64_set(&hwc->prev_count, cpu_clock(cpu));
Peter Zijlstra039fc912009-03-13 16:43:47 +01002059 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2060 hwc->hrtimer.function = perf_swcounter_hrtimer;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002061 if (hwc->irq_period) {
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002062 __hrtimer_start_range_ns(&hwc->hrtimer,
2063 ns_to_ktime(hwc->irq_period), 0,
2064 HRTIMER_MODE_REL, 0);
2065 }
2066
2067 return 0;
2068}
2069
Ingo Molnar5c92d122008-12-11 13:21:10 +01002070static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
2071{
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002072 hrtimer_cancel(&counter->hw.hrtimer);
Paul Mackerras9abf8a02009-01-09 16:26:43 +11002073 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01002074}
2075
2076static void cpu_clock_perf_counter_read(struct perf_counter *counter)
2077{
Paul Mackerras9abf8a02009-01-09 16:26:43 +11002078 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01002079}
2080
2081static const struct hw_perf_counter_ops perf_ops_cpu_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +01002082 .enable = cpu_clock_perf_counter_enable,
2083 .disable = cpu_clock_perf_counter_disable,
2084 .read = cpu_clock_perf_counter_read,
Ingo Molnar5c92d122008-12-11 13:21:10 +01002085};
2086
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01002087/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002088 * Software counter: task time clock
2089 */
2090
2091/*
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01002092 * Called from within the scheduler:
2093 */
2094static u64 task_clock_perf_counter_val(struct perf_counter *counter, int update)
Ingo Molnarbae43c92008-12-11 14:03:20 +01002095{
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01002096 struct task_struct *curr = counter->task;
2097 u64 delta;
2098
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01002099 delta = __task_delta_exec(curr, update);
2100
2101 return curr->se.sum_exec_runtime + delta;
2102}
2103
2104static void task_clock_perf_counter_update(struct perf_counter *counter, u64 now)
2105{
2106 u64 prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002107 s64 delta;
Ingo Molnarbae43c92008-12-11 14:03:20 +01002108
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002109 prev = atomic64_read(&counter->hw.prev_count);
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002110
2111 atomic64_set(&counter->hw.prev_count, now);
2112
2113 delta = now - prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002114
2115 atomic64_add(delta, &counter->count);
Ingo Molnarbae43c92008-12-11 14:03:20 +01002116}
2117
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01002118static int task_clock_perf_counter_enable(struct perf_counter *counter)
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002119{
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002120 struct hw_perf_counter *hwc = &counter->hw;
2121
2122 atomic64_set(&hwc->prev_count, task_clock_perf_counter_val(counter, 0));
Peter Zijlstra039fc912009-03-13 16:43:47 +01002123 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2124 hwc->hrtimer.function = perf_swcounter_hrtimer;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002125 if (hwc->irq_period) {
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002126 __hrtimer_start_range_ns(&hwc->hrtimer,
2127 ns_to_ktime(hwc->irq_period), 0,
2128 HRTIMER_MODE_REL, 0);
2129 }
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01002130
2131 return 0;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002132}
2133
2134static void task_clock_perf_counter_disable(struct perf_counter *counter)
2135{
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002136 hrtimer_cancel(&counter->hw.hrtimer);
2137 task_clock_perf_counter_update(counter,
2138 task_clock_perf_counter_val(counter, 0));
2139}
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01002140
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002141static void task_clock_perf_counter_read(struct perf_counter *counter)
2142{
2143 task_clock_perf_counter_update(counter,
2144 task_clock_perf_counter_val(counter, 1));
Ingo Molnarbae43c92008-12-11 14:03:20 +01002145}
2146
2147static const struct hw_perf_counter_ops perf_ops_task_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +01002148 .enable = task_clock_perf_counter_enable,
2149 .disable = task_clock_perf_counter_disable,
2150 .read = task_clock_perf_counter_read,
Ingo Molnarbae43c92008-12-11 14:03:20 +01002151};
2152
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002153/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002154 * Software counter: cpu migrations
2155 */
2156
Paul Mackerras23a185c2009-02-09 22:42:47 +11002157static inline u64 get_cpu_migrations(struct perf_counter *counter)
Ingo Molnar6c594c22008-12-14 12:34:15 +01002158{
Paul Mackerras23a185c2009-02-09 22:42:47 +11002159 struct task_struct *curr = counter->ctx->task;
2160
2161 if (curr)
2162 return curr->se.nr_migrations;
2163 return cpu_nr_migrations(smp_processor_id());
Ingo Molnar6c594c22008-12-14 12:34:15 +01002164}
2165
2166static void cpu_migrations_perf_counter_update(struct perf_counter *counter)
2167{
2168 u64 prev, now;
2169 s64 delta;
2170
2171 prev = atomic64_read(&counter->hw.prev_count);
Paul Mackerras23a185c2009-02-09 22:42:47 +11002172 now = get_cpu_migrations(counter);
Ingo Molnar6c594c22008-12-14 12:34:15 +01002173
2174 atomic64_set(&counter->hw.prev_count, now);
2175
2176 delta = now - prev;
Ingo Molnar6c594c22008-12-14 12:34:15 +01002177
2178 atomic64_add(delta, &counter->count);
2179}
2180
2181static void cpu_migrations_perf_counter_read(struct perf_counter *counter)
2182{
2183 cpu_migrations_perf_counter_update(counter);
2184}
2185
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01002186static int cpu_migrations_perf_counter_enable(struct perf_counter *counter)
Ingo Molnar6c594c22008-12-14 12:34:15 +01002187{
Paul Mackerrasc07c99b2009-02-13 22:10:34 +11002188 if (counter->prev_state <= PERF_COUNTER_STATE_OFF)
2189 atomic64_set(&counter->hw.prev_count,
2190 get_cpu_migrations(counter));
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01002191 return 0;
Ingo Molnar6c594c22008-12-14 12:34:15 +01002192}
2193
2194static void cpu_migrations_perf_counter_disable(struct perf_counter *counter)
2195{
2196 cpu_migrations_perf_counter_update(counter);
2197}
2198
2199static const struct hw_perf_counter_ops perf_ops_cpu_migrations = {
Ingo Molnar76715812008-12-17 14:20:28 +01002200 .enable = cpu_migrations_perf_counter_enable,
2201 .disable = cpu_migrations_perf_counter_disable,
2202 .read = cpu_migrations_perf_counter_read,
Ingo Molnar6c594c22008-12-14 12:34:15 +01002203};
2204
Peter Zijlstrae077df42009-03-19 20:26:17 +01002205#ifdef CONFIG_EVENT_PROFILE
2206void perf_tpcounter_event(int event_id)
2207{
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002208 struct pt_regs *regs = get_irq_regs();
2209
2210 if (!regs)
2211 regs = task_pt_regs(current);
2212
2213 __perf_swcounter_event(PERF_TYPE_TRACEPOINT, event_id, 1, 1, regs);
Peter Zijlstrae077df42009-03-19 20:26:17 +01002214}
2215
2216extern int ftrace_profile_enable(int);
2217extern void ftrace_profile_disable(int);
2218
2219static void tp_perf_counter_destroy(struct perf_counter *counter)
2220{
Peter Zijlstraf4a2deb42009-03-23 18:22:06 +01002221 ftrace_profile_disable(perf_event_id(&counter->hw_event));
Peter Zijlstrae077df42009-03-19 20:26:17 +01002222}
2223
2224static const struct hw_perf_counter_ops *
2225tp_perf_counter_init(struct perf_counter *counter)
2226{
Peter Zijlstraf4a2deb42009-03-23 18:22:06 +01002227 int event_id = perf_event_id(&counter->hw_event);
Peter Zijlstrae077df42009-03-19 20:26:17 +01002228 int ret;
2229
2230 ret = ftrace_profile_enable(event_id);
2231 if (ret)
2232 return NULL;
2233
2234 counter->destroy = tp_perf_counter_destroy;
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002235 counter->hw.irq_period = counter->hw_event.irq_period;
Peter Zijlstrae077df42009-03-19 20:26:17 +01002236
2237 return &perf_ops_generic;
2238}
2239#else
2240static const struct hw_perf_counter_ops *
2241tp_perf_counter_init(struct perf_counter *counter)
2242{
2243 return NULL;
2244}
2245#endif
2246
Ingo Molnar5c92d122008-12-11 13:21:10 +01002247static const struct hw_perf_counter_ops *
2248sw_perf_counter_init(struct perf_counter *counter)
2249{
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002250 struct perf_counter_hw_event *hw_event = &counter->hw_event;
Ingo Molnar5c92d122008-12-11 13:21:10 +01002251 const struct hw_perf_counter_ops *hw_ops = NULL;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002252 struct hw_perf_counter *hwc = &counter->hw;
Ingo Molnar5c92d122008-12-11 13:21:10 +01002253
Paul Mackerras0475f9e2009-02-11 14:35:35 +11002254 /*
2255 * Software counters (currently) can't in general distinguish
2256 * between user, kernel and hypervisor events.
2257 * However, context switches and cpu migrations are considered
2258 * to be kernel events, and page faults are never hypervisor
2259 * events.
2260 */
Peter Zijlstraf4a2deb42009-03-23 18:22:06 +01002261 switch (perf_event_id(&counter->hw_event)) {
Ingo Molnar5c92d122008-12-11 13:21:10 +01002262 case PERF_COUNT_CPU_CLOCK:
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002263 hw_ops = &perf_ops_cpu_clock;
2264
2265 if (hw_event->irq_period && hw_event->irq_period < 10000)
2266 hw_event->irq_period = 10000;
Ingo Molnar5c92d122008-12-11 13:21:10 +01002267 break;
Ingo Molnarbae43c92008-12-11 14:03:20 +01002268 case PERF_COUNT_TASK_CLOCK:
Paul Mackerras23a185c2009-02-09 22:42:47 +11002269 /*
2270 * If the user instantiates this as a per-cpu counter,
2271 * use the cpu_clock counter instead.
2272 */
2273 if (counter->ctx->task)
2274 hw_ops = &perf_ops_task_clock;
2275 else
2276 hw_ops = &perf_ops_cpu_clock;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002277
2278 if (hw_event->irq_period && hw_event->irq_period < 10000)
2279 hw_event->irq_period = 10000;
Ingo Molnarbae43c92008-12-11 14:03:20 +01002280 break;
Ingo Molnare06c61a2008-12-14 14:44:31 +01002281 case PERF_COUNT_PAGE_FAULTS:
Peter Zijlstraac17dc82009-03-13 12:21:34 +01002282 case PERF_COUNT_PAGE_FAULTS_MIN:
2283 case PERF_COUNT_PAGE_FAULTS_MAJ:
Ingo Molnar5d6a27d2008-12-14 12:28:33 +01002284 case PERF_COUNT_CONTEXT_SWITCHES:
Peter Zijlstra4a0deca2009-03-19 20:26:12 +01002285 hw_ops = &perf_ops_generic;
Ingo Molnar5d6a27d2008-12-14 12:28:33 +01002286 break;
Ingo Molnar6c594c22008-12-14 12:34:15 +01002287 case PERF_COUNT_CPU_MIGRATIONS:
Paul Mackerras0475f9e2009-02-11 14:35:35 +11002288 if (!counter->hw_event.exclude_kernel)
2289 hw_ops = &perf_ops_cpu_migrations;
Ingo Molnar6c594c22008-12-14 12:34:15 +01002290 break;
Ingo Molnar5c92d122008-12-11 13:21:10 +01002291 }
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002292
2293 if (hw_ops)
2294 hwc->irq_period = hw_event->irq_period;
2295
Ingo Molnar5c92d122008-12-11 13:21:10 +01002296 return hw_ops;
2297}
2298
Thomas Gleixner0793a612008-12-04 20:12:29 +01002299/*
2300 * Allocate and initialize a counter structure
2301 */
2302static struct perf_counter *
Ingo Molnar04289bb2008-12-11 08:38:42 +01002303perf_counter_alloc(struct perf_counter_hw_event *hw_event,
2304 int cpu,
Paul Mackerras23a185c2009-02-09 22:42:47 +11002305 struct perf_counter_context *ctx,
Ingo Molnar9b51f662008-12-12 13:49:45 +01002306 struct perf_counter *group_leader,
2307 gfp_t gfpflags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01002308{
Ingo Molnar5c92d122008-12-11 13:21:10 +01002309 const struct hw_perf_counter_ops *hw_ops;
Ingo Molnar621a01e2008-12-11 12:46:46 +01002310 struct perf_counter *counter;
Thomas Gleixner0793a612008-12-04 20:12:29 +01002311
Ingo Molnar9b51f662008-12-12 13:49:45 +01002312 counter = kzalloc(sizeof(*counter), gfpflags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002313 if (!counter)
2314 return NULL;
2315
Ingo Molnar04289bb2008-12-11 08:38:42 +01002316 /*
2317 * Single counters are their own group leaders, with an
2318 * empty sibling list:
2319 */
2320 if (!group_leader)
2321 group_leader = counter;
2322
Thomas Gleixner0793a612008-12-04 20:12:29 +01002323 mutex_init(&counter->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01002324 INIT_LIST_HEAD(&counter->list_entry);
Peter Zijlstra592903c2009-03-13 12:21:36 +01002325 INIT_LIST_HEAD(&counter->event_entry);
Ingo Molnar04289bb2008-12-11 08:38:42 +01002326 INIT_LIST_HEAD(&counter->sibling_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002327 init_waitqueue_head(&counter->waitq);
2328
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002329 mutex_init(&counter->mmap_mutex);
2330
Paul Mackerrasd859e292009-01-17 18:10:22 +11002331 INIT_LIST_HEAD(&counter->child_list);
2332
Ingo Molnar9f66a382008-12-10 12:33:23 +01002333 counter->cpu = cpu;
2334 counter->hw_event = *hw_event;
Ingo Molnar04289bb2008-12-11 08:38:42 +01002335 counter->group_leader = group_leader;
Ingo Molnar621a01e2008-12-11 12:46:46 +01002336 counter->hw_ops = NULL;
Paul Mackerras23a185c2009-02-09 22:42:47 +11002337 counter->ctx = ctx;
Ingo Molnar621a01e2008-12-11 12:46:46 +01002338
Ingo Molnar235c7fc2008-12-21 14:43:25 +01002339 counter->state = PERF_COUNTER_STATE_INACTIVE;
Ingo Molnara86ed502008-12-17 00:43:10 +01002340 if (hw_event->disabled)
2341 counter->state = PERF_COUNTER_STATE_OFF;
2342
Ingo Molnar5c92d122008-12-11 13:21:10 +01002343 hw_ops = NULL;
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002344
Peter Zijlstraf4a2deb42009-03-23 18:22:06 +01002345 if (perf_event_raw(hw_event)) {
Ingo Molnar5c92d122008-12-11 13:21:10 +01002346 hw_ops = hw_perf_counter_init(counter);
Peter Zijlstraf4a2deb42009-03-23 18:22:06 +01002347 goto done;
2348 }
2349
2350 switch (perf_event_type(hw_event)) {
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002351 case PERF_TYPE_HARDWARE:
2352 hw_ops = hw_perf_counter_init(counter);
2353 break;
2354
2355 case PERF_TYPE_SOFTWARE:
2356 hw_ops = sw_perf_counter_init(counter);
2357 break;
2358
2359 case PERF_TYPE_TRACEPOINT:
2360 hw_ops = tp_perf_counter_init(counter);
2361 break;
2362 }
Ingo Molnar5c92d122008-12-11 13:21:10 +01002363
Ingo Molnar621a01e2008-12-11 12:46:46 +01002364 if (!hw_ops) {
2365 kfree(counter);
2366 return NULL;
2367 }
Peter Zijlstraf4a2deb42009-03-23 18:22:06 +01002368done:
Ingo Molnar621a01e2008-12-11 12:46:46 +01002369 counter->hw_ops = hw_ops;
Thomas Gleixner0793a612008-12-04 20:12:29 +01002370
2371 return counter;
2372}
2373
2374/**
Paul Mackerras2743a5b2009-03-04 20:36:51 +11002375 * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
Ingo Molnar9f66a382008-12-10 12:33:23 +01002376 *
2377 * @hw_event_uptr: event type attributes for monitoring/sampling
Thomas Gleixner0793a612008-12-04 20:12:29 +01002378 * @pid: target pid
Ingo Molnar9f66a382008-12-10 12:33:23 +01002379 * @cpu: target cpu
2380 * @group_fd: group leader counter fd
Thomas Gleixner0793a612008-12-04 20:12:29 +01002381 */
Paul Mackerras2743a5b2009-03-04 20:36:51 +11002382SYSCALL_DEFINE5(perf_counter_open,
Paul Mackerrasf3dfd262009-02-26 22:43:46 +11002383 const struct perf_counter_hw_event __user *, hw_event_uptr,
Paul Mackerras2743a5b2009-03-04 20:36:51 +11002384 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01002385{
Ingo Molnar04289bb2008-12-11 08:38:42 +01002386 struct perf_counter *counter, *group_leader;
Ingo Molnar9f66a382008-12-10 12:33:23 +01002387 struct perf_counter_hw_event hw_event;
Ingo Molnar04289bb2008-12-11 08:38:42 +01002388 struct perf_counter_context *ctx;
Ingo Molnar9b51f662008-12-12 13:49:45 +01002389 struct file *counter_file = NULL;
Ingo Molnar04289bb2008-12-11 08:38:42 +01002390 struct file *group_file = NULL;
2391 int fput_needed = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01002392 int fput_needed2 = 0;
Thomas Gleixner0793a612008-12-04 20:12:29 +01002393 int ret;
2394
Paul Mackerras2743a5b2009-03-04 20:36:51 +11002395 /* for future expandability... */
2396 if (flags)
2397 return -EINVAL;
2398
Ingo Molnar9f66a382008-12-10 12:33:23 +01002399 if (copy_from_user(&hw_event, hw_event_uptr, sizeof(hw_event)) != 0)
Thomas Gleixnereab656a2008-12-08 19:26:59 +01002400 return -EFAULT;
2401
Ingo Molnar04289bb2008-12-11 08:38:42 +01002402 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01002403 * Get the target context (task or percpu):
2404 */
2405 ctx = find_get_context(pid, cpu);
2406 if (IS_ERR(ctx))
2407 return PTR_ERR(ctx);
2408
2409 /*
2410 * Look up the group leader (we will attach this counter to it):
Ingo Molnar04289bb2008-12-11 08:38:42 +01002411 */
2412 group_leader = NULL;
2413 if (group_fd != -1) {
2414 ret = -EINVAL;
2415 group_file = fget_light(group_fd, &fput_needed);
2416 if (!group_file)
Ingo Molnarccff2862008-12-11 11:26:29 +01002417 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01002418 if (group_file->f_op != &perf_fops)
Ingo Molnarccff2862008-12-11 11:26:29 +01002419 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01002420
2421 group_leader = group_file->private_data;
2422 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01002423 * Do not allow a recursive hierarchy (this new sibling
2424 * becoming part of another group-sibling):
Ingo Molnar04289bb2008-12-11 08:38:42 +01002425 */
Ingo Molnarccff2862008-12-11 11:26:29 +01002426 if (group_leader->group_leader != group_leader)
2427 goto err_put_context;
2428 /*
2429 * Do not allow to attach to a group in a different
2430 * task or CPU context:
2431 */
2432 if (group_leader->ctx != ctx)
2433 goto err_put_context;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11002434 /*
2435 * Only a group leader can be exclusive or pinned
2436 */
2437 if (hw_event.exclusive || hw_event.pinned)
2438 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01002439 }
2440
Ingo Molnar5c92d122008-12-11 13:21:10 +01002441 ret = -EINVAL;
Paul Mackerras23a185c2009-02-09 22:42:47 +11002442 counter = perf_counter_alloc(&hw_event, cpu, ctx, group_leader,
2443 GFP_KERNEL);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002444 if (!counter)
2445 goto err_put_context;
2446
Thomas Gleixner0793a612008-12-04 20:12:29 +01002447 ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
2448 if (ret < 0)
Ingo Molnar9b51f662008-12-12 13:49:45 +01002449 goto err_free_put_context;
2450
2451 counter_file = fget_light(ret, &fput_needed2);
2452 if (!counter_file)
2453 goto err_free_put_context;
2454
2455 counter->filp = counter_file;
Paul Mackerrasd859e292009-01-17 18:10:22 +11002456 mutex_lock(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01002457 perf_install_in_context(ctx, counter, cpu);
Paul Mackerrasd859e292009-01-17 18:10:22 +11002458 mutex_unlock(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01002459
2460 fput_light(counter_file, fput_needed2);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002461
Ingo Molnar04289bb2008-12-11 08:38:42 +01002462out_fput:
2463 fput_light(group_file, fput_needed);
2464
Thomas Gleixner0793a612008-12-04 20:12:29 +01002465 return ret;
2466
Ingo Molnar9b51f662008-12-12 13:49:45 +01002467err_free_put_context:
Thomas Gleixner0793a612008-12-04 20:12:29 +01002468 kfree(counter);
2469
2470err_put_context:
2471 put_context(ctx);
2472
Ingo Molnar04289bb2008-12-11 08:38:42 +01002473 goto out_fput;
Thomas Gleixner0793a612008-12-04 20:12:29 +01002474}
2475
Ingo Molnar9b51f662008-12-12 13:49:45 +01002476/*
2477 * Initialize the perf_counter context in a task_struct:
2478 */
2479static void
2480__perf_counter_init_context(struct perf_counter_context *ctx,
2481 struct task_struct *task)
2482{
2483 memset(ctx, 0, sizeof(*ctx));
2484 spin_lock_init(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +11002485 mutex_init(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01002486 INIT_LIST_HEAD(&ctx->counter_list);
Peter Zijlstra592903c2009-03-13 12:21:36 +01002487 INIT_LIST_HEAD(&ctx->event_list);
Ingo Molnar9b51f662008-12-12 13:49:45 +01002488 ctx->task = task;
2489}
2490
2491/*
2492 * inherit a counter from parent task to child task:
2493 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11002494static struct perf_counter *
Ingo Molnar9b51f662008-12-12 13:49:45 +01002495inherit_counter(struct perf_counter *parent_counter,
2496 struct task_struct *parent,
2497 struct perf_counter_context *parent_ctx,
2498 struct task_struct *child,
Paul Mackerrasd859e292009-01-17 18:10:22 +11002499 struct perf_counter *group_leader,
Ingo Molnar9b51f662008-12-12 13:49:45 +01002500 struct perf_counter_context *child_ctx)
2501{
2502 struct perf_counter *child_counter;
2503
Paul Mackerrasd859e292009-01-17 18:10:22 +11002504 /*
2505 * Instead of creating recursive hierarchies of counters,
2506 * we link inherited counters back to the original parent,
2507 * which has a filp for sure, which we use as the reference
2508 * count:
2509 */
2510 if (parent_counter->parent)
2511 parent_counter = parent_counter->parent;
2512
Ingo Molnar9b51f662008-12-12 13:49:45 +01002513 child_counter = perf_counter_alloc(&parent_counter->hw_event,
Paul Mackerras23a185c2009-02-09 22:42:47 +11002514 parent_counter->cpu, child_ctx,
2515 group_leader, GFP_KERNEL);
Ingo Molnar9b51f662008-12-12 13:49:45 +01002516 if (!child_counter)
Paul Mackerrasd859e292009-01-17 18:10:22 +11002517 return NULL;
Ingo Molnar9b51f662008-12-12 13:49:45 +01002518
2519 /*
2520 * Link it up in the child's context:
2521 */
Ingo Molnar9b51f662008-12-12 13:49:45 +01002522 child_counter->task = child;
Paul Mackerras53cfbf52009-03-25 22:46:58 +11002523 add_counter_to_ctx(child_counter, child_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01002524
2525 child_counter->parent = parent_counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01002526 /*
2527 * inherit into child's child as well:
2528 */
2529 child_counter->hw_event.inherit = 1;
2530
2531 /*
2532 * Get a reference to the parent filp - we will fput it
2533 * when the child counter exits. This is safe to do because
2534 * we are in the parent and we know that the filp still
2535 * exists and has a nonzero count:
2536 */
2537 atomic_long_inc(&parent_counter->filp->f_count);
2538
Paul Mackerrasd859e292009-01-17 18:10:22 +11002539 /*
2540 * Link this into the parent counter's child list
2541 */
2542 mutex_lock(&parent_counter->mutex);
2543 list_add_tail(&child_counter->child_list, &parent_counter->child_list);
2544
2545 /*
2546 * Make the child state follow the state of the parent counter,
2547 * not its hw_event.disabled bit. We hold the parent's mutex,
2548 * so we won't race with perf_counter_{en,dis}able_family.
2549 */
2550 if (parent_counter->state >= PERF_COUNTER_STATE_INACTIVE)
2551 child_counter->state = PERF_COUNTER_STATE_INACTIVE;
2552 else
2553 child_counter->state = PERF_COUNTER_STATE_OFF;
2554
2555 mutex_unlock(&parent_counter->mutex);
2556
2557 return child_counter;
2558}
2559
2560static int inherit_group(struct perf_counter *parent_counter,
2561 struct task_struct *parent,
2562 struct perf_counter_context *parent_ctx,
2563 struct task_struct *child,
2564 struct perf_counter_context *child_ctx)
2565{
2566 struct perf_counter *leader;
2567 struct perf_counter *sub;
2568
2569 leader = inherit_counter(parent_counter, parent, parent_ctx,
2570 child, NULL, child_ctx);
2571 if (!leader)
2572 return -ENOMEM;
2573 list_for_each_entry(sub, &parent_counter->sibling_list, list_entry) {
2574 if (!inherit_counter(sub, parent, parent_ctx,
2575 child, leader, child_ctx))
2576 return -ENOMEM;
2577 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01002578 return 0;
2579}
2580
Paul Mackerrasd859e292009-01-17 18:10:22 +11002581static void sync_child_counter(struct perf_counter *child_counter,
2582 struct perf_counter *parent_counter)
2583{
2584 u64 parent_val, child_val;
2585
2586 parent_val = atomic64_read(&parent_counter->count);
2587 child_val = atomic64_read(&child_counter->count);
2588
2589 /*
2590 * Add back the child's count to the parent's count:
2591 */
2592 atomic64_add(child_val, &parent_counter->count);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11002593 atomic64_add(child_counter->total_time_enabled,
2594 &parent_counter->child_total_time_enabled);
2595 atomic64_add(child_counter->total_time_running,
2596 &parent_counter->child_total_time_running);
Paul Mackerrasd859e292009-01-17 18:10:22 +11002597
2598 /*
2599 * Remove this counter from the parent's list
2600 */
2601 mutex_lock(&parent_counter->mutex);
2602 list_del_init(&child_counter->child_list);
2603 mutex_unlock(&parent_counter->mutex);
2604
2605 /*
2606 * Release the parent counter, if this was the last
2607 * reference to it.
2608 */
2609 fput(parent_counter->filp);
2610}
2611
Ingo Molnar9b51f662008-12-12 13:49:45 +01002612static void
2613__perf_counter_exit_task(struct task_struct *child,
2614 struct perf_counter *child_counter,
2615 struct perf_counter_context *child_ctx)
2616{
2617 struct perf_counter *parent_counter;
Paul Mackerrasd859e292009-01-17 18:10:22 +11002618 struct perf_counter *sub, *tmp;
Ingo Molnar9b51f662008-12-12 13:49:45 +01002619
2620 /*
Ingo Molnar235c7fc2008-12-21 14:43:25 +01002621 * If we do not self-reap then we have to wait for the
2622 * child task to unschedule (it will happen for sure),
2623 * so that its counter is at its final count. (This
2624 * condition triggers rarely - child tasks usually get
2625 * off their CPU before the parent has a chance to
2626 * get this far into the reaping action)
Ingo Molnar9b51f662008-12-12 13:49:45 +01002627 */
Ingo Molnar235c7fc2008-12-21 14:43:25 +01002628 if (child != current) {
2629 wait_task_inactive(child, 0);
2630 list_del_init(&child_counter->list_entry);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11002631 update_counter_times(child_counter);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01002632 } else {
Ingo Molnar0cc0c022008-12-14 23:20:36 +01002633 struct perf_cpu_context *cpuctx;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01002634 unsigned long flags;
2635 u64 perf_flags;
2636
2637 /*
2638 * Disable and unlink this counter.
2639 *
2640 * Be careful about zapping the list - IRQ/NMI context
2641 * could still be processing it:
2642 */
2643 curr_rq_lock_irq_save(&flags);
2644 perf_flags = hw_perf_save_disable();
Ingo Molnar0cc0c022008-12-14 23:20:36 +01002645
2646 cpuctx = &__get_cpu_var(perf_cpu_context);
2647
Paul Mackerrasd859e292009-01-17 18:10:22 +11002648 group_sched_out(child_counter, cpuctx, child_ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11002649 update_counter_times(child_counter);
Ingo Molnar0cc0c022008-12-14 23:20:36 +01002650
Ingo Molnar235c7fc2008-12-21 14:43:25 +01002651 list_del_init(&child_counter->list_entry);
2652
2653 child_ctx->nr_counters--;
2654
2655 hw_perf_restore(perf_flags);
2656 curr_rq_unlock_irq_restore(&flags);
Ingo Molnar0cc0c022008-12-14 23:20:36 +01002657 }
2658
Ingo Molnar9b51f662008-12-12 13:49:45 +01002659 parent_counter = child_counter->parent;
2660 /*
2661 * It can happen that parent exits first, and has counters
2662 * that are still around due to the child reference. These
2663 * counters need to be zapped - but otherwise linger.
2664 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11002665 if (parent_counter) {
2666 sync_child_counter(child_counter, parent_counter);
2667 list_for_each_entry_safe(sub, tmp, &child_counter->sibling_list,
2668 list_entry) {
Paul Mackerras4bcf3492009-02-11 13:53:19 +01002669 if (sub->parent) {
Paul Mackerrasd859e292009-01-17 18:10:22 +11002670 sync_child_counter(sub, sub->parent);
Peter Zijlstraf1600952009-03-19 20:26:16 +01002671 free_counter(sub);
Paul Mackerras4bcf3492009-02-11 13:53:19 +01002672 }
Paul Mackerrasd859e292009-01-17 18:10:22 +11002673 }
Peter Zijlstraf1600952009-03-19 20:26:16 +01002674 free_counter(child_counter);
Paul Mackerras4bcf3492009-02-11 13:53:19 +01002675 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01002676}
2677
2678/*
Paul Mackerrasd859e292009-01-17 18:10:22 +11002679 * When a child task exits, feed back counter values to parent counters.
Ingo Molnar9b51f662008-12-12 13:49:45 +01002680 *
Paul Mackerrasd859e292009-01-17 18:10:22 +11002681 * Note: we may be running in child context, but the PID is not hashed
Ingo Molnar9b51f662008-12-12 13:49:45 +01002682 * anymore so new counters will not be added.
2683 */
2684void perf_counter_exit_task(struct task_struct *child)
2685{
2686 struct perf_counter *child_counter, *tmp;
2687 struct perf_counter_context *child_ctx;
2688
2689 child_ctx = &child->perf_counter_ctx;
2690
2691 if (likely(!child_ctx->nr_counters))
2692 return;
2693
2694 list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list,
2695 list_entry)
2696 __perf_counter_exit_task(child, child_counter, child_ctx);
2697}
2698
2699/*
2700 * Initialize the perf_counter context in task_struct
2701 */
2702void perf_counter_init_task(struct task_struct *child)
2703{
2704 struct perf_counter_context *child_ctx, *parent_ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +11002705 struct perf_counter *counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01002706 struct task_struct *parent = current;
Ingo Molnar9b51f662008-12-12 13:49:45 +01002707
2708 child_ctx = &child->perf_counter_ctx;
2709 parent_ctx = &parent->perf_counter_ctx;
2710
2711 __perf_counter_init_context(child_ctx, child);
2712
2713 /*
2714 * This is executed from the parent task context, so inherit
2715 * counters that have been marked for cloning:
2716 */
2717
2718 if (likely(!parent_ctx->nr_counters))
2719 return;
2720
2721 /*
2722 * Lock the parent list. No need to lock the child - not PID
2723 * hashed yet and not running, so nobody can access it.
2724 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11002725 mutex_lock(&parent_ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01002726
2727 /*
2728 * We dont have to disable NMIs - we are only looking at
2729 * the list, not manipulating it:
2730 */
2731 list_for_each_entry(counter, &parent_ctx->counter_list, list_entry) {
Paul Mackerrasd859e292009-01-17 18:10:22 +11002732 if (!counter->hw_event.inherit)
Ingo Molnar9b51f662008-12-12 13:49:45 +01002733 continue;
2734
Paul Mackerrasd859e292009-01-17 18:10:22 +11002735 if (inherit_group(counter, parent,
Ingo Molnar9b51f662008-12-12 13:49:45 +01002736 parent_ctx, child, child_ctx))
2737 break;
2738 }
2739
Paul Mackerrasd859e292009-01-17 18:10:22 +11002740 mutex_unlock(&parent_ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01002741}
2742
Ingo Molnar04289bb2008-12-11 08:38:42 +01002743static void __cpuinit perf_counter_init_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01002744{
Ingo Molnar04289bb2008-12-11 08:38:42 +01002745 struct perf_cpu_context *cpuctx;
Thomas Gleixner0793a612008-12-04 20:12:29 +01002746
Ingo Molnar04289bb2008-12-11 08:38:42 +01002747 cpuctx = &per_cpu(perf_cpu_context, cpu);
2748 __perf_counter_init_context(&cpuctx->ctx, NULL);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002749
2750 mutex_lock(&perf_resource_mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01002751 cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu;
Thomas Gleixner0793a612008-12-04 20:12:29 +01002752 mutex_unlock(&perf_resource_mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01002753
Paul Mackerras01d02872009-01-14 13:44:19 +11002754 hw_perf_counter_setup(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002755}
2756
2757#ifdef CONFIG_HOTPLUG_CPU
Ingo Molnar04289bb2008-12-11 08:38:42 +01002758static void __perf_counter_exit_cpu(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01002759{
2760 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
2761 struct perf_counter_context *ctx = &cpuctx->ctx;
2762 struct perf_counter *counter, *tmp;
2763
Ingo Molnar04289bb2008-12-11 08:38:42 +01002764 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry)
2765 __perf_counter_remove_from_context(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002766}
Ingo Molnar04289bb2008-12-11 08:38:42 +01002767static void perf_counter_exit_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01002768{
Paul Mackerrasd859e292009-01-17 18:10:22 +11002769 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
2770 struct perf_counter_context *ctx = &cpuctx->ctx;
2771
2772 mutex_lock(&ctx->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01002773 smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1);
Paul Mackerrasd859e292009-01-17 18:10:22 +11002774 mutex_unlock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002775}
2776#else
Ingo Molnar04289bb2008-12-11 08:38:42 +01002777static inline void perf_counter_exit_cpu(int cpu) { }
Thomas Gleixner0793a612008-12-04 20:12:29 +01002778#endif
2779
2780static int __cpuinit
2781perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
2782{
2783 unsigned int cpu = (long)hcpu;
2784
2785 switch (action) {
2786
2787 case CPU_UP_PREPARE:
2788 case CPU_UP_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01002789 perf_counter_init_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002790 break;
2791
2792 case CPU_DOWN_PREPARE:
2793 case CPU_DOWN_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01002794 perf_counter_exit_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002795 break;
2796
2797 default:
2798 break;
2799 }
2800
2801 return NOTIFY_OK;
2802}
2803
2804static struct notifier_block __cpuinitdata perf_cpu_nb = {
2805 .notifier_call = perf_cpu_notify,
2806};
2807
2808static int __init perf_counter_init(void)
2809{
2810 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
2811 (void *)(long)smp_processor_id());
2812 register_cpu_notifier(&perf_cpu_nb);
2813
2814 return 0;
2815}
2816early_initcall(perf_counter_init);
2817
2818static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
2819{
2820 return sprintf(buf, "%d\n", perf_reserved_percpu);
2821}
2822
2823static ssize_t
2824perf_set_reserve_percpu(struct sysdev_class *class,
2825 const char *buf,
2826 size_t count)
2827{
2828 struct perf_cpu_context *cpuctx;
2829 unsigned long val;
2830 int err, cpu, mpt;
2831
2832 err = strict_strtoul(buf, 10, &val);
2833 if (err)
2834 return err;
2835 if (val > perf_max_counters)
2836 return -EINVAL;
2837
2838 mutex_lock(&perf_resource_mutex);
2839 perf_reserved_percpu = val;
2840 for_each_online_cpu(cpu) {
2841 cpuctx = &per_cpu(perf_cpu_context, cpu);
2842 spin_lock_irq(&cpuctx->ctx.lock);
2843 mpt = min(perf_max_counters - cpuctx->ctx.nr_counters,
2844 perf_max_counters - perf_reserved_percpu);
2845 cpuctx->max_pertask = mpt;
2846 spin_unlock_irq(&cpuctx->ctx.lock);
2847 }
2848 mutex_unlock(&perf_resource_mutex);
2849
2850 return count;
2851}
2852
2853static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
2854{
2855 return sprintf(buf, "%d\n", perf_overcommit);
2856}
2857
2858static ssize_t
2859perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
2860{
2861 unsigned long val;
2862 int err;
2863
2864 err = strict_strtoul(buf, 10, &val);
2865 if (err)
2866 return err;
2867 if (val > 1)
2868 return -EINVAL;
2869
2870 mutex_lock(&perf_resource_mutex);
2871 perf_overcommit = val;
2872 mutex_unlock(&perf_resource_mutex);
2873
2874 return count;
2875}
2876
2877static SYSDEV_CLASS_ATTR(
2878 reserve_percpu,
2879 0644,
2880 perf_show_reserve_percpu,
2881 perf_set_reserve_percpu
2882 );
2883
2884static SYSDEV_CLASS_ATTR(
2885 overcommit,
2886 0644,
2887 perf_show_overcommit,
2888 perf_set_overcommit
2889 );
2890
2891static struct attribute *perfclass_attrs[] = {
2892 &attr_reserve_percpu.attr,
2893 &attr_overcommit.attr,
2894 NULL
2895};
2896
2897static struct attribute_group perfclass_attr_group = {
2898 .attrs = perfclass_attrs,
2899 .name = "perf_counters",
2900};
2901
2902static int __init perf_counter_sysfs_init(void)
2903{
2904 return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
2905 &perfclass_attr_group);
2906}
2907device_initcall(perf_counter_sysfs_init);