blob: 8b89b40bd0f04fb3243d115f1b68b7186ffcbe4d [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>
Ingo Molnar22a4f652009-06-01 10:13:37 +020019#include <linux/dcache.h>
Thomas Gleixner0793a612008-12-04 20:12:29 +010020#include <linux/percpu.h>
Ingo Molnar22a4f652009-06-01 10:13:37 +020021#include <linux/ptrace.h>
Peter Zijlstrab9cacc72009-03-25 12:30:22 +010022#include <linux/vmstat.h>
23#include <linux/hardirq.h>
24#include <linux/rculist.h>
Thomas Gleixner0793a612008-12-04 20:12:29 +010025#include <linux/uaccess.h>
26#include <linux/syscalls.h>
27#include <linux/anon_inodes.h>
Ingo Molnaraa9c4c02008-12-17 14:10:57 +010028#include <linux/kernel_stat.h>
Thomas Gleixner0793a612008-12-04 20:12:29 +010029#include <linux/perf_counter.h>
30
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 Zijlstra60313eb2009-06-04 16:53:44 +020043static atomic_t nr_mmap_counters __read_mostly;
Peter Zijlstra60313eb2009-06-04 16:53:44 +020044static atomic_t nr_comm_counters __read_mostly;
Peter Zijlstra9ee318a2009-04-09 10:53:44 +020045
Peter Zijlstra1ccd1542009-04-09 10:53:45 +020046int sysctl_perf_counter_priv __read_mostly; /* do we need to be privileged */
Peter Zijlstra789f90f2009-05-15 15:19:27 +020047int sysctl_perf_counter_mlock __read_mostly = 512; /* 'free' kb per user */
Peter Zijlstraa78ac322009-05-25 17:39:05 +020048int sysctl_perf_counter_limit __read_mostly = 100000; /* max NMIs per second */
Peter Zijlstra1ccd1542009-04-09 10:53:45 +020049
Peter Zijlstraa96bbc12009-06-03 14:01:36 +020050static atomic64_t perf_counter_id;
51
Thomas Gleixner0793a612008-12-04 20:12:29 +010052/*
Ingo Molnar1dce8d92009-05-04 19:23:18 +020053 * Lock for (sysadmin-configurable) counter reservations:
Thomas Gleixner0793a612008-12-04 20:12:29 +010054 */
Ingo Molnar1dce8d92009-05-04 19:23:18 +020055static DEFINE_SPINLOCK(perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +010056
57/*
58 * Architecture provided APIs - weak aliases:
59 */
Robert Richter4aeb0b42009-04-29 12:47:03 +020060extern __weak const struct pmu *hw_perf_counter_init(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +010061{
Paul Mackerrasff6f0542009-01-09 16:19:25 +110062 return NULL;
Thomas Gleixner0793a612008-12-04 20:12:29 +010063}
64
Peter Zijlstra9e35ad32009-05-13 16:21:38 +020065void __weak hw_perf_disable(void) { barrier(); }
66void __weak hw_perf_enable(void) { barrier(); }
67
Paul Mackerras01d02872009-01-14 13:44:19 +110068void __weak hw_perf_counter_setup(int cpu) { barrier(); }
Ingo Molnar22a4f652009-06-01 10:13:37 +020069
70int __weak
71hw_perf_group_sched_in(struct perf_counter *group_leader,
Paul Mackerras3cbed422009-01-09 16:43:42 +110072 struct perf_cpu_context *cpuctx,
73 struct perf_counter_context *ctx, int cpu)
74{
75 return 0;
76}
Thomas Gleixner0793a612008-12-04 20:12:29 +010077
Paul Mackerras4eb96fc2009-01-09 17:24:34 +110078void __weak perf_counter_print_debug(void) { }
79
Peter Zijlstra9e35ad32009-05-13 16:21:38 +020080static DEFINE_PER_CPU(int, disable_count);
81
82void __perf_disable(void)
83{
84 __get_cpu_var(disable_count)++;
85}
86
87bool __perf_enable(void)
88{
89 return !--__get_cpu_var(disable_count);
90}
91
92void perf_disable(void)
93{
94 __perf_disable();
95 hw_perf_disable();
96}
Peter Zijlstra9e35ad32009-05-13 16:21:38 +020097
98void perf_enable(void)
99{
100 if (__perf_enable())
101 hw_perf_enable();
102}
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200103
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000104static void get_ctx(struct perf_counter_context *ctx)
105{
106 atomic_inc(&ctx->refcount);
107}
108
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000109static void free_ctx(struct rcu_head *head)
110{
111 struct perf_counter_context *ctx;
112
113 ctx = container_of(head, struct perf_counter_context, rcu_head);
114 kfree(ctx);
115}
116
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000117static void put_ctx(struct perf_counter_context *ctx)
118{
Paul Mackerras564c2b22009-05-22 14:27:22 +1000119 if (atomic_dec_and_test(&ctx->refcount)) {
120 if (ctx->parent_ctx)
121 put_ctx(ctx->parent_ctx);
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000122 if (ctx->task)
123 put_task_struct(ctx->task);
124 call_rcu(&ctx->rcu_head, free_ctx);
Paul Mackerras564c2b22009-05-22 14:27:22 +1000125 }
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000126}
127
Peter Zijlstrafccc7142009-05-23 18:28:56 +0200128/*
Paul Mackerras25346b92009-06-01 17:48:12 +1000129 * Get the perf_counter_context for a task and lock it.
130 * This has to cope with with the fact that until it is locked,
131 * the context could get moved to another task.
132 */
Ingo Molnar22a4f652009-06-01 10:13:37 +0200133static struct perf_counter_context *
134perf_lock_task_context(struct task_struct *task, unsigned long *flags)
Paul Mackerras25346b92009-06-01 17:48:12 +1000135{
136 struct perf_counter_context *ctx;
137
138 rcu_read_lock();
139 retry:
140 ctx = rcu_dereference(task->perf_counter_ctxp);
141 if (ctx) {
142 /*
143 * If this context is a clone of another, it might
144 * get swapped for another underneath us by
145 * perf_counter_task_sched_out, though the
146 * rcu_read_lock() protects us from any context
147 * getting freed. Lock the context and check if it
148 * got swapped before we could get the lock, and retry
149 * if so. If we locked the right context, then it
150 * can't get swapped on us any more.
151 */
152 spin_lock_irqsave(&ctx->lock, *flags);
153 if (ctx != rcu_dereference(task->perf_counter_ctxp)) {
154 spin_unlock_irqrestore(&ctx->lock, *flags);
155 goto retry;
156 }
157 }
158 rcu_read_unlock();
159 return ctx;
160}
161
162/*
163 * Get the context for a task and increment its pin_count so it
164 * can't get swapped to another task. This also increments its
165 * reference count so that the context can't get freed.
166 */
167static struct perf_counter_context *perf_pin_task_context(struct task_struct *task)
168{
169 struct perf_counter_context *ctx;
170 unsigned long flags;
171
172 ctx = perf_lock_task_context(task, &flags);
173 if (ctx) {
174 ++ctx->pin_count;
175 get_ctx(ctx);
176 spin_unlock_irqrestore(&ctx->lock, flags);
177 }
178 return ctx;
179}
180
181static void perf_unpin_context(struct perf_counter_context *ctx)
182{
183 unsigned long flags;
184
185 spin_lock_irqsave(&ctx->lock, flags);
186 --ctx->pin_count;
187 spin_unlock_irqrestore(&ctx->lock, flags);
188 put_ctx(ctx);
189}
190
191/*
Peter Zijlstrafccc7142009-05-23 18:28:56 +0200192 * Add a counter from the lists for its context.
193 * Must be called with ctx->mutex and ctx->lock held.
194 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100195static void
196list_add_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
197{
198 struct perf_counter *group_leader = counter->group_leader;
199
200 /*
201 * Depending on whether it is a standalone or sibling counter,
202 * add it straight to the context's counter list, or to the group
203 * leader's sibling list:
204 */
Peter Zijlstra3df5eda2009-05-08 18:52:22 +0200205 if (group_leader == counter)
Ingo Molnar04289bb2008-12-11 08:38:42 +0100206 list_add_tail(&counter->list_entry, &ctx->counter_list);
Peter Zijlstra5c148192009-03-25 12:30:23 +0100207 else {
Ingo Molnar04289bb2008-12-11 08:38:42 +0100208 list_add_tail(&counter->list_entry, &group_leader->sibling_list);
Peter Zijlstra5c148192009-03-25 12:30:23 +0100209 group_leader->nr_siblings++;
210 }
Peter Zijlstra592903c2009-03-13 12:21:36 +0100211
212 list_add_rcu(&counter->event_entry, &ctx->event_list);
Peter Zijlstra8bc20952009-05-15 20:45:59 +0200213 ctx->nr_counters++;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100214}
215
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000216/*
217 * Remove a counter from the lists for its context.
Peter Zijlstrafccc7142009-05-23 18:28:56 +0200218 * Must be called with ctx->mutex and ctx->lock held.
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000219 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100220static void
221list_del_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
222{
223 struct perf_counter *sibling, *tmp;
224
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000225 if (list_empty(&counter->list_entry))
226 return;
Peter Zijlstra8bc20952009-05-15 20:45:59 +0200227 ctx->nr_counters--;
228
Ingo Molnar04289bb2008-12-11 08:38:42 +0100229 list_del_init(&counter->list_entry);
Peter Zijlstra592903c2009-03-13 12:21:36 +0100230 list_del_rcu(&counter->event_entry);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100231
Peter Zijlstra5c148192009-03-25 12:30:23 +0100232 if (counter->group_leader != counter)
233 counter->group_leader->nr_siblings--;
234
Ingo Molnar04289bb2008-12-11 08:38:42 +0100235 /*
236 * If this was a group counter with sibling counters then
237 * upgrade the siblings to singleton counters by adding them
238 * to the context list directly:
239 */
240 list_for_each_entry_safe(sibling, tmp,
241 &counter->sibling_list, list_entry) {
242
Peter Zijlstra75564232009-03-13 12:21:29 +0100243 list_move_tail(&sibling->list_entry, &ctx->counter_list);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100244 sibling->group_leader = sibling;
245 }
246}
247
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100248static void
249counter_sched_out(struct perf_counter *counter,
250 struct perf_cpu_context *cpuctx,
251 struct perf_counter_context *ctx)
252{
253 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
254 return;
255
256 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200257 counter->tstamp_stopped = ctx->time;
Robert Richter4aeb0b42009-04-29 12:47:03 +0200258 counter->pmu->disable(counter);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100259 counter->oncpu = -1;
260
261 if (!is_software_counter(counter))
262 cpuctx->active_oncpu--;
263 ctx->nr_active--;
Peter Zijlstra0d486962009-06-02 19:22:16 +0200264 if (counter->attr.exclusive || !cpuctx->active_oncpu)
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100265 cpuctx->exclusive = 0;
266}
267
Paul Mackerrasd859e292009-01-17 18:10:22 +1100268static void
269group_sched_out(struct perf_counter *group_counter,
270 struct perf_cpu_context *cpuctx,
271 struct perf_counter_context *ctx)
272{
273 struct perf_counter *counter;
274
275 if (group_counter->state != PERF_COUNTER_STATE_ACTIVE)
276 return;
277
278 counter_sched_out(group_counter, cpuctx, ctx);
279
280 /*
281 * Schedule out siblings (if any):
282 */
283 list_for_each_entry(counter, &group_counter->sibling_list, list_entry)
284 counter_sched_out(counter, cpuctx, ctx);
285
Peter Zijlstra0d486962009-06-02 19:22:16 +0200286 if (group_counter->attr.exclusive)
Paul Mackerrasd859e292009-01-17 18:10:22 +1100287 cpuctx->exclusive = 0;
288}
289
Thomas Gleixner0793a612008-12-04 20:12:29 +0100290/*
291 * Cross CPU call to remove a performance counter
292 *
293 * We disable the counter on the hardware level first. After that we
294 * remove it from the context list.
295 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100296static void __perf_counter_remove_from_context(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100297{
298 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
299 struct perf_counter *counter = info;
300 struct perf_counter_context *ctx = counter->ctx;
301
302 /*
303 * If this is a task context, we need to check whether it is
304 * the current task context of this cpu. If not it has been
305 * scheduled out before the smp call arrived.
306 */
Peter Zijlstra665c2142009-05-29 14:51:57 +0200307 if (ctx->task && cpuctx->task_ctx != ctx)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100308 return;
309
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200310 spin_lock(&ctx->lock);
Ingo Molnar34adc802009-05-20 20:13:28 +0200311 /*
312 * Protect the list operation against NMI by disabling the
313 * counters on a global level.
314 */
315 perf_disable();
Thomas Gleixner0793a612008-12-04 20:12:29 +0100316
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100317 counter_sched_out(counter, cpuctx, ctx);
318
Ingo Molnar04289bb2008-12-11 08:38:42 +0100319 list_del_counter(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100320
321 if (!ctx->task) {
322 /*
323 * Allow more per task counters with respect to the
324 * reservation:
325 */
326 cpuctx->max_pertask =
327 min(perf_max_counters - ctx->nr_counters,
328 perf_max_counters - perf_reserved_percpu);
329 }
330
Ingo Molnar34adc802009-05-20 20:13:28 +0200331 perf_enable();
Peter Zijlstra665c2142009-05-29 14:51:57 +0200332 spin_unlock(&ctx->lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100333}
334
335
336/*
337 * Remove the counter from a task's (or a CPU's) list of counters.
338 *
Peter Zijlstrafccc7142009-05-23 18:28:56 +0200339 * Must be called with ctx->mutex held.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100340 *
341 * CPU counters are removed with a smp call. For task counters we only
342 * call when the task is on a CPU.
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000343 *
344 * If counter->ctx is a cloned context, callers must make sure that
345 * every task struct that counter->ctx->task could possibly point to
346 * remains valid. This is OK when called from perf_release since
347 * that only calls us on the top-level context, which can't be a clone.
348 * When called from perf_counter_exit_task, it's OK because the
349 * context has been detached from its task.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100350 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100351static void perf_counter_remove_from_context(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100352{
353 struct perf_counter_context *ctx = counter->ctx;
354 struct task_struct *task = ctx->task;
355
356 if (!task) {
357 /*
358 * Per cpu counters are removed via an smp call and
359 * the removal is always sucessful.
360 */
361 smp_call_function_single(counter->cpu,
Ingo Molnar04289bb2008-12-11 08:38:42 +0100362 __perf_counter_remove_from_context,
Thomas Gleixner0793a612008-12-04 20:12:29 +0100363 counter, 1);
364 return;
365 }
366
367retry:
Ingo Molnar04289bb2008-12-11 08:38:42 +0100368 task_oncpu_function_call(task, __perf_counter_remove_from_context,
Thomas Gleixner0793a612008-12-04 20:12:29 +0100369 counter);
370
371 spin_lock_irq(&ctx->lock);
372 /*
373 * If the context is active we need to retry the smp call.
374 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100375 if (ctx->nr_active && !list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100376 spin_unlock_irq(&ctx->lock);
377 goto retry;
378 }
379
380 /*
381 * The lock prevents that this context is scheduled in so we
Ingo Molnar04289bb2008-12-11 08:38:42 +0100382 * can remove the counter safely, if the call above did not
Thomas Gleixner0793a612008-12-04 20:12:29 +0100383 * succeed.
384 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100385 if (!list_empty(&counter->list_entry)) {
Ingo Molnar04289bb2008-12-11 08:38:42 +0100386 list_del_counter(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100387 }
388 spin_unlock_irq(&ctx->lock);
389}
390
Peter Zijlstra4af49982009-04-06 11:45:10 +0200391static inline u64 perf_clock(void)
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100392{
Peter Zijlstra4af49982009-04-06 11:45:10 +0200393 return cpu_clock(smp_processor_id());
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100394}
395
396/*
397 * Update the record of the current time in a context.
398 */
Peter Zijlstra4af49982009-04-06 11:45:10 +0200399static void update_context_time(struct perf_counter_context *ctx)
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100400{
Peter Zijlstra4af49982009-04-06 11:45:10 +0200401 u64 now = perf_clock();
402
403 ctx->time += now - ctx->timestamp;
404 ctx->timestamp = now;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100405}
406
407/*
408 * Update the total_time_enabled and total_time_running fields for a counter.
409 */
410static void update_counter_times(struct perf_counter *counter)
411{
412 struct perf_counter_context *ctx = counter->ctx;
413 u64 run_end;
414
Peter Zijlstra4af49982009-04-06 11:45:10 +0200415 if (counter->state < PERF_COUNTER_STATE_INACTIVE)
416 return;
417
418 counter->total_time_enabled = ctx->time - counter->tstamp_enabled;
419
420 if (counter->state == PERF_COUNTER_STATE_INACTIVE)
421 run_end = counter->tstamp_stopped;
422 else
423 run_end = ctx->time;
424
425 counter->total_time_running = run_end - counter->tstamp_running;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100426}
427
428/*
429 * Update total_time_enabled and total_time_running for all counters in a group.
430 */
431static void update_group_times(struct perf_counter *leader)
432{
433 struct perf_counter *counter;
434
435 update_counter_times(leader);
436 list_for_each_entry(counter, &leader->sibling_list, list_entry)
437 update_counter_times(counter);
438}
439
440/*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100441 * Cross CPU call to disable a performance counter
442 */
443static void __perf_counter_disable(void *info)
444{
445 struct perf_counter *counter = info;
446 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
447 struct perf_counter_context *ctx = counter->ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100448
449 /*
450 * If this is a per-task counter, need to check whether this
451 * counter's task is the current task on this cpu.
452 */
Peter Zijlstra665c2142009-05-29 14:51:57 +0200453 if (ctx->task && cpuctx->task_ctx != ctx)
Paul Mackerrasd859e292009-01-17 18:10:22 +1100454 return;
455
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200456 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100457
458 /*
459 * If the counter is on, turn it off.
460 * If it is in error state, leave it in error state.
461 */
462 if (counter->state >= PERF_COUNTER_STATE_INACTIVE) {
Peter Zijlstra4af49982009-04-06 11:45:10 +0200463 update_context_time(ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100464 update_counter_times(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100465 if (counter == counter->group_leader)
466 group_sched_out(counter, cpuctx, ctx);
467 else
468 counter_sched_out(counter, cpuctx, ctx);
469 counter->state = PERF_COUNTER_STATE_OFF;
470 }
471
Peter Zijlstra665c2142009-05-29 14:51:57 +0200472 spin_unlock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100473}
474
475/*
476 * Disable a counter.
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000477 *
478 * If counter->ctx is a cloned context, callers must make sure that
479 * every task struct that counter->ctx->task could possibly point to
480 * remains valid. This condition is satisifed when called through
481 * perf_counter_for_each_child or perf_counter_for_each because they
482 * hold the top-level counter's child_mutex, so any descendant that
483 * goes to exit will block in sync_child_counter.
484 * When called from perf_pending_counter it's OK because counter->ctx
485 * is the current context on this CPU and preemption is disabled,
486 * hence we can't get into perf_counter_task_sched_out for this context.
Paul Mackerrasd859e292009-01-17 18:10:22 +1100487 */
488static void perf_counter_disable(struct perf_counter *counter)
489{
490 struct perf_counter_context *ctx = counter->ctx;
491 struct task_struct *task = ctx->task;
492
493 if (!task) {
494 /*
495 * Disable the counter on the cpu that it's on
496 */
497 smp_call_function_single(counter->cpu, __perf_counter_disable,
498 counter, 1);
499 return;
500 }
501
502 retry:
503 task_oncpu_function_call(task, __perf_counter_disable, counter);
504
505 spin_lock_irq(&ctx->lock);
506 /*
507 * If the counter is still active, we need to retry the cross-call.
508 */
509 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
510 spin_unlock_irq(&ctx->lock);
511 goto retry;
512 }
513
514 /*
515 * Since we have the lock this context can't be scheduled
516 * in, so we can change the state safely.
517 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100518 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
519 update_counter_times(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100520 counter->state = PERF_COUNTER_STATE_OFF;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100521 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100522
523 spin_unlock_irq(&ctx->lock);
524}
525
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100526static int
527counter_sched_in(struct perf_counter *counter,
528 struct perf_cpu_context *cpuctx,
529 struct perf_counter_context *ctx,
530 int cpu)
531{
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100532 if (counter->state <= PERF_COUNTER_STATE_OFF)
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100533 return 0;
534
535 counter->state = PERF_COUNTER_STATE_ACTIVE;
536 counter->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */
537 /*
538 * The new state must be visible before we turn it on in the hardware:
539 */
540 smp_wmb();
541
Robert Richter4aeb0b42009-04-29 12:47:03 +0200542 if (counter->pmu->enable(counter)) {
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100543 counter->state = PERF_COUNTER_STATE_INACTIVE;
544 counter->oncpu = -1;
545 return -EAGAIN;
546 }
547
Peter Zijlstra4af49982009-04-06 11:45:10 +0200548 counter->tstamp_running += ctx->time - counter->tstamp_stopped;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100549
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100550 if (!is_software_counter(counter))
551 cpuctx->active_oncpu++;
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100552 ctx->nr_active++;
553
Peter Zijlstra0d486962009-06-02 19:22:16 +0200554 if (counter->attr.exclusive)
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100555 cpuctx->exclusive = 1;
556
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100557 return 0;
558}
559
Paul Mackerras6751b712009-05-11 12:08:02 +1000560static int
561group_sched_in(struct perf_counter *group_counter,
562 struct perf_cpu_context *cpuctx,
563 struct perf_counter_context *ctx,
564 int cpu)
565{
566 struct perf_counter *counter, *partial_group;
567 int ret;
568
569 if (group_counter->state == PERF_COUNTER_STATE_OFF)
570 return 0;
571
572 ret = hw_perf_group_sched_in(group_counter, cpuctx, ctx, cpu);
573 if (ret)
574 return ret < 0 ? ret : 0;
575
Paul Mackerras6751b712009-05-11 12:08:02 +1000576 if (counter_sched_in(group_counter, cpuctx, ctx, cpu))
577 return -EAGAIN;
578
579 /*
580 * Schedule in siblings as one group (if any):
581 */
582 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
Paul Mackerras6751b712009-05-11 12:08:02 +1000583 if (counter_sched_in(counter, cpuctx, ctx, cpu)) {
584 partial_group = counter;
585 goto group_error;
586 }
587 }
588
589 return 0;
590
591group_error:
592 /*
593 * Groups can be scheduled in as one unit only, so undo any
594 * partial group before returning:
595 */
596 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
597 if (counter == partial_group)
598 break;
599 counter_sched_out(counter, cpuctx, ctx);
600 }
601 counter_sched_out(group_counter, cpuctx, ctx);
602
603 return -EAGAIN;
604}
605
Thomas Gleixner0793a612008-12-04 20:12:29 +0100606/*
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100607 * Return 1 for a group consisting entirely of software counters,
608 * 0 if the group contains any hardware counters.
609 */
610static int is_software_only_group(struct perf_counter *leader)
611{
612 struct perf_counter *counter;
613
614 if (!is_software_counter(leader))
615 return 0;
Peter Zijlstra5c148192009-03-25 12:30:23 +0100616
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100617 list_for_each_entry(counter, &leader->sibling_list, list_entry)
618 if (!is_software_counter(counter))
619 return 0;
Peter Zijlstra5c148192009-03-25 12:30:23 +0100620
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100621 return 1;
622}
623
624/*
625 * Work out whether we can put this counter group on the CPU now.
626 */
627static int group_can_go_on(struct perf_counter *counter,
628 struct perf_cpu_context *cpuctx,
629 int can_add_hw)
630{
631 /*
632 * Groups consisting entirely of software counters can always go on.
633 */
634 if (is_software_only_group(counter))
635 return 1;
636 /*
637 * If an exclusive group is already on, no other hardware
638 * counters can go on.
639 */
640 if (cpuctx->exclusive)
641 return 0;
642 /*
643 * If this group is exclusive and there are already
644 * counters on the CPU, it can't go on.
645 */
Peter Zijlstra0d486962009-06-02 19:22:16 +0200646 if (counter->attr.exclusive && cpuctx->active_oncpu)
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100647 return 0;
648 /*
649 * Otherwise, try to add it if all previous groups were able
650 * to go on.
651 */
652 return can_add_hw;
653}
654
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100655static void add_counter_to_ctx(struct perf_counter *counter,
656 struct perf_counter_context *ctx)
657{
658 list_add_counter(counter, ctx);
Peter Zijlstra4af49982009-04-06 11:45:10 +0200659 counter->tstamp_enabled = ctx->time;
660 counter->tstamp_running = ctx->time;
661 counter->tstamp_stopped = ctx->time;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100662}
663
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100664/*
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100665 * Cross CPU call to install and enable a performance counter
Peter Zijlstra682076a2009-05-23 18:28:57 +0200666 *
667 * Must be called with ctx->mutex held
Thomas Gleixner0793a612008-12-04 20:12:29 +0100668 */
669static void __perf_install_in_context(void *info)
670{
671 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
672 struct perf_counter *counter = info;
673 struct perf_counter_context *ctx = counter->ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100674 struct perf_counter *leader = counter->group_leader;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100675 int cpu = smp_processor_id();
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100676 int err;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100677
678 /*
679 * If this is a task context, we need to check whether it is
680 * the current task context of this cpu. If not it has been
681 * scheduled out before the smp call arrived.
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000682 * Or possibly this is the right context but it isn't
683 * on this cpu because it had no counters.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100684 */
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000685 if (ctx->task && cpuctx->task_ctx != ctx) {
Peter Zijlstra665c2142009-05-29 14:51:57 +0200686 if (cpuctx->task_ctx || ctx->task != current)
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000687 return;
688 cpuctx->task_ctx = ctx;
689 }
Thomas Gleixner0793a612008-12-04 20:12:29 +0100690
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200691 spin_lock(&ctx->lock);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000692 ctx->is_active = 1;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200693 update_context_time(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100694
695 /*
696 * Protect the list operation against NMI by disabling the
697 * counters on a global level. NOP for non NMI based counters.
698 */
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200699 perf_disable();
Thomas Gleixner0793a612008-12-04 20:12:29 +0100700
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100701 add_counter_to_ctx(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100702
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100703 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100704 * Don't put the counter on if it is disabled or if
705 * it is in a group and the group isn't on.
706 */
707 if (counter->state != PERF_COUNTER_STATE_INACTIVE ||
708 (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE))
709 goto unlock;
710
711 /*
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100712 * An exclusive counter can't go on if there are already active
713 * hardware counters, and no hardware counter can go on if there
714 * is already an exclusive counter on.
715 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100716 if (!group_can_go_on(counter, cpuctx, 1))
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100717 err = -EEXIST;
718 else
719 err = counter_sched_in(counter, cpuctx, ctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100720
Paul Mackerrasd859e292009-01-17 18:10:22 +1100721 if (err) {
722 /*
723 * This counter couldn't go on. If it is in a group
724 * then we have to pull the whole group off.
725 * If the counter group is pinned then put it in error state.
726 */
727 if (leader != counter)
728 group_sched_out(leader, cpuctx, ctx);
Peter Zijlstra0d486962009-06-02 19:22:16 +0200729 if (leader->attr.pinned) {
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100730 update_group_times(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100731 leader->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100732 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100733 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100734
735 if (!err && !ctx->task && cpuctx->max_pertask)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100736 cpuctx->max_pertask--;
737
Paul Mackerrasd859e292009-01-17 18:10:22 +1100738 unlock:
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200739 perf_enable();
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100740
Peter Zijlstra665c2142009-05-29 14:51:57 +0200741 spin_unlock(&ctx->lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100742}
743
744/*
745 * Attach a performance counter to a context
746 *
747 * First we add the counter to the list with the hardware enable bit
748 * in counter->hw_config cleared.
749 *
750 * If the counter is attached to a task which is on a CPU we use a smp
751 * call to enable it in the task context. The task might have been
752 * scheduled away, but we check this in the smp call again.
Paul Mackerrasd859e292009-01-17 18:10:22 +1100753 *
754 * Must be called with ctx->mutex held.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100755 */
756static void
757perf_install_in_context(struct perf_counter_context *ctx,
758 struct perf_counter *counter,
759 int cpu)
760{
761 struct task_struct *task = ctx->task;
762
Thomas Gleixner0793a612008-12-04 20:12:29 +0100763 if (!task) {
764 /*
765 * Per cpu counters are installed via an smp call and
766 * the install is always sucessful.
767 */
768 smp_call_function_single(cpu, __perf_install_in_context,
769 counter, 1);
770 return;
771 }
772
Thomas Gleixner0793a612008-12-04 20:12:29 +0100773retry:
774 task_oncpu_function_call(task, __perf_install_in_context,
775 counter);
776
777 spin_lock_irq(&ctx->lock);
778 /*
Thomas Gleixner0793a612008-12-04 20:12:29 +0100779 * we need to retry the smp call.
780 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100781 if (ctx->is_active && list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100782 spin_unlock_irq(&ctx->lock);
783 goto retry;
784 }
785
786 /*
787 * The lock prevents that this context is scheduled in so we
788 * can add the counter safely, if it the call above did not
789 * succeed.
790 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100791 if (list_empty(&counter->list_entry))
792 add_counter_to_ctx(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100793 spin_unlock_irq(&ctx->lock);
794}
795
Paul Mackerrasd859e292009-01-17 18:10:22 +1100796/*
797 * Cross CPU call to enable a performance counter
798 */
799static void __perf_counter_enable(void *info)
Ingo Molnar04289bb2008-12-11 08:38:42 +0100800{
Paul Mackerrasd859e292009-01-17 18:10:22 +1100801 struct perf_counter *counter = info;
802 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
803 struct perf_counter_context *ctx = counter->ctx;
804 struct perf_counter *leader = counter->group_leader;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100805 int err;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100806
807 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100808 * If this is a per-task counter, need to check whether this
809 * counter's task is the current task on this cpu.
Ingo Molnar04289bb2008-12-11 08:38:42 +0100810 */
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000811 if (ctx->task && cpuctx->task_ctx != ctx) {
Peter Zijlstra665c2142009-05-29 14:51:57 +0200812 if (cpuctx->task_ctx || ctx->task != current)
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000813 return;
814 cpuctx->task_ctx = ctx;
815 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100816
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200817 spin_lock(&ctx->lock);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000818 ctx->is_active = 1;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200819 update_context_time(ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100820
821 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
822 goto unlock;
823 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200824 counter->tstamp_enabled = ctx->time - counter->total_time_enabled;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100825
826 /*
827 * If the counter is in a group and isn't the group leader,
828 * then don't put it on unless the group is on.
829 */
830 if (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE)
831 goto unlock;
832
Paul Mackerrase758a332009-05-12 21:59:01 +1000833 if (!group_can_go_on(counter, cpuctx, 1)) {
Paul Mackerrasd859e292009-01-17 18:10:22 +1100834 err = -EEXIST;
Paul Mackerrase758a332009-05-12 21:59:01 +1000835 } else {
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200836 perf_disable();
Paul Mackerrase758a332009-05-12 21:59:01 +1000837 if (counter == leader)
838 err = group_sched_in(counter, cpuctx, ctx,
839 smp_processor_id());
840 else
841 err = counter_sched_in(counter, cpuctx, ctx,
842 smp_processor_id());
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200843 perf_enable();
Paul Mackerrase758a332009-05-12 21:59:01 +1000844 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100845
846 if (err) {
847 /*
848 * If this counter can't go on and it's part of a
849 * group, then the whole group has to come off.
850 */
851 if (leader != counter)
852 group_sched_out(leader, cpuctx, ctx);
Peter Zijlstra0d486962009-06-02 19:22:16 +0200853 if (leader->attr.pinned) {
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100854 update_group_times(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100855 leader->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100856 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100857 }
858
859 unlock:
Peter Zijlstra665c2142009-05-29 14:51:57 +0200860 spin_unlock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100861}
862
863/*
864 * Enable a counter.
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000865 *
866 * If counter->ctx is a cloned context, callers must make sure that
867 * every task struct that counter->ctx->task could possibly point to
868 * remains valid. This condition is satisfied when called through
869 * perf_counter_for_each_child or perf_counter_for_each as described
870 * for perf_counter_disable.
Paul Mackerrasd859e292009-01-17 18:10:22 +1100871 */
872static void perf_counter_enable(struct perf_counter *counter)
873{
874 struct perf_counter_context *ctx = counter->ctx;
875 struct task_struct *task = ctx->task;
876
877 if (!task) {
878 /*
879 * Enable the counter on the cpu that it's on
880 */
881 smp_call_function_single(counter->cpu, __perf_counter_enable,
882 counter, 1);
883 return;
884 }
885
886 spin_lock_irq(&ctx->lock);
887 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
888 goto out;
889
890 /*
891 * If the counter is in error state, clear that first.
892 * That way, if we see the counter in error state below, we
893 * know that it has gone back into error state, as distinct
894 * from the task having been scheduled away before the
895 * cross-call arrived.
896 */
897 if (counter->state == PERF_COUNTER_STATE_ERROR)
898 counter->state = PERF_COUNTER_STATE_OFF;
899
900 retry:
901 spin_unlock_irq(&ctx->lock);
902 task_oncpu_function_call(task, __perf_counter_enable, counter);
903
904 spin_lock_irq(&ctx->lock);
905
906 /*
907 * If the context is active and the counter is still off,
908 * we need to retry the cross-call.
909 */
910 if (ctx->is_active && counter->state == PERF_COUNTER_STATE_OFF)
911 goto retry;
912
913 /*
914 * Since we have the lock this context can't be scheduled
915 * in, so we can change the state safely.
916 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100917 if (counter->state == PERF_COUNTER_STATE_OFF) {
Paul Mackerrasd859e292009-01-17 18:10:22 +1100918 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200919 counter->tstamp_enabled =
920 ctx->time - counter->total_time_enabled;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100921 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100922 out:
923 spin_unlock_irq(&ctx->lock);
924}
925
Peter Zijlstra2023b352009-05-05 17:50:26 +0200926static int perf_counter_refresh(struct perf_counter *counter, int refresh)
Peter Zijlstra79f14642009-04-06 11:45:07 +0200927{
Peter Zijlstra2023b352009-05-05 17:50:26 +0200928 /*
929 * not supported on inherited counters
930 */
Peter Zijlstra0d486962009-06-02 19:22:16 +0200931 if (counter->attr.inherit)
Peter Zijlstra2023b352009-05-05 17:50:26 +0200932 return -EINVAL;
933
Peter Zijlstra79f14642009-04-06 11:45:07 +0200934 atomic_add(refresh, &counter->event_limit);
935 perf_counter_enable(counter);
Peter Zijlstra2023b352009-05-05 17:50:26 +0200936
937 return 0;
Peter Zijlstra79f14642009-04-06 11:45:07 +0200938}
939
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100940void __perf_counter_sched_out(struct perf_counter_context *ctx,
941 struct perf_cpu_context *cpuctx)
942{
943 struct perf_counter *counter;
944
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100945 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100946 ctx->is_active = 0;
947 if (likely(!ctx->nr_counters))
948 goto out;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200949 update_context_time(ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100950
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200951 perf_disable();
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100952 if (ctx->nr_active) {
Peter Zijlstraafedadf2009-05-20 12:21:22 +0200953 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
954 if (counter != counter->group_leader)
955 counter_sched_out(counter, cpuctx, ctx);
956 else
957 group_sched_out(counter, cpuctx, ctx);
958 }
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100959 }
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200960 perf_enable();
Paul Mackerrasd859e292009-01-17 18:10:22 +1100961 out:
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100962 spin_unlock(&ctx->lock);
963}
964
Thomas Gleixner0793a612008-12-04 20:12:29 +0100965/*
Paul Mackerras564c2b22009-05-22 14:27:22 +1000966 * Test whether two contexts are equivalent, i.e. whether they
967 * have both been cloned from the same version of the same context
968 * and they both have the same number of enabled counters.
969 * If the number of enabled counters is the same, then the set
970 * of enabled counters should be the same, because these are both
971 * inherited contexts, therefore we can't access individual counters
972 * in them directly with an fd; we can only enable/disable all
973 * counters via prctl, or enable/disable all counters in a family
974 * via ioctl, which will have the same effect on both contexts.
975 */
976static int context_equiv(struct perf_counter_context *ctx1,
977 struct perf_counter_context *ctx2)
978{
979 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
Paul Mackerrasad3a37d2009-05-29 16:06:20 +1000980 && ctx1->parent_gen == ctx2->parent_gen
Paul Mackerras25346b92009-06-01 17:48:12 +1000981 && !ctx1->pin_count && !ctx2->pin_count;
Paul Mackerras564c2b22009-05-22 14:27:22 +1000982}
983
984/*
Thomas Gleixner0793a612008-12-04 20:12:29 +0100985 * Called from scheduler to remove the counters of the current task,
986 * with interrupts disabled.
987 *
988 * We stop each counter and update the counter value in counter->count.
989 *
Ingo Molnar76715812008-12-17 14:20:28 +0100990 * This does not protect us against NMI, but disable()
Thomas Gleixner0793a612008-12-04 20:12:29 +0100991 * sets the disabled bit in the control field of counter _before_
992 * accessing the counter control register. If a NMI hits, then it will
993 * not restart the counter.
994 */
Paul Mackerras564c2b22009-05-22 14:27:22 +1000995void perf_counter_task_sched_out(struct task_struct *task,
996 struct task_struct *next, int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100997{
998 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000999 struct perf_counter_context *ctx = task->perf_counter_ctxp;
Paul Mackerras564c2b22009-05-22 14:27:22 +10001000 struct perf_counter_context *next_ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001001 struct perf_counter_context *parent;
Peter Zijlstra4a0deca2009-03-19 20:26:12 +01001002 struct pt_regs *regs;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001003 int do_switch = 1;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001004
Peter Zijlstra10989fb2009-05-25 14:45:28 +02001005 regs = task_pt_regs(task);
1006 perf_swcounter_event(PERF_COUNT_CONTEXT_SWITCHES, 1, 1, regs, 0);
1007
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001008 if (likely(!ctx || !cpuctx->task_ctx))
Thomas Gleixner0793a612008-12-04 20:12:29 +01001009 return;
1010
Peter Zijlstrabce379b2009-04-06 11:45:13 +02001011 update_context_time(ctx);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001012
1013 rcu_read_lock();
1014 parent = rcu_dereference(ctx->parent_ctx);
Paul Mackerras564c2b22009-05-22 14:27:22 +10001015 next_ctx = next->perf_counter_ctxp;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001016 if (parent && next_ctx &&
1017 rcu_dereference(next_ctx->parent_ctx) == parent) {
1018 /*
1019 * Looks like the two contexts are clones, so we might be
1020 * able to optimize the context switch. We lock both
1021 * contexts and check that they are clones under the
1022 * lock (including re-checking that neither has been
1023 * uncloned in the meantime). It doesn't matter which
1024 * order we take the locks because no other cpu could
1025 * be trying to lock both of these tasks.
1026 */
1027 spin_lock(&ctx->lock);
1028 spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
1029 if (context_equiv(ctx, next_ctx)) {
Peter Zijlstra665c2142009-05-29 14:51:57 +02001030 /*
1031 * XXX do we need a memory barrier of sorts
1032 * wrt to rcu_dereference() of perf_counter_ctxp
1033 */
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001034 task->perf_counter_ctxp = next_ctx;
1035 next->perf_counter_ctxp = ctx;
1036 ctx->task = next;
1037 next_ctx->task = task;
1038 do_switch = 0;
1039 }
1040 spin_unlock(&next_ctx->lock);
1041 spin_unlock(&ctx->lock);
Paul Mackerras564c2b22009-05-22 14:27:22 +10001042 }
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001043 rcu_read_unlock();
Paul Mackerras564c2b22009-05-22 14:27:22 +10001044
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001045 if (do_switch) {
1046 __perf_counter_sched_out(ctx, cpuctx);
1047 cpuctx->task_ctx = NULL;
1048 }
Thomas Gleixner0793a612008-12-04 20:12:29 +01001049}
1050
Peter Zijlstra665c2142009-05-29 14:51:57 +02001051/*
1052 * Called with IRQs disabled
1053 */
Paul Mackerrasa08b1592009-05-11 15:46:10 +10001054static void __perf_counter_task_sched_out(struct perf_counter_context *ctx)
1055{
1056 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1057
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001058 if (!cpuctx->task_ctx)
1059 return;
Ingo Molnar012b84d2009-05-17 11:08:41 +02001060
1061 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
1062 return;
1063
Paul Mackerrasa08b1592009-05-11 15:46:10 +10001064 __perf_counter_sched_out(ctx, cpuctx);
1065 cpuctx->task_ctx = NULL;
1066}
1067
Peter Zijlstra665c2142009-05-29 14:51:57 +02001068/*
1069 * Called with IRQs disabled
1070 */
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001071static void perf_counter_cpu_sched_out(struct perf_cpu_context *cpuctx)
Ingo Molnar04289bb2008-12-11 08:38:42 +01001072{
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001073 __perf_counter_sched_out(&cpuctx->ctx, cpuctx);
Ingo Molnar04289bb2008-12-11 08:38:42 +01001074}
1075
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001076static void
1077__perf_counter_sched_in(struct perf_counter_context *ctx,
1078 struct perf_cpu_context *cpuctx, int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001079{
Thomas Gleixner0793a612008-12-04 20:12:29 +01001080 struct perf_counter *counter;
Paul Mackerrasdd0e6ba2009-01-12 15:11:00 +11001081 int can_add_hw = 1;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001082
Thomas Gleixner0793a612008-12-04 20:12:29 +01001083 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001084 ctx->is_active = 1;
1085 if (likely(!ctx->nr_counters))
1086 goto out;
1087
Peter Zijlstra4af49982009-04-06 11:45:10 +02001088 ctx->timestamp = perf_clock();
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001089
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001090 perf_disable();
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001091
1092 /*
1093 * First go through the list and put on any pinned groups
1094 * in order to give them the best chance of going on.
1095 */
Ingo Molnar04289bb2008-12-11 08:38:42 +01001096 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001097 if (counter->state <= PERF_COUNTER_STATE_OFF ||
Peter Zijlstra0d486962009-06-02 19:22:16 +02001098 !counter->attr.pinned)
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001099 continue;
1100 if (counter->cpu != -1 && counter->cpu != cpu)
1101 continue;
1102
Peter Zijlstraafedadf2009-05-20 12:21:22 +02001103 if (counter != counter->group_leader)
1104 counter_sched_in(counter, cpuctx, ctx, cpu);
1105 else {
1106 if (group_can_go_on(counter, cpuctx, 1))
1107 group_sched_in(counter, cpuctx, ctx, cpu);
1108 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001109
1110 /*
1111 * If this pinned group hasn't been scheduled,
1112 * put it in error state.
1113 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001114 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
1115 update_group_times(counter);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001116 counter->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001117 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001118 }
1119
1120 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1121 /*
1122 * Ignore counters in OFF or ERROR state, and
1123 * ignore pinned counters since we did them already.
1124 */
1125 if (counter->state <= PERF_COUNTER_STATE_OFF ||
Peter Zijlstra0d486962009-06-02 19:22:16 +02001126 counter->attr.pinned)
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001127 continue;
1128
Ingo Molnar04289bb2008-12-11 08:38:42 +01001129 /*
1130 * Listen to the 'cpu' scheduling filter constraint
1131 * of counters:
1132 */
Thomas Gleixner0793a612008-12-04 20:12:29 +01001133 if (counter->cpu != -1 && counter->cpu != cpu)
1134 continue;
1135
Peter Zijlstraafedadf2009-05-20 12:21:22 +02001136 if (counter != counter->group_leader) {
1137 if (counter_sched_in(counter, cpuctx, ctx, cpu))
Paul Mackerrasdd0e6ba2009-01-12 15:11:00 +11001138 can_add_hw = 0;
Peter Zijlstraafedadf2009-05-20 12:21:22 +02001139 } else {
1140 if (group_can_go_on(counter, cpuctx, can_add_hw)) {
1141 if (group_sched_in(counter, cpuctx, ctx, cpu))
1142 can_add_hw = 0;
1143 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001144 }
Thomas Gleixner0793a612008-12-04 20:12:29 +01001145 }
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001146 perf_enable();
Paul Mackerrasd859e292009-01-17 18:10:22 +11001147 out:
Thomas Gleixner0793a612008-12-04 20:12:29 +01001148 spin_unlock(&ctx->lock);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001149}
Ingo Molnar04289bb2008-12-11 08:38:42 +01001150
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001151/*
1152 * Called from scheduler to add the counters of the current task
1153 * with interrupts disabled.
1154 *
1155 * We restore the counter value and then enable it.
1156 *
1157 * This does not protect us against NMI, but enable()
1158 * sets the enabled bit in the control field of counter _before_
1159 * accessing the counter control register. If a NMI hits, then it will
1160 * keep the counter running.
1161 */
1162void perf_counter_task_sched_in(struct task_struct *task, int cpu)
1163{
1164 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001165 struct perf_counter_context *ctx = task->perf_counter_ctxp;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001166
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001167 if (likely(!ctx))
1168 return;
Paul Mackerras564c2b22009-05-22 14:27:22 +10001169 if (cpuctx->task_ctx == ctx)
1170 return;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001171 __perf_counter_sched_in(ctx, cpuctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001172 cpuctx->task_ctx = ctx;
1173}
1174
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001175static void perf_counter_cpu_sched_in(struct perf_cpu_context *cpuctx, int cpu)
1176{
1177 struct perf_counter_context *ctx = &cpuctx->ctx;
1178
1179 __perf_counter_sched_in(ctx, cpuctx, cpu);
1180}
1181
Peter Zijlstraa78ac322009-05-25 17:39:05 +02001182#define MAX_INTERRUPTS (~0ULL)
1183
1184static void perf_log_throttle(struct perf_counter *counter, int enable);
Peter Zijlstra26b119b2009-05-20 12:21:20 +02001185static void perf_log_period(struct perf_counter *counter, u64 period);
1186
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001187static void perf_adjust_period(struct perf_counter *counter, u64 events)
1188{
1189 struct hw_perf_counter *hwc = &counter->hw;
1190 u64 period, sample_period;
1191 s64 delta;
1192
1193 events *= hwc->sample_period;
1194 period = div64_u64(events, counter->attr.sample_freq);
1195
1196 delta = (s64)(period - hwc->sample_period);
1197 delta = (delta + 7) / 8; /* low pass filter */
1198
1199 sample_period = hwc->sample_period + delta;
1200
1201 if (!sample_period)
1202 sample_period = 1;
1203
1204 perf_log_period(counter, sample_period);
1205
1206 hwc->sample_period = sample_period;
1207}
1208
1209static void perf_ctx_adjust_freq(struct perf_counter_context *ctx)
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001210{
1211 struct perf_counter *counter;
Peter Zijlstra6a24ed6c2009-06-05 18:01:29 +02001212 struct hw_perf_counter *hwc;
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001213 u64 interrupts, freq;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001214
1215 spin_lock(&ctx->lock);
1216 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1217 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
1218 continue;
1219
Peter Zijlstra6a24ed6c2009-06-05 18:01:29 +02001220 hwc = &counter->hw;
1221
1222 interrupts = hwc->interrupts;
1223 hwc->interrupts = 0;
Peter Zijlstraa78ac322009-05-25 17:39:05 +02001224
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001225 /*
1226 * unthrottle counters on the tick
1227 */
Peter Zijlstraa78ac322009-05-25 17:39:05 +02001228 if (interrupts == MAX_INTERRUPTS) {
1229 perf_log_throttle(counter, 1);
1230 counter->pmu->unthrottle(counter);
1231 interrupts = 2*sysctl_perf_counter_limit/HZ;
1232 }
1233
Peter Zijlstra0d486962009-06-02 19:22:16 +02001234 if (!counter->attr.freq || !counter->attr.sample_freq)
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001235 continue;
1236
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001237 /*
1238 * if the specified freq < HZ then we need to skip ticks
1239 */
Peter Zijlstra6a24ed6c2009-06-05 18:01:29 +02001240 if (counter->attr.sample_freq < HZ) {
1241 freq = counter->attr.sample_freq;
1242
1243 hwc->freq_count += freq;
1244 hwc->freq_interrupts += interrupts;
1245
1246 if (hwc->freq_count < HZ)
1247 continue;
1248
1249 interrupts = hwc->freq_interrupts;
1250 hwc->freq_interrupts = 0;
1251 hwc->freq_count -= HZ;
1252 } else
1253 freq = HZ;
1254
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001255 perf_adjust_period(counter, freq * interrupts);
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001256
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001257 /*
1258 * In order to avoid being stalled by an (accidental) huge
1259 * sample period, force reset the sample period if we didn't
1260 * get any events in this freq period.
1261 */
1262 if (!interrupts) {
1263 perf_disable();
1264 counter->pmu->disable(counter);
1265 atomic_set(&hwc->period_left, 0);
1266 counter->pmu->enable(counter);
1267 perf_enable();
1268 }
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001269 }
1270 spin_unlock(&ctx->lock);
1271}
1272
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001273/*
1274 * Round-robin a context's counters:
1275 */
1276static void rotate_ctx(struct perf_counter_context *ctx)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001277{
Thomas Gleixner0793a612008-12-04 20:12:29 +01001278 struct perf_counter *counter;
1279
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001280 if (!ctx->nr_counters)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001281 return;
1282
Thomas Gleixner0793a612008-12-04 20:12:29 +01001283 spin_lock(&ctx->lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001284 /*
Ingo Molnar04289bb2008-12-11 08:38:42 +01001285 * Rotate the first entry last (works just fine for group counters too):
Thomas Gleixner0793a612008-12-04 20:12:29 +01001286 */
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001287 perf_disable();
Ingo Molnar04289bb2008-12-11 08:38:42 +01001288 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Peter Zijlstra75564232009-03-13 12:21:29 +01001289 list_move_tail(&counter->list_entry, &ctx->counter_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001290 break;
1291 }
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001292 perf_enable();
Thomas Gleixner0793a612008-12-04 20:12:29 +01001293
1294 spin_unlock(&ctx->lock);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001295}
Thomas Gleixner0793a612008-12-04 20:12:29 +01001296
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001297void perf_counter_task_tick(struct task_struct *curr, int cpu)
1298{
Peter Zijlstra7fc23a52009-05-08 18:52:21 +02001299 struct perf_cpu_context *cpuctx;
1300 struct perf_counter_context *ctx;
1301
1302 if (!atomic_read(&nr_counters))
1303 return;
1304
1305 cpuctx = &per_cpu(perf_cpu_context, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001306 ctx = curr->perf_counter_ctxp;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001307
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001308 perf_ctx_adjust_freq(&cpuctx->ctx);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001309 if (ctx)
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001310 perf_ctx_adjust_freq(ctx);
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001311
Ingo Molnarb82914c2009-05-04 18:54:32 +02001312 perf_counter_cpu_sched_out(cpuctx);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001313 if (ctx)
1314 __perf_counter_task_sched_out(ctx);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001315
Ingo Molnarb82914c2009-05-04 18:54:32 +02001316 rotate_ctx(&cpuctx->ctx);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001317 if (ctx)
1318 rotate_ctx(ctx);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001319
Ingo Molnarb82914c2009-05-04 18:54:32 +02001320 perf_counter_cpu_sched_in(cpuctx, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001321 if (ctx)
1322 perf_counter_task_sched_in(curr, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001323}
1324
1325/*
Thomas Gleixner0793a612008-12-04 20:12:29 +01001326 * Cross CPU call to read the hardware counter
1327 */
Ingo Molnar76715812008-12-17 14:20:28 +01001328static void __read(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001329{
Ingo Molnar621a01e2008-12-11 12:46:46 +01001330 struct perf_counter *counter = info;
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001331 struct perf_counter_context *ctx = counter->ctx;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001332 unsigned long flags;
Ingo Molnar621a01e2008-12-11 12:46:46 +01001333
Peter Zijlstra849691a2009-04-06 11:45:12 +02001334 local_irq_save(flags);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001335 if (ctx->is_active)
Peter Zijlstra4af49982009-04-06 11:45:10 +02001336 update_context_time(ctx);
Robert Richter4aeb0b42009-04-29 12:47:03 +02001337 counter->pmu->read(counter);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001338 update_counter_times(counter);
Peter Zijlstra849691a2009-04-06 11:45:12 +02001339 local_irq_restore(flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001340}
1341
Ingo Molnar04289bb2008-12-11 08:38:42 +01001342static u64 perf_counter_read(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001343{
1344 /*
1345 * If counter is enabled and currently active on a CPU, update the
1346 * value in the counter structure:
1347 */
Ingo Molnar6a930702008-12-11 15:17:03 +01001348 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
Thomas Gleixner0793a612008-12-04 20:12:29 +01001349 smp_call_function_single(counter->oncpu,
Ingo Molnar76715812008-12-17 14:20:28 +01001350 __read, counter, 1);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001351 } else if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
1352 update_counter_times(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001353 }
1354
Ingo Molnaree060942008-12-13 09:00:03 +01001355 return atomic64_read(&counter->count);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001356}
1357
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001358/*
1359 * Initialize the perf_counter context in a task_struct:
1360 */
1361static void
1362__perf_counter_init_context(struct perf_counter_context *ctx,
1363 struct task_struct *task)
1364{
1365 memset(ctx, 0, sizeof(*ctx));
1366 spin_lock_init(&ctx->lock);
1367 mutex_init(&ctx->mutex);
1368 INIT_LIST_HEAD(&ctx->counter_list);
1369 INIT_LIST_HEAD(&ctx->event_list);
1370 atomic_set(&ctx->refcount, 1);
1371 ctx->task = task;
1372}
1373
Thomas Gleixner0793a612008-12-04 20:12:29 +01001374static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
1375{
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001376 struct perf_counter_context *parent_ctx;
Ingo Molnar22a4f652009-06-01 10:13:37 +02001377 struct perf_counter_context *ctx;
1378 struct perf_cpu_context *cpuctx;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001379 struct task_struct *task;
Paul Mackerras25346b92009-06-01 17:48:12 +10001380 unsigned long flags;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001381 int err;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001382
1383 /*
1384 * If cpu is not a wildcard then this is a percpu counter:
1385 */
1386 if (cpu != -1) {
1387 /* Must be root to operate on a CPU counter: */
Peter Zijlstra1ccd1542009-04-09 10:53:45 +02001388 if (sysctl_perf_counter_priv && !capable(CAP_SYS_ADMIN))
Thomas Gleixner0793a612008-12-04 20:12:29 +01001389 return ERR_PTR(-EACCES);
1390
1391 if (cpu < 0 || cpu > num_possible_cpus())
1392 return ERR_PTR(-EINVAL);
1393
1394 /*
1395 * We could be clever and allow to attach a counter to an
1396 * offline CPU and activate it when the CPU comes up, but
1397 * that's for later.
1398 */
1399 if (!cpu_isset(cpu, cpu_online_map))
1400 return ERR_PTR(-ENODEV);
1401
1402 cpuctx = &per_cpu(perf_cpu_context, cpu);
1403 ctx = &cpuctx->ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001404 get_ctx(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001405
Thomas Gleixner0793a612008-12-04 20:12:29 +01001406 return ctx;
1407 }
1408
1409 rcu_read_lock();
1410 if (!pid)
1411 task = current;
1412 else
1413 task = find_task_by_vpid(pid);
1414 if (task)
1415 get_task_struct(task);
1416 rcu_read_unlock();
1417
1418 if (!task)
1419 return ERR_PTR(-ESRCH);
1420
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001421 /*
1422 * Can't attach counters to a dying task.
1423 */
1424 err = -ESRCH;
1425 if (task->flags & PF_EXITING)
1426 goto errout;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001427
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001428 /* Reuse ptrace permission checks for now. */
1429 err = -EACCES;
1430 if (!ptrace_may_access(task, PTRACE_MODE_READ))
1431 goto errout;
1432
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001433 retry:
Paul Mackerras25346b92009-06-01 17:48:12 +10001434 ctx = perf_lock_task_context(task, &flags);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001435 if (ctx) {
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001436 parent_ctx = ctx->parent_ctx;
1437 if (parent_ctx) {
1438 put_ctx(parent_ctx);
1439 ctx->parent_ctx = NULL; /* no longer a clone */
1440 }
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001441 /*
1442 * Get an extra reference before dropping the lock so that
1443 * this context won't get freed if the task exits.
1444 */
1445 get_ctx(ctx);
Paul Mackerras25346b92009-06-01 17:48:12 +10001446 spin_unlock_irqrestore(&ctx->lock, flags);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001447 }
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001448
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001449 if (!ctx) {
1450 ctx = kmalloc(sizeof(struct perf_counter_context), GFP_KERNEL);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001451 err = -ENOMEM;
1452 if (!ctx)
1453 goto errout;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001454 __perf_counter_init_context(ctx, task);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001455 get_ctx(ctx);
1456 if (cmpxchg(&task->perf_counter_ctxp, NULL, ctx)) {
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001457 /*
1458 * We raced with some other task; use
1459 * the context they set.
1460 */
1461 kfree(ctx);
Paul Mackerras25346b92009-06-01 17:48:12 +10001462 goto retry;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001463 }
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001464 get_task_struct(task);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001465 }
1466
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001467 put_task_struct(task);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001468 return ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001469
1470 errout:
1471 put_task_struct(task);
1472 return ERR_PTR(err);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001473}
1474
Peter Zijlstra592903c2009-03-13 12:21:36 +01001475static void free_counter_rcu(struct rcu_head *head)
1476{
1477 struct perf_counter *counter;
1478
1479 counter = container_of(head, struct perf_counter, rcu_head);
Peter Zijlstra709e50c2009-06-02 14:13:15 +02001480 if (counter->ns)
1481 put_pid_ns(counter->ns);
Peter Zijlstra592903c2009-03-13 12:21:36 +01001482 kfree(counter);
1483}
1484
Peter Zijlstra925d5192009-03-30 19:07:02 +02001485static void perf_pending_sync(struct perf_counter *counter);
1486
Peter Zijlstraf1600952009-03-19 20:26:16 +01001487static void free_counter(struct perf_counter *counter)
1488{
Peter Zijlstra925d5192009-03-30 19:07:02 +02001489 perf_pending_sync(counter);
1490
Peter Zijlstra7fc23a52009-05-08 18:52:21 +02001491 atomic_dec(&nr_counters);
Peter Zijlstra0d486962009-06-02 19:22:16 +02001492 if (counter->attr.mmap)
Peter Zijlstra60313eb2009-06-04 16:53:44 +02001493 atomic_dec(&nr_mmap_counters);
Peter Zijlstra0d486962009-06-02 19:22:16 +02001494 if (counter->attr.comm)
Peter Zijlstra60313eb2009-06-04 16:53:44 +02001495 atomic_dec(&nr_comm_counters);
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02001496
Peter Zijlstrae077df42009-03-19 20:26:17 +01001497 if (counter->destroy)
1498 counter->destroy(counter);
1499
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001500 put_ctx(counter->ctx);
Peter Zijlstraf1600952009-03-19 20:26:16 +01001501 call_rcu(&counter->rcu_head, free_counter_rcu);
1502}
1503
Thomas Gleixner0793a612008-12-04 20:12:29 +01001504/*
1505 * Called when the last reference to the file is gone.
1506 */
1507static int perf_release(struct inode *inode, struct file *file)
1508{
1509 struct perf_counter *counter = file->private_data;
1510 struct perf_counter_context *ctx = counter->ctx;
1511
1512 file->private_data = NULL;
1513
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001514 WARN_ON_ONCE(ctx->parent_ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001515 mutex_lock(&ctx->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01001516 perf_counter_remove_from_context(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001517 mutex_unlock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001518
Peter Zijlstra082ff5a2009-05-23 18:29:00 +02001519 mutex_lock(&counter->owner->perf_counter_mutex);
1520 list_del_init(&counter->owner_entry);
1521 mutex_unlock(&counter->owner->perf_counter_mutex);
1522 put_task_struct(counter->owner);
1523
Peter Zijlstraf1600952009-03-19 20:26:16 +01001524 free_counter(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001525
1526 return 0;
1527}
1528
1529/*
1530 * Read the performance counter - simple non blocking version for now
1531 */
1532static ssize_t
1533perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
1534{
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001535 u64 values[3];
1536 int n;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001537
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001538 /*
1539 * Return end-of-file for a read on a counter that is in
1540 * error state (i.e. because it was pinned but it couldn't be
1541 * scheduled on to the CPU at some point).
1542 */
1543 if (counter->state == PERF_COUNTER_STATE_ERROR)
1544 return 0;
1545
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001546 WARN_ON_ONCE(counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001547 mutex_lock(&counter->child_mutex);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001548 values[0] = perf_counter_read(counter);
1549 n = 1;
Peter Zijlstra0d486962009-06-02 19:22:16 +02001550 if (counter->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001551 values[n++] = counter->total_time_enabled +
1552 atomic64_read(&counter->child_total_time_enabled);
Peter Zijlstra0d486962009-06-02 19:22:16 +02001553 if (counter->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001554 values[n++] = counter->total_time_running +
1555 atomic64_read(&counter->child_total_time_running);
Peter Zijlstra0d486962009-06-02 19:22:16 +02001556 if (counter->attr.read_format & PERF_FORMAT_ID)
Peter Zijlstra8e5799b2009-06-02 15:08:15 +02001557 values[n++] = counter->id;
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001558 mutex_unlock(&counter->child_mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001559
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001560 if (count < n * sizeof(u64))
1561 return -EINVAL;
1562 count = n * sizeof(u64);
1563
1564 if (copy_to_user(buf, values, count))
1565 return -EFAULT;
1566
1567 return count;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001568}
1569
1570static ssize_t
Thomas Gleixner0793a612008-12-04 20:12:29 +01001571perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
1572{
1573 struct perf_counter *counter = file->private_data;
1574
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001575 return perf_read_hw(counter, buf, count);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001576}
1577
1578static unsigned int perf_poll(struct file *file, poll_table *wait)
1579{
1580 struct perf_counter *counter = file->private_data;
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001581 struct perf_mmap_data *data;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001582 unsigned int events = POLL_HUP;
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001583
1584 rcu_read_lock();
1585 data = rcu_dereference(counter->data);
1586 if (data)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001587 events = atomic_xchg(&data->poll, 0);
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001588 rcu_read_unlock();
Thomas Gleixner0793a612008-12-04 20:12:29 +01001589
1590 poll_wait(file, &counter->waitq, wait);
1591
Thomas Gleixner0793a612008-12-04 20:12:29 +01001592 return events;
1593}
1594
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001595static void perf_counter_reset(struct perf_counter *counter)
1596{
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001597 (void)perf_counter_read(counter);
Paul Mackerras615a3f12009-05-11 15:50:21 +10001598 atomic64_set(&counter->count, 0);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001599 perf_counter_update_userpage(counter);
1600}
1601
1602static void perf_counter_for_each_sibling(struct perf_counter *counter,
1603 void (*func)(struct perf_counter *))
1604{
1605 struct perf_counter_context *ctx = counter->ctx;
1606 struct perf_counter *sibling;
1607
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001608 WARN_ON_ONCE(ctx->parent_ctx);
Peter Zijlstra682076a2009-05-23 18:28:57 +02001609 mutex_lock(&ctx->mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001610 counter = counter->group_leader;
1611
1612 func(counter);
1613 list_for_each_entry(sibling, &counter->sibling_list, list_entry)
1614 func(sibling);
Peter Zijlstra682076a2009-05-23 18:28:57 +02001615 mutex_unlock(&ctx->mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001616}
1617
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001618/*
1619 * Holding the top-level counter's child_mutex means that any
1620 * descendant process that has inherited this counter will block
1621 * in sync_child_counter if it goes to exit, thus satisfying the
1622 * task existence requirements of perf_counter_enable/disable.
1623 */
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001624static void perf_counter_for_each_child(struct perf_counter *counter,
1625 void (*func)(struct perf_counter *))
1626{
1627 struct perf_counter *child;
1628
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001629 WARN_ON_ONCE(counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001630 mutex_lock(&counter->child_mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001631 func(counter);
1632 list_for_each_entry(child, &counter->child_list, child_list)
1633 func(child);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001634 mutex_unlock(&counter->child_mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001635}
1636
1637static void perf_counter_for_each(struct perf_counter *counter,
1638 void (*func)(struct perf_counter *))
1639{
1640 struct perf_counter *child;
1641
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001642 WARN_ON_ONCE(counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001643 mutex_lock(&counter->child_mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001644 perf_counter_for_each_sibling(counter, func);
1645 list_for_each_entry(child, &counter->child_list, child_list)
1646 perf_counter_for_each_sibling(child, func);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001647 mutex_unlock(&counter->child_mutex);
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001648}
1649
Peter Zijlstra08247e32009-06-02 16:46:57 +02001650static int perf_counter_period(struct perf_counter *counter, u64 __user *arg)
1651{
1652 struct perf_counter_context *ctx = counter->ctx;
1653 unsigned long size;
1654 int ret = 0;
1655 u64 value;
1656
Peter Zijlstra0d486962009-06-02 19:22:16 +02001657 if (!counter->attr.sample_period)
Peter Zijlstra08247e32009-06-02 16:46:57 +02001658 return -EINVAL;
1659
1660 size = copy_from_user(&value, arg, sizeof(value));
1661 if (size != sizeof(value))
1662 return -EFAULT;
1663
1664 if (!value)
1665 return -EINVAL;
1666
1667 spin_lock_irq(&ctx->lock);
Peter Zijlstra0d486962009-06-02 19:22:16 +02001668 if (counter->attr.freq) {
Peter Zijlstra08247e32009-06-02 16:46:57 +02001669 if (value > sysctl_perf_counter_limit) {
1670 ret = -EINVAL;
1671 goto unlock;
1672 }
1673
Peter Zijlstra0d486962009-06-02 19:22:16 +02001674 counter->attr.sample_freq = value;
Peter Zijlstra08247e32009-06-02 16:46:57 +02001675 } else {
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001676 perf_log_period(counter, value);
1677
Peter Zijlstra0d486962009-06-02 19:22:16 +02001678 counter->attr.sample_period = value;
Peter Zijlstra08247e32009-06-02 16:46:57 +02001679 counter->hw.sample_period = value;
Peter Zijlstra08247e32009-06-02 16:46:57 +02001680 }
1681unlock:
1682 spin_unlock_irq(&ctx->lock);
1683
1684 return ret;
1685}
1686
Paul Mackerrasd859e292009-01-17 18:10:22 +11001687static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1688{
1689 struct perf_counter *counter = file->private_data;
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001690 void (*func)(struct perf_counter *);
1691 u32 flags = arg;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001692
1693 switch (cmd) {
1694 case PERF_COUNTER_IOC_ENABLE:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001695 func = perf_counter_enable;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001696 break;
1697 case PERF_COUNTER_IOC_DISABLE:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001698 func = perf_counter_disable;
Peter Zijlstra79f14642009-04-06 11:45:07 +02001699 break;
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001700 case PERF_COUNTER_IOC_RESET:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001701 func = perf_counter_reset;
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001702 break;
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001703
1704 case PERF_COUNTER_IOC_REFRESH:
1705 return perf_counter_refresh(counter, arg);
Peter Zijlstra08247e32009-06-02 16:46:57 +02001706
1707 case PERF_COUNTER_IOC_PERIOD:
1708 return perf_counter_period(counter, (u64 __user *)arg);
1709
Paul Mackerrasd859e292009-01-17 18:10:22 +11001710 default:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001711 return -ENOTTY;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001712 }
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001713
1714 if (flags & PERF_IOC_FLAG_GROUP)
1715 perf_counter_for_each(counter, func);
1716 else
1717 perf_counter_for_each_child(counter, func);
1718
1719 return 0;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001720}
1721
Peter Zijlstra771d7cd2009-05-25 14:45:26 +02001722int perf_counter_task_enable(void)
1723{
1724 struct perf_counter *counter;
1725
1726 mutex_lock(&current->perf_counter_mutex);
1727 list_for_each_entry(counter, &current->perf_counter_list, owner_entry)
1728 perf_counter_for_each_child(counter, perf_counter_enable);
1729 mutex_unlock(&current->perf_counter_mutex);
1730
1731 return 0;
1732}
1733
1734int perf_counter_task_disable(void)
1735{
1736 struct perf_counter *counter;
1737
1738 mutex_lock(&current->perf_counter_mutex);
1739 list_for_each_entry(counter, &current->perf_counter_list, owner_entry)
1740 perf_counter_for_each_child(counter, perf_counter_disable);
1741 mutex_unlock(&current->perf_counter_mutex);
1742
1743 return 0;
1744}
1745
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001746/*
1747 * Callers need to ensure there can be no nesting of this function, otherwise
1748 * the seqlock logic goes bad. We can not serialize this because the arch
1749 * code calls this from NMI context.
1750 */
1751void perf_counter_update_userpage(struct perf_counter *counter)
Paul Mackerras37d81822009-03-23 18:22:08 +01001752{
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001753 struct perf_counter_mmap_page *userpg;
Ingo Molnar22a4f652009-06-01 10:13:37 +02001754 struct perf_mmap_data *data;
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001755
1756 rcu_read_lock();
1757 data = rcu_dereference(counter->data);
1758 if (!data)
1759 goto unlock;
1760
1761 userpg = data->user_page;
Paul Mackerras37d81822009-03-23 18:22:08 +01001762
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001763 /*
1764 * Disable preemption so as to not let the corresponding user-space
1765 * spin too long if we get preempted.
1766 */
1767 preempt_disable();
Paul Mackerras37d81822009-03-23 18:22:08 +01001768 ++userpg->lock;
Peter Zijlstra92f22a32009-04-02 11:12:04 +02001769 barrier();
Paul Mackerras37d81822009-03-23 18:22:08 +01001770 userpg->index = counter->hw.idx;
1771 userpg->offset = atomic64_read(&counter->count);
1772 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
1773 userpg->offset -= atomic64_read(&counter->hw.prev_count);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001774
Peter Zijlstra92f22a32009-04-02 11:12:04 +02001775 barrier();
Paul Mackerras37d81822009-03-23 18:22:08 +01001776 ++userpg->lock;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001777 preempt_enable();
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001778unlock:
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001779 rcu_read_unlock();
Paul Mackerras37d81822009-03-23 18:22:08 +01001780}
1781
1782static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1783{
1784 struct perf_counter *counter = vma->vm_file->private_data;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001785 struct perf_mmap_data *data;
1786 int ret = VM_FAULT_SIGBUS;
Paul Mackerras37d81822009-03-23 18:22:08 +01001787
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001788 rcu_read_lock();
1789 data = rcu_dereference(counter->data);
1790 if (!data)
1791 goto unlock;
Paul Mackerras37d81822009-03-23 18:22:08 +01001792
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001793 if (vmf->pgoff == 0) {
1794 vmf->page = virt_to_page(data->user_page);
1795 } else {
1796 int nr = vmf->pgoff - 1;
1797
1798 if ((unsigned)nr > data->nr_pages)
1799 goto unlock;
1800
1801 vmf->page = virt_to_page(data->data_pages[nr]);
1802 }
Paul Mackerras37d81822009-03-23 18:22:08 +01001803 get_page(vmf->page);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001804 ret = 0;
1805unlock:
1806 rcu_read_unlock();
1807
1808 return ret;
1809}
1810
1811static int perf_mmap_data_alloc(struct perf_counter *counter, int nr_pages)
1812{
1813 struct perf_mmap_data *data;
1814 unsigned long size;
1815 int i;
1816
1817 WARN_ON(atomic_read(&counter->mmap_count));
1818
1819 size = sizeof(struct perf_mmap_data);
1820 size += nr_pages * sizeof(void *);
1821
1822 data = kzalloc(size, GFP_KERNEL);
1823 if (!data)
1824 goto fail;
1825
1826 data->user_page = (void *)get_zeroed_page(GFP_KERNEL);
1827 if (!data->user_page)
1828 goto fail_user_page;
1829
1830 for (i = 0; i < nr_pages; i++) {
1831 data->data_pages[i] = (void *)get_zeroed_page(GFP_KERNEL);
1832 if (!data->data_pages[i])
1833 goto fail_data_pages;
1834 }
1835
1836 data->nr_pages = nr_pages;
Peter Zijlstra22c15582009-05-05 17:50:25 +02001837 atomic_set(&data->lock, -1);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001838
1839 rcu_assign_pointer(counter->data, data);
1840
Paul Mackerras37d81822009-03-23 18:22:08 +01001841 return 0;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001842
1843fail_data_pages:
1844 for (i--; i >= 0; i--)
1845 free_page((unsigned long)data->data_pages[i]);
1846
1847 free_page((unsigned long)data->user_page);
1848
1849fail_user_page:
1850 kfree(data);
1851
1852fail:
1853 return -ENOMEM;
1854}
1855
1856static void __perf_mmap_data_free(struct rcu_head *rcu_head)
1857{
Ingo Molnar22a4f652009-06-01 10:13:37 +02001858 struct perf_mmap_data *data;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001859 int i;
1860
Ingo Molnar22a4f652009-06-01 10:13:37 +02001861 data = container_of(rcu_head, struct perf_mmap_data, rcu_head);
1862
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001863 free_page((unsigned long)data->user_page);
1864 for (i = 0; i < data->nr_pages; i++)
1865 free_page((unsigned long)data->data_pages[i]);
1866 kfree(data);
1867}
1868
1869static void perf_mmap_data_free(struct perf_counter *counter)
1870{
1871 struct perf_mmap_data *data = counter->data;
1872
1873 WARN_ON(atomic_read(&counter->mmap_count));
1874
1875 rcu_assign_pointer(counter->data, NULL);
1876 call_rcu(&data->rcu_head, __perf_mmap_data_free);
1877}
1878
1879static void perf_mmap_open(struct vm_area_struct *vma)
1880{
1881 struct perf_counter *counter = vma->vm_file->private_data;
1882
1883 atomic_inc(&counter->mmap_count);
1884}
1885
1886static void perf_mmap_close(struct vm_area_struct *vma)
1887{
1888 struct perf_counter *counter = vma->vm_file->private_data;
1889
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001890 WARN_ON_ONCE(counter->ctx->parent_ctx);
Ingo Molnar22a4f652009-06-01 10:13:37 +02001891 if (atomic_dec_and_mutex_lock(&counter->mmap_count, &counter->mmap_mutex)) {
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001892 struct user_struct *user = current_user();
1893
1894 atomic_long_sub(counter->data->nr_pages + 1, &user->locked_vm);
Peter Zijlstrac5078f72009-05-05 17:50:24 +02001895 vma->vm_mm->locked_vm -= counter->data->nr_locked;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001896 perf_mmap_data_free(counter);
1897 mutex_unlock(&counter->mmap_mutex);
1898 }
Paul Mackerras37d81822009-03-23 18:22:08 +01001899}
1900
1901static struct vm_operations_struct perf_mmap_vmops = {
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001902 .open = perf_mmap_open,
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001903 .close = perf_mmap_close,
Paul Mackerras37d81822009-03-23 18:22:08 +01001904 .fault = perf_mmap_fault,
1905};
1906
1907static int perf_mmap(struct file *file, struct vm_area_struct *vma)
1908{
1909 struct perf_counter *counter = file->private_data;
Ingo Molnar22a4f652009-06-01 10:13:37 +02001910 unsigned long user_locked, user_lock_limit;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001911 struct user_struct *user = current_user();
Ingo Molnar22a4f652009-06-01 10:13:37 +02001912 unsigned long locked, lock_limit;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001913 unsigned long vma_size;
1914 unsigned long nr_pages;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001915 long user_extra, extra;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001916 int ret = 0;
Paul Mackerras37d81822009-03-23 18:22:08 +01001917
1918 if (!(vma->vm_flags & VM_SHARED) || (vma->vm_flags & VM_WRITE))
1919 return -EINVAL;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001920
1921 vma_size = vma->vm_end - vma->vm_start;
1922 nr_pages = (vma_size / PAGE_SIZE) - 1;
1923
Peter Zijlstra7730d862009-03-25 12:48:31 +01001924 /*
1925 * If we have data pages ensure they're a power-of-two number, so we
1926 * can do bitmasks instead of modulo.
1927 */
1928 if (nr_pages != 0 && !is_power_of_2(nr_pages))
Paul Mackerras37d81822009-03-23 18:22:08 +01001929 return -EINVAL;
1930
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001931 if (vma_size != PAGE_SIZE * (1 + nr_pages))
Paul Mackerras37d81822009-03-23 18:22:08 +01001932 return -EINVAL;
1933
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001934 if (vma->vm_pgoff != 0)
1935 return -EINVAL;
Paul Mackerras37d81822009-03-23 18:22:08 +01001936
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001937 WARN_ON_ONCE(counter->ctx->parent_ctx);
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001938 mutex_lock(&counter->mmap_mutex);
1939 if (atomic_inc_not_zero(&counter->mmap_count)) {
1940 if (nr_pages != counter->data->nr_pages)
1941 ret = -EINVAL;
1942 goto unlock;
1943 }
1944
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001945 user_extra = nr_pages + 1;
1946 user_lock_limit = sysctl_perf_counter_mlock >> (PAGE_SHIFT - 10);
Ingo Molnara3862d32009-05-24 09:02:37 +02001947
1948 /*
1949 * Increase the limit linearly with more CPUs:
1950 */
1951 user_lock_limit *= num_online_cpus();
1952
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001953 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
Peter Zijlstrac5078f72009-05-05 17:50:24 +02001954
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001955 extra = 0;
1956 if (user_locked > user_lock_limit)
1957 extra = user_locked - user_lock_limit;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001958
1959 lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
1960 lock_limit >>= PAGE_SHIFT;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001961 locked = vma->vm_mm->locked_vm + extra;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001962
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001963 if ((locked > lock_limit) && !capable(CAP_IPC_LOCK)) {
1964 ret = -EPERM;
1965 goto unlock;
1966 }
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001967
1968 WARN_ON(counter->data);
1969 ret = perf_mmap_data_alloc(counter, nr_pages);
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001970 if (ret)
1971 goto unlock;
1972
1973 atomic_set(&counter->mmap_count, 1);
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001974 atomic_long_add(user_extra, &user->locked_vm);
Peter Zijlstrac5078f72009-05-05 17:50:24 +02001975 vma->vm_mm->locked_vm += extra;
1976 counter->data->nr_locked = extra;
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001977unlock:
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001978 mutex_unlock(&counter->mmap_mutex);
Paul Mackerras37d81822009-03-23 18:22:08 +01001979
1980 vma->vm_flags &= ~VM_MAYWRITE;
1981 vma->vm_flags |= VM_RESERVED;
1982 vma->vm_ops = &perf_mmap_vmops;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001983
1984 return ret;
Paul Mackerras37d81822009-03-23 18:22:08 +01001985}
1986
Peter Zijlstra3c446b32009-04-06 11:45:01 +02001987static int perf_fasync(int fd, struct file *filp, int on)
1988{
Peter Zijlstra3c446b32009-04-06 11:45:01 +02001989 struct inode *inode = filp->f_path.dentry->d_inode;
Ingo Molnar22a4f652009-06-01 10:13:37 +02001990 struct perf_counter *counter = filp->private_data;
Peter Zijlstra3c446b32009-04-06 11:45:01 +02001991 int retval;
1992
1993 mutex_lock(&inode->i_mutex);
1994 retval = fasync_helper(fd, filp, on, &counter->fasync);
1995 mutex_unlock(&inode->i_mutex);
1996
1997 if (retval < 0)
1998 return retval;
1999
2000 return 0;
2001}
2002
Thomas Gleixner0793a612008-12-04 20:12:29 +01002003static const struct file_operations perf_fops = {
2004 .release = perf_release,
2005 .read = perf_read,
2006 .poll = perf_poll,
Paul Mackerrasd859e292009-01-17 18:10:22 +11002007 .unlocked_ioctl = perf_ioctl,
2008 .compat_ioctl = perf_ioctl,
Paul Mackerras37d81822009-03-23 18:22:08 +01002009 .mmap = perf_mmap,
Peter Zijlstra3c446b32009-04-06 11:45:01 +02002010 .fasync = perf_fasync,
Thomas Gleixner0793a612008-12-04 20:12:29 +01002011};
2012
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002013/*
Peter Zijlstra925d5192009-03-30 19:07:02 +02002014 * Perf counter wakeup
2015 *
2016 * If there's data, ensure we set the poll() state and publish everything
2017 * to user-space before waking everybody up.
2018 */
2019
2020void perf_counter_wakeup(struct perf_counter *counter)
2021{
Peter Zijlstra925d5192009-03-30 19:07:02 +02002022 wake_up_all(&counter->waitq);
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002023
2024 if (counter->pending_kill) {
2025 kill_fasync(&counter->fasync, SIGIO, counter->pending_kill);
2026 counter->pending_kill = 0;
2027 }
Peter Zijlstra925d5192009-03-30 19:07:02 +02002028}
2029
2030/*
2031 * Pending wakeups
2032 *
2033 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
2034 *
2035 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
2036 * single linked list and use cmpxchg() to add entries lockless.
2037 */
2038
Peter Zijlstra79f14642009-04-06 11:45:07 +02002039static void perf_pending_counter(struct perf_pending_entry *entry)
2040{
2041 struct perf_counter *counter = container_of(entry,
2042 struct perf_counter, pending);
2043
2044 if (counter->pending_disable) {
2045 counter->pending_disable = 0;
2046 perf_counter_disable(counter);
2047 }
2048
2049 if (counter->pending_wakeup) {
2050 counter->pending_wakeup = 0;
2051 perf_counter_wakeup(counter);
2052 }
2053}
2054
Peter Zijlstra671dec52009-04-06 11:45:02 +02002055#define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
Peter Zijlstra925d5192009-03-30 19:07:02 +02002056
Peter Zijlstra671dec52009-04-06 11:45:02 +02002057static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = {
Peter Zijlstra925d5192009-03-30 19:07:02 +02002058 PENDING_TAIL,
2059};
2060
Peter Zijlstra671dec52009-04-06 11:45:02 +02002061static void perf_pending_queue(struct perf_pending_entry *entry,
2062 void (*func)(struct perf_pending_entry *))
Peter Zijlstra925d5192009-03-30 19:07:02 +02002063{
Peter Zijlstra671dec52009-04-06 11:45:02 +02002064 struct perf_pending_entry **head;
Peter Zijlstra925d5192009-03-30 19:07:02 +02002065
Peter Zijlstra671dec52009-04-06 11:45:02 +02002066 if (cmpxchg(&entry->next, NULL, PENDING_TAIL) != NULL)
Peter Zijlstra925d5192009-03-30 19:07:02 +02002067 return;
2068
Peter Zijlstra671dec52009-04-06 11:45:02 +02002069 entry->func = func;
2070
2071 head = &get_cpu_var(perf_pending_head);
Peter Zijlstra925d5192009-03-30 19:07:02 +02002072
2073 do {
Peter Zijlstra671dec52009-04-06 11:45:02 +02002074 entry->next = *head;
2075 } while (cmpxchg(head, entry->next, entry) != entry->next);
Peter Zijlstra925d5192009-03-30 19:07:02 +02002076
2077 set_perf_counter_pending();
2078
Peter Zijlstra671dec52009-04-06 11:45:02 +02002079 put_cpu_var(perf_pending_head);
Peter Zijlstra925d5192009-03-30 19:07:02 +02002080}
2081
2082static int __perf_pending_run(void)
2083{
Peter Zijlstra671dec52009-04-06 11:45:02 +02002084 struct perf_pending_entry *list;
Peter Zijlstra925d5192009-03-30 19:07:02 +02002085 int nr = 0;
2086
Peter Zijlstra671dec52009-04-06 11:45:02 +02002087 list = xchg(&__get_cpu_var(perf_pending_head), PENDING_TAIL);
Peter Zijlstra925d5192009-03-30 19:07:02 +02002088 while (list != PENDING_TAIL) {
Peter Zijlstra671dec52009-04-06 11:45:02 +02002089 void (*func)(struct perf_pending_entry *);
2090 struct perf_pending_entry *entry = list;
Peter Zijlstra925d5192009-03-30 19:07:02 +02002091
2092 list = list->next;
2093
Peter Zijlstra671dec52009-04-06 11:45:02 +02002094 func = entry->func;
2095 entry->next = NULL;
Peter Zijlstra925d5192009-03-30 19:07:02 +02002096 /*
2097 * Ensure we observe the unqueue before we issue the wakeup,
2098 * so that we won't be waiting forever.
2099 * -- see perf_not_pending().
2100 */
2101 smp_wmb();
2102
Peter Zijlstra671dec52009-04-06 11:45:02 +02002103 func(entry);
Peter Zijlstra925d5192009-03-30 19:07:02 +02002104 nr++;
2105 }
2106
2107 return nr;
2108}
2109
2110static inline int perf_not_pending(struct perf_counter *counter)
2111{
2112 /*
2113 * If we flush on whatever cpu we run, there is a chance we don't
2114 * need to wait.
2115 */
2116 get_cpu();
2117 __perf_pending_run();
2118 put_cpu();
2119
2120 /*
2121 * Ensure we see the proper queue state before going to sleep
2122 * so that we do not miss the wakeup. -- see perf_pending_handle()
2123 */
2124 smp_rmb();
Peter Zijlstra671dec52009-04-06 11:45:02 +02002125 return counter->pending.next == NULL;
Peter Zijlstra925d5192009-03-30 19:07:02 +02002126}
2127
2128static void perf_pending_sync(struct perf_counter *counter)
2129{
2130 wait_event(counter->waitq, perf_not_pending(counter));
2131}
2132
2133void perf_counter_do_pending(void)
2134{
2135 __perf_pending_run();
2136}
2137
2138/*
Peter Zijlstra394ee072009-03-30 19:07:14 +02002139 * Callchain support -- arch specific
2140 */
2141
Peter Zijlstra9c03d882009-04-06 11:45:00 +02002142__weak struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
Peter Zijlstra394ee072009-03-30 19:07:14 +02002143{
2144 return NULL;
2145}
2146
2147/*
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002148 * Output
2149 */
2150
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002151struct perf_output_handle {
2152 struct perf_counter *counter;
2153 struct perf_mmap_data *data;
Peter Zijlstra8e3747c2009-06-02 16:16:02 +02002154 unsigned long head;
2155 unsigned long offset;
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002156 int nmi;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002157 int overflow;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002158 int locked;
2159 unsigned long flags;
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002160};
2161
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002162static void perf_output_wakeup(struct perf_output_handle *handle)
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002163{
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002164 atomic_set(&handle->data->poll, POLL_IN);
2165
Peter Zijlstra671dec52009-04-06 11:45:02 +02002166 if (handle->nmi) {
Peter Zijlstra79f14642009-04-06 11:45:07 +02002167 handle->counter->pending_wakeup = 1;
Peter Zijlstra671dec52009-04-06 11:45:02 +02002168 perf_pending_queue(&handle->counter->pending,
Peter Zijlstra79f14642009-04-06 11:45:07 +02002169 perf_pending_counter);
Peter Zijlstra671dec52009-04-06 11:45:02 +02002170 } else
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002171 perf_counter_wakeup(handle->counter);
2172}
2173
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002174/*
2175 * Curious locking construct.
2176 *
2177 * We need to ensure a later event doesn't publish a head when a former
2178 * event isn't done writing. However since we need to deal with NMIs we
2179 * cannot fully serialize things.
2180 *
2181 * What we do is serialize between CPUs so we only have to deal with NMI
2182 * nesting on a single CPU.
2183 *
2184 * We only publish the head (and generate a wakeup) when the outer-most
2185 * event completes.
2186 */
2187static void perf_output_lock(struct perf_output_handle *handle)
2188{
2189 struct perf_mmap_data *data = handle->data;
2190 int cpu;
2191
2192 handle->locked = 0;
2193
2194 local_irq_save(handle->flags);
2195 cpu = smp_processor_id();
2196
2197 if (in_nmi() && atomic_read(&data->lock) == cpu)
2198 return;
2199
Peter Zijlstra22c15582009-05-05 17:50:25 +02002200 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002201 cpu_relax();
2202
2203 handle->locked = 1;
2204}
2205
2206static void perf_output_unlock(struct perf_output_handle *handle)
2207{
2208 struct perf_mmap_data *data = handle->data;
Peter Zijlstra8e3747c2009-06-02 16:16:02 +02002209 unsigned long head;
2210 int cpu;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002211
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002212 data->done_head = data->head;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002213
2214 if (!handle->locked)
2215 goto out;
2216
2217again:
2218 /*
2219 * The xchg implies a full barrier that ensures all writes are done
2220 * before we publish the new head, matched by a rmb() in userspace when
2221 * reading this position.
2222 */
Peter Zijlstra8e3747c2009-06-02 16:16:02 +02002223 while ((head = atomic_long_xchg(&data->done_head, 0)))
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002224 data->user_page->data_head = head;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002225
2226 /*
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002227 * NMI can happen here, which means we can miss a done_head update.
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002228 */
2229
Peter Zijlstra22c15582009-05-05 17:50:25 +02002230 cpu = atomic_xchg(&data->lock, -1);
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002231 WARN_ON_ONCE(cpu != smp_processor_id());
2232
2233 /*
2234 * Therefore we have to validate we did not indeed do so.
2235 */
Peter Zijlstra8e3747c2009-06-02 16:16:02 +02002236 if (unlikely(atomic_long_read(&data->done_head))) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002237 /*
2238 * Since we had it locked, we can lock it again.
2239 */
Peter Zijlstra22c15582009-05-05 17:50:25 +02002240 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002241 cpu_relax();
2242
2243 goto again;
2244 }
2245
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002246 if (atomic_xchg(&data->wakeup, 0))
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002247 perf_output_wakeup(handle);
2248out:
2249 local_irq_restore(handle->flags);
2250}
2251
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002252static int perf_output_begin(struct perf_output_handle *handle,
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002253 struct perf_counter *counter, unsigned int size,
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002254 int nmi, int overflow)
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002255{
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002256 struct perf_mmap_data *data;
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002257 unsigned int offset, head;
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002258
Peter Zijlstra2023b352009-05-05 17:50:26 +02002259 /*
2260 * For inherited counters we send all the output towards the parent.
2261 */
2262 if (counter->parent)
2263 counter = counter->parent;
2264
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002265 rcu_read_lock();
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002266 data = rcu_dereference(counter->data);
2267 if (!data)
2268 goto out;
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002269
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002270 handle->data = data;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002271 handle->counter = counter;
2272 handle->nmi = nmi;
2273 handle->overflow = overflow;
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002274
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002275 if (!data->nr_pages)
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002276 goto fail;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002277
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002278 perf_output_lock(handle);
2279
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002280 do {
Paul Mackerras6dc5f2a2009-06-05 12:36:28 +10002281 offset = head = atomic_long_read(&data->head);
Peter Zijlstrac7138f32009-03-24 13:18:16 +01002282 head += size;
Peter Zijlstra8e3747c2009-06-02 16:16:02 +02002283 } while (atomic_long_cmpxchg(&data->head, offset, head) != offset);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002284
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002285 handle->offset = offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01002286 handle->head = head;
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002287
2288 if ((offset >> PAGE_SHIFT) != (head >> PAGE_SHIFT))
2289 atomic_set(&data->wakeup, 1);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002290
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002291 return 0;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002292
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002293fail:
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002294 perf_output_wakeup(handle);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002295out:
2296 rcu_read_unlock();
2297
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002298 return -ENOSPC;
2299}
2300
2301static void perf_output_copy(struct perf_output_handle *handle,
Peter Zijlstra089dd792009-06-05 14:04:55 +02002302 const void *buf, unsigned int len)
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002303{
2304 unsigned int pages_mask;
2305 unsigned int offset;
2306 unsigned int size;
2307 void **pages;
2308
2309 offset = handle->offset;
2310 pages_mask = handle->data->nr_pages - 1;
2311 pages = handle->data->data_pages;
2312
2313 do {
2314 unsigned int page_offset;
2315 int nr;
2316
2317 nr = (offset >> PAGE_SHIFT) & pages_mask;
2318 page_offset = offset & (PAGE_SIZE - 1);
2319 size = min_t(unsigned int, PAGE_SIZE - page_offset, len);
2320
2321 memcpy(pages[nr] + page_offset, buf, size);
2322
2323 len -= size;
2324 buf += size;
2325 offset += size;
2326 } while (len);
2327
2328 handle->offset = offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01002329
Peter Zijlstra53020fe2009-05-13 21:26:19 +02002330 /*
2331 * Check we didn't copy past our reservation window, taking the
2332 * possible unsigned int wrap into account.
2333 */
Peter Zijlstra8e3747c2009-06-02 16:16:02 +02002334 WARN_ON_ONCE(((long)(handle->head - handle->offset)) < 0);
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002335}
2336
Peter Zijlstra5c148192009-03-25 12:30:23 +01002337#define perf_output_put(handle, x) \
2338 perf_output_copy((handle), &(x), sizeof(x))
2339
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002340static void perf_output_end(struct perf_output_handle *handle)
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002341{
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002342 struct perf_counter *counter = handle->counter;
2343 struct perf_mmap_data *data = handle->data;
2344
Peter Zijlstra0d486962009-06-02 19:22:16 +02002345 int wakeup_events = counter->attr.wakeup_events;
Peter Zijlstrac4578102009-04-02 11:12:01 +02002346
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002347 if (handle->overflow && wakeup_events) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002348 int events = atomic_inc_return(&data->events);
Peter Zijlstrac4578102009-04-02 11:12:01 +02002349 if (events >= wakeup_events) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002350 atomic_sub(wakeup_events, &data->events);
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002351 atomic_set(&data->wakeup, 1);
Peter Zijlstrac4578102009-04-02 11:12:01 +02002352 }
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002353 }
2354
2355 perf_output_unlock(handle);
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002356 rcu_read_unlock();
2357}
2358
Peter Zijlstra709e50c2009-06-02 14:13:15 +02002359static u32 perf_counter_pid(struct perf_counter *counter, struct task_struct *p)
2360{
2361 /*
2362 * only top level counters have the pid namespace they were created in
2363 */
2364 if (counter->parent)
2365 counter = counter->parent;
2366
2367 return task_tgid_nr_ns(p, counter->ns);
2368}
2369
2370static u32 perf_counter_tid(struct perf_counter *counter, struct task_struct *p)
2371{
2372 /*
2373 * only top level counters have the pid namespace they were created in
2374 */
2375 if (counter->parent)
2376 counter = counter->parent;
2377
2378 return task_pid_nr_ns(p, counter->ns);
2379}
2380
Peter Zijlstradf1a1322009-06-10 21:02:22 +02002381static void perf_counter_output(struct perf_counter *counter, int nmi,
2382 struct perf_sample_data *data)
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002383{
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002384 int ret;
Peter Zijlstra0d486962009-06-02 19:22:16 +02002385 u64 sample_type = counter->attr.sample_type;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002386 struct perf_output_handle handle;
2387 struct perf_event_header header;
2388 u64 ip;
Peter Zijlstra5c148192009-03-25 12:30:23 +01002389 struct {
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002390 u32 pid, tid;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002391 } tid_entry;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002392 struct {
Peter Zijlstra8e5799b2009-06-02 15:08:15 +02002393 u64 id;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002394 u64 counter;
2395 } group_entry;
Peter Zijlstra394ee072009-03-30 19:07:14 +02002396 struct perf_callchain_entry *callchain = NULL;
2397 int callchain_size = 0;
Peter Zijlstra339f7c92009-04-06 11:45:06 +02002398 u64 time;
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002399 struct {
2400 u32 cpu, reserved;
2401 } cpu_entry;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002402
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02002403 header.type = 0;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002404 header.size = sizeof(header);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002405
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02002406 header.misc = PERF_EVENT_MISC_OVERFLOW;
Peter Zijlstradf1a1322009-06-10 21:02:22 +02002407 header.misc |= perf_misc_flags(data->regs);
Peter Zijlstra6fab0192009-04-08 15:01:26 +02002408
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002409 if (sample_type & PERF_SAMPLE_IP) {
Peter Zijlstradf1a1322009-06-10 21:02:22 +02002410 ip = perf_instruction_pointer(data->regs);
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002411 header.type |= PERF_SAMPLE_IP;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002412 header.size += sizeof(ip);
2413 }
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002414
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002415 if (sample_type & PERF_SAMPLE_TID) {
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002416 /* namespace issues */
Peter Zijlstra709e50c2009-06-02 14:13:15 +02002417 tid_entry.pid = perf_counter_pid(counter, current);
2418 tid_entry.tid = perf_counter_tid(counter, current);
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002419
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002420 header.type |= PERF_SAMPLE_TID;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002421 header.size += sizeof(tid_entry);
2422 }
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002423
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002424 if (sample_type & PERF_SAMPLE_TIME) {
Peter Zijlstra4d855452009-04-08 15:01:32 +02002425 /*
2426 * Maybe do better on x86 and provide cpu_clock_nmi()
2427 */
2428 time = sched_clock();
2429
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002430 header.type |= PERF_SAMPLE_TIME;
Peter Zijlstra4d855452009-04-08 15:01:32 +02002431 header.size += sizeof(u64);
2432 }
2433
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002434 if (sample_type & PERF_SAMPLE_ADDR) {
2435 header.type |= PERF_SAMPLE_ADDR;
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002436 header.size += sizeof(u64);
2437 }
2438
Peter Zijlstraac4bcf82009-06-05 14:44:52 +02002439 if (sample_type & PERF_SAMPLE_ID) {
2440 header.type |= PERF_SAMPLE_ID;
Peter Zijlstraa85f61a2009-05-08 18:52:23 +02002441 header.size += sizeof(u64);
2442 }
2443
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002444 if (sample_type & PERF_SAMPLE_CPU) {
2445 header.type |= PERF_SAMPLE_CPU;
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002446 header.size += sizeof(cpu_entry);
2447
2448 cpu_entry.cpu = raw_smp_processor_id();
2449 }
2450
Peter Zijlstra689802b2009-06-05 15:05:43 +02002451 if (sample_type & PERF_SAMPLE_PERIOD) {
2452 header.type |= PERF_SAMPLE_PERIOD;
2453 header.size += sizeof(u64);
2454 }
2455
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002456 if (sample_type & PERF_SAMPLE_GROUP) {
2457 header.type |= PERF_SAMPLE_GROUP;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002458 header.size += sizeof(u64) +
2459 counter->nr_siblings * sizeof(group_entry);
2460 }
2461
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002462 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
Peter Zijlstradf1a1322009-06-10 21:02:22 +02002463 callchain = perf_callchain(data->regs);
Peter Zijlstra394ee072009-03-30 19:07:14 +02002464
2465 if (callchain) {
Peter Zijlstra9c03d882009-04-06 11:45:00 +02002466 callchain_size = (1 + callchain->nr) * sizeof(u64);
Peter Zijlstra394ee072009-03-30 19:07:14 +02002467
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002468 header.type |= PERF_SAMPLE_CALLCHAIN;
Peter Zijlstra394ee072009-03-30 19:07:14 +02002469 header.size += callchain_size;
2470 }
2471 }
2472
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002473 ret = perf_output_begin(&handle, counter, header.size, nmi, 1);
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002474 if (ret)
2475 return;
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002476
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002477 perf_output_put(&handle, header);
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002478
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002479 if (sample_type & PERF_SAMPLE_IP)
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002480 perf_output_put(&handle, ip);
2481
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002482 if (sample_type & PERF_SAMPLE_TID)
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002483 perf_output_put(&handle, tid_entry);
2484
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002485 if (sample_type & PERF_SAMPLE_TIME)
Peter Zijlstra4d855452009-04-08 15:01:32 +02002486 perf_output_put(&handle, time);
2487
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002488 if (sample_type & PERF_SAMPLE_ADDR)
Peter Zijlstradf1a1322009-06-10 21:02:22 +02002489 perf_output_put(&handle, data->addr);
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002490
Peter Zijlstraac4bcf82009-06-05 14:44:52 +02002491 if (sample_type & PERF_SAMPLE_ID)
2492 perf_output_put(&handle, counter->id);
Peter Zijlstraa85f61a2009-05-08 18:52:23 +02002493
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002494 if (sample_type & PERF_SAMPLE_CPU)
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002495 perf_output_put(&handle, cpu_entry);
2496
Peter Zijlstra689802b2009-06-05 15:05:43 +02002497 if (sample_type & PERF_SAMPLE_PERIOD)
Peter Zijlstra9e350de2009-06-10 21:34:59 +02002498 perf_output_put(&handle, data->period);
Peter Zijlstra689802b2009-06-05 15:05:43 +02002499
Peter Zijlstra2023b352009-05-05 17:50:26 +02002500 /*
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002501 * XXX PERF_SAMPLE_GROUP vs inherited counters seems difficult.
Peter Zijlstra2023b352009-05-05 17:50:26 +02002502 */
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002503 if (sample_type & PERF_SAMPLE_GROUP) {
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002504 struct perf_counter *leader, *sub;
2505 u64 nr = counter->nr_siblings;
2506
2507 perf_output_put(&handle, nr);
2508
2509 leader = counter->group_leader;
2510 list_for_each_entry(sub, &leader->sibling_list, list_entry) {
2511 if (sub != counter)
Robert Richter4aeb0b42009-04-29 12:47:03 +02002512 sub->pmu->read(sub);
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002513
Peter Zijlstra8e5799b2009-06-02 15:08:15 +02002514 group_entry.id = sub->id;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002515 group_entry.counter = atomic64_read(&sub->count);
2516
2517 perf_output_put(&handle, group_entry);
2518 }
2519 }
2520
Peter Zijlstra394ee072009-03-30 19:07:14 +02002521 if (callchain)
2522 perf_output_copy(&handle, callchain, callchain_size);
2523
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002524 perf_output_end(&handle);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002525}
2526
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002527/*
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002528 * fork tracking
2529 */
2530
2531struct perf_fork_event {
2532 struct task_struct *task;
2533
2534 struct {
2535 struct perf_event_header header;
2536
2537 u32 pid;
2538 u32 ppid;
2539 } event;
2540};
2541
2542static void perf_counter_fork_output(struct perf_counter *counter,
2543 struct perf_fork_event *fork_event)
2544{
2545 struct perf_output_handle handle;
2546 int size = fork_event->event.header.size;
2547 struct task_struct *task = fork_event->task;
2548 int ret = perf_output_begin(&handle, counter, size, 0, 0);
2549
2550 if (ret)
2551 return;
2552
2553 fork_event->event.pid = perf_counter_pid(counter, task);
2554 fork_event->event.ppid = perf_counter_pid(counter, task->real_parent);
2555
2556 perf_output_put(&handle, fork_event->event);
2557 perf_output_end(&handle);
2558}
2559
2560static int perf_counter_fork_match(struct perf_counter *counter)
2561{
Peter Zijlstrad99e9442009-06-04 17:08:58 +02002562 if (counter->attr.comm || counter->attr.mmap)
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002563 return 1;
2564
2565 return 0;
2566}
2567
2568static void perf_counter_fork_ctx(struct perf_counter_context *ctx,
2569 struct perf_fork_event *fork_event)
2570{
2571 struct perf_counter *counter;
2572
2573 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2574 return;
2575
2576 rcu_read_lock();
2577 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2578 if (perf_counter_fork_match(counter))
2579 perf_counter_fork_output(counter, fork_event);
2580 }
2581 rcu_read_unlock();
2582}
2583
2584static void perf_counter_fork_event(struct perf_fork_event *fork_event)
2585{
2586 struct perf_cpu_context *cpuctx;
2587 struct perf_counter_context *ctx;
2588
2589 cpuctx = &get_cpu_var(perf_cpu_context);
2590 perf_counter_fork_ctx(&cpuctx->ctx, fork_event);
2591 put_cpu_var(perf_cpu_context);
2592
2593 rcu_read_lock();
2594 /*
2595 * doesn't really matter which of the child contexts the
2596 * events ends up in.
2597 */
2598 ctx = rcu_dereference(current->perf_counter_ctxp);
2599 if (ctx)
2600 perf_counter_fork_ctx(ctx, fork_event);
2601 rcu_read_unlock();
2602}
2603
2604void perf_counter_fork(struct task_struct *task)
2605{
2606 struct perf_fork_event fork_event;
2607
2608 if (!atomic_read(&nr_comm_counters) &&
Peter Zijlstrad99e9442009-06-04 17:08:58 +02002609 !atomic_read(&nr_mmap_counters))
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002610 return;
2611
2612 fork_event = (struct perf_fork_event){
2613 .task = task,
2614 .event = {
2615 .header = {
2616 .type = PERF_EVENT_FORK,
2617 .size = sizeof(fork_event.event),
2618 },
2619 },
2620 };
2621
2622 perf_counter_fork_event(&fork_event);
2623}
2624
2625/*
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002626 * comm tracking
2627 */
2628
2629struct perf_comm_event {
Ingo Molnar22a4f652009-06-01 10:13:37 +02002630 struct task_struct *task;
2631 char *comm;
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002632 int comm_size;
2633
2634 struct {
2635 struct perf_event_header header;
2636
2637 u32 pid;
2638 u32 tid;
2639 } event;
2640};
2641
2642static void perf_counter_comm_output(struct perf_counter *counter,
2643 struct perf_comm_event *comm_event)
2644{
2645 struct perf_output_handle handle;
2646 int size = comm_event->event.header.size;
2647 int ret = perf_output_begin(&handle, counter, size, 0, 0);
2648
2649 if (ret)
2650 return;
2651
Peter Zijlstra709e50c2009-06-02 14:13:15 +02002652 comm_event->event.pid = perf_counter_pid(counter, comm_event->task);
2653 comm_event->event.tid = perf_counter_tid(counter, comm_event->task);
2654
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002655 perf_output_put(&handle, comm_event->event);
2656 perf_output_copy(&handle, comm_event->comm,
2657 comm_event->comm_size);
2658 perf_output_end(&handle);
2659}
2660
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002661static int perf_counter_comm_match(struct perf_counter *counter)
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002662{
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002663 if (counter->attr.comm)
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002664 return 1;
2665
2666 return 0;
2667}
2668
2669static void perf_counter_comm_ctx(struct perf_counter_context *ctx,
2670 struct perf_comm_event *comm_event)
2671{
2672 struct perf_counter *counter;
2673
2674 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2675 return;
2676
2677 rcu_read_lock();
2678 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002679 if (perf_counter_comm_match(counter))
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002680 perf_counter_comm_output(counter, comm_event);
2681 }
2682 rcu_read_unlock();
2683}
2684
2685static void perf_counter_comm_event(struct perf_comm_event *comm_event)
2686{
2687 struct perf_cpu_context *cpuctx;
Peter Zijlstra665c2142009-05-29 14:51:57 +02002688 struct perf_counter_context *ctx;
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002689 unsigned int size;
2690 char *comm = comm_event->task->comm;
2691
Ingo Molnar888fcee2009-04-09 09:48:22 +02002692 size = ALIGN(strlen(comm)+1, sizeof(u64));
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002693
2694 comm_event->comm = comm;
2695 comm_event->comm_size = size;
2696
2697 comm_event->event.header.size = sizeof(comm_event->event) + size;
2698
2699 cpuctx = &get_cpu_var(perf_cpu_context);
2700 perf_counter_comm_ctx(&cpuctx->ctx, comm_event);
2701 put_cpu_var(perf_cpu_context);
Peter Zijlstra665c2142009-05-29 14:51:57 +02002702
2703 rcu_read_lock();
2704 /*
2705 * doesn't really matter which of the child contexts the
2706 * events ends up in.
2707 */
2708 ctx = rcu_dereference(current->perf_counter_ctxp);
2709 if (ctx)
2710 perf_counter_comm_ctx(ctx, comm_event);
2711 rcu_read_unlock();
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002712}
2713
2714void perf_counter_comm(struct task_struct *task)
2715{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002716 struct perf_comm_event comm_event;
2717
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002718 if (!atomic_read(&nr_comm_counters))
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002719 return;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10002720
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002721 comm_event = (struct perf_comm_event){
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002722 .task = task,
2723 .event = {
2724 .header = { .type = PERF_EVENT_COMM, },
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002725 },
2726 };
2727
2728 perf_counter_comm_event(&comm_event);
2729}
2730
2731/*
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002732 * mmap tracking
2733 */
2734
2735struct perf_mmap_event {
Peter Zijlstra089dd792009-06-05 14:04:55 +02002736 struct vm_area_struct *vma;
2737
2738 const char *file_name;
2739 int file_size;
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002740
2741 struct {
2742 struct perf_event_header header;
2743
2744 u32 pid;
2745 u32 tid;
2746 u64 start;
2747 u64 len;
2748 u64 pgoff;
2749 } event;
2750};
2751
2752static void perf_counter_mmap_output(struct perf_counter *counter,
2753 struct perf_mmap_event *mmap_event)
2754{
2755 struct perf_output_handle handle;
2756 int size = mmap_event->event.header.size;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002757 int ret = perf_output_begin(&handle, counter, size, 0, 0);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002758
2759 if (ret)
2760 return;
2761
Peter Zijlstra709e50c2009-06-02 14:13:15 +02002762 mmap_event->event.pid = perf_counter_pid(counter, current);
2763 mmap_event->event.tid = perf_counter_tid(counter, current);
2764
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002765 perf_output_put(&handle, mmap_event->event);
2766 perf_output_copy(&handle, mmap_event->file_name,
2767 mmap_event->file_size);
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002768 perf_output_end(&handle);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002769}
2770
2771static int perf_counter_mmap_match(struct perf_counter *counter,
2772 struct perf_mmap_event *mmap_event)
2773{
Peter Zijlstrad99e9442009-06-04 17:08:58 +02002774 if (counter->attr.mmap)
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002775 return 1;
2776
2777 return 0;
2778}
2779
2780static void perf_counter_mmap_ctx(struct perf_counter_context *ctx,
2781 struct perf_mmap_event *mmap_event)
2782{
2783 struct perf_counter *counter;
2784
2785 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2786 return;
2787
2788 rcu_read_lock();
2789 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2790 if (perf_counter_mmap_match(counter, mmap_event))
2791 perf_counter_mmap_output(counter, mmap_event);
2792 }
2793 rcu_read_unlock();
2794}
2795
2796static void perf_counter_mmap_event(struct perf_mmap_event *mmap_event)
2797{
2798 struct perf_cpu_context *cpuctx;
Peter Zijlstra665c2142009-05-29 14:51:57 +02002799 struct perf_counter_context *ctx;
Peter Zijlstra089dd792009-06-05 14:04:55 +02002800 struct vm_area_struct *vma = mmap_event->vma;
2801 struct file *file = vma->vm_file;
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002802 unsigned int size;
2803 char tmp[16];
2804 char *buf = NULL;
Peter Zijlstra089dd792009-06-05 14:04:55 +02002805 const char *name;
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002806
2807 if (file) {
2808 buf = kzalloc(PATH_MAX, GFP_KERNEL);
2809 if (!buf) {
2810 name = strncpy(tmp, "//enomem", sizeof(tmp));
2811 goto got_name;
2812 }
Peter Zijlstrad3d21c42009-04-09 10:53:46 +02002813 name = d_path(&file->f_path, buf, PATH_MAX);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002814 if (IS_ERR(name)) {
2815 name = strncpy(tmp, "//toolong", sizeof(tmp));
2816 goto got_name;
2817 }
2818 } else {
Peter Zijlstra089dd792009-06-05 14:04:55 +02002819 name = arch_vma_name(mmap_event->vma);
2820 if (name)
2821 goto got_name;
2822
2823 if (!vma->vm_mm) {
2824 name = strncpy(tmp, "[vdso]", sizeof(tmp));
2825 goto got_name;
2826 }
2827
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002828 name = strncpy(tmp, "//anon", sizeof(tmp));
2829 goto got_name;
2830 }
2831
2832got_name:
Ingo Molnar888fcee2009-04-09 09:48:22 +02002833 size = ALIGN(strlen(name)+1, sizeof(u64));
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002834
2835 mmap_event->file_name = name;
2836 mmap_event->file_size = size;
2837
2838 mmap_event->event.header.size = sizeof(mmap_event->event) + size;
2839
2840 cpuctx = &get_cpu_var(perf_cpu_context);
2841 perf_counter_mmap_ctx(&cpuctx->ctx, mmap_event);
2842 put_cpu_var(perf_cpu_context);
2843
Peter Zijlstra665c2142009-05-29 14:51:57 +02002844 rcu_read_lock();
2845 /*
2846 * doesn't really matter which of the child contexts the
2847 * events ends up in.
2848 */
2849 ctx = rcu_dereference(current->perf_counter_ctxp);
2850 if (ctx)
2851 perf_counter_mmap_ctx(ctx, mmap_event);
2852 rcu_read_unlock();
2853
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002854 kfree(buf);
2855}
2856
Peter Zijlstra089dd792009-06-05 14:04:55 +02002857void __perf_counter_mmap(struct vm_area_struct *vma)
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002858{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002859 struct perf_mmap_event mmap_event;
2860
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002861 if (!atomic_read(&nr_mmap_counters))
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002862 return;
2863
2864 mmap_event = (struct perf_mmap_event){
Peter Zijlstra089dd792009-06-05 14:04:55 +02002865 .vma = vma,
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002866 .event = {
2867 .header = { .type = PERF_EVENT_MMAP, },
Peter Zijlstra089dd792009-06-05 14:04:55 +02002868 .start = vma->vm_start,
2869 .len = vma->vm_end - vma->vm_start,
2870 .pgoff = vma->vm_pgoff,
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002871 },
2872 };
2873
2874 perf_counter_mmap_event(&mmap_event);
2875}
2876
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002877/*
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002878 * Log sample_period changes so that analyzing tools can re-normalize the
Peter Zijlstrae220d2d2009-05-23 18:28:55 +02002879 * event flow.
Peter Zijlstra26b119b2009-05-20 12:21:20 +02002880 */
2881
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02002882struct freq_event {
2883 struct perf_event_header header;
2884 u64 time;
2885 u64 id;
2886 u64 period;
2887};
2888
Peter Zijlstra26b119b2009-05-20 12:21:20 +02002889static void perf_log_period(struct perf_counter *counter, u64 period)
2890{
2891 struct perf_output_handle handle;
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02002892 struct freq_event event;
Peter Zijlstra26b119b2009-05-20 12:21:20 +02002893 int ret;
2894
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02002895 if (counter->hw.sample_period == period)
2896 return;
2897
2898 if (counter->attr.sample_type & PERF_SAMPLE_PERIOD)
2899 return;
2900
2901 event = (struct freq_event) {
Peter Zijlstra26b119b2009-05-20 12:21:20 +02002902 .header = {
2903 .type = PERF_EVENT_PERIOD,
2904 .misc = 0,
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02002905 .size = sizeof(event),
Peter Zijlstra26b119b2009-05-20 12:21:20 +02002906 },
2907 .time = sched_clock(),
Peter Zijlstra689802b2009-06-05 15:05:43 +02002908 .id = counter->id,
Peter Zijlstra26b119b2009-05-20 12:21:20 +02002909 .period = period,
2910 };
2911
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02002912 ret = perf_output_begin(&handle, counter, sizeof(event), 1, 0);
Peter Zijlstra26b119b2009-05-20 12:21:20 +02002913 if (ret)
2914 return;
2915
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02002916 perf_output_put(&handle, event);
Peter Zijlstra26b119b2009-05-20 12:21:20 +02002917 perf_output_end(&handle);
2918}
2919
2920/*
Peter Zijlstraa78ac322009-05-25 17:39:05 +02002921 * IRQ throttle logging
2922 */
2923
2924static void perf_log_throttle(struct perf_counter *counter, int enable)
2925{
2926 struct perf_output_handle handle;
2927 int ret;
2928
2929 struct {
2930 struct perf_event_header header;
2931 u64 time;
2932 } throttle_event = {
2933 .header = {
2934 .type = PERF_EVENT_THROTTLE + 1,
2935 .misc = 0,
2936 .size = sizeof(throttle_event),
2937 },
2938 .time = sched_clock(),
2939 };
2940
Ingo Molnar0127c3e2009-05-25 22:03:26 +02002941 ret = perf_output_begin(&handle, counter, sizeof(throttle_event), 1, 0);
Peter Zijlstraa78ac322009-05-25 17:39:05 +02002942 if (ret)
2943 return;
2944
2945 perf_output_put(&handle, throttle_event);
2946 perf_output_end(&handle);
2947}
2948
2949/*
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002950 * Generic counter overflow handling.
2951 */
2952
Peter Zijlstradf1a1322009-06-10 21:02:22 +02002953int perf_counter_overflow(struct perf_counter *counter, int nmi,
2954 struct perf_sample_data *data)
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002955{
Peter Zijlstra79f14642009-04-06 11:45:07 +02002956 int events = atomic_read(&counter->event_limit);
Peter Zijlstraa78ac322009-05-25 17:39:05 +02002957 int throttle = counter->pmu->unthrottle != NULL;
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02002958 struct hw_perf_counter *hwc = &counter->hw;
Peter Zijlstra79f14642009-04-06 11:45:07 +02002959 int ret = 0;
2960
Peter Zijlstraa78ac322009-05-25 17:39:05 +02002961 if (!throttle) {
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02002962 hwc->interrupts++;
Ingo Molnar128f0482009-06-03 22:19:36 +02002963 } else {
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02002964 if (hwc->interrupts != MAX_INTERRUPTS) {
2965 hwc->interrupts++;
2966 if (HZ * hwc->interrupts > (u64)sysctl_perf_counter_limit) {
2967 hwc->interrupts = MAX_INTERRUPTS;
Ingo Molnar128f0482009-06-03 22:19:36 +02002968 perf_log_throttle(counter, 0);
2969 ret = 1;
2970 }
2971 } else {
2972 /*
2973 * Keep re-disabling counters even though on the previous
2974 * pass we disabled it - just in case we raced with a
2975 * sched-in and the counter got enabled again:
2976 */
Peter Zijlstraa78ac322009-05-25 17:39:05 +02002977 ret = 1;
2978 }
2979 }
Peter Zijlstra60db5e02009-05-15 15:19:28 +02002980
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02002981 if (counter->attr.freq) {
2982 u64 now = sched_clock();
2983 s64 delta = now - hwc->freq_stamp;
2984
2985 hwc->freq_stamp = now;
2986
2987 if (delta > 0 && delta < TICK_NSEC)
2988 perf_adjust_period(counter, NSEC_PER_SEC / (int)delta);
2989 }
2990
Peter Zijlstra2023b352009-05-05 17:50:26 +02002991 /*
2992 * XXX event_limit might not quite work as expected on inherited
2993 * counters
2994 */
2995
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002996 counter->pending_kill = POLL_IN;
Peter Zijlstra79f14642009-04-06 11:45:07 +02002997 if (events && atomic_dec_and_test(&counter->event_limit)) {
2998 ret = 1;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002999 counter->pending_kill = POLL_HUP;
Peter Zijlstra79f14642009-04-06 11:45:07 +02003000 if (nmi) {
3001 counter->pending_disable = 1;
3002 perf_pending_queue(&counter->pending,
3003 perf_pending_counter);
3004 } else
3005 perf_counter_disable(counter);
3006 }
3007
Peter Zijlstradf1a1322009-06-10 21:02:22 +02003008 perf_counter_output(counter, nmi, data);
Peter Zijlstra79f14642009-04-06 11:45:07 +02003009 return ret;
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02003010}
3011
3012/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003013 * Generic software counter infrastructure
3014 */
3015
3016static void perf_swcounter_update(struct perf_counter *counter)
3017{
3018 struct hw_perf_counter *hwc = &counter->hw;
3019 u64 prev, now;
3020 s64 delta;
3021
3022again:
3023 prev = atomic64_read(&hwc->prev_count);
3024 now = atomic64_read(&hwc->count);
3025 if (atomic64_cmpxchg(&hwc->prev_count, prev, now) != prev)
3026 goto again;
3027
3028 delta = now - prev;
3029
3030 atomic64_add(delta, &counter->count);
3031 atomic64_sub(delta, &hwc->period_left);
3032}
3033
3034static void perf_swcounter_set_period(struct perf_counter *counter)
3035{
3036 struct hw_perf_counter *hwc = &counter->hw;
3037 s64 left = atomic64_read(&hwc->period_left);
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003038 s64 period = hwc->sample_period;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003039
3040 if (unlikely(left <= -period)) {
3041 left = period;
3042 atomic64_set(&hwc->period_left, left);
Peter Zijlstra9e350de2009-06-10 21:34:59 +02003043 hwc->last_period = period;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003044 }
3045
3046 if (unlikely(left <= 0)) {
3047 left += period;
3048 atomic64_add(period, &hwc->period_left);
Peter Zijlstra9e350de2009-06-10 21:34:59 +02003049 hwc->last_period = period;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003050 }
3051
3052 atomic64_set(&hwc->prev_count, -left);
3053 atomic64_set(&hwc->count, -left);
3054}
3055
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003056static enum hrtimer_restart perf_swcounter_hrtimer(struct hrtimer *hrtimer)
3057{
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02003058 enum hrtimer_restart ret = HRTIMER_RESTART;
Peter Zijlstradf1a1322009-06-10 21:02:22 +02003059 struct perf_sample_data data;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003060 struct perf_counter *counter;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003061 u64 period;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003062
3063 counter = container_of(hrtimer, struct perf_counter, hw.hrtimer);
Robert Richter4aeb0b42009-04-29 12:47:03 +02003064 counter->pmu->read(counter);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003065
Peter Zijlstradf1a1322009-06-10 21:02:22 +02003066 data.addr = 0;
3067 data.regs = get_irq_regs();
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003068 /*
3069 * In case we exclude kernel IPs or are somehow not in interrupt
3070 * context, provide the next best thing, the user IP.
3071 */
Peter Zijlstradf1a1322009-06-10 21:02:22 +02003072 if ((counter->attr.exclude_kernel || !data.regs) &&
Peter Zijlstra0d486962009-06-02 19:22:16 +02003073 !counter->attr.exclude_user)
Peter Zijlstradf1a1322009-06-10 21:02:22 +02003074 data.regs = task_pt_regs(current);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003075
Peter Zijlstradf1a1322009-06-10 21:02:22 +02003076 if (data.regs) {
3077 if (perf_counter_overflow(counter, 0, &data))
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02003078 ret = HRTIMER_NORESTART;
3079 }
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003080
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003081 period = max_t(u64, 10000, counter->hw.sample_period);
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003082 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003083
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02003084 return ret;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003085}
3086
3087static void perf_swcounter_overflow(struct perf_counter *counter,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003088 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003089{
Peter Zijlstradf1a1322009-06-10 21:02:22 +02003090 struct perf_sample_data data = {
Peter Zijlstra9e350de2009-06-10 21:34:59 +02003091 .regs = regs,
3092 .addr = addr,
3093 .period = counter->hw.last_period,
Peter Zijlstradf1a1322009-06-10 21:02:22 +02003094 };
3095
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003096 perf_swcounter_update(counter);
3097 perf_swcounter_set_period(counter);
Peter Zijlstradf1a1322009-06-10 21:02:22 +02003098 if (perf_counter_overflow(counter, nmi, &data))
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02003099 /* soft-disable the counter */
3100 ;
3101
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003102}
3103
Paul Mackerras880ca152009-06-01 17:49:14 +10003104static int perf_swcounter_is_counting(struct perf_counter *counter)
3105{
3106 struct perf_counter_context *ctx;
3107 unsigned long flags;
3108 int count;
3109
3110 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
3111 return 1;
3112
3113 if (counter->state != PERF_COUNTER_STATE_INACTIVE)
3114 return 0;
3115
3116 /*
3117 * If the counter is inactive, it could be just because
3118 * its task is scheduled out, or because it's in a group
3119 * which could not go on the PMU. We want to count in
3120 * the first case but not the second. If the context is
3121 * currently active then an inactive software counter must
3122 * be the second case. If it's not currently active then
3123 * we need to know whether the counter was active when the
3124 * context was last active, which we can determine by
3125 * comparing counter->tstamp_stopped with ctx->time.
3126 *
3127 * We are within an RCU read-side critical section,
3128 * which protects the existence of *ctx.
3129 */
3130 ctx = counter->ctx;
3131 spin_lock_irqsave(&ctx->lock, flags);
3132 count = 1;
3133 /* Re-check state now we have the lock */
3134 if (counter->state < PERF_COUNTER_STATE_INACTIVE ||
3135 counter->ctx->is_active ||
3136 counter->tstamp_stopped < ctx->time)
3137 count = 0;
3138 spin_unlock_irqrestore(&ctx->lock, flags);
3139 return count;
3140}
3141
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003142static int perf_swcounter_match(struct perf_counter *counter,
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003143 enum perf_event_types type,
3144 u32 event, struct pt_regs *regs)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003145{
Paul Mackerras880ca152009-06-01 17:49:14 +10003146 if (!perf_swcounter_is_counting(counter))
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003147 return 0;
3148
Ingo Molnara21ca2c2009-06-06 09:58:57 +02003149 if (counter->attr.type != type)
3150 return 0;
3151 if (counter->attr.config != event)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003152 return 0;
3153
Paul Mackerras3f731ca2009-06-01 17:52:30 +10003154 if (regs) {
Peter Zijlstra0d486962009-06-02 19:22:16 +02003155 if (counter->attr.exclude_user && user_mode(regs))
Paul Mackerras3f731ca2009-06-01 17:52:30 +10003156 return 0;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003157
Peter Zijlstra0d486962009-06-02 19:22:16 +02003158 if (counter->attr.exclude_kernel && !user_mode(regs))
Paul Mackerras3f731ca2009-06-01 17:52:30 +10003159 return 0;
3160 }
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003161
3162 return 1;
3163}
3164
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003165static void perf_swcounter_add(struct perf_counter *counter, u64 nr,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003166 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003167{
3168 int neg = atomic64_add_negative(nr, &counter->hw.count);
Ingo Molnar22a4f652009-06-01 10:13:37 +02003169
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003170 if (counter->hw.sample_period && !neg && regs)
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003171 perf_swcounter_overflow(counter, nmi, regs, addr);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003172}
3173
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003174static void perf_swcounter_ctx_event(struct perf_counter_context *ctx,
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003175 enum perf_event_types type, u32 event,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003176 u64 nr, int nmi, struct pt_regs *regs,
3177 u64 addr)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003178{
3179 struct perf_counter *counter;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003180
Peter Zijlstra01ef09d2009-03-19 20:26:11 +01003181 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003182 return;
3183
Peter Zijlstra592903c2009-03-13 12:21:36 +01003184 rcu_read_lock();
3185 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003186 if (perf_swcounter_match(counter, type, event, regs))
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003187 perf_swcounter_add(counter, nr, nmi, regs, addr);
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003188 }
Peter Zijlstra592903c2009-03-13 12:21:36 +01003189 rcu_read_unlock();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003190}
3191
Peter Zijlstra96f6d442009-03-23 18:22:07 +01003192static int *perf_swcounter_recursion_context(struct perf_cpu_context *cpuctx)
3193{
3194 if (in_nmi())
3195 return &cpuctx->recursion[3];
3196
3197 if (in_irq())
3198 return &cpuctx->recursion[2];
3199
3200 if (in_softirq())
3201 return &cpuctx->recursion[1];
3202
3203 return &cpuctx->recursion[0];
3204}
3205
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003206static void __perf_swcounter_event(enum perf_event_types type, u32 event,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003207 u64 nr, int nmi, struct pt_regs *regs,
3208 u64 addr)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003209{
3210 struct perf_cpu_context *cpuctx = &get_cpu_var(perf_cpu_context);
Peter Zijlstra96f6d442009-03-23 18:22:07 +01003211 int *recursion = perf_swcounter_recursion_context(cpuctx);
Peter Zijlstra665c2142009-05-29 14:51:57 +02003212 struct perf_counter_context *ctx;
Peter Zijlstra96f6d442009-03-23 18:22:07 +01003213
3214 if (*recursion)
3215 goto out;
3216
3217 (*recursion)++;
3218 barrier();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003219
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003220 perf_swcounter_ctx_event(&cpuctx->ctx, type, event,
3221 nr, nmi, regs, addr);
Peter Zijlstra665c2142009-05-29 14:51:57 +02003222 rcu_read_lock();
3223 /*
3224 * doesn't really matter which of the child contexts the
3225 * events ends up in.
3226 */
3227 ctx = rcu_dereference(current->perf_counter_ctxp);
3228 if (ctx)
3229 perf_swcounter_ctx_event(ctx, type, event, nr, nmi, regs, addr);
3230 rcu_read_unlock();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003231
Peter Zijlstra96f6d442009-03-23 18:22:07 +01003232 barrier();
3233 (*recursion)--;
3234
3235out:
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003236 put_cpu_var(perf_cpu_context);
3237}
3238
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003239void
3240perf_swcounter_event(u32 event, u64 nr, int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003241{
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003242 __perf_swcounter_event(PERF_TYPE_SOFTWARE, event, nr, nmi, regs, addr);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003243}
3244
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003245static void perf_swcounter_read(struct perf_counter *counter)
3246{
3247 perf_swcounter_update(counter);
3248}
3249
3250static int perf_swcounter_enable(struct perf_counter *counter)
3251{
3252 perf_swcounter_set_period(counter);
3253 return 0;
3254}
3255
3256static void perf_swcounter_disable(struct perf_counter *counter)
3257{
3258 perf_swcounter_update(counter);
3259}
3260
Robert Richter4aeb0b42009-04-29 12:47:03 +02003261static const struct pmu perf_ops_generic = {
Peter Zijlstraac17dc82009-03-13 12:21:34 +01003262 .enable = perf_swcounter_enable,
3263 .disable = perf_swcounter_disable,
3264 .read = perf_swcounter_read,
3265};
3266
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003267/*
3268 * Software counter: cpu wall time clock
3269 */
3270
Paul Mackerras9abf8a02009-01-09 16:26:43 +11003271static void cpu_clock_perf_counter_update(struct perf_counter *counter)
3272{
3273 int cpu = raw_smp_processor_id();
3274 s64 prev;
3275 u64 now;
3276
3277 now = cpu_clock(cpu);
3278 prev = atomic64_read(&counter->hw.prev_count);
3279 atomic64_set(&counter->hw.prev_count, now);
3280 atomic64_add(now - prev, &counter->count);
3281}
3282
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003283static int cpu_clock_perf_counter_enable(struct perf_counter *counter)
3284{
3285 struct hw_perf_counter *hwc = &counter->hw;
3286 int cpu = raw_smp_processor_id();
3287
3288 atomic64_set(&hwc->prev_count, cpu_clock(cpu));
Peter Zijlstra039fc912009-03-13 16:43:47 +01003289 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
3290 hwc->hrtimer.function = perf_swcounter_hrtimer;
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003291 if (hwc->sample_period) {
3292 u64 period = max_t(u64, 10000, hwc->sample_period);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003293 __hrtimer_start_range_ns(&hwc->hrtimer,
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003294 ns_to_ktime(period), 0,
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003295 HRTIMER_MODE_REL, 0);
3296 }
3297
3298 return 0;
3299}
3300
Ingo Molnar5c92d122008-12-11 13:21:10 +01003301static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
3302{
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003303 if (counter->hw.sample_period)
Peter Zijlstrab986d7e2009-05-20 12:21:21 +02003304 hrtimer_cancel(&counter->hw.hrtimer);
Paul Mackerras9abf8a02009-01-09 16:26:43 +11003305 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01003306}
3307
3308static void cpu_clock_perf_counter_read(struct perf_counter *counter)
3309{
Paul Mackerras9abf8a02009-01-09 16:26:43 +11003310 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01003311}
3312
Robert Richter4aeb0b42009-04-29 12:47:03 +02003313static const struct pmu perf_ops_cpu_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +01003314 .enable = cpu_clock_perf_counter_enable,
3315 .disable = cpu_clock_perf_counter_disable,
3316 .read = cpu_clock_perf_counter_read,
Ingo Molnar5c92d122008-12-11 13:21:10 +01003317};
3318
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01003319/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003320 * Software counter: task time clock
3321 */
3322
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02003323static void task_clock_perf_counter_update(struct perf_counter *counter, u64 now)
Ingo Molnarbae43c92008-12-11 14:03:20 +01003324{
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02003325 u64 prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003326 s64 delta;
Ingo Molnarbae43c92008-12-11 14:03:20 +01003327
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02003328 prev = atomic64_xchg(&counter->hw.prev_count, now);
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003329 delta = now - prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003330 atomic64_add(delta, &counter->count);
Ingo Molnarbae43c92008-12-11 14:03:20 +01003331}
3332
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01003333static int task_clock_perf_counter_enable(struct perf_counter *counter)
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003334{
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003335 struct hw_perf_counter *hwc = &counter->hw;
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02003336 u64 now;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003337
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02003338 now = counter->ctx->time;
3339
3340 atomic64_set(&hwc->prev_count, now);
Peter Zijlstra039fc912009-03-13 16:43:47 +01003341 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
3342 hwc->hrtimer.function = perf_swcounter_hrtimer;
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003343 if (hwc->sample_period) {
3344 u64 period = max_t(u64, 10000, hwc->sample_period);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003345 __hrtimer_start_range_ns(&hwc->hrtimer,
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003346 ns_to_ktime(period), 0,
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003347 HRTIMER_MODE_REL, 0);
3348 }
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01003349
3350 return 0;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003351}
3352
3353static void task_clock_perf_counter_disable(struct perf_counter *counter)
3354{
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003355 if (counter->hw.sample_period)
Peter Zijlstrab986d7e2009-05-20 12:21:21 +02003356 hrtimer_cancel(&counter->hw.hrtimer);
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02003357 task_clock_perf_counter_update(counter, counter->ctx->time);
3358
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003359}
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01003360
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003361static void task_clock_perf_counter_read(struct perf_counter *counter)
3362{
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02003363 u64 time;
3364
3365 if (!in_nmi()) {
3366 update_context_time(counter->ctx);
3367 time = counter->ctx->time;
3368 } else {
3369 u64 now = perf_clock();
3370 u64 delta = now - counter->ctx->timestamp;
3371 time = counter->ctx->time + delta;
3372 }
3373
3374 task_clock_perf_counter_update(counter, time);
Ingo Molnarbae43c92008-12-11 14:03:20 +01003375}
3376
Robert Richter4aeb0b42009-04-29 12:47:03 +02003377static const struct pmu perf_ops_task_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +01003378 .enable = task_clock_perf_counter_enable,
3379 .disable = task_clock_perf_counter_disable,
3380 .read = task_clock_perf_counter_read,
Ingo Molnarbae43c92008-12-11 14:03:20 +01003381};
3382
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003383/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003384 * Software counter: cpu migrations
3385 */
Paul Mackerras3f731ca2009-06-01 17:52:30 +10003386void perf_counter_task_migration(struct task_struct *task, int cpu)
Ingo Molnar6c594c22008-12-14 12:34:15 +01003387{
Paul Mackerras3f731ca2009-06-01 17:52:30 +10003388 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
3389 struct perf_counter_context *ctx;
Paul Mackerras23a185c2009-02-09 22:42:47 +11003390
Paul Mackerras3f731ca2009-06-01 17:52:30 +10003391 perf_swcounter_ctx_event(&cpuctx->ctx, PERF_TYPE_SOFTWARE,
3392 PERF_COUNT_CPU_MIGRATIONS,
3393 1, 1, NULL, 0);
3394
3395 ctx = perf_pin_task_context(task);
3396 if (ctx) {
3397 perf_swcounter_ctx_event(ctx, PERF_TYPE_SOFTWARE,
3398 PERF_COUNT_CPU_MIGRATIONS,
3399 1, 1, NULL, 0);
3400 perf_unpin_context(ctx);
3401 }
Ingo Molnar6c594c22008-12-14 12:34:15 +01003402}
3403
Peter Zijlstrae077df42009-03-19 20:26:17 +01003404#ifdef CONFIG_EVENT_PROFILE
3405void perf_tpcounter_event(int event_id)
3406{
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003407 struct pt_regs *regs = get_irq_regs();
3408
3409 if (!regs)
3410 regs = task_pt_regs(current);
3411
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003412 __perf_swcounter_event(PERF_TYPE_TRACEPOINT, event_id, 1, 1, regs, 0);
Peter Zijlstrae077df42009-03-19 20:26:17 +01003413}
Steven Whitehouseff7b1b42009-04-15 16:55:05 +01003414EXPORT_SYMBOL_GPL(perf_tpcounter_event);
Peter Zijlstrae077df42009-03-19 20:26:17 +01003415
3416extern int ftrace_profile_enable(int);
3417extern void ftrace_profile_disable(int);
3418
3419static void tp_perf_counter_destroy(struct perf_counter *counter)
3420{
Peter Zijlstra0d486962009-06-02 19:22:16 +02003421 ftrace_profile_disable(perf_event_id(&counter->attr));
Peter Zijlstrae077df42009-03-19 20:26:17 +01003422}
3423
Robert Richter4aeb0b42009-04-29 12:47:03 +02003424static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
Peter Zijlstrae077df42009-03-19 20:26:17 +01003425{
Peter Zijlstra0d486962009-06-02 19:22:16 +02003426 int event_id = perf_event_id(&counter->attr);
Peter Zijlstrae077df42009-03-19 20:26:17 +01003427 int ret;
3428
3429 ret = ftrace_profile_enable(event_id);
3430 if (ret)
3431 return NULL;
3432
3433 counter->destroy = tp_perf_counter_destroy;
3434
3435 return &perf_ops_generic;
3436}
3437#else
Robert Richter4aeb0b42009-04-29 12:47:03 +02003438static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
Peter Zijlstrae077df42009-03-19 20:26:17 +01003439{
3440 return NULL;
3441}
3442#endif
3443
Robert Richter4aeb0b42009-04-29 12:47:03 +02003444static const struct pmu *sw_perf_counter_init(struct perf_counter *counter)
Ingo Molnar5c92d122008-12-11 13:21:10 +01003445{
Robert Richter4aeb0b42009-04-29 12:47:03 +02003446 const struct pmu *pmu = NULL;
Ingo Molnar5c92d122008-12-11 13:21:10 +01003447
Paul Mackerras0475f9e2009-02-11 14:35:35 +11003448 /*
3449 * Software counters (currently) can't in general distinguish
3450 * between user, kernel and hypervisor events.
3451 * However, context switches and cpu migrations are considered
3452 * to be kernel events, and page faults are never hypervisor
3453 * events.
3454 */
Ingo Molnara21ca2c2009-06-06 09:58:57 +02003455 switch (counter->attr.config) {
Ingo Molnar5c92d122008-12-11 13:21:10 +01003456 case PERF_COUNT_CPU_CLOCK:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003457 pmu = &perf_ops_cpu_clock;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003458
Ingo Molnar5c92d122008-12-11 13:21:10 +01003459 break;
Ingo Molnarbae43c92008-12-11 14:03:20 +01003460 case PERF_COUNT_TASK_CLOCK:
Paul Mackerras23a185c2009-02-09 22:42:47 +11003461 /*
3462 * If the user instantiates this as a per-cpu counter,
3463 * use the cpu_clock counter instead.
3464 */
3465 if (counter->ctx->task)
Robert Richter4aeb0b42009-04-29 12:47:03 +02003466 pmu = &perf_ops_task_clock;
Paul Mackerras23a185c2009-02-09 22:42:47 +11003467 else
Robert Richter4aeb0b42009-04-29 12:47:03 +02003468 pmu = &perf_ops_cpu_clock;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003469
Ingo Molnarbae43c92008-12-11 14:03:20 +01003470 break;
Ingo Molnare06c61a2008-12-14 14:44:31 +01003471 case PERF_COUNT_PAGE_FAULTS:
Peter Zijlstraac17dc82009-03-13 12:21:34 +01003472 case PERF_COUNT_PAGE_FAULTS_MIN:
3473 case PERF_COUNT_PAGE_FAULTS_MAJ:
Ingo Molnar5d6a27d2008-12-14 12:28:33 +01003474 case PERF_COUNT_CONTEXT_SWITCHES:
Ingo Molnar6c594c22008-12-14 12:34:15 +01003475 case PERF_COUNT_CPU_MIGRATIONS:
Paul Mackerras3f731ca2009-06-01 17:52:30 +10003476 pmu = &perf_ops_generic;
Ingo Molnar6c594c22008-12-14 12:34:15 +01003477 break;
Ingo Molnar5c92d122008-12-11 13:21:10 +01003478 }
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003479
Robert Richter4aeb0b42009-04-29 12:47:03 +02003480 return pmu;
Ingo Molnar5c92d122008-12-11 13:21:10 +01003481}
3482
Thomas Gleixner0793a612008-12-04 20:12:29 +01003483/*
3484 * Allocate and initialize a counter structure
3485 */
3486static struct perf_counter *
Peter Zijlstra0d486962009-06-02 19:22:16 +02003487perf_counter_alloc(struct perf_counter_attr *attr,
Ingo Molnar04289bb2008-12-11 08:38:42 +01003488 int cpu,
Paul Mackerras23a185c2009-02-09 22:42:47 +11003489 struct perf_counter_context *ctx,
Ingo Molnar9b51f662008-12-12 13:49:45 +01003490 struct perf_counter *group_leader,
3491 gfp_t gfpflags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003492{
Robert Richter4aeb0b42009-04-29 12:47:03 +02003493 const struct pmu *pmu;
Ingo Molnar621a01e2008-12-11 12:46:46 +01003494 struct perf_counter *counter;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003495 struct hw_perf_counter *hwc;
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003496 long err;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003497
Ingo Molnar9b51f662008-12-12 13:49:45 +01003498 counter = kzalloc(sizeof(*counter), gfpflags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003499 if (!counter)
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003500 return ERR_PTR(-ENOMEM);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003501
Ingo Molnar04289bb2008-12-11 08:38:42 +01003502 /*
3503 * Single counters are their own group leaders, with an
3504 * empty sibling list:
3505 */
3506 if (!group_leader)
3507 group_leader = counter;
3508
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003509 mutex_init(&counter->child_mutex);
3510 INIT_LIST_HEAD(&counter->child_list);
3511
Ingo Molnar04289bb2008-12-11 08:38:42 +01003512 INIT_LIST_HEAD(&counter->list_entry);
Peter Zijlstra592903c2009-03-13 12:21:36 +01003513 INIT_LIST_HEAD(&counter->event_entry);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003514 INIT_LIST_HEAD(&counter->sibling_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003515 init_waitqueue_head(&counter->waitq);
3516
Peter Zijlstra7b732a72009-03-23 18:22:10 +01003517 mutex_init(&counter->mmap_mutex);
3518
Peter Zijlstraa96bbc12009-06-03 14:01:36 +02003519 counter->cpu = cpu;
Peter Zijlstra0d486962009-06-02 19:22:16 +02003520 counter->attr = *attr;
Peter Zijlstraa96bbc12009-06-03 14:01:36 +02003521 counter->group_leader = group_leader;
3522 counter->pmu = NULL;
3523 counter->ctx = ctx;
3524 counter->oncpu = -1;
Ingo Molnar329d8762009-05-26 08:10:00 +02003525
Peter Zijlstraa96bbc12009-06-03 14:01:36 +02003526 counter->ns = get_pid_ns(current->nsproxy->pid_ns);
3527 counter->id = atomic64_inc_return(&perf_counter_id);
3528
3529 counter->state = PERF_COUNTER_STATE_INACTIVE;
3530
Peter Zijlstra0d486962009-06-02 19:22:16 +02003531 if (attr->disabled)
Ingo Molnara86ed502008-12-17 00:43:10 +01003532 counter->state = PERF_COUNTER_STATE_OFF;
3533
Robert Richter4aeb0b42009-04-29 12:47:03 +02003534 pmu = NULL;
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003535
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003536 hwc = &counter->hw;
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02003537 hwc->sample_period = attr->sample_period;
Peter Zijlstra0d486962009-06-02 19:22:16 +02003538 if (attr->freq && attr->sample_freq)
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02003539 hwc->sample_period = 1;
3540
3541 atomic64_set(&hwc->period_left, hwc->sample_period);
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003542
Peter Zijlstra2023b352009-05-05 17:50:26 +02003543 /*
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003544 * we currently do not support PERF_SAMPLE_GROUP on inherited counters
Peter Zijlstra2023b352009-05-05 17:50:26 +02003545 */
Peter Zijlstra0d486962009-06-02 19:22:16 +02003546 if (attr->inherit && (attr->sample_type & PERF_SAMPLE_GROUP))
Peter Zijlstra2023b352009-05-05 17:50:26 +02003547 goto done;
3548
Ingo Molnara21ca2c2009-06-06 09:58:57 +02003549 if (attr->type == PERF_TYPE_RAW) {
Robert Richter4aeb0b42009-04-29 12:47:03 +02003550 pmu = hw_perf_counter_init(counter);
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01003551 goto done;
3552 }
3553
Ingo Molnara21ca2c2009-06-06 09:58:57 +02003554 switch (attr->type) {
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003555 case PERF_TYPE_HARDWARE:
Ingo Molnar8326f442009-06-05 20:22:46 +02003556 case PERF_TYPE_HW_CACHE:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003557 pmu = hw_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003558 break;
3559
3560 case PERF_TYPE_SOFTWARE:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003561 pmu = sw_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003562 break;
3563
3564 case PERF_TYPE_TRACEPOINT:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003565 pmu = tp_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003566 break;
3567 }
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01003568done:
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003569 err = 0;
Robert Richter4aeb0b42009-04-29 12:47:03 +02003570 if (!pmu)
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003571 err = -EINVAL;
Robert Richter4aeb0b42009-04-29 12:47:03 +02003572 else if (IS_ERR(pmu))
3573 err = PTR_ERR(pmu);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003574
3575 if (err) {
Peter Zijlstraa96bbc12009-06-03 14:01:36 +02003576 if (counter->ns)
3577 put_pid_ns(counter->ns);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003578 kfree(counter);
3579 return ERR_PTR(err);
3580 }
3581
Robert Richter4aeb0b42009-04-29 12:47:03 +02003582 counter->pmu = pmu;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003583
Peter Zijlstra7fc23a52009-05-08 18:52:21 +02003584 atomic_inc(&nr_counters);
Peter Zijlstra0d486962009-06-02 19:22:16 +02003585 if (counter->attr.mmap)
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003586 atomic_inc(&nr_mmap_counters);
Peter Zijlstra0d486962009-06-02 19:22:16 +02003587 if (counter->attr.comm)
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003588 atomic_inc(&nr_comm_counters);
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02003589
Thomas Gleixner0793a612008-12-04 20:12:29 +01003590 return counter;
3591}
3592
3593/**
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003594 * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
Ingo Molnar9f66a382008-12-10 12:33:23 +01003595 *
Peter Zijlstra0d486962009-06-02 19:22:16 +02003596 * @attr_uptr: event type attributes for monitoring/sampling
Thomas Gleixner0793a612008-12-04 20:12:29 +01003597 * @pid: target pid
Ingo Molnar9f66a382008-12-10 12:33:23 +01003598 * @cpu: target cpu
3599 * @group_fd: group leader counter fd
Thomas Gleixner0793a612008-12-04 20:12:29 +01003600 */
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003601SYSCALL_DEFINE5(perf_counter_open,
Peter Zijlstra0d486962009-06-02 19:22:16 +02003602 const struct perf_counter_attr __user *, attr_uptr,
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003603 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003604{
Ingo Molnar04289bb2008-12-11 08:38:42 +01003605 struct perf_counter *counter, *group_leader;
Peter Zijlstra0d486962009-06-02 19:22:16 +02003606 struct perf_counter_attr attr;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003607 struct perf_counter_context *ctx;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003608 struct file *counter_file = NULL;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003609 struct file *group_file = NULL;
3610 int fput_needed = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003611 int fput_needed2 = 0;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003612 int ret;
3613
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003614 /* for future expandability... */
3615 if (flags)
3616 return -EINVAL;
3617
Peter Zijlstra0d486962009-06-02 19:22:16 +02003618 if (copy_from_user(&attr, attr_uptr, sizeof(attr)) != 0)
Thomas Gleixnereab656a2008-12-08 19:26:59 +01003619 return -EFAULT;
3620
Ingo Molnar04289bb2008-12-11 08:38:42 +01003621 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01003622 * Get the target context (task or percpu):
3623 */
3624 ctx = find_get_context(pid, cpu);
3625 if (IS_ERR(ctx))
3626 return PTR_ERR(ctx);
3627
3628 /*
3629 * Look up the group leader (we will attach this counter to it):
Ingo Molnar04289bb2008-12-11 08:38:42 +01003630 */
3631 group_leader = NULL;
3632 if (group_fd != -1) {
3633 ret = -EINVAL;
3634 group_file = fget_light(group_fd, &fput_needed);
3635 if (!group_file)
Ingo Molnarccff2862008-12-11 11:26:29 +01003636 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003637 if (group_file->f_op != &perf_fops)
Ingo Molnarccff2862008-12-11 11:26:29 +01003638 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003639
3640 group_leader = group_file->private_data;
3641 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01003642 * Do not allow a recursive hierarchy (this new sibling
3643 * becoming part of another group-sibling):
Ingo Molnar04289bb2008-12-11 08:38:42 +01003644 */
Ingo Molnarccff2862008-12-11 11:26:29 +01003645 if (group_leader->group_leader != group_leader)
3646 goto err_put_context;
3647 /*
3648 * Do not allow to attach to a group in a different
3649 * task or CPU context:
3650 */
3651 if (group_leader->ctx != ctx)
3652 goto err_put_context;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11003653 /*
3654 * Only a group leader can be exclusive or pinned
3655 */
Peter Zijlstra0d486962009-06-02 19:22:16 +02003656 if (attr.exclusive || attr.pinned)
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11003657 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003658 }
3659
Peter Zijlstra0d486962009-06-02 19:22:16 +02003660 counter = perf_counter_alloc(&attr, cpu, ctx, group_leader,
Paul Mackerras23a185c2009-02-09 22:42:47 +11003661 GFP_KERNEL);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003662 ret = PTR_ERR(counter);
3663 if (IS_ERR(counter))
Thomas Gleixner0793a612008-12-04 20:12:29 +01003664 goto err_put_context;
3665
Thomas Gleixner0793a612008-12-04 20:12:29 +01003666 ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
3667 if (ret < 0)
Ingo Molnar9b51f662008-12-12 13:49:45 +01003668 goto err_free_put_context;
3669
3670 counter_file = fget_light(ret, &fput_needed2);
3671 if (!counter_file)
3672 goto err_free_put_context;
3673
3674 counter->filp = counter_file;
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003675 WARN_ON_ONCE(ctx->parent_ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003676 mutex_lock(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003677 perf_install_in_context(ctx, counter, cpu);
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003678 ++ctx->generation;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003679 mutex_unlock(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003680
Peter Zijlstra082ff5a2009-05-23 18:29:00 +02003681 counter->owner = current;
3682 get_task_struct(current);
3683 mutex_lock(&current->perf_counter_mutex);
3684 list_add_tail(&counter->owner_entry, &current->perf_counter_list);
3685 mutex_unlock(&current->perf_counter_mutex);
3686
Ingo Molnar9b51f662008-12-12 13:49:45 +01003687 fput_light(counter_file, fput_needed2);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003688
Ingo Molnar04289bb2008-12-11 08:38:42 +01003689out_fput:
3690 fput_light(group_file, fput_needed);
3691
Thomas Gleixner0793a612008-12-04 20:12:29 +01003692 return ret;
3693
Ingo Molnar9b51f662008-12-12 13:49:45 +01003694err_free_put_context:
Thomas Gleixner0793a612008-12-04 20:12:29 +01003695 kfree(counter);
3696
3697err_put_context:
Paul Mackerrasc93f7662009-05-28 22:18:17 +10003698 put_ctx(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003699
Ingo Molnar04289bb2008-12-11 08:38:42 +01003700 goto out_fput;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003701}
3702
Ingo Molnar9b51f662008-12-12 13:49:45 +01003703/*
Ingo Molnar9b51f662008-12-12 13:49:45 +01003704 * inherit a counter from parent task to child task:
3705 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11003706static struct perf_counter *
Ingo Molnar9b51f662008-12-12 13:49:45 +01003707inherit_counter(struct perf_counter *parent_counter,
3708 struct task_struct *parent,
3709 struct perf_counter_context *parent_ctx,
3710 struct task_struct *child,
Paul Mackerrasd859e292009-01-17 18:10:22 +11003711 struct perf_counter *group_leader,
Ingo Molnar9b51f662008-12-12 13:49:45 +01003712 struct perf_counter_context *child_ctx)
3713{
3714 struct perf_counter *child_counter;
3715
Paul Mackerrasd859e292009-01-17 18:10:22 +11003716 /*
3717 * Instead of creating recursive hierarchies of counters,
3718 * we link inherited counters back to the original parent,
3719 * which has a filp for sure, which we use as the reference
3720 * count:
3721 */
3722 if (parent_counter->parent)
3723 parent_counter = parent_counter->parent;
3724
Peter Zijlstra0d486962009-06-02 19:22:16 +02003725 child_counter = perf_counter_alloc(&parent_counter->attr,
Paul Mackerras23a185c2009-02-09 22:42:47 +11003726 parent_counter->cpu, child_ctx,
3727 group_leader, GFP_KERNEL);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003728 if (IS_ERR(child_counter))
3729 return child_counter;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10003730 get_ctx(child_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003731
3732 /*
Paul Mackerras564c2b22009-05-22 14:27:22 +10003733 * Make the child state follow the state of the parent counter,
Peter Zijlstra0d486962009-06-02 19:22:16 +02003734 * not its attr.disabled bit. We hold the parent's mutex,
Ingo Molnar22a4f652009-06-01 10:13:37 +02003735 * so we won't race with perf_counter_{en, dis}able_family.
Paul Mackerras564c2b22009-05-22 14:27:22 +10003736 */
3737 if (parent_counter->state >= PERF_COUNTER_STATE_INACTIVE)
3738 child_counter->state = PERF_COUNTER_STATE_INACTIVE;
3739 else
3740 child_counter->state = PERF_COUNTER_STATE_OFF;
3741
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02003742 if (parent_counter->attr.freq)
3743 child_counter->hw.sample_period = parent_counter->hw.sample_period;
3744
Paul Mackerras564c2b22009-05-22 14:27:22 +10003745 /*
Ingo Molnar9b51f662008-12-12 13:49:45 +01003746 * Link it up in the child's context:
3747 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +11003748 add_counter_to_ctx(child_counter, child_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003749
3750 child_counter->parent = parent_counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003751 /*
3752 * inherit into child's child as well:
3753 */
Peter Zijlstra0d486962009-06-02 19:22:16 +02003754 child_counter->attr.inherit = 1;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003755
3756 /*
3757 * Get a reference to the parent filp - we will fput it
3758 * when the child counter exits. This is safe to do because
3759 * we are in the parent and we know that the filp still
3760 * exists and has a nonzero count:
3761 */
3762 atomic_long_inc(&parent_counter->filp->f_count);
3763
Paul Mackerrasd859e292009-01-17 18:10:22 +11003764 /*
3765 * Link this into the parent counter's child list
3766 */
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003767 WARN_ON_ONCE(parent_counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003768 mutex_lock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003769 list_add_tail(&child_counter->child_list, &parent_counter->child_list);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003770 mutex_unlock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003771
3772 return child_counter;
3773}
3774
3775static int inherit_group(struct perf_counter *parent_counter,
3776 struct task_struct *parent,
3777 struct perf_counter_context *parent_ctx,
3778 struct task_struct *child,
3779 struct perf_counter_context *child_ctx)
3780{
3781 struct perf_counter *leader;
3782 struct perf_counter *sub;
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003783 struct perf_counter *child_ctr;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003784
3785 leader = inherit_counter(parent_counter, parent, parent_ctx,
3786 child, NULL, child_ctx);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003787 if (IS_ERR(leader))
3788 return PTR_ERR(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003789 list_for_each_entry(sub, &parent_counter->sibling_list, list_entry) {
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003790 child_ctr = inherit_counter(sub, parent, parent_ctx,
3791 child, leader, child_ctx);
3792 if (IS_ERR(child_ctr))
3793 return PTR_ERR(child_ctr);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003794 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01003795 return 0;
3796}
3797
Paul Mackerrasd859e292009-01-17 18:10:22 +11003798static void sync_child_counter(struct perf_counter *child_counter,
3799 struct perf_counter *parent_counter)
3800{
Peter Zijlstra8bc20952009-05-15 20:45:59 +02003801 u64 child_val;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003802
Paul Mackerrasd859e292009-01-17 18:10:22 +11003803 child_val = atomic64_read(&child_counter->count);
3804
3805 /*
3806 * Add back the child's count to the parent's count:
3807 */
3808 atomic64_add(child_val, &parent_counter->count);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11003809 atomic64_add(child_counter->total_time_enabled,
3810 &parent_counter->child_total_time_enabled);
3811 atomic64_add(child_counter->total_time_running,
3812 &parent_counter->child_total_time_running);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003813
3814 /*
3815 * Remove this counter from the parent's list
3816 */
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003817 WARN_ON_ONCE(parent_counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003818 mutex_lock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003819 list_del_init(&child_counter->child_list);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003820 mutex_unlock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003821
3822 /*
3823 * Release the parent counter, if this was the last
3824 * reference to it.
3825 */
3826 fput(parent_counter->filp);
3827}
3828
Ingo Molnar9b51f662008-12-12 13:49:45 +01003829static void
Peter Zijlstrabbbee902009-05-29 14:25:58 +02003830__perf_counter_exit_task(struct perf_counter *child_counter,
Ingo Molnar9b51f662008-12-12 13:49:45 +01003831 struct perf_counter_context *child_ctx)
3832{
3833 struct perf_counter *parent_counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003834
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003835 update_counter_times(child_counter);
Peter Zijlstraaa9c67f2009-05-23 18:28:59 +02003836 perf_counter_remove_from_context(child_counter);
Ingo Molnar0cc0c022008-12-14 23:20:36 +01003837
Ingo Molnar9b51f662008-12-12 13:49:45 +01003838 parent_counter = child_counter->parent;
3839 /*
3840 * It can happen that parent exits first, and has counters
3841 * that are still around due to the child reference. These
3842 * counters need to be zapped - but otherwise linger.
3843 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11003844 if (parent_counter) {
3845 sync_child_counter(child_counter, parent_counter);
Peter Zijlstraf1600952009-03-19 20:26:16 +01003846 free_counter(child_counter);
Paul Mackerras4bcf3492009-02-11 13:53:19 +01003847 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01003848}
3849
3850/*
Paul Mackerrasd859e292009-01-17 18:10:22 +11003851 * When a child task exits, feed back counter values to parent counters.
Ingo Molnar9b51f662008-12-12 13:49:45 +01003852 */
3853void perf_counter_exit_task(struct task_struct *child)
3854{
3855 struct perf_counter *child_counter, *tmp;
3856 struct perf_counter_context *child_ctx;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003857 unsigned long flags;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003858
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003859 if (likely(!child->perf_counter_ctxp))
Ingo Molnar9b51f662008-12-12 13:49:45 +01003860 return;
3861
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003862 local_irq_save(flags);
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003863 /*
3864 * We can't reschedule here because interrupts are disabled,
3865 * and either child is current or it is a task that can't be
3866 * scheduled, so we are now safe from rescheduling changing
3867 * our context.
3868 */
3869 child_ctx = child->perf_counter_ctxp;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003870 __perf_counter_task_sched_out(child_ctx);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10003871
3872 /*
3873 * Take the context lock here so that if find_get_context is
3874 * reading child->perf_counter_ctxp, we wait until it has
3875 * incremented the context's refcount before we do put_ctx below.
3876 */
3877 spin_lock(&child_ctx->lock);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003878 child->perf_counter_ctxp = NULL;
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003879 if (child_ctx->parent_ctx) {
3880 /*
3881 * This context is a clone; unclone it so it can't get
3882 * swapped to another process while we're removing all
3883 * the counters from it.
3884 */
3885 put_ctx(child_ctx->parent_ctx);
3886 child_ctx->parent_ctx = NULL;
3887 }
Paul Mackerrasc93f7662009-05-28 22:18:17 +10003888 spin_unlock(&child_ctx->lock);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003889 local_irq_restore(flags);
3890
Peter Zijlstra66fff222009-06-10 22:53:37 +02003891 /*
3892 * We can recurse on the same lock type through:
3893 *
3894 * __perf_counter_exit_task()
3895 * sync_child_counter()
3896 * fput(parent_counter->filp)
3897 * perf_release()
3898 * mutex_lock(&ctx->mutex)
3899 *
3900 * But since its the parent context it won't be the same instance.
3901 */
3902 mutex_lock_nested(&child_ctx->mutex, SINGLE_DEPTH_NESTING);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003903
Peter Zijlstra8bc20952009-05-15 20:45:59 +02003904again:
Ingo Molnar9b51f662008-12-12 13:49:45 +01003905 list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list,
3906 list_entry)
Peter Zijlstrabbbee902009-05-29 14:25:58 +02003907 __perf_counter_exit_task(child_counter, child_ctx);
Peter Zijlstra8bc20952009-05-15 20:45:59 +02003908
3909 /*
3910 * If the last counter was a group counter, it will have appended all
3911 * its siblings to the list, but we obtained 'tmp' before that which
3912 * will still point to the list head terminating the iteration.
3913 */
3914 if (!list_empty(&child_ctx->counter_list))
3915 goto again;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003916
3917 mutex_unlock(&child_ctx->mutex);
3918
3919 put_ctx(child_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003920}
3921
3922/*
Peter Zijlstrabbbee902009-05-29 14:25:58 +02003923 * free an unexposed, unused context as created by inheritance by
3924 * init_task below, used by fork() in case of fail.
3925 */
3926void perf_counter_free_task(struct task_struct *task)
3927{
3928 struct perf_counter_context *ctx = task->perf_counter_ctxp;
3929 struct perf_counter *counter, *tmp;
3930
3931 if (!ctx)
3932 return;
3933
3934 mutex_lock(&ctx->mutex);
3935again:
3936 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry) {
3937 struct perf_counter *parent = counter->parent;
3938
3939 if (WARN_ON_ONCE(!parent))
3940 continue;
3941
3942 mutex_lock(&parent->child_mutex);
3943 list_del_init(&counter->child_list);
3944 mutex_unlock(&parent->child_mutex);
3945
3946 fput(parent->filp);
3947
3948 list_del_counter(counter, ctx);
3949 free_counter(counter);
3950 }
3951
3952 if (!list_empty(&ctx->counter_list))
3953 goto again;
3954
3955 mutex_unlock(&ctx->mutex);
3956
3957 put_ctx(ctx);
3958}
3959
3960/*
Ingo Molnar9b51f662008-12-12 13:49:45 +01003961 * Initialize the perf_counter context in task_struct
3962 */
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02003963int perf_counter_init_task(struct task_struct *child)
Ingo Molnar9b51f662008-12-12 13:49:45 +01003964{
3965 struct perf_counter_context *child_ctx, *parent_ctx;
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003966 struct perf_counter_context *cloned_ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003967 struct perf_counter *counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003968 struct task_struct *parent = current;
Paul Mackerras564c2b22009-05-22 14:27:22 +10003969 int inherited_all = 1;
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02003970 int ret = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003971
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003972 child->perf_counter_ctxp = NULL;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003973
Peter Zijlstra082ff5a2009-05-23 18:29:00 +02003974 mutex_init(&child->perf_counter_mutex);
3975 INIT_LIST_HEAD(&child->perf_counter_list);
3976
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003977 if (likely(!parent->perf_counter_ctxp))
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02003978 return 0;
3979
Ingo Molnar9b51f662008-12-12 13:49:45 +01003980 /*
3981 * This is executed from the parent task context, so inherit
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003982 * counters that have been marked for cloning.
3983 * First allocate and initialize a context for the child.
Ingo Molnar9b51f662008-12-12 13:49:45 +01003984 */
3985
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003986 child_ctx = kmalloc(sizeof(struct perf_counter_context), GFP_KERNEL);
3987 if (!child_ctx)
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02003988 return -ENOMEM;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003989
3990 __perf_counter_init_context(child_ctx, child);
3991 child->perf_counter_ctxp = child_ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10003992 get_task_struct(child);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003993
Ingo Molnar9b51f662008-12-12 13:49:45 +01003994 /*
Paul Mackerras25346b92009-06-01 17:48:12 +10003995 * If the parent's context is a clone, pin it so it won't get
3996 * swapped under us.
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003997 */
Paul Mackerras25346b92009-06-01 17:48:12 +10003998 parent_ctx = perf_pin_task_context(parent);
3999
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004000 /*
4001 * No need to check if parent_ctx != NULL here; since we saw
4002 * it non-NULL earlier, the only reason for it to become NULL
4003 * is if we exit, and since we're currently in the middle of
4004 * a fork we can't be exiting at the same time.
4005 */
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004006
4007 /*
Ingo Molnar9b51f662008-12-12 13:49:45 +01004008 * Lock the parent list. No need to lock the child - not PID
4009 * hashed yet and not running, so nobody can access it.
4010 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11004011 mutex_lock(&parent_ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01004012
4013 /*
4014 * We dont have to disable NMIs - we are only looking at
4015 * the list, not manipulating it:
4016 */
Peter Zijlstrad7b629a2009-05-20 12:21:19 +02004017 list_for_each_entry_rcu(counter, &parent_ctx->event_list, event_entry) {
4018 if (counter != counter->group_leader)
4019 continue;
4020
Peter Zijlstra0d486962009-06-02 19:22:16 +02004021 if (!counter->attr.inherit) {
Paul Mackerras564c2b22009-05-22 14:27:22 +10004022 inherited_all = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004023 continue;
Paul Mackerras564c2b22009-05-22 14:27:22 +10004024 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01004025
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02004026 ret = inherit_group(counter, parent, parent_ctx,
4027 child, child_ctx);
4028 if (ret) {
Paul Mackerras564c2b22009-05-22 14:27:22 +10004029 inherited_all = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004030 break;
Paul Mackerras564c2b22009-05-22 14:27:22 +10004031 }
4032 }
4033
4034 if (inherited_all) {
4035 /*
4036 * Mark the child context as a clone of the parent
4037 * context, or of whatever the parent is a clone of.
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004038 * Note that if the parent is a clone, it could get
4039 * uncloned at any point, but that doesn't matter
4040 * because the list of counters and the generation
4041 * count can't have changed since we took the mutex.
Paul Mackerras564c2b22009-05-22 14:27:22 +10004042 */
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004043 cloned_ctx = rcu_dereference(parent_ctx->parent_ctx);
4044 if (cloned_ctx) {
4045 child_ctx->parent_ctx = cloned_ctx;
Paul Mackerras25346b92009-06-01 17:48:12 +10004046 child_ctx->parent_gen = parent_ctx->parent_gen;
Paul Mackerras564c2b22009-05-22 14:27:22 +10004047 } else {
4048 child_ctx->parent_ctx = parent_ctx;
4049 child_ctx->parent_gen = parent_ctx->generation;
4050 }
4051 get_ctx(child_ctx->parent_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01004052 }
4053
Paul Mackerrasd859e292009-01-17 18:10:22 +11004054 mutex_unlock(&parent_ctx->mutex);
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02004055
Paul Mackerras25346b92009-06-01 17:48:12 +10004056 perf_unpin_context(parent_ctx);
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004057
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02004058 return ret;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004059}
4060
Ingo Molnar04289bb2008-12-11 08:38:42 +01004061static void __cpuinit perf_counter_init_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01004062{
Ingo Molnar04289bb2008-12-11 08:38:42 +01004063 struct perf_cpu_context *cpuctx;
Thomas Gleixner0793a612008-12-04 20:12:29 +01004064
Ingo Molnar04289bb2008-12-11 08:38:42 +01004065 cpuctx = &per_cpu(perf_cpu_context, cpu);
4066 __perf_counter_init_context(&cpuctx->ctx, NULL);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004067
Ingo Molnar1dce8d92009-05-04 19:23:18 +02004068 spin_lock(&perf_resource_lock);
Ingo Molnar04289bb2008-12-11 08:38:42 +01004069 cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu;
Ingo Molnar1dce8d92009-05-04 19:23:18 +02004070 spin_unlock(&perf_resource_lock);
Ingo Molnar04289bb2008-12-11 08:38:42 +01004071
Paul Mackerras01d02872009-01-14 13:44:19 +11004072 hw_perf_counter_setup(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004073}
4074
4075#ifdef CONFIG_HOTPLUG_CPU
Ingo Molnar04289bb2008-12-11 08:38:42 +01004076static void __perf_counter_exit_cpu(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01004077{
4078 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
4079 struct perf_counter_context *ctx = &cpuctx->ctx;
4080 struct perf_counter *counter, *tmp;
4081
Ingo Molnar04289bb2008-12-11 08:38:42 +01004082 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry)
4083 __perf_counter_remove_from_context(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004084}
Ingo Molnar04289bb2008-12-11 08:38:42 +01004085static void perf_counter_exit_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01004086{
Paul Mackerrasd859e292009-01-17 18:10:22 +11004087 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
4088 struct perf_counter_context *ctx = &cpuctx->ctx;
4089
4090 mutex_lock(&ctx->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01004091 smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1);
Paul Mackerrasd859e292009-01-17 18:10:22 +11004092 mutex_unlock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004093}
4094#else
Ingo Molnar04289bb2008-12-11 08:38:42 +01004095static inline void perf_counter_exit_cpu(int cpu) { }
Thomas Gleixner0793a612008-12-04 20:12:29 +01004096#endif
4097
4098static int __cpuinit
4099perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
4100{
4101 unsigned int cpu = (long)hcpu;
4102
4103 switch (action) {
4104
4105 case CPU_UP_PREPARE:
4106 case CPU_UP_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01004107 perf_counter_init_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004108 break;
4109
4110 case CPU_DOWN_PREPARE:
4111 case CPU_DOWN_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01004112 perf_counter_exit_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004113 break;
4114
4115 default:
4116 break;
4117 }
4118
4119 return NOTIFY_OK;
4120}
4121
Paul Mackerrasf38b0822009-06-02 21:05:16 +10004122/*
4123 * This has to have a higher priority than migration_notifier in sched.c.
4124 */
Thomas Gleixner0793a612008-12-04 20:12:29 +01004125static struct notifier_block __cpuinitdata perf_cpu_nb = {
4126 .notifier_call = perf_cpu_notify,
Paul Mackerrasf38b0822009-06-02 21:05:16 +10004127 .priority = 20,
Thomas Gleixner0793a612008-12-04 20:12:29 +01004128};
4129
Ingo Molnar0d905bc2009-05-04 19:13:30 +02004130void __init perf_counter_init(void)
Thomas Gleixner0793a612008-12-04 20:12:29 +01004131{
4132 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
4133 (void *)(long)smp_processor_id());
4134 register_cpu_notifier(&perf_cpu_nb);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004135}
Thomas Gleixner0793a612008-12-04 20:12:29 +01004136
4137static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
4138{
4139 return sprintf(buf, "%d\n", perf_reserved_percpu);
4140}
4141
4142static ssize_t
4143perf_set_reserve_percpu(struct sysdev_class *class,
4144 const char *buf,
4145 size_t count)
4146{
4147 struct perf_cpu_context *cpuctx;
4148 unsigned long val;
4149 int err, cpu, mpt;
4150
4151 err = strict_strtoul(buf, 10, &val);
4152 if (err)
4153 return err;
4154 if (val > perf_max_counters)
4155 return -EINVAL;
4156
Ingo Molnar1dce8d92009-05-04 19:23:18 +02004157 spin_lock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004158 perf_reserved_percpu = val;
4159 for_each_online_cpu(cpu) {
4160 cpuctx = &per_cpu(perf_cpu_context, cpu);
4161 spin_lock_irq(&cpuctx->ctx.lock);
4162 mpt = min(perf_max_counters - cpuctx->ctx.nr_counters,
4163 perf_max_counters - perf_reserved_percpu);
4164 cpuctx->max_pertask = mpt;
4165 spin_unlock_irq(&cpuctx->ctx.lock);
4166 }
Ingo Molnar1dce8d92009-05-04 19:23:18 +02004167 spin_unlock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004168
4169 return count;
4170}
4171
4172static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
4173{
4174 return sprintf(buf, "%d\n", perf_overcommit);
4175}
4176
4177static ssize_t
4178perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
4179{
4180 unsigned long val;
4181 int err;
4182
4183 err = strict_strtoul(buf, 10, &val);
4184 if (err)
4185 return err;
4186 if (val > 1)
4187 return -EINVAL;
4188
Ingo Molnar1dce8d92009-05-04 19:23:18 +02004189 spin_lock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004190 perf_overcommit = val;
Ingo Molnar1dce8d92009-05-04 19:23:18 +02004191 spin_unlock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004192
4193 return count;
4194}
4195
4196static SYSDEV_CLASS_ATTR(
4197 reserve_percpu,
4198 0644,
4199 perf_show_reserve_percpu,
4200 perf_set_reserve_percpu
4201 );
4202
4203static SYSDEV_CLASS_ATTR(
4204 overcommit,
4205 0644,
4206 perf_show_overcommit,
4207 perf_set_overcommit
4208 );
4209
4210static struct attribute *perfclass_attrs[] = {
4211 &attr_reserve_percpu.attr,
4212 &attr_overcommit.attr,
4213 NULL
4214};
4215
4216static struct attribute_group perfclass_attr_group = {
4217 .attrs = perfclass_attrs,
4218 .name = "perf_counters",
4219};
4220
4221static int __init perf_counter_sysfs_init(void)
4222{
4223 return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
4224 &perfclass_attr_group);
4225}
4226device_initcall(perf_counter_sysfs_init);