blob: 2d13427383051fdb2fa421be6ecfd787aff34452 [file] [log] [blame]
Thomas Gleixner0793a612008-12-04 20:12:29 +01001/*
2 * Performance counter core code
3 *
Ingo Molnar98144512009-04-29 14:52:50 +02004 * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
5 * Copyright (C) 2008-2009 Red Hat, Inc., Ingo Molnar
6 * Copyright (C) 2008-2009 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
Paul Mackerrasc5dd0162009-04-30 09:48:16 +10007 * Copyright © 2009 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
Peter Zijlstra7b732a72009-03-23 18:22:10 +01008 *
9 * For licensing details see kernel-base/COPYING
Thomas Gleixner0793a612008-12-04 20:12:29 +010010 */
11
12#include <linux/fs.h>
Peter Zijlstrab9cacc72009-03-25 12:30:22 +010013#include <linux/mm.h>
Thomas Gleixner0793a612008-12-04 20:12:29 +010014#include <linux/cpu.h>
15#include <linux/smp.h>
Ingo Molnar04289bb2008-12-11 08:38:42 +010016#include <linux/file.h>
Thomas Gleixner0793a612008-12-04 20:12:29 +010017#include <linux/poll.h>
18#include <linux/sysfs.h>
19#include <linux/ptrace.h>
20#include <linux/percpu.h>
Peter Zijlstrab9cacc72009-03-25 12:30:22 +010021#include <linux/vmstat.h>
22#include <linux/hardirq.h>
23#include <linux/rculist.h>
Thomas Gleixner0793a612008-12-04 20:12:29 +010024#include <linux/uaccess.h>
25#include <linux/syscalls.h>
26#include <linux/anon_inodes.h>
Ingo Molnaraa9c4c02008-12-17 14:10:57 +010027#include <linux/kernel_stat.h>
Thomas Gleixner0793a612008-12-04 20:12:29 +010028#include <linux/perf_counter.h>
Peter Zijlstra0a4a9392009-03-30 19:07:05 +020029#include <linux/dcache.h>
Thomas Gleixner0793a612008-12-04 20:12:29 +010030
Tim Blechmann4e193bd2009-03-14 14:29:25 +010031#include <asm/irq_regs.h>
32
Thomas Gleixner0793a612008-12-04 20:12:29 +010033/*
34 * Each CPU has a list of per CPU counters:
35 */
36DEFINE_PER_CPU(struct perf_cpu_context, perf_cpu_context);
37
Ingo Molnar088e2852008-12-14 20:21:00 +010038int perf_max_counters __read_mostly = 1;
Thomas Gleixner0793a612008-12-04 20:12:29 +010039static int perf_reserved_percpu __read_mostly;
40static int perf_overcommit __read_mostly = 1;
41
Peter Zijlstra9ee318a2009-04-09 10:53:44 +020042static atomic_t nr_mmap_tracking __read_mostly;
43static atomic_t nr_munmap_tracking __read_mostly;
44static atomic_t nr_comm_tracking __read_mostly;
45
Peter Zijlstra1ccd1542009-04-09 10:53:45 +020046int sysctl_perf_counter_priv __read_mostly; /* do we need to be privileged */
Peter Zijlstrac5078f72009-05-05 17:50:24 +020047int sysctl_perf_counter_mlock __read_mostly = 128; /* 'free' kb per counter */
Peter Zijlstra1ccd1542009-04-09 10:53:45 +020048
Thomas Gleixner0793a612008-12-04 20:12:29 +010049/*
Ingo Molnar1dce8d92009-05-04 19:23:18 +020050 * Lock for (sysadmin-configurable) counter reservations:
Thomas Gleixner0793a612008-12-04 20:12:29 +010051 */
Ingo Molnar1dce8d92009-05-04 19:23:18 +020052static DEFINE_SPINLOCK(perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +010053
54/*
55 * Architecture provided APIs - weak aliases:
56 */
Robert Richter4aeb0b42009-04-29 12:47:03 +020057extern __weak const struct pmu *hw_perf_counter_init(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +010058{
Paul Mackerrasff6f0542009-01-09 16:19:25 +110059 return NULL;
Thomas Gleixner0793a612008-12-04 20:12:29 +010060}
61
Ingo Molnar01b28382008-12-11 13:45:51 +010062u64 __weak hw_perf_save_disable(void) { return 0; }
Yinghai Lu01ea1cc2008-12-26 21:05:06 -080063void __weak hw_perf_restore(u64 ctrl) { barrier(); }
Paul Mackerras01d02872009-01-14 13:44:19 +110064void __weak hw_perf_counter_setup(int cpu) { barrier(); }
Paul Mackerras3cbed422009-01-09 16:43:42 +110065int __weak hw_perf_group_sched_in(struct perf_counter *group_leader,
66 struct perf_cpu_context *cpuctx,
67 struct perf_counter_context *ctx, int cpu)
68{
69 return 0;
70}
Thomas Gleixner0793a612008-12-04 20:12:29 +010071
Paul Mackerras4eb96fc2009-01-09 17:24:34 +110072void __weak perf_counter_print_debug(void) { }
73
Ingo Molnar04289bb2008-12-11 08:38:42 +010074static void
75list_add_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
76{
77 struct perf_counter *group_leader = counter->group_leader;
78
79 /*
80 * Depending on whether it is a standalone or sibling counter,
81 * add it straight to the context's counter list, or to the group
82 * leader's sibling list:
83 */
84 if (counter->group_leader == counter)
85 list_add_tail(&counter->list_entry, &ctx->counter_list);
Peter Zijlstra5c148192009-03-25 12:30:23 +010086 else {
Ingo Molnar04289bb2008-12-11 08:38:42 +010087 list_add_tail(&counter->list_entry, &group_leader->sibling_list);
Peter Zijlstra5c148192009-03-25 12:30:23 +010088 group_leader->nr_siblings++;
89 }
Peter Zijlstra592903c2009-03-13 12:21:36 +010090
91 list_add_rcu(&counter->event_entry, &ctx->event_list);
Ingo Molnar04289bb2008-12-11 08:38:42 +010092}
93
94static void
95list_del_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
96{
97 struct perf_counter *sibling, *tmp;
98
99 list_del_init(&counter->list_entry);
Peter Zijlstra592903c2009-03-13 12:21:36 +0100100 list_del_rcu(&counter->event_entry);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100101
Peter Zijlstra5c148192009-03-25 12:30:23 +0100102 if (counter->group_leader != counter)
103 counter->group_leader->nr_siblings--;
104
Ingo Molnar04289bb2008-12-11 08:38:42 +0100105 /*
106 * If this was a group counter with sibling counters then
107 * upgrade the siblings to singleton counters by adding them
108 * to the context list directly:
109 */
110 list_for_each_entry_safe(sibling, tmp,
111 &counter->sibling_list, list_entry) {
112
Peter Zijlstra75564232009-03-13 12:21:29 +0100113 list_move_tail(&sibling->list_entry, &ctx->counter_list);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100114 sibling->group_leader = sibling;
115 }
116}
117
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100118static void
119counter_sched_out(struct perf_counter *counter,
120 struct perf_cpu_context *cpuctx,
121 struct perf_counter_context *ctx)
122{
123 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
124 return;
125
126 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200127 counter->tstamp_stopped = ctx->time;
Robert Richter4aeb0b42009-04-29 12:47:03 +0200128 counter->pmu->disable(counter);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100129 counter->oncpu = -1;
130
131 if (!is_software_counter(counter))
132 cpuctx->active_oncpu--;
133 ctx->nr_active--;
134 if (counter->hw_event.exclusive || !cpuctx->active_oncpu)
135 cpuctx->exclusive = 0;
136}
137
Paul Mackerrasd859e292009-01-17 18:10:22 +1100138static void
139group_sched_out(struct perf_counter *group_counter,
140 struct perf_cpu_context *cpuctx,
141 struct perf_counter_context *ctx)
142{
143 struct perf_counter *counter;
144
145 if (group_counter->state != PERF_COUNTER_STATE_ACTIVE)
146 return;
147
148 counter_sched_out(group_counter, cpuctx, ctx);
149
150 /*
151 * Schedule out siblings (if any):
152 */
153 list_for_each_entry(counter, &group_counter->sibling_list, list_entry)
154 counter_sched_out(counter, cpuctx, ctx);
155
156 if (group_counter->hw_event.exclusive)
157 cpuctx->exclusive = 0;
158}
159
Thomas Gleixner0793a612008-12-04 20:12:29 +0100160/*
161 * Cross CPU call to remove a performance counter
162 *
163 * We disable the counter on the hardware level first. After that we
164 * remove it from the context list.
165 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100166static void __perf_counter_remove_from_context(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100167{
168 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
169 struct perf_counter *counter = info;
170 struct perf_counter_context *ctx = counter->ctx;
Ingo Molnar9b51f662008-12-12 13:49:45 +0100171 unsigned long flags;
Ingo Molnar5c92d122008-12-11 13:21:10 +0100172 u64 perf_flags;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100173
174 /*
175 * If this is a task context, we need to check whether it is
176 * the current task context of this cpu. If not it has been
177 * scheduled out before the smp call arrived.
178 */
179 if (ctx->task && cpuctx->task_ctx != ctx)
180 return;
181
Peter Zijlstra849691a2009-04-06 11:45:12 +0200182 spin_lock_irqsave(&ctx->lock, flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100183
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100184 counter_sched_out(counter, cpuctx, ctx);
185
186 counter->task = NULL;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100187 ctx->nr_counters--;
188
189 /*
190 * Protect the list operation against NMI by disabling the
191 * counters on a global level. NOP for non NMI based counters.
192 */
Ingo Molnar01b28382008-12-11 13:45:51 +0100193 perf_flags = hw_perf_save_disable();
Ingo Molnar04289bb2008-12-11 08:38:42 +0100194 list_del_counter(counter, ctx);
Ingo Molnar01b28382008-12-11 13:45:51 +0100195 hw_perf_restore(perf_flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100196
197 if (!ctx->task) {
198 /*
199 * Allow more per task counters with respect to the
200 * reservation:
201 */
202 cpuctx->max_pertask =
203 min(perf_max_counters - ctx->nr_counters,
204 perf_max_counters - perf_reserved_percpu);
205 }
206
Peter Zijlstra849691a2009-04-06 11:45:12 +0200207 spin_unlock_irqrestore(&ctx->lock, flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100208}
209
210
211/*
212 * Remove the counter from a task's (or a CPU's) list of counters.
213 *
Paul Mackerrasd859e292009-01-17 18:10:22 +1100214 * Must be called with counter->mutex and ctx->mutex held.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100215 *
216 * CPU counters are removed with a smp call. For task counters we only
217 * call when the task is on a CPU.
218 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100219static void perf_counter_remove_from_context(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100220{
221 struct perf_counter_context *ctx = counter->ctx;
222 struct task_struct *task = ctx->task;
223
224 if (!task) {
225 /*
226 * Per cpu counters are removed via an smp call and
227 * the removal is always sucessful.
228 */
229 smp_call_function_single(counter->cpu,
Ingo Molnar04289bb2008-12-11 08:38:42 +0100230 __perf_counter_remove_from_context,
Thomas Gleixner0793a612008-12-04 20:12:29 +0100231 counter, 1);
232 return;
233 }
234
235retry:
Ingo Molnar04289bb2008-12-11 08:38:42 +0100236 task_oncpu_function_call(task, __perf_counter_remove_from_context,
Thomas Gleixner0793a612008-12-04 20:12:29 +0100237 counter);
238
239 spin_lock_irq(&ctx->lock);
240 /*
241 * If the context is active we need to retry the smp call.
242 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100243 if (ctx->nr_active && !list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100244 spin_unlock_irq(&ctx->lock);
245 goto retry;
246 }
247
248 /*
249 * The lock prevents that this context is scheduled in so we
Ingo Molnar04289bb2008-12-11 08:38:42 +0100250 * can remove the counter safely, if the call above did not
Thomas Gleixner0793a612008-12-04 20:12:29 +0100251 * succeed.
252 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100253 if (!list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100254 ctx->nr_counters--;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100255 list_del_counter(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100256 counter->task = NULL;
257 }
258 spin_unlock_irq(&ctx->lock);
259}
260
Peter Zijlstra4af49982009-04-06 11:45:10 +0200261static inline u64 perf_clock(void)
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100262{
Peter Zijlstra4af49982009-04-06 11:45:10 +0200263 return cpu_clock(smp_processor_id());
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100264}
265
266/*
267 * Update the record of the current time in a context.
268 */
Peter Zijlstra4af49982009-04-06 11:45:10 +0200269static void update_context_time(struct perf_counter_context *ctx)
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100270{
Peter Zijlstra4af49982009-04-06 11:45:10 +0200271 u64 now = perf_clock();
272
273 ctx->time += now - ctx->timestamp;
274 ctx->timestamp = now;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100275}
276
277/*
278 * Update the total_time_enabled and total_time_running fields for a counter.
279 */
280static void update_counter_times(struct perf_counter *counter)
281{
282 struct perf_counter_context *ctx = counter->ctx;
283 u64 run_end;
284
Peter Zijlstra4af49982009-04-06 11:45:10 +0200285 if (counter->state < PERF_COUNTER_STATE_INACTIVE)
286 return;
287
288 counter->total_time_enabled = ctx->time - counter->tstamp_enabled;
289
290 if (counter->state == PERF_COUNTER_STATE_INACTIVE)
291 run_end = counter->tstamp_stopped;
292 else
293 run_end = ctx->time;
294
295 counter->total_time_running = run_end - counter->tstamp_running;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100296}
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
Peter Zijlstra849691a2009-04-06 11:45:12 +0200327 spin_lock_irqsave(&ctx->lock, flags);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100328
329 /*
330 * If the counter is on, turn it off.
331 * If it is in error state, leave it in error state.
332 */
333 if (counter->state >= PERF_COUNTER_STATE_INACTIVE) {
Peter Zijlstra4af49982009-04-06 11:45:10 +0200334 update_context_time(ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100335 update_counter_times(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100336 if (counter == counter->group_leader)
337 group_sched_out(counter, cpuctx, ctx);
338 else
339 counter_sched_out(counter, cpuctx, ctx);
340 counter->state = PERF_COUNTER_STATE_OFF;
341 }
342
Peter Zijlstra849691a2009-04-06 11:45:12 +0200343 spin_unlock_irqrestore(&ctx->lock, flags);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100344}
345
346/*
347 * Disable a counter.
348 */
349static void perf_counter_disable(struct perf_counter *counter)
350{
351 struct perf_counter_context *ctx = counter->ctx;
352 struct task_struct *task = ctx->task;
353
354 if (!task) {
355 /*
356 * Disable the counter on the cpu that it's on
357 */
358 smp_call_function_single(counter->cpu, __perf_counter_disable,
359 counter, 1);
360 return;
361 }
362
363 retry:
364 task_oncpu_function_call(task, __perf_counter_disable, counter);
365
366 spin_lock_irq(&ctx->lock);
367 /*
368 * If the counter is still active, we need to retry the cross-call.
369 */
370 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
371 spin_unlock_irq(&ctx->lock);
372 goto retry;
373 }
374
375 /*
376 * Since we have the lock this context can't be scheduled
377 * in, so we can change the state safely.
378 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100379 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
380 update_counter_times(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100381 counter->state = PERF_COUNTER_STATE_OFF;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100382 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100383
384 spin_unlock_irq(&ctx->lock);
385}
386
387/*
388 * Disable a counter and all its children.
389 */
390static void perf_counter_disable_family(struct perf_counter *counter)
391{
392 struct perf_counter *child;
393
394 perf_counter_disable(counter);
395
396 /*
397 * Lock the mutex to protect the list of children
398 */
399 mutex_lock(&counter->mutex);
400 list_for_each_entry(child, &counter->child_list, child_list)
401 perf_counter_disable(child);
402 mutex_unlock(&counter->mutex);
403}
404
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100405static int
406counter_sched_in(struct perf_counter *counter,
407 struct perf_cpu_context *cpuctx,
408 struct perf_counter_context *ctx,
409 int cpu)
410{
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100411 if (counter->state <= PERF_COUNTER_STATE_OFF)
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100412 return 0;
413
414 counter->state = PERF_COUNTER_STATE_ACTIVE;
415 counter->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */
416 /*
417 * The new state must be visible before we turn it on in the hardware:
418 */
419 smp_wmb();
420
Robert Richter4aeb0b42009-04-29 12:47:03 +0200421 if (counter->pmu->enable(counter)) {
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100422 counter->state = PERF_COUNTER_STATE_INACTIVE;
423 counter->oncpu = -1;
424 return -EAGAIN;
425 }
426
Peter Zijlstra4af49982009-04-06 11:45:10 +0200427 counter->tstamp_running += ctx->time - counter->tstamp_stopped;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100428
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100429 if (!is_software_counter(counter))
430 cpuctx->active_oncpu++;
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100431 ctx->nr_active++;
432
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100433 if (counter->hw_event.exclusive)
434 cpuctx->exclusive = 1;
435
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100436 return 0;
437}
438
Thomas Gleixner0793a612008-12-04 20:12:29 +0100439/*
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100440 * Return 1 for a group consisting entirely of software counters,
441 * 0 if the group contains any hardware counters.
442 */
443static int is_software_only_group(struct perf_counter *leader)
444{
445 struct perf_counter *counter;
446
447 if (!is_software_counter(leader))
448 return 0;
Peter Zijlstra5c148192009-03-25 12:30:23 +0100449
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100450 list_for_each_entry(counter, &leader->sibling_list, list_entry)
451 if (!is_software_counter(counter))
452 return 0;
Peter Zijlstra5c148192009-03-25 12:30:23 +0100453
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100454 return 1;
455}
456
457/*
458 * Work out whether we can put this counter group on the CPU now.
459 */
460static int group_can_go_on(struct perf_counter *counter,
461 struct perf_cpu_context *cpuctx,
462 int can_add_hw)
463{
464 /*
465 * Groups consisting entirely of software counters can always go on.
466 */
467 if (is_software_only_group(counter))
468 return 1;
469 /*
470 * If an exclusive group is already on, no other hardware
471 * counters can go on.
472 */
473 if (cpuctx->exclusive)
474 return 0;
475 /*
476 * If this group is exclusive and there are already
477 * counters on the CPU, it can't go on.
478 */
479 if (counter->hw_event.exclusive && cpuctx->active_oncpu)
480 return 0;
481 /*
482 * Otherwise, try to add it if all previous groups were able
483 * to go on.
484 */
485 return can_add_hw;
486}
487
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100488static void add_counter_to_ctx(struct perf_counter *counter,
489 struct perf_counter_context *ctx)
490{
491 list_add_counter(counter, ctx);
492 ctx->nr_counters++;
493 counter->prev_state = PERF_COUNTER_STATE_OFF;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200494 counter->tstamp_enabled = ctx->time;
495 counter->tstamp_running = ctx->time;
496 counter->tstamp_stopped = ctx->time;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100497}
498
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100499/*
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100500 * Cross CPU call to install and enable a performance counter
Thomas Gleixner0793a612008-12-04 20:12:29 +0100501 */
502static void __perf_install_in_context(void *info)
503{
504 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
505 struct perf_counter *counter = info;
506 struct perf_counter_context *ctx = counter->ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100507 struct perf_counter *leader = counter->group_leader;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100508 int cpu = smp_processor_id();
Ingo Molnar9b51f662008-12-12 13:49:45 +0100509 unsigned long flags;
Ingo Molnar5c92d122008-12-11 13:21:10 +0100510 u64 perf_flags;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100511 int err;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100512
513 /*
514 * If this is a task context, we need to check whether it is
515 * the current task context of this cpu. If not it has been
516 * scheduled out before the smp call arrived.
517 */
518 if (ctx->task && cpuctx->task_ctx != ctx)
519 return;
520
Peter Zijlstra849691a2009-04-06 11:45:12 +0200521 spin_lock_irqsave(&ctx->lock, flags);
Peter Zijlstra4af49982009-04-06 11:45:10 +0200522 update_context_time(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100523
524 /*
525 * Protect the list operation against NMI by disabling the
526 * counters on a global level. NOP for non NMI based counters.
527 */
Ingo Molnar01b28382008-12-11 13:45:51 +0100528 perf_flags = hw_perf_save_disable();
Thomas Gleixner0793a612008-12-04 20:12:29 +0100529
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100530 add_counter_to_ctx(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100531
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100532 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100533 * Don't put the counter on if it is disabled or if
534 * it is in a group and the group isn't on.
535 */
536 if (counter->state != PERF_COUNTER_STATE_INACTIVE ||
537 (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE))
538 goto unlock;
539
540 /*
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100541 * An exclusive counter can't go on if there are already active
542 * hardware counters, and no hardware counter can go on if there
543 * is already an exclusive counter on.
544 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100545 if (!group_can_go_on(counter, cpuctx, 1))
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100546 err = -EEXIST;
547 else
548 err = counter_sched_in(counter, cpuctx, ctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100549
Paul Mackerrasd859e292009-01-17 18:10:22 +1100550 if (err) {
551 /*
552 * This counter couldn't go on. If it is in a group
553 * then we have to pull the whole group off.
554 * If the counter group is pinned then put it in error state.
555 */
556 if (leader != counter)
557 group_sched_out(leader, cpuctx, ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100558 if (leader->hw_event.pinned) {
559 update_group_times(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100560 leader->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100561 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100562 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100563
564 if (!err && !ctx->task && cpuctx->max_pertask)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100565 cpuctx->max_pertask--;
566
Paul Mackerrasd859e292009-01-17 18:10:22 +1100567 unlock:
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100568 hw_perf_restore(perf_flags);
569
Peter Zijlstra849691a2009-04-06 11:45:12 +0200570 spin_unlock_irqrestore(&ctx->lock, flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100571}
572
573/*
574 * Attach a performance counter to a context
575 *
576 * First we add the counter to the list with the hardware enable bit
577 * in counter->hw_config cleared.
578 *
579 * If the counter is attached to a task which is on a CPU we use a smp
580 * call to enable it in the task context. The task might have been
581 * scheduled away, but we check this in the smp call again.
Paul Mackerrasd859e292009-01-17 18:10:22 +1100582 *
583 * Must be called with ctx->mutex held.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100584 */
585static void
586perf_install_in_context(struct perf_counter_context *ctx,
587 struct perf_counter *counter,
588 int cpu)
589{
590 struct task_struct *task = ctx->task;
591
Thomas Gleixner0793a612008-12-04 20:12:29 +0100592 if (!task) {
593 /*
594 * Per cpu counters are installed via an smp call and
595 * the install is always sucessful.
596 */
597 smp_call_function_single(cpu, __perf_install_in_context,
598 counter, 1);
599 return;
600 }
601
602 counter->task = task;
603retry:
604 task_oncpu_function_call(task, __perf_install_in_context,
605 counter);
606
607 spin_lock_irq(&ctx->lock);
608 /*
Thomas Gleixner0793a612008-12-04 20:12:29 +0100609 * we need to retry the smp call.
610 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100611 if (ctx->is_active && list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100612 spin_unlock_irq(&ctx->lock);
613 goto retry;
614 }
615
616 /*
617 * The lock prevents that this context is scheduled in so we
618 * can add the counter safely, if it the call above did not
619 * succeed.
620 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100621 if (list_empty(&counter->list_entry))
622 add_counter_to_ctx(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100623 spin_unlock_irq(&ctx->lock);
624}
625
Paul Mackerrasd859e292009-01-17 18:10:22 +1100626/*
627 * Cross CPU call to enable a performance counter
628 */
629static void __perf_counter_enable(void *info)
Ingo Molnar04289bb2008-12-11 08:38:42 +0100630{
Paul Mackerrasd859e292009-01-17 18:10:22 +1100631 struct perf_counter *counter = info;
632 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
633 struct perf_counter_context *ctx = counter->ctx;
634 struct perf_counter *leader = counter->group_leader;
635 unsigned long flags;
636 int err;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100637
638 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100639 * If this is a per-task counter, need to check whether this
640 * counter's task is the current task on this cpu.
Ingo Molnar04289bb2008-12-11 08:38:42 +0100641 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100642 if (ctx->task && cpuctx->task_ctx != ctx)
643 return;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100644
Peter Zijlstra849691a2009-04-06 11:45:12 +0200645 spin_lock_irqsave(&ctx->lock, flags);
Peter Zijlstra4af49982009-04-06 11:45:10 +0200646 update_context_time(ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100647
Paul Mackerrasc07c99b2009-02-13 22:10:34 +1100648 counter->prev_state = counter->state;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100649 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
650 goto unlock;
651 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200652 counter->tstamp_enabled = ctx->time - counter->total_time_enabled;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100653
654 /*
655 * If the counter is in a group and isn't the group leader,
656 * then don't put it on unless the group is on.
657 */
658 if (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE)
659 goto unlock;
660
661 if (!group_can_go_on(counter, cpuctx, 1))
662 err = -EEXIST;
663 else
664 err = counter_sched_in(counter, cpuctx, ctx,
665 smp_processor_id());
666
667 if (err) {
668 /*
669 * If this counter can't go on and it's part of a
670 * group, then the whole group has to come off.
671 */
672 if (leader != counter)
673 group_sched_out(leader, cpuctx, ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100674 if (leader->hw_event.pinned) {
675 update_group_times(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100676 leader->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100677 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100678 }
679
680 unlock:
Peter Zijlstra849691a2009-04-06 11:45:12 +0200681 spin_unlock_irqrestore(&ctx->lock, flags);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100682}
683
684/*
685 * Enable a counter.
686 */
687static void perf_counter_enable(struct perf_counter *counter)
688{
689 struct perf_counter_context *ctx = counter->ctx;
690 struct task_struct *task = ctx->task;
691
692 if (!task) {
693 /*
694 * Enable the counter on the cpu that it's on
695 */
696 smp_call_function_single(counter->cpu, __perf_counter_enable,
697 counter, 1);
698 return;
699 }
700
701 spin_lock_irq(&ctx->lock);
702 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
703 goto out;
704
705 /*
706 * If the counter is in error state, clear that first.
707 * That way, if we see the counter in error state below, we
708 * know that it has gone back into error state, as distinct
709 * from the task having been scheduled away before the
710 * cross-call arrived.
711 */
712 if (counter->state == PERF_COUNTER_STATE_ERROR)
713 counter->state = PERF_COUNTER_STATE_OFF;
714
715 retry:
716 spin_unlock_irq(&ctx->lock);
717 task_oncpu_function_call(task, __perf_counter_enable, counter);
718
719 spin_lock_irq(&ctx->lock);
720
721 /*
722 * If the context is active and the counter is still off,
723 * we need to retry the cross-call.
724 */
725 if (ctx->is_active && counter->state == PERF_COUNTER_STATE_OFF)
726 goto retry;
727
728 /*
729 * Since we have the lock this context can't be scheduled
730 * in, so we can change the state safely.
731 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100732 if (counter->state == PERF_COUNTER_STATE_OFF) {
Paul Mackerrasd859e292009-01-17 18:10:22 +1100733 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200734 counter->tstamp_enabled =
735 ctx->time - counter->total_time_enabled;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100736 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100737 out:
738 spin_unlock_irq(&ctx->lock);
739}
740
Peter Zijlstra79f14642009-04-06 11:45:07 +0200741static void perf_counter_refresh(struct perf_counter *counter, int refresh)
742{
743 atomic_add(refresh, &counter->event_limit);
744 perf_counter_enable(counter);
745}
746
Paul Mackerrasd859e292009-01-17 18:10:22 +1100747/*
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;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200775 update_context_time(ctx);
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 Zijlstrabce379b2009-04-06 11:45:13 +0200807 update_context_time(ctx);
808
Peter Zijlstra4a0deca2009-03-19 20:26:12 +0100809 regs = task_pt_regs(task);
Peter Zijlstra78f13e92009-04-08 15:01:33 +0200810 perf_swcounter_event(PERF_COUNT_CONTEXT_SWITCHES, 1, 1, regs, 0);
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100811 __perf_counter_sched_out(ctx, cpuctx);
812
Thomas Gleixner0793a612008-12-04 20:12:29 +0100813 cpuctx->task_ctx = NULL;
814}
815
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100816static void perf_counter_cpu_sched_out(struct perf_cpu_context *cpuctx)
Ingo Molnar04289bb2008-12-11 08:38:42 +0100817{
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100818 __perf_counter_sched_out(&cpuctx->ctx, cpuctx);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100819}
820
Ingo Molnar79958882008-12-17 08:54:56 +0100821static int
Ingo Molnar04289bb2008-12-11 08:38:42 +0100822group_sched_in(struct perf_counter *group_counter,
823 struct perf_cpu_context *cpuctx,
824 struct perf_counter_context *ctx,
825 int cpu)
826{
Ingo Molnar95cdd2e2008-12-21 13:50:42 +0100827 struct perf_counter *counter, *partial_group;
Paul Mackerras3cbed422009-01-09 16:43:42 +1100828 int ret;
829
830 if (group_counter->state == PERF_COUNTER_STATE_OFF)
831 return 0;
832
833 ret = hw_perf_group_sched_in(group_counter, cpuctx, ctx, cpu);
834 if (ret)
835 return ret < 0 ? ret : 0;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100836
Paul Mackerrasc07c99b2009-02-13 22:10:34 +1100837 group_counter->prev_state = group_counter->state;
Ingo Molnar95cdd2e2008-12-21 13:50:42 +0100838 if (counter_sched_in(group_counter, cpuctx, ctx, cpu))
839 return -EAGAIN;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100840
841 /*
842 * Schedule in siblings as one group (if any):
843 */
Ingo Molnar79958882008-12-17 08:54:56 +0100844 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
Paul Mackerrasc07c99b2009-02-13 22:10:34 +1100845 counter->prev_state = counter->state;
Ingo Molnar95cdd2e2008-12-21 13:50:42 +0100846 if (counter_sched_in(counter, cpuctx, ctx, cpu)) {
847 partial_group = counter;
848 goto group_error;
849 }
Ingo Molnar79958882008-12-17 08:54:56 +0100850 }
851
Paul Mackerras3cbed422009-01-09 16:43:42 +1100852 return 0;
Ingo Molnar95cdd2e2008-12-21 13:50:42 +0100853
854group_error:
855 /*
856 * Groups can be scheduled in as one unit only, so undo any
857 * partial group before returning:
858 */
859 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
860 if (counter == partial_group)
861 break;
862 counter_sched_out(counter, cpuctx, ctx);
863 }
864 counter_sched_out(group_counter, cpuctx, ctx);
865
866 return -EAGAIN;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100867}
868
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100869static void
870__perf_counter_sched_in(struct perf_counter_context *ctx,
871 struct perf_cpu_context *cpuctx, int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100872{
Thomas Gleixner0793a612008-12-04 20:12:29 +0100873 struct perf_counter *counter;
Paul Mackerras3cbed422009-01-09 16:43:42 +1100874 u64 flags;
Paul Mackerrasdd0e6ba2009-01-12 15:11:00 +1100875 int can_add_hw = 1;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100876
Thomas Gleixner0793a612008-12-04 20:12:29 +0100877 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100878 ctx->is_active = 1;
879 if (likely(!ctx->nr_counters))
880 goto out;
881
Peter Zijlstra4af49982009-04-06 11:45:10 +0200882 ctx->timestamp = perf_clock();
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100883
Paul Mackerras3cbed422009-01-09 16:43:42 +1100884 flags = hw_perf_save_disable();
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100885
886 /*
887 * First go through the list and put on any pinned groups
888 * in order to give them the best chance of going on.
889 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100890 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100891 if (counter->state <= PERF_COUNTER_STATE_OFF ||
892 !counter->hw_event.pinned)
893 continue;
894 if (counter->cpu != -1 && counter->cpu != cpu)
895 continue;
896
897 if (group_can_go_on(counter, cpuctx, 1))
898 group_sched_in(counter, cpuctx, ctx, cpu);
899
900 /*
901 * If this pinned group hasn't been scheduled,
902 * put it in error state.
903 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100904 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
905 update_group_times(counter);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100906 counter->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100907 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100908 }
909
910 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
911 /*
912 * Ignore counters in OFF or ERROR state, and
913 * ignore pinned counters since we did them already.
914 */
915 if (counter->state <= PERF_COUNTER_STATE_OFF ||
916 counter->hw_event.pinned)
917 continue;
918
Ingo Molnar04289bb2008-12-11 08:38:42 +0100919 /*
920 * Listen to the 'cpu' scheduling filter constraint
921 * of counters:
922 */
Thomas Gleixner0793a612008-12-04 20:12:29 +0100923 if (counter->cpu != -1 && counter->cpu != cpu)
924 continue;
925
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100926 if (group_can_go_on(counter, cpuctx, can_add_hw)) {
Paul Mackerrasdd0e6ba2009-01-12 15:11:00 +1100927 if (group_sched_in(counter, cpuctx, ctx, cpu))
928 can_add_hw = 0;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100929 }
Thomas Gleixner0793a612008-12-04 20:12:29 +0100930 }
Paul Mackerras3cbed422009-01-09 16:43:42 +1100931 hw_perf_restore(flags);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100932 out:
Thomas Gleixner0793a612008-12-04 20:12:29 +0100933 spin_unlock(&ctx->lock);
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100934}
Ingo Molnar04289bb2008-12-11 08:38:42 +0100935
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100936/*
937 * Called from scheduler to add the counters of the current task
938 * with interrupts disabled.
939 *
940 * We restore the counter value and then enable it.
941 *
942 * This does not protect us against NMI, but enable()
943 * sets the enabled bit in the control field of counter _before_
944 * accessing the counter control register. If a NMI hits, then it will
945 * keep the counter running.
946 */
947void perf_counter_task_sched_in(struct task_struct *task, int cpu)
948{
949 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
950 struct perf_counter_context *ctx = &task->perf_counter_ctx;
951
952 __perf_counter_sched_in(ctx, cpuctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100953 cpuctx->task_ctx = ctx;
954}
955
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100956static void perf_counter_cpu_sched_in(struct perf_cpu_context *cpuctx, int cpu)
957{
958 struct perf_counter_context *ctx = &cpuctx->ctx;
959
960 __perf_counter_sched_in(ctx, cpuctx, cpu);
961}
962
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100963int perf_counter_task_disable(void)
964{
965 struct task_struct *curr = current;
966 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
967 struct perf_counter *counter;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100968 unsigned long flags;
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100969 u64 perf_flags;
970 int cpu;
971
972 if (likely(!ctx->nr_counters))
973 return 0;
974
Peter Zijlstra849691a2009-04-06 11:45:12 +0200975 local_irq_save(flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100976 cpu = smp_processor_id();
977
978 perf_counter_task_sched_out(curr, cpu);
979
980 spin_lock(&ctx->lock);
981
982 /*
983 * Disable all the counters:
984 */
985 perf_flags = hw_perf_save_disable();
986
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100987 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100988 if (counter->state != PERF_COUNTER_STATE_ERROR) {
989 update_group_times(counter);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100990 counter->state = PERF_COUNTER_STATE_OFF;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100991 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100992 }
Ingo Molnar9b51f662008-12-12 13:49:45 +0100993
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100994 hw_perf_restore(perf_flags);
995
Peter Zijlstra849691a2009-04-06 11:45:12 +0200996 spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100997
998 return 0;
999}
1000
1001int perf_counter_task_enable(void)
1002{
1003 struct task_struct *curr = current;
1004 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
1005 struct perf_counter *counter;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001006 unsigned long flags;
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001007 u64 perf_flags;
1008 int cpu;
1009
1010 if (likely(!ctx->nr_counters))
1011 return 0;
1012
Peter Zijlstra849691a2009-04-06 11:45:12 +02001013 local_irq_save(flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001014 cpu = smp_processor_id();
1015
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001016 perf_counter_task_sched_out(curr, cpu);
1017
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001018 spin_lock(&ctx->lock);
1019
1020 /*
1021 * Disable all the counters:
1022 */
1023 perf_flags = hw_perf_save_disable();
1024
1025 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001026 if (counter->state > PERF_COUNTER_STATE_OFF)
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001027 continue;
Ingo Molnar6a930702008-12-11 15:17:03 +01001028 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +02001029 counter->tstamp_enabled =
1030 ctx->time - counter->total_time_enabled;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001031 counter->hw_event.disabled = 0;
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001032 }
1033 hw_perf_restore(perf_flags);
1034
1035 spin_unlock(&ctx->lock);
1036
1037 perf_counter_task_sched_in(curr, cpu);
1038
Peter Zijlstra849691a2009-04-06 11:45:12 +02001039 local_irq_restore(flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001040
1041 return 0;
1042}
1043
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001044/*
1045 * Round-robin a context's counters:
1046 */
1047static void rotate_ctx(struct perf_counter_context *ctx)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001048{
Thomas Gleixner0793a612008-12-04 20:12:29 +01001049 struct perf_counter *counter;
Ingo Molnar5c92d122008-12-11 13:21:10 +01001050 u64 perf_flags;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001051
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001052 if (!ctx->nr_counters)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001053 return;
1054
Thomas Gleixner0793a612008-12-04 20:12:29 +01001055 spin_lock(&ctx->lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001056 /*
Ingo Molnar04289bb2008-12-11 08:38:42 +01001057 * Rotate the first entry last (works just fine for group counters too):
Thomas Gleixner0793a612008-12-04 20:12:29 +01001058 */
Ingo Molnar01b28382008-12-11 13:45:51 +01001059 perf_flags = hw_perf_save_disable();
Ingo Molnar04289bb2008-12-11 08:38:42 +01001060 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Peter Zijlstra75564232009-03-13 12:21:29 +01001061 list_move_tail(&counter->list_entry, &ctx->counter_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001062 break;
1063 }
Ingo Molnar01b28382008-12-11 13:45:51 +01001064 hw_perf_restore(perf_flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001065
1066 spin_unlock(&ctx->lock);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001067}
Thomas Gleixner0793a612008-12-04 20:12:29 +01001068
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001069void perf_counter_task_tick(struct task_struct *curr, int cpu)
1070{
1071 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
1072 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001073
Ingo Molnarb82914c2009-05-04 18:54:32 +02001074 perf_counter_cpu_sched_out(cpuctx);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001075 perf_counter_task_sched_out(curr, cpu);
1076
Ingo Molnarb82914c2009-05-04 18:54:32 +02001077 rotate_ctx(&cpuctx->ctx);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001078 rotate_ctx(ctx);
1079
Ingo Molnarb82914c2009-05-04 18:54:32 +02001080 perf_counter_cpu_sched_in(cpuctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001081 perf_counter_task_sched_in(curr, cpu);
1082}
1083
1084/*
Thomas Gleixner0793a612008-12-04 20:12:29 +01001085 * Cross CPU call to read the hardware counter
1086 */
Ingo Molnar76715812008-12-17 14:20:28 +01001087static void __read(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001088{
Ingo Molnar621a01e2008-12-11 12:46:46 +01001089 struct perf_counter *counter = info;
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001090 struct perf_counter_context *ctx = counter->ctx;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001091 unsigned long flags;
Ingo Molnar621a01e2008-12-11 12:46:46 +01001092
Peter Zijlstra849691a2009-04-06 11:45:12 +02001093 local_irq_save(flags);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001094 if (ctx->is_active)
Peter Zijlstra4af49982009-04-06 11:45:10 +02001095 update_context_time(ctx);
Robert Richter4aeb0b42009-04-29 12:47:03 +02001096 counter->pmu->read(counter);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001097 update_counter_times(counter);
Peter Zijlstra849691a2009-04-06 11:45:12 +02001098 local_irq_restore(flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001099}
1100
Ingo Molnar04289bb2008-12-11 08:38:42 +01001101static u64 perf_counter_read(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001102{
1103 /*
1104 * If counter is enabled and currently active on a CPU, update the
1105 * value in the counter structure:
1106 */
Ingo Molnar6a930702008-12-11 15:17:03 +01001107 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
Thomas Gleixner0793a612008-12-04 20:12:29 +01001108 smp_call_function_single(counter->oncpu,
Ingo Molnar76715812008-12-17 14:20:28 +01001109 __read, counter, 1);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001110 } else if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
1111 update_counter_times(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001112 }
1113
Ingo Molnaree060942008-12-13 09:00:03 +01001114 return atomic64_read(&counter->count);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001115}
1116
Thomas Gleixner0793a612008-12-04 20:12:29 +01001117static void put_context(struct perf_counter_context *ctx)
1118{
1119 if (ctx->task)
1120 put_task_struct(ctx->task);
1121}
1122
1123static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
1124{
1125 struct perf_cpu_context *cpuctx;
1126 struct perf_counter_context *ctx;
1127 struct task_struct *task;
1128
1129 /*
1130 * If cpu is not a wildcard then this is a percpu counter:
1131 */
1132 if (cpu != -1) {
1133 /* Must be root to operate on a CPU counter: */
Peter Zijlstra1ccd1542009-04-09 10:53:45 +02001134 if (sysctl_perf_counter_priv && !capable(CAP_SYS_ADMIN))
Thomas Gleixner0793a612008-12-04 20:12:29 +01001135 return ERR_PTR(-EACCES);
1136
1137 if (cpu < 0 || cpu > num_possible_cpus())
1138 return ERR_PTR(-EINVAL);
1139
1140 /*
1141 * We could be clever and allow to attach a counter to an
1142 * offline CPU and activate it when the CPU comes up, but
1143 * that's for later.
1144 */
1145 if (!cpu_isset(cpu, cpu_online_map))
1146 return ERR_PTR(-ENODEV);
1147
1148 cpuctx = &per_cpu(perf_cpu_context, cpu);
1149 ctx = &cpuctx->ctx;
1150
Thomas Gleixner0793a612008-12-04 20:12:29 +01001151 return ctx;
1152 }
1153
1154 rcu_read_lock();
1155 if (!pid)
1156 task = current;
1157 else
1158 task = find_task_by_vpid(pid);
1159 if (task)
1160 get_task_struct(task);
1161 rcu_read_unlock();
1162
1163 if (!task)
1164 return ERR_PTR(-ESRCH);
1165
1166 ctx = &task->perf_counter_ctx;
1167 ctx->task = task;
1168
1169 /* Reuse ptrace permission checks for now. */
1170 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
1171 put_context(ctx);
1172 return ERR_PTR(-EACCES);
1173 }
1174
1175 return ctx;
1176}
1177
Peter Zijlstra592903c2009-03-13 12:21:36 +01001178static void free_counter_rcu(struct rcu_head *head)
1179{
1180 struct perf_counter *counter;
1181
1182 counter = container_of(head, struct perf_counter, rcu_head);
1183 kfree(counter);
1184}
1185
Peter Zijlstra925d5192009-03-30 19:07:02 +02001186static void perf_pending_sync(struct perf_counter *counter);
1187
Peter Zijlstraf1600952009-03-19 20:26:16 +01001188static void free_counter(struct perf_counter *counter)
1189{
Peter Zijlstra925d5192009-03-30 19:07:02 +02001190 perf_pending_sync(counter);
1191
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02001192 if (counter->hw_event.mmap)
1193 atomic_dec(&nr_mmap_tracking);
1194 if (counter->hw_event.munmap)
1195 atomic_dec(&nr_munmap_tracking);
1196 if (counter->hw_event.comm)
1197 atomic_dec(&nr_comm_tracking);
1198
Peter Zijlstrae077df42009-03-19 20:26:17 +01001199 if (counter->destroy)
1200 counter->destroy(counter);
1201
Peter Zijlstraf1600952009-03-19 20:26:16 +01001202 call_rcu(&counter->rcu_head, free_counter_rcu);
1203}
1204
Thomas Gleixner0793a612008-12-04 20:12:29 +01001205/*
1206 * Called when the last reference to the file is gone.
1207 */
1208static int perf_release(struct inode *inode, struct file *file)
1209{
1210 struct perf_counter *counter = file->private_data;
1211 struct perf_counter_context *ctx = counter->ctx;
1212
1213 file->private_data = NULL;
1214
Paul Mackerrasd859e292009-01-17 18:10:22 +11001215 mutex_lock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001216 mutex_lock(&counter->mutex);
1217
Ingo Molnar04289bb2008-12-11 08:38:42 +01001218 perf_counter_remove_from_context(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001219
1220 mutex_unlock(&counter->mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001221 mutex_unlock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001222
Peter Zijlstraf1600952009-03-19 20:26:16 +01001223 free_counter(counter);
Mike Galbraith5af75912009-02-11 10:53:37 +01001224 put_context(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001225
1226 return 0;
1227}
1228
1229/*
1230 * Read the performance counter - simple non blocking version for now
1231 */
1232static ssize_t
1233perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
1234{
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001235 u64 values[3];
1236 int n;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001237
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001238 /*
1239 * Return end-of-file for a read on a counter that is in
1240 * error state (i.e. because it was pinned but it couldn't be
1241 * scheduled on to the CPU at some point).
1242 */
1243 if (counter->state == PERF_COUNTER_STATE_ERROR)
1244 return 0;
1245
Thomas Gleixner0793a612008-12-04 20:12:29 +01001246 mutex_lock(&counter->mutex);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001247 values[0] = perf_counter_read(counter);
1248 n = 1;
1249 if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1250 values[n++] = counter->total_time_enabled +
1251 atomic64_read(&counter->child_total_time_enabled);
1252 if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1253 values[n++] = counter->total_time_running +
1254 atomic64_read(&counter->child_total_time_running);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001255 mutex_unlock(&counter->mutex);
1256
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001257 if (count < n * sizeof(u64))
1258 return -EINVAL;
1259 count = n * sizeof(u64);
1260
1261 if (copy_to_user(buf, values, count))
1262 return -EFAULT;
1263
1264 return count;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001265}
1266
1267static ssize_t
Thomas Gleixner0793a612008-12-04 20:12:29 +01001268perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
1269{
1270 struct perf_counter *counter = file->private_data;
1271
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001272 return perf_read_hw(counter, buf, count);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001273}
1274
1275static unsigned int perf_poll(struct file *file, poll_table *wait)
1276{
1277 struct perf_counter *counter = file->private_data;
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001278 struct perf_mmap_data *data;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001279 unsigned int events = POLL_HUP;
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001280
1281 rcu_read_lock();
1282 data = rcu_dereference(counter->data);
1283 if (data)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001284 events = atomic_xchg(&data->poll, 0);
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001285 rcu_read_unlock();
Thomas Gleixner0793a612008-12-04 20:12:29 +01001286
1287 poll_wait(file, &counter->waitq, wait);
1288
Thomas Gleixner0793a612008-12-04 20:12:29 +01001289 return events;
1290}
1291
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001292static void perf_counter_reset(struct perf_counter *counter)
1293{
1294 atomic_set(&counter->count, 0);
1295}
1296
Paul Mackerrasd859e292009-01-17 18:10:22 +11001297static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1298{
1299 struct perf_counter *counter = file->private_data;
1300 int err = 0;
1301
1302 switch (cmd) {
1303 case PERF_COUNTER_IOC_ENABLE:
1304 perf_counter_enable_family(counter);
1305 break;
1306 case PERF_COUNTER_IOC_DISABLE:
1307 perf_counter_disable_family(counter);
1308 break;
Peter Zijlstra79f14642009-04-06 11:45:07 +02001309 case PERF_COUNTER_IOC_REFRESH:
1310 perf_counter_refresh(counter, arg);
1311 break;
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001312 case PERF_COUNTER_IOC_RESET:
1313 perf_counter_reset(counter);
1314 break;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001315 default:
1316 err = -ENOTTY;
1317 }
1318 return err;
1319}
1320
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001321/*
1322 * Callers need to ensure there can be no nesting of this function, otherwise
1323 * the seqlock logic goes bad. We can not serialize this because the arch
1324 * code calls this from NMI context.
1325 */
1326void perf_counter_update_userpage(struct perf_counter *counter)
Paul Mackerras37d81822009-03-23 18:22:08 +01001327{
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001328 struct perf_mmap_data *data;
1329 struct perf_counter_mmap_page *userpg;
1330
1331 rcu_read_lock();
1332 data = rcu_dereference(counter->data);
1333 if (!data)
1334 goto unlock;
1335
1336 userpg = data->user_page;
Paul Mackerras37d81822009-03-23 18:22:08 +01001337
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001338 /*
1339 * Disable preemption so as to not let the corresponding user-space
1340 * spin too long if we get preempted.
1341 */
1342 preempt_disable();
Paul Mackerras37d81822009-03-23 18:22:08 +01001343 ++userpg->lock;
Peter Zijlstra92f22a32009-04-02 11:12:04 +02001344 barrier();
Paul Mackerras37d81822009-03-23 18:22:08 +01001345 userpg->index = counter->hw.idx;
1346 userpg->offset = atomic64_read(&counter->count);
1347 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
1348 userpg->offset -= atomic64_read(&counter->hw.prev_count);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001349
Peter Zijlstra92f22a32009-04-02 11:12:04 +02001350 barrier();
Paul Mackerras37d81822009-03-23 18:22:08 +01001351 ++userpg->lock;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001352 preempt_enable();
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001353unlock:
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001354 rcu_read_unlock();
Paul Mackerras37d81822009-03-23 18:22:08 +01001355}
1356
1357static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1358{
1359 struct perf_counter *counter = vma->vm_file->private_data;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001360 struct perf_mmap_data *data;
1361 int ret = VM_FAULT_SIGBUS;
Paul Mackerras37d81822009-03-23 18:22:08 +01001362
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001363 rcu_read_lock();
1364 data = rcu_dereference(counter->data);
1365 if (!data)
1366 goto unlock;
Paul Mackerras37d81822009-03-23 18:22:08 +01001367
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001368 if (vmf->pgoff == 0) {
1369 vmf->page = virt_to_page(data->user_page);
1370 } else {
1371 int nr = vmf->pgoff - 1;
1372
1373 if ((unsigned)nr > data->nr_pages)
1374 goto unlock;
1375
1376 vmf->page = virt_to_page(data->data_pages[nr]);
1377 }
Paul Mackerras37d81822009-03-23 18:22:08 +01001378 get_page(vmf->page);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001379 ret = 0;
1380unlock:
1381 rcu_read_unlock();
1382
1383 return ret;
1384}
1385
1386static int perf_mmap_data_alloc(struct perf_counter *counter, int nr_pages)
1387{
1388 struct perf_mmap_data *data;
1389 unsigned long size;
1390 int i;
1391
1392 WARN_ON(atomic_read(&counter->mmap_count));
1393
1394 size = sizeof(struct perf_mmap_data);
1395 size += nr_pages * sizeof(void *);
1396
1397 data = kzalloc(size, GFP_KERNEL);
1398 if (!data)
1399 goto fail;
1400
1401 data->user_page = (void *)get_zeroed_page(GFP_KERNEL);
1402 if (!data->user_page)
1403 goto fail_user_page;
1404
1405 for (i = 0; i < nr_pages; i++) {
1406 data->data_pages[i] = (void *)get_zeroed_page(GFP_KERNEL);
1407 if (!data->data_pages[i])
1408 goto fail_data_pages;
1409 }
1410
1411 data->nr_pages = nr_pages;
1412
1413 rcu_assign_pointer(counter->data, data);
1414
Paul Mackerras37d81822009-03-23 18:22:08 +01001415 return 0;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001416
1417fail_data_pages:
1418 for (i--; i >= 0; i--)
1419 free_page((unsigned long)data->data_pages[i]);
1420
1421 free_page((unsigned long)data->user_page);
1422
1423fail_user_page:
1424 kfree(data);
1425
1426fail:
1427 return -ENOMEM;
1428}
1429
1430static void __perf_mmap_data_free(struct rcu_head *rcu_head)
1431{
1432 struct perf_mmap_data *data = container_of(rcu_head,
1433 struct perf_mmap_data, rcu_head);
1434 int i;
1435
1436 free_page((unsigned long)data->user_page);
1437 for (i = 0; i < data->nr_pages; i++)
1438 free_page((unsigned long)data->data_pages[i]);
1439 kfree(data);
1440}
1441
1442static void perf_mmap_data_free(struct perf_counter *counter)
1443{
1444 struct perf_mmap_data *data = counter->data;
1445
1446 WARN_ON(atomic_read(&counter->mmap_count));
1447
1448 rcu_assign_pointer(counter->data, NULL);
1449 call_rcu(&data->rcu_head, __perf_mmap_data_free);
1450}
1451
1452static void perf_mmap_open(struct vm_area_struct *vma)
1453{
1454 struct perf_counter *counter = vma->vm_file->private_data;
1455
1456 atomic_inc(&counter->mmap_count);
1457}
1458
1459static void perf_mmap_close(struct vm_area_struct *vma)
1460{
1461 struct perf_counter *counter = vma->vm_file->private_data;
1462
1463 if (atomic_dec_and_mutex_lock(&counter->mmap_count,
1464 &counter->mmap_mutex)) {
Peter Zijlstrac5078f72009-05-05 17:50:24 +02001465 vma->vm_mm->locked_vm -= counter->data->nr_locked;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001466 perf_mmap_data_free(counter);
1467 mutex_unlock(&counter->mmap_mutex);
1468 }
Paul Mackerras37d81822009-03-23 18:22:08 +01001469}
1470
1471static struct vm_operations_struct perf_mmap_vmops = {
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001472 .open = perf_mmap_open,
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001473 .close = perf_mmap_close,
Paul Mackerras37d81822009-03-23 18:22:08 +01001474 .fault = perf_mmap_fault,
1475};
1476
1477static int perf_mmap(struct file *file, struct vm_area_struct *vma)
1478{
1479 struct perf_counter *counter = file->private_data;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001480 unsigned long vma_size;
1481 unsigned long nr_pages;
1482 unsigned long locked, lock_limit;
1483 int ret = 0;
Peter Zijlstrac5078f72009-05-05 17:50:24 +02001484 long extra;
Paul Mackerras37d81822009-03-23 18:22:08 +01001485
1486 if (!(vma->vm_flags & VM_SHARED) || (vma->vm_flags & VM_WRITE))
1487 return -EINVAL;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001488
1489 vma_size = vma->vm_end - vma->vm_start;
1490 nr_pages = (vma_size / PAGE_SIZE) - 1;
1491
Peter Zijlstra7730d862009-03-25 12:48:31 +01001492 /*
1493 * If we have data pages ensure they're a power-of-two number, so we
1494 * can do bitmasks instead of modulo.
1495 */
1496 if (nr_pages != 0 && !is_power_of_2(nr_pages))
Paul Mackerras37d81822009-03-23 18:22:08 +01001497 return -EINVAL;
1498
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001499 if (vma_size != PAGE_SIZE * (1 + nr_pages))
Paul Mackerras37d81822009-03-23 18:22:08 +01001500 return -EINVAL;
1501
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001502 if (vma->vm_pgoff != 0)
1503 return -EINVAL;
Paul Mackerras37d81822009-03-23 18:22:08 +01001504
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001505 mutex_lock(&counter->mmap_mutex);
1506 if (atomic_inc_not_zero(&counter->mmap_count)) {
1507 if (nr_pages != counter->data->nr_pages)
1508 ret = -EINVAL;
1509 goto unlock;
1510 }
1511
Peter Zijlstrac5078f72009-05-05 17:50:24 +02001512 extra = nr_pages /* + 1 only account the data pages */;
1513 extra -= sysctl_perf_counter_mlock >> (PAGE_SHIFT - 10);
1514 if (extra < 0)
1515 extra = 0;
1516
1517 locked = vma->vm_mm->locked_vm + extra;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001518
1519 lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
1520 lock_limit >>= PAGE_SHIFT;
1521
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001522 if ((locked > lock_limit) && !capable(CAP_IPC_LOCK)) {
1523 ret = -EPERM;
1524 goto unlock;
1525 }
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001526
1527 WARN_ON(counter->data);
1528 ret = perf_mmap_data_alloc(counter, nr_pages);
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001529 if (ret)
1530 goto unlock;
1531
1532 atomic_set(&counter->mmap_count, 1);
Peter Zijlstrac5078f72009-05-05 17:50:24 +02001533 vma->vm_mm->locked_vm += extra;
1534 counter->data->nr_locked = extra;
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001535unlock:
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001536 mutex_unlock(&counter->mmap_mutex);
Paul Mackerras37d81822009-03-23 18:22:08 +01001537
1538 vma->vm_flags &= ~VM_MAYWRITE;
1539 vma->vm_flags |= VM_RESERVED;
1540 vma->vm_ops = &perf_mmap_vmops;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001541
1542 return ret;
Paul Mackerras37d81822009-03-23 18:22:08 +01001543}
1544
Peter Zijlstra3c446b32009-04-06 11:45:01 +02001545static int perf_fasync(int fd, struct file *filp, int on)
1546{
1547 struct perf_counter *counter = filp->private_data;
1548 struct inode *inode = filp->f_path.dentry->d_inode;
1549 int retval;
1550
1551 mutex_lock(&inode->i_mutex);
1552 retval = fasync_helper(fd, filp, on, &counter->fasync);
1553 mutex_unlock(&inode->i_mutex);
1554
1555 if (retval < 0)
1556 return retval;
1557
1558 return 0;
1559}
1560
Thomas Gleixner0793a612008-12-04 20:12:29 +01001561static const struct file_operations perf_fops = {
1562 .release = perf_release,
1563 .read = perf_read,
1564 .poll = perf_poll,
Paul Mackerrasd859e292009-01-17 18:10:22 +11001565 .unlocked_ioctl = perf_ioctl,
1566 .compat_ioctl = perf_ioctl,
Paul Mackerras37d81822009-03-23 18:22:08 +01001567 .mmap = perf_mmap,
Peter Zijlstra3c446b32009-04-06 11:45:01 +02001568 .fasync = perf_fasync,
Thomas Gleixner0793a612008-12-04 20:12:29 +01001569};
1570
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001571/*
Peter Zijlstra925d5192009-03-30 19:07:02 +02001572 * Perf counter wakeup
1573 *
1574 * If there's data, ensure we set the poll() state and publish everything
1575 * to user-space before waking everybody up.
1576 */
1577
1578void perf_counter_wakeup(struct perf_counter *counter)
1579{
Peter Zijlstra925d5192009-03-30 19:07:02 +02001580 wake_up_all(&counter->waitq);
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02001581
1582 if (counter->pending_kill) {
1583 kill_fasync(&counter->fasync, SIGIO, counter->pending_kill);
1584 counter->pending_kill = 0;
1585 }
Peter Zijlstra925d5192009-03-30 19:07:02 +02001586}
1587
1588/*
1589 * Pending wakeups
1590 *
1591 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
1592 *
1593 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
1594 * single linked list and use cmpxchg() to add entries lockless.
1595 */
1596
Peter Zijlstra79f14642009-04-06 11:45:07 +02001597static void perf_pending_counter(struct perf_pending_entry *entry)
1598{
1599 struct perf_counter *counter = container_of(entry,
1600 struct perf_counter, pending);
1601
1602 if (counter->pending_disable) {
1603 counter->pending_disable = 0;
1604 perf_counter_disable(counter);
1605 }
1606
1607 if (counter->pending_wakeup) {
1608 counter->pending_wakeup = 0;
1609 perf_counter_wakeup(counter);
1610 }
1611}
1612
Peter Zijlstra671dec52009-04-06 11:45:02 +02001613#define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
Peter Zijlstra925d5192009-03-30 19:07:02 +02001614
Peter Zijlstra671dec52009-04-06 11:45:02 +02001615static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = {
Peter Zijlstra925d5192009-03-30 19:07:02 +02001616 PENDING_TAIL,
1617};
1618
Peter Zijlstra671dec52009-04-06 11:45:02 +02001619static void perf_pending_queue(struct perf_pending_entry *entry,
1620 void (*func)(struct perf_pending_entry *))
Peter Zijlstra925d5192009-03-30 19:07:02 +02001621{
Peter Zijlstra671dec52009-04-06 11:45:02 +02001622 struct perf_pending_entry **head;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001623
Peter Zijlstra671dec52009-04-06 11:45:02 +02001624 if (cmpxchg(&entry->next, NULL, PENDING_TAIL) != NULL)
Peter Zijlstra925d5192009-03-30 19:07:02 +02001625 return;
1626
Peter Zijlstra671dec52009-04-06 11:45:02 +02001627 entry->func = func;
1628
1629 head = &get_cpu_var(perf_pending_head);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001630
1631 do {
Peter Zijlstra671dec52009-04-06 11:45:02 +02001632 entry->next = *head;
1633 } while (cmpxchg(head, entry->next, entry) != entry->next);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001634
1635 set_perf_counter_pending();
1636
Peter Zijlstra671dec52009-04-06 11:45:02 +02001637 put_cpu_var(perf_pending_head);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001638}
1639
1640static int __perf_pending_run(void)
1641{
Peter Zijlstra671dec52009-04-06 11:45:02 +02001642 struct perf_pending_entry *list;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001643 int nr = 0;
1644
Peter Zijlstra671dec52009-04-06 11:45:02 +02001645 list = xchg(&__get_cpu_var(perf_pending_head), PENDING_TAIL);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001646 while (list != PENDING_TAIL) {
Peter Zijlstra671dec52009-04-06 11:45:02 +02001647 void (*func)(struct perf_pending_entry *);
1648 struct perf_pending_entry *entry = list;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001649
1650 list = list->next;
1651
Peter Zijlstra671dec52009-04-06 11:45:02 +02001652 func = entry->func;
1653 entry->next = NULL;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001654 /*
1655 * Ensure we observe the unqueue before we issue the wakeup,
1656 * so that we won't be waiting forever.
1657 * -- see perf_not_pending().
1658 */
1659 smp_wmb();
1660
Peter Zijlstra671dec52009-04-06 11:45:02 +02001661 func(entry);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001662 nr++;
1663 }
1664
1665 return nr;
1666}
1667
1668static inline int perf_not_pending(struct perf_counter *counter)
1669{
1670 /*
1671 * If we flush on whatever cpu we run, there is a chance we don't
1672 * need to wait.
1673 */
1674 get_cpu();
1675 __perf_pending_run();
1676 put_cpu();
1677
1678 /*
1679 * Ensure we see the proper queue state before going to sleep
1680 * so that we do not miss the wakeup. -- see perf_pending_handle()
1681 */
1682 smp_rmb();
Peter Zijlstra671dec52009-04-06 11:45:02 +02001683 return counter->pending.next == NULL;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001684}
1685
1686static void perf_pending_sync(struct perf_counter *counter)
1687{
1688 wait_event(counter->waitq, perf_not_pending(counter));
1689}
1690
1691void perf_counter_do_pending(void)
1692{
1693 __perf_pending_run();
1694}
1695
1696/*
Peter Zijlstra394ee072009-03-30 19:07:14 +02001697 * Callchain support -- arch specific
1698 */
1699
Peter Zijlstra9c03d882009-04-06 11:45:00 +02001700__weak struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
Peter Zijlstra394ee072009-03-30 19:07:14 +02001701{
1702 return NULL;
1703}
1704
1705/*
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001706 * Output
1707 */
1708
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001709struct perf_output_handle {
1710 struct perf_counter *counter;
1711 struct perf_mmap_data *data;
1712 unsigned int offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01001713 unsigned int head;
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001714 int nmi;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02001715 int overflow;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001716 int locked;
1717 unsigned long flags;
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001718};
1719
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001720static void perf_output_wakeup(struct perf_output_handle *handle)
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001721{
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001722 atomic_set(&handle->data->poll, POLL_IN);
1723
Peter Zijlstra671dec52009-04-06 11:45:02 +02001724 if (handle->nmi) {
Peter Zijlstra79f14642009-04-06 11:45:07 +02001725 handle->counter->pending_wakeup = 1;
Peter Zijlstra671dec52009-04-06 11:45:02 +02001726 perf_pending_queue(&handle->counter->pending,
Peter Zijlstra79f14642009-04-06 11:45:07 +02001727 perf_pending_counter);
Peter Zijlstra671dec52009-04-06 11:45:02 +02001728 } else
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001729 perf_counter_wakeup(handle->counter);
1730}
1731
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001732/*
1733 * Curious locking construct.
1734 *
1735 * We need to ensure a later event doesn't publish a head when a former
1736 * event isn't done writing. However since we need to deal with NMIs we
1737 * cannot fully serialize things.
1738 *
1739 * What we do is serialize between CPUs so we only have to deal with NMI
1740 * nesting on a single CPU.
1741 *
1742 * We only publish the head (and generate a wakeup) when the outer-most
1743 * event completes.
1744 */
1745static void perf_output_lock(struct perf_output_handle *handle)
1746{
1747 struct perf_mmap_data *data = handle->data;
1748 int cpu;
1749
1750 handle->locked = 0;
1751
1752 local_irq_save(handle->flags);
1753 cpu = smp_processor_id();
1754
1755 if (in_nmi() && atomic_read(&data->lock) == cpu)
1756 return;
1757
1758 while (atomic_cmpxchg(&data->lock, 0, cpu) != 0)
1759 cpu_relax();
1760
1761 handle->locked = 1;
1762}
1763
1764static void perf_output_unlock(struct perf_output_handle *handle)
1765{
1766 struct perf_mmap_data *data = handle->data;
1767 int head, cpu;
1768
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02001769 data->done_head = data->head;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001770
1771 if (!handle->locked)
1772 goto out;
1773
1774again:
1775 /*
1776 * The xchg implies a full barrier that ensures all writes are done
1777 * before we publish the new head, matched by a rmb() in userspace when
1778 * reading this position.
1779 */
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02001780 while ((head = atomic_xchg(&data->done_head, 0)))
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001781 data->user_page->data_head = head;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001782
1783 /*
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02001784 * NMI can happen here, which means we can miss a done_head update.
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001785 */
1786
1787 cpu = atomic_xchg(&data->lock, 0);
1788 WARN_ON_ONCE(cpu != smp_processor_id());
1789
1790 /*
1791 * Therefore we have to validate we did not indeed do so.
1792 */
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02001793 if (unlikely(atomic_read(&data->done_head))) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001794 /*
1795 * Since we had it locked, we can lock it again.
1796 */
1797 while (atomic_cmpxchg(&data->lock, 0, cpu) != 0)
1798 cpu_relax();
1799
1800 goto again;
1801 }
1802
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02001803 if (atomic_xchg(&data->wakeup, 0))
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001804 perf_output_wakeup(handle);
1805out:
1806 local_irq_restore(handle->flags);
1807}
1808
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001809static int perf_output_begin(struct perf_output_handle *handle,
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001810 struct perf_counter *counter, unsigned int size,
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02001811 int nmi, int overflow)
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001812{
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001813 struct perf_mmap_data *data;
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001814 unsigned int offset, head;
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001815
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001816 rcu_read_lock();
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001817 data = rcu_dereference(counter->data);
1818 if (!data)
1819 goto out;
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001820
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001821 handle->data = data;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02001822 handle->counter = counter;
1823 handle->nmi = nmi;
1824 handle->overflow = overflow;
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001825
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001826 if (!data->nr_pages)
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001827 goto fail;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001828
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001829 perf_output_lock(handle);
1830
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001831 do {
1832 offset = head = atomic_read(&data->head);
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001833 head += size;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001834 } while (atomic_cmpxchg(&data->head, offset, head) != offset);
1835
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001836 handle->offset = offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01001837 handle->head = head;
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02001838
1839 if ((offset >> PAGE_SHIFT) != (head >> PAGE_SHIFT))
1840 atomic_set(&data->wakeup, 1);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001841
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001842 return 0;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001843
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001844fail:
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001845 perf_output_wakeup(handle);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001846out:
1847 rcu_read_unlock();
1848
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001849 return -ENOSPC;
1850}
1851
1852static void perf_output_copy(struct perf_output_handle *handle,
1853 void *buf, unsigned int len)
1854{
1855 unsigned int pages_mask;
1856 unsigned int offset;
1857 unsigned int size;
1858 void **pages;
1859
1860 offset = handle->offset;
1861 pages_mask = handle->data->nr_pages - 1;
1862 pages = handle->data->data_pages;
1863
1864 do {
1865 unsigned int page_offset;
1866 int nr;
1867
1868 nr = (offset >> PAGE_SHIFT) & pages_mask;
1869 page_offset = offset & (PAGE_SIZE - 1);
1870 size = min_t(unsigned int, PAGE_SIZE - page_offset, len);
1871
1872 memcpy(pages[nr] + page_offset, buf, size);
1873
1874 len -= size;
1875 buf += size;
1876 offset += size;
1877 } while (len);
1878
1879 handle->offset = offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01001880
1881 WARN_ON_ONCE(handle->offset > handle->head);
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001882}
1883
Peter Zijlstra5c148192009-03-25 12:30:23 +01001884#define perf_output_put(handle, x) \
1885 perf_output_copy((handle), &(x), sizeof(x))
1886
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001887static void perf_output_end(struct perf_output_handle *handle)
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001888{
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001889 struct perf_counter *counter = handle->counter;
1890 struct perf_mmap_data *data = handle->data;
1891
1892 int wakeup_events = counter->hw_event.wakeup_events;
Peter Zijlstrac4578102009-04-02 11:12:01 +02001893
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02001894 if (handle->overflow && wakeup_events) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001895 int events = atomic_inc_return(&data->events);
Peter Zijlstrac4578102009-04-02 11:12:01 +02001896 if (events >= wakeup_events) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001897 atomic_sub(wakeup_events, &data->events);
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02001898 atomic_set(&data->wakeup, 1);
Peter Zijlstrac4578102009-04-02 11:12:01 +02001899 }
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001900 }
1901
1902 perf_output_unlock(handle);
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001903 rcu_read_unlock();
1904}
1905
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02001906static void perf_counter_output(struct perf_counter *counter,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02001907 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001908{
Peter Zijlstra5ed00412009-03-30 19:07:12 +02001909 int ret;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02001910 u64 record_type = counter->hw_event.record_type;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02001911 struct perf_output_handle handle;
1912 struct perf_event_header header;
1913 u64 ip;
Peter Zijlstra5c148192009-03-25 12:30:23 +01001914 struct {
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01001915 u32 pid, tid;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02001916 } tid_entry;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02001917 struct {
1918 u64 event;
1919 u64 counter;
1920 } group_entry;
Peter Zijlstra394ee072009-03-30 19:07:14 +02001921 struct perf_callchain_entry *callchain = NULL;
1922 int callchain_size = 0;
Peter Zijlstra339f7c92009-04-06 11:45:06 +02001923 u64 time;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001924
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02001925 header.type = 0;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02001926 header.size = sizeof(header);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001927
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02001928 header.misc = PERF_EVENT_MISC_OVERFLOW;
1929 header.misc |= user_mode(regs) ?
Peter Zijlstra6fab0192009-04-08 15:01:26 +02001930 PERF_EVENT_MISC_USER : PERF_EVENT_MISC_KERNEL;
1931
Peter Zijlstra8a057d82009-04-02 11:11:59 +02001932 if (record_type & PERF_RECORD_IP) {
1933 ip = instruction_pointer(regs);
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02001934 header.type |= PERF_RECORD_IP;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02001935 header.size += sizeof(ip);
1936 }
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01001937
Peter Zijlstra8a057d82009-04-02 11:11:59 +02001938 if (record_type & PERF_RECORD_TID) {
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01001939 /* namespace issues */
Peter Zijlstra5ed00412009-03-30 19:07:12 +02001940 tid_entry.pid = current->group_leader->pid;
1941 tid_entry.tid = current->pid;
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01001942
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02001943 header.type |= PERF_RECORD_TID;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02001944 header.size += sizeof(tid_entry);
1945 }
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01001946
Peter Zijlstra4d855452009-04-08 15:01:32 +02001947 if (record_type & PERF_RECORD_TIME) {
1948 /*
1949 * Maybe do better on x86 and provide cpu_clock_nmi()
1950 */
1951 time = sched_clock();
1952
1953 header.type |= PERF_RECORD_TIME;
1954 header.size += sizeof(u64);
1955 }
1956
Peter Zijlstra78f13e92009-04-08 15:01:33 +02001957 if (record_type & PERF_RECORD_ADDR) {
1958 header.type |= PERF_RECORD_ADDR;
1959 header.size += sizeof(u64);
1960 }
1961
Peter Zijlstra8a057d82009-04-02 11:11:59 +02001962 if (record_type & PERF_RECORD_GROUP) {
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02001963 header.type |= PERF_RECORD_GROUP;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02001964 header.size += sizeof(u64) +
1965 counter->nr_siblings * sizeof(group_entry);
1966 }
1967
1968 if (record_type & PERF_RECORD_CALLCHAIN) {
Peter Zijlstra394ee072009-03-30 19:07:14 +02001969 callchain = perf_callchain(regs);
1970
1971 if (callchain) {
Peter Zijlstra9c03d882009-04-06 11:45:00 +02001972 callchain_size = (1 + callchain->nr) * sizeof(u64);
Peter Zijlstra394ee072009-03-30 19:07:14 +02001973
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02001974 header.type |= PERF_RECORD_CALLCHAIN;
Peter Zijlstra394ee072009-03-30 19:07:14 +02001975 header.size += callchain_size;
1976 }
1977 }
1978
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02001979 ret = perf_output_begin(&handle, counter, header.size, nmi, 1);
Peter Zijlstra5ed00412009-03-30 19:07:12 +02001980 if (ret)
1981 return;
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01001982
Peter Zijlstra5ed00412009-03-30 19:07:12 +02001983 perf_output_put(&handle, header);
Peter Zijlstra5ed00412009-03-30 19:07:12 +02001984
Peter Zijlstra8a057d82009-04-02 11:11:59 +02001985 if (record_type & PERF_RECORD_IP)
1986 perf_output_put(&handle, ip);
1987
1988 if (record_type & PERF_RECORD_TID)
Peter Zijlstra5ed00412009-03-30 19:07:12 +02001989 perf_output_put(&handle, tid_entry);
1990
Peter Zijlstra4d855452009-04-08 15:01:32 +02001991 if (record_type & PERF_RECORD_TIME)
1992 perf_output_put(&handle, time);
1993
Peter Zijlstra78f13e92009-04-08 15:01:33 +02001994 if (record_type & PERF_RECORD_ADDR)
1995 perf_output_put(&handle, addr);
1996
Peter Zijlstra8a057d82009-04-02 11:11:59 +02001997 if (record_type & PERF_RECORD_GROUP) {
1998 struct perf_counter *leader, *sub;
1999 u64 nr = counter->nr_siblings;
2000
2001 perf_output_put(&handle, nr);
2002
2003 leader = counter->group_leader;
2004 list_for_each_entry(sub, &leader->sibling_list, list_entry) {
2005 if (sub != counter)
Robert Richter4aeb0b42009-04-29 12:47:03 +02002006 sub->pmu->read(sub);
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002007
2008 group_entry.event = sub->hw_event.config;
2009 group_entry.counter = atomic64_read(&sub->count);
2010
2011 perf_output_put(&handle, group_entry);
2012 }
2013 }
2014
Peter Zijlstra394ee072009-03-30 19:07:14 +02002015 if (callchain)
2016 perf_output_copy(&handle, callchain, callchain_size);
2017
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002018 perf_output_end(&handle);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002019}
2020
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002021/*
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002022 * comm tracking
2023 */
2024
2025struct perf_comm_event {
2026 struct task_struct *task;
2027 char *comm;
2028 int comm_size;
2029
2030 struct {
2031 struct perf_event_header header;
2032
2033 u32 pid;
2034 u32 tid;
2035 } event;
2036};
2037
2038static void perf_counter_comm_output(struct perf_counter *counter,
2039 struct perf_comm_event *comm_event)
2040{
2041 struct perf_output_handle handle;
2042 int size = comm_event->event.header.size;
2043 int ret = perf_output_begin(&handle, counter, size, 0, 0);
2044
2045 if (ret)
2046 return;
2047
2048 perf_output_put(&handle, comm_event->event);
2049 perf_output_copy(&handle, comm_event->comm,
2050 comm_event->comm_size);
2051 perf_output_end(&handle);
2052}
2053
2054static int perf_counter_comm_match(struct perf_counter *counter,
2055 struct perf_comm_event *comm_event)
2056{
2057 if (counter->hw_event.comm &&
2058 comm_event->event.header.type == PERF_EVENT_COMM)
2059 return 1;
2060
2061 return 0;
2062}
2063
2064static void perf_counter_comm_ctx(struct perf_counter_context *ctx,
2065 struct perf_comm_event *comm_event)
2066{
2067 struct perf_counter *counter;
2068
2069 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2070 return;
2071
2072 rcu_read_lock();
2073 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2074 if (perf_counter_comm_match(counter, comm_event))
2075 perf_counter_comm_output(counter, comm_event);
2076 }
2077 rcu_read_unlock();
2078}
2079
2080static void perf_counter_comm_event(struct perf_comm_event *comm_event)
2081{
2082 struct perf_cpu_context *cpuctx;
2083 unsigned int size;
2084 char *comm = comm_event->task->comm;
2085
Ingo Molnar888fcee2009-04-09 09:48:22 +02002086 size = ALIGN(strlen(comm)+1, sizeof(u64));
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002087
2088 comm_event->comm = comm;
2089 comm_event->comm_size = size;
2090
2091 comm_event->event.header.size = sizeof(comm_event->event) + size;
2092
2093 cpuctx = &get_cpu_var(perf_cpu_context);
2094 perf_counter_comm_ctx(&cpuctx->ctx, comm_event);
2095 put_cpu_var(perf_cpu_context);
2096
2097 perf_counter_comm_ctx(&current->perf_counter_ctx, comm_event);
2098}
2099
2100void perf_counter_comm(struct task_struct *task)
2101{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002102 struct perf_comm_event comm_event;
2103
2104 if (!atomic_read(&nr_comm_tracking))
2105 return;
2106
2107 comm_event = (struct perf_comm_event){
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002108 .task = task,
2109 .event = {
2110 .header = { .type = PERF_EVENT_COMM, },
2111 .pid = task->group_leader->pid,
2112 .tid = task->pid,
2113 },
2114 };
2115
2116 perf_counter_comm_event(&comm_event);
2117}
2118
2119/*
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002120 * mmap tracking
2121 */
2122
2123struct perf_mmap_event {
2124 struct file *file;
2125 char *file_name;
2126 int file_size;
2127
2128 struct {
2129 struct perf_event_header header;
2130
2131 u32 pid;
2132 u32 tid;
2133 u64 start;
2134 u64 len;
2135 u64 pgoff;
2136 } event;
2137};
2138
2139static void perf_counter_mmap_output(struct perf_counter *counter,
2140 struct perf_mmap_event *mmap_event)
2141{
2142 struct perf_output_handle handle;
2143 int size = mmap_event->event.header.size;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002144 int ret = perf_output_begin(&handle, counter, size, 0, 0);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002145
2146 if (ret)
2147 return;
2148
2149 perf_output_put(&handle, mmap_event->event);
2150 perf_output_copy(&handle, mmap_event->file_name,
2151 mmap_event->file_size);
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002152 perf_output_end(&handle);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002153}
2154
2155static int perf_counter_mmap_match(struct perf_counter *counter,
2156 struct perf_mmap_event *mmap_event)
2157{
2158 if (counter->hw_event.mmap &&
2159 mmap_event->event.header.type == PERF_EVENT_MMAP)
2160 return 1;
2161
2162 if (counter->hw_event.munmap &&
2163 mmap_event->event.header.type == PERF_EVENT_MUNMAP)
2164 return 1;
2165
2166 return 0;
2167}
2168
2169static void perf_counter_mmap_ctx(struct perf_counter_context *ctx,
2170 struct perf_mmap_event *mmap_event)
2171{
2172 struct perf_counter *counter;
2173
2174 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2175 return;
2176
2177 rcu_read_lock();
2178 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2179 if (perf_counter_mmap_match(counter, mmap_event))
2180 perf_counter_mmap_output(counter, mmap_event);
2181 }
2182 rcu_read_unlock();
2183}
2184
2185static void perf_counter_mmap_event(struct perf_mmap_event *mmap_event)
2186{
2187 struct perf_cpu_context *cpuctx;
2188 struct file *file = mmap_event->file;
2189 unsigned int size;
2190 char tmp[16];
2191 char *buf = NULL;
2192 char *name;
2193
2194 if (file) {
2195 buf = kzalloc(PATH_MAX, GFP_KERNEL);
2196 if (!buf) {
2197 name = strncpy(tmp, "//enomem", sizeof(tmp));
2198 goto got_name;
2199 }
Peter Zijlstrad3d21c42009-04-09 10:53:46 +02002200 name = d_path(&file->f_path, buf, PATH_MAX);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002201 if (IS_ERR(name)) {
2202 name = strncpy(tmp, "//toolong", sizeof(tmp));
2203 goto got_name;
2204 }
2205 } else {
2206 name = strncpy(tmp, "//anon", sizeof(tmp));
2207 goto got_name;
2208 }
2209
2210got_name:
Ingo Molnar888fcee2009-04-09 09:48:22 +02002211 size = ALIGN(strlen(name)+1, sizeof(u64));
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002212
2213 mmap_event->file_name = name;
2214 mmap_event->file_size = size;
2215
2216 mmap_event->event.header.size = sizeof(mmap_event->event) + size;
2217
2218 cpuctx = &get_cpu_var(perf_cpu_context);
2219 perf_counter_mmap_ctx(&cpuctx->ctx, mmap_event);
2220 put_cpu_var(perf_cpu_context);
2221
2222 perf_counter_mmap_ctx(&current->perf_counter_ctx, mmap_event);
2223
2224 kfree(buf);
2225}
2226
2227void perf_counter_mmap(unsigned long addr, unsigned long len,
2228 unsigned long pgoff, struct file *file)
2229{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002230 struct perf_mmap_event mmap_event;
2231
2232 if (!atomic_read(&nr_mmap_tracking))
2233 return;
2234
2235 mmap_event = (struct perf_mmap_event){
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002236 .file = file,
2237 .event = {
2238 .header = { .type = PERF_EVENT_MMAP, },
2239 .pid = current->group_leader->pid,
2240 .tid = current->pid,
2241 .start = addr,
2242 .len = len,
2243 .pgoff = pgoff,
2244 },
2245 };
2246
2247 perf_counter_mmap_event(&mmap_event);
2248}
2249
2250void perf_counter_munmap(unsigned long addr, unsigned long len,
2251 unsigned long pgoff, struct file *file)
2252{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002253 struct perf_mmap_event mmap_event;
2254
2255 if (!atomic_read(&nr_munmap_tracking))
2256 return;
2257
2258 mmap_event = (struct perf_mmap_event){
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002259 .file = file,
2260 .event = {
2261 .header = { .type = PERF_EVENT_MUNMAP, },
2262 .pid = current->group_leader->pid,
2263 .tid = current->pid,
2264 .start = addr,
2265 .len = len,
2266 .pgoff = pgoff,
2267 },
2268 };
2269
2270 perf_counter_mmap_event(&mmap_event);
2271}
2272
2273/*
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002274 * Generic counter overflow handling.
2275 */
2276
2277int perf_counter_overflow(struct perf_counter *counter,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002278 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002279{
Peter Zijlstra79f14642009-04-06 11:45:07 +02002280 int events = atomic_read(&counter->event_limit);
2281 int ret = 0;
2282
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002283 counter->pending_kill = POLL_IN;
Peter Zijlstra79f14642009-04-06 11:45:07 +02002284 if (events && atomic_dec_and_test(&counter->event_limit)) {
2285 ret = 1;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002286 counter->pending_kill = POLL_HUP;
Peter Zijlstra79f14642009-04-06 11:45:07 +02002287 if (nmi) {
2288 counter->pending_disable = 1;
2289 perf_pending_queue(&counter->pending,
2290 perf_pending_counter);
2291 } else
2292 perf_counter_disable(counter);
2293 }
2294
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002295 perf_counter_output(counter, nmi, regs, addr);
Peter Zijlstra79f14642009-04-06 11:45:07 +02002296 return ret;
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002297}
2298
2299/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002300 * Generic software counter infrastructure
2301 */
2302
2303static void perf_swcounter_update(struct perf_counter *counter)
2304{
2305 struct hw_perf_counter *hwc = &counter->hw;
2306 u64 prev, now;
2307 s64 delta;
2308
2309again:
2310 prev = atomic64_read(&hwc->prev_count);
2311 now = atomic64_read(&hwc->count);
2312 if (atomic64_cmpxchg(&hwc->prev_count, prev, now) != prev)
2313 goto again;
2314
2315 delta = now - prev;
2316
2317 atomic64_add(delta, &counter->count);
2318 atomic64_sub(delta, &hwc->period_left);
2319}
2320
2321static void perf_swcounter_set_period(struct perf_counter *counter)
2322{
2323 struct hw_perf_counter *hwc = &counter->hw;
2324 s64 left = atomic64_read(&hwc->period_left);
2325 s64 period = hwc->irq_period;
2326
2327 if (unlikely(left <= -period)) {
2328 left = period;
2329 atomic64_set(&hwc->period_left, left);
2330 }
2331
2332 if (unlikely(left <= 0)) {
2333 left += period;
2334 atomic64_add(period, &hwc->period_left);
2335 }
2336
2337 atomic64_set(&hwc->prev_count, -left);
2338 atomic64_set(&hwc->count, -left);
2339}
2340
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002341static enum hrtimer_restart perf_swcounter_hrtimer(struct hrtimer *hrtimer)
2342{
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002343 enum hrtimer_restart ret = HRTIMER_RESTART;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002344 struct perf_counter *counter;
2345 struct pt_regs *regs;
2346
2347 counter = container_of(hrtimer, struct perf_counter, hw.hrtimer);
Robert Richter4aeb0b42009-04-29 12:47:03 +02002348 counter->pmu->read(counter);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002349
2350 regs = get_irq_regs();
2351 /*
2352 * In case we exclude kernel IPs or are somehow not in interrupt
2353 * context, provide the next best thing, the user IP.
2354 */
2355 if ((counter->hw_event.exclude_kernel || !regs) &&
2356 !counter->hw_event.exclude_user)
2357 regs = task_pt_regs(current);
2358
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002359 if (regs) {
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002360 if (perf_counter_overflow(counter, 0, regs, 0))
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002361 ret = HRTIMER_NORESTART;
2362 }
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002363
2364 hrtimer_forward_now(hrtimer, ns_to_ktime(counter->hw.irq_period));
2365
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002366 return ret;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002367}
2368
2369static void perf_swcounter_overflow(struct perf_counter *counter,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002370 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002371{
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002372 perf_swcounter_update(counter);
2373 perf_swcounter_set_period(counter);
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002374 if (perf_counter_overflow(counter, nmi, regs, addr))
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002375 /* soft-disable the counter */
2376 ;
2377
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002378}
2379
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002380static int perf_swcounter_match(struct perf_counter *counter,
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002381 enum perf_event_types type,
2382 u32 event, struct pt_regs *regs)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002383{
2384 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
2385 return 0;
2386
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002387 if (perf_event_raw(&counter->hw_event))
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002388 return 0;
2389
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002390 if (perf_event_type(&counter->hw_event) != type)
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002391 return 0;
2392
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002393 if (perf_event_id(&counter->hw_event) != event)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002394 return 0;
2395
2396 if (counter->hw_event.exclude_user && user_mode(regs))
2397 return 0;
2398
2399 if (counter->hw_event.exclude_kernel && !user_mode(regs))
2400 return 0;
2401
2402 return 1;
2403}
2404
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002405static void perf_swcounter_add(struct perf_counter *counter, u64 nr,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002406 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002407{
2408 int neg = atomic64_add_negative(nr, &counter->hw.count);
2409 if (counter->hw.irq_period && !neg)
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002410 perf_swcounter_overflow(counter, nmi, regs, addr);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002411}
2412
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002413static void perf_swcounter_ctx_event(struct perf_counter_context *ctx,
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002414 enum perf_event_types type, u32 event,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002415 u64 nr, int nmi, struct pt_regs *regs,
2416 u64 addr)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002417{
2418 struct perf_counter *counter;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002419
Peter Zijlstra01ef09d2009-03-19 20:26:11 +01002420 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002421 return;
2422
Peter Zijlstra592903c2009-03-13 12:21:36 +01002423 rcu_read_lock();
2424 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002425 if (perf_swcounter_match(counter, type, event, regs))
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002426 perf_swcounter_add(counter, nr, nmi, regs, addr);
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002427 }
Peter Zijlstra592903c2009-03-13 12:21:36 +01002428 rcu_read_unlock();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002429}
2430
Peter Zijlstra96f6d442009-03-23 18:22:07 +01002431static int *perf_swcounter_recursion_context(struct perf_cpu_context *cpuctx)
2432{
2433 if (in_nmi())
2434 return &cpuctx->recursion[3];
2435
2436 if (in_irq())
2437 return &cpuctx->recursion[2];
2438
2439 if (in_softirq())
2440 return &cpuctx->recursion[1];
2441
2442 return &cpuctx->recursion[0];
2443}
2444
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002445static void __perf_swcounter_event(enum perf_event_types type, u32 event,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002446 u64 nr, int nmi, struct pt_regs *regs,
2447 u64 addr)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002448{
2449 struct perf_cpu_context *cpuctx = &get_cpu_var(perf_cpu_context);
Peter Zijlstra96f6d442009-03-23 18:22:07 +01002450 int *recursion = perf_swcounter_recursion_context(cpuctx);
2451
2452 if (*recursion)
2453 goto out;
2454
2455 (*recursion)++;
2456 barrier();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002457
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002458 perf_swcounter_ctx_event(&cpuctx->ctx, type, event,
2459 nr, nmi, regs, addr);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002460 if (cpuctx->task_ctx) {
2461 perf_swcounter_ctx_event(cpuctx->task_ctx, type, event,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002462 nr, nmi, regs, addr);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002463 }
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002464
Peter Zijlstra96f6d442009-03-23 18:22:07 +01002465 barrier();
2466 (*recursion)--;
2467
2468out:
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002469 put_cpu_var(perf_cpu_context);
2470}
2471
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002472void
2473perf_swcounter_event(u32 event, u64 nr, int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002474{
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002475 __perf_swcounter_event(PERF_TYPE_SOFTWARE, event, nr, nmi, regs, addr);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002476}
2477
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002478static void perf_swcounter_read(struct perf_counter *counter)
2479{
2480 perf_swcounter_update(counter);
2481}
2482
2483static int perf_swcounter_enable(struct perf_counter *counter)
2484{
2485 perf_swcounter_set_period(counter);
2486 return 0;
2487}
2488
2489static void perf_swcounter_disable(struct perf_counter *counter)
2490{
2491 perf_swcounter_update(counter);
2492}
2493
Robert Richter4aeb0b42009-04-29 12:47:03 +02002494static const struct pmu perf_ops_generic = {
Peter Zijlstraac17dc82009-03-13 12:21:34 +01002495 .enable = perf_swcounter_enable,
2496 .disable = perf_swcounter_disable,
2497 .read = perf_swcounter_read,
2498};
2499
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002500/*
2501 * Software counter: cpu wall time clock
2502 */
2503
Paul Mackerras9abf8a02009-01-09 16:26:43 +11002504static void cpu_clock_perf_counter_update(struct perf_counter *counter)
2505{
2506 int cpu = raw_smp_processor_id();
2507 s64 prev;
2508 u64 now;
2509
2510 now = cpu_clock(cpu);
2511 prev = atomic64_read(&counter->hw.prev_count);
2512 atomic64_set(&counter->hw.prev_count, now);
2513 atomic64_add(now - prev, &counter->count);
2514}
2515
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002516static int cpu_clock_perf_counter_enable(struct perf_counter *counter)
2517{
2518 struct hw_perf_counter *hwc = &counter->hw;
2519 int cpu = raw_smp_processor_id();
2520
2521 atomic64_set(&hwc->prev_count, cpu_clock(cpu));
Peter Zijlstra039fc912009-03-13 16:43:47 +01002522 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2523 hwc->hrtimer.function = perf_swcounter_hrtimer;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002524 if (hwc->irq_period) {
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002525 __hrtimer_start_range_ns(&hwc->hrtimer,
2526 ns_to_ktime(hwc->irq_period), 0,
2527 HRTIMER_MODE_REL, 0);
2528 }
2529
2530 return 0;
2531}
2532
Ingo Molnar5c92d122008-12-11 13:21:10 +01002533static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
2534{
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002535 hrtimer_cancel(&counter->hw.hrtimer);
Paul Mackerras9abf8a02009-01-09 16:26:43 +11002536 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01002537}
2538
2539static void cpu_clock_perf_counter_read(struct perf_counter *counter)
2540{
Paul Mackerras9abf8a02009-01-09 16:26:43 +11002541 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01002542}
2543
Robert Richter4aeb0b42009-04-29 12:47:03 +02002544static const struct pmu perf_ops_cpu_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +01002545 .enable = cpu_clock_perf_counter_enable,
2546 .disable = cpu_clock_perf_counter_disable,
2547 .read = cpu_clock_perf_counter_read,
Ingo Molnar5c92d122008-12-11 13:21:10 +01002548};
2549
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01002550/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002551 * Software counter: task time clock
2552 */
2553
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02002554static void task_clock_perf_counter_update(struct perf_counter *counter, u64 now)
Ingo Molnarbae43c92008-12-11 14:03:20 +01002555{
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02002556 u64 prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002557 s64 delta;
Ingo Molnarbae43c92008-12-11 14:03:20 +01002558
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02002559 prev = atomic64_xchg(&counter->hw.prev_count, now);
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002560 delta = now - prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002561 atomic64_add(delta, &counter->count);
Ingo Molnarbae43c92008-12-11 14:03:20 +01002562}
2563
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01002564static int task_clock_perf_counter_enable(struct perf_counter *counter)
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002565{
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002566 struct hw_perf_counter *hwc = &counter->hw;
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02002567 u64 now;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002568
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02002569 now = counter->ctx->time;
2570
2571 atomic64_set(&hwc->prev_count, now);
Peter Zijlstra039fc912009-03-13 16:43:47 +01002572 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2573 hwc->hrtimer.function = perf_swcounter_hrtimer;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002574 if (hwc->irq_period) {
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002575 __hrtimer_start_range_ns(&hwc->hrtimer,
2576 ns_to_ktime(hwc->irq_period), 0,
2577 HRTIMER_MODE_REL, 0);
2578 }
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01002579
2580 return 0;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002581}
2582
2583static void task_clock_perf_counter_disable(struct perf_counter *counter)
2584{
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002585 hrtimer_cancel(&counter->hw.hrtimer);
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02002586 task_clock_perf_counter_update(counter, counter->ctx->time);
2587
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002588}
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01002589
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002590static void task_clock_perf_counter_read(struct perf_counter *counter)
2591{
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02002592 u64 time;
2593
2594 if (!in_nmi()) {
2595 update_context_time(counter->ctx);
2596 time = counter->ctx->time;
2597 } else {
2598 u64 now = perf_clock();
2599 u64 delta = now - counter->ctx->timestamp;
2600 time = counter->ctx->time + delta;
2601 }
2602
2603 task_clock_perf_counter_update(counter, time);
Ingo Molnarbae43c92008-12-11 14:03:20 +01002604}
2605
Robert Richter4aeb0b42009-04-29 12:47:03 +02002606static const struct pmu perf_ops_task_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +01002607 .enable = task_clock_perf_counter_enable,
2608 .disable = task_clock_perf_counter_disable,
2609 .read = task_clock_perf_counter_read,
Ingo Molnarbae43c92008-12-11 14:03:20 +01002610};
2611
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002612/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002613 * Software counter: cpu migrations
2614 */
2615
Paul Mackerras23a185c2009-02-09 22:42:47 +11002616static inline u64 get_cpu_migrations(struct perf_counter *counter)
Ingo Molnar6c594c22008-12-14 12:34:15 +01002617{
Paul Mackerras23a185c2009-02-09 22:42:47 +11002618 struct task_struct *curr = counter->ctx->task;
2619
2620 if (curr)
2621 return curr->se.nr_migrations;
2622 return cpu_nr_migrations(smp_processor_id());
Ingo Molnar6c594c22008-12-14 12:34:15 +01002623}
2624
2625static void cpu_migrations_perf_counter_update(struct perf_counter *counter)
2626{
2627 u64 prev, now;
2628 s64 delta;
2629
2630 prev = atomic64_read(&counter->hw.prev_count);
Paul Mackerras23a185c2009-02-09 22:42:47 +11002631 now = get_cpu_migrations(counter);
Ingo Molnar6c594c22008-12-14 12:34:15 +01002632
2633 atomic64_set(&counter->hw.prev_count, now);
2634
2635 delta = now - prev;
Ingo Molnar6c594c22008-12-14 12:34:15 +01002636
2637 atomic64_add(delta, &counter->count);
2638}
2639
2640static void cpu_migrations_perf_counter_read(struct perf_counter *counter)
2641{
2642 cpu_migrations_perf_counter_update(counter);
2643}
2644
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01002645static int cpu_migrations_perf_counter_enable(struct perf_counter *counter)
Ingo Molnar6c594c22008-12-14 12:34:15 +01002646{
Paul Mackerrasc07c99b2009-02-13 22:10:34 +11002647 if (counter->prev_state <= PERF_COUNTER_STATE_OFF)
2648 atomic64_set(&counter->hw.prev_count,
2649 get_cpu_migrations(counter));
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01002650 return 0;
Ingo Molnar6c594c22008-12-14 12:34:15 +01002651}
2652
2653static void cpu_migrations_perf_counter_disable(struct perf_counter *counter)
2654{
2655 cpu_migrations_perf_counter_update(counter);
2656}
2657
Robert Richter4aeb0b42009-04-29 12:47:03 +02002658static const struct pmu perf_ops_cpu_migrations = {
Ingo Molnar76715812008-12-17 14:20:28 +01002659 .enable = cpu_migrations_perf_counter_enable,
2660 .disable = cpu_migrations_perf_counter_disable,
2661 .read = cpu_migrations_perf_counter_read,
Ingo Molnar6c594c22008-12-14 12:34:15 +01002662};
2663
Peter Zijlstrae077df42009-03-19 20:26:17 +01002664#ifdef CONFIG_EVENT_PROFILE
2665void perf_tpcounter_event(int event_id)
2666{
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002667 struct pt_regs *regs = get_irq_regs();
2668
2669 if (!regs)
2670 regs = task_pt_regs(current);
2671
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002672 __perf_swcounter_event(PERF_TYPE_TRACEPOINT, event_id, 1, 1, regs, 0);
Peter Zijlstrae077df42009-03-19 20:26:17 +01002673}
Steven Whitehouseff7b1b42009-04-15 16:55:05 +01002674EXPORT_SYMBOL_GPL(perf_tpcounter_event);
Peter Zijlstrae077df42009-03-19 20:26:17 +01002675
2676extern int ftrace_profile_enable(int);
2677extern void ftrace_profile_disable(int);
2678
2679static void tp_perf_counter_destroy(struct perf_counter *counter)
2680{
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002681 ftrace_profile_disable(perf_event_id(&counter->hw_event));
Peter Zijlstrae077df42009-03-19 20:26:17 +01002682}
2683
Robert Richter4aeb0b42009-04-29 12:47:03 +02002684static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
Peter Zijlstrae077df42009-03-19 20:26:17 +01002685{
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002686 int event_id = perf_event_id(&counter->hw_event);
Peter Zijlstrae077df42009-03-19 20:26:17 +01002687 int ret;
2688
2689 ret = ftrace_profile_enable(event_id);
2690 if (ret)
2691 return NULL;
2692
2693 counter->destroy = tp_perf_counter_destroy;
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002694 counter->hw.irq_period = counter->hw_event.irq_period;
Peter Zijlstrae077df42009-03-19 20:26:17 +01002695
2696 return &perf_ops_generic;
2697}
2698#else
Robert Richter4aeb0b42009-04-29 12:47:03 +02002699static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
Peter Zijlstrae077df42009-03-19 20:26:17 +01002700{
2701 return NULL;
2702}
2703#endif
2704
Robert Richter4aeb0b42009-04-29 12:47:03 +02002705static const struct pmu *sw_perf_counter_init(struct perf_counter *counter)
Ingo Molnar5c92d122008-12-11 13:21:10 +01002706{
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002707 struct perf_counter_hw_event *hw_event = &counter->hw_event;
Robert Richter4aeb0b42009-04-29 12:47:03 +02002708 const struct pmu *pmu = NULL;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002709 struct hw_perf_counter *hwc = &counter->hw;
Ingo Molnar5c92d122008-12-11 13:21:10 +01002710
Paul Mackerras0475f9e2009-02-11 14:35:35 +11002711 /*
2712 * Software counters (currently) can't in general distinguish
2713 * between user, kernel and hypervisor events.
2714 * However, context switches and cpu migrations are considered
2715 * to be kernel events, and page faults are never hypervisor
2716 * events.
2717 */
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002718 switch (perf_event_id(&counter->hw_event)) {
Ingo Molnar5c92d122008-12-11 13:21:10 +01002719 case PERF_COUNT_CPU_CLOCK:
Robert Richter4aeb0b42009-04-29 12:47:03 +02002720 pmu = &perf_ops_cpu_clock;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002721
2722 if (hw_event->irq_period && hw_event->irq_period < 10000)
2723 hw_event->irq_period = 10000;
Ingo Molnar5c92d122008-12-11 13:21:10 +01002724 break;
Ingo Molnarbae43c92008-12-11 14:03:20 +01002725 case PERF_COUNT_TASK_CLOCK:
Paul Mackerras23a185c2009-02-09 22:42:47 +11002726 /*
2727 * If the user instantiates this as a per-cpu counter,
2728 * use the cpu_clock counter instead.
2729 */
2730 if (counter->ctx->task)
Robert Richter4aeb0b42009-04-29 12:47:03 +02002731 pmu = &perf_ops_task_clock;
Paul Mackerras23a185c2009-02-09 22:42:47 +11002732 else
Robert Richter4aeb0b42009-04-29 12:47:03 +02002733 pmu = &perf_ops_cpu_clock;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002734
2735 if (hw_event->irq_period && hw_event->irq_period < 10000)
2736 hw_event->irq_period = 10000;
Ingo Molnarbae43c92008-12-11 14:03:20 +01002737 break;
Ingo Molnare06c61a2008-12-14 14:44:31 +01002738 case PERF_COUNT_PAGE_FAULTS:
Peter Zijlstraac17dc82009-03-13 12:21:34 +01002739 case PERF_COUNT_PAGE_FAULTS_MIN:
2740 case PERF_COUNT_PAGE_FAULTS_MAJ:
Ingo Molnar5d6a27d2008-12-14 12:28:33 +01002741 case PERF_COUNT_CONTEXT_SWITCHES:
Robert Richter4aeb0b42009-04-29 12:47:03 +02002742 pmu = &perf_ops_generic;
Ingo Molnar5d6a27d2008-12-14 12:28:33 +01002743 break;
Ingo Molnar6c594c22008-12-14 12:34:15 +01002744 case PERF_COUNT_CPU_MIGRATIONS:
Paul Mackerras0475f9e2009-02-11 14:35:35 +11002745 if (!counter->hw_event.exclude_kernel)
Robert Richter4aeb0b42009-04-29 12:47:03 +02002746 pmu = &perf_ops_cpu_migrations;
Ingo Molnar6c594c22008-12-14 12:34:15 +01002747 break;
Ingo Molnar5c92d122008-12-11 13:21:10 +01002748 }
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002749
Robert Richter4aeb0b42009-04-29 12:47:03 +02002750 if (pmu)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002751 hwc->irq_period = hw_event->irq_period;
2752
Robert Richter4aeb0b42009-04-29 12:47:03 +02002753 return pmu;
Ingo Molnar5c92d122008-12-11 13:21:10 +01002754}
2755
Thomas Gleixner0793a612008-12-04 20:12:29 +01002756/*
2757 * Allocate and initialize a counter structure
2758 */
2759static struct perf_counter *
Ingo Molnar04289bb2008-12-11 08:38:42 +01002760perf_counter_alloc(struct perf_counter_hw_event *hw_event,
2761 int cpu,
Paul Mackerras23a185c2009-02-09 22:42:47 +11002762 struct perf_counter_context *ctx,
Ingo Molnar9b51f662008-12-12 13:49:45 +01002763 struct perf_counter *group_leader,
2764 gfp_t gfpflags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01002765{
Robert Richter4aeb0b42009-04-29 12:47:03 +02002766 const struct pmu *pmu;
Ingo Molnar621a01e2008-12-11 12:46:46 +01002767 struct perf_counter *counter;
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02002768 long err;
Thomas Gleixner0793a612008-12-04 20:12:29 +01002769
Ingo Molnar9b51f662008-12-12 13:49:45 +01002770 counter = kzalloc(sizeof(*counter), gfpflags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002771 if (!counter)
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02002772 return ERR_PTR(-ENOMEM);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002773
Ingo Molnar04289bb2008-12-11 08:38:42 +01002774 /*
2775 * Single counters are their own group leaders, with an
2776 * empty sibling list:
2777 */
2778 if (!group_leader)
2779 group_leader = counter;
2780
Thomas Gleixner0793a612008-12-04 20:12:29 +01002781 mutex_init(&counter->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01002782 INIT_LIST_HEAD(&counter->list_entry);
Peter Zijlstra592903c2009-03-13 12:21:36 +01002783 INIT_LIST_HEAD(&counter->event_entry);
Ingo Molnar04289bb2008-12-11 08:38:42 +01002784 INIT_LIST_HEAD(&counter->sibling_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002785 init_waitqueue_head(&counter->waitq);
2786
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002787 mutex_init(&counter->mmap_mutex);
2788
Paul Mackerrasd859e292009-01-17 18:10:22 +11002789 INIT_LIST_HEAD(&counter->child_list);
2790
Ingo Molnar9f66a382008-12-10 12:33:23 +01002791 counter->cpu = cpu;
2792 counter->hw_event = *hw_event;
Ingo Molnar04289bb2008-12-11 08:38:42 +01002793 counter->group_leader = group_leader;
Robert Richter4aeb0b42009-04-29 12:47:03 +02002794 counter->pmu = NULL;
Paul Mackerras23a185c2009-02-09 22:42:47 +11002795 counter->ctx = ctx;
Ingo Molnar621a01e2008-12-11 12:46:46 +01002796
Ingo Molnar235c7fc2008-12-21 14:43:25 +01002797 counter->state = PERF_COUNTER_STATE_INACTIVE;
Ingo Molnara86ed502008-12-17 00:43:10 +01002798 if (hw_event->disabled)
2799 counter->state = PERF_COUNTER_STATE_OFF;
2800
Robert Richter4aeb0b42009-04-29 12:47:03 +02002801 pmu = NULL;
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002802
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002803 if (perf_event_raw(hw_event)) {
Robert Richter4aeb0b42009-04-29 12:47:03 +02002804 pmu = hw_perf_counter_init(counter);
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002805 goto done;
2806 }
2807
2808 switch (perf_event_type(hw_event)) {
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002809 case PERF_TYPE_HARDWARE:
Robert Richter4aeb0b42009-04-29 12:47:03 +02002810 pmu = hw_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002811 break;
2812
2813 case PERF_TYPE_SOFTWARE:
Robert Richter4aeb0b42009-04-29 12:47:03 +02002814 pmu = sw_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002815 break;
2816
2817 case PERF_TYPE_TRACEPOINT:
Robert Richter4aeb0b42009-04-29 12:47:03 +02002818 pmu = tp_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002819 break;
2820 }
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002821done:
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02002822 err = 0;
Robert Richter4aeb0b42009-04-29 12:47:03 +02002823 if (!pmu)
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02002824 err = -EINVAL;
Robert Richter4aeb0b42009-04-29 12:47:03 +02002825 else if (IS_ERR(pmu))
2826 err = PTR_ERR(pmu);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02002827
2828 if (err) {
2829 kfree(counter);
2830 return ERR_PTR(err);
2831 }
2832
Robert Richter4aeb0b42009-04-29 12:47:03 +02002833 counter->pmu = pmu;
Thomas Gleixner0793a612008-12-04 20:12:29 +01002834
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002835 if (counter->hw_event.mmap)
2836 atomic_inc(&nr_mmap_tracking);
2837 if (counter->hw_event.munmap)
2838 atomic_inc(&nr_munmap_tracking);
2839 if (counter->hw_event.comm)
2840 atomic_inc(&nr_comm_tracking);
2841
Thomas Gleixner0793a612008-12-04 20:12:29 +01002842 return counter;
2843}
2844
2845/**
Paul Mackerras2743a5b2009-03-04 20:36:51 +11002846 * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
Ingo Molnar9f66a382008-12-10 12:33:23 +01002847 *
2848 * @hw_event_uptr: event type attributes for monitoring/sampling
Thomas Gleixner0793a612008-12-04 20:12:29 +01002849 * @pid: target pid
Ingo Molnar9f66a382008-12-10 12:33:23 +01002850 * @cpu: target cpu
2851 * @group_fd: group leader counter fd
Thomas Gleixner0793a612008-12-04 20:12:29 +01002852 */
Paul Mackerras2743a5b2009-03-04 20:36:51 +11002853SYSCALL_DEFINE5(perf_counter_open,
Paul Mackerrasf3dfd262009-02-26 22:43:46 +11002854 const struct perf_counter_hw_event __user *, hw_event_uptr,
Paul Mackerras2743a5b2009-03-04 20:36:51 +11002855 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01002856{
Ingo Molnar04289bb2008-12-11 08:38:42 +01002857 struct perf_counter *counter, *group_leader;
Ingo Molnar9f66a382008-12-10 12:33:23 +01002858 struct perf_counter_hw_event hw_event;
Ingo Molnar04289bb2008-12-11 08:38:42 +01002859 struct perf_counter_context *ctx;
Ingo Molnar9b51f662008-12-12 13:49:45 +01002860 struct file *counter_file = NULL;
Ingo Molnar04289bb2008-12-11 08:38:42 +01002861 struct file *group_file = NULL;
2862 int fput_needed = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01002863 int fput_needed2 = 0;
Thomas Gleixner0793a612008-12-04 20:12:29 +01002864 int ret;
2865
Paul Mackerras2743a5b2009-03-04 20:36:51 +11002866 /* for future expandability... */
2867 if (flags)
2868 return -EINVAL;
2869
Ingo Molnar9f66a382008-12-10 12:33:23 +01002870 if (copy_from_user(&hw_event, hw_event_uptr, sizeof(hw_event)) != 0)
Thomas Gleixnereab656a2008-12-08 19:26:59 +01002871 return -EFAULT;
2872
Ingo Molnar04289bb2008-12-11 08:38:42 +01002873 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01002874 * Get the target context (task or percpu):
2875 */
2876 ctx = find_get_context(pid, cpu);
2877 if (IS_ERR(ctx))
2878 return PTR_ERR(ctx);
2879
2880 /*
2881 * Look up the group leader (we will attach this counter to it):
Ingo Molnar04289bb2008-12-11 08:38:42 +01002882 */
2883 group_leader = NULL;
2884 if (group_fd != -1) {
2885 ret = -EINVAL;
2886 group_file = fget_light(group_fd, &fput_needed);
2887 if (!group_file)
Ingo Molnarccff2862008-12-11 11:26:29 +01002888 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01002889 if (group_file->f_op != &perf_fops)
Ingo Molnarccff2862008-12-11 11:26:29 +01002890 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01002891
2892 group_leader = group_file->private_data;
2893 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01002894 * Do not allow a recursive hierarchy (this new sibling
2895 * becoming part of another group-sibling):
Ingo Molnar04289bb2008-12-11 08:38:42 +01002896 */
Ingo Molnarccff2862008-12-11 11:26:29 +01002897 if (group_leader->group_leader != group_leader)
2898 goto err_put_context;
2899 /*
2900 * Do not allow to attach to a group in a different
2901 * task or CPU context:
2902 */
2903 if (group_leader->ctx != ctx)
2904 goto err_put_context;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11002905 /*
2906 * Only a group leader can be exclusive or pinned
2907 */
2908 if (hw_event.exclusive || hw_event.pinned)
2909 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01002910 }
2911
Paul Mackerras23a185c2009-02-09 22:42:47 +11002912 counter = perf_counter_alloc(&hw_event, cpu, ctx, group_leader,
2913 GFP_KERNEL);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02002914 ret = PTR_ERR(counter);
2915 if (IS_ERR(counter))
Thomas Gleixner0793a612008-12-04 20:12:29 +01002916 goto err_put_context;
2917
Thomas Gleixner0793a612008-12-04 20:12:29 +01002918 ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
2919 if (ret < 0)
Ingo Molnar9b51f662008-12-12 13:49:45 +01002920 goto err_free_put_context;
2921
2922 counter_file = fget_light(ret, &fput_needed2);
2923 if (!counter_file)
2924 goto err_free_put_context;
2925
2926 counter->filp = counter_file;
Paul Mackerrasd859e292009-01-17 18:10:22 +11002927 mutex_lock(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01002928 perf_install_in_context(ctx, counter, cpu);
Paul Mackerrasd859e292009-01-17 18:10:22 +11002929 mutex_unlock(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01002930
2931 fput_light(counter_file, fput_needed2);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002932
Ingo Molnar04289bb2008-12-11 08:38:42 +01002933out_fput:
2934 fput_light(group_file, fput_needed);
2935
Thomas Gleixner0793a612008-12-04 20:12:29 +01002936 return ret;
2937
Ingo Molnar9b51f662008-12-12 13:49:45 +01002938err_free_put_context:
Thomas Gleixner0793a612008-12-04 20:12:29 +01002939 kfree(counter);
2940
2941err_put_context:
2942 put_context(ctx);
2943
Ingo Molnar04289bb2008-12-11 08:38:42 +01002944 goto out_fput;
Thomas Gleixner0793a612008-12-04 20:12:29 +01002945}
2946
Ingo Molnar9b51f662008-12-12 13:49:45 +01002947/*
2948 * Initialize the perf_counter context in a task_struct:
2949 */
2950static void
2951__perf_counter_init_context(struct perf_counter_context *ctx,
2952 struct task_struct *task)
2953{
2954 memset(ctx, 0, sizeof(*ctx));
2955 spin_lock_init(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +11002956 mutex_init(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01002957 INIT_LIST_HEAD(&ctx->counter_list);
Peter Zijlstra592903c2009-03-13 12:21:36 +01002958 INIT_LIST_HEAD(&ctx->event_list);
Ingo Molnar9b51f662008-12-12 13:49:45 +01002959 ctx->task = task;
2960}
2961
2962/*
2963 * inherit a counter from parent task to child task:
2964 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11002965static struct perf_counter *
Ingo Molnar9b51f662008-12-12 13:49:45 +01002966inherit_counter(struct perf_counter *parent_counter,
2967 struct task_struct *parent,
2968 struct perf_counter_context *parent_ctx,
2969 struct task_struct *child,
Paul Mackerrasd859e292009-01-17 18:10:22 +11002970 struct perf_counter *group_leader,
Ingo Molnar9b51f662008-12-12 13:49:45 +01002971 struct perf_counter_context *child_ctx)
2972{
2973 struct perf_counter *child_counter;
2974
Paul Mackerrasd859e292009-01-17 18:10:22 +11002975 /*
2976 * Instead of creating recursive hierarchies of counters,
2977 * we link inherited counters back to the original parent,
2978 * which has a filp for sure, which we use as the reference
2979 * count:
2980 */
2981 if (parent_counter->parent)
2982 parent_counter = parent_counter->parent;
2983
Ingo Molnar9b51f662008-12-12 13:49:45 +01002984 child_counter = perf_counter_alloc(&parent_counter->hw_event,
Paul Mackerras23a185c2009-02-09 22:42:47 +11002985 parent_counter->cpu, child_ctx,
2986 group_leader, GFP_KERNEL);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02002987 if (IS_ERR(child_counter))
2988 return child_counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01002989
2990 /*
2991 * Link it up in the child's context:
2992 */
Ingo Molnar9b51f662008-12-12 13:49:45 +01002993 child_counter->task = child;
Paul Mackerras53cfbf52009-03-25 22:46:58 +11002994 add_counter_to_ctx(child_counter, child_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01002995
2996 child_counter->parent = parent_counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01002997 /*
2998 * inherit into child's child as well:
2999 */
3000 child_counter->hw_event.inherit = 1;
3001
3002 /*
3003 * Get a reference to the parent filp - we will fput it
3004 * when the child counter exits. This is safe to do because
3005 * we are in the parent and we know that the filp still
3006 * exists and has a nonzero count:
3007 */
3008 atomic_long_inc(&parent_counter->filp->f_count);
3009
Paul Mackerrasd859e292009-01-17 18:10:22 +11003010 /*
3011 * Link this into the parent counter's child list
3012 */
3013 mutex_lock(&parent_counter->mutex);
3014 list_add_tail(&child_counter->child_list, &parent_counter->child_list);
3015
3016 /*
3017 * Make the child state follow the state of the parent counter,
3018 * not its hw_event.disabled bit. We hold the parent's mutex,
3019 * so we won't race with perf_counter_{en,dis}able_family.
3020 */
3021 if (parent_counter->state >= PERF_COUNTER_STATE_INACTIVE)
3022 child_counter->state = PERF_COUNTER_STATE_INACTIVE;
3023 else
3024 child_counter->state = PERF_COUNTER_STATE_OFF;
3025
3026 mutex_unlock(&parent_counter->mutex);
3027
3028 return child_counter;
3029}
3030
3031static int inherit_group(struct perf_counter *parent_counter,
3032 struct task_struct *parent,
3033 struct perf_counter_context *parent_ctx,
3034 struct task_struct *child,
3035 struct perf_counter_context *child_ctx)
3036{
3037 struct perf_counter *leader;
3038 struct perf_counter *sub;
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003039 struct perf_counter *child_ctr;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003040
3041 leader = inherit_counter(parent_counter, parent, parent_ctx,
3042 child, NULL, child_ctx);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003043 if (IS_ERR(leader))
3044 return PTR_ERR(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003045 list_for_each_entry(sub, &parent_counter->sibling_list, list_entry) {
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003046 child_ctr = inherit_counter(sub, parent, parent_ctx,
3047 child, leader, child_ctx);
3048 if (IS_ERR(child_ctr))
3049 return PTR_ERR(child_ctr);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003050 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01003051 return 0;
3052}
3053
Paul Mackerrasd859e292009-01-17 18:10:22 +11003054static void sync_child_counter(struct perf_counter *child_counter,
3055 struct perf_counter *parent_counter)
3056{
3057 u64 parent_val, child_val;
3058
3059 parent_val = atomic64_read(&parent_counter->count);
3060 child_val = atomic64_read(&child_counter->count);
3061
3062 /*
3063 * Add back the child's count to the parent's count:
3064 */
3065 atomic64_add(child_val, &parent_counter->count);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11003066 atomic64_add(child_counter->total_time_enabled,
3067 &parent_counter->child_total_time_enabled);
3068 atomic64_add(child_counter->total_time_running,
3069 &parent_counter->child_total_time_running);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003070
3071 /*
3072 * Remove this counter from the parent's list
3073 */
3074 mutex_lock(&parent_counter->mutex);
3075 list_del_init(&child_counter->child_list);
3076 mutex_unlock(&parent_counter->mutex);
3077
3078 /*
3079 * Release the parent counter, if this was the last
3080 * reference to it.
3081 */
3082 fput(parent_counter->filp);
3083}
3084
Ingo Molnar9b51f662008-12-12 13:49:45 +01003085static void
3086__perf_counter_exit_task(struct task_struct *child,
3087 struct perf_counter *child_counter,
3088 struct perf_counter_context *child_ctx)
3089{
3090 struct perf_counter *parent_counter;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003091 struct perf_counter *sub, *tmp;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003092
3093 /*
Ingo Molnar235c7fc2008-12-21 14:43:25 +01003094 * If we do not self-reap then we have to wait for the
3095 * child task to unschedule (it will happen for sure),
3096 * so that its counter is at its final count. (This
3097 * condition triggers rarely - child tasks usually get
3098 * off their CPU before the parent has a chance to
3099 * get this far into the reaping action)
Ingo Molnar9b51f662008-12-12 13:49:45 +01003100 */
Ingo Molnar235c7fc2008-12-21 14:43:25 +01003101 if (child != current) {
3102 wait_task_inactive(child, 0);
3103 list_del_init(&child_counter->list_entry);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11003104 update_counter_times(child_counter);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01003105 } else {
Ingo Molnar0cc0c022008-12-14 23:20:36 +01003106 struct perf_cpu_context *cpuctx;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01003107 unsigned long flags;
3108 u64 perf_flags;
3109
3110 /*
3111 * Disable and unlink this counter.
3112 *
3113 * Be careful about zapping the list - IRQ/NMI context
3114 * could still be processing it:
3115 */
Peter Zijlstra849691a2009-04-06 11:45:12 +02003116 local_irq_save(flags);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01003117 perf_flags = hw_perf_save_disable();
Ingo Molnar0cc0c022008-12-14 23:20:36 +01003118
3119 cpuctx = &__get_cpu_var(perf_cpu_context);
3120
Paul Mackerrasd859e292009-01-17 18:10:22 +11003121 group_sched_out(child_counter, cpuctx, child_ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11003122 update_counter_times(child_counter);
Ingo Molnar0cc0c022008-12-14 23:20:36 +01003123
Ingo Molnar235c7fc2008-12-21 14:43:25 +01003124 list_del_init(&child_counter->list_entry);
3125
3126 child_ctx->nr_counters--;
3127
3128 hw_perf_restore(perf_flags);
Peter Zijlstra849691a2009-04-06 11:45:12 +02003129 local_irq_restore(flags);
Ingo Molnar0cc0c022008-12-14 23:20:36 +01003130 }
3131
Ingo Molnar9b51f662008-12-12 13:49:45 +01003132 parent_counter = child_counter->parent;
3133 /*
3134 * It can happen that parent exits first, and has counters
3135 * that are still around due to the child reference. These
3136 * counters need to be zapped - but otherwise linger.
3137 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11003138 if (parent_counter) {
3139 sync_child_counter(child_counter, parent_counter);
3140 list_for_each_entry_safe(sub, tmp, &child_counter->sibling_list,
3141 list_entry) {
Paul Mackerras4bcf3492009-02-11 13:53:19 +01003142 if (sub->parent) {
Paul Mackerrasd859e292009-01-17 18:10:22 +11003143 sync_child_counter(sub, sub->parent);
Peter Zijlstraf1600952009-03-19 20:26:16 +01003144 free_counter(sub);
Paul Mackerras4bcf3492009-02-11 13:53:19 +01003145 }
Paul Mackerrasd859e292009-01-17 18:10:22 +11003146 }
Peter Zijlstraf1600952009-03-19 20:26:16 +01003147 free_counter(child_counter);
Paul Mackerras4bcf3492009-02-11 13:53:19 +01003148 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01003149}
3150
3151/*
Paul Mackerrasd859e292009-01-17 18:10:22 +11003152 * When a child task exits, feed back counter values to parent counters.
Ingo Molnar9b51f662008-12-12 13:49:45 +01003153 *
Paul Mackerrasd859e292009-01-17 18:10:22 +11003154 * Note: we may be running in child context, but the PID is not hashed
Ingo Molnar9b51f662008-12-12 13:49:45 +01003155 * anymore so new counters will not be added.
3156 */
3157void perf_counter_exit_task(struct task_struct *child)
3158{
3159 struct perf_counter *child_counter, *tmp;
3160 struct perf_counter_context *child_ctx;
3161
3162 child_ctx = &child->perf_counter_ctx;
3163
3164 if (likely(!child_ctx->nr_counters))
3165 return;
3166
3167 list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list,
3168 list_entry)
3169 __perf_counter_exit_task(child, child_counter, child_ctx);
3170}
3171
3172/*
3173 * Initialize the perf_counter context in task_struct
3174 */
3175void perf_counter_init_task(struct task_struct *child)
3176{
3177 struct perf_counter_context *child_ctx, *parent_ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003178 struct perf_counter *counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003179 struct task_struct *parent = current;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003180
3181 child_ctx = &child->perf_counter_ctx;
3182 parent_ctx = &parent->perf_counter_ctx;
3183
3184 __perf_counter_init_context(child_ctx, child);
3185
3186 /*
3187 * This is executed from the parent task context, so inherit
3188 * counters that have been marked for cloning:
3189 */
3190
3191 if (likely(!parent_ctx->nr_counters))
3192 return;
3193
3194 /*
3195 * Lock the parent list. No need to lock the child - not PID
3196 * hashed yet and not running, so nobody can access it.
3197 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11003198 mutex_lock(&parent_ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003199
3200 /*
3201 * We dont have to disable NMIs - we are only looking at
3202 * the list, not manipulating it:
3203 */
3204 list_for_each_entry(counter, &parent_ctx->counter_list, list_entry) {
Paul Mackerrasd859e292009-01-17 18:10:22 +11003205 if (!counter->hw_event.inherit)
Ingo Molnar9b51f662008-12-12 13:49:45 +01003206 continue;
3207
Paul Mackerrasd859e292009-01-17 18:10:22 +11003208 if (inherit_group(counter, parent,
Ingo Molnar9b51f662008-12-12 13:49:45 +01003209 parent_ctx, child, child_ctx))
3210 break;
3211 }
3212
Paul Mackerrasd859e292009-01-17 18:10:22 +11003213 mutex_unlock(&parent_ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003214}
3215
Ingo Molnar04289bb2008-12-11 08:38:42 +01003216static void __cpuinit perf_counter_init_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003217{
Ingo Molnar04289bb2008-12-11 08:38:42 +01003218 struct perf_cpu_context *cpuctx;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003219
Ingo Molnar04289bb2008-12-11 08:38:42 +01003220 cpuctx = &per_cpu(perf_cpu_context, cpu);
3221 __perf_counter_init_context(&cpuctx->ctx, NULL);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003222
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003223 spin_lock(&perf_resource_lock);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003224 cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu;
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003225 spin_unlock(&perf_resource_lock);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003226
Paul Mackerras01d02872009-01-14 13:44:19 +11003227 hw_perf_counter_setup(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003228}
3229
3230#ifdef CONFIG_HOTPLUG_CPU
Ingo Molnar04289bb2008-12-11 08:38:42 +01003231static void __perf_counter_exit_cpu(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003232{
3233 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
3234 struct perf_counter_context *ctx = &cpuctx->ctx;
3235 struct perf_counter *counter, *tmp;
3236
Ingo Molnar04289bb2008-12-11 08:38:42 +01003237 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry)
3238 __perf_counter_remove_from_context(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003239}
Ingo Molnar04289bb2008-12-11 08:38:42 +01003240static void perf_counter_exit_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003241{
Paul Mackerrasd859e292009-01-17 18:10:22 +11003242 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
3243 struct perf_counter_context *ctx = &cpuctx->ctx;
3244
3245 mutex_lock(&ctx->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003246 smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003247 mutex_unlock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003248}
3249#else
Ingo Molnar04289bb2008-12-11 08:38:42 +01003250static inline void perf_counter_exit_cpu(int cpu) { }
Thomas Gleixner0793a612008-12-04 20:12:29 +01003251#endif
3252
3253static int __cpuinit
3254perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
3255{
3256 unsigned int cpu = (long)hcpu;
3257
3258 switch (action) {
3259
3260 case CPU_UP_PREPARE:
3261 case CPU_UP_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01003262 perf_counter_init_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003263 break;
3264
3265 case CPU_DOWN_PREPARE:
3266 case CPU_DOWN_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01003267 perf_counter_exit_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003268 break;
3269
3270 default:
3271 break;
3272 }
3273
3274 return NOTIFY_OK;
3275}
3276
3277static struct notifier_block __cpuinitdata perf_cpu_nb = {
3278 .notifier_call = perf_cpu_notify,
3279};
3280
Ingo Molnar0d905bc2009-05-04 19:13:30 +02003281void __init perf_counter_init(void)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003282{
3283 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
3284 (void *)(long)smp_processor_id());
3285 register_cpu_notifier(&perf_cpu_nb);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003286}
Thomas Gleixner0793a612008-12-04 20:12:29 +01003287
3288static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
3289{
3290 return sprintf(buf, "%d\n", perf_reserved_percpu);
3291}
3292
3293static ssize_t
3294perf_set_reserve_percpu(struct sysdev_class *class,
3295 const char *buf,
3296 size_t count)
3297{
3298 struct perf_cpu_context *cpuctx;
3299 unsigned long val;
3300 int err, cpu, mpt;
3301
3302 err = strict_strtoul(buf, 10, &val);
3303 if (err)
3304 return err;
3305 if (val > perf_max_counters)
3306 return -EINVAL;
3307
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003308 spin_lock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003309 perf_reserved_percpu = val;
3310 for_each_online_cpu(cpu) {
3311 cpuctx = &per_cpu(perf_cpu_context, cpu);
3312 spin_lock_irq(&cpuctx->ctx.lock);
3313 mpt = min(perf_max_counters - cpuctx->ctx.nr_counters,
3314 perf_max_counters - perf_reserved_percpu);
3315 cpuctx->max_pertask = mpt;
3316 spin_unlock_irq(&cpuctx->ctx.lock);
3317 }
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003318 spin_unlock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003319
3320 return count;
3321}
3322
3323static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
3324{
3325 return sprintf(buf, "%d\n", perf_overcommit);
3326}
3327
3328static ssize_t
3329perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
3330{
3331 unsigned long val;
3332 int err;
3333
3334 err = strict_strtoul(buf, 10, &val);
3335 if (err)
3336 return err;
3337 if (val > 1)
3338 return -EINVAL;
3339
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003340 spin_lock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003341 perf_overcommit = val;
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003342 spin_unlock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003343
3344 return count;
3345}
3346
3347static SYSDEV_CLASS_ATTR(
3348 reserve_percpu,
3349 0644,
3350 perf_show_reserve_percpu,
3351 perf_set_reserve_percpu
3352 );
3353
3354static SYSDEV_CLASS_ATTR(
3355 overcommit,
3356 0644,
3357 perf_show_overcommit,
3358 perf_set_overcommit
3359 );
3360
3361static struct attribute *perfclass_attrs[] = {
3362 &attr_reserve_percpu.attr,
3363 &attr_overcommit.attr,
3364 NULL
3365};
3366
3367static struct attribute_group perfclass_attr_group = {
3368 .attrs = perfclass_attrs,
3369 .name = "perf_counters",
3370};
3371
3372static int __init perf_counter_sysfs_init(void)
3373{
3374 return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
3375 &perfclass_attr_group);
3376}
3377device_initcall(perf_counter_sysfs_init);