blob: 5eacaaf3f9cdc3cf95e919d1cd485aea833f836a [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 Zijlstra6a24ed6c2009-06-05 18:01:29 +02001190 struct hw_perf_counter *hwc;
Peter Zijlstrab23f3322009-06-02 15:13:03 +02001191 u64 interrupts, sample_period;
Peter Zijlstra6a24ed6c2009-06-05 18:01:29 +02001192 u64 events, period, freq;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001193 s64 delta;
1194
1195 spin_lock(&ctx->lock);
1196 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1197 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
1198 continue;
1199
Peter Zijlstra6a24ed6c2009-06-05 18:01:29 +02001200 hwc = &counter->hw;
1201
1202 interrupts = hwc->interrupts;
1203 hwc->interrupts = 0;
Peter Zijlstraa78ac322009-05-25 17:39:05 +02001204
1205 if (interrupts == MAX_INTERRUPTS) {
1206 perf_log_throttle(counter, 1);
1207 counter->pmu->unthrottle(counter);
1208 interrupts = 2*sysctl_perf_counter_limit/HZ;
1209 }
1210
Peter Zijlstra0d486962009-06-02 19:22:16 +02001211 if (!counter->attr.freq || !counter->attr.sample_freq)
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001212 continue;
1213
Peter Zijlstra6a24ed6c2009-06-05 18:01:29 +02001214 if (counter->attr.sample_freq < HZ) {
1215 freq = counter->attr.sample_freq;
1216
1217 hwc->freq_count += freq;
1218 hwc->freq_interrupts += interrupts;
1219
1220 if (hwc->freq_count < HZ)
1221 continue;
1222
1223 interrupts = hwc->freq_interrupts;
1224 hwc->freq_interrupts = 0;
1225 hwc->freq_count -= HZ;
1226 } else
1227 freq = HZ;
1228
1229 events = freq * interrupts * hwc->sample_period;
Peter Zijlstra0d486962009-06-02 19:22:16 +02001230 period = div64_u64(events, counter->attr.sample_freq);
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001231
Peter Zijlstra6a24ed6c2009-06-05 18:01:29 +02001232 delta = (s64)(1 + period - hwc->sample_period);
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001233 delta >>= 1;
1234
Peter Zijlstra6a24ed6c2009-06-05 18:01:29 +02001235 sample_period = hwc->sample_period + delta;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001236
Peter Zijlstrab23f3322009-06-02 15:13:03 +02001237 if (!sample_period)
1238 sample_period = 1;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001239
Peter Zijlstrab23f3322009-06-02 15:13:03 +02001240 perf_log_period(counter, sample_period);
Peter Zijlstra26b119b2009-05-20 12:21:20 +02001241
Peter Zijlstra6a24ed6c2009-06-05 18:01:29 +02001242 hwc->sample_period = sample_period;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001243 }
1244 spin_unlock(&ctx->lock);
1245}
1246
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001247/*
1248 * Round-robin a context's counters:
1249 */
1250static void rotate_ctx(struct perf_counter_context *ctx)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001251{
Thomas Gleixner0793a612008-12-04 20:12:29 +01001252 struct perf_counter *counter;
1253
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001254 if (!ctx->nr_counters)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001255 return;
1256
Thomas Gleixner0793a612008-12-04 20:12:29 +01001257 spin_lock(&ctx->lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001258 /*
Ingo Molnar04289bb2008-12-11 08:38:42 +01001259 * Rotate the first entry last (works just fine for group counters too):
Thomas Gleixner0793a612008-12-04 20:12:29 +01001260 */
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001261 perf_disable();
Ingo Molnar04289bb2008-12-11 08:38:42 +01001262 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Peter Zijlstra75564232009-03-13 12:21:29 +01001263 list_move_tail(&counter->list_entry, &ctx->counter_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001264 break;
1265 }
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001266 perf_enable();
Thomas Gleixner0793a612008-12-04 20:12:29 +01001267
1268 spin_unlock(&ctx->lock);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001269}
Thomas Gleixner0793a612008-12-04 20:12:29 +01001270
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001271void perf_counter_task_tick(struct task_struct *curr, int cpu)
1272{
Peter Zijlstra7fc23a52009-05-08 18:52:21 +02001273 struct perf_cpu_context *cpuctx;
1274 struct perf_counter_context *ctx;
1275
1276 if (!atomic_read(&nr_counters))
1277 return;
1278
1279 cpuctx = &per_cpu(perf_cpu_context, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001280 ctx = curr->perf_counter_ctxp;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001281
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001282 perf_adjust_freq(&cpuctx->ctx);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001283 if (ctx)
1284 perf_adjust_freq(ctx);
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001285
Ingo Molnarb82914c2009-05-04 18:54:32 +02001286 perf_counter_cpu_sched_out(cpuctx);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001287 if (ctx)
1288 __perf_counter_task_sched_out(ctx);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001289
Ingo Molnarb82914c2009-05-04 18:54:32 +02001290 rotate_ctx(&cpuctx->ctx);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001291 if (ctx)
1292 rotate_ctx(ctx);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001293
Ingo Molnarb82914c2009-05-04 18:54:32 +02001294 perf_counter_cpu_sched_in(cpuctx, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001295 if (ctx)
1296 perf_counter_task_sched_in(curr, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001297}
1298
1299/*
Thomas Gleixner0793a612008-12-04 20:12:29 +01001300 * Cross CPU call to read the hardware counter
1301 */
Ingo Molnar76715812008-12-17 14:20:28 +01001302static void __read(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001303{
Ingo Molnar621a01e2008-12-11 12:46:46 +01001304 struct perf_counter *counter = info;
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001305 struct perf_counter_context *ctx = counter->ctx;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001306 unsigned long flags;
Ingo Molnar621a01e2008-12-11 12:46:46 +01001307
Peter Zijlstra849691a2009-04-06 11:45:12 +02001308 local_irq_save(flags);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001309 if (ctx->is_active)
Peter Zijlstra4af49982009-04-06 11:45:10 +02001310 update_context_time(ctx);
Robert Richter4aeb0b42009-04-29 12:47:03 +02001311 counter->pmu->read(counter);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001312 update_counter_times(counter);
Peter Zijlstra849691a2009-04-06 11:45:12 +02001313 local_irq_restore(flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001314}
1315
Ingo Molnar04289bb2008-12-11 08:38:42 +01001316static u64 perf_counter_read(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001317{
1318 /*
1319 * If counter is enabled and currently active on a CPU, update the
1320 * value in the counter structure:
1321 */
Ingo Molnar6a930702008-12-11 15:17:03 +01001322 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
Thomas Gleixner0793a612008-12-04 20:12:29 +01001323 smp_call_function_single(counter->oncpu,
Ingo Molnar76715812008-12-17 14:20:28 +01001324 __read, counter, 1);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001325 } else if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
1326 update_counter_times(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001327 }
1328
Ingo Molnaree060942008-12-13 09:00:03 +01001329 return atomic64_read(&counter->count);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001330}
1331
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001332/*
1333 * Initialize the perf_counter context in a task_struct:
1334 */
1335static void
1336__perf_counter_init_context(struct perf_counter_context *ctx,
1337 struct task_struct *task)
1338{
1339 memset(ctx, 0, sizeof(*ctx));
1340 spin_lock_init(&ctx->lock);
1341 mutex_init(&ctx->mutex);
1342 INIT_LIST_HEAD(&ctx->counter_list);
1343 INIT_LIST_HEAD(&ctx->event_list);
1344 atomic_set(&ctx->refcount, 1);
1345 ctx->task = task;
1346}
1347
Thomas Gleixner0793a612008-12-04 20:12:29 +01001348static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
1349{
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001350 struct perf_counter_context *parent_ctx;
Ingo Molnar22a4f652009-06-01 10:13:37 +02001351 struct perf_counter_context *ctx;
1352 struct perf_cpu_context *cpuctx;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001353 struct task_struct *task;
Paul Mackerras25346b932009-06-01 17:48:12 +10001354 unsigned long flags;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001355 int err;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001356
1357 /*
1358 * If cpu is not a wildcard then this is a percpu counter:
1359 */
1360 if (cpu != -1) {
1361 /* Must be root to operate on a CPU counter: */
Peter Zijlstra1ccd1542009-04-09 10:53:45 +02001362 if (sysctl_perf_counter_priv && !capable(CAP_SYS_ADMIN))
Thomas Gleixner0793a612008-12-04 20:12:29 +01001363 return ERR_PTR(-EACCES);
1364
1365 if (cpu < 0 || cpu > num_possible_cpus())
1366 return ERR_PTR(-EINVAL);
1367
1368 /*
1369 * We could be clever and allow to attach a counter to an
1370 * offline CPU and activate it when the CPU comes up, but
1371 * that's for later.
1372 */
1373 if (!cpu_isset(cpu, cpu_online_map))
1374 return ERR_PTR(-ENODEV);
1375
1376 cpuctx = &per_cpu(perf_cpu_context, cpu);
1377 ctx = &cpuctx->ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001378 get_ctx(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001379
Thomas Gleixner0793a612008-12-04 20:12:29 +01001380 return ctx;
1381 }
1382
1383 rcu_read_lock();
1384 if (!pid)
1385 task = current;
1386 else
1387 task = find_task_by_vpid(pid);
1388 if (task)
1389 get_task_struct(task);
1390 rcu_read_unlock();
1391
1392 if (!task)
1393 return ERR_PTR(-ESRCH);
1394
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001395 /*
1396 * Can't attach counters to a dying task.
1397 */
1398 err = -ESRCH;
1399 if (task->flags & PF_EXITING)
1400 goto errout;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001401
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001402 /* Reuse ptrace permission checks for now. */
1403 err = -EACCES;
1404 if (!ptrace_may_access(task, PTRACE_MODE_READ))
1405 goto errout;
1406
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001407 retry:
Paul Mackerras25346b932009-06-01 17:48:12 +10001408 ctx = perf_lock_task_context(task, &flags);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001409 if (ctx) {
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001410 parent_ctx = ctx->parent_ctx;
1411 if (parent_ctx) {
1412 put_ctx(parent_ctx);
1413 ctx->parent_ctx = NULL; /* no longer a clone */
1414 }
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001415 /*
1416 * Get an extra reference before dropping the lock so that
1417 * this context won't get freed if the task exits.
1418 */
1419 get_ctx(ctx);
Paul Mackerras25346b932009-06-01 17:48:12 +10001420 spin_unlock_irqrestore(&ctx->lock, flags);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001421 }
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001422
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001423 if (!ctx) {
1424 ctx = kmalloc(sizeof(struct perf_counter_context), GFP_KERNEL);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001425 err = -ENOMEM;
1426 if (!ctx)
1427 goto errout;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001428 __perf_counter_init_context(ctx, task);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001429 get_ctx(ctx);
1430 if (cmpxchg(&task->perf_counter_ctxp, NULL, ctx)) {
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001431 /*
1432 * We raced with some other task; use
1433 * the context they set.
1434 */
1435 kfree(ctx);
Paul Mackerras25346b932009-06-01 17:48:12 +10001436 goto retry;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001437 }
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001438 get_task_struct(task);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001439 }
1440
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001441 put_task_struct(task);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001442 return ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001443
1444 errout:
1445 put_task_struct(task);
1446 return ERR_PTR(err);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001447}
1448
Peter Zijlstra592903c2009-03-13 12:21:36 +01001449static void free_counter_rcu(struct rcu_head *head)
1450{
1451 struct perf_counter *counter;
1452
1453 counter = container_of(head, struct perf_counter, rcu_head);
Peter Zijlstra709e50c2009-06-02 14:13:15 +02001454 if (counter->ns)
1455 put_pid_ns(counter->ns);
Peter Zijlstra592903c2009-03-13 12:21:36 +01001456 kfree(counter);
1457}
1458
Peter Zijlstra925d5192009-03-30 19:07:02 +02001459static void perf_pending_sync(struct perf_counter *counter);
1460
Peter Zijlstraf1600952009-03-19 20:26:16 +01001461static void free_counter(struct perf_counter *counter)
1462{
Peter Zijlstra925d5192009-03-30 19:07:02 +02001463 perf_pending_sync(counter);
1464
Peter Zijlstra7fc23a52009-05-08 18:52:21 +02001465 atomic_dec(&nr_counters);
Peter Zijlstra0d486962009-06-02 19:22:16 +02001466 if (counter->attr.mmap)
Peter Zijlstra60313eb2009-06-04 16:53:44 +02001467 atomic_dec(&nr_mmap_counters);
Peter Zijlstra0d486962009-06-02 19:22:16 +02001468 if (counter->attr.comm)
Peter Zijlstra60313eb2009-06-04 16:53:44 +02001469 atomic_dec(&nr_comm_counters);
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02001470
Peter Zijlstrae077df42009-03-19 20:26:17 +01001471 if (counter->destroy)
1472 counter->destroy(counter);
1473
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001474 put_ctx(counter->ctx);
Peter Zijlstraf1600952009-03-19 20:26:16 +01001475 call_rcu(&counter->rcu_head, free_counter_rcu);
1476}
1477
Thomas Gleixner0793a612008-12-04 20:12:29 +01001478/*
1479 * Called when the last reference to the file is gone.
1480 */
1481static int perf_release(struct inode *inode, struct file *file)
1482{
1483 struct perf_counter *counter = file->private_data;
1484 struct perf_counter_context *ctx = counter->ctx;
1485
1486 file->private_data = NULL;
1487
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001488 WARN_ON_ONCE(ctx->parent_ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001489 mutex_lock(&ctx->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01001490 perf_counter_remove_from_context(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001491 mutex_unlock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001492
Peter Zijlstra082ff5a2009-05-23 18:29:00 +02001493 mutex_lock(&counter->owner->perf_counter_mutex);
1494 list_del_init(&counter->owner_entry);
1495 mutex_unlock(&counter->owner->perf_counter_mutex);
1496 put_task_struct(counter->owner);
1497
Peter Zijlstraf1600952009-03-19 20:26:16 +01001498 free_counter(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001499
1500 return 0;
1501}
1502
1503/*
1504 * Read the performance counter - simple non blocking version for now
1505 */
1506static ssize_t
1507perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
1508{
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001509 u64 values[3];
1510 int n;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001511
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001512 /*
1513 * Return end-of-file for a read on a counter that is in
1514 * error state (i.e. because it was pinned but it couldn't be
1515 * scheduled on to the CPU at some point).
1516 */
1517 if (counter->state == PERF_COUNTER_STATE_ERROR)
1518 return 0;
1519
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001520 WARN_ON_ONCE(counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001521 mutex_lock(&counter->child_mutex);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001522 values[0] = perf_counter_read(counter);
1523 n = 1;
Peter Zijlstra0d486962009-06-02 19:22:16 +02001524 if (counter->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001525 values[n++] = counter->total_time_enabled +
1526 atomic64_read(&counter->child_total_time_enabled);
Peter Zijlstra0d486962009-06-02 19:22:16 +02001527 if (counter->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001528 values[n++] = counter->total_time_running +
1529 atomic64_read(&counter->child_total_time_running);
Peter Zijlstra0d486962009-06-02 19:22:16 +02001530 if (counter->attr.read_format & PERF_FORMAT_ID)
Peter Zijlstra8e5799b2009-06-02 15:08:15 +02001531 values[n++] = counter->id;
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001532 mutex_unlock(&counter->child_mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001533
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001534 if (count < n * sizeof(u64))
1535 return -EINVAL;
1536 count = n * sizeof(u64);
1537
1538 if (copy_to_user(buf, values, count))
1539 return -EFAULT;
1540
1541 return count;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001542}
1543
1544static ssize_t
Thomas Gleixner0793a612008-12-04 20:12:29 +01001545perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
1546{
1547 struct perf_counter *counter = file->private_data;
1548
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001549 return perf_read_hw(counter, buf, count);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001550}
1551
1552static unsigned int perf_poll(struct file *file, poll_table *wait)
1553{
1554 struct perf_counter *counter = file->private_data;
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001555 struct perf_mmap_data *data;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001556 unsigned int events = POLL_HUP;
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001557
1558 rcu_read_lock();
1559 data = rcu_dereference(counter->data);
1560 if (data)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001561 events = atomic_xchg(&data->poll, 0);
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001562 rcu_read_unlock();
Thomas Gleixner0793a612008-12-04 20:12:29 +01001563
1564 poll_wait(file, &counter->waitq, wait);
1565
Thomas Gleixner0793a612008-12-04 20:12:29 +01001566 return events;
1567}
1568
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001569static void perf_counter_reset(struct perf_counter *counter)
1570{
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001571 (void)perf_counter_read(counter);
Paul Mackerras615a3f12009-05-11 15:50:21 +10001572 atomic64_set(&counter->count, 0);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001573 perf_counter_update_userpage(counter);
1574}
1575
1576static void perf_counter_for_each_sibling(struct perf_counter *counter,
1577 void (*func)(struct perf_counter *))
1578{
1579 struct perf_counter_context *ctx = counter->ctx;
1580 struct perf_counter *sibling;
1581
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001582 WARN_ON_ONCE(ctx->parent_ctx);
Peter Zijlstra682076a2009-05-23 18:28:57 +02001583 mutex_lock(&ctx->mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001584 counter = counter->group_leader;
1585
1586 func(counter);
1587 list_for_each_entry(sibling, &counter->sibling_list, list_entry)
1588 func(sibling);
Peter Zijlstra682076a2009-05-23 18:28:57 +02001589 mutex_unlock(&ctx->mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001590}
1591
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001592/*
1593 * Holding the top-level counter's child_mutex means that any
1594 * descendant process that has inherited this counter will block
1595 * in sync_child_counter if it goes to exit, thus satisfying the
1596 * task existence requirements of perf_counter_enable/disable.
1597 */
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001598static void perf_counter_for_each_child(struct perf_counter *counter,
1599 void (*func)(struct perf_counter *))
1600{
1601 struct perf_counter *child;
1602
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001603 WARN_ON_ONCE(counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001604 mutex_lock(&counter->child_mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001605 func(counter);
1606 list_for_each_entry(child, &counter->child_list, child_list)
1607 func(child);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001608 mutex_unlock(&counter->child_mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001609}
1610
1611static void perf_counter_for_each(struct perf_counter *counter,
1612 void (*func)(struct perf_counter *))
1613{
1614 struct perf_counter *child;
1615
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001616 WARN_ON_ONCE(counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001617 mutex_lock(&counter->child_mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001618 perf_counter_for_each_sibling(counter, func);
1619 list_for_each_entry(child, &counter->child_list, child_list)
1620 perf_counter_for_each_sibling(child, func);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001621 mutex_unlock(&counter->child_mutex);
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001622}
1623
Peter Zijlstra08247e32009-06-02 16:46:57 +02001624static int perf_counter_period(struct perf_counter *counter, u64 __user *arg)
1625{
1626 struct perf_counter_context *ctx = counter->ctx;
1627 unsigned long size;
1628 int ret = 0;
1629 u64 value;
1630
Peter Zijlstra0d486962009-06-02 19:22:16 +02001631 if (!counter->attr.sample_period)
Peter Zijlstra08247e32009-06-02 16:46:57 +02001632 return -EINVAL;
1633
1634 size = copy_from_user(&value, arg, sizeof(value));
1635 if (size != sizeof(value))
1636 return -EFAULT;
1637
1638 if (!value)
1639 return -EINVAL;
1640
1641 spin_lock_irq(&ctx->lock);
Peter Zijlstra0d486962009-06-02 19:22:16 +02001642 if (counter->attr.freq) {
Peter Zijlstra08247e32009-06-02 16:46:57 +02001643 if (value > sysctl_perf_counter_limit) {
1644 ret = -EINVAL;
1645 goto unlock;
1646 }
1647
Peter Zijlstra0d486962009-06-02 19:22:16 +02001648 counter->attr.sample_freq = value;
Peter Zijlstra08247e32009-06-02 16:46:57 +02001649 } else {
Peter Zijlstra0d486962009-06-02 19:22:16 +02001650 counter->attr.sample_period = value;
Peter Zijlstra08247e32009-06-02 16:46:57 +02001651 counter->hw.sample_period = value;
1652
1653 perf_log_period(counter, value);
1654 }
1655unlock:
1656 spin_unlock_irq(&ctx->lock);
1657
1658 return ret;
1659}
1660
Paul Mackerrasd859e292009-01-17 18:10:22 +11001661static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1662{
1663 struct perf_counter *counter = file->private_data;
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001664 void (*func)(struct perf_counter *);
1665 u32 flags = arg;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001666
1667 switch (cmd) {
1668 case PERF_COUNTER_IOC_ENABLE:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001669 func = perf_counter_enable;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001670 break;
1671 case PERF_COUNTER_IOC_DISABLE:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001672 func = perf_counter_disable;
Peter Zijlstra79f14642009-04-06 11:45:07 +02001673 break;
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001674 case PERF_COUNTER_IOC_RESET:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001675 func = perf_counter_reset;
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001676 break;
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001677
1678 case PERF_COUNTER_IOC_REFRESH:
1679 return perf_counter_refresh(counter, arg);
Peter Zijlstra08247e32009-06-02 16:46:57 +02001680
1681 case PERF_COUNTER_IOC_PERIOD:
1682 return perf_counter_period(counter, (u64 __user *)arg);
1683
Paul Mackerrasd859e292009-01-17 18:10:22 +11001684 default:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001685 return -ENOTTY;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001686 }
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001687
1688 if (flags & PERF_IOC_FLAG_GROUP)
1689 perf_counter_for_each(counter, func);
1690 else
1691 perf_counter_for_each_child(counter, func);
1692
1693 return 0;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001694}
1695
Peter Zijlstra771d7cd2009-05-25 14:45:26 +02001696int perf_counter_task_enable(void)
1697{
1698 struct perf_counter *counter;
1699
1700 mutex_lock(&current->perf_counter_mutex);
1701 list_for_each_entry(counter, &current->perf_counter_list, owner_entry)
1702 perf_counter_for_each_child(counter, perf_counter_enable);
1703 mutex_unlock(&current->perf_counter_mutex);
1704
1705 return 0;
1706}
1707
1708int perf_counter_task_disable(void)
1709{
1710 struct perf_counter *counter;
1711
1712 mutex_lock(&current->perf_counter_mutex);
1713 list_for_each_entry(counter, &current->perf_counter_list, owner_entry)
1714 perf_counter_for_each_child(counter, perf_counter_disable);
1715 mutex_unlock(&current->perf_counter_mutex);
1716
1717 return 0;
1718}
1719
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001720/*
1721 * Callers need to ensure there can be no nesting of this function, otherwise
1722 * the seqlock logic goes bad. We can not serialize this because the arch
1723 * code calls this from NMI context.
1724 */
1725void perf_counter_update_userpage(struct perf_counter *counter)
Paul Mackerras37d81822009-03-23 18:22:08 +01001726{
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001727 struct perf_counter_mmap_page *userpg;
Ingo Molnar22a4f652009-06-01 10:13:37 +02001728 struct perf_mmap_data *data;
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001729
1730 rcu_read_lock();
1731 data = rcu_dereference(counter->data);
1732 if (!data)
1733 goto unlock;
1734
1735 userpg = data->user_page;
Paul Mackerras37d81822009-03-23 18:22:08 +01001736
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001737 /*
1738 * Disable preemption so as to not let the corresponding user-space
1739 * spin too long if we get preempted.
1740 */
1741 preempt_disable();
Paul Mackerras37d81822009-03-23 18:22:08 +01001742 ++userpg->lock;
Peter Zijlstra92f22a32009-04-02 11:12:04 +02001743 barrier();
Paul Mackerras37d81822009-03-23 18:22:08 +01001744 userpg->index = counter->hw.idx;
1745 userpg->offset = atomic64_read(&counter->count);
1746 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
1747 userpg->offset -= atomic64_read(&counter->hw.prev_count);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001748
Peter Zijlstra92f22a32009-04-02 11:12:04 +02001749 barrier();
Paul Mackerras37d81822009-03-23 18:22:08 +01001750 ++userpg->lock;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001751 preempt_enable();
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001752unlock:
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001753 rcu_read_unlock();
Paul Mackerras37d81822009-03-23 18:22:08 +01001754}
1755
1756static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1757{
1758 struct perf_counter *counter = vma->vm_file->private_data;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001759 struct perf_mmap_data *data;
1760 int ret = VM_FAULT_SIGBUS;
Paul Mackerras37d81822009-03-23 18:22:08 +01001761
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001762 rcu_read_lock();
1763 data = rcu_dereference(counter->data);
1764 if (!data)
1765 goto unlock;
Paul Mackerras37d81822009-03-23 18:22:08 +01001766
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001767 if (vmf->pgoff == 0) {
1768 vmf->page = virt_to_page(data->user_page);
1769 } else {
1770 int nr = vmf->pgoff - 1;
1771
1772 if ((unsigned)nr > data->nr_pages)
1773 goto unlock;
1774
1775 vmf->page = virt_to_page(data->data_pages[nr]);
1776 }
Paul Mackerras37d81822009-03-23 18:22:08 +01001777 get_page(vmf->page);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001778 ret = 0;
1779unlock:
1780 rcu_read_unlock();
1781
1782 return ret;
1783}
1784
1785static int perf_mmap_data_alloc(struct perf_counter *counter, int nr_pages)
1786{
1787 struct perf_mmap_data *data;
1788 unsigned long size;
1789 int i;
1790
1791 WARN_ON(atomic_read(&counter->mmap_count));
1792
1793 size = sizeof(struct perf_mmap_data);
1794 size += nr_pages * sizeof(void *);
1795
1796 data = kzalloc(size, GFP_KERNEL);
1797 if (!data)
1798 goto fail;
1799
1800 data->user_page = (void *)get_zeroed_page(GFP_KERNEL);
1801 if (!data->user_page)
1802 goto fail_user_page;
1803
1804 for (i = 0; i < nr_pages; i++) {
1805 data->data_pages[i] = (void *)get_zeroed_page(GFP_KERNEL);
1806 if (!data->data_pages[i])
1807 goto fail_data_pages;
1808 }
1809
1810 data->nr_pages = nr_pages;
Peter Zijlstra22c15582009-05-05 17:50:25 +02001811 atomic_set(&data->lock, -1);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001812
1813 rcu_assign_pointer(counter->data, data);
1814
Paul Mackerras37d81822009-03-23 18:22:08 +01001815 return 0;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001816
1817fail_data_pages:
1818 for (i--; i >= 0; i--)
1819 free_page((unsigned long)data->data_pages[i]);
1820
1821 free_page((unsigned long)data->user_page);
1822
1823fail_user_page:
1824 kfree(data);
1825
1826fail:
1827 return -ENOMEM;
1828}
1829
1830static void __perf_mmap_data_free(struct rcu_head *rcu_head)
1831{
Ingo Molnar22a4f652009-06-01 10:13:37 +02001832 struct perf_mmap_data *data;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001833 int i;
1834
Ingo Molnar22a4f652009-06-01 10:13:37 +02001835 data = container_of(rcu_head, struct perf_mmap_data, rcu_head);
1836
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001837 free_page((unsigned long)data->user_page);
1838 for (i = 0; i < data->nr_pages; i++)
1839 free_page((unsigned long)data->data_pages[i]);
1840 kfree(data);
1841}
1842
1843static void perf_mmap_data_free(struct perf_counter *counter)
1844{
1845 struct perf_mmap_data *data = counter->data;
1846
1847 WARN_ON(atomic_read(&counter->mmap_count));
1848
1849 rcu_assign_pointer(counter->data, NULL);
1850 call_rcu(&data->rcu_head, __perf_mmap_data_free);
1851}
1852
1853static void perf_mmap_open(struct vm_area_struct *vma)
1854{
1855 struct perf_counter *counter = vma->vm_file->private_data;
1856
1857 atomic_inc(&counter->mmap_count);
1858}
1859
1860static void perf_mmap_close(struct vm_area_struct *vma)
1861{
1862 struct perf_counter *counter = vma->vm_file->private_data;
1863
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001864 WARN_ON_ONCE(counter->ctx->parent_ctx);
Ingo Molnar22a4f652009-06-01 10:13:37 +02001865 if (atomic_dec_and_mutex_lock(&counter->mmap_count, &counter->mmap_mutex)) {
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001866 struct user_struct *user = current_user();
1867
1868 atomic_long_sub(counter->data->nr_pages + 1, &user->locked_vm);
Peter Zijlstrac5078f72009-05-05 17:50:24 +02001869 vma->vm_mm->locked_vm -= counter->data->nr_locked;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001870 perf_mmap_data_free(counter);
1871 mutex_unlock(&counter->mmap_mutex);
1872 }
Paul Mackerras37d81822009-03-23 18:22:08 +01001873}
1874
1875static struct vm_operations_struct perf_mmap_vmops = {
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001876 .open = perf_mmap_open,
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001877 .close = perf_mmap_close,
Paul Mackerras37d81822009-03-23 18:22:08 +01001878 .fault = perf_mmap_fault,
1879};
1880
1881static int perf_mmap(struct file *file, struct vm_area_struct *vma)
1882{
1883 struct perf_counter *counter = file->private_data;
Ingo Molnar22a4f652009-06-01 10:13:37 +02001884 unsigned long user_locked, user_lock_limit;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001885 struct user_struct *user = current_user();
Ingo Molnar22a4f652009-06-01 10:13:37 +02001886 unsigned long locked, lock_limit;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001887 unsigned long vma_size;
1888 unsigned long nr_pages;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001889 long user_extra, extra;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001890 int ret = 0;
Paul Mackerras37d81822009-03-23 18:22:08 +01001891
1892 if (!(vma->vm_flags & VM_SHARED) || (vma->vm_flags & VM_WRITE))
1893 return -EINVAL;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001894
1895 vma_size = vma->vm_end - vma->vm_start;
1896 nr_pages = (vma_size / PAGE_SIZE) - 1;
1897
Peter Zijlstra7730d862009-03-25 12:48:31 +01001898 /*
1899 * If we have data pages ensure they're a power-of-two number, so we
1900 * can do bitmasks instead of modulo.
1901 */
1902 if (nr_pages != 0 && !is_power_of_2(nr_pages))
Paul Mackerras37d81822009-03-23 18:22:08 +01001903 return -EINVAL;
1904
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001905 if (vma_size != PAGE_SIZE * (1 + nr_pages))
Paul Mackerras37d81822009-03-23 18:22:08 +01001906 return -EINVAL;
1907
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001908 if (vma->vm_pgoff != 0)
1909 return -EINVAL;
Paul Mackerras37d81822009-03-23 18:22:08 +01001910
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001911 WARN_ON_ONCE(counter->ctx->parent_ctx);
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001912 mutex_lock(&counter->mmap_mutex);
1913 if (atomic_inc_not_zero(&counter->mmap_count)) {
1914 if (nr_pages != counter->data->nr_pages)
1915 ret = -EINVAL;
1916 goto unlock;
1917 }
1918
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001919 user_extra = nr_pages + 1;
1920 user_lock_limit = sysctl_perf_counter_mlock >> (PAGE_SHIFT - 10);
Ingo Molnara3862d32009-05-24 09:02:37 +02001921
1922 /*
1923 * Increase the limit linearly with more CPUs:
1924 */
1925 user_lock_limit *= num_online_cpus();
1926
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001927 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
Peter Zijlstrac5078f72009-05-05 17:50:24 +02001928
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001929 extra = 0;
1930 if (user_locked > user_lock_limit)
1931 extra = user_locked - user_lock_limit;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001932
1933 lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
1934 lock_limit >>= PAGE_SHIFT;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001935 locked = vma->vm_mm->locked_vm + extra;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001936
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001937 if ((locked > lock_limit) && !capable(CAP_IPC_LOCK)) {
1938 ret = -EPERM;
1939 goto unlock;
1940 }
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001941
1942 WARN_ON(counter->data);
1943 ret = perf_mmap_data_alloc(counter, nr_pages);
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001944 if (ret)
1945 goto unlock;
1946
1947 atomic_set(&counter->mmap_count, 1);
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001948 atomic_long_add(user_extra, &user->locked_vm);
Peter Zijlstrac5078f72009-05-05 17:50:24 +02001949 vma->vm_mm->locked_vm += extra;
1950 counter->data->nr_locked = extra;
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001951unlock:
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001952 mutex_unlock(&counter->mmap_mutex);
Paul Mackerras37d81822009-03-23 18:22:08 +01001953
1954 vma->vm_flags &= ~VM_MAYWRITE;
1955 vma->vm_flags |= VM_RESERVED;
1956 vma->vm_ops = &perf_mmap_vmops;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001957
1958 return ret;
Paul Mackerras37d81822009-03-23 18:22:08 +01001959}
1960
Peter Zijlstra3c446b3d2009-04-06 11:45:01 +02001961static int perf_fasync(int fd, struct file *filp, int on)
1962{
Peter Zijlstra3c446b3d2009-04-06 11:45:01 +02001963 struct inode *inode = filp->f_path.dentry->d_inode;
Ingo Molnar22a4f652009-06-01 10:13:37 +02001964 struct perf_counter *counter = filp->private_data;
Peter Zijlstra3c446b3d2009-04-06 11:45:01 +02001965 int retval;
1966
1967 mutex_lock(&inode->i_mutex);
1968 retval = fasync_helper(fd, filp, on, &counter->fasync);
1969 mutex_unlock(&inode->i_mutex);
1970
1971 if (retval < 0)
1972 return retval;
1973
1974 return 0;
1975}
1976
Thomas Gleixner0793a612008-12-04 20:12:29 +01001977static const struct file_operations perf_fops = {
1978 .release = perf_release,
1979 .read = perf_read,
1980 .poll = perf_poll,
Paul Mackerrasd859e292009-01-17 18:10:22 +11001981 .unlocked_ioctl = perf_ioctl,
1982 .compat_ioctl = perf_ioctl,
Paul Mackerras37d81822009-03-23 18:22:08 +01001983 .mmap = perf_mmap,
Peter Zijlstra3c446b3d2009-04-06 11:45:01 +02001984 .fasync = perf_fasync,
Thomas Gleixner0793a612008-12-04 20:12:29 +01001985};
1986
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001987/*
Peter Zijlstra925d5192009-03-30 19:07:02 +02001988 * Perf counter wakeup
1989 *
1990 * If there's data, ensure we set the poll() state and publish everything
1991 * to user-space before waking everybody up.
1992 */
1993
1994void perf_counter_wakeup(struct perf_counter *counter)
1995{
Peter Zijlstra925d5192009-03-30 19:07:02 +02001996 wake_up_all(&counter->waitq);
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02001997
1998 if (counter->pending_kill) {
1999 kill_fasync(&counter->fasync, SIGIO, counter->pending_kill);
2000 counter->pending_kill = 0;
2001 }
Peter Zijlstra925d5192009-03-30 19:07:02 +02002002}
2003
2004/*
2005 * Pending wakeups
2006 *
2007 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
2008 *
2009 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
2010 * single linked list and use cmpxchg() to add entries lockless.
2011 */
2012
Peter Zijlstra79f14642009-04-06 11:45:07 +02002013static void perf_pending_counter(struct perf_pending_entry *entry)
2014{
2015 struct perf_counter *counter = container_of(entry,
2016 struct perf_counter, pending);
2017
2018 if (counter->pending_disable) {
2019 counter->pending_disable = 0;
2020 perf_counter_disable(counter);
2021 }
2022
2023 if (counter->pending_wakeup) {
2024 counter->pending_wakeup = 0;
2025 perf_counter_wakeup(counter);
2026 }
2027}
2028
Peter Zijlstra671dec52009-04-06 11:45:02 +02002029#define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
Peter Zijlstra925d5192009-03-30 19:07:02 +02002030
Peter Zijlstra671dec52009-04-06 11:45:02 +02002031static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = {
Peter Zijlstra925d5192009-03-30 19:07:02 +02002032 PENDING_TAIL,
2033};
2034
Peter Zijlstra671dec52009-04-06 11:45:02 +02002035static void perf_pending_queue(struct perf_pending_entry *entry,
2036 void (*func)(struct perf_pending_entry *))
Peter Zijlstra925d5192009-03-30 19:07:02 +02002037{
Peter Zijlstra671dec52009-04-06 11:45:02 +02002038 struct perf_pending_entry **head;
Peter Zijlstra925d5192009-03-30 19:07:02 +02002039
Peter Zijlstra671dec52009-04-06 11:45:02 +02002040 if (cmpxchg(&entry->next, NULL, PENDING_TAIL) != NULL)
Peter Zijlstra925d5192009-03-30 19:07:02 +02002041 return;
2042
Peter Zijlstra671dec52009-04-06 11:45:02 +02002043 entry->func = func;
2044
2045 head = &get_cpu_var(perf_pending_head);
Peter Zijlstra925d5192009-03-30 19:07:02 +02002046
2047 do {
Peter Zijlstra671dec52009-04-06 11:45:02 +02002048 entry->next = *head;
2049 } while (cmpxchg(head, entry->next, entry) != entry->next);
Peter Zijlstra925d5192009-03-30 19:07:02 +02002050
2051 set_perf_counter_pending();
2052
Peter Zijlstra671dec52009-04-06 11:45:02 +02002053 put_cpu_var(perf_pending_head);
Peter Zijlstra925d5192009-03-30 19:07:02 +02002054}
2055
2056static int __perf_pending_run(void)
2057{
Peter Zijlstra671dec52009-04-06 11:45:02 +02002058 struct perf_pending_entry *list;
Peter Zijlstra925d5192009-03-30 19:07:02 +02002059 int nr = 0;
2060
Peter Zijlstra671dec52009-04-06 11:45:02 +02002061 list = xchg(&__get_cpu_var(perf_pending_head), PENDING_TAIL);
Peter Zijlstra925d5192009-03-30 19:07:02 +02002062 while (list != PENDING_TAIL) {
Peter Zijlstra671dec52009-04-06 11:45:02 +02002063 void (*func)(struct perf_pending_entry *);
2064 struct perf_pending_entry *entry = list;
Peter Zijlstra925d5192009-03-30 19:07:02 +02002065
2066 list = list->next;
2067
Peter Zijlstra671dec52009-04-06 11:45:02 +02002068 func = entry->func;
2069 entry->next = NULL;
Peter Zijlstra925d5192009-03-30 19:07:02 +02002070 /*
2071 * Ensure we observe the unqueue before we issue the wakeup,
2072 * so that we won't be waiting forever.
2073 * -- see perf_not_pending().
2074 */
2075 smp_wmb();
2076
Peter Zijlstra671dec52009-04-06 11:45:02 +02002077 func(entry);
Peter Zijlstra925d5192009-03-30 19:07:02 +02002078 nr++;
2079 }
2080
2081 return nr;
2082}
2083
2084static inline int perf_not_pending(struct perf_counter *counter)
2085{
2086 /*
2087 * If we flush on whatever cpu we run, there is a chance we don't
2088 * need to wait.
2089 */
2090 get_cpu();
2091 __perf_pending_run();
2092 put_cpu();
2093
2094 /*
2095 * Ensure we see the proper queue state before going to sleep
2096 * so that we do not miss the wakeup. -- see perf_pending_handle()
2097 */
2098 smp_rmb();
Peter Zijlstra671dec52009-04-06 11:45:02 +02002099 return counter->pending.next == NULL;
Peter Zijlstra925d5192009-03-30 19:07:02 +02002100}
2101
2102static void perf_pending_sync(struct perf_counter *counter)
2103{
2104 wait_event(counter->waitq, perf_not_pending(counter));
2105}
2106
2107void perf_counter_do_pending(void)
2108{
2109 __perf_pending_run();
2110}
2111
2112/*
Peter Zijlstra394ee072009-03-30 19:07:14 +02002113 * Callchain support -- arch specific
2114 */
2115
Peter Zijlstra9c03d882009-04-06 11:45:00 +02002116__weak struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
Peter Zijlstra394ee072009-03-30 19:07:14 +02002117{
2118 return NULL;
2119}
2120
2121/*
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002122 * Output
2123 */
2124
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002125struct perf_output_handle {
2126 struct perf_counter *counter;
2127 struct perf_mmap_data *data;
Peter Zijlstra8e3747c2009-06-02 16:16:02 +02002128 unsigned long head;
2129 unsigned long offset;
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002130 int nmi;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002131 int overflow;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002132 int locked;
2133 unsigned long flags;
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002134};
2135
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002136static void perf_output_wakeup(struct perf_output_handle *handle)
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002137{
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002138 atomic_set(&handle->data->poll, POLL_IN);
2139
Peter Zijlstra671dec52009-04-06 11:45:02 +02002140 if (handle->nmi) {
Peter Zijlstra79f14642009-04-06 11:45:07 +02002141 handle->counter->pending_wakeup = 1;
Peter Zijlstra671dec52009-04-06 11:45:02 +02002142 perf_pending_queue(&handle->counter->pending,
Peter Zijlstra79f14642009-04-06 11:45:07 +02002143 perf_pending_counter);
Peter Zijlstra671dec52009-04-06 11:45:02 +02002144 } else
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002145 perf_counter_wakeup(handle->counter);
2146}
2147
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002148/*
2149 * Curious locking construct.
2150 *
2151 * We need to ensure a later event doesn't publish a head when a former
2152 * event isn't done writing. However since we need to deal with NMIs we
2153 * cannot fully serialize things.
2154 *
2155 * What we do is serialize between CPUs so we only have to deal with NMI
2156 * nesting on a single CPU.
2157 *
2158 * We only publish the head (and generate a wakeup) when the outer-most
2159 * event completes.
2160 */
2161static void perf_output_lock(struct perf_output_handle *handle)
2162{
2163 struct perf_mmap_data *data = handle->data;
2164 int cpu;
2165
2166 handle->locked = 0;
2167
2168 local_irq_save(handle->flags);
2169 cpu = smp_processor_id();
2170
2171 if (in_nmi() && atomic_read(&data->lock) == cpu)
2172 return;
2173
Peter Zijlstra22c15582009-05-05 17:50:25 +02002174 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002175 cpu_relax();
2176
2177 handle->locked = 1;
2178}
2179
2180static void perf_output_unlock(struct perf_output_handle *handle)
2181{
2182 struct perf_mmap_data *data = handle->data;
Peter Zijlstra8e3747c2009-06-02 16:16:02 +02002183 unsigned long head;
2184 int cpu;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002185
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002186 data->done_head = data->head;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002187
2188 if (!handle->locked)
2189 goto out;
2190
2191again:
2192 /*
2193 * The xchg implies a full barrier that ensures all writes are done
2194 * before we publish the new head, matched by a rmb() in userspace when
2195 * reading this position.
2196 */
Peter Zijlstra8e3747c2009-06-02 16:16:02 +02002197 while ((head = atomic_long_xchg(&data->done_head, 0)))
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002198 data->user_page->data_head = head;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002199
2200 /*
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002201 * NMI can happen here, which means we can miss a done_head update.
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002202 */
2203
Peter Zijlstra22c15582009-05-05 17:50:25 +02002204 cpu = atomic_xchg(&data->lock, -1);
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002205 WARN_ON_ONCE(cpu != smp_processor_id());
2206
2207 /*
2208 * Therefore we have to validate we did not indeed do so.
2209 */
Peter Zijlstra8e3747c2009-06-02 16:16:02 +02002210 if (unlikely(atomic_long_read(&data->done_head))) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002211 /*
2212 * Since we had it locked, we can lock it again.
2213 */
Peter Zijlstra22c15582009-05-05 17:50:25 +02002214 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002215 cpu_relax();
2216
2217 goto again;
2218 }
2219
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002220 if (atomic_xchg(&data->wakeup, 0))
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002221 perf_output_wakeup(handle);
2222out:
2223 local_irq_restore(handle->flags);
2224}
2225
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002226static int perf_output_begin(struct perf_output_handle *handle,
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002227 struct perf_counter *counter, unsigned int size,
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002228 int nmi, int overflow)
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002229{
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002230 struct perf_mmap_data *data;
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002231 unsigned int offset, head;
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002232
Peter Zijlstra2023b352009-05-05 17:50:26 +02002233 /*
2234 * For inherited counters we send all the output towards the parent.
2235 */
2236 if (counter->parent)
2237 counter = counter->parent;
2238
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002239 rcu_read_lock();
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002240 data = rcu_dereference(counter->data);
2241 if (!data)
2242 goto out;
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002243
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002244 handle->data = data;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002245 handle->counter = counter;
2246 handle->nmi = nmi;
2247 handle->overflow = overflow;
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002248
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002249 if (!data->nr_pages)
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002250 goto fail;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002251
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002252 perf_output_lock(handle);
2253
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002254 do {
Paul Mackerras6dc5f2a2009-06-05 12:36:28 +10002255 offset = head = atomic_long_read(&data->head);
Peter Zijlstrac7138f32009-03-24 13:18:16 +01002256 head += size;
Peter Zijlstra8e3747c2009-06-02 16:16:02 +02002257 } while (atomic_long_cmpxchg(&data->head, offset, head) != offset);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002258
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002259 handle->offset = offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01002260 handle->head = head;
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002261
2262 if ((offset >> PAGE_SHIFT) != (head >> PAGE_SHIFT))
2263 atomic_set(&data->wakeup, 1);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002264
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002265 return 0;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002266
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002267fail:
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002268 perf_output_wakeup(handle);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002269out:
2270 rcu_read_unlock();
2271
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002272 return -ENOSPC;
2273}
2274
2275static void perf_output_copy(struct perf_output_handle *handle,
Peter Zijlstra089dd792009-06-05 14:04:55 +02002276 const void *buf, unsigned int len)
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002277{
2278 unsigned int pages_mask;
2279 unsigned int offset;
2280 unsigned int size;
2281 void **pages;
2282
2283 offset = handle->offset;
2284 pages_mask = handle->data->nr_pages - 1;
2285 pages = handle->data->data_pages;
2286
2287 do {
2288 unsigned int page_offset;
2289 int nr;
2290
2291 nr = (offset >> PAGE_SHIFT) & pages_mask;
2292 page_offset = offset & (PAGE_SIZE - 1);
2293 size = min_t(unsigned int, PAGE_SIZE - page_offset, len);
2294
2295 memcpy(pages[nr] + page_offset, buf, size);
2296
2297 len -= size;
2298 buf += size;
2299 offset += size;
2300 } while (len);
2301
2302 handle->offset = offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01002303
Peter Zijlstra53020fe2009-05-13 21:26:19 +02002304 /*
2305 * Check we didn't copy past our reservation window, taking the
2306 * possible unsigned int wrap into account.
2307 */
Peter Zijlstra8e3747c2009-06-02 16:16:02 +02002308 WARN_ON_ONCE(((long)(handle->head - handle->offset)) < 0);
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002309}
2310
Peter Zijlstra5c148192009-03-25 12:30:23 +01002311#define perf_output_put(handle, x) \
2312 perf_output_copy((handle), &(x), sizeof(x))
2313
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002314static void perf_output_end(struct perf_output_handle *handle)
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002315{
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002316 struct perf_counter *counter = handle->counter;
2317 struct perf_mmap_data *data = handle->data;
2318
Peter Zijlstra0d486962009-06-02 19:22:16 +02002319 int wakeup_events = counter->attr.wakeup_events;
Peter Zijlstrac4578102009-04-02 11:12:01 +02002320
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002321 if (handle->overflow && wakeup_events) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002322 int events = atomic_inc_return(&data->events);
Peter Zijlstrac4578102009-04-02 11:12:01 +02002323 if (events >= wakeup_events) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002324 atomic_sub(wakeup_events, &data->events);
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002325 atomic_set(&data->wakeup, 1);
Peter Zijlstrac4578102009-04-02 11:12:01 +02002326 }
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002327 }
2328
2329 perf_output_unlock(handle);
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002330 rcu_read_unlock();
2331}
2332
Peter Zijlstra709e50c2009-06-02 14:13:15 +02002333static u32 perf_counter_pid(struct perf_counter *counter, struct task_struct *p)
2334{
2335 /*
2336 * only top level counters have the pid namespace they were created in
2337 */
2338 if (counter->parent)
2339 counter = counter->parent;
2340
2341 return task_tgid_nr_ns(p, counter->ns);
2342}
2343
2344static u32 perf_counter_tid(struct perf_counter *counter, struct task_struct *p)
2345{
2346 /*
2347 * only top level counters have the pid namespace they were created in
2348 */
2349 if (counter->parent)
2350 counter = counter->parent;
2351
2352 return task_pid_nr_ns(p, counter->ns);
2353}
2354
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002355static void perf_counter_output(struct perf_counter *counter,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002356 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002357{
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002358 int ret;
Peter Zijlstra0d486962009-06-02 19:22:16 +02002359 u64 sample_type = counter->attr.sample_type;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002360 struct perf_output_handle handle;
2361 struct perf_event_header header;
2362 u64 ip;
Peter Zijlstra5c148192009-03-25 12:30:23 +01002363 struct {
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002364 u32 pid, tid;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002365 } tid_entry;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002366 struct {
Peter Zijlstra8e5799b2009-06-02 15:08:15 +02002367 u64 id;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002368 u64 counter;
2369 } group_entry;
Peter Zijlstra394ee072009-03-30 19:07:14 +02002370 struct perf_callchain_entry *callchain = NULL;
2371 int callchain_size = 0;
Peter Zijlstra339f7c92009-04-06 11:45:06 +02002372 u64 time;
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002373 struct {
2374 u32 cpu, reserved;
2375 } cpu_entry;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002376
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02002377 header.type = 0;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002378 header.size = sizeof(header);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002379
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02002380 header.misc = PERF_EVENT_MISC_OVERFLOW;
Paul Mackerras9d23a902009-05-14 21:48:08 +10002381 header.misc |= perf_misc_flags(regs);
Peter Zijlstra6fab0192009-04-08 15:01:26 +02002382
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002383 if (sample_type & PERF_SAMPLE_IP) {
Paul Mackerras9d23a902009-05-14 21:48:08 +10002384 ip = perf_instruction_pointer(regs);
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002385 header.type |= PERF_SAMPLE_IP;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002386 header.size += sizeof(ip);
2387 }
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002388
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002389 if (sample_type & PERF_SAMPLE_TID) {
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002390 /* namespace issues */
Peter Zijlstra709e50c2009-06-02 14:13:15 +02002391 tid_entry.pid = perf_counter_pid(counter, current);
2392 tid_entry.tid = perf_counter_tid(counter, current);
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002393
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002394 header.type |= PERF_SAMPLE_TID;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002395 header.size += sizeof(tid_entry);
2396 }
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002397
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002398 if (sample_type & PERF_SAMPLE_TIME) {
Peter Zijlstra4d855452009-04-08 15:01:32 +02002399 /*
2400 * Maybe do better on x86 and provide cpu_clock_nmi()
2401 */
2402 time = sched_clock();
2403
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002404 header.type |= PERF_SAMPLE_TIME;
Peter Zijlstra4d855452009-04-08 15:01:32 +02002405 header.size += sizeof(u64);
2406 }
2407
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002408 if (sample_type & PERF_SAMPLE_ADDR) {
2409 header.type |= PERF_SAMPLE_ADDR;
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002410 header.size += sizeof(u64);
2411 }
2412
Peter Zijlstraac4bcf82009-06-05 14:44:52 +02002413 if (sample_type & PERF_SAMPLE_ID) {
2414 header.type |= PERF_SAMPLE_ID;
Peter Zijlstraa85f61a2009-05-08 18:52:23 +02002415 header.size += sizeof(u64);
2416 }
2417
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002418 if (sample_type & PERF_SAMPLE_CPU) {
2419 header.type |= PERF_SAMPLE_CPU;
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002420 header.size += sizeof(cpu_entry);
2421
2422 cpu_entry.cpu = raw_smp_processor_id();
2423 }
2424
Peter Zijlstra689802b2009-06-05 15:05:43 +02002425 if (sample_type & PERF_SAMPLE_PERIOD) {
2426 header.type |= PERF_SAMPLE_PERIOD;
2427 header.size += sizeof(u64);
2428 }
2429
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002430 if (sample_type & PERF_SAMPLE_GROUP) {
2431 header.type |= PERF_SAMPLE_GROUP;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002432 header.size += sizeof(u64) +
2433 counter->nr_siblings * sizeof(group_entry);
2434 }
2435
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002436 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
Peter Zijlstra394ee072009-03-30 19:07:14 +02002437 callchain = perf_callchain(regs);
2438
2439 if (callchain) {
Peter Zijlstra9c03d882009-04-06 11:45:00 +02002440 callchain_size = (1 + callchain->nr) * sizeof(u64);
Peter Zijlstra394ee072009-03-30 19:07:14 +02002441
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002442 header.type |= PERF_SAMPLE_CALLCHAIN;
Peter Zijlstra394ee072009-03-30 19:07:14 +02002443 header.size += callchain_size;
2444 }
2445 }
2446
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002447 ret = perf_output_begin(&handle, counter, header.size, nmi, 1);
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002448 if (ret)
2449 return;
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002450
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002451 perf_output_put(&handle, header);
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002452
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002453 if (sample_type & PERF_SAMPLE_IP)
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002454 perf_output_put(&handle, ip);
2455
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002456 if (sample_type & PERF_SAMPLE_TID)
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002457 perf_output_put(&handle, tid_entry);
2458
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002459 if (sample_type & PERF_SAMPLE_TIME)
Peter Zijlstra4d855452009-04-08 15:01:32 +02002460 perf_output_put(&handle, time);
2461
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002462 if (sample_type & PERF_SAMPLE_ADDR)
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002463 perf_output_put(&handle, addr);
2464
Peter Zijlstraac4bcf82009-06-05 14:44:52 +02002465 if (sample_type & PERF_SAMPLE_ID)
2466 perf_output_put(&handle, counter->id);
Peter Zijlstraa85f61a2009-05-08 18:52:23 +02002467
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002468 if (sample_type & PERF_SAMPLE_CPU)
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002469 perf_output_put(&handle, cpu_entry);
2470
Peter Zijlstra689802b2009-06-05 15:05:43 +02002471 if (sample_type & PERF_SAMPLE_PERIOD)
2472 perf_output_put(&handle, counter->hw.sample_period);
2473
Peter Zijlstra2023b352009-05-05 17:50:26 +02002474 /*
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002475 * XXX PERF_SAMPLE_GROUP vs inherited counters seems difficult.
Peter Zijlstra2023b352009-05-05 17:50:26 +02002476 */
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002477 if (sample_type & PERF_SAMPLE_GROUP) {
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002478 struct perf_counter *leader, *sub;
2479 u64 nr = counter->nr_siblings;
2480
2481 perf_output_put(&handle, nr);
2482
2483 leader = counter->group_leader;
2484 list_for_each_entry(sub, &leader->sibling_list, list_entry) {
2485 if (sub != counter)
Robert Richter4aeb0b42009-04-29 12:47:03 +02002486 sub->pmu->read(sub);
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002487
Peter Zijlstra8e5799b2009-06-02 15:08:15 +02002488 group_entry.id = sub->id;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002489 group_entry.counter = atomic64_read(&sub->count);
2490
2491 perf_output_put(&handle, group_entry);
2492 }
2493 }
2494
Peter Zijlstra394ee072009-03-30 19:07:14 +02002495 if (callchain)
2496 perf_output_copy(&handle, callchain, callchain_size);
2497
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002498 perf_output_end(&handle);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002499}
2500
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002501/*
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002502 * fork tracking
2503 */
2504
2505struct perf_fork_event {
2506 struct task_struct *task;
2507
2508 struct {
2509 struct perf_event_header header;
2510
2511 u32 pid;
2512 u32 ppid;
2513 } event;
2514};
2515
2516static void perf_counter_fork_output(struct perf_counter *counter,
2517 struct perf_fork_event *fork_event)
2518{
2519 struct perf_output_handle handle;
2520 int size = fork_event->event.header.size;
2521 struct task_struct *task = fork_event->task;
2522 int ret = perf_output_begin(&handle, counter, size, 0, 0);
2523
2524 if (ret)
2525 return;
2526
2527 fork_event->event.pid = perf_counter_pid(counter, task);
2528 fork_event->event.ppid = perf_counter_pid(counter, task->real_parent);
2529
2530 perf_output_put(&handle, fork_event->event);
2531 perf_output_end(&handle);
2532}
2533
2534static int perf_counter_fork_match(struct perf_counter *counter)
2535{
Peter Zijlstrad99e9442009-06-04 17:08:58 +02002536 if (counter->attr.comm || counter->attr.mmap)
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002537 return 1;
2538
2539 return 0;
2540}
2541
2542static void perf_counter_fork_ctx(struct perf_counter_context *ctx,
2543 struct perf_fork_event *fork_event)
2544{
2545 struct perf_counter *counter;
2546
2547 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2548 return;
2549
2550 rcu_read_lock();
2551 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2552 if (perf_counter_fork_match(counter))
2553 perf_counter_fork_output(counter, fork_event);
2554 }
2555 rcu_read_unlock();
2556}
2557
2558static void perf_counter_fork_event(struct perf_fork_event *fork_event)
2559{
2560 struct perf_cpu_context *cpuctx;
2561 struct perf_counter_context *ctx;
2562
2563 cpuctx = &get_cpu_var(perf_cpu_context);
2564 perf_counter_fork_ctx(&cpuctx->ctx, fork_event);
2565 put_cpu_var(perf_cpu_context);
2566
2567 rcu_read_lock();
2568 /*
2569 * doesn't really matter which of the child contexts the
2570 * events ends up in.
2571 */
2572 ctx = rcu_dereference(current->perf_counter_ctxp);
2573 if (ctx)
2574 perf_counter_fork_ctx(ctx, fork_event);
2575 rcu_read_unlock();
2576}
2577
2578void perf_counter_fork(struct task_struct *task)
2579{
2580 struct perf_fork_event fork_event;
2581
2582 if (!atomic_read(&nr_comm_counters) &&
Peter Zijlstrad99e9442009-06-04 17:08:58 +02002583 !atomic_read(&nr_mmap_counters))
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002584 return;
2585
2586 fork_event = (struct perf_fork_event){
2587 .task = task,
2588 .event = {
2589 .header = {
2590 .type = PERF_EVENT_FORK,
2591 .size = sizeof(fork_event.event),
2592 },
2593 },
2594 };
2595
2596 perf_counter_fork_event(&fork_event);
2597}
2598
2599/*
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002600 * comm tracking
2601 */
2602
2603struct perf_comm_event {
Ingo Molnar22a4f652009-06-01 10:13:37 +02002604 struct task_struct *task;
2605 char *comm;
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002606 int comm_size;
2607
2608 struct {
2609 struct perf_event_header header;
2610
2611 u32 pid;
2612 u32 tid;
2613 } event;
2614};
2615
2616static void perf_counter_comm_output(struct perf_counter *counter,
2617 struct perf_comm_event *comm_event)
2618{
2619 struct perf_output_handle handle;
2620 int size = comm_event->event.header.size;
2621 int ret = perf_output_begin(&handle, counter, size, 0, 0);
2622
2623 if (ret)
2624 return;
2625
Peter Zijlstra709e50c2009-06-02 14:13:15 +02002626 comm_event->event.pid = perf_counter_pid(counter, comm_event->task);
2627 comm_event->event.tid = perf_counter_tid(counter, comm_event->task);
2628
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002629 perf_output_put(&handle, comm_event->event);
2630 perf_output_copy(&handle, comm_event->comm,
2631 comm_event->comm_size);
2632 perf_output_end(&handle);
2633}
2634
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002635static int perf_counter_comm_match(struct perf_counter *counter)
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002636{
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002637 if (counter->attr.comm)
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002638 return 1;
2639
2640 return 0;
2641}
2642
2643static void perf_counter_comm_ctx(struct perf_counter_context *ctx,
2644 struct perf_comm_event *comm_event)
2645{
2646 struct perf_counter *counter;
2647
2648 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2649 return;
2650
2651 rcu_read_lock();
2652 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002653 if (perf_counter_comm_match(counter))
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002654 perf_counter_comm_output(counter, comm_event);
2655 }
2656 rcu_read_unlock();
2657}
2658
2659static void perf_counter_comm_event(struct perf_comm_event *comm_event)
2660{
2661 struct perf_cpu_context *cpuctx;
Peter Zijlstra665c2142009-05-29 14:51:57 +02002662 struct perf_counter_context *ctx;
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002663 unsigned int size;
2664 char *comm = comm_event->task->comm;
2665
Ingo Molnar888fcee2009-04-09 09:48:22 +02002666 size = ALIGN(strlen(comm)+1, sizeof(u64));
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002667
2668 comm_event->comm = comm;
2669 comm_event->comm_size = size;
2670
2671 comm_event->event.header.size = sizeof(comm_event->event) + size;
2672
2673 cpuctx = &get_cpu_var(perf_cpu_context);
2674 perf_counter_comm_ctx(&cpuctx->ctx, comm_event);
2675 put_cpu_var(perf_cpu_context);
Peter Zijlstra665c2142009-05-29 14:51:57 +02002676
2677 rcu_read_lock();
2678 /*
2679 * doesn't really matter which of the child contexts the
2680 * events ends up in.
2681 */
2682 ctx = rcu_dereference(current->perf_counter_ctxp);
2683 if (ctx)
2684 perf_counter_comm_ctx(ctx, comm_event);
2685 rcu_read_unlock();
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002686}
2687
2688void perf_counter_comm(struct task_struct *task)
2689{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002690 struct perf_comm_event comm_event;
2691
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002692 if (!atomic_read(&nr_comm_counters))
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002693 return;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10002694
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002695 comm_event = (struct perf_comm_event){
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002696 .task = task,
2697 .event = {
2698 .header = { .type = PERF_EVENT_COMM, },
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002699 },
2700 };
2701
2702 perf_counter_comm_event(&comm_event);
2703}
2704
2705/*
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002706 * mmap tracking
2707 */
2708
2709struct perf_mmap_event {
Peter Zijlstra089dd792009-06-05 14:04:55 +02002710 struct vm_area_struct *vma;
2711
2712 const char *file_name;
2713 int file_size;
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002714
2715 struct {
2716 struct perf_event_header header;
2717
2718 u32 pid;
2719 u32 tid;
2720 u64 start;
2721 u64 len;
2722 u64 pgoff;
2723 } event;
2724};
2725
2726static void perf_counter_mmap_output(struct perf_counter *counter,
2727 struct perf_mmap_event *mmap_event)
2728{
2729 struct perf_output_handle handle;
2730 int size = mmap_event->event.header.size;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002731 int ret = perf_output_begin(&handle, counter, size, 0, 0);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002732
2733 if (ret)
2734 return;
2735
Peter Zijlstra709e50c2009-06-02 14:13:15 +02002736 mmap_event->event.pid = perf_counter_pid(counter, current);
2737 mmap_event->event.tid = perf_counter_tid(counter, current);
2738
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002739 perf_output_put(&handle, mmap_event->event);
2740 perf_output_copy(&handle, mmap_event->file_name,
2741 mmap_event->file_size);
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002742 perf_output_end(&handle);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002743}
2744
2745static int perf_counter_mmap_match(struct perf_counter *counter,
2746 struct perf_mmap_event *mmap_event)
2747{
Peter Zijlstrad99e9442009-06-04 17:08:58 +02002748 if (counter->attr.mmap)
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002749 return 1;
2750
2751 return 0;
2752}
2753
2754static void perf_counter_mmap_ctx(struct perf_counter_context *ctx,
2755 struct perf_mmap_event *mmap_event)
2756{
2757 struct perf_counter *counter;
2758
2759 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2760 return;
2761
2762 rcu_read_lock();
2763 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2764 if (perf_counter_mmap_match(counter, mmap_event))
2765 perf_counter_mmap_output(counter, mmap_event);
2766 }
2767 rcu_read_unlock();
2768}
2769
2770static void perf_counter_mmap_event(struct perf_mmap_event *mmap_event)
2771{
2772 struct perf_cpu_context *cpuctx;
Peter Zijlstra665c2142009-05-29 14:51:57 +02002773 struct perf_counter_context *ctx;
Peter Zijlstra089dd792009-06-05 14:04:55 +02002774 struct vm_area_struct *vma = mmap_event->vma;
2775 struct file *file = vma->vm_file;
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002776 unsigned int size;
2777 char tmp[16];
2778 char *buf = NULL;
Peter Zijlstra089dd792009-06-05 14:04:55 +02002779 const char *name;
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002780
2781 if (file) {
2782 buf = kzalloc(PATH_MAX, GFP_KERNEL);
2783 if (!buf) {
2784 name = strncpy(tmp, "//enomem", sizeof(tmp));
2785 goto got_name;
2786 }
Peter Zijlstrad3d21c42009-04-09 10:53:46 +02002787 name = d_path(&file->f_path, buf, PATH_MAX);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002788 if (IS_ERR(name)) {
2789 name = strncpy(tmp, "//toolong", sizeof(tmp));
2790 goto got_name;
2791 }
2792 } else {
Peter Zijlstra089dd792009-06-05 14:04:55 +02002793 name = arch_vma_name(mmap_event->vma);
2794 if (name)
2795 goto got_name;
2796
2797 if (!vma->vm_mm) {
2798 name = strncpy(tmp, "[vdso]", sizeof(tmp));
2799 goto got_name;
2800 }
2801
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002802 name = strncpy(tmp, "//anon", sizeof(tmp));
2803 goto got_name;
2804 }
2805
2806got_name:
Ingo Molnar888fcee2009-04-09 09:48:22 +02002807 size = ALIGN(strlen(name)+1, sizeof(u64));
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002808
2809 mmap_event->file_name = name;
2810 mmap_event->file_size = size;
2811
2812 mmap_event->event.header.size = sizeof(mmap_event->event) + size;
2813
2814 cpuctx = &get_cpu_var(perf_cpu_context);
2815 perf_counter_mmap_ctx(&cpuctx->ctx, mmap_event);
2816 put_cpu_var(perf_cpu_context);
2817
Peter Zijlstra665c2142009-05-29 14:51:57 +02002818 rcu_read_lock();
2819 /*
2820 * doesn't really matter which of the child contexts the
2821 * events ends up in.
2822 */
2823 ctx = rcu_dereference(current->perf_counter_ctxp);
2824 if (ctx)
2825 perf_counter_mmap_ctx(ctx, mmap_event);
2826 rcu_read_unlock();
2827
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002828 kfree(buf);
2829}
2830
Peter Zijlstra089dd792009-06-05 14:04:55 +02002831void __perf_counter_mmap(struct vm_area_struct *vma)
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002832{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002833 struct perf_mmap_event mmap_event;
2834
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002835 if (!atomic_read(&nr_mmap_counters))
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002836 return;
2837
2838 mmap_event = (struct perf_mmap_event){
Peter Zijlstra089dd792009-06-05 14:04:55 +02002839 .vma = vma,
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002840 .event = {
2841 .header = { .type = PERF_EVENT_MMAP, },
Peter Zijlstra089dd792009-06-05 14:04:55 +02002842 .start = vma->vm_start,
2843 .len = vma->vm_end - vma->vm_start,
2844 .pgoff = vma->vm_pgoff,
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002845 },
2846 };
2847
2848 perf_counter_mmap_event(&mmap_event);
2849}
2850
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002851/*
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002852 * Log sample_period changes so that analyzing tools can re-normalize the
Peter Zijlstrae220d2d2009-05-23 18:28:55 +02002853 * event flow.
Peter Zijlstra26b119b2009-05-20 12:21:20 +02002854 */
2855
2856static void perf_log_period(struct perf_counter *counter, u64 period)
2857{
2858 struct perf_output_handle handle;
2859 int ret;
2860
2861 struct {
2862 struct perf_event_header header;
2863 u64 time;
Peter Zijlstra689802b2009-06-05 15:05:43 +02002864 u64 id;
Peter Zijlstra26b119b2009-05-20 12:21:20 +02002865 u64 period;
2866 } freq_event = {
2867 .header = {
2868 .type = PERF_EVENT_PERIOD,
2869 .misc = 0,
2870 .size = sizeof(freq_event),
2871 },
2872 .time = sched_clock(),
Peter Zijlstra689802b2009-06-05 15:05:43 +02002873 .id = counter->id,
Peter Zijlstra26b119b2009-05-20 12:21:20 +02002874 .period = period,
2875 };
2876
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002877 if (counter->hw.sample_period == period)
Peter Zijlstra26b119b2009-05-20 12:21:20 +02002878 return;
2879
2880 ret = perf_output_begin(&handle, counter, sizeof(freq_event), 0, 0);
2881 if (ret)
2882 return;
2883
2884 perf_output_put(&handle, freq_event);
2885 perf_output_end(&handle);
2886}
2887
2888/*
Peter Zijlstraa78ac322009-05-25 17:39:05 +02002889 * IRQ throttle logging
2890 */
2891
2892static void perf_log_throttle(struct perf_counter *counter, int enable)
2893{
2894 struct perf_output_handle handle;
2895 int ret;
2896
2897 struct {
2898 struct perf_event_header header;
2899 u64 time;
2900 } throttle_event = {
2901 .header = {
2902 .type = PERF_EVENT_THROTTLE + 1,
2903 .misc = 0,
2904 .size = sizeof(throttle_event),
2905 },
2906 .time = sched_clock(),
2907 };
2908
Ingo Molnar0127c3e2009-05-25 22:03:26 +02002909 ret = perf_output_begin(&handle, counter, sizeof(throttle_event), 1, 0);
Peter Zijlstraa78ac322009-05-25 17:39:05 +02002910 if (ret)
2911 return;
2912
2913 perf_output_put(&handle, throttle_event);
2914 perf_output_end(&handle);
2915}
2916
2917/*
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002918 * Generic counter overflow handling.
2919 */
2920
2921int perf_counter_overflow(struct perf_counter *counter,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002922 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002923{
Peter Zijlstra79f14642009-04-06 11:45:07 +02002924 int events = atomic_read(&counter->event_limit);
Peter Zijlstraa78ac322009-05-25 17:39:05 +02002925 int throttle = counter->pmu->unthrottle != NULL;
Peter Zijlstra79f14642009-04-06 11:45:07 +02002926 int ret = 0;
2927
Peter Zijlstraa78ac322009-05-25 17:39:05 +02002928 if (!throttle) {
2929 counter->hw.interrupts++;
Ingo Molnar128f0482009-06-03 22:19:36 +02002930 } else {
2931 if (counter->hw.interrupts != MAX_INTERRUPTS) {
2932 counter->hw.interrupts++;
2933 if (HZ*counter->hw.interrupts > (u64)sysctl_perf_counter_limit) {
2934 counter->hw.interrupts = MAX_INTERRUPTS;
2935 perf_log_throttle(counter, 0);
2936 ret = 1;
2937 }
2938 } else {
2939 /*
2940 * Keep re-disabling counters even though on the previous
2941 * pass we disabled it - just in case we raced with a
2942 * sched-in and the counter got enabled again:
2943 */
Peter Zijlstraa78ac322009-05-25 17:39:05 +02002944 ret = 1;
2945 }
2946 }
Peter Zijlstra60db5e02009-05-15 15:19:28 +02002947
Peter Zijlstra2023b352009-05-05 17:50:26 +02002948 /*
2949 * XXX event_limit might not quite work as expected on inherited
2950 * counters
2951 */
2952
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002953 counter->pending_kill = POLL_IN;
Peter Zijlstra79f14642009-04-06 11:45:07 +02002954 if (events && atomic_dec_and_test(&counter->event_limit)) {
2955 ret = 1;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002956 counter->pending_kill = POLL_HUP;
Peter Zijlstra79f14642009-04-06 11:45:07 +02002957 if (nmi) {
2958 counter->pending_disable = 1;
2959 perf_pending_queue(&counter->pending,
2960 perf_pending_counter);
2961 } else
2962 perf_counter_disable(counter);
2963 }
2964
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002965 perf_counter_output(counter, nmi, regs, addr);
Peter Zijlstra79f14642009-04-06 11:45:07 +02002966 return ret;
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002967}
2968
2969/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002970 * Generic software counter infrastructure
2971 */
2972
2973static void perf_swcounter_update(struct perf_counter *counter)
2974{
2975 struct hw_perf_counter *hwc = &counter->hw;
2976 u64 prev, now;
2977 s64 delta;
2978
2979again:
2980 prev = atomic64_read(&hwc->prev_count);
2981 now = atomic64_read(&hwc->count);
2982 if (atomic64_cmpxchg(&hwc->prev_count, prev, now) != prev)
2983 goto again;
2984
2985 delta = now - prev;
2986
2987 atomic64_add(delta, &counter->count);
2988 atomic64_sub(delta, &hwc->period_left);
2989}
2990
2991static void perf_swcounter_set_period(struct perf_counter *counter)
2992{
2993 struct hw_perf_counter *hwc = &counter->hw;
2994 s64 left = atomic64_read(&hwc->period_left);
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002995 s64 period = hwc->sample_period;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002996
2997 if (unlikely(left <= -period)) {
2998 left = period;
2999 atomic64_set(&hwc->period_left, left);
3000 }
3001
3002 if (unlikely(left <= 0)) {
3003 left += period;
3004 atomic64_add(period, &hwc->period_left);
3005 }
3006
3007 atomic64_set(&hwc->prev_count, -left);
3008 atomic64_set(&hwc->count, -left);
3009}
3010
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003011static enum hrtimer_restart perf_swcounter_hrtimer(struct hrtimer *hrtimer)
3012{
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02003013 enum hrtimer_restart ret = HRTIMER_RESTART;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003014 struct perf_counter *counter;
3015 struct pt_regs *regs;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003016 u64 period;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003017
3018 counter = container_of(hrtimer, struct perf_counter, hw.hrtimer);
Robert Richter4aeb0b42009-04-29 12:47:03 +02003019 counter->pmu->read(counter);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003020
3021 regs = get_irq_regs();
3022 /*
3023 * In case we exclude kernel IPs or are somehow not in interrupt
3024 * context, provide the next best thing, the user IP.
3025 */
Peter Zijlstra0d486962009-06-02 19:22:16 +02003026 if ((counter->attr.exclude_kernel || !regs) &&
3027 !counter->attr.exclude_user)
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003028 regs = task_pt_regs(current);
3029
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02003030 if (regs) {
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003031 if (perf_counter_overflow(counter, 0, regs, 0))
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02003032 ret = HRTIMER_NORESTART;
3033 }
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003034
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003035 period = max_t(u64, 10000, counter->hw.sample_period);
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003036 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003037
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02003038 return ret;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003039}
3040
3041static void perf_swcounter_overflow(struct perf_counter *counter,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003042 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003043{
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003044 perf_swcounter_update(counter);
3045 perf_swcounter_set_period(counter);
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003046 if (perf_counter_overflow(counter, nmi, regs, addr))
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02003047 /* soft-disable the counter */
3048 ;
3049
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003050}
3051
Paul Mackerras880ca152009-06-01 17:49:14 +10003052static int perf_swcounter_is_counting(struct perf_counter *counter)
3053{
3054 struct perf_counter_context *ctx;
3055 unsigned long flags;
3056 int count;
3057
3058 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
3059 return 1;
3060
3061 if (counter->state != PERF_COUNTER_STATE_INACTIVE)
3062 return 0;
3063
3064 /*
3065 * If the counter is inactive, it could be just because
3066 * its task is scheduled out, or because it's in a group
3067 * which could not go on the PMU. We want to count in
3068 * the first case but not the second. If the context is
3069 * currently active then an inactive software counter must
3070 * be the second case. If it's not currently active then
3071 * we need to know whether the counter was active when the
3072 * context was last active, which we can determine by
3073 * comparing counter->tstamp_stopped with ctx->time.
3074 *
3075 * We are within an RCU read-side critical section,
3076 * which protects the existence of *ctx.
3077 */
3078 ctx = counter->ctx;
3079 spin_lock_irqsave(&ctx->lock, flags);
3080 count = 1;
3081 /* Re-check state now we have the lock */
3082 if (counter->state < PERF_COUNTER_STATE_INACTIVE ||
3083 counter->ctx->is_active ||
3084 counter->tstamp_stopped < ctx->time)
3085 count = 0;
3086 spin_unlock_irqrestore(&ctx->lock, flags);
3087 return count;
3088}
3089
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003090static int perf_swcounter_match(struct perf_counter *counter,
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003091 enum perf_event_types type,
3092 u32 event, struct pt_regs *regs)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003093{
Paul Mackerras880ca152009-06-01 17:49:14 +10003094 if (!perf_swcounter_is_counting(counter))
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003095 return 0;
3096
Ingo Molnara21ca2c2009-06-06 09:58:57 +02003097 if (counter->attr.type != type)
3098 return 0;
3099 if (counter->attr.config != event)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003100 return 0;
3101
Paul Mackerras3f731ca2009-06-01 17:52:30 +10003102 if (regs) {
Peter Zijlstra0d486962009-06-02 19:22:16 +02003103 if (counter->attr.exclude_user && user_mode(regs))
Paul Mackerras3f731ca2009-06-01 17:52:30 +10003104 return 0;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003105
Peter Zijlstra0d486962009-06-02 19:22:16 +02003106 if (counter->attr.exclude_kernel && !user_mode(regs))
Paul Mackerras3f731ca2009-06-01 17:52:30 +10003107 return 0;
3108 }
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003109
3110 return 1;
3111}
3112
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003113static void perf_swcounter_add(struct perf_counter *counter, u64 nr,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003114 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003115{
3116 int neg = atomic64_add_negative(nr, &counter->hw.count);
Ingo Molnar22a4f652009-06-01 10:13:37 +02003117
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003118 if (counter->hw.sample_period && !neg && regs)
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003119 perf_swcounter_overflow(counter, nmi, regs, addr);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003120}
3121
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003122static void perf_swcounter_ctx_event(struct perf_counter_context *ctx,
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003123 enum perf_event_types type, u32 event,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003124 u64 nr, int nmi, struct pt_regs *regs,
3125 u64 addr)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003126{
3127 struct perf_counter *counter;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003128
Peter Zijlstra01ef09d2009-03-19 20:26:11 +01003129 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003130 return;
3131
Peter Zijlstra592903c2009-03-13 12:21:36 +01003132 rcu_read_lock();
3133 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003134 if (perf_swcounter_match(counter, type, event, regs))
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003135 perf_swcounter_add(counter, nr, nmi, regs, addr);
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003136 }
Peter Zijlstra592903c2009-03-13 12:21:36 +01003137 rcu_read_unlock();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003138}
3139
Peter Zijlstra96f6d442009-03-23 18:22:07 +01003140static int *perf_swcounter_recursion_context(struct perf_cpu_context *cpuctx)
3141{
3142 if (in_nmi())
3143 return &cpuctx->recursion[3];
3144
3145 if (in_irq())
3146 return &cpuctx->recursion[2];
3147
3148 if (in_softirq())
3149 return &cpuctx->recursion[1];
3150
3151 return &cpuctx->recursion[0];
3152}
3153
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003154static void __perf_swcounter_event(enum perf_event_types type, u32 event,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003155 u64 nr, int nmi, struct pt_regs *regs,
3156 u64 addr)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003157{
3158 struct perf_cpu_context *cpuctx = &get_cpu_var(perf_cpu_context);
Peter Zijlstra96f6d442009-03-23 18:22:07 +01003159 int *recursion = perf_swcounter_recursion_context(cpuctx);
Peter Zijlstra665c2142009-05-29 14:51:57 +02003160 struct perf_counter_context *ctx;
Peter Zijlstra96f6d442009-03-23 18:22:07 +01003161
3162 if (*recursion)
3163 goto out;
3164
3165 (*recursion)++;
3166 barrier();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003167
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003168 perf_swcounter_ctx_event(&cpuctx->ctx, type, event,
3169 nr, nmi, regs, addr);
Peter Zijlstra665c2142009-05-29 14:51:57 +02003170 rcu_read_lock();
3171 /*
3172 * doesn't really matter which of the child contexts the
3173 * events ends up in.
3174 */
3175 ctx = rcu_dereference(current->perf_counter_ctxp);
3176 if (ctx)
3177 perf_swcounter_ctx_event(ctx, type, event, nr, nmi, regs, addr);
3178 rcu_read_unlock();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003179
Peter Zijlstra96f6d442009-03-23 18:22:07 +01003180 barrier();
3181 (*recursion)--;
3182
3183out:
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003184 put_cpu_var(perf_cpu_context);
3185}
3186
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003187void
3188perf_swcounter_event(u32 event, u64 nr, int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003189{
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003190 __perf_swcounter_event(PERF_TYPE_SOFTWARE, event, nr, nmi, regs, addr);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003191}
3192
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003193static void perf_swcounter_read(struct perf_counter *counter)
3194{
3195 perf_swcounter_update(counter);
3196}
3197
3198static int perf_swcounter_enable(struct perf_counter *counter)
3199{
3200 perf_swcounter_set_period(counter);
3201 return 0;
3202}
3203
3204static void perf_swcounter_disable(struct perf_counter *counter)
3205{
3206 perf_swcounter_update(counter);
3207}
3208
Robert Richter4aeb0b42009-04-29 12:47:03 +02003209static const struct pmu perf_ops_generic = {
Peter Zijlstraac17dc82009-03-13 12:21:34 +01003210 .enable = perf_swcounter_enable,
3211 .disable = perf_swcounter_disable,
3212 .read = perf_swcounter_read,
3213};
3214
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003215/*
3216 * Software counter: cpu wall time clock
3217 */
3218
Paul Mackerras9abf8a02009-01-09 16:26:43 +11003219static void cpu_clock_perf_counter_update(struct perf_counter *counter)
3220{
3221 int cpu = raw_smp_processor_id();
3222 s64 prev;
3223 u64 now;
3224
3225 now = cpu_clock(cpu);
3226 prev = atomic64_read(&counter->hw.prev_count);
3227 atomic64_set(&counter->hw.prev_count, now);
3228 atomic64_add(now - prev, &counter->count);
3229}
3230
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003231static int cpu_clock_perf_counter_enable(struct perf_counter *counter)
3232{
3233 struct hw_perf_counter *hwc = &counter->hw;
3234 int cpu = raw_smp_processor_id();
3235
3236 atomic64_set(&hwc->prev_count, cpu_clock(cpu));
Peter Zijlstra039fc912009-03-13 16:43:47 +01003237 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
3238 hwc->hrtimer.function = perf_swcounter_hrtimer;
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003239 if (hwc->sample_period) {
3240 u64 period = max_t(u64, 10000, hwc->sample_period);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003241 __hrtimer_start_range_ns(&hwc->hrtimer,
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003242 ns_to_ktime(period), 0,
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003243 HRTIMER_MODE_REL, 0);
3244 }
3245
3246 return 0;
3247}
3248
Ingo Molnar5c92d122008-12-11 13:21:10 +01003249static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
3250{
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003251 if (counter->hw.sample_period)
Peter Zijlstrab986d7e2009-05-20 12:21:21 +02003252 hrtimer_cancel(&counter->hw.hrtimer);
Paul Mackerras9abf8a02009-01-09 16:26:43 +11003253 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01003254}
3255
3256static void cpu_clock_perf_counter_read(struct perf_counter *counter)
3257{
Paul Mackerras9abf8a02009-01-09 16:26:43 +11003258 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01003259}
3260
Robert Richter4aeb0b42009-04-29 12:47:03 +02003261static const struct pmu perf_ops_cpu_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +01003262 .enable = cpu_clock_perf_counter_enable,
3263 .disable = cpu_clock_perf_counter_disable,
3264 .read = cpu_clock_perf_counter_read,
Ingo Molnar5c92d122008-12-11 13:21:10 +01003265};
3266
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01003267/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003268 * Software counter: task time clock
3269 */
3270
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02003271static void task_clock_perf_counter_update(struct perf_counter *counter, u64 now)
Ingo Molnarbae43c92008-12-11 14:03:20 +01003272{
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02003273 u64 prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003274 s64 delta;
Ingo Molnarbae43c92008-12-11 14:03:20 +01003275
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02003276 prev = atomic64_xchg(&counter->hw.prev_count, now);
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003277 delta = now - prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003278 atomic64_add(delta, &counter->count);
Ingo Molnarbae43c92008-12-11 14:03:20 +01003279}
3280
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01003281static int task_clock_perf_counter_enable(struct perf_counter *counter)
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003282{
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003283 struct hw_perf_counter *hwc = &counter->hw;
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02003284 u64 now;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003285
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02003286 now = counter->ctx->time;
3287
3288 atomic64_set(&hwc->prev_count, now);
Peter Zijlstra039fc912009-03-13 16:43:47 +01003289 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
3290 hwc->hrtimer.function = perf_swcounter_hrtimer;
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003291 if (hwc->sample_period) {
3292 u64 period = max_t(u64, 10000, hwc->sample_period);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003293 __hrtimer_start_range_ns(&hwc->hrtimer,
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003294 ns_to_ktime(period), 0,
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003295 HRTIMER_MODE_REL, 0);
3296 }
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01003297
3298 return 0;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003299}
3300
3301static void task_clock_perf_counter_disable(struct perf_counter *counter)
3302{
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003303 if (counter->hw.sample_period)
Peter Zijlstrab986d7e2009-05-20 12:21:21 +02003304 hrtimer_cancel(&counter->hw.hrtimer);
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02003305 task_clock_perf_counter_update(counter, counter->ctx->time);
3306
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003307}
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01003308
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003309static void task_clock_perf_counter_read(struct perf_counter *counter)
3310{
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02003311 u64 time;
3312
3313 if (!in_nmi()) {
3314 update_context_time(counter->ctx);
3315 time = counter->ctx->time;
3316 } else {
3317 u64 now = perf_clock();
3318 u64 delta = now - counter->ctx->timestamp;
3319 time = counter->ctx->time + delta;
3320 }
3321
3322 task_clock_perf_counter_update(counter, time);
Ingo Molnarbae43c92008-12-11 14:03:20 +01003323}
3324
Robert Richter4aeb0b42009-04-29 12:47:03 +02003325static const struct pmu perf_ops_task_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +01003326 .enable = task_clock_perf_counter_enable,
3327 .disable = task_clock_perf_counter_disable,
3328 .read = task_clock_perf_counter_read,
Ingo Molnarbae43c92008-12-11 14:03:20 +01003329};
3330
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003331/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003332 * Software counter: cpu migrations
3333 */
Paul Mackerras3f731ca2009-06-01 17:52:30 +10003334void perf_counter_task_migration(struct task_struct *task, int cpu)
Ingo Molnar6c594c22008-12-14 12:34:15 +01003335{
Paul Mackerras3f731ca2009-06-01 17:52:30 +10003336 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
3337 struct perf_counter_context *ctx;
Paul Mackerras23a185c2009-02-09 22:42:47 +11003338
Paul Mackerras3f731ca2009-06-01 17:52:30 +10003339 perf_swcounter_ctx_event(&cpuctx->ctx, PERF_TYPE_SOFTWARE,
3340 PERF_COUNT_CPU_MIGRATIONS,
3341 1, 1, NULL, 0);
3342
3343 ctx = perf_pin_task_context(task);
3344 if (ctx) {
3345 perf_swcounter_ctx_event(ctx, PERF_TYPE_SOFTWARE,
3346 PERF_COUNT_CPU_MIGRATIONS,
3347 1, 1, NULL, 0);
3348 perf_unpin_context(ctx);
3349 }
Ingo Molnar6c594c22008-12-14 12:34:15 +01003350}
3351
Peter Zijlstrae077df42009-03-19 20:26:17 +01003352#ifdef CONFIG_EVENT_PROFILE
3353void perf_tpcounter_event(int event_id)
3354{
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003355 struct pt_regs *regs = get_irq_regs();
3356
3357 if (!regs)
3358 regs = task_pt_regs(current);
3359
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003360 __perf_swcounter_event(PERF_TYPE_TRACEPOINT, event_id, 1, 1, regs, 0);
Peter Zijlstrae077df42009-03-19 20:26:17 +01003361}
Steven Whitehouseff7b1b42009-04-15 16:55:05 +01003362EXPORT_SYMBOL_GPL(perf_tpcounter_event);
Peter Zijlstrae077df42009-03-19 20:26:17 +01003363
3364extern int ftrace_profile_enable(int);
3365extern void ftrace_profile_disable(int);
3366
3367static void tp_perf_counter_destroy(struct perf_counter *counter)
3368{
Peter Zijlstra0d486962009-06-02 19:22:16 +02003369 ftrace_profile_disable(perf_event_id(&counter->attr));
Peter Zijlstrae077df42009-03-19 20:26:17 +01003370}
3371
Robert Richter4aeb0b42009-04-29 12:47:03 +02003372static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
Peter Zijlstrae077df42009-03-19 20:26:17 +01003373{
Peter Zijlstra0d486962009-06-02 19:22:16 +02003374 int event_id = perf_event_id(&counter->attr);
Peter Zijlstrae077df42009-03-19 20:26:17 +01003375 int ret;
3376
3377 ret = ftrace_profile_enable(event_id);
3378 if (ret)
3379 return NULL;
3380
3381 counter->destroy = tp_perf_counter_destroy;
Peter Zijlstra0d486962009-06-02 19:22:16 +02003382 counter->hw.sample_period = counter->attr.sample_period;
Peter Zijlstrae077df42009-03-19 20:26:17 +01003383
3384 return &perf_ops_generic;
3385}
3386#else
Robert Richter4aeb0b42009-04-29 12:47:03 +02003387static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
Peter Zijlstrae077df42009-03-19 20:26:17 +01003388{
3389 return NULL;
3390}
3391#endif
3392
Robert Richter4aeb0b42009-04-29 12:47:03 +02003393static const struct pmu *sw_perf_counter_init(struct perf_counter *counter)
Ingo Molnar5c92d122008-12-11 13:21:10 +01003394{
Robert Richter4aeb0b42009-04-29 12:47:03 +02003395 const struct pmu *pmu = NULL;
Ingo Molnar5c92d122008-12-11 13:21:10 +01003396
Paul Mackerras0475f9e2009-02-11 14:35:35 +11003397 /*
3398 * Software counters (currently) can't in general distinguish
3399 * between user, kernel and hypervisor events.
3400 * However, context switches and cpu migrations are considered
3401 * to be kernel events, and page faults are never hypervisor
3402 * events.
3403 */
Ingo Molnara21ca2c2009-06-06 09:58:57 +02003404 switch (counter->attr.config) {
Ingo Molnar5c92d122008-12-11 13:21:10 +01003405 case PERF_COUNT_CPU_CLOCK:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003406 pmu = &perf_ops_cpu_clock;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003407
Ingo Molnar5c92d122008-12-11 13:21:10 +01003408 break;
Ingo Molnarbae43c92008-12-11 14:03:20 +01003409 case PERF_COUNT_TASK_CLOCK:
Paul Mackerras23a185c2009-02-09 22:42:47 +11003410 /*
3411 * If the user instantiates this as a per-cpu counter,
3412 * use the cpu_clock counter instead.
3413 */
3414 if (counter->ctx->task)
Robert Richter4aeb0b42009-04-29 12:47:03 +02003415 pmu = &perf_ops_task_clock;
Paul Mackerras23a185c2009-02-09 22:42:47 +11003416 else
Robert Richter4aeb0b42009-04-29 12:47:03 +02003417 pmu = &perf_ops_cpu_clock;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003418
Ingo Molnarbae43c92008-12-11 14:03:20 +01003419 break;
Ingo Molnare06c61a2008-12-14 14:44:31 +01003420 case PERF_COUNT_PAGE_FAULTS:
Peter Zijlstraac17dc82009-03-13 12:21:34 +01003421 case PERF_COUNT_PAGE_FAULTS_MIN:
3422 case PERF_COUNT_PAGE_FAULTS_MAJ:
Ingo Molnar5d6a27d2008-12-14 12:28:33 +01003423 case PERF_COUNT_CONTEXT_SWITCHES:
Ingo Molnar6c594c22008-12-14 12:34:15 +01003424 case PERF_COUNT_CPU_MIGRATIONS:
Paul Mackerras3f731ca2009-06-01 17:52:30 +10003425 pmu = &perf_ops_generic;
Ingo Molnar6c594c22008-12-14 12:34:15 +01003426 break;
Ingo Molnar5c92d122008-12-11 13:21:10 +01003427 }
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003428
Robert Richter4aeb0b42009-04-29 12:47:03 +02003429 return pmu;
Ingo Molnar5c92d122008-12-11 13:21:10 +01003430}
3431
Thomas Gleixner0793a612008-12-04 20:12:29 +01003432/*
3433 * Allocate and initialize a counter structure
3434 */
3435static struct perf_counter *
Peter Zijlstra0d486962009-06-02 19:22:16 +02003436perf_counter_alloc(struct perf_counter_attr *attr,
Ingo Molnar04289bb2008-12-11 08:38:42 +01003437 int cpu,
Paul Mackerras23a185c2009-02-09 22:42:47 +11003438 struct perf_counter_context *ctx,
Ingo Molnar9b51f662008-12-12 13:49:45 +01003439 struct perf_counter *group_leader,
3440 gfp_t gfpflags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003441{
Robert Richter4aeb0b42009-04-29 12:47:03 +02003442 const struct pmu *pmu;
Ingo Molnar621a01e2008-12-11 12:46:46 +01003443 struct perf_counter *counter;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003444 struct hw_perf_counter *hwc;
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003445 long err;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003446
Ingo Molnar9b51f662008-12-12 13:49:45 +01003447 counter = kzalloc(sizeof(*counter), gfpflags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003448 if (!counter)
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003449 return ERR_PTR(-ENOMEM);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003450
Ingo Molnar04289bb2008-12-11 08:38:42 +01003451 /*
3452 * Single counters are their own group leaders, with an
3453 * empty sibling list:
3454 */
3455 if (!group_leader)
3456 group_leader = counter;
3457
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003458 mutex_init(&counter->child_mutex);
3459 INIT_LIST_HEAD(&counter->child_list);
3460
Ingo Molnar04289bb2008-12-11 08:38:42 +01003461 INIT_LIST_HEAD(&counter->list_entry);
Peter Zijlstra592903c2009-03-13 12:21:36 +01003462 INIT_LIST_HEAD(&counter->event_entry);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003463 INIT_LIST_HEAD(&counter->sibling_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003464 init_waitqueue_head(&counter->waitq);
3465
Peter Zijlstra7b732a72009-03-23 18:22:10 +01003466 mutex_init(&counter->mmap_mutex);
3467
Peter Zijlstraa96bbc12009-06-03 14:01:36 +02003468 counter->cpu = cpu;
Peter Zijlstra0d486962009-06-02 19:22:16 +02003469 counter->attr = *attr;
Peter Zijlstraa96bbc12009-06-03 14:01:36 +02003470 counter->group_leader = group_leader;
3471 counter->pmu = NULL;
3472 counter->ctx = ctx;
3473 counter->oncpu = -1;
Ingo Molnar329d8762009-05-26 08:10:00 +02003474
Peter Zijlstraa96bbc12009-06-03 14:01:36 +02003475 counter->ns = get_pid_ns(current->nsproxy->pid_ns);
3476 counter->id = atomic64_inc_return(&perf_counter_id);
3477
3478 counter->state = PERF_COUNTER_STATE_INACTIVE;
3479
Peter Zijlstra0d486962009-06-02 19:22:16 +02003480 if (attr->disabled)
Ingo Molnara86ed502008-12-17 00:43:10 +01003481 counter->state = PERF_COUNTER_STATE_OFF;
3482
Robert Richter4aeb0b42009-04-29 12:47:03 +02003483 pmu = NULL;
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003484
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003485 hwc = &counter->hw;
Peter Zijlstra0d486962009-06-02 19:22:16 +02003486 if (attr->freq && attr->sample_freq)
3487 hwc->sample_period = div64_u64(TICK_NSEC, attr->sample_freq);
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003488 else
Peter Zijlstra0d486962009-06-02 19:22:16 +02003489 hwc->sample_period = attr->sample_period;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003490
Peter Zijlstra2023b352009-05-05 17:50:26 +02003491 /*
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003492 * we currently do not support PERF_SAMPLE_GROUP on inherited counters
Peter Zijlstra2023b352009-05-05 17:50:26 +02003493 */
Peter Zijlstra0d486962009-06-02 19:22:16 +02003494 if (attr->inherit && (attr->sample_type & PERF_SAMPLE_GROUP))
Peter Zijlstra2023b352009-05-05 17:50:26 +02003495 goto done;
3496
Ingo Molnara21ca2c2009-06-06 09:58:57 +02003497 if (attr->type == PERF_TYPE_RAW) {
Robert Richter4aeb0b42009-04-29 12:47:03 +02003498 pmu = hw_perf_counter_init(counter);
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01003499 goto done;
3500 }
3501
Ingo Molnara21ca2c2009-06-06 09:58:57 +02003502 switch (attr->type) {
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003503 case PERF_TYPE_HARDWARE:
Ingo Molnar8326f442009-06-05 20:22:46 +02003504 case PERF_TYPE_HW_CACHE:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003505 pmu = hw_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003506 break;
3507
3508 case PERF_TYPE_SOFTWARE:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003509 pmu = sw_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003510 break;
3511
3512 case PERF_TYPE_TRACEPOINT:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003513 pmu = tp_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003514 break;
3515 }
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01003516done:
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003517 err = 0;
Robert Richter4aeb0b42009-04-29 12:47:03 +02003518 if (!pmu)
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003519 err = -EINVAL;
Robert Richter4aeb0b42009-04-29 12:47:03 +02003520 else if (IS_ERR(pmu))
3521 err = PTR_ERR(pmu);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003522
3523 if (err) {
Peter Zijlstraa96bbc12009-06-03 14:01:36 +02003524 if (counter->ns)
3525 put_pid_ns(counter->ns);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003526 kfree(counter);
3527 return ERR_PTR(err);
3528 }
3529
Robert Richter4aeb0b42009-04-29 12:47:03 +02003530 counter->pmu = pmu;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003531
Peter Zijlstra7fc23a52009-05-08 18:52:21 +02003532 atomic_inc(&nr_counters);
Peter Zijlstra0d486962009-06-02 19:22:16 +02003533 if (counter->attr.mmap)
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003534 atomic_inc(&nr_mmap_counters);
Peter Zijlstra0d486962009-06-02 19:22:16 +02003535 if (counter->attr.comm)
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003536 atomic_inc(&nr_comm_counters);
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02003537
Thomas Gleixner0793a612008-12-04 20:12:29 +01003538 return counter;
3539}
3540
3541/**
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003542 * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
Ingo Molnar9f66a382008-12-10 12:33:23 +01003543 *
Peter Zijlstra0d486962009-06-02 19:22:16 +02003544 * @attr_uptr: event type attributes for monitoring/sampling
Thomas Gleixner0793a612008-12-04 20:12:29 +01003545 * @pid: target pid
Ingo Molnar9f66a382008-12-10 12:33:23 +01003546 * @cpu: target cpu
3547 * @group_fd: group leader counter fd
Thomas Gleixner0793a612008-12-04 20:12:29 +01003548 */
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003549SYSCALL_DEFINE5(perf_counter_open,
Peter Zijlstra0d486962009-06-02 19:22:16 +02003550 const struct perf_counter_attr __user *, attr_uptr,
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003551 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003552{
Ingo Molnar04289bb2008-12-11 08:38:42 +01003553 struct perf_counter *counter, *group_leader;
Peter Zijlstra0d486962009-06-02 19:22:16 +02003554 struct perf_counter_attr attr;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003555 struct perf_counter_context *ctx;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003556 struct file *counter_file = NULL;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003557 struct file *group_file = NULL;
3558 int fput_needed = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003559 int fput_needed2 = 0;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003560 int ret;
3561
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003562 /* for future expandability... */
3563 if (flags)
3564 return -EINVAL;
3565
Peter Zijlstra0d486962009-06-02 19:22:16 +02003566 if (copy_from_user(&attr, attr_uptr, sizeof(attr)) != 0)
Thomas Gleixnereab656a2008-12-08 19:26:59 +01003567 return -EFAULT;
3568
Ingo Molnar04289bb2008-12-11 08:38:42 +01003569 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01003570 * Get the target context (task or percpu):
3571 */
3572 ctx = find_get_context(pid, cpu);
3573 if (IS_ERR(ctx))
3574 return PTR_ERR(ctx);
3575
3576 /*
3577 * Look up the group leader (we will attach this counter to it):
Ingo Molnar04289bb2008-12-11 08:38:42 +01003578 */
3579 group_leader = NULL;
3580 if (group_fd != -1) {
3581 ret = -EINVAL;
3582 group_file = fget_light(group_fd, &fput_needed);
3583 if (!group_file)
Ingo Molnarccff2862008-12-11 11:26:29 +01003584 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003585 if (group_file->f_op != &perf_fops)
Ingo Molnarccff2862008-12-11 11:26:29 +01003586 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003587
3588 group_leader = group_file->private_data;
3589 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01003590 * Do not allow a recursive hierarchy (this new sibling
3591 * becoming part of another group-sibling):
Ingo Molnar04289bb2008-12-11 08:38:42 +01003592 */
Ingo Molnarccff2862008-12-11 11:26:29 +01003593 if (group_leader->group_leader != group_leader)
3594 goto err_put_context;
3595 /*
3596 * Do not allow to attach to a group in a different
3597 * task or CPU context:
3598 */
3599 if (group_leader->ctx != ctx)
3600 goto err_put_context;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11003601 /*
3602 * Only a group leader can be exclusive or pinned
3603 */
Peter Zijlstra0d486962009-06-02 19:22:16 +02003604 if (attr.exclusive || attr.pinned)
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11003605 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003606 }
3607
Peter Zijlstra0d486962009-06-02 19:22:16 +02003608 counter = perf_counter_alloc(&attr, cpu, ctx, group_leader,
Paul Mackerras23a185c2009-02-09 22:42:47 +11003609 GFP_KERNEL);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003610 ret = PTR_ERR(counter);
3611 if (IS_ERR(counter))
Thomas Gleixner0793a612008-12-04 20:12:29 +01003612 goto err_put_context;
3613
Thomas Gleixner0793a612008-12-04 20:12:29 +01003614 ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
3615 if (ret < 0)
Ingo Molnar9b51f662008-12-12 13:49:45 +01003616 goto err_free_put_context;
3617
3618 counter_file = fget_light(ret, &fput_needed2);
3619 if (!counter_file)
3620 goto err_free_put_context;
3621
3622 counter->filp = counter_file;
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003623 WARN_ON_ONCE(ctx->parent_ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003624 mutex_lock(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003625 perf_install_in_context(ctx, counter, cpu);
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003626 ++ctx->generation;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003627 mutex_unlock(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003628
Peter Zijlstra082ff5a2009-05-23 18:29:00 +02003629 counter->owner = current;
3630 get_task_struct(current);
3631 mutex_lock(&current->perf_counter_mutex);
3632 list_add_tail(&counter->owner_entry, &current->perf_counter_list);
3633 mutex_unlock(&current->perf_counter_mutex);
3634
Ingo Molnar9b51f662008-12-12 13:49:45 +01003635 fput_light(counter_file, fput_needed2);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003636
Ingo Molnar04289bb2008-12-11 08:38:42 +01003637out_fput:
3638 fput_light(group_file, fput_needed);
3639
Thomas Gleixner0793a612008-12-04 20:12:29 +01003640 return ret;
3641
Ingo Molnar9b51f662008-12-12 13:49:45 +01003642err_free_put_context:
Thomas Gleixner0793a612008-12-04 20:12:29 +01003643 kfree(counter);
3644
3645err_put_context:
Paul Mackerrasc93f7662009-05-28 22:18:17 +10003646 put_ctx(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003647
Ingo Molnar04289bb2008-12-11 08:38:42 +01003648 goto out_fput;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003649}
3650
Ingo Molnar9b51f662008-12-12 13:49:45 +01003651/*
Ingo Molnar9b51f662008-12-12 13:49:45 +01003652 * inherit a counter from parent task to child task:
3653 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11003654static struct perf_counter *
Ingo Molnar9b51f662008-12-12 13:49:45 +01003655inherit_counter(struct perf_counter *parent_counter,
3656 struct task_struct *parent,
3657 struct perf_counter_context *parent_ctx,
3658 struct task_struct *child,
Paul Mackerrasd859e292009-01-17 18:10:22 +11003659 struct perf_counter *group_leader,
Ingo Molnar9b51f662008-12-12 13:49:45 +01003660 struct perf_counter_context *child_ctx)
3661{
3662 struct perf_counter *child_counter;
3663
Paul Mackerrasd859e292009-01-17 18:10:22 +11003664 /*
3665 * Instead of creating recursive hierarchies of counters,
3666 * we link inherited counters back to the original parent,
3667 * which has a filp for sure, which we use as the reference
3668 * count:
3669 */
3670 if (parent_counter->parent)
3671 parent_counter = parent_counter->parent;
3672
Peter Zijlstra0d486962009-06-02 19:22:16 +02003673 child_counter = perf_counter_alloc(&parent_counter->attr,
Paul Mackerras23a185c2009-02-09 22:42:47 +11003674 parent_counter->cpu, child_ctx,
3675 group_leader, GFP_KERNEL);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003676 if (IS_ERR(child_counter))
3677 return child_counter;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10003678 get_ctx(child_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003679
3680 /*
Paul Mackerras564c2b22009-05-22 14:27:22 +10003681 * Make the child state follow the state of the parent counter,
Peter Zijlstra0d486962009-06-02 19:22:16 +02003682 * not its attr.disabled bit. We hold the parent's mutex,
Ingo Molnar22a4f652009-06-01 10:13:37 +02003683 * so we won't race with perf_counter_{en, dis}able_family.
Paul Mackerras564c2b22009-05-22 14:27:22 +10003684 */
3685 if (parent_counter->state >= PERF_COUNTER_STATE_INACTIVE)
3686 child_counter->state = PERF_COUNTER_STATE_INACTIVE;
3687 else
3688 child_counter->state = PERF_COUNTER_STATE_OFF;
3689
3690 /*
Ingo Molnar9b51f662008-12-12 13:49:45 +01003691 * Link it up in the child's context:
3692 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +11003693 add_counter_to_ctx(child_counter, child_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003694
3695 child_counter->parent = parent_counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003696 /*
3697 * inherit into child's child as well:
3698 */
Peter Zijlstra0d486962009-06-02 19:22:16 +02003699 child_counter->attr.inherit = 1;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003700
3701 /*
3702 * Get a reference to the parent filp - we will fput it
3703 * when the child counter exits. This is safe to do because
3704 * we are in the parent and we know that the filp still
3705 * exists and has a nonzero count:
3706 */
3707 atomic_long_inc(&parent_counter->filp->f_count);
3708
Paul Mackerrasd859e292009-01-17 18:10:22 +11003709 /*
3710 * Link this into the parent counter's child list
3711 */
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003712 WARN_ON_ONCE(parent_counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003713 mutex_lock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003714 list_add_tail(&child_counter->child_list, &parent_counter->child_list);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003715 mutex_unlock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003716
3717 return child_counter;
3718}
3719
3720static int inherit_group(struct perf_counter *parent_counter,
3721 struct task_struct *parent,
3722 struct perf_counter_context *parent_ctx,
3723 struct task_struct *child,
3724 struct perf_counter_context *child_ctx)
3725{
3726 struct perf_counter *leader;
3727 struct perf_counter *sub;
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003728 struct perf_counter *child_ctr;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003729
3730 leader = inherit_counter(parent_counter, parent, parent_ctx,
3731 child, NULL, child_ctx);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003732 if (IS_ERR(leader))
3733 return PTR_ERR(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003734 list_for_each_entry(sub, &parent_counter->sibling_list, list_entry) {
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003735 child_ctr = inherit_counter(sub, parent, parent_ctx,
3736 child, leader, child_ctx);
3737 if (IS_ERR(child_ctr))
3738 return PTR_ERR(child_ctr);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003739 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01003740 return 0;
3741}
3742
Paul Mackerrasd859e292009-01-17 18:10:22 +11003743static void sync_child_counter(struct perf_counter *child_counter,
3744 struct perf_counter *parent_counter)
3745{
Peter Zijlstra8bc20952009-05-15 20:45:59 +02003746 u64 child_val;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003747
Paul Mackerrasd859e292009-01-17 18:10:22 +11003748 child_val = atomic64_read(&child_counter->count);
3749
3750 /*
3751 * Add back the child's count to the parent's count:
3752 */
3753 atomic64_add(child_val, &parent_counter->count);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11003754 atomic64_add(child_counter->total_time_enabled,
3755 &parent_counter->child_total_time_enabled);
3756 atomic64_add(child_counter->total_time_running,
3757 &parent_counter->child_total_time_running);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003758
3759 /*
3760 * Remove this counter from the parent's list
3761 */
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003762 WARN_ON_ONCE(parent_counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003763 mutex_lock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003764 list_del_init(&child_counter->child_list);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003765 mutex_unlock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003766
3767 /*
3768 * Release the parent counter, if this was the last
3769 * reference to it.
3770 */
3771 fput(parent_counter->filp);
3772}
3773
Ingo Molnar9b51f662008-12-12 13:49:45 +01003774static void
Peter Zijlstrabbbee902009-05-29 14:25:58 +02003775__perf_counter_exit_task(struct perf_counter *child_counter,
Ingo Molnar9b51f662008-12-12 13:49:45 +01003776 struct perf_counter_context *child_ctx)
3777{
3778 struct perf_counter *parent_counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003779
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003780 update_counter_times(child_counter);
Peter Zijlstraaa9c67f2009-05-23 18:28:59 +02003781 perf_counter_remove_from_context(child_counter);
Ingo Molnar0cc0c022008-12-14 23:20:36 +01003782
Ingo Molnar9b51f662008-12-12 13:49:45 +01003783 parent_counter = child_counter->parent;
3784 /*
3785 * It can happen that parent exits first, and has counters
3786 * that are still around due to the child reference. These
3787 * counters need to be zapped - but otherwise linger.
3788 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11003789 if (parent_counter) {
3790 sync_child_counter(child_counter, parent_counter);
Peter Zijlstraf1600952009-03-19 20:26:16 +01003791 free_counter(child_counter);
Paul Mackerras4bcf3492009-02-11 13:53:19 +01003792 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01003793}
3794
3795/*
Paul Mackerrasd859e292009-01-17 18:10:22 +11003796 * When a child task exits, feed back counter values to parent counters.
Ingo Molnar9b51f662008-12-12 13:49:45 +01003797 */
3798void perf_counter_exit_task(struct task_struct *child)
3799{
3800 struct perf_counter *child_counter, *tmp;
3801 struct perf_counter_context *child_ctx;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003802 unsigned long flags;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003803
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003804 if (likely(!child->perf_counter_ctxp))
Ingo Molnar9b51f662008-12-12 13:49:45 +01003805 return;
3806
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003807 local_irq_save(flags);
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003808 /*
3809 * We can't reschedule here because interrupts are disabled,
3810 * and either child is current or it is a task that can't be
3811 * scheduled, so we are now safe from rescheduling changing
3812 * our context.
3813 */
3814 child_ctx = child->perf_counter_ctxp;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003815 __perf_counter_task_sched_out(child_ctx);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10003816
3817 /*
3818 * Take the context lock here so that if find_get_context is
3819 * reading child->perf_counter_ctxp, we wait until it has
3820 * incremented the context's refcount before we do put_ctx below.
3821 */
3822 spin_lock(&child_ctx->lock);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003823 child->perf_counter_ctxp = NULL;
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003824 if (child_ctx->parent_ctx) {
3825 /*
3826 * This context is a clone; unclone it so it can't get
3827 * swapped to another process while we're removing all
3828 * the counters from it.
3829 */
3830 put_ctx(child_ctx->parent_ctx);
3831 child_ctx->parent_ctx = NULL;
3832 }
Paul Mackerrasc93f7662009-05-28 22:18:17 +10003833 spin_unlock(&child_ctx->lock);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003834 local_irq_restore(flags);
3835
3836 mutex_lock(&child_ctx->mutex);
3837
Peter Zijlstra8bc20952009-05-15 20:45:59 +02003838again:
Ingo Molnar9b51f662008-12-12 13:49:45 +01003839 list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list,
3840 list_entry)
Peter Zijlstrabbbee902009-05-29 14:25:58 +02003841 __perf_counter_exit_task(child_counter, child_ctx);
Peter Zijlstra8bc20952009-05-15 20:45:59 +02003842
3843 /*
3844 * If the last counter was a group counter, it will have appended all
3845 * its siblings to the list, but we obtained 'tmp' before that which
3846 * will still point to the list head terminating the iteration.
3847 */
3848 if (!list_empty(&child_ctx->counter_list))
3849 goto again;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003850
3851 mutex_unlock(&child_ctx->mutex);
3852
3853 put_ctx(child_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003854}
3855
3856/*
Peter Zijlstrabbbee902009-05-29 14:25:58 +02003857 * free an unexposed, unused context as created by inheritance by
3858 * init_task below, used by fork() in case of fail.
3859 */
3860void perf_counter_free_task(struct task_struct *task)
3861{
3862 struct perf_counter_context *ctx = task->perf_counter_ctxp;
3863 struct perf_counter *counter, *tmp;
3864
3865 if (!ctx)
3866 return;
3867
3868 mutex_lock(&ctx->mutex);
3869again:
3870 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry) {
3871 struct perf_counter *parent = counter->parent;
3872
3873 if (WARN_ON_ONCE(!parent))
3874 continue;
3875
3876 mutex_lock(&parent->child_mutex);
3877 list_del_init(&counter->child_list);
3878 mutex_unlock(&parent->child_mutex);
3879
3880 fput(parent->filp);
3881
3882 list_del_counter(counter, ctx);
3883 free_counter(counter);
3884 }
3885
3886 if (!list_empty(&ctx->counter_list))
3887 goto again;
3888
3889 mutex_unlock(&ctx->mutex);
3890
3891 put_ctx(ctx);
3892}
3893
3894/*
Ingo Molnar9b51f662008-12-12 13:49:45 +01003895 * Initialize the perf_counter context in task_struct
3896 */
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02003897int perf_counter_init_task(struct task_struct *child)
Ingo Molnar9b51f662008-12-12 13:49:45 +01003898{
3899 struct perf_counter_context *child_ctx, *parent_ctx;
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003900 struct perf_counter_context *cloned_ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003901 struct perf_counter *counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003902 struct task_struct *parent = current;
Paul Mackerras564c2b22009-05-22 14:27:22 +10003903 int inherited_all = 1;
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02003904 int ret = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003905
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003906 child->perf_counter_ctxp = NULL;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003907
Peter Zijlstra082ff5a2009-05-23 18:29:00 +02003908 mutex_init(&child->perf_counter_mutex);
3909 INIT_LIST_HEAD(&child->perf_counter_list);
3910
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003911 if (likely(!parent->perf_counter_ctxp))
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02003912 return 0;
3913
Ingo Molnar9b51f662008-12-12 13:49:45 +01003914 /*
3915 * This is executed from the parent task context, so inherit
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003916 * counters that have been marked for cloning.
3917 * First allocate and initialize a context for the child.
Ingo Molnar9b51f662008-12-12 13:49:45 +01003918 */
3919
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003920 child_ctx = kmalloc(sizeof(struct perf_counter_context), GFP_KERNEL);
3921 if (!child_ctx)
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02003922 return -ENOMEM;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003923
3924 __perf_counter_init_context(child_ctx, child);
3925 child->perf_counter_ctxp = child_ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10003926 get_task_struct(child);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003927
Ingo Molnar9b51f662008-12-12 13:49:45 +01003928 /*
Paul Mackerras25346b932009-06-01 17:48:12 +10003929 * If the parent's context is a clone, pin it so it won't get
3930 * swapped under us.
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003931 */
Paul Mackerras25346b932009-06-01 17:48:12 +10003932 parent_ctx = perf_pin_task_context(parent);
3933
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003934 /*
3935 * No need to check if parent_ctx != NULL here; since we saw
3936 * it non-NULL earlier, the only reason for it to become NULL
3937 * is if we exit, and since we're currently in the middle of
3938 * a fork we can't be exiting at the same time.
3939 */
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003940
3941 /*
Ingo Molnar9b51f662008-12-12 13:49:45 +01003942 * Lock the parent list. No need to lock the child - not PID
3943 * hashed yet and not running, so nobody can access it.
3944 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11003945 mutex_lock(&parent_ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003946
3947 /*
3948 * We dont have to disable NMIs - we are only looking at
3949 * the list, not manipulating it:
3950 */
Peter Zijlstrad7b629a2009-05-20 12:21:19 +02003951 list_for_each_entry_rcu(counter, &parent_ctx->event_list, event_entry) {
3952 if (counter != counter->group_leader)
3953 continue;
3954
Peter Zijlstra0d486962009-06-02 19:22:16 +02003955 if (!counter->attr.inherit) {
Paul Mackerras564c2b22009-05-22 14:27:22 +10003956 inherited_all = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003957 continue;
Paul Mackerras564c2b22009-05-22 14:27:22 +10003958 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01003959
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02003960 ret = inherit_group(counter, parent, parent_ctx,
3961 child, child_ctx);
3962 if (ret) {
Paul Mackerras564c2b22009-05-22 14:27:22 +10003963 inherited_all = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003964 break;
Paul Mackerras564c2b22009-05-22 14:27:22 +10003965 }
3966 }
3967
3968 if (inherited_all) {
3969 /*
3970 * Mark the child context as a clone of the parent
3971 * context, or of whatever the parent is a clone of.
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003972 * Note that if the parent is a clone, it could get
3973 * uncloned at any point, but that doesn't matter
3974 * because the list of counters and the generation
3975 * count can't have changed since we took the mutex.
Paul Mackerras564c2b22009-05-22 14:27:22 +10003976 */
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003977 cloned_ctx = rcu_dereference(parent_ctx->parent_ctx);
3978 if (cloned_ctx) {
3979 child_ctx->parent_ctx = cloned_ctx;
Paul Mackerras25346b932009-06-01 17:48:12 +10003980 child_ctx->parent_gen = parent_ctx->parent_gen;
Paul Mackerras564c2b22009-05-22 14:27:22 +10003981 } else {
3982 child_ctx->parent_ctx = parent_ctx;
3983 child_ctx->parent_gen = parent_ctx->generation;
3984 }
3985 get_ctx(child_ctx->parent_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003986 }
3987
Paul Mackerrasd859e292009-01-17 18:10:22 +11003988 mutex_unlock(&parent_ctx->mutex);
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02003989
Paul Mackerras25346b932009-06-01 17:48:12 +10003990 perf_unpin_context(parent_ctx);
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003991
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02003992 return ret;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003993}
3994
Ingo Molnar04289bb2008-12-11 08:38:42 +01003995static void __cpuinit perf_counter_init_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003996{
Ingo Molnar04289bb2008-12-11 08:38:42 +01003997 struct perf_cpu_context *cpuctx;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003998
Ingo Molnar04289bb2008-12-11 08:38:42 +01003999 cpuctx = &per_cpu(perf_cpu_context, cpu);
4000 __perf_counter_init_context(&cpuctx->ctx, NULL);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004001
Ingo Molnar1dce8d92009-05-04 19:23:18 +02004002 spin_lock(&perf_resource_lock);
Ingo Molnar04289bb2008-12-11 08:38:42 +01004003 cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu;
Ingo Molnar1dce8d92009-05-04 19:23:18 +02004004 spin_unlock(&perf_resource_lock);
Ingo Molnar04289bb2008-12-11 08:38:42 +01004005
Paul Mackerras01d02872009-01-14 13:44:19 +11004006 hw_perf_counter_setup(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004007}
4008
4009#ifdef CONFIG_HOTPLUG_CPU
Ingo Molnar04289bb2008-12-11 08:38:42 +01004010static void __perf_counter_exit_cpu(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01004011{
4012 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
4013 struct perf_counter_context *ctx = &cpuctx->ctx;
4014 struct perf_counter *counter, *tmp;
4015
Ingo Molnar04289bb2008-12-11 08:38:42 +01004016 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry)
4017 __perf_counter_remove_from_context(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004018}
Ingo Molnar04289bb2008-12-11 08:38:42 +01004019static void perf_counter_exit_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01004020{
Paul Mackerrasd859e292009-01-17 18:10:22 +11004021 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
4022 struct perf_counter_context *ctx = &cpuctx->ctx;
4023
4024 mutex_lock(&ctx->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01004025 smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1);
Paul Mackerrasd859e292009-01-17 18:10:22 +11004026 mutex_unlock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004027}
4028#else
Ingo Molnar04289bb2008-12-11 08:38:42 +01004029static inline void perf_counter_exit_cpu(int cpu) { }
Thomas Gleixner0793a612008-12-04 20:12:29 +01004030#endif
4031
4032static int __cpuinit
4033perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
4034{
4035 unsigned int cpu = (long)hcpu;
4036
4037 switch (action) {
4038
4039 case CPU_UP_PREPARE:
4040 case CPU_UP_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01004041 perf_counter_init_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004042 break;
4043
4044 case CPU_DOWN_PREPARE:
4045 case CPU_DOWN_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01004046 perf_counter_exit_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004047 break;
4048
4049 default:
4050 break;
4051 }
4052
4053 return NOTIFY_OK;
4054}
4055
Paul Mackerrasf38b0822009-06-02 21:05:16 +10004056/*
4057 * This has to have a higher priority than migration_notifier in sched.c.
4058 */
Thomas Gleixner0793a612008-12-04 20:12:29 +01004059static struct notifier_block __cpuinitdata perf_cpu_nb = {
4060 .notifier_call = perf_cpu_notify,
Paul Mackerrasf38b0822009-06-02 21:05:16 +10004061 .priority = 20,
Thomas Gleixner0793a612008-12-04 20:12:29 +01004062};
4063
Ingo Molnar0d905bc2009-05-04 19:13:30 +02004064void __init perf_counter_init(void)
Thomas Gleixner0793a612008-12-04 20:12:29 +01004065{
4066 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
4067 (void *)(long)smp_processor_id());
4068 register_cpu_notifier(&perf_cpu_nb);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004069}
Thomas Gleixner0793a612008-12-04 20:12:29 +01004070
4071static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
4072{
4073 return sprintf(buf, "%d\n", perf_reserved_percpu);
4074}
4075
4076static ssize_t
4077perf_set_reserve_percpu(struct sysdev_class *class,
4078 const char *buf,
4079 size_t count)
4080{
4081 struct perf_cpu_context *cpuctx;
4082 unsigned long val;
4083 int err, cpu, mpt;
4084
4085 err = strict_strtoul(buf, 10, &val);
4086 if (err)
4087 return err;
4088 if (val > perf_max_counters)
4089 return -EINVAL;
4090
Ingo Molnar1dce8d92009-05-04 19:23:18 +02004091 spin_lock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004092 perf_reserved_percpu = val;
4093 for_each_online_cpu(cpu) {
4094 cpuctx = &per_cpu(perf_cpu_context, cpu);
4095 spin_lock_irq(&cpuctx->ctx.lock);
4096 mpt = min(perf_max_counters - cpuctx->ctx.nr_counters,
4097 perf_max_counters - perf_reserved_percpu);
4098 cpuctx->max_pertask = mpt;
4099 spin_unlock_irq(&cpuctx->ctx.lock);
4100 }
Ingo Molnar1dce8d92009-05-04 19:23:18 +02004101 spin_unlock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004102
4103 return count;
4104}
4105
4106static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
4107{
4108 return sprintf(buf, "%d\n", perf_overcommit);
4109}
4110
4111static ssize_t
4112perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
4113{
4114 unsigned long val;
4115 int err;
4116
4117 err = strict_strtoul(buf, 10, &val);
4118 if (err)
4119 return err;
4120 if (val > 1)
4121 return -EINVAL;
4122
Ingo Molnar1dce8d92009-05-04 19:23:18 +02004123 spin_lock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004124 perf_overcommit = val;
Ingo Molnar1dce8d92009-05-04 19:23:18 +02004125 spin_unlock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004126
4127 return count;
4128}
4129
4130static SYSDEV_CLASS_ATTR(
4131 reserve_percpu,
4132 0644,
4133 perf_show_reserve_percpu,
4134 perf_set_reserve_percpu
4135 );
4136
4137static SYSDEV_CLASS_ATTR(
4138 overcommit,
4139 0644,
4140 perf_show_overcommit,
4141 perf_set_overcommit
4142 );
4143
4144static struct attribute *perfclass_attrs[] = {
4145 &attr_reserve_percpu.attr,
4146 &attr_overcommit.attr,
4147 NULL
4148};
4149
4150static struct attribute_group perfclass_attr_group = {
4151 .attrs = perfclass_attrs,
4152 .name = "perf_counters",
4153};
4154
4155static int __init perf_counter_sysfs_init(void)
4156{
4157 return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
4158 &perfclass_attr_group);
4159}
4160device_initcall(perf_counter_sysfs_init);