blob: 0c000d305e0e022fa011102dd8a482078476e78c [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;
235
236 /*
237 * If this is a task context, we need to check whether it is
238 * the current task context of this cpu. If not it has been
239 * scheduled out before the smp call arrived.
240 */
Peter Zijlstra665c2142009-05-29 14:51:57 +0200241 if (ctx->task && cpuctx->task_ctx != ctx)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100242 return;
243
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200244 spin_lock(&ctx->lock);
Ingo Molnar34adc802009-05-20 20:13:28 +0200245 /*
246 * Protect the list operation against NMI by disabling the
247 * counters on a global level.
248 */
249 perf_disable();
Thomas Gleixner0793a612008-12-04 20:12:29 +0100250
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100251 counter_sched_out(counter, cpuctx, ctx);
252
Ingo Molnar04289bb2008-12-11 08:38:42 +0100253 list_del_counter(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100254
255 if (!ctx->task) {
256 /*
257 * Allow more per task counters with respect to the
258 * reservation:
259 */
260 cpuctx->max_pertask =
261 min(perf_max_counters - ctx->nr_counters,
262 perf_max_counters - perf_reserved_percpu);
263 }
264
Ingo Molnar34adc802009-05-20 20:13:28 +0200265 perf_enable();
Peter Zijlstra665c2142009-05-29 14:51:57 +0200266 spin_unlock(&ctx->lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100267}
268
269
270/*
271 * Remove the counter from a task's (or a CPU's) list of counters.
272 *
Peter Zijlstrafccc7142009-05-23 18:28:56 +0200273 * Must be called with ctx->mutex held.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100274 *
275 * CPU counters are removed with a smp call. For task counters we only
276 * call when the task is on a CPU.
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000277 *
278 * If counter->ctx is a cloned context, callers must make sure that
279 * every task struct that counter->ctx->task could possibly point to
280 * remains valid. This is OK when called from perf_release since
281 * that only calls us on the top-level context, which can't be a clone.
282 * When called from perf_counter_exit_task, it's OK because the
283 * context has been detached from its task.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100284 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100285static void perf_counter_remove_from_context(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100286{
287 struct perf_counter_context *ctx = counter->ctx;
288 struct task_struct *task = ctx->task;
289
290 if (!task) {
291 /*
292 * Per cpu counters are removed via an smp call and
293 * the removal is always sucessful.
294 */
295 smp_call_function_single(counter->cpu,
Ingo Molnar04289bb2008-12-11 08:38:42 +0100296 __perf_counter_remove_from_context,
Thomas Gleixner0793a612008-12-04 20:12:29 +0100297 counter, 1);
298 return;
299 }
300
301retry:
Ingo Molnar04289bb2008-12-11 08:38:42 +0100302 task_oncpu_function_call(task, __perf_counter_remove_from_context,
Thomas Gleixner0793a612008-12-04 20:12:29 +0100303 counter);
304
305 spin_lock_irq(&ctx->lock);
306 /*
307 * If the context is active we need to retry the smp call.
308 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100309 if (ctx->nr_active && !list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100310 spin_unlock_irq(&ctx->lock);
311 goto retry;
312 }
313
314 /*
315 * The lock prevents that this context is scheduled in so we
Ingo Molnar04289bb2008-12-11 08:38:42 +0100316 * can remove the counter safely, if the call above did not
Thomas Gleixner0793a612008-12-04 20:12:29 +0100317 * succeed.
318 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100319 if (!list_empty(&counter->list_entry)) {
Ingo Molnar04289bb2008-12-11 08:38:42 +0100320 list_del_counter(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100321 }
322 spin_unlock_irq(&ctx->lock);
323}
324
Peter Zijlstra4af49982009-04-06 11:45:10 +0200325static inline u64 perf_clock(void)
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100326{
Peter Zijlstra4af49982009-04-06 11:45:10 +0200327 return cpu_clock(smp_processor_id());
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100328}
329
330/*
331 * Update the record of the current time in a context.
332 */
Peter Zijlstra4af49982009-04-06 11:45:10 +0200333static void update_context_time(struct perf_counter_context *ctx)
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100334{
Peter Zijlstra4af49982009-04-06 11:45:10 +0200335 u64 now = perf_clock();
336
337 ctx->time += now - ctx->timestamp;
338 ctx->timestamp = now;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100339}
340
341/*
342 * Update the total_time_enabled and total_time_running fields for a counter.
343 */
344static void update_counter_times(struct perf_counter *counter)
345{
346 struct perf_counter_context *ctx = counter->ctx;
347 u64 run_end;
348
Peter Zijlstra4af49982009-04-06 11:45:10 +0200349 if (counter->state < PERF_COUNTER_STATE_INACTIVE)
350 return;
351
352 counter->total_time_enabled = ctx->time - counter->tstamp_enabled;
353
354 if (counter->state == PERF_COUNTER_STATE_INACTIVE)
355 run_end = counter->tstamp_stopped;
356 else
357 run_end = ctx->time;
358
359 counter->total_time_running = run_end - counter->tstamp_running;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100360}
361
362/*
363 * Update total_time_enabled and total_time_running for all counters in a group.
364 */
365static void update_group_times(struct perf_counter *leader)
366{
367 struct perf_counter *counter;
368
369 update_counter_times(leader);
370 list_for_each_entry(counter, &leader->sibling_list, list_entry)
371 update_counter_times(counter);
372}
373
374/*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100375 * Cross CPU call to disable a performance counter
376 */
377static void __perf_counter_disable(void *info)
378{
379 struct perf_counter *counter = info;
380 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
381 struct perf_counter_context *ctx = counter->ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100382
383 /*
384 * If this is a per-task counter, need to check whether this
385 * counter's task is the current task on this cpu.
386 */
Peter Zijlstra665c2142009-05-29 14:51:57 +0200387 if (ctx->task && cpuctx->task_ctx != ctx)
Paul Mackerrasd859e292009-01-17 18:10:22 +1100388 return;
389
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200390 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100391
392 /*
393 * If the counter is on, turn it off.
394 * If it is in error state, leave it in error state.
395 */
396 if (counter->state >= PERF_COUNTER_STATE_INACTIVE) {
Peter Zijlstra4af49982009-04-06 11:45:10 +0200397 update_context_time(ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100398 update_counter_times(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100399 if (counter == counter->group_leader)
400 group_sched_out(counter, cpuctx, ctx);
401 else
402 counter_sched_out(counter, cpuctx, ctx);
403 counter->state = PERF_COUNTER_STATE_OFF;
404 }
405
Peter Zijlstra665c2142009-05-29 14:51:57 +0200406 spin_unlock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100407}
408
409/*
410 * Disable a counter.
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000411 *
412 * If counter->ctx is a cloned context, callers must make sure that
413 * every task struct that counter->ctx->task could possibly point to
414 * remains valid. This condition is satisifed when called through
415 * perf_counter_for_each_child or perf_counter_for_each because they
416 * hold the top-level counter's child_mutex, so any descendant that
417 * goes to exit will block in sync_child_counter.
418 * When called from perf_pending_counter it's OK because counter->ctx
419 * is the current context on this CPU and preemption is disabled,
420 * hence we can't get into perf_counter_task_sched_out for this context.
Paul Mackerrasd859e292009-01-17 18:10:22 +1100421 */
422static void perf_counter_disable(struct perf_counter *counter)
423{
424 struct perf_counter_context *ctx = counter->ctx;
425 struct task_struct *task = ctx->task;
426
427 if (!task) {
428 /*
429 * Disable the counter on the cpu that it's on
430 */
431 smp_call_function_single(counter->cpu, __perf_counter_disable,
432 counter, 1);
433 return;
434 }
435
436 retry:
437 task_oncpu_function_call(task, __perf_counter_disable, counter);
438
439 spin_lock_irq(&ctx->lock);
440 /*
441 * If the counter is still active, we need to retry the cross-call.
442 */
443 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
444 spin_unlock_irq(&ctx->lock);
445 goto retry;
446 }
447
448 /*
449 * Since we have the lock this context can't be scheduled
450 * in, so we can change the state safely.
451 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100452 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
453 update_counter_times(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100454 counter->state = PERF_COUNTER_STATE_OFF;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100455 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100456
457 spin_unlock_irq(&ctx->lock);
458}
459
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100460static int
461counter_sched_in(struct perf_counter *counter,
462 struct perf_cpu_context *cpuctx,
463 struct perf_counter_context *ctx,
464 int cpu)
465{
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100466 if (counter->state <= PERF_COUNTER_STATE_OFF)
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100467 return 0;
468
469 counter->state = PERF_COUNTER_STATE_ACTIVE;
470 counter->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */
471 /*
472 * The new state must be visible before we turn it on in the hardware:
473 */
474 smp_wmb();
475
Robert Richter4aeb0b42009-04-29 12:47:03 +0200476 if (counter->pmu->enable(counter)) {
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100477 counter->state = PERF_COUNTER_STATE_INACTIVE;
478 counter->oncpu = -1;
479 return -EAGAIN;
480 }
481
Peter Zijlstra4af49982009-04-06 11:45:10 +0200482 counter->tstamp_running += ctx->time - counter->tstamp_stopped;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100483
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100484 if (!is_software_counter(counter))
485 cpuctx->active_oncpu++;
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100486 ctx->nr_active++;
487
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100488 if (counter->hw_event.exclusive)
489 cpuctx->exclusive = 1;
490
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100491 return 0;
492}
493
Paul Mackerras6751b712009-05-11 12:08:02 +1000494static int
495group_sched_in(struct perf_counter *group_counter,
496 struct perf_cpu_context *cpuctx,
497 struct perf_counter_context *ctx,
498 int cpu)
499{
500 struct perf_counter *counter, *partial_group;
501 int ret;
502
503 if (group_counter->state == PERF_COUNTER_STATE_OFF)
504 return 0;
505
506 ret = hw_perf_group_sched_in(group_counter, cpuctx, ctx, cpu);
507 if (ret)
508 return ret < 0 ? ret : 0;
509
510 group_counter->prev_state = group_counter->state;
511 if (counter_sched_in(group_counter, cpuctx, ctx, cpu))
512 return -EAGAIN;
513
514 /*
515 * Schedule in siblings as one group (if any):
516 */
517 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
518 counter->prev_state = counter->state;
519 if (counter_sched_in(counter, cpuctx, ctx, cpu)) {
520 partial_group = counter;
521 goto group_error;
522 }
523 }
524
525 return 0;
526
527group_error:
528 /*
529 * Groups can be scheduled in as one unit only, so undo any
530 * partial group before returning:
531 */
532 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
533 if (counter == partial_group)
534 break;
535 counter_sched_out(counter, cpuctx, ctx);
536 }
537 counter_sched_out(group_counter, cpuctx, ctx);
538
539 return -EAGAIN;
540}
541
Thomas Gleixner0793a612008-12-04 20:12:29 +0100542/*
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100543 * Return 1 for a group consisting entirely of software counters,
544 * 0 if the group contains any hardware counters.
545 */
546static int is_software_only_group(struct perf_counter *leader)
547{
548 struct perf_counter *counter;
549
550 if (!is_software_counter(leader))
551 return 0;
Peter Zijlstra5c148192009-03-25 12:30:23 +0100552
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100553 list_for_each_entry(counter, &leader->sibling_list, list_entry)
554 if (!is_software_counter(counter))
555 return 0;
Peter Zijlstra5c148192009-03-25 12:30:23 +0100556
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100557 return 1;
558}
559
560/*
561 * Work out whether we can put this counter group on the CPU now.
562 */
563static int group_can_go_on(struct perf_counter *counter,
564 struct perf_cpu_context *cpuctx,
565 int can_add_hw)
566{
567 /*
568 * Groups consisting entirely of software counters can always go on.
569 */
570 if (is_software_only_group(counter))
571 return 1;
572 /*
573 * If an exclusive group is already on, no other hardware
574 * counters can go on.
575 */
576 if (cpuctx->exclusive)
577 return 0;
578 /*
579 * If this group is exclusive and there are already
580 * counters on the CPU, it can't go on.
581 */
582 if (counter->hw_event.exclusive && cpuctx->active_oncpu)
583 return 0;
584 /*
585 * Otherwise, try to add it if all previous groups were able
586 * to go on.
587 */
588 return can_add_hw;
589}
590
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100591static void add_counter_to_ctx(struct perf_counter *counter,
592 struct perf_counter_context *ctx)
593{
594 list_add_counter(counter, ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100595 counter->prev_state = PERF_COUNTER_STATE_OFF;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200596 counter->tstamp_enabled = ctx->time;
597 counter->tstamp_running = ctx->time;
598 counter->tstamp_stopped = ctx->time;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100599}
600
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100601/*
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100602 * Cross CPU call to install and enable a performance counter
Peter Zijlstra682076a2009-05-23 18:28:57 +0200603 *
604 * Must be called with ctx->mutex held
Thomas Gleixner0793a612008-12-04 20:12:29 +0100605 */
606static void __perf_install_in_context(void *info)
607{
608 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
609 struct perf_counter *counter = info;
610 struct perf_counter_context *ctx = counter->ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100611 struct perf_counter *leader = counter->group_leader;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100612 int cpu = smp_processor_id();
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100613 int err;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100614
615 /*
616 * If this is a task context, we need to check whether it is
617 * the current task context of this cpu. If not it has been
618 * scheduled out before the smp call arrived.
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000619 * Or possibly this is the right context but it isn't
620 * on this cpu because it had no counters.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100621 */
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000622 if (ctx->task && cpuctx->task_ctx != ctx) {
Peter Zijlstra665c2142009-05-29 14:51:57 +0200623 if (cpuctx->task_ctx || ctx->task != current)
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000624 return;
625 cpuctx->task_ctx = ctx;
626 }
Thomas Gleixner0793a612008-12-04 20:12:29 +0100627
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200628 spin_lock(&ctx->lock);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000629 ctx->is_active = 1;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200630 update_context_time(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100631
632 /*
633 * Protect the list operation against NMI by disabling the
634 * counters on a global level. NOP for non NMI based counters.
635 */
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200636 perf_disable();
Thomas Gleixner0793a612008-12-04 20:12:29 +0100637
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100638 add_counter_to_ctx(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100639
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100640 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100641 * Don't put the counter on if it is disabled or if
642 * it is in a group and the group isn't on.
643 */
644 if (counter->state != PERF_COUNTER_STATE_INACTIVE ||
645 (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE))
646 goto unlock;
647
648 /*
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100649 * An exclusive counter can't go on if there are already active
650 * hardware counters, and no hardware counter can go on if there
651 * is already an exclusive counter on.
652 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100653 if (!group_can_go_on(counter, cpuctx, 1))
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100654 err = -EEXIST;
655 else
656 err = counter_sched_in(counter, cpuctx, ctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100657
Paul Mackerrasd859e292009-01-17 18:10:22 +1100658 if (err) {
659 /*
660 * This counter couldn't go on. If it is in a group
661 * then we have to pull the whole group off.
662 * If the counter group is pinned then put it in error state.
663 */
664 if (leader != counter)
665 group_sched_out(leader, cpuctx, ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100666 if (leader->hw_event.pinned) {
667 update_group_times(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100668 leader->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100669 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100670 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100671
672 if (!err && !ctx->task && cpuctx->max_pertask)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100673 cpuctx->max_pertask--;
674
Paul Mackerrasd859e292009-01-17 18:10:22 +1100675 unlock:
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200676 perf_enable();
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100677
Peter Zijlstra665c2142009-05-29 14:51:57 +0200678 spin_unlock(&ctx->lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100679}
680
681/*
682 * Attach a performance counter to a context
683 *
684 * First we add the counter to the list with the hardware enable bit
685 * in counter->hw_config cleared.
686 *
687 * If the counter is attached to a task which is on a CPU we use a smp
688 * call to enable it in the task context. The task might have been
689 * scheduled away, but we check this in the smp call again.
Paul Mackerrasd859e292009-01-17 18:10:22 +1100690 *
691 * Must be called with ctx->mutex held.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100692 */
693static void
694perf_install_in_context(struct perf_counter_context *ctx,
695 struct perf_counter *counter,
696 int cpu)
697{
698 struct task_struct *task = ctx->task;
699
Thomas Gleixner0793a612008-12-04 20:12:29 +0100700 if (!task) {
701 /*
702 * Per cpu counters are installed via an smp call and
703 * the install is always sucessful.
704 */
705 smp_call_function_single(cpu, __perf_install_in_context,
706 counter, 1);
707 return;
708 }
709
Thomas Gleixner0793a612008-12-04 20:12:29 +0100710retry:
711 task_oncpu_function_call(task, __perf_install_in_context,
712 counter);
713
714 spin_lock_irq(&ctx->lock);
715 /*
Thomas Gleixner0793a612008-12-04 20:12:29 +0100716 * we need to retry the smp call.
717 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100718 if (ctx->is_active && list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100719 spin_unlock_irq(&ctx->lock);
720 goto retry;
721 }
722
723 /*
724 * The lock prevents that this context is scheduled in so we
725 * can add the counter safely, if it the call above did not
726 * succeed.
727 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100728 if (list_empty(&counter->list_entry))
729 add_counter_to_ctx(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100730 spin_unlock_irq(&ctx->lock);
731}
732
Paul Mackerrasd859e292009-01-17 18:10:22 +1100733/*
734 * Cross CPU call to enable a performance counter
735 */
736static void __perf_counter_enable(void *info)
Ingo Molnar04289bb2008-12-11 08:38:42 +0100737{
Paul Mackerrasd859e292009-01-17 18:10:22 +1100738 struct perf_counter *counter = info;
739 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
740 struct perf_counter_context *ctx = counter->ctx;
741 struct perf_counter *leader = counter->group_leader;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100742 int err;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100743
744 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100745 * If this is a per-task counter, need to check whether this
746 * counter's task is the current task on this cpu.
Ingo Molnar04289bb2008-12-11 08:38:42 +0100747 */
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000748 if (ctx->task && cpuctx->task_ctx != ctx) {
Peter Zijlstra665c2142009-05-29 14:51:57 +0200749 if (cpuctx->task_ctx || ctx->task != current)
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000750 return;
751 cpuctx->task_ctx = ctx;
752 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100753
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200754 spin_lock(&ctx->lock);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000755 ctx->is_active = 1;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200756 update_context_time(ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100757
Paul Mackerrasc07c99b2009-02-13 22:10:34 +1100758 counter->prev_state = counter->state;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100759 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
760 goto unlock;
761 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200762 counter->tstamp_enabled = ctx->time - counter->total_time_enabled;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100763
764 /*
765 * If the counter is in a group and isn't the group leader,
766 * then don't put it on unless the group is on.
767 */
768 if (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE)
769 goto unlock;
770
Paul Mackerrase758a332009-05-12 21:59:01 +1000771 if (!group_can_go_on(counter, cpuctx, 1)) {
Paul Mackerrasd859e292009-01-17 18:10:22 +1100772 err = -EEXIST;
Paul Mackerrase758a332009-05-12 21:59:01 +1000773 } else {
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200774 perf_disable();
Paul Mackerrase758a332009-05-12 21:59:01 +1000775 if (counter == leader)
776 err = group_sched_in(counter, cpuctx, ctx,
777 smp_processor_id());
778 else
779 err = counter_sched_in(counter, cpuctx, ctx,
780 smp_processor_id());
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200781 perf_enable();
Paul Mackerrase758a332009-05-12 21:59:01 +1000782 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100783
784 if (err) {
785 /*
786 * If this counter can't go on and it's part of a
787 * group, then the whole group has to come off.
788 */
789 if (leader != counter)
790 group_sched_out(leader, cpuctx, ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100791 if (leader->hw_event.pinned) {
792 update_group_times(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100793 leader->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100794 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100795 }
796
797 unlock:
Peter Zijlstra665c2142009-05-29 14:51:57 +0200798 spin_unlock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100799}
800
801/*
802 * Enable a counter.
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000803 *
804 * If counter->ctx is a cloned context, callers must make sure that
805 * every task struct that counter->ctx->task could possibly point to
806 * remains valid. This condition is satisfied when called through
807 * perf_counter_for_each_child or perf_counter_for_each as described
808 * for perf_counter_disable.
Paul Mackerrasd859e292009-01-17 18:10:22 +1100809 */
810static void perf_counter_enable(struct perf_counter *counter)
811{
812 struct perf_counter_context *ctx = counter->ctx;
813 struct task_struct *task = ctx->task;
814
815 if (!task) {
816 /*
817 * Enable the counter on the cpu that it's on
818 */
819 smp_call_function_single(counter->cpu, __perf_counter_enable,
820 counter, 1);
821 return;
822 }
823
824 spin_lock_irq(&ctx->lock);
825 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
826 goto out;
827
828 /*
829 * If the counter is in error state, clear that first.
830 * That way, if we see the counter in error state below, we
831 * know that it has gone back into error state, as distinct
832 * from the task having been scheduled away before the
833 * cross-call arrived.
834 */
835 if (counter->state == PERF_COUNTER_STATE_ERROR)
836 counter->state = PERF_COUNTER_STATE_OFF;
837
838 retry:
839 spin_unlock_irq(&ctx->lock);
840 task_oncpu_function_call(task, __perf_counter_enable, counter);
841
842 spin_lock_irq(&ctx->lock);
843
844 /*
845 * If the context is active and the counter is still off,
846 * we need to retry the cross-call.
847 */
848 if (ctx->is_active && counter->state == PERF_COUNTER_STATE_OFF)
849 goto retry;
850
851 /*
852 * Since we have the lock this context can't be scheduled
853 * in, so we can change the state safely.
854 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100855 if (counter->state == PERF_COUNTER_STATE_OFF) {
Paul Mackerrasd859e292009-01-17 18:10:22 +1100856 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200857 counter->tstamp_enabled =
858 ctx->time - counter->total_time_enabled;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100859 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100860 out:
861 spin_unlock_irq(&ctx->lock);
862}
863
Peter Zijlstra2023b352009-05-05 17:50:26 +0200864static int perf_counter_refresh(struct perf_counter *counter, int refresh)
Peter Zijlstra79f14642009-04-06 11:45:07 +0200865{
Peter Zijlstra2023b352009-05-05 17:50:26 +0200866 /*
867 * not supported on inherited counters
868 */
869 if (counter->hw_event.inherit)
870 return -EINVAL;
871
Peter Zijlstra79f14642009-04-06 11:45:07 +0200872 atomic_add(refresh, &counter->event_limit);
873 perf_counter_enable(counter);
Peter Zijlstra2023b352009-05-05 17:50:26 +0200874
875 return 0;
Peter Zijlstra79f14642009-04-06 11:45:07 +0200876}
877
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100878void __perf_counter_sched_out(struct perf_counter_context *ctx,
879 struct perf_cpu_context *cpuctx)
880{
881 struct perf_counter *counter;
882
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100883 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100884 ctx->is_active = 0;
885 if (likely(!ctx->nr_counters))
886 goto out;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200887 update_context_time(ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100888
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200889 perf_disable();
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100890 if (ctx->nr_active) {
Peter Zijlstraafedadf2009-05-20 12:21:22 +0200891 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
892 if (counter != counter->group_leader)
893 counter_sched_out(counter, cpuctx, ctx);
894 else
895 group_sched_out(counter, cpuctx, ctx);
896 }
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100897 }
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200898 perf_enable();
Paul Mackerrasd859e292009-01-17 18:10:22 +1100899 out:
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100900 spin_unlock(&ctx->lock);
901}
902
Thomas Gleixner0793a612008-12-04 20:12:29 +0100903/*
Paul Mackerras564c2b22009-05-22 14:27:22 +1000904 * Test whether two contexts are equivalent, i.e. whether they
905 * have both been cloned from the same version of the same context
906 * and they both have the same number of enabled counters.
907 * If the number of enabled counters is the same, then the set
908 * of enabled counters should be the same, because these are both
909 * inherited contexts, therefore we can't access individual counters
910 * in them directly with an fd; we can only enable/disable all
911 * counters via prctl, or enable/disable all counters in a family
912 * via ioctl, which will have the same effect on both contexts.
913 */
914static int context_equiv(struct perf_counter_context *ctx1,
915 struct perf_counter_context *ctx2)
916{
917 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
Paul Mackerrasad3a37d2009-05-29 16:06:20 +1000918 && ctx1->parent_gen == ctx2->parent_gen
919 && ctx1->parent_gen != ~0ull;
Paul Mackerras564c2b22009-05-22 14:27:22 +1000920}
921
922/*
Thomas Gleixner0793a612008-12-04 20:12:29 +0100923 * Called from scheduler to remove the counters of the current task,
924 * with interrupts disabled.
925 *
926 * We stop each counter and update the counter value in counter->count.
927 *
Ingo Molnar76715812008-12-17 14:20:28 +0100928 * This does not protect us against NMI, but disable()
Thomas Gleixner0793a612008-12-04 20:12:29 +0100929 * sets the disabled bit in the control field of counter _before_
930 * accessing the counter control register. If a NMI hits, then it will
931 * not restart the counter.
932 */
Paul Mackerras564c2b22009-05-22 14:27:22 +1000933void perf_counter_task_sched_out(struct task_struct *task,
934 struct task_struct *next, int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100935{
936 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000937 struct perf_counter_context *ctx = task->perf_counter_ctxp;
Paul Mackerras564c2b22009-05-22 14:27:22 +1000938 struct perf_counter_context *next_ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000939 struct perf_counter_context *parent;
Peter Zijlstra4a0deca2009-03-19 20:26:12 +0100940 struct pt_regs *regs;
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000941 int do_switch = 1;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100942
Peter Zijlstra10989fb2009-05-25 14:45:28 +0200943 regs = task_pt_regs(task);
944 perf_swcounter_event(PERF_COUNT_CONTEXT_SWITCHES, 1, 1, regs, 0);
945
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000946 if (likely(!ctx || !cpuctx->task_ctx))
Thomas Gleixner0793a612008-12-04 20:12:29 +0100947 return;
948
Peter Zijlstrabce379b2009-04-06 11:45:13 +0200949 update_context_time(ctx);
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000950
951 rcu_read_lock();
952 parent = rcu_dereference(ctx->parent_ctx);
Paul Mackerras564c2b22009-05-22 14:27:22 +1000953 next_ctx = next->perf_counter_ctxp;
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000954 if (parent && next_ctx &&
955 rcu_dereference(next_ctx->parent_ctx) == parent) {
956 /*
957 * Looks like the two contexts are clones, so we might be
958 * able to optimize the context switch. We lock both
959 * contexts and check that they are clones under the
960 * lock (including re-checking that neither has been
961 * uncloned in the meantime). It doesn't matter which
962 * order we take the locks because no other cpu could
963 * be trying to lock both of these tasks.
964 */
965 spin_lock(&ctx->lock);
966 spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
967 if (context_equiv(ctx, next_ctx)) {
Peter Zijlstra665c2142009-05-29 14:51:57 +0200968 /*
969 * XXX do we need a memory barrier of sorts
970 * wrt to rcu_dereference() of perf_counter_ctxp
971 */
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000972 task->perf_counter_ctxp = next_ctx;
973 next->perf_counter_ctxp = ctx;
974 ctx->task = next;
975 next_ctx->task = task;
976 do_switch = 0;
977 }
978 spin_unlock(&next_ctx->lock);
979 spin_unlock(&ctx->lock);
Paul Mackerras564c2b22009-05-22 14:27:22 +1000980 }
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000981 rcu_read_unlock();
Paul Mackerras564c2b22009-05-22 14:27:22 +1000982
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000983 if (do_switch) {
984 __perf_counter_sched_out(ctx, cpuctx);
985 cpuctx->task_ctx = NULL;
986 }
Thomas Gleixner0793a612008-12-04 20:12:29 +0100987}
988
Peter Zijlstra665c2142009-05-29 14:51:57 +0200989/*
990 * Called with IRQs disabled
991 */
Paul Mackerrasa08b1592009-05-11 15:46:10 +1000992static void __perf_counter_task_sched_out(struct perf_counter_context *ctx)
993{
994 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
995
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000996 if (!cpuctx->task_ctx)
997 return;
Ingo Molnar012b84d2009-05-17 11:08:41 +0200998
999 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
1000 return;
1001
Paul Mackerrasa08b1592009-05-11 15:46:10 +10001002 __perf_counter_sched_out(ctx, cpuctx);
1003 cpuctx->task_ctx = NULL;
1004}
1005
Peter Zijlstra665c2142009-05-29 14:51:57 +02001006/*
1007 * Called with IRQs disabled
1008 */
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001009static void perf_counter_cpu_sched_out(struct perf_cpu_context *cpuctx)
Ingo Molnar04289bb2008-12-11 08:38:42 +01001010{
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001011 __perf_counter_sched_out(&cpuctx->ctx, cpuctx);
Ingo Molnar04289bb2008-12-11 08:38:42 +01001012}
1013
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001014static void
1015__perf_counter_sched_in(struct perf_counter_context *ctx,
1016 struct perf_cpu_context *cpuctx, int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001017{
Thomas Gleixner0793a612008-12-04 20:12:29 +01001018 struct perf_counter *counter;
Paul Mackerrasdd0e6ba2009-01-12 15:11:00 +11001019 int can_add_hw = 1;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001020
Thomas Gleixner0793a612008-12-04 20:12:29 +01001021 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001022 ctx->is_active = 1;
1023 if (likely(!ctx->nr_counters))
1024 goto out;
1025
Peter Zijlstra4af49982009-04-06 11:45:10 +02001026 ctx->timestamp = perf_clock();
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001027
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001028 perf_disable();
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001029
1030 /*
1031 * First go through the list and put on any pinned groups
1032 * in order to give them the best chance of going on.
1033 */
Ingo Molnar04289bb2008-12-11 08:38:42 +01001034 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001035 if (counter->state <= PERF_COUNTER_STATE_OFF ||
1036 !counter->hw_event.pinned)
1037 continue;
1038 if (counter->cpu != -1 && counter->cpu != cpu)
1039 continue;
1040
Peter Zijlstraafedadf2009-05-20 12:21:22 +02001041 if (counter != counter->group_leader)
1042 counter_sched_in(counter, cpuctx, ctx, cpu);
1043 else {
1044 if (group_can_go_on(counter, cpuctx, 1))
1045 group_sched_in(counter, cpuctx, ctx, cpu);
1046 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001047
1048 /*
1049 * If this pinned group hasn't been scheduled,
1050 * put it in error state.
1051 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001052 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
1053 update_group_times(counter);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001054 counter->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001055 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001056 }
1057
1058 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1059 /*
1060 * Ignore counters in OFF or ERROR state, and
1061 * ignore pinned counters since we did them already.
1062 */
1063 if (counter->state <= PERF_COUNTER_STATE_OFF ||
1064 counter->hw_event.pinned)
1065 continue;
1066
Ingo Molnar04289bb2008-12-11 08:38:42 +01001067 /*
1068 * Listen to the 'cpu' scheduling filter constraint
1069 * of counters:
1070 */
Thomas Gleixner0793a612008-12-04 20:12:29 +01001071 if (counter->cpu != -1 && counter->cpu != cpu)
1072 continue;
1073
Peter Zijlstraafedadf2009-05-20 12:21:22 +02001074 if (counter != counter->group_leader) {
1075 if (counter_sched_in(counter, cpuctx, ctx, cpu))
Paul Mackerrasdd0e6ba2009-01-12 15:11:00 +11001076 can_add_hw = 0;
Peter Zijlstraafedadf2009-05-20 12:21:22 +02001077 } else {
1078 if (group_can_go_on(counter, cpuctx, can_add_hw)) {
1079 if (group_sched_in(counter, cpuctx, ctx, cpu))
1080 can_add_hw = 0;
1081 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001082 }
Thomas Gleixner0793a612008-12-04 20:12:29 +01001083 }
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001084 perf_enable();
Paul Mackerrasd859e292009-01-17 18:10:22 +11001085 out:
Thomas Gleixner0793a612008-12-04 20:12:29 +01001086 spin_unlock(&ctx->lock);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001087}
Ingo Molnar04289bb2008-12-11 08:38:42 +01001088
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001089/*
1090 * Called from scheduler to add the counters of the current task
1091 * with interrupts disabled.
1092 *
1093 * We restore the counter value and then enable it.
1094 *
1095 * This does not protect us against NMI, but enable()
1096 * sets the enabled bit in the control field of counter _before_
1097 * accessing the counter control register. If a NMI hits, then it will
1098 * keep the counter running.
1099 */
1100void perf_counter_task_sched_in(struct task_struct *task, int cpu)
1101{
1102 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001103 struct perf_counter_context *ctx = task->perf_counter_ctxp;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001104
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001105 if (likely(!ctx))
1106 return;
Paul Mackerras564c2b22009-05-22 14:27:22 +10001107 if (cpuctx->task_ctx == ctx)
1108 return;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001109 __perf_counter_sched_in(ctx, cpuctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001110 cpuctx->task_ctx = ctx;
1111}
1112
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001113static void perf_counter_cpu_sched_in(struct perf_cpu_context *cpuctx, int cpu)
1114{
1115 struct perf_counter_context *ctx = &cpuctx->ctx;
1116
1117 __perf_counter_sched_in(ctx, cpuctx, cpu);
1118}
1119
Peter Zijlstraa78ac322009-05-25 17:39:05 +02001120#define MAX_INTERRUPTS (~0ULL)
1121
1122static void perf_log_throttle(struct perf_counter *counter, int enable);
Peter Zijlstra26b119b2009-05-20 12:21:20 +02001123static void perf_log_period(struct perf_counter *counter, u64 period);
1124
1125static void perf_adjust_freq(struct perf_counter_context *ctx)
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001126{
1127 struct perf_counter *counter;
Peter Zijlstraa78ac322009-05-25 17:39:05 +02001128 u64 interrupts, irq_period;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001129 u64 events, period;
1130 s64 delta;
1131
1132 spin_lock(&ctx->lock);
1133 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1134 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
1135 continue;
1136
Peter Zijlstraa78ac322009-05-25 17:39:05 +02001137 interrupts = counter->hw.interrupts;
1138 counter->hw.interrupts = 0;
1139
1140 if (interrupts == MAX_INTERRUPTS) {
1141 perf_log_throttle(counter, 1);
1142 counter->pmu->unthrottle(counter);
1143 interrupts = 2*sysctl_perf_counter_limit/HZ;
1144 }
1145
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001146 if (!counter->hw_event.freq || !counter->hw_event.irq_freq)
1147 continue;
1148
Peter Zijlstraa78ac322009-05-25 17:39:05 +02001149 events = HZ * interrupts * counter->hw.irq_period;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001150 period = div64_u64(events, counter->hw_event.irq_freq);
1151
1152 delta = (s64)(1 + period - counter->hw.irq_period);
1153 delta >>= 1;
1154
1155 irq_period = counter->hw.irq_period + delta;
1156
1157 if (!irq_period)
1158 irq_period = 1;
1159
Peter Zijlstra26b119b2009-05-20 12:21:20 +02001160 perf_log_period(counter, irq_period);
1161
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001162 counter->hw.irq_period = irq_period;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001163 }
1164 spin_unlock(&ctx->lock);
1165}
1166
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001167/*
1168 * Round-robin a context's counters:
1169 */
1170static void rotate_ctx(struct perf_counter_context *ctx)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001171{
Thomas Gleixner0793a612008-12-04 20:12:29 +01001172 struct perf_counter *counter;
1173
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001174 if (!ctx->nr_counters)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001175 return;
1176
Thomas Gleixner0793a612008-12-04 20:12:29 +01001177 spin_lock(&ctx->lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001178 /*
Ingo Molnar04289bb2008-12-11 08:38:42 +01001179 * Rotate the first entry last (works just fine for group counters too):
Thomas Gleixner0793a612008-12-04 20:12:29 +01001180 */
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001181 perf_disable();
Ingo Molnar04289bb2008-12-11 08:38:42 +01001182 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Peter Zijlstra75564232009-03-13 12:21:29 +01001183 list_move_tail(&counter->list_entry, &ctx->counter_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001184 break;
1185 }
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001186 perf_enable();
Thomas Gleixner0793a612008-12-04 20:12:29 +01001187
1188 spin_unlock(&ctx->lock);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001189}
Thomas Gleixner0793a612008-12-04 20:12:29 +01001190
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001191void perf_counter_task_tick(struct task_struct *curr, int cpu)
1192{
Peter Zijlstra7fc23a52009-05-08 18:52:21 +02001193 struct perf_cpu_context *cpuctx;
1194 struct perf_counter_context *ctx;
1195
1196 if (!atomic_read(&nr_counters))
1197 return;
1198
1199 cpuctx = &per_cpu(perf_cpu_context, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001200 ctx = curr->perf_counter_ctxp;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001201
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001202 perf_adjust_freq(&cpuctx->ctx);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001203 if (ctx)
1204 perf_adjust_freq(ctx);
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001205
Ingo Molnarb82914c2009-05-04 18:54:32 +02001206 perf_counter_cpu_sched_out(cpuctx);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001207 if (ctx)
1208 __perf_counter_task_sched_out(ctx);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001209
Ingo Molnarb82914c2009-05-04 18:54:32 +02001210 rotate_ctx(&cpuctx->ctx);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001211 if (ctx)
1212 rotate_ctx(ctx);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001213
Ingo Molnarb82914c2009-05-04 18:54:32 +02001214 perf_counter_cpu_sched_in(cpuctx, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001215 if (ctx)
1216 perf_counter_task_sched_in(curr, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001217}
1218
1219/*
Thomas Gleixner0793a612008-12-04 20:12:29 +01001220 * Cross CPU call to read the hardware counter
1221 */
Ingo Molnar76715812008-12-17 14:20:28 +01001222static void __read(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001223{
Ingo Molnar621a01e2008-12-11 12:46:46 +01001224 struct perf_counter *counter = info;
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001225 struct perf_counter_context *ctx = counter->ctx;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001226 unsigned long flags;
Ingo Molnar621a01e2008-12-11 12:46:46 +01001227
Peter Zijlstra849691a2009-04-06 11:45:12 +02001228 local_irq_save(flags);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001229 if (ctx->is_active)
Peter Zijlstra4af49982009-04-06 11:45:10 +02001230 update_context_time(ctx);
Robert Richter4aeb0b42009-04-29 12:47:03 +02001231 counter->pmu->read(counter);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001232 update_counter_times(counter);
Peter Zijlstra849691a2009-04-06 11:45:12 +02001233 local_irq_restore(flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001234}
1235
Ingo Molnar04289bb2008-12-11 08:38:42 +01001236static u64 perf_counter_read(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001237{
1238 /*
1239 * If counter is enabled and currently active on a CPU, update the
1240 * value in the counter structure:
1241 */
Ingo Molnar6a930702008-12-11 15:17:03 +01001242 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
Thomas Gleixner0793a612008-12-04 20:12:29 +01001243 smp_call_function_single(counter->oncpu,
Ingo Molnar76715812008-12-17 14:20:28 +01001244 __read, counter, 1);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001245 } else if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
1246 update_counter_times(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001247 }
1248
Ingo Molnaree060942008-12-13 09:00:03 +01001249 return atomic64_read(&counter->count);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001250}
1251
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001252/*
1253 * Initialize the perf_counter context in a task_struct:
1254 */
1255static void
1256__perf_counter_init_context(struct perf_counter_context *ctx,
1257 struct task_struct *task)
1258{
1259 memset(ctx, 0, sizeof(*ctx));
1260 spin_lock_init(&ctx->lock);
1261 mutex_init(&ctx->mutex);
1262 INIT_LIST_HEAD(&ctx->counter_list);
1263 INIT_LIST_HEAD(&ctx->event_list);
1264 atomic_set(&ctx->refcount, 1);
1265 ctx->task = task;
1266}
1267
Thomas Gleixner0793a612008-12-04 20:12:29 +01001268static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
1269{
1270 struct perf_cpu_context *cpuctx;
1271 struct perf_counter_context *ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001272 struct perf_counter_context *parent_ctx;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001273 struct task_struct *task;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001274 int err;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001275
1276 /*
1277 * If cpu is not a wildcard then this is a percpu counter:
1278 */
1279 if (cpu != -1) {
1280 /* Must be root to operate on a CPU counter: */
Peter Zijlstra1ccd1542009-04-09 10:53:45 +02001281 if (sysctl_perf_counter_priv && !capable(CAP_SYS_ADMIN))
Thomas Gleixner0793a612008-12-04 20:12:29 +01001282 return ERR_PTR(-EACCES);
1283
1284 if (cpu < 0 || cpu > num_possible_cpus())
1285 return ERR_PTR(-EINVAL);
1286
1287 /*
1288 * We could be clever and allow to attach a counter to an
1289 * offline CPU and activate it when the CPU comes up, but
1290 * that's for later.
1291 */
1292 if (!cpu_isset(cpu, cpu_online_map))
1293 return ERR_PTR(-ENODEV);
1294
1295 cpuctx = &per_cpu(perf_cpu_context, cpu);
1296 ctx = &cpuctx->ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001297 get_ctx(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001298
Thomas Gleixner0793a612008-12-04 20:12:29 +01001299 return ctx;
1300 }
1301
1302 rcu_read_lock();
1303 if (!pid)
1304 task = current;
1305 else
1306 task = find_task_by_vpid(pid);
1307 if (task)
1308 get_task_struct(task);
1309 rcu_read_unlock();
1310
1311 if (!task)
1312 return ERR_PTR(-ESRCH);
1313
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001314 /*
1315 * Can't attach counters to a dying task.
1316 */
1317 err = -ESRCH;
1318 if (task->flags & PF_EXITING)
1319 goto errout;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001320
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001321 /* Reuse ptrace permission checks for now. */
1322 err = -EACCES;
1323 if (!ptrace_may_access(task, PTRACE_MODE_READ))
1324 goto errout;
1325
1326 retry_lock:
1327 rcu_read_lock();
1328 retry:
1329 ctx = rcu_dereference(task->perf_counter_ctxp);
1330 if (ctx) {
1331 /*
1332 * If this context is a clone of another, it might
1333 * get swapped for another underneath us by
1334 * perf_counter_task_sched_out, though the
1335 * rcu_read_lock() protects us from any context
1336 * getting freed. Lock the context and check if it
1337 * got swapped before we could get the lock, and retry
1338 * if so. If we locked the right context, then it
1339 * can't get swapped on us any more and we can
1340 * unclone it if necessary.
1341 * Once it's not a clone things will be stable.
1342 */
1343 spin_lock_irq(&ctx->lock);
1344 if (ctx != rcu_dereference(task->perf_counter_ctxp)) {
1345 spin_unlock_irq(&ctx->lock);
1346 goto retry;
1347 }
1348 parent_ctx = ctx->parent_ctx;
1349 if (parent_ctx) {
1350 put_ctx(parent_ctx);
1351 ctx->parent_ctx = NULL; /* no longer a clone */
1352 }
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001353 /*
1354 * Get an extra reference before dropping the lock so that
1355 * this context won't get freed if the task exits.
1356 */
1357 get_ctx(ctx);
1358 spin_unlock_irq(&ctx->lock);
1359 }
1360 rcu_read_unlock();
1361
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001362 if (!ctx) {
1363 ctx = kmalloc(sizeof(struct perf_counter_context), GFP_KERNEL);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001364 err = -ENOMEM;
1365 if (!ctx)
1366 goto errout;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001367 __perf_counter_init_context(ctx, task);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001368 get_ctx(ctx);
1369 if (cmpxchg(&task->perf_counter_ctxp, NULL, ctx)) {
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001370 /*
1371 * We raced with some other task; use
1372 * the context they set.
1373 */
1374 kfree(ctx);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001375 goto retry_lock;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001376 }
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001377 get_task_struct(task);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001378 }
1379
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001380 put_task_struct(task);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001381 return ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001382
1383 errout:
1384 put_task_struct(task);
1385 return ERR_PTR(err);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001386}
1387
Peter Zijlstra592903c2009-03-13 12:21:36 +01001388static void free_counter_rcu(struct rcu_head *head)
1389{
1390 struct perf_counter *counter;
1391
1392 counter = container_of(head, struct perf_counter, rcu_head);
1393 kfree(counter);
1394}
1395
Peter Zijlstra925d5192009-03-30 19:07:02 +02001396static void perf_pending_sync(struct perf_counter *counter);
1397
Peter Zijlstraf1600952009-03-19 20:26:16 +01001398static void free_counter(struct perf_counter *counter)
1399{
Peter Zijlstra925d5192009-03-30 19:07:02 +02001400 perf_pending_sync(counter);
1401
Peter Zijlstra7fc23a52009-05-08 18:52:21 +02001402 atomic_dec(&nr_counters);
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02001403 if (counter->hw_event.mmap)
1404 atomic_dec(&nr_mmap_tracking);
1405 if (counter->hw_event.munmap)
1406 atomic_dec(&nr_munmap_tracking);
1407 if (counter->hw_event.comm)
1408 atomic_dec(&nr_comm_tracking);
1409
Peter Zijlstrae077df42009-03-19 20:26:17 +01001410 if (counter->destroy)
1411 counter->destroy(counter);
1412
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001413 put_ctx(counter->ctx);
Peter Zijlstraf1600952009-03-19 20:26:16 +01001414 call_rcu(&counter->rcu_head, free_counter_rcu);
1415}
1416
Thomas Gleixner0793a612008-12-04 20:12:29 +01001417/*
1418 * Called when the last reference to the file is gone.
1419 */
1420static int perf_release(struct inode *inode, struct file *file)
1421{
1422 struct perf_counter *counter = file->private_data;
1423 struct perf_counter_context *ctx = counter->ctx;
1424
1425 file->private_data = NULL;
1426
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001427 WARN_ON_ONCE(ctx->parent_ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001428 mutex_lock(&ctx->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01001429 perf_counter_remove_from_context(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001430 mutex_unlock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001431
Peter Zijlstra082ff5a2009-05-23 18:29:00 +02001432 mutex_lock(&counter->owner->perf_counter_mutex);
1433 list_del_init(&counter->owner_entry);
1434 mutex_unlock(&counter->owner->perf_counter_mutex);
1435 put_task_struct(counter->owner);
1436
Peter Zijlstraf1600952009-03-19 20:26:16 +01001437 free_counter(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001438
1439 return 0;
1440}
1441
1442/*
1443 * Read the performance counter - simple non blocking version for now
1444 */
1445static ssize_t
1446perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
1447{
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001448 u64 values[3];
1449 int n;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001450
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001451 /*
1452 * Return end-of-file for a read on a counter that is in
1453 * error state (i.e. because it was pinned but it couldn't be
1454 * scheduled on to the CPU at some point).
1455 */
1456 if (counter->state == PERF_COUNTER_STATE_ERROR)
1457 return 0;
1458
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001459 WARN_ON_ONCE(counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001460 mutex_lock(&counter->child_mutex);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001461 values[0] = perf_counter_read(counter);
1462 n = 1;
1463 if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1464 values[n++] = counter->total_time_enabled +
1465 atomic64_read(&counter->child_total_time_enabled);
1466 if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1467 values[n++] = counter->total_time_running +
1468 atomic64_read(&counter->child_total_time_running);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001469 mutex_unlock(&counter->child_mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001470
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001471 if (count < n * sizeof(u64))
1472 return -EINVAL;
1473 count = n * sizeof(u64);
1474
1475 if (copy_to_user(buf, values, count))
1476 return -EFAULT;
1477
1478 return count;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001479}
1480
1481static ssize_t
Thomas Gleixner0793a612008-12-04 20:12:29 +01001482perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
1483{
1484 struct perf_counter *counter = file->private_data;
1485
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001486 return perf_read_hw(counter, buf, count);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001487}
1488
1489static unsigned int perf_poll(struct file *file, poll_table *wait)
1490{
1491 struct perf_counter *counter = file->private_data;
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001492 struct perf_mmap_data *data;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001493 unsigned int events = POLL_HUP;
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001494
1495 rcu_read_lock();
1496 data = rcu_dereference(counter->data);
1497 if (data)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001498 events = atomic_xchg(&data->poll, 0);
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001499 rcu_read_unlock();
Thomas Gleixner0793a612008-12-04 20:12:29 +01001500
1501 poll_wait(file, &counter->waitq, wait);
1502
Thomas Gleixner0793a612008-12-04 20:12:29 +01001503 return events;
1504}
1505
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001506static void perf_counter_reset(struct perf_counter *counter)
1507{
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001508 (void)perf_counter_read(counter);
Paul Mackerras615a3f12009-05-11 15:50:21 +10001509 atomic64_set(&counter->count, 0);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001510 perf_counter_update_userpage(counter);
1511}
1512
1513static void perf_counter_for_each_sibling(struct perf_counter *counter,
1514 void (*func)(struct perf_counter *))
1515{
1516 struct perf_counter_context *ctx = counter->ctx;
1517 struct perf_counter *sibling;
1518
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001519 WARN_ON_ONCE(ctx->parent_ctx);
Peter Zijlstra682076a2009-05-23 18:28:57 +02001520 mutex_lock(&ctx->mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001521 counter = counter->group_leader;
1522
1523 func(counter);
1524 list_for_each_entry(sibling, &counter->sibling_list, list_entry)
1525 func(sibling);
Peter Zijlstra682076a2009-05-23 18:28:57 +02001526 mutex_unlock(&ctx->mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001527}
1528
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001529/*
1530 * Holding the top-level counter's child_mutex means that any
1531 * descendant process that has inherited this counter will block
1532 * in sync_child_counter if it goes to exit, thus satisfying the
1533 * task existence requirements of perf_counter_enable/disable.
1534 */
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001535static void perf_counter_for_each_child(struct perf_counter *counter,
1536 void (*func)(struct perf_counter *))
1537{
1538 struct perf_counter *child;
1539
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001540 WARN_ON_ONCE(counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001541 mutex_lock(&counter->child_mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001542 func(counter);
1543 list_for_each_entry(child, &counter->child_list, child_list)
1544 func(child);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001545 mutex_unlock(&counter->child_mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001546}
1547
1548static void perf_counter_for_each(struct perf_counter *counter,
1549 void (*func)(struct perf_counter *))
1550{
1551 struct perf_counter *child;
1552
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001553 WARN_ON_ONCE(counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001554 mutex_lock(&counter->child_mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001555 perf_counter_for_each_sibling(counter, func);
1556 list_for_each_entry(child, &counter->child_list, child_list)
1557 perf_counter_for_each_sibling(child, func);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001558 mutex_unlock(&counter->child_mutex);
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001559}
1560
Paul Mackerrasd859e292009-01-17 18:10:22 +11001561static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1562{
1563 struct perf_counter *counter = file->private_data;
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001564 void (*func)(struct perf_counter *);
1565 u32 flags = arg;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001566
1567 switch (cmd) {
1568 case PERF_COUNTER_IOC_ENABLE:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001569 func = perf_counter_enable;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001570 break;
1571 case PERF_COUNTER_IOC_DISABLE:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001572 func = perf_counter_disable;
Peter Zijlstra79f14642009-04-06 11:45:07 +02001573 break;
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001574 case PERF_COUNTER_IOC_RESET:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001575 func = perf_counter_reset;
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001576 break;
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001577
1578 case PERF_COUNTER_IOC_REFRESH:
1579 return perf_counter_refresh(counter, arg);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001580 default:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001581 return -ENOTTY;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001582 }
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001583
1584 if (flags & PERF_IOC_FLAG_GROUP)
1585 perf_counter_for_each(counter, func);
1586 else
1587 perf_counter_for_each_child(counter, func);
1588
1589 return 0;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001590}
1591
Peter Zijlstra771d7cd2009-05-25 14:45:26 +02001592int perf_counter_task_enable(void)
1593{
1594 struct perf_counter *counter;
1595
1596 mutex_lock(&current->perf_counter_mutex);
1597 list_for_each_entry(counter, &current->perf_counter_list, owner_entry)
1598 perf_counter_for_each_child(counter, perf_counter_enable);
1599 mutex_unlock(&current->perf_counter_mutex);
1600
1601 return 0;
1602}
1603
1604int perf_counter_task_disable(void)
1605{
1606 struct perf_counter *counter;
1607
1608 mutex_lock(&current->perf_counter_mutex);
1609 list_for_each_entry(counter, &current->perf_counter_list, owner_entry)
1610 perf_counter_for_each_child(counter, perf_counter_disable);
1611 mutex_unlock(&current->perf_counter_mutex);
1612
1613 return 0;
1614}
1615
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001616/*
1617 * Callers need to ensure there can be no nesting of this function, otherwise
1618 * the seqlock logic goes bad. We can not serialize this because the arch
1619 * code calls this from NMI context.
1620 */
1621void perf_counter_update_userpage(struct perf_counter *counter)
Paul Mackerras37d81822009-03-23 18:22:08 +01001622{
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001623 struct perf_mmap_data *data;
1624 struct perf_counter_mmap_page *userpg;
1625
1626 rcu_read_lock();
1627 data = rcu_dereference(counter->data);
1628 if (!data)
1629 goto unlock;
1630
1631 userpg = data->user_page;
Paul Mackerras37d81822009-03-23 18:22:08 +01001632
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001633 /*
1634 * Disable preemption so as to not let the corresponding user-space
1635 * spin too long if we get preempted.
1636 */
1637 preempt_disable();
Paul Mackerras37d81822009-03-23 18:22:08 +01001638 ++userpg->lock;
Peter Zijlstra92f22a32009-04-02 11:12:04 +02001639 barrier();
Paul Mackerras37d81822009-03-23 18:22:08 +01001640 userpg->index = counter->hw.idx;
1641 userpg->offset = atomic64_read(&counter->count);
1642 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
1643 userpg->offset -= atomic64_read(&counter->hw.prev_count);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001644
Peter Zijlstra92f22a32009-04-02 11:12:04 +02001645 barrier();
Paul Mackerras37d81822009-03-23 18:22:08 +01001646 ++userpg->lock;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001647 preempt_enable();
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001648unlock:
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001649 rcu_read_unlock();
Paul Mackerras37d81822009-03-23 18:22:08 +01001650}
1651
1652static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1653{
1654 struct perf_counter *counter = vma->vm_file->private_data;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001655 struct perf_mmap_data *data;
1656 int ret = VM_FAULT_SIGBUS;
Paul Mackerras37d81822009-03-23 18:22:08 +01001657
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001658 rcu_read_lock();
1659 data = rcu_dereference(counter->data);
1660 if (!data)
1661 goto unlock;
Paul Mackerras37d81822009-03-23 18:22:08 +01001662
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001663 if (vmf->pgoff == 0) {
1664 vmf->page = virt_to_page(data->user_page);
1665 } else {
1666 int nr = vmf->pgoff - 1;
1667
1668 if ((unsigned)nr > data->nr_pages)
1669 goto unlock;
1670
1671 vmf->page = virt_to_page(data->data_pages[nr]);
1672 }
Paul Mackerras37d81822009-03-23 18:22:08 +01001673 get_page(vmf->page);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001674 ret = 0;
1675unlock:
1676 rcu_read_unlock();
1677
1678 return ret;
1679}
1680
1681static int perf_mmap_data_alloc(struct perf_counter *counter, int nr_pages)
1682{
1683 struct perf_mmap_data *data;
1684 unsigned long size;
1685 int i;
1686
1687 WARN_ON(atomic_read(&counter->mmap_count));
1688
1689 size = sizeof(struct perf_mmap_data);
1690 size += nr_pages * sizeof(void *);
1691
1692 data = kzalloc(size, GFP_KERNEL);
1693 if (!data)
1694 goto fail;
1695
1696 data->user_page = (void *)get_zeroed_page(GFP_KERNEL);
1697 if (!data->user_page)
1698 goto fail_user_page;
1699
1700 for (i = 0; i < nr_pages; i++) {
1701 data->data_pages[i] = (void *)get_zeroed_page(GFP_KERNEL);
1702 if (!data->data_pages[i])
1703 goto fail_data_pages;
1704 }
1705
1706 data->nr_pages = nr_pages;
Peter Zijlstra22c15582009-05-05 17:50:25 +02001707 atomic_set(&data->lock, -1);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001708
1709 rcu_assign_pointer(counter->data, data);
1710
Paul Mackerras37d81822009-03-23 18:22:08 +01001711 return 0;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001712
1713fail_data_pages:
1714 for (i--; i >= 0; i--)
1715 free_page((unsigned long)data->data_pages[i]);
1716
1717 free_page((unsigned long)data->user_page);
1718
1719fail_user_page:
1720 kfree(data);
1721
1722fail:
1723 return -ENOMEM;
1724}
1725
1726static void __perf_mmap_data_free(struct rcu_head *rcu_head)
1727{
1728 struct perf_mmap_data *data = container_of(rcu_head,
1729 struct perf_mmap_data, rcu_head);
1730 int i;
1731
1732 free_page((unsigned long)data->user_page);
1733 for (i = 0; i < data->nr_pages; i++)
1734 free_page((unsigned long)data->data_pages[i]);
1735 kfree(data);
1736}
1737
1738static void perf_mmap_data_free(struct perf_counter *counter)
1739{
1740 struct perf_mmap_data *data = counter->data;
1741
1742 WARN_ON(atomic_read(&counter->mmap_count));
1743
1744 rcu_assign_pointer(counter->data, NULL);
1745 call_rcu(&data->rcu_head, __perf_mmap_data_free);
1746}
1747
1748static void perf_mmap_open(struct vm_area_struct *vma)
1749{
1750 struct perf_counter *counter = vma->vm_file->private_data;
1751
1752 atomic_inc(&counter->mmap_count);
1753}
1754
1755static void perf_mmap_close(struct vm_area_struct *vma)
1756{
1757 struct perf_counter *counter = vma->vm_file->private_data;
1758
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001759 WARN_ON_ONCE(counter->ctx->parent_ctx);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001760 if (atomic_dec_and_mutex_lock(&counter->mmap_count,
1761 &counter->mmap_mutex)) {
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001762 struct user_struct *user = current_user();
1763
1764 atomic_long_sub(counter->data->nr_pages + 1, &user->locked_vm);
Peter Zijlstrac5078f72009-05-05 17:50:24 +02001765 vma->vm_mm->locked_vm -= counter->data->nr_locked;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001766 perf_mmap_data_free(counter);
1767 mutex_unlock(&counter->mmap_mutex);
1768 }
Paul Mackerras37d81822009-03-23 18:22:08 +01001769}
1770
1771static struct vm_operations_struct perf_mmap_vmops = {
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001772 .open = perf_mmap_open,
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001773 .close = perf_mmap_close,
Paul Mackerras37d81822009-03-23 18:22:08 +01001774 .fault = perf_mmap_fault,
1775};
1776
1777static int perf_mmap(struct file *file, struct vm_area_struct *vma)
1778{
1779 struct perf_counter *counter = file->private_data;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001780 struct user_struct *user = current_user();
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001781 unsigned long vma_size;
1782 unsigned long nr_pages;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001783 unsigned long user_locked, user_lock_limit;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001784 unsigned long locked, lock_limit;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001785 long user_extra, extra;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001786 int ret = 0;
Paul Mackerras37d81822009-03-23 18:22:08 +01001787
1788 if (!(vma->vm_flags & VM_SHARED) || (vma->vm_flags & VM_WRITE))
1789 return -EINVAL;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001790
1791 vma_size = vma->vm_end - vma->vm_start;
1792 nr_pages = (vma_size / PAGE_SIZE) - 1;
1793
Peter Zijlstra7730d862009-03-25 12:48:31 +01001794 /*
1795 * If we have data pages ensure they're a power-of-two number, so we
1796 * can do bitmasks instead of modulo.
1797 */
1798 if (nr_pages != 0 && !is_power_of_2(nr_pages))
Paul Mackerras37d81822009-03-23 18:22:08 +01001799 return -EINVAL;
1800
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001801 if (vma_size != PAGE_SIZE * (1 + nr_pages))
Paul Mackerras37d81822009-03-23 18:22:08 +01001802 return -EINVAL;
1803
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001804 if (vma->vm_pgoff != 0)
1805 return -EINVAL;
Paul Mackerras37d81822009-03-23 18:22:08 +01001806
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001807 WARN_ON_ONCE(counter->ctx->parent_ctx);
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001808 mutex_lock(&counter->mmap_mutex);
1809 if (atomic_inc_not_zero(&counter->mmap_count)) {
1810 if (nr_pages != counter->data->nr_pages)
1811 ret = -EINVAL;
1812 goto unlock;
1813 }
1814
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001815 user_extra = nr_pages + 1;
1816 user_lock_limit = sysctl_perf_counter_mlock >> (PAGE_SHIFT - 10);
Ingo Molnara3862d32009-05-24 09:02:37 +02001817
1818 /*
1819 * Increase the limit linearly with more CPUs:
1820 */
1821 user_lock_limit *= num_online_cpus();
1822
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001823 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
Peter Zijlstrac5078f72009-05-05 17:50:24 +02001824
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001825 extra = 0;
1826 if (user_locked > user_lock_limit)
1827 extra = user_locked - user_lock_limit;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001828
1829 lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
1830 lock_limit >>= PAGE_SHIFT;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001831 locked = vma->vm_mm->locked_vm + extra;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001832
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001833 if ((locked > lock_limit) && !capable(CAP_IPC_LOCK)) {
1834 ret = -EPERM;
1835 goto unlock;
1836 }
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001837
1838 WARN_ON(counter->data);
1839 ret = perf_mmap_data_alloc(counter, nr_pages);
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001840 if (ret)
1841 goto unlock;
1842
1843 atomic_set(&counter->mmap_count, 1);
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001844 atomic_long_add(user_extra, &user->locked_vm);
Peter Zijlstrac5078f72009-05-05 17:50:24 +02001845 vma->vm_mm->locked_vm += extra;
1846 counter->data->nr_locked = extra;
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001847unlock:
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001848 mutex_unlock(&counter->mmap_mutex);
Paul Mackerras37d81822009-03-23 18:22:08 +01001849
1850 vma->vm_flags &= ~VM_MAYWRITE;
1851 vma->vm_flags |= VM_RESERVED;
1852 vma->vm_ops = &perf_mmap_vmops;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001853
1854 return ret;
Paul Mackerras37d81822009-03-23 18:22:08 +01001855}
1856
Peter Zijlstra3c446b3d2009-04-06 11:45:01 +02001857static int perf_fasync(int fd, struct file *filp, int on)
1858{
1859 struct perf_counter *counter = filp->private_data;
1860 struct inode *inode = filp->f_path.dentry->d_inode;
1861 int retval;
1862
1863 mutex_lock(&inode->i_mutex);
1864 retval = fasync_helper(fd, filp, on, &counter->fasync);
1865 mutex_unlock(&inode->i_mutex);
1866
1867 if (retval < 0)
1868 return retval;
1869
1870 return 0;
1871}
1872
Thomas Gleixner0793a612008-12-04 20:12:29 +01001873static const struct file_operations perf_fops = {
1874 .release = perf_release,
1875 .read = perf_read,
1876 .poll = perf_poll,
Paul Mackerrasd859e292009-01-17 18:10:22 +11001877 .unlocked_ioctl = perf_ioctl,
1878 .compat_ioctl = perf_ioctl,
Paul Mackerras37d81822009-03-23 18:22:08 +01001879 .mmap = perf_mmap,
Peter Zijlstra3c446b3d2009-04-06 11:45:01 +02001880 .fasync = perf_fasync,
Thomas Gleixner0793a612008-12-04 20:12:29 +01001881};
1882
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001883/*
Peter Zijlstra925d5192009-03-30 19:07:02 +02001884 * Perf counter wakeup
1885 *
1886 * If there's data, ensure we set the poll() state and publish everything
1887 * to user-space before waking everybody up.
1888 */
1889
1890void perf_counter_wakeup(struct perf_counter *counter)
1891{
Peter Zijlstra925d5192009-03-30 19:07:02 +02001892 wake_up_all(&counter->waitq);
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02001893
1894 if (counter->pending_kill) {
1895 kill_fasync(&counter->fasync, SIGIO, counter->pending_kill);
1896 counter->pending_kill = 0;
1897 }
Peter Zijlstra925d5192009-03-30 19:07:02 +02001898}
1899
1900/*
1901 * Pending wakeups
1902 *
1903 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
1904 *
1905 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
1906 * single linked list and use cmpxchg() to add entries lockless.
1907 */
1908
Peter Zijlstra79f14642009-04-06 11:45:07 +02001909static void perf_pending_counter(struct perf_pending_entry *entry)
1910{
1911 struct perf_counter *counter = container_of(entry,
1912 struct perf_counter, pending);
1913
1914 if (counter->pending_disable) {
1915 counter->pending_disable = 0;
1916 perf_counter_disable(counter);
1917 }
1918
1919 if (counter->pending_wakeup) {
1920 counter->pending_wakeup = 0;
1921 perf_counter_wakeup(counter);
1922 }
1923}
1924
Peter Zijlstra671dec52009-04-06 11:45:02 +02001925#define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
Peter Zijlstra925d5192009-03-30 19:07:02 +02001926
Peter Zijlstra671dec52009-04-06 11:45:02 +02001927static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = {
Peter Zijlstra925d5192009-03-30 19:07:02 +02001928 PENDING_TAIL,
1929};
1930
Peter Zijlstra671dec52009-04-06 11:45:02 +02001931static void perf_pending_queue(struct perf_pending_entry *entry,
1932 void (*func)(struct perf_pending_entry *))
Peter Zijlstra925d5192009-03-30 19:07:02 +02001933{
Peter Zijlstra671dec52009-04-06 11:45:02 +02001934 struct perf_pending_entry **head;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001935
Peter Zijlstra671dec52009-04-06 11:45:02 +02001936 if (cmpxchg(&entry->next, NULL, PENDING_TAIL) != NULL)
Peter Zijlstra925d5192009-03-30 19:07:02 +02001937 return;
1938
Peter Zijlstra671dec52009-04-06 11:45:02 +02001939 entry->func = func;
1940
1941 head = &get_cpu_var(perf_pending_head);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001942
1943 do {
Peter Zijlstra671dec52009-04-06 11:45:02 +02001944 entry->next = *head;
1945 } while (cmpxchg(head, entry->next, entry) != entry->next);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001946
1947 set_perf_counter_pending();
1948
Peter Zijlstra671dec52009-04-06 11:45:02 +02001949 put_cpu_var(perf_pending_head);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001950}
1951
1952static int __perf_pending_run(void)
1953{
Peter Zijlstra671dec52009-04-06 11:45:02 +02001954 struct perf_pending_entry *list;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001955 int nr = 0;
1956
Peter Zijlstra671dec52009-04-06 11:45:02 +02001957 list = xchg(&__get_cpu_var(perf_pending_head), PENDING_TAIL);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001958 while (list != PENDING_TAIL) {
Peter Zijlstra671dec52009-04-06 11:45:02 +02001959 void (*func)(struct perf_pending_entry *);
1960 struct perf_pending_entry *entry = list;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001961
1962 list = list->next;
1963
Peter Zijlstra671dec52009-04-06 11:45:02 +02001964 func = entry->func;
1965 entry->next = NULL;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001966 /*
1967 * Ensure we observe the unqueue before we issue the wakeup,
1968 * so that we won't be waiting forever.
1969 * -- see perf_not_pending().
1970 */
1971 smp_wmb();
1972
Peter Zijlstra671dec52009-04-06 11:45:02 +02001973 func(entry);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001974 nr++;
1975 }
1976
1977 return nr;
1978}
1979
1980static inline int perf_not_pending(struct perf_counter *counter)
1981{
1982 /*
1983 * If we flush on whatever cpu we run, there is a chance we don't
1984 * need to wait.
1985 */
1986 get_cpu();
1987 __perf_pending_run();
1988 put_cpu();
1989
1990 /*
1991 * Ensure we see the proper queue state before going to sleep
1992 * so that we do not miss the wakeup. -- see perf_pending_handle()
1993 */
1994 smp_rmb();
Peter Zijlstra671dec52009-04-06 11:45:02 +02001995 return counter->pending.next == NULL;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001996}
1997
1998static void perf_pending_sync(struct perf_counter *counter)
1999{
2000 wait_event(counter->waitq, perf_not_pending(counter));
2001}
2002
2003void perf_counter_do_pending(void)
2004{
2005 __perf_pending_run();
2006}
2007
2008/*
Peter Zijlstra394ee072009-03-30 19:07:14 +02002009 * Callchain support -- arch specific
2010 */
2011
Peter Zijlstra9c03d882009-04-06 11:45:00 +02002012__weak struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
Peter Zijlstra394ee072009-03-30 19:07:14 +02002013{
2014 return NULL;
2015}
2016
2017/*
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002018 * Output
2019 */
2020
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002021struct perf_output_handle {
2022 struct perf_counter *counter;
2023 struct perf_mmap_data *data;
2024 unsigned int offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01002025 unsigned int head;
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002026 int nmi;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002027 int overflow;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002028 int locked;
2029 unsigned long flags;
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002030};
2031
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002032static void perf_output_wakeup(struct perf_output_handle *handle)
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002033{
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002034 atomic_set(&handle->data->poll, POLL_IN);
2035
Peter Zijlstra671dec52009-04-06 11:45:02 +02002036 if (handle->nmi) {
Peter Zijlstra79f14642009-04-06 11:45:07 +02002037 handle->counter->pending_wakeup = 1;
Peter Zijlstra671dec52009-04-06 11:45:02 +02002038 perf_pending_queue(&handle->counter->pending,
Peter Zijlstra79f14642009-04-06 11:45:07 +02002039 perf_pending_counter);
Peter Zijlstra671dec52009-04-06 11:45:02 +02002040 } else
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002041 perf_counter_wakeup(handle->counter);
2042}
2043
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002044/*
2045 * Curious locking construct.
2046 *
2047 * We need to ensure a later event doesn't publish a head when a former
2048 * event isn't done writing. However since we need to deal with NMIs we
2049 * cannot fully serialize things.
2050 *
2051 * What we do is serialize between CPUs so we only have to deal with NMI
2052 * nesting on a single CPU.
2053 *
2054 * We only publish the head (and generate a wakeup) when the outer-most
2055 * event completes.
2056 */
2057static void perf_output_lock(struct perf_output_handle *handle)
2058{
2059 struct perf_mmap_data *data = handle->data;
2060 int cpu;
2061
2062 handle->locked = 0;
2063
2064 local_irq_save(handle->flags);
2065 cpu = smp_processor_id();
2066
2067 if (in_nmi() && atomic_read(&data->lock) == cpu)
2068 return;
2069
Peter Zijlstra22c15582009-05-05 17:50:25 +02002070 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002071 cpu_relax();
2072
2073 handle->locked = 1;
2074}
2075
2076static void perf_output_unlock(struct perf_output_handle *handle)
2077{
2078 struct perf_mmap_data *data = handle->data;
2079 int head, cpu;
2080
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002081 data->done_head = data->head;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002082
2083 if (!handle->locked)
2084 goto out;
2085
2086again:
2087 /*
2088 * The xchg implies a full barrier that ensures all writes are done
2089 * before we publish the new head, matched by a rmb() in userspace when
2090 * reading this position.
2091 */
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002092 while ((head = atomic_xchg(&data->done_head, 0)))
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002093 data->user_page->data_head = head;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002094
2095 /*
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002096 * NMI can happen here, which means we can miss a done_head update.
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002097 */
2098
Peter Zijlstra22c15582009-05-05 17:50:25 +02002099 cpu = atomic_xchg(&data->lock, -1);
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002100 WARN_ON_ONCE(cpu != smp_processor_id());
2101
2102 /*
2103 * Therefore we have to validate we did not indeed do so.
2104 */
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002105 if (unlikely(atomic_read(&data->done_head))) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002106 /*
2107 * Since we had it locked, we can lock it again.
2108 */
Peter Zijlstra22c15582009-05-05 17:50:25 +02002109 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002110 cpu_relax();
2111
2112 goto again;
2113 }
2114
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002115 if (atomic_xchg(&data->wakeup, 0))
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002116 perf_output_wakeup(handle);
2117out:
2118 local_irq_restore(handle->flags);
2119}
2120
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002121static int perf_output_begin(struct perf_output_handle *handle,
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002122 struct perf_counter *counter, unsigned int size,
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002123 int nmi, int overflow)
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002124{
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002125 struct perf_mmap_data *data;
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002126 unsigned int offset, head;
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002127
Peter Zijlstra2023b352009-05-05 17:50:26 +02002128 /*
2129 * For inherited counters we send all the output towards the parent.
2130 */
2131 if (counter->parent)
2132 counter = counter->parent;
2133
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002134 rcu_read_lock();
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002135 data = rcu_dereference(counter->data);
2136 if (!data)
2137 goto out;
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002138
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002139 handle->data = data;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002140 handle->counter = counter;
2141 handle->nmi = nmi;
2142 handle->overflow = overflow;
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002143
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002144 if (!data->nr_pages)
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002145 goto fail;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002146
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002147 perf_output_lock(handle);
2148
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002149 do {
2150 offset = head = atomic_read(&data->head);
Peter Zijlstrac7138f32009-03-24 13:18:16 +01002151 head += size;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002152 } while (atomic_cmpxchg(&data->head, offset, head) != offset);
2153
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002154 handle->offset = offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01002155 handle->head = head;
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002156
2157 if ((offset >> PAGE_SHIFT) != (head >> PAGE_SHIFT))
2158 atomic_set(&data->wakeup, 1);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002159
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002160 return 0;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002161
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002162fail:
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002163 perf_output_wakeup(handle);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002164out:
2165 rcu_read_unlock();
2166
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002167 return -ENOSPC;
2168}
2169
2170static void perf_output_copy(struct perf_output_handle *handle,
2171 void *buf, unsigned int len)
2172{
2173 unsigned int pages_mask;
2174 unsigned int offset;
2175 unsigned int size;
2176 void **pages;
2177
2178 offset = handle->offset;
2179 pages_mask = handle->data->nr_pages - 1;
2180 pages = handle->data->data_pages;
2181
2182 do {
2183 unsigned int page_offset;
2184 int nr;
2185
2186 nr = (offset >> PAGE_SHIFT) & pages_mask;
2187 page_offset = offset & (PAGE_SIZE - 1);
2188 size = min_t(unsigned int, PAGE_SIZE - page_offset, len);
2189
2190 memcpy(pages[nr] + page_offset, buf, size);
2191
2192 len -= size;
2193 buf += size;
2194 offset += size;
2195 } while (len);
2196
2197 handle->offset = offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01002198
Peter Zijlstra53020fe2009-05-13 21:26:19 +02002199 /*
2200 * Check we didn't copy past our reservation window, taking the
2201 * possible unsigned int wrap into account.
2202 */
2203 WARN_ON_ONCE(((int)(handle->head - handle->offset)) < 0);
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002204}
2205
Peter Zijlstra5c148192009-03-25 12:30:23 +01002206#define perf_output_put(handle, x) \
2207 perf_output_copy((handle), &(x), sizeof(x))
2208
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002209static void perf_output_end(struct perf_output_handle *handle)
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002210{
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002211 struct perf_counter *counter = handle->counter;
2212 struct perf_mmap_data *data = handle->data;
2213
2214 int wakeup_events = counter->hw_event.wakeup_events;
Peter Zijlstrac4578102009-04-02 11:12:01 +02002215
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002216 if (handle->overflow && wakeup_events) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002217 int events = atomic_inc_return(&data->events);
Peter Zijlstrac4578102009-04-02 11:12:01 +02002218 if (events >= wakeup_events) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002219 atomic_sub(wakeup_events, &data->events);
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002220 atomic_set(&data->wakeup, 1);
Peter Zijlstrac4578102009-04-02 11:12:01 +02002221 }
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002222 }
2223
2224 perf_output_unlock(handle);
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002225 rcu_read_unlock();
2226}
2227
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002228static void perf_counter_output(struct perf_counter *counter,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002229 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002230{
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002231 int ret;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002232 u64 record_type = counter->hw_event.record_type;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002233 struct perf_output_handle handle;
2234 struct perf_event_header header;
2235 u64 ip;
Peter Zijlstra5c148192009-03-25 12:30:23 +01002236 struct {
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002237 u32 pid, tid;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002238 } tid_entry;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002239 struct {
2240 u64 event;
2241 u64 counter;
2242 } group_entry;
Peter Zijlstra394ee072009-03-30 19:07:14 +02002243 struct perf_callchain_entry *callchain = NULL;
2244 int callchain_size = 0;
Peter Zijlstra339f7c92009-04-06 11:45:06 +02002245 u64 time;
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002246 struct {
2247 u32 cpu, reserved;
2248 } cpu_entry;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002249
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02002250 header.type = 0;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002251 header.size = sizeof(header);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002252
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02002253 header.misc = PERF_EVENT_MISC_OVERFLOW;
Paul Mackerras9d23a902009-05-14 21:48:08 +10002254 header.misc |= perf_misc_flags(regs);
Peter Zijlstra6fab0192009-04-08 15:01:26 +02002255
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002256 if (record_type & PERF_RECORD_IP) {
Paul Mackerras9d23a902009-05-14 21:48:08 +10002257 ip = perf_instruction_pointer(regs);
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02002258 header.type |= PERF_RECORD_IP;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002259 header.size += sizeof(ip);
2260 }
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002261
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002262 if (record_type & PERF_RECORD_TID) {
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002263 /* namespace issues */
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002264 tid_entry.pid = current->group_leader->pid;
2265 tid_entry.tid = current->pid;
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002266
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02002267 header.type |= PERF_RECORD_TID;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002268 header.size += sizeof(tid_entry);
2269 }
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002270
Peter Zijlstra4d855452009-04-08 15:01:32 +02002271 if (record_type & PERF_RECORD_TIME) {
2272 /*
2273 * Maybe do better on x86 and provide cpu_clock_nmi()
2274 */
2275 time = sched_clock();
2276
2277 header.type |= PERF_RECORD_TIME;
2278 header.size += sizeof(u64);
2279 }
2280
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002281 if (record_type & PERF_RECORD_ADDR) {
2282 header.type |= PERF_RECORD_ADDR;
2283 header.size += sizeof(u64);
2284 }
2285
Peter Zijlstraa85f61a2009-05-08 18:52:23 +02002286 if (record_type & PERF_RECORD_CONFIG) {
2287 header.type |= PERF_RECORD_CONFIG;
2288 header.size += sizeof(u64);
2289 }
2290
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002291 if (record_type & PERF_RECORD_CPU) {
2292 header.type |= PERF_RECORD_CPU;
2293 header.size += sizeof(cpu_entry);
2294
2295 cpu_entry.cpu = raw_smp_processor_id();
2296 }
2297
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002298 if (record_type & PERF_RECORD_GROUP) {
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02002299 header.type |= PERF_RECORD_GROUP;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002300 header.size += sizeof(u64) +
2301 counter->nr_siblings * sizeof(group_entry);
2302 }
2303
2304 if (record_type & PERF_RECORD_CALLCHAIN) {
Peter Zijlstra394ee072009-03-30 19:07:14 +02002305 callchain = perf_callchain(regs);
2306
2307 if (callchain) {
Peter Zijlstra9c03d882009-04-06 11:45:00 +02002308 callchain_size = (1 + callchain->nr) * sizeof(u64);
Peter Zijlstra394ee072009-03-30 19:07:14 +02002309
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02002310 header.type |= PERF_RECORD_CALLCHAIN;
Peter Zijlstra394ee072009-03-30 19:07:14 +02002311 header.size += callchain_size;
2312 }
2313 }
2314
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002315 ret = perf_output_begin(&handle, counter, header.size, nmi, 1);
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002316 if (ret)
2317 return;
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002318
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002319 perf_output_put(&handle, header);
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002320
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002321 if (record_type & PERF_RECORD_IP)
2322 perf_output_put(&handle, ip);
2323
2324 if (record_type & PERF_RECORD_TID)
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002325 perf_output_put(&handle, tid_entry);
2326
Peter Zijlstra4d855452009-04-08 15:01:32 +02002327 if (record_type & PERF_RECORD_TIME)
2328 perf_output_put(&handle, time);
2329
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002330 if (record_type & PERF_RECORD_ADDR)
2331 perf_output_put(&handle, addr);
2332
Peter Zijlstraa85f61a2009-05-08 18:52:23 +02002333 if (record_type & PERF_RECORD_CONFIG)
2334 perf_output_put(&handle, counter->hw_event.config);
2335
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002336 if (record_type & PERF_RECORD_CPU)
2337 perf_output_put(&handle, cpu_entry);
2338
Peter Zijlstra2023b352009-05-05 17:50:26 +02002339 /*
2340 * XXX PERF_RECORD_GROUP vs inherited counters seems difficult.
2341 */
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002342 if (record_type & PERF_RECORD_GROUP) {
2343 struct perf_counter *leader, *sub;
2344 u64 nr = counter->nr_siblings;
2345
2346 perf_output_put(&handle, nr);
2347
2348 leader = counter->group_leader;
2349 list_for_each_entry(sub, &leader->sibling_list, list_entry) {
2350 if (sub != counter)
Robert Richter4aeb0b42009-04-29 12:47:03 +02002351 sub->pmu->read(sub);
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002352
2353 group_entry.event = sub->hw_event.config;
2354 group_entry.counter = atomic64_read(&sub->count);
2355
2356 perf_output_put(&handle, group_entry);
2357 }
2358 }
2359
Peter Zijlstra394ee072009-03-30 19:07:14 +02002360 if (callchain)
2361 perf_output_copy(&handle, callchain, callchain_size);
2362
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002363 perf_output_end(&handle);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002364}
2365
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002366/*
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002367 * comm tracking
2368 */
2369
2370struct perf_comm_event {
2371 struct task_struct *task;
2372 char *comm;
2373 int comm_size;
2374
2375 struct {
2376 struct perf_event_header header;
2377
2378 u32 pid;
2379 u32 tid;
2380 } event;
2381};
2382
2383static void perf_counter_comm_output(struct perf_counter *counter,
2384 struct perf_comm_event *comm_event)
2385{
2386 struct perf_output_handle handle;
2387 int size = comm_event->event.header.size;
2388 int ret = perf_output_begin(&handle, counter, size, 0, 0);
2389
2390 if (ret)
2391 return;
2392
2393 perf_output_put(&handle, comm_event->event);
2394 perf_output_copy(&handle, comm_event->comm,
2395 comm_event->comm_size);
2396 perf_output_end(&handle);
2397}
2398
2399static int perf_counter_comm_match(struct perf_counter *counter,
2400 struct perf_comm_event *comm_event)
2401{
2402 if (counter->hw_event.comm &&
2403 comm_event->event.header.type == PERF_EVENT_COMM)
2404 return 1;
2405
2406 return 0;
2407}
2408
2409static void perf_counter_comm_ctx(struct perf_counter_context *ctx,
2410 struct perf_comm_event *comm_event)
2411{
2412 struct perf_counter *counter;
2413
2414 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2415 return;
2416
2417 rcu_read_lock();
2418 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2419 if (perf_counter_comm_match(counter, comm_event))
2420 perf_counter_comm_output(counter, comm_event);
2421 }
2422 rcu_read_unlock();
2423}
2424
2425static void perf_counter_comm_event(struct perf_comm_event *comm_event)
2426{
2427 struct perf_cpu_context *cpuctx;
Peter Zijlstra665c2142009-05-29 14:51:57 +02002428 struct perf_counter_context *ctx;
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002429 unsigned int size;
2430 char *comm = comm_event->task->comm;
2431
Ingo Molnar888fcee2009-04-09 09:48:22 +02002432 size = ALIGN(strlen(comm)+1, sizeof(u64));
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002433
2434 comm_event->comm = comm;
2435 comm_event->comm_size = size;
2436
2437 comm_event->event.header.size = sizeof(comm_event->event) + size;
2438
2439 cpuctx = &get_cpu_var(perf_cpu_context);
2440 perf_counter_comm_ctx(&cpuctx->ctx, comm_event);
2441 put_cpu_var(perf_cpu_context);
Peter Zijlstra665c2142009-05-29 14:51:57 +02002442
2443 rcu_read_lock();
2444 /*
2445 * doesn't really matter which of the child contexts the
2446 * events ends up in.
2447 */
2448 ctx = rcu_dereference(current->perf_counter_ctxp);
2449 if (ctx)
2450 perf_counter_comm_ctx(ctx, comm_event);
2451 rcu_read_unlock();
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002452}
2453
2454void perf_counter_comm(struct task_struct *task)
2455{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002456 struct perf_comm_event comm_event;
2457
2458 if (!atomic_read(&nr_comm_tracking))
2459 return;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10002460
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002461 comm_event = (struct perf_comm_event){
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002462 .task = task,
2463 .event = {
2464 .header = { .type = PERF_EVENT_COMM, },
2465 .pid = task->group_leader->pid,
2466 .tid = task->pid,
2467 },
2468 };
2469
2470 perf_counter_comm_event(&comm_event);
2471}
2472
2473/*
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002474 * mmap tracking
2475 */
2476
2477struct perf_mmap_event {
2478 struct file *file;
2479 char *file_name;
2480 int file_size;
2481
2482 struct {
2483 struct perf_event_header header;
2484
2485 u32 pid;
2486 u32 tid;
2487 u64 start;
2488 u64 len;
2489 u64 pgoff;
2490 } event;
2491};
2492
2493static void perf_counter_mmap_output(struct perf_counter *counter,
2494 struct perf_mmap_event *mmap_event)
2495{
2496 struct perf_output_handle handle;
2497 int size = mmap_event->event.header.size;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002498 int ret = perf_output_begin(&handle, counter, size, 0, 0);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002499
2500 if (ret)
2501 return;
2502
2503 perf_output_put(&handle, mmap_event->event);
2504 perf_output_copy(&handle, mmap_event->file_name,
2505 mmap_event->file_size);
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002506 perf_output_end(&handle);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002507}
2508
2509static int perf_counter_mmap_match(struct perf_counter *counter,
2510 struct perf_mmap_event *mmap_event)
2511{
2512 if (counter->hw_event.mmap &&
2513 mmap_event->event.header.type == PERF_EVENT_MMAP)
2514 return 1;
2515
2516 if (counter->hw_event.munmap &&
2517 mmap_event->event.header.type == PERF_EVENT_MUNMAP)
2518 return 1;
2519
2520 return 0;
2521}
2522
2523static void perf_counter_mmap_ctx(struct perf_counter_context *ctx,
2524 struct perf_mmap_event *mmap_event)
2525{
2526 struct perf_counter *counter;
2527
2528 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2529 return;
2530
2531 rcu_read_lock();
2532 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2533 if (perf_counter_mmap_match(counter, mmap_event))
2534 perf_counter_mmap_output(counter, mmap_event);
2535 }
2536 rcu_read_unlock();
2537}
2538
2539static void perf_counter_mmap_event(struct perf_mmap_event *mmap_event)
2540{
2541 struct perf_cpu_context *cpuctx;
Peter Zijlstra665c2142009-05-29 14:51:57 +02002542 struct perf_counter_context *ctx;
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002543 struct file *file = mmap_event->file;
2544 unsigned int size;
2545 char tmp[16];
2546 char *buf = NULL;
2547 char *name;
2548
2549 if (file) {
2550 buf = kzalloc(PATH_MAX, GFP_KERNEL);
2551 if (!buf) {
2552 name = strncpy(tmp, "//enomem", sizeof(tmp));
2553 goto got_name;
2554 }
Peter Zijlstrad3d21c42009-04-09 10:53:46 +02002555 name = d_path(&file->f_path, buf, PATH_MAX);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002556 if (IS_ERR(name)) {
2557 name = strncpy(tmp, "//toolong", sizeof(tmp));
2558 goto got_name;
2559 }
2560 } else {
2561 name = strncpy(tmp, "//anon", sizeof(tmp));
2562 goto got_name;
2563 }
2564
2565got_name:
Ingo Molnar888fcee2009-04-09 09:48:22 +02002566 size = ALIGN(strlen(name)+1, sizeof(u64));
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002567
2568 mmap_event->file_name = name;
2569 mmap_event->file_size = size;
2570
2571 mmap_event->event.header.size = sizeof(mmap_event->event) + size;
2572
2573 cpuctx = &get_cpu_var(perf_cpu_context);
2574 perf_counter_mmap_ctx(&cpuctx->ctx, mmap_event);
2575 put_cpu_var(perf_cpu_context);
2576
Peter Zijlstra665c2142009-05-29 14:51:57 +02002577 rcu_read_lock();
2578 /*
2579 * doesn't really matter which of the child contexts the
2580 * events ends up in.
2581 */
2582 ctx = rcu_dereference(current->perf_counter_ctxp);
2583 if (ctx)
2584 perf_counter_mmap_ctx(ctx, mmap_event);
2585 rcu_read_unlock();
2586
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002587 kfree(buf);
2588}
2589
2590void perf_counter_mmap(unsigned long addr, unsigned long len,
2591 unsigned long pgoff, struct file *file)
2592{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002593 struct perf_mmap_event mmap_event;
2594
2595 if (!atomic_read(&nr_mmap_tracking))
2596 return;
2597
2598 mmap_event = (struct perf_mmap_event){
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002599 .file = file,
2600 .event = {
2601 .header = { .type = PERF_EVENT_MMAP, },
2602 .pid = current->group_leader->pid,
2603 .tid = current->pid,
2604 .start = addr,
2605 .len = len,
2606 .pgoff = pgoff,
2607 },
2608 };
2609
2610 perf_counter_mmap_event(&mmap_event);
2611}
2612
2613void perf_counter_munmap(unsigned long addr, unsigned long len,
2614 unsigned long pgoff, struct file *file)
2615{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002616 struct perf_mmap_event mmap_event;
2617
2618 if (!atomic_read(&nr_munmap_tracking))
2619 return;
2620
2621 mmap_event = (struct perf_mmap_event){
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002622 .file = file,
2623 .event = {
2624 .header = { .type = PERF_EVENT_MUNMAP, },
2625 .pid = current->group_leader->pid,
2626 .tid = current->pid,
2627 .start = addr,
2628 .len = len,
2629 .pgoff = pgoff,
2630 },
2631 };
2632
2633 perf_counter_mmap_event(&mmap_event);
2634}
2635
2636/*
Peter Zijlstrae220d2d2009-05-23 18:28:55 +02002637 * Log irq_period changes so that analyzing tools can re-normalize the
2638 * event flow.
Peter Zijlstra26b119b2009-05-20 12:21:20 +02002639 */
2640
2641static void perf_log_period(struct perf_counter *counter, u64 period)
2642{
2643 struct perf_output_handle handle;
2644 int ret;
2645
2646 struct {
2647 struct perf_event_header header;
2648 u64 time;
2649 u64 period;
2650 } freq_event = {
2651 .header = {
2652 .type = PERF_EVENT_PERIOD,
2653 .misc = 0,
2654 .size = sizeof(freq_event),
2655 },
2656 .time = sched_clock(),
2657 .period = period,
2658 };
2659
2660 if (counter->hw.irq_period == period)
2661 return;
2662
2663 ret = perf_output_begin(&handle, counter, sizeof(freq_event), 0, 0);
2664 if (ret)
2665 return;
2666
2667 perf_output_put(&handle, freq_event);
2668 perf_output_end(&handle);
2669}
2670
2671/*
Peter Zijlstraa78ac322009-05-25 17:39:05 +02002672 * IRQ throttle logging
2673 */
2674
2675static void perf_log_throttle(struct perf_counter *counter, int enable)
2676{
2677 struct perf_output_handle handle;
2678 int ret;
2679
2680 struct {
2681 struct perf_event_header header;
2682 u64 time;
2683 } throttle_event = {
2684 .header = {
2685 .type = PERF_EVENT_THROTTLE + 1,
2686 .misc = 0,
2687 .size = sizeof(throttle_event),
2688 },
2689 .time = sched_clock(),
2690 };
2691
Ingo Molnar0127c3e2009-05-25 22:03:26 +02002692 ret = perf_output_begin(&handle, counter, sizeof(throttle_event), 1, 0);
Peter Zijlstraa78ac322009-05-25 17:39:05 +02002693 if (ret)
2694 return;
2695
2696 perf_output_put(&handle, throttle_event);
2697 perf_output_end(&handle);
2698}
2699
2700/*
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002701 * Generic counter overflow handling.
2702 */
2703
2704int perf_counter_overflow(struct perf_counter *counter,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002705 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002706{
Peter Zijlstra79f14642009-04-06 11:45:07 +02002707 int events = atomic_read(&counter->event_limit);
Peter Zijlstraa78ac322009-05-25 17:39:05 +02002708 int throttle = counter->pmu->unthrottle != NULL;
Peter Zijlstra79f14642009-04-06 11:45:07 +02002709 int ret = 0;
2710
Peter Zijlstraa78ac322009-05-25 17:39:05 +02002711 if (!throttle) {
2712 counter->hw.interrupts++;
2713 } else if (counter->hw.interrupts != MAX_INTERRUPTS) {
2714 counter->hw.interrupts++;
2715 if (HZ*counter->hw.interrupts > (u64)sysctl_perf_counter_limit) {
2716 counter->hw.interrupts = MAX_INTERRUPTS;
2717 perf_log_throttle(counter, 0);
2718 ret = 1;
2719 }
2720 }
Peter Zijlstra60db5e02009-05-15 15:19:28 +02002721
Peter Zijlstra2023b352009-05-05 17:50:26 +02002722 /*
2723 * XXX event_limit might not quite work as expected on inherited
2724 * counters
2725 */
2726
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002727 counter->pending_kill = POLL_IN;
Peter Zijlstra79f14642009-04-06 11:45:07 +02002728 if (events && atomic_dec_and_test(&counter->event_limit)) {
2729 ret = 1;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002730 counter->pending_kill = POLL_HUP;
Peter Zijlstra79f14642009-04-06 11:45:07 +02002731 if (nmi) {
2732 counter->pending_disable = 1;
2733 perf_pending_queue(&counter->pending,
2734 perf_pending_counter);
2735 } else
2736 perf_counter_disable(counter);
2737 }
2738
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002739 perf_counter_output(counter, nmi, regs, addr);
Peter Zijlstra79f14642009-04-06 11:45:07 +02002740 return ret;
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002741}
2742
2743/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002744 * Generic software counter infrastructure
2745 */
2746
2747static void perf_swcounter_update(struct perf_counter *counter)
2748{
2749 struct hw_perf_counter *hwc = &counter->hw;
2750 u64 prev, now;
2751 s64 delta;
2752
2753again:
2754 prev = atomic64_read(&hwc->prev_count);
2755 now = atomic64_read(&hwc->count);
2756 if (atomic64_cmpxchg(&hwc->prev_count, prev, now) != prev)
2757 goto again;
2758
2759 delta = now - prev;
2760
2761 atomic64_add(delta, &counter->count);
2762 atomic64_sub(delta, &hwc->period_left);
2763}
2764
2765static void perf_swcounter_set_period(struct perf_counter *counter)
2766{
2767 struct hw_perf_counter *hwc = &counter->hw;
2768 s64 left = atomic64_read(&hwc->period_left);
2769 s64 period = hwc->irq_period;
2770
2771 if (unlikely(left <= -period)) {
2772 left = period;
2773 atomic64_set(&hwc->period_left, left);
2774 }
2775
2776 if (unlikely(left <= 0)) {
2777 left += period;
2778 atomic64_add(period, &hwc->period_left);
2779 }
2780
2781 atomic64_set(&hwc->prev_count, -left);
2782 atomic64_set(&hwc->count, -left);
2783}
2784
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002785static enum hrtimer_restart perf_swcounter_hrtimer(struct hrtimer *hrtimer)
2786{
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002787 enum hrtimer_restart ret = HRTIMER_RESTART;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002788 struct perf_counter *counter;
2789 struct pt_regs *regs;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02002790 u64 period;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002791
2792 counter = container_of(hrtimer, struct perf_counter, hw.hrtimer);
Robert Richter4aeb0b42009-04-29 12:47:03 +02002793 counter->pmu->read(counter);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002794
2795 regs = get_irq_regs();
2796 /*
2797 * In case we exclude kernel IPs or are somehow not in interrupt
2798 * context, provide the next best thing, the user IP.
2799 */
2800 if ((counter->hw_event.exclude_kernel || !regs) &&
2801 !counter->hw_event.exclude_user)
2802 regs = task_pt_regs(current);
2803
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002804 if (regs) {
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002805 if (perf_counter_overflow(counter, 0, regs, 0))
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002806 ret = HRTIMER_NORESTART;
2807 }
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002808
Peter Zijlstra60db5e02009-05-15 15:19:28 +02002809 period = max_t(u64, 10000, counter->hw.irq_period);
2810 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002811
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002812 return ret;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002813}
2814
2815static void perf_swcounter_overflow(struct perf_counter *counter,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002816 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002817{
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002818 perf_swcounter_update(counter);
2819 perf_swcounter_set_period(counter);
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002820 if (perf_counter_overflow(counter, nmi, regs, addr))
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002821 /* soft-disable the counter */
2822 ;
2823
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002824}
2825
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002826static int perf_swcounter_match(struct perf_counter *counter,
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002827 enum perf_event_types type,
2828 u32 event, struct pt_regs *regs)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002829{
2830 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
2831 return 0;
2832
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002833 if (perf_event_raw(&counter->hw_event))
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002834 return 0;
2835
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002836 if (perf_event_type(&counter->hw_event) != type)
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002837 return 0;
2838
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002839 if (perf_event_id(&counter->hw_event) != event)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002840 return 0;
2841
2842 if (counter->hw_event.exclude_user && user_mode(regs))
2843 return 0;
2844
2845 if (counter->hw_event.exclude_kernel && !user_mode(regs))
2846 return 0;
2847
2848 return 1;
2849}
2850
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002851static void perf_swcounter_add(struct perf_counter *counter, u64 nr,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002852 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002853{
2854 int neg = atomic64_add_negative(nr, &counter->hw.count);
2855 if (counter->hw.irq_period && !neg)
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002856 perf_swcounter_overflow(counter, nmi, regs, addr);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002857}
2858
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002859static void perf_swcounter_ctx_event(struct perf_counter_context *ctx,
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002860 enum perf_event_types type, u32 event,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002861 u64 nr, int nmi, struct pt_regs *regs,
2862 u64 addr)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002863{
2864 struct perf_counter *counter;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002865
Peter Zijlstra01ef09d2009-03-19 20:26:11 +01002866 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002867 return;
2868
Peter Zijlstra592903c2009-03-13 12:21:36 +01002869 rcu_read_lock();
2870 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002871 if (perf_swcounter_match(counter, type, event, regs))
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002872 perf_swcounter_add(counter, nr, nmi, regs, addr);
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002873 }
Peter Zijlstra592903c2009-03-13 12:21:36 +01002874 rcu_read_unlock();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002875}
2876
Peter Zijlstra96f6d442009-03-23 18:22:07 +01002877static int *perf_swcounter_recursion_context(struct perf_cpu_context *cpuctx)
2878{
2879 if (in_nmi())
2880 return &cpuctx->recursion[3];
2881
2882 if (in_irq())
2883 return &cpuctx->recursion[2];
2884
2885 if (in_softirq())
2886 return &cpuctx->recursion[1];
2887
2888 return &cpuctx->recursion[0];
2889}
2890
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002891static void __perf_swcounter_event(enum perf_event_types type, u32 event,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002892 u64 nr, int nmi, struct pt_regs *regs,
2893 u64 addr)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002894{
2895 struct perf_cpu_context *cpuctx = &get_cpu_var(perf_cpu_context);
Peter Zijlstra96f6d442009-03-23 18:22:07 +01002896 int *recursion = perf_swcounter_recursion_context(cpuctx);
Peter Zijlstra665c2142009-05-29 14:51:57 +02002897 struct perf_counter_context *ctx;
Peter Zijlstra96f6d442009-03-23 18:22:07 +01002898
2899 if (*recursion)
2900 goto out;
2901
2902 (*recursion)++;
2903 barrier();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002904
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002905 perf_swcounter_ctx_event(&cpuctx->ctx, type, event,
2906 nr, nmi, regs, addr);
Peter Zijlstra665c2142009-05-29 14:51:57 +02002907 rcu_read_lock();
2908 /*
2909 * doesn't really matter which of the child contexts the
2910 * events ends up in.
2911 */
2912 ctx = rcu_dereference(current->perf_counter_ctxp);
2913 if (ctx)
2914 perf_swcounter_ctx_event(ctx, type, event, nr, nmi, regs, addr);
2915 rcu_read_unlock();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002916
Peter Zijlstra96f6d442009-03-23 18:22:07 +01002917 barrier();
2918 (*recursion)--;
2919
2920out:
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002921 put_cpu_var(perf_cpu_context);
2922}
2923
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002924void
2925perf_swcounter_event(u32 event, u64 nr, int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002926{
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002927 __perf_swcounter_event(PERF_TYPE_SOFTWARE, event, nr, nmi, regs, addr);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002928}
2929
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002930static void perf_swcounter_read(struct perf_counter *counter)
2931{
2932 perf_swcounter_update(counter);
2933}
2934
2935static int perf_swcounter_enable(struct perf_counter *counter)
2936{
2937 perf_swcounter_set_period(counter);
2938 return 0;
2939}
2940
2941static void perf_swcounter_disable(struct perf_counter *counter)
2942{
2943 perf_swcounter_update(counter);
2944}
2945
Robert Richter4aeb0b42009-04-29 12:47:03 +02002946static const struct pmu perf_ops_generic = {
Peter Zijlstraac17dc82009-03-13 12:21:34 +01002947 .enable = perf_swcounter_enable,
2948 .disable = perf_swcounter_disable,
2949 .read = perf_swcounter_read,
2950};
2951
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002952/*
2953 * Software counter: cpu wall time clock
2954 */
2955
Paul Mackerras9abf8a02009-01-09 16:26:43 +11002956static void cpu_clock_perf_counter_update(struct perf_counter *counter)
2957{
2958 int cpu = raw_smp_processor_id();
2959 s64 prev;
2960 u64 now;
2961
2962 now = cpu_clock(cpu);
2963 prev = atomic64_read(&counter->hw.prev_count);
2964 atomic64_set(&counter->hw.prev_count, now);
2965 atomic64_add(now - prev, &counter->count);
2966}
2967
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002968static int cpu_clock_perf_counter_enable(struct perf_counter *counter)
2969{
2970 struct hw_perf_counter *hwc = &counter->hw;
2971 int cpu = raw_smp_processor_id();
2972
2973 atomic64_set(&hwc->prev_count, cpu_clock(cpu));
Peter Zijlstra039fc912009-03-13 16:43:47 +01002974 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2975 hwc->hrtimer.function = perf_swcounter_hrtimer;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002976 if (hwc->irq_period) {
Peter Zijlstra60db5e02009-05-15 15:19:28 +02002977 u64 period = max_t(u64, 10000, hwc->irq_period);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002978 __hrtimer_start_range_ns(&hwc->hrtimer,
Peter Zijlstra60db5e02009-05-15 15:19:28 +02002979 ns_to_ktime(period), 0,
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002980 HRTIMER_MODE_REL, 0);
2981 }
2982
2983 return 0;
2984}
2985
Ingo Molnar5c92d122008-12-11 13:21:10 +01002986static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
2987{
Peter Zijlstrab986d7e2009-05-20 12:21:21 +02002988 if (counter->hw.irq_period)
2989 hrtimer_cancel(&counter->hw.hrtimer);
Paul Mackerras9abf8a02009-01-09 16:26:43 +11002990 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01002991}
2992
2993static void cpu_clock_perf_counter_read(struct perf_counter *counter)
2994{
Paul Mackerras9abf8a02009-01-09 16:26:43 +11002995 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01002996}
2997
Robert Richter4aeb0b42009-04-29 12:47:03 +02002998static const struct pmu perf_ops_cpu_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +01002999 .enable = cpu_clock_perf_counter_enable,
3000 .disable = cpu_clock_perf_counter_disable,
3001 .read = cpu_clock_perf_counter_read,
Ingo Molnar5c92d122008-12-11 13:21:10 +01003002};
3003
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01003004/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003005 * Software counter: task time clock
3006 */
3007
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02003008static void task_clock_perf_counter_update(struct perf_counter *counter, u64 now)
Ingo Molnarbae43c92008-12-11 14:03:20 +01003009{
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02003010 u64 prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003011 s64 delta;
Ingo Molnarbae43c92008-12-11 14:03:20 +01003012
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02003013 prev = atomic64_xchg(&counter->hw.prev_count, now);
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003014 delta = now - prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003015 atomic64_add(delta, &counter->count);
Ingo Molnarbae43c92008-12-11 14:03:20 +01003016}
3017
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01003018static int task_clock_perf_counter_enable(struct perf_counter *counter)
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003019{
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003020 struct hw_perf_counter *hwc = &counter->hw;
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02003021 u64 now;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003022
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02003023 now = counter->ctx->time;
3024
3025 atomic64_set(&hwc->prev_count, now);
Peter Zijlstra039fc912009-03-13 16:43:47 +01003026 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
3027 hwc->hrtimer.function = perf_swcounter_hrtimer;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003028 if (hwc->irq_period) {
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003029 u64 period = max_t(u64, 10000, hwc->irq_period);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003030 __hrtimer_start_range_ns(&hwc->hrtimer,
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003031 ns_to_ktime(period), 0,
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003032 HRTIMER_MODE_REL, 0);
3033 }
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01003034
3035 return 0;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003036}
3037
3038static void task_clock_perf_counter_disable(struct perf_counter *counter)
3039{
Peter Zijlstrab986d7e2009-05-20 12:21:21 +02003040 if (counter->hw.irq_period)
3041 hrtimer_cancel(&counter->hw.hrtimer);
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02003042 task_clock_perf_counter_update(counter, counter->ctx->time);
3043
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003044}
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01003045
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003046static void task_clock_perf_counter_read(struct perf_counter *counter)
3047{
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02003048 u64 time;
3049
3050 if (!in_nmi()) {
3051 update_context_time(counter->ctx);
3052 time = counter->ctx->time;
3053 } else {
3054 u64 now = perf_clock();
3055 u64 delta = now - counter->ctx->timestamp;
3056 time = counter->ctx->time + delta;
3057 }
3058
3059 task_clock_perf_counter_update(counter, time);
Ingo Molnarbae43c92008-12-11 14:03:20 +01003060}
3061
Robert Richter4aeb0b42009-04-29 12:47:03 +02003062static const struct pmu perf_ops_task_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +01003063 .enable = task_clock_perf_counter_enable,
3064 .disable = task_clock_perf_counter_disable,
3065 .read = task_clock_perf_counter_read,
Ingo Molnarbae43c92008-12-11 14:03:20 +01003066};
3067
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003068/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003069 * Software counter: cpu migrations
3070 */
3071
Paul Mackerras23a185c2009-02-09 22:42:47 +11003072static inline u64 get_cpu_migrations(struct perf_counter *counter)
Ingo Molnar6c594c22008-12-14 12:34:15 +01003073{
Paul Mackerras23a185c2009-02-09 22:42:47 +11003074 struct task_struct *curr = counter->ctx->task;
3075
3076 if (curr)
3077 return curr->se.nr_migrations;
3078 return cpu_nr_migrations(smp_processor_id());
Ingo Molnar6c594c22008-12-14 12:34:15 +01003079}
3080
3081static void cpu_migrations_perf_counter_update(struct perf_counter *counter)
3082{
3083 u64 prev, now;
3084 s64 delta;
3085
3086 prev = atomic64_read(&counter->hw.prev_count);
Paul Mackerras23a185c2009-02-09 22:42:47 +11003087 now = get_cpu_migrations(counter);
Ingo Molnar6c594c22008-12-14 12:34:15 +01003088
3089 atomic64_set(&counter->hw.prev_count, now);
3090
3091 delta = now - prev;
Ingo Molnar6c594c22008-12-14 12:34:15 +01003092
3093 atomic64_add(delta, &counter->count);
3094}
3095
3096static void cpu_migrations_perf_counter_read(struct perf_counter *counter)
3097{
3098 cpu_migrations_perf_counter_update(counter);
3099}
3100
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01003101static int cpu_migrations_perf_counter_enable(struct perf_counter *counter)
Ingo Molnar6c594c22008-12-14 12:34:15 +01003102{
Paul Mackerrasc07c99b2009-02-13 22:10:34 +11003103 if (counter->prev_state <= PERF_COUNTER_STATE_OFF)
3104 atomic64_set(&counter->hw.prev_count,
3105 get_cpu_migrations(counter));
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01003106 return 0;
Ingo Molnar6c594c22008-12-14 12:34:15 +01003107}
3108
3109static void cpu_migrations_perf_counter_disable(struct perf_counter *counter)
3110{
3111 cpu_migrations_perf_counter_update(counter);
3112}
3113
Robert Richter4aeb0b42009-04-29 12:47:03 +02003114static const struct pmu perf_ops_cpu_migrations = {
Ingo Molnar76715812008-12-17 14:20:28 +01003115 .enable = cpu_migrations_perf_counter_enable,
3116 .disable = cpu_migrations_perf_counter_disable,
3117 .read = cpu_migrations_perf_counter_read,
Ingo Molnar6c594c22008-12-14 12:34:15 +01003118};
3119
Peter Zijlstrae077df42009-03-19 20:26:17 +01003120#ifdef CONFIG_EVENT_PROFILE
3121void perf_tpcounter_event(int event_id)
3122{
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003123 struct pt_regs *regs = get_irq_regs();
3124
3125 if (!regs)
3126 regs = task_pt_regs(current);
3127
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003128 __perf_swcounter_event(PERF_TYPE_TRACEPOINT, event_id, 1, 1, regs, 0);
Peter Zijlstrae077df42009-03-19 20:26:17 +01003129}
Steven Whitehouseff7b1b42009-04-15 16:55:05 +01003130EXPORT_SYMBOL_GPL(perf_tpcounter_event);
Peter Zijlstrae077df42009-03-19 20:26:17 +01003131
3132extern int ftrace_profile_enable(int);
3133extern void ftrace_profile_disable(int);
3134
3135static void tp_perf_counter_destroy(struct perf_counter *counter)
3136{
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01003137 ftrace_profile_disable(perf_event_id(&counter->hw_event));
Peter Zijlstrae077df42009-03-19 20:26:17 +01003138}
3139
Robert Richter4aeb0b42009-04-29 12:47:03 +02003140static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
Peter Zijlstrae077df42009-03-19 20:26:17 +01003141{
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01003142 int event_id = perf_event_id(&counter->hw_event);
Peter Zijlstrae077df42009-03-19 20:26:17 +01003143 int ret;
3144
3145 ret = ftrace_profile_enable(event_id);
3146 if (ret)
3147 return NULL;
3148
3149 counter->destroy = tp_perf_counter_destroy;
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003150 counter->hw.irq_period = counter->hw_event.irq_period;
Peter Zijlstrae077df42009-03-19 20:26:17 +01003151
3152 return &perf_ops_generic;
3153}
3154#else
Robert Richter4aeb0b42009-04-29 12:47:03 +02003155static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
Peter Zijlstrae077df42009-03-19 20:26:17 +01003156{
3157 return NULL;
3158}
3159#endif
3160
Robert Richter4aeb0b42009-04-29 12:47:03 +02003161static const struct pmu *sw_perf_counter_init(struct perf_counter *counter)
Ingo Molnar5c92d122008-12-11 13:21:10 +01003162{
Robert Richter4aeb0b42009-04-29 12:47:03 +02003163 const struct pmu *pmu = NULL;
Ingo Molnar5c92d122008-12-11 13:21:10 +01003164
Paul Mackerras0475f9e2009-02-11 14:35:35 +11003165 /*
3166 * Software counters (currently) can't in general distinguish
3167 * between user, kernel and hypervisor events.
3168 * However, context switches and cpu migrations are considered
3169 * to be kernel events, and page faults are never hypervisor
3170 * events.
3171 */
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01003172 switch (perf_event_id(&counter->hw_event)) {
Ingo Molnar5c92d122008-12-11 13:21:10 +01003173 case PERF_COUNT_CPU_CLOCK:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003174 pmu = &perf_ops_cpu_clock;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003175
Ingo Molnar5c92d122008-12-11 13:21:10 +01003176 break;
Ingo Molnarbae43c92008-12-11 14:03:20 +01003177 case PERF_COUNT_TASK_CLOCK:
Paul Mackerras23a185c2009-02-09 22:42:47 +11003178 /*
3179 * If the user instantiates this as a per-cpu counter,
3180 * use the cpu_clock counter instead.
3181 */
3182 if (counter->ctx->task)
Robert Richter4aeb0b42009-04-29 12:47:03 +02003183 pmu = &perf_ops_task_clock;
Paul Mackerras23a185c2009-02-09 22:42:47 +11003184 else
Robert Richter4aeb0b42009-04-29 12:47:03 +02003185 pmu = &perf_ops_cpu_clock;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003186
Ingo Molnarbae43c92008-12-11 14:03:20 +01003187 break;
Ingo Molnare06c61a2008-12-14 14:44:31 +01003188 case PERF_COUNT_PAGE_FAULTS:
Peter Zijlstraac17dc82009-03-13 12:21:34 +01003189 case PERF_COUNT_PAGE_FAULTS_MIN:
3190 case PERF_COUNT_PAGE_FAULTS_MAJ:
Ingo Molnar5d6a27d2008-12-14 12:28:33 +01003191 case PERF_COUNT_CONTEXT_SWITCHES:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003192 pmu = &perf_ops_generic;
Ingo Molnar5d6a27d2008-12-14 12:28:33 +01003193 break;
Ingo Molnar6c594c22008-12-14 12:34:15 +01003194 case PERF_COUNT_CPU_MIGRATIONS:
Paul Mackerras0475f9e2009-02-11 14:35:35 +11003195 if (!counter->hw_event.exclude_kernel)
Robert Richter4aeb0b42009-04-29 12:47:03 +02003196 pmu = &perf_ops_cpu_migrations;
Ingo Molnar6c594c22008-12-14 12:34:15 +01003197 break;
Ingo Molnar5c92d122008-12-11 13:21:10 +01003198 }
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003199
Robert Richter4aeb0b42009-04-29 12:47:03 +02003200 return pmu;
Ingo Molnar5c92d122008-12-11 13:21:10 +01003201}
3202
Thomas Gleixner0793a612008-12-04 20:12:29 +01003203/*
3204 * Allocate and initialize a counter structure
3205 */
3206static struct perf_counter *
Ingo Molnar04289bb2008-12-11 08:38:42 +01003207perf_counter_alloc(struct perf_counter_hw_event *hw_event,
3208 int cpu,
Paul Mackerras23a185c2009-02-09 22:42:47 +11003209 struct perf_counter_context *ctx,
Ingo Molnar9b51f662008-12-12 13:49:45 +01003210 struct perf_counter *group_leader,
3211 gfp_t gfpflags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003212{
Robert Richter4aeb0b42009-04-29 12:47:03 +02003213 const struct pmu *pmu;
Ingo Molnar621a01e2008-12-11 12:46:46 +01003214 struct perf_counter *counter;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003215 struct hw_perf_counter *hwc;
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003216 long err;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003217
Ingo Molnar9b51f662008-12-12 13:49:45 +01003218 counter = kzalloc(sizeof(*counter), gfpflags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003219 if (!counter)
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003220 return ERR_PTR(-ENOMEM);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003221
Ingo Molnar04289bb2008-12-11 08:38:42 +01003222 /*
3223 * Single counters are their own group leaders, with an
3224 * empty sibling list:
3225 */
3226 if (!group_leader)
3227 group_leader = counter;
3228
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003229 mutex_init(&counter->child_mutex);
3230 INIT_LIST_HEAD(&counter->child_list);
3231
Ingo Molnar04289bb2008-12-11 08:38:42 +01003232 INIT_LIST_HEAD(&counter->list_entry);
Peter Zijlstra592903c2009-03-13 12:21:36 +01003233 INIT_LIST_HEAD(&counter->event_entry);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003234 INIT_LIST_HEAD(&counter->sibling_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003235 init_waitqueue_head(&counter->waitq);
3236
Peter Zijlstra7b732a72009-03-23 18:22:10 +01003237 mutex_init(&counter->mmap_mutex);
3238
Ingo Molnar9f66a382008-12-10 12:33:23 +01003239 counter->cpu = cpu;
3240 counter->hw_event = *hw_event;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003241 counter->group_leader = group_leader;
Robert Richter4aeb0b42009-04-29 12:47:03 +02003242 counter->pmu = NULL;
Paul Mackerras23a185c2009-02-09 22:42:47 +11003243 counter->ctx = ctx;
Ingo Molnar329d8762009-05-26 08:10:00 +02003244 counter->oncpu = -1;
3245
Ingo Molnar235c7fc2008-12-21 14:43:25 +01003246 counter->state = PERF_COUNTER_STATE_INACTIVE;
Ingo Molnara86ed502008-12-17 00:43:10 +01003247 if (hw_event->disabled)
3248 counter->state = PERF_COUNTER_STATE_OFF;
3249
Robert Richter4aeb0b42009-04-29 12:47:03 +02003250 pmu = NULL;
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003251
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003252 hwc = &counter->hw;
3253 if (hw_event->freq && hw_event->irq_freq)
Peter Zijlstra2e569d32009-05-15 15:37:47 +02003254 hwc->irq_period = div64_u64(TICK_NSEC, hw_event->irq_freq);
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003255 else
3256 hwc->irq_period = hw_event->irq_period;
3257
Peter Zijlstra2023b352009-05-05 17:50:26 +02003258 /*
3259 * we currently do not support PERF_RECORD_GROUP on inherited counters
3260 */
3261 if (hw_event->inherit && (hw_event->record_type & PERF_RECORD_GROUP))
3262 goto done;
3263
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01003264 if (perf_event_raw(hw_event)) {
Robert Richter4aeb0b42009-04-29 12:47:03 +02003265 pmu = hw_perf_counter_init(counter);
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01003266 goto done;
3267 }
3268
3269 switch (perf_event_type(hw_event)) {
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003270 case PERF_TYPE_HARDWARE:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003271 pmu = hw_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003272 break;
3273
3274 case PERF_TYPE_SOFTWARE:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003275 pmu = sw_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003276 break;
3277
3278 case PERF_TYPE_TRACEPOINT:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003279 pmu = tp_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003280 break;
3281 }
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01003282done:
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003283 err = 0;
Robert Richter4aeb0b42009-04-29 12:47:03 +02003284 if (!pmu)
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003285 err = -EINVAL;
Robert Richter4aeb0b42009-04-29 12:47:03 +02003286 else if (IS_ERR(pmu))
3287 err = PTR_ERR(pmu);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003288
3289 if (err) {
3290 kfree(counter);
3291 return ERR_PTR(err);
3292 }
3293
Robert Richter4aeb0b42009-04-29 12:47:03 +02003294 counter->pmu = pmu;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003295
Peter Zijlstra7fc23a52009-05-08 18:52:21 +02003296 atomic_inc(&nr_counters);
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02003297 if (counter->hw_event.mmap)
3298 atomic_inc(&nr_mmap_tracking);
3299 if (counter->hw_event.munmap)
3300 atomic_inc(&nr_munmap_tracking);
3301 if (counter->hw_event.comm)
3302 atomic_inc(&nr_comm_tracking);
3303
Thomas Gleixner0793a612008-12-04 20:12:29 +01003304 return counter;
3305}
3306
3307/**
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003308 * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
Ingo Molnar9f66a382008-12-10 12:33:23 +01003309 *
3310 * @hw_event_uptr: event type attributes for monitoring/sampling
Thomas Gleixner0793a612008-12-04 20:12:29 +01003311 * @pid: target pid
Ingo Molnar9f66a382008-12-10 12:33:23 +01003312 * @cpu: target cpu
3313 * @group_fd: group leader counter fd
Thomas Gleixner0793a612008-12-04 20:12:29 +01003314 */
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003315SYSCALL_DEFINE5(perf_counter_open,
Paul Mackerrasf3dfd262009-02-26 22:43:46 +11003316 const struct perf_counter_hw_event __user *, hw_event_uptr,
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003317 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003318{
Ingo Molnar04289bb2008-12-11 08:38:42 +01003319 struct perf_counter *counter, *group_leader;
Ingo Molnar9f66a382008-12-10 12:33:23 +01003320 struct perf_counter_hw_event hw_event;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003321 struct perf_counter_context *ctx;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003322 struct file *counter_file = NULL;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003323 struct file *group_file = NULL;
3324 int fput_needed = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003325 int fput_needed2 = 0;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003326 int ret;
3327
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003328 /* for future expandability... */
3329 if (flags)
3330 return -EINVAL;
3331
Ingo Molnar9f66a382008-12-10 12:33:23 +01003332 if (copy_from_user(&hw_event, hw_event_uptr, sizeof(hw_event)) != 0)
Thomas Gleixnereab656a2008-12-08 19:26:59 +01003333 return -EFAULT;
3334
Ingo Molnar04289bb2008-12-11 08:38:42 +01003335 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01003336 * Get the target context (task or percpu):
3337 */
3338 ctx = find_get_context(pid, cpu);
3339 if (IS_ERR(ctx))
3340 return PTR_ERR(ctx);
3341
3342 /*
3343 * Look up the group leader (we will attach this counter to it):
Ingo Molnar04289bb2008-12-11 08:38:42 +01003344 */
3345 group_leader = NULL;
3346 if (group_fd != -1) {
3347 ret = -EINVAL;
3348 group_file = fget_light(group_fd, &fput_needed);
3349 if (!group_file)
Ingo Molnarccff2862008-12-11 11:26:29 +01003350 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003351 if (group_file->f_op != &perf_fops)
Ingo Molnarccff2862008-12-11 11:26:29 +01003352 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003353
3354 group_leader = group_file->private_data;
3355 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01003356 * Do not allow a recursive hierarchy (this new sibling
3357 * becoming part of another group-sibling):
Ingo Molnar04289bb2008-12-11 08:38:42 +01003358 */
Ingo Molnarccff2862008-12-11 11:26:29 +01003359 if (group_leader->group_leader != group_leader)
3360 goto err_put_context;
3361 /*
3362 * Do not allow to attach to a group in a different
3363 * task or CPU context:
3364 */
3365 if (group_leader->ctx != ctx)
3366 goto err_put_context;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11003367 /*
3368 * Only a group leader can be exclusive or pinned
3369 */
3370 if (hw_event.exclusive || hw_event.pinned)
3371 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003372 }
3373
Paul Mackerras23a185c2009-02-09 22:42:47 +11003374 counter = perf_counter_alloc(&hw_event, cpu, ctx, group_leader,
3375 GFP_KERNEL);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003376 ret = PTR_ERR(counter);
3377 if (IS_ERR(counter))
Thomas Gleixner0793a612008-12-04 20:12:29 +01003378 goto err_put_context;
3379
Thomas Gleixner0793a612008-12-04 20:12:29 +01003380 ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
3381 if (ret < 0)
Ingo Molnar9b51f662008-12-12 13:49:45 +01003382 goto err_free_put_context;
3383
3384 counter_file = fget_light(ret, &fput_needed2);
3385 if (!counter_file)
3386 goto err_free_put_context;
3387
3388 counter->filp = counter_file;
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003389 WARN_ON_ONCE(ctx->parent_ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003390 mutex_lock(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003391 perf_install_in_context(ctx, counter, cpu);
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003392 ++ctx->generation;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003393 mutex_unlock(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003394
Peter Zijlstra082ff5a2009-05-23 18:29:00 +02003395 counter->owner = current;
3396 get_task_struct(current);
3397 mutex_lock(&current->perf_counter_mutex);
3398 list_add_tail(&counter->owner_entry, &current->perf_counter_list);
3399 mutex_unlock(&current->perf_counter_mutex);
3400
Ingo Molnar9b51f662008-12-12 13:49:45 +01003401 fput_light(counter_file, fput_needed2);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003402
Ingo Molnar04289bb2008-12-11 08:38:42 +01003403out_fput:
3404 fput_light(group_file, fput_needed);
3405
Thomas Gleixner0793a612008-12-04 20:12:29 +01003406 return ret;
3407
Ingo Molnar9b51f662008-12-12 13:49:45 +01003408err_free_put_context:
Thomas Gleixner0793a612008-12-04 20:12:29 +01003409 kfree(counter);
3410
3411err_put_context:
Paul Mackerrasc93f7662009-05-28 22:18:17 +10003412 put_ctx(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003413
Ingo Molnar04289bb2008-12-11 08:38:42 +01003414 goto out_fput;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003415}
3416
Ingo Molnar9b51f662008-12-12 13:49:45 +01003417/*
Ingo Molnar9b51f662008-12-12 13:49:45 +01003418 * inherit a counter from parent task to child task:
3419 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11003420static struct perf_counter *
Ingo Molnar9b51f662008-12-12 13:49:45 +01003421inherit_counter(struct perf_counter *parent_counter,
3422 struct task_struct *parent,
3423 struct perf_counter_context *parent_ctx,
3424 struct task_struct *child,
Paul Mackerrasd859e292009-01-17 18:10:22 +11003425 struct perf_counter *group_leader,
Ingo Molnar9b51f662008-12-12 13:49:45 +01003426 struct perf_counter_context *child_ctx)
3427{
3428 struct perf_counter *child_counter;
3429
Paul Mackerrasd859e292009-01-17 18:10:22 +11003430 /*
3431 * Instead of creating recursive hierarchies of counters,
3432 * we link inherited counters back to the original parent,
3433 * which has a filp for sure, which we use as the reference
3434 * count:
3435 */
3436 if (parent_counter->parent)
3437 parent_counter = parent_counter->parent;
3438
Ingo Molnar9b51f662008-12-12 13:49:45 +01003439 child_counter = perf_counter_alloc(&parent_counter->hw_event,
Paul Mackerras23a185c2009-02-09 22:42:47 +11003440 parent_counter->cpu, child_ctx,
3441 group_leader, GFP_KERNEL);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003442 if (IS_ERR(child_counter))
3443 return child_counter;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10003444 get_ctx(child_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003445
3446 /*
Paul Mackerras564c2b22009-05-22 14:27:22 +10003447 * Make the child state follow the state of the parent counter,
3448 * not its hw_event.disabled bit. We hold the parent's mutex,
3449 * so we won't race with perf_counter_{en,dis}able_family.
3450 */
3451 if (parent_counter->state >= PERF_COUNTER_STATE_INACTIVE)
3452 child_counter->state = PERF_COUNTER_STATE_INACTIVE;
3453 else
3454 child_counter->state = PERF_COUNTER_STATE_OFF;
3455
3456 /*
Ingo Molnar9b51f662008-12-12 13:49:45 +01003457 * Link it up in the child's context:
3458 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +11003459 add_counter_to_ctx(child_counter, child_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003460
3461 child_counter->parent = parent_counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003462 /*
3463 * inherit into child's child as well:
3464 */
3465 child_counter->hw_event.inherit = 1;
3466
3467 /*
3468 * Get a reference to the parent filp - we will fput it
3469 * when the child counter exits. This is safe to do because
3470 * we are in the parent and we know that the filp still
3471 * exists and has a nonzero count:
3472 */
3473 atomic_long_inc(&parent_counter->filp->f_count);
3474
Paul Mackerrasd859e292009-01-17 18:10:22 +11003475 /*
3476 * Link this into the parent counter's child list
3477 */
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003478 WARN_ON_ONCE(parent_counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003479 mutex_lock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003480 list_add_tail(&child_counter->child_list, &parent_counter->child_list);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003481 mutex_unlock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003482
3483 return child_counter;
3484}
3485
3486static int inherit_group(struct perf_counter *parent_counter,
3487 struct task_struct *parent,
3488 struct perf_counter_context *parent_ctx,
3489 struct task_struct *child,
3490 struct perf_counter_context *child_ctx)
3491{
3492 struct perf_counter *leader;
3493 struct perf_counter *sub;
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003494 struct perf_counter *child_ctr;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003495
3496 leader = inherit_counter(parent_counter, parent, parent_ctx,
3497 child, NULL, child_ctx);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003498 if (IS_ERR(leader))
3499 return PTR_ERR(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003500 list_for_each_entry(sub, &parent_counter->sibling_list, list_entry) {
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003501 child_ctr = inherit_counter(sub, parent, parent_ctx,
3502 child, leader, child_ctx);
3503 if (IS_ERR(child_ctr))
3504 return PTR_ERR(child_ctr);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003505 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01003506 return 0;
3507}
3508
Paul Mackerrasd859e292009-01-17 18:10:22 +11003509static void sync_child_counter(struct perf_counter *child_counter,
3510 struct perf_counter *parent_counter)
3511{
Peter Zijlstra8bc20952009-05-15 20:45:59 +02003512 u64 child_val;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003513
Paul Mackerrasd859e292009-01-17 18:10:22 +11003514 child_val = atomic64_read(&child_counter->count);
3515
3516 /*
3517 * Add back the child's count to the parent's count:
3518 */
3519 atomic64_add(child_val, &parent_counter->count);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11003520 atomic64_add(child_counter->total_time_enabled,
3521 &parent_counter->child_total_time_enabled);
3522 atomic64_add(child_counter->total_time_running,
3523 &parent_counter->child_total_time_running);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003524
3525 /*
3526 * Remove this counter from the parent's list
3527 */
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003528 WARN_ON_ONCE(parent_counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003529 mutex_lock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003530 list_del_init(&child_counter->child_list);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003531 mutex_unlock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003532
3533 /*
3534 * Release the parent counter, if this was the last
3535 * reference to it.
3536 */
3537 fput(parent_counter->filp);
3538}
3539
Ingo Molnar9b51f662008-12-12 13:49:45 +01003540static void
3541__perf_counter_exit_task(struct task_struct *child,
3542 struct perf_counter *child_counter,
3543 struct perf_counter_context *child_ctx)
3544{
3545 struct perf_counter *parent_counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003546
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003547 update_counter_times(child_counter);
Peter Zijlstraaa9c67f2009-05-23 18:28:59 +02003548 perf_counter_remove_from_context(child_counter);
Ingo Molnar0cc0c022008-12-14 23:20:36 +01003549
Ingo Molnar9b51f662008-12-12 13:49:45 +01003550 parent_counter = child_counter->parent;
3551 /*
3552 * It can happen that parent exits first, and has counters
3553 * that are still around due to the child reference. These
3554 * counters need to be zapped - but otherwise linger.
3555 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11003556 if (parent_counter) {
3557 sync_child_counter(child_counter, parent_counter);
Peter Zijlstraf1600952009-03-19 20:26:16 +01003558 free_counter(child_counter);
Paul Mackerras4bcf3492009-02-11 13:53:19 +01003559 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01003560}
3561
3562/*
Paul Mackerrasd859e292009-01-17 18:10:22 +11003563 * When a child task exits, feed back counter values to parent counters.
Ingo Molnar9b51f662008-12-12 13:49:45 +01003564 */
3565void perf_counter_exit_task(struct task_struct *child)
3566{
3567 struct perf_counter *child_counter, *tmp;
3568 struct perf_counter_context *child_ctx;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003569 unsigned long flags;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003570
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003571 if (likely(!child->perf_counter_ctxp))
Ingo Molnar9b51f662008-12-12 13:49:45 +01003572 return;
3573
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003574 local_irq_save(flags);
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003575 /*
3576 * We can't reschedule here because interrupts are disabled,
3577 * and either child is current or it is a task that can't be
3578 * scheduled, so we are now safe from rescheduling changing
3579 * our context.
3580 */
3581 child_ctx = child->perf_counter_ctxp;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003582 __perf_counter_task_sched_out(child_ctx);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10003583
3584 /*
3585 * Take the context lock here so that if find_get_context is
3586 * reading child->perf_counter_ctxp, we wait until it has
3587 * incremented the context's refcount before we do put_ctx below.
3588 */
3589 spin_lock(&child_ctx->lock);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003590 child->perf_counter_ctxp = NULL;
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003591 if (child_ctx->parent_ctx) {
3592 /*
3593 * This context is a clone; unclone it so it can't get
3594 * swapped to another process while we're removing all
3595 * the counters from it.
3596 */
3597 put_ctx(child_ctx->parent_ctx);
3598 child_ctx->parent_ctx = NULL;
3599 }
Paul Mackerrasc93f7662009-05-28 22:18:17 +10003600 spin_unlock(&child_ctx->lock);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003601 local_irq_restore(flags);
3602
3603 mutex_lock(&child_ctx->mutex);
3604
Peter Zijlstra8bc20952009-05-15 20:45:59 +02003605again:
Ingo Molnar9b51f662008-12-12 13:49:45 +01003606 list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list,
3607 list_entry)
3608 __perf_counter_exit_task(child, child_counter, child_ctx);
Peter Zijlstra8bc20952009-05-15 20:45:59 +02003609
3610 /*
3611 * If the last counter was a group counter, it will have appended all
3612 * its siblings to the list, but we obtained 'tmp' before that which
3613 * will still point to the list head terminating the iteration.
3614 */
3615 if (!list_empty(&child_ctx->counter_list))
3616 goto again;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003617
3618 mutex_unlock(&child_ctx->mutex);
3619
3620 put_ctx(child_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003621}
3622
3623/*
3624 * Initialize the perf_counter context in task_struct
3625 */
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02003626int perf_counter_init_task(struct task_struct *child)
Ingo Molnar9b51f662008-12-12 13:49:45 +01003627{
3628 struct perf_counter_context *child_ctx, *parent_ctx;
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003629 struct perf_counter_context *cloned_ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003630 struct perf_counter *counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003631 struct task_struct *parent = current;
Paul Mackerras564c2b22009-05-22 14:27:22 +10003632 int inherited_all = 1;
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003633 u64 cloned_gen;
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02003634 int ret = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003635
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003636 child->perf_counter_ctxp = NULL;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003637
Peter Zijlstra082ff5a2009-05-23 18:29:00 +02003638 mutex_init(&child->perf_counter_mutex);
3639 INIT_LIST_HEAD(&child->perf_counter_list);
3640
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003641 if (likely(!parent->perf_counter_ctxp))
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02003642 return 0;
3643
Ingo Molnar9b51f662008-12-12 13:49:45 +01003644 /*
3645 * This is executed from the parent task context, so inherit
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003646 * counters that have been marked for cloning.
3647 * First allocate and initialize a context for the child.
Ingo Molnar9b51f662008-12-12 13:49:45 +01003648 */
3649
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003650 child_ctx = kmalloc(sizeof(struct perf_counter_context), GFP_KERNEL);
3651 if (!child_ctx)
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02003652 return -ENOMEM;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003653
3654 __perf_counter_init_context(child_ctx, child);
3655 child->perf_counter_ctxp = child_ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10003656 get_task_struct(child);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003657
Ingo Molnar9b51f662008-12-12 13:49:45 +01003658 /*
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003659 * If the parent's context is a clone, temporarily set its
3660 * parent_gen to an impossible value (all 1s) so it won't get
3661 * swapped under us. The rcu_read_lock makes sure that
3662 * parent_ctx continues to exist even if it gets swapped to
3663 * another process and then freed while we are trying to get
3664 * its lock.
3665 */
3666 rcu_read_lock();
3667 retry:
3668 parent_ctx = rcu_dereference(parent->perf_counter_ctxp);
3669 /*
3670 * No need to check if parent_ctx != NULL here; since we saw
3671 * it non-NULL earlier, the only reason for it to become NULL
3672 * is if we exit, and since we're currently in the middle of
3673 * a fork we can't be exiting at the same time.
3674 */
3675 spin_lock_irq(&parent_ctx->lock);
3676 if (parent_ctx != rcu_dereference(parent->perf_counter_ctxp)) {
3677 spin_unlock_irq(&parent_ctx->lock);
3678 goto retry;
3679 }
3680 cloned_gen = parent_ctx->parent_gen;
3681 if (parent_ctx->parent_ctx)
3682 parent_ctx->parent_gen = ~0ull;
3683 spin_unlock_irq(&parent_ctx->lock);
3684 rcu_read_unlock();
3685
3686 /*
Ingo Molnar9b51f662008-12-12 13:49:45 +01003687 * Lock the parent list. No need to lock the child - not PID
3688 * hashed yet and not running, so nobody can access it.
3689 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11003690 mutex_lock(&parent_ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003691
3692 /*
3693 * We dont have to disable NMIs - we are only looking at
3694 * the list, not manipulating it:
3695 */
Peter Zijlstrad7b629a2009-05-20 12:21:19 +02003696 list_for_each_entry_rcu(counter, &parent_ctx->event_list, event_entry) {
3697 if (counter != counter->group_leader)
3698 continue;
3699
Paul Mackerras564c2b22009-05-22 14:27:22 +10003700 if (!counter->hw_event.inherit) {
3701 inherited_all = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003702 continue;
Paul Mackerras564c2b22009-05-22 14:27:22 +10003703 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01003704
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02003705 ret = inherit_group(counter, parent, parent_ctx,
3706 child, child_ctx);
3707 if (ret) {
Paul Mackerras564c2b22009-05-22 14:27:22 +10003708 inherited_all = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003709 break;
Paul Mackerras564c2b22009-05-22 14:27:22 +10003710 }
3711 }
3712
3713 if (inherited_all) {
3714 /*
3715 * Mark the child context as a clone of the parent
3716 * context, or of whatever the parent is a clone of.
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003717 * Note that if the parent is a clone, it could get
3718 * uncloned at any point, but that doesn't matter
3719 * because the list of counters and the generation
3720 * count can't have changed since we took the mutex.
Paul Mackerras564c2b22009-05-22 14:27:22 +10003721 */
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003722 cloned_ctx = rcu_dereference(parent_ctx->parent_ctx);
3723 if (cloned_ctx) {
3724 child_ctx->parent_ctx = cloned_ctx;
3725 child_ctx->parent_gen = cloned_gen;
Paul Mackerras564c2b22009-05-22 14:27:22 +10003726 } else {
3727 child_ctx->parent_ctx = parent_ctx;
3728 child_ctx->parent_gen = parent_ctx->generation;
3729 }
3730 get_ctx(child_ctx->parent_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003731 }
3732
Paul Mackerrasd859e292009-01-17 18:10:22 +11003733 mutex_unlock(&parent_ctx->mutex);
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02003734
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003735 /*
3736 * Restore the clone status of the parent.
3737 */
3738 if (parent_ctx->parent_ctx) {
3739 spin_lock_irq(&parent_ctx->lock);
3740 if (parent_ctx->parent_ctx)
3741 parent_ctx->parent_gen = cloned_gen;
3742 spin_unlock_irq(&parent_ctx->lock);
3743 }
3744
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02003745 return ret;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003746}
3747
Ingo Molnar04289bb2008-12-11 08:38:42 +01003748static void __cpuinit perf_counter_init_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003749{
Ingo Molnar04289bb2008-12-11 08:38:42 +01003750 struct perf_cpu_context *cpuctx;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003751
Ingo Molnar04289bb2008-12-11 08:38:42 +01003752 cpuctx = &per_cpu(perf_cpu_context, cpu);
3753 __perf_counter_init_context(&cpuctx->ctx, NULL);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003754
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003755 spin_lock(&perf_resource_lock);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003756 cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu;
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003757 spin_unlock(&perf_resource_lock);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003758
Paul Mackerras01d02872009-01-14 13:44:19 +11003759 hw_perf_counter_setup(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003760}
3761
3762#ifdef CONFIG_HOTPLUG_CPU
Ingo Molnar04289bb2008-12-11 08:38:42 +01003763static void __perf_counter_exit_cpu(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003764{
3765 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
3766 struct perf_counter_context *ctx = &cpuctx->ctx;
3767 struct perf_counter *counter, *tmp;
3768
Ingo Molnar04289bb2008-12-11 08:38:42 +01003769 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry)
3770 __perf_counter_remove_from_context(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003771}
Ingo Molnar04289bb2008-12-11 08:38:42 +01003772static void perf_counter_exit_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003773{
Paul Mackerrasd859e292009-01-17 18:10:22 +11003774 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
3775 struct perf_counter_context *ctx = &cpuctx->ctx;
3776
3777 mutex_lock(&ctx->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003778 smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003779 mutex_unlock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003780}
3781#else
Ingo Molnar04289bb2008-12-11 08:38:42 +01003782static inline void perf_counter_exit_cpu(int cpu) { }
Thomas Gleixner0793a612008-12-04 20:12:29 +01003783#endif
3784
3785static int __cpuinit
3786perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
3787{
3788 unsigned int cpu = (long)hcpu;
3789
3790 switch (action) {
3791
3792 case CPU_UP_PREPARE:
3793 case CPU_UP_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01003794 perf_counter_init_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003795 break;
3796
3797 case CPU_DOWN_PREPARE:
3798 case CPU_DOWN_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01003799 perf_counter_exit_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003800 break;
3801
3802 default:
3803 break;
3804 }
3805
3806 return NOTIFY_OK;
3807}
3808
3809static struct notifier_block __cpuinitdata perf_cpu_nb = {
3810 .notifier_call = perf_cpu_notify,
3811};
3812
Ingo Molnar0d905bc2009-05-04 19:13:30 +02003813void __init perf_counter_init(void)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003814{
3815 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
3816 (void *)(long)smp_processor_id());
3817 register_cpu_notifier(&perf_cpu_nb);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003818}
Thomas Gleixner0793a612008-12-04 20:12:29 +01003819
3820static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
3821{
3822 return sprintf(buf, "%d\n", perf_reserved_percpu);
3823}
3824
3825static ssize_t
3826perf_set_reserve_percpu(struct sysdev_class *class,
3827 const char *buf,
3828 size_t count)
3829{
3830 struct perf_cpu_context *cpuctx;
3831 unsigned long val;
3832 int err, cpu, mpt;
3833
3834 err = strict_strtoul(buf, 10, &val);
3835 if (err)
3836 return err;
3837 if (val > perf_max_counters)
3838 return -EINVAL;
3839
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003840 spin_lock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003841 perf_reserved_percpu = val;
3842 for_each_online_cpu(cpu) {
3843 cpuctx = &per_cpu(perf_cpu_context, cpu);
3844 spin_lock_irq(&cpuctx->ctx.lock);
3845 mpt = min(perf_max_counters - cpuctx->ctx.nr_counters,
3846 perf_max_counters - perf_reserved_percpu);
3847 cpuctx->max_pertask = mpt;
3848 spin_unlock_irq(&cpuctx->ctx.lock);
3849 }
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003850 spin_unlock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003851
3852 return count;
3853}
3854
3855static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
3856{
3857 return sprintf(buf, "%d\n", perf_overcommit);
3858}
3859
3860static ssize_t
3861perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
3862{
3863 unsigned long val;
3864 int err;
3865
3866 err = strict_strtoul(buf, 10, &val);
3867 if (err)
3868 return err;
3869 if (val > 1)
3870 return -EINVAL;
3871
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003872 spin_lock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003873 perf_overcommit = val;
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003874 spin_unlock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003875
3876 return count;
3877}
3878
3879static SYSDEV_CLASS_ATTR(
3880 reserve_percpu,
3881 0644,
3882 perf_show_reserve_percpu,
3883 perf_set_reserve_percpu
3884 );
3885
3886static SYSDEV_CLASS_ATTR(
3887 overcommit,
3888 0644,
3889 perf_show_overcommit,
3890 perf_set_overcommit
3891 );
3892
3893static struct attribute *perfclass_attrs[] = {
3894 &attr_reserve_percpu.attr,
3895 &attr_overcommit.attr,
3896 NULL
3897};
3898
3899static struct attribute_group perfclass_attr_group = {
3900 .attrs = perfclass_attrs,
3901 .name = "perf_counters",
3902};
3903
3904static int __init perf_counter_sysfs_init(void)
3905{
3906 return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
3907 &perfclass_attr_group);
3908}
3909device_initcall(perf_counter_sysfs_init);