blob: db843f812a607bd75e24058ca197255310140d29 [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 Zijlstra7fc23a52009-05-08 18:52:21 +020042static atomic_t nr_counters __read_mostly;
Peter Zijlstra9ee318a2009-04-09 10:53:44 +020043static atomic_t nr_mmap_tracking __read_mostly;
44static atomic_t nr_munmap_tracking __read_mostly;
45static atomic_t nr_comm_tracking __read_mostly;
46
Peter Zijlstra1ccd1542009-04-09 10:53:45 +020047int sysctl_perf_counter_priv __read_mostly; /* do we need to be privileged */
Peter Zijlstra789f90f2009-05-15 15:19:27 +020048int sysctl_perf_counter_mlock __read_mostly = 512; /* 'free' kb per user */
Peter Zijlstraa78ac322009-05-25 17:39:05 +020049int sysctl_perf_counter_limit __read_mostly = 100000; /* max NMIs per second */
Peter Zijlstra1ccd1542009-04-09 10:53:45 +020050
Thomas Gleixner0793a612008-12-04 20:12:29 +010051/*
Ingo Molnar1dce8d92009-05-04 19:23:18 +020052 * Lock for (sysadmin-configurable) counter reservations:
Thomas Gleixner0793a612008-12-04 20:12:29 +010053 */
Ingo Molnar1dce8d92009-05-04 19:23:18 +020054static DEFINE_SPINLOCK(perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +010055
56/*
57 * Architecture provided APIs - weak aliases:
58 */
Robert Richter4aeb0b42009-04-29 12:47:03 +020059extern __weak const struct pmu *hw_perf_counter_init(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +010060{
Paul Mackerrasff6f0542009-01-09 16:19:25 +110061 return NULL;
Thomas Gleixner0793a612008-12-04 20:12:29 +010062}
63
Peter Zijlstra9e35ad32009-05-13 16:21:38 +020064void __weak hw_perf_disable(void) { barrier(); }
65void __weak hw_perf_enable(void) { barrier(); }
66
Paul Mackerras01d02872009-01-14 13:44:19 +110067void __weak hw_perf_counter_setup(int cpu) { barrier(); }
Paul Mackerras3cbed422009-01-09 16:43:42 +110068int __weak hw_perf_group_sched_in(struct perf_counter *group_leader,
69 struct perf_cpu_context *cpuctx,
70 struct perf_counter_context *ctx, int cpu)
71{
72 return 0;
73}
Thomas Gleixner0793a612008-12-04 20:12:29 +010074
Paul Mackerras4eb96fc2009-01-09 17:24:34 +110075void __weak perf_counter_print_debug(void) { }
76
Peter Zijlstra9e35ad32009-05-13 16:21:38 +020077static DEFINE_PER_CPU(int, disable_count);
78
79void __perf_disable(void)
80{
81 __get_cpu_var(disable_count)++;
82}
83
84bool __perf_enable(void)
85{
86 return !--__get_cpu_var(disable_count);
87}
88
89void perf_disable(void)
90{
91 __perf_disable();
92 hw_perf_disable();
93}
Peter Zijlstra9e35ad32009-05-13 16:21:38 +020094
95void perf_enable(void)
96{
97 if (__perf_enable())
98 hw_perf_enable();
99}
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200100
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000101static void get_ctx(struct perf_counter_context *ctx)
102{
103 atomic_inc(&ctx->refcount);
104}
105
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000106static void free_ctx(struct rcu_head *head)
107{
108 struct perf_counter_context *ctx;
109
110 ctx = container_of(head, struct perf_counter_context, rcu_head);
111 kfree(ctx);
112}
113
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000114static void put_ctx(struct perf_counter_context *ctx)
115{
Paul Mackerras564c2b22009-05-22 14:27:22 +1000116 if (atomic_dec_and_test(&ctx->refcount)) {
117 if (ctx->parent_ctx)
118 put_ctx(ctx->parent_ctx);
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000119 if (ctx->task)
120 put_task_struct(ctx->task);
121 call_rcu(&ctx->rcu_head, free_ctx);
Paul Mackerras564c2b22009-05-22 14:27:22 +1000122 }
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000123}
124
Peter Zijlstrafccc7142009-05-23 18:28:56 +0200125/*
126 * Add a counter from the lists for its context.
127 * Must be called with ctx->mutex and ctx->lock held.
128 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100129static void
130list_add_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
131{
132 struct perf_counter *group_leader = counter->group_leader;
133
134 /*
135 * Depending on whether it is a standalone or sibling counter,
136 * add it straight to the context's counter list, or to the group
137 * leader's sibling list:
138 */
Peter Zijlstra3df5eda2009-05-08 18:52:22 +0200139 if (group_leader == counter)
Ingo Molnar04289bb2008-12-11 08:38:42 +0100140 list_add_tail(&counter->list_entry, &ctx->counter_list);
Peter Zijlstra5c148192009-03-25 12:30:23 +0100141 else {
Ingo Molnar04289bb2008-12-11 08:38:42 +0100142 list_add_tail(&counter->list_entry, &group_leader->sibling_list);
Peter Zijlstra5c148192009-03-25 12:30:23 +0100143 group_leader->nr_siblings++;
144 }
Peter Zijlstra592903c2009-03-13 12:21:36 +0100145
146 list_add_rcu(&counter->event_entry, &ctx->event_list);
Peter Zijlstra8bc20952009-05-15 20:45:59 +0200147 ctx->nr_counters++;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100148}
149
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000150/*
151 * Remove a counter from the lists for its context.
Peter Zijlstrafccc7142009-05-23 18:28:56 +0200152 * Must be called with ctx->mutex and ctx->lock held.
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000153 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100154static void
155list_del_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
156{
157 struct perf_counter *sibling, *tmp;
158
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000159 if (list_empty(&counter->list_entry))
160 return;
Peter Zijlstra8bc20952009-05-15 20:45:59 +0200161 ctx->nr_counters--;
162
Ingo Molnar04289bb2008-12-11 08:38:42 +0100163 list_del_init(&counter->list_entry);
Peter Zijlstra592903c2009-03-13 12:21:36 +0100164 list_del_rcu(&counter->event_entry);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100165
Peter Zijlstra5c148192009-03-25 12:30:23 +0100166 if (counter->group_leader != counter)
167 counter->group_leader->nr_siblings--;
168
Ingo Molnar04289bb2008-12-11 08:38:42 +0100169 /*
170 * If this was a group counter with sibling counters then
171 * upgrade the siblings to singleton counters by adding them
172 * to the context list directly:
173 */
174 list_for_each_entry_safe(sibling, tmp,
175 &counter->sibling_list, list_entry) {
176
Peter Zijlstra75564232009-03-13 12:21:29 +0100177 list_move_tail(&sibling->list_entry, &ctx->counter_list);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100178 sibling->group_leader = sibling;
179 }
180}
181
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100182static void
183counter_sched_out(struct perf_counter *counter,
184 struct perf_cpu_context *cpuctx,
185 struct perf_counter_context *ctx)
186{
187 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
188 return;
189
190 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200191 counter->tstamp_stopped = ctx->time;
Robert Richter4aeb0b42009-04-29 12:47:03 +0200192 counter->pmu->disable(counter);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100193 counter->oncpu = -1;
194
195 if (!is_software_counter(counter))
196 cpuctx->active_oncpu--;
197 ctx->nr_active--;
198 if (counter->hw_event.exclusive || !cpuctx->active_oncpu)
199 cpuctx->exclusive = 0;
200}
201
Paul Mackerrasd859e292009-01-17 18:10:22 +1100202static void
203group_sched_out(struct perf_counter *group_counter,
204 struct perf_cpu_context *cpuctx,
205 struct perf_counter_context *ctx)
206{
207 struct perf_counter *counter;
208
209 if (group_counter->state != PERF_COUNTER_STATE_ACTIVE)
210 return;
211
212 counter_sched_out(group_counter, cpuctx, ctx);
213
214 /*
215 * Schedule out siblings (if any):
216 */
217 list_for_each_entry(counter, &group_counter->sibling_list, list_entry)
218 counter_sched_out(counter, cpuctx, ctx);
219
220 if (group_counter->hw_event.exclusive)
221 cpuctx->exclusive = 0;
222}
223
Thomas Gleixner0793a612008-12-04 20:12:29 +0100224/*
225 * Cross CPU call to remove a performance counter
226 *
227 * We disable the counter on the hardware level first. After that we
228 * remove it from the context list.
229 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100230static void __perf_counter_remove_from_context(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100231{
232 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
233 struct perf_counter *counter = info;
234 struct perf_counter_context *ctx = counter->ctx;
Ingo Molnar9b51f662008-12-12 13:49:45 +0100235 unsigned long flags;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100236
237 /*
238 * If this is a task context, we need to check whether it is
239 * the current task context of this cpu. If not it has been
240 * scheduled out before the smp call arrived.
241 */
242 if (ctx->task && cpuctx->task_ctx != ctx)
243 return;
244
Peter Zijlstra849691a2009-04-06 11:45:12 +0200245 spin_lock_irqsave(&ctx->lock, flags);
Ingo Molnar34adc802009-05-20 20:13:28 +0200246 /*
247 * Protect the list operation against NMI by disabling the
248 * counters on a global level.
249 */
250 perf_disable();
Thomas Gleixner0793a612008-12-04 20:12:29 +0100251
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100252 counter_sched_out(counter, cpuctx, ctx);
253
Ingo Molnar04289bb2008-12-11 08:38:42 +0100254 list_del_counter(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100255
256 if (!ctx->task) {
257 /*
258 * Allow more per task counters with respect to the
259 * reservation:
260 */
261 cpuctx->max_pertask =
262 min(perf_max_counters - ctx->nr_counters,
263 perf_max_counters - perf_reserved_percpu);
264 }
265
Ingo Molnar34adc802009-05-20 20:13:28 +0200266 perf_enable();
Peter Zijlstra849691a2009-04-06 11:45:12 +0200267 spin_unlock_irqrestore(&ctx->lock, flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100268}
269
270
271/*
272 * Remove the counter from a task's (or a CPU's) list of counters.
273 *
Peter Zijlstrafccc7142009-05-23 18:28:56 +0200274 * Must be called with ctx->mutex held.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100275 *
276 * CPU counters are removed with a smp call. For task counters we only
277 * call when the task is on a CPU.
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000278 *
279 * If counter->ctx is a cloned context, callers must make sure that
280 * every task struct that counter->ctx->task could possibly point to
281 * remains valid. This is OK when called from perf_release since
282 * that only calls us on the top-level context, which can't be a clone.
283 * When called from perf_counter_exit_task, it's OK because the
284 * context has been detached from its task.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100285 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100286static void perf_counter_remove_from_context(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100287{
288 struct perf_counter_context *ctx = counter->ctx;
289 struct task_struct *task = ctx->task;
290
291 if (!task) {
292 /*
293 * Per cpu counters are removed via an smp call and
294 * the removal is always sucessful.
295 */
296 smp_call_function_single(counter->cpu,
Ingo Molnar04289bb2008-12-11 08:38:42 +0100297 __perf_counter_remove_from_context,
Thomas Gleixner0793a612008-12-04 20:12:29 +0100298 counter, 1);
299 return;
300 }
301
302retry:
Ingo Molnar04289bb2008-12-11 08:38:42 +0100303 task_oncpu_function_call(task, __perf_counter_remove_from_context,
Thomas Gleixner0793a612008-12-04 20:12:29 +0100304 counter);
305
306 spin_lock_irq(&ctx->lock);
307 /*
308 * If the context is active we need to retry the smp call.
309 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100310 if (ctx->nr_active && !list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100311 spin_unlock_irq(&ctx->lock);
312 goto retry;
313 }
314
315 /*
316 * The lock prevents that this context is scheduled in so we
Ingo Molnar04289bb2008-12-11 08:38:42 +0100317 * can remove the counter safely, if the call above did not
Thomas Gleixner0793a612008-12-04 20:12:29 +0100318 * succeed.
319 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100320 if (!list_empty(&counter->list_entry)) {
Ingo Molnar04289bb2008-12-11 08:38:42 +0100321 list_del_counter(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100322 }
323 spin_unlock_irq(&ctx->lock);
324}
325
Peter Zijlstra4af49982009-04-06 11:45:10 +0200326static inline u64 perf_clock(void)
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100327{
Peter Zijlstra4af49982009-04-06 11:45:10 +0200328 return cpu_clock(smp_processor_id());
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100329}
330
331/*
332 * Update the record of the current time in a context.
333 */
Peter Zijlstra4af49982009-04-06 11:45:10 +0200334static void update_context_time(struct perf_counter_context *ctx)
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100335{
Peter Zijlstra4af49982009-04-06 11:45:10 +0200336 u64 now = perf_clock();
337
338 ctx->time += now - ctx->timestamp;
339 ctx->timestamp = now;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100340}
341
342/*
343 * Update the total_time_enabled and total_time_running fields for a counter.
344 */
345static void update_counter_times(struct perf_counter *counter)
346{
347 struct perf_counter_context *ctx = counter->ctx;
348 u64 run_end;
349
Peter Zijlstra4af49982009-04-06 11:45:10 +0200350 if (counter->state < PERF_COUNTER_STATE_INACTIVE)
351 return;
352
353 counter->total_time_enabled = ctx->time - counter->tstamp_enabled;
354
355 if (counter->state == PERF_COUNTER_STATE_INACTIVE)
356 run_end = counter->tstamp_stopped;
357 else
358 run_end = ctx->time;
359
360 counter->total_time_running = run_end - counter->tstamp_running;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100361}
362
363/*
364 * Update total_time_enabled and total_time_running for all counters in a group.
365 */
366static void update_group_times(struct perf_counter *leader)
367{
368 struct perf_counter *counter;
369
370 update_counter_times(leader);
371 list_for_each_entry(counter, &leader->sibling_list, list_entry)
372 update_counter_times(counter);
373}
374
375/*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100376 * Cross CPU call to disable a performance counter
377 */
378static void __perf_counter_disable(void *info)
379{
380 struct perf_counter *counter = info;
381 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
382 struct perf_counter_context *ctx = counter->ctx;
383 unsigned long flags;
384
385 /*
386 * If this is a per-task counter, need to check whether this
387 * counter's task is the current task on this cpu.
388 */
389 if (ctx->task && cpuctx->task_ctx != ctx)
390 return;
391
Peter Zijlstra849691a2009-04-06 11:45:12 +0200392 spin_lock_irqsave(&ctx->lock, flags);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100393
394 /*
395 * If the counter is on, turn it off.
396 * If it is in error state, leave it in error state.
397 */
398 if (counter->state >= PERF_COUNTER_STATE_INACTIVE) {
Peter Zijlstra4af49982009-04-06 11:45:10 +0200399 update_context_time(ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100400 update_counter_times(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100401 if (counter == counter->group_leader)
402 group_sched_out(counter, cpuctx, ctx);
403 else
404 counter_sched_out(counter, cpuctx, ctx);
405 counter->state = PERF_COUNTER_STATE_OFF;
406 }
407
Peter Zijlstra849691a2009-04-06 11:45:12 +0200408 spin_unlock_irqrestore(&ctx->lock, flags);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100409}
410
411/*
412 * Disable a counter.
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000413 *
414 * If counter->ctx is a cloned context, callers must make sure that
415 * every task struct that counter->ctx->task could possibly point to
416 * remains valid. This condition is satisifed when called through
417 * perf_counter_for_each_child or perf_counter_for_each because they
418 * hold the top-level counter's child_mutex, so any descendant that
419 * goes to exit will block in sync_child_counter.
420 * When called from perf_pending_counter it's OK because counter->ctx
421 * is the current context on this CPU and preemption is disabled,
422 * hence we can't get into perf_counter_task_sched_out for this context.
Paul Mackerrasd859e292009-01-17 18:10:22 +1100423 */
424static void perf_counter_disable(struct perf_counter *counter)
425{
426 struct perf_counter_context *ctx = counter->ctx;
427 struct task_struct *task = ctx->task;
428
429 if (!task) {
430 /*
431 * Disable the counter on the cpu that it's on
432 */
433 smp_call_function_single(counter->cpu, __perf_counter_disable,
434 counter, 1);
435 return;
436 }
437
438 retry:
439 task_oncpu_function_call(task, __perf_counter_disable, counter);
440
441 spin_lock_irq(&ctx->lock);
442 /*
443 * If the counter is still active, we need to retry the cross-call.
444 */
445 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
446 spin_unlock_irq(&ctx->lock);
447 goto retry;
448 }
449
450 /*
451 * Since we have the lock this context can't be scheduled
452 * in, so we can change the state safely.
453 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100454 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
455 update_counter_times(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100456 counter->state = PERF_COUNTER_STATE_OFF;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100457 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100458
459 spin_unlock_irq(&ctx->lock);
460}
461
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100462static int
463counter_sched_in(struct perf_counter *counter,
464 struct perf_cpu_context *cpuctx,
465 struct perf_counter_context *ctx,
466 int cpu)
467{
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100468 if (counter->state <= PERF_COUNTER_STATE_OFF)
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100469 return 0;
470
471 counter->state = PERF_COUNTER_STATE_ACTIVE;
472 counter->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */
473 /*
474 * The new state must be visible before we turn it on in the hardware:
475 */
476 smp_wmb();
477
Robert Richter4aeb0b42009-04-29 12:47:03 +0200478 if (counter->pmu->enable(counter)) {
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100479 counter->state = PERF_COUNTER_STATE_INACTIVE;
480 counter->oncpu = -1;
481 return -EAGAIN;
482 }
483
Peter Zijlstra4af49982009-04-06 11:45:10 +0200484 counter->tstamp_running += ctx->time - counter->tstamp_stopped;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100485
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100486 if (!is_software_counter(counter))
487 cpuctx->active_oncpu++;
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100488 ctx->nr_active++;
489
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100490 if (counter->hw_event.exclusive)
491 cpuctx->exclusive = 1;
492
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100493 return 0;
494}
495
Paul Mackerras6751b712009-05-11 12:08:02 +1000496static int
497group_sched_in(struct perf_counter *group_counter,
498 struct perf_cpu_context *cpuctx,
499 struct perf_counter_context *ctx,
500 int cpu)
501{
502 struct perf_counter *counter, *partial_group;
503 int ret;
504
505 if (group_counter->state == PERF_COUNTER_STATE_OFF)
506 return 0;
507
508 ret = hw_perf_group_sched_in(group_counter, cpuctx, ctx, cpu);
509 if (ret)
510 return ret < 0 ? ret : 0;
511
512 group_counter->prev_state = group_counter->state;
513 if (counter_sched_in(group_counter, cpuctx, ctx, cpu))
514 return -EAGAIN;
515
516 /*
517 * Schedule in siblings as one group (if any):
518 */
519 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
520 counter->prev_state = counter->state;
521 if (counter_sched_in(counter, cpuctx, ctx, cpu)) {
522 partial_group = counter;
523 goto group_error;
524 }
525 }
526
527 return 0;
528
529group_error:
530 /*
531 * Groups can be scheduled in as one unit only, so undo any
532 * partial group before returning:
533 */
534 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
535 if (counter == partial_group)
536 break;
537 counter_sched_out(counter, cpuctx, ctx);
538 }
539 counter_sched_out(group_counter, cpuctx, ctx);
540
541 return -EAGAIN;
542}
543
Thomas Gleixner0793a612008-12-04 20:12:29 +0100544/*
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100545 * Return 1 for a group consisting entirely of software counters,
546 * 0 if the group contains any hardware counters.
547 */
548static int is_software_only_group(struct perf_counter *leader)
549{
550 struct perf_counter *counter;
551
552 if (!is_software_counter(leader))
553 return 0;
Peter Zijlstra5c148192009-03-25 12:30:23 +0100554
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100555 list_for_each_entry(counter, &leader->sibling_list, list_entry)
556 if (!is_software_counter(counter))
557 return 0;
Peter Zijlstra5c148192009-03-25 12:30:23 +0100558
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100559 return 1;
560}
561
562/*
563 * Work out whether we can put this counter group on the CPU now.
564 */
565static int group_can_go_on(struct perf_counter *counter,
566 struct perf_cpu_context *cpuctx,
567 int can_add_hw)
568{
569 /*
570 * Groups consisting entirely of software counters can always go on.
571 */
572 if (is_software_only_group(counter))
573 return 1;
574 /*
575 * If an exclusive group is already on, no other hardware
576 * counters can go on.
577 */
578 if (cpuctx->exclusive)
579 return 0;
580 /*
581 * If this group is exclusive and there are already
582 * counters on the CPU, it can't go on.
583 */
584 if (counter->hw_event.exclusive && cpuctx->active_oncpu)
585 return 0;
586 /*
587 * Otherwise, try to add it if all previous groups were able
588 * to go on.
589 */
590 return can_add_hw;
591}
592
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100593static void add_counter_to_ctx(struct perf_counter *counter,
594 struct perf_counter_context *ctx)
595{
596 list_add_counter(counter, ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100597 counter->prev_state = PERF_COUNTER_STATE_OFF;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200598 counter->tstamp_enabled = ctx->time;
599 counter->tstamp_running = ctx->time;
600 counter->tstamp_stopped = ctx->time;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100601}
602
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100603/*
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100604 * Cross CPU call to install and enable a performance counter
Peter Zijlstra682076a2009-05-23 18:28:57 +0200605 *
606 * Must be called with ctx->mutex held
Thomas Gleixner0793a612008-12-04 20:12:29 +0100607 */
608static void __perf_install_in_context(void *info)
609{
610 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
611 struct perf_counter *counter = info;
612 struct perf_counter_context *ctx = counter->ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100613 struct perf_counter *leader = counter->group_leader;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100614 int cpu = smp_processor_id();
Ingo Molnar9b51f662008-12-12 13:49:45 +0100615 unsigned long flags;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100616 int err;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100617
618 /*
619 * If this is a task context, we need to check whether it is
620 * the current task context of this cpu. If not it has been
621 * scheduled out before the smp call arrived.
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000622 * Or possibly this is the right context but it isn't
623 * on this cpu because it had no counters.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100624 */
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000625 if (ctx->task && cpuctx->task_ctx != ctx) {
626 if (cpuctx->task_ctx || ctx->task != current)
627 return;
628 cpuctx->task_ctx = ctx;
629 }
Thomas Gleixner0793a612008-12-04 20:12:29 +0100630
Peter Zijlstra849691a2009-04-06 11:45:12 +0200631 spin_lock_irqsave(&ctx->lock, flags);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000632 ctx->is_active = 1;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200633 update_context_time(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100634
635 /*
636 * Protect the list operation against NMI by disabling the
637 * counters on a global level. NOP for non NMI based counters.
638 */
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200639 perf_disable();
Thomas Gleixner0793a612008-12-04 20:12:29 +0100640
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100641 add_counter_to_ctx(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100642
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100643 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100644 * Don't put the counter on if it is disabled or if
645 * it is in a group and the group isn't on.
646 */
647 if (counter->state != PERF_COUNTER_STATE_INACTIVE ||
648 (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE))
649 goto unlock;
650
651 /*
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100652 * An exclusive counter can't go on if there are already active
653 * hardware counters, and no hardware counter can go on if there
654 * is already an exclusive counter on.
655 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100656 if (!group_can_go_on(counter, cpuctx, 1))
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100657 err = -EEXIST;
658 else
659 err = counter_sched_in(counter, cpuctx, ctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100660
Paul Mackerrasd859e292009-01-17 18:10:22 +1100661 if (err) {
662 /*
663 * This counter couldn't go on. If it is in a group
664 * then we have to pull the whole group off.
665 * If the counter group is pinned then put it in error state.
666 */
667 if (leader != counter)
668 group_sched_out(leader, cpuctx, ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100669 if (leader->hw_event.pinned) {
670 update_group_times(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100671 leader->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100672 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100673 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100674
675 if (!err && !ctx->task && cpuctx->max_pertask)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100676 cpuctx->max_pertask--;
677
Paul Mackerrasd859e292009-01-17 18:10:22 +1100678 unlock:
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200679 perf_enable();
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100680
Peter Zijlstra849691a2009-04-06 11:45:12 +0200681 spin_unlock_irqrestore(&ctx->lock, flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100682}
683
684/*
685 * Attach a performance counter to a context
686 *
687 * First we add the counter to the list with the hardware enable bit
688 * in counter->hw_config cleared.
689 *
690 * If the counter is attached to a task which is on a CPU we use a smp
691 * call to enable it in the task context. The task might have been
692 * scheduled away, but we check this in the smp call again.
Paul Mackerrasd859e292009-01-17 18:10:22 +1100693 *
694 * Must be called with ctx->mutex held.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100695 */
696static void
697perf_install_in_context(struct perf_counter_context *ctx,
698 struct perf_counter *counter,
699 int cpu)
700{
701 struct task_struct *task = ctx->task;
702
Thomas Gleixner0793a612008-12-04 20:12:29 +0100703 if (!task) {
704 /*
705 * Per cpu counters are installed via an smp call and
706 * the install is always sucessful.
707 */
708 smp_call_function_single(cpu, __perf_install_in_context,
709 counter, 1);
710 return;
711 }
712
Thomas Gleixner0793a612008-12-04 20:12:29 +0100713retry:
714 task_oncpu_function_call(task, __perf_install_in_context,
715 counter);
716
717 spin_lock_irq(&ctx->lock);
718 /*
Thomas Gleixner0793a612008-12-04 20:12:29 +0100719 * we need to retry the smp call.
720 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100721 if (ctx->is_active && list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100722 spin_unlock_irq(&ctx->lock);
723 goto retry;
724 }
725
726 /*
727 * The lock prevents that this context is scheduled in so we
728 * can add the counter safely, if it the call above did not
729 * succeed.
730 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100731 if (list_empty(&counter->list_entry))
732 add_counter_to_ctx(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100733 spin_unlock_irq(&ctx->lock);
734}
735
Paul Mackerrasd859e292009-01-17 18:10:22 +1100736/*
737 * Cross CPU call to enable a performance counter
738 */
739static void __perf_counter_enable(void *info)
Ingo Molnar04289bb2008-12-11 08:38:42 +0100740{
Paul Mackerrasd859e292009-01-17 18:10:22 +1100741 struct perf_counter *counter = info;
742 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
743 struct perf_counter_context *ctx = counter->ctx;
744 struct perf_counter *leader = counter->group_leader;
745 unsigned long flags;
746 int err;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100747
748 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100749 * If this is a per-task counter, need to check whether this
750 * counter's task is the current task on this cpu.
Ingo Molnar04289bb2008-12-11 08:38:42 +0100751 */
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000752 if (ctx->task && cpuctx->task_ctx != ctx) {
753 if (cpuctx->task_ctx || ctx->task != current)
754 return;
755 cpuctx->task_ctx = ctx;
756 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100757
Peter Zijlstra849691a2009-04-06 11:45:12 +0200758 spin_lock_irqsave(&ctx->lock, flags);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000759 ctx->is_active = 1;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200760 update_context_time(ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100761
Paul Mackerrasc07c99b2009-02-13 22:10:34 +1100762 counter->prev_state = counter->state;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100763 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
764 goto unlock;
765 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200766 counter->tstamp_enabled = ctx->time - counter->total_time_enabled;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100767
768 /*
769 * If the counter is in a group and isn't the group leader,
770 * then don't put it on unless the group is on.
771 */
772 if (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE)
773 goto unlock;
774
Paul Mackerrase758a332009-05-12 21:59:01 +1000775 if (!group_can_go_on(counter, cpuctx, 1)) {
Paul Mackerrasd859e292009-01-17 18:10:22 +1100776 err = -EEXIST;
Paul Mackerrase758a332009-05-12 21:59:01 +1000777 } else {
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200778 perf_disable();
Paul Mackerrase758a332009-05-12 21:59:01 +1000779 if (counter == leader)
780 err = group_sched_in(counter, cpuctx, ctx,
781 smp_processor_id());
782 else
783 err = counter_sched_in(counter, cpuctx, ctx,
784 smp_processor_id());
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200785 perf_enable();
Paul Mackerrase758a332009-05-12 21:59:01 +1000786 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100787
788 if (err) {
789 /*
790 * If this counter can't go on and it's part of a
791 * group, then the whole group has to come off.
792 */
793 if (leader != counter)
794 group_sched_out(leader, cpuctx, ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100795 if (leader->hw_event.pinned) {
796 update_group_times(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100797 leader->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100798 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100799 }
800
801 unlock:
Peter Zijlstra849691a2009-04-06 11:45:12 +0200802 spin_unlock_irqrestore(&ctx->lock, flags);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100803}
804
805/*
806 * Enable a counter.
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000807 *
808 * If counter->ctx is a cloned context, callers must make sure that
809 * every task struct that counter->ctx->task could possibly point to
810 * remains valid. This condition is satisfied when called through
811 * perf_counter_for_each_child or perf_counter_for_each as described
812 * for perf_counter_disable.
Paul Mackerrasd859e292009-01-17 18:10:22 +1100813 */
814static void perf_counter_enable(struct perf_counter *counter)
815{
816 struct perf_counter_context *ctx = counter->ctx;
817 struct task_struct *task = ctx->task;
818
819 if (!task) {
820 /*
821 * Enable the counter on the cpu that it's on
822 */
823 smp_call_function_single(counter->cpu, __perf_counter_enable,
824 counter, 1);
825 return;
826 }
827
828 spin_lock_irq(&ctx->lock);
829 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
830 goto out;
831
832 /*
833 * If the counter is in error state, clear that first.
834 * That way, if we see the counter in error state below, we
835 * know that it has gone back into error state, as distinct
836 * from the task having been scheduled away before the
837 * cross-call arrived.
838 */
839 if (counter->state == PERF_COUNTER_STATE_ERROR)
840 counter->state = PERF_COUNTER_STATE_OFF;
841
842 retry:
843 spin_unlock_irq(&ctx->lock);
844 task_oncpu_function_call(task, __perf_counter_enable, counter);
845
846 spin_lock_irq(&ctx->lock);
847
848 /*
849 * If the context is active and the counter is still off,
850 * we need to retry the cross-call.
851 */
852 if (ctx->is_active && counter->state == PERF_COUNTER_STATE_OFF)
853 goto retry;
854
855 /*
856 * Since we have the lock this context can't be scheduled
857 * in, so we can change the state safely.
858 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100859 if (counter->state == PERF_COUNTER_STATE_OFF) {
Paul Mackerrasd859e292009-01-17 18:10:22 +1100860 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200861 counter->tstamp_enabled =
862 ctx->time - counter->total_time_enabled;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100863 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100864 out:
865 spin_unlock_irq(&ctx->lock);
866}
867
Peter Zijlstra2023b352009-05-05 17:50:26 +0200868static int perf_counter_refresh(struct perf_counter *counter, int refresh)
Peter Zijlstra79f14642009-04-06 11:45:07 +0200869{
Peter Zijlstra2023b352009-05-05 17:50:26 +0200870 /*
871 * not supported on inherited counters
872 */
873 if (counter->hw_event.inherit)
874 return -EINVAL;
875
Peter Zijlstra79f14642009-04-06 11:45:07 +0200876 atomic_add(refresh, &counter->event_limit);
877 perf_counter_enable(counter);
Peter Zijlstra2023b352009-05-05 17:50:26 +0200878
879 return 0;
Peter Zijlstra79f14642009-04-06 11:45:07 +0200880}
881
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100882void __perf_counter_sched_out(struct perf_counter_context *ctx,
883 struct perf_cpu_context *cpuctx)
884{
885 struct perf_counter *counter;
886
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100887 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100888 ctx->is_active = 0;
889 if (likely(!ctx->nr_counters))
890 goto out;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200891 update_context_time(ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100892
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200893 perf_disable();
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100894 if (ctx->nr_active) {
Peter Zijlstraafedadf2009-05-20 12:21:22 +0200895 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
896 if (counter != counter->group_leader)
897 counter_sched_out(counter, cpuctx, ctx);
898 else
899 group_sched_out(counter, cpuctx, ctx);
900 }
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100901 }
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200902 perf_enable();
Paul Mackerrasd859e292009-01-17 18:10:22 +1100903 out:
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100904 spin_unlock(&ctx->lock);
905}
906
Thomas Gleixner0793a612008-12-04 20:12:29 +0100907/*
Paul Mackerras564c2b22009-05-22 14:27:22 +1000908 * Test whether two contexts are equivalent, i.e. whether they
909 * have both been cloned from the same version of the same context
910 * and they both have the same number of enabled counters.
911 * If the number of enabled counters is the same, then the set
912 * of enabled counters should be the same, because these are both
913 * inherited contexts, therefore we can't access individual counters
914 * in them directly with an fd; we can only enable/disable all
915 * counters via prctl, or enable/disable all counters in a family
916 * via ioctl, which will have the same effect on both contexts.
917 */
918static int context_equiv(struct perf_counter_context *ctx1,
919 struct perf_counter_context *ctx2)
920{
921 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
Paul Mackerrasad3a37d2009-05-29 16:06:20 +1000922 && ctx1->parent_gen == ctx2->parent_gen
923 && ctx1->parent_gen != ~0ull;
Paul Mackerras564c2b22009-05-22 14:27:22 +1000924}
925
926/*
Thomas Gleixner0793a612008-12-04 20:12:29 +0100927 * Called from scheduler to remove the counters of the current task,
928 * with interrupts disabled.
929 *
930 * We stop each counter and update the counter value in counter->count.
931 *
Ingo Molnar76715812008-12-17 14:20:28 +0100932 * This does not protect us against NMI, but disable()
Thomas Gleixner0793a612008-12-04 20:12:29 +0100933 * sets the disabled bit in the control field of counter _before_
934 * accessing the counter control register. If a NMI hits, then it will
935 * not restart the counter.
936 */
Paul Mackerras564c2b22009-05-22 14:27:22 +1000937void perf_counter_task_sched_out(struct task_struct *task,
938 struct task_struct *next, int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100939{
940 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000941 struct perf_counter_context *ctx = task->perf_counter_ctxp;
Paul Mackerras564c2b22009-05-22 14:27:22 +1000942 struct perf_counter_context *next_ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000943 struct perf_counter_context *parent;
Peter Zijlstra4a0deca2009-03-19 20:26:12 +0100944 struct pt_regs *regs;
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000945 int do_switch = 1;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100946
Peter Zijlstra10989fb2009-05-25 14:45:28 +0200947 regs = task_pt_regs(task);
948 perf_swcounter_event(PERF_COUNT_CONTEXT_SWITCHES, 1, 1, regs, 0);
949
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000950 if (likely(!ctx || !cpuctx->task_ctx))
Thomas Gleixner0793a612008-12-04 20:12:29 +0100951 return;
952
Peter Zijlstrabce379b2009-04-06 11:45:13 +0200953 update_context_time(ctx);
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000954
955 rcu_read_lock();
956 parent = rcu_dereference(ctx->parent_ctx);
Paul Mackerras564c2b22009-05-22 14:27:22 +1000957 next_ctx = next->perf_counter_ctxp;
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000958 if (parent && next_ctx &&
959 rcu_dereference(next_ctx->parent_ctx) == parent) {
960 /*
961 * Looks like the two contexts are clones, so we might be
962 * able to optimize the context switch. We lock both
963 * contexts and check that they are clones under the
964 * lock (including re-checking that neither has been
965 * uncloned in the meantime). It doesn't matter which
966 * order we take the locks because no other cpu could
967 * be trying to lock both of these tasks.
968 */
969 spin_lock(&ctx->lock);
970 spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
971 if (context_equiv(ctx, next_ctx)) {
972 task->perf_counter_ctxp = next_ctx;
973 next->perf_counter_ctxp = ctx;
974 ctx->task = next;
975 next_ctx->task = task;
976 do_switch = 0;
977 }
978 spin_unlock(&next_ctx->lock);
979 spin_unlock(&ctx->lock);
Paul Mackerras564c2b22009-05-22 14:27:22 +1000980 }
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000981 rcu_read_unlock();
Paul Mackerras564c2b22009-05-22 14:27:22 +1000982
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000983 if (do_switch) {
984 __perf_counter_sched_out(ctx, cpuctx);
985 cpuctx->task_ctx = NULL;
986 }
Thomas Gleixner0793a612008-12-04 20:12:29 +0100987}
988
Paul Mackerrasa08b1592009-05-11 15:46:10 +1000989static void __perf_counter_task_sched_out(struct perf_counter_context *ctx)
990{
991 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
992
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000993 if (!cpuctx->task_ctx)
994 return;
Paul Mackerrasa08b1592009-05-11 15:46:10 +1000995 __perf_counter_sched_out(ctx, cpuctx);
996 cpuctx->task_ctx = NULL;
997}
998
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100999static void perf_counter_cpu_sched_out(struct perf_cpu_context *cpuctx)
Ingo Molnar04289bb2008-12-11 08:38:42 +01001000{
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001001 __perf_counter_sched_out(&cpuctx->ctx, cpuctx);
Ingo Molnar04289bb2008-12-11 08:38:42 +01001002}
1003
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001004static void
1005__perf_counter_sched_in(struct perf_counter_context *ctx,
1006 struct perf_cpu_context *cpuctx, int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001007{
Thomas Gleixner0793a612008-12-04 20:12:29 +01001008 struct perf_counter *counter;
Paul Mackerrasdd0e6ba2009-01-12 15:11:00 +11001009 int can_add_hw = 1;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001010
Thomas Gleixner0793a612008-12-04 20:12:29 +01001011 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001012 ctx->is_active = 1;
1013 if (likely(!ctx->nr_counters))
1014 goto out;
1015
Peter Zijlstra4af49982009-04-06 11:45:10 +02001016 ctx->timestamp = perf_clock();
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001017
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001018 perf_disable();
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001019
1020 /*
1021 * First go through the list and put on any pinned groups
1022 * in order to give them the best chance of going on.
1023 */
Ingo Molnar04289bb2008-12-11 08:38:42 +01001024 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001025 if (counter->state <= PERF_COUNTER_STATE_OFF ||
1026 !counter->hw_event.pinned)
1027 continue;
1028 if (counter->cpu != -1 && counter->cpu != cpu)
1029 continue;
1030
Peter Zijlstraafedadf2009-05-20 12:21:22 +02001031 if (counter != counter->group_leader)
1032 counter_sched_in(counter, cpuctx, ctx, cpu);
1033 else {
1034 if (group_can_go_on(counter, cpuctx, 1))
1035 group_sched_in(counter, cpuctx, ctx, cpu);
1036 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001037
1038 /*
1039 * If this pinned group hasn't been scheduled,
1040 * put it in error state.
1041 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001042 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
1043 update_group_times(counter);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001044 counter->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001045 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001046 }
1047
1048 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1049 /*
1050 * Ignore counters in OFF or ERROR state, and
1051 * ignore pinned counters since we did them already.
1052 */
1053 if (counter->state <= PERF_COUNTER_STATE_OFF ||
1054 counter->hw_event.pinned)
1055 continue;
1056
Ingo Molnar04289bb2008-12-11 08:38:42 +01001057 /*
1058 * Listen to the 'cpu' scheduling filter constraint
1059 * of counters:
1060 */
Thomas Gleixner0793a612008-12-04 20:12:29 +01001061 if (counter->cpu != -1 && counter->cpu != cpu)
1062 continue;
1063
Peter Zijlstraafedadf2009-05-20 12:21:22 +02001064 if (counter != counter->group_leader) {
1065 if (counter_sched_in(counter, cpuctx, ctx, cpu))
Paul Mackerrasdd0e6ba2009-01-12 15:11:00 +11001066 can_add_hw = 0;
Peter Zijlstraafedadf2009-05-20 12:21:22 +02001067 } else {
1068 if (group_can_go_on(counter, cpuctx, can_add_hw)) {
1069 if (group_sched_in(counter, cpuctx, ctx, cpu))
1070 can_add_hw = 0;
1071 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001072 }
Thomas Gleixner0793a612008-12-04 20:12:29 +01001073 }
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001074 perf_enable();
Paul Mackerrasd859e292009-01-17 18:10:22 +11001075 out:
Thomas Gleixner0793a612008-12-04 20:12:29 +01001076 spin_unlock(&ctx->lock);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001077}
Ingo Molnar04289bb2008-12-11 08:38:42 +01001078
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001079/*
1080 * Called from scheduler to add the counters of the current task
1081 * with interrupts disabled.
1082 *
1083 * We restore the counter value and then enable it.
1084 *
1085 * This does not protect us against NMI, but enable()
1086 * sets the enabled bit in the control field of counter _before_
1087 * accessing the counter control register. If a NMI hits, then it will
1088 * keep the counter running.
1089 */
1090void perf_counter_task_sched_in(struct task_struct *task, int cpu)
1091{
1092 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001093 struct perf_counter_context *ctx = task->perf_counter_ctxp;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001094
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001095 if (likely(!ctx))
1096 return;
Paul Mackerras564c2b22009-05-22 14:27:22 +10001097 if (cpuctx->task_ctx == ctx)
1098 return;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001099 __perf_counter_sched_in(ctx, cpuctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001100 cpuctx->task_ctx = ctx;
1101}
1102
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001103static void perf_counter_cpu_sched_in(struct perf_cpu_context *cpuctx, int cpu)
1104{
1105 struct perf_counter_context *ctx = &cpuctx->ctx;
1106
1107 __perf_counter_sched_in(ctx, cpuctx, cpu);
1108}
1109
Peter Zijlstraa78ac322009-05-25 17:39:05 +02001110#define MAX_INTERRUPTS (~0ULL)
1111
1112static void perf_log_throttle(struct perf_counter *counter, int enable);
Peter Zijlstra26b119b2009-05-20 12:21:20 +02001113static void perf_log_period(struct perf_counter *counter, u64 period);
1114
1115static void perf_adjust_freq(struct perf_counter_context *ctx)
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001116{
1117 struct perf_counter *counter;
Peter Zijlstraa78ac322009-05-25 17:39:05 +02001118 u64 interrupts, irq_period;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001119 u64 events, period;
1120 s64 delta;
1121
1122 spin_lock(&ctx->lock);
1123 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1124 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
1125 continue;
1126
Peter Zijlstraa78ac322009-05-25 17:39:05 +02001127 interrupts = counter->hw.interrupts;
1128 counter->hw.interrupts = 0;
1129
1130 if (interrupts == MAX_INTERRUPTS) {
1131 perf_log_throttle(counter, 1);
1132 counter->pmu->unthrottle(counter);
1133 interrupts = 2*sysctl_perf_counter_limit/HZ;
1134 }
1135
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001136 if (!counter->hw_event.freq || !counter->hw_event.irq_freq)
1137 continue;
1138
Peter Zijlstraa78ac322009-05-25 17:39:05 +02001139 events = HZ * interrupts * counter->hw.irq_period;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001140 period = div64_u64(events, counter->hw_event.irq_freq);
1141
1142 delta = (s64)(1 + period - counter->hw.irq_period);
1143 delta >>= 1;
1144
1145 irq_period = counter->hw.irq_period + delta;
1146
1147 if (!irq_period)
1148 irq_period = 1;
1149
Peter Zijlstra26b119b2009-05-20 12:21:20 +02001150 perf_log_period(counter, irq_period);
1151
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001152 counter->hw.irq_period = irq_period;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001153 }
1154 spin_unlock(&ctx->lock);
1155}
1156
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001157/*
1158 * Round-robin a context's counters:
1159 */
1160static void rotate_ctx(struct perf_counter_context *ctx)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001161{
Thomas Gleixner0793a612008-12-04 20:12:29 +01001162 struct perf_counter *counter;
1163
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001164 if (!ctx->nr_counters)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001165 return;
1166
Thomas Gleixner0793a612008-12-04 20:12:29 +01001167 spin_lock(&ctx->lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001168 /*
Ingo Molnar04289bb2008-12-11 08:38:42 +01001169 * Rotate the first entry last (works just fine for group counters too):
Thomas Gleixner0793a612008-12-04 20:12:29 +01001170 */
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001171 perf_disable();
Ingo Molnar04289bb2008-12-11 08:38:42 +01001172 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Peter Zijlstra75564232009-03-13 12:21:29 +01001173 list_move_tail(&counter->list_entry, &ctx->counter_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001174 break;
1175 }
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001176 perf_enable();
Thomas Gleixner0793a612008-12-04 20:12:29 +01001177
1178 spin_unlock(&ctx->lock);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001179}
Thomas Gleixner0793a612008-12-04 20:12:29 +01001180
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001181void perf_counter_task_tick(struct task_struct *curr, int cpu)
1182{
Peter Zijlstra7fc23a52009-05-08 18:52:21 +02001183 struct perf_cpu_context *cpuctx;
1184 struct perf_counter_context *ctx;
1185
1186 if (!atomic_read(&nr_counters))
1187 return;
1188
1189 cpuctx = &per_cpu(perf_cpu_context, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001190 ctx = curr->perf_counter_ctxp;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001191
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001192 perf_adjust_freq(&cpuctx->ctx);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001193 if (ctx)
1194 perf_adjust_freq(ctx);
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001195
Ingo Molnarb82914c2009-05-04 18:54:32 +02001196 perf_counter_cpu_sched_out(cpuctx);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001197 if (ctx)
1198 __perf_counter_task_sched_out(ctx);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001199
Ingo Molnarb82914c2009-05-04 18:54:32 +02001200 rotate_ctx(&cpuctx->ctx);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001201 if (ctx)
1202 rotate_ctx(ctx);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001203
Ingo Molnarb82914c2009-05-04 18:54:32 +02001204 perf_counter_cpu_sched_in(cpuctx, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001205 if (ctx)
1206 perf_counter_task_sched_in(curr, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001207}
1208
1209/*
Thomas Gleixner0793a612008-12-04 20:12:29 +01001210 * Cross CPU call to read the hardware counter
1211 */
Ingo Molnar76715812008-12-17 14:20:28 +01001212static void __read(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001213{
Ingo Molnar621a01e2008-12-11 12:46:46 +01001214 struct perf_counter *counter = info;
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001215 struct perf_counter_context *ctx = counter->ctx;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001216 unsigned long flags;
Ingo Molnar621a01e2008-12-11 12:46:46 +01001217
Peter Zijlstra849691a2009-04-06 11:45:12 +02001218 local_irq_save(flags);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001219 if (ctx->is_active)
Peter Zijlstra4af49982009-04-06 11:45:10 +02001220 update_context_time(ctx);
Robert Richter4aeb0b42009-04-29 12:47:03 +02001221 counter->pmu->read(counter);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001222 update_counter_times(counter);
Peter Zijlstra849691a2009-04-06 11:45:12 +02001223 local_irq_restore(flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001224}
1225
Ingo Molnar04289bb2008-12-11 08:38:42 +01001226static u64 perf_counter_read(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001227{
1228 /*
1229 * If counter is enabled and currently active on a CPU, update the
1230 * value in the counter structure:
1231 */
Ingo Molnar6a930702008-12-11 15:17:03 +01001232 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
Thomas Gleixner0793a612008-12-04 20:12:29 +01001233 smp_call_function_single(counter->oncpu,
Ingo Molnar76715812008-12-17 14:20:28 +01001234 __read, counter, 1);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001235 } else if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
1236 update_counter_times(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001237 }
1238
Ingo Molnaree060942008-12-13 09:00:03 +01001239 return atomic64_read(&counter->count);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001240}
1241
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001242/*
1243 * Initialize the perf_counter context in a task_struct:
1244 */
1245static void
1246__perf_counter_init_context(struct perf_counter_context *ctx,
1247 struct task_struct *task)
1248{
1249 memset(ctx, 0, sizeof(*ctx));
1250 spin_lock_init(&ctx->lock);
1251 mutex_init(&ctx->mutex);
1252 INIT_LIST_HEAD(&ctx->counter_list);
1253 INIT_LIST_HEAD(&ctx->event_list);
1254 atomic_set(&ctx->refcount, 1);
1255 ctx->task = task;
1256}
1257
Thomas Gleixner0793a612008-12-04 20:12:29 +01001258static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
1259{
1260 struct perf_cpu_context *cpuctx;
1261 struct perf_counter_context *ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001262 struct perf_counter_context *parent_ctx;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001263 struct task_struct *task;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001264 int err;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001265
1266 /*
1267 * If cpu is not a wildcard then this is a percpu counter:
1268 */
1269 if (cpu != -1) {
1270 /* Must be root to operate on a CPU counter: */
Peter Zijlstra1ccd1542009-04-09 10:53:45 +02001271 if (sysctl_perf_counter_priv && !capable(CAP_SYS_ADMIN))
Thomas Gleixner0793a612008-12-04 20:12:29 +01001272 return ERR_PTR(-EACCES);
1273
1274 if (cpu < 0 || cpu > num_possible_cpus())
1275 return ERR_PTR(-EINVAL);
1276
1277 /*
1278 * We could be clever and allow to attach a counter to an
1279 * offline CPU and activate it when the CPU comes up, but
1280 * that's for later.
1281 */
1282 if (!cpu_isset(cpu, cpu_online_map))
1283 return ERR_PTR(-ENODEV);
1284
1285 cpuctx = &per_cpu(perf_cpu_context, cpu);
1286 ctx = &cpuctx->ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001287 get_ctx(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001288
Thomas Gleixner0793a612008-12-04 20:12:29 +01001289 return ctx;
1290 }
1291
1292 rcu_read_lock();
1293 if (!pid)
1294 task = current;
1295 else
1296 task = find_task_by_vpid(pid);
1297 if (task)
1298 get_task_struct(task);
1299 rcu_read_unlock();
1300
1301 if (!task)
1302 return ERR_PTR(-ESRCH);
1303
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001304 /*
1305 * Can't attach counters to a dying task.
1306 */
1307 err = -ESRCH;
1308 if (task->flags & PF_EXITING)
1309 goto errout;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001310
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001311 /* Reuse ptrace permission checks for now. */
1312 err = -EACCES;
1313 if (!ptrace_may_access(task, PTRACE_MODE_READ))
1314 goto errout;
1315
1316 retry_lock:
1317 rcu_read_lock();
1318 retry:
1319 ctx = rcu_dereference(task->perf_counter_ctxp);
1320 if (ctx) {
1321 /*
1322 * If this context is a clone of another, it might
1323 * get swapped for another underneath us by
1324 * perf_counter_task_sched_out, though the
1325 * rcu_read_lock() protects us from any context
1326 * getting freed. Lock the context and check if it
1327 * got swapped before we could get the lock, and retry
1328 * if so. If we locked the right context, then it
1329 * can't get swapped on us any more and we can
1330 * unclone it if necessary.
1331 * Once it's not a clone things will be stable.
1332 */
1333 spin_lock_irq(&ctx->lock);
1334 if (ctx != rcu_dereference(task->perf_counter_ctxp)) {
1335 spin_unlock_irq(&ctx->lock);
1336 goto retry;
1337 }
1338 parent_ctx = ctx->parent_ctx;
1339 if (parent_ctx) {
1340 put_ctx(parent_ctx);
1341 ctx->parent_ctx = NULL; /* no longer a clone */
1342 }
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001343 /*
1344 * Get an extra reference before dropping the lock so that
1345 * this context won't get freed if the task exits.
1346 */
1347 get_ctx(ctx);
1348 spin_unlock_irq(&ctx->lock);
1349 }
1350 rcu_read_unlock();
1351
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001352 if (!ctx) {
1353 ctx = kmalloc(sizeof(struct perf_counter_context), GFP_KERNEL);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001354 err = -ENOMEM;
1355 if (!ctx)
1356 goto errout;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001357 __perf_counter_init_context(ctx, task);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001358 get_ctx(ctx);
1359 if (cmpxchg(&task->perf_counter_ctxp, NULL, ctx)) {
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001360 /*
1361 * We raced with some other task; use
1362 * the context they set.
1363 */
1364 kfree(ctx);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001365 goto retry_lock;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001366 }
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001367 get_task_struct(task);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001368 }
1369
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001370 put_task_struct(task);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001371 return ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001372
1373 errout:
1374 put_task_struct(task);
1375 return ERR_PTR(err);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001376}
1377
Peter Zijlstra592903c2009-03-13 12:21:36 +01001378static void free_counter_rcu(struct rcu_head *head)
1379{
1380 struct perf_counter *counter;
1381
1382 counter = container_of(head, struct perf_counter, rcu_head);
1383 kfree(counter);
1384}
1385
Peter Zijlstra925d5192009-03-30 19:07:02 +02001386static void perf_pending_sync(struct perf_counter *counter);
1387
Peter Zijlstraf1600952009-03-19 20:26:16 +01001388static void free_counter(struct perf_counter *counter)
1389{
Peter Zijlstra925d5192009-03-30 19:07:02 +02001390 perf_pending_sync(counter);
1391
Peter Zijlstra7fc23a52009-05-08 18:52:21 +02001392 atomic_dec(&nr_counters);
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02001393 if (counter->hw_event.mmap)
1394 atomic_dec(&nr_mmap_tracking);
1395 if (counter->hw_event.munmap)
1396 atomic_dec(&nr_munmap_tracking);
1397 if (counter->hw_event.comm)
1398 atomic_dec(&nr_comm_tracking);
1399
Peter Zijlstrae077df42009-03-19 20:26:17 +01001400 if (counter->destroy)
1401 counter->destroy(counter);
1402
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001403 put_ctx(counter->ctx);
Peter Zijlstraf1600952009-03-19 20:26:16 +01001404 call_rcu(&counter->rcu_head, free_counter_rcu);
1405}
1406
Thomas Gleixner0793a612008-12-04 20:12:29 +01001407/*
1408 * Called when the last reference to the file is gone.
1409 */
1410static int perf_release(struct inode *inode, struct file *file)
1411{
1412 struct perf_counter *counter = file->private_data;
1413 struct perf_counter_context *ctx = counter->ctx;
1414
1415 file->private_data = NULL;
1416
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001417 WARN_ON_ONCE(ctx->parent_ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001418 mutex_lock(&ctx->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01001419 perf_counter_remove_from_context(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001420 mutex_unlock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001421
Peter Zijlstra082ff5a2009-05-23 18:29:00 +02001422 mutex_lock(&counter->owner->perf_counter_mutex);
1423 list_del_init(&counter->owner_entry);
1424 mutex_unlock(&counter->owner->perf_counter_mutex);
1425 put_task_struct(counter->owner);
1426
Peter Zijlstraf1600952009-03-19 20:26:16 +01001427 free_counter(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001428
1429 return 0;
1430}
1431
1432/*
1433 * Read the performance counter - simple non blocking version for now
1434 */
1435static ssize_t
1436perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
1437{
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001438 u64 values[3];
1439 int n;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001440
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001441 /*
1442 * Return end-of-file for a read on a counter that is in
1443 * error state (i.e. because it was pinned but it couldn't be
1444 * scheduled on to the CPU at some point).
1445 */
1446 if (counter->state == PERF_COUNTER_STATE_ERROR)
1447 return 0;
1448
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001449 WARN_ON_ONCE(counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001450 mutex_lock(&counter->child_mutex);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001451 values[0] = perf_counter_read(counter);
1452 n = 1;
1453 if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1454 values[n++] = counter->total_time_enabled +
1455 atomic64_read(&counter->child_total_time_enabled);
1456 if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1457 values[n++] = counter->total_time_running +
1458 atomic64_read(&counter->child_total_time_running);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001459 mutex_unlock(&counter->child_mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001460
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001461 if (count < n * sizeof(u64))
1462 return -EINVAL;
1463 count = n * sizeof(u64);
1464
1465 if (copy_to_user(buf, values, count))
1466 return -EFAULT;
1467
1468 return count;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001469}
1470
1471static ssize_t
Thomas Gleixner0793a612008-12-04 20:12:29 +01001472perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
1473{
1474 struct perf_counter *counter = file->private_data;
1475
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001476 return perf_read_hw(counter, buf, count);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001477}
1478
1479static unsigned int perf_poll(struct file *file, poll_table *wait)
1480{
1481 struct perf_counter *counter = file->private_data;
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001482 struct perf_mmap_data *data;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001483 unsigned int events = POLL_HUP;
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001484
1485 rcu_read_lock();
1486 data = rcu_dereference(counter->data);
1487 if (data)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001488 events = atomic_xchg(&data->poll, 0);
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001489 rcu_read_unlock();
Thomas Gleixner0793a612008-12-04 20:12:29 +01001490
1491 poll_wait(file, &counter->waitq, wait);
1492
Thomas Gleixner0793a612008-12-04 20:12:29 +01001493 return events;
1494}
1495
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001496static void perf_counter_reset(struct perf_counter *counter)
1497{
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001498 (void)perf_counter_read(counter);
Paul Mackerras615a3f12009-05-11 15:50:21 +10001499 atomic64_set(&counter->count, 0);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001500 perf_counter_update_userpage(counter);
1501}
1502
1503static void perf_counter_for_each_sibling(struct perf_counter *counter,
1504 void (*func)(struct perf_counter *))
1505{
1506 struct perf_counter_context *ctx = counter->ctx;
1507 struct perf_counter *sibling;
1508
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001509 WARN_ON_ONCE(ctx->parent_ctx);
Peter Zijlstra682076a2009-05-23 18:28:57 +02001510 mutex_lock(&ctx->mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001511 counter = counter->group_leader;
1512
1513 func(counter);
1514 list_for_each_entry(sibling, &counter->sibling_list, list_entry)
1515 func(sibling);
Peter Zijlstra682076a2009-05-23 18:28:57 +02001516 mutex_unlock(&ctx->mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001517}
1518
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001519/*
1520 * Holding the top-level counter's child_mutex means that any
1521 * descendant process that has inherited this counter will block
1522 * in sync_child_counter if it goes to exit, thus satisfying the
1523 * task existence requirements of perf_counter_enable/disable.
1524 */
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001525static void perf_counter_for_each_child(struct perf_counter *counter,
1526 void (*func)(struct perf_counter *))
1527{
1528 struct perf_counter *child;
1529
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001530 WARN_ON_ONCE(counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001531 mutex_lock(&counter->child_mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001532 func(counter);
1533 list_for_each_entry(child, &counter->child_list, child_list)
1534 func(child);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001535 mutex_unlock(&counter->child_mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001536}
1537
1538static void perf_counter_for_each(struct perf_counter *counter,
1539 void (*func)(struct perf_counter *))
1540{
1541 struct perf_counter *child;
1542
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001543 WARN_ON_ONCE(counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001544 mutex_lock(&counter->child_mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001545 perf_counter_for_each_sibling(counter, func);
1546 list_for_each_entry(child, &counter->child_list, child_list)
1547 perf_counter_for_each_sibling(child, func);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001548 mutex_unlock(&counter->child_mutex);
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001549}
1550
Paul Mackerrasd859e292009-01-17 18:10:22 +11001551static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1552{
1553 struct perf_counter *counter = file->private_data;
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001554 void (*func)(struct perf_counter *);
1555 u32 flags = arg;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001556
1557 switch (cmd) {
1558 case PERF_COUNTER_IOC_ENABLE:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001559 func = perf_counter_enable;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001560 break;
1561 case PERF_COUNTER_IOC_DISABLE:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001562 func = perf_counter_disable;
Peter Zijlstra79f14642009-04-06 11:45:07 +02001563 break;
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001564 case PERF_COUNTER_IOC_RESET:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001565 func = perf_counter_reset;
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001566 break;
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001567
1568 case PERF_COUNTER_IOC_REFRESH:
1569 return perf_counter_refresh(counter, arg);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001570 default:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001571 return -ENOTTY;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001572 }
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001573
1574 if (flags & PERF_IOC_FLAG_GROUP)
1575 perf_counter_for_each(counter, func);
1576 else
1577 perf_counter_for_each_child(counter, func);
1578
1579 return 0;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001580}
1581
Peter Zijlstra771d7cd2009-05-25 14:45:26 +02001582int perf_counter_task_enable(void)
1583{
1584 struct perf_counter *counter;
1585
1586 mutex_lock(&current->perf_counter_mutex);
1587 list_for_each_entry(counter, &current->perf_counter_list, owner_entry)
1588 perf_counter_for_each_child(counter, perf_counter_enable);
1589 mutex_unlock(&current->perf_counter_mutex);
1590
1591 return 0;
1592}
1593
1594int perf_counter_task_disable(void)
1595{
1596 struct perf_counter *counter;
1597
1598 mutex_lock(&current->perf_counter_mutex);
1599 list_for_each_entry(counter, &current->perf_counter_list, owner_entry)
1600 perf_counter_for_each_child(counter, perf_counter_disable);
1601 mutex_unlock(&current->perf_counter_mutex);
1602
1603 return 0;
1604}
1605
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001606/*
1607 * Callers need to ensure there can be no nesting of this function, otherwise
1608 * the seqlock logic goes bad. We can not serialize this because the arch
1609 * code calls this from NMI context.
1610 */
1611void perf_counter_update_userpage(struct perf_counter *counter)
Paul Mackerras37d81822009-03-23 18:22:08 +01001612{
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001613 struct perf_mmap_data *data;
1614 struct perf_counter_mmap_page *userpg;
1615
1616 rcu_read_lock();
1617 data = rcu_dereference(counter->data);
1618 if (!data)
1619 goto unlock;
1620
1621 userpg = data->user_page;
Paul Mackerras37d81822009-03-23 18:22:08 +01001622
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001623 /*
1624 * Disable preemption so as to not let the corresponding user-space
1625 * spin too long if we get preempted.
1626 */
1627 preempt_disable();
Paul Mackerras37d81822009-03-23 18:22:08 +01001628 ++userpg->lock;
Peter Zijlstra92f22a32009-04-02 11:12:04 +02001629 barrier();
Paul Mackerras37d81822009-03-23 18:22:08 +01001630 userpg->index = counter->hw.idx;
1631 userpg->offset = atomic64_read(&counter->count);
1632 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
1633 userpg->offset -= atomic64_read(&counter->hw.prev_count);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001634
Peter Zijlstra92f22a32009-04-02 11:12:04 +02001635 barrier();
Paul Mackerras37d81822009-03-23 18:22:08 +01001636 ++userpg->lock;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001637 preempt_enable();
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001638unlock:
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001639 rcu_read_unlock();
Paul Mackerras37d81822009-03-23 18:22:08 +01001640}
1641
1642static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1643{
1644 struct perf_counter *counter = vma->vm_file->private_data;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001645 struct perf_mmap_data *data;
1646 int ret = VM_FAULT_SIGBUS;
Paul Mackerras37d81822009-03-23 18:22:08 +01001647
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001648 rcu_read_lock();
1649 data = rcu_dereference(counter->data);
1650 if (!data)
1651 goto unlock;
Paul Mackerras37d81822009-03-23 18:22:08 +01001652
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001653 if (vmf->pgoff == 0) {
1654 vmf->page = virt_to_page(data->user_page);
1655 } else {
1656 int nr = vmf->pgoff - 1;
1657
1658 if ((unsigned)nr > data->nr_pages)
1659 goto unlock;
1660
1661 vmf->page = virt_to_page(data->data_pages[nr]);
1662 }
Paul Mackerras37d81822009-03-23 18:22:08 +01001663 get_page(vmf->page);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001664 ret = 0;
1665unlock:
1666 rcu_read_unlock();
1667
1668 return ret;
1669}
1670
1671static int perf_mmap_data_alloc(struct perf_counter *counter, int nr_pages)
1672{
1673 struct perf_mmap_data *data;
1674 unsigned long size;
1675 int i;
1676
1677 WARN_ON(atomic_read(&counter->mmap_count));
1678
1679 size = sizeof(struct perf_mmap_data);
1680 size += nr_pages * sizeof(void *);
1681
1682 data = kzalloc(size, GFP_KERNEL);
1683 if (!data)
1684 goto fail;
1685
1686 data->user_page = (void *)get_zeroed_page(GFP_KERNEL);
1687 if (!data->user_page)
1688 goto fail_user_page;
1689
1690 for (i = 0; i < nr_pages; i++) {
1691 data->data_pages[i] = (void *)get_zeroed_page(GFP_KERNEL);
1692 if (!data->data_pages[i])
1693 goto fail_data_pages;
1694 }
1695
1696 data->nr_pages = nr_pages;
Peter Zijlstra22c15582009-05-05 17:50:25 +02001697 atomic_set(&data->lock, -1);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001698
1699 rcu_assign_pointer(counter->data, data);
1700
Paul Mackerras37d81822009-03-23 18:22:08 +01001701 return 0;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001702
1703fail_data_pages:
1704 for (i--; i >= 0; i--)
1705 free_page((unsigned long)data->data_pages[i]);
1706
1707 free_page((unsigned long)data->user_page);
1708
1709fail_user_page:
1710 kfree(data);
1711
1712fail:
1713 return -ENOMEM;
1714}
1715
1716static void __perf_mmap_data_free(struct rcu_head *rcu_head)
1717{
1718 struct perf_mmap_data *data = container_of(rcu_head,
1719 struct perf_mmap_data, rcu_head);
1720 int i;
1721
1722 free_page((unsigned long)data->user_page);
1723 for (i = 0; i < data->nr_pages; i++)
1724 free_page((unsigned long)data->data_pages[i]);
1725 kfree(data);
1726}
1727
1728static void perf_mmap_data_free(struct perf_counter *counter)
1729{
1730 struct perf_mmap_data *data = counter->data;
1731
1732 WARN_ON(atomic_read(&counter->mmap_count));
1733
1734 rcu_assign_pointer(counter->data, NULL);
1735 call_rcu(&data->rcu_head, __perf_mmap_data_free);
1736}
1737
1738static void perf_mmap_open(struct vm_area_struct *vma)
1739{
1740 struct perf_counter *counter = vma->vm_file->private_data;
1741
1742 atomic_inc(&counter->mmap_count);
1743}
1744
1745static void perf_mmap_close(struct vm_area_struct *vma)
1746{
1747 struct perf_counter *counter = vma->vm_file->private_data;
1748
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001749 WARN_ON_ONCE(counter->ctx->parent_ctx);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001750 if (atomic_dec_and_mutex_lock(&counter->mmap_count,
1751 &counter->mmap_mutex)) {
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001752 struct user_struct *user = current_user();
1753
1754 atomic_long_sub(counter->data->nr_pages + 1, &user->locked_vm);
Peter Zijlstrac5078f72009-05-05 17:50:24 +02001755 vma->vm_mm->locked_vm -= counter->data->nr_locked;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001756 perf_mmap_data_free(counter);
1757 mutex_unlock(&counter->mmap_mutex);
1758 }
Paul Mackerras37d81822009-03-23 18:22:08 +01001759}
1760
1761static struct vm_operations_struct perf_mmap_vmops = {
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001762 .open = perf_mmap_open,
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001763 .close = perf_mmap_close,
Paul Mackerras37d81822009-03-23 18:22:08 +01001764 .fault = perf_mmap_fault,
1765};
1766
1767static int perf_mmap(struct file *file, struct vm_area_struct *vma)
1768{
1769 struct perf_counter *counter = file->private_data;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001770 struct user_struct *user = current_user();
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001771 unsigned long vma_size;
1772 unsigned long nr_pages;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001773 unsigned long user_locked, user_lock_limit;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001774 unsigned long locked, lock_limit;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001775 long user_extra, extra;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001776 int ret = 0;
Paul Mackerras37d81822009-03-23 18:22:08 +01001777
1778 if (!(vma->vm_flags & VM_SHARED) || (vma->vm_flags & VM_WRITE))
1779 return -EINVAL;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001780
1781 vma_size = vma->vm_end - vma->vm_start;
1782 nr_pages = (vma_size / PAGE_SIZE) - 1;
1783
Peter Zijlstra7730d862009-03-25 12:48:31 +01001784 /*
1785 * If we have data pages ensure they're a power-of-two number, so we
1786 * can do bitmasks instead of modulo.
1787 */
1788 if (nr_pages != 0 && !is_power_of_2(nr_pages))
Paul Mackerras37d81822009-03-23 18:22:08 +01001789 return -EINVAL;
1790
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001791 if (vma_size != PAGE_SIZE * (1 + nr_pages))
Paul Mackerras37d81822009-03-23 18:22:08 +01001792 return -EINVAL;
1793
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001794 if (vma->vm_pgoff != 0)
1795 return -EINVAL;
Paul Mackerras37d81822009-03-23 18:22:08 +01001796
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001797 WARN_ON_ONCE(counter->ctx->parent_ctx);
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001798 mutex_lock(&counter->mmap_mutex);
1799 if (atomic_inc_not_zero(&counter->mmap_count)) {
1800 if (nr_pages != counter->data->nr_pages)
1801 ret = -EINVAL;
1802 goto unlock;
1803 }
1804
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001805 user_extra = nr_pages + 1;
1806 user_lock_limit = sysctl_perf_counter_mlock >> (PAGE_SHIFT - 10);
Ingo Molnara3862d32009-05-24 09:02:37 +02001807
1808 /*
1809 * Increase the limit linearly with more CPUs:
1810 */
1811 user_lock_limit *= num_online_cpus();
1812
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001813 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
Peter Zijlstrac5078f72009-05-05 17:50:24 +02001814
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001815 extra = 0;
1816 if (user_locked > user_lock_limit)
1817 extra = user_locked - user_lock_limit;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001818
1819 lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
1820 lock_limit >>= PAGE_SHIFT;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001821 locked = vma->vm_mm->locked_vm + extra;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001822
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001823 if ((locked > lock_limit) && !capable(CAP_IPC_LOCK)) {
1824 ret = -EPERM;
1825 goto unlock;
1826 }
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001827
1828 WARN_ON(counter->data);
1829 ret = perf_mmap_data_alloc(counter, nr_pages);
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001830 if (ret)
1831 goto unlock;
1832
1833 atomic_set(&counter->mmap_count, 1);
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001834 atomic_long_add(user_extra, &user->locked_vm);
Peter Zijlstrac5078f72009-05-05 17:50:24 +02001835 vma->vm_mm->locked_vm += extra;
1836 counter->data->nr_locked = extra;
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001837unlock:
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001838 mutex_unlock(&counter->mmap_mutex);
Paul Mackerras37d81822009-03-23 18:22:08 +01001839
1840 vma->vm_flags &= ~VM_MAYWRITE;
1841 vma->vm_flags |= VM_RESERVED;
1842 vma->vm_ops = &perf_mmap_vmops;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001843
1844 return ret;
Paul Mackerras37d81822009-03-23 18:22:08 +01001845}
1846
Peter Zijlstra3c446b32009-04-06 11:45:01 +02001847static int perf_fasync(int fd, struct file *filp, int on)
1848{
1849 struct perf_counter *counter = filp->private_data;
1850 struct inode *inode = filp->f_path.dentry->d_inode;
1851 int retval;
1852
1853 mutex_lock(&inode->i_mutex);
1854 retval = fasync_helper(fd, filp, on, &counter->fasync);
1855 mutex_unlock(&inode->i_mutex);
1856
1857 if (retval < 0)
1858 return retval;
1859
1860 return 0;
1861}
1862
Thomas Gleixner0793a612008-12-04 20:12:29 +01001863static const struct file_operations perf_fops = {
1864 .release = perf_release,
1865 .read = perf_read,
1866 .poll = perf_poll,
Paul Mackerrasd859e292009-01-17 18:10:22 +11001867 .unlocked_ioctl = perf_ioctl,
1868 .compat_ioctl = perf_ioctl,
Paul Mackerras37d81822009-03-23 18:22:08 +01001869 .mmap = perf_mmap,
Peter Zijlstra3c446b32009-04-06 11:45:01 +02001870 .fasync = perf_fasync,
Thomas Gleixner0793a612008-12-04 20:12:29 +01001871};
1872
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001873/*
Peter Zijlstra925d5192009-03-30 19:07:02 +02001874 * Perf counter wakeup
1875 *
1876 * If there's data, ensure we set the poll() state and publish everything
1877 * to user-space before waking everybody up.
1878 */
1879
1880void perf_counter_wakeup(struct perf_counter *counter)
1881{
Peter Zijlstra925d5192009-03-30 19:07:02 +02001882 wake_up_all(&counter->waitq);
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02001883
1884 if (counter->pending_kill) {
1885 kill_fasync(&counter->fasync, SIGIO, counter->pending_kill);
1886 counter->pending_kill = 0;
1887 }
Peter Zijlstra925d5192009-03-30 19:07:02 +02001888}
1889
1890/*
1891 * Pending wakeups
1892 *
1893 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
1894 *
1895 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
1896 * single linked list and use cmpxchg() to add entries lockless.
1897 */
1898
Peter Zijlstra79f14642009-04-06 11:45:07 +02001899static void perf_pending_counter(struct perf_pending_entry *entry)
1900{
1901 struct perf_counter *counter = container_of(entry,
1902 struct perf_counter, pending);
1903
1904 if (counter->pending_disable) {
1905 counter->pending_disable = 0;
1906 perf_counter_disable(counter);
1907 }
1908
1909 if (counter->pending_wakeup) {
1910 counter->pending_wakeup = 0;
1911 perf_counter_wakeup(counter);
1912 }
1913}
1914
Peter Zijlstra671dec52009-04-06 11:45:02 +02001915#define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
Peter Zijlstra925d5192009-03-30 19:07:02 +02001916
Peter Zijlstra671dec52009-04-06 11:45:02 +02001917static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = {
Peter Zijlstra925d5192009-03-30 19:07:02 +02001918 PENDING_TAIL,
1919};
1920
Peter Zijlstra671dec52009-04-06 11:45:02 +02001921static void perf_pending_queue(struct perf_pending_entry *entry,
1922 void (*func)(struct perf_pending_entry *))
Peter Zijlstra925d5192009-03-30 19:07:02 +02001923{
Peter Zijlstra671dec52009-04-06 11:45:02 +02001924 struct perf_pending_entry **head;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001925
Peter Zijlstra671dec52009-04-06 11:45:02 +02001926 if (cmpxchg(&entry->next, NULL, PENDING_TAIL) != NULL)
Peter Zijlstra925d5192009-03-30 19:07:02 +02001927 return;
1928
Peter Zijlstra671dec52009-04-06 11:45:02 +02001929 entry->func = func;
1930
1931 head = &get_cpu_var(perf_pending_head);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001932
1933 do {
Peter Zijlstra671dec52009-04-06 11:45:02 +02001934 entry->next = *head;
1935 } while (cmpxchg(head, entry->next, entry) != entry->next);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001936
1937 set_perf_counter_pending();
1938
Peter Zijlstra671dec52009-04-06 11:45:02 +02001939 put_cpu_var(perf_pending_head);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001940}
1941
1942static int __perf_pending_run(void)
1943{
Peter Zijlstra671dec52009-04-06 11:45:02 +02001944 struct perf_pending_entry *list;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001945 int nr = 0;
1946
Peter Zijlstra671dec52009-04-06 11:45:02 +02001947 list = xchg(&__get_cpu_var(perf_pending_head), PENDING_TAIL);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001948 while (list != PENDING_TAIL) {
Peter Zijlstra671dec52009-04-06 11:45:02 +02001949 void (*func)(struct perf_pending_entry *);
1950 struct perf_pending_entry *entry = list;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001951
1952 list = list->next;
1953
Peter Zijlstra671dec52009-04-06 11:45:02 +02001954 func = entry->func;
1955 entry->next = NULL;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001956 /*
1957 * Ensure we observe the unqueue before we issue the wakeup,
1958 * so that we won't be waiting forever.
1959 * -- see perf_not_pending().
1960 */
1961 smp_wmb();
1962
Peter Zijlstra671dec52009-04-06 11:45:02 +02001963 func(entry);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001964 nr++;
1965 }
1966
1967 return nr;
1968}
1969
1970static inline int perf_not_pending(struct perf_counter *counter)
1971{
1972 /*
1973 * If we flush on whatever cpu we run, there is a chance we don't
1974 * need to wait.
1975 */
1976 get_cpu();
1977 __perf_pending_run();
1978 put_cpu();
1979
1980 /*
1981 * Ensure we see the proper queue state before going to sleep
1982 * so that we do not miss the wakeup. -- see perf_pending_handle()
1983 */
1984 smp_rmb();
Peter Zijlstra671dec52009-04-06 11:45:02 +02001985 return counter->pending.next == NULL;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001986}
1987
1988static void perf_pending_sync(struct perf_counter *counter)
1989{
1990 wait_event(counter->waitq, perf_not_pending(counter));
1991}
1992
1993void perf_counter_do_pending(void)
1994{
1995 __perf_pending_run();
1996}
1997
1998/*
Peter Zijlstra394ee072009-03-30 19:07:14 +02001999 * Callchain support -- arch specific
2000 */
2001
Peter Zijlstra9c03d882009-04-06 11:45:00 +02002002__weak struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
Peter Zijlstra394ee072009-03-30 19:07:14 +02002003{
2004 return NULL;
2005}
2006
2007/*
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002008 * Output
2009 */
2010
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002011struct perf_output_handle {
2012 struct perf_counter *counter;
2013 struct perf_mmap_data *data;
2014 unsigned int offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01002015 unsigned int head;
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002016 int nmi;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002017 int overflow;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002018 int locked;
2019 unsigned long flags;
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002020};
2021
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002022static void perf_output_wakeup(struct perf_output_handle *handle)
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002023{
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002024 atomic_set(&handle->data->poll, POLL_IN);
2025
Peter Zijlstra671dec52009-04-06 11:45:02 +02002026 if (handle->nmi) {
Peter Zijlstra79f14642009-04-06 11:45:07 +02002027 handle->counter->pending_wakeup = 1;
Peter Zijlstra671dec52009-04-06 11:45:02 +02002028 perf_pending_queue(&handle->counter->pending,
Peter Zijlstra79f14642009-04-06 11:45:07 +02002029 perf_pending_counter);
Peter Zijlstra671dec52009-04-06 11:45:02 +02002030 } else
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002031 perf_counter_wakeup(handle->counter);
2032}
2033
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002034/*
2035 * Curious locking construct.
2036 *
2037 * We need to ensure a later event doesn't publish a head when a former
2038 * event isn't done writing. However since we need to deal with NMIs we
2039 * cannot fully serialize things.
2040 *
2041 * What we do is serialize between CPUs so we only have to deal with NMI
2042 * nesting on a single CPU.
2043 *
2044 * We only publish the head (and generate a wakeup) when the outer-most
2045 * event completes.
2046 */
2047static void perf_output_lock(struct perf_output_handle *handle)
2048{
2049 struct perf_mmap_data *data = handle->data;
2050 int cpu;
2051
2052 handle->locked = 0;
2053
2054 local_irq_save(handle->flags);
2055 cpu = smp_processor_id();
2056
2057 if (in_nmi() && atomic_read(&data->lock) == cpu)
2058 return;
2059
Peter Zijlstra22c15582009-05-05 17:50:25 +02002060 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002061 cpu_relax();
2062
2063 handle->locked = 1;
2064}
2065
2066static void perf_output_unlock(struct perf_output_handle *handle)
2067{
2068 struct perf_mmap_data *data = handle->data;
2069 int head, cpu;
2070
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002071 data->done_head = data->head;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002072
2073 if (!handle->locked)
2074 goto out;
2075
2076again:
2077 /*
2078 * The xchg implies a full barrier that ensures all writes are done
2079 * before we publish the new head, matched by a rmb() in userspace when
2080 * reading this position.
2081 */
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002082 while ((head = atomic_xchg(&data->done_head, 0)))
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002083 data->user_page->data_head = head;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002084
2085 /*
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002086 * NMI can happen here, which means we can miss a done_head update.
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002087 */
2088
Peter Zijlstra22c15582009-05-05 17:50:25 +02002089 cpu = atomic_xchg(&data->lock, -1);
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002090 WARN_ON_ONCE(cpu != smp_processor_id());
2091
2092 /*
2093 * Therefore we have to validate we did not indeed do so.
2094 */
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002095 if (unlikely(atomic_read(&data->done_head))) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002096 /*
2097 * Since we had it locked, we can lock it again.
2098 */
Peter Zijlstra22c15582009-05-05 17:50:25 +02002099 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002100 cpu_relax();
2101
2102 goto again;
2103 }
2104
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002105 if (atomic_xchg(&data->wakeup, 0))
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002106 perf_output_wakeup(handle);
2107out:
2108 local_irq_restore(handle->flags);
2109}
2110
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002111static int perf_output_begin(struct perf_output_handle *handle,
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002112 struct perf_counter *counter, unsigned int size,
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002113 int nmi, int overflow)
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002114{
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002115 struct perf_mmap_data *data;
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002116 unsigned int offset, head;
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002117
Peter Zijlstra2023b352009-05-05 17:50:26 +02002118 /*
2119 * For inherited counters we send all the output towards the parent.
2120 */
2121 if (counter->parent)
2122 counter = counter->parent;
2123
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002124 rcu_read_lock();
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002125 data = rcu_dereference(counter->data);
2126 if (!data)
2127 goto out;
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002128
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002129 handle->data = data;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002130 handle->counter = counter;
2131 handle->nmi = nmi;
2132 handle->overflow = overflow;
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002133
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002134 if (!data->nr_pages)
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002135 goto fail;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002136
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002137 perf_output_lock(handle);
2138
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002139 do {
2140 offset = head = atomic_read(&data->head);
Peter Zijlstrac7138f32009-03-24 13:18:16 +01002141 head += size;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002142 } while (atomic_cmpxchg(&data->head, offset, head) != offset);
2143
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002144 handle->offset = offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01002145 handle->head = head;
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002146
2147 if ((offset >> PAGE_SHIFT) != (head >> PAGE_SHIFT))
2148 atomic_set(&data->wakeup, 1);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002149
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002150 return 0;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002151
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002152fail:
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002153 perf_output_wakeup(handle);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002154out:
2155 rcu_read_unlock();
2156
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002157 return -ENOSPC;
2158}
2159
2160static void perf_output_copy(struct perf_output_handle *handle,
2161 void *buf, unsigned int len)
2162{
2163 unsigned int pages_mask;
2164 unsigned int offset;
2165 unsigned int size;
2166 void **pages;
2167
2168 offset = handle->offset;
2169 pages_mask = handle->data->nr_pages - 1;
2170 pages = handle->data->data_pages;
2171
2172 do {
2173 unsigned int page_offset;
2174 int nr;
2175
2176 nr = (offset >> PAGE_SHIFT) & pages_mask;
2177 page_offset = offset & (PAGE_SIZE - 1);
2178 size = min_t(unsigned int, PAGE_SIZE - page_offset, len);
2179
2180 memcpy(pages[nr] + page_offset, buf, size);
2181
2182 len -= size;
2183 buf += size;
2184 offset += size;
2185 } while (len);
2186
2187 handle->offset = offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01002188
Peter Zijlstra53020fe2009-05-13 21:26:19 +02002189 /*
2190 * Check we didn't copy past our reservation window, taking the
2191 * possible unsigned int wrap into account.
2192 */
2193 WARN_ON_ONCE(((int)(handle->head - handle->offset)) < 0);
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002194}
2195
Peter Zijlstra5c148192009-03-25 12:30:23 +01002196#define perf_output_put(handle, x) \
2197 perf_output_copy((handle), &(x), sizeof(x))
2198
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002199static void perf_output_end(struct perf_output_handle *handle)
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002200{
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002201 struct perf_counter *counter = handle->counter;
2202 struct perf_mmap_data *data = handle->data;
2203
2204 int wakeup_events = counter->hw_event.wakeup_events;
Peter Zijlstrac4578102009-04-02 11:12:01 +02002205
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002206 if (handle->overflow && wakeup_events) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002207 int events = atomic_inc_return(&data->events);
Peter Zijlstrac4578102009-04-02 11:12:01 +02002208 if (events >= wakeup_events) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002209 atomic_sub(wakeup_events, &data->events);
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002210 atomic_set(&data->wakeup, 1);
Peter Zijlstrac4578102009-04-02 11:12:01 +02002211 }
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002212 }
2213
2214 perf_output_unlock(handle);
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002215 rcu_read_unlock();
2216}
2217
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002218static void perf_counter_output(struct perf_counter *counter,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002219 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002220{
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002221 int ret;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002222 u64 record_type = counter->hw_event.record_type;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002223 struct perf_output_handle handle;
2224 struct perf_event_header header;
2225 u64 ip;
Peter Zijlstra5c148192009-03-25 12:30:23 +01002226 struct {
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002227 u32 pid, tid;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002228 } tid_entry;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002229 struct {
2230 u64 event;
2231 u64 counter;
2232 } group_entry;
Peter Zijlstra394ee072009-03-30 19:07:14 +02002233 struct perf_callchain_entry *callchain = NULL;
2234 int callchain_size = 0;
Peter Zijlstra339f7c92009-04-06 11:45:06 +02002235 u64 time;
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002236 struct {
2237 u32 cpu, reserved;
2238 } cpu_entry;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002239
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02002240 header.type = 0;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002241 header.size = sizeof(header);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002242
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02002243 header.misc = PERF_EVENT_MISC_OVERFLOW;
Paul Mackerras9d23a902009-05-14 21:48:08 +10002244 header.misc |= perf_misc_flags(regs);
Peter Zijlstra6fab0192009-04-08 15:01:26 +02002245
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002246 if (record_type & PERF_RECORD_IP) {
Paul Mackerras9d23a902009-05-14 21:48:08 +10002247 ip = perf_instruction_pointer(regs);
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02002248 header.type |= PERF_RECORD_IP;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002249 header.size += sizeof(ip);
2250 }
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002251
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002252 if (record_type & PERF_RECORD_TID) {
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002253 /* namespace issues */
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002254 tid_entry.pid = current->group_leader->pid;
2255 tid_entry.tid = current->pid;
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002256
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02002257 header.type |= PERF_RECORD_TID;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002258 header.size += sizeof(tid_entry);
2259 }
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002260
Peter Zijlstra4d855452009-04-08 15:01:32 +02002261 if (record_type & PERF_RECORD_TIME) {
2262 /*
2263 * Maybe do better on x86 and provide cpu_clock_nmi()
2264 */
2265 time = sched_clock();
2266
2267 header.type |= PERF_RECORD_TIME;
2268 header.size += sizeof(u64);
2269 }
2270
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002271 if (record_type & PERF_RECORD_ADDR) {
2272 header.type |= PERF_RECORD_ADDR;
2273 header.size += sizeof(u64);
2274 }
2275
Peter Zijlstraa85f61a2009-05-08 18:52:23 +02002276 if (record_type & PERF_RECORD_CONFIG) {
2277 header.type |= PERF_RECORD_CONFIG;
2278 header.size += sizeof(u64);
2279 }
2280
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002281 if (record_type & PERF_RECORD_CPU) {
2282 header.type |= PERF_RECORD_CPU;
2283 header.size += sizeof(cpu_entry);
2284
2285 cpu_entry.cpu = raw_smp_processor_id();
2286 }
2287
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002288 if (record_type & PERF_RECORD_GROUP) {
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02002289 header.type |= PERF_RECORD_GROUP;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002290 header.size += sizeof(u64) +
2291 counter->nr_siblings * sizeof(group_entry);
2292 }
2293
2294 if (record_type & PERF_RECORD_CALLCHAIN) {
Peter Zijlstra394ee072009-03-30 19:07:14 +02002295 callchain = perf_callchain(regs);
2296
2297 if (callchain) {
Peter Zijlstra9c03d882009-04-06 11:45:00 +02002298 callchain_size = (1 + callchain->nr) * sizeof(u64);
Peter Zijlstra394ee072009-03-30 19:07:14 +02002299
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02002300 header.type |= PERF_RECORD_CALLCHAIN;
Peter Zijlstra394ee072009-03-30 19:07:14 +02002301 header.size += callchain_size;
2302 }
2303 }
2304
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002305 ret = perf_output_begin(&handle, counter, header.size, nmi, 1);
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002306 if (ret)
2307 return;
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002308
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002309 perf_output_put(&handle, header);
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002310
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002311 if (record_type & PERF_RECORD_IP)
2312 perf_output_put(&handle, ip);
2313
2314 if (record_type & PERF_RECORD_TID)
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002315 perf_output_put(&handle, tid_entry);
2316
Peter Zijlstra4d855452009-04-08 15:01:32 +02002317 if (record_type & PERF_RECORD_TIME)
2318 perf_output_put(&handle, time);
2319
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002320 if (record_type & PERF_RECORD_ADDR)
2321 perf_output_put(&handle, addr);
2322
Peter Zijlstraa85f61a2009-05-08 18:52:23 +02002323 if (record_type & PERF_RECORD_CONFIG)
2324 perf_output_put(&handle, counter->hw_event.config);
2325
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002326 if (record_type & PERF_RECORD_CPU)
2327 perf_output_put(&handle, cpu_entry);
2328
Peter Zijlstra2023b352009-05-05 17:50:26 +02002329 /*
2330 * XXX PERF_RECORD_GROUP vs inherited counters seems difficult.
2331 */
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002332 if (record_type & PERF_RECORD_GROUP) {
2333 struct perf_counter *leader, *sub;
2334 u64 nr = counter->nr_siblings;
2335
2336 perf_output_put(&handle, nr);
2337
2338 leader = counter->group_leader;
2339 list_for_each_entry(sub, &leader->sibling_list, list_entry) {
2340 if (sub != counter)
Robert Richter4aeb0b42009-04-29 12:47:03 +02002341 sub->pmu->read(sub);
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002342
2343 group_entry.event = sub->hw_event.config;
2344 group_entry.counter = atomic64_read(&sub->count);
2345
2346 perf_output_put(&handle, group_entry);
2347 }
2348 }
2349
Peter Zijlstra394ee072009-03-30 19:07:14 +02002350 if (callchain)
2351 perf_output_copy(&handle, callchain, callchain_size);
2352
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002353 perf_output_end(&handle);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002354}
2355
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002356/*
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002357 * comm tracking
2358 */
2359
2360struct perf_comm_event {
2361 struct task_struct *task;
2362 char *comm;
2363 int comm_size;
2364
2365 struct {
2366 struct perf_event_header header;
2367
2368 u32 pid;
2369 u32 tid;
2370 } event;
2371};
2372
2373static void perf_counter_comm_output(struct perf_counter *counter,
2374 struct perf_comm_event *comm_event)
2375{
2376 struct perf_output_handle handle;
2377 int size = comm_event->event.header.size;
2378 int ret = perf_output_begin(&handle, counter, size, 0, 0);
2379
2380 if (ret)
2381 return;
2382
2383 perf_output_put(&handle, comm_event->event);
2384 perf_output_copy(&handle, comm_event->comm,
2385 comm_event->comm_size);
2386 perf_output_end(&handle);
2387}
2388
2389static int perf_counter_comm_match(struct perf_counter *counter,
2390 struct perf_comm_event *comm_event)
2391{
2392 if (counter->hw_event.comm &&
2393 comm_event->event.header.type == PERF_EVENT_COMM)
2394 return 1;
2395
2396 return 0;
2397}
2398
2399static void perf_counter_comm_ctx(struct perf_counter_context *ctx,
2400 struct perf_comm_event *comm_event)
2401{
2402 struct perf_counter *counter;
2403
2404 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2405 return;
2406
2407 rcu_read_lock();
2408 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2409 if (perf_counter_comm_match(counter, comm_event))
2410 perf_counter_comm_output(counter, comm_event);
2411 }
2412 rcu_read_unlock();
2413}
2414
2415static void perf_counter_comm_event(struct perf_comm_event *comm_event)
2416{
2417 struct perf_cpu_context *cpuctx;
2418 unsigned int size;
2419 char *comm = comm_event->task->comm;
2420
Ingo Molnar888fcee2009-04-09 09:48:22 +02002421 size = ALIGN(strlen(comm)+1, sizeof(u64));
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002422
2423 comm_event->comm = comm;
2424 comm_event->comm_size = size;
2425
2426 comm_event->event.header.size = sizeof(comm_event->event) + size;
2427
2428 cpuctx = &get_cpu_var(perf_cpu_context);
2429 perf_counter_comm_ctx(&cpuctx->ctx, comm_event);
2430 put_cpu_var(perf_cpu_context);
2431
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10002432 perf_counter_comm_ctx(current->perf_counter_ctxp, comm_event);
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002433}
2434
2435void perf_counter_comm(struct task_struct *task)
2436{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002437 struct perf_comm_event comm_event;
2438
2439 if (!atomic_read(&nr_comm_tracking))
2440 return;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10002441 if (!current->perf_counter_ctxp)
2442 return;
2443
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002444 comm_event = (struct perf_comm_event){
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002445 .task = task,
2446 .event = {
2447 .header = { .type = PERF_EVENT_COMM, },
2448 .pid = task->group_leader->pid,
2449 .tid = task->pid,
2450 },
2451 };
2452
2453 perf_counter_comm_event(&comm_event);
2454}
2455
2456/*
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002457 * mmap tracking
2458 */
2459
2460struct perf_mmap_event {
2461 struct file *file;
2462 char *file_name;
2463 int file_size;
2464
2465 struct {
2466 struct perf_event_header header;
2467
2468 u32 pid;
2469 u32 tid;
2470 u64 start;
2471 u64 len;
2472 u64 pgoff;
2473 } event;
2474};
2475
2476static void perf_counter_mmap_output(struct perf_counter *counter,
2477 struct perf_mmap_event *mmap_event)
2478{
2479 struct perf_output_handle handle;
2480 int size = mmap_event->event.header.size;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002481 int ret = perf_output_begin(&handle, counter, size, 0, 0);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002482
2483 if (ret)
2484 return;
2485
2486 perf_output_put(&handle, mmap_event->event);
2487 perf_output_copy(&handle, mmap_event->file_name,
2488 mmap_event->file_size);
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002489 perf_output_end(&handle);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002490}
2491
2492static int perf_counter_mmap_match(struct perf_counter *counter,
2493 struct perf_mmap_event *mmap_event)
2494{
2495 if (counter->hw_event.mmap &&
2496 mmap_event->event.header.type == PERF_EVENT_MMAP)
2497 return 1;
2498
2499 if (counter->hw_event.munmap &&
2500 mmap_event->event.header.type == PERF_EVENT_MUNMAP)
2501 return 1;
2502
2503 return 0;
2504}
2505
2506static void perf_counter_mmap_ctx(struct perf_counter_context *ctx,
2507 struct perf_mmap_event *mmap_event)
2508{
2509 struct perf_counter *counter;
2510
2511 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2512 return;
2513
2514 rcu_read_lock();
2515 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2516 if (perf_counter_mmap_match(counter, mmap_event))
2517 perf_counter_mmap_output(counter, mmap_event);
2518 }
2519 rcu_read_unlock();
2520}
2521
2522static void perf_counter_mmap_event(struct perf_mmap_event *mmap_event)
2523{
2524 struct perf_cpu_context *cpuctx;
2525 struct file *file = mmap_event->file;
2526 unsigned int size;
2527 char tmp[16];
2528 char *buf = NULL;
2529 char *name;
2530
2531 if (file) {
2532 buf = kzalloc(PATH_MAX, GFP_KERNEL);
2533 if (!buf) {
2534 name = strncpy(tmp, "//enomem", sizeof(tmp));
2535 goto got_name;
2536 }
Peter Zijlstrad3d21c42009-04-09 10:53:46 +02002537 name = d_path(&file->f_path, buf, PATH_MAX);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002538 if (IS_ERR(name)) {
2539 name = strncpy(tmp, "//toolong", sizeof(tmp));
2540 goto got_name;
2541 }
2542 } else {
2543 name = strncpy(tmp, "//anon", sizeof(tmp));
2544 goto got_name;
2545 }
2546
2547got_name:
Ingo Molnar888fcee2009-04-09 09:48:22 +02002548 size = ALIGN(strlen(name)+1, sizeof(u64));
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002549
2550 mmap_event->file_name = name;
2551 mmap_event->file_size = size;
2552
2553 mmap_event->event.header.size = sizeof(mmap_event->event) + size;
2554
2555 cpuctx = &get_cpu_var(perf_cpu_context);
2556 perf_counter_mmap_ctx(&cpuctx->ctx, mmap_event);
2557 put_cpu_var(perf_cpu_context);
2558
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10002559 perf_counter_mmap_ctx(current->perf_counter_ctxp, mmap_event);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002560
2561 kfree(buf);
2562}
2563
2564void perf_counter_mmap(unsigned long addr, unsigned long len,
2565 unsigned long pgoff, struct file *file)
2566{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002567 struct perf_mmap_event mmap_event;
2568
2569 if (!atomic_read(&nr_mmap_tracking))
2570 return;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10002571 if (!current->perf_counter_ctxp)
2572 return;
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002573
2574 mmap_event = (struct perf_mmap_event){
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002575 .file = file,
2576 .event = {
2577 .header = { .type = PERF_EVENT_MMAP, },
2578 .pid = current->group_leader->pid,
2579 .tid = current->pid,
2580 .start = addr,
2581 .len = len,
2582 .pgoff = pgoff,
2583 },
2584 };
2585
2586 perf_counter_mmap_event(&mmap_event);
2587}
2588
2589void perf_counter_munmap(unsigned long addr, unsigned long len,
2590 unsigned long pgoff, struct file *file)
2591{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002592 struct perf_mmap_event mmap_event;
2593
2594 if (!atomic_read(&nr_munmap_tracking))
2595 return;
2596
2597 mmap_event = (struct perf_mmap_event){
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002598 .file = file,
2599 .event = {
2600 .header = { .type = PERF_EVENT_MUNMAP, },
2601 .pid = current->group_leader->pid,
2602 .tid = current->pid,
2603 .start = addr,
2604 .len = len,
2605 .pgoff = pgoff,
2606 },
2607 };
2608
2609 perf_counter_mmap_event(&mmap_event);
2610}
2611
2612/*
Peter Zijlstrae220d2d2009-05-23 18:28:55 +02002613 * Log irq_period changes so that analyzing tools can re-normalize the
2614 * event flow.
Peter Zijlstra26b119b2009-05-20 12:21:20 +02002615 */
2616
2617static void perf_log_period(struct perf_counter *counter, u64 period)
2618{
2619 struct perf_output_handle handle;
2620 int ret;
2621
2622 struct {
2623 struct perf_event_header header;
2624 u64 time;
2625 u64 period;
2626 } freq_event = {
2627 .header = {
2628 .type = PERF_EVENT_PERIOD,
2629 .misc = 0,
2630 .size = sizeof(freq_event),
2631 },
2632 .time = sched_clock(),
2633 .period = period,
2634 };
2635
2636 if (counter->hw.irq_period == period)
2637 return;
2638
2639 ret = perf_output_begin(&handle, counter, sizeof(freq_event), 0, 0);
2640 if (ret)
2641 return;
2642
2643 perf_output_put(&handle, freq_event);
2644 perf_output_end(&handle);
2645}
2646
2647/*
Peter Zijlstraa78ac322009-05-25 17:39:05 +02002648 * IRQ throttle logging
2649 */
2650
2651static void perf_log_throttle(struct perf_counter *counter, int enable)
2652{
2653 struct perf_output_handle handle;
2654 int ret;
2655
2656 struct {
2657 struct perf_event_header header;
2658 u64 time;
2659 } throttle_event = {
2660 .header = {
2661 .type = PERF_EVENT_THROTTLE + 1,
2662 .misc = 0,
2663 .size = sizeof(throttle_event),
2664 },
2665 .time = sched_clock(),
2666 };
2667
Ingo Molnar0127c3e2009-05-25 22:03:26 +02002668 ret = perf_output_begin(&handle, counter, sizeof(throttle_event), 1, 0);
Peter Zijlstraa78ac322009-05-25 17:39:05 +02002669 if (ret)
2670 return;
2671
2672 perf_output_put(&handle, throttle_event);
2673 perf_output_end(&handle);
2674}
2675
2676/*
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002677 * Generic counter overflow handling.
2678 */
2679
2680int perf_counter_overflow(struct perf_counter *counter,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002681 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002682{
Peter Zijlstra79f14642009-04-06 11:45:07 +02002683 int events = atomic_read(&counter->event_limit);
Peter Zijlstraa78ac322009-05-25 17:39:05 +02002684 int throttle = counter->pmu->unthrottle != NULL;
Peter Zijlstra79f14642009-04-06 11:45:07 +02002685 int ret = 0;
2686
Peter Zijlstraa78ac322009-05-25 17:39:05 +02002687 if (!throttle) {
2688 counter->hw.interrupts++;
2689 } else if (counter->hw.interrupts != MAX_INTERRUPTS) {
2690 counter->hw.interrupts++;
2691 if (HZ*counter->hw.interrupts > (u64)sysctl_perf_counter_limit) {
2692 counter->hw.interrupts = MAX_INTERRUPTS;
2693 perf_log_throttle(counter, 0);
2694 ret = 1;
2695 }
2696 }
Peter Zijlstra60db5e02009-05-15 15:19:28 +02002697
Peter Zijlstra2023b352009-05-05 17:50:26 +02002698 /*
2699 * XXX event_limit might not quite work as expected on inherited
2700 * counters
2701 */
2702
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002703 counter->pending_kill = POLL_IN;
Peter Zijlstra79f14642009-04-06 11:45:07 +02002704 if (events && atomic_dec_and_test(&counter->event_limit)) {
2705 ret = 1;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002706 counter->pending_kill = POLL_HUP;
Peter Zijlstra79f14642009-04-06 11:45:07 +02002707 if (nmi) {
2708 counter->pending_disable = 1;
2709 perf_pending_queue(&counter->pending,
2710 perf_pending_counter);
2711 } else
2712 perf_counter_disable(counter);
2713 }
2714
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002715 perf_counter_output(counter, nmi, regs, addr);
Peter Zijlstra79f14642009-04-06 11:45:07 +02002716 return ret;
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002717}
2718
2719/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002720 * Generic software counter infrastructure
2721 */
2722
2723static void perf_swcounter_update(struct perf_counter *counter)
2724{
2725 struct hw_perf_counter *hwc = &counter->hw;
2726 u64 prev, now;
2727 s64 delta;
2728
2729again:
2730 prev = atomic64_read(&hwc->prev_count);
2731 now = atomic64_read(&hwc->count);
2732 if (atomic64_cmpxchg(&hwc->prev_count, prev, now) != prev)
2733 goto again;
2734
2735 delta = now - prev;
2736
2737 atomic64_add(delta, &counter->count);
2738 atomic64_sub(delta, &hwc->period_left);
2739}
2740
2741static void perf_swcounter_set_period(struct perf_counter *counter)
2742{
2743 struct hw_perf_counter *hwc = &counter->hw;
2744 s64 left = atomic64_read(&hwc->period_left);
2745 s64 period = hwc->irq_period;
2746
2747 if (unlikely(left <= -period)) {
2748 left = period;
2749 atomic64_set(&hwc->period_left, left);
2750 }
2751
2752 if (unlikely(left <= 0)) {
2753 left += period;
2754 atomic64_add(period, &hwc->period_left);
2755 }
2756
2757 atomic64_set(&hwc->prev_count, -left);
2758 atomic64_set(&hwc->count, -left);
2759}
2760
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002761static enum hrtimer_restart perf_swcounter_hrtimer(struct hrtimer *hrtimer)
2762{
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002763 enum hrtimer_restart ret = HRTIMER_RESTART;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002764 struct perf_counter *counter;
2765 struct pt_regs *regs;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02002766 u64 period;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002767
2768 counter = container_of(hrtimer, struct perf_counter, hw.hrtimer);
Robert Richter4aeb0b42009-04-29 12:47:03 +02002769 counter->pmu->read(counter);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002770
2771 regs = get_irq_regs();
2772 /*
2773 * In case we exclude kernel IPs or are somehow not in interrupt
2774 * context, provide the next best thing, the user IP.
2775 */
2776 if ((counter->hw_event.exclude_kernel || !regs) &&
2777 !counter->hw_event.exclude_user)
2778 regs = task_pt_regs(current);
2779
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002780 if (regs) {
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002781 if (perf_counter_overflow(counter, 0, regs, 0))
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002782 ret = HRTIMER_NORESTART;
2783 }
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002784
Peter Zijlstra60db5e02009-05-15 15:19:28 +02002785 period = max_t(u64, 10000, counter->hw.irq_period);
2786 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002787
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002788 return ret;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002789}
2790
2791static void perf_swcounter_overflow(struct perf_counter *counter,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002792 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002793{
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002794 perf_swcounter_update(counter);
2795 perf_swcounter_set_period(counter);
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002796 if (perf_counter_overflow(counter, nmi, regs, addr))
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002797 /* soft-disable the counter */
2798 ;
2799
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002800}
2801
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002802static int perf_swcounter_match(struct perf_counter *counter,
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002803 enum perf_event_types type,
2804 u32 event, struct pt_regs *regs)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002805{
2806 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
2807 return 0;
2808
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002809 if (perf_event_raw(&counter->hw_event))
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002810 return 0;
2811
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002812 if (perf_event_type(&counter->hw_event) != type)
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002813 return 0;
2814
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002815 if (perf_event_id(&counter->hw_event) != event)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002816 return 0;
2817
2818 if (counter->hw_event.exclude_user && user_mode(regs))
2819 return 0;
2820
2821 if (counter->hw_event.exclude_kernel && !user_mode(regs))
2822 return 0;
2823
2824 return 1;
2825}
2826
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002827static void perf_swcounter_add(struct perf_counter *counter, u64 nr,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002828 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002829{
2830 int neg = atomic64_add_negative(nr, &counter->hw.count);
2831 if (counter->hw.irq_period && !neg)
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002832 perf_swcounter_overflow(counter, nmi, regs, addr);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002833}
2834
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002835static void perf_swcounter_ctx_event(struct perf_counter_context *ctx,
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002836 enum perf_event_types type, u32 event,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002837 u64 nr, int nmi, struct pt_regs *regs,
2838 u64 addr)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002839{
2840 struct perf_counter *counter;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002841
Peter Zijlstra01ef09d2009-03-19 20:26:11 +01002842 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002843 return;
2844
Peter Zijlstra592903c2009-03-13 12:21:36 +01002845 rcu_read_lock();
2846 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002847 if (perf_swcounter_match(counter, type, event, regs))
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002848 perf_swcounter_add(counter, nr, nmi, regs, addr);
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002849 }
Peter Zijlstra592903c2009-03-13 12:21:36 +01002850 rcu_read_unlock();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002851}
2852
Peter Zijlstra96f6d442009-03-23 18:22:07 +01002853static int *perf_swcounter_recursion_context(struct perf_cpu_context *cpuctx)
2854{
2855 if (in_nmi())
2856 return &cpuctx->recursion[3];
2857
2858 if (in_irq())
2859 return &cpuctx->recursion[2];
2860
2861 if (in_softirq())
2862 return &cpuctx->recursion[1];
2863
2864 return &cpuctx->recursion[0];
2865}
2866
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002867static void __perf_swcounter_event(enum perf_event_types type, u32 event,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002868 u64 nr, int nmi, struct pt_regs *regs,
2869 u64 addr)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002870{
2871 struct perf_cpu_context *cpuctx = &get_cpu_var(perf_cpu_context);
Peter Zijlstra96f6d442009-03-23 18:22:07 +01002872 int *recursion = perf_swcounter_recursion_context(cpuctx);
2873
2874 if (*recursion)
2875 goto out;
2876
2877 (*recursion)++;
2878 barrier();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002879
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002880 perf_swcounter_ctx_event(&cpuctx->ctx, type, event,
2881 nr, nmi, regs, addr);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002882 if (cpuctx->task_ctx) {
2883 perf_swcounter_ctx_event(cpuctx->task_ctx, type, event,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002884 nr, nmi, regs, addr);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002885 }
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002886
Peter Zijlstra96f6d442009-03-23 18:22:07 +01002887 barrier();
2888 (*recursion)--;
2889
2890out:
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002891 put_cpu_var(perf_cpu_context);
2892}
2893
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002894void
2895perf_swcounter_event(u32 event, u64 nr, int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002896{
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002897 __perf_swcounter_event(PERF_TYPE_SOFTWARE, event, nr, nmi, regs, addr);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002898}
2899
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002900static void perf_swcounter_read(struct perf_counter *counter)
2901{
2902 perf_swcounter_update(counter);
2903}
2904
2905static int perf_swcounter_enable(struct perf_counter *counter)
2906{
2907 perf_swcounter_set_period(counter);
2908 return 0;
2909}
2910
2911static void perf_swcounter_disable(struct perf_counter *counter)
2912{
2913 perf_swcounter_update(counter);
2914}
2915
Robert Richter4aeb0b42009-04-29 12:47:03 +02002916static const struct pmu perf_ops_generic = {
Peter Zijlstraac17dc82009-03-13 12:21:34 +01002917 .enable = perf_swcounter_enable,
2918 .disable = perf_swcounter_disable,
2919 .read = perf_swcounter_read,
2920};
2921
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002922/*
2923 * Software counter: cpu wall time clock
2924 */
2925
Paul Mackerras9abf8a02009-01-09 16:26:43 +11002926static void cpu_clock_perf_counter_update(struct perf_counter *counter)
2927{
2928 int cpu = raw_smp_processor_id();
2929 s64 prev;
2930 u64 now;
2931
2932 now = cpu_clock(cpu);
2933 prev = atomic64_read(&counter->hw.prev_count);
2934 atomic64_set(&counter->hw.prev_count, now);
2935 atomic64_add(now - prev, &counter->count);
2936}
2937
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002938static int cpu_clock_perf_counter_enable(struct perf_counter *counter)
2939{
2940 struct hw_perf_counter *hwc = &counter->hw;
2941 int cpu = raw_smp_processor_id();
2942
2943 atomic64_set(&hwc->prev_count, cpu_clock(cpu));
Peter Zijlstra039fc912009-03-13 16:43:47 +01002944 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2945 hwc->hrtimer.function = perf_swcounter_hrtimer;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002946 if (hwc->irq_period) {
Peter Zijlstra60db5e02009-05-15 15:19:28 +02002947 u64 period = max_t(u64, 10000, hwc->irq_period);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002948 __hrtimer_start_range_ns(&hwc->hrtimer,
Peter Zijlstra60db5e02009-05-15 15:19:28 +02002949 ns_to_ktime(period), 0,
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002950 HRTIMER_MODE_REL, 0);
2951 }
2952
2953 return 0;
2954}
2955
Ingo Molnar5c92d122008-12-11 13:21:10 +01002956static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
2957{
Peter Zijlstrab986d7e2009-05-20 12:21:21 +02002958 if (counter->hw.irq_period)
2959 hrtimer_cancel(&counter->hw.hrtimer);
Paul Mackerras9abf8a02009-01-09 16:26:43 +11002960 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01002961}
2962
2963static void cpu_clock_perf_counter_read(struct perf_counter *counter)
2964{
Paul Mackerras9abf8a02009-01-09 16:26:43 +11002965 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01002966}
2967
Robert Richter4aeb0b42009-04-29 12:47:03 +02002968static const struct pmu perf_ops_cpu_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +01002969 .enable = cpu_clock_perf_counter_enable,
2970 .disable = cpu_clock_perf_counter_disable,
2971 .read = cpu_clock_perf_counter_read,
Ingo Molnar5c92d122008-12-11 13:21:10 +01002972};
2973
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01002974/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002975 * Software counter: task time clock
2976 */
2977
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02002978static void task_clock_perf_counter_update(struct perf_counter *counter, u64 now)
Ingo Molnarbae43c92008-12-11 14:03:20 +01002979{
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02002980 u64 prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002981 s64 delta;
Ingo Molnarbae43c92008-12-11 14:03:20 +01002982
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02002983 prev = atomic64_xchg(&counter->hw.prev_count, now);
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002984 delta = now - prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002985 atomic64_add(delta, &counter->count);
Ingo Molnarbae43c92008-12-11 14:03:20 +01002986}
2987
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01002988static int task_clock_perf_counter_enable(struct perf_counter *counter)
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002989{
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002990 struct hw_perf_counter *hwc = &counter->hw;
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02002991 u64 now;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002992
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02002993 now = counter->ctx->time;
2994
2995 atomic64_set(&hwc->prev_count, now);
Peter Zijlstra039fc912009-03-13 16:43:47 +01002996 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2997 hwc->hrtimer.function = perf_swcounter_hrtimer;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002998 if (hwc->irq_period) {
Peter Zijlstra60db5e02009-05-15 15:19:28 +02002999 u64 period = max_t(u64, 10000, hwc->irq_period);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003000 __hrtimer_start_range_ns(&hwc->hrtimer,
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003001 ns_to_ktime(period), 0,
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003002 HRTIMER_MODE_REL, 0);
3003 }
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01003004
3005 return 0;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003006}
3007
3008static void task_clock_perf_counter_disable(struct perf_counter *counter)
3009{
Peter Zijlstrab986d7e2009-05-20 12:21:21 +02003010 if (counter->hw.irq_period)
3011 hrtimer_cancel(&counter->hw.hrtimer);
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02003012 task_clock_perf_counter_update(counter, counter->ctx->time);
3013
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003014}
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01003015
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003016static void task_clock_perf_counter_read(struct perf_counter *counter)
3017{
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02003018 u64 time;
3019
3020 if (!in_nmi()) {
3021 update_context_time(counter->ctx);
3022 time = counter->ctx->time;
3023 } else {
3024 u64 now = perf_clock();
3025 u64 delta = now - counter->ctx->timestamp;
3026 time = counter->ctx->time + delta;
3027 }
3028
3029 task_clock_perf_counter_update(counter, time);
Ingo Molnarbae43c92008-12-11 14:03:20 +01003030}
3031
Robert Richter4aeb0b42009-04-29 12:47:03 +02003032static const struct pmu perf_ops_task_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +01003033 .enable = task_clock_perf_counter_enable,
3034 .disable = task_clock_perf_counter_disable,
3035 .read = task_clock_perf_counter_read,
Ingo Molnarbae43c92008-12-11 14:03:20 +01003036};
3037
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003038/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003039 * Software counter: cpu migrations
3040 */
3041
Paul Mackerras23a185c2009-02-09 22:42:47 +11003042static inline u64 get_cpu_migrations(struct perf_counter *counter)
Ingo Molnar6c594c22008-12-14 12:34:15 +01003043{
Paul Mackerras23a185c2009-02-09 22:42:47 +11003044 struct task_struct *curr = counter->ctx->task;
3045
3046 if (curr)
3047 return curr->se.nr_migrations;
3048 return cpu_nr_migrations(smp_processor_id());
Ingo Molnar6c594c22008-12-14 12:34:15 +01003049}
3050
3051static void cpu_migrations_perf_counter_update(struct perf_counter *counter)
3052{
3053 u64 prev, now;
3054 s64 delta;
3055
3056 prev = atomic64_read(&counter->hw.prev_count);
Paul Mackerras23a185c2009-02-09 22:42:47 +11003057 now = get_cpu_migrations(counter);
Ingo Molnar6c594c22008-12-14 12:34:15 +01003058
3059 atomic64_set(&counter->hw.prev_count, now);
3060
3061 delta = now - prev;
Ingo Molnar6c594c22008-12-14 12:34:15 +01003062
3063 atomic64_add(delta, &counter->count);
3064}
3065
3066static void cpu_migrations_perf_counter_read(struct perf_counter *counter)
3067{
3068 cpu_migrations_perf_counter_update(counter);
3069}
3070
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01003071static int cpu_migrations_perf_counter_enable(struct perf_counter *counter)
Ingo Molnar6c594c22008-12-14 12:34:15 +01003072{
Paul Mackerrasc07c99b2009-02-13 22:10:34 +11003073 if (counter->prev_state <= PERF_COUNTER_STATE_OFF)
3074 atomic64_set(&counter->hw.prev_count,
3075 get_cpu_migrations(counter));
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01003076 return 0;
Ingo Molnar6c594c22008-12-14 12:34:15 +01003077}
3078
3079static void cpu_migrations_perf_counter_disable(struct perf_counter *counter)
3080{
3081 cpu_migrations_perf_counter_update(counter);
3082}
3083
Robert Richter4aeb0b42009-04-29 12:47:03 +02003084static const struct pmu perf_ops_cpu_migrations = {
Ingo Molnar76715812008-12-17 14:20:28 +01003085 .enable = cpu_migrations_perf_counter_enable,
3086 .disable = cpu_migrations_perf_counter_disable,
3087 .read = cpu_migrations_perf_counter_read,
Ingo Molnar6c594c22008-12-14 12:34:15 +01003088};
3089
Peter Zijlstrae077df42009-03-19 20:26:17 +01003090#ifdef CONFIG_EVENT_PROFILE
3091void perf_tpcounter_event(int event_id)
3092{
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003093 struct pt_regs *regs = get_irq_regs();
3094
3095 if (!regs)
3096 regs = task_pt_regs(current);
3097
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003098 __perf_swcounter_event(PERF_TYPE_TRACEPOINT, event_id, 1, 1, regs, 0);
Peter Zijlstrae077df42009-03-19 20:26:17 +01003099}
Steven Whitehouseff7b1b42009-04-15 16:55:05 +01003100EXPORT_SYMBOL_GPL(perf_tpcounter_event);
Peter Zijlstrae077df42009-03-19 20:26:17 +01003101
3102extern int ftrace_profile_enable(int);
3103extern void ftrace_profile_disable(int);
3104
3105static void tp_perf_counter_destroy(struct perf_counter *counter)
3106{
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01003107 ftrace_profile_disable(perf_event_id(&counter->hw_event));
Peter Zijlstrae077df42009-03-19 20:26:17 +01003108}
3109
Robert Richter4aeb0b42009-04-29 12:47:03 +02003110static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
Peter Zijlstrae077df42009-03-19 20:26:17 +01003111{
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01003112 int event_id = perf_event_id(&counter->hw_event);
Peter Zijlstrae077df42009-03-19 20:26:17 +01003113 int ret;
3114
3115 ret = ftrace_profile_enable(event_id);
3116 if (ret)
3117 return NULL;
3118
3119 counter->destroy = tp_perf_counter_destroy;
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003120 counter->hw.irq_period = counter->hw_event.irq_period;
Peter Zijlstrae077df42009-03-19 20:26:17 +01003121
3122 return &perf_ops_generic;
3123}
3124#else
Robert Richter4aeb0b42009-04-29 12:47:03 +02003125static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
Peter Zijlstrae077df42009-03-19 20:26:17 +01003126{
3127 return NULL;
3128}
3129#endif
3130
Robert Richter4aeb0b42009-04-29 12:47:03 +02003131static const struct pmu *sw_perf_counter_init(struct perf_counter *counter)
Ingo Molnar5c92d122008-12-11 13:21:10 +01003132{
Robert Richter4aeb0b42009-04-29 12:47:03 +02003133 const struct pmu *pmu = NULL;
Ingo Molnar5c92d122008-12-11 13:21:10 +01003134
Paul Mackerras0475f9e2009-02-11 14:35:35 +11003135 /*
3136 * Software counters (currently) can't in general distinguish
3137 * between user, kernel and hypervisor events.
3138 * However, context switches and cpu migrations are considered
3139 * to be kernel events, and page faults are never hypervisor
3140 * events.
3141 */
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01003142 switch (perf_event_id(&counter->hw_event)) {
Ingo Molnar5c92d122008-12-11 13:21:10 +01003143 case PERF_COUNT_CPU_CLOCK:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003144 pmu = &perf_ops_cpu_clock;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003145
Ingo Molnar5c92d122008-12-11 13:21:10 +01003146 break;
Ingo Molnarbae43c92008-12-11 14:03:20 +01003147 case PERF_COUNT_TASK_CLOCK:
Paul Mackerras23a185c2009-02-09 22:42:47 +11003148 /*
3149 * If the user instantiates this as a per-cpu counter,
3150 * use the cpu_clock counter instead.
3151 */
3152 if (counter->ctx->task)
Robert Richter4aeb0b42009-04-29 12:47:03 +02003153 pmu = &perf_ops_task_clock;
Paul Mackerras23a185c2009-02-09 22:42:47 +11003154 else
Robert Richter4aeb0b42009-04-29 12:47:03 +02003155 pmu = &perf_ops_cpu_clock;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003156
Ingo Molnarbae43c92008-12-11 14:03:20 +01003157 break;
Ingo Molnare06c61a2008-12-14 14:44:31 +01003158 case PERF_COUNT_PAGE_FAULTS:
Peter Zijlstraac17dc82009-03-13 12:21:34 +01003159 case PERF_COUNT_PAGE_FAULTS_MIN:
3160 case PERF_COUNT_PAGE_FAULTS_MAJ:
Ingo Molnar5d6a27d2008-12-14 12:28:33 +01003161 case PERF_COUNT_CONTEXT_SWITCHES:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003162 pmu = &perf_ops_generic;
Ingo Molnar5d6a27d2008-12-14 12:28:33 +01003163 break;
Ingo Molnar6c594c22008-12-14 12:34:15 +01003164 case PERF_COUNT_CPU_MIGRATIONS:
Paul Mackerras0475f9e2009-02-11 14:35:35 +11003165 if (!counter->hw_event.exclude_kernel)
Robert Richter4aeb0b42009-04-29 12:47:03 +02003166 pmu = &perf_ops_cpu_migrations;
Ingo Molnar6c594c22008-12-14 12:34:15 +01003167 break;
Ingo Molnar5c92d122008-12-11 13:21:10 +01003168 }
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003169
Robert Richter4aeb0b42009-04-29 12:47:03 +02003170 return pmu;
Ingo Molnar5c92d122008-12-11 13:21:10 +01003171}
3172
Thomas Gleixner0793a612008-12-04 20:12:29 +01003173/*
3174 * Allocate and initialize a counter structure
3175 */
3176static struct perf_counter *
Ingo Molnar04289bb2008-12-11 08:38:42 +01003177perf_counter_alloc(struct perf_counter_hw_event *hw_event,
3178 int cpu,
Paul Mackerras23a185c2009-02-09 22:42:47 +11003179 struct perf_counter_context *ctx,
Ingo Molnar9b51f662008-12-12 13:49:45 +01003180 struct perf_counter *group_leader,
3181 gfp_t gfpflags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003182{
Robert Richter4aeb0b42009-04-29 12:47:03 +02003183 const struct pmu *pmu;
Ingo Molnar621a01e2008-12-11 12:46:46 +01003184 struct perf_counter *counter;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003185 struct hw_perf_counter *hwc;
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003186 long err;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003187
Ingo Molnar9b51f662008-12-12 13:49:45 +01003188 counter = kzalloc(sizeof(*counter), gfpflags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003189 if (!counter)
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003190 return ERR_PTR(-ENOMEM);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003191
Ingo Molnar04289bb2008-12-11 08:38:42 +01003192 /*
3193 * Single counters are their own group leaders, with an
3194 * empty sibling list:
3195 */
3196 if (!group_leader)
3197 group_leader = counter;
3198
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003199 mutex_init(&counter->child_mutex);
3200 INIT_LIST_HEAD(&counter->child_list);
3201
Ingo Molnar04289bb2008-12-11 08:38:42 +01003202 INIT_LIST_HEAD(&counter->list_entry);
Peter Zijlstra592903c2009-03-13 12:21:36 +01003203 INIT_LIST_HEAD(&counter->event_entry);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003204 INIT_LIST_HEAD(&counter->sibling_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003205 init_waitqueue_head(&counter->waitq);
3206
Peter Zijlstra7b732a72009-03-23 18:22:10 +01003207 mutex_init(&counter->mmap_mutex);
3208
Ingo Molnar9f66a382008-12-10 12:33:23 +01003209 counter->cpu = cpu;
3210 counter->hw_event = *hw_event;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003211 counter->group_leader = group_leader;
Robert Richter4aeb0b42009-04-29 12:47:03 +02003212 counter->pmu = NULL;
Paul Mackerras23a185c2009-02-09 22:42:47 +11003213 counter->ctx = ctx;
Ingo Molnar329d8762009-05-26 08:10:00 +02003214 counter->oncpu = -1;
3215
Ingo Molnar235c7fc2008-12-21 14:43:25 +01003216 counter->state = PERF_COUNTER_STATE_INACTIVE;
Ingo Molnara86ed502008-12-17 00:43:10 +01003217 if (hw_event->disabled)
3218 counter->state = PERF_COUNTER_STATE_OFF;
3219
Robert Richter4aeb0b42009-04-29 12:47:03 +02003220 pmu = NULL;
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003221
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003222 hwc = &counter->hw;
3223 if (hw_event->freq && hw_event->irq_freq)
Peter Zijlstra2e569d32009-05-15 15:37:47 +02003224 hwc->irq_period = div64_u64(TICK_NSEC, hw_event->irq_freq);
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003225 else
3226 hwc->irq_period = hw_event->irq_period;
3227
Peter Zijlstra2023b352009-05-05 17:50:26 +02003228 /*
3229 * we currently do not support PERF_RECORD_GROUP on inherited counters
3230 */
3231 if (hw_event->inherit && (hw_event->record_type & PERF_RECORD_GROUP))
3232 goto done;
3233
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01003234 if (perf_event_raw(hw_event)) {
Robert Richter4aeb0b42009-04-29 12:47:03 +02003235 pmu = hw_perf_counter_init(counter);
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01003236 goto done;
3237 }
3238
3239 switch (perf_event_type(hw_event)) {
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003240 case PERF_TYPE_HARDWARE:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003241 pmu = hw_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003242 break;
3243
3244 case PERF_TYPE_SOFTWARE:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003245 pmu = sw_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003246 break;
3247
3248 case PERF_TYPE_TRACEPOINT:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003249 pmu = tp_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003250 break;
3251 }
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01003252done:
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003253 err = 0;
Robert Richter4aeb0b42009-04-29 12:47:03 +02003254 if (!pmu)
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003255 err = -EINVAL;
Robert Richter4aeb0b42009-04-29 12:47:03 +02003256 else if (IS_ERR(pmu))
3257 err = PTR_ERR(pmu);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003258
3259 if (err) {
3260 kfree(counter);
3261 return ERR_PTR(err);
3262 }
3263
Robert Richter4aeb0b42009-04-29 12:47:03 +02003264 counter->pmu = pmu;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003265
Peter Zijlstra7fc23a52009-05-08 18:52:21 +02003266 atomic_inc(&nr_counters);
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02003267 if (counter->hw_event.mmap)
3268 atomic_inc(&nr_mmap_tracking);
3269 if (counter->hw_event.munmap)
3270 atomic_inc(&nr_munmap_tracking);
3271 if (counter->hw_event.comm)
3272 atomic_inc(&nr_comm_tracking);
3273
Thomas Gleixner0793a612008-12-04 20:12:29 +01003274 return counter;
3275}
3276
3277/**
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003278 * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
Ingo Molnar9f66a382008-12-10 12:33:23 +01003279 *
3280 * @hw_event_uptr: event type attributes for monitoring/sampling
Thomas Gleixner0793a612008-12-04 20:12:29 +01003281 * @pid: target pid
Ingo Molnar9f66a382008-12-10 12:33:23 +01003282 * @cpu: target cpu
3283 * @group_fd: group leader counter fd
Thomas Gleixner0793a612008-12-04 20:12:29 +01003284 */
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003285SYSCALL_DEFINE5(perf_counter_open,
Paul Mackerrasf3dfd262009-02-26 22:43:46 +11003286 const struct perf_counter_hw_event __user *, hw_event_uptr,
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003287 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003288{
Ingo Molnar04289bb2008-12-11 08:38:42 +01003289 struct perf_counter *counter, *group_leader;
Ingo Molnar9f66a382008-12-10 12:33:23 +01003290 struct perf_counter_hw_event hw_event;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003291 struct perf_counter_context *ctx;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003292 struct file *counter_file = NULL;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003293 struct file *group_file = NULL;
3294 int fput_needed = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003295 int fput_needed2 = 0;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003296 int ret;
3297
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003298 /* for future expandability... */
3299 if (flags)
3300 return -EINVAL;
3301
Ingo Molnar9f66a382008-12-10 12:33:23 +01003302 if (copy_from_user(&hw_event, hw_event_uptr, sizeof(hw_event)) != 0)
Thomas Gleixnereab656a2008-12-08 19:26:59 +01003303 return -EFAULT;
3304
Ingo Molnar04289bb2008-12-11 08:38:42 +01003305 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01003306 * Get the target context (task or percpu):
3307 */
3308 ctx = find_get_context(pid, cpu);
3309 if (IS_ERR(ctx))
3310 return PTR_ERR(ctx);
3311
3312 /*
3313 * Look up the group leader (we will attach this counter to it):
Ingo Molnar04289bb2008-12-11 08:38:42 +01003314 */
3315 group_leader = NULL;
3316 if (group_fd != -1) {
3317 ret = -EINVAL;
3318 group_file = fget_light(group_fd, &fput_needed);
3319 if (!group_file)
Ingo Molnarccff2862008-12-11 11:26:29 +01003320 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003321 if (group_file->f_op != &perf_fops)
Ingo Molnarccff2862008-12-11 11:26:29 +01003322 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003323
3324 group_leader = group_file->private_data;
3325 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01003326 * Do not allow a recursive hierarchy (this new sibling
3327 * becoming part of another group-sibling):
Ingo Molnar04289bb2008-12-11 08:38:42 +01003328 */
Ingo Molnarccff2862008-12-11 11:26:29 +01003329 if (group_leader->group_leader != group_leader)
3330 goto err_put_context;
3331 /*
3332 * Do not allow to attach to a group in a different
3333 * task or CPU context:
3334 */
3335 if (group_leader->ctx != ctx)
3336 goto err_put_context;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11003337 /*
3338 * Only a group leader can be exclusive or pinned
3339 */
3340 if (hw_event.exclusive || hw_event.pinned)
3341 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003342 }
3343
Paul Mackerras23a185c2009-02-09 22:42:47 +11003344 counter = perf_counter_alloc(&hw_event, cpu, ctx, group_leader,
3345 GFP_KERNEL);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003346 ret = PTR_ERR(counter);
3347 if (IS_ERR(counter))
Thomas Gleixner0793a612008-12-04 20:12:29 +01003348 goto err_put_context;
3349
Thomas Gleixner0793a612008-12-04 20:12:29 +01003350 ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
3351 if (ret < 0)
Ingo Molnar9b51f662008-12-12 13:49:45 +01003352 goto err_free_put_context;
3353
3354 counter_file = fget_light(ret, &fput_needed2);
3355 if (!counter_file)
3356 goto err_free_put_context;
3357
3358 counter->filp = counter_file;
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003359 WARN_ON_ONCE(ctx->parent_ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003360 mutex_lock(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003361 perf_install_in_context(ctx, counter, cpu);
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003362 ++ctx->generation;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003363 mutex_unlock(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003364
Peter Zijlstra082ff5a2009-05-23 18:29:00 +02003365 counter->owner = current;
3366 get_task_struct(current);
3367 mutex_lock(&current->perf_counter_mutex);
3368 list_add_tail(&counter->owner_entry, &current->perf_counter_list);
3369 mutex_unlock(&current->perf_counter_mutex);
3370
Ingo Molnar9b51f662008-12-12 13:49:45 +01003371 fput_light(counter_file, fput_needed2);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003372
Ingo Molnar04289bb2008-12-11 08:38:42 +01003373out_fput:
3374 fput_light(group_file, fput_needed);
3375
Thomas Gleixner0793a612008-12-04 20:12:29 +01003376 return ret;
3377
Ingo Molnar9b51f662008-12-12 13:49:45 +01003378err_free_put_context:
Thomas Gleixner0793a612008-12-04 20:12:29 +01003379 kfree(counter);
3380
3381err_put_context:
Paul Mackerrasc93f7662009-05-28 22:18:17 +10003382 put_ctx(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003383
Ingo Molnar04289bb2008-12-11 08:38:42 +01003384 goto out_fput;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003385}
3386
Ingo Molnar9b51f662008-12-12 13:49:45 +01003387/*
Ingo Molnar9b51f662008-12-12 13:49:45 +01003388 * inherit a counter from parent task to child task:
3389 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11003390static struct perf_counter *
Ingo Molnar9b51f662008-12-12 13:49:45 +01003391inherit_counter(struct perf_counter *parent_counter,
3392 struct task_struct *parent,
3393 struct perf_counter_context *parent_ctx,
3394 struct task_struct *child,
Paul Mackerrasd859e292009-01-17 18:10:22 +11003395 struct perf_counter *group_leader,
Ingo Molnar9b51f662008-12-12 13:49:45 +01003396 struct perf_counter_context *child_ctx)
3397{
3398 struct perf_counter *child_counter;
3399
Paul Mackerrasd859e292009-01-17 18:10:22 +11003400 /*
3401 * Instead of creating recursive hierarchies of counters,
3402 * we link inherited counters back to the original parent,
3403 * which has a filp for sure, which we use as the reference
3404 * count:
3405 */
3406 if (parent_counter->parent)
3407 parent_counter = parent_counter->parent;
3408
Ingo Molnar9b51f662008-12-12 13:49:45 +01003409 child_counter = perf_counter_alloc(&parent_counter->hw_event,
Paul Mackerras23a185c2009-02-09 22:42:47 +11003410 parent_counter->cpu, child_ctx,
3411 group_leader, GFP_KERNEL);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003412 if (IS_ERR(child_counter))
3413 return child_counter;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10003414 get_ctx(child_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003415
3416 /*
Paul Mackerras564c2b22009-05-22 14:27:22 +10003417 * Make the child state follow the state of the parent counter,
3418 * not its hw_event.disabled bit. We hold the parent's mutex,
3419 * so we won't race with perf_counter_{en,dis}able_family.
3420 */
3421 if (parent_counter->state >= PERF_COUNTER_STATE_INACTIVE)
3422 child_counter->state = PERF_COUNTER_STATE_INACTIVE;
3423 else
3424 child_counter->state = PERF_COUNTER_STATE_OFF;
3425
3426 /*
Ingo Molnar9b51f662008-12-12 13:49:45 +01003427 * Link it up in the child's context:
3428 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +11003429 add_counter_to_ctx(child_counter, child_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003430
3431 child_counter->parent = parent_counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003432 /*
3433 * inherit into child's child as well:
3434 */
3435 child_counter->hw_event.inherit = 1;
3436
3437 /*
3438 * Get a reference to the parent filp - we will fput it
3439 * when the child counter exits. This is safe to do because
3440 * we are in the parent and we know that the filp still
3441 * exists and has a nonzero count:
3442 */
3443 atomic_long_inc(&parent_counter->filp->f_count);
3444
Paul Mackerrasd859e292009-01-17 18:10:22 +11003445 /*
3446 * Link this into the parent counter's child list
3447 */
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003448 WARN_ON_ONCE(parent_counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003449 mutex_lock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003450 list_add_tail(&child_counter->child_list, &parent_counter->child_list);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003451 mutex_unlock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003452
3453 return child_counter;
3454}
3455
3456static int inherit_group(struct perf_counter *parent_counter,
3457 struct task_struct *parent,
3458 struct perf_counter_context *parent_ctx,
3459 struct task_struct *child,
3460 struct perf_counter_context *child_ctx)
3461{
3462 struct perf_counter *leader;
3463 struct perf_counter *sub;
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003464 struct perf_counter *child_ctr;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003465
3466 leader = inherit_counter(parent_counter, parent, parent_ctx,
3467 child, NULL, child_ctx);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003468 if (IS_ERR(leader))
3469 return PTR_ERR(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003470 list_for_each_entry(sub, &parent_counter->sibling_list, list_entry) {
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003471 child_ctr = inherit_counter(sub, parent, parent_ctx,
3472 child, leader, child_ctx);
3473 if (IS_ERR(child_ctr))
3474 return PTR_ERR(child_ctr);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003475 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01003476 return 0;
3477}
3478
Paul Mackerrasd859e292009-01-17 18:10:22 +11003479static void sync_child_counter(struct perf_counter *child_counter,
3480 struct perf_counter *parent_counter)
3481{
Peter Zijlstra8bc20952009-05-15 20:45:59 +02003482 u64 child_val;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003483
Paul Mackerrasd859e292009-01-17 18:10:22 +11003484 child_val = atomic64_read(&child_counter->count);
3485
3486 /*
3487 * Add back the child's count to the parent's count:
3488 */
3489 atomic64_add(child_val, &parent_counter->count);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11003490 atomic64_add(child_counter->total_time_enabled,
3491 &parent_counter->child_total_time_enabled);
3492 atomic64_add(child_counter->total_time_running,
3493 &parent_counter->child_total_time_running);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003494
3495 /*
3496 * Remove this counter from the parent's list
3497 */
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003498 WARN_ON_ONCE(parent_counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003499 mutex_lock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003500 list_del_init(&child_counter->child_list);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003501 mutex_unlock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003502
3503 /*
3504 * Release the parent counter, if this was the last
3505 * reference to it.
3506 */
3507 fput(parent_counter->filp);
3508}
3509
Ingo Molnar9b51f662008-12-12 13:49:45 +01003510static void
3511__perf_counter_exit_task(struct task_struct *child,
3512 struct perf_counter *child_counter,
3513 struct perf_counter_context *child_ctx)
3514{
3515 struct perf_counter *parent_counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003516
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003517 update_counter_times(child_counter);
Peter Zijlstraaa9c67f2009-05-23 18:28:59 +02003518 perf_counter_remove_from_context(child_counter);
Ingo Molnar0cc0c022008-12-14 23:20:36 +01003519
Ingo Molnar9b51f662008-12-12 13:49:45 +01003520 parent_counter = child_counter->parent;
3521 /*
3522 * It can happen that parent exits first, and has counters
3523 * that are still around due to the child reference. These
3524 * counters need to be zapped - but otherwise linger.
3525 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11003526 if (parent_counter) {
3527 sync_child_counter(child_counter, parent_counter);
Peter Zijlstraf1600952009-03-19 20:26:16 +01003528 free_counter(child_counter);
Paul Mackerras4bcf3492009-02-11 13:53:19 +01003529 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01003530}
3531
3532/*
Paul Mackerrasd859e292009-01-17 18:10:22 +11003533 * When a child task exits, feed back counter values to parent counters.
Ingo Molnar9b51f662008-12-12 13:49:45 +01003534 */
3535void perf_counter_exit_task(struct task_struct *child)
3536{
3537 struct perf_counter *child_counter, *tmp;
3538 struct perf_counter_context *child_ctx;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003539 unsigned long flags;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003540
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003541 if (likely(!child->perf_counter_ctxp))
Ingo Molnar9b51f662008-12-12 13:49:45 +01003542 return;
3543
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003544 local_irq_save(flags);
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003545 /*
3546 * We can't reschedule here because interrupts are disabled,
3547 * and either child is current or it is a task that can't be
3548 * scheduled, so we are now safe from rescheduling changing
3549 * our context.
3550 */
3551 child_ctx = child->perf_counter_ctxp;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003552 __perf_counter_task_sched_out(child_ctx);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10003553
3554 /*
3555 * Take the context lock here so that if find_get_context is
3556 * reading child->perf_counter_ctxp, we wait until it has
3557 * incremented the context's refcount before we do put_ctx below.
3558 */
3559 spin_lock(&child_ctx->lock);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003560 child->perf_counter_ctxp = NULL;
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003561 if (child_ctx->parent_ctx) {
3562 /*
3563 * This context is a clone; unclone it so it can't get
3564 * swapped to another process while we're removing all
3565 * the counters from it.
3566 */
3567 put_ctx(child_ctx->parent_ctx);
3568 child_ctx->parent_ctx = NULL;
3569 }
Paul Mackerrasc93f7662009-05-28 22:18:17 +10003570 spin_unlock(&child_ctx->lock);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003571 local_irq_restore(flags);
3572
3573 mutex_lock(&child_ctx->mutex);
3574
Peter Zijlstra8bc20952009-05-15 20:45:59 +02003575again:
Ingo Molnar9b51f662008-12-12 13:49:45 +01003576 list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list,
3577 list_entry)
3578 __perf_counter_exit_task(child, child_counter, child_ctx);
Peter Zijlstra8bc20952009-05-15 20:45:59 +02003579
3580 /*
3581 * If the last counter was a group counter, it will have appended all
3582 * its siblings to the list, but we obtained 'tmp' before that which
3583 * will still point to the list head terminating the iteration.
3584 */
3585 if (!list_empty(&child_ctx->counter_list))
3586 goto again;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003587
3588 mutex_unlock(&child_ctx->mutex);
3589
3590 put_ctx(child_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003591}
3592
3593/*
3594 * Initialize the perf_counter context in task_struct
3595 */
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02003596int perf_counter_init_task(struct task_struct *child)
Ingo Molnar9b51f662008-12-12 13:49:45 +01003597{
3598 struct perf_counter_context *child_ctx, *parent_ctx;
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003599 struct perf_counter_context *cloned_ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003600 struct perf_counter *counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003601 struct task_struct *parent = current;
Paul Mackerras564c2b22009-05-22 14:27:22 +10003602 int inherited_all = 1;
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003603 u64 cloned_gen;
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02003604 int ret = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003605
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003606 child->perf_counter_ctxp = NULL;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003607
Peter Zijlstra082ff5a2009-05-23 18:29:00 +02003608 mutex_init(&child->perf_counter_mutex);
3609 INIT_LIST_HEAD(&child->perf_counter_list);
3610
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003611 if (likely(!parent->perf_counter_ctxp))
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02003612 return 0;
3613
Ingo Molnar9b51f662008-12-12 13:49:45 +01003614 /*
3615 * This is executed from the parent task context, so inherit
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003616 * counters that have been marked for cloning.
3617 * First allocate and initialize a context for the child.
Ingo Molnar9b51f662008-12-12 13:49:45 +01003618 */
3619
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003620 child_ctx = kmalloc(sizeof(struct perf_counter_context), GFP_KERNEL);
3621 if (!child_ctx)
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02003622 return -ENOMEM;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003623
3624 __perf_counter_init_context(child_ctx, child);
3625 child->perf_counter_ctxp = child_ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10003626 get_task_struct(child);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003627
Ingo Molnar9b51f662008-12-12 13:49:45 +01003628 /*
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003629 * If the parent's context is a clone, temporarily set its
3630 * parent_gen to an impossible value (all 1s) so it won't get
3631 * swapped under us. The rcu_read_lock makes sure that
3632 * parent_ctx continues to exist even if it gets swapped to
3633 * another process and then freed while we are trying to get
3634 * its lock.
3635 */
3636 rcu_read_lock();
3637 retry:
3638 parent_ctx = rcu_dereference(parent->perf_counter_ctxp);
3639 /*
3640 * No need to check if parent_ctx != NULL here; since we saw
3641 * it non-NULL earlier, the only reason for it to become NULL
3642 * is if we exit, and since we're currently in the middle of
3643 * a fork we can't be exiting at the same time.
3644 */
3645 spin_lock_irq(&parent_ctx->lock);
3646 if (parent_ctx != rcu_dereference(parent->perf_counter_ctxp)) {
3647 spin_unlock_irq(&parent_ctx->lock);
3648 goto retry;
3649 }
3650 cloned_gen = parent_ctx->parent_gen;
3651 if (parent_ctx->parent_ctx)
3652 parent_ctx->parent_gen = ~0ull;
3653 spin_unlock_irq(&parent_ctx->lock);
3654 rcu_read_unlock();
3655
3656 /*
Ingo Molnar9b51f662008-12-12 13:49:45 +01003657 * Lock the parent list. No need to lock the child - not PID
3658 * hashed yet and not running, so nobody can access it.
3659 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11003660 mutex_lock(&parent_ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003661
3662 /*
3663 * We dont have to disable NMIs - we are only looking at
3664 * the list, not manipulating it:
3665 */
Peter Zijlstrad7b629a2009-05-20 12:21:19 +02003666 list_for_each_entry_rcu(counter, &parent_ctx->event_list, event_entry) {
3667 if (counter != counter->group_leader)
3668 continue;
3669
Paul Mackerras564c2b22009-05-22 14:27:22 +10003670 if (!counter->hw_event.inherit) {
3671 inherited_all = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003672 continue;
Paul Mackerras564c2b22009-05-22 14:27:22 +10003673 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01003674
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02003675 ret = inherit_group(counter, parent, parent_ctx,
3676 child, child_ctx);
3677 if (ret) {
Paul Mackerras564c2b22009-05-22 14:27:22 +10003678 inherited_all = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003679 break;
Paul Mackerras564c2b22009-05-22 14:27:22 +10003680 }
3681 }
3682
3683 if (inherited_all) {
3684 /*
3685 * Mark the child context as a clone of the parent
3686 * context, or of whatever the parent is a clone of.
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003687 * Note that if the parent is a clone, it could get
3688 * uncloned at any point, but that doesn't matter
3689 * because the list of counters and the generation
3690 * count can't have changed since we took the mutex.
Paul Mackerras564c2b22009-05-22 14:27:22 +10003691 */
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003692 cloned_ctx = rcu_dereference(parent_ctx->parent_ctx);
3693 if (cloned_ctx) {
3694 child_ctx->parent_ctx = cloned_ctx;
3695 child_ctx->parent_gen = cloned_gen;
Paul Mackerras564c2b22009-05-22 14:27:22 +10003696 } else {
3697 child_ctx->parent_ctx = parent_ctx;
3698 child_ctx->parent_gen = parent_ctx->generation;
3699 }
3700 get_ctx(child_ctx->parent_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003701 }
3702
Paul Mackerrasd859e292009-01-17 18:10:22 +11003703 mutex_unlock(&parent_ctx->mutex);
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02003704
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003705 /*
3706 * Restore the clone status of the parent.
3707 */
3708 if (parent_ctx->parent_ctx) {
3709 spin_lock_irq(&parent_ctx->lock);
3710 if (parent_ctx->parent_ctx)
3711 parent_ctx->parent_gen = cloned_gen;
3712 spin_unlock_irq(&parent_ctx->lock);
3713 }
3714
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02003715 return ret;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003716}
3717
Ingo Molnar04289bb2008-12-11 08:38:42 +01003718static void __cpuinit perf_counter_init_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003719{
Ingo Molnar04289bb2008-12-11 08:38:42 +01003720 struct perf_cpu_context *cpuctx;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003721
Ingo Molnar04289bb2008-12-11 08:38:42 +01003722 cpuctx = &per_cpu(perf_cpu_context, cpu);
3723 __perf_counter_init_context(&cpuctx->ctx, NULL);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003724
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003725 spin_lock(&perf_resource_lock);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003726 cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu;
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003727 spin_unlock(&perf_resource_lock);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003728
Paul Mackerras01d02872009-01-14 13:44:19 +11003729 hw_perf_counter_setup(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003730}
3731
3732#ifdef CONFIG_HOTPLUG_CPU
Ingo Molnar04289bb2008-12-11 08:38:42 +01003733static void __perf_counter_exit_cpu(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003734{
3735 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
3736 struct perf_counter_context *ctx = &cpuctx->ctx;
3737 struct perf_counter *counter, *tmp;
3738
Ingo Molnar04289bb2008-12-11 08:38:42 +01003739 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry)
3740 __perf_counter_remove_from_context(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003741}
Ingo Molnar04289bb2008-12-11 08:38:42 +01003742static void perf_counter_exit_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003743{
Paul Mackerrasd859e292009-01-17 18:10:22 +11003744 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
3745 struct perf_counter_context *ctx = &cpuctx->ctx;
3746
3747 mutex_lock(&ctx->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003748 smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003749 mutex_unlock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003750}
3751#else
Ingo Molnar04289bb2008-12-11 08:38:42 +01003752static inline void perf_counter_exit_cpu(int cpu) { }
Thomas Gleixner0793a612008-12-04 20:12:29 +01003753#endif
3754
3755static int __cpuinit
3756perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
3757{
3758 unsigned int cpu = (long)hcpu;
3759
3760 switch (action) {
3761
3762 case CPU_UP_PREPARE:
3763 case CPU_UP_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01003764 perf_counter_init_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003765 break;
3766
3767 case CPU_DOWN_PREPARE:
3768 case CPU_DOWN_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01003769 perf_counter_exit_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003770 break;
3771
3772 default:
3773 break;
3774 }
3775
3776 return NOTIFY_OK;
3777}
3778
3779static struct notifier_block __cpuinitdata perf_cpu_nb = {
3780 .notifier_call = perf_cpu_notify,
3781};
3782
Ingo Molnar0d905bc2009-05-04 19:13:30 +02003783void __init perf_counter_init(void)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003784{
3785 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
3786 (void *)(long)smp_processor_id());
3787 register_cpu_notifier(&perf_cpu_nb);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003788}
Thomas Gleixner0793a612008-12-04 20:12:29 +01003789
3790static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
3791{
3792 return sprintf(buf, "%d\n", perf_reserved_percpu);
3793}
3794
3795static ssize_t
3796perf_set_reserve_percpu(struct sysdev_class *class,
3797 const char *buf,
3798 size_t count)
3799{
3800 struct perf_cpu_context *cpuctx;
3801 unsigned long val;
3802 int err, cpu, mpt;
3803
3804 err = strict_strtoul(buf, 10, &val);
3805 if (err)
3806 return err;
3807 if (val > perf_max_counters)
3808 return -EINVAL;
3809
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003810 spin_lock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003811 perf_reserved_percpu = val;
3812 for_each_online_cpu(cpu) {
3813 cpuctx = &per_cpu(perf_cpu_context, cpu);
3814 spin_lock_irq(&cpuctx->ctx.lock);
3815 mpt = min(perf_max_counters - cpuctx->ctx.nr_counters,
3816 perf_max_counters - perf_reserved_percpu);
3817 cpuctx->max_pertask = mpt;
3818 spin_unlock_irq(&cpuctx->ctx.lock);
3819 }
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003820 spin_unlock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003821
3822 return count;
3823}
3824
3825static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
3826{
3827 return sprintf(buf, "%d\n", perf_overcommit);
3828}
3829
3830static ssize_t
3831perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
3832{
3833 unsigned long val;
3834 int err;
3835
3836 err = strict_strtoul(buf, 10, &val);
3837 if (err)
3838 return err;
3839 if (val > 1)
3840 return -EINVAL;
3841
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003842 spin_lock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003843 perf_overcommit = val;
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003844 spin_unlock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003845
3846 return count;
3847}
3848
3849static SYSDEV_CLASS_ATTR(
3850 reserve_percpu,
3851 0644,
3852 perf_show_reserve_percpu,
3853 perf_set_reserve_percpu
3854 );
3855
3856static SYSDEV_CLASS_ATTR(
3857 overcommit,
3858 0644,
3859 perf_show_overcommit,
3860 perf_set_overcommit
3861 );
3862
3863static struct attribute *perfclass_attrs[] = {
3864 &attr_reserve_percpu.attr,
3865 &attr_overcommit.attr,
3866 NULL
3867};
3868
3869static struct attribute_group perfclass_attr_group = {
3870 .attrs = perfclass_attrs,
3871 .name = "perf_counters",
3872};
3873
3874static int __init perf_counter_sysfs_init(void)
3875{
3876 return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
3877 &perfclass_attr_group);
3878}
3879device_initcall(perf_counter_sysfs_init);