blob: f8390668c39129dfd2a65b6970dcfcd6333e4805 [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 Mackerras25346b932009-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 Mackerras25346b932009-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 Mackerras25346b932009-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
1187static void perf_adjust_freq(struct perf_counter_context *ctx)
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001188{
1189 struct perf_counter *counter;
Peter Zijlstrab23f3322009-06-02 15:13:03 +02001190 u64 interrupts, sample_period;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001191 u64 events, period;
1192 s64 delta;
1193
1194 spin_lock(&ctx->lock);
1195 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1196 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
1197 continue;
1198
Peter Zijlstraa78ac322009-05-25 17:39:05 +02001199 interrupts = counter->hw.interrupts;
1200 counter->hw.interrupts = 0;
1201
1202 if (interrupts == MAX_INTERRUPTS) {
1203 perf_log_throttle(counter, 1);
1204 counter->pmu->unthrottle(counter);
1205 interrupts = 2*sysctl_perf_counter_limit/HZ;
1206 }
1207
Peter Zijlstra0d486962009-06-02 19:22:16 +02001208 if (!counter->attr.freq || !counter->attr.sample_freq)
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001209 continue;
1210
Peter Zijlstrab23f3322009-06-02 15:13:03 +02001211 events = HZ * interrupts * counter->hw.sample_period;
Peter Zijlstra0d486962009-06-02 19:22:16 +02001212 period = div64_u64(events, counter->attr.sample_freq);
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001213
Peter Zijlstrab23f3322009-06-02 15:13:03 +02001214 delta = (s64)(1 + period - counter->hw.sample_period);
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001215 delta >>= 1;
1216
Peter Zijlstrab23f3322009-06-02 15:13:03 +02001217 sample_period = counter->hw.sample_period + delta;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001218
Peter Zijlstrab23f3322009-06-02 15:13:03 +02001219 if (!sample_period)
1220 sample_period = 1;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001221
Peter Zijlstrab23f3322009-06-02 15:13:03 +02001222 perf_log_period(counter, sample_period);
Peter Zijlstra26b119b2009-05-20 12:21:20 +02001223
Peter Zijlstrab23f3322009-06-02 15:13:03 +02001224 counter->hw.sample_period = sample_period;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001225 }
1226 spin_unlock(&ctx->lock);
1227}
1228
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001229/*
1230 * Round-robin a context's counters:
1231 */
1232static void rotate_ctx(struct perf_counter_context *ctx)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001233{
Thomas Gleixner0793a612008-12-04 20:12:29 +01001234 struct perf_counter *counter;
1235
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001236 if (!ctx->nr_counters)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001237 return;
1238
Thomas Gleixner0793a612008-12-04 20:12:29 +01001239 spin_lock(&ctx->lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001240 /*
Ingo Molnar04289bb2008-12-11 08:38:42 +01001241 * Rotate the first entry last (works just fine for group counters too):
Thomas Gleixner0793a612008-12-04 20:12:29 +01001242 */
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001243 perf_disable();
Ingo Molnar04289bb2008-12-11 08:38:42 +01001244 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Peter Zijlstra75564232009-03-13 12:21:29 +01001245 list_move_tail(&counter->list_entry, &ctx->counter_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001246 break;
1247 }
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001248 perf_enable();
Thomas Gleixner0793a612008-12-04 20:12:29 +01001249
1250 spin_unlock(&ctx->lock);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001251}
Thomas Gleixner0793a612008-12-04 20:12:29 +01001252
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001253void perf_counter_task_tick(struct task_struct *curr, int cpu)
1254{
Peter Zijlstra7fc23a52009-05-08 18:52:21 +02001255 struct perf_cpu_context *cpuctx;
1256 struct perf_counter_context *ctx;
1257
1258 if (!atomic_read(&nr_counters))
1259 return;
1260
1261 cpuctx = &per_cpu(perf_cpu_context, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001262 ctx = curr->perf_counter_ctxp;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001263
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001264 perf_adjust_freq(&cpuctx->ctx);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001265 if (ctx)
1266 perf_adjust_freq(ctx);
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001267
Ingo Molnarb82914c2009-05-04 18:54:32 +02001268 perf_counter_cpu_sched_out(cpuctx);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001269 if (ctx)
1270 __perf_counter_task_sched_out(ctx);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001271
Ingo Molnarb82914c2009-05-04 18:54:32 +02001272 rotate_ctx(&cpuctx->ctx);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001273 if (ctx)
1274 rotate_ctx(ctx);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001275
Ingo Molnarb82914c2009-05-04 18:54:32 +02001276 perf_counter_cpu_sched_in(cpuctx, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001277 if (ctx)
1278 perf_counter_task_sched_in(curr, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001279}
1280
1281/*
Thomas Gleixner0793a612008-12-04 20:12:29 +01001282 * Cross CPU call to read the hardware counter
1283 */
Ingo Molnar76715812008-12-17 14:20:28 +01001284static void __read(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001285{
Ingo Molnar621a01e2008-12-11 12:46:46 +01001286 struct perf_counter *counter = info;
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001287 struct perf_counter_context *ctx = counter->ctx;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001288 unsigned long flags;
Ingo Molnar621a01e2008-12-11 12:46:46 +01001289
Peter Zijlstra849691a2009-04-06 11:45:12 +02001290 local_irq_save(flags);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001291 if (ctx->is_active)
Peter Zijlstra4af49982009-04-06 11:45:10 +02001292 update_context_time(ctx);
Robert Richter4aeb0b42009-04-29 12:47:03 +02001293 counter->pmu->read(counter);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001294 update_counter_times(counter);
Peter Zijlstra849691a2009-04-06 11:45:12 +02001295 local_irq_restore(flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001296}
1297
Ingo Molnar04289bb2008-12-11 08:38:42 +01001298static u64 perf_counter_read(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001299{
1300 /*
1301 * If counter is enabled and currently active on a CPU, update the
1302 * value in the counter structure:
1303 */
Ingo Molnar6a930702008-12-11 15:17:03 +01001304 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
Thomas Gleixner0793a612008-12-04 20:12:29 +01001305 smp_call_function_single(counter->oncpu,
Ingo Molnar76715812008-12-17 14:20:28 +01001306 __read, counter, 1);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001307 } else if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
1308 update_counter_times(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001309 }
1310
Ingo Molnaree060942008-12-13 09:00:03 +01001311 return atomic64_read(&counter->count);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001312}
1313
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001314/*
1315 * Initialize the perf_counter context in a task_struct:
1316 */
1317static void
1318__perf_counter_init_context(struct perf_counter_context *ctx,
1319 struct task_struct *task)
1320{
1321 memset(ctx, 0, sizeof(*ctx));
1322 spin_lock_init(&ctx->lock);
1323 mutex_init(&ctx->mutex);
1324 INIT_LIST_HEAD(&ctx->counter_list);
1325 INIT_LIST_HEAD(&ctx->event_list);
1326 atomic_set(&ctx->refcount, 1);
1327 ctx->task = task;
1328}
1329
Thomas Gleixner0793a612008-12-04 20:12:29 +01001330static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
1331{
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001332 struct perf_counter_context *parent_ctx;
Ingo Molnar22a4f652009-06-01 10:13:37 +02001333 struct perf_counter_context *ctx;
1334 struct perf_cpu_context *cpuctx;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001335 struct task_struct *task;
Paul Mackerras25346b932009-06-01 17:48:12 +10001336 unsigned long flags;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001337 int err;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001338
1339 /*
1340 * If cpu is not a wildcard then this is a percpu counter:
1341 */
1342 if (cpu != -1) {
1343 /* Must be root to operate on a CPU counter: */
Peter Zijlstra1ccd1542009-04-09 10:53:45 +02001344 if (sysctl_perf_counter_priv && !capable(CAP_SYS_ADMIN))
Thomas Gleixner0793a612008-12-04 20:12:29 +01001345 return ERR_PTR(-EACCES);
1346
1347 if (cpu < 0 || cpu > num_possible_cpus())
1348 return ERR_PTR(-EINVAL);
1349
1350 /*
1351 * We could be clever and allow to attach a counter to an
1352 * offline CPU and activate it when the CPU comes up, but
1353 * that's for later.
1354 */
1355 if (!cpu_isset(cpu, cpu_online_map))
1356 return ERR_PTR(-ENODEV);
1357
1358 cpuctx = &per_cpu(perf_cpu_context, cpu);
1359 ctx = &cpuctx->ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001360 get_ctx(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001361
Thomas Gleixner0793a612008-12-04 20:12:29 +01001362 return ctx;
1363 }
1364
1365 rcu_read_lock();
1366 if (!pid)
1367 task = current;
1368 else
1369 task = find_task_by_vpid(pid);
1370 if (task)
1371 get_task_struct(task);
1372 rcu_read_unlock();
1373
1374 if (!task)
1375 return ERR_PTR(-ESRCH);
1376
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001377 /*
1378 * Can't attach counters to a dying task.
1379 */
1380 err = -ESRCH;
1381 if (task->flags & PF_EXITING)
1382 goto errout;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001383
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001384 /* Reuse ptrace permission checks for now. */
1385 err = -EACCES;
1386 if (!ptrace_may_access(task, PTRACE_MODE_READ))
1387 goto errout;
1388
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001389 retry:
Paul Mackerras25346b932009-06-01 17:48:12 +10001390 ctx = perf_lock_task_context(task, &flags);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001391 if (ctx) {
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001392 parent_ctx = ctx->parent_ctx;
1393 if (parent_ctx) {
1394 put_ctx(parent_ctx);
1395 ctx->parent_ctx = NULL; /* no longer a clone */
1396 }
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001397 /*
1398 * Get an extra reference before dropping the lock so that
1399 * this context won't get freed if the task exits.
1400 */
1401 get_ctx(ctx);
Paul Mackerras25346b932009-06-01 17:48:12 +10001402 spin_unlock_irqrestore(&ctx->lock, flags);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001403 }
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001404
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001405 if (!ctx) {
1406 ctx = kmalloc(sizeof(struct perf_counter_context), GFP_KERNEL);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001407 err = -ENOMEM;
1408 if (!ctx)
1409 goto errout;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001410 __perf_counter_init_context(ctx, task);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001411 get_ctx(ctx);
1412 if (cmpxchg(&task->perf_counter_ctxp, NULL, ctx)) {
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001413 /*
1414 * We raced with some other task; use
1415 * the context they set.
1416 */
1417 kfree(ctx);
Paul Mackerras25346b932009-06-01 17:48:12 +10001418 goto retry;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001419 }
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001420 get_task_struct(task);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001421 }
1422
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001423 put_task_struct(task);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001424 return ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001425
1426 errout:
1427 put_task_struct(task);
1428 return ERR_PTR(err);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001429}
1430
Peter Zijlstra592903c2009-03-13 12:21:36 +01001431static void free_counter_rcu(struct rcu_head *head)
1432{
1433 struct perf_counter *counter;
1434
1435 counter = container_of(head, struct perf_counter, rcu_head);
Peter Zijlstra709e50c2009-06-02 14:13:15 +02001436 if (counter->ns)
1437 put_pid_ns(counter->ns);
Peter Zijlstra592903c2009-03-13 12:21:36 +01001438 kfree(counter);
1439}
1440
Peter Zijlstra925d5192009-03-30 19:07:02 +02001441static void perf_pending_sync(struct perf_counter *counter);
1442
Peter Zijlstraf1600952009-03-19 20:26:16 +01001443static void free_counter(struct perf_counter *counter)
1444{
Peter Zijlstra925d5192009-03-30 19:07:02 +02001445 perf_pending_sync(counter);
1446
Peter Zijlstra7fc23a52009-05-08 18:52:21 +02001447 atomic_dec(&nr_counters);
Peter Zijlstra0d486962009-06-02 19:22:16 +02001448 if (counter->attr.mmap)
Peter Zijlstra60313eb2009-06-04 16:53:44 +02001449 atomic_dec(&nr_mmap_counters);
Peter Zijlstra0d486962009-06-02 19:22:16 +02001450 if (counter->attr.comm)
Peter Zijlstra60313eb2009-06-04 16:53:44 +02001451 atomic_dec(&nr_comm_counters);
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02001452
Peter Zijlstrae077df42009-03-19 20:26:17 +01001453 if (counter->destroy)
1454 counter->destroy(counter);
1455
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001456 put_ctx(counter->ctx);
Peter Zijlstraf1600952009-03-19 20:26:16 +01001457 call_rcu(&counter->rcu_head, free_counter_rcu);
1458}
1459
Thomas Gleixner0793a612008-12-04 20:12:29 +01001460/*
1461 * Called when the last reference to the file is gone.
1462 */
1463static int perf_release(struct inode *inode, struct file *file)
1464{
1465 struct perf_counter *counter = file->private_data;
1466 struct perf_counter_context *ctx = counter->ctx;
1467
1468 file->private_data = NULL;
1469
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001470 WARN_ON_ONCE(ctx->parent_ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001471 mutex_lock(&ctx->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01001472 perf_counter_remove_from_context(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001473 mutex_unlock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001474
Peter Zijlstra082ff5a2009-05-23 18:29:00 +02001475 mutex_lock(&counter->owner->perf_counter_mutex);
1476 list_del_init(&counter->owner_entry);
1477 mutex_unlock(&counter->owner->perf_counter_mutex);
1478 put_task_struct(counter->owner);
1479
Peter Zijlstraf1600952009-03-19 20:26:16 +01001480 free_counter(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001481
1482 return 0;
1483}
1484
1485/*
1486 * Read the performance counter - simple non blocking version for now
1487 */
1488static ssize_t
1489perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
1490{
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001491 u64 values[3];
1492 int n;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001493
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001494 /*
1495 * Return end-of-file for a read on a counter that is in
1496 * error state (i.e. because it was pinned but it couldn't be
1497 * scheduled on to the CPU at some point).
1498 */
1499 if (counter->state == PERF_COUNTER_STATE_ERROR)
1500 return 0;
1501
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001502 WARN_ON_ONCE(counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001503 mutex_lock(&counter->child_mutex);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001504 values[0] = perf_counter_read(counter);
1505 n = 1;
Peter Zijlstra0d486962009-06-02 19:22:16 +02001506 if (counter->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001507 values[n++] = counter->total_time_enabled +
1508 atomic64_read(&counter->child_total_time_enabled);
Peter Zijlstra0d486962009-06-02 19:22:16 +02001509 if (counter->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001510 values[n++] = counter->total_time_running +
1511 atomic64_read(&counter->child_total_time_running);
Peter Zijlstra0d486962009-06-02 19:22:16 +02001512 if (counter->attr.read_format & PERF_FORMAT_ID)
Peter Zijlstra8e5799b2009-06-02 15:08:15 +02001513 values[n++] = counter->id;
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001514 mutex_unlock(&counter->child_mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001515
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001516 if (count < n * sizeof(u64))
1517 return -EINVAL;
1518 count = n * sizeof(u64);
1519
1520 if (copy_to_user(buf, values, count))
1521 return -EFAULT;
1522
1523 return count;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001524}
1525
1526static ssize_t
Thomas Gleixner0793a612008-12-04 20:12:29 +01001527perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
1528{
1529 struct perf_counter *counter = file->private_data;
1530
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001531 return perf_read_hw(counter, buf, count);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001532}
1533
1534static unsigned int perf_poll(struct file *file, poll_table *wait)
1535{
1536 struct perf_counter *counter = file->private_data;
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001537 struct perf_mmap_data *data;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001538 unsigned int events = POLL_HUP;
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001539
1540 rcu_read_lock();
1541 data = rcu_dereference(counter->data);
1542 if (data)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001543 events = atomic_xchg(&data->poll, 0);
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001544 rcu_read_unlock();
Thomas Gleixner0793a612008-12-04 20:12:29 +01001545
1546 poll_wait(file, &counter->waitq, wait);
1547
Thomas Gleixner0793a612008-12-04 20:12:29 +01001548 return events;
1549}
1550
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001551static void perf_counter_reset(struct perf_counter *counter)
1552{
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001553 (void)perf_counter_read(counter);
Paul Mackerras615a3f12009-05-11 15:50:21 +10001554 atomic64_set(&counter->count, 0);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001555 perf_counter_update_userpage(counter);
1556}
1557
1558static void perf_counter_for_each_sibling(struct perf_counter *counter,
1559 void (*func)(struct perf_counter *))
1560{
1561 struct perf_counter_context *ctx = counter->ctx;
1562 struct perf_counter *sibling;
1563
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001564 WARN_ON_ONCE(ctx->parent_ctx);
Peter Zijlstra682076a2009-05-23 18:28:57 +02001565 mutex_lock(&ctx->mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001566 counter = counter->group_leader;
1567
1568 func(counter);
1569 list_for_each_entry(sibling, &counter->sibling_list, list_entry)
1570 func(sibling);
Peter Zijlstra682076a2009-05-23 18:28:57 +02001571 mutex_unlock(&ctx->mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001572}
1573
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001574/*
1575 * Holding the top-level counter's child_mutex means that any
1576 * descendant process that has inherited this counter will block
1577 * in sync_child_counter if it goes to exit, thus satisfying the
1578 * task existence requirements of perf_counter_enable/disable.
1579 */
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001580static void perf_counter_for_each_child(struct perf_counter *counter,
1581 void (*func)(struct perf_counter *))
1582{
1583 struct perf_counter *child;
1584
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001585 WARN_ON_ONCE(counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001586 mutex_lock(&counter->child_mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001587 func(counter);
1588 list_for_each_entry(child, &counter->child_list, child_list)
1589 func(child);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001590 mutex_unlock(&counter->child_mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001591}
1592
1593static void perf_counter_for_each(struct perf_counter *counter,
1594 void (*func)(struct perf_counter *))
1595{
1596 struct perf_counter *child;
1597
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001598 WARN_ON_ONCE(counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001599 mutex_lock(&counter->child_mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001600 perf_counter_for_each_sibling(counter, func);
1601 list_for_each_entry(child, &counter->child_list, child_list)
1602 perf_counter_for_each_sibling(child, func);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001603 mutex_unlock(&counter->child_mutex);
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001604}
1605
Peter Zijlstra08247e32009-06-02 16:46:57 +02001606static int perf_counter_period(struct perf_counter *counter, u64 __user *arg)
1607{
1608 struct perf_counter_context *ctx = counter->ctx;
1609 unsigned long size;
1610 int ret = 0;
1611 u64 value;
1612
Peter Zijlstra0d486962009-06-02 19:22:16 +02001613 if (!counter->attr.sample_period)
Peter Zijlstra08247e32009-06-02 16:46:57 +02001614 return -EINVAL;
1615
1616 size = copy_from_user(&value, arg, sizeof(value));
1617 if (size != sizeof(value))
1618 return -EFAULT;
1619
1620 if (!value)
1621 return -EINVAL;
1622
1623 spin_lock_irq(&ctx->lock);
Peter Zijlstra0d486962009-06-02 19:22:16 +02001624 if (counter->attr.freq) {
Peter Zijlstra08247e32009-06-02 16:46:57 +02001625 if (value > sysctl_perf_counter_limit) {
1626 ret = -EINVAL;
1627 goto unlock;
1628 }
1629
Peter Zijlstra0d486962009-06-02 19:22:16 +02001630 counter->attr.sample_freq = value;
Peter Zijlstra08247e32009-06-02 16:46:57 +02001631 } else {
Peter Zijlstra0d486962009-06-02 19:22:16 +02001632 counter->attr.sample_period = value;
Peter Zijlstra08247e32009-06-02 16:46:57 +02001633 counter->hw.sample_period = value;
1634
1635 perf_log_period(counter, value);
1636 }
1637unlock:
1638 spin_unlock_irq(&ctx->lock);
1639
1640 return ret;
1641}
1642
Paul Mackerrasd859e292009-01-17 18:10:22 +11001643static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1644{
1645 struct perf_counter *counter = file->private_data;
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001646 void (*func)(struct perf_counter *);
1647 u32 flags = arg;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001648
1649 switch (cmd) {
1650 case PERF_COUNTER_IOC_ENABLE:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001651 func = perf_counter_enable;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001652 break;
1653 case PERF_COUNTER_IOC_DISABLE:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001654 func = perf_counter_disable;
Peter Zijlstra79f14642009-04-06 11:45:07 +02001655 break;
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001656 case PERF_COUNTER_IOC_RESET:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001657 func = perf_counter_reset;
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001658 break;
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001659
1660 case PERF_COUNTER_IOC_REFRESH:
1661 return perf_counter_refresh(counter, arg);
Peter Zijlstra08247e32009-06-02 16:46:57 +02001662
1663 case PERF_COUNTER_IOC_PERIOD:
1664 return perf_counter_period(counter, (u64 __user *)arg);
1665
Paul Mackerrasd859e292009-01-17 18:10:22 +11001666 default:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001667 return -ENOTTY;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001668 }
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001669
1670 if (flags & PERF_IOC_FLAG_GROUP)
1671 perf_counter_for_each(counter, func);
1672 else
1673 perf_counter_for_each_child(counter, func);
1674
1675 return 0;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001676}
1677
Peter Zijlstra771d7cd2009-05-25 14:45:26 +02001678int perf_counter_task_enable(void)
1679{
1680 struct perf_counter *counter;
1681
1682 mutex_lock(&current->perf_counter_mutex);
1683 list_for_each_entry(counter, &current->perf_counter_list, owner_entry)
1684 perf_counter_for_each_child(counter, perf_counter_enable);
1685 mutex_unlock(&current->perf_counter_mutex);
1686
1687 return 0;
1688}
1689
1690int perf_counter_task_disable(void)
1691{
1692 struct perf_counter *counter;
1693
1694 mutex_lock(&current->perf_counter_mutex);
1695 list_for_each_entry(counter, &current->perf_counter_list, owner_entry)
1696 perf_counter_for_each_child(counter, perf_counter_disable);
1697 mutex_unlock(&current->perf_counter_mutex);
1698
1699 return 0;
1700}
1701
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001702/*
1703 * Callers need to ensure there can be no nesting of this function, otherwise
1704 * the seqlock logic goes bad. We can not serialize this because the arch
1705 * code calls this from NMI context.
1706 */
1707void perf_counter_update_userpage(struct perf_counter *counter)
Paul Mackerras37d81822009-03-23 18:22:08 +01001708{
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001709 struct perf_counter_mmap_page *userpg;
Ingo Molnar22a4f652009-06-01 10:13:37 +02001710 struct perf_mmap_data *data;
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001711
1712 rcu_read_lock();
1713 data = rcu_dereference(counter->data);
1714 if (!data)
1715 goto unlock;
1716
1717 userpg = data->user_page;
Paul Mackerras37d81822009-03-23 18:22:08 +01001718
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001719 /*
1720 * Disable preemption so as to not let the corresponding user-space
1721 * spin too long if we get preempted.
1722 */
1723 preempt_disable();
Paul Mackerras37d81822009-03-23 18:22:08 +01001724 ++userpg->lock;
Peter Zijlstra92f22a32009-04-02 11:12:04 +02001725 barrier();
Paul Mackerras37d81822009-03-23 18:22:08 +01001726 userpg->index = counter->hw.idx;
1727 userpg->offset = atomic64_read(&counter->count);
1728 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
1729 userpg->offset -= atomic64_read(&counter->hw.prev_count);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001730
Peter Zijlstra92f22a32009-04-02 11:12:04 +02001731 barrier();
Paul Mackerras37d81822009-03-23 18:22:08 +01001732 ++userpg->lock;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001733 preempt_enable();
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001734unlock:
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001735 rcu_read_unlock();
Paul Mackerras37d81822009-03-23 18:22:08 +01001736}
1737
1738static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1739{
1740 struct perf_counter *counter = vma->vm_file->private_data;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001741 struct perf_mmap_data *data;
1742 int ret = VM_FAULT_SIGBUS;
Paul Mackerras37d81822009-03-23 18:22:08 +01001743
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001744 rcu_read_lock();
1745 data = rcu_dereference(counter->data);
1746 if (!data)
1747 goto unlock;
Paul Mackerras37d81822009-03-23 18:22:08 +01001748
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001749 if (vmf->pgoff == 0) {
1750 vmf->page = virt_to_page(data->user_page);
1751 } else {
1752 int nr = vmf->pgoff - 1;
1753
1754 if ((unsigned)nr > data->nr_pages)
1755 goto unlock;
1756
1757 vmf->page = virt_to_page(data->data_pages[nr]);
1758 }
Paul Mackerras37d81822009-03-23 18:22:08 +01001759 get_page(vmf->page);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001760 ret = 0;
1761unlock:
1762 rcu_read_unlock();
1763
1764 return ret;
1765}
1766
1767static int perf_mmap_data_alloc(struct perf_counter *counter, int nr_pages)
1768{
1769 struct perf_mmap_data *data;
1770 unsigned long size;
1771 int i;
1772
1773 WARN_ON(atomic_read(&counter->mmap_count));
1774
1775 size = sizeof(struct perf_mmap_data);
1776 size += nr_pages * sizeof(void *);
1777
1778 data = kzalloc(size, GFP_KERNEL);
1779 if (!data)
1780 goto fail;
1781
1782 data->user_page = (void *)get_zeroed_page(GFP_KERNEL);
1783 if (!data->user_page)
1784 goto fail_user_page;
1785
1786 for (i = 0; i < nr_pages; i++) {
1787 data->data_pages[i] = (void *)get_zeroed_page(GFP_KERNEL);
1788 if (!data->data_pages[i])
1789 goto fail_data_pages;
1790 }
1791
1792 data->nr_pages = nr_pages;
Peter Zijlstra22c15582009-05-05 17:50:25 +02001793 atomic_set(&data->lock, -1);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001794
1795 rcu_assign_pointer(counter->data, data);
1796
Paul Mackerras37d81822009-03-23 18:22:08 +01001797 return 0;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001798
1799fail_data_pages:
1800 for (i--; i >= 0; i--)
1801 free_page((unsigned long)data->data_pages[i]);
1802
1803 free_page((unsigned long)data->user_page);
1804
1805fail_user_page:
1806 kfree(data);
1807
1808fail:
1809 return -ENOMEM;
1810}
1811
1812static void __perf_mmap_data_free(struct rcu_head *rcu_head)
1813{
Ingo Molnar22a4f652009-06-01 10:13:37 +02001814 struct perf_mmap_data *data;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001815 int i;
1816
Ingo Molnar22a4f652009-06-01 10:13:37 +02001817 data = container_of(rcu_head, struct perf_mmap_data, rcu_head);
1818
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001819 free_page((unsigned long)data->user_page);
1820 for (i = 0; i < data->nr_pages; i++)
1821 free_page((unsigned long)data->data_pages[i]);
1822 kfree(data);
1823}
1824
1825static void perf_mmap_data_free(struct perf_counter *counter)
1826{
1827 struct perf_mmap_data *data = counter->data;
1828
1829 WARN_ON(atomic_read(&counter->mmap_count));
1830
1831 rcu_assign_pointer(counter->data, NULL);
1832 call_rcu(&data->rcu_head, __perf_mmap_data_free);
1833}
1834
1835static void perf_mmap_open(struct vm_area_struct *vma)
1836{
1837 struct perf_counter *counter = vma->vm_file->private_data;
1838
1839 atomic_inc(&counter->mmap_count);
1840}
1841
1842static void perf_mmap_close(struct vm_area_struct *vma)
1843{
1844 struct perf_counter *counter = vma->vm_file->private_data;
1845
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001846 WARN_ON_ONCE(counter->ctx->parent_ctx);
Ingo Molnar22a4f652009-06-01 10:13:37 +02001847 if (atomic_dec_and_mutex_lock(&counter->mmap_count, &counter->mmap_mutex)) {
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001848 struct user_struct *user = current_user();
1849
1850 atomic_long_sub(counter->data->nr_pages + 1, &user->locked_vm);
Peter Zijlstrac5078f72009-05-05 17:50:24 +02001851 vma->vm_mm->locked_vm -= counter->data->nr_locked;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001852 perf_mmap_data_free(counter);
1853 mutex_unlock(&counter->mmap_mutex);
1854 }
Paul Mackerras37d81822009-03-23 18:22:08 +01001855}
1856
1857static struct vm_operations_struct perf_mmap_vmops = {
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001858 .open = perf_mmap_open,
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001859 .close = perf_mmap_close,
Paul Mackerras37d81822009-03-23 18:22:08 +01001860 .fault = perf_mmap_fault,
1861};
1862
1863static int perf_mmap(struct file *file, struct vm_area_struct *vma)
1864{
1865 struct perf_counter *counter = file->private_data;
Ingo Molnar22a4f652009-06-01 10:13:37 +02001866 unsigned long user_locked, user_lock_limit;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001867 struct user_struct *user = current_user();
Ingo Molnar22a4f652009-06-01 10:13:37 +02001868 unsigned long locked, lock_limit;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001869 unsigned long vma_size;
1870 unsigned long nr_pages;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001871 long user_extra, extra;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001872 int ret = 0;
Paul Mackerras37d81822009-03-23 18:22:08 +01001873
1874 if (!(vma->vm_flags & VM_SHARED) || (vma->vm_flags & VM_WRITE))
1875 return -EINVAL;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001876
1877 vma_size = vma->vm_end - vma->vm_start;
1878 nr_pages = (vma_size / PAGE_SIZE) - 1;
1879
Peter Zijlstra7730d862009-03-25 12:48:31 +01001880 /*
1881 * If we have data pages ensure they're a power-of-two number, so we
1882 * can do bitmasks instead of modulo.
1883 */
1884 if (nr_pages != 0 && !is_power_of_2(nr_pages))
Paul Mackerras37d81822009-03-23 18:22:08 +01001885 return -EINVAL;
1886
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001887 if (vma_size != PAGE_SIZE * (1 + nr_pages))
Paul Mackerras37d81822009-03-23 18:22:08 +01001888 return -EINVAL;
1889
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001890 if (vma->vm_pgoff != 0)
1891 return -EINVAL;
Paul Mackerras37d81822009-03-23 18:22:08 +01001892
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001893 WARN_ON_ONCE(counter->ctx->parent_ctx);
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001894 mutex_lock(&counter->mmap_mutex);
1895 if (atomic_inc_not_zero(&counter->mmap_count)) {
1896 if (nr_pages != counter->data->nr_pages)
1897 ret = -EINVAL;
1898 goto unlock;
1899 }
1900
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001901 user_extra = nr_pages + 1;
1902 user_lock_limit = sysctl_perf_counter_mlock >> (PAGE_SHIFT - 10);
Ingo Molnara3862d32009-05-24 09:02:37 +02001903
1904 /*
1905 * Increase the limit linearly with more CPUs:
1906 */
1907 user_lock_limit *= num_online_cpus();
1908
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001909 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
Peter Zijlstrac5078f72009-05-05 17:50:24 +02001910
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001911 extra = 0;
1912 if (user_locked > user_lock_limit)
1913 extra = user_locked - user_lock_limit;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001914
1915 lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
1916 lock_limit >>= PAGE_SHIFT;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001917 locked = vma->vm_mm->locked_vm + extra;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001918
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001919 if ((locked > lock_limit) && !capable(CAP_IPC_LOCK)) {
1920 ret = -EPERM;
1921 goto unlock;
1922 }
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001923
1924 WARN_ON(counter->data);
1925 ret = perf_mmap_data_alloc(counter, nr_pages);
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001926 if (ret)
1927 goto unlock;
1928
1929 atomic_set(&counter->mmap_count, 1);
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001930 atomic_long_add(user_extra, &user->locked_vm);
Peter Zijlstrac5078f72009-05-05 17:50:24 +02001931 vma->vm_mm->locked_vm += extra;
1932 counter->data->nr_locked = extra;
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001933unlock:
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001934 mutex_unlock(&counter->mmap_mutex);
Paul Mackerras37d81822009-03-23 18:22:08 +01001935
1936 vma->vm_flags &= ~VM_MAYWRITE;
1937 vma->vm_flags |= VM_RESERVED;
1938 vma->vm_ops = &perf_mmap_vmops;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001939
1940 return ret;
Paul Mackerras37d81822009-03-23 18:22:08 +01001941}
1942
Peter Zijlstra3c446b3d2009-04-06 11:45:01 +02001943static int perf_fasync(int fd, struct file *filp, int on)
1944{
Peter Zijlstra3c446b3d2009-04-06 11:45:01 +02001945 struct inode *inode = filp->f_path.dentry->d_inode;
Ingo Molnar22a4f652009-06-01 10:13:37 +02001946 struct perf_counter *counter = filp->private_data;
Peter Zijlstra3c446b3d2009-04-06 11:45:01 +02001947 int retval;
1948
1949 mutex_lock(&inode->i_mutex);
1950 retval = fasync_helper(fd, filp, on, &counter->fasync);
1951 mutex_unlock(&inode->i_mutex);
1952
1953 if (retval < 0)
1954 return retval;
1955
1956 return 0;
1957}
1958
Thomas Gleixner0793a612008-12-04 20:12:29 +01001959static const struct file_operations perf_fops = {
1960 .release = perf_release,
1961 .read = perf_read,
1962 .poll = perf_poll,
Paul Mackerrasd859e292009-01-17 18:10:22 +11001963 .unlocked_ioctl = perf_ioctl,
1964 .compat_ioctl = perf_ioctl,
Paul Mackerras37d81822009-03-23 18:22:08 +01001965 .mmap = perf_mmap,
Peter Zijlstra3c446b3d2009-04-06 11:45:01 +02001966 .fasync = perf_fasync,
Thomas Gleixner0793a612008-12-04 20:12:29 +01001967};
1968
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001969/*
Peter Zijlstra925d5192009-03-30 19:07:02 +02001970 * Perf counter wakeup
1971 *
1972 * If there's data, ensure we set the poll() state and publish everything
1973 * to user-space before waking everybody up.
1974 */
1975
1976void perf_counter_wakeup(struct perf_counter *counter)
1977{
Peter Zijlstra925d5192009-03-30 19:07:02 +02001978 wake_up_all(&counter->waitq);
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02001979
1980 if (counter->pending_kill) {
1981 kill_fasync(&counter->fasync, SIGIO, counter->pending_kill);
1982 counter->pending_kill = 0;
1983 }
Peter Zijlstra925d5192009-03-30 19:07:02 +02001984}
1985
1986/*
1987 * Pending wakeups
1988 *
1989 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
1990 *
1991 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
1992 * single linked list and use cmpxchg() to add entries lockless.
1993 */
1994
Peter Zijlstra79f14642009-04-06 11:45:07 +02001995static void perf_pending_counter(struct perf_pending_entry *entry)
1996{
1997 struct perf_counter *counter = container_of(entry,
1998 struct perf_counter, pending);
1999
2000 if (counter->pending_disable) {
2001 counter->pending_disable = 0;
2002 perf_counter_disable(counter);
2003 }
2004
2005 if (counter->pending_wakeup) {
2006 counter->pending_wakeup = 0;
2007 perf_counter_wakeup(counter);
2008 }
2009}
2010
Peter Zijlstra671dec52009-04-06 11:45:02 +02002011#define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
Peter Zijlstra925d5192009-03-30 19:07:02 +02002012
Peter Zijlstra671dec52009-04-06 11:45:02 +02002013static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = {
Peter Zijlstra925d5192009-03-30 19:07:02 +02002014 PENDING_TAIL,
2015};
2016
Peter Zijlstra671dec52009-04-06 11:45:02 +02002017static void perf_pending_queue(struct perf_pending_entry *entry,
2018 void (*func)(struct perf_pending_entry *))
Peter Zijlstra925d5192009-03-30 19:07:02 +02002019{
Peter Zijlstra671dec52009-04-06 11:45:02 +02002020 struct perf_pending_entry **head;
Peter Zijlstra925d5192009-03-30 19:07:02 +02002021
Peter Zijlstra671dec52009-04-06 11:45:02 +02002022 if (cmpxchg(&entry->next, NULL, PENDING_TAIL) != NULL)
Peter Zijlstra925d5192009-03-30 19:07:02 +02002023 return;
2024
Peter Zijlstra671dec52009-04-06 11:45:02 +02002025 entry->func = func;
2026
2027 head = &get_cpu_var(perf_pending_head);
Peter Zijlstra925d5192009-03-30 19:07:02 +02002028
2029 do {
Peter Zijlstra671dec52009-04-06 11:45:02 +02002030 entry->next = *head;
2031 } while (cmpxchg(head, entry->next, entry) != entry->next);
Peter Zijlstra925d5192009-03-30 19:07:02 +02002032
2033 set_perf_counter_pending();
2034
Peter Zijlstra671dec52009-04-06 11:45:02 +02002035 put_cpu_var(perf_pending_head);
Peter Zijlstra925d5192009-03-30 19:07:02 +02002036}
2037
2038static int __perf_pending_run(void)
2039{
Peter Zijlstra671dec52009-04-06 11:45:02 +02002040 struct perf_pending_entry *list;
Peter Zijlstra925d5192009-03-30 19:07:02 +02002041 int nr = 0;
2042
Peter Zijlstra671dec52009-04-06 11:45:02 +02002043 list = xchg(&__get_cpu_var(perf_pending_head), PENDING_TAIL);
Peter Zijlstra925d5192009-03-30 19:07:02 +02002044 while (list != PENDING_TAIL) {
Peter Zijlstra671dec52009-04-06 11:45:02 +02002045 void (*func)(struct perf_pending_entry *);
2046 struct perf_pending_entry *entry = list;
Peter Zijlstra925d5192009-03-30 19:07:02 +02002047
2048 list = list->next;
2049
Peter Zijlstra671dec52009-04-06 11:45:02 +02002050 func = entry->func;
2051 entry->next = NULL;
Peter Zijlstra925d5192009-03-30 19:07:02 +02002052 /*
2053 * Ensure we observe the unqueue before we issue the wakeup,
2054 * so that we won't be waiting forever.
2055 * -- see perf_not_pending().
2056 */
2057 smp_wmb();
2058
Peter Zijlstra671dec52009-04-06 11:45:02 +02002059 func(entry);
Peter Zijlstra925d5192009-03-30 19:07:02 +02002060 nr++;
2061 }
2062
2063 return nr;
2064}
2065
2066static inline int perf_not_pending(struct perf_counter *counter)
2067{
2068 /*
2069 * If we flush on whatever cpu we run, there is a chance we don't
2070 * need to wait.
2071 */
2072 get_cpu();
2073 __perf_pending_run();
2074 put_cpu();
2075
2076 /*
2077 * Ensure we see the proper queue state before going to sleep
2078 * so that we do not miss the wakeup. -- see perf_pending_handle()
2079 */
2080 smp_rmb();
Peter Zijlstra671dec52009-04-06 11:45:02 +02002081 return counter->pending.next == NULL;
Peter Zijlstra925d5192009-03-30 19:07:02 +02002082}
2083
2084static void perf_pending_sync(struct perf_counter *counter)
2085{
2086 wait_event(counter->waitq, perf_not_pending(counter));
2087}
2088
2089void perf_counter_do_pending(void)
2090{
2091 __perf_pending_run();
2092}
2093
2094/*
Peter Zijlstra394ee072009-03-30 19:07:14 +02002095 * Callchain support -- arch specific
2096 */
2097
Peter Zijlstra9c03d882009-04-06 11:45:00 +02002098__weak struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
Peter Zijlstra394ee072009-03-30 19:07:14 +02002099{
2100 return NULL;
2101}
2102
2103/*
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002104 * Output
2105 */
2106
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002107struct perf_output_handle {
2108 struct perf_counter *counter;
2109 struct perf_mmap_data *data;
Peter Zijlstra8e3747c2009-06-02 16:16:02 +02002110 unsigned long head;
2111 unsigned long offset;
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002112 int nmi;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002113 int overflow;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002114 int locked;
2115 unsigned long flags;
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002116};
2117
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002118static void perf_output_wakeup(struct perf_output_handle *handle)
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002119{
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002120 atomic_set(&handle->data->poll, POLL_IN);
2121
Peter Zijlstra671dec52009-04-06 11:45:02 +02002122 if (handle->nmi) {
Peter Zijlstra79f14642009-04-06 11:45:07 +02002123 handle->counter->pending_wakeup = 1;
Peter Zijlstra671dec52009-04-06 11:45:02 +02002124 perf_pending_queue(&handle->counter->pending,
Peter Zijlstra79f14642009-04-06 11:45:07 +02002125 perf_pending_counter);
Peter Zijlstra671dec52009-04-06 11:45:02 +02002126 } else
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002127 perf_counter_wakeup(handle->counter);
2128}
2129
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002130/*
2131 * Curious locking construct.
2132 *
2133 * We need to ensure a later event doesn't publish a head when a former
2134 * event isn't done writing. However since we need to deal with NMIs we
2135 * cannot fully serialize things.
2136 *
2137 * What we do is serialize between CPUs so we only have to deal with NMI
2138 * nesting on a single CPU.
2139 *
2140 * We only publish the head (and generate a wakeup) when the outer-most
2141 * event completes.
2142 */
2143static void perf_output_lock(struct perf_output_handle *handle)
2144{
2145 struct perf_mmap_data *data = handle->data;
2146 int cpu;
2147
2148 handle->locked = 0;
2149
2150 local_irq_save(handle->flags);
2151 cpu = smp_processor_id();
2152
2153 if (in_nmi() && atomic_read(&data->lock) == cpu)
2154 return;
2155
Peter Zijlstra22c15582009-05-05 17:50:25 +02002156 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002157 cpu_relax();
2158
2159 handle->locked = 1;
2160}
2161
2162static void perf_output_unlock(struct perf_output_handle *handle)
2163{
2164 struct perf_mmap_data *data = handle->data;
Peter Zijlstra8e3747c2009-06-02 16:16:02 +02002165 unsigned long head;
2166 int cpu;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002167
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002168 data->done_head = data->head;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002169
2170 if (!handle->locked)
2171 goto out;
2172
2173again:
2174 /*
2175 * The xchg implies a full barrier that ensures all writes are done
2176 * before we publish the new head, matched by a rmb() in userspace when
2177 * reading this position.
2178 */
Peter Zijlstra8e3747c2009-06-02 16:16:02 +02002179 while ((head = atomic_long_xchg(&data->done_head, 0)))
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002180 data->user_page->data_head = head;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002181
2182 /*
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002183 * NMI can happen here, which means we can miss a done_head update.
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002184 */
2185
Peter Zijlstra22c15582009-05-05 17:50:25 +02002186 cpu = atomic_xchg(&data->lock, -1);
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002187 WARN_ON_ONCE(cpu != smp_processor_id());
2188
2189 /*
2190 * Therefore we have to validate we did not indeed do so.
2191 */
Peter Zijlstra8e3747c2009-06-02 16:16:02 +02002192 if (unlikely(atomic_long_read(&data->done_head))) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002193 /*
2194 * Since we had it locked, we can lock it again.
2195 */
Peter Zijlstra22c15582009-05-05 17:50:25 +02002196 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002197 cpu_relax();
2198
2199 goto again;
2200 }
2201
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002202 if (atomic_xchg(&data->wakeup, 0))
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002203 perf_output_wakeup(handle);
2204out:
2205 local_irq_restore(handle->flags);
2206}
2207
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002208static int perf_output_begin(struct perf_output_handle *handle,
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002209 struct perf_counter *counter, unsigned int size,
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002210 int nmi, int overflow)
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002211{
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002212 struct perf_mmap_data *data;
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002213 unsigned int offset, head;
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002214
Peter Zijlstra2023b352009-05-05 17:50:26 +02002215 /*
2216 * For inherited counters we send all the output towards the parent.
2217 */
2218 if (counter->parent)
2219 counter = counter->parent;
2220
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002221 rcu_read_lock();
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002222 data = rcu_dereference(counter->data);
2223 if (!data)
2224 goto out;
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002225
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002226 handle->data = data;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002227 handle->counter = counter;
2228 handle->nmi = nmi;
2229 handle->overflow = overflow;
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002230
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002231 if (!data->nr_pages)
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002232 goto fail;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002233
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002234 perf_output_lock(handle);
2235
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002236 do {
Paul Mackerras6dc5f2a2009-06-05 12:36:28 +10002237 offset = head = atomic_long_read(&data->head);
Peter Zijlstrac7138f32009-03-24 13:18:16 +01002238 head += size;
Peter Zijlstra8e3747c2009-06-02 16:16:02 +02002239 } while (atomic_long_cmpxchg(&data->head, offset, head) != offset);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002240
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002241 handle->offset = offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01002242 handle->head = head;
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002243
2244 if ((offset >> PAGE_SHIFT) != (head >> PAGE_SHIFT))
2245 atomic_set(&data->wakeup, 1);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002246
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002247 return 0;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002248
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002249fail:
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002250 perf_output_wakeup(handle);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002251out:
2252 rcu_read_unlock();
2253
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002254 return -ENOSPC;
2255}
2256
2257static void perf_output_copy(struct perf_output_handle *handle,
Peter Zijlstra089dd792009-06-05 14:04:55 +02002258 const void *buf, unsigned int len)
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002259{
2260 unsigned int pages_mask;
2261 unsigned int offset;
2262 unsigned int size;
2263 void **pages;
2264
2265 offset = handle->offset;
2266 pages_mask = handle->data->nr_pages - 1;
2267 pages = handle->data->data_pages;
2268
2269 do {
2270 unsigned int page_offset;
2271 int nr;
2272
2273 nr = (offset >> PAGE_SHIFT) & pages_mask;
2274 page_offset = offset & (PAGE_SIZE - 1);
2275 size = min_t(unsigned int, PAGE_SIZE - page_offset, len);
2276
2277 memcpy(pages[nr] + page_offset, buf, size);
2278
2279 len -= size;
2280 buf += size;
2281 offset += size;
2282 } while (len);
2283
2284 handle->offset = offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01002285
Peter Zijlstra53020fe2009-05-13 21:26:19 +02002286 /*
2287 * Check we didn't copy past our reservation window, taking the
2288 * possible unsigned int wrap into account.
2289 */
Peter Zijlstra8e3747c2009-06-02 16:16:02 +02002290 WARN_ON_ONCE(((long)(handle->head - handle->offset)) < 0);
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002291}
2292
Peter Zijlstra5c148192009-03-25 12:30:23 +01002293#define perf_output_put(handle, x) \
2294 perf_output_copy((handle), &(x), sizeof(x))
2295
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002296static void perf_output_end(struct perf_output_handle *handle)
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002297{
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002298 struct perf_counter *counter = handle->counter;
2299 struct perf_mmap_data *data = handle->data;
2300
Peter Zijlstra0d486962009-06-02 19:22:16 +02002301 int wakeup_events = counter->attr.wakeup_events;
Peter Zijlstrac4578102009-04-02 11:12:01 +02002302
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002303 if (handle->overflow && wakeup_events) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002304 int events = atomic_inc_return(&data->events);
Peter Zijlstrac4578102009-04-02 11:12:01 +02002305 if (events >= wakeup_events) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002306 atomic_sub(wakeup_events, &data->events);
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002307 atomic_set(&data->wakeup, 1);
Peter Zijlstrac4578102009-04-02 11:12:01 +02002308 }
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002309 }
2310
2311 perf_output_unlock(handle);
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002312 rcu_read_unlock();
2313}
2314
Peter Zijlstra709e50c2009-06-02 14:13:15 +02002315static u32 perf_counter_pid(struct perf_counter *counter, struct task_struct *p)
2316{
2317 /*
2318 * only top level counters have the pid namespace they were created in
2319 */
2320 if (counter->parent)
2321 counter = counter->parent;
2322
2323 return task_tgid_nr_ns(p, counter->ns);
2324}
2325
2326static u32 perf_counter_tid(struct perf_counter *counter, struct task_struct *p)
2327{
2328 /*
2329 * only top level counters have the pid namespace they were created in
2330 */
2331 if (counter->parent)
2332 counter = counter->parent;
2333
2334 return task_pid_nr_ns(p, counter->ns);
2335}
2336
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002337static void perf_counter_output(struct perf_counter *counter,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002338 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002339{
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002340 int ret;
Peter Zijlstra0d486962009-06-02 19:22:16 +02002341 u64 sample_type = counter->attr.sample_type;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002342 struct perf_output_handle handle;
2343 struct perf_event_header header;
2344 u64 ip;
Peter Zijlstra5c148192009-03-25 12:30:23 +01002345 struct {
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002346 u32 pid, tid;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002347 } tid_entry;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002348 struct {
Peter Zijlstra8e5799b2009-06-02 15:08:15 +02002349 u64 id;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002350 u64 counter;
2351 } group_entry;
Peter Zijlstra394ee072009-03-30 19:07:14 +02002352 struct perf_callchain_entry *callchain = NULL;
2353 int callchain_size = 0;
Peter Zijlstra339f7c92009-04-06 11:45:06 +02002354 u64 time;
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002355 struct {
2356 u32 cpu, reserved;
2357 } cpu_entry;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002358
Peter Zijlstra6b6e54862009-04-08 15:01:27 +02002359 header.type = 0;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002360 header.size = sizeof(header);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002361
Peter Zijlstra6b6e54862009-04-08 15:01:27 +02002362 header.misc = PERF_EVENT_MISC_OVERFLOW;
Paul Mackerras9d23a902009-05-14 21:48:08 +10002363 header.misc |= perf_misc_flags(regs);
Peter Zijlstra6fab0192009-04-08 15:01:26 +02002364
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002365 if (sample_type & PERF_SAMPLE_IP) {
Paul Mackerras9d23a902009-05-14 21:48:08 +10002366 ip = perf_instruction_pointer(regs);
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002367 header.type |= PERF_SAMPLE_IP;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002368 header.size += sizeof(ip);
2369 }
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002370
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002371 if (sample_type & PERF_SAMPLE_TID) {
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002372 /* namespace issues */
Peter Zijlstra709e50c2009-06-02 14:13:15 +02002373 tid_entry.pid = perf_counter_pid(counter, current);
2374 tid_entry.tid = perf_counter_tid(counter, current);
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002375
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002376 header.type |= PERF_SAMPLE_TID;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002377 header.size += sizeof(tid_entry);
2378 }
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002379
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002380 if (sample_type & PERF_SAMPLE_TIME) {
Peter Zijlstra4d855452009-04-08 15:01:32 +02002381 /*
2382 * Maybe do better on x86 and provide cpu_clock_nmi()
2383 */
2384 time = sched_clock();
2385
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002386 header.type |= PERF_SAMPLE_TIME;
Peter Zijlstra4d855452009-04-08 15:01:32 +02002387 header.size += sizeof(u64);
2388 }
2389
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002390 if (sample_type & PERF_SAMPLE_ADDR) {
2391 header.type |= PERF_SAMPLE_ADDR;
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002392 header.size += sizeof(u64);
2393 }
2394
Peter Zijlstraac4bcf82009-06-05 14:44:52 +02002395 if (sample_type & PERF_SAMPLE_ID) {
2396 header.type |= PERF_SAMPLE_ID;
Peter Zijlstraa85f61a2009-05-08 18:52:23 +02002397 header.size += sizeof(u64);
2398 }
2399
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002400 if (sample_type & PERF_SAMPLE_CPU) {
2401 header.type |= PERF_SAMPLE_CPU;
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002402 header.size += sizeof(cpu_entry);
2403
2404 cpu_entry.cpu = raw_smp_processor_id();
2405 }
2406
Peter Zijlstra689802b2009-06-05 15:05:43 +02002407 if (sample_type & PERF_SAMPLE_PERIOD) {
2408 header.type |= PERF_SAMPLE_PERIOD;
2409 header.size += sizeof(u64);
2410 }
2411
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002412 if (sample_type & PERF_SAMPLE_GROUP) {
2413 header.type |= PERF_SAMPLE_GROUP;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002414 header.size += sizeof(u64) +
2415 counter->nr_siblings * sizeof(group_entry);
2416 }
2417
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002418 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
Peter Zijlstra394ee072009-03-30 19:07:14 +02002419 callchain = perf_callchain(regs);
2420
2421 if (callchain) {
Peter Zijlstra9c03d882009-04-06 11:45:00 +02002422 callchain_size = (1 + callchain->nr) * sizeof(u64);
Peter Zijlstra394ee072009-03-30 19:07:14 +02002423
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002424 header.type |= PERF_SAMPLE_CALLCHAIN;
Peter Zijlstra394ee072009-03-30 19:07:14 +02002425 header.size += callchain_size;
2426 }
2427 }
2428
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002429 ret = perf_output_begin(&handle, counter, header.size, nmi, 1);
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002430 if (ret)
2431 return;
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002432
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002433 perf_output_put(&handle, header);
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002434
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002435 if (sample_type & PERF_SAMPLE_IP)
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002436 perf_output_put(&handle, ip);
2437
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002438 if (sample_type & PERF_SAMPLE_TID)
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002439 perf_output_put(&handle, tid_entry);
2440
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002441 if (sample_type & PERF_SAMPLE_TIME)
Peter Zijlstra4d855452009-04-08 15:01:32 +02002442 perf_output_put(&handle, time);
2443
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002444 if (sample_type & PERF_SAMPLE_ADDR)
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002445 perf_output_put(&handle, addr);
2446
Peter Zijlstraac4bcf82009-06-05 14:44:52 +02002447 if (sample_type & PERF_SAMPLE_ID)
2448 perf_output_put(&handle, counter->id);
Peter Zijlstraa85f61a2009-05-08 18:52:23 +02002449
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002450 if (sample_type & PERF_SAMPLE_CPU)
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002451 perf_output_put(&handle, cpu_entry);
2452
Peter Zijlstra689802b2009-06-05 15:05:43 +02002453 if (sample_type & PERF_SAMPLE_PERIOD)
2454 perf_output_put(&handle, counter->hw.sample_period);
2455
Peter Zijlstra2023b352009-05-05 17:50:26 +02002456 /*
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002457 * XXX PERF_SAMPLE_GROUP vs inherited counters seems difficult.
Peter Zijlstra2023b352009-05-05 17:50:26 +02002458 */
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002459 if (sample_type & PERF_SAMPLE_GROUP) {
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002460 struct perf_counter *leader, *sub;
2461 u64 nr = counter->nr_siblings;
2462
2463 perf_output_put(&handle, nr);
2464
2465 leader = counter->group_leader;
2466 list_for_each_entry(sub, &leader->sibling_list, list_entry) {
2467 if (sub != counter)
Robert Richter4aeb0b42009-04-29 12:47:03 +02002468 sub->pmu->read(sub);
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002469
Peter Zijlstra8e5799b2009-06-02 15:08:15 +02002470 group_entry.id = sub->id;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002471 group_entry.counter = atomic64_read(&sub->count);
2472
2473 perf_output_put(&handle, group_entry);
2474 }
2475 }
2476
Peter Zijlstra394ee072009-03-30 19:07:14 +02002477 if (callchain)
2478 perf_output_copy(&handle, callchain, callchain_size);
2479
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002480 perf_output_end(&handle);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002481}
2482
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002483/*
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002484 * fork tracking
2485 */
2486
2487struct perf_fork_event {
2488 struct task_struct *task;
2489
2490 struct {
2491 struct perf_event_header header;
2492
2493 u32 pid;
2494 u32 ppid;
2495 } event;
2496};
2497
2498static void perf_counter_fork_output(struct perf_counter *counter,
2499 struct perf_fork_event *fork_event)
2500{
2501 struct perf_output_handle handle;
2502 int size = fork_event->event.header.size;
2503 struct task_struct *task = fork_event->task;
2504 int ret = perf_output_begin(&handle, counter, size, 0, 0);
2505
2506 if (ret)
2507 return;
2508
2509 fork_event->event.pid = perf_counter_pid(counter, task);
2510 fork_event->event.ppid = perf_counter_pid(counter, task->real_parent);
2511
2512 perf_output_put(&handle, fork_event->event);
2513 perf_output_end(&handle);
2514}
2515
2516static int perf_counter_fork_match(struct perf_counter *counter)
2517{
Peter Zijlstrad99e9442009-06-04 17:08:58 +02002518 if (counter->attr.comm || counter->attr.mmap)
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002519 return 1;
2520
2521 return 0;
2522}
2523
2524static void perf_counter_fork_ctx(struct perf_counter_context *ctx,
2525 struct perf_fork_event *fork_event)
2526{
2527 struct perf_counter *counter;
2528
2529 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2530 return;
2531
2532 rcu_read_lock();
2533 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2534 if (perf_counter_fork_match(counter))
2535 perf_counter_fork_output(counter, fork_event);
2536 }
2537 rcu_read_unlock();
2538}
2539
2540static void perf_counter_fork_event(struct perf_fork_event *fork_event)
2541{
2542 struct perf_cpu_context *cpuctx;
2543 struct perf_counter_context *ctx;
2544
2545 cpuctx = &get_cpu_var(perf_cpu_context);
2546 perf_counter_fork_ctx(&cpuctx->ctx, fork_event);
2547 put_cpu_var(perf_cpu_context);
2548
2549 rcu_read_lock();
2550 /*
2551 * doesn't really matter which of the child contexts the
2552 * events ends up in.
2553 */
2554 ctx = rcu_dereference(current->perf_counter_ctxp);
2555 if (ctx)
2556 perf_counter_fork_ctx(ctx, fork_event);
2557 rcu_read_unlock();
2558}
2559
2560void perf_counter_fork(struct task_struct *task)
2561{
2562 struct perf_fork_event fork_event;
2563
2564 if (!atomic_read(&nr_comm_counters) &&
Peter Zijlstrad99e9442009-06-04 17:08:58 +02002565 !atomic_read(&nr_mmap_counters))
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002566 return;
2567
2568 fork_event = (struct perf_fork_event){
2569 .task = task,
2570 .event = {
2571 .header = {
2572 .type = PERF_EVENT_FORK,
2573 .size = sizeof(fork_event.event),
2574 },
2575 },
2576 };
2577
2578 perf_counter_fork_event(&fork_event);
2579}
2580
2581/*
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002582 * comm tracking
2583 */
2584
2585struct perf_comm_event {
Ingo Molnar22a4f652009-06-01 10:13:37 +02002586 struct task_struct *task;
2587 char *comm;
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002588 int comm_size;
2589
2590 struct {
2591 struct perf_event_header header;
2592
2593 u32 pid;
2594 u32 tid;
2595 } event;
2596};
2597
2598static void perf_counter_comm_output(struct perf_counter *counter,
2599 struct perf_comm_event *comm_event)
2600{
2601 struct perf_output_handle handle;
2602 int size = comm_event->event.header.size;
2603 int ret = perf_output_begin(&handle, counter, size, 0, 0);
2604
2605 if (ret)
2606 return;
2607
Peter Zijlstra709e50c2009-06-02 14:13:15 +02002608 comm_event->event.pid = perf_counter_pid(counter, comm_event->task);
2609 comm_event->event.tid = perf_counter_tid(counter, comm_event->task);
2610
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002611 perf_output_put(&handle, comm_event->event);
2612 perf_output_copy(&handle, comm_event->comm,
2613 comm_event->comm_size);
2614 perf_output_end(&handle);
2615}
2616
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002617static int perf_counter_comm_match(struct perf_counter *counter)
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002618{
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002619 if (counter->attr.comm)
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002620 return 1;
2621
2622 return 0;
2623}
2624
2625static void perf_counter_comm_ctx(struct perf_counter_context *ctx,
2626 struct perf_comm_event *comm_event)
2627{
2628 struct perf_counter *counter;
2629
2630 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2631 return;
2632
2633 rcu_read_lock();
2634 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002635 if (perf_counter_comm_match(counter))
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002636 perf_counter_comm_output(counter, comm_event);
2637 }
2638 rcu_read_unlock();
2639}
2640
2641static void perf_counter_comm_event(struct perf_comm_event *comm_event)
2642{
2643 struct perf_cpu_context *cpuctx;
Peter Zijlstra665c2142009-05-29 14:51:57 +02002644 struct perf_counter_context *ctx;
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002645 unsigned int size;
2646 char *comm = comm_event->task->comm;
2647
Ingo Molnar888fcee2009-04-09 09:48:22 +02002648 size = ALIGN(strlen(comm)+1, sizeof(u64));
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002649
2650 comm_event->comm = comm;
2651 comm_event->comm_size = size;
2652
2653 comm_event->event.header.size = sizeof(comm_event->event) + size;
2654
2655 cpuctx = &get_cpu_var(perf_cpu_context);
2656 perf_counter_comm_ctx(&cpuctx->ctx, comm_event);
2657 put_cpu_var(perf_cpu_context);
Peter Zijlstra665c2142009-05-29 14:51:57 +02002658
2659 rcu_read_lock();
2660 /*
2661 * doesn't really matter which of the child contexts the
2662 * events ends up in.
2663 */
2664 ctx = rcu_dereference(current->perf_counter_ctxp);
2665 if (ctx)
2666 perf_counter_comm_ctx(ctx, comm_event);
2667 rcu_read_unlock();
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002668}
2669
2670void perf_counter_comm(struct task_struct *task)
2671{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002672 struct perf_comm_event comm_event;
2673
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002674 if (!atomic_read(&nr_comm_counters))
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002675 return;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10002676
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002677 comm_event = (struct perf_comm_event){
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002678 .task = task,
2679 .event = {
2680 .header = { .type = PERF_EVENT_COMM, },
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002681 },
2682 };
2683
2684 perf_counter_comm_event(&comm_event);
2685}
2686
2687/*
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002688 * mmap tracking
2689 */
2690
2691struct perf_mmap_event {
Peter Zijlstra089dd792009-06-05 14:04:55 +02002692 struct vm_area_struct *vma;
2693
2694 const char *file_name;
2695 int file_size;
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002696
2697 struct {
2698 struct perf_event_header header;
2699
2700 u32 pid;
2701 u32 tid;
2702 u64 start;
2703 u64 len;
2704 u64 pgoff;
2705 } event;
2706};
2707
2708static void perf_counter_mmap_output(struct perf_counter *counter,
2709 struct perf_mmap_event *mmap_event)
2710{
2711 struct perf_output_handle handle;
2712 int size = mmap_event->event.header.size;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002713 int ret = perf_output_begin(&handle, counter, size, 0, 0);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002714
2715 if (ret)
2716 return;
2717
Peter Zijlstra709e50c2009-06-02 14:13:15 +02002718 mmap_event->event.pid = perf_counter_pid(counter, current);
2719 mmap_event->event.tid = perf_counter_tid(counter, current);
2720
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002721 perf_output_put(&handle, mmap_event->event);
2722 perf_output_copy(&handle, mmap_event->file_name,
2723 mmap_event->file_size);
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002724 perf_output_end(&handle);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002725}
2726
2727static int perf_counter_mmap_match(struct perf_counter *counter,
2728 struct perf_mmap_event *mmap_event)
2729{
Peter Zijlstrad99e9442009-06-04 17:08:58 +02002730 if (counter->attr.mmap)
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002731 return 1;
2732
2733 return 0;
2734}
2735
2736static void perf_counter_mmap_ctx(struct perf_counter_context *ctx,
2737 struct perf_mmap_event *mmap_event)
2738{
2739 struct perf_counter *counter;
2740
2741 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2742 return;
2743
2744 rcu_read_lock();
2745 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2746 if (perf_counter_mmap_match(counter, mmap_event))
2747 perf_counter_mmap_output(counter, mmap_event);
2748 }
2749 rcu_read_unlock();
2750}
2751
2752static void perf_counter_mmap_event(struct perf_mmap_event *mmap_event)
2753{
2754 struct perf_cpu_context *cpuctx;
Peter Zijlstra665c2142009-05-29 14:51:57 +02002755 struct perf_counter_context *ctx;
Peter Zijlstra089dd792009-06-05 14:04:55 +02002756 struct vm_area_struct *vma = mmap_event->vma;
2757 struct file *file = vma->vm_file;
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002758 unsigned int size;
2759 char tmp[16];
2760 char *buf = NULL;
Peter Zijlstra089dd792009-06-05 14:04:55 +02002761 const char *name;
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002762
2763 if (file) {
2764 buf = kzalloc(PATH_MAX, GFP_KERNEL);
2765 if (!buf) {
2766 name = strncpy(tmp, "//enomem", sizeof(tmp));
2767 goto got_name;
2768 }
Peter Zijlstrad3d21c42009-04-09 10:53:46 +02002769 name = d_path(&file->f_path, buf, PATH_MAX);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002770 if (IS_ERR(name)) {
2771 name = strncpy(tmp, "//toolong", sizeof(tmp));
2772 goto got_name;
2773 }
2774 } else {
Peter Zijlstra089dd792009-06-05 14:04:55 +02002775 name = arch_vma_name(mmap_event->vma);
2776 if (name)
2777 goto got_name;
2778
2779 if (!vma->vm_mm) {
2780 name = strncpy(tmp, "[vdso]", sizeof(tmp));
2781 goto got_name;
2782 }
2783
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002784 name = strncpy(tmp, "//anon", sizeof(tmp));
2785 goto got_name;
2786 }
2787
2788got_name:
Ingo Molnar888fcee2009-04-09 09:48:22 +02002789 size = ALIGN(strlen(name)+1, sizeof(u64));
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002790
2791 mmap_event->file_name = name;
2792 mmap_event->file_size = size;
2793
2794 mmap_event->event.header.size = sizeof(mmap_event->event) + size;
2795
2796 cpuctx = &get_cpu_var(perf_cpu_context);
2797 perf_counter_mmap_ctx(&cpuctx->ctx, mmap_event);
2798 put_cpu_var(perf_cpu_context);
2799
Peter Zijlstra665c2142009-05-29 14:51:57 +02002800 rcu_read_lock();
2801 /*
2802 * doesn't really matter which of the child contexts the
2803 * events ends up in.
2804 */
2805 ctx = rcu_dereference(current->perf_counter_ctxp);
2806 if (ctx)
2807 perf_counter_mmap_ctx(ctx, mmap_event);
2808 rcu_read_unlock();
2809
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002810 kfree(buf);
2811}
2812
Peter Zijlstra089dd792009-06-05 14:04:55 +02002813void __perf_counter_mmap(struct vm_area_struct *vma)
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002814{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002815 struct perf_mmap_event mmap_event;
2816
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002817 if (!atomic_read(&nr_mmap_counters))
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002818 return;
2819
2820 mmap_event = (struct perf_mmap_event){
Peter Zijlstra089dd792009-06-05 14:04:55 +02002821 .vma = vma,
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002822 .event = {
2823 .header = { .type = PERF_EVENT_MMAP, },
Peter Zijlstra089dd792009-06-05 14:04:55 +02002824 .start = vma->vm_start,
2825 .len = vma->vm_end - vma->vm_start,
2826 .pgoff = vma->vm_pgoff,
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002827 },
2828 };
2829
2830 perf_counter_mmap_event(&mmap_event);
2831}
2832
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002833/*
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002834 * Log sample_period changes so that analyzing tools can re-normalize the
Peter Zijlstrae220d2d2009-05-23 18:28:55 +02002835 * event flow.
Peter Zijlstra26b119b2009-05-20 12:21:20 +02002836 */
2837
2838static void perf_log_period(struct perf_counter *counter, u64 period)
2839{
2840 struct perf_output_handle handle;
2841 int ret;
2842
2843 struct {
2844 struct perf_event_header header;
2845 u64 time;
Peter Zijlstra689802b2009-06-05 15:05:43 +02002846 u64 id;
Peter Zijlstra26b119b2009-05-20 12:21:20 +02002847 u64 period;
2848 } freq_event = {
2849 .header = {
2850 .type = PERF_EVENT_PERIOD,
2851 .misc = 0,
2852 .size = sizeof(freq_event),
2853 },
2854 .time = sched_clock(),
Peter Zijlstra689802b2009-06-05 15:05:43 +02002855 .id = counter->id,
Peter Zijlstra26b119b2009-05-20 12:21:20 +02002856 .period = period,
2857 };
2858
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002859 if (counter->hw.sample_period == period)
Peter Zijlstra26b119b2009-05-20 12:21:20 +02002860 return;
2861
2862 ret = perf_output_begin(&handle, counter, sizeof(freq_event), 0, 0);
2863 if (ret)
2864 return;
2865
2866 perf_output_put(&handle, freq_event);
2867 perf_output_end(&handle);
2868}
2869
2870/*
Peter Zijlstraa78ac322009-05-25 17:39:05 +02002871 * IRQ throttle logging
2872 */
2873
2874static void perf_log_throttle(struct perf_counter *counter, int enable)
2875{
2876 struct perf_output_handle handle;
2877 int ret;
2878
2879 struct {
2880 struct perf_event_header header;
2881 u64 time;
2882 } throttle_event = {
2883 .header = {
2884 .type = PERF_EVENT_THROTTLE + 1,
2885 .misc = 0,
2886 .size = sizeof(throttle_event),
2887 },
2888 .time = sched_clock(),
2889 };
2890
Ingo Molnar0127c3e2009-05-25 22:03:26 +02002891 ret = perf_output_begin(&handle, counter, sizeof(throttle_event), 1, 0);
Peter Zijlstraa78ac322009-05-25 17:39:05 +02002892 if (ret)
2893 return;
2894
2895 perf_output_put(&handle, throttle_event);
2896 perf_output_end(&handle);
2897}
2898
2899/*
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002900 * Generic counter overflow handling.
2901 */
2902
2903int perf_counter_overflow(struct perf_counter *counter,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002904 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002905{
Peter Zijlstra79f14642009-04-06 11:45:07 +02002906 int events = atomic_read(&counter->event_limit);
Peter Zijlstraa78ac322009-05-25 17:39:05 +02002907 int throttle = counter->pmu->unthrottle != NULL;
Peter Zijlstra79f14642009-04-06 11:45:07 +02002908 int ret = 0;
2909
Peter Zijlstraa78ac322009-05-25 17:39:05 +02002910 if (!throttle) {
2911 counter->hw.interrupts++;
Ingo Molnar128f0482009-06-03 22:19:36 +02002912 } else {
2913 if (counter->hw.interrupts != MAX_INTERRUPTS) {
2914 counter->hw.interrupts++;
2915 if (HZ*counter->hw.interrupts > (u64)sysctl_perf_counter_limit) {
2916 counter->hw.interrupts = MAX_INTERRUPTS;
2917 perf_log_throttle(counter, 0);
2918 ret = 1;
2919 }
2920 } else {
2921 /*
2922 * Keep re-disabling counters even though on the previous
2923 * pass we disabled it - just in case we raced with a
2924 * sched-in and the counter got enabled again:
2925 */
Peter Zijlstraa78ac322009-05-25 17:39:05 +02002926 ret = 1;
2927 }
2928 }
Peter Zijlstra60db5e02009-05-15 15:19:28 +02002929
Peter Zijlstra2023b352009-05-05 17:50:26 +02002930 /*
2931 * XXX event_limit might not quite work as expected on inherited
2932 * counters
2933 */
2934
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002935 counter->pending_kill = POLL_IN;
Peter Zijlstra79f14642009-04-06 11:45:07 +02002936 if (events && atomic_dec_and_test(&counter->event_limit)) {
2937 ret = 1;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002938 counter->pending_kill = POLL_HUP;
Peter Zijlstra79f14642009-04-06 11:45:07 +02002939 if (nmi) {
2940 counter->pending_disable = 1;
2941 perf_pending_queue(&counter->pending,
2942 perf_pending_counter);
2943 } else
2944 perf_counter_disable(counter);
2945 }
2946
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002947 perf_counter_output(counter, nmi, regs, addr);
Peter Zijlstra79f14642009-04-06 11:45:07 +02002948 return ret;
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002949}
2950
2951/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002952 * Generic software counter infrastructure
2953 */
2954
2955static void perf_swcounter_update(struct perf_counter *counter)
2956{
2957 struct hw_perf_counter *hwc = &counter->hw;
2958 u64 prev, now;
2959 s64 delta;
2960
2961again:
2962 prev = atomic64_read(&hwc->prev_count);
2963 now = atomic64_read(&hwc->count);
2964 if (atomic64_cmpxchg(&hwc->prev_count, prev, now) != prev)
2965 goto again;
2966
2967 delta = now - prev;
2968
2969 atomic64_add(delta, &counter->count);
2970 atomic64_sub(delta, &hwc->period_left);
2971}
2972
2973static void perf_swcounter_set_period(struct perf_counter *counter)
2974{
2975 struct hw_perf_counter *hwc = &counter->hw;
2976 s64 left = atomic64_read(&hwc->period_left);
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002977 s64 period = hwc->sample_period;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002978
2979 if (unlikely(left <= -period)) {
2980 left = period;
2981 atomic64_set(&hwc->period_left, left);
2982 }
2983
2984 if (unlikely(left <= 0)) {
2985 left += period;
2986 atomic64_add(period, &hwc->period_left);
2987 }
2988
2989 atomic64_set(&hwc->prev_count, -left);
2990 atomic64_set(&hwc->count, -left);
2991}
2992
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002993static enum hrtimer_restart perf_swcounter_hrtimer(struct hrtimer *hrtimer)
2994{
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002995 enum hrtimer_restart ret = HRTIMER_RESTART;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002996 struct perf_counter *counter;
2997 struct pt_regs *regs;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02002998 u64 period;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002999
3000 counter = container_of(hrtimer, struct perf_counter, hw.hrtimer);
Robert Richter4aeb0b42009-04-29 12:47:03 +02003001 counter->pmu->read(counter);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003002
3003 regs = get_irq_regs();
3004 /*
3005 * In case we exclude kernel IPs or are somehow not in interrupt
3006 * context, provide the next best thing, the user IP.
3007 */
Peter Zijlstra0d486962009-06-02 19:22:16 +02003008 if ((counter->attr.exclude_kernel || !regs) &&
3009 !counter->attr.exclude_user)
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003010 regs = task_pt_regs(current);
3011
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02003012 if (regs) {
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003013 if (perf_counter_overflow(counter, 0, regs, 0))
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02003014 ret = HRTIMER_NORESTART;
3015 }
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003016
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003017 period = max_t(u64, 10000, counter->hw.sample_period);
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003018 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003019
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02003020 return ret;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003021}
3022
3023static void perf_swcounter_overflow(struct perf_counter *counter,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003024 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003025{
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003026 perf_swcounter_update(counter);
3027 perf_swcounter_set_period(counter);
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003028 if (perf_counter_overflow(counter, nmi, regs, addr))
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02003029 /* soft-disable the counter */
3030 ;
3031
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003032}
3033
Paul Mackerras880ca152009-06-01 17:49:14 +10003034static int perf_swcounter_is_counting(struct perf_counter *counter)
3035{
3036 struct perf_counter_context *ctx;
3037 unsigned long flags;
3038 int count;
3039
3040 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
3041 return 1;
3042
3043 if (counter->state != PERF_COUNTER_STATE_INACTIVE)
3044 return 0;
3045
3046 /*
3047 * If the counter is inactive, it could be just because
3048 * its task is scheduled out, or because it's in a group
3049 * which could not go on the PMU. We want to count in
3050 * the first case but not the second. If the context is
3051 * currently active then an inactive software counter must
3052 * be the second case. If it's not currently active then
3053 * we need to know whether the counter was active when the
3054 * context was last active, which we can determine by
3055 * comparing counter->tstamp_stopped with ctx->time.
3056 *
3057 * We are within an RCU read-side critical section,
3058 * which protects the existence of *ctx.
3059 */
3060 ctx = counter->ctx;
3061 spin_lock_irqsave(&ctx->lock, flags);
3062 count = 1;
3063 /* Re-check state now we have the lock */
3064 if (counter->state < PERF_COUNTER_STATE_INACTIVE ||
3065 counter->ctx->is_active ||
3066 counter->tstamp_stopped < ctx->time)
3067 count = 0;
3068 spin_unlock_irqrestore(&ctx->lock, flags);
3069 return count;
3070}
3071
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003072static int perf_swcounter_match(struct perf_counter *counter,
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003073 enum perf_event_types type,
3074 u32 event, struct pt_regs *regs)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003075{
Paul Mackerras880ca152009-06-01 17:49:14 +10003076 u64 event_config;
3077
3078 event_config = ((u64) type << PERF_COUNTER_TYPE_SHIFT) | event;
3079
3080 if (!perf_swcounter_is_counting(counter))
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003081 return 0;
3082
Peter Zijlstra0d486962009-06-02 19:22:16 +02003083 if (counter->attr.config != event_config)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003084 return 0;
3085
Paul Mackerras3f731ca2009-06-01 17:52:30 +10003086 if (regs) {
Peter Zijlstra0d486962009-06-02 19:22:16 +02003087 if (counter->attr.exclude_user && user_mode(regs))
Paul Mackerras3f731ca2009-06-01 17:52:30 +10003088 return 0;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003089
Peter Zijlstra0d486962009-06-02 19:22:16 +02003090 if (counter->attr.exclude_kernel && !user_mode(regs))
Paul Mackerras3f731ca2009-06-01 17:52:30 +10003091 return 0;
3092 }
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003093
3094 return 1;
3095}
3096
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003097static void perf_swcounter_add(struct perf_counter *counter, u64 nr,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003098 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003099{
3100 int neg = atomic64_add_negative(nr, &counter->hw.count);
Ingo Molnar22a4f652009-06-01 10:13:37 +02003101
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003102 if (counter->hw.sample_period && !neg && regs)
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003103 perf_swcounter_overflow(counter, nmi, regs, addr);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003104}
3105
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003106static void perf_swcounter_ctx_event(struct perf_counter_context *ctx,
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003107 enum perf_event_types type, u32 event,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003108 u64 nr, int nmi, struct pt_regs *regs,
3109 u64 addr)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003110{
3111 struct perf_counter *counter;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003112
Peter Zijlstra01ef09d2009-03-19 20:26:11 +01003113 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003114 return;
3115
Peter Zijlstra592903c2009-03-13 12:21:36 +01003116 rcu_read_lock();
3117 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003118 if (perf_swcounter_match(counter, type, event, regs))
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003119 perf_swcounter_add(counter, nr, nmi, regs, addr);
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003120 }
Peter Zijlstra592903c2009-03-13 12:21:36 +01003121 rcu_read_unlock();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003122}
3123
Peter Zijlstra96f6d442009-03-23 18:22:07 +01003124static int *perf_swcounter_recursion_context(struct perf_cpu_context *cpuctx)
3125{
3126 if (in_nmi())
3127 return &cpuctx->recursion[3];
3128
3129 if (in_irq())
3130 return &cpuctx->recursion[2];
3131
3132 if (in_softirq())
3133 return &cpuctx->recursion[1];
3134
3135 return &cpuctx->recursion[0];
3136}
3137
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003138static void __perf_swcounter_event(enum perf_event_types type, u32 event,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003139 u64 nr, int nmi, struct pt_regs *regs,
3140 u64 addr)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003141{
3142 struct perf_cpu_context *cpuctx = &get_cpu_var(perf_cpu_context);
Peter Zijlstra96f6d442009-03-23 18:22:07 +01003143 int *recursion = perf_swcounter_recursion_context(cpuctx);
Peter Zijlstra665c2142009-05-29 14:51:57 +02003144 struct perf_counter_context *ctx;
Peter Zijlstra96f6d442009-03-23 18:22:07 +01003145
3146 if (*recursion)
3147 goto out;
3148
3149 (*recursion)++;
3150 barrier();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003151
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003152 perf_swcounter_ctx_event(&cpuctx->ctx, type, event,
3153 nr, nmi, regs, addr);
Peter Zijlstra665c2142009-05-29 14:51:57 +02003154 rcu_read_lock();
3155 /*
3156 * doesn't really matter which of the child contexts the
3157 * events ends up in.
3158 */
3159 ctx = rcu_dereference(current->perf_counter_ctxp);
3160 if (ctx)
3161 perf_swcounter_ctx_event(ctx, type, event, nr, nmi, regs, addr);
3162 rcu_read_unlock();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003163
Peter Zijlstra96f6d442009-03-23 18:22:07 +01003164 barrier();
3165 (*recursion)--;
3166
3167out:
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003168 put_cpu_var(perf_cpu_context);
3169}
3170
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003171void
3172perf_swcounter_event(u32 event, u64 nr, int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003173{
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003174 __perf_swcounter_event(PERF_TYPE_SOFTWARE, event, nr, nmi, regs, addr);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003175}
3176
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003177static void perf_swcounter_read(struct perf_counter *counter)
3178{
3179 perf_swcounter_update(counter);
3180}
3181
3182static int perf_swcounter_enable(struct perf_counter *counter)
3183{
3184 perf_swcounter_set_period(counter);
3185 return 0;
3186}
3187
3188static void perf_swcounter_disable(struct perf_counter *counter)
3189{
3190 perf_swcounter_update(counter);
3191}
3192
Robert Richter4aeb0b42009-04-29 12:47:03 +02003193static const struct pmu perf_ops_generic = {
Peter Zijlstraac17dc82009-03-13 12:21:34 +01003194 .enable = perf_swcounter_enable,
3195 .disable = perf_swcounter_disable,
3196 .read = perf_swcounter_read,
3197};
3198
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003199/*
3200 * Software counter: cpu wall time clock
3201 */
3202
Paul Mackerras9abf8a02009-01-09 16:26:43 +11003203static void cpu_clock_perf_counter_update(struct perf_counter *counter)
3204{
3205 int cpu = raw_smp_processor_id();
3206 s64 prev;
3207 u64 now;
3208
3209 now = cpu_clock(cpu);
3210 prev = atomic64_read(&counter->hw.prev_count);
3211 atomic64_set(&counter->hw.prev_count, now);
3212 atomic64_add(now - prev, &counter->count);
3213}
3214
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003215static int cpu_clock_perf_counter_enable(struct perf_counter *counter)
3216{
3217 struct hw_perf_counter *hwc = &counter->hw;
3218 int cpu = raw_smp_processor_id();
3219
3220 atomic64_set(&hwc->prev_count, cpu_clock(cpu));
Peter Zijlstra039fc912009-03-13 16:43:47 +01003221 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
3222 hwc->hrtimer.function = perf_swcounter_hrtimer;
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003223 if (hwc->sample_period) {
3224 u64 period = max_t(u64, 10000, hwc->sample_period);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003225 __hrtimer_start_range_ns(&hwc->hrtimer,
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003226 ns_to_ktime(period), 0,
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003227 HRTIMER_MODE_REL, 0);
3228 }
3229
3230 return 0;
3231}
3232
Ingo Molnar5c92d122008-12-11 13:21:10 +01003233static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
3234{
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003235 if (counter->hw.sample_period)
Peter Zijlstrab986d7e2009-05-20 12:21:21 +02003236 hrtimer_cancel(&counter->hw.hrtimer);
Paul Mackerras9abf8a02009-01-09 16:26:43 +11003237 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01003238}
3239
3240static void cpu_clock_perf_counter_read(struct perf_counter *counter)
3241{
Paul Mackerras9abf8a02009-01-09 16:26:43 +11003242 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01003243}
3244
Robert Richter4aeb0b42009-04-29 12:47:03 +02003245static const struct pmu perf_ops_cpu_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +01003246 .enable = cpu_clock_perf_counter_enable,
3247 .disable = cpu_clock_perf_counter_disable,
3248 .read = cpu_clock_perf_counter_read,
Ingo Molnar5c92d122008-12-11 13:21:10 +01003249};
3250
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01003251/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003252 * Software counter: task time clock
3253 */
3254
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02003255static void task_clock_perf_counter_update(struct perf_counter *counter, u64 now)
Ingo Molnarbae43c92008-12-11 14:03:20 +01003256{
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02003257 u64 prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003258 s64 delta;
Ingo Molnarbae43c92008-12-11 14:03:20 +01003259
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02003260 prev = atomic64_xchg(&counter->hw.prev_count, now);
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003261 delta = now - prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003262 atomic64_add(delta, &counter->count);
Ingo Molnarbae43c92008-12-11 14:03:20 +01003263}
3264
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01003265static int task_clock_perf_counter_enable(struct perf_counter *counter)
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003266{
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003267 struct hw_perf_counter *hwc = &counter->hw;
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02003268 u64 now;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003269
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02003270 now = counter->ctx->time;
3271
3272 atomic64_set(&hwc->prev_count, now);
Peter Zijlstra039fc912009-03-13 16:43:47 +01003273 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
3274 hwc->hrtimer.function = perf_swcounter_hrtimer;
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003275 if (hwc->sample_period) {
3276 u64 period = max_t(u64, 10000, hwc->sample_period);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003277 __hrtimer_start_range_ns(&hwc->hrtimer,
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003278 ns_to_ktime(period), 0,
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003279 HRTIMER_MODE_REL, 0);
3280 }
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01003281
3282 return 0;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003283}
3284
3285static void task_clock_perf_counter_disable(struct perf_counter *counter)
3286{
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003287 if (counter->hw.sample_period)
Peter Zijlstrab986d7e2009-05-20 12:21:21 +02003288 hrtimer_cancel(&counter->hw.hrtimer);
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02003289 task_clock_perf_counter_update(counter, counter->ctx->time);
3290
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003291}
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01003292
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003293static void task_clock_perf_counter_read(struct perf_counter *counter)
3294{
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02003295 u64 time;
3296
3297 if (!in_nmi()) {
3298 update_context_time(counter->ctx);
3299 time = counter->ctx->time;
3300 } else {
3301 u64 now = perf_clock();
3302 u64 delta = now - counter->ctx->timestamp;
3303 time = counter->ctx->time + delta;
3304 }
3305
3306 task_clock_perf_counter_update(counter, time);
Ingo Molnarbae43c92008-12-11 14:03:20 +01003307}
3308
Robert Richter4aeb0b42009-04-29 12:47:03 +02003309static const struct pmu perf_ops_task_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +01003310 .enable = task_clock_perf_counter_enable,
3311 .disable = task_clock_perf_counter_disable,
3312 .read = task_clock_perf_counter_read,
Ingo Molnarbae43c92008-12-11 14:03:20 +01003313};
3314
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003315/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003316 * Software counter: cpu migrations
3317 */
Paul Mackerras3f731ca2009-06-01 17:52:30 +10003318void perf_counter_task_migration(struct task_struct *task, int cpu)
Ingo Molnar6c594c22008-12-14 12:34:15 +01003319{
Paul Mackerras3f731ca2009-06-01 17:52:30 +10003320 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
3321 struct perf_counter_context *ctx;
Paul Mackerras23a185c2009-02-09 22:42:47 +11003322
Paul Mackerras3f731ca2009-06-01 17:52:30 +10003323 perf_swcounter_ctx_event(&cpuctx->ctx, PERF_TYPE_SOFTWARE,
3324 PERF_COUNT_CPU_MIGRATIONS,
3325 1, 1, NULL, 0);
3326
3327 ctx = perf_pin_task_context(task);
3328 if (ctx) {
3329 perf_swcounter_ctx_event(ctx, PERF_TYPE_SOFTWARE,
3330 PERF_COUNT_CPU_MIGRATIONS,
3331 1, 1, NULL, 0);
3332 perf_unpin_context(ctx);
3333 }
Ingo Molnar6c594c22008-12-14 12:34:15 +01003334}
3335
Peter Zijlstrae077df42009-03-19 20:26:17 +01003336#ifdef CONFIG_EVENT_PROFILE
3337void perf_tpcounter_event(int event_id)
3338{
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003339 struct pt_regs *regs = get_irq_regs();
3340
3341 if (!regs)
3342 regs = task_pt_regs(current);
3343
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003344 __perf_swcounter_event(PERF_TYPE_TRACEPOINT, event_id, 1, 1, regs, 0);
Peter Zijlstrae077df42009-03-19 20:26:17 +01003345}
Steven Whitehouseff7b1b42009-04-15 16:55:05 +01003346EXPORT_SYMBOL_GPL(perf_tpcounter_event);
Peter Zijlstrae077df42009-03-19 20:26:17 +01003347
3348extern int ftrace_profile_enable(int);
3349extern void ftrace_profile_disable(int);
3350
3351static void tp_perf_counter_destroy(struct perf_counter *counter)
3352{
Peter Zijlstra0d486962009-06-02 19:22:16 +02003353 ftrace_profile_disable(perf_event_id(&counter->attr));
Peter Zijlstrae077df42009-03-19 20:26:17 +01003354}
3355
Robert Richter4aeb0b42009-04-29 12:47:03 +02003356static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
Peter Zijlstrae077df42009-03-19 20:26:17 +01003357{
Peter Zijlstra0d486962009-06-02 19:22:16 +02003358 int event_id = perf_event_id(&counter->attr);
Peter Zijlstrae077df42009-03-19 20:26:17 +01003359 int ret;
3360
3361 ret = ftrace_profile_enable(event_id);
3362 if (ret)
3363 return NULL;
3364
3365 counter->destroy = tp_perf_counter_destroy;
Peter Zijlstra0d486962009-06-02 19:22:16 +02003366 counter->hw.sample_period = counter->attr.sample_period;
Peter Zijlstrae077df42009-03-19 20:26:17 +01003367
3368 return &perf_ops_generic;
3369}
3370#else
Robert Richter4aeb0b42009-04-29 12:47:03 +02003371static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
Peter Zijlstrae077df42009-03-19 20:26:17 +01003372{
3373 return NULL;
3374}
3375#endif
3376
Robert Richter4aeb0b42009-04-29 12:47:03 +02003377static const struct pmu *sw_perf_counter_init(struct perf_counter *counter)
Ingo Molnar5c92d122008-12-11 13:21:10 +01003378{
Robert Richter4aeb0b42009-04-29 12:47:03 +02003379 const struct pmu *pmu = NULL;
Ingo Molnar5c92d122008-12-11 13:21:10 +01003380
Paul Mackerras0475f9e2009-02-11 14:35:35 +11003381 /*
3382 * Software counters (currently) can't in general distinguish
3383 * between user, kernel and hypervisor events.
3384 * However, context switches and cpu migrations are considered
3385 * to be kernel events, and page faults are never hypervisor
3386 * events.
3387 */
Peter Zijlstra0d486962009-06-02 19:22:16 +02003388 switch (perf_event_id(&counter->attr)) {
Ingo Molnar5c92d122008-12-11 13:21:10 +01003389 case PERF_COUNT_CPU_CLOCK:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003390 pmu = &perf_ops_cpu_clock;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003391
Ingo Molnar5c92d122008-12-11 13:21:10 +01003392 break;
Ingo Molnarbae43c92008-12-11 14:03:20 +01003393 case PERF_COUNT_TASK_CLOCK:
Paul Mackerras23a185c2009-02-09 22:42:47 +11003394 /*
3395 * If the user instantiates this as a per-cpu counter,
3396 * use the cpu_clock counter instead.
3397 */
3398 if (counter->ctx->task)
Robert Richter4aeb0b42009-04-29 12:47:03 +02003399 pmu = &perf_ops_task_clock;
Paul Mackerras23a185c2009-02-09 22:42:47 +11003400 else
Robert Richter4aeb0b42009-04-29 12:47:03 +02003401 pmu = &perf_ops_cpu_clock;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003402
Ingo Molnarbae43c92008-12-11 14:03:20 +01003403 break;
Ingo Molnare06c61a2008-12-14 14:44:31 +01003404 case PERF_COUNT_PAGE_FAULTS:
Peter Zijlstraac17dc82009-03-13 12:21:34 +01003405 case PERF_COUNT_PAGE_FAULTS_MIN:
3406 case PERF_COUNT_PAGE_FAULTS_MAJ:
Ingo Molnar5d6a27d2008-12-14 12:28:33 +01003407 case PERF_COUNT_CONTEXT_SWITCHES:
Ingo Molnar6c594c22008-12-14 12:34:15 +01003408 case PERF_COUNT_CPU_MIGRATIONS:
Paul Mackerras3f731ca2009-06-01 17:52:30 +10003409 pmu = &perf_ops_generic;
Ingo Molnar6c594c22008-12-14 12:34:15 +01003410 break;
Ingo Molnar5c92d122008-12-11 13:21:10 +01003411 }
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003412
Robert Richter4aeb0b42009-04-29 12:47:03 +02003413 return pmu;
Ingo Molnar5c92d122008-12-11 13:21:10 +01003414}
3415
Thomas Gleixner0793a612008-12-04 20:12:29 +01003416/*
3417 * Allocate and initialize a counter structure
3418 */
3419static struct perf_counter *
Peter Zijlstra0d486962009-06-02 19:22:16 +02003420perf_counter_alloc(struct perf_counter_attr *attr,
Ingo Molnar04289bb2008-12-11 08:38:42 +01003421 int cpu,
Paul Mackerras23a185c2009-02-09 22:42:47 +11003422 struct perf_counter_context *ctx,
Ingo Molnar9b51f662008-12-12 13:49:45 +01003423 struct perf_counter *group_leader,
3424 gfp_t gfpflags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003425{
Robert Richter4aeb0b42009-04-29 12:47:03 +02003426 const struct pmu *pmu;
Ingo Molnar621a01e2008-12-11 12:46:46 +01003427 struct perf_counter *counter;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003428 struct hw_perf_counter *hwc;
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003429 long err;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003430
Ingo Molnar9b51f662008-12-12 13:49:45 +01003431 counter = kzalloc(sizeof(*counter), gfpflags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003432 if (!counter)
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003433 return ERR_PTR(-ENOMEM);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003434
Ingo Molnar04289bb2008-12-11 08:38:42 +01003435 /*
3436 * Single counters are their own group leaders, with an
3437 * empty sibling list:
3438 */
3439 if (!group_leader)
3440 group_leader = counter;
3441
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003442 mutex_init(&counter->child_mutex);
3443 INIT_LIST_HEAD(&counter->child_list);
3444
Ingo Molnar04289bb2008-12-11 08:38:42 +01003445 INIT_LIST_HEAD(&counter->list_entry);
Peter Zijlstra592903c2009-03-13 12:21:36 +01003446 INIT_LIST_HEAD(&counter->event_entry);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003447 INIT_LIST_HEAD(&counter->sibling_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003448 init_waitqueue_head(&counter->waitq);
3449
Peter Zijlstra7b732a72009-03-23 18:22:10 +01003450 mutex_init(&counter->mmap_mutex);
3451
Peter Zijlstraa96bbc12009-06-03 14:01:36 +02003452 counter->cpu = cpu;
Peter Zijlstra0d486962009-06-02 19:22:16 +02003453 counter->attr = *attr;
Peter Zijlstraa96bbc12009-06-03 14:01:36 +02003454 counter->group_leader = group_leader;
3455 counter->pmu = NULL;
3456 counter->ctx = ctx;
3457 counter->oncpu = -1;
Ingo Molnar329d8762009-05-26 08:10:00 +02003458
Peter Zijlstraa96bbc12009-06-03 14:01:36 +02003459 counter->ns = get_pid_ns(current->nsproxy->pid_ns);
3460 counter->id = atomic64_inc_return(&perf_counter_id);
3461
3462 counter->state = PERF_COUNTER_STATE_INACTIVE;
3463
Peter Zijlstra0d486962009-06-02 19:22:16 +02003464 if (attr->disabled)
Ingo Molnara86ed502008-12-17 00:43:10 +01003465 counter->state = PERF_COUNTER_STATE_OFF;
3466
Robert Richter4aeb0b42009-04-29 12:47:03 +02003467 pmu = NULL;
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003468
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003469 hwc = &counter->hw;
Peter Zijlstra0d486962009-06-02 19:22:16 +02003470 if (attr->freq && attr->sample_freq)
3471 hwc->sample_period = div64_u64(TICK_NSEC, attr->sample_freq);
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003472 else
Peter Zijlstra0d486962009-06-02 19:22:16 +02003473 hwc->sample_period = attr->sample_period;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003474
Peter Zijlstra2023b352009-05-05 17:50:26 +02003475 /*
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003476 * we currently do not support PERF_SAMPLE_GROUP on inherited counters
Peter Zijlstra2023b352009-05-05 17:50:26 +02003477 */
Peter Zijlstra0d486962009-06-02 19:22:16 +02003478 if (attr->inherit && (attr->sample_type & PERF_SAMPLE_GROUP))
Peter Zijlstra2023b352009-05-05 17:50:26 +02003479 goto done;
3480
Peter Zijlstra0d486962009-06-02 19:22:16 +02003481 if (perf_event_raw(attr)) {
Robert Richter4aeb0b42009-04-29 12:47:03 +02003482 pmu = hw_perf_counter_init(counter);
Peter Zijlstraf4a2deb42009-03-23 18:22:06 +01003483 goto done;
3484 }
3485
Peter Zijlstra0d486962009-06-02 19:22:16 +02003486 switch (perf_event_type(attr)) {
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003487 case PERF_TYPE_HARDWARE:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003488 pmu = hw_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003489 break;
3490
3491 case PERF_TYPE_SOFTWARE:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003492 pmu = sw_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003493 break;
3494
3495 case PERF_TYPE_TRACEPOINT:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003496 pmu = tp_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003497 break;
3498 }
Peter Zijlstraf4a2deb42009-03-23 18:22:06 +01003499done:
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003500 err = 0;
Robert Richter4aeb0b42009-04-29 12:47:03 +02003501 if (!pmu)
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003502 err = -EINVAL;
Robert Richter4aeb0b42009-04-29 12:47:03 +02003503 else if (IS_ERR(pmu))
3504 err = PTR_ERR(pmu);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003505
3506 if (err) {
Peter Zijlstraa96bbc12009-06-03 14:01:36 +02003507 if (counter->ns)
3508 put_pid_ns(counter->ns);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003509 kfree(counter);
3510 return ERR_PTR(err);
3511 }
3512
Robert Richter4aeb0b42009-04-29 12:47:03 +02003513 counter->pmu = pmu;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003514
Peter Zijlstra7fc23a52009-05-08 18:52:21 +02003515 atomic_inc(&nr_counters);
Peter Zijlstra0d486962009-06-02 19:22:16 +02003516 if (counter->attr.mmap)
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003517 atomic_inc(&nr_mmap_counters);
Peter Zijlstra0d486962009-06-02 19:22:16 +02003518 if (counter->attr.comm)
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003519 atomic_inc(&nr_comm_counters);
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02003520
Thomas Gleixner0793a612008-12-04 20:12:29 +01003521 return counter;
3522}
3523
3524/**
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003525 * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
Ingo Molnar9f66a382008-12-10 12:33:23 +01003526 *
Peter Zijlstra0d486962009-06-02 19:22:16 +02003527 * @attr_uptr: event type attributes for monitoring/sampling
Thomas Gleixner0793a612008-12-04 20:12:29 +01003528 * @pid: target pid
Ingo Molnar9f66a382008-12-10 12:33:23 +01003529 * @cpu: target cpu
3530 * @group_fd: group leader counter fd
Thomas Gleixner0793a612008-12-04 20:12:29 +01003531 */
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003532SYSCALL_DEFINE5(perf_counter_open,
Peter Zijlstra0d486962009-06-02 19:22:16 +02003533 const struct perf_counter_attr __user *, attr_uptr,
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003534 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003535{
Ingo Molnar04289bb2008-12-11 08:38:42 +01003536 struct perf_counter *counter, *group_leader;
Peter Zijlstra0d486962009-06-02 19:22:16 +02003537 struct perf_counter_attr attr;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003538 struct perf_counter_context *ctx;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003539 struct file *counter_file = NULL;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003540 struct file *group_file = NULL;
3541 int fput_needed = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003542 int fput_needed2 = 0;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003543 int ret;
3544
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003545 /* for future expandability... */
3546 if (flags)
3547 return -EINVAL;
3548
Peter Zijlstra0d486962009-06-02 19:22:16 +02003549 if (copy_from_user(&attr, attr_uptr, sizeof(attr)) != 0)
Thomas Gleixnereab656a2008-12-08 19:26:59 +01003550 return -EFAULT;
3551
Ingo Molnar04289bb2008-12-11 08:38:42 +01003552 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01003553 * Get the target context (task or percpu):
3554 */
3555 ctx = find_get_context(pid, cpu);
3556 if (IS_ERR(ctx))
3557 return PTR_ERR(ctx);
3558
3559 /*
3560 * Look up the group leader (we will attach this counter to it):
Ingo Molnar04289bb2008-12-11 08:38:42 +01003561 */
3562 group_leader = NULL;
3563 if (group_fd != -1) {
3564 ret = -EINVAL;
3565 group_file = fget_light(group_fd, &fput_needed);
3566 if (!group_file)
Ingo Molnarccff2862008-12-11 11:26:29 +01003567 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003568 if (group_file->f_op != &perf_fops)
Ingo Molnarccff2862008-12-11 11:26:29 +01003569 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003570
3571 group_leader = group_file->private_data;
3572 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01003573 * Do not allow a recursive hierarchy (this new sibling
3574 * becoming part of another group-sibling):
Ingo Molnar04289bb2008-12-11 08:38:42 +01003575 */
Ingo Molnarccff2862008-12-11 11:26:29 +01003576 if (group_leader->group_leader != group_leader)
3577 goto err_put_context;
3578 /*
3579 * Do not allow to attach to a group in a different
3580 * task or CPU context:
3581 */
3582 if (group_leader->ctx != ctx)
3583 goto err_put_context;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11003584 /*
3585 * Only a group leader can be exclusive or pinned
3586 */
Peter Zijlstra0d486962009-06-02 19:22:16 +02003587 if (attr.exclusive || attr.pinned)
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11003588 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003589 }
3590
Peter Zijlstra0d486962009-06-02 19:22:16 +02003591 counter = perf_counter_alloc(&attr, cpu, ctx, group_leader,
Paul Mackerras23a185c2009-02-09 22:42:47 +11003592 GFP_KERNEL);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003593 ret = PTR_ERR(counter);
3594 if (IS_ERR(counter))
Thomas Gleixner0793a612008-12-04 20:12:29 +01003595 goto err_put_context;
3596
Thomas Gleixner0793a612008-12-04 20:12:29 +01003597 ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
3598 if (ret < 0)
Ingo Molnar9b51f662008-12-12 13:49:45 +01003599 goto err_free_put_context;
3600
3601 counter_file = fget_light(ret, &fput_needed2);
3602 if (!counter_file)
3603 goto err_free_put_context;
3604
3605 counter->filp = counter_file;
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003606 WARN_ON_ONCE(ctx->parent_ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003607 mutex_lock(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003608 perf_install_in_context(ctx, counter, cpu);
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003609 ++ctx->generation;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003610 mutex_unlock(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003611
Peter Zijlstra082ff5a2009-05-23 18:29:00 +02003612 counter->owner = current;
3613 get_task_struct(current);
3614 mutex_lock(&current->perf_counter_mutex);
3615 list_add_tail(&counter->owner_entry, &current->perf_counter_list);
3616 mutex_unlock(&current->perf_counter_mutex);
3617
Ingo Molnar9b51f662008-12-12 13:49:45 +01003618 fput_light(counter_file, fput_needed2);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003619
Ingo Molnar04289bb2008-12-11 08:38:42 +01003620out_fput:
3621 fput_light(group_file, fput_needed);
3622
Thomas Gleixner0793a612008-12-04 20:12:29 +01003623 return ret;
3624
Ingo Molnar9b51f662008-12-12 13:49:45 +01003625err_free_put_context:
Thomas Gleixner0793a612008-12-04 20:12:29 +01003626 kfree(counter);
3627
3628err_put_context:
Paul Mackerrasc93f7662009-05-28 22:18:17 +10003629 put_ctx(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003630
Ingo Molnar04289bb2008-12-11 08:38:42 +01003631 goto out_fput;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003632}
3633
Ingo Molnar9b51f662008-12-12 13:49:45 +01003634/*
Ingo Molnar9b51f662008-12-12 13:49:45 +01003635 * inherit a counter from parent task to child task:
3636 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11003637static struct perf_counter *
Ingo Molnar9b51f662008-12-12 13:49:45 +01003638inherit_counter(struct perf_counter *parent_counter,
3639 struct task_struct *parent,
3640 struct perf_counter_context *parent_ctx,
3641 struct task_struct *child,
Paul Mackerrasd859e292009-01-17 18:10:22 +11003642 struct perf_counter *group_leader,
Ingo Molnar9b51f662008-12-12 13:49:45 +01003643 struct perf_counter_context *child_ctx)
3644{
3645 struct perf_counter *child_counter;
3646
Paul Mackerrasd859e292009-01-17 18:10:22 +11003647 /*
3648 * Instead of creating recursive hierarchies of counters,
3649 * we link inherited counters back to the original parent,
3650 * which has a filp for sure, which we use as the reference
3651 * count:
3652 */
3653 if (parent_counter->parent)
3654 parent_counter = parent_counter->parent;
3655
Peter Zijlstra0d486962009-06-02 19:22:16 +02003656 child_counter = perf_counter_alloc(&parent_counter->attr,
Paul Mackerras23a185c2009-02-09 22:42:47 +11003657 parent_counter->cpu, child_ctx,
3658 group_leader, GFP_KERNEL);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003659 if (IS_ERR(child_counter))
3660 return child_counter;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10003661 get_ctx(child_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003662
3663 /*
Paul Mackerras564c2b22009-05-22 14:27:22 +10003664 * Make the child state follow the state of the parent counter,
Peter Zijlstra0d486962009-06-02 19:22:16 +02003665 * not its attr.disabled bit. We hold the parent's mutex,
Ingo Molnar22a4f652009-06-01 10:13:37 +02003666 * so we won't race with perf_counter_{en, dis}able_family.
Paul Mackerras564c2b22009-05-22 14:27:22 +10003667 */
3668 if (parent_counter->state >= PERF_COUNTER_STATE_INACTIVE)
3669 child_counter->state = PERF_COUNTER_STATE_INACTIVE;
3670 else
3671 child_counter->state = PERF_COUNTER_STATE_OFF;
3672
3673 /*
Ingo Molnar9b51f662008-12-12 13:49:45 +01003674 * Link it up in the child's context:
3675 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +11003676 add_counter_to_ctx(child_counter, child_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003677
3678 child_counter->parent = parent_counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003679 /*
3680 * inherit into child's child as well:
3681 */
Peter Zijlstra0d486962009-06-02 19:22:16 +02003682 child_counter->attr.inherit = 1;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003683
3684 /*
3685 * Get a reference to the parent filp - we will fput it
3686 * when the child counter exits. This is safe to do because
3687 * we are in the parent and we know that the filp still
3688 * exists and has a nonzero count:
3689 */
3690 atomic_long_inc(&parent_counter->filp->f_count);
3691
Paul Mackerrasd859e292009-01-17 18:10:22 +11003692 /*
3693 * Link this into the parent counter's child list
3694 */
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003695 WARN_ON_ONCE(parent_counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003696 mutex_lock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003697 list_add_tail(&child_counter->child_list, &parent_counter->child_list);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003698 mutex_unlock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003699
3700 return child_counter;
3701}
3702
3703static int inherit_group(struct perf_counter *parent_counter,
3704 struct task_struct *parent,
3705 struct perf_counter_context *parent_ctx,
3706 struct task_struct *child,
3707 struct perf_counter_context *child_ctx)
3708{
3709 struct perf_counter *leader;
3710 struct perf_counter *sub;
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003711 struct perf_counter *child_ctr;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003712
3713 leader = inherit_counter(parent_counter, parent, parent_ctx,
3714 child, NULL, child_ctx);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003715 if (IS_ERR(leader))
3716 return PTR_ERR(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003717 list_for_each_entry(sub, &parent_counter->sibling_list, list_entry) {
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003718 child_ctr = inherit_counter(sub, parent, parent_ctx,
3719 child, leader, child_ctx);
3720 if (IS_ERR(child_ctr))
3721 return PTR_ERR(child_ctr);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003722 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01003723 return 0;
3724}
3725
Paul Mackerrasd859e292009-01-17 18:10:22 +11003726static void sync_child_counter(struct perf_counter *child_counter,
3727 struct perf_counter *parent_counter)
3728{
Peter Zijlstra8bc20952009-05-15 20:45:59 +02003729 u64 child_val;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003730
Paul Mackerrasd859e292009-01-17 18:10:22 +11003731 child_val = atomic64_read(&child_counter->count);
3732
3733 /*
3734 * Add back the child's count to the parent's count:
3735 */
3736 atomic64_add(child_val, &parent_counter->count);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11003737 atomic64_add(child_counter->total_time_enabled,
3738 &parent_counter->child_total_time_enabled);
3739 atomic64_add(child_counter->total_time_running,
3740 &parent_counter->child_total_time_running);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003741
3742 /*
3743 * Remove this counter from the parent's list
3744 */
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003745 WARN_ON_ONCE(parent_counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003746 mutex_lock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003747 list_del_init(&child_counter->child_list);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003748 mutex_unlock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003749
3750 /*
3751 * Release the parent counter, if this was the last
3752 * reference to it.
3753 */
3754 fput(parent_counter->filp);
3755}
3756
Ingo Molnar9b51f662008-12-12 13:49:45 +01003757static void
Peter Zijlstrabbbee902009-05-29 14:25:58 +02003758__perf_counter_exit_task(struct perf_counter *child_counter,
Ingo Molnar9b51f662008-12-12 13:49:45 +01003759 struct perf_counter_context *child_ctx)
3760{
3761 struct perf_counter *parent_counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003762
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003763 update_counter_times(child_counter);
Peter Zijlstraaa9c67f2009-05-23 18:28:59 +02003764 perf_counter_remove_from_context(child_counter);
Ingo Molnar0cc0c022008-12-14 23:20:36 +01003765
Ingo Molnar9b51f662008-12-12 13:49:45 +01003766 parent_counter = child_counter->parent;
3767 /*
3768 * It can happen that parent exits first, and has counters
3769 * that are still around due to the child reference. These
3770 * counters need to be zapped - but otherwise linger.
3771 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11003772 if (parent_counter) {
3773 sync_child_counter(child_counter, parent_counter);
Peter Zijlstraf1600952009-03-19 20:26:16 +01003774 free_counter(child_counter);
Paul Mackerras4bcf3492009-02-11 13:53:19 +01003775 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01003776}
3777
3778/*
Paul Mackerrasd859e292009-01-17 18:10:22 +11003779 * When a child task exits, feed back counter values to parent counters.
Ingo Molnar9b51f662008-12-12 13:49:45 +01003780 */
3781void perf_counter_exit_task(struct task_struct *child)
3782{
3783 struct perf_counter *child_counter, *tmp;
3784 struct perf_counter_context *child_ctx;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003785 unsigned long flags;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003786
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003787 if (likely(!child->perf_counter_ctxp))
Ingo Molnar9b51f662008-12-12 13:49:45 +01003788 return;
3789
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003790 local_irq_save(flags);
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003791 /*
3792 * We can't reschedule here because interrupts are disabled,
3793 * and either child is current or it is a task that can't be
3794 * scheduled, so we are now safe from rescheduling changing
3795 * our context.
3796 */
3797 child_ctx = child->perf_counter_ctxp;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003798 __perf_counter_task_sched_out(child_ctx);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10003799
3800 /*
3801 * Take the context lock here so that if find_get_context is
3802 * reading child->perf_counter_ctxp, we wait until it has
3803 * incremented the context's refcount before we do put_ctx below.
3804 */
3805 spin_lock(&child_ctx->lock);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003806 child->perf_counter_ctxp = NULL;
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003807 if (child_ctx->parent_ctx) {
3808 /*
3809 * This context is a clone; unclone it so it can't get
3810 * swapped to another process while we're removing all
3811 * the counters from it.
3812 */
3813 put_ctx(child_ctx->parent_ctx);
3814 child_ctx->parent_ctx = NULL;
3815 }
Paul Mackerrasc93f7662009-05-28 22:18:17 +10003816 spin_unlock(&child_ctx->lock);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003817 local_irq_restore(flags);
3818
3819 mutex_lock(&child_ctx->mutex);
3820
Peter Zijlstra8bc20952009-05-15 20:45:59 +02003821again:
Ingo Molnar9b51f662008-12-12 13:49:45 +01003822 list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list,
3823 list_entry)
Peter Zijlstrabbbee902009-05-29 14:25:58 +02003824 __perf_counter_exit_task(child_counter, child_ctx);
Peter Zijlstra8bc20952009-05-15 20:45:59 +02003825
3826 /*
3827 * If the last counter was a group counter, it will have appended all
3828 * its siblings to the list, but we obtained 'tmp' before that which
3829 * will still point to the list head terminating the iteration.
3830 */
3831 if (!list_empty(&child_ctx->counter_list))
3832 goto again;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003833
3834 mutex_unlock(&child_ctx->mutex);
3835
3836 put_ctx(child_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003837}
3838
3839/*
Peter Zijlstrabbbee902009-05-29 14:25:58 +02003840 * free an unexposed, unused context as created by inheritance by
3841 * init_task below, used by fork() in case of fail.
3842 */
3843void perf_counter_free_task(struct task_struct *task)
3844{
3845 struct perf_counter_context *ctx = task->perf_counter_ctxp;
3846 struct perf_counter *counter, *tmp;
3847
3848 if (!ctx)
3849 return;
3850
3851 mutex_lock(&ctx->mutex);
3852again:
3853 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry) {
3854 struct perf_counter *parent = counter->parent;
3855
3856 if (WARN_ON_ONCE(!parent))
3857 continue;
3858
3859 mutex_lock(&parent->child_mutex);
3860 list_del_init(&counter->child_list);
3861 mutex_unlock(&parent->child_mutex);
3862
3863 fput(parent->filp);
3864
3865 list_del_counter(counter, ctx);
3866 free_counter(counter);
3867 }
3868
3869 if (!list_empty(&ctx->counter_list))
3870 goto again;
3871
3872 mutex_unlock(&ctx->mutex);
3873
3874 put_ctx(ctx);
3875}
3876
3877/*
Ingo Molnar9b51f662008-12-12 13:49:45 +01003878 * Initialize the perf_counter context in task_struct
3879 */
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02003880int perf_counter_init_task(struct task_struct *child)
Ingo Molnar9b51f662008-12-12 13:49:45 +01003881{
3882 struct perf_counter_context *child_ctx, *parent_ctx;
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003883 struct perf_counter_context *cloned_ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003884 struct perf_counter *counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003885 struct task_struct *parent = current;
Paul Mackerras564c2b22009-05-22 14:27:22 +10003886 int inherited_all = 1;
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02003887 int ret = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003888
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003889 child->perf_counter_ctxp = NULL;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003890
Peter Zijlstra082ff5a2009-05-23 18:29:00 +02003891 mutex_init(&child->perf_counter_mutex);
3892 INIT_LIST_HEAD(&child->perf_counter_list);
3893
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003894 if (likely(!parent->perf_counter_ctxp))
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02003895 return 0;
3896
Ingo Molnar9b51f662008-12-12 13:49:45 +01003897 /*
3898 * This is executed from the parent task context, so inherit
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003899 * counters that have been marked for cloning.
3900 * First allocate and initialize a context for the child.
Ingo Molnar9b51f662008-12-12 13:49:45 +01003901 */
3902
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003903 child_ctx = kmalloc(sizeof(struct perf_counter_context), GFP_KERNEL);
3904 if (!child_ctx)
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02003905 return -ENOMEM;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003906
3907 __perf_counter_init_context(child_ctx, child);
3908 child->perf_counter_ctxp = child_ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10003909 get_task_struct(child);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003910
Ingo Molnar9b51f662008-12-12 13:49:45 +01003911 /*
Paul Mackerras25346b932009-06-01 17:48:12 +10003912 * If the parent's context is a clone, pin it so it won't get
3913 * swapped under us.
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003914 */
Paul Mackerras25346b932009-06-01 17:48:12 +10003915 parent_ctx = perf_pin_task_context(parent);
3916
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003917 /*
3918 * No need to check if parent_ctx != NULL here; since we saw
3919 * it non-NULL earlier, the only reason for it to become NULL
3920 * is if we exit, and since we're currently in the middle of
3921 * a fork we can't be exiting at the same time.
3922 */
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003923
3924 /*
Ingo Molnar9b51f662008-12-12 13:49:45 +01003925 * Lock the parent list. No need to lock the child - not PID
3926 * hashed yet and not running, so nobody can access it.
3927 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11003928 mutex_lock(&parent_ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003929
3930 /*
3931 * We dont have to disable NMIs - we are only looking at
3932 * the list, not manipulating it:
3933 */
Peter Zijlstrad7b629a2009-05-20 12:21:19 +02003934 list_for_each_entry_rcu(counter, &parent_ctx->event_list, event_entry) {
3935 if (counter != counter->group_leader)
3936 continue;
3937
Peter Zijlstra0d486962009-06-02 19:22:16 +02003938 if (!counter->attr.inherit) {
Paul Mackerras564c2b22009-05-22 14:27:22 +10003939 inherited_all = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003940 continue;
Paul Mackerras564c2b22009-05-22 14:27:22 +10003941 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01003942
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02003943 ret = inherit_group(counter, parent, parent_ctx,
3944 child, child_ctx);
3945 if (ret) {
Paul Mackerras564c2b22009-05-22 14:27:22 +10003946 inherited_all = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003947 break;
Paul Mackerras564c2b22009-05-22 14:27:22 +10003948 }
3949 }
3950
3951 if (inherited_all) {
3952 /*
3953 * Mark the child context as a clone of the parent
3954 * context, or of whatever the parent is a clone of.
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003955 * Note that if the parent is a clone, it could get
3956 * uncloned at any point, but that doesn't matter
3957 * because the list of counters and the generation
3958 * count can't have changed since we took the mutex.
Paul Mackerras564c2b22009-05-22 14:27:22 +10003959 */
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003960 cloned_ctx = rcu_dereference(parent_ctx->parent_ctx);
3961 if (cloned_ctx) {
3962 child_ctx->parent_ctx = cloned_ctx;
Paul Mackerras25346b932009-06-01 17:48:12 +10003963 child_ctx->parent_gen = parent_ctx->parent_gen;
Paul Mackerras564c2b22009-05-22 14:27:22 +10003964 } else {
3965 child_ctx->parent_ctx = parent_ctx;
3966 child_ctx->parent_gen = parent_ctx->generation;
3967 }
3968 get_ctx(child_ctx->parent_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003969 }
3970
Paul Mackerrasd859e292009-01-17 18:10:22 +11003971 mutex_unlock(&parent_ctx->mutex);
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02003972
Paul Mackerras25346b932009-06-01 17:48:12 +10003973 perf_unpin_context(parent_ctx);
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003974
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02003975 return ret;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003976}
3977
Ingo Molnar04289bb2008-12-11 08:38:42 +01003978static void __cpuinit perf_counter_init_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003979{
Ingo Molnar04289bb2008-12-11 08:38:42 +01003980 struct perf_cpu_context *cpuctx;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003981
Ingo Molnar04289bb2008-12-11 08:38:42 +01003982 cpuctx = &per_cpu(perf_cpu_context, cpu);
3983 __perf_counter_init_context(&cpuctx->ctx, NULL);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003984
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003985 spin_lock(&perf_resource_lock);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003986 cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu;
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003987 spin_unlock(&perf_resource_lock);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003988
Paul Mackerras01d02872009-01-14 13:44:19 +11003989 hw_perf_counter_setup(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003990}
3991
3992#ifdef CONFIG_HOTPLUG_CPU
Ingo Molnar04289bb2008-12-11 08:38:42 +01003993static void __perf_counter_exit_cpu(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003994{
3995 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
3996 struct perf_counter_context *ctx = &cpuctx->ctx;
3997 struct perf_counter *counter, *tmp;
3998
Ingo Molnar04289bb2008-12-11 08:38:42 +01003999 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry)
4000 __perf_counter_remove_from_context(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004001}
Ingo Molnar04289bb2008-12-11 08:38:42 +01004002static void perf_counter_exit_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01004003{
Paul Mackerrasd859e292009-01-17 18:10:22 +11004004 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
4005 struct perf_counter_context *ctx = &cpuctx->ctx;
4006
4007 mutex_lock(&ctx->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01004008 smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1);
Paul Mackerrasd859e292009-01-17 18:10:22 +11004009 mutex_unlock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004010}
4011#else
Ingo Molnar04289bb2008-12-11 08:38:42 +01004012static inline void perf_counter_exit_cpu(int cpu) { }
Thomas Gleixner0793a612008-12-04 20:12:29 +01004013#endif
4014
4015static int __cpuinit
4016perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
4017{
4018 unsigned int cpu = (long)hcpu;
4019
4020 switch (action) {
4021
4022 case CPU_UP_PREPARE:
4023 case CPU_UP_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01004024 perf_counter_init_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004025 break;
4026
4027 case CPU_DOWN_PREPARE:
4028 case CPU_DOWN_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01004029 perf_counter_exit_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004030 break;
4031
4032 default:
4033 break;
4034 }
4035
4036 return NOTIFY_OK;
4037}
4038
Paul Mackerrasf38b0822009-06-02 21:05:16 +10004039/*
4040 * This has to have a higher priority than migration_notifier in sched.c.
4041 */
Thomas Gleixner0793a612008-12-04 20:12:29 +01004042static struct notifier_block __cpuinitdata perf_cpu_nb = {
4043 .notifier_call = perf_cpu_notify,
Paul Mackerrasf38b0822009-06-02 21:05:16 +10004044 .priority = 20,
Thomas Gleixner0793a612008-12-04 20:12:29 +01004045};
4046
Ingo Molnar0d905bc2009-05-04 19:13:30 +02004047void __init perf_counter_init(void)
Thomas Gleixner0793a612008-12-04 20:12:29 +01004048{
4049 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
4050 (void *)(long)smp_processor_id());
4051 register_cpu_notifier(&perf_cpu_nb);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004052}
Thomas Gleixner0793a612008-12-04 20:12:29 +01004053
4054static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
4055{
4056 return sprintf(buf, "%d\n", perf_reserved_percpu);
4057}
4058
4059static ssize_t
4060perf_set_reserve_percpu(struct sysdev_class *class,
4061 const char *buf,
4062 size_t count)
4063{
4064 struct perf_cpu_context *cpuctx;
4065 unsigned long val;
4066 int err, cpu, mpt;
4067
4068 err = strict_strtoul(buf, 10, &val);
4069 if (err)
4070 return err;
4071 if (val > perf_max_counters)
4072 return -EINVAL;
4073
Ingo Molnar1dce8d92009-05-04 19:23:18 +02004074 spin_lock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004075 perf_reserved_percpu = val;
4076 for_each_online_cpu(cpu) {
4077 cpuctx = &per_cpu(perf_cpu_context, cpu);
4078 spin_lock_irq(&cpuctx->ctx.lock);
4079 mpt = min(perf_max_counters - cpuctx->ctx.nr_counters,
4080 perf_max_counters - perf_reserved_percpu);
4081 cpuctx->max_pertask = mpt;
4082 spin_unlock_irq(&cpuctx->ctx.lock);
4083 }
Ingo Molnar1dce8d92009-05-04 19:23:18 +02004084 spin_unlock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004085
4086 return count;
4087}
4088
4089static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
4090{
4091 return sprintf(buf, "%d\n", perf_overcommit);
4092}
4093
4094static ssize_t
4095perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
4096{
4097 unsigned long val;
4098 int err;
4099
4100 err = strict_strtoul(buf, 10, &val);
4101 if (err)
4102 return err;
4103 if (val > 1)
4104 return -EINVAL;
4105
Ingo Molnar1dce8d92009-05-04 19:23:18 +02004106 spin_lock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004107 perf_overcommit = val;
Ingo Molnar1dce8d92009-05-04 19:23:18 +02004108 spin_unlock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004109
4110 return count;
4111}
4112
4113static SYSDEV_CLASS_ATTR(
4114 reserve_percpu,
4115 0644,
4116 perf_show_reserve_percpu,
4117 perf_set_reserve_percpu
4118 );
4119
4120static SYSDEV_CLASS_ATTR(
4121 overcommit,
4122 0644,
4123 perf_show_overcommit,
4124 perf_set_overcommit
4125 );
4126
4127static struct attribute *perfclass_attrs[] = {
4128 &attr_reserve_percpu.attr,
4129 &attr_overcommit.attr,
4130 NULL
4131};
4132
4133static struct attribute_group perfclass_attr_group = {
4134 .attrs = perfclass_attrs,
4135 .name = "perf_counters",
4136};
4137
4138static int __init perf_counter_sysfs_init(void)
4139{
4140 return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
4141 &perfclass_attr_group);
4142}
4143device_initcall(perf_counter_sysfs_init);