blob: 727624db5078bef5fcdcaa3c97e63768f8e29c86 [file] [log] [blame]
Thomas Gleixner0793a612008-12-04 20:12:29 +01001/*
2 * Performance counter core code
3 *
4 * Copyright(C) 2008 Thomas Gleixner <tglx@linutronix.de>
5 * Copyright(C) 2008 Red Hat, Inc., Ingo Molnar
6 *
Peter Zijlstra7b732a72009-03-23 18:22:10 +01007 *
8 * For licensing details see kernel-base/COPYING
Thomas Gleixner0793a612008-12-04 20:12:29 +01009 */
10
11#include <linux/fs.h>
Peter Zijlstrab9cacc72009-03-25 12:30:22 +010012#include <linux/mm.h>
Thomas Gleixner0793a612008-12-04 20:12:29 +010013#include <linux/cpu.h>
14#include <linux/smp.h>
Ingo Molnar04289bb2008-12-11 08:38:42 +010015#include <linux/file.h>
Thomas Gleixner0793a612008-12-04 20:12:29 +010016#include <linux/poll.h>
17#include <linux/sysfs.h>
18#include <linux/ptrace.h>
19#include <linux/percpu.h>
Peter Zijlstrab9cacc72009-03-25 12:30:22 +010020#include <linux/vmstat.h>
21#include <linux/hardirq.h>
22#include <linux/rculist.h>
Thomas Gleixner0793a612008-12-04 20:12:29 +010023#include <linux/uaccess.h>
24#include <linux/syscalls.h>
25#include <linux/anon_inodes.h>
Ingo Molnaraa9c4c02008-12-17 14:10:57 +010026#include <linux/kernel_stat.h>
Thomas Gleixner0793a612008-12-04 20:12:29 +010027#include <linux/perf_counter.h>
Peter Zijlstra0a4a9392009-03-30 19:07:05 +020028#include <linux/dcache.h>
Thomas Gleixner0793a612008-12-04 20:12:29 +010029
Tim Blechmann4e193bd2009-03-14 14:29:25 +010030#include <asm/irq_regs.h>
31
Thomas Gleixner0793a612008-12-04 20:12:29 +010032/*
33 * Each CPU has a list of per CPU counters:
34 */
35DEFINE_PER_CPU(struct perf_cpu_context, perf_cpu_context);
36
Ingo Molnar088e2852008-12-14 20:21:00 +010037int perf_max_counters __read_mostly = 1;
Thomas Gleixner0793a612008-12-04 20:12:29 +010038static int perf_reserved_percpu __read_mostly;
39static int perf_overcommit __read_mostly = 1;
40
41/*
42 * Mutex for (sysadmin-configurable) counter reservations:
43 */
44static DEFINE_MUTEX(perf_resource_mutex);
45
46/*
47 * Architecture provided APIs - weak aliases:
48 */
Ingo Molnar5c92d122008-12-11 13:21:10 +010049extern __weak const struct hw_perf_counter_ops *
Ingo Molnar621a01e2008-12-11 12:46:46 +010050hw_perf_counter_init(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +010051{
Paul Mackerrasff6f0542009-01-09 16:19:25 +110052 return NULL;
Thomas Gleixner0793a612008-12-04 20:12:29 +010053}
54
Ingo Molnar01b28382008-12-11 13:45:51 +010055u64 __weak hw_perf_save_disable(void) { return 0; }
Yinghai Lu01ea1cc2008-12-26 21:05:06 -080056void __weak hw_perf_restore(u64 ctrl) { barrier(); }
Paul Mackerras01d02872009-01-14 13:44:19 +110057void __weak hw_perf_counter_setup(int cpu) { barrier(); }
Paul Mackerras3cbed422009-01-09 16:43:42 +110058int __weak hw_perf_group_sched_in(struct perf_counter *group_leader,
59 struct perf_cpu_context *cpuctx,
60 struct perf_counter_context *ctx, int cpu)
61{
62 return 0;
63}
Thomas Gleixner0793a612008-12-04 20:12:29 +010064
Paul Mackerras4eb96fc2009-01-09 17:24:34 +110065void __weak perf_counter_print_debug(void) { }
66
Ingo Molnar04289bb2008-12-11 08:38:42 +010067static void
68list_add_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
69{
70 struct perf_counter *group_leader = counter->group_leader;
71
72 /*
73 * Depending on whether it is a standalone or sibling counter,
74 * add it straight to the context's counter list, or to the group
75 * leader's sibling list:
76 */
77 if (counter->group_leader == counter)
78 list_add_tail(&counter->list_entry, &ctx->counter_list);
Peter Zijlstra5c148192009-03-25 12:30:23 +010079 else {
Ingo Molnar04289bb2008-12-11 08:38:42 +010080 list_add_tail(&counter->list_entry, &group_leader->sibling_list);
Peter Zijlstra5c148192009-03-25 12:30:23 +010081 group_leader->nr_siblings++;
82 }
Peter Zijlstra592903c2009-03-13 12:21:36 +010083
84 list_add_rcu(&counter->event_entry, &ctx->event_list);
Ingo Molnar04289bb2008-12-11 08:38:42 +010085}
86
87static void
88list_del_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
89{
90 struct perf_counter *sibling, *tmp;
91
92 list_del_init(&counter->list_entry);
Peter Zijlstra592903c2009-03-13 12:21:36 +010093 list_del_rcu(&counter->event_entry);
Ingo Molnar04289bb2008-12-11 08:38:42 +010094
Peter Zijlstra5c148192009-03-25 12:30:23 +010095 if (counter->group_leader != counter)
96 counter->group_leader->nr_siblings--;
97
Ingo Molnar04289bb2008-12-11 08:38:42 +010098 /*
99 * If this was a group counter with sibling counters then
100 * upgrade the siblings to singleton counters by adding them
101 * to the context list directly:
102 */
103 list_for_each_entry_safe(sibling, tmp,
104 &counter->sibling_list, list_entry) {
105
Peter Zijlstra75564232009-03-13 12:21:29 +0100106 list_move_tail(&sibling->list_entry, &ctx->counter_list);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100107 sibling->group_leader = sibling;
108 }
109}
110
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100111static void
112counter_sched_out(struct perf_counter *counter,
113 struct perf_cpu_context *cpuctx,
114 struct perf_counter_context *ctx)
115{
116 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
117 return;
118
119 counter->state = PERF_COUNTER_STATE_INACTIVE;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100120 counter->tstamp_stopped = ctx->time_now;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100121 counter->hw_ops->disable(counter);
122 counter->oncpu = -1;
123
124 if (!is_software_counter(counter))
125 cpuctx->active_oncpu--;
126 ctx->nr_active--;
127 if (counter->hw_event.exclusive || !cpuctx->active_oncpu)
128 cpuctx->exclusive = 0;
129}
130
Paul Mackerrasd859e292009-01-17 18:10:22 +1100131static void
132group_sched_out(struct perf_counter *group_counter,
133 struct perf_cpu_context *cpuctx,
134 struct perf_counter_context *ctx)
135{
136 struct perf_counter *counter;
137
138 if (group_counter->state != PERF_COUNTER_STATE_ACTIVE)
139 return;
140
141 counter_sched_out(group_counter, cpuctx, ctx);
142
143 /*
144 * Schedule out siblings (if any):
145 */
146 list_for_each_entry(counter, &group_counter->sibling_list, list_entry)
147 counter_sched_out(counter, cpuctx, ctx);
148
149 if (group_counter->hw_event.exclusive)
150 cpuctx->exclusive = 0;
151}
152
Thomas Gleixner0793a612008-12-04 20:12:29 +0100153/*
154 * Cross CPU call to remove a performance counter
155 *
156 * We disable the counter on the hardware level first. After that we
157 * remove it from the context list.
158 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100159static void __perf_counter_remove_from_context(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100160{
161 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
162 struct perf_counter *counter = info;
163 struct perf_counter_context *ctx = counter->ctx;
Ingo Molnar9b51f662008-12-12 13:49:45 +0100164 unsigned long flags;
Ingo Molnar5c92d122008-12-11 13:21:10 +0100165 u64 perf_flags;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100166
167 /*
168 * If this is a task context, we need to check whether it is
169 * the current task context of this cpu. If not it has been
170 * scheduled out before the smp call arrived.
171 */
172 if (ctx->task && cpuctx->task_ctx != ctx)
173 return;
174
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100175 curr_rq_lock_irq_save(&flags);
176 spin_lock(&ctx->lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100177
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100178 counter_sched_out(counter, cpuctx, ctx);
179
180 counter->task = NULL;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100181 ctx->nr_counters--;
182
183 /*
184 * Protect the list operation against NMI by disabling the
185 * counters on a global level. NOP for non NMI based counters.
186 */
Ingo Molnar01b28382008-12-11 13:45:51 +0100187 perf_flags = hw_perf_save_disable();
Ingo Molnar04289bb2008-12-11 08:38:42 +0100188 list_del_counter(counter, ctx);
Ingo Molnar01b28382008-12-11 13:45:51 +0100189 hw_perf_restore(perf_flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100190
191 if (!ctx->task) {
192 /*
193 * Allow more per task counters with respect to the
194 * reservation:
195 */
196 cpuctx->max_pertask =
197 min(perf_max_counters - ctx->nr_counters,
198 perf_max_counters - perf_reserved_percpu);
199 }
200
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100201 spin_unlock(&ctx->lock);
202 curr_rq_unlock_irq_restore(&flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100203}
204
205
206/*
207 * Remove the counter from a task's (or a CPU's) list of counters.
208 *
Paul Mackerrasd859e292009-01-17 18:10:22 +1100209 * Must be called with counter->mutex and ctx->mutex held.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100210 *
211 * CPU counters are removed with a smp call. For task counters we only
212 * call when the task is on a CPU.
213 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100214static void perf_counter_remove_from_context(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100215{
216 struct perf_counter_context *ctx = counter->ctx;
217 struct task_struct *task = ctx->task;
218
219 if (!task) {
220 /*
221 * Per cpu counters are removed via an smp call and
222 * the removal is always sucessful.
223 */
224 smp_call_function_single(counter->cpu,
Ingo Molnar04289bb2008-12-11 08:38:42 +0100225 __perf_counter_remove_from_context,
Thomas Gleixner0793a612008-12-04 20:12:29 +0100226 counter, 1);
227 return;
228 }
229
230retry:
Ingo Molnar04289bb2008-12-11 08:38:42 +0100231 task_oncpu_function_call(task, __perf_counter_remove_from_context,
Thomas Gleixner0793a612008-12-04 20:12:29 +0100232 counter);
233
234 spin_lock_irq(&ctx->lock);
235 /*
236 * If the context is active we need to retry the smp call.
237 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100238 if (ctx->nr_active && !list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100239 spin_unlock_irq(&ctx->lock);
240 goto retry;
241 }
242
243 /*
244 * The lock prevents that this context is scheduled in so we
Ingo Molnar04289bb2008-12-11 08:38:42 +0100245 * can remove the counter safely, if the call above did not
Thomas Gleixner0793a612008-12-04 20:12:29 +0100246 * succeed.
247 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100248 if (!list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100249 ctx->nr_counters--;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100250 list_del_counter(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100251 counter->task = NULL;
252 }
253 spin_unlock_irq(&ctx->lock);
254}
255
Paul Mackerrasd859e292009-01-17 18:10:22 +1100256/*
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100257 * Get the current time for this context.
258 * If this is a task context, we use the task's task clock,
259 * or for a per-cpu context, we use the cpu clock.
260 */
261static u64 get_context_time(struct perf_counter_context *ctx, int update)
262{
263 struct task_struct *curr = ctx->task;
264
265 if (!curr)
266 return cpu_clock(smp_processor_id());
267
268 return __task_delta_exec(curr, update) + curr->se.sum_exec_runtime;
269}
270
271/*
272 * Update the record of the current time in a context.
273 */
274static void update_context_time(struct perf_counter_context *ctx, int update)
275{
276 ctx->time_now = get_context_time(ctx, update) - ctx->time_lost;
277}
278
279/*
280 * Update the total_time_enabled and total_time_running fields for a counter.
281 */
282static void update_counter_times(struct perf_counter *counter)
283{
284 struct perf_counter_context *ctx = counter->ctx;
285 u64 run_end;
286
287 if (counter->state >= PERF_COUNTER_STATE_INACTIVE) {
288 counter->total_time_enabled = ctx->time_now -
289 counter->tstamp_enabled;
290 if (counter->state == PERF_COUNTER_STATE_INACTIVE)
291 run_end = counter->tstamp_stopped;
292 else
293 run_end = ctx->time_now;
294 counter->total_time_running = run_end - counter->tstamp_running;
295 }
296}
297
298/*
299 * Update total_time_enabled and total_time_running for all counters in a group.
300 */
301static void update_group_times(struct perf_counter *leader)
302{
303 struct perf_counter *counter;
304
305 update_counter_times(leader);
306 list_for_each_entry(counter, &leader->sibling_list, list_entry)
307 update_counter_times(counter);
308}
309
310/*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100311 * Cross CPU call to disable a performance counter
312 */
313static void __perf_counter_disable(void *info)
314{
315 struct perf_counter *counter = info;
316 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
317 struct perf_counter_context *ctx = counter->ctx;
318 unsigned long flags;
319
320 /*
321 * If this is a per-task counter, need to check whether this
322 * counter's task is the current task on this cpu.
323 */
324 if (ctx->task && cpuctx->task_ctx != ctx)
325 return;
326
327 curr_rq_lock_irq_save(&flags);
328 spin_lock(&ctx->lock);
329
330 /*
331 * If the counter is on, turn it off.
332 * If it is in error state, leave it in error state.
333 */
334 if (counter->state >= PERF_COUNTER_STATE_INACTIVE) {
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100335 update_context_time(ctx, 1);
336 update_counter_times(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100337 if (counter == counter->group_leader)
338 group_sched_out(counter, cpuctx, ctx);
339 else
340 counter_sched_out(counter, cpuctx, ctx);
341 counter->state = PERF_COUNTER_STATE_OFF;
342 }
343
344 spin_unlock(&ctx->lock);
345 curr_rq_unlock_irq_restore(&flags);
346}
347
348/*
349 * Disable a counter.
350 */
351static void perf_counter_disable(struct perf_counter *counter)
352{
353 struct perf_counter_context *ctx = counter->ctx;
354 struct task_struct *task = ctx->task;
355
356 if (!task) {
357 /*
358 * Disable the counter on the cpu that it's on
359 */
360 smp_call_function_single(counter->cpu, __perf_counter_disable,
361 counter, 1);
362 return;
363 }
364
365 retry:
366 task_oncpu_function_call(task, __perf_counter_disable, counter);
367
368 spin_lock_irq(&ctx->lock);
369 /*
370 * If the counter is still active, we need to retry the cross-call.
371 */
372 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
373 spin_unlock_irq(&ctx->lock);
374 goto retry;
375 }
376
377 /*
378 * Since we have the lock this context can't be scheduled
379 * in, so we can change the state safely.
380 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100381 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
382 update_counter_times(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100383 counter->state = PERF_COUNTER_STATE_OFF;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100384 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100385
386 spin_unlock_irq(&ctx->lock);
387}
388
389/*
390 * Disable a counter and all its children.
391 */
392static void perf_counter_disable_family(struct perf_counter *counter)
393{
394 struct perf_counter *child;
395
396 perf_counter_disable(counter);
397
398 /*
399 * Lock the mutex to protect the list of children
400 */
401 mutex_lock(&counter->mutex);
402 list_for_each_entry(child, &counter->child_list, child_list)
403 perf_counter_disable(child);
404 mutex_unlock(&counter->mutex);
405}
406
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100407static int
408counter_sched_in(struct perf_counter *counter,
409 struct perf_cpu_context *cpuctx,
410 struct perf_counter_context *ctx,
411 int cpu)
412{
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100413 if (counter->state <= PERF_COUNTER_STATE_OFF)
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100414 return 0;
415
416 counter->state = PERF_COUNTER_STATE_ACTIVE;
417 counter->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */
418 /*
419 * The new state must be visible before we turn it on in the hardware:
420 */
421 smp_wmb();
422
423 if (counter->hw_ops->enable(counter)) {
424 counter->state = PERF_COUNTER_STATE_INACTIVE;
425 counter->oncpu = -1;
426 return -EAGAIN;
427 }
428
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100429 counter->tstamp_running += ctx->time_now - counter->tstamp_stopped;
430
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100431 if (!is_software_counter(counter))
432 cpuctx->active_oncpu++;
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100433 ctx->nr_active++;
434
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100435 if (counter->hw_event.exclusive)
436 cpuctx->exclusive = 1;
437
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100438 return 0;
439}
440
Thomas Gleixner0793a612008-12-04 20:12:29 +0100441/*
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100442 * Return 1 for a group consisting entirely of software counters,
443 * 0 if the group contains any hardware counters.
444 */
445static int is_software_only_group(struct perf_counter *leader)
446{
447 struct perf_counter *counter;
448
449 if (!is_software_counter(leader))
450 return 0;
Peter Zijlstra5c148192009-03-25 12:30:23 +0100451
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100452 list_for_each_entry(counter, &leader->sibling_list, list_entry)
453 if (!is_software_counter(counter))
454 return 0;
Peter Zijlstra5c148192009-03-25 12:30:23 +0100455
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100456 return 1;
457}
458
459/*
460 * Work out whether we can put this counter group on the CPU now.
461 */
462static int group_can_go_on(struct perf_counter *counter,
463 struct perf_cpu_context *cpuctx,
464 int can_add_hw)
465{
466 /*
467 * Groups consisting entirely of software counters can always go on.
468 */
469 if (is_software_only_group(counter))
470 return 1;
471 /*
472 * If an exclusive group is already on, no other hardware
473 * counters can go on.
474 */
475 if (cpuctx->exclusive)
476 return 0;
477 /*
478 * If this group is exclusive and there are already
479 * counters on the CPU, it can't go on.
480 */
481 if (counter->hw_event.exclusive && cpuctx->active_oncpu)
482 return 0;
483 /*
484 * Otherwise, try to add it if all previous groups were able
485 * to go on.
486 */
487 return can_add_hw;
488}
489
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100490static void add_counter_to_ctx(struct perf_counter *counter,
491 struct perf_counter_context *ctx)
492{
493 list_add_counter(counter, ctx);
494 ctx->nr_counters++;
495 counter->prev_state = PERF_COUNTER_STATE_OFF;
496 counter->tstamp_enabled = ctx->time_now;
497 counter->tstamp_running = ctx->time_now;
498 counter->tstamp_stopped = ctx->time_now;
499}
500
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100501/*
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100502 * Cross CPU call to install and enable a performance counter
Thomas Gleixner0793a612008-12-04 20:12:29 +0100503 */
504static void __perf_install_in_context(void *info)
505{
506 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
507 struct perf_counter *counter = info;
508 struct perf_counter_context *ctx = counter->ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100509 struct perf_counter *leader = counter->group_leader;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100510 int cpu = smp_processor_id();
Ingo Molnar9b51f662008-12-12 13:49:45 +0100511 unsigned long flags;
Ingo Molnar5c92d122008-12-11 13:21:10 +0100512 u64 perf_flags;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100513 int err;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100514
515 /*
516 * If this is a task context, we need to check whether it is
517 * the current task context of this cpu. If not it has been
518 * scheduled out before the smp call arrived.
519 */
520 if (ctx->task && cpuctx->task_ctx != ctx)
521 return;
522
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100523 curr_rq_lock_irq_save(&flags);
524 spin_lock(&ctx->lock);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100525 update_context_time(ctx, 1);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100526
527 /*
528 * Protect the list operation against NMI by disabling the
529 * counters on a global level. NOP for non NMI based counters.
530 */
Ingo Molnar01b28382008-12-11 13:45:51 +0100531 perf_flags = hw_perf_save_disable();
Thomas Gleixner0793a612008-12-04 20:12:29 +0100532
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100533 add_counter_to_ctx(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100534
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100535 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100536 * Don't put the counter on if it is disabled or if
537 * it is in a group and the group isn't on.
538 */
539 if (counter->state != PERF_COUNTER_STATE_INACTIVE ||
540 (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE))
541 goto unlock;
542
543 /*
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100544 * An exclusive counter can't go on if there are already active
545 * hardware counters, and no hardware counter can go on if there
546 * is already an exclusive counter on.
547 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100548 if (!group_can_go_on(counter, cpuctx, 1))
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100549 err = -EEXIST;
550 else
551 err = counter_sched_in(counter, cpuctx, ctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100552
Paul Mackerrasd859e292009-01-17 18:10:22 +1100553 if (err) {
554 /*
555 * This counter couldn't go on. If it is in a group
556 * then we have to pull the whole group off.
557 * If the counter group is pinned then put it in error state.
558 */
559 if (leader != counter)
560 group_sched_out(leader, cpuctx, ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100561 if (leader->hw_event.pinned) {
562 update_group_times(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100563 leader->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100564 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100565 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100566
567 if (!err && !ctx->task && cpuctx->max_pertask)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100568 cpuctx->max_pertask--;
569
Paul Mackerrasd859e292009-01-17 18:10:22 +1100570 unlock:
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100571 hw_perf_restore(perf_flags);
572
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100573 spin_unlock(&ctx->lock);
574 curr_rq_unlock_irq_restore(&flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100575}
576
577/*
578 * Attach a performance counter to a context
579 *
580 * First we add the counter to the list with the hardware enable bit
581 * in counter->hw_config cleared.
582 *
583 * If the counter is attached to a task which is on a CPU we use a smp
584 * call to enable it in the task context. The task might have been
585 * scheduled away, but we check this in the smp call again.
Paul Mackerrasd859e292009-01-17 18:10:22 +1100586 *
587 * Must be called with ctx->mutex held.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100588 */
589static void
590perf_install_in_context(struct perf_counter_context *ctx,
591 struct perf_counter *counter,
592 int cpu)
593{
594 struct task_struct *task = ctx->task;
595
Thomas Gleixner0793a612008-12-04 20:12:29 +0100596 if (!task) {
597 /*
598 * Per cpu counters are installed via an smp call and
599 * the install is always sucessful.
600 */
601 smp_call_function_single(cpu, __perf_install_in_context,
602 counter, 1);
603 return;
604 }
605
606 counter->task = task;
607retry:
608 task_oncpu_function_call(task, __perf_install_in_context,
609 counter);
610
611 spin_lock_irq(&ctx->lock);
612 /*
Thomas Gleixner0793a612008-12-04 20:12:29 +0100613 * we need to retry the smp call.
614 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100615 if (ctx->is_active && list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100616 spin_unlock_irq(&ctx->lock);
617 goto retry;
618 }
619
620 /*
621 * The lock prevents that this context is scheduled in so we
622 * can add the counter safely, if it the call above did not
623 * succeed.
624 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100625 if (list_empty(&counter->list_entry))
626 add_counter_to_ctx(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100627 spin_unlock_irq(&ctx->lock);
628}
629
Paul Mackerrasd859e292009-01-17 18:10:22 +1100630/*
631 * Cross CPU call to enable a performance counter
632 */
633static void __perf_counter_enable(void *info)
Ingo Molnar04289bb2008-12-11 08:38:42 +0100634{
Paul Mackerrasd859e292009-01-17 18:10:22 +1100635 struct perf_counter *counter = info;
636 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
637 struct perf_counter_context *ctx = counter->ctx;
638 struct perf_counter *leader = counter->group_leader;
639 unsigned long flags;
640 int err;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100641
642 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100643 * If this is a per-task counter, need to check whether this
644 * counter's task is the current task on this cpu.
Ingo Molnar04289bb2008-12-11 08:38:42 +0100645 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100646 if (ctx->task && cpuctx->task_ctx != ctx)
647 return;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100648
Paul Mackerrasd859e292009-01-17 18:10:22 +1100649 curr_rq_lock_irq_save(&flags);
650 spin_lock(&ctx->lock);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100651 update_context_time(ctx, 1);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100652
Paul Mackerrasc07c99b2009-02-13 22:10:34 +1100653 counter->prev_state = counter->state;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100654 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
655 goto unlock;
656 counter->state = PERF_COUNTER_STATE_INACTIVE;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100657 counter->tstamp_enabled = ctx->time_now - counter->total_time_enabled;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100658
659 /*
660 * If the counter is in a group and isn't the group leader,
661 * then don't put it on unless the group is on.
662 */
663 if (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE)
664 goto unlock;
665
666 if (!group_can_go_on(counter, cpuctx, 1))
667 err = -EEXIST;
668 else
669 err = counter_sched_in(counter, cpuctx, ctx,
670 smp_processor_id());
671
672 if (err) {
673 /*
674 * If this counter can't go on and it's part of a
675 * group, then the whole group has to come off.
676 */
677 if (leader != counter)
678 group_sched_out(leader, cpuctx, ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100679 if (leader->hw_event.pinned) {
680 update_group_times(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100681 leader->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100682 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100683 }
684
685 unlock:
686 spin_unlock(&ctx->lock);
687 curr_rq_unlock_irq_restore(&flags);
688}
689
690/*
691 * Enable a counter.
692 */
693static void perf_counter_enable(struct perf_counter *counter)
694{
695 struct perf_counter_context *ctx = counter->ctx;
696 struct task_struct *task = ctx->task;
697
698 if (!task) {
699 /*
700 * Enable the counter on the cpu that it's on
701 */
702 smp_call_function_single(counter->cpu, __perf_counter_enable,
703 counter, 1);
704 return;
705 }
706
707 spin_lock_irq(&ctx->lock);
708 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
709 goto out;
710
711 /*
712 * If the counter is in error state, clear that first.
713 * That way, if we see the counter in error state below, we
714 * know that it has gone back into error state, as distinct
715 * from the task having been scheduled away before the
716 * cross-call arrived.
717 */
718 if (counter->state == PERF_COUNTER_STATE_ERROR)
719 counter->state = PERF_COUNTER_STATE_OFF;
720
721 retry:
722 spin_unlock_irq(&ctx->lock);
723 task_oncpu_function_call(task, __perf_counter_enable, counter);
724
725 spin_lock_irq(&ctx->lock);
726
727 /*
728 * If the context is active and the counter is still off,
729 * we need to retry the cross-call.
730 */
731 if (ctx->is_active && counter->state == PERF_COUNTER_STATE_OFF)
732 goto retry;
733
734 /*
735 * Since we have the lock this context can't be scheduled
736 * in, so we can change the state safely.
737 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100738 if (counter->state == PERF_COUNTER_STATE_OFF) {
Paul Mackerrasd859e292009-01-17 18:10:22 +1100739 counter->state = PERF_COUNTER_STATE_INACTIVE;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100740 counter->tstamp_enabled = ctx->time_now -
741 counter->total_time_enabled;
742 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100743 out:
744 spin_unlock_irq(&ctx->lock);
745}
746
747/*
748 * Enable a counter and all its children.
749 */
750static void perf_counter_enable_family(struct perf_counter *counter)
751{
752 struct perf_counter *child;
753
754 perf_counter_enable(counter);
755
756 /*
757 * Lock the mutex to protect the list of children
758 */
759 mutex_lock(&counter->mutex);
760 list_for_each_entry(child, &counter->child_list, child_list)
761 perf_counter_enable(child);
762 mutex_unlock(&counter->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100763}
764
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100765void __perf_counter_sched_out(struct perf_counter_context *ctx,
766 struct perf_cpu_context *cpuctx)
767{
768 struct perf_counter *counter;
Paul Mackerras3cbed422009-01-09 16:43:42 +1100769 u64 flags;
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100770
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100771 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100772 ctx->is_active = 0;
773 if (likely(!ctx->nr_counters))
774 goto out;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100775 update_context_time(ctx, 0);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100776
Paul Mackerras3cbed422009-01-09 16:43:42 +1100777 flags = hw_perf_save_disable();
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100778 if (ctx->nr_active) {
779 list_for_each_entry(counter, &ctx->counter_list, list_entry)
780 group_sched_out(counter, cpuctx, ctx);
781 }
Paul Mackerras3cbed422009-01-09 16:43:42 +1100782 hw_perf_restore(flags);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100783 out:
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100784 spin_unlock(&ctx->lock);
785}
786
Thomas Gleixner0793a612008-12-04 20:12:29 +0100787/*
788 * Called from scheduler to remove the counters of the current task,
789 * with interrupts disabled.
790 *
791 * We stop each counter and update the counter value in counter->count.
792 *
Ingo Molnar76715812008-12-17 14:20:28 +0100793 * This does not protect us against NMI, but disable()
Thomas Gleixner0793a612008-12-04 20:12:29 +0100794 * sets the disabled bit in the control field of counter _before_
795 * accessing the counter control register. If a NMI hits, then it will
796 * not restart the counter.
797 */
798void perf_counter_task_sched_out(struct task_struct *task, int cpu)
799{
800 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
801 struct perf_counter_context *ctx = &task->perf_counter_ctx;
Peter Zijlstra4a0deca2009-03-19 20:26:12 +0100802 struct pt_regs *regs;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100803
804 if (likely(!cpuctx->task_ctx))
805 return;
806
Peter Zijlstra4a0deca2009-03-19 20:26:12 +0100807 regs = task_pt_regs(task);
808 perf_swcounter_event(PERF_COUNT_CONTEXT_SWITCHES, 1, 1, regs);
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100809 __perf_counter_sched_out(ctx, cpuctx);
810
Thomas Gleixner0793a612008-12-04 20:12:29 +0100811 cpuctx->task_ctx = NULL;
812}
813
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100814static void perf_counter_cpu_sched_out(struct perf_cpu_context *cpuctx)
Ingo Molnar04289bb2008-12-11 08:38:42 +0100815{
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100816 __perf_counter_sched_out(&cpuctx->ctx, cpuctx);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100817}
818
Ingo Molnar79958882008-12-17 08:54:56 +0100819static int
Ingo Molnar04289bb2008-12-11 08:38:42 +0100820group_sched_in(struct perf_counter *group_counter,
821 struct perf_cpu_context *cpuctx,
822 struct perf_counter_context *ctx,
823 int cpu)
824{
Ingo Molnar95cdd2e2008-12-21 13:50:42 +0100825 struct perf_counter *counter, *partial_group;
Paul Mackerras3cbed422009-01-09 16:43:42 +1100826 int ret;
827
828 if (group_counter->state == PERF_COUNTER_STATE_OFF)
829 return 0;
830
831 ret = hw_perf_group_sched_in(group_counter, cpuctx, ctx, cpu);
832 if (ret)
833 return ret < 0 ? ret : 0;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100834
Paul Mackerrasc07c99b2009-02-13 22:10:34 +1100835 group_counter->prev_state = group_counter->state;
Ingo Molnar95cdd2e2008-12-21 13:50:42 +0100836 if (counter_sched_in(group_counter, cpuctx, ctx, cpu))
837 return -EAGAIN;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100838
839 /*
840 * Schedule in siblings as one group (if any):
841 */
Ingo Molnar79958882008-12-17 08:54:56 +0100842 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
Paul Mackerrasc07c99b2009-02-13 22:10:34 +1100843 counter->prev_state = counter->state;
Ingo Molnar95cdd2e2008-12-21 13:50:42 +0100844 if (counter_sched_in(counter, cpuctx, ctx, cpu)) {
845 partial_group = counter;
846 goto group_error;
847 }
Ingo Molnar79958882008-12-17 08:54:56 +0100848 }
849
Paul Mackerras3cbed422009-01-09 16:43:42 +1100850 return 0;
Ingo Molnar95cdd2e2008-12-21 13:50:42 +0100851
852group_error:
853 /*
854 * Groups can be scheduled in as one unit only, so undo any
855 * partial group before returning:
856 */
857 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
858 if (counter == partial_group)
859 break;
860 counter_sched_out(counter, cpuctx, ctx);
861 }
862 counter_sched_out(group_counter, cpuctx, ctx);
863
864 return -EAGAIN;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100865}
866
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100867static void
868__perf_counter_sched_in(struct perf_counter_context *ctx,
869 struct perf_cpu_context *cpuctx, int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100870{
Thomas Gleixner0793a612008-12-04 20:12:29 +0100871 struct perf_counter *counter;
Paul Mackerras3cbed422009-01-09 16:43:42 +1100872 u64 flags;
Paul Mackerrasdd0e6ba2009-01-12 15:11:00 +1100873 int can_add_hw = 1;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100874
Thomas Gleixner0793a612008-12-04 20:12:29 +0100875 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100876 ctx->is_active = 1;
877 if (likely(!ctx->nr_counters))
878 goto out;
879
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100880 /*
881 * Add any time since the last sched_out to the lost time
882 * so it doesn't get included in the total_time_enabled and
883 * total_time_running measures for counters in the context.
884 */
885 ctx->time_lost = get_context_time(ctx, 0) - ctx->time_now;
886
Paul Mackerras3cbed422009-01-09 16:43:42 +1100887 flags = hw_perf_save_disable();
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100888
889 /*
890 * First go through the list and put on any pinned groups
891 * in order to give them the best chance of going on.
892 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100893 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100894 if (counter->state <= PERF_COUNTER_STATE_OFF ||
895 !counter->hw_event.pinned)
896 continue;
897 if (counter->cpu != -1 && counter->cpu != cpu)
898 continue;
899
900 if (group_can_go_on(counter, cpuctx, 1))
901 group_sched_in(counter, cpuctx, ctx, cpu);
902
903 /*
904 * If this pinned group hasn't been scheduled,
905 * put it in error state.
906 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100907 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
908 update_group_times(counter);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100909 counter->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100910 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100911 }
912
913 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
914 /*
915 * Ignore counters in OFF or ERROR state, and
916 * ignore pinned counters since we did them already.
917 */
918 if (counter->state <= PERF_COUNTER_STATE_OFF ||
919 counter->hw_event.pinned)
920 continue;
921
Ingo Molnar04289bb2008-12-11 08:38:42 +0100922 /*
923 * Listen to the 'cpu' scheduling filter constraint
924 * of counters:
925 */
Thomas Gleixner0793a612008-12-04 20:12:29 +0100926 if (counter->cpu != -1 && counter->cpu != cpu)
927 continue;
928
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100929 if (group_can_go_on(counter, cpuctx, can_add_hw)) {
Paul Mackerrasdd0e6ba2009-01-12 15:11:00 +1100930 if (group_sched_in(counter, cpuctx, ctx, cpu))
931 can_add_hw = 0;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100932 }
Thomas Gleixner0793a612008-12-04 20:12:29 +0100933 }
Paul Mackerras3cbed422009-01-09 16:43:42 +1100934 hw_perf_restore(flags);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100935 out:
Thomas Gleixner0793a612008-12-04 20:12:29 +0100936 spin_unlock(&ctx->lock);
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100937}
Ingo Molnar04289bb2008-12-11 08:38:42 +0100938
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100939/*
940 * Called from scheduler to add the counters of the current task
941 * with interrupts disabled.
942 *
943 * We restore the counter value and then enable it.
944 *
945 * This does not protect us against NMI, but enable()
946 * sets the enabled bit in the control field of counter _before_
947 * accessing the counter control register. If a NMI hits, then it will
948 * keep the counter running.
949 */
950void perf_counter_task_sched_in(struct task_struct *task, int cpu)
951{
952 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
953 struct perf_counter_context *ctx = &task->perf_counter_ctx;
954
955 __perf_counter_sched_in(ctx, cpuctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100956 cpuctx->task_ctx = ctx;
957}
958
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100959static void perf_counter_cpu_sched_in(struct perf_cpu_context *cpuctx, int cpu)
960{
961 struct perf_counter_context *ctx = &cpuctx->ctx;
962
963 __perf_counter_sched_in(ctx, cpuctx, cpu);
964}
965
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100966int perf_counter_task_disable(void)
967{
968 struct task_struct *curr = current;
969 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
970 struct perf_counter *counter;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100971 unsigned long flags;
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100972 u64 perf_flags;
973 int cpu;
974
975 if (likely(!ctx->nr_counters))
976 return 0;
977
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100978 curr_rq_lock_irq_save(&flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100979 cpu = smp_processor_id();
980
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100981 /* force the update of the task clock: */
982 __task_delta_exec(curr, 1);
983
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100984 perf_counter_task_sched_out(curr, cpu);
985
986 spin_lock(&ctx->lock);
987
988 /*
989 * Disable all the counters:
990 */
991 perf_flags = hw_perf_save_disable();
992
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100993 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100994 if (counter->state != PERF_COUNTER_STATE_ERROR) {
995 update_group_times(counter);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100996 counter->state = PERF_COUNTER_STATE_OFF;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100997 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100998 }
Ingo Molnar9b51f662008-12-12 13:49:45 +0100999
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001000 hw_perf_restore(perf_flags);
1001
1002 spin_unlock(&ctx->lock);
1003
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001004 curr_rq_unlock_irq_restore(&flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001005
1006 return 0;
1007}
1008
1009int perf_counter_task_enable(void)
1010{
1011 struct task_struct *curr = current;
1012 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
1013 struct perf_counter *counter;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001014 unsigned long flags;
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001015 u64 perf_flags;
1016 int cpu;
1017
1018 if (likely(!ctx->nr_counters))
1019 return 0;
1020
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001021 curr_rq_lock_irq_save(&flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001022 cpu = smp_processor_id();
1023
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001024 /* force the update of the task clock: */
1025 __task_delta_exec(curr, 1);
1026
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001027 perf_counter_task_sched_out(curr, cpu);
1028
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001029 spin_lock(&ctx->lock);
1030
1031 /*
1032 * Disable all the counters:
1033 */
1034 perf_flags = hw_perf_save_disable();
1035
1036 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001037 if (counter->state > PERF_COUNTER_STATE_OFF)
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001038 continue;
Ingo Molnar6a930702008-12-11 15:17:03 +01001039 counter->state = PERF_COUNTER_STATE_INACTIVE;
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001040 counter->tstamp_enabled = ctx->time_now -
1041 counter->total_time_enabled;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001042 counter->hw_event.disabled = 0;
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001043 }
1044 hw_perf_restore(perf_flags);
1045
1046 spin_unlock(&ctx->lock);
1047
1048 perf_counter_task_sched_in(curr, cpu);
1049
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001050 curr_rq_unlock_irq_restore(&flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001051
1052 return 0;
1053}
1054
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001055/*
1056 * Round-robin a context's counters:
1057 */
1058static void rotate_ctx(struct perf_counter_context *ctx)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001059{
Thomas Gleixner0793a612008-12-04 20:12:29 +01001060 struct perf_counter *counter;
Ingo Molnar5c92d122008-12-11 13:21:10 +01001061 u64 perf_flags;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001062
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001063 if (!ctx->nr_counters)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001064 return;
1065
Thomas Gleixner0793a612008-12-04 20:12:29 +01001066 spin_lock(&ctx->lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001067 /*
Ingo Molnar04289bb2008-12-11 08:38:42 +01001068 * Rotate the first entry last (works just fine for group counters too):
Thomas Gleixner0793a612008-12-04 20:12:29 +01001069 */
Ingo Molnar01b28382008-12-11 13:45:51 +01001070 perf_flags = hw_perf_save_disable();
Ingo Molnar04289bb2008-12-11 08:38:42 +01001071 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Peter Zijlstra75564232009-03-13 12:21:29 +01001072 list_move_tail(&counter->list_entry, &ctx->counter_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001073 break;
1074 }
Ingo Molnar01b28382008-12-11 13:45:51 +01001075 hw_perf_restore(perf_flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001076
1077 spin_unlock(&ctx->lock);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001078}
Thomas Gleixner0793a612008-12-04 20:12:29 +01001079
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001080void perf_counter_task_tick(struct task_struct *curr, int cpu)
1081{
1082 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
1083 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
1084 const int rotate_percpu = 0;
1085
1086 if (rotate_percpu)
1087 perf_counter_cpu_sched_out(cpuctx);
1088 perf_counter_task_sched_out(curr, cpu);
1089
1090 if (rotate_percpu)
1091 rotate_ctx(&cpuctx->ctx);
1092 rotate_ctx(ctx);
1093
1094 if (rotate_percpu)
1095 perf_counter_cpu_sched_in(cpuctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001096 perf_counter_task_sched_in(curr, cpu);
1097}
1098
1099/*
Thomas Gleixner0793a612008-12-04 20:12:29 +01001100 * Cross CPU call to read the hardware counter
1101 */
Ingo Molnar76715812008-12-17 14:20:28 +01001102static void __read(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001103{
Ingo Molnar621a01e2008-12-11 12:46:46 +01001104 struct perf_counter *counter = info;
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001105 struct perf_counter_context *ctx = counter->ctx;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001106 unsigned long flags;
Ingo Molnar621a01e2008-12-11 12:46:46 +01001107
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001108 curr_rq_lock_irq_save(&flags);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001109 if (ctx->is_active)
1110 update_context_time(ctx, 1);
Ingo Molnar76715812008-12-17 14:20:28 +01001111 counter->hw_ops->read(counter);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001112 update_counter_times(counter);
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001113 curr_rq_unlock_irq_restore(&flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001114}
1115
Ingo Molnar04289bb2008-12-11 08:38:42 +01001116static u64 perf_counter_read(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001117{
1118 /*
1119 * If counter is enabled and currently active on a CPU, update the
1120 * value in the counter structure:
1121 */
Ingo Molnar6a930702008-12-11 15:17:03 +01001122 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
Thomas Gleixner0793a612008-12-04 20:12:29 +01001123 smp_call_function_single(counter->oncpu,
Ingo Molnar76715812008-12-17 14:20:28 +01001124 __read, counter, 1);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001125 } else if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
1126 update_counter_times(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001127 }
1128
Ingo Molnaree060942008-12-13 09:00:03 +01001129 return atomic64_read(&counter->count);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001130}
1131
Thomas Gleixner0793a612008-12-04 20:12:29 +01001132static void put_context(struct perf_counter_context *ctx)
1133{
1134 if (ctx->task)
1135 put_task_struct(ctx->task);
1136}
1137
1138static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
1139{
1140 struct perf_cpu_context *cpuctx;
1141 struct perf_counter_context *ctx;
1142 struct task_struct *task;
1143
1144 /*
1145 * If cpu is not a wildcard then this is a percpu counter:
1146 */
1147 if (cpu != -1) {
1148 /* Must be root to operate on a CPU counter: */
1149 if (!capable(CAP_SYS_ADMIN))
1150 return ERR_PTR(-EACCES);
1151
1152 if (cpu < 0 || cpu > num_possible_cpus())
1153 return ERR_PTR(-EINVAL);
1154
1155 /*
1156 * We could be clever and allow to attach a counter to an
1157 * offline CPU and activate it when the CPU comes up, but
1158 * that's for later.
1159 */
1160 if (!cpu_isset(cpu, cpu_online_map))
1161 return ERR_PTR(-ENODEV);
1162
1163 cpuctx = &per_cpu(perf_cpu_context, cpu);
1164 ctx = &cpuctx->ctx;
1165
Thomas Gleixner0793a612008-12-04 20:12:29 +01001166 return ctx;
1167 }
1168
1169 rcu_read_lock();
1170 if (!pid)
1171 task = current;
1172 else
1173 task = find_task_by_vpid(pid);
1174 if (task)
1175 get_task_struct(task);
1176 rcu_read_unlock();
1177
1178 if (!task)
1179 return ERR_PTR(-ESRCH);
1180
1181 ctx = &task->perf_counter_ctx;
1182 ctx->task = task;
1183
1184 /* Reuse ptrace permission checks for now. */
1185 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
1186 put_context(ctx);
1187 return ERR_PTR(-EACCES);
1188 }
1189
1190 return ctx;
1191}
1192
Peter Zijlstra592903c2009-03-13 12:21:36 +01001193static void free_counter_rcu(struct rcu_head *head)
1194{
1195 struct perf_counter *counter;
1196
1197 counter = container_of(head, struct perf_counter, rcu_head);
1198 kfree(counter);
1199}
1200
Peter Zijlstra925d5192009-03-30 19:07:02 +02001201static void perf_pending_sync(struct perf_counter *counter);
1202
Peter Zijlstraf1600952009-03-19 20:26:16 +01001203static void free_counter(struct perf_counter *counter)
1204{
Peter Zijlstra925d5192009-03-30 19:07:02 +02001205 perf_pending_sync(counter);
1206
Peter Zijlstrae077df42009-03-19 20:26:17 +01001207 if (counter->destroy)
1208 counter->destroy(counter);
1209
Peter Zijlstraf1600952009-03-19 20:26:16 +01001210 call_rcu(&counter->rcu_head, free_counter_rcu);
1211}
1212
Thomas Gleixner0793a612008-12-04 20:12:29 +01001213/*
1214 * Called when the last reference to the file is gone.
1215 */
1216static int perf_release(struct inode *inode, struct file *file)
1217{
1218 struct perf_counter *counter = file->private_data;
1219 struct perf_counter_context *ctx = counter->ctx;
1220
1221 file->private_data = NULL;
1222
Paul Mackerrasd859e292009-01-17 18:10:22 +11001223 mutex_lock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001224 mutex_lock(&counter->mutex);
1225
Ingo Molnar04289bb2008-12-11 08:38:42 +01001226 perf_counter_remove_from_context(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001227
1228 mutex_unlock(&counter->mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001229 mutex_unlock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001230
Peter Zijlstraf1600952009-03-19 20:26:16 +01001231 free_counter(counter);
Mike Galbraith5af75912009-02-11 10:53:37 +01001232 put_context(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001233
1234 return 0;
1235}
1236
1237/*
1238 * Read the performance counter - simple non blocking version for now
1239 */
1240static ssize_t
1241perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
1242{
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001243 u64 values[3];
1244 int n;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001245
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001246 /*
1247 * Return end-of-file for a read on a counter that is in
1248 * error state (i.e. because it was pinned but it couldn't be
1249 * scheduled on to the CPU at some point).
1250 */
1251 if (counter->state == PERF_COUNTER_STATE_ERROR)
1252 return 0;
1253
Thomas Gleixner0793a612008-12-04 20:12:29 +01001254 mutex_lock(&counter->mutex);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001255 values[0] = perf_counter_read(counter);
1256 n = 1;
1257 if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1258 values[n++] = counter->total_time_enabled +
1259 atomic64_read(&counter->child_total_time_enabled);
1260 if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1261 values[n++] = counter->total_time_running +
1262 atomic64_read(&counter->child_total_time_running);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001263 mutex_unlock(&counter->mutex);
1264
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001265 if (count < n * sizeof(u64))
1266 return -EINVAL;
1267 count = n * sizeof(u64);
1268
1269 if (copy_to_user(buf, values, count))
1270 return -EFAULT;
1271
1272 return count;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001273}
1274
1275static ssize_t
Thomas Gleixner0793a612008-12-04 20:12:29 +01001276perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
1277{
1278 struct perf_counter *counter = file->private_data;
1279
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001280 return perf_read_hw(counter, buf, count);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001281}
1282
1283static unsigned int perf_poll(struct file *file, poll_table *wait)
1284{
1285 struct perf_counter *counter = file->private_data;
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001286 struct perf_mmap_data *data;
1287 unsigned int events;
1288
1289 rcu_read_lock();
1290 data = rcu_dereference(counter->data);
1291 if (data)
1292 events = atomic_xchg(&data->wakeup, 0);
1293 else
1294 events = POLL_HUP;
1295 rcu_read_unlock();
Thomas Gleixner0793a612008-12-04 20:12:29 +01001296
1297 poll_wait(file, &counter->waitq, wait);
1298
Thomas Gleixner0793a612008-12-04 20:12:29 +01001299 return events;
1300}
1301
Paul Mackerrasd859e292009-01-17 18:10:22 +11001302static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1303{
1304 struct perf_counter *counter = file->private_data;
1305 int err = 0;
1306
1307 switch (cmd) {
1308 case PERF_COUNTER_IOC_ENABLE:
1309 perf_counter_enable_family(counter);
1310 break;
1311 case PERF_COUNTER_IOC_DISABLE:
1312 perf_counter_disable_family(counter);
1313 break;
1314 default:
1315 err = -ENOTTY;
1316 }
1317 return err;
1318}
1319
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001320/*
1321 * Callers need to ensure there can be no nesting of this function, otherwise
1322 * the seqlock logic goes bad. We can not serialize this because the arch
1323 * code calls this from NMI context.
1324 */
1325void perf_counter_update_userpage(struct perf_counter *counter)
Paul Mackerras37d81822009-03-23 18:22:08 +01001326{
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001327 struct perf_mmap_data *data;
1328 struct perf_counter_mmap_page *userpg;
1329
1330 rcu_read_lock();
1331 data = rcu_dereference(counter->data);
1332 if (!data)
1333 goto unlock;
1334
1335 userpg = data->user_page;
Paul Mackerras37d81822009-03-23 18:22:08 +01001336
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001337 /*
1338 * Disable preemption so as to not let the corresponding user-space
1339 * spin too long if we get preempted.
1340 */
1341 preempt_disable();
Paul Mackerras37d81822009-03-23 18:22:08 +01001342 ++userpg->lock;
Peter Zijlstra92f22a32009-04-02 11:12:04 +02001343 barrier();
Paul Mackerras37d81822009-03-23 18:22:08 +01001344 userpg->index = counter->hw.idx;
1345 userpg->offset = atomic64_read(&counter->count);
1346 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
1347 userpg->offset -= atomic64_read(&counter->hw.prev_count);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001348
Peter Zijlstra92f22a32009-04-02 11:12:04 +02001349 barrier();
Paul Mackerras37d81822009-03-23 18:22:08 +01001350 ++userpg->lock;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001351 preempt_enable();
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001352unlock:
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001353 rcu_read_unlock();
Paul Mackerras37d81822009-03-23 18:22:08 +01001354}
1355
1356static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1357{
1358 struct perf_counter *counter = vma->vm_file->private_data;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001359 struct perf_mmap_data *data;
1360 int ret = VM_FAULT_SIGBUS;
Paul Mackerras37d81822009-03-23 18:22:08 +01001361
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001362 rcu_read_lock();
1363 data = rcu_dereference(counter->data);
1364 if (!data)
1365 goto unlock;
Paul Mackerras37d81822009-03-23 18:22:08 +01001366
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001367 if (vmf->pgoff == 0) {
1368 vmf->page = virt_to_page(data->user_page);
1369 } else {
1370 int nr = vmf->pgoff - 1;
1371
1372 if ((unsigned)nr > data->nr_pages)
1373 goto unlock;
1374
1375 vmf->page = virt_to_page(data->data_pages[nr]);
1376 }
Paul Mackerras37d81822009-03-23 18:22:08 +01001377 get_page(vmf->page);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001378 ret = 0;
1379unlock:
1380 rcu_read_unlock();
1381
1382 return ret;
1383}
1384
1385static int perf_mmap_data_alloc(struct perf_counter *counter, int nr_pages)
1386{
1387 struct perf_mmap_data *data;
1388 unsigned long size;
1389 int i;
1390
1391 WARN_ON(atomic_read(&counter->mmap_count));
1392
1393 size = sizeof(struct perf_mmap_data);
1394 size += nr_pages * sizeof(void *);
1395
1396 data = kzalloc(size, GFP_KERNEL);
1397 if (!data)
1398 goto fail;
1399
1400 data->user_page = (void *)get_zeroed_page(GFP_KERNEL);
1401 if (!data->user_page)
1402 goto fail_user_page;
1403
1404 for (i = 0; i < nr_pages; i++) {
1405 data->data_pages[i] = (void *)get_zeroed_page(GFP_KERNEL);
1406 if (!data->data_pages[i])
1407 goto fail_data_pages;
1408 }
1409
1410 data->nr_pages = nr_pages;
1411
1412 rcu_assign_pointer(counter->data, data);
1413
Paul Mackerras37d81822009-03-23 18:22:08 +01001414 return 0;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001415
1416fail_data_pages:
1417 for (i--; i >= 0; i--)
1418 free_page((unsigned long)data->data_pages[i]);
1419
1420 free_page((unsigned long)data->user_page);
1421
1422fail_user_page:
1423 kfree(data);
1424
1425fail:
1426 return -ENOMEM;
1427}
1428
1429static void __perf_mmap_data_free(struct rcu_head *rcu_head)
1430{
1431 struct perf_mmap_data *data = container_of(rcu_head,
1432 struct perf_mmap_data, rcu_head);
1433 int i;
1434
1435 free_page((unsigned long)data->user_page);
1436 for (i = 0; i < data->nr_pages; i++)
1437 free_page((unsigned long)data->data_pages[i]);
1438 kfree(data);
1439}
1440
1441static void perf_mmap_data_free(struct perf_counter *counter)
1442{
1443 struct perf_mmap_data *data = counter->data;
1444
1445 WARN_ON(atomic_read(&counter->mmap_count));
1446
1447 rcu_assign_pointer(counter->data, NULL);
1448 call_rcu(&data->rcu_head, __perf_mmap_data_free);
1449}
1450
1451static void perf_mmap_open(struct vm_area_struct *vma)
1452{
1453 struct perf_counter *counter = vma->vm_file->private_data;
1454
1455 atomic_inc(&counter->mmap_count);
1456}
1457
1458static void perf_mmap_close(struct vm_area_struct *vma)
1459{
1460 struct perf_counter *counter = vma->vm_file->private_data;
1461
1462 if (atomic_dec_and_mutex_lock(&counter->mmap_count,
1463 &counter->mmap_mutex)) {
1464 perf_mmap_data_free(counter);
1465 mutex_unlock(&counter->mmap_mutex);
1466 }
Paul Mackerras37d81822009-03-23 18:22:08 +01001467}
1468
1469static struct vm_operations_struct perf_mmap_vmops = {
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001470 .open = perf_mmap_open,
1471 .close = perf_mmap_close,
Paul Mackerras37d81822009-03-23 18:22:08 +01001472 .fault = perf_mmap_fault,
1473};
1474
1475static int perf_mmap(struct file *file, struct vm_area_struct *vma)
1476{
1477 struct perf_counter *counter = file->private_data;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001478 unsigned long vma_size;
1479 unsigned long nr_pages;
1480 unsigned long locked, lock_limit;
1481 int ret = 0;
Paul Mackerras37d81822009-03-23 18:22:08 +01001482
1483 if (!(vma->vm_flags & VM_SHARED) || (vma->vm_flags & VM_WRITE))
1484 return -EINVAL;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001485
1486 vma_size = vma->vm_end - vma->vm_start;
1487 nr_pages = (vma_size / PAGE_SIZE) - 1;
1488
Peter Zijlstra7730d862009-03-25 12:48:31 +01001489 /*
1490 * If we have data pages ensure they're a power-of-two number, so we
1491 * can do bitmasks instead of modulo.
1492 */
1493 if (nr_pages != 0 && !is_power_of_2(nr_pages))
Paul Mackerras37d81822009-03-23 18:22:08 +01001494 return -EINVAL;
1495
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001496 if (vma_size != PAGE_SIZE * (1 + nr_pages))
Paul Mackerras37d81822009-03-23 18:22:08 +01001497 return -EINVAL;
1498
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001499 if (vma->vm_pgoff != 0)
1500 return -EINVAL;
Paul Mackerras37d81822009-03-23 18:22:08 +01001501
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001502 locked = vma_size >> PAGE_SHIFT;
1503 locked += vma->vm_mm->locked_vm;
1504
1505 lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
1506 lock_limit >>= PAGE_SHIFT;
1507
1508 if ((locked > lock_limit) && !capable(CAP_IPC_LOCK))
1509 return -EPERM;
1510
1511 mutex_lock(&counter->mmap_mutex);
1512 if (atomic_inc_not_zero(&counter->mmap_count))
1513 goto out;
1514
1515 WARN_ON(counter->data);
1516 ret = perf_mmap_data_alloc(counter, nr_pages);
1517 if (!ret)
1518 atomic_set(&counter->mmap_count, 1);
1519out:
1520 mutex_unlock(&counter->mmap_mutex);
Paul Mackerras37d81822009-03-23 18:22:08 +01001521
1522 vma->vm_flags &= ~VM_MAYWRITE;
1523 vma->vm_flags |= VM_RESERVED;
1524 vma->vm_ops = &perf_mmap_vmops;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001525
1526 return ret;
Paul Mackerras37d81822009-03-23 18:22:08 +01001527}
1528
Thomas Gleixner0793a612008-12-04 20:12:29 +01001529static const struct file_operations perf_fops = {
1530 .release = perf_release,
1531 .read = perf_read,
1532 .poll = perf_poll,
Paul Mackerrasd859e292009-01-17 18:10:22 +11001533 .unlocked_ioctl = perf_ioctl,
1534 .compat_ioctl = perf_ioctl,
Paul Mackerras37d81822009-03-23 18:22:08 +01001535 .mmap = perf_mmap,
Thomas Gleixner0793a612008-12-04 20:12:29 +01001536};
1537
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001538/*
Peter Zijlstra925d5192009-03-30 19:07:02 +02001539 * Perf counter wakeup
1540 *
1541 * If there's data, ensure we set the poll() state and publish everything
1542 * to user-space before waking everybody up.
1543 */
1544
1545void perf_counter_wakeup(struct perf_counter *counter)
1546{
1547 struct perf_mmap_data *data;
1548
1549 rcu_read_lock();
1550 data = rcu_dereference(counter->data);
1551 if (data) {
1552 (void)atomic_xchg(&data->wakeup, POLL_IN);
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001553 /*
1554 * Ensure all data writes are issued before updating the
1555 * user-space data head information. The matching rmb()
1556 * will be in userspace after reading this value.
1557 */
1558 smp_wmb();
1559 data->user_page->data_head = atomic_read(&data->head);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001560 }
1561 rcu_read_unlock();
1562
1563 wake_up_all(&counter->waitq);
1564}
1565
1566/*
1567 * Pending wakeups
1568 *
1569 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
1570 *
1571 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
1572 * single linked list and use cmpxchg() to add entries lockless.
1573 */
1574
1575#define PENDING_TAIL ((struct perf_wakeup_entry *)-1UL)
1576
1577static DEFINE_PER_CPU(struct perf_wakeup_entry *, perf_wakeup_head) = {
1578 PENDING_TAIL,
1579};
1580
1581static void perf_pending_queue(struct perf_counter *counter)
1582{
1583 struct perf_wakeup_entry **head;
1584 struct perf_wakeup_entry *prev, *next;
1585
1586 if (cmpxchg(&counter->wakeup.next, NULL, PENDING_TAIL) != NULL)
1587 return;
1588
1589 head = &get_cpu_var(perf_wakeup_head);
1590
1591 do {
1592 prev = counter->wakeup.next = *head;
1593 next = &counter->wakeup;
1594 } while (cmpxchg(head, prev, next) != prev);
1595
1596 set_perf_counter_pending();
1597
1598 put_cpu_var(perf_wakeup_head);
1599}
1600
1601static int __perf_pending_run(void)
1602{
1603 struct perf_wakeup_entry *list;
1604 int nr = 0;
1605
1606 list = xchg(&__get_cpu_var(perf_wakeup_head), PENDING_TAIL);
1607 while (list != PENDING_TAIL) {
1608 struct perf_counter *counter = container_of(list,
1609 struct perf_counter, wakeup);
1610
1611 list = list->next;
1612
1613 counter->wakeup.next = NULL;
1614 /*
1615 * Ensure we observe the unqueue before we issue the wakeup,
1616 * so that we won't be waiting forever.
1617 * -- see perf_not_pending().
1618 */
1619 smp_wmb();
1620
1621 perf_counter_wakeup(counter);
1622 nr++;
1623 }
1624
1625 return nr;
1626}
1627
1628static inline int perf_not_pending(struct perf_counter *counter)
1629{
1630 /*
1631 * If we flush on whatever cpu we run, there is a chance we don't
1632 * need to wait.
1633 */
1634 get_cpu();
1635 __perf_pending_run();
1636 put_cpu();
1637
1638 /*
1639 * Ensure we see the proper queue state before going to sleep
1640 * so that we do not miss the wakeup. -- see perf_pending_handle()
1641 */
1642 smp_rmb();
1643 return counter->wakeup.next == NULL;
1644}
1645
1646static void perf_pending_sync(struct perf_counter *counter)
1647{
1648 wait_event(counter->waitq, perf_not_pending(counter));
1649}
1650
1651void perf_counter_do_pending(void)
1652{
1653 __perf_pending_run();
1654}
1655
1656/*
Peter Zijlstra394ee072009-03-30 19:07:14 +02001657 * Callchain support -- arch specific
1658 */
1659
Peter Zijlstra9c03d882009-04-06 11:45:00 +02001660__weak struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
Peter Zijlstra394ee072009-03-30 19:07:14 +02001661{
1662 return NULL;
1663}
1664
1665/*
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001666 * Output
1667 */
1668
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001669struct perf_output_handle {
1670 struct perf_counter *counter;
1671 struct perf_mmap_data *data;
1672 unsigned int offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01001673 unsigned int head;
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001674 int wakeup;
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001675 int nmi;
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001676};
1677
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001678static inline void __perf_output_wakeup(struct perf_output_handle *handle)
1679{
1680 if (handle->nmi)
1681 perf_pending_queue(handle->counter);
1682 else
1683 perf_counter_wakeup(handle->counter);
1684}
1685
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001686static int perf_output_begin(struct perf_output_handle *handle,
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001687 struct perf_counter *counter, unsigned int size,
1688 int nmi)
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001689{
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001690 struct perf_mmap_data *data;
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001691 unsigned int offset, head;
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001692
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001693 rcu_read_lock();
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001694 data = rcu_dereference(counter->data);
1695 if (!data)
1696 goto out;
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001697
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001698 handle->counter = counter;
1699 handle->nmi = nmi;
1700
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001701 if (!data->nr_pages)
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001702 goto fail;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001703
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001704 do {
1705 offset = head = atomic_read(&data->head);
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001706 head += size;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001707 } while (atomic_cmpxchg(&data->head, offset, head) != offset);
1708
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001709 handle->data = data;
1710 handle->offset = offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01001711 handle->head = head;
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001712 handle->wakeup = (offset >> PAGE_SHIFT) != (head >> PAGE_SHIFT);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001713
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001714 return 0;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001715
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001716fail:
1717 __perf_output_wakeup(handle);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001718out:
1719 rcu_read_unlock();
1720
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001721 return -ENOSPC;
1722}
1723
1724static void perf_output_copy(struct perf_output_handle *handle,
1725 void *buf, unsigned int len)
1726{
1727 unsigned int pages_mask;
1728 unsigned int offset;
1729 unsigned int size;
1730 void **pages;
1731
1732 offset = handle->offset;
1733 pages_mask = handle->data->nr_pages - 1;
1734 pages = handle->data->data_pages;
1735
1736 do {
1737 unsigned int page_offset;
1738 int nr;
1739
1740 nr = (offset >> PAGE_SHIFT) & pages_mask;
1741 page_offset = offset & (PAGE_SIZE - 1);
1742 size = min_t(unsigned int, PAGE_SIZE - page_offset, len);
1743
1744 memcpy(pages[nr] + page_offset, buf, size);
1745
1746 len -= size;
1747 buf += size;
1748 offset += size;
1749 } while (len);
1750
1751 handle->offset = offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01001752
1753 WARN_ON_ONCE(handle->offset > handle->head);
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001754}
1755
Peter Zijlstra5c148192009-03-25 12:30:23 +01001756#define perf_output_put(handle, x) \
1757 perf_output_copy((handle), &(x), sizeof(x))
1758
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001759static void perf_output_end(struct perf_output_handle *handle)
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001760{
Peter Zijlstrac4578102009-04-02 11:12:01 +02001761 int wakeup_events = handle->counter->hw_event.wakeup_events;
1762
1763 if (wakeup_events) {
1764 int events = atomic_inc_return(&handle->data->events);
1765 if (events >= wakeup_events) {
1766 atomic_sub(wakeup_events, &handle->data->events);
1767 __perf_output_wakeup(handle);
1768 }
1769 } else if (handle->wakeup)
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001770 __perf_output_wakeup(handle);
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001771 rcu_read_unlock();
1772}
1773
Peter Zijlstra8a057d82009-04-02 11:11:59 +02001774void perf_counter_output(struct perf_counter *counter,
1775 int nmi, struct pt_regs *regs)
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001776{
Peter Zijlstra5ed00412009-03-30 19:07:12 +02001777 int ret;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02001778 u64 record_type = counter->hw_event.record_type;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02001779 struct perf_output_handle handle;
1780 struct perf_event_header header;
1781 u64 ip;
Peter Zijlstra5c148192009-03-25 12:30:23 +01001782 struct {
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01001783 u32 pid, tid;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02001784 } tid_entry;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02001785 struct {
1786 u64 event;
1787 u64 counter;
1788 } group_entry;
Peter Zijlstra394ee072009-03-30 19:07:14 +02001789 struct perf_callchain_entry *callchain = NULL;
1790 int callchain_size = 0;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001791
Peter Zijlstra8a057d82009-04-02 11:11:59 +02001792 header.type = PERF_EVENT_COUNTER_OVERFLOW;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02001793 header.size = sizeof(header);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001794
Peter Zijlstra8a057d82009-04-02 11:11:59 +02001795 if (record_type & PERF_RECORD_IP) {
1796 ip = instruction_pointer(regs);
1797 header.type |= __PERF_EVENT_IP;
1798 header.size += sizeof(ip);
1799 }
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01001800
Peter Zijlstra8a057d82009-04-02 11:11:59 +02001801 if (record_type & PERF_RECORD_TID) {
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01001802 /* namespace issues */
Peter Zijlstra5ed00412009-03-30 19:07:12 +02001803 tid_entry.pid = current->group_leader->pid;
1804 tid_entry.tid = current->pid;
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01001805
Peter Zijlstra5ed00412009-03-30 19:07:12 +02001806 header.type |= __PERF_EVENT_TID;
1807 header.size += sizeof(tid_entry);
1808 }
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01001809
Peter Zijlstra8a057d82009-04-02 11:11:59 +02001810 if (record_type & PERF_RECORD_GROUP) {
1811 header.type |= __PERF_EVENT_GROUP;
1812 header.size += sizeof(u64) +
1813 counter->nr_siblings * sizeof(group_entry);
1814 }
1815
1816 if (record_type & PERF_RECORD_CALLCHAIN) {
Peter Zijlstra394ee072009-03-30 19:07:14 +02001817 callchain = perf_callchain(regs);
1818
1819 if (callchain) {
Peter Zijlstra9c03d882009-04-06 11:45:00 +02001820 callchain_size = (1 + callchain->nr) * sizeof(u64);
Peter Zijlstra394ee072009-03-30 19:07:14 +02001821
1822 header.type |= __PERF_EVENT_CALLCHAIN;
1823 header.size += callchain_size;
1824 }
1825 }
1826
Peter Zijlstra5ed00412009-03-30 19:07:12 +02001827 ret = perf_output_begin(&handle, counter, header.size, nmi);
1828 if (ret)
1829 return;
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01001830
Peter Zijlstra5ed00412009-03-30 19:07:12 +02001831 perf_output_put(&handle, header);
Peter Zijlstra5ed00412009-03-30 19:07:12 +02001832
Peter Zijlstra8a057d82009-04-02 11:11:59 +02001833 if (record_type & PERF_RECORD_IP)
1834 perf_output_put(&handle, ip);
1835
1836 if (record_type & PERF_RECORD_TID)
Peter Zijlstra5ed00412009-03-30 19:07:12 +02001837 perf_output_put(&handle, tid_entry);
1838
Peter Zijlstra8a057d82009-04-02 11:11:59 +02001839 if (record_type & PERF_RECORD_GROUP) {
1840 struct perf_counter *leader, *sub;
1841 u64 nr = counter->nr_siblings;
1842
1843 perf_output_put(&handle, nr);
1844
1845 leader = counter->group_leader;
1846 list_for_each_entry(sub, &leader->sibling_list, list_entry) {
1847 if (sub != counter)
1848 sub->hw_ops->read(sub);
1849
1850 group_entry.event = sub->hw_event.config;
1851 group_entry.counter = atomic64_read(&sub->count);
1852
1853 perf_output_put(&handle, group_entry);
1854 }
1855 }
1856
Peter Zijlstra394ee072009-03-30 19:07:14 +02001857 if (callchain)
1858 perf_output_copy(&handle, callchain, callchain_size);
1859
Peter Zijlstra5ed00412009-03-30 19:07:12 +02001860 perf_output_end(&handle);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001861}
1862
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001863/*
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02001864 * mmap tracking
1865 */
1866
1867struct perf_mmap_event {
1868 struct file *file;
1869 char *file_name;
1870 int file_size;
1871
1872 struct {
1873 struct perf_event_header header;
1874
1875 u32 pid;
1876 u32 tid;
1877 u64 start;
1878 u64 len;
1879 u64 pgoff;
1880 } event;
1881};
1882
1883static void perf_counter_mmap_output(struct perf_counter *counter,
1884 struct perf_mmap_event *mmap_event)
1885{
1886 struct perf_output_handle handle;
1887 int size = mmap_event->event.header.size;
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001888 int ret = perf_output_begin(&handle, counter, size, 0);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02001889
1890 if (ret)
1891 return;
1892
1893 perf_output_put(&handle, mmap_event->event);
1894 perf_output_copy(&handle, mmap_event->file_name,
1895 mmap_event->file_size);
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001896 perf_output_end(&handle);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02001897}
1898
1899static int perf_counter_mmap_match(struct perf_counter *counter,
1900 struct perf_mmap_event *mmap_event)
1901{
1902 if (counter->hw_event.mmap &&
1903 mmap_event->event.header.type == PERF_EVENT_MMAP)
1904 return 1;
1905
1906 if (counter->hw_event.munmap &&
1907 mmap_event->event.header.type == PERF_EVENT_MUNMAP)
1908 return 1;
1909
1910 return 0;
1911}
1912
1913static void perf_counter_mmap_ctx(struct perf_counter_context *ctx,
1914 struct perf_mmap_event *mmap_event)
1915{
1916 struct perf_counter *counter;
1917
1918 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
1919 return;
1920
1921 rcu_read_lock();
1922 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
1923 if (perf_counter_mmap_match(counter, mmap_event))
1924 perf_counter_mmap_output(counter, mmap_event);
1925 }
1926 rcu_read_unlock();
1927}
1928
1929static void perf_counter_mmap_event(struct perf_mmap_event *mmap_event)
1930{
1931 struct perf_cpu_context *cpuctx;
1932 struct file *file = mmap_event->file;
1933 unsigned int size;
1934 char tmp[16];
1935 char *buf = NULL;
1936 char *name;
1937
1938 if (file) {
1939 buf = kzalloc(PATH_MAX, GFP_KERNEL);
1940 if (!buf) {
1941 name = strncpy(tmp, "//enomem", sizeof(tmp));
1942 goto got_name;
1943 }
1944 name = dentry_path(file->f_dentry, buf, PATH_MAX);
1945 if (IS_ERR(name)) {
1946 name = strncpy(tmp, "//toolong", sizeof(tmp));
1947 goto got_name;
1948 }
1949 } else {
1950 name = strncpy(tmp, "//anon", sizeof(tmp));
1951 goto got_name;
1952 }
1953
1954got_name:
1955 size = ALIGN(strlen(name), sizeof(u64));
1956
1957 mmap_event->file_name = name;
1958 mmap_event->file_size = size;
1959
1960 mmap_event->event.header.size = sizeof(mmap_event->event) + size;
1961
1962 cpuctx = &get_cpu_var(perf_cpu_context);
1963 perf_counter_mmap_ctx(&cpuctx->ctx, mmap_event);
1964 put_cpu_var(perf_cpu_context);
1965
1966 perf_counter_mmap_ctx(&current->perf_counter_ctx, mmap_event);
1967
1968 kfree(buf);
1969}
1970
1971void perf_counter_mmap(unsigned long addr, unsigned long len,
1972 unsigned long pgoff, struct file *file)
1973{
1974 struct perf_mmap_event mmap_event = {
1975 .file = file,
1976 .event = {
1977 .header = { .type = PERF_EVENT_MMAP, },
1978 .pid = current->group_leader->pid,
1979 .tid = current->pid,
1980 .start = addr,
1981 .len = len,
1982 .pgoff = pgoff,
1983 },
1984 };
1985
1986 perf_counter_mmap_event(&mmap_event);
1987}
1988
1989void perf_counter_munmap(unsigned long addr, unsigned long len,
1990 unsigned long pgoff, struct file *file)
1991{
1992 struct perf_mmap_event mmap_event = {
1993 .file = file,
1994 .event = {
1995 .header = { .type = PERF_EVENT_MUNMAP, },
1996 .pid = current->group_leader->pid,
1997 .tid = current->pid,
1998 .start = addr,
1999 .len = len,
2000 .pgoff = pgoff,
2001 },
2002 };
2003
2004 perf_counter_mmap_event(&mmap_event);
2005}
2006
2007/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002008 * Generic software counter infrastructure
2009 */
2010
2011static void perf_swcounter_update(struct perf_counter *counter)
2012{
2013 struct hw_perf_counter *hwc = &counter->hw;
2014 u64 prev, now;
2015 s64 delta;
2016
2017again:
2018 prev = atomic64_read(&hwc->prev_count);
2019 now = atomic64_read(&hwc->count);
2020 if (atomic64_cmpxchg(&hwc->prev_count, prev, now) != prev)
2021 goto again;
2022
2023 delta = now - prev;
2024
2025 atomic64_add(delta, &counter->count);
2026 atomic64_sub(delta, &hwc->period_left);
2027}
2028
2029static void perf_swcounter_set_period(struct perf_counter *counter)
2030{
2031 struct hw_perf_counter *hwc = &counter->hw;
2032 s64 left = atomic64_read(&hwc->period_left);
2033 s64 period = hwc->irq_period;
2034
2035 if (unlikely(left <= -period)) {
2036 left = period;
2037 atomic64_set(&hwc->period_left, left);
2038 }
2039
2040 if (unlikely(left <= 0)) {
2041 left += period;
2042 atomic64_add(period, &hwc->period_left);
2043 }
2044
2045 atomic64_set(&hwc->prev_count, -left);
2046 atomic64_set(&hwc->count, -left);
2047}
2048
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002049static enum hrtimer_restart perf_swcounter_hrtimer(struct hrtimer *hrtimer)
2050{
2051 struct perf_counter *counter;
2052 struct pt_regs *regs;
2053
2054 counter = container_of(hrtimer, struct perf_counter, hw.hrtimer);
2055 counter->hw_ops->read(counter);
2056
2057 regs = get_irq_regs();
2058 /*
2059 * In case we exclude kernel IPs or are somehow not in interrupt
2060 * context, provide the next best thing, the user IP.
2061 */
2062 if ((counter->hw_event.exclude_kernel || !regs) &&
2063 !counter->hw_event.exclude_user)
2064 regs = task_pt_regs(current);
2065
2066 if (regs)
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002067 perf_counter_output(counter, 0, regs);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002068
2069 hrtimer_forward_now(hrtimer, ns_to_ktime(counter->hw.irq_period));
2070
2071 return HRTIMER_RESTART;
2072}
2073
2074static void perf_swcounter_overflow(struct perf_counter *counter,
2075 int nmi, struct pt_regs *regs)
2076{
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002077 perf_swcounter_update(counter);
2078 perf_swcounter_set_period(counter);
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002079 perf_counter_output(counter, nmi, regs);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002080}
2081
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002082static int perf_swcounter_match(struct perf_counter *counter,
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002083 enum perf_event_types type,
2084 u32 event, struct pt_regs *regs)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002085{
2086 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
2087 return 0;
2088
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002089 if (perf_event_raw(&counter->hw_event))
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002090 return 0;
2091
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002092 if (perf_event_type(&counter->hw_event) != type)
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002093 return 0;
2094
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002095 if (perf_event_id(&counter->hw_event) != event)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002096 return 0;
2097
2098 if (counter->hw_event.exclude_user && user_mode(regs))
2099 return 0;
2100
2101 if (counter->hw_event.exclude_kernel && !user_mode(regs))
2102 return 0;
2103
2104 return 1;
2105}
2106
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002107static void perf_swcounter_add(struct perf_counter *counter, u64 nr,
2108 int nmi, struct pt_regs *regs)
2109{
2110 int neg = atomic64_add_negative(nr, &counter->hw.count);
2111 if (counter->hw.irq_period && !neg)
2112 perf_swcounter_overflow(counter, nmi, regs);
2113}
2114
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002115static void perf_swcounter_ctx_event(struct perf_counter_context *ctx,
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002116 enum perf_event_types type, u32 event,
2117 u64 nr, int nmi, struct pt_regs *regs)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002118{
2119 struct perf_counter *counter;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002120
Peter Zijlstra01ef09d2009-03-19 20:26:11 +01002121 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002122 return;
2123
Peter Zijlstra592903c2009-03-13 12:21:36 +01002124 rcu_read_lock();
2125 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002126 if (perf_swcounter_match(counter, type, event, regs))
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002127 perf_swcounter_add(counter, nr, nmi, regs);
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002128 }
Peter Zijlstra592903c2009-03-13 12:21:36 +01002129 rcu_read_unlock();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002130}
2131
Peter Zijlstra96f6d442009-03-23 18:22:07 +01002132static int *perf_swcounter_recursion_context(struct perf_cpu_context *cpuctx)
2133{
2134 if (in_nmi())
2135 return &cpuctx->recursion[3];
2136
2137 if (in_irq())
2138 return &cpuctx->recursion[2];
2139
2140 if (in_softirq())
2141 return &cpuctx->recursion[1];
2142
2143 return &cpuctx->recursion[0];
2144}
2145
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002146static void __perf_swcounter_event(enum perf_event_types type, u32 event,
2147 u64 nr, int nmi, struct pt_regs *regs)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002148{
2149 struct perf_cpu_context *cpuctx = &get_cpu_var(perf_cpu_context);
Peter Zijlstra96f6d442009-03-23 18:22:07 +01002150 int *recursion = perf_swcounter_recursion_context(cpuctx);
2151
2152 if (*recursion)
2153 goto out;
2154
2155 (*recursion)++;
2156 barrier();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002157
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002158 perf_swcounter_ctx_event(&cpuctx->ctx, type, event, nr, nmi, regs);
2159 if (cpuctx->task_ctx) {
2160 perf_swcounter_ctx_event(cpuctx->task_ctx, type, event,
2161 nr, nmi, regs);
2162 }
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002163
Peter Zijlstra96f6d442009-03-23 18:22:07 +01002164 barrier();
2165 (*recursion)--;
2166
2167out:
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002168 put_cpu_var(perf_cpu_context);
2169}
2170
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002171void perf_swcounter_event(u32 event, u64 nr, int nmi, struct pt_regs *regs)
2172{
2173 __perf_swcounter_event(PERF_TYPE_SOFTWARE, event, nr, nmi, regs);
2174}
2175
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002176static void perf_swcounter_read(struct perf_counter *counter)
2177{
2178 perf_swcounter_update(counter);
2179}
2180
2181static int perf_swcounter_enable(struct perf_counter *counter)
2182{
2183 perf_swcounter_set_period(counter);
2184 return 0;
2185}
2186
2187static void perf_swcounter_disable(struct perf_counter *counter)
2188{
2189 perf_swcounter_update(counter);
2190}
2191
Peter Zijlstraac17dc82009-03-13 12:21:34 +01002192static const struct hw_perf_counter_ops perf_ops_generic = {
2193 .enable = perf_swcounter_enable,
2194 .disable = perf_swcounter_disable,
2195 .read = perf_swcounter_read,
2196};
2197
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002198/*
2199 * Software counter: cpu wall time clock
2200 */
2201
Paul Mackerras9abf8a02009-01-09 16:26:43 +11002202static void cpu_clock_perf_counter_update(struct perf_counter *counter)
2203{
2204 int cpu = raw_smp_processor_id();
2205 s64 prev;
2206 u64 now;
2207
2208 now = cpu_clock(cpu);
2209 prev = atomic64_read(&counter->hw.prev_count);
2210 atomic64_set(&counter->hw.prev_count, now);
2211 atomic64_add(now - prev, &counter->count);
2212}
2213
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002214static int cpu_clock_perf_counter_enable(struct perf_counter *counter)
2215{
2216 struct hw_perf_counter *hwc = &counter->hw;
2217 int cpu = raw_smp_processor_id();
2218
2219 atomic64_set(&hwc->prev_count, cpu_clock(cpu));
Peter Zijlstra039fc912009-03-13 16:43:47 +01002220 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2221 hwc->hrtimer.function = perf_swcounter_hrtimer;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002222 if (hwc->irq_period) {
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002223 __hrtimer_start_range_ns(&hwc->hrtimer,
2224 ns_to_ktime(hwc->irq_period), 0,
2225 HRTIMER_MODE_REL, 0);
2226 }
2227
2228 return 0;
2229}
2230
Ingo Molnar5c92d122008-12-11 13:21:10 +01002231static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
2232{
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002233 hrtimer_cancel(&counter->hw.hrtimer);
Paul Mackerras9abf8a02009-01-09 16:26:43 +11002234 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01002235}
2236
2237static void cpu_clock_perf_counter_read(struct perf_counter *counter)
2238{
Paul Mackerras9abf8a02009-01-09 16:26:43 +11002239 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01002240}
2241
2242static const struct hw_perf_counter_ops perf_ops_cpu_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +01002243 .enable = cpu_clock_perf_counter_enable,
2244 .disable = cpu_clock_perf_counter_disable,
2245 .read = cpu_clock_perf_counter_read,
Ingo Molnar5c92d122008-12-11 13:21:10 +01002246};
2247
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01002248/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002249 * Software counter: task time clock
2250 */
2251
2252/*
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01002253 * Called from within the scheduler:
2254 */
2255static u64 task_clock_perf_counter_val(struct perf_counter *counter, int update)
Ingo Molnarbae43c92008-12-11 14:03:20 +01002256{
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01002257 struct task_struct *curr = counter->task;
2258 u64 delta;
2259
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01002260 delta = __task_delta_exec(curr, update);
2261
2262 return curr->se.sum_exec_runtime + delta;
2263}
2264
2265static void task_clock_perf_counter_update(struct perf_counter *counter, u64 now)
2266{
2267 u64 prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002268 s64 delta;
Ingo Molnarbae43c92008-12-11 14:03:20 +01002269
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002270 prev = atomic64_read(&counter->hw.prev_count);
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002271
2272 atomic64_set(&counter->hw.prev_count, now);
2273
2274 delta = now - prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002275
2276 atomic64_add(delta, &counter->count);
Ingo Molnarbae43c92008-12-11 14:03:20 +01002277}
2278
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01002279static int task_clock_perf_counter_enable(struct perf_counter *counter)
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002280{
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002281 struct hw_perf_counter *hwc = &counter->hw;
2282
2283 atomic64_set(&hwc->prev_count, task_clock_perf_counter_val(counter, 0));
Peter Zijlstra039fc912009-03-13 16:43:47 +01002284 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2285 hwc->hrtimer.function = perf_swcounter_hrtimer;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002286 if (hwc->irq_period) {
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002287 __hrtimer_start_range_ns(&hwc->hrtimer,
2288 ns_to_ktime(hwc->irq_period), 0,
2289 HRTIMER_MODE_REL, 0);
2290 }
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01002291
2292 return 0;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002293}
2294
2295static void task_clock_perf_counter_disable(struct perf_counter *counter)
2296{
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002297 hrtimer_cancel(&counter->hw.hrtimer);
2298 task_clock_perf_counter_update(counter,
2299 task_clock_perf_counter_val(counter, 0));
2300}
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01002301
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002302static void task_clock_perf_counter_read(struct perf_counter *counter)
2303{
2304 task_clock_perf_counter_update(counter,
2305 task_clock_perf_counter_val(counter, 1));
Ingo Molnarbae43c92008-12-11 14:03:20 +01002306}
2307
2308static const struct hw_perf_counter_ops perf_ops_task_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +01002309 .enable = task_clock_perf_counter_enable,
2310 .disable = task_clock_perf_counter_disable,
2311 .read = task_clock_perf_counter_read,
Ingo Molnarbae43c92008-12-11 14:03:20 +01002312};
2313
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002314/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002315 * Software counter: cpu migrations
2316 */
2317
Paul Mackerras23a185c2009-02-09 22:42:47 +11002318static inline u64 get_cpu_migrations(struct perf_counter *counter)
Ingo Molnar6c594c22008-12-14 12:34:15 +01002319{
Paul Mackerras23a185c2009-02-09 22:42:47 +11002320 struct task_struct *curr = counter->ctx->task;
2321
2322 if (curr)
2323 return curr->se.nr_migrations;
2324 return cpu_nr_migrations(smp_processor_id());
Ingo Molnar6c594c22008-12-14 12:34:15 +01002325}
2326
2327static void cpu_migrations_perf_counter_update(struct perf_counter *counter)
2328{
2329 u64 prev, now;
2330 s64 delta;
2331
2332 prev = atomic64_read(&counter->hw.prev_count);
Paul Mackerras23a185c2009-02-09 22:42:47 +11002333 now = get_cpu_migrations(counter);
Ingo Molnar6c594c22008-12-14 12:34:15 +01002334
2335 atomic64_set(&counter->hw.prev_count, now);
2336
2337 delta = now - prev;
Ingo Molnar6c594c22008-12-14 12:34:15 +01002338
2339 atomic64_add(delta, &counter->count);
2340}
2341
2342static void cpu_migrations_perf_counter_read(struct perf_counter *counter)
2343{
2344 cpu_migrations_perf_counter_update(counter);
2345}
2346
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01002347static int cpu_migrations_perf_counter_enable(struct perf_counter *counter)
Ingo Molnar6c594c22008-12-14 12:34:15 +01002348{
Paul Mackerrasc07c99b2009-02-13 22:10:34 +11002349 if (counter->prev_state <= PERF_COUNTER_STATE_OFF)
2350 atomic64_set(&counter->hw.prev_count,
2351 get_cpu_migrations(counter));
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01002352 return 0;
Ingo Molnar6c594c22008-12-14 12:34:15 +01002353}
2354
2355static void cpu_migrations_perf_counter_disable(struct perf_counter *counter)
2356{
2357 cpu_migrations_perf_counter_update(counter);
2358}
2359
2360static const struct hw_perf_counter_ops perf_ops_cpu_migrations = {
Ingo Molnar76715812008-12-17 14:20:28 +01002361 .enable = cpu_migrations_perf_counter_enable,
2362 .disable = cpu_migrations_perf_counter_disable,
2363 .read = cpu_migrations_perf_counter_read,
Ingo Molnar6c594c22008-12-14 12:34:15 +01002364};
2365
Peter Zijlstrae077df42009-03-19 20:26:17 +01002366#ifdef CONFIG_EVENT_PROFILE
2367void perf_tpcounter_event(int event_id)
2368{
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002369 struct pt_regs *regs = get_irq_regs();
2370
2371 if (!regs)
2372 regs = task_pt_regs(current);
2373
2374 __perf_swcounter_event(PERF_TYPE_TRACEPOINT, event_id, 1, 1, regs);
Peter Zijlstrae077df42009-03-19 20:26:17 +01002375}
2376
2377extern int ftrace_profile_enable(int);
2378extern void ftrace_profile_disable(int);
2379
2380static void tp_perf_counter_destroy(struct perf_counter *counter)
2381{
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002382 ftrace_profile_disable(perf_event_id(&counter->hw_event));
Peter Zijlstrae077df42009-03-19 20:26:17 +01002383}
2384
2385static const struct hw_perf_counter_ops *
2386tp_perf_counter_init(struct perf_counter *counter)
2387{
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002388 int event_id = perf_event_id(&counter->hw_event);
Peter Zijlstrae077df42009-03-19 20:26:17 +01002389 int ret;
2390
2391 ret = ftrace_profile_enable(event_id);
2392 if (ret)
2393 return NULL;
2394
2395 counter->destroy = tp_perf_counter_destroy;
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002396 counter->hw.irq_period = counter->hw_event.irq_period;
Peter Zijlstrae077df42009-03-19 20:26:17 +01002397
2398 return &perf_ops_generic;
2399}
2400#else
2401static const struct hw_perf_counter_ops *
2402tp_perf_counter_init(struct perf_counter *counter)
2403{
2404 return NULL;
2405}
2406#endif
2407
Ingo Molnar5c92d122008-12-11 13:21:10 +01002408static const struct hw_perf_counter_ops *
2409sw_perf_counter_init(struct perf_counter *counter)
2410{
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002411 struct perf_counter_hw_event *hw_event = &counter->hw_event;
Ingo Molnar5c92d122008-12-11 13:21:10 +01002412 const struct hw_perf_counter_ops *hw_ops = NULL;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002413 struct hw_perf_counter *hwc = &counter->hw;
Ingo Molnar5c92d122008-12-11 13:21:10 +01002414
Paul Mackerras0475f9e2009-02-11 14:35:35 +11002415 /*
2416 * Software counters (currently) can't in general distinguish
2417 * between user, kernel and hypervisor events.
2418 * However, context switches and cpu migrations are considered
2419 * to be kernel events, and page faults are never hypervisor
2420 * events.
2421 */
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002422 switch (perf_event_id(&counter->hw_event)) {
Ingo Molnar5c92d122008-12-11 13:21:10 +01002423 case PERF_COUNT_CPU_CLOCK:
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002424 hw_ops = &perf_ops_cpu_clock;
2425
2426 if (hw_event->irq_period && hw_event->irq_period < 10000)
2427 hw_event->irq_period = 10000;
Ingo Molnar5c92d122008-12-11 13:21:10 +01002428 break;
Ingo Molnarbae43c92008-12-11 14:03:20 +01002429 case PERF_COUNT_TASK_CLOCK:
Paul Mackerras23a185c2009-02-09 22:42:47 +11002430 /*
2431 * If the user instantiates this as a per-cpu counter,
2432 * use the cpu_clock counter instead.
2433 */
2434 if (counter->ctx->task)
2435 hw_ops = &perf_ops_task_clock;
2436 else
2437 hw_ops = &perf_ops_cpu_clock;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002438
2439 if (hw_event->irq_period && hw_event->irq_period < 10000)
2440 hw_event->irq_period = 10000;
Ingo Molnarbae43c92008-12-11 14:03:20 +01002441 break;
Ingo Molnare06c61a2008-12-14 14:44:31 +01002442 case PERF_COUNT_PAGE_FAULTS:
Peter Zijlstraac17dc82009-03-13 12:21:34 +01002443 case PERF_COUNT_PAGE_FAULTS_MIN:
2444 case PERF_COUNT_PAGE_FAULTS_MAJ:
Ingo Molnar5d6a27d2008-12-14 12:28:33 +01002445 case PERF_COUNT_CONTEXT_SWITCHES:
Peter Zijlstra4a0deca2009-03-19 20:26:12 +01002446 hw_ops = &perf_ops_generic;
Ingo Molnar5d6a27d2008-12-14 12:28:33 +01002447 break;
Ingo Molnar6c594c22008-12-14 12:34:15 +01002448 case PERF_COUNT_CPU_MIGRATIONS:
Paul Mackerras0475f9e2009-02-11 14:35:35 +11002449 if (!counter->hw_event.exclude_kernel)
2450 hw_ops = &perf_ops_cpu_migrations;
Ingo Molnar6c594c22008-12-14 12:34:15 +01002451 break;
Ingo Molnar5c92d122008-12-11 13:21:10 +01002452 }
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002453
2454 if (hw_ops)
2455 hwc->irq_period = hw_event->irq_period;
2456
Ingo Molnar5c92d122008-12-11 13:21:10 +01002457 return hw_ops;
2458}
2459
Thomas Gleixner0793a612008-12-04 20:12:29 +01002460/*
2461 * Allocate and initialize a counter structure
2462 */
2463static struct perf_counter *
Ingo Molnar04289bb2008-12-11 08:38:42 +01002464perf_counter_alloc(struct perf_counter_hw_event *hw_event,
2465 int cpu,
Paul Mackerras23a185c2009-02-09 22:42:47 +11002466 struct perf_counter_context *ctx,
Ingo Molnar9b51f662008-12-12 13:49:45 +01002467 struct perf_counter *group_leader,
2468 gfp_t gfpflags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01002469{
Ingo Molnar5c92d122008-12-11 13:21:10 +01002470 const struct hw_perf_counter_ops *hw_ops;
Ingo Molnar621a01e2008-12-11 12:46:46 +01002471 struct perf_counter *counter;
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02002472 long err;
Thomas Gleixner0793a612008-12-04 20:12:29 +01002473
Ingo Molnar9b51f662008-12-12 13:49:45 +01002474 counter = kzalloc(sizeof(*counter), gfpflags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002475 if (!counter)
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02002476 return ERR_PTR(-ENOMEM);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002477
Ingo Molnar04289bb2008-12-11 08:38:42 +01002478 /*
2479 * Single counters are their own group leaders, with an
2480 * empty sibling list:
2481 */
2482 if (!group_leader)
2483 group_leader = counter;
2484
Thomas Gleixner0793a612008-12-04 20:12:29 +01002485 mutex_init(&counter->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01002486 INIT_LIST_HEAD(&counter->list_entry);
Peter Zijlstra592903c2009-03-13 12:21:36 +01002487 INIT_LIST_HEAD(&counter->event_entry);
Ingo Molnar04289bb2008-12-11 08:38:42 +01002488 INIT_LIST_HEAD(&counter->sibling_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002489 init_waitqueue_head(&counter->waitq);
2490
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002491 mutex_init(&counter->mmap_mutex);
2492
Paul Mackerrasd859e292009-01-17 18:10:22 +11002493 INIT_LIST_HEAD(&counter->child_list);
2494
Ingo Molnar9f66a382008-12-10 12:33:23 +01002495 counter->cpu = cpu;
2496 counter->hw_event = *hw_event;
Ingo Molnar04289bb2008-12-11 08:38:42 +01002497 counter->group_leader = group_leader;
Ingo Molnar621a01e2008-12-11 12:46:46 +01002498 counter->hw_ops = NULL;
Paul Mackerras23a185c2009-02-09 22:42:47 +11002499 counter->ctx = ctx;
Ingo Molnar621a01e2008-12-11 12:46:46 +01002500
Ingo Molnar235c7fc2008-12-21 14:43:25 +01002501 counter->state = PERF_COUNTER_STATE_INACTIVE;
Ingo Molnara86ed502008-12-17 00:43:10 +01002502 if (hw_event->disabled)
2503 counter->state = PERF_COUNTER_STATE_OFF;
2504
Ingo Molnar5c92d122008-12-11 13:21:10 +01002505 hw_ops = NULL;
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002506
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002507 if (perf_event_raw(hw_event)) {
Ingo Molnar5c92d122008-12-11 13:21:10 +01002508 hw_ops = hw_perf_counter_init(counter);
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002509 goto done;
2510 }
2511
2512 switch (perf_event_type(hw_event)) {
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002513 case PERF_TYPE_HARDWARE:
2514 hw_ops = hw_perf_counter_init(counter);
2515 break;
2516
2517 case PERF_TYPE_SOFTWARE:
2518 hw_ops = sw_perf_counter_init(counter);
2519 break;
2520
2521 case PERF_TYPE_TRACEPOINT:
2522 hw_ops = tp_perf_counter_init(counter);
2523 break;
2524 }
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002525done:
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02002526 err = 0;
2527 if (!hw_ops)
2528 err = -EINVAL;
2529 else if (IS_ERR(hw_ops))
2530 err = PTR_ERR(hw_ops);
2531
2532 if (err) {
2533 kfree(counter);
2534 return ERR_PTR(err);
2535 }
2536
Ingo Molnar621a01e2008-12-11 12:46:46 +01002537 counter->hw_ops = hw_ops;
Thomas Gleixner0793a612008-12-04 20:12:29 +01002538
2539 return counter;
2540}
2541
2542/**
Paul Mackerras2743a5b2009-03-04 20:36:51 +11002543 * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
Ingo Molnar9f66a382008-12-10 12:33:23 +01002544 *
2545 * @hw_event_uptr: event type attributes for monitoring/sampling
Thomas Gleixner0793a612008-12-04 20:12:29 +01002546 * @pid: target pid
Ingo Molnar9f66a382008-12-10 12:33:23 +01002547 * @cpu: target cpu
2548 * @group_fd: group leader counter fd
Thomas Gleixner0793a612008-12-04 20:12:29 +01002549 */
Paul Mackerras2743a5b2009-03-04 20:36:51 +11002550SYSCALL_DEFINE5(perf_counter_open,
Paul Mackerrasf3dfd262009-02-26 22:43:46 +11002551 const struct perf_counter_hw_event __user *, hw_event_uptr,
Paul Mackerras2743a5b2009-03-04 20:36:51 +11002552 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01002553{
Ingo Molnar04289bb2008-12-11 08:38:42 +01002554 struct perf_counter *counter, *group_leader;
Ingo Molnar9f66a382008-12-10 12:33:23 +01002555 struct perf_counter_hw_event hw_event;
Ingo Molnar04289bb2008-12-11 08:38:42 +01002556 struct perf_counter_context *ctx;
Ingo Molnar9b51f662008-12-12 13:49:45 +01002557 struct file *counter_file = NULL;
Ingo Molnar04289bb2008-12-11 08:38:42 +01002558 struct file *group_file = NULL;
2559 int fput_needed = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01002560 int fput_needed2 = 0;
Thomas Gleixner0793a612008-12-04 20:12:29 +01002561 int ret;
2562
Paul Mackerras2743a5b2009-03-04 20:36:51 +11002563 /* for future expandability... */
2564 if (flags)
2565 return -EINVAL;
2566
Ingo Molnar9f66a382008-12-10 12:33:23 +01002567 if (copy_from_user(&hw_event, hw_event_uptr, sizeof(hw_event)) != 0)
Thomas Gleixnereab656a2008-12-08 19:26:59 +01002568 return -EFAULT;
2569
Ingo Molnar04289bb2008-12-11 08:38:42 +01002570 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01002571 * Get the target context (task or percpu):
2572 */
2573 ctx = find_get_context(pid, cpu);
2574 if (IS_ERR(ctx))
2575 return PTR_ERR(ctx);
2576
2577 /*
2578 * Look up the group leader (we will attach this counter to it):
Ingo Molnar04289bb2008-12-11 08:38:42 +01002579 */
2580 group_leader = NULL;
2581 if (group_fd != -1) {
2582 ret = -EINVAL;
2583 group_file = fget_light(group_fd, &fput_needed);
2584 if (!group_file)
Ingo Molnarccff2862008-12-11 11:26:29 +01002585 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01002586 if (group_file->f_op != &perf_fops)
Ingo Molnarccff2862008-12-11 11:26:29 +01002587 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01002588
2589 group_leader = group_file->private_data;
2590 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01002591 * Do not allow a recursive hierarchy (this new sibling
2592 * becoming part of another group-sibling):
Ingo Molnar04289bb2008-12-11 08:38:42 +01002593 */
Ingo Molnarccff2862008-12-11 11:26:29 +01002594 if (group_leader->group_leader != group_leader)
2595 goto err_put_context;
2596 /*
2597 * Do not allow to attach to a group in a different
2598 * task or CPU context:
2599 */
2600 if (group_leader->ctx != ctx)
2601 goto err_put_context;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11002602 /*
2603 * Only a group leader can be exclusive or pinned
2604 */
2605 if (hw_event.exclusive || hw_event.pinned)
2606 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01002607 }
2608
Paul Mackerras23a185c2009-02-09 22:42:47 +11002609 counter = perf_counter_alloc(&hw_event, cpu, ctx, group_leader,
2610 GFP_KERNEL);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02002611 ret = PTR_ERR(counter);
2612 if (IS_ERR(counter))
Thomas Gleixner0793a612008-12-04 20:12:29 +01002613 goto err_put_context;
2614
Thomas Gleixner0793a612008-12-04 20:12:29 +01002615 ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
2616 if (ret < 0)
Ingo Molnar9b51f662008-12-12 13:49:45 +01002617 goto err_free_put_context;
2618
2619 counter_file = fget_light(ret, &fput_needed2);
2620 if (!counter_file)
2621 goto err_free_put_context;
2622
2623 counter->filp = counter_file;
Paul Mackerrasd859e292009-01-17 18:10:22 +11002624 mutex_lock(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01002625 perf_install_in_context(ctx, counter, cpu);
Paul Mackerrasd859e292009-01-17 18:10:22 +11002626 mutex_unlock(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01002627
2628 fput_light(counter_file, fput_needed2);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002629
Ingo Molnar04289bb2008-12-11 08:38:42 +01002630out_fput:
2631 fput_light(group_file, fput_needed);
2632
Thomas Gleixner0793a612008-12-04 20:12:29 +01002633 return ret;
2634
Ingo Molnar9b51f662008-12-12 13:49:45 +01002635err_free_put_context:
Thomas Gleixner0793a612008-12-04 20:12:29 +01002636 kfree(counter);
2637
2638err_put_context:
2639 put_context(ctx);
2640
Ingo Molnar04289bb2008-12-11 08:38:42 +01002641 goto out_fput;
Thomas Gleixner0793a612008-12-04 20:12:29 +01002642}
2643
Ingo Molnar9b51f662008-12-12 13:49:45 +01002644/*
2645 * Initialize the perf_counter context in a task_struct:
2646 */
2647static void
2648__perf_counter_init_context(struct perf_counter_context *ctx,
2649 struct task_struct *task)
2650{
2651 memset(ctx, 0, sizeof(*ctx));
2652 spin_lock_init(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +11002653 mutex_init(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01002654 INIT_LIST_HEAD(&ctx->counter_list);
Peter Zijlstra592903c2009-03-13 12:21:36 +01002655 INIT_LIST_HEAD(&ctx->event_list);
Ingo Molnar9b51f662008-12-12 13:49:45 +01002656 ctx->task = task;
2657}
2658
2659/*
2660 * inherit a counter from parent task to child task:
2661 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11002662static struct perf_counter *
Ingo Molnar9b51f662008-12-12 13:49:45 +01002663inherit_counter(struct perf_counter *parent_counter,
2664 struct task_struct *parent,
2665 struct perf_counter_context *parent_ctx,
2666 struct task_struct *child,
Paul Mackerrasd859e292009-01-17 18:10:22 +11002667 struct perf_counter *group_leader,
Ingo Molnar9b51f662008-12-12 13:49:45 +01002668 struct perf_counter_context *child_ctx)
2669{
2670 struct perf_counter *child_counter;
2671
Paul Mackerrasd859e292009-01-17 18:10:22 +11002672 /*
2673 * Instead of creating recursive hierarchies of counters,
2674 * we link inherited counters back to the original parent,
2675 * which has a filp for sure, which we use as the reference
2676 * count:
2677 */
2678 if (parent_counter->parent)
2679 parent_counter = parent_counter->parent;
2680
Ingo Molnar9b51f662008-12-12 13:49:45 +01002681 child_counter = perf_counter_alloc(&parent_counter->hw_event,
Paul Mackerras23a185c2009-02-09 22:42:47 +11002682 parent_counter->cpu, child_ctx,
2683 group_leader, GFP_KERNEL);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02002684 if (IS_ERR(child_counter))
2685 return child_counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01002686
2687 /*
2688 * Link it up in the child's context:
2689 */
Ingo Molnar9b51f662008-12-12 13:49:45 +01002690 child_counter->task = child;
Paul Mackerras53cfbf52009-03-25 22:46:58 +11002691 add_counter_to_ctx(child_counter, child_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01002692
2693 child_counter->parent = parent_counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01002694 /*
2695 * inherit into child's child as well:
2696 */
2697 child_counter->hw_event.inherit = 1;
2698
2699 /*
2700 * Get a reference to the parent filp - we will fput it
2701 * when the child counter exits. This is safe to do because
2702 * we are in the parent and we know that the filp still
2703 * exists and has a nonzero count:
2704 */
2705 atomic_long_inc(&parent_counter->filp->f_count);
2706
Paul Mackerrasd859e292009-01-17 18:10:22 +11002707 /*
2708 * Link this into the parent counter's child list
2709 */
2710 mutex_lock(&parent_counter->mutex);
2711 list_add_tail(&child_counter->child_list, &parent_counter->child_list);
2712
2713 /*
2714 * Make the child state follow the state of the parent counter,
2715 * not its hw_event.disabled bit. We hold the parent's mutex,
2716 * so we won't race with perf_counter_{en,dis}able_family.
2717 */
2718 if (parent_counter->state >= PERF_COUNTER_STATE_INACTIVE)
2719 child_counter->state = PERF_COUNTER_STATE_INACTIVE;
2720 else
2721 child_counter->state = PERF_COUNTER_STATE_OFF;
2722
2723 mutex_unlock(&parent_counter->mutex);
2724
2725 return child_counter;
2726}
2727
2728static int inherit_group(struct perf_counter *parent_counter,
2729 struct task_struct *parent,
2730 struct perf_counter_context *parent_ctx,
2731 struct task_struct *child,
2732 struct perf_counter_context *child_ctx)
2733{
2734 struct perf_counter *leader;
2735 struct perf_counter *sub;
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02002736 struct perf_counter *child_ctr;
Paul Mackerrasd859e292009-01-17 18:10:22 +11002737
2738 leader = inherit_counter(parent_counter, parent, parent_ctx,
2739 child, NULL, child_ctx);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02002740 if (IS_ERR(leader))
2741 return PTR_ERR(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +11002742 list_for_each_entry(sub, &parent_counter->sibling_list, list_entry) {
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02002743 child_ctr = inherit_counter(sub, parent, parent_ctx,
2744 child, leader, child_ctx);
2745 if (IS_ERR(child_ctr))
2746 return PTR_ERR(child_ctr);
Paul Mackerrasd859e292009-01-17 18:10:22 +11002747 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01002748 return 0;
2749}
2750
Paul Mackerrasd859e292009-01-17 18:10:22 +11002751static void sync_child_counter(struct perf_counter *child_counter,
2752 struct perf_counter *parent_counter)
2753{
2754 u64 parent_val, child_val;
2755
2756 parent_val = atomic64_read(&parent_counter->count);
2757 child_val = atomic64_read(&child_counter->count);
2758
2759 /*
2760 * Add back the child's count to the parent's count:
2761 */
2762 atomic64_add(child_val, &parent_counter->count);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11002763 atomic64_add(child_counter->total_time_enabled,
2764 &parent_counter->child_total_time_enabled);
2765 atomic64_add(child_counter->total_time_running,
2766 &parent_counter->child_total_time_running);
Paul Mackerrasd859e292009-01-17 18:10:22 +11002767
2768 /*
2769 * Remove this counter from the parent's list
2770 */
2771 mutex_lock(&parent_counter->mutex);
2772 list_del_init(&child_counter->child_list);
2773 mutex_unlock(&parent_counter->mutex);
2774
2775 /*
2776 * Release the parent counter, if this was the last
2777 * reference to it.
2778 */
2779 fput(parent_counter->filp);
2780}
2781
Ingo Molnar9b51f662008-12-12 13:49:45 +01002782static void
2783__perf_counter_exit_task(struct task_struct *child,
2784 struct perf_counter *child_counter,
2785 struct perf_counter_context *child_ctx)
2786{
2787 struct perf_counter *parent_counter;
Paul Mackerrasd859e292009-01-17 18:10:22 +11002788 struct perf_counter *sub, *tmp;
Ingo Molnar9b51f662008-12-12 13:49:45 +01002789
2790 /*
Ingo Molnar235c7fc2008-12-21 14:43:25 +01002791 * If we do not self-reap then we have to wait for the
2792 * child task to unschedule (it will happen for sure),
2793 * so that its counter is at its final count. (This
2794 * condition triggers rarely - child tasks usually get
2795 * off their CPU before the parent has a chance to
2796 * get this far into the reaping action)
Ingo Molnar9b51f662008-12-12 13:49:45 +01002797 */
Ingo Molnar235c7fc2008-12-21 14:43:25 +01002798 if (child != current) {
2799 wait_task_inactive(child, 0);
2800 list_del_init(&child_counter->list_entry);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11002801 update_counter_times(child_counter);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01002802 } else {
Ingo Molnar0cc0c022008-12-14 23:20:36 +01002803 struct perf_cpu_context *cpuctx;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01002804 unsigned long flags;
2805 u64 perf_flags;
2806
2807 /*
2808 * Disable and unlink this counter.
2809 *
2810 * Be careful about zapping the list - IRQ/NMI context
2811 * could still be processing it:
2812 */
2813 curr_rq_lock_irq_save(&flags);
2814 perf_flags = hw_perf_save_disable();
Ingo Molnar0cc0c022008-12-14 23:20:36 +01002815
2816 cpuctx = &__get_cpu_var(perf_cpu_context);
2817
Paul Mackerrasd859e292009-01-17 18:10:22 +11002818 group_sched_out(child_counter, cpuctx, child_ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11002819 update_counter_times(child_counter);
Ingo Molnar0cc0c022008-12-14 23:20:36 +01002820
Ingo Molnar235c7fc2008-12-21 14:43:25 +01002821 list_del_init(&child_counter->list_entry);
2822
2823 child_ctx->nr_counters--;
2824
2825 hw_perf_restore(perf_flags);
2826 curr_rq_unlock_irq_restore(&flags);
Ingo Molnar0cc0c022008-12-14 23:20:36 +01002827 }
2828
Ingo Molnar9b51f662008-12-12 13:49:45 +01002829 parent_counter = child_counter->parent;
2830 /*
2831 * It can happen that parent exits first, and has counters
2832 * that are still around due to the child reference. These
2833 * counters need to be zapped - but otherwise linger.
2834 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11002835 if (parent_counter) {
2836 sync_child_counter(child_counter, parent_counter);
2837 list_for_each_entry_safe(sub, tmp, &child_counter->sibling_list,
2838 list_entry) {
Paul Mackerras4bcf3492009-02-11 13:53:19 +01002839 if (sub->parent) {
Paul Mackerrasd859e292009-01-17 18:10:22 +11002840 sync_child_counter(sub, sub->parent);
Peter Zijlstraf1600952009-03-19 20:26:16 +01002841 free_counter(sub);
Paul Mackerras4bcf3492009-02-11 13:53:19 +01002842 }
Paul Mackerrasd859e292009-01-17 18:10:22 +11002843 }
Peter Zijlstraf1600952009-03-19 20:26:16 +01002844 free_counter(child_counter);
Paul Mackerras4bcf3492009-02-11 13:53:19 +01002845 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01002846}
2847
2848/*
Paul Mackerrasd859e292009-01-17 18:10:22 +11002849 * When a child task exits, feed back counter values to parent counters.
Ingo Molnar9b51f662008-12-12 13:49:45 +01002850 *
Paul Mackerrasd859e292009-01-17 18:10:22 +11002851 * Note: we may be running in child context, but the PID is not hashed
Ingo Molnar9b51f662008-12-12 13:49:45 +01002852 * anymore so new counters will not be added.
2853 */
2854void perf_counter_exit_task(struct task_struct *child)
2855{
2856 struct perf_counter *child_counter, *tmp;
2857 struct perf_counter_context *child_ctx;
2858
2859 child_ctx = &child->perf_counter_ctx;
2860
2861 if (likely(!child_ctx->nr_counters))
2862 return;
2863
2864 list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list,
2865 list_entry)
2866 __perf_counter_exit_task(child, child_counter, child_ctx);
2867}
2868
2869/*
2870 * Initialize the perf_counter context in task_struct
2871 */
2872void perf_counter_init_task(struct task_struct *child)
2873{
2874 struct perf_counter_context *child_ctx, *parent_ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +11002875 struct perf_counter *counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01002876 struct task_struct *parent = current;
Ingo Molnar9b51f662008-12-12 13:49:45 +01002877
2878 child_ctx = &child->perf_counter_ctx;
2879 parent_ctx = &parent->perf_counter_ctx;
2880
2881 __perf_counter_init_context(child_ctx, child);
2882
2883 /*
2884 * This is executed from the parent task context, so inherit
2885 * counters that have been marked for cloning:
2886 */
2887
2888 if (likely(!parent_ctx->nr_counters))
2889 return;
2890
2891 /*
2892 * Lock the parent list. No need to lock the child - not PID
2893 * hashed yet and not running, so nobody can access it.
2894 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11002895 mutex_lock(&parent_ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01002896
2897 /*
2898 * We dont have to disable NMIs - we are only looking at
2899 * the list, not manipulating it:
2900 */
2901 list_for_each_entry(counter, &parent_ctx->counter_list, list_entry) {
Paul Mackerrasd859e292009-01-17 18:10:22 +11002902 if (!counter->hw_event.inherit)
Ingo Molnar9b51f662008-12-12 13:49:45 +01002903 continue;
2904
Paul Mackerrasd859e292009-01-17 18:10:22 +11002905 if (inherit_group(counter, parent,
Ingo Molnar9b51f662008-12-12 13:49:45 +01002906 parent_ctx, child, child_ctx))
2907 break;
2908 }
2909
Paul Mackerrasd859e292009-01-17 18:10:22 +11002910 mutex_unlock(&parent_ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01002911}
2912
Ingo Molnar04289bb2008-12-11 08:38:42 +01002913static void __cpuinit perf_counter_init_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01002914{
Ingo Molnar04289bb2008-12-11 08:38:42 +01002915 struct perf_cpu_context *cpuctx;
Thomas Gleixner0793a612008-12-04 20:12:29 +01002916
Ingo Molnar04289bb2008-12-11 08:38:42 +01002917 cpuctx = &per_cpu(perf_cpu_context, cpu);
2918 __perf_counter_init_context(&cpuctx->ctx, NULL);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002919
2920 mutex_lock(&perf_resource_mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01002921 cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu;
Thomas Gleixner0793a612008-12-04 20:12:29 +01002922 mutex_unlock(&perf_resource_mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01002923
Paul Mackerras01d02872009-01-14 13:44:19 +11002924 hw_perf_counter_setup(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002925}
2926
2927#ifdef CONFIG_HOTPLUG_CPU
Ingo Molnar04289bb2008-12-11 08:38:42 +01002928static void __perf_counter_exit_cpu(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01002929{
2930 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
2931 struct perf_counter_context *ctx = &cpuctx->ctx;
2932 struct perf_counter *counter, *tmp;
2933
Ingo Molnar04289bb2008-12-11 08:38:42 +01002934 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry)
2935 __perf_counter_remove_from_context(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002936}
Ingo Molnar04289bb2008-12-11 08:38:42 +01002937static void perf_counter_exit_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01002938{
Paul Mackerrasd859e292009-01-17 18:10:22 +11002939 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
2940 struct perf_counter_context *ctx = &cpuctx->ctx;
2941
2942 mutex_lock(&ctx->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01002943 smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1);
Paul Mackerrasd859e292009-01-17 18:10:22 +11002944 mutex_unlock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002945}
2946#else
Ingo Molnar04289bb2008-12-11 08:38:42 +01002947static inline void perf_counter_exit_cpu(int cpu) { }
Thomas Gleixner0793a612008-12-04 20:12:29 +01002948#endif
2949
2950static int __cpuinit
2951perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
2952{
2953 unsigned int cpu = (long)hcpu;
2954
2955 switch (action) {
2956
2957 case CPU_UP_PREPARE:
2958 case CPU_UP_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01002959 perf_counter_init_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002960 break;
2961
2962 case CPU_DOWN_PREPARE:
2963 case CPU_DOWN_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01002964 perf_counter_exit_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002965 break;
2966
2967 default:
2968 break;
2969 }
2970
2971 return NOTIFY_OK;
2972}
2973
2974static struct notifier_block __cpuinitdata perf_cpu_nb = {
2975 .notifier_call = perf_cpu_notify,
2976};
2977
2978static int __init perf_counter_init(void)
2979{
2980 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
2981 (void *)(long)smp_processor_id());
2982 register_cpu_notifier(&perf_cpu_nb);
2983
2984 return 0;
2985}
2986early_initcall(perf_counter_init);
2987
2988static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
2989{
2990 return sprintf(buf, "%d\n", perf_reserved_percpu);
2991}
2992
2993static ssize_t
2994perf_set_reserve_percpu(struct sysdev_class *class,
2995 const char *buf,
2996 size_t count)
2997{
2998 struct perf_cpu_context *cpuctx;
2999 unsigned long val;
3000 int err, cpu, mpt;
3001
3002 err = strict_strtoul(buf, 10, &val);
3003 if (err)
3004 return err;
3005 if (val > perf_max_counters)
3006 return -EINVAL;
3007
3008 mutex_lock(&perf_resource_mutex);
3009 perf_reserved_percpu = val;
3010 for_each_online_cpu(cpu) {
3011 cpuctx = &per_cpu(perf_cpu_context, cpu);
3012 spin_lock_irq(&cpuctx->ctx.lock);
3013 mpt = min(perf_max_counters - cpuctx->ctx.nr_counters,
3014 perf_max_counters - perf_reserved_percpu);
3015 cpuctx->max_pertask = mpt;
3016 spin_unlock_irq(&cpuctx->ctx.lock);
3017 }
3018 mutex_unlock(&perf_resource_mutex);
3019
3020 return count;
3021}
3022
3023static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
3024{
3025 return sprintf(buf, "%d\n", perf_overcommit);
3026}
3027
3028static ssize_t
3029perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
3030{
3031 unsigned long val;
3032 int err;
3033
3034 err = strict_strtoul(buf, 10, &val);
3035 if (err)
3036 return err;
3037 if (val > 1)
3038 return -EINVAL;
3039
3040 mutex_lock(&perf_resource_mutex);
3041 perf_overcommit = val;
3042 mutex_unlock(&perf_resource_mutex);
3043
3044 return count;
3045}
3046
3047static SYSDEV_CLASS_ATTR(
3048 reserve_percpu,
3049 0644,
3050 perf_show_reserve_percpu,
3051 perf_set_reserve_percpu
3052 );
3053
3054static SYSDEV_CLASS_ATTR(
3055 overcommit,
3056 0644,
3057 perf_show_overcommit,
3058 perf_set_overcommit
3059 );
3060
3061static struct attribute *perfclass_attrs[] = {
3062 &attr_reserve_percpu.attr,
3063 &attr_overcommit.attr,
3064 NULL
3065};
3066
3067static struct attribute_group perfclass_attr_group = {
3068 .attrs = perfclass_attrs,
3069 .name = "perf_counters",
3070};
3071
3072static int __init perf_counter_sysfs_init(void)
3073{
3074 return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
3075 &perfclass_attr_group);
3076}
3077device_initcall(perf_counter_sysfs_init);