blob: eb346048f00f5beaa1fb1439543db0ae3445672f [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
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200237 local_irq_save(flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100238 /*
239 * If this is a task context, we need to check whether it is
240 * the current task context of this cpu. If not it has been
241 * scheduled out before the smp call arrived.
242 */
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200243 if (ctx->task && cpuctx->task_ctx != ctx) {
244 local_irq_restore(flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100245 return;
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200246 }
Thomas Gleixner0793a612008-12-04 20:12:29 +0100247
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200248 spin_lock(&ctx->lock);
Ingo Molnar34adc802009-05-20 20:13:28 +0200249 /*
250 * Protect the list operation against NMI by disabling the
251 * counters on a global level.
252 */
253 perf_disable();
Thomas Gleixner0793a612008-12-04 20:12:29 +0100254
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100255 counter_sched_out(counter, cpuctx, ctx);
256
Ingo Molnar04289bb2008-12-11 08:38:42 +0100257 list_del_counter(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100258
259 if (!ctx->task) {
260 /*
261 * Allow more per task counters with respect to the
262 * reservation:
263 */
264 cpuctx->max_pertask =
265 min(perf_max_counters - ctx->nr_counters,
266 perf_max_counters - perf_reserved_percpu);
267 }
268
Ingo Molnar34adc802009-05-20 20:13:28 +0200269 perf_enable();
Peter Zijlstra849691a2009-04-06 11:45:12 +0200270 spin_unlock_irqrestore(&ctx->lock, flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100271}
272
273
274/*
275 * Remove the counter from a task's (or a CPU's) list of counters.
276 *
Peter Zijlstrafccc7142009-05-23 18:28:56 +0200277 * Must be called with ctx->mutex held.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100278 *
279 * CPU counters are removed with a smp call. For task counters we only
280 * call when the task is on a CPU.
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000281 *
282 * If counter->ctx is a cloned context, callers must make sure that
283 * every task struct that counter->ctx->task could possibly point to
284 * remains valid. This is OK when called from perf_release since
285 * that only calls us on the top-level context, which can't be a clone.
286 * When called from perf_counter_exit_task, it's OK because the
287 * context has been detached from its task.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100288 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100289static void perf_counter_remove_from_context(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100290{
291 struct perf_counter_context *ctx = counter->ctx;
292 struct task_struct *task = ctx->task;
293
294 if (!task) {
295 /*
296 * Per cpu counters are removed via an smp call and
297 * the removal is always sucessful.
298 */
299 smp_call_function_single(counter->cpu,
Ingo Molnar04289bb2008-12-11 08:38:42 +0100300 __perf_counter_remove_from_context,
Thomas Gleixner0793a612008-12-04 20:12:29 +0100301 counter, 1);
302 return;
303 }
304
305retry:
Ingo Molnar04289bb2008-12-11 08:38:42 +0100306 task_oncpu_function_call(task, __perf_counter_remove_from_context,
Thomas Gleixner0793a612008-12-04 20:12:29 +0100307 counter);
308
309 spin_lock_irq(&ctx->lock);
310 /*
311 * If the context is active we need to retry the smp call.
312 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100313 if (ctx->nr_active && !list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100314 spin_unlock_irq(&ctx->lock);
315 goto retry;
316 }
317
318 /*
319 * The lock prevents that this context is scheduled in so we
Ingo Molnar04289bb2008-12-11 08:38:42 +0100320 * can remove the counter safely, if the call above did not
Thomas Gleixner0793a612008-12-04 20:12:29 +0100321 * succeed.
322 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100323 if (!list_empty(&counter->list_entry)) {
Ingo Molnar04289bb2008-12-11 08:38:42 +0100324 list_del_counter(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100325 }
326 spin_unlock_irq(&ctx->lock);
327}
328
Peter Zijlstra4af49982009-04-06 11:45:10 +0200329static inline u64 perf_clock(void)
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100330{
Peter Zijlstra4af49982009-04-06 11:45:10 +0200331 return cpu_clock(smp_processor_id());
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100332}
333
334/*
335 * Update the record of the current time in a context.
336 */
Peter Zijlstra4af49982009-04-06 11:45:10 +0200337static void update_context_time(struct perf_counter_context *ctx)
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100338{
Peter Zijlstra4af49982009-04-06 11:45:10 +0200339 u64 now = perf_clock();
340
341 ctx->time += now - ctx->timestamp;
342 ctx->timestamp = now;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100343}
344
345/*
346 * Update the total_time_enabled and total_time_running fields for a counter.
347 */
348static void update_counter_times(struct perf_counter *counter)
349{
350 struct perf_counter_context *ctx = counter->ctx;
351 u64 run_end;
352
Peter Zijlstra4af49982009-04-06 11:45:10 +0200353 if (counter->state < PERF_COUNTER_STATE_INACTIVE)
354 return;
355
356 counter->total_time_enabled = ctx->time - counter->tstamp_enabled;
357
358 if (counter->state == PERF_COUNTER_STATE_INACTIVE)
359 run_end = counter->tstamp_stopped;
360 else
361 run_end = ctx->time;
362
363 counter->total_time_running = run_end - counter->tstamp_running;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100364}
365
366/*
367 * Update total_time_enabled and total_time_running for all counters in a group.
368 */
369static void update_group_times(struct perf_counter *leader)
370{
371 struct perf_counter *counter;
372
373 update_counter_times(leader);
374 list_for_each_entry(counter, &leader->sibling_list, list_entry)
375 update_counter_times(counter);
376}
377
378/*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100379 * Cross CPU call to disable a performance counter
380 */
381static void __perf_counter_disable(void *info)
382{
383 struct perf_counter *counter = info;
384 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
385 struct perf_counter_context *ctx = counter->ctx;
386 unsigned long flags;
387
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200388 local_irq_save(flags);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100389 /*
390 * If this is a per-task counter, need to check whether this
391 * counter's task is the current task on this cpu.
392 */
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200393 if (ctx->task && cpuctx->task_ctx != ctx) {
394 local_irq_restore(flags);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100395 return;
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200396 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100397
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200398 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100399
400 /*
401 * If the counter is on, turn it off.
402 * If it is in error state, leave it in error state.
403 */
404 if (counter->state >= PERF_COUNTER_STATE_INACTIVE) {
Peter Zijlstra4af49982009-04-06 11:45:10 +0200405 update_context_time(ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100406 update_counter_times(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100407 if (counter == counter->group_leader)
408 group_sched_out(counter, cpuctx, ctx);
409 else
410 counter_sched_out(counter, cpuctx, ctx);
411 counter->state = PERF_COUNTER_STATE_OFF;
412 }
413
Peter Zijlstra849691a2009-04-06 11:45:12 +0200414 spin_unlock_irqrestore(&ctx->lock, flags);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100415}
416
417/*
418 * Disable a counter.
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000419 *
420 * If counter->ctx is a cloned context, callers must make sure that
421 * every task struct that counter->ctx->task could possibly point to
422 * remains valid. This condition is satisifed when called through
423 * perf_counter_for_each_child or perf_counter_for_each because they
424 * hold the top-level counter's child_mutex, so any descendant that
425 * goes to exit will block in sync_child_counter.
426 * When called from perf_pending_counter it's OK because counter->ctx
427 * is the current context on this CPU and preemption is disabled,
428 * hence we can't get into perf_counter_task_sched_out for this context.
Paul Mackerrasd859e292009-01-17 18:10:22 +1100429 */
430static void perf_counter_disable(struct perf_counter *counter)
431{
432 struct perf_counter_context *ctx = counter->ctx;
433 struct task_struct *task = ctx->task;
434
435 if (!task) {
436 /*
437 * Disable the counter on the cpu that it's on
438 */
439 smp_call_function_single(counter->cpu, __perf_counter_disable,
440 counter, 1);
441 return;
442 }
443
444 retry:
445 task_oncpu_function_call(task, __perf_counter_disable, counter);
446
447 spin_lock_irq(&ctx->lock);
448 /*
449 * If the counter is still active, we need to retry the cross-call.
450 */
451 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
452 spin_unlock_irq(&ctx->lock);
453 goto retry;
454 }
455
456 /*
457 * Since we have the lock this context can't be scheduled
458 * in, so we can change the state safely.
459 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100460 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
461 update_counter_times(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100462 counter->state = PERF_COUNTER_STATE_OFF;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100463 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100464
465 spin_unlock_irq(&ctx->lock);
466}
467
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100468static int
469counter_sched_in(struct perf_counter *counter,
470 struct perf_cpu_context *cpuctx,
471 struct perf_counter_context *ctx,
472 int cpu)
473{
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100474 if (counter->state <= PERF_COUNTER_STATE_OFF)
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100475 return 0;
476
477 counter->state = PERF_COUNTER_STATE_ACTIVE;
478 counter->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */
479 /*
480 * The new state must be visible before we turn it on in the hardware:
481 */
482 smp_wmb();
483
Robert Richter4aeb0b42009-04-29 12:47:03 +0200484 if (counter->pmu->enable(counter)) {
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100485 counter->state = PERF_COUNTER_STATE_INACTIVE;
486 counter->oncpu = -1;
487 return -EAGAIN;
488 }
489
Peter Zijlstra4af49982009-04-06 11:45:10 +0200490 counter->tstamp_running += ctx->time - counter->tstamp_stopped;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100491
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100492 if (!is_software_counter(counter))
493 cpuctx->active_oncpu++;
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100494 ctx->nr_active++;
495
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100496 if (counter->hw_event.exclusive)
497 cpuctx->exclusive = 1;
498
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100499 return 0;
500}
501
Paul Mackerras6751b712009-05-11 12:08:02 +1000502static int
503group_sched_in(struct perf_counter *group_counter,
504 struct perf_cpu_context *cpuctx,
505 struct perf_counter_context *ctx,
506 int cpu)
507{
508 struct perf_counter *counter, *partial_group;
509 int ret;
510
511 if (group_counter->state == PERF_COUNTER_STATE_OFF)
512 return 0;
513
514 ret = hw_perf_group_sched_in(group_counter, cpuctx, ctx, cpu);
515 if (ret)
516 return ret < 0 ? ret : 0;
517
518 group_counter->prev_state = group_counter->state;
519 if (counter_sched_in(group_counter, cpuctx, ctx, cpu))
520 return -EAGAIN;
521
522 /*
523 * Schedule in siblings as one group (if any):
524 */
525 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
526 counter->prev_state = counter->state;
527 if (counter_sched_in(counter, cpuctx, ctx, cpu)) {
528 partial_group = counter;
529 goto group_error;
530 }
531 }
532
533 return 0;
534
535group_error:
536 /*
537 * Groups can be scheduled in as one unit only, so undo any
538 * partial group before returning:
539 */
540 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
541 if (counter == partial_group)
542 break;
543 counter_sched_out(counter, cpuctx, ctx);
544 }
545 counter_sched_out(group_counter, cpuctx, ctx);
546
547 return -EAGAIN;
548}
549
Thomas Gleixner0793a612008-12-04 20:12:29 +0100550/*
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100551 * Return 1 for a group consisting entirely of software counters,
552 * 0 if the group contains any hardware counters.
553 */
554static int is_software_only_group(struct perf_counter *leader)
555{
556 struct perf_counter *counter;
557
558 if (!is_software_counter(leader))
559 return 0;
Peter Zijlstra5c148192009-03-25 12:30:23 +0100560
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100561 list_for_each_entry(counter, &leader->sibling_list, list_entry)
562 if (!is_software_counter(counter))
563 return 0;
Peter Zijlstra5c148192009-03-25 12:30:23 +0100564
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100565 return 1;
566}
567
568/*
569 * Work out whether we can put this counter group on the CPU now.
570 */
571static int group_can_go_on(struct perf_counter *counter,
572 struct perf_cpu_context *cpuctx,
573 int can_add_hw)
574{
575 /*
576 * Groups consisting entirely of software counters can always go on.
577 */
578 if (is_software_only_group(counter))
579 return 1;
580 /*
581 * If an exclusive group is already on, no other hardware
582 * counters can go on.
583 */
584 if (cpuctx->exclusive)
585 return 0;
586 /*
587 * If this group is exclusive and there are already
588 * counters on the CPU, it can't go on.
589 */
590 if (counter->hw_event.exclusive && cpuctx->active_oncpu)
591 return 0;
592 /*
593 * Otherwise, try to add it if all previous groups were able
594 * to go on.
595 */
596 return can_add_hw;
597}
598
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100599static void add_counter_to_ctx(struct perf_counter *counter,
600 struct perf_counter_context *ctx)
601{
602 list_add_counter(counter, ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100603 counter->prev_state = PERF_COUNTER_STATE_OFF;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200604 counter->tstamp_enabled = ctx->time;
605 counter->tstamp_running = ctx->time;
606 counter->tstamp_stopped = ctx->time;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100607}
608
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100609/*
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100610 * Cross CPU call to install and enable a performance counter
Peter Zijlstra682076a2009-05-23 18:28:57 +0200611 *
612 * Must be called with ctx->mutex held
Thomas Gleixner0793a612008-12-04 20:12:29 +0100613 */
614static void __perf_install_in_context(void *info)
615{
616 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
617 struct perf_counter *counter = info;
618 struct perf_counter_context *ctx = counter->ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100619 struct perf_counter *leader = counter->group_leader;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100620 int cpu = smp_processor_id();
Ingo Molnar9b51f662008-12-12 13:49:45 +0100621 unsigned long flags;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100622 int err;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100623
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200624 local_irq_save(flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100625 /*
626 * If this is a task context, we need to check whether it is
627 * the current task context of this cpu. If not it has been
628 * scheduled out before the smp call arrived.
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000629 * Or possibly this is the right context but it isn't
630 * on this cpu because it had no counters.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100631 */
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000632 if (ctx->task && cpuctx->task_ctx != ctx) {
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200633 if (cpuctx->task_ctx || ctx->task != current) {
634 local_irq_restore(flags);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000635 return;
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200636 }
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000637 cpuctx->task_ctx = ctx;
638 }
Thomas Gleixner0793a612008-12-04 20:12:29 +0100639
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200640 spin_lock(&ctx->lock);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000641 ctx->is_active = 1;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200642 update_context_time(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100643
644 /*
645 * Protect the list operation against NMI by disabling the
646 * counters on a global level. NOP for non NMI based counters.
647 */
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200648 perf_disable();
Thomas Gleixner0793a612008-12-04 20:12:29 +0100649
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100650 add_counter_to_ctx(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100651
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100652 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100653 * Don't put the counter on if it is disabled or if
654 * it is in a group and the group isn't on.
655 */
656 if (counter->state != PERF_COUNTER_STATE_INACTIVE ||
657 (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE))
658 goto unlock;
659
660 /*
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100661 * An exclusive counter can't go on if there are already active
662 * hardware counters, and no hardware counter can go on if there
663 * is already an exclusive counter on.
664 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100665 if (!group_can_go_on(counter, cpuctx, 1))
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100666 err = -EEXIST;
667 else
668 err = counter_sched_in(counter, cpuctx, ctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100669
Paul Mackerrasd859e292009-01-17 18:10:22 +1100670 if (err) {
671 /*
672 * This counter couldn't go on. If it is in a group
673 * then we have to pull the whole group off.
674 * If the counter group is pinned then put it in error state.
675 */
676 if (leader != counter)
677 group_sched_out(leader, cpuctx, ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100678 if (leader->hw_event.pinned) {
679 update_group_times(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100680 leader->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100681 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100682 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100683
684 if (!err && !ctx->task && cpuctx->max_pertask)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100685 cpuctx->max_pertask--;
686
Paul Mackerrasd859e292009-01-17 18:10:22 +1100687 unlock:
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200688 perf_enable();
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100689
Peter Zijlstra849691a2009-04-06 11:45:12 +0200690 spin_unlock_irqrestore(&ctx->lock, flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100691}
692
693/*
694 * Attach a performance counter to a context
695 *
696 * First we add the counter to the list with the hardware enable bit
697 * in counter->hw_config cleared.
698 *
699 * If the counter is attached to a task which is on a CPU we use a smp
700 * call to enable it in the task context. The task might have been
701 * scheduled away, but we check this in the smp call again.
Paul Mackerrasd859e292009-01-17 18:10:22 +1100702 *
703 * Must be called with ctx->mutex held.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100704 */
705static void
706perf_install_in_context(struct perf_counter_context *ctx,
707 struct perf_counter *counter,
708 int cpu)
709{
710 struct task_struct *task = ctx->task;
711
Thomas Gleixner0793a612008-12-04 20:12:29 +0100712 if (!task) {
713 /*
714 * Per cpu counters are installed via an smp call and
715 * the install is always sucessful.
716 */
717 smp_call_function_single(cpu, __perf_install_in_context,
718 counter, 1);
719 return;
720 }
721
Thomas Gleixner0793a612008-12-04 20:12:29 +0100722retry:
723 task_oncpu_function_call(task, __perf_install_in_context,
724 counter);
725
726 spin_lock_irq(&ctx->lock);
727 /*
Thomas Gleixner0793a612008-12-04 20:12:29 +0100728 * we need to retry the smp call.
729 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100730 if (ctx->is_active && list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100731 spin_unlock_irq(&ctx->lock);
732 goto retry;
733 }
734
735 /*
736 * The lock prevents that this context is scheduled in so we
737 * can add the counter safely, if it the call above did not
738 * succeed.
739 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100740 if (list_empty(&counter->list_entry))
741 add_counter_to_ctx(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100742 spin_unlock_irq(&ctx->lock);
743}
744
Paul Mackerrasd859e292009-01-17 18:10:22 +1100745/*
746 * Cross CPU call to enable a performance counter
747 */
748static void __perf_counter_enable(void *info)
Ingo Molnar04289bb2008-12-11 08:38:42 +0100749{
Paul Mackerrasd859e292009-01-17 18:10:22 +1100750 struct perf_counter *counter = info;
751 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
752 struct perf_counter_context *ctx = counter->ctx;
753 struct perf_counter *leader = counter->group_leader;
754 unsigned long flags;
755 int err;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100756
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200757 local_irq_save(flags);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100758 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100759 * If this is a per-task counter, need to check whether this
760 * counter's task is the current task on this cpu.
Ingo Molnar04289bb2008-12-11 08:38:42 +0100761 */
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000762 if (ctx->task && cpuctx->task_ctx != ctx) {
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200763 if (cpuctx->task_ctx || ctx->task != current) {
764 local_irq_restore(flags);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000765 return;
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200766 }
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000767 cpuctx->task_ctx = ctx;
768 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100769
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200770 spin_lock(&ctx->lock);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000771 ctx->is_active = 1;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200772 update_context_time(ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100773
Paul Mackerrasc07c99b2009-02-13 22:10:34 +1100774 counter->prev_state = counter->state;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100775 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
776 goto unlock;
777 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200778 counter->tstamp_enabled = ctx->time - counter->total_time_enabled;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100779
780 /*
781 * If the counter is in a group and isn't the group leader,
782 * then don't put it on unless the group is on.
783 */
784 if (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE)
785 goto unlock;
786
Paul Mackerrase758a332009-05-12 21:59:01 +1000787 if (!group_can_go_on(counter, cpuctx, 1)) {
Paul Mackerrasd859e292009-01-17 18:10:22 +1100788 err = -EEXIST;
Paul Mackerrase758a332009-05-12 21:59:01 +1000789 } else {
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200790 perf_disable();
Paul Mackerrase758a332009-05-12 21:59:01 +1000791 if (counter == leader)
792 err = group_sched_in(counter, cpuctx, ctx,
793 smp_processor_id());
794 else
795 err = counter_sched_in(counter, cpuctx, ctx,
796 smp_processor_id());
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200797 perf_enable();
Paul Mackerrase758a332009-05-12 21:59:01 +1000798 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100799
800 if (err) {
801 /*
802 * If this counter can't go on and it's part of a
803 * group, then the whole group has to come off.
804 */
805 if (leader != counter)
806 group_sched_out(leader, cpuctx, ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100807 if (leader->hw_event.pinned) {
808 update_group_times(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100809 leader->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100810 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100811 }
812
813 unlock:
Peter Zijlstra849691a2009-04-06 11:45:12 +0200814 spin_unlock_irqrestore(&ctx->lock, flags);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100815}
816
817/*
818 * Enable a counter.
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000819 *
820 * If counter->ctx is a cloned context, callers must make sure that
821 * every task struct that counter->ctx->task could possibly point to
822 * remains valid. This condition is satisfied when called through
823 * perf_counter_for_each_child or perf_counter_for_each as described
824 * for perf_counter_disable.
Paul Mackerrasd859e292009-01-17 18:10:22 +1100825 */
826static void perf_counter_enable(struct perf_counter *counter)
827{
828 struct perf_counter_context *ctx = counter->ctx;
829 struct task_struct *task = ctx->task;
830
831 if (!task) {
832 /*
833 * Enable the counter on the cpu that it's on
834 */
835 smp_call_function_single(counter->cpu, __perf_counter_enable,
836 counter, 1);
837 return;
838 }
839
840 spin_lock_irq(&ctx->lock);
841 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
842 goto out;
843
844 /*
845 * If the counter is in error state, clear that first.
846 * That way, if we see the counter in error state below, we
847 * know that it has gone back into error state, as distinct
848 * from the task having been scheduled away before the
849 * cross-call arrived.
850 */
851 if (counter->state == PERF_COUNTER_STATE_ERROR)
852 counter->state = PERF_COUNTER_STATE_OFF;
853
854 retry:
855 spin_unlock_irq(&ctx->lock);
856 task_oncpu_function_call(task, __perf_counter_enable, counter);
857
858 spin_lock_irq(&ctx->lock);
859
860 /*
861 * If the context is active and the counter is still off,
862 * we need to retry the cross-call.
863 */
864 if (ctx->is_active && counter->state == PERF_COUNTER_STATE_OFF)
865 goto retry;
866
867 /*
868 * Since we have the lock this context can't be scheduled
869 * in, so we can change the state safely.
870 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100871 if (counter->state == PERF_COUNTER_STATE_OFF) {
Paul Mackerrasd859e292009-01-17 18:10:22 +1100872 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200873 counter->tstamp_enabled =
874 ctx->time - counter->total_time_enabled;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100875 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100876 out:
877 spin_unlock_irq(&ctx->lock);
878}
879
Peter Zijlstra2023b352009-05-05 17:50:26 +0200880static int perf_counter_refresh(struct perf_counter *counter, int refresh)
Peter Zijlstra79f14642009-04-06 11:45:07 +0200881{
Peter Zijlstra2023b352009-05-05 17:50:26 +0200882 /*
883 * not supported on inherited counters
884 */
885 if (counter->hw_event.inherit)
886 return -EINVAL;
887
Peter Zijlstra79f14642009-04-06 11:45:07 +0200888 atomic_add(refresh, &counter->event_limit);
889 perf_counter_enable(counter);
Peter Zijlstra2023b352009-05-05 17:50:26 +0200890
891 return 0;
Peter Zijlstra79f14642009-04-06 11:45:07 +0200892}
893
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100894void __perf_counter_sched_out(struct perf_counter_context *ctx,
895 struct perf_cpu_context *cpuctx)
896{
897 struct perf_counter *counter;
898
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100899 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100900 ctx->is_active = 0;
901 if (likely(!ctx->nr_counters))
902 goto out;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200903 update_context_time(ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100904
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200905 perf_disable();
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100906 if (ctx->nr_active) {
Peter Zijlstraafedadf2009-05-20 12:21:22 +0200907 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
908 if (counter != counter->group_leader)
909 counter_sched_out(counter, cpuctx, ctx);
910 else
911 group_sched_out(counter, cpuctx, ctx);
912 }
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100913 }
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200914 perf_enable();
Paul Mackerrasd859e292009-01-17 18:10:22 +1100915 out:
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100916 spin_unlock(&ctx->lock);
917}
918
Thomas Gleixner0793a612008-12-04 20:12:29 +0100919/*
Paul Mackerras564c2b22009-05-22 14:27:22 +1000920 * Test whether two contexts are equivalent, i.e. whether they
921 * have both been cloned from the same version of the same context
922 * and they both have the same number of enabled counters.
923 * If the number of enabled counters is the same, then the set
924 * of enabled counters should be the same, because these are both
925 * inherited contexts, therefore we can't access individual counters
926 * in them directly with an fd; we can only enable/disable all
927 * counters via prctl, or enable/disable all counters in a family
928 * via ioctl, which will have the same effect on both contexts.
929 */
930static int context_equiv(struct perf_counter_context *ctx1,
931 struct perf_counter_context *ctx2)
932{
933 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
Paul Mackerrasad3a37d2009-05-29 16:06:20 +1000934 && ctx1->parent_gen == ctx2->parent_gen
935 && ctx1->parent_gen != ~0ull;
Paul Mackerras564c2b22009-05-22 14:27:22 +1000936}
937
938/*
Thomas Gleixner0793a612008-12-04 20:12:29 +0100939 * Called from scheduler to remove the counters of the current task,
940 * with interrupts disabled.
941 *
942 * We stop each counter and update the counter value in counter->count.
943 *
Ingo Molnar76715812008-12-17 14:20:28 +0100944 * This does not protect us against NMI, but disable()
Thomas Gleixner0793a612008-12-04 20:12:29 +0100945 * sets the disabled bit in the control field of counter _before_
946 * accessing the counter control register. If a NMI hits, then it will
947 * not restart the counter.
948 */
Paul Mackerras564c2b22009-05-22 14:27:22 +1000949void perf_counter_task_sched_out(struct task_struct *task,
950 struct task_struct *next, int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100951{
952 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000953 struct perf_counter_context *ctx = task->perf_counter_ctxp;
Paul Mackerras564c2b22009-05-22 14:27:22 +1000954 struct perf_counter_context *next_ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000955 struct perf_counter_context *parent;
Peter Zijlstra4a0deca2009-03-19 20:26:12 +0100956 struct pt_regs *regs;
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000957 int do_switch = 1;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100958
Peter Zijlstra10989fb2009-05-25 14:45:28 +0200959 regs = task_pt_regs(task);
960 perf_swcounter_event(PERF_COUNT_CONTEXT_SWITCHES, 1, 1, regs, 0);
961
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000962 if (likely(!ctx || !cpuctx->task_ctx))
Thomas Gleixner0793a612008-12-04 20:12:29 +0100963 return;
964
Peter Zijlstrabce379b2009-04-06 11:45:13 +0200965 update_context_time(ctx);
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000966
967 rcu_read_lock();
968 parent = rcu_dereference(ctx->parent_ctx);
Paul Mackerras564c2b22009-05-22 14:27:22 +1000969 next_ctx = next->perf_counter_ctxp;
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000970 if (parent && next_ctx &&
971 rcu_dereference(next_ctx->parent_ctx) == parent) {
972 /*
973 * Looks like the two contexts are clones, so we might be
974 * able to optimize the context switch. We lock both
975 * contexts and check that they are clones under the
976 * lock (including re-checking that neither has been
977 * uncloned in the meantime). It doesn't matter which
978 * order we take the locks because no other cpu could
979 * be trying to lock both of these tasks.
980 */
981 spin_lock(&ctx->lock);
982 spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
983 if (context_equiv(ctx, next_ctx)) {
984 task->perf_counter_ctxp = next_ctx;
985 next->perf_counter_ctxp = ctx;
986 ctx->task = next;
987 next_ctx->task = task;
988 do_switch = 0;
989 }
990 spin_unlock(&next_ctx->lock);
991 spin_unlock(&ctx->lock);
Paul Mackerras564c2b22009-05-22 14:27:22 +1000992 }
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000993 rcu_read_unlock();
Paul Mackerras564c2b22009-05-22 14:27:22 +1000994
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000995 if (do_switch) {
996 __perf_counter_sched_out(ctx, cpuctx);
997 cpuctx->task_ctx = NULL;
998 }
Thomas Gleixner0793a612008-12-04 20:12:29 +0100999}
1000
Paul Mackerrasa08b1592009-05-11 15:46:10 +10001001static void __perf_counter_task_sched_out(struct perf_counter_context *ctx)
1002{
1003 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1004
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001005 if (!cpuctx->task_ctx)
1006 return;
Paul Mackerrasa08b1592009-05-11 15:46:10 +10001007 __perf_counter_sched_out(ctx, cpuctx);
1008 cpuctx->task_ctx = NULL;
1009}
1010
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001011static void perf_counter_cpu_sched_out(struct perf_cpu_context *cpuctx)
Ingo Molnar04289bb2008-12-11 08:38:42 +01001012{
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001013 __perf_counter_sched_out(&cpuctx->ctx, cpuctx);
Ingo Molnar04289bb2008-12-11 08:38:42 +01001014}
1015
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001016static void
1017__perf_counter_sched_in(struct perf_counter_context *ctx,
1018 struct perf_cpu_context *cpuctx, int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001019{
Thomas Gleixner0793a612008-12-04 20:12:29 +01001020 struct perf_counter *counter;
Paul Mackerrasdd0e6ba2009-01-12 15:11:00 +11001021 int can_add_hw = 1;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001022
Thomas Gleixner0793a612008-12-04 20:12:29 +01001023 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001024 ctx->is_active = 1;
1025 if (likely(!ctx->nr_counters))
1026 goto out;
1027
Peter Zijlstra4af49982009-04-06 11:45:10 +02001028 ctx->timestamp = perf_clock();
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001029
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001030 perf_disable();
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001031
1032 /*
1033 * First go through the list and put on any pinned groups
1034 * in order to give them the best chance of going on.
1035 */
Ingo Molnar04289bb2008-12-11 08:38:42 +01001036 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001037 if (counter->state <= PERF_COUNTER_STATE_OFF ||
1038 !counter->hw_event.pinned)
1039 continue;
1040 if (counter->cpu != -1 && counter->cpu != cpu)
1041 continue;
1042
Peter Zijlstraafedadf2009-05-20 12:21:22 +02001043 if (counter != counter->group_leader)
1044 counter_sched_in(counter, cpuctx, ctx, cpu);
1045 else {
1046 if (group_can_go_on(counter, cpuctx, 1))
1047 group_sched_in(counter, cpuctx, ctx, cpu);
1048 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001049
1050 /*
1051 * If this pinned group hasn't been scheduled,
1052 * put it in error state.
1053 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001054 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
1055 update_group_times(counter);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001056 counter->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001057 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001058 }
1059
1060 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1061 /*
1062 * Ignore counters in OFF or ERROR state, and
1063 * ignore pinned counters since we did them already.
1064 */
1065 if (counter->state <= PERF_COUNTER_STATE_OFF ||
1066 counter->hw_event.pinned)
1067 continue;
1068
Ingo Molnar04289bb2008-12-11 08:38:42 +01001069 /*
1070 * Listen to the 'cpu' scheduling filter constraint
1071 * of counters:
1072 */
Thomas Gleixner0793a612008-12-04 20:12:29 +01001073 if (counter->cpu != -1 && counter->cpu != cpu)
1074 continue;
1075
Peter Zijlstraafedadf2009-05-20 12:21:22 +02001076 if (counter != counter->group_leader) {
1077 if (counter_sched_in(counter, cpuctx, ctx, cpu))
Paul Mackerrasdd0e6ba2009-01-12 15:11:00 +11001078 can_add_hw = 0;
Peter Zijlstraafedadf2009-05-20 12:21:22 +02001079 } else {
1080 if (group_can_go_on(counter, cpuctx, can_add_hw)) {
1081 if (group_sched_in(counter, cpuctx, ctx, cpu))
1082 can_add_hw = 0;
1083 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001084 }
Thomas Gleixner0793a612008-12-04 20:12:29 +01001085 }
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001086 perf_enable();
Paul Mackerrasd859e292009-01-17 18:10:22 +11001087 out:
Thomas Gleixner0793a612008-12-04 20:12:29 +01001088 spin_unlock(&ctx->lock);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001089}
Ingo Molnar04289bb2008-12-11 08:38:42 +01001090
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001091/*
1092 * Called from scheduler to add the counters of the current task
1093 * with interrupts disabled.
1094 *
1095 * We restore the counter value and then enable it.
1096 *
1097 * This does not protect us against NMI, but enable()
1098 * sets the enabled bit in the control field of counter _before_
1099 * accessing the counter control register. If a NMI hits, then it will
1100 * keep the counter running.
1101 */
1102void perf_counter_task_sched_in(struct task_struct *task, int cpu)
1103{
1104 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001105 struct perf_counter_context *ctx = task->perf_counter_ctxp;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001106
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001107 if (likely(!ctx))
1108 return;
Paul Mackerras564c2b22009-05-22 14:27:22 +10001109 if (cpuctx->task_ctx == ctx)
1110 return;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001111 __perf_counter_sched_in(ctx, cpuctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001112 cpuctx->task_ctx = ctx;
1113}
1114
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001115static void perf_counter_cpu_sched_in(struct perf_cpu_context *cpuctx, int cpu)
1116{
1117 struct perf_counter_context *ctx = &cpuctx->ctx;
1118
1119 __perf_counter_sched_in(ctx, cpuctx, cpu);
1120}
1121
Peter Zijlstraa78ac322009-05-25 17:39:05 +02001122#define MAX_INTERRUPTS (~0ULL)
1123
1124static void perf_log_throttle(struct perf_counter *counter, int enable);
Peter Zijlstra26b119b2009-05-20 12:21:20 +02001125static void perf_log_period(struct perf_counter *counter, u64 period);
1126
1127static void perf_adjust_freq(struct perf_counter_context *ctx)
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001128{
1129 struct perf_counter *counter;
Peter Zijlstraa78ac322009-05-25 17:39:05 +02001130 u64 interrupts, irq_period;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001131 u64 events, period;
1132 s64 delta;
1133
1134 spin_lock(&ctx->lock);
1135 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1136 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
1137 continue;
1138
Peter Zijlstraa78ac322009-05-25 17:39:05 +02001139 interrupts = counter->hw.interrupts;
1140 counter->hw.interrupts = 0;
1141
1142 if (interrupts == MAX_INTERRUPTS) {
1143 perf_log_throttle(counter, 1);
1144 counter->pmu->unthrottle(counter);
1145 interrupts = 2*sysctl_perf_counter_limit/HZ;
1146 }
1147
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001148 if (!counter->hw_event.freq || !counter->hw_event.irq_freq)
1149 continue;
1150
Peter Zijlstraa78ac322009-05-25 17:39:05 +02001151 events = HZ * interrupts * counter->hw.irq_period;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001152 period = div64_u64(events, counter->hw_event.irq_freq);
1153
1154 delta = (s64)(1 + period - counter->hw.irq_period);
1155 delta >>= 1;
1156
1157 irq_period = counter->hw.irq_period + delta;
1158
1159 if (!irq_period)
1160 irq_period = 1;
1161
Peter Zijlstra26b119b2009-05-20 12:21:20 +02001162 perf_log_period(counter, irq_period);
1163
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001164 counter->hw.irq_period = irq_period;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001165 }
1166 spin_unlock(&ctx->lock);
1167}
1168
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001169/*
1170 * Round-robin a context's counters:
1171 */
1172static void rotate_ctx(struct perf_counter_context *ctx)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001173{
Thomas Gleixner0793a612008-12-04 20:12:29 +01001174 struct perf_counter *counter;
1175
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001176 if (!ctx->nr_counters)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001177 return;
1178
Thomas Gleixner0793a612008-12-04 20:12:29 +01001179 spin_lock(&ctx->lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001180 /*
Ingo Molnar04289bb2008-12-11 08:38:42 +01001181 * Rotate the first entry last (works just fine for group counters too):
Thomas Gleixner0793a612008-12-04 20:12:29 +01001182 */
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001183 perf_disable();
Ingo Molnar04289bb2008-12-11 08:38:42 +01001184 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Peter Zijlstra75564232009-03-13 12:21:29 +01001185 list_move_tail(&counter->list_entry, &ctx->counter_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001186 break;
1187 }
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001188 perf_enable();
Thomas Gleixner0793a612008-12-04 20:12:29 +01001189
1190 spin_unlock(&ctx->lock);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001191}
Thomas Gleixner0793a612008-12-04 20:12:29 +01001192
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001193void perf_counter_task_tick(struct task_struct *curr, int cpu)
1194{
Peter Zijlstra7fc23a52009-05-08 18:52:21 +02001195 struct perf_cpu_context *cpuctx;
1196 struct perf_counter_context *ctx;
1197
1198 if (!atomic_read(&nr_counters))
1199 return;
1200
1201 cpuctx = &per_cpu(perf_cpu_context, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001202 ctx = curr->perf_counter_ctxp;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001203
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001204 perf_adjust_freq(&cpuctx->ctx);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001205 if (ctx)
1206 perf_adjust_freq(ctx);
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001207
Ingo Molnarb82914c2009-05-04 18:54:32 +02001208 perf_counter_cpu_sched_out(cpuctx);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001209 if (ctx)
1210 __perf_counter_task_sched_out(ctx);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001211
Ingo Molnarb82914c2009-05-04 18:54:32 +02001212 rotate_ctx(&cpuctx->ctx);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001213 if (ctx)
1214 rotate_ctx(ctx);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001215
Ingo Molnarb82914c2009-05-04 18:54:32 +02001216 perf_counter_cpu_sched_in(cpuctx, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001217 if (ctx)
1218 perf_counter_task_sched_in(curr, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001219}
1220
1221/*
Thomas Gleixner0793a612008-12-04 20:12:29 +01001222 * Cross CPU call to read the hardware counter
1223 */
Ingo Molnar76715812008-12-17 14:20:28 +01001224static void __read(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001225{
Ingo Molnar621a01e2008-12-11 12:46:46 +01001226 struct perf_counter *counter = info;
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001227 struct perf_counter_context *ctx = counter->ctx;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001228 unsigned long flags;
Ingo Molnar621a01e2008-12-11 12:46:46 +01001229
Peter Zijlstra849691a2009-04-06 11:45:12 +02001230 local_irq_save(flags);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001231 if (ctx->is_active)
Peter Zijlstra4af49982009-04-06 11:45:10 +02001232 update_context_time(ctx);
Robert Richter4aeb0b42009-04-29 12:47:03 +02001233 counter->pmu->read(counter);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001234 update_counter_times(counter);
Peter Zijlstra849691a2009-04-06 11:45:12 +02001235 local_irq_restore(flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001236}
1237
Ingo Molnar04289bb2008-12-11 08:38:42 +01001238static u64 perf_counter_read(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001239{
1240 /*
1241 * If counter is enabled and currently active on a CPU, update the
1242 * value in the counter structure:
1243 */
Ingo Molnar6a930702008-12-11 15:17:03 +01001244 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
Thomas Gleixner0793a612008-12-04 20:12:29 +01001245 smp_call_function_single(counter->oncpu,
Ingo Molnar76715812008-12-17 14:20:28 +01001246 __read, counter, 1);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001247 } else if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
1248 update_counter_times(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001249 }
1250
Ingo Molnaree060942008-12-13 09:00:03 +01001251 return atomic64_read(&counter->count);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001252}
1253
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001254/*
1255 * Initialize the perf_counter context in a task_struct:
1256 */
1257static void
1258__perf_counter_init_context(struct perf_counter_context *ctx,
1259 struct task_struct *task)
1260{
1261 memset(ctx, 0, sizeof(*ctx));
1262 spin_lock_init(&ctx->lock);
1263 mutex_init(&ctx->mutex);
1264 INIT_LIST_HEAD(&ctx->counter_list);
1265 INIT_LIST_HEAD(&ctx->event_list);
1266 atomic_set(&ctx->refcount, 1);
1267 ctx->task = task;
1268}
1269
Thomas Gleixner0793a612008-12-04 20:12:29 +01001270static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
1271{
1272 struct perf_cpu_context *cpuctx;
1273 struct perf_counter_context *ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001274 struct perf_counter_context *parent_ctx;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001275 struct task_struct *task;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001276 int err;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001277
1278 /*
1279 * If cpu is not a wildcard then this is a percpu counter:
1280 */
1281 if (cpu != -1) {
1282 /* Must be root to operate on a CPU counter: */
Peter Zijlstra1ccd1542009-04-09 10:53:45 +02001283 if (sysctl_perf_counter_priv && !capable(CAP_SYS_ADMIN))
Thomas Gleixner0793a612008-12-04 20:12:29 +01001284 return ERR_PTR(-EACCES);
1285
1286 if (cpu < 0 || cpu > num_possible_cpus())
1287 return ERR_PTR(-EINVAL);
1288
1289 /*
1290 * We could be clever and allow to attach a counter to an
1291 * offline CPU and activate it when the CPU comes up, but
1292 * that's for later.
1293 */
1294 if (!cpu_isset(cpu, cpu_online_map))
1295 return ERR_PTR(-ENODEV);
1296
1297 cpuctx = &per_cpu(perf_cpu_context, cpu);
1298 ctx = &cpuctx->ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001299 get_ctx(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001300
Thomas Gleixner0793a612008-12-04 20:12:29 +01001301 return ctx;
1302 }
1303
1304 rcu_read_lock();
1305 if (!pid)
1306 task = current;
1307 else
1308 task = find_task_by_vpid(pid);
1309 if (task)
1310 get_task_struct(task);
1311 rcu_read_unlock();
1312
1313 if (!task)
1314 return ERR_PTR(-ESRCH);
1315
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001316 /*
1317 * Can't attach counters to a dying task.
1318 */
1319 err = -ESRCH;
1320 if (task->flags & PF_EXITING)
1321 goto errout;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001322
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001323 /* Reuse ptrace permission checks for now. */
1324 err = -EACCES;
1325 if (!ptrace_may_access(task, PTRACE_MODE_READ))
1326 goto errout;
1327
1328 retry_lock:
1329 rcu_read_lock();
1330 retry:
1331 ctx = rcu_dereference(task->perf_counter_ctxp);
1332 if (ctx) {
1333 /*
1334 * If this context is a clone of another, it might
1335 * get swapped for another underneath us by
1336 * perf_counter_task_sched_out, though the
1337 * rcu_read_lock() protects us from any context
1338 * getting freed. Lock the context and check if it
1339 * got swapped before we could get the lock, and retry
1340 * if so. If we locked the right context, then it
1341 * can't get swapped on us any more and we can
1342 * unclone it if necessary.
1343 * Once it's not a clone things will be stable.
1344 */
1345 spin_lock_irq(&ctx->lock);
1346 if (ctx != rcu_dereference(task->perf_counter_ctxp)) {
1347 spin_unlock_irq(&ctx->lock);
1348 goto retry;
1349 }
1350 parent_ctx = ctx->parent_ctx;
1351 if (parent_ctx) {
1352 put_ctx(parent_ctx);
1353 ctx->parent_ctx = NULL; /* no longer a clone */
1354 }
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001355 /*
1356 * Get an extra reference before dropping the lock so that
1357 * this context won't get freed if the task exits.
1358 */
1359 get_ctx(ctx);
1360 spin_unlock_irq(&ctx->lock);
1361 }
1362 rcu_read_unlock();
1363
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001364 if (!ctx) {
1365 ctx = kmalloc(sizeof(struct perf_counter_context), GFP_KERNEL);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001366 err = -ENOMEM;
1367 if (!ctx)
1368 goto errout;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001369 __perf_counter_init_context(ctx, task);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001370 get_ctx(ctx);
1371 if (cmpxchg(&task->perf_counter_ctxp, NULL, ctx)) {
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001372 /*
1373 * We raced with some other task; use
1374 * the context they set.
1375 */
1376 kfree(ctx);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001377 goto retry_lock;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001378 }
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001379 get_task_struct(task);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001380 }
1381
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001382 put_task_struct(task);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001383 return ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001384
1385 errout:
1386 put_task_struct(task);
1387 return ERR_PTR(err);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001388}
1389
Peter Zijlstra592903c2009-03-13 12:21:36 +01001390static void free_counter_rcu(struct rcu_head *head)
1391{
1392 struct perf_counter *counter;
1393
1394 counter = container_of(head, struct perf_counter, rcu_head);
1395 kfree(counter);
1396}
1397
Peter Zijlstra925d5192009-03-30 19:07:02 +02001398static void perf_pending_sync(struct perf_counter *counter);
1399
Peter Zijlstraf1600952009-03-19 20:26:16 +01001400static void free_counter(struct perf_counter *counter)
1401{
Peter Zijlstra925d5192009-03-30 19:07:02 +02001402 perf_pending_sync(counter);
1403
Peter Zijlstra7fc23a52009-05-08 18:52:21 +02001404 atomic_dec(&nr_counters);
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02001405 if (counter->hw_event.mmap)
1406 atomic_dec(&nr_mmap_tracking);
1407 if (counter->hw_event.munmap)
1408 atomic_dec(&nr_munmap_tracking);
1409 if (counter->hw_event.comm)
1410 atomic_dec(&nr_comm_tracking);
1411
Peter Zijlstrae077df42009-03-19 20:26:17 +01001412 if (counter->destroy)
1413 counter->destroy(counter);
1414
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001415 put_ctx(counter->ctx);
Peter Zijlstraf1600952009-03-19 20:26:16 +01001416 call_rcu(&counter->rcu_head, free_counter_rcu);
1417}
1418
Thomas Gleixner0793a612008-12-04 20:12:29 +01001419/*
1420 * Called when the last reference to the file is gone.
1421 */
1422static int perf_release(struct inode *inode, struct file *file)
1423{
1424 struct perf_counter *counter = file->private_data;
1425 struct perf_counter_context *ctx = counter->ctx;
1426
1427 file->private_data = NULL;
1428
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001429 WARN_ON_ONCE(ctx->parent_ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001430 mutex_lock(&ctx->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01001431 perf_counter_remove_from_context(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001432 mutex_unlock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001433
Peter Zijlstra082ff5a2009-05-23 18:29:00 +02001434 mutex_lock(&counter->owner->perf_counter_mutex);
1435 list_del_init(&counter->owner_entry);
1436 mutex_unlock(&counter->owner->perf_counter_mutex);
1437 put_task_struct(counter->owner);
1438
Peter Zijlstraf1600952009-03-19 20:26:16 +01001439 free_counter(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001440
1441 return 0;
1442}
1443
1444/*
1445 * Read the performance counter - simple non blocking version for now
1446 */
1447static ssize_t
1448perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
1449{
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001450 u64 values[3];
1451 int n;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001452
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001453 /*
1454 * Return end-of-file for a read on a counter that is in
1455 * error state (i.e. because it was pinned but it couldn't be
1456 * scheduled on to the CPU at some point).
1457 */
1458 if (counter->state == PERF_COUNTER_STATE_ERROR)
1459 return 0;
1460
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001461 WARN_ON_ONCE(counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001462 mutex_lock(&counter->child_mutex);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001463 values[0] = perf_counter_read(counter);
1464 n = 1;
1465 if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1466 values[n++] = counter->total_time_enabled +
1467 atomic64_read(&counter->child_total_time_enabled);
1468 if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1469 values[n++] = counter->total_time_running +
1470 atomic64_read(&counter->child_total_time_running);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001471 mutex_unlock(&counter->child_mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001472
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001473 if (count < n * sizeof(u64))
1474 return -EINVAL;
1475 count = n * sizeof(u64);
1476
1477 if (copy_to_user(buf, values, count))
1478 return -EFAULT;
1479
1480 return count;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001481}
1482
1483static ssize_t
Thomas Gleixner0793a612008-12-04 20:12:29 +01001484perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
1485{
1486 struct perf_counter *counter = file->private_data;
1487
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001488 return perf_read_hw(counter, buf, count);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001489}
1490
1491static unsigned int perf_poll(struct file *file, poll_table *wait)
1492{
1493 struct perf_counter *counter = file->private_data;
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001494 struct perf_mmap_data *data;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001495 unsigned int events = POLL_HUP;
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001496
1497 rcu_read_lock();
1498 data = rcu_dereference(counter->data);
1499 if (data)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001500 events = atomic_xchg(&data->poll, 0);
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001501 rcu_read_unlock();
Thomas Gleixner0793a612008-12-04 20:12:29 +01001502
1503 poll_wait(file, &counter->waitq, wait);
1504
Thomas Gleixner0793a612008-12-04 20:12:29 +01001505 return events;
1506}
1507
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001508static void perf_counter_reset(struct perf_counter *counter)
1509{
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001510 (void)perf_counter_read(counter);
Paul Mackerras615a3f12009-05-11 15:50:21 +10001511 atomic64_set(&counter->count, 0);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001512 perf_counter_update_userpage(counter);
1513}
1514
1515static void perf_counter_for_each_sibling(struct perf_counter *counter,
1516 void (*func)(struct perf_counter *))
1517{
1518 struct perf_counter_context *ctx = counter->ctx;
1519 struct perf_counter *sibling;
1520
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001521 WARN_ON_ONCE(ctx->parent_ctx);
Peter Zijlstra682076a2009-05-23 18:28:57 +02001522 mutex_lock(&ctx->mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001523 counter = counter->group_leader;
1524
1525 func(counter);
1526 list_for_each_entry(sibling, &counter->sibling_list, list_entry)
1527 func(sibling);
Peter Zijlstra682076a2009-05-23 18:28:57 +02001528 mutex_unlock(&ctx->mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001529}
1530
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001531/*
1532 * Holding the top-level counter's child_mutex means that any
1533 * descendant process that has inherited this counter will block
1534 * in sync_child_counter if it goes to exit, thus satisfying the
1535 * task existence requirements of perf_counter_enable/disable.
1536 */
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001537static void perf_counter_for_each_child(struct perf_counter *counter,
1538 void (*func)(struct perf_counter *))
1539{
1540 struct perf_counter *child;
1541
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001542 WARN_ON_ONCE(counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001543 mutex_lock(&counter->child_mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001544 func(counter);
1545 list_for_each_entry(child, &counter->child_list, child_list)
1546 func(child);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001547 mutex_unlock(&counter->child_mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001548}
1549
1550static void perf_counter_for_each(struct perf_counter *counter,
1551 void (*func)(struct perf_counter *))
1552{
1553 struct perf_counter *child;
1554
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001555 WARN_ON_ONCE(counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001556 mutex_lock(&counter->child_mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001557 perf_counter_for_each_sibling(counter, func);
1558 list_for_each_entry(child, &counter->child_list, child_list)
1559 perf_counter_for_each_sibling(child, func);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001560 mutex_unlock(&counter->child_mutex);
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001561}
1562
Paul Mackerrasd859e292009-01-17 18:10:22 +11001563static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1564{
1565 struct perf_counter *counter = file->private_data;
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001566 void (*func)(struct perf_counter *);
1567 u32 flags = arg;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001568
1569 switch (cmd) {
1570 case PERF_COUNTER_IOC_ENABLE:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001571 func = perf_counter_enable;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001572 break;
1573 case PERF_COUNTER_IOC_DISABLE:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001574 func = perf_counter_disable;
Peter Zijlstra79f14642009-04-06 11:45:07 +02001575 break;
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001576 case PERF_COUNTER_IOC_RESET:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001577 func = perf_counter_reset;
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001578 break;
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001579
1580 case PERF_COUNTER_IOC_REFRESH:
1581 return perf_counter_refresh(counter, arg);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001582 default:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001583 return -ENOTTY;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001584 }
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001585
1586 if (flags & PERF_IOC_FLAG_GROUP)
1587 perf_counter_for_each(counter, func);
1588 else
1589 perf_counter_for_each_child(counter, func);
1590
1591 return 0;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001592}
1593
Peter Zijlstra771d7cd2009-05-25 14:45:26 +02001594int perf_counter_task_enable(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_enable);
1601 mutex_unlock(&current->perf_counter_mutex);
1602
1603 return 0;
1604}
1605
1606int perf_counter_task_disable(void)
1607{
1608 struct perf_counter *counter;
1609
1610 mutex_lock(&current->perf_counter_mutex);
1611 list_for_each_entry(counter, &current->perf_counter_list, owner_entry)
1612 perf_counter_for_each_child(counter, perf_counter_disable);
1613 mutex_unlock(&current->perf_counter_mutex);
1614
1615 return 0;
1616}
1617
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001618/*
1619 * Callers need to ensure there can be no nesting of this function, otherwise
1620 * the seqlock logic goes bad. We can not serialize this because the arch
1621 * code calls this from NMI context.
1622 */
1623void perf_counter_update_userpage(struct perf_counter *counter)
Paul Mackerras37d81822009-03-23 18:22:08 +01001624{
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001625 struct perf_mmap_data *data;
1626 struct perf_counter_mmap_page *userpg;
1627
1628 rcu_read_lock();
1629 data = rcu_dereference(counter->data);
1630 if (!data)
1631 goto unlock;
1632
1633 userpg = data->user_page;
Paul Mackerras37d81822009-03-23 18:22:08 +01001634
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001635 /*
1636 * Disable preemption so as to not let the corresponding user-space
1637 * spin too long if we get preempted.
1638 */
1639 preempt_disable();
Paul Mackerras37d81822009-03-23 18:22:08 +01001640 ++userpg->lock;
Peter Zijlstra92f22a32009-04-02 11:12:04 +02001641 barrier();
Paul Mackerras37d81822009-03-23 18:22:08 +01001642 userpg->index = counter->hw.idx;
1643 userpg->offset = atomic64_read(&counter->count);
1644 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
1645 userpg->offset -= atomic64_read(&counter->hw.prev_count);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001646
Peter Zijlstra92f22a32009-04-02 11:12:04 +02001647 barrier();
Paul Mackerras37d81822009-03-23 18:22:08 +01001648 ++userpg->lock;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001649 preempt_enable();
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001650unlock:
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001651 rcu_read_unlock();
Paul Mackerras37d81822009-03-23 18:22:08 +01001652}
1653
1654static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1655{
1656 struct perf_counter *counter = vma->vm_file->private_data;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001657 struct perf_mmap_data *data;
1658 int ret = VM_FAULT_SIGBUS;
Paul Mackerras37d81822009-03-23 18:22:08 +01001659
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001660 rcu_read_lock();
1661 data = rcu_dereference(counter->data);
1662 if (!data)
1663 goto unlock;
Paul Mackerras37d81822009-03-23 18:22:08 +01001664
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001665 if (vmf->pgoff == 0) {
1666 vmf->page = virt_to_page(data->user_page);
1667 } else {
1668 int nr = vmf->pgoff - 1;
1669
1670 if ((unsigned)nr > data->nr_pages)
1671 goto unlock;
1672
1673 vmf->page = virt_to_page(data->data_pages[nr]);
1674 }
Paul Mackerras37d81822009-03-23 18:22:08 +01001675 get_page(vmf->page);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001676 ret = 0;
1677unlock:
1678 rcu_read_unlock();
1679
1680 return ret;
1681}
1682
1683static int perf_mmap_data_alloc(struct perf_counter *counter, int nr_pages)
1684{
1685 struct perf_mmap_data *data;
1686 unsigned long size;
1687 int i;
1688
1689 WARN_ON(atomic_read(&counter->mmap_count));
1690
1691 size = sizeof(struct perf_mmap_data);
1692 size += nr_pages * sizeof(void *);
1693
1694 data = kzalloc(size, GFP_KERNEL);
1695 if (!data)
1696 goto fail;
1697
1698 data->user_page = (void *)get_zeroed_page(GFP_KERNEL);
1699 if (!data->user_page)
1700 goto fail_user_page;
1701
1702 for (i = 0; i < nr_pages; i++) {
1703 data->data_pages[i] = (void *)get_zeroed_page(GFP_KERNEL);
1704 if (!data->data_pages[i])
1705 goto fail_data_pages;
1706 }
1707
1708 data->nr_pages = nr_pages;
Peter Zijlstra22c15582009-05-05 17:50:25 +02001709 atomic_set(&data->lock, -1);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001710
1711 rcu_assign_pointer(counter->data, data);
1712
Paul Mackerras37d81822009-03-23 18:22:08 +01001713 return 0;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001714
1715fail_data_pages:
1716 for (i--; i >= 0; i--)
1717 free_page((unsigned long)data->data_pages[i]);
1718
1719 free_page((unsigned long)data->user_page);
1720
1721fail_user_page:
1722 kfree(data);
1723
1724fail:
1725 return -ENOMEM;
1726}
1727
1728static void __perf_mmap_data_free(struct rcu_head *rcu_head)
1729{
1730 struct perf_mmap_data *data = container_of(rcu_head,
1731 struct perf_mmap_data, rcu_head);
1732 int i;
1733
1734 free_page((unsigned long)data->user_page);
1735 for (i = 0; i < data->nr_pages; i++)
1736 free_page((unsigned long)data->data_pages[i]);
1737 kfree(data);
1738}
1739
1740static void perf_mmap_data_free(struct perf_counter *counter)
1741{
1742 struct perf_mmap_data *data = counter->data;
1743
1744 WARN_ON(atomic_read(&counter->mmap_count));
1745
1746 rcu_assign_pointer(counter->data, NULL);
1747 call_rcu(&data->rcu_head, __perf_mmap_data_free);
1748}
1749
1750static void perf_mmap_open(struct vm_area_struct *vma)
1751{
1752 struct perf_counter *counter = vma->vm_file->private_data;
1753
1754 atomic_inc(&counter->mmap_count);
1755}
1756
1757static void perf_mmap_close(struct vm_area_struct *vma)
1758{
1759 struct perf_counter *counter = vma->vm_file->private_data;
1760
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001761 WARN_ON_ONCE(counter->ctx->parent_ctx);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001762 if (atomic_dec_and_mutex_lock(&counter->mmap_count,
1763 &counter->mmap_mutex)) {
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001764 struct user_struct *user = current_user();
1765
1766 atomic_long_sub(counter->data->nr_pages + 1, &user->locked_vm);
Peter Zijlstrac5078f72009-05-05 17:50:24 +02001767 vma->vm_mm->locked_vm -= counter->data->nr_locked;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001768 perf_mmap_data_free(counter);
1769 mutex_unlock(&counter->mmap_mutex);
1770 }
Paul Mackerras37d81822009-03-23 18:22:08 +01001771}
1772
1773static struct vm_operations_struct perf_mmap_vmops = {
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001774 .open = perf_mmap_open,
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001775 .close = perf_mmap_close,
Paul Mackerras37d81822009-03-23 18:22:08 +01001776 .fault = perf_mmap_fault,
1777};
1778
1779static int perf_mmap(struct file *file, struct vm_area_struct *vma)
1780{
1781 struct perf_counter *counter = file->private_data;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001782 struct user_struct *user = current_user();
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001783 unsigned long vma_size;
1784 unsigned long nr_pages;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001785 unsigned long user_locked, user_lock_limit;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001786 unsigned long locked, lock_limit;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001787 long user_extra, extra;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001788 int ret = 0;
Paul Mackerras37d81822009-03-23 18:22:08 +01001789
1790 if (!(vma->vm_flags & VM_SHARED) || (vma->vm_flags & VM_WRITE))
1791 return -EINVAL;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001792
1793 vma_size = vma->vm_end - vma->vm_start;
1794 nr_pages = (vma_size / PAGE_SIZE) - 1;
1795
Peter Zijlstra7730d862009-03-25 12:48:31 +01001796 /*
1797 * If we have data pages ensure they're a power-of-two number, so we
1798 * can do bitmasks instead of modulo.
1799 */
1800 if (nr_pages != 0 && !is_power_of_2(nr_pages))
Paul Mackerras37d81822009-03-23 18:22:08 +01001801 return -EINVAL;
1802
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001803 if (vma_size != PAGE_SIZE * (1 + nr_pages))
Paul Mackerras37d81822009-03-23 18:22:08 +01001804 return -EINVAL;
1805
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001806 if (vma->vm_pgoff != 0)
1807 return -EINVAL;
Paul Mackerras37d81822009-03-23 18:22:08 +01001808
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001809 WARN_ON_ONCE(counter->ctx->parent_ctx);
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001810 mutex_lock(&counter->mmap_mutex);
1811 if (atomic_inc_not_zero(&counter->mmap_count)) {
1812 if (nr_pages != counter->data->nr_pages)
1813 ret = -EINVAL;
1814 goto unlock;
1815 }
1816
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001817 user_extra = nr_pages + 1;
1818 user_lock_limit = sysctl_perf_counter_mlock >> (PAGE_SHIFT - 10);
Ingo Molnara3862d32009-05-24 09:02:37 +02001819
1820 /*
1821 * Increase the limit linearly with more CPUs:
1822 */
1823 user_lock_limit *= num_online_cpus();
1824
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001825 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
Peter Zijlstrac5078f72009-05-05 17:50:24 +02001826
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001827 extra = 0;
1828 if (user_locked > user_lock_limit)
1829 extra = user_locked - user_lock_limit;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001830
1831 lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
1832 lock_limit >>= PAGE_SHIFT;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001833 locked = vma->vm_mm->locked_vm + extra;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001834
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001835 if ((locked > lock_limit) && !capable(CAP_IPC_LOCK)) {
1836 ret = -EPERM;
1837 goto unlock;
1838 }
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001839
1840 WARN_ON(counter->data);
1841 ret = perf_mmap_data_alloc(counter, nr_pages);
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001842 if (ret)
1843 goto unlock;
1844
1845 atomic_set(&counter->mmap_count, 1);
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001846 atomic_long_add(user_extra, &user->locked_vm);
Peter Zijlstrac5078f72009-05-05 17:50:24 +02001847 vma->vm_mm->locked_vm += extra;
1848 counter->data->nr_locked = extra;
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001849unlock:
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001850 mutex_unlock(&counter->mmap_mutex);
Paul Mackerras37d81822009-03-23 18:22:08 +01001851
1852 vma->vm_flags &= ~VM_MAYWRITE;
1853 vma->vm_flags |= VM_RESERVED;
1854 vma->vm_ops = &perf_mmap_vmops;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001855
1856 return ret;
Paul Mackerras37d81822009-03-23 18:22:08 +01001857}
1858
Peter Zijlstra3c446b3d2009-04-06 11:45:01 +02001859static int perf_fasync(int fd, struct file *filp, int on)
1860{
1861 struct perf_counter *counter = filp->private_data;
1862 struct inode *inode = filp->f_path.dentry->d_inode;
1863 int retval;
1864
1865 mutex_lock(&inode->i_mutex);
1866 retval = fasync_helper(fd, filp, on, &counter->fasync);
1867 mutex_unlock(&inode->i_mutex);
1868
1869 if (retval < 0)
1870 return retval;
1871
1872 return 0;
1873}
1874
Thomas Gleixner0793a612008-12-04 20:12:29 +01001875static const struct file_operations perf_fops = {
1876 .release = perf_release,
1877 .read = perf_read,
1878 .poll = perf_poll,
Paul Mackerrasd859e292009-01-17 18:10:22 +11001879 .unlocked_ioctl = perf_ioctl,
1880 .compat_ioctl = perf_ioctl,
Paul Mackerras37d81822009-03-23 18:22:08 +01001881 .mmap = perf_mmap,
Peter Zijlstra3c446b3d2009-04-06 11:45:01 +02001882 .fasync = perf_fasync,
Thomas Gleixner0793a612008-12-04 20:12:29 +01001883};
1884
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001885/*
Peter Zijlstra925d5192009-03-30 19:07:02 +02001886 * Perf counter wakeup
1887 *
1888 * If there's data, ensure we set the poll() state and publish everything
1889 * to user-space before waking everybody up.
1890 */
1891
1892void perf_counter_wakeup(struct perf_counter *counter)
1893{
Peter Zijlstra925d5192009-03-30 19:07:02 +02001894 wake_up_all(&counter->waitq);
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02001895
1896 if (counter->pending_kill) {
1897 kill_fasync(&counter->fasync, SIGIO, counter->pending_kill);
1898 counter->pending_kill = 0;
1899 }
Peter Zijlstra925d5192009-03-30 19:07:02 +02001900}
1901
1902/*
1903 * Pending wakeups
1904 *
1905 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
1906 *
1907 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
1908 * single linked list and use cmpxchg() to add entries lockless.
1909 */
1910
Peter Zijlstra79f14642009-04-06 11:45:07 +02001911static void perf_pending_counter(struct perf_pending_entry *entry)
1912{
1913 struct perf_counter *counter = container_of(entry,
1914 struct perf_counter, pending);
1915
1916 if (counter->pending_disable) {
1917 counter->pending_disable = 0;
1918 perf_counter_disable(counter);
1919 }
1920
1921 if (counter->pending_wakeup) {
1922 counter->pending_wakeup = 0;
1923 perf_counter_wakeup(counter);
1924 }
1925}
1926
Peter Zijlstra671dec52009-04-06 11:45:02 +02001927#define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
Peter Zijlstra925d5192009-03-30 19:07:02 +02001928
Peter Zijlstra671dec52009-04-06 11:45:02 +02001929static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = {
Peter Zijlstra925d5192009-03-30 19:07:02 +02001930 PENDING_TAIL,
1931};
1932
Peter Zijlstra671dec52009-04-06 11:45:02 +02001933static void perf_pending_queue(struct perf_pending_entry *entry,
1934 void (*func)(struct perf_pending_entry *))
Peter Zijlstra925d5192009-03-30 19:07:02 +02001935{
Peter Zijlstra671dec52009-04-06 11:45:02 +02001936 struct perf_pending_entry **head;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001937
Peter Zijlstra671dec52009-04-06 11:45:02 +02001938 if (cmpxchg(&entry->next, NULL, PENDING_TAIL) != NULL)
Peter Zijlstra925d5192009-03-30 19:07:02 +02001939 return;
1940
Peter Zijlstra671dec52009-04-06 11:45:02 +02001941 entry->func = func;
1942
1943 head = &get_cpu_var(perf_pending_head);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001944
1945 do {
Peter Zijlstra671dec52009-04-06 11:45:02 +02001946 entry->next = *head;
1947 } while (cmpxchg(head, entry->next, entry) != entry->next);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001948
1949 set_perf_counter_pending();
1950
Peter Zijlstra671dec52009-04-06 11:45:02 +02001951 put_cpu_var(perf_pending_head);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001952}
1953
1954static int __perf_pending_run(void)
1955{
Peter Zijlstra671dec52009-04-06 11:45:02 +02001956 struct perf_pending_entry *list;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001957 int nr = 0;
1958
Peter Zijlstra671dec52009-04-06 11:45:02 +02001959 list = xchg(&__get_cpu_var(perf_pending_head), PENDING_TAIL);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001960 while (list != PENDING_TAIL) {
Peter Zijlstra671dec52009-04-06 11:45:02 +02001961 void (*func)(struct perf_pending_entry *);
1962 struct perf_pending_entry *entry = list;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001963
1964 list = list->next;
1965
Peter Zijlstra671dec52009-04-06 11:45:02 +02001966 func = entry->func;
1967 entry->next = NULL;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001968 /*
1969 * Ensure we observe the unqueue before we issue the wakeup,
1970 * so that we won't be waiting forever.
1971 * -- see perf_not_pending().
1972 */
1973 smp_wmb();
1974
Peter Zijlstra671dec52009-04-06 11:45:02 +02001975 func(entry);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001976 nr++;
1977 }
1978
1979 return nr;
1980}
1981
1982static inline int perf_not_pending(struct perf_counter *counter)
1983{
1984 /*
1985 * If we flush on whatever cpu we run, there is a chance we don't
1986 * need to wait.
1987 */
1988 get_cpu();
1989 __perf_pending_run();
1990 put_cpu();
1991
1992 /*
1993 * Ensure we see the proper queue state before going to sleep
1994 * so that we do not miss the wakeup. -- see perf_pending_handle()
1995 */
1996 smp_rmb();
Peter Zijlstra671dec52009-04-06 11:45:02 +02001997 return counter->pending.next == NULL;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001998}
1999
2000static void perf_pending_sync(struct perf_counter *counter)
2001{
2002 wait_event(counter->waitq, perf_not_pending(counter));
2003}
2004
2005void perf_counter_do_pending(void)
2006{
2007 __perf_pending_run();
2008}
2009
2010/*
Peter Zijlstra394ee072009-03-30 19:07:14 +02002011 * Callchain support -- arch specific
2012 */
2013
Peter Zijlstra9c03d882009-04-06 11:45:00 +02002014__weak struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
Peter Zijlstra394ee072009-03-30 19:07:14 +02002015{
2016 return NULL;
2017}
2018
2019/*
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002020 * Output
2021 */
2022
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002023struct perf_output_handle {
2024 struct perf_counter *counter;
2025 struct perf_mmap_data *data;
2026 unsigned int offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01002027 unsigned int head;
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002028 int nmi;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002029 int overflow;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002030 int locked;
2031 unsigned long flags;
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002032};
2033
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002034static void perf_output_wakeup(struct perf_output_handle *handle)
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002035{
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002036 atomic_set(&handle->data->poll, POLL_IN);
2037
Peter Zijlstra671dec52009-04-06 11:45:02 +02002038 if (handle->nmi) {
Peter Zijlstra79f14642009-04-06 11:45:07 +02002039 handle->counter->pending_wakeup = 1;
Peter Zijlstra671dec52009-04-06 11:45:02 +02002040 perf_pending_queue(&handle->counter->pending,
Peter Zijlstra79f14642009-04-06 11:45:07 +02002041 perf_pending_counter);
Peter Zijlstra671dec52009-04-06 11:45:02 +02002042 } else
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002043 perf_counter_wakeup(handle->counter);
2044}
2045
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002046/*
2047 * Curious locking construct.
2048 *
2049 * We need to ensure a later event doesn't publish a head when a former
2050 * event isn't done writing. However since we need to deal with NMIs we
2051 * cannot fully serialize things.
2052 *
2053 * What we do is serialize between CPUs so we only have to deal with NMI
2054 * nesting on a single CPU.
2055 *
2056 * We only publish the head (and generate a wakeup) when the outer-most
2057 * event completes.
2058 */
2059static void perf_output_lock(struct perf_output_handle *handle)
2060{
2061 struct perf_mmap_data *data = handle->data;
2062 int cpu;
2063
2064 handle->locked = 0;
2065
2066 local_irq_save(handle->flags);
2067 cpu = smp_processor_id();
2068
2069 if (in_nmi() && atomic_read(&data->lock) == cpu)
2070 return;
2071
Peter Zijlstra22c15582009-05-05 17:50:25 +02002072 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002073 cpu_relax();
2074
2075 handle->locked = 1;
2076}
2077
2078static void perf_output_unlock(struct perf_output_handle *handle)
2079{
2080 struct perf_mmap_data *data = handle->data;
2081 int head, cpu;
2082
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002083 data->done_head = data->head;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002084
2085 if (!handle->locked)
2086 goto out;
2087
2088again:
2089 /*
2090 * The xchg implies a full barrier that ensures all writes are done
2091 * before we publish the new head, matched by a rmb() in userspace when
2092 * reading this position.
2093 */
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002094 while ((head = atomic_xchg(&data->done_head, 0)))
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002095 data->user_page->data_head = head;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002096
2097 /*
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002098 * NMI can happen here, which means we can miss a done_head update.
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002099 */
2100
Peter Zijlstra22c15582009-05-05 17:50:25 +02002101 cpu = atomic_xchg(&data->lock, -1);
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002102 WARN_ON_ONCE(cpu != smp_processor_id());
2103
2104 /*
2105 * Therefore we have to validate we did not indeed do so.
2106 */
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002107 if (unlikely(atomic_read(&data->done_head))) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002108 /*
2109 * Since we had it locked, we can lock it again.
2110 */
Peter Zijlstra22c15582009-05-05 17:50:25 +02002111 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002112 cpu_relax();
2113
2114 goto again;
2115 }
2116
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002117 if (atomic_xchg(&data->wakeup, 0))
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002118 perf_output_wakeup(handle);
2119out:
2120 local_irq_restore(handle->flags);
2121}
2122
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002123static int perf_output_begin(struct perf_output_handle *handle,
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002124 struct perf_counter *counter, unsigned int size,
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002125 int nmi, int overflow)
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002126{
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002127 struct perf_mmap_data *data;
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002128 unsigned int offset, head;
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002129
Peter Zijlstra2023b352009-05-05 17:50:26 +02002130 /*
2131 * For inherited counters we send all the output towards the parent.
2132 */
2133 if (counter->parent)
2134 counter = counter->parent;
2135
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002136 rcu_read_lock();
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002137 data = rcu_dereference(counter->data);
2138 if (!data)
2139 goto out;
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002140
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002141 handle->data = data;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002142 handle->counter = counter;
2143 handle->nmi = nmi;
2144 handle->overflow = overflow;
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002145
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002146 if (!data->nr_pages)
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002147 goto fail;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002148
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002149 perf_output_lock(handle);
2150
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002151 do {
2152 offset = head = atomic_read(&data->head);
Peter Zijlstrac7138f32009-03-24 13:18:16 +01002153 head += size;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002154 } while (atomic_cmpxchg(&data->head, offset, head) != offset);
2155
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002156 handle->offset = offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01002157 handle->head = head;
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002158
2159 if ((offset >> PAGE_SHIFT) != (head >> PAGE_SHIFT))
2160 atomic_set(&data->wakeup, 1);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002161
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002162 return 0;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002163
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002164fail:
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002165 perf_output_wakeup(handle);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002166out:
2167 rcu_read_unlock();
2168
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002169 return -ENOSPC;
2170}
2171
2172static void perf_output_copy(struct perf_output_handle *handle,
2173 void *buf, unsigned int len)
2174{
2175 unsigned int pages_mask;
2176 unsigned int offset;
2177 unsigned int size;
2178 void **pages;
2179
2180 offset = handle->offset;
2181 pages_mask = handle->data->nr_pages - 1;
2182 pages = handle->data->data_pages;
2183
2184 do {
2185 unsigned int page_offset;
2186 int nr;
2187
2188 nr = (offset >> PAGE_SHIFT) & pages_mask;
2189 page_offset = offset & (PAGE_SIZE - 1);
2190 size = min_t(unsigned int, PAGE_SIZE - page_offset, len);
2191
2192 memcpy(pages[nr] + page_offset, buf, size);
2193
2194 len -= size;
2195 buf += size;
2196 offset += size;
2197 } while (len);
2198
2199 handle->offset = offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01002200
Peter Zijlstra53020fe2009-05-13 21:26:19 +02002201 /*
2202 * Check we didn't copy past our reservation window, taking the
2203 * possible unsigned int wrap into account.
2204 */
2205 WARN_ON_ONCE(((int)(handle->head - handle->offset)) < 0);
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002206}
2207
Peter Zijlstra5c148192009-03-25 12:30:23 +01002208#define perf_output_put(handle, x) \
2209 perf_output_copy((handle), &(x), sizeof(x))
2210
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002211static void perf_output_end(struct perf_output_handle *handle)
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002212{
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002213 struct perf_counter *counter = handle->counter;
2214 struct perf_mmap_data *data = handle->data;
2215
2216 int wakeup_events = counter->hw_event.wakeup_events;
Peter Zijlstrac4578102009-04-02 11:12:01 +02002217
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002218 if (handle->overflow && wakeup_events) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002219 int events = atomic_inc_return(&data->events);
Peter Zijlstrac4578102009-04-02 11:12:01 +02002220 if (events >= wakeup_events) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002221 atomic_sub(wakeup_events, &data->events);
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002222 atomic_set(&data->wakeup, 1);
Peter Zijlstrac4578102009-04-02 11:12:01 +02002223 }
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002224 }
2225
2226 perf_output_unlock(handle);
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002227 rcu_read_unlock();
2228}
2229
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002230static void perf_counter_output(struct perf_counter *counter,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002231 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002232{
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002233 int ret;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002234 u64 record_type = counter->hw_event.record_type;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002235 struct perf_output_handle handle;
2236 struct perf_event_header header;
2237 u64 ip;
Peter Zijlstra5c148192009-03-25 12:30:23 +01002238 struct {
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002239 u32 pid, tid;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002240 } tid_entry;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002241 struct {
2242 u64 event;
2243 u64 counter;
2244 } group_entry;
Peter Zijlstra394ee072009-03-30 19:07:14 +02002245 struct perf_callchain_entry *callchain = NULL;
2246 int callchain_size = 0;
Peter Zijlstra339f7c92009-04-06 11:45:06 +02002247 u64 time;
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002248 struct {
2249 u32 cpu, reserved;
2250 } cpu_entry;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002251
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02002252 header.type = 0;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002253 header.size = sizeof(header);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002254
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02002255 header.misc = PERF_EVENT_MISC_OVERFLOW;
Paul Mackerras9d23a902009-05-14 21:48:08 +10002256 header.misc |= perf_misc_flags(regs);
Peter Zijlstra6fab0192009-04-08 15:01:26 +02002257
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002258 if (record_type & PERF_RECORD_IP) {
Paul Mackerras9d23a902009-05-14 21:48:08 +10002259 ip = perf_instruction_pointer(regs);
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02002260 header.type |= PERF_RECORD_IP;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002261 header.size += sizeof(ip);
2262 }
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002263
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002264 if (record_type & PERF_RECORD_TID) {
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002265 /* namespace issues */
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002266 tid_entry.pid = current->group_leader->pid;
2267 tid_entry.tid = current->pid;
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002268
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02002269 header.type |= PERF_RECORD_TID;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002270 header.size += sizeof(tid_entry);
2271 }
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002272
Peter Zijlstra4d855452009-04-08 15:01:32 +02002273 if (record_type & PERF_RECORD_TIME) {
2274 /*
2275 * Maybe do better on x86 and provide cpu_clock_nmi()
2276 */
2277 time = sched_clock();
2278
2279 header.type |= PERF_RECORD_TIME;
2280 header.size += sizeof(u64);
2281 }
2282
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002283 if (record_type & PERF_RECORD_ADDR) {
2284 header.type |= PERF_RECORD_ADDR;
2285 header.size += sizeof(u64);
2286 }
2287
Peter Zijlstraa85f61a2009-05-08 18:52:23 +02002288 if (record_type & PERF_RECORD_CONFIG) {
2289 header.type |= PERF_RECORD_CONFIG;
2290 header.size += sizeof(u64);
2291 }
2292
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002293 if (record_type & PERF_RECORD_CPU) {
2294 header.type |= PERF_RECORD_CPU;
2295 header.size += sizeof(cpu_entry);
2296
2297 cpu_entry.cpu = raw_smp_processor_id();
2298 }
2299
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002300 if (record_type & PERF_RECORD_GROUP) {
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02002301 header.type |= PERF_RECORD_GROUP;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002302 header.size += sizeof(u64) +
2303 counter->nr_siblings * sizeof(group_entry);
2304 }
2305
2306 if (record_type & PERF_RECORD_CALLCHAIN) {
Peter Zijlstra394ee072009-03-30 19:07:14 +02002307 callchain = perf_callchain(regs);
2308
2309 if (callchain) {
Peter Zijlstra9c03d882009-04-06 11:45:00 +02002310 callchain_size = (1 + callchain->nr) * sizeof(u64);
Peter Zijlstra394ee072009-03-30 19:07:14 +02002311
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02002312 header.type |= PERF_RECORD_CALLCHAIN;
Peter Zijlstra394ee072009-03-30 19:07:14 +02002313 header.size += callchain_size;
2314 }
2315 }
2316
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002317 ret = perf_output_begin(&handle, counter, header.size, nmi, 1);
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002318 if (ret)
2319 return;
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002320
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002321 perf_output_put(&handle, header);
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002322
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002323 if (record_type & PERF_RECORD_IP)
2324 perf_output_put(&handle, ip);
2325
2326 if (record_type & PERF_RECORD_TID)
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002327 perf_output_put(&handle, tid_entry);
2328
Peter Zijlstra4d855452009-04-08 15:01:32 +02002329 if (record_type & PERF_RECORD_TIME)
2330 perf_output_put(&handle, time);
2331
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002332 if (record_type & PERF_RECORD_ADDR)
2333 perf_output_put(&handle, addr);
2334
Peter Zijlstraa85f61a2009-05-08 18:52:23 +02002335 if (record_type & PERF_RECORD_CONFIG)
2336 perf_output_put(&handle, counter->hw_event.config);
2337
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002338 if (record_type & PERF_RECORD_CPU)
2339 perf_output_put(&handle, cpu_entry);
2340
Peter Zijlstra2023b352009-05-05 17:50:26 +02002341 /*
2342 * XXX PERF_RECORD_GROUP vs inherited counters seems difficult.
2343 */
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002344 if (record_type & PERF_RECORD_GROUP) {
2345 struct perf_counter *leader, *sub;
2346 u64 nr = counter->nr_siblings;
2347
2348 perf_output_put(&handle, nr);
2349
2350 leader = counter->group_leader;
2351 list_for_each_entry(sub, &leader->sibling_list, list_entry) {
2352 if (sub != counter)
Robert Richter4aeb0b42009-04-29 12:47:03 +02002353 sub->pmu->read(sub);
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002354
2355 group_entry.event = sub->hw_event.config;
2356 group_entry.counter = atomic64_read(&sub->count);
2357
2358 perf_output_put(&handle, group_entry);
2359 }
2360 }
2361
Peter Zijlstra394ee072009-03-30 19:07:14 +02002362 if (callchain)
2363 perf_output_copy(&handle, callchain, callchain_size);
2364
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002365 perf_output_end(&handle);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002366}
2367
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002368/*
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002369 * comm tracking
2370 */
2371
2372struct perf_comm_event {
2373 struct task_struct *task;
2374 char *comm;
2375 int comm_size;
2376
2377 struct {
2378 struct perf_event_header header;
2379
2380 u32 pid;
2381 u32 tid;
2382 } event;
2383};
2384
2385static void perf_counter_comm_output(struct perf_counter *counter,
2386 struct perf_comm_event *comm_event)
2387{
2388 struct perf_output_handle handle;
2389 int size = comm_event->event.header.size;
2390 int ret = perf_output_begin(&handle, counter, size, 0, 0);
2391
2392 if (ret)
2393 return;
2394
2395 perf_output_put(&handle, comm_event->event);
2396 perf_output_copy(&handle, comm_event->comm,
2397 comm_event->comm_size);
2398 perf_output_end(&handle);
2399}
2400
2401static int perf_counter_comm_match(struct perf_counter *counter,
2402 struct perf_comm_event *comm_event)
2403{
2404 if (counter->hw_event.comm &&
2405 comm_event->event.header.type == PERF_EVENT_COMM)
2406 return 1;
2407
2408 return 0;
2409}
2410
2411static void perf_counter_comm_ctx(struct perf_counter_context *ctx,
2412 struct perf_comm_event *comm_event)
2413{
2414 struct perf_counter *counter;
2415
2416 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2417 return;
2418
2419 rcu_read_lock();
2420 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2421 if (perf_counter_comm_match(counter, comm_event))
2422 perf_counter_comm_output(counter, comm_event);
2423 }
2424 rcu_read_unlock();
2425}
2426
2427static void perf_counter_comm_event(struct perf_comm_event *comm_event)
2428{
2429 struct perf_cpu_context *cpuctx;
2430 unsigned int size;
2431 char *comm = comm_event->task->comm;
2432
Ingo Molnar888fcee2009-04-09 09:48:22 +02002433 size = ALIGN(strlen(comm)+1, sizeof(u64));
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002434
2435 comm_event->comm = comm;
2436 comm_event->comm_size = size;
2437
2438 comm_event->event.header.size = sizeof(comm_event->event) + size;
2439
2440 cpuctx = &get_cpu_var(perf_cpu_context);
2441 perf_counter_comm_ctx(&cpuctx->ctx, comm_event);
2442 put_cpu_var(perf_cpu_context);
2443
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10002444 perf_counter_comm_ctx(current->perf_counter_ctxp, comm_event);
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002445}
2446
2447void perf_counter_comm(struct task_struct *task)
2448{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002449 struct perf_comm_event comm_event;
2450
2451 if (!atomic_read(&nr_comm_tracking))
2452 return;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10002453 if (!current->perf_counter_ctxp)
2454 return;
2455
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002456 comm_event = (struct perf_comm_event){
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002457 .task = task,
2458 .event = {
2459 .header = { .type = PERF_EVENT_COMM, },
2460 .pid = task->group_leader->pid,
2461 .tid = task->pid,
2462 },
2463 };
2464
2465 perf_counter_comm_event(&comm_event);
2466}
2467
2468/*
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002469 * mmap tracking
2470 */
2471
2472struct perf_mmap_event {
2473 struct file *file;
2474 char *file_name;
2475 int file_size;
2476
2477 struct {
2478 struct perf_event_header header;
2479
2480 u32 pid;
2481 u32 tid;
2482 u64 start;
2483 u64 len;
2484 u64 pgoff;
2485 } event;
2486};
2487
2488static void perf_counter_mmap_output(struct perf_counter *counter,
2489 struct perf_mmap_event *mmap_event)
2490{
2491 struct perf_output_handle handle;
2492 int size = mmap_event->event.header.size;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002493 int ret = perf_output_begin(&handle, counter, size, 0, 0);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002494
2495 if (ret)
2496 return;
2497
2498 perf_output_put(&handle, mmap_event->event);
2499 perf_output_copy(&handle, mmap_event->file_name,
2500 mmap_event->file_size);
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002501 perf_output_end(&handle);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002502}
2503
2504static int perf_counter_mmap_match(struct perf_counter *counter,
2505 struct perf_mmap_event *mmap_event)
2506{
2507 if (counter->hw_event.mmap &&
2508 mmap_event->event.header.type == PERF_EVENT_MMAP)
2509 return 1;
2510
2511 if (counter->hw_event.munmap &&
2512 mmap_event->event.header.type == PERF_EVENT_MUNMAP)
2513 return 1;
2514
2515 return 0;
2516}
2517
2518static void perf_counter_mmap_ctx(struct perf_counter_context *ctx,
2519 struct perf_mmap_event *mmap_event)
2520{
2521 struct perf_counter *counter;
2522
2523 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2524 return;
2525
2526 rcu_read_lock();
2527 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2528 if (perf_counter_mmap_match(counter, mmap_event))
2529 perf_counter_mmap_output(counter, mmap_event);
2530 }
2531 rcu_read_unlock();
2532}
2533
2534static void perf_counter_mmap_event(struct perf_mmap_event *mmap_event)
2535{
2536 struct perf_cpu_context *cpuctx;
2537 struct file *file = mmap_event->file;
2538 unsigned int size;
2539 char tmp[16];
2540 char *buf = NULL;
2541 char *name;
2542
2543 if (file) {
2544 buf = kzalloc(PATH_MAX, GFP_KERNEL);
2545 if (!buf) {
2546 name = strncpy(tmp, "//enomem", sizeof(tmp));
2547 goto got_name;
2548 }
Peter Zijlstrad3d21c42009-04-09 10:53:46 +02002549 name = d_path(&file->f_path, buf, PATH_MAX);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002550 if (IS_ERR(name)) {
2551 name = strncpy(tmp, "//toolong", sizeof(tmp));
2552 goto got_name;
2553 }
2554 } else {
2555 name = strncpy(tmp, "//anon", sizeof(tmp));
2556 goto got_name;
2557 }
2558
2559got_name:
Ingo Molnar888fcee2009-04-09 09:48:22 +02002560 size = ALIGN(strlen(name)+1, sizeof(u64));
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002561
2562 mmap_event->file_name = name;
2563 mmap_event->file_size = size;
2564
2565 mmap_event->event.header.size = sizeof(mmap_event->event) + size;
2566
2567 cpuctx = &get_cpu_var(perf_cpu_context);
2568 perf_counter_mmap_ctx(&cpuctx->ctx, mmap_event);
2569 put_cpu_var(perf_cpu_context);
2570
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10002571 perf_counter_mmap_ctx(current->perf_counter_ctxp, mmap_event);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002572
2573 kfree(buf);
2574}
2575
2576void perf_counter_mmap(unsigned long addr, unsigned long len,
2577 unsigned long pgoff, struct file *file)
2578{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002579 struct perf_mmap_event mmap_event;
2580
2581 if (!atomic_read(&nr_mmap_tracking))
2582 return;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10002583 if (!current->perf_counter_ctxp)
2584 return;
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002585
2586 mmap_event = (struct perf_mmap_event){
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002587 .file = file,
2588 .event = {
2589 .header = { .type = PERF_EVENT_MMAP, },
2590 .pid = current->group_leader->pid,
2591 .tid = current->pid,
2592 .start = addr,
2593 .len = len,
2594 .pgoff = pgoff,
2595 },
2596 };
2597
2598 perf_counter_mmap_event(&mmap_event);
2599}
2600
2601void perf_counter_munmap(unsigned long addr, unsigned long len,
2602 unsigned long pgoff, struct file *file)
2603{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002604 struct perf_mmap_event mmap_event;
2605
2606 if (!atomic_read(&nr_munmap_tracking))
2607 return;
2608
2609 mmap_event = (struct perf_mmap_event){
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002610 .file = file,
2611 .event = {
2612 .header = { .type = PERF_EVENT_MUNMAP, },
2613 .pid = current->group_leader->pid,
2614 .tid = current->pid,
2615 .start = addr,
2616 .len = len,
2617 .pgoff = pgoff,
2618 },
2619 };
2620
2621 perf_counter_mmap_event(&mmap_event);
2622}
2623
2624/*
Peter Zijlstrae220d2d2009-05-23 18:28:55 +02002625 * Log irq_period changes so that analyzing tools can re-normalize the
2626 * event flow.
Peter Zijlstra26b119b2009-05-20 12:21:20 +02002627 */
2628
2629static void perf_log_period(struct perf_counter *counter, u64 period)
2630{
2631 struct perf_output_handle handle;
2632 int ret;
2633
2634 struct {
2635 struct perf_event_header header;
2636 u64 time;
2637 u64 period;
2638 } freq_event = {
2639 .header = {
2640 .type = PERF_EVENT_PERIOD,
2641 .misc = 0,
2642 .size = sizeof(freq_event),
2643 },
2644 .time = sched_clock(),
2645 .period = period,
2646 };
2647
2648 if (counter->hw.irq_period == period)
2649 return;
2650
2651 ret = perf_output_begin(&handle, counter, sizeof(freq_event), 0, 0);
2652 if (ret)
2653 return;
2654
2655 perf_output_put(&handle, freq_event);
2656 perf_output_end(&handle);
2657}
2658
2659/*
Peter Zijlstraa78ac322009-05-25 17:39:05 +02002660 * IRQ throttle logging
2661 */
2662
2663static void perf_log_throttle(struct perf_counter *counter, int enable)
2664{
2665 struct perf_output_handle handle;
2666 int ret;
2667
2668 struct {
2669 struct perf_event_header header;
2670 u64 time;
2671 } throttle_event = {
2672 .header = {
2673 .type = PERF_EVENT_THROTTLE + 1,
2674 .misc = 0,
2675 .size = sizeof(throttle_event),
2676 },
2677 .time = sched_clock(),
2678 };
2679
Ingo Molnar0127c3e2009-05-25 22:03:26 +02002680 ret = perf_output_begin(&handle, counter, sizeof(throttle_event), 1, 0);
Peter Zijlstraa78ac322009-05-25 17:39:05 +02002681 if (ret)
2682 return;
2683
2684 perf_output_put(&handle, throttle_event);
2685 perf_output_end(&handle);
2686}
2687
2688/*
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002689 * Generic counter overflow handling.
2690 */
2691
2692int perf_counter_overflow(struct perf_counter *counter,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002693 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002694{
Peter Zijlstra79f14642009-04-06 11:45:07 +02002695 int events = atomic_read(&counter->event_limit);
Peter Zijlstraa78ac322009-05-25 17:39:05 +02002696 int throttle = counter->pmu->unthrottle != NULL;
Peter Zijlstra79f14642009-04-06 11:45:07 +02002697 int ret = 0;
2698
Peter Zijlstraa78ac322009-05-25 17:39:05 +02002699 if (!throttle) {
2700 counter->hw.interrupts++;
2701 } else if (counter->hw.interrupts != MAX_INTERRUPTS) {
2702 counter->hw.interrupts++;
2703 if (HZ*counter->hw.interrupts > (u64)sysctl_perf_counter_limit) {
2704 counter->hw.interrupts = MAX_INTERRUPTS;
2705 perf_log_throttle(counter, 0);
2706 ret = 1;
2707 }
2708 }
Peter Zijlstra60db5e02009-05-15 15:19:28 +02002709
Peter Zijlstra2023b352009-05-05 17:50:26 +02002710 /*
2711 * XXX event_limit might not quite work as expected on inherited
2712 * counters
2713 */
2714
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002715 counter->pending_kill = POLL_IN;
Peter Zijlstra79f14642009-04-06 11:45:07 +02002716 if (events && atomic_dec_and_test(&counter->event_limit)) {
2717 ret = 1;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002718 counter->pending_kill = POLL_HUP;
Peter Zijlstra79f14642009-04-06 11:45:07 +02002719 if (nmi) {
2720 counter->pending_disable = 1;
2721 perf_pending_queue(&counter->pending,
2722 perf_pending_counter);
2723 } else
2724 perf_counter_disable(counter);
2725 }
2726
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002727 perf_counter_output(counter, nmi, regs, addr);
Peter Zijlstra79f14642009-04-06 11:45:07 +02002728 return ret;
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002729}
2730
2731/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002732 * Generic software counter infrastructure
2733 */
2734
2735static void perf_swcounter_update(struct perf_counter *counter)
2736{
2737 struct hw_perf_counter *hwc = &counter->hw;
2738 u64 prev, now;
2739 s64 delta;
2740
2741again:
2742 prev = atomic64_read(&hwc->prev_count);
2743 now = atomic64_read(&hwc->count);
2744 if (atomic64_cmpxchg(&hwc->prev_count, prev, now) != prev)
2745 goto again;
2746
2747 delta = now - prev;
2748
2749 atomic64_add(delta, &counter->count);
2750 atomic64_sub(delta, &hwc->period_left);
2751}
2752
2753static void perf_swcounter_set_period(struct perf_counter *counter)
2754{
2755 struct hw_perf_counter *hwc = &counter->hw;
2756 s64 left = atomic64_read(&hwc->period_left);
2757 s64 period = hwc->irq_period;
2758
2759 if (unlikely(left <= -period)) {
2760 left = period;
2761 atomic64_set(&hwc->period_left, left);
2762 }
2763
2764 if (unlikely(left <= 0)) {
2765 left += period;
2766 atomic64_add(period, &hwc->period_left);
2767 }
2768
2769 atomic64_set(&hwc->prev_count, -left);
2770 atomic64_set(&hwc->count, -left);
2771}
2772
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002773static enum hrtimer_restart perf_swcounter_hrtimer(struct hrtimer *hrtimer)
2774{
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002775 enum hrtimer_restart ret = HRTIMER_RESTART;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002776 struct perf_counter *counter;
2777 struct pt_regs *regs;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02002778 u64 period;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002779
2780 counter = container_of(hrtimer, struct perf_counter, hw.hrtimer);
Robert Richter4aeb0b42009-04-29 12:47:03 +02002781 counter->pmu->read(counter);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002782
2783 regs = get_irq_regs();
2784 /*
2785 * In case we exclude kernel IPs or are somehow not in interrupt
2786 * context, provide the next best thing, the user IP.
2787 */
2788 if ((counter->hw_event.exclude_kernel || !regs) &&
2789 !counter->hw_event.exclude_user)
2790 regs = task_pt_regs(current);
2791
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002792 if (regs) {
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002793 if (perf_counter_overflow(counter, 0, regs, 0))
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002794 ret = HRTIMER_NORESTART;
2795 }
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002796
Peter Zijlstra60db5e02009-05-15 15:19:28 +02002797 period = max_t(u64, 10000, counter->hw.irq_period);
2798 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002799
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002800 return ret;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002801}
2802
2803static void perf_swcounter_overflow(struct perf_counter *counter,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002804 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002805{
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002806 perf_swcounter_update(counter);
2807 perf_swcounter_set_period(counter);
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002808 if (perf_counter_overflow(counter, nmi, regs, addr))
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002809 /* soft-disable the counter */
2810 ;
2811
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002812}
2813
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002814static int perf_swcounter_match(struct perf_counter *counter,
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002815 enum perf_event_types type,
2816 u32 event, struct pt_regs *regs)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002817{
2818 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
2819 return 0;
2820
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002821 if (perf_event_raw(&counter->hw_event))
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002822 return 0;
2823
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002824 if (perf_event_type(&counter->hw_event) != type)
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002825 return 0;
2826
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002827 if (perf_event_id(&counter->hw_event) != event)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002828 return 0;
2829
2830 if (counter->hw_event.exclude_user && user_mode(regs))
2831 return 0;
2832
2833 if (counter->hw_event.exclude_kernel && !user_mode(regs))
2834 return 0;
2835
2836 return 1;
2837}
2838
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002839static void perf_swcounter_add(struct perf_counter *counter, u64 nr,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002840 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002841{
2842 int neg = atomic64_add_negative(nr, &counter->hw.count);
2843 if (counter->hw.irq_period && !neg)
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002844 perf_swcounter_overflow(counter, nmi, regs, addr);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002845}
2846
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002847static void perf_swcounter_ctx_event(struct perf_counter_context *ctx,
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002848 enum perf_event_types type, u32 event,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002849 u64 nr, int nmi, struct pt_regs *regs,
2850 u64 addr)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002851{
2852 struct perf_counter *counter;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002853
Peter Zijlstra01ef09d2009-03-19 20:26:11 +01002854 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002855 return;
2856
Peter Zijlstra592903c2009-03-13 12:21:36 +01002857 rcu_read_lock();
2858 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002859 if (perf_swcounter_match(counter, type, event, regs))
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002860 perf_swcounter_add(counter, nr, nmi, regs, addr);
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002861 }
Peter Zijlstra592903c2009-03-13 12:21:36 +01002862 rcu_read_unlock();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002863}
2864
Peter Zijlstra96f6d442009-03-23 18:22:07 +01002865static int *perf_swcounter_recursion_context(struct perf_cpu_context *cpuctx)
2866{
2867 if (in_nmi())
2868 return &cpuctx->recursion[3];
2869
2870 if (in_irq())
2871 return &cpuctx->recursion[2];
2872
2873 if (in_softirq())
2874 return &cpuctx->recursion[1];
2875
2876 return &cpuctx->recursion[0];
2877}
2878
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002879static void __perf_swcounter_event(enum perf_event_types type, u32 event,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002880 u64 nr, int nmi, struct pt_regs *regs,
2881 u64 addr)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002882{
2883 struct perf_cpu_context *cpuctx = &get_cpu_var(perf_cpu_context);
Peter Zijlstra96f6d442009-03-23 18:22:07 +01002884 int *recursion = perf_swcounter_recursion_context(cpuctx);
2885
2886 if (*recursion)
2887 goto out;
2888
2889 (*recursion)++;
2890 barrier();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002891
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002892 perf_swcounter_ctx_event(&cpuctx->ctx, type, event,
2893 nr, nmi, regs, addr);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002894 if (cpuctx->task_ctx) {
2895 perf_swcounter_ctx_event(cpuctx->task_ctx, type, event,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002896 nr, nmi, regs, addr);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002897 }
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002898
Peter Zijlstra96f6d442009-03-23 18:22:07 +01002899 barrier();
2900 (*recursion)--;
2901
2902out:
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002903 put_cpu_var(perf_cpu_context);
2904}
2905
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002906void
2907perf_swcounter_event(u32 event, u64 nr, int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002908{
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002909 __perf_swcounter_event(PERF_TYPE_SOFTWARE, event, nr, nmi, regs, addr);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002910}
2911
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002912static void perf_swcounter_read(struct perf_counter *counter)
2913{
2914 perf_swcounter_update(counter);
2915}
2916
2917static int perf_swcounter_enable(struct perf_counter *counter)
2918{
2919 perf_swcounter_set_period(counter);
2920 return 0;
2921}
2922
2923static void perf_swcounter_disable(struct perf_counter *counter)
2924{
2925 perf_swcounter_update(counter);
2926}
2927
Robert Richter4aeb0b42009-04-29 12:47:03 +02002928static const struct pmu perf_ops_generic = {
Peter Zijlstraac17dc82009-03-13 12:21:34 +01002929 .enable = perf_swcounter_enable,
2930 .disable = perf_swcounter_disable,
2931 .read = perf_swcounter_read,
2932};
2933
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002934/*
2935 * Software counter: cpu wall time clock
2936 */
2937
Paul Mackerras9abf8a02009-01-09 16:26:43 +11002938static void cpu_clock_perf_counter_update(struct perf_counter *counter)
2939{
2940 int cpu = raw_smp_processor_id();
2941 s64 prev;
2942 u64 now;
2943
2944 now = cpu_clock(cpu);
2945 prev = atomic64_read(&counter->hw.prev_count);
2946 atomic64_set(&counter->hw.prev_count, now);
2947 atomic64_add(now - prev, &counter->count);
2948}
2949
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002950static int cpu_clock_perf_counter_enable(struct perf_counter *counter)
2951{
2952 struct hw_perf_counter *hwc = &counter->hw;
2953 int cpu = raw_smp_processor_id();
2954
2955 atomic64_set(&hwc->prev_count, cpu_clock(cpu));
Peter Zijlstra039fc912009-03-13 16:43:47 +01002956 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2957 hwc->hrtimer.function = perf_swcounter_hrtimer;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002958 if (hwc->irq_period) {
Peter Zijlstra60db5e02009-05-15 15:19:28 +02002959 u64 period = max_t(u64, 10000, hwc->irq_period);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002960 __hrtimer_start_range_ns(&hwc->hrtimer,
Peter Zijlstra60db5e02009-05-15 15:19:28 +02002961 ns_to_ktime(period), 0,
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002962 HRTIMER_MODE_REL, 0);
2963 }
2964
2965 return 0;
2966}
2967
Ingo Molnar5c92d122008-12-11 13:21:10 +01002968static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
2969{
Peter Zijlstrab986d7e2009-05-20 12:21:21 +02002970 if (counter->hw.irq_period)
2971 hrtimer_cancel(&counter->hw.hrtimer);
Paul Mackerras9abf8a02009-01-09 16:26:43 +11002972 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01002973}
2974
2975static void cpu_clock_perf_counter_read(struct perf_counter *counter)
2976{
Paul Mackerras9abf8a02009-01-09 16:26:43 +11002977 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01002978}
2979
Robert Richter4aeb0b42009-04-29 12:47:03 +02002980static const struct pmu perf_ops_cpu_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +01002981 .enable = cpu_clock_perf_counter_enable,
2982 .disable = cpu_clock_perf_counter_disable,
2983 .read = cpu_clock_perf_counter_read,
Ingo Molnar5c92d122008-12-11 13:21:10 +01002984};
2985
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01002986/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002987 * Software counter: task time clock
2988 */
2989
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02002990static void task_clock_perf_counter_update(struct perf_counter *counter, u64 now)
Ingo Molnarbae43c92008-12-11 14:03:20 +01002991{
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02002992 u64 prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002993 s64 delta;
Ingo Molnarbae43c92008-12-11 14:03:20 +01002994
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02002995 prev = atomic64_xchg(&counter->hw.prev_count, now);
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002996 delta = now - prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002997 atomic64_add(delta, &counter->count);
Ingo Molnarbae43c92008-12-11 14:03:20 +01002998}
2999
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01003000static int task_clock_perf_counter_enable(struct perf_counter *counter)
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003001{
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003002 struct hw_perf_counter *hwc = &counter->hw;
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02003003 u64 now;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003004
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02003005 now = counter->ctx->time;
3006
3007 atomic64_set(&hwc->prev_count, now);
Peter Zijlstra039fc912009-03-13 16:43:47 +01003008 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
3009 hwc->hrtimer.function = perf_swcounter_hrtimer;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003010 if (hwc->irq_period) {
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003011 u64 period = max_t(u64, 10000, hwc->irq_period);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003012 __hrtimer_start_range_ns(&hwc->hrtimer,
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003013 ns_to_ktime(period), 0,
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003014 HRTIMER_MODE_REL, 0);
3015 }
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01003016
3017 return 0;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003018}
3019
3020static void task_clock_perf_counter_disable(struct perf_counter *counter)
3021{
Peter Zijlstrab986d7e2009-05-20 12:21:21 +02003022 if (counter->hw.irq_period)
3023 hrtimer_cancel(&counter->hw.hrtimer);
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02003024 task_clock_perf_counter_update(counter, counter->ctx->time);
3025
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003026}
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01003027
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003028static void task_clock_perf_counter_read(struct perf_counter *counter)
3029{
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02003030 u64 time;
3031
3032 if (!in_nmi()) {
3033 update_context_time(counter->ctx);
3034 time = counter->ctx->time;
3035 } else {
3036 u64 now = perf_clock();
3037 u64 delta = now - counter->ctx->timestamp;
3038 time = counter->ctx->time + delta;
3039 }
3040
3041 task_clock_perf_counter_update(counter, time);
Ingo Molnarbae43c92008-12-11 14:03:20 +01003042}
3043
Robert Richter4aeb0b42009-04-29 12:47:03 +02003044static const struct pmu perf_ops_task_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +01003045 .enable = task_clock_perf_counter_enable,
3046 .disable = task_clock_perf_counter_disable,
3047 .read = task_clock_perf_counter_read,
Ingo Molnarbae43c92008-12-11 14:03:20 +01003048};
3049
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003050/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003051 * Software counter: cpu migrations
3052 */
3053
Paul Mackerras23a185c2009-02-09 22:42:47 +11003054static inline u64 get_cpu_migrations(struct perf_counter *counter)
Ingo Molnar6c594c22008-12-14 12:34:15 +01003055{
Paul Mackerras23a185c2009-02-09 22:42:47 +11003056 struct task_struct *curr = counter->ctx->task;
3057
3058 if (curr)
3059 return curr->se.nr_migrations;
3060 return cpu_nr_migrations(smp_processor_id());
Ingo Molnar6c594c22008-12-14 12:34:15 +01003061}
3062
3063static void cpu_migrations_perf_counter_update(struct perf_counter *counter)
3064{
3065 u64 prev, now;
3066 s64 delta;
3067
3068 prev = atomic64_read(&counter->hw.prev_count);
Paul Mackerras23a185c2009-02-09 22:42:47 +11003069 now = get_cpu_migrations(counter);
Ingo Molnar6c594c22008-12-14 12:34:15 +01003070
3071 atomic64_set(&counter->hw.prev_count, now);
3072
3073 delta = now - prev;
Ingo Molnar6c594c22008-12-14 12:34:15 +01003074
3075 atomic64_add(delta, &counter->count);
3076}
3077
3078static void cpu_migrations_perf_counter_read(struct perf_counter *counter)
3079{
3080 cpu_migrations_perf_counter_update(counter);
3081}
3082
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01003083static int cpu_migrations_perf_counter_enable(struct perf_counter *counter)
Ingo Molnar6c594c22008-12-14 12:34:15 +01003084{
Paul Mackerrasc07c99b2009-02-13 22:10:34 +11003085 if (counter->prev_state <= PERF_COUNTER_STATE_OFF)
3086 atomic64_set(&counter->hw.prev_count,
3087 get_cpu_migrations(counter));
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01003088 return 0;
Ingo Molnar6c594c22008-12-14 12:34:15 +01003089}
3090
3091static void cpu_migrations_perf_counter_disable(struct perf_counter *counter)
3092{
3093 cpu_migrations_perf_counter_update(counter);
3094}
3095
Robert Richter4aeb0b42009-04-29 12:47:03 +02003096static const struct pmu perf_ops_cpu_migrations = {
Ingo Molnar76715812008-12-17 14:20:28 +01003097 .enable = cpu_migrations_perf_counter_enable,
3098 .disable = cpu_migrations_perf_counter_disable,
3099 .read = cpu_migrations_perf_counter_read,
Ingo Molnar6c594c22008-12-14 12:34:15 +01003100};
3101
Peter Zijlstrae077df42009-03-19 20:26:17 +01003102#ifdef CONFIG_EVENT_PROFILE
3103void perf_tpcounter_event(int event_id)
3104{
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003105 struct pt_regs *regs = get_irq_regs();
3106
3107 if (!regs)
3108 regs = task_pt_regs(current);
3109
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003110 __perf_swcounter_event(PERF_TYPE_TRACEPOINT, event_id, 1, 1, regs, 0);
Peter Zijlstrae077df42009-03-19 20:26:17 +01003111}
Steven Whitehouseff7b1b42009-04-15 16:55:05 +01003112EXPORT_SYMBOL_GPL(perf_tpcounter_event);
Peter Zijlstrae077df42009-03-19 20:26:17 +01003113
3114extern int ftrace_profile_enable(int);
3115extern void ftrace_profile_disable(int);
3116
3117static void tp_perf_counter_destroy(struct perf_counter *counter)
3118{
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01003119 ftrace_profile_disable(perf_event_id(&counter->hw_event));
Peter Zijlstrae077df42009-03-19 20:26:17 +01003120}
3121
Robert Richter4aeb0b42009-04-29 12:47:03 +02003122static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
Peter Zijlstrae077df42009-03-19 20:26:17 +01003123{
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01003124 int event_id = perf_event_id(&counter->hw_event);
Peter Zijlstrae077df42009-03-19 20:26:17 +01003125 int ret;
3126
3127 ret = ftrace_profile_enable(event_id);
3128 if (ret)
3129 return NULL;
3130
3131 counter->destroy = tp_perf_counter_destroy;
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003132 counter->hw.irq_period = counter->hw_event.irq_period;
Peter Zijlstrae077df42009-03-19 20:26:17 +01003133
3134 return &perf_ops_generic;
3135}
3136#else
Robert Richter4aeb0b42009-04-29 12:47:03 +02003137static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
Peter Zijlstrae077df42009-03-19 20:26:17 +01003138{
3139 return NULL;
3140}
3141#endif
3142
Robert Richter4aeb0b42009-04-29 12:47:03 +02003143static const struct pmu *sw_perf_counter_init(struct perf_counter *counter)
Ingo Molnar5c92d122008-12-11 13:21:10 +01003144{
Robert Richter4aeb0b42009-04-29 12:47:03 +02003145 const struct pmu *pmu = NULL;
Ingo Molnar5c92d122008-12-11 13:21:10 +01003146
Paul Mackerras0475f9e2009-02-11 14:35:35 +11003147 /*
3148 * Software counters (currently) can't in general distinguish
3149 * between user, kernel and hypervisor events.
3150 * However, context switches and cpu migrations are considered
3151 * to be kernel events, and page faults are never hypervisor
3152 * events.
3153 */
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01003154 switch (perf_event_id(&counter->hw_event)) {
Ingo Molnar5c92d122008-12-11 13:21:10 +01003155 case PERF_COUNT_CPU_CLOCK:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003156 pmu = &perf_ops_cpu_clock;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003157
Ingo Molnar5c92d122008-12-11 13:21:10 +01003158 break;
Ingo Molnarbae43c92008-12-11 14:03:20 +01003159 case PERF_COUNT_TASK_CLOCK:
Paul Mackerras23a185c2009-02-09 22:42:47 +11003160 /*
3161 * If the user instantiates this as a per-cpu counter,
3162 * use the cpu_clock counter instead.
3163 */
3164 if (counter->ctx->task)
Robert Richter4aeb0b42009-04-29 12:47:03 +02003165 pmu = &perf_ops_task_clock;
Paul Mackerras23a185c2009-02-09 22:42:47 +11003166 else
Robert Richter4aeb0b42009-04-29 12:47:03 +02003167 pmu = &perf_ops_cpu_clock;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003168
Ingo Molnarbae43c92008-12-11 14:03:20 +01003169 break;
Ingo Molnare06c61a2008-12-14 14:44:31 +01003170 case PERF_COUNT_PAGE_FAULTS:
Peter Zijlstraac17dc82009-03-13 12:21:34 +01003171 case PERF_COUNT_PAGE_FAULTS_MIN:
3172 case PERF_COUNT_PAGE_FAULTS_MAJ:
Ingo Molnar5d6a27d2008-12-14 12:28:33 +01003173 case PERF_COUNT_CONTEXT_SWITCHES:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003174 pmu = &perf_ops_generic;
Ingo Molnar5d6a27d2008-12-14 12:28:33 +01003175 break;
Ingo Molnar6c594c22008-12-14 12:34:15 +01003176 case PERF_COUNT_CPU_MIGRATIONS:
Paul Mackerras0475f9e2009-02-11 14:35:35 +11003177 if (!counter->hw_event.exclude_kernel)
Robert Richter4aeb0b42009-04-29 12:47:03 +02003178 pmu = &perf_ops_cpu_migrations;
Ingo Molnar6c594c22008-12-14 12:34:15 +01003179 break;
Ingo Molnar5c92d122008-12-11 13:21:10 +01003180 }
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003181
Robert Richter4aeb0b42009-04-29 12:47:03 +02003182 return pmu;
Ingo Molnar5c92d122008-12-11 13:21:10 +01003183}
3184
Thomas Gleixner0793a612008-12-04 20:12:29 +01003185/*
3186 * Allocate and initialize a counter structure
3187 */
3188static struct perf_counter *
Ingo Molnar04289bb2008-12-11 08:38:42 +01003189perf_counter_alloc(struct perf_counter_hw_event *hw_event,
3190 int cpu,
Paul Mackerras23a185c2009-02-09 22:42:47 +11003191 struct perf_counter_context *ctx,
Ingo Molnar9b51f662008-12-12 13:49:45 +01003192 struct perf_counter *group_leader,
3193 gfp_t gfpflags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003194{
Robert Richter4aeb0b42009-04-29 12:47:03 +02003195 const struct pmu *pmu;
Ingo Molnar621a01e2008-12-11 12:46:46 +01003196 struct perf_counter *counter;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003197 struct hw_perf_counter *hwc;
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003198 long err;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003199
Ingo Molnar9b51f662008-12-12 13:49:45 +01003200 counter = kzalloc(sizeof(*counter), gfpflags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003201 if (!counter)
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003202 return ERR_PTR(-ENOMEM);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003203
Ingo Molnar04289bb2008-12-11 08:38:42 +01003204 /*
3205 * Single counters are their own group leaders, with an
3206 * empty sibling list:
3207 */
3208 if (!group_leader)
3209 group_leader = counter;
3210
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003211 mutex_init(&counter->child_mutex);
3212 INIT_LIST_HEAD(&counter->child_list);
3213
Ingo Molnar04289bb2008-12-11 08:38:42 +01003214 INIT_LIST_HEAD(&counter->list_entry);
Peter Zijlstra592903c2009-03-13 12:21:36 +01003215 INIT_LIST_HEAD(&counter->event_entry);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003216 INIT_LIST_HEAD(&counter->sibling_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003217 init_waitqueue_head(&counter->waitq);
3218
Peter Zijlstra7b732a72009-03-23 18:22:10 +01003219 mutex_init(&counter->mmap_mutex);
3220
Ingo Molnar9f66a382008-12-10 12:33:23 +01003221 counter->cpu = cpu;
3222 counter->hw_event = *hw_event;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003223 counter->group_leader = group_leader;
Robert Richter4aeb0b42009-04-29 12:47:03 +02003224 counter->pmu = NULL;
Paul Mackerras23a185c2009-02-09 22:42:47 +11003225 counter->ctx = ctx;
Ingo Molnar329d8762009-05-26 08:10:00 +02003226 counter->oncpu = -1;
3227
Ingo Molnar235c7fc2008-12-21 14:43:25 +01003228 counter->state = PERF_COUNTER_STATE_INACTIVE;
Ingo Molnara86ed502008-12-17 00:43:10 +01003229 if (hw_event->disabled)
3230 counter->state = PERF_COUNTER_STATE_OFF;
3231
Robert Richter4aeb0b42009-04-29 12:47:03 +02003232 pmu = NULL;
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003233
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003234 hwc = &counter->hw;
3235 if (hw_event->freq && hw_event->irq_freq)
Peter Zijlstra2e569d32009-05-15 15:37:47 +02003236 hwc->irq_period = div64_u64(TICK_NSEC, hw_event->irq_freq);
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003237 else
3238 hwc->irq_period = hw_event->irq_period;
3239
Peter Zijlstra2023b352009-05-05 17:50:26 +02003240 /*
3241 * we currently do not support PERF_RECORD_GROUP on inherited counters
3242 */
3243 if (hw_event->inherit && (hw_event->record_type & PERF_RECORD_GROUP))
3244 goto done;
3245
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01003246 if (perf_event_raw(hw_event)) {
Robert Richter4aeb0b42009-04-29 12:47:03 +02003247 pmu = hw_perf_counter_init(counter);
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01003248 goto done;
3249 }
3250
3251 switch (perf_event_type(hw_event)) {
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003252 case PERF_TYPE_HARDWARE:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003253 pmu = hw_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003254 break;
3255
3256 case PERF_TYPE_SOFTWARE:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003257 pmu = sw_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003258 break;
3259
3260 case PERF_TYPE_TRACEPOINT:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003261 pmu = tp_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003262 break;
3263 }
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01003264done:
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003265 err = 0;
Robert Richter4aeb0b42009-04-29 12:47:03 +02003266 if (!pmu)
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003267 err = -EINVAL;
Robert Richter4aeb0b42009-04-29 12:47:03 +02003268 else if (IS_ERR(pmu))
3269 err = PTR_ERR(pmu);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003270
3271 if (err) {
3272 kfree(counter);
3273 return ERR_PTR(err);
3274 }
3275
Robert Richter4aeb0b42009-04-29 12:47:03 +02003276 counter->pmu = pmu;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003277
Peter Zijlstra7fc23a52009-05-08 18:52:21 +02003278 atomic_inc(&nr_counters);
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02003279 if (counter->hw_event.mmap)
3280 atomic_inc(&nr_mmap_tracking);
3281 if (counter->hw_event.munmap)
3282 atomic_inc(&nr_munmap_tracking);
3283 if (counter->hw_event.comm)
3284 atomic_inc(&nr_comm_tracking);
3285
Thomas Gleixner0793a612008-12-04 20:12:29 +01003286 return counter;
3287}
3288
3289/**
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003290 * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
Ingo Molnar9f66a382008-12-10 12:33:23 +01003291 *
3292 * @hw_event_uptr: event type attributes for monitoring/sampling
Thomas Gleixner0793a612008-12-04 20:12:29 +01003293 * @pid: target pid
Ingo Molnar9f66a382008-12-10 12:33:23 +01003294 * @cpu: target cpu
3295 * @group_fd: group leader counter fd
Thomas Gleixner0793a612008-12-04 20:12:29 +01003296 */
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003297SYSCALL_DEFINE5(perf_counter_open,
Paul Mackerrasf3dfd262009-02-26 22:43:46 +11003298 const struct perf_counter_hw_event __user *, hw_event_uptr,
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003299 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003300{
Ingo Molnar04289bb2008-12-11 08:38:42 +01003301 struct perf_counter *counter, *group_leader;
Ingo Molnar9f66a382008-12-10 12:33:23 +01003302 struct perf_counter_hw_event hw_event;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003303 struct perf_counter_context *ctx;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003304 struct file *counter_file = NULL;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003305 struct file *group_file = NULL;
3306 int fput_needed = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003307 int fput_needed2 = 0;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003308 int ret;
3309
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003310 /* for future expandability... */
3311 if (flags)
3312 return -EINVAL;
3313
Ingo Molnar9f66a382008-12-10 12:33:23 +01003314 if (copy_from_user(&hw_event, hw_event_uptr, sizeof(hw_event)) != 0)
Thomas Gleixnereab656a2008-12-08 19:26:59 +01003315 return -EFAULT;
3316
Ingo Molnar04289bb2008-12-11 08:38:42 +01003317 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01003318 * Get the target context (task or percpu):
3319 */
3320 ctx = find_get_context(pid, cpu);
3321 if (IS_ERR(ctx))
3322 return PTR_ERR(ctx);
3323
3324 /*
3325 * Look up the group leader (we will attach this counter to it):
Ingo Molnar04289bb2008-12-11 08:38:42 +01003326 */
3327 group_leader = NULL;
3328 if (group_fd != -1) {
3329 ret = -EINVAL;
3330 group_file = fget_light(group_fd, &fput_needed);
3331 if (!group_file)
Ingo Molnarccff2862008-12-11 11:26:29 +01003332 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003333 if (group_file->f_op != &perf_fops)
Ingo Molnarccff2862008-12-11 11:26:29 +01003334 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003335
3336 group_leader = group_file->private_data;
3337 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01003338 * Do not allow a recursive hierarchy (this new sibling
3339 * becoming part of another group-sibling):
Ingo Molnar04289bb2008-12-11 08:38:42 +01003340 */
Ingo Molnarccff2862008-12-11 11:26:29 +01003341 if (group_leader->group_leader != group_leader)
3342 goto err_put_context;
3343 /*
3344 * Do not allow to attach to a group in a different
3345 * task or CPU context:
3346 */
3347 if (group_leader->ctx != ctx)
3348 goto err_put_context;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11003349 /*
3350 * Only a group leader can be exclusive or pinned
3351 */
3352 if (hw_event.exclusive || hw_event.pinned)
3353 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003354 }
3355
Paul Mackerras23a185c2009-02-09 22:42:47 +11003356 counter = perf_counter_alloc(&hw_event, cpu, ctx, group_leader,
3357 GFP_KERNEL);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003358 ret = PTR_ERR(counter);
3359 if (IS_ERR(counter))
Thomas Gleixner0793a612008-12-04 20:12:29 +01003360 goto err_put_context;
3361
Thomas Gleixner0793a612008-12-04 20:12:29 +01003362 ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
3363 if (ret < 0)
Ingo Molnar9b51f662008-12-12 13:49:45 +01003364 goto err_free_put_context;
3365
3366 counter_file = fget_light(ret, &fput_needed2);
3367 if (!counter_file)
3368 goto err_free_put_context;
3369
3370 counter->filp = counter_file;
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003371 WARN_ON_ONCE(ctx->parent_ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003372 mutex_lock(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003373 perf_install_in_context(ctx, counter, cpu);
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003374 ++ctx->generation;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003375 mutex_unlock(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003376
Peter Zijlstra082ff5a2009-05-23 18:29:00 +02003377 counter->owner = current;
3378 get_task_struct(current);
3379 mutex_lock(&current->perf_counter_mutex);
3380 list_add_tail(&counter->owner_entry, &current->perf_counter_list);
3381 mutex_unlock(&current->perf_counter_mutex);
3382
Ingo Molnar9b51f662008-12-12 13:49:45 +01003383 fput_light(counter_file, fput_needed2);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003384
Ingo Molnar04289bb2008-12-11 08:38:42 +01003385out_fput:
3386 fput_light(group_file, fput_needed);
3387
Thomas Gleixner0793a612008-12-04 20:12:29 +01003388 return ret;
3389
Ingo Molnar9b51f662008-12-12 13:49:45 +01003390err_free_put_context:
Thomas Gleixner0793a612008-12-04 20:12:29 +01003391 kfree(counter);
3392
3393err_put_context:
Paul Mackerrasc93f7662009-05-28 22:18:17 +10003394 put_ctx(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003395
Ingo Molnar04289bb2008-12-11 08:38:42 +01003396 goto out_fput;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003397}
3398
Ingo Molnar9b51f662008-12-12 13:49:45 +01003399/*
Ingo Molnar9b51f662008-12-12 13:49:45 +01003400 * inherit a counter from parent task to child task:
3401 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11003402static struct perf_counter *
Ingo Molnar9b51f662008-12-12 13:49:45 +01003403inherit_counter(struct perf_counter *parent_counter,
3404 struct task_struct *parent,
3405 struct perf_counter_context *parent_ctx,
3406 struct task_struct *child,
Paul Mackerrasd859e292009-01-17 18:10:22 +11003407 struct perf_counter *group_leader,
Ingo Molnar9b51f662008-12-12 13:49:45 +01003408 struct perf_counter_context *child_ctx)
3409{
3410 struct perf_counter *child_counter;
3411
Paul Mackerrasd859e292009-01-17 18:10:22 +11003412 /*
3413 * Instead of creating recursive hierarchies of counters,
3414 * we link inherited counters back to the original parent,
3415 * which has a filp for sure, which we use as the reference
3416 * count:
3417 */
3418 if (parent_counter->parent)
3419 parent_counter = parent_counter->parent;
3420
Ingo Molnar9b51f662008-12-12 13:49:45 +01003421 child_counter = perf_counter_alloc(&parent_counter->hw_event,
Paul Mackerras23a185c2009-02-09 22:42:47 +11003422 parent_counter->cpu, child_ctx,
3423 group_leader, GFP_KERNEL);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003424 if (IS_ERR(child_counter))
3425 return child_counter;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10003426 get_ctx(child_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003427
3428 /*
Paul Mackerras564c2b22009-05-22 14:27:22 +10003429 * Make the child state follow the state of the parent counter,
3430 * not its hw_event.disabled bit. We hold the parent's mutex,
3431 * so we won't race with perf_counter_{en,dis}able_family.
3432 */
3433 if (parent_counter->state >= PERF_COUNTER_STATE_INACTIVE)
3434 child_counter->state = PERF_COUNTER_STATE_INACTIVE;
3435 else
3436 child_counter->state = PERF_COUNTER_STATE_OFF;
3437
3438 /*
Ingo Molnar9b51f662008-12-12 13:49:45 +01003439 * Link it up in the child's context:
3440 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +11003441 add_counter_to_ctx(child_counter, child_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003442
3443 child_counter->parent = parent_counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003444 /*
3445 * inherit into child's child as well:
3446 */
3447 child_counter->hw_event.inherit = 1;
3448
3449 /*
3450 * Get a reference to the parent filp - we will fput it
3451 * when the child counter exits. This is safe to do because
3452 * we are in the parent and we know that the filp still
3453 * exists and has a nonzero count:
3454 */
3455 atomic_long_inc(&parent_counter->filp->f_count);
3456
Paul Mackerrasd859e292009-01-17 18:10:22 +11003457 /*
3458 * Link this into the parent counter's child list
3459 */
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003460 WARN_ON_ONCE(parent_counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003461 mutex_lock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003462 list_add_tail(&child_counter->child_list, &parent_counter->child_list);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003463 mutex_unlock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003464
3465 return child_counter;
3466}
3467
3468static int inherit_group(struct perf_counter *parent_counter,
3469 struct task_struct *parent,
3470 struct perf_counter_context *parent_ctx,
3471 struct task_struct *child,
3472 struct perf_counter_context *child_ctx)
3473{
3474 struct perf_counter *leader;
3475 struct perf_counter *sub;
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003476 struct perf_counter *child_ctr;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003477
3478 leader = inherit_counter(parent_counter, parent, parent_ctx,
3479 child, NULL, child_ctx);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003480 if (IS_ERR(leader))
3481 return PTR_ERR(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003482 list_for_each_entry(sub, &parent_counter->sibling_list, list_entry) {
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003483 child_ctr = inherit_counter(sub, parent, parent_ctx,
3484 child, leader, child_ctx);
3485 if (IS_ERR(child_ctr))
3486 return PTR_ERR(child_ctr);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003487 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01003488 return 0;
3489}
3490
Paul Mackerrasd859e292009-01-17 18:10:22 +11003491static void sync_child_counter(struct perf_counter *child_counter,
3492 struct perf_counter *parent_counter)
3493{
Peter Zijlstra8bc20952009-05-15 20:45:59 +02003494 u64 child_val;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003495
Paul Mackerrasd859e292009-01-17 18:10:22 +11003496 child_val = atomic64_read(&child_counter->count);
3497
3498 /*
3499 * Add back the child's count to the parent's count:
3500 */
3501 atomic64_add(child_val, &parent_counter->count);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11003502 atomic64_add(child_counter->total_time_enabled,
3503 &parent_counter->child_total_time_enabled);
3504 atomic64_add(child_counter->total_time_running,
3505 &parent_counter->child_total_time_running);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003506
3507 /*
3508 * Remove this counter from the parent's list
3509 */
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003510 WARN_ON_ONCE(parent_counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003511 mutex_lock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003512 list_del_init(&child_counter->child_list);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003513 mutex_unlock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003514
3515 /*
3516 * Release the parent counter, if this was the last
3517 * reference to it.
3518 */
3519 fput(parent_counter->filp);
3520}
3521
Ingo Molnar9b51f662008-12-12 13:49:45 +01003522static void
3523__perf_counter_exit_task(struct task_struct *child,
3524 struct perf_counter *child_counter,
3525 struct perf_counter_context *child_ctx)
3526{
3527 struct perf_counter *parent_counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003528
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003529 update_counter_times(child_counter);
Peter Zijlstraaa9c67f2009-05-23 18:28:59 +02003530 perf_counter_remove_from_context(child_counter);
Ingo Molnar0cc0c022008-12-14 23:20:36 +01003531
Ingo Molnar9b51f662008-12-12 13:49:45 +01003532 parent_counter = child_counter->parent;
3533 /*
3534 * It can happen that parent exits first, and has counters
3535 * that are still around due to the child reference. These
3536 * counters need to be zapped - but otherwise linger.
3537 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11003538 if (parent_counter) {
3539 sync_child_counter(child_counter, parent_counter);
Peter Zijlstraf1600952009-03-19 20:26:16 +01003540 free_counter(child_counter);
Paul Mackerras4bcf3492009-02-11 13:53:19 +01003541 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01003542}
3543
3544/*
Paul Mackerrasd859e292009-01-17 18:10:22 +11003545 * When a child task exits, feed back counter values to parent counters.
Ingo Molnar9b51f662008-12-12 13:49:45 +01003546 */
3547void perf_counter_exit_task(struct task_struct *child)
3548{
3549 struct perf_counter *child_counter, *tmp;
3550 struct perf_counter_context *child_ctx;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003551 unsigned long flags;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003552
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003553 if (likely(!child->perf_counter_ctxp))
Ingo Molnar9b51f662008-12-12 13:49:45 +01003554 return;
3555
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003556 local_irq_save(flags);
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003557 /*
3558 * We can't reschedule here because interrupts are disabled,
3559 * and either child is current or it is a task that can't be
3560 * scheduled, so we are now safe from rescheduling changing
3561 * our context.
3562 */
3563 child_ctx = child->perf_counter_ctxp;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003564 __perf_counter_task_sched_out(child_ctx);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10003565
3566 /*
3567 * Take the context lock here so that if find_get_context is
3568 * reading child->perf_counter_ctxp, we wait until it has
3569 * incremented the context's refcount before we do put_ctx below.
3570 */
3571 spin_lock(&child_ctx->lock);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003572 child->perf_counter_ctxp = NULL;
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003573 if (child_ctx->parent_ctx) {
3574 /*
3575 * This context is a clone; unclone it so it can't get
3576 * swapped to another process while we're removing all
3577 * the counters from it.
3578 */
3579 put_ctx(child_ctx->parent_ctx);
3580 child_ctx->parent_ctx = NULL;
3581 }
Paul Mackerrasc93f7662009-05-28 22:18:17 +10003582 spin_unlock(&child_ctx->lock);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003583 local_irq_restore(flags);
3584
3585 mutex_lock(&child_ctx->mutex);
3586
Peter Zijlstra8bc20952009-05-15 20:45:59 +02003587again:
Ingo Molnar9b51f662008-12-12 13:49:45 +01003588 list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list,
3589 list_entry)
3590 __perf_counter_exit_task(child, child_counter, child_ctx);
Peter Zijlstra8bc20952009-05-15 20:45:59 +02003591
3592 /*
3593 * If the last counter was a group counter, it will have appended all
3594 * its siblings to the list, but we obtained 'tmp' before that which
3595 * will still point to the list head terminating the iteration.
3596 */
3597 if (!list_empty(&child_ctx->counter_list))
3598 goto again;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003599
3600 mutex_unlock(&child_ctx->mutex);
3601
3602 put_ctx(child_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003603}
3604
3605/*
3606 * Initialize the perf_counter context in task_struct
3607 */
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02003608int perf_counter_init_task(struct task_struct *child)
Ingo Molnar9b51f662008-12-12 13:49:45 +01003609{
3610 struct perf_counter_context *child_ctx, *parent_ctx;
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003611 struct perf_counter_context *cloned_ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003612 struct perf_counter *counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003613 struct task_struct *parent = current;
Paul Mackerras564c2b22009-05-22 14:27:22 +10003614 int inherited_all = 1;
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003615 u64 cloned_gen;
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02003616 int ret = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003617
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003618 child->perf_counter_ctxp = NULL;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003619
Peter Zijlstra082ff5a2009-05-23 18:29:00 +02003620 mutex_init(&child->perf_counter_mutex);
3621 INIT_LIST_HEAD(&child->perf_counter_list);
3622
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003623 if (likely(!parent->perf_counter_ctxp))
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02003624 return 0;
3625
Ingo Molnar9b51f662008-12-12 13:49:45 +01003626 /*
3627 * This is executed from the parent task context, so inherit
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003628 * counters that have been marked for cloning.
3629 * First allocate and initialize a context for the child.
Ingo Molnar9b51f662008-12-12 13:49:45 +01003630 */
3631
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003632 child_ctx = kmalloc(sizeof(struct perf_counter_context), GFP_KERNEL);
3633 if (!child_ctx)
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02003634 return -ENOMEM;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003635
3636 __perf_counter_init_context(child_ctx, child);
3637 child->perf_counter_ctxp = child_ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10003638 get_task_struct(child);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003639
Ingo Molnar9b51f662008-12-12 13:49:45 +01003640 /*
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003641 * If the parent's context is a clone, temporarily set its
3642 * parent_gen to an impossible value (all 1s) so it won't get
3643 * swapped under us. The rcu_read_lock makes sure that
3644 * parent_ctx continues to exist even if it gets swapped to
3645 * another process and then freed while we are trying to get
3646 * its lock.
3647 */
3648 rcu_read_lock();
3649 retry:
3650 parent_ctx = rcu_dereference(parent->perf_counter_ctxp);
3651 /*
3652 * No need to check if parent_ctx != NULL here; since we saw
3653 * it non-NULL earlier, the only reason for it to become NULL
3654 * is if we exit, and since we're currently in the middle of
3655 * a fork we can't be exiting at the same time.
3656 */
3657 spin_lock_irq(&parent_ctx->lock);
3658 if (parent_ctx != rcu_dereference(parent->perf_counter_ctxp)) {
3659 spin_unlock_irq(&parent_ctx->lock);
3660 goto retry;
3661 }
3662 cloned_gen = parent_ctx->parent_gen;
3663 if (parent_ctx->parent_ctx)
3664 parent_ctx->parent_gen = ~0ull;
3665 spin_unlock_irq(&parent_ctx->lock);
3666 rcu_read_unlock();
3667
3668 /*
Ingo Molnar9b51f662008-12-12 13:49:45 +01003669 * Lock the parent list. No need to lock the child - not PID
3670 * hashed yet and not running, so nobody can access it.
3671 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11003672 mutex_lock(&parent_ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003673
3674 /*
3675 * We dont have to disable NMIs - we are only looking at
3676 * the list, not manipulating it:
3677 */
Peter Zijlstrad7b629a2009-05-20 12:21:19 +02003678 list_for_each_entry_rcu(counter, &parent_ctx->event_list, event_entry) {
3679 if (counter != counter->group_leader)
3680 continue;
3681
Paul Mackerras564c2b22009-05-22 14:27:22 +10003682 if (!counter->hw_event.inherit) {
3683 inherited_all = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003684 continue;
Paul Mackerras564c2b22009-05-22 14:27:22 +10003685 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01003686
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02003687 ret = inherit_group(counter, parent, parent_ctx,
3688 child, child_ctx);
3689 if (ret) {
Paul Mackerras564c2b22009-05-22 14:27:22 +10003690 inherited_all = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003691 break;
Paul Mackerras564c2b22009-05-22 14:27:22 +10003692 }
3693 }
3694
3695 if (inherited_all) {
3696 /*
3697 * Mark the child context as a clone of the parent
3698 * context, or of whatever the parent is a clone of.
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003699 * Note that if the parent is a clone, it could get
3700 * uncloned at any point, but that doesn't matter
3701 * because the list of counters and the generation
3702 * count can't have changed since we took the mutex.
Paul Mackerras564c2b22009-05-22 14:27:22 +10003703 */
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003704 cloned_ctx = rcu_dereference(parent_ctx->parent_ctx);
3705 if (cloned_ctx) {
3706 child_ctx->parent_ctx = cloned_ctx;
3707 child_ctx->parent_gen = cloned_gen;
Paul Mackerras564c2b22009-05-22 14:27:22 +10003708 } else {
3709 child_ctx->parent_ctx = parent_ctx;
3710 child_ctx->parent_gen = parent_ctx->generation;
3711 }
3712 get_ctx(child_ctx->parent_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003713 }
3714
Paul Mackerrasd859e292009-01-17 18:10:22 +11003715 mutex_unlock(&parent_ctx->mutex);
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02003716
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003717 /*
3718 * Restore the clone status of the parent.
3719 */
3720 if (parent_ctx->parent_ctx) {
3721 spin_lock_irq(&parent_ctx->lock);
3722 if (parent_ctx->parent_ctx)
3723 parent_ctx->parent_gen = cloned_gen;
3724 spin_unlock_irq(&parent_ctx->lock);
3725 }
3726
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02003727 return ret;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003728}
3729
Ingo Molnar04289bb2008-12-11 08:38:42 +01003730static void __cpuinit perf_counter_init_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003731{
Ingo Molnar04289bb2008-12-11 08:38:42 +01003732 struct perf_cpu_context *cpuctx;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003733
Ingo Molnar04289bb2008-12-11 08:38:42 +01003734 cpuctx = &per_cpu(perf_cpu_context, cpu);
3735 __perf_counter_init_context(&cpuctx->ctx, NULL);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003736
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003737 spin_lock(&perf_resource_lock);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003738 cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu;
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003739 spin_unlock(&perf_resource_lock);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003740
Paul Mackerras01d02872009-01-14 13:44:19 +11003741 hw_perf_counter_setup(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003742}
3743
3744#ifdef CONFIG_HOTPLUG_CPU
Ingo Molnar04289bb2008-12-11 08:38:42 +01003745static void __perf_counter_exit_cpu(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003746{
3747 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
3748 struct perf_counter_context *ctx = &cpuctx->ctx;
3749 struct perf_counter *counter, *tmp;
3750
Ingo Molnar04289bb2008-12-11 08:38:42 +01003751 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry)
3752 __perf_counter_remove_from_context(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003753}
Ingo Molnar04289bb2008-12-11 08:38:42 +01003754static void perf_counter_exit_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003755{
Paul Mackerrasd859e292009-01-17 18:10:22 +11003756 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
3757 struct perf_counter_context *ctx = &cpuctx->ctx;
3758
3759 mutex_lock(&ctx->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003760 smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003761 mutex_unlock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003762}
3763#else
Ingo Molnar04289bb2008-12-11 08:38:42 +01003764static inline void perf_counter_exit_cpu(int cpu) { }
Thomas Gleixner0793a612008-12-04 20:12:29 +01003765#endif
3766
3767static int __cpuinit
3768perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
3769{
3770 unsigned int cpu = (long)hcpu;
3771
3772 switch (action) {
3773
3774 case CPU_UP_PREPARE:
3775 case CPU_UP_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01003776 perf_counter_init_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003777 break;
3778
3779 case CPU_DOWN_PREPARE:
3780 case CPU_DOWN_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01003781 perf_counter_exit_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003782 break;
3783
3784 default:
3785 break;
3786 }
3787
3788 return NOTIFY_OK;
3789}
3790
3791static struct notifier_block __cpuinitdata perf_cpu_nb = {
3792 .notifier_call = perf_cpu_notify,
3793};
3794
Ingo Molnar0d905bc2009-05-04 19:13:30 +02003795void __init perf_counter_init(void)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003796{
3797 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
3798 (void *)(long)smp_processor_id());
3799 register_cpu_notifier(&perf_cpu_nb);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003800}
Thomas Gleixner0793a612008-12-04 20:12:29 +01003801
3802static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
3803{
3804 return sprintf(buf, "%d\n", perf_reserved_percpu);
3805}
3806
3807static ssize_t
3808perf_set_reserve_percpu(struct sysdev_class *class,
3809 const char *buf,
3810 size_t count)
3811{
3812 struct perf_cpu_context *cpuctx;
3813 unsigned long val;
3814 int err, cpu, mpt;
3815
3816 err = strict_strtoul(buf, 10, &val);
3817 if (err)
3818 return err;
3819 if (val > perf_max_counters)
3820 return -EINVAL;
3821
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003822 spin_lock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003823 perf_reserved_percpu = val;
3824 for_each_online_cpu(cpu) {
3825 cpuctx = &per_cpu(perf_cpu_context, cpu);
3826 spin_lock_irq(&cpuctx->ctx.lock);
3827 mpt = min(perf_max_counters - cpuctx->ctx.nr_counters,
3828 perf_max_counters - perf_reserved_percpu);
3829 cpuctx->max_pertask = mpt;
3830 spin_unlock_irq(&cpuctx->ctx.lock);
3831 }
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003832 spin_unlock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003833
3834 return count;
3835}
3836
3837static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
3838{
3839 return sprintf(buf, "%d\n", perf_overcommit);
3840}
3841
3842static ssize_t
3843perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
3844{
3845 unsigned long val;
3846 int err;
3847
3848 err = strict_strtoul(buf, 10, &val);
3849 if (err)
3850 return err;
3851 if (val > 1)
3852 return -EINVAL;
3853
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003854 spin_lock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003855 perf_overcommit = val;
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003856 spin_unlock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003857
3858 return count;
3859}
3860
3861static SYSDEV_CLASS_ATTR(
3862 reserve_percpu,
3863 0644,
3864 perf_show_reserve_percpu,
3865 perf_set_reserve_percpu
3866 );
3867
3868static SYSDEV_CLASS_ATTR(
3869 overcommit,
3870 0644,
3871 perf_show_overcommit,
3872 perf_set_overcommit
3873 );
3874
3875static struct attribute *perfclass_attrs[] = {
3876 &attr_reserve_percpu.attr,
3877 &attr_overcommit.attr,
3878 NULL
3879};
3880
3881static struct attribute_group perfclass_attr_group = {
3882 .attrs = perfclass_attrs,
3883 .name = "perf_counters",
3884};
3885
3886static int __init perf_counter_sysfs_init(void)
3887{
3888 return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
3889 &perfclass_attr_group);
3890}
3891device_initcall(perf_counter_sysfs_init);