blob: c2b19c111718735c3db3d577e2482566a3498eb6 [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 Zijlstra07647712009-06-11 11:18:36 +020046/*
Peter Zijlstradf58ab22009-06-11 11:25:05 +020047 * perf counter paranoia level:
48 * 0 - not paranoid
49 * 1 - disallow cpu counters to unpriv
50 * 2 - disallow kernel profiling to unpriv
Peter Zijlstra07647712009-06-11 11:18:36 +020051 */
Peter Zijlstradf58ab22009-06-11 11:25:05 +020052int sysctl_perf_counter_paranoid __read_mostly;
Peter Zijlstra07647712009-06-11 11:18:36 +020053
54static inline bool perf_paranoid_cpu(void)
55{
56 return sysctl_perf_counter_paranoid > 0;
57}
58
59static inline bool perf_paranoid_kernel(void)
60{
61 return sysctl_perf_counter_paranoid > 1;
62}
63
Peter Zijlstra789f90f2009-05-15 15:19:27 +020064int sysctl_perf_counter_mlock __read_mostly = 512; /* 'free' kb per user */
Peter Zijlstradf58ab22009-06-11 11:25:05 +020065
66/*
67 * max perf counter sample rate
68 */
69int sysctl_perf_counter_sample_rate __read_mostly = 100000;
Peter Zijlstra1ccd1542009-04-09 10:53:45 +020070
Peter Zijlstraa96bbc12009-06-03 14:01:36 +020071static atomic64_t perf_counter_id;
72
Thomas Gleixner0793a612008-12-04 20:12:29 +010073/*
Ingo Molnar1dce8d92009-05-04 19:23:18 +020074 * Lock for (sysadmin-configurable) counter reservations:
Thomas Gleixner0793a612008-12-04 20:12:29 +010075 */
Ingo Molnar1dce8d92009-05-04 19:23:18 +020076static DEFINE_SPINLOCK(perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +010077
78/*
79 * Architecture provided APIs - weak aliases:
80 */
Robert Richter4aeb0b42009-04-29 12:47:03 +020081extern __weak const struct pmu *hw_perf_counter_init(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +010082{
Paul Mackerrasff6f0542009-01-09 16:19:25 +110083 return NULL;
Thomas Gleixner0793a612008-12-04 20:12:29 +010084}
85
Peter Zijlstra9e35ad32009-05-13 16:21:38 +020086void __weak hw_perf_disable(void) { barrier(); }
87void __weak hw_perf_enable(void) { barrier(); }
88
Paul Mackerras01d02872009-01-14 13:44:19 +110089void __weak hw_perf_counter_setup(int cpu) { barrier(); }
Ingo Molnar22a4f652009-06-01 10:13:37 +020090
91int __weak
92hw_perf_group_sched_in(struct perf_counter *group_leader,
Paul Mackerras3cbed422009-01-09 16:43:42 +110093 struct perf_cpu_context *cpuctx,
94 struct perf_counter_context *ctx, int cpu)
95{
96 return 0;
97}
Thomas Gleixner0793a612008-12-04 20:12:29 +010098
Paul Mackerras4eb96fc2009-01-09 17:24:34 +110099void __weak perf_counter_print_debug(void) { }
100
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200101static DEFINE_PER_CPU(int, disable_count);
102
103void __perf_disable(void)
104{
105 __get_cpu_var(disable_count)++;
106}
107
108bool __perf_enable(void)
109{
110 return !--__get_cpu_var(disable_count);
111}
112
113void perf_disable(void)
114{
115 __perf_disable();
116 hw_perf_disable();
117}
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200118
119void perf_enable(void)
120{
121 if (__perf_enable())
122 hw_perf_enable();
123}
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200124
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000125static void get_ctx(struct perf_counter_context *ctx)
126{
Peter Zijlstrae5289d42009-06-19 13:22:51 +0200127 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000128}
129
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000130static void free_ctx(struct rcu_head *head)
131{
132 struct perf_counter_context *ctx;
133
134 ctx = container_of(head, struct perf_counter_context, rcu_head);
135 kfree(ctx);
136}
137
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000138static void put_ctx(struct perf_counter_context *ctx)
139{
Paul Mackerras564c2b22009-05-22 14:27:22 +1000140 if (atomic_dec_and_test(&ctx->refcount)) {
141 if (ctx->parent_ctx)
142 put_ctx(ctx->parent_ctx);
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000143 if (ctx->task)
144 put_task_struct(ctx->task);
145 call_rcu(&ctx->rcu_head, free_ctx);
Paul Mackerras564c2b22009-05-22 14:27:22 +1000146 }
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000147}
148
Peter Zijlstrafccc7142009-05-23 18:28:56 +0200149/*
Paul Mackerras25346b932009-06-01 17:48:12 +1000150 * Get the perf_counter_context for a task and lock it.
151 * This has to cope with with the fact that until it is locked,
152 * the context could get moved to another task.
153 */
Ingo Molnar22a4f652009-06-01 10:13:37 +0200154static struct perf_counter_context *
155perf_lock_task_context(struct task_struct *task, unsigned long *flags)
Paul Mackerras25346b932009-06-01 17:48:12 +1000156{
157 struct perf_counter_context *ctx;
158
159 rcu_read_lock();
160 retry:
161 ctx = rcu_dereference(task->perf_counter_ctxp);
162 if (ctx) {
163 /*
164 * If this context is a clone of another, it might
165 * get swapped for another underneath us by
166 * perf_counter_task_sched_out, though the
167 * rcu_read_lock() protects us from any context
168 * getting freed. Lock the context and check if it
169 * got swapped before we could get the lock, and retry
170 * if so. If we locked the right context, then it
171 * can't get swapped on us any more.
172 */
173 spin_lock_irqsave(&ctx->lock, *flags);
174 if (ctx != rcu_dereference(task->perf_counter_ctxp)) {
175 spin_unlock_irqrestore(&ctx->lock, *flags);
176 goto retry;
177 }
Peter Zijlstrab49a9e72009-06-19 17:39:33 +0200178
179 if (!atomic_inc_not_zero(&ctx->refcount)) {
180 spin_unlock_irqrestore(&ctx->lock, *flags);
181 ctx = NULL;
182 }
Paul Mackerras25346b932009-06-01 17:48:12 +1000183 }
184 rcu_read_unlock();
185 return ctx;
186}
187
188/*
189 * Get the context for a task and increment its pin_count so it
190 * can't get swapped to another task. This also increments its
191 * reference count so that the context can't get freed.
192 */
193static struct perf_counter_context *perf_pin_task_context(struct task_struct *task)
194{
195 struct perf_counter_context *ctx;
196 unsigned long flags;
197
198 ctx = perf_lock_task_context(task, &flags);
199 if (ctx) {
200 ++ctx->pin_count;
Paul Mackerras25346b932009-06-01 17:48:12 +1000201 spin_unlock_irqrestore(&ctx->lock, flags);
202 }
203 return ctx;
204}
205
206static void perf_unpin_context(struct perf_counter_context *ctx)
207{
208 unsigned long flags;
209
210 spin_lock_irqsave(&ctx->lock, flags);
211 --ctx->pin_count;
212 spin_unlock_irqrestore(&ctx->lock, flags);
213 put_ctx(ctx);
214}
215
216/*
Peter Zijlstrafccc7142009-05-23 18:28:56 +0200217 * Add a counter from the lists for its context.
218 * Must be called with ctx->mutex and ctx->lock held.
219 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100220static void
221list_add_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
222{
223 struct perf_counter *group_leader = counter->group_leader;
224
225 /*
226 * Depending on whether it is a standalone or sibling counter,
227 * add it straight to the context's counter list, or to the group
228 * leader's sibling list:
229 */
Peter Zijlstra3df5eda2009-05-08 18:52:22 +0200230 if (group_leader == counter)
Ingo Molnar04289bb2008-12-11 08:38:42 +0100231 list_add_tail(&counter->list_entry, &ctx->counter_list);
Peter Zijlstra5c148192009-03-25 12:30:23 +0100232 else {
Ingo Molnar04289bb2008-12-11 08:38:42 +0100233 list_add_tail(&counter->list_entry, &group_leader->sibling_list);
Peter Zijlstra5c148192009-03-25 12:30:23 +0100234 group_leader->nr_siblings++;
235 }
Peter Zijlstra592903c2009-03-13 12:21:36 +0100236
237 list_add_rcu(&counter->event_entry, &ctx->event_list);
Peter Zijlstra8bc20952009-05-15 20:45:59 +0200238 ctx->nr_counters++;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100239}
240
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000241/*
242 * Remove a counter from the lists for its context.
Peter Zijlstrafccc7142009-05-23 18:28:56 +0200243 * Must be called with ctx->mutex and ctx->lock held.
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000244 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100245static void
246list_del_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
247{
248 struct perf_counter *sibling, *tmp;
249
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000250 if (list_empty(&counter->list_entry))
251 return;
Peter Zijlstra8bc20952009-05-15 20:45:59 +0200252 ctx->nr_counters--;
253
Ingo Molnar04289bb2008-12-11 08:38:42 +0100254 list_del_init(&counter->list_entry);
Peter Zijlstra592903c2009-03-13 12:21:36 +0100255 list_del_rcu(&counter->event_entry);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100256
Peter Zijlstra5c148192009-03-25 12:30:23 +0100257 if (counter->group_leader != counter)
258 counter->group_leader->nr_siblings--;
259
Ingo Molnar04289bb2008-12-11 08:38:42 +0100260 /*
261 * If this was a group counter with sibling counters then
262 * upgrade the siblings to singleton counters by adding them
263 * to the context list directly:
264 */
265 list_for_each_entry_safe(sibling, tmp,
266 &counter->sibling_list, list_entry) {
267
Peter Zijlstra75564232009-03-13 12:21:29 +0100268 list_move_tail(&sibling->list_entry, &ctx->counter_list);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100269 sibling->group_leader = sibling;
270 }
271}
272
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100273static void
274counter_sched_out(struct perf_counter *counter,
275 struct perf_cpu_context *cpuctx,
276 struct perf_counter_context *ctx)
277{
278 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
279 return;
280
281 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200282 counter->tstamp_stopped = ctx->time;
Robert Richter4aeb0b42009-04-29 12:47:03 +0200283 counter->pmu->disable(counter);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100284 counter->oncpu = -1;
285
286 if (!is_software_counter(counter))
287 cpuctx->active_oncpu--;
288 ctx->nr_active--;
Peter Zijlstra0d486962009-06-02 19:22:16 +0200289 if (counter->attr.exclusive || !cpuctx->active_oncpu)
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100290 cpuctx->exclusive = 0;
291}
292
Paul Mackerrasd859e292009-01-17 18:10:22 +1100293static void
294group_sched_out(struct perf_counter *group_counter,
295 struct perf_cpu_context *cpuctx,
296 struct perf_counter_context *ctx)
297{
298 struct perf_counter *counter;
299
300 if (group_counter->state != PERF_COUNTER_STATE_ACTIVE)
301 return;
302
303 counter_sched_out(group_counter, cpuctx, ctx);
304
305 /*
306 * Schedule out siblings (if any):
307 */
308 list_for_each_entry(counter, &group_counter->sibling_list, list_entry)
309 counter_sched_out(counter, cpuctx, ctx);
310
Peter Zijlstra0d486962009-06-02 19:22:16 +0200311 if (group_counter->attr.exclusive)
Paul Mackerrasd859e292009-01-17 18:10:22 +1100312 cpuctx->exclusive = 0;
313}
314
Thomas Gleixner0793a612008-12-04 20:12:29 +0100315/*
316 * Cross CPU call to remove a performance counter
317 *
318 * We disable the counter on the hardware level first. After that we
319 * remove it from the context list.
320 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100321static void __perf_counter_remove_from_context(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100322{
323 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
324 struct perf_counter *counter = info;
325 struct perf_counter_context *ctx = counter->ctx;
326
327 /*
328 * If this is a task context, we need to check whether it is
329 * the current task context of this cpu. If not it has been
330 * scheduled out before the smp call arrived.
331 */
Peter Zijlstra665c2142009-05-29 14:51:57 +0200332 if (ctx->task && cpuctx->task_ctx != ctx)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100333 return;
334
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200335 spin_lock(&ctx->lock);
Ingo Molnar34adc802009-05-20 20:13:28 +0200336 /*
337 * Protect the list operation against NMI by disabling the
338 * counters on a global level.
339 */
340 perf_disable();
Thomas Gleixner0793a612008-12-04 20:12:29 +0100341
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100342 counter_sched_out(counter, cpuctx, ctx);
343
Ingo Molnar04289bb2008-12-11 08:38:42 +0100344 list_del_counter(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100345
346 if (!ctx->task) {
347 /*
348 * Allow more per task counters with respect to the
349 * reservation:
350 */
351 cpuctx->max_pertask =
352 min(perf_max_counters - ctx->nr_counters,
353 perf_max_counters - perf_reserved_percpu);
354 }
355
Ingo Molnar34adc802009-05-20 20:13:28 +0200356 perf_enable();
Peter Zijlstra665c2142009-05-29 14:51:57 +0200357 spin_unlock(&ctx->lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100358}
359
360
361/*
362 * Remove the counter from a task's (or a CPU's) list of counters.
363 *
Peter Zijlstrafccc7142009-05-23 18:28:56 +0200364 * Must be called with ctx->mutex held.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100365 *
366 * CPU counters are removed with a smp call. For task counters we only
367 * call when the task is on a CPU.
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000368 *
369 * If counter->ctx is a cloned context, callers must make sure that
370 * every task struct that counter->ctx->task could possibly point to
371 * remains valid. This is OK when called from perf_release since
372 * that only calls us on the top-level context, which can't be a clone.
373 * When called from perf_counter_exit_task, it's OK because the
374 * context has been detached from its task.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100375 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100376static void perf_counter_remove_from_context(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100377{
378 struct perf_counter_context *ctx = counter->ctx;
379 struct task_struct *task = ctx->task;
380
381 if (!task) {
382 /*
383 * Per cpu counters are removed via an smp call and
384 * the removal is always sucessful.
385 */
386 smp_call_function_single(counter->cpu,
Ingo Molnar04289bb2008-12-11 08:38:42 +0100387 __perf_counter_remove_from_context,
Thomas Gleixner0793a612008-12-04 20:12:29 +0100388 counter, 1);
389 return;
390 }
391
392retry:
Ingo Molnar04289bb2008-12-11 08:38:42 +0100393 task_oncpu_function_call(task, __perf_counter_remove_from_context,
Thomas Gleixner0793a612008-12-04 20:12:29 +0100394 counter);
395
396 spin_lock_irq(&ctx->lock);
397 /*
398 * If the context is active we need to retry the smp call.
399 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100400 if (ctx->nr_active && !list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100401 spin_unlock_irq(&ctx->lock);
402 goto retry;
403 }
404
405 /*
406 * The lock prevents that this context is scheduled in so we
Ingo Molnar04289bb2008-12-11 08:38:42 +0100407 * can remove the counter safely, if the call above did not
Thomas Gleixner0793a612008-12-04 20:12:29 +0100408 * succeed.
409 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100410 if (!list_empty(&counter->list_entry)) {
Ingo Molnar04289bb2008-12-11 08:38:42 +0100411 list_del_counter(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100412 }
413 spin_unlock_irq(&ctx->lock);
414}
415
Peter Zijlstra4af49982009-04-06 11:45:10 +0200416static inline u64 perf_clock(void)
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100417{
Peter Zijlstra4af49982009-04-06 11:45:10 +0200418 return cpu_clock(smp_processor_id());
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100419}
420
421/*
422 * Update the record of the current time in a context.
423 */
Peter Zijlstra4af49982009-04-06 11:45:10 +0200424static void update_context_time(struct perf_counter_context *ctx)
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100425{
Peter Zijlstra4af49982009-04-06 11:45:10 +0200426 u64 now = perf_clock();
427
428 ctx->time += now - ctx->timestamp;
429 ctx->timestamp = now;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100430}
431
432/*
433 * Update the total_time_enabled and total_time_running fields for a counter.
434 */
435static void update_counter_times(struct perf_counter *counter)
436{
437 struct perf_counter_context *ctx = counter->ctx;
438 u64 run_end;
439
Peter Zijlstra4af49982009-04-06 11:45:10 +0200440 if (counter->state < PERF_COUNTER_STATE_INACTIVE)
441 return;
442
443 counter->total_time_enabled = ctx->time - counter->tstamp_enabled;
444
445 if (counter->state == PERF_COUNTER_STATE_INACTIVE)
446 run_end = counter->tstamp_stopped;
447 else
448 run_end = ctx->time;
449
450 counter->total_time_running = run_end - counter->tstamp_running;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100451}
452
453/*
454 * Update total_time_enabled and total_time_running for all counters in a group.
455 */
456static void update_group_times(struct perf_counter *leader)
457{
458 struct perf_counter *counter;
459
460 update_counter_times(leader);
461 list_for_each_entry(counter, &leader->sibling_list, list_entry)
462 update_counter_times(counter);
463}
464
465/*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100466 * Cross CPU call to disable a performance counter
467 */
468static void __perf_counter_disable(void *info)
469{
470 struct perf_counter *counter = info;
471 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
472 struct perf_counter_context *ctx = counter->ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100473
474 /*
475 * If this is a per-task counter, need to check whether this
476 * counter's task is the current task on this cpu.
477 */
Peter Zijlstra665c2142009-05-29 14:51:57 +0200478 if (ctx->task && cpuctx->task_ctx != ctx)
Paul Mackerrasd859e292009-01-17 18:10:22 +1100479 return;
480
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200481 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100482
483 /*
484 * If the counter is on, turn it off.
485 * If it is in error state, leave it in error state.
486 */
487 if (counter->state >= PERF_COUNTER_STATE_INACTIVE) {
Peter Zijlstra4af49982009-04-06 11:45:10 +0200488 update_context_time(ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100489 update_counter_times(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100490 if (counter == counter->group_leader)
491 group_sched_out(counter, cpuctx, ctx);
492 else
493 counter_sched_out(counter, cpuctx, ctx);
494 counter->state = PERF_COUNTER_STATE_OFF;
495 }
496
Peter Zijlstra665c2142009-05-29 14:51:57 +0200497 spin_unlock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100498}
499
500/*
501 * Disable a counter.
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000502 *
503 * If counter->ctx is a cloned context, callers must make sure that
504 * every task struct that counter->ctx->task could possibly point to
505 * remains valid. This condition is satisifed when called through
506 * perf_counter_for_each_child or perf_counter_for_each because they
507 * hold the top-level counter's child_mutex, so any descendant that
508 * goes to exit will block in sync_child_counter.
509 * When called from perf_pending_counter it's OK because counter->ctx
510 * is the current context on this CPU and preemption is disabled,
511 * hence we can't get into perf_counter_task_sched_out for this context.
Paul Mackerrasd859e292009-01-17 18:10:22 +1100512 */
513static void perf_counter_disable(struct perf_counter *counter)
514{
515 struct perf_counter_context *ctx = counter->ctx;
516 struct task_struct *task = ctx->task;
517
518 if (!task) {
519 /*
520 * Disable the counter on the cpu that it's on
521 */
522 smp_call_function_single(counter->cpu, __perf_counter_disable,
523 counter, 1);
524 return;
525 }
526
527 retry:
528 task_oncpu_function_call(task, __perf_counter_disable, counter);
529
530 spin_lock_irq(&ctx->lock);
531 /*
532 * If the counter is still active, we need to retry the cross-call.
533 */
534 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
535 spin_unlock_irq(&ctx->lock);
536 goto retry;
537 }
538
539 /*
540 * Since we have the lock this context can't be scheduled
541 * in, so we can change the state safely.
542 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100543 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
544 update_counter_times(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100545 counter->state = PERF_COUNTER_STATE_OFF;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100546 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100547
548 spin_unlock_irq(&ctx->lock);
549}
550
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100551static int
552counter_sched_in(struct perf_counter *counter,
553 struct perf_cpu_context *cpuctx,
554 struct perf_counter_context *ctx,
555 int cpu)
556{
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100557 if (counter->state <= PERF_COUNTER_STATE_OFF)
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100558 return 0;
559
560 counter->state = PERF_COUNTER_STATE_ACTIVE;
561 counter->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */
562 /*
563 * The new state must be visible before we turn it on in the hardware:
564 */
565 smp_wmb();
566
Robert Richter4aeb0b42009-04-29 12:47:03 +0200567 if (counter->pmu->enable(counter)) {
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100568 counter->state = PERF_COUNTER_STATE_INACTIVE;
569 counter->oncpu = -1;
570 return -EAGAIN;
571 }
572
Peter Zijlstra4af49982009-04-06 11:45:10 +0200573 counter->tstamp_running += ctx->time - counter->tstamp_stopped;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100574
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100575 if (!is_software_counter(counter))
576 cpuctx->active_oncpu++;
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100577 ctx->nr_active++;
578
Peter Zijlstra0d486962009-06-02 19:22:16 +0200579 if (counter->attr.exclusive)
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100580 cpuctx->exclusive = 1;
581
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100582 return 0;
583}
584
Paul Mackerras6751b712009-05-11 12:08:02 +1000585static int
586group_sched_in(struct perf_counter *group_counter,
587 struct perf_cpu_context *cpuctx,
588 struct perf_counter_context *ctx,
589 int cpu)
590{
591 struct perf_counter *counter, *partial_group;
592 int ret;
593
594 if (group_counter->state == PERF_COUNTER_STATE_OFF)
595 return 0;
596
597 ret = hw_perf_group_sched_in(group_counter, cpuctx, ctx, cpu);
598 if (ret)
599 return ret < 0 ? ret : 0;
600
Paul Mackerras6751b712009-05-11 12:08:02 +1000601 if (counter_sched_in(group_counter, cpuctx, ctx, cpu))
602 return -EAGAIN;
603
604 /*
605 * Schedule in siblings as one group (if any):
606 */
607 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
Paul Mackerras6751b712009-05-11 12:08:02 +1000608 if (counter_sched_in(counter, cpuctx, ctx, cpu)) {
609 partial_group = counter;
610 goto group_error;
611 }
612 }
613
614 return 0;
615
616group_error:
617 /*
618 * Groups can be scheduled in as one unit only, so undo any
619 * partial group before returning:
620 */
621 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
622 if (counter == partial_group)
623 break;
624 counter_sched_out(counter, cpuctx, ctx);
625 }
626 counter_sched_out(group_counter, cpuctx, ctx);
627
628 return -EAGAIN;
629}
630
Thomas Gleixner0793a612008-12-04 20:12:29 +0100631/*
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100632 * Return 1 for a group consisting entirely of software counters,
633 * 0 if the group contains any hardware counters.
634 */
635static int is_software_only_group(struct perf_counter *leader)
636{
637 struct perf_counter *counter;
638
639 if (!is_software_counter(leader))
640 return 0;
Peter Zijlstra5c148192009-03-25 12:30:23 +0100641
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100642 list_for_each_entry(counter, &leader->sibling_list, list_entry)
643 if (!is_software_counter(counter))
644 return 0;
Peter Zijlstra5c148192009-03-25 12:30:23 +0100645
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100646 return 1;
647}
648
649/*
650 * Work out whether we can put this counter group on the CPU now.
651 */
652static int group_can_go_on(struct perf_counter *counter,
653 struct perf_cpu_context *cpuctx,
654 int can_add_hw)
655{
656 /*
657 * Groups consisting entirely of software counters can always go on.
658 */
659 if (is_software_only_group(counter))
660 return 1;
661 /*
662 * If an exclusive group is already on, no other hardware
663 * counters can go on.
664 */
665 if (cpuctx->exclusive)
666 return 0;
667 /*
668 * If this group is exclusive and there are already
669 * counters on the CPU, it can't go on.
670 */
Peter Zijlstra0d486962009-06-02 19:22:16 +0200671 if (counter->attr.exclusive && cpuctx->active_oncpu)
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100672 return 0;
673 /*
674 * Otherwise, try to add it if all previous groups were able
675 * to go on.
676 */
677 return can_add_hw;
678}
679
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100680static void add_counter_to_ctx(struct perf_counter *counter,
681 struct perf_counter_context *ctx)
682{
683 list_add_counter(counter, ctx);
Peter Zijlstra4af49982009-04-06 11:45:10 +0200684 counter->tstamp_enabled = ctx->time;
685 counter->tstamp_running = ctx->time;
686 counter->tstamp_stopped = ctx->time;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100687}
688
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100689/*
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100690 * Cross CPU call to install and enable a performance counter
Peter Zijlstra682076a2009-05-23 18:28:57 +0200691 *
692 * Must be called with ctx->mutex held
Thomas Gleixner0793a612008-12-04 20:12:29 +0100693 */
694static void __perf_install_in_context(void *info)
695{
696 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
697 struct perf_counter *counter = info;
698 struct perf_counter_context *ctx = counter->ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100699 struct perf_counter *leader = counter->group_leader;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100700 int cpu = smp_processor_id();
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100701 int err;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100702
703 /*
704 * If this is a task context, we need to check whether it is
705 * the current task context of this cpu. If not it has been
706 * scheduled out before the smp call arrived.
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000707 * Or possibly this is the right context but it isn't
708 * on this cpu because it had no counters.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100709 */
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000710 if (ctx->task && cpuctx->task_ctx != ctx) {
Peter Zijlstra665c2142009-05-29 14:51:57 +0200711 if (cpuctx->task_ctx || ctx->task != current)
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000712 return;
713 cpuctx->task_ctx = ctx;
714 }
Thomas Gleixner0793a612008-12-04 20:12:29 +0100715
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200716 spin_lock(&ctx->lock);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000717 ctx->is_active = 1;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200718 update_context_time(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100719
720 /*
721 * Protect the list operation against NMI by disabling the
722 * counters on a global level. NOP for non NMI based counters.
723 */
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200724 perf_disable();
Thomas Gleixner0793a612008-12-04 20:12:29 +0100725
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100726 add_counter_to_ctx(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100727
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100728 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100729 * Don't put the counter on if it is disabled or if
730 * it is in a group and the group isn't on.
731 */
732 if (counter->state != PERF_COUNTER_STATE_INACTIVE ||
733 (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE))
734 goto unlock;
735
736 /*
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100737 * An exclusive counter can't go on if there are already active
738 * hardware counters, and no hardware counter can go on if there
739 * is already an exclusive counter on.
740 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100741 if (!group_can_go_on(counter, cpuctx, 1))
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100742 err = -EEXIST;
743 else
744 err = counter_sched_in(counter, cpuctx, ctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100745
Paul Mackerrasd859e292009-01-17 18:10:22 +1100746 if (err) {
747 /*
748 * This counter couldn't go on. If it is in a group
749 * then we have to pull the whole group off.
750 * If the counter group is pinned then put it in error state.
751 */
752 if (leader != counter)
753 group_sched_out(leader, cpuctx, ctx);
Peter Zijlstra0d486962009-06-02 19:22:16 +0200754 if (leader->attr.pinned) {
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100755 update_group_times(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100756 leader->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100757 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100758 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100759
760 if (!err && !ctx->task && cpuctx->max_pertask)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100761 cpuctx->max_pertask--;
762
Paul Mackerrasd859e292009-01-17 18:10:22 +1100763 unlock:
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200764 perf_enable();
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100765
Peter Zijlstra665c2142009-05-29 14:51:57 +0200766 spin_unlock(&ctx->lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100767}
768
769/*
770 * Attach a performance counter to a context
771 *
772 * First we add the counter to the list with the hardware enable bit
773 * in counter->hw_config cleared.
774 *
775 * If the counter is attached to a task which is on a CPU we use a smp
776 * call to enable it in the task context. The task might have been
777 * scheduled away, but we check this in the smp call again.
Paul Mackerrasd859e292009-01-17 18:10:22 +1100778 *
779 * Must be called with ctx->mutex held.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100780 */
781static void
782perf_install_in_context(struct perf_counter_context *ctx,
783 struct perf_counter *counter,
784 int cpu)
785{
786 struct task_struct *task = ctx->task;
787
Thomas Gleixner0793a612008-12-04 20:12:29 +0100788 if (!task) {
789 /*
790 * Per cpu counters are installed via an smp call and
791 * the install is always sucessful.
792 */
793 smp_call_function_single(cpu, __perf_install_in_context,
794 counter, 1);
795 return;
796 }
797
Thomas Gleixner0793a612008-12-04 20:12:29 +0100798retry:
799 task_oncpu_function_call(task, __perf_install_in_context,
800 counter);
801
802 spin_lock_irq(&ctx->lock);
803 /*
Thomas Gleixner0793a612008-12-04 20:12:29 +0100804 * we need to retry the smp call.
805 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100806 if (ctx->is_active && list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100807 spin_unlock_irq(&ctx->lock);
808 goto retry;
809 }
810
811 /*
812 * The lock prevents that this context is scheduled in so we
813 * can add the counter safely, if it the call above did not
814 * succeed.
815 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100816 if (list_empty(&counter->list_entry))
817 add_counter_to_ctx(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100818 spin_unlock_irq(&ctx->lock);
819}
820
Paul Mackerrasd859e292009-01-17 18:10:22 +1100821/*
822 * Cross CPU call to enable a performance counter
823 */
824static void __perf_counter_enable(void *info)
Ingo Molnar04289bb2008-12-11 08:38:42 +0100825{
Paul Mackerrasd859e292009-01-17 18:10:22 +1100826 struct perf_counter *counter = info;
827 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
828 struct perf_counter_context *ctx = counter->ctx;
829 struct perf_counter *leader = counter->group_leader;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100830 int err;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100831
832 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100833 * If this is a per-task counter, need to check whether this
834 * counter's task is the current task on this cpu.
Ingo Molnar04289bb2008-12-11 08:38:42 +0100835 */
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000836 if (ctx->task && cpuctx->task_ctx != ctx) {
Peter Zijlstra665c2142009-05-29 14:51:57 +0200837 if (cpuctx->task_ctx || ctx->task != current)
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000838 return;
839 cpuctx->task_ctx = ctx;
840 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100841
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200842 spin_lock(&ctx->lock);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000843 ctx->is_active = 1;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200844 update_context_time(ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100845
846 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
847 goto unlock;
848 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200849 counter->tstamp_enabled = ctx->time - counter->total_time_enabled;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100850
851 /*
852 * If the counter is in a group and isn't the group leader,
853 * then don't put it on unless the group is on.
854 */
855 if (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE)
856 goto unlock;
857
Paul Mackerrase758a332009-05-12 21:59:01 +1000858 if (!group_can_go_on(counter, cpuctx, 1)) {
Paul Mackerrasd859e292009-01-17 18:10:22 +1100859 err = -EEXIST;
Paul Mackerrase758a332009-05-12 21:59:01 +1000860 } else {
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200861 perf_disable();
Paul Mackerrase758a332009-05-12 21:59:01 +1000862 if (counter == leader)
863 err = group_sched_in(counter, cpuctx, ctx,
864 smp_processor_id());
865 else
866 err = counter_sched_in(counter, cpuctx, ctx,
867 smp_processor_id());
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200868 perf_enable();
Paul Mackerrase758a332009-05-12 21:59:01 +1000869 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100870
871 if (err) {
872 /*
873 * If this counter can't go on and it's part of a
874 * group, then the whole group has to come off.
875 */
876 if (leader != counter)
877 group_sched_out(leader, cpuctx, ctx);
Peter Zijlstra0d486962009-06-02 19:22:16 +0200878 if (leader->attr.pinned) {
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100879 update_group_times(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100880 leader->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100881 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100882 }
883
884 unlock:
Peter Zijlstra665c2142009-05-29 14:51:57 +0200885 spin_unlock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100886}
887
888/*
889 * Enable a counter.
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000890 *
891 * If counter->ctx is a cloned context, callers must make sure that
892 * every task struct that counter->ctx->task could possibly point to
893 * remains valid. This condition is satisfied when called through
894 * perf_counter_for_each_child or perf_counter_for_each as described
895 * for perf_counter_disable.
Paul Mackerrasd859e292009-01-17 18:10:22 +1100896 */
897static void perf_counter_enable(struct perf_counter *counter)
898{
899 struct perf_counter_context *ctx = counter->ctx;
900 struct task_struct *task = ctx->task;
901
902 if (!task) {
903 /*
904 * Enable the counter on the cpu that it's on
905 */
906 smp_call_function_single(counter->cpu, __perf_counter_enable,
907 counter, 1);
908 return;
909 }
910
911 spin_lock_irq(&ctx->lock);
912 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
913 goto out;
914
915 /*
916 * If the counter is in error state, clear that first.
917 * That way, if we see the counter in error state below, we
918 * know that it has gone back into error state, as distinct
919 * from the task having been scheduled away before the
920 * cross-call arrived.
921 */
922 if (counter->state == PERF_COUNTER_STATE_ERROR)
923 counter->state = PERF_COUNTER_STATE_OFF;
924
925 retry:
926 spin_unlock_irq(&ctx->lock);
927 task_oncpu_function_call(task, __perf_counter_enable, counter);
928
929 spin_lock_irq(&ctx->lock);
930
931 /*
932 * If the context is active and the counter is still off,
933 * we need to retry the cross-call.
934 */
935 if (ctx->is_active && counter->state == PERF_COUNTER_STATE_OFF)
936 goto retry;
937
938 /*
939 * Since we have the lock this context can't be scheduled
940 * in, so we can change the state safely.
941 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100942 if (counter->state == PERF_COUNTER_STATE_OFF) {
Paul Mackerrasd859e292009-01-17 18:10:22 +1100943 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200944 counter->tstamp_enabled =
945 ctx->time - counter->total_time_enabled;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100946 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100947 out:
948 spin_unlock_irq(&ctx->lock);
949}
950
Peter Zijlstra2023b352009-05-05 17:50:26 +0200951static int perf_counter_refresh(struct perf_counter *counter, int refresh)
Peter Zijlstra79f14642009-04-06 11:45:07 +0200952{
Peter Zijlstra2023b352009-05-05 17:50:26 +0200953 /*
954 * not supported on inherited counters
955 */
Peter Zijlstra0d486962009-06-02 19:22:16 +0200956 if (counter->attr.inherit)
Peter Zijlstra2023b352009-05-05 17:50:26 +0200957 return -EINVAL;
958
Peter Zijlstra79f14642009-04-06 11:45:07 +0200959 atomic_add(refresh, &counter->event_limit);
960 perf_counter_enable(counter);
Peter Zijlstra2023b352009-05-05 17:50:26 +0200961
962 return 0;
Peter Zijlstra79f14642009-04-06 11:45:07 +0200963}
964
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100965void __perf_counter_sched_out(struct perf_counter_context *ctx,
966 struct perf_cpu_context *cpuctx)
967{
968 struct perf_counter *counter;
969
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100970 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100971 ctx->is_active = 0;
972 if (likely(!ctx->nr_counters))
973 goto out;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200974 update_context_time(ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100975
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200976 perf_disable();
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100977 if (ctx->nr_active) {
Peter Zijlstraafedadf2009-05-20 12:21:22 +0200978 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
979 if (counter != counter->group_leader)
980 counter_sched_out(counter, cpuctx, ctx);
981 else
982 group_sched_out(counter, cpuctx, ctx);
983 }
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100984 }
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200985 perf_enable();
Paul Mackerrasd859e292009-01-17 18:10:22 +1100986 out:
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100987 spin_unlock(&ctx->lock);
988}
989
Thomas Gleixner0793a612008-12-04 20:12:29 +0100990/*
Paul Mackerras564c2b22009-05-22 14:27:22 +1000991 * Test whether two contexts are equivalent, i.e. whether they
992 * have both been cloned from the same version of the same context
993 * and they both have the same number of enabled counters.
994 * If the number of enabled counters is the same, then the set
995 * of enabled counters should be the same, because these are both
996 * inherited contexts, therefore we can't access individual counters
997 * in them directly with an fd; we can only enable/disable all
998 * counters via prctl, or enable/disable all counters in a family
999 * via ioctl, which will have the same effect on both contexts.
1000 */
1001static int context_equiv(struct perf_counter_context *ctx1,
1002 struct perf_counter_context *ctx2)
1003{
1004 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001005 && ctx1->parent_gen == ctx2->parent_gen
Paul Mackerras25346b932009-06-01 17:48:12 +10001006 && !ctx1->pin_count && !ctx2->pin_count;
Paul Mackerras564c2b22009-05-22 14:27:22 +10001007}
1008
1009/*
Thomas Gleixner0793a612008-12-04 20:12:29 +01001010 * Called from scheduler to remove the counters of the current task,
1011 * with interrupts disabled.
1012 *
1013 * We stop each counter and update the counter value in counter->count.
1014 *
Ingo Molnar76715812008-12-17 14:20:28 +01001015 * This does not protect us against NMI, but disable()
Thomas Gleixner0793a612008-12-04 20:12:29 +01001016 * sets the disabled bit in the control field of counter _before_
1017 * accessing the counter control register. If a NMI hits, then it will
1018 * not restart the counter.
1019 */
Paul Mackerras564c2b22009-05-22 14:27:22 +10001020void perf_counter_task_sched_out(struct task_struct *task,
1021 struct task_struct *next, int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001022{
1023 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001024 struct perf_counter_context *ctx = task->perf_counter_ctxp;
Paul Mackerras564c2b22009-05-22 14:27:22 +10001025 struct perf_counter_context *next_ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001026 struct perf_counter_context *parent;
Peter Zijlstra4a0deca2009-03-19 20:26:12 +01001027 struct pt_regs *regs;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001028 int do_switch = 1;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001029
Peter Zijlstra10989fb2009-05-25 14:45:28 +02001030 regs = task_pt_regs(task);
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +02001031 perf_swcounter_event(PERF_COUNT_SW_CONTEXT_SWITCHES, 1, 1, regs, 0);
Peter Zijlstra10989fb2009-05-25 14:45:28 +02001032
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001033 if (likely(!ctx || !cpuctx->task_ctx))
Thomas Gleixner0793a612008-12-04 20:12:29 +01001034 return;
1035
Peter Zijlstrabce379b2009-04-06 11:45:13 +02001036 update_context_time(ctx);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001037
1038 rcu_read_lock();
1039 parent = rcu_dereference(ctx->parent_ctx);
Paul Mackerras564c2b22009-05-22 14:27:22 +10001040 next_ctx = next->perf_counter_ctxp;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001041 if (parent && next_ctx &&
1042 rcu_dereference(next_ctx->parent_ctx) == parent) {
1043 /*
1044 * Looks like the two contexts are clones, so we might be
1045 * able to optimize the context switch. We lock both
1046 * contexts and check that they are clones under the
1047 * lock (including re-checking that neither has been
1048 * uncloned in the meantime). It doesn't matter which
1049 * order we take the locks because no other cpu could
1050 * be trying to lock both of these tasks.
1051 */
1052 spin_lock(&ctx->lock);
1053 spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
1054 if (context_equiv(ctx, next_ctx)) {
Peter Zijlstra665c2142009-05-29 14:51:57 +02001055 /*
1056 * XXX do we need a memory barrier of sorts
1057 * wrt to rcu_dereference() of perf_counter_ctxp
1058 */
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001059 task->perf_counter_ctxp = next_ctx;
1060 next->perf_counter_ctxp = ctx;
1061 ctx->task = next;
1062 next_ctx->task = task;
1063 do_switch = 0;
1064 }
1065 spin_unlock(&next_ctx->lock);
1066 spin_unlock(&ctx->lock);
Paul Mackerras564c2b22009-05-22 14:27:22 +10001067 }
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001068 rcu_read_unlock();
Paul Mackerras564c2b22009-05-22 14:27:22 +10001069
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001070 if (do_switch) {
1071 __perf_counter_sched_out(ctx, cpuctx);
1072 cpuctx->task_ctx = NULL;
1073 }
Thomas Gleixner0793a612008-12-04 20:12:29 +01001074}
1075
Peter Zijlstra665c2142009-05-29 14:51:57 +02001076/*
1077 * Called with IRQs disabled
1078 */
Paul Mackerrasa08b1592009-05-11 15:46:10 +10001079static void __perf_counter_task_sched_out(struct perf_counter_context *ctx)
1080{
1081 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1082
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001083 if (!cpuctx->task_ctx)
1084 return;
Ingo Molnar012b84d2009-05-17 11:08:41 +02001085
1086 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
1087 return;
1088
Paul Mackerrasa08b1592009-05-11 15:46:10 +10001089 __perf_counter_sched_out(ctx, cpuctx);
1090 cpuctx->task_ctx = NULL;
1091}
1092
Peter Zijlstra665c2142009-05-29 14:51:57 +02001093/*
1094 * Called with IRQs disabled
1095 */
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001096static void perf_counter_cpu_sched_out(struct perf_cpu_context *cpuctx)
Ingo Molnar04289bb2008-12-11 08:38:42 +01001097{
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001098 __perf_counter_sched_out(&cpuctx->ctx, cpuctx);
Ingo Molnar04289bb2008-12-11 08:38:42 +01001099}
1100
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001101static void
1102__perf_counter_sched_in(struct perf_counter_context *ctx,
1103 struct perf_cpu_context *cpuctx, int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001104{
Thomas Gleixner0793a612008-12-04 20:12:29 +01001105 struct perf_counter *counter;
Paul Mackerrasdd0e6ba2009-01-12 15:11:00 +11001106 int can_add_hw = 1;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001107
Thomas Gleixner0793a612008-12-04 20:12:29 +01001108 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001109 ctx->is_active = 1;
1110 if (likely(!ctx->nr_counters))
1111 goto out;
1112
Peter Zijlstra4af49982009-04-06 11:45:10 +02001113 ctx->timestamp = perf_clock();
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001114
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001115 perf_disable();
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001116
1117 /*
1118 * First go through the list and put on any pinned groups
1119 * in order to give them the best chance of going on.
1120 */
Ingo Molnar04289bb2008-12-11 08:38:42 +01001121 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001122 if (counter->state <= PERF_COUNTER_STATE_OFF ||
Peter Zijlstra0d486962009-06-02 19:22:16 +02001123 !counter->attr.pinned)
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001124 continue;
1125 if (counter->cpu != -1 && counter->cpu != cpu)
1126 continue;
1127
Peter Zijlstraafedadf2009-05-20 12:21:22 +02001128 if (counter != counter->group_leader)
1129 counter_sched_in(counter, cpuctx, ctx, cpu);
1130 else {
1131 if (group_can_go_on(counter, cpuctx, 1))
1132 group_sched_in(counter, cpuctx, ctx, cpu);
1133 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001134
1135 /*
1136 * If this pinned group hasn't been scheduled,
1137 * put it in error state.
1138 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001139 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
1140 update_group_times(counter);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001141 counter->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001142 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001143 }
1144
1145 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1146 /*
1147 * Ignore counters in OFF or ERROR state, and
1148 * ignore pinned counters since we did them already.
1149 */
1150 if (counter->state <= PERF_COUNTER_STATE_OFF ||
Peter Zijlstra0d486962009-06-02 19:22:16 +02001151 counter->attr.pinned)
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001152 continue;
1153
Ingo Molnar04289bb2008-12-11 08:38:42 +01001154 /*
1155 * Listen to the 'cpu' scheduling filter constraint
1156 * of counters:
1157 */
Thomas Gleixner0793a612008-12-04 20:12:29 +01001158 if (counter->cpu != -1 && counter->cpu != cpu)
1159 continue;
1160
Peter Zijlstraafedadf2009-05-20 12:21:22 +02001161 if (counter != counter->group_leader) {
1162 if (counter_sched_in(counter, cpuctx, ctx, cpu))
Paul Mackerrasdd0e6ba2009-01-12 15:11:00 +11001163 can_add_hw = 0;
Peter Zijlstraafedadf2009-05-20 12:21:22 +02001164 } else {
1165 if (group_can_go_on(counter, cpuctx, can_add_hw)) {
1166 if (group_sched_in(counter, cpuctx, ctx, cpu))
1167 can_add_hw = 0;
1168 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001169 }
Thomas Gleixner0793a612008-12-04 20:12:29 +01001170 }
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001171 perf_enable();
Paul Mackerrasd859e292009-01-17 18:10:22 +11001172 out:
Thomas Gleixner0793a612008-12-04 20:12:29 +01001173 spin_unlock(&ctx->lock);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001174}
Ingo Molnar04289bb2008-12-11 08:38:42 +01001175
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001176/*
1177 * Called from scheduler to add the counters of the current task
1178 * with interrupts disabled.
1179 *
1180 * We restore the counter value and then enable it.
1181 *
1182 * This does not protect us against NMI, but enable()
1183 * sets the enabled bit in the control field of counter _before_
1184 * accessing the counter control register. If a NMI hits, then it will
1185 * keep the counter running.
1186 */
1187void perf_counter_task_sched_in(struct task_struct *task, int cpu)
1188{
1189 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001190 struct perf_counter_context *ctx = task->perf_counter_ctxp;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001191
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001192 if (likely(!ctx))
1193 return;
Paul Mackerras564c2b22009-05-22 14:27:22 +10001194 if (cpuctx->task_ctx == ctx)
1195 return;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001196 __perf_counter_sched_in(ctx, cpuctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001197 cpuctx->task_ctx = ctx;
1198}
1199
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001200static void perf_counter_cpu_sched_in(struct perf_cpu_context *cpuctx, int cpu)
1201{
1202 struct perf_counter_context *ctx = &cpuctx->ctx;
1203
1204 __perf_counter_sched_in(ctx, cpuctx, cpu);
1205}
1206
Peter Zijlstraa78ac322009-05-25 17:39:05 +02001207#define MAX_INTERRUPTS (~0ULL)
1208
1209static void perf_log_throttle(struct perf_counter *counter, int enable);
Peter Zijlstra26b119b2009-05-20 12:21:20 +02001210static void perf_log_period(struct perf_counter *counter, u64 period);
1211
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001212static void perf_adjust_period(struct perf_counter *counter, u64 events)
1213{
1214 struct hw_perf_counter *hwc = &counter->hw;
1215 u64 period, sample_period;
1216 s64 delta;
1217
1218 events *= hwc->sample_period;
1219 period = div64_u64(events, counter->attr.sample_freq);
1220
1221 delta = (s64)(period - hwc->sample_period);
1222 delta = (delta + 7) / 8; /* low pass filter */
1223
1224 sample_period = hwc->sample_period + delta;
1225
1226 if (!sample_period)
1227 sample_period = 1;
1228
1229 perf_log_period(counter, sample_period);
1230
1231 hwc->sample_period = sample_period;
1232}
1233
1234static void perf_ctx_adjust_freq(struct perf_counter_context *ctx)
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001235{
1236 struct perf_counter *counter;
Peter Zijlstra6a24ed6c2009-06-05 18:01:29 +02001237 struct hw_perf_counter *hwc;
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001238 u64 interrupts, freq;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001239
1240 spin_lock(&ctx->lock);
1241 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1242 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
1243 continue;
1244
Peter Zijlstra6a24ed6c2009-06-05 18:01:29 +02001245 hwc = &counter->hw;
1246
1247 interrupts = hwc->interrupts;
1248 hwc->interrupts = 0;
Peter Zijlstraa78ac322009-05-25 17:39:05 +02001249
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001250 /*
1251 * unthrottle counters on the tick
1252 */
Peter Zijlstraa78ac322009-05-25 17:39:05 +02001253 if (interrupts == MAX_INTERRUPTS) {
1254 perf_log_throttle(counter, 1);
1255 counter->pmu->unthrottle(counter);
Peter Zijlstradf58ab22009-06-11 11:25:05 +02001256 interrupts = 2*sysctl_perf_counter_sample_rate/HZ;
Peter Zijlstraa78ac322009-05-25 17:39:05 +02001257 }
1258
Peter Zijlstra0d486962009-06-02 19:22:16 +02001259 if (!counter->attr.freq || !counter->attr.sample_freq)
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001260 continue;
1261
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001262 /*
1263 * if the specified freq < HZ then we need to skip ticks
1264 */
Peter Zijlstra6a24ed6c2009-06-05 18:01:29 +02001265 if (counter->attr.sample_freq < HZ) {
1266 freq = counter->attr.sample_freq;
1267
1268 hwc->freq_count += freq;
1269 hwc->freq_interrupts += interrupts;
1270
1271 if (hwc->freq_count < HZ)
1272 continue;
1273
1274 interrupts = hwc->freq_interrupts;
1275 hwc->freq_interrupts = 0;
1276 hwc->freq_count -= HZ;
1277 } else
1278 freq = HZ;
1279
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001280 perf_adjust_period(counter, freq * interrupts);
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001281
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001282 /*
1283 * In order to avoid being stalled by an (accidental) huge
1284 * sample period, force reset the sample period if we didn't
1285 * get any events in this freq period.
1286 */
1287 if (!interrupts) {
1288 perf_disable();
1289 counter->pmu->disable(counter);
Paul Mackerras87847b82009-06-13 17:06:50 +10001290 atomic64_set(&hwc->period_left, 0);
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001291 counter->pmu->enable(counter);
1292 perf_enable();
1293 }
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001294 }
1295 spin_unlock(&ctx->lock);
1296}
1297
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001298/*
1299 * Round-robin a context's counters:
1300 */
1301static void rotate_ctx(struct perf_counter_context *ctx)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001302{
Thomas Gleixner0793a612008-12-04 20:12:29 +01001303 struct perf_counter *counter;
1304
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001305 if (!ctx->nr_counters)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001306 return;
1307
Thomas Gleixner0793a612008-12-04 20:12:29 +01001308 spin_lock(&ctx->lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001309 /*
Ingo Molnar04289bb2008-12-11 08:38:42 +01001310 * Rotate the first entry last (works just fine for group counters too):
Thomas Gleixner0793a612008-12-04 20:12:29 +01001311 */
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001312 perf_disable();
Ingo Molnar04289bb2008-12-11 08:38:42 +01001313 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Peter Zijlstra75564232009-03-13 12:21:29 +01001314 list_move_tail(&counter->list_entry, &ctx->counter_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001315 break;
1316 }
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001317 perf_enable();
Thomas Gleixner0793a612008-12-04 20:12:29 +01001318
1319 spin_unlock(&ctx->lock);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001320}
Thomas Gleixner0793a612008-12-04 20:12:29 +01001321
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001322void perf_counter_task_tick(struct task_struct *curr, int cpu)
1323{
Peter Zijlstra7fc23a52009-05-08 18:52:21 +02001324 struct perf_cpu_context *cpuctx;
1325 struct perf_counter_context *ctx;
1326
1327 if (!atomic_read(&nr_counters))
1328 return;
1329
1330 cpuctx = &per_cpu(perf_cpu_context, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001331 ctx = curr->perf_counter_ctxp;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001332
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001333 perf_ctx_adjust_freq(&cpuctx->ctx);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001334 if (ctx)
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001335 perf_ctx_adjust_freq(ctx);
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001336
Ingo Molnarb82914c2009-05-04 18:54:32 +02001337 perf_counter_cpu_sched_out(cpuctx);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001338 if (ctx)
1339 __perf_counter_task_sched_out(ctx);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001340
Ingo Molnarb82914c2009-05-04 18:54:32 +02001341 rotate_ctx(&cpuctx->ctx);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001342 if (ctx)
1343 rotate_ctx(ctx);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001344
Ingo Molnarb82914c2009-05-04 18:54:32 +02001345 perf_counter_cpu_sched_in(cpuctx, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001346 if (ctx)
1347 perf_counter_task_sched_in(curr, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001348}
1349
1350/*
Thomas Gleixner0793a612008-12-04 20:12:29 +01001351 * Cross CPU call to read the hardware counter
1352 */
Ingo Molnar76715812008-12-17 14:20:28 +01001353static void __read(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001354{
Ingo Molnar621a01e2008-12-11 12:46:46 +01001355 struct perf_counter *counter = info;
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001356 struct perf_counter_context *ctx = counter->ctx;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001357 unsigned long flags;
Ingo Molnar621a01e2008-12-11 12:46:46 +01001358
Peter Zijlstra849691a2009-04-06 11:45:12 +02001359 local_irq_save(flags);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001360 if (ctx->is_active)
Peter Zijlstra4af49982009-04-06 11:45:10 +02001361 update_context_time(ctx);
Robert Richter4aeb0b42009-04-29 12:47:03 +02001362 counter->pmu->read(counter);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001363 update_counter_times(counter);
Peter Zijlstra849691a2009-04-06 11:45:12 +02001364 local_irq_restore(flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001365}
1366
Ingo Molnar04289bb2008-12-11 08:38:42 +01001367static u64 perf_counter_read(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001368{
1369 /*
1370 * If counter is enabled and currently active on a CPU, update the
1371 * value in the counter structure:
1372 */
Ingo Molnar6a930702008-12-11 15:17:03 +01001373 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
Thomas Gleixner0793a612008-12-04 20:12:29 +01001374 smp_call_function_single(counter->oncpu,
Ingo Molnar76715812008-12-17 14:20:28 +01001375 __read, counter, 1);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001376 } else if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
1377 update_counter_times(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001378 }
1379
Ingo Molnaree060942008-12-13 09:00:03 +01001380 return atomic64_read(&counter->count);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001381}
1382
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001383/*
1384 * Initialize the perf_counter context in a task_struct:
1385 */
1386static void
1387__perf_counter_init_context(struct perf_counter_context *ctx,
1388 struct task_struct *task)
1389{
1390 memset(ctx, 0, sizeof(*ctx));
1391 spin_lock_init(&ctx->lock);
1392 mutex_init(&ctx->mutex);
1393 INIT_LIST_HEAD(&ctx->counter_list);
1394 INIT_LIST_HEAD(&ctx->event_list);
1395 atomic_set(&ctx->refcount, 1);
1396 ctx->task = task;
1397}
1398
Thomas Gleixner0793a612008-12-04 20:12:29 +01001399static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
1400{
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001401 struct perf_counter_context *parent_ctx;
Ingo Molnar22a4f652009-06-01 10:13:37 +02001402 struct perf_counter_context *ctx;
1403 struct perf_cpu_context *cpuctx;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001404 struct task_struct *task;
Paul Mackerras25346b932009-06-01 17:48:12 +10001405 unsigned long flags;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001406 int err;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001407
1408 /*
1409 * If cpu is not a wildcard then this is a percpu counter:
1410 */
1411 if (cpu != -1) {
1412 /* Must be root to operate on a CPU counter: */
Peter Zijlstra07647712009-06-11 11:18:36 +02001413 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
Thomas Gleixner0793a612008-12-04 20:12:29 +01001414 return ERR_PTR(-EACCES);
1415
1416 if (cpu < 0 || cpu > num_possible_cpus())
1417 return ERR_PTR(-EINVAL);
1418
1419 /*
1420 * We could be clever and allow to attach a counter to an
1421 * offline CPU and activate it when the CPU comes up, but
1422 * that's for later.
1423 */
1424 if (!cpu_isset(cpu, cpu_online_map))
1425 return ERR_PTR(-ENODEV);
1426
1427 cpuctx = &per_cpu(perf_cpu_context, cpu);
1428 ctx = &cpuctx->ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001429 get_ctx(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001430
Thomas Gleixner0793a612008-12-04 20:12:29 +01001431 return ctx;
1432 }
1433
1434 rcu_read_lock();
1435 if (!pid)
1436 task = current;
1437 else
1438 task = find_task_by_vpid(pid);
1439 if (task)
1440 get_task_struct(task);
1441 rcu_read_unlock();
1442
1443 if (!task)
1444 return ERR_PTR(-ESRCH);
1445
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001446 /*
1447 * Can't attach counters to a dying task.
1448 */
1449 err = -ESRCH;
1450 if (task->flags & PF_EXITING)
1451 goto errout;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001452
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001453 /* Reuse ptrace permission checks for now. */
1454 err = -EACCES;
1455 if (!ptrace_may_access(task, PTRACE_MODE_READ))
1456 goto errout;
1457
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001458 retry:
Paul Mackerras25346b932009-06-01 17:48:12 +10001459 ctx = perf_lock_task_context(task, &flags);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001460 if (ctx) {
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001461 parent_ctx = ctx->parent_ctx;
1462 if (parent_ctx) {
1463 put_ctx(parent_ctx);
1464 ctx->parent_ctx = NULL; /* no longer a clone */
1465 }
Paul Mackerras25346b932009-06-01 17:48:12 +10001466 spin_unlock_irqrestore(&ctx->lock, flags);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001467 }
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001468
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001469 if (!ctx) {
1470 ctx = kmalloc(sizeof(struct perf_counter_context), GFP_KERNEL);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001471 err = -ENOMEM;
1472 if (!ctx)
1473 goto errout;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001474 __perf_counter_init_context(ctx, task);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001475 get_ctx(ctx);
1476 if (cmpxchg(&task->perf_counter_ctxp, NULL, ctx)) {
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001477 /*
1478 * We raced with some other task; use
1479 * the context they set.
1480 */
1481 kfree(ctx);
Paul Mackerras25346b932009-06-01 17:48:12 +10001482 goto retry;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001483 }
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001484 get_task_struct(task);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001485 }
1486
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001487 put_task_struct(task);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001488 return ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001489
1490 errout:
1491 put_task_struct(task);
1492 return ERR_PTR(err);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001493}
1494
Peter Zijlstra592903c2009-03-13 12:21:36 +01001495static void free_counter_rcu(struct rcu_head *head)
1496{
1497 struct perf_counter *counter;
1498
1499 counter = container_of(head, struct perf_counter, rcu_head);
Peter Zijlstra709e50c2009-06-02 14:13:15 +02001500 if (counter->ns)
1501 put_pid_ns(counter->ns);
Peter Zijlstra592903c2009-03-13 12:21:36 +01001502 kfree(counter);
1503}
1504
Peter Zijlstra925d5192009-03-30 19:07:02 +02001505static void perf_pending_sync(struct perf_counter *counter);
1506
Peter Zijlstraf1600952009-03-19 20:26:16 +01001507static void free_counter(struct perf_counter *counter)
1508{
Peter Zijlstra925d5192009-03-30 19:07:02 +02001509 perf_pending_sync(counter);
1510
Peter Zijlstraf3440112009-06-22 13:58:35 +02001511 if (!counter->parent) {
1512 atomic_dec(&nr_counters);
1513 if (counter->attr.mmap)
1514 atomic_dec(&nr_mmap_counters);
1515 if (counter->attr.comm)
1516 atomic_dec(&nr_comm_counters);
1517 }
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02001518
Peter Zijlstrae077df42009-03-19 20:26:17 +01001519 if (counter->destroy)
1520 counter->destroy(counter);
1521
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001522 put_ctx(counter->ctx);
Peter Zijlstraf1600952009-03-19 20:26:16 +01001523 call_rcu(&counter->rcu_head, free_counter_rcu);
1524}
1525
Thomas Gleixner0793a612008-12-04 20:12:29 +01001526/*
1527 * Called when the last reference to the file is gone.
1528 */
1529static int perf_release(struct inode *inode, struct file *file)
1530{
1531 struct perf_counter *counter = file->private_data;
1532 struct perf_counter_context *ctx = counter->ctx;
1533
1534 file->private_data = NULL;
1535
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001536 WARN_ON_ONCE(ctx->parent_ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001537 mutex_lock(&ctx->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01001538 perf_counter_remove_from_context(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001539 mutex_unlock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001540
Peter Zijlstra082ff5a2009-05-23 18:29:00 +02001541 mutex_lock(&counter->owner->perf_counter_mutex);
1542 list_del_init(&counter->owner_entry);
1543 mutex_unlock(&counter->owner->perf_counter_mutex);
1544 put_task_struct(counter->owner);
1545
Peter Zijlstraf1600952009-03-19 20:26:16 +01001546 free_counter(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001547
1548 return 0;
1549}
1550
1551/*
1552 * Read the performance counter - simple non blocking version for now
1553 */
1554static ssize_t
1555perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
1556{
Marti Raudseppd5e8da62009-06-13 02:35:01 +03001557 u64 values[4];
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001558 int n;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001559
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001560 /*
1561 * Return end-of-file for a read on a counter that is in
1562 * error state (i.e. because it was pinned but it couldn't be
1563 * scheduled on to the CPU at some point).
1564 */
1565 if (counter->state == PERF_COUNTER_STATE_ERROR)
1566 return 0;
1567
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001568 WARN_ON_ONCE(counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001569 mutex_lock(&counter->child_mutex);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001570 values[0] = perf_counter_read(counter);
1571 n = 1;
Peter Zijlstra0d486962009-06-02 19:22:16 +02001572 if (counter->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001573 values[n++] = counter->total_time_enabled +
1574 atomic64_read(&counter->child_total_time_enabled);
Peter Zijlstra0d486962009-06-02 19:22:16 +02001575 if (counter->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001576 values[n++] = counter->total_time_running +
1577 atomic64_read(&counter->child_total_time_running);
Peter Zijlstra0d486962009-06-02 19:22:16 +02001578 if (counter->attr.read_format & PERF_FORMAT_ID)
Peter Zijlstra8e5799b2009-06-02 15:08:15 +02001579 values[n++] = counter->id;
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001580 mutex_unlock(&counter->child_mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001581
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001582 if (count < n * sizeof(u64))
1583 return -EINVAL;
1584 count = n * sizeof(u64);
1585
1586 if (copy_to_user(buf, values, count))
1587 return -EFAULT;
1588
1589 return count;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001590}
1591
1592static ssize_t
Thomas Gleixner0793a612008-12-04 20:12:29 +01001593perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
1594{
1595 struct perf_counter *counter = file->private_data;
1596
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001597 return perf_read_hw(counter, buf, count);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001598}
1599
1600static unsigned int perf_poll(struct file *file, poll_table *wait)
1601{
1602 struct perf_counter *counter = file->private_data;
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001603 struct perf_mmap_data *data;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001604 unsigned int events = POLL_HUP;
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001605
1606 rcu_read_lock();
1607 data = rcu_dereference(counter->data);
1608 if (data)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001609 events = atomic_xchg(&data->poll, 0);
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001610 rcu_read_unlock();
Thomas Gleixner0793a612008-12-04 20:12:29 +01001611
1612 poll_wait(file, &counter->waitq, wait);
1613
Thomas Gleixner0793a612008-12-04 20:12:29 +01001614 return events;
1615}
1616
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001617static void perf_counter_reset(struct perf_counter *counter)
1618{
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001619 (void)perf_counter_read(counter);
Paul Mackerras615a3f12009-05-11 15:50:21 +10001620 atomic64_set(&counter->count, 0);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001621 perf_counter_update_userpage(counter);
1622}
1623
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001624/*
1625 * Holding the top-level counter's child_mutex means that any
1626 * descendant process that has inherited this counter will block
1627 * in sync_child_counter if it goes to exit, thus satisfying the
1628 * task existence requirements of perf_counter_enable/disable.
1629 */
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001630static void perf_counter_for_each_child(struct perf_counter *counter,
1631 void (*func)(struct perf_counter *))
1632{
1633 struct perf_counter *child;
1634
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001635 WARN_ON_ONCE(counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001636 mutex_lock(&counter->child_mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001637 func(counter);
1638 list_for_each_entry(child, &counter->child_list, child_list)
1639 func(child);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001640 mutex_unlock(&counter->child_mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001641}
1642
1643static void perf_counter_for_each(struct perf_counter *counter,
1644 void (*func)(struct perf_counter *))
1645{
Peter Zijlstra75f937f2009-06-15 15:05:12 +02001646 struct perf_counter_context *ctx = counter->ctx;
1647 struct perf_counter *sibling;
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001648
Peter Zijlstra75f937f2009-06-15 15:05:12 +02001649 WARN_ON_ONCE(ctx->parent_ctx);
1650 mutex_lock(&ctx->mutex);
1651 counter = counter->group_leader;
1652
1653 perf_counter_for_each_child(counter, func);
1654 func(counter);
1655 list_for_each_entry(sibling, &counter->sibling_list, list_entry)
1656 perf_counter_for_each_child(counter, func);
1657 mutex_unlock(&ctx->mutex);
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001658}
1659
Peter Zijlstra08247e32009-06-02 16:46:57 +02001660static int perf_counter_period(struct perf_counter *counter, u64 __user *arg)
1661{
1662 struct perf_counter_context *ctx = counter->ctx;
1663 unsigned long size;
1664 int ret = 0;
1665 u64 value;
1666
Peter Zijlstra0d486962009-06-02 19:22:16 +02001667 if (!counter->attr.sample_period)
Peter Zijlstra08247e32009-06-02 16:46:57 +02001668 return -EINVAL;
1669
1670 size = copy_from_user(&value, arg, sizeof(value));
1671 if (size != sizeof(value))
1672 return -EFAULT;
1673
1674 if (!value)
1675 return -EINVAL;
1676
1677 spin_lock_irq(&ctx->lock);
Peter Zijlstra0d486962009-06-02 19:22:16 +02001678 if (counter->attr.freq) {
Peter Zijlstradf58ab22009-06-11 11:25:05 +02001679 if (value > sysctl_perf_counter_sample_rate) {
Peter Zijlstra08247e32009-06-02 16:46:57 +02001680 ret = -EINVAL;
1681 goto unlock;
1682 }
1683
Peter Zijlstra0d486962009-06-02 19:22:16 +02001684 counter->attr.sample_freq = value;
Peter Zijlstra08247e32009-06-02 16:46:57 +02001685 } else {
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001686 perf_log_period(counter, value);
1687
Peter Zijlstra0d486962009-06-02 19:22:16 +02001688 counter->attr.sample_period = value;
Peter Zijlstra08247e32009-06-02 16:46:57 +02001689 counter->hw.sample_period = value;
Peter Zijlstra08247e32009-06-02 16:46:57 +02001690 }
1691unlock:
1692 spin_unlock_irq(&ctx->lock);
1693
1694 return ret;
1695}
1696
Paul Mackerrasd859e292009-01-17 18:10:22 +11001697static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1698{
1699 struct perf_counter *counter = file->private_data;
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001700 void (*func)(struct perf_counter *);
1701 u32 flags = arg;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001702
1703 switch (cmd) {
1704 case PERF_COUNTER_IOC_ENABLE:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001705 func = perf_counter_enable;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001706 break;
1707 case PERF_COUNTER_IOC_DISABLE:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001708 func = perf_counter_disable;
Peter Zijlstra79f14642009-04-06 11:45:07 +02001709 break;
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001710 case PERF_COUNTER_IOC_RESET:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001711 func = perf_counter_reset;
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001712 break;
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001713
1714 case PERF_COUNTER_IOC_REFRESH:
1715 return perf_counter_refresh(counter, arg);
Peter Zijlstra08247e32009-06-02 16:46:57 +02001716
1717 case PERF_COUNTER_IOC_PERIOD:
1718 return perf_counter_period(counter, (u64 __user *)arg);
1719
Paul Mackerrasd859e292009-01-17 18:10:22 +11001720 default:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001721 return -ENOTTY;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001722 }
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001723
1724 if (flags & PERF_IOC_FLAG_GROUP)
1725 perf_counter_for_each(counter, func);
1726 else
1727 perf_counter_for_each_child(counter, func);
1728
1729 return 0;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001730}
1731
Peter Zijlstra771d7cd2009-05-25 14:45:26 +02001732int perf_counter_task_enable(void)
1733{
1734 struct perf_counter *counter;
1735
1736 mutex_lock(&current->perf_counter_mutex);
1737 list_for_each_entry(counter, &current->perf_counter_list, owner_entry)
1738 perf_counter_for_each_child(counter, perf_counter_enable);
1739 mutex_unlock(&current->perf_counter_mutex);
1740
1741 return 0;
1742}
1743
1744int perf_counter_task_disable(void)
1745{
1746 struct perf_counter *counter;
1747
1748 mutex_lock(&current->perf_counter_mutex);
1749 list_for_each_entry(counter, &current->perf_counter_list, owner_entry)
1750 perf_counter_for_each_child(counter, perf_counter_disable);
1751 mutex_unlock(&current->perf_counter_mutex);
1752
1753 return 0;
1754}
1755
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001756/*
1757 * Callers need to ensure there can be no nesting of this function, otherwise
1758 * the seqlock logic goes bad. We can not serialize this because the arch
1759 * code calls this from NMI context.
1760 */
1761void perf_counter_update_userpage(struct perf_counter *counter)
Paul Mackerras37d81822009-03-23 18:22:08 +01001762{
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001763 struct perf_counter_mmap_page *userpg;
Ingo Molnar22a4f652009-06-01 10:13:37 +02001764 struct perf_mmap_data *data;
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001765
1766 rcu_read_lock();
1767 data = rcu_dereference(counter->data);
1768 if (!data)
1769 goto unlock;
1770
1771 userpg = data->user_page;
Paul Mackerras37d81822009-03-23 18:22:08 +01001772
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001773 /*
1774 * Disable preemption so as to not let the corresponding user-space
1775 * spin too long if we get preempted.
1776 */
1777 preempt_disable();
Paul Mackerras37d81822009-03-23 18:22:08 +01001778 ++userpg->lock;
Peter Zijlstra92f22a32009-04-02 11:12:04 +02001779 barrier();
Paul Mackerras37d81822009-03-23 18:22:08 +01001780 userpg->index = counter->hw.idx;
1781 userpg->offset = atomic64_read(&counter->count);
1782 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
1783 userpg->offset -= atomic64_read(&counter->hw.prev_count);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001784
Peter Zijlstra92f22a32009-04-02 11:12:04 +02001785 barrier();
Paul Mackerras37d81822009-03-23 18:22:08 +01001786 ++userpg->lock;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001787 preempt_enable();
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001788unlock:
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001789 rcu_read_unlock();
Paul Mackerras37d81822009-03-23 18:22:08 +01001790}
1791
1792static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1793{
1794 struct perf_counter *counter = vma->vm_file->private_data;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001795 struct perf_mmap_data *data;
1796 int ret = VM_FAULT_SIGBUS;
Paul Mackerras37d81822009-03-23 18:22:08 +01001797
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01001798 if (vmf->flags & FAULT_FLAG_MKWRITE) {
1799 if (vmf->pgoff == 0)
1800 ret = 0;
1801 return ret;
1802 }
1803
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001804 rcu_read_lock();
1805 data = rcu_dereference(counter->data);
1806 if (!data)
1807 goto unlock;
Paul Mackerras37d81822009-03-23 18:22:08 +01001808
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001809 if (vmf->pgoff == 0) {
1810 vmf->page = virt_to_page(data->user_page);
1811 } else {
1812 int nr = vmf->pgoff - 1;
1813
1814 if ((unsigned)nr > data->nr_pages)
1815 goto unlock;
1816
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01001817 if (vmf->flags & FAULT_FLAG_WRITE)
1818 goto unlock;
1819
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001820 vmf->page = virt_to_page(data->data_pages[nr]);
1821 }
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01001822
Paul Mackerras37d81822009-03-23 18:22:08 +01001823 get_page(vmf->page);
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01001824 vmf->page->mapping = vma->vm_file->f_mapping;
1825 vmf->page->index = vmf->pgoff;
1826
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001827 ret = 0;
1828unlock:
1829 rcu_read_unlock();
1830
1831 return ret;
1832}
1833
1834static int perf_mmap_data_alloc(struct perf_counter *counter, int nr_pages)
1835{
1836 struct perf_mmap_data *data;
1837 unsigned long size;
1838 int i;
1839
1840 WARN_ON(atomic_read(&counter->mmap_count));
1841
1842 size = sizeof(struct perf_mmap_data);
1843 size += nr_pages * sizeof(void *);
1844
1845 data = kzalloc(size, GFP_KERNEL);
1846 if (!data)
1847 goto fail;
1848
1849 data->user_page = (void *)get_zeroed_page(GFP_KERNEL);
1850 if (!data->user_page)
1851 goto fail_user_page;
1852
1853 for (i = 0; i < nr_pages; i++) {
1854 data->data_pages[i] = (void *)get_zeroed_page(GFP_KERNEL);
1855 if (!data->data_pages[i])
1856 goto fail_data_pages;
1857 }
1858
1859 data->nr_pages = nr_pages;
Peter Zijlstra22c15582009-05-05 17:50:25 +02001860 atomic_set(&data->lock, -1);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001861
1862 rcu_assign_pointer(counter->data, data);
1863
Paul Mackerras37d81822009-03-23 18:22:08 +01001864 return 0;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001865
1866fail_data_pages:
1867 for (i--; i >= 0; i--)
1868 free_page((unsigned long)data->data_pages[i]);
1869
1870 free_page((unsigned long)data->user_page);
1871
1872fail_user_page:
1873 kfree(data);
1874
1875fail:
1876 return -ENOMEM;
1877}
1878
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01001879static void perf_mmap_free_page(unsigned long addr)
1880{
1881 struct page *page = virt_to_page(addr);
1882
1883 page->mapping = NULL;
1884 __free_page(page);
1885}
1886
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001887static void __perf_mmap_data_free(struct rcu_head *rcu_head)
1888{
Ingo Molnar22a4f652009-06-01 10:13:37 +02001889 struct perf_mmap_data *data;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001890 int i;
1891
Ingo Molnar22a4f652009-06-01 10:13:37 +02001892 data = container_of(rcu_head, struct perf_mmap_data, rcu_head);
1893
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01001894 perf_mmap_free_page((unsigned long)data->user_page);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001895 for (i = 0; i < data->nr_pages; i++)
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01001896 perf_mmap_free_page((unsigned long)data->data_pages[i]);
1897
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001898 kfree(data);
1899}
1900
1901static void perf_mmap_data_free(struct perf_counter *counter)
1902{
1903 struct perf_mmap_data *data = counter->data;
1904
1905 WARN_ON(atomic_read(&counter->mmap_count));
1906
1907 rcu_assign_pointer(counter->data, NULL);
1908 call_rcu(&data->rcu_head, __perf_mmap_data_free);
1909}
1910
1911static void perf_mmap_open(struct vm_area_struct *vma)
1912{
1913 struct perf_counter *counter = vma->vm_file->private_data;
1914
1915 atomic_inc(&counter->mmap_count);
1916}
1917
1918static void perf_mmap_close(struct vm_area_struct *vma)
1919{
1920 struct perf_counter *counter = vma->vm_file->private_data;
1921
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001922 WARN_ON_ONCE(counter->ctx->parent_ctx);
Ingo Molnar22a4f652009-06-01 10:13:37 +02001923 if (atomic_dec_and_mutex_lock(&counter->mmap_count, &counter->mmap_mutex)) {
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001924 struct user_struct *user = current_user();
1925
1926 atomic_long_sub(counter->data->nr_pages + 1, &user->locked_vm);
Peter Zijlstrac5078f72009-05-05 17:50:24 +02001927 vma->vm_mm->locked_vm -= counter->data->nr_locked;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001928 perf_mmap_data_free(counter);
1929 mutex_unlock(&counter->mmap_mutex);
1930 }
Paul Mackerras37d81822009-03-23 18:22:08 +01001931}
1932
1933static struct vm_operations_struct perf_mmap_vmops = {
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01001934 .open = perf_mmap_open,
1935 .close = perf_mmap_close,
1936 .fault = perf_mmap_fault,
1937 .page_mkwrite = perf_mmap_fault,
Paul Mackerras37d81822009-03-23 18:22:08 +01001938};
1939
1940static int perf_mmap(struct file *file, struct vm_area_struct *vma)
1941{
1942 struct perf_counter *counter = file->private_data;
Ingo Molnar22a4f652009-06-01 10:13:37 +02001943 unsigned long user_locked, user_lock_limit;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001944 struct user_struct *user = current_user();
Ingo Molnar22a4f652009-06-01 10:13:37 +02001945 unsigned long locked, lock_limit;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001946 unsigned long vma_size;
1947 unsigned long nr_pages;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001948 long user_extra, extra;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001949 int ret = 0;
Paul Mackerras37d81822009-03-23 18:22:08 +01001950
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01001951 if (!(vma->vm_flags & VM_SHARED))
Paul Mackerras37d81822009-03-23 18:22:08 +01001952 return -EINVAL;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001953
1954 vma_size = vma->vm_end - vma->vm_start;
1955 nr_pages = (vma_size / PAGE_SIZE) - 1;
1956
Peter Zijlstra7730d862009-03-25 12:48:31 +01001957 /*
1958 * If we have data pages ensure they're a power-of-two number, so we
1959 * can do bitmasks instead of modulo.
1960 */
1961 if (nr_pages != 0 && !is_power_of_2(nr_pages))
Paul Mackerras37d81822009-03-23 18:22:08 +01001962 return -EINVAL;
1963
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001964 if (vma_size != PAGE_SIZE * (1 + nr_pages))
Paul Mackerras37d81822009-03-23 18:22:08 +01001965 return -EINVAL;
1966
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001967 if (vma->vm_pgoff != 0)
1968 return -EINVAL;
Paul Mackerras37d81822009-03-23 18:22:08 +01001969
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001970 WARN_ON_ONCE(counter->ctx->parent_ctx);
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001971 mutex_lock(&counter->mmap_mutex);
1972 if (atomic_inc_not_zero(&counter->mmap_count)) {
1973 if (nr_pages != counter->data->nr_pages)
1974 ret = -EINVAL;
1975 goto unlock;
1976 }
1977
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001978 user_extra = nr_pages + 1;
1979 user_lock_limit = sysctl_perf_counter_mlock >> (PAGE_SHIFT - 10);
Ingo Molnara3862d32009-05-24 09:02:37 +02001980
1981 /*
1982 * Increase the limit linearly with more CPUs:
1983 */
1984 user_lock_limit *= num_online_cpus();
1985
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001986 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
Peter Zijlstrac5078f72009-05-05 17:50:24 +02001987
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001988 extra = 0;
1989 if (user_locked > user_lock_limit)
1990 extra = user_locked - user_lock_limit;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001991
1992 lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
1993 lock_limit >>= PAGE_SHIFT;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001994 locked = vma->vm_mm->locked_vm + extra;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001995
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001996 if ((locked > lock_limit) && !capable(CAP_IPC_LOCK)) {
1997 ret = -EPERM;
1998 goto unlock;
1999 }
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002000
2001 WARN_ON(counter->data);
2002 ret = perf_mmap_data_alloc(counter, nr_pages);
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02002003 if (ret)
2004 goto unlock;
2005
2006 atomic_set(&counter->mmap_count, 1);
Peter Zijlstra789f90f2009-05-15 15:19:27 +02002007 atomic_long_add(user_extra, &user->locked_vm);
Peter Zijlstrac5078f72009-05-05 17:50:24 +02002008 vma->vm_mm->locked_vm += extra;
2009 counter->data->nr_locked = extra;
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002010 if (vma->vm_flags & VM_WRITE)
2011 counter->data->writable = 1;
2012
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02002013unlock:
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002014 mutex_unlock(&counter->mmap_mutex);
Paul Mackerras37d81822009-03-23 18:22:08 +01002015
Paul Mackerras37d81822009-03-23 18:22:08 +01002016 vma->vm_flags |= VM_RESERVED;
2017 vma->vm_ops = &perf_mmap_vmops;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002018
2019 return ret;
Paul Mackerras37d81822009-03-23 18:22:08 +01002020}
2021
Peter Zijlstra3c446b3d2009-04-06 11:45:01 +02002022static int perf_fasync(int fd, struct file *filp, int on)
2023{
Peter Zijlstra3c446b3d2009-04-06 11:45:01 +02002024 struct inode *inode = filp->f_path.dentry->d_inode;
Ingo Molnar22a4f652009-06-01 10:13:37 +02002025 struct perf_counter *counter = filp->private_data;
Peter Zijlstra3c446b3d2009-04-06 11:45:01 +02002026 int retval;
2027
2028 mutex_lock(&inode->i_mutex);
2029 retval = fasync_helper(fd, filp, on, &counter->fasync);
2030 mutex_unlock(&inode->i_mutex);
2031
2032 if (retval < 0)
2033 return retval;
2034
2035 return 0;
2036}
2037
Thomas Gleixner0793a612008-12-04 20:12:29 +01002038static const struct file_operations perf_fops = {
2039 .release = perf_release,
2040 .read = perf_read,
2041 .poll = perf_poll,
Paul Mackerrasd859e292009-01-17 18:10:22 +11002042 .unlocked_ioctl = perf_ioctl,
2043 .compat_ioctl = perf_ioctl,
Paul Mackerras37d81822009-03-23 18:22:08 +01002044 .mmap = perf_mmap,
Peter Zijlstra3c446b3d2009-04-06 11:45:01 +02002045 .fasync = perf_fasync,
Thomas Gleixner0793a612008-12-04 20:12:29 +01002046};
2047
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002048/*
Peter Zijlstra925d5192009-03-30 19:07:02 +02002049 * Perf counter wakeup
2050 *
2051 * If there's data, ensure we set the poll() state and publish everything
2052 * to user-space before waking everybody up.
2053 */
2054
2055void perf_counter_wakeup(struct perf_counter *counter)
2056{
Peter Zijlstra925d5192009-03-30 19:07:02 +02002057 wake_up_all(&counter->waitq);
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002058
2059 if (counter->pending_kill) {
2060 kill_fasync(&counter->fasync, SIGIO, counter->pending_kill);
2061 counter->pending_kill = 0;
2062 }
Peter Zijlstra925d5192009-03-30 19:07:02 +02002063}
2064
2065/*
2066 * Pending wakeups
2067 *
2068 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
2069 *
2070 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
2071 * single linked list and use cmpxchg() to add entries lockless.
2072 */
2073
Peter Zijlstra79f14642009-04-06 11:45:07 +02002074static void perf_pending_counter(struct perf_pending_entry *entry)
2075{
2076 struct perf_counter *counter = container_of(entry,
2077 struct perf_counter, pending);
2078
2079 if (counter->pending_disable) {
2080 counter->pending_disable = 0;
2081 perf_counter_disable(counter);
2082 }
2083
2084 if (counter->pending_wakeup) {
2085 counter->pending_wakeup = 0;
2086 perf_counter_wakeup(counter);
2087 }
2088}
2089
Peter Zijlstra671dec52009-04-06 11:45:02 +02002090#define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
Peter Zijlstra925d5192009-03-30 19:07:02 +02002091
Peter Zijlstra671dec52009-04-06 11:45:02 +02002092static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = {
Peter Zijlstra925d5192009-03-30 19:07:02 +02002093 PENDING_TAIL,
2094};
2095
Peter Zijlstra671dec52009-04-06 11:45:02 +02002096static void perf_pending_queue(struct perf_pending_entry *entry,
2097 void (*func)(struct perf_pending_entry *))
Peter Zijlstra925d5192009-03-30 19:07:02 +02002098{
Peter Zijlstra671dec52009-04-06 11:45:02 +02002099 struct perf_pending_entry **head;
Peter Zijlstra925d5192009-03-30 19:07:02 +02002100
Peter Zijlstra671dec52009-04-06 11:45:02 +02002101 if (cmpxchg(&entry->next, NULL, PENDING_TAIL) != NULL)
Peter Zijlstra925d5192009-03-30 19:07:02 +02002102 return;
2103
Peter Zijlstra671dec52009-04-06 11:45:02 +02002104 entry->func = func;
2105
2106 head = &get_cpu_var(perf_pending_head);
Peter Zijlstra925d5192009-03-30 19:07:02 +02002107
2108 do {
Peter Zijlstra671dec52009-04-06 11:45:02 +02002109 entry->next = *head;
2110 } while (cmpxchg(head, entry->next, entry) != entry->next);
Peter Zijlstra925d5192009-03-30 19:07:02 +02002111
2112 set_perf_counter_pending();
2113
Peter Zijlstra671dec52009-04-06 11:45:02 +02002114 put_cpu_var(perf_pending_head);
Peter Zijlstra925d5192009-03-30 19:07:02 +02002115}
2116
2117static int __perf_pending_run(void)
2118{
Peter Zijlstra671dec52009-04-06 11:45:02 +02002119 struct perf_pending_entry *list;
Peter Zijlstra925d5192009-03-30 19:07:02 +02002120 int nr = 0;
2121
Peter Zijlstra671dec52009-04-06 11:45:02 +02002122 list = xchg(&__get_cpu_var(perf_pending_head), PENDING_TAIL);
Peter Zijlstra925d5192009-03-30 19:07:02 +02002123 while (list != PENDING_TAIL) {
Peter Zijlstra671dec52009-04-06 11:45:02 +02002124 void (*func)(struct perf_pending_entry *);
2125 struct perf_pending_entry *entry = list;
Peter Zijlstra925d5192009-03-30 19:07:02 +02002126
2127 list = list->next;
2128
Peter Zijlstra671dec52009-04-06 11:45:02 +02002129 func = entry->func;
2130 entry->next = NULL;
Peter Zijlstra925d5192009-03-30 19:07:02 +02002131 /*
2132 * Ensure we observe the unqueue before we issue the wakeup,
2133 * so that we won't be waiting forever.
2134 * -- see perf_not_pending().
2135 */
2136 smp_wmb();
2137
Peter Zijlstra671dec52009-04-06 11:45:02 +02002138 func(entry);
Peter Zijlstra925d5192009-03-30 19:07:02 +02002139 nr++;
2140 }
2141
2142 return nr;
2143}
2144
2145static inline int perf_not_pending(struct perf_counter *counter)
2146{
2147 /*
2148 * If we flush on whatever cpu we run, there is a chance we don't
2149 * need to wait.
2150 */
2151 get_cpu();
2152 __perf_pending_run();
2153 put_cpu();
2154
2155 /*
2156 * Ensure we see the proper queue state before going to sleep
2157 * so that we do not miss the wakeup. -- see perf_pending_handle()
2158 */
2159 smp_rmb();
Peter Zijlstra671dec52009-04-06 11:45:02 +02002160 return counter->pending.next == NULL;
Peter Zijlstra925d5192009-03-30 19:07:02 +02002161}
2162
2163static void perf_pending_sync(struct perf_counter *counter)
2164{
2165 wait_event(counter->waitq, perf_not_pending(counter));
2166}
2167
2168void perf_counter_do_pending(void)
2169{
2170 __perf_pending_run();
2171}
2172
2173/*
Peter Zijlstra394ee072009-03-30 19:07:14 +02002174 * Callchain support -- arch specific
2175 */
2176
Peter Zijlstra9c03d882009-04-06 11:45:00 +02002177__weak struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
Peter Zijlstra394ee072009-03-30 19:07:14 +02002178{
2179 return NULL;
2180}
2181
2182/*
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002183 * Output
2184 */
2185
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002186struct perf_output_handle {
2187 struct perf_counter *counter;
2188 struct perf_mmap_data *data;
Peter Zijlstra8e3747c2009-06-02 16:16:02 +02002189 unsigned long head;
2190 unsigned long offset;
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002191 int nmi;
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002192 int sample;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002193 int locked;
2194 unsigned long flags;
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002195};
2196
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002197static bool perf_output_space(struct perf_mmap_data *data,
2198 unsigned int offset, unsigned int head)
2199{
2200 unsigned long tail;
2201 unsigned long mask;
2202
2203 if (!data->writable)
2204 return true;
2205
2206 mask = (data->nr_pages << PAGE_SHIFT) - 1;
2207 /*
2208 * Userspace could choose to issue a mb() before updating the tail
2209 * pointer. So that all reads will be completed before the write is
2210 * issued.
2211 */
2212 tail = ACCESS_ONCE(data->user_page->data_tail);
2213 smp_rmb();
2214
2215 offset = (offset - tail) & mask;
2216 head = (head - tail) & mask;
2217
2218 if ((int)(head - offset) < 0)
2219 return false;
2220
2221 return true;
2222}
2223
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002224static void perf_output_wakeup(struct perf_output_handle *handle)
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002225{
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002226 atomic_set(&handle->data->poll, POLL_IN);
2227
Peter Zijlstra671dec52009-04-06 11:45:02 +02002228 if (handle->nmi) {
Peter Zijlstra79f14642009-04-06 11:45:07 +02002229 handle->counter->pending_wakeup = 1;
Peter Zijlstra671dec52009-04-06 11:45:02 +02002230 perf_pending_queue(&handle->counter->pending,
Peter Zijlstra79f14642009-04-06 11:45:07 +02002231 perf_pending_counter);
Peter Zijlstra671dec52009-04-06 11:45:02 +02002232 } else
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002233 perf_counter_wakeup(handle->counter);
2234}
2235
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002236/*
2237 * Curious locking construct.
2238 *
2239 * We need to ensure a later event doesn't publish a head when a former
2240 * event isn't done writing. However since we need to deal with NMIs we
2241 * cannot fully serialize things.
2242 *
2243 * What we do is serialize between CPUs so we only have to deal with NMI
2244 * nesting on a single CPU.
2245 *
2246 * We only publish the head (and generate a wakeup) when the outer-most
2247 * event completes.
2248 */
2249static void perf_output_lock(struct perf_output_handle *handle)
2250{
2251 struct perf_mmap_data *data = handle->data;
2252 int cpu;
2253
2254 handle->locked = 0;
2255
2256 local_irq_save(handle->flags);
2257 cpu = smp_processor_id();
2258
2259 if (in_nmi() && atomic_read(&data->lock) == cpu)
2260 return;
2261
Peter Zijlstra22c15582009-05-05 17:50:25 +02002262 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002263 cpu_relax();
2264
2265 handle->locked = 1;
2266}
2267
2268static void perf_output_unlock(struct perf_output_handle *handle)
2269{
2270 struct perf_mmap_data *data = handle->data;
Peter Zijlstra8e3747c2009-06-02 16:16:02 +02002271 unsigned long head;
2272 int cpu;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002273
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002274 data->done_head = data->head;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002275
2276 if (!handle->locked)
2277 goto out;
2278
2279again:
2280 /*
2281 * The xchg implies a full barrier that ensures all writes are done
2282 * before we publish the new head, matched by a rmb() in userspace when
2283 * reading this position.
2284 */
Peter Zijlstra8e3747c2009-06-02 16:16:02 +02002285 while ((head = atomic_long_xchg(&data->done_head, 0)))
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002286 data->user_page->data_head = head;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002287
2288 /*
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002289 * NMI can happen here, which means we can miss a done_head update.
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002290 */
2291
Peter Zijlstra22c15582009-05-05 17:50:25 +02002292 cpu = atomic_xchg(&data->lock, -1);
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002293 WARN_ON_ONCE(cpu != smp_processor_id());
2294
2295 /*
2296 * Therefore we have to validate we did not indeed do so.
2297 */
Peter Zijlstra8e3747c2009-06-02 16:16:02 +02002298 if (unlikely(atomic_long_read(&data->done_head))) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002299 /*
2300 * Since we had it locked, we can lock it again.
2301 */
Peter Zijlstra22c15582009-05-05 17:50:25 +02002302 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002303 cpu_relax();
2304
2305 goto again;
2306 }
2307
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002308 if (atomic_xchg(&data->wakeup, 0))
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002309 perf_output_wakeup(handle);
2310out:
2311 local_irq_restore(handle->flags);
2312}
2313
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002314static void perf_output_copy(struct perf_output_handle *handle,
Peter Zijlstra089dd792009-06-05 14:04:55 +02002315 const void *buf, unsigned int len)
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002316{
2317 unsigned int pages_mask;
2318 unsigned int offset;
2319 unsigned int size;
2320 void **pages;
2321
2322 offset = handle->offset;
2323 pages_mask = handle->data->nr_pages - 1;
2324 pages = handle->data->data_pages;
2325
2326 do {
2327 unsigned int page_offset;
2328 int nr;
2329
2330 nr = (offset >> PAGE_SHIFT) & pages_mask;
2331 page_offset = offset & (PAGE_SIZE - 1);
2332 size = min_t(unsigned int, PAGE_SIZE - page_offset, len);
2333
2334 memcpy(pages[nr] + page_offset, buf, size);
2335
2336 len -= size;
2337 buf += size;
2338 offset += size;
2339 } while (len);
2340
2341 handle->offset = offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01002342
Peter Zijlstra53020fe2009-05-13 21:26:19 +02002343 /*
2344 * Check we didn't copy past our reservation window, taking the
2345 * possible unsigned int wrap into account.
2346 */
Peter Zijlstra8e3747c2009-06-02 16:16:02 +02002347 WARN_ON_ONCE(((long)(handle->head - handle->offset)) < 0);
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002348}
2349
Peter Zijlstra5c148192009-03-25 12:30:23 +01002350#define perf_output_put(handle, x) \
2351 perf_output_copy((handle), &(x), sizeof(x))
2352
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002353static int perf_output_begin(struct perf_output_handle *handle,
2354 struct perf_counter *counter, unsigned int size,
2355 int nmi, int sample)
2356{
2357 struct perf_mmap_data *data;
2358 unsigned int offset, head;
2359 int have_lost;
2360 struct {
2361 struct perf_event_header header;
2362 u64 id;
2363 u64 lost;
2364 } lost_event;
2365
2366 /*
2367 * For inherited counters we send all the output towards the parent.
2368 */
2369 if (counter->parent)
2370 counter = counter->parent;
2371
2372 rcu_read_lock();
2373 data = rcu_dereference(counter->data);
2374 if (!data)
2375 goto out;
2376
2377 handle->data = data;
2378 handle->counter = counter;
2379 handle->nmi = nmi;
2380 handle->sample = sample;
2381
2382 if (!data->nr_pages)
2383 goto fail;
2384
2385 have_lost = atomic_read(&data->lost);
2386 if (have_lost)
2387 size += sizeof(lost_event);
2388
2389 perf_output_lock(handle);
2390
2391 do {
2392 offset = head = atomic_long_read(&data->head);
2393 head += size;
2394 if (unlikely(!perf_output_space(data, offset, head)))
2395 goto fail;
2396 } while (atomic_long_cmpxchg(&data->head, offset, head) != offset);
2397
2398 handle->offset = offset;
2399 handle->head = head;
2400
2401 if ((offset >> PAGE_SHIFT) != (head >> PAGE_SHIFT))
2402 atomic_set(&data->wakeup, 1);
2403
2404 if (have_lost) {
2405 lost_event.header.type = PERF_EVENT_LOST;
2406 lost_event.header.misc = 0;
2407 lost_event.header.size = sizeof(lost_event);
2408 lost_event.id = counter->id;
2409 lost_event.lost = atomic_xchg(&data->lost, 0);
2410
2411 perf_output_put(handle, lost_event);
2412 }
2413
2414 return 0;
2415
2416fail:
2417 atomic_inc(&data->lost);
2418 perf_output_unlock(handle);
2419out:
2420 rcu_read_unlock();
2421
2422 return -ENOSPC;
2423}
2424
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002425static void perf_output_end(struct perf_output_handle *handle)
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002426{
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002427 struct perf_counter *counter = handle->counter;
2428 struct perf_mmap_data *data = handle->data;
2429
Peter Zijlstra0d486962009-06-02 19:22:16 +02002430 int wakeup_events = counter->attr.wakeup_events;
Peter Zijlstrac4578102009-04-02 11:12:01 +02002431
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002432 if (handle->sample && wakeup_events) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002433 int events = atomic_inc_return(&data->events);
Peter Zijlstrac4578102009-04-02 11:12:01 +02002434 if (events >= wakeup_events) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002435 atomic_sub(wakeup_events, &data->events);
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002436 atomic_set(&data->wakeup, 1);
Peter Zijlstrac4578102009-04-02 11:12:01 +02002437 }
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002438 }
2439
2440 perf_output_unlock(handle);
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002441 rcu_read_unlock();
2442}
2443
Peter Zijlstra709e50c2009-06-02 14:13:15 +02002444static u32 perf_counter_pid(struct perf_counter *counter, struct task_struct *p)
2445{
2446 /*
2447 * only top level counters have the pid namespace they were created in
2448 */
2449 if (counter->parent)
2450 counter = counter->parent;
2451
2452 return task_tgid_nr_ns(p, counter->ns);
2453}
2454
2455static u32 perf_counter_tid(struct perf_counter *counter, struct task_struct *p)
2456{
2457 /*
2458 * only top level counters have the pid namespace they were created in
2459 */
2460 if (counter->parent)
2461 counter = counter->parent;
2462
2463 return task_pid_nr_ns(p, counter->ns);
2464}
2465
Peter Zijlstradf1a1322009-06-10 21:02:22 +02002466static void perf_counter_output(struct perf_counter *counter, int nmi,
2467 struct perf_sample_data *data)
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002468{
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002469 int ret;
Peter Zijlstra0d486962009-06-02 19:22:16 +02002470 u64 sample_type = counter->attr.sample_type;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002471 struct perf_output_handle handle;
2472 struct perf_event_header header;
2473 u64 ip;
Peter Zijlstra5c148192009-03-25 12:30:23 +01002474 struct {
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002475 u32 pid, tid;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002476 } tid_entry;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002477 struct {
Peter Zijlstra8e5799b2009-06-02 15:08:15 +02002478 u64 id;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002479 u64 counter;
2480 } group_entry;
Peter Zijlstra394ee072009-03-30 19:07:14 +02002481 struct perf_callchain_entry *callchain = NULL;
2482 int callchain_size = 0;
Peter Zijlstra339f7c92009-04-06 11:45:06 +02002483 u64 time;
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002484 struct {
2485 u32 cpu, reserved;
2486 } cpu_entry;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002487
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02002488 header.type = 0;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002489 header.size = sizeof(header);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002490
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02002491 header.misc = PERF_EVENT_MISC_OVERFLOW;
Peter Zijlstradf1a1322009-06-10 21:02:22 +02002492 header.misc |= perf_misc_flags(data->regs);
Peter Zijlstra6fab0192009-04-08 15:01:26 +02002493
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002494 if (sample_type & PERF_SAMPLE_IP) {
Peter Zijlstradf1a1322009-06-10 21:02:22 +02002495 ip = perf_instruction_pointer(data->regs);
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002496 header.type |= PERF_SAMPLE_IP;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002497 header.size += sizeof(ip);
2498 }
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002499
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002500 if (sample_type & PERF_SAMPLE_TID) {
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002501 /* namespace issues */
Peter Zijlstra709e50c2009-06-02 14:13:15 +02002502 tid_entry.pid = perf_counter_pid(counter, current);
2503 tid_entry.tid = perf_counter_tid(counter, current);
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002504
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002505 header.type |= PERF_SAMPLE_TID;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002506 header.size += sizeof(tid_entry);
2507 }
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002508
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002509 if (sample_type & PERF_SAMPLE_TIME) {
Peter Zijlstra4d855452009-04-08 15:01:32 +02002510 /*
2511 * Maybe do better on x86 and provide cpu_clock_nmi()
2512 */
2513 time = sched_clock();
2514
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002515 header.type |= PERF_SAMPLE_TIME;
Peter Zijlstra4d855452009-04-08 15:01:32 +02002516 header.size += sizeof(u64);
2517 }
2518
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002519 if (sample_type & PERF_SAMPLE_ADDR) {
2520 header.type |= PERF_SAMPLE_ADDR;
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002521 header.size += sizeof(u64);
2522 }
2523
Peter Zijlstraac4bcf82009-06-05 14:44:52 +02002524 if (sample_type & PERF_SAMPLE_ID) {
2525 header.type |= PERF_SAMPLE_ID;
Peter Zijlstraa85f61a2009-05-08 18:52:23 +02002526 header.size += sizeof(u64);
2527 }
2528
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002529 if (sample_type & PERF_SAMPLE_CPU) {
2530 header.type |= PERF_SAMPLE_CPU;
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002531 header.size += sizeof(cpu_entry);
2532
2533 cpu_entry.cpu = raw_smp_processor_id();
2534 }
2535
Peter Zijlstra689802b2009-06-05 15:05:43 +02002536 if (sample_type & PERF_SAMPLE_PERIOD) {
2537 header.type |= PERF_SAMPLE_PERIOD;
2538 header.size += sizeof(u64);
2539 }
2540
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002541 if (sample_type & PERF_SAMPLE_GROUP) {
2542 header.type |= PERF_SAMPLE_GROUP;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002543 header.size += sizeof(u64) +
2544 counter->nr_siblings * sizeof(group_entry);
2545 }
2546
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002547 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
Peter Zijlstradf1a1322009-06-10 21:02:22 +02002548 callchain = perf_callchain(data->regs);
Peter Zijlstra394ee072009-03-30 19:07:14 +02002549
2550 if (callchain) {
Peter Zijlstra9c03d882009-04-06 11:45:00 +02002551 callchain_size = (1 + callchain->nr) * sizeof(u64);
Peter Zijlstra394ee072009-03-30 19:07:14 +02002552
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002553 header.type |= PERF_SAMPLE_CALLCHAIN;
Peter Zijlstra394ee072009-03-30 19:07:14 +02002554 header.size += callchain_size;
2555 }
2556 }
2557
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002558 ret = perf_output_begin(&handle, counter, header.size, nmi, 1);
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002559 if (ret)
2560 return;
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002561
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002562 perf_output_put(&handle, header);
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002563
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002564 if (sample_type & PERF_SAMPLE_IP)
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002565 perf_output_put(&handle, ip);
2566
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002567 if (sample_type & PERF_SAMPLE_TID)
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002568 perf_output_put(&handle, tid_entry);
2569
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002570 if (sample_type & PERF_SAMPLE_TIME)
Peter Zijlstra4d855452009-04-08 15:01:32 +02002571 perf_output_put(&handle, time);
2572
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002573 if (sample_type & PERF_SAMPLE_ADDR)
Peter Zijlstradf1a1322009-06-10 21:02:22 +02002574 perf_output_put(&handle, data->addr);
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002575
Peter Zijlstraac4bcf82009-06-05 14:44:52 +02002576 if (sample_type & PERF_SAMPLE_ID)
2577 perf_output_put(&handle, counter->id);
Peter Zijlstraa85f61a2009-05-08 18:52:23 +02002578
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002579 if (sample_type & PERF_SAMPLE_CPU)
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002580 perf_output_put(&handle, cpu_entry);
2581
Peter Zijlstra689802b2009-06-05 15:05:43 +02002582 if (sample_type & PERF_SAMPLE_PERIOD)
Peter Zijlstra9e350de2009-06-10 21:34:59 +02002583 perf_output_put(&handle, data->period);
Peter Zijlstra689802b2009-06-05 15:05:43 +02002584
Peter Zijlstra2023b352009-05-05 17:50:26 +02002585 /*
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002586 * XXX PERF_SAMPLE_GROUP vs inherited counters seems difficult.
Peter Zijlstra2023b352009-05-05 17:50:26 +02002587 */
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002588 if (sample_type & PERF_SAMPLE_GROUP) {
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002589 struct perf_counter *leader, *sub;
2590 u64 nr = counter->nr_siblings;
2591
2592 perf_output_put(&handle, nr);
2593
2594 leader = counter->group_leader;
2595 list_for_each_entry(sub, &leader->sibling_list, list_entry) {
2596 if (sub != counter)
Robert Richter4aeb0b42009-04-29 12:47:03 +02002597 sub->pmu->read(sub);
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002598
Peter Zijlstra8e5799b2009-06-02 15:08:15 +02002599 group_entry.id = sub->id;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002600 group_entry.counter = atomic64_read(&sub->count);
2601
2602 perf_output_put(&handle, group_entry);
2603 }
2604 }
2605
Peter Zijlstra394ee072009-03-30 19:07:14 +02002606 if (callchain)
2607 perf_output_copy(&handle, callchain, callchain_size);
2608
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002609 perf_output_end(&handle);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002610}
2611
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002612/*
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002613 * fork tracking
2614 */
2615
2616struct perf_fork_event {
2617 struct task_struct *task;
2618
2619 struct {
2620 struct perf_event_header header;
2621
2622 u32 pid;
2623 u32 ppid;
2624 } event;
2625};
2626
2627static void perf_counter_fork_output(struct perf_counter *counter,
2628 struct perf_fork_event *fork_event)
2629{
2630 struct perf_output_handle handle;
2631 int size = fork_event->event.header.size;
2632 struct task_struct *task = fork_event->task;
2633 int ret = perf_output_begin(&handle, counter, size, 0, 0);
2634
2635 if (ret)
2636 return;
2637
2638 fork_event->event.pid = perf_counter_pid(counter, task);
2639 fork_event->event.ppid = perf_counter_pid(counter, task->real_parent);
2640
2641 perf_output_put(&handle, fork_event->event);
2642 perf_output_end(&handle);
2643}
2644
2645static int perf_counter_fork_match(struct perf_counter *counter)
2646{
Peter Zijlstrad99e9442009-06-04 17:08:58 +02002647 if (counter->attr.comm || counter->attr.mmap)
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002648 return 1;
2649
2650 return 0;
2651}
2652
2653static void perf_counter_fork_ctx(struct perf_counter_context *ctx,
2654 struct perf_fork_event *fork_event)
2655{
2656 struct perf_counter *counter;
2657
2658 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2659 return;
2660
2661 rcu_read_lock();
2662 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2663 if (perf_counter_fork_match(counter))
2664 perf_counter_fork_output(counter, fork_event);
2665 }
2666 rcu_read_unlock();
2667}
2668
2669static void perf_counter_fork_event(struct perf_fork_event *fork_event)
2670{
2671 struct perf_cpu_context *cpuctx;
2672 struct perf_counter_context *ctx;
2673
2674 cpuctx = &get_cpu_var(perf_cpu_context);
2675 perf_counter_fork_ctx(&cpuctx->ctx, fork_event);
2676 put_cpu_var(perf_cpu_context);
2677
2678 rcu_read_lock();
2679 /*
2680 * doesn't really matter which of the child contexts the
2681 * events ends up in.
2682 */
2683 ctx = rcu_dereference(current->perf_counter_ctxp);
2684 if (ctx)
2685 perf_counter_fork_ctx(ctx, fork_event);
2686 rcu_read_unlock();
2687}
2688
2689void perf_counter_fork(struct task_struct *task)
2690{
2691 struct perf_fork_event fork_event;
2692
2693 if (!atomic_read(&nr_comm_counters) &&
Peter Zijlstrad99e9442009-06-04 17:08:58 +02002694 !atomic_read(&nr_mmap_counters))
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002695 return;
2696
2697 fork_event = (struct perf_fork_event){
2698 .task = task,
2699 .event = {
2700 .header = {
2701 .type = PERF_EVENT_FORK,
2702 .size = sizeof(fork_event.event),
2703 },
2704 },
2705 };
2706
2707 perf_counter_fork_event(&fork_event);
2708}
2709
2710/*
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002711 * comm tracking
2712 */
2713
2714struct perf_comm_event {
Ingo Molnar22a4f652009-06-01 10:13:37 +02002715 struct task_struct *task;
2716 char *comm;
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002717 int comm_size;
2718
2719 struct {
2720 struct perf_event_header header;
2721
2722 u32 pid;
2723 u32 tid;
2724 } event;
2725};
2726
2727static void perf_counter_comm_output(struct perf_counter *counter,
2728 struct perf_comm_event *comm_event)
2729{
2730 struct perf_output_handle handle;
2731 int size = comm_event->event.header.size;
2732 int ret = perf_output_begin(&handle, counter, size, 0, 0);
2733
2734 if (ret)
2735 return;
2736
Peter Zijlstra709e50c2009-06-02 14:13:15 +02002737 comm_event->event.pid = perf_counter_pid(counter, comm_event->task);
2738 comm_event->event.tid = perf_counter_tid(counter, comm_event->task);
2739
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002740 perf_output_put(&handle, comm_event->event);
2741 perf_output_copy(&handle, comm_event->comm,
2742 comm_event->comm_size);
2743 perf_output_end(&handle);
2744}
2745
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002746static int perf_counter_comm_match(struct perf_counter *counter)
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002747{
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002748 if (counter->attr.comm)
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002749 return 1;
2750
2751 return 0;
2752}
2753
2754static void perf_counter_comm_ctx(struct perf_counter_context *ctx,
2755 struct perf_comm_event *comm_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) {
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002764 if (perf_counter_comm_match(counter))
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002765 perf_counter_comm_output(counter, comm_event);
2766 }
2767 rcu_read_unlock();
2768}
2769
2770static void perf_counter_comm_event(struct perf_comm_event *comm_event)
2771{
2772 struct perf_cpu_context *cpuctx;
Peter Zijlstra665c2142009-05-29 14:51:57 +02002773 struct perf_counter_context *ctx;
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002774 unsigned int size;
2775 char *comm = comm_event->task->comm;
2776
Ingo Molnar888fcee2009-04-09 09:48:22 +02002777 size = ALIGN(strlen(comm)+1, sizeof(u64));
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002778
2779 comm_event->comm = comm;
2780 comm_event->comm_size = size;
2781
2782 comm_event->event.header.size = sizeof(comm_event->event) + size;
2783
2784 cpuctx = &get_cpu_var(perf_cpu_context);
2785 perf_counter_comm_ctx(&cpuctx->ctx, comm_event);
2786 put_cpu_var(perf_cpu_context);
Peter Zijlstra665c2142009-05-29 14:51:57 +02002787
2788 rcu_read_lock();
2789 /*
2790 * doesn't really matter which of the child contexts the
2791 * events ends up in.
2792 */
2793 ctx = rcu_dereference(current->perf_counter_ctxp);
2794 if (ctx)
2795 perf_counter_comm_ctx(ctx, comm_event);
2796 rcu_read_unlock();
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002797}
2798
2799void perf_counter_comm(struct task_struct *task)
2800{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002801 struct perf_comm_event comm_event;
2802
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002803 if (!atomic_read(&nr_comm_counters))
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002804 return;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10002805
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002806 comm_event = (struct perf_comm_event){
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002807 .task = task,
2808 .event = {
2809 .header = { .type = PERF_EVENT_COMM, },
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002810 },
2811 };
2812
2813 perf_counter_comm_event(&comm_event);
2814}
2815
2816/*
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002817 * mmap tracking
2818 */
2819
2820struct perf_mmap_event {
Peter Zijlstra089dd792009-06-05 14:04:55 +02002821 struct vm_area_struct *vma;
2822
2823 const char *file_name;
2824 int file_size;
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002825
2826 struct {
2827 struct perf_event_header header;
2828
2829 u32 pid;
2830 u32 tid;
2831 u64 start;
2832 u64 len;
2833 u64 pgoff;
2834 } event;
2835};
2836
2837static void perf_counter_mmap_output(struct perf_counter *counter,
2838 struct perf_mmap_event *mmap_event)
2839{
2840 struct perf_output_handle handle;
2841 int size = mmap_event->event.header.size;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002842 int ret = perf_output_begin(&handle, counter, size, 0, 0);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002843
2844 if (ret)
2845 return;
2846
Peter Zijlstra709e50c2009-06-02 14:13:15 +02002847 mmap_event->event.pid = perf_counter_pid(counter, current);
2848 mmap_event->event.tid = perf_counter_tid(counter, current);
2849
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002850 perf_output_put(&handle, mmap_event->event);
2851 perf_output_copy(&handle, mmap_event->file_name,
2852 mmap_event->file_size);
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002853 perf_output_end(&handle);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002854}
2855
2856static int perf_counter_mmap_match(struct perf_counter *counter,
2857 struct perf_mmap_event *mmap_event)
2858{
Peter Zijlstrad99e9442009-06-04 17:08:58 +02002859 if (counter->attr.mmap)
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002860 return 1;
2861
2862 return 0;
2863}
2864
2865static void perf_counter_mmap_ctx(struct perf_counter_context *ctx,
2866 struct perf_mmap_event *mmap_event)
2867{
2868 struct perf_counter *counter;
2869
2870 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2871 return;
2872
2873 rcu_read_lock();
2874 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2875 if (perf_counter_mmap_match(counter, mmap_event))
2876 perf_counter_mmap_output(counter, mmap_event);
2877 }
2878 rcu_read_unlock();
2879}
2880
2881static void perf_counter_mmap_event(struct perf_mmap_event *mmap_event)
2882{
2883 struct perf_cpu_context *cpuctx;
Peter Zijlstra665c2142009-05-29 14:51:57 +02002884 struct perf_counter_context *ctx;
Peter Zijlstra089dd792009-06-05 14:04:55 +02002885 struct vm_area_struct *vma = mmap_event->vma;
2886 struct file *file = vma->vm_file;
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002887 unsigned int size;
2888 char tmp[16];
2889 char *buf = NULL;
Peter Zijlstra089dd792009-06-05 14:04:55 +02002890 const char *name;
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002891
2892 if (file) {
2893 buf = kzalloc(PATH_MAX, GFP_KERNEL);
2894 if (!buf) {
2895 name = strncpy(tmp, "//enomem", sizeof(tmp));
2896 goto got_name;
2897 }
Peter Zijlstrad3d21c42009-04-09 10:53:46 +02002898 name = d_path(&file->f_path, buf, PATH_MAX);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002899 if (IS_ERR(name)) {
2900 name = strncpy(tmp, "//toolong", sizeof(tmp));
2901 goto got_name;
2902 }
2903 } else {
Peter Zijlstra089dd792009-06-05 14:04:55 +02002904 name = arch_vma_name(mmap_event->vma);
2905 if (name)
2906 goto got_name;
2907
2908 if (!vma->vm_mm) {
2909 name = strncpy(tmp, "[vdso]", sizeof(tmp));
2910 goto got_name;
2911 }
2912
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002913 name = strncpy(tmp, "//anon", sizeof(tmp));
2914 goto got_name;
2915 }
2916
2917got_name:
Ingo Molnar888fcee2009-04-09 09:48:22 +02002918 size = ALIGN(strlen(name)+1, sizeof(u64));
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002919
2920 mmap_event->file_name = name;
2921 mmap_event->file_size = size;
2922
2923 mmap_event->event.header.size = sizeof(mmap_event->event) + size;
2924
2925 cpuctx = &get_cpu_var(perf_cpu_context);
2926 perf_counter_mmap_ctx(&cpuctx->ctx, mmap_event);
2927 put_cpu_var(perf_cpu_context);
2928
Peter Zijlstra665c2142009-05-29 14:51:57 +02002929 rcu_read_lock();
2930 /*
2931 * doesn't really matter which of the child contexts the
2932 * events ends up in.
2933 */
2934 ctx = rcu_dereference(current->perf_counter_ctxp);
2935 if (ctx)
2936 perf_counter_mmap_ctx(ctx, mmap_event);
2937 rcu_read_unlock();
2938
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002939 kfree(buf);
2940}
2941
Peter Zijlstra089dd792009-06-05 14:04:55 +02002942void __perf_counter_mmap(struct vm_area_struct *vma)
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002943{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002944 struct perf_mmap_event mmap_event;
2945
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002946 if (!atomic_read(&nr_mmap_counters))
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002947 return;
2948
2949 mmap_event = (struct perf_mmap_event){
Peter Zijlstra089dd792009-06-05 14:04:55 +02002950 .vma = vma,
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002951 .event = {
2952 .header = { .type = PERF_EVENT_MMAP, },
Peter Zijlstra089dd792009-06-05 14:04:55 +02002953 .start = vma->vm_start,
2954 .len = vma->vm_end - vma->vm_start,
2955 .pgoff = vma->vm_pgoff,
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002956 },
2957 };
2958
2959 perf_counter_mmap_event(&mmap_event);
2960}
2961
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002962/*
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002963 * Log sample_period changes so that analyzing tools can re-normalize the
Peter Zijlstrae220d2d2009-05-23 18:28:55 +02002964 * event flow.
Peter Zijlstra26b119b2009-05-20 12:21:20 +02002965 */
2966
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02002967struct freq_event {
2968 struct perf_event_header header;
2969 u64 time;
2970 u64 id;
2971 u64 period;
2972};
2973
Peter Zijlstra26b119b2009-05-20 12:21:20 +02002974static void perf_log_period(struct perf_counter *counter, u64 period)
2975{
2976 struct perf_output_handle handle;
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02002977 struct freq_event event;
Peter Zijlstra26b119b2009-05-20 12:21:20 +02002978 int ret;
2979
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02002980 if (counter->hw.sample_period == period)
2981 return;
2982
2983 if (counter->attr.sample_type & PERF_SAMPLE_PERIOD)
2984 return;
2985
2986 event = (struct freq_event) {
Peter Zijlstra26b119b2009-05-20 12:21:20 +02002987 .header = {
2988 .type = PERF_EVENT_PERIOD,
2989 .misc = 0,
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02002990 .size = sizeof(event),
Peter Zijlstra26b119b2009-05-20 12:21:20 +02002991 },
2992 .time = sched_clock(),
Peter Zijlstra689802b2009-06-05 15:05:43 +02002993 .id = counter->id,
Peter Zijlstra26b119b2009-05-20 12:21:20 +02002994 .period = period,
2995 };
2996
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02002997 ret = perf_output_begin(&handle, counter, sizeof(event), 1, 0);
Peter Zijlstra26b119b2009-05-20 12:21:20 +02002998 if (ret)
2999 return;
3000
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02003001 perf_output_put(&handle, event);
Peter Zijlstra26b119b2009-05-20 12:21:20 +02003002 perf_output_end(&handle);
3003}
3004
3005/*
Peter Zijlstraa78ac322009-05-25 17:39:05 +02003006 * IRQ throttle logging
3007 */
3008
3009static void perf_log_throttle(struct perf_counter *counter, int enable)
3010{
3011 struct perf_output_handle handle;
3012 int ret;
3013
3014 struct {
3015 struct perf_event_header header;
3016 u64 time;
Peter Zijlstracca3f452009-06-11 14:57:55 +02003017 u64 id;
Peter Zijlstraa78ac322009-05-25 17:39:05 +02003018 } throttle_event = {
3019 .header = {
3020 .type = PERF_EVENT_THROTTLE + 1,
3021 .misc = 0,
3022 .size = sizeof(throttle_event),
3023 },
Peter Zijlstracca3f452009-06-11 14:57:55 +02003024 .time = sched_clock(),
3025 .id = counter->id,
Peter Zijlstraa78ac322009-05-25 17:39:05 +02003026 };
3027
Ingo Molnar0127c3e2009-05-25 22:03:26 +02003028 ret = perf_output_begin(&handle, counter, sizeof(throttle_event), 1, 0);
Peter Zijlstraa78ac322009-05-25 17:39:05 +02003029 if (ret)
3030 return;
3031
3032 perf_output_put(&handle, throttle_event);
3033 perf_output_end(&handle);
3034}
3035
3036/*
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01003037 * Generic counter overflow handling, sampling.
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02003038 */
3039
Peter Zijlstradf1a1322009-06-10 21:02:22 +02003040int perf_counter_overflow(struct perf_counter *counter, int nmi,
3041 struct perf_sample_data *data)
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02003042{
Peter Zijlstra79f14642009-04-06 11:45:07 +02003043 int events = atomic_read(&counter->event_limit);
Peter Zijlstraa78ac322009-05-25 17:39:05 +02003044 int throttle = counter->pmu->unthrottle != NULL;
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02003045 struct hw_perf_counter *hwc = &counter->hw;
Peter Zijlstra79f14642009-04-06 11:45:07 +02003046 int ret = 0;
3047
Peter Zijlstraa78ac322009-05-25 17:39:05 +02003048 if (!throttle) {
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02003049 hwc->interrupts++;
Ingo Molnar128f0482009-06-03 22:19:36 +02003050 } else {
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02003051 if (hwc->interrupts != MAX_INTERRUPTS) {
3052 hwc->interrupts++;
Peter Zijlstradf58ab22009-06-11 11:25:05 +02003053 if (HZ * hwc->interrupts >
3054 (u64)sysctl_perf_counter_sample_rate) {
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02003055 hwc->interrupts = MAX_INTERRUPTS;
Ingo Molnar128f0482009-06-03 22:19:36 +02003056 perf_log_throttle(counter, 0);
3057 ret = 1;
3058 }
3059 } else {
3060 /*
3061 * Keep re-disabling counters even though on the previous
3062 * pass we disabled it - just in case we raced with a
3063 * sched-in and the counter got enabled again:
3064 */
Peter Zijlstraa78ac322009-05-25 17:39:05 +02003065 ret = 1;
3066 }
3067 }
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003068
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02003069 if (counter->attr.freq) {
3070 u64 now = sched_clock();
3071 s64 delta = now - hwc->freq_stamp;
3072
3073 hwc->freq_stamp = now;
3074
3075 if (delta > 0 && delta < TICK_NSEC)
3076 perf_adjust_period(counter, NSEC_PER_SEC / (int)delta);
3077 }
3078
Peter Zijlstra2023b352009-05-05 17:50:26 +02003079 /*
3080 * XXX event_limit might not quite work as expected on inherited
3081 * counters
3082 */
3083
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02003084 counter->pending_kill = POLL_IN;
Peter Zijlstra79f14642009-04-06 11:45:07 +02003085 if (events && atomic_dec_and_test(&counter->event_limit)) {
3086 ret = 1;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02003087 counter->pending_kill = POLL_HUP;
Peter Zijlstra79f14642009-04-06 11:45:07 +02003088 if (nmi) {
3089 counter->pending_disable = 1;
3090 perf_pending_queue(&counter->pending,
3091 perf_pending_counter);
3092 } else
3093 perf_counter_disable(counter);
3094 }
3095
Peter Zijlstradf1a1322009-06-10 21:02:22 +02003096 perf_counter_output(counter, nmi, data);
Peter Zijlstra79f14642009-04-06 11:45:07 +02003097 return ret;
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02003098}
3099
3100/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003101 * Generic software counter infrastructure
3102 */
3103
3104static void perf_swcounter_update(struct perf_counter *counter)
3105{
3106 struct hw_perf_counter *hwc = &counter->hw;
3107 u64 prev, now;
3108 s64 delta;
3109
3110again:
3111 prev = atomic64_read(&hwc->prev_count);
3112 now = atomic64_read(&hwc->count);
3113 if (atomic64_cmpxchg(&hwc->prev_count, prev, now) != prev)
3114 goto again;
3115
3116 delta = now - prev;
3117
3118 atomic64_add(delta, &counter->count);
3119 atomic64_sub(delta, &hwc->period_left);
3120}
3121
3122static void perf_swcounter_set_period(struct perf_counter *counter)
3123{
3124 struct hw_perf_counter *hwc = &counter->hw;
3125 s64 left = atomic64_read(&hwc->period_left);
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003126 s64 period = hwc->sample_period;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003127
3128 if (unlikely(left <= -period)) {
3129 left = period;
3130 atomic64_set(&hwc->period_left, left);
Peter Zijlstra9e350de2009-06-10 21:34:59 +02003131 hwc->last_period = period;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003132 }
3133
3134 if (unlikely(left <= 0)) {
3135 left += period;
3136 atomic64_add(period, &hwc->period_left);
Peter Zijlstra9e350de2009-06-10 21:34:59 +02003137 hwc->last_period = period;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003138 }
3139
3140 atomic64_set(&hwc->prev_count, -left);
3141 atomic64_set(&hwc->count, -left);
3142}
3143
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003144static enum hrtimer_restart perf_swcounter_hrtimer(struct hrtimer *hrtimer)
3145{
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02003146 enum hrtimer_restart ret = HRTIMER_RESTART;
Peter Zijlstradf1a1322009-06-10 21:02:22 +02003147 struct perf_sample_data data;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003148 struct perf_counter *counter;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003149 u64 period;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003150
3151 counter = container_of(hrtimer, struct perf_counter, hw.hrtimer);
Robert Richter4aeb0b42009-04-29 12:47:03 +02003152 counter->pmu->read(counter);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003153
Peter Zijlstradf1a1322009-06-10 21:02:22 +02003154 data.addr = 0;
3155 data.regs = get_irq_regs();
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003156 /*
3157 * In case we exclude kernel IPs or are somehow not in interrupt
3158 * context, provide the next best thing, the user IP.
3159 */
Peter Zijlstradf1a1322009-06-10 21:02:22 +02003160 if ((counter->attr.exclude_kernel || !data.regs) &&
Peter Zijlstra0d486962009-06-02 19:22:16 +02003161 !counter->attr.exclude_user)
Peter Zijlstradf1a1322009-06-10 21:02:22 +02003162 data.regs = task_pt_regs(current);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003163
Peter Zijlstradf1a1322009-06-10 21:02:22 +02003164 if (data.regs) {
3165 if (perf_counter_overflow(counter, 0, &data))
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02003166 ret = HRTIMER_NORESTART;
3167 }
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003168
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003169 period = max_t(u64, 10000, counter->hw.sample_period);
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003170 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003171
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02003172 return ret;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003173}
3174
3175static void perf_swcounter_overflow(struct perf_counter *counter,
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003176 int nmi, struct perf_sample_data *data)
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003177{
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003178 data->period = counter->hw.last_period;
Peter Zijlstradf1a1322009-06-10 21:02:22 +02003179
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003180 perf_swcounter_update(counter);
3181 perf_swcounter_set_period(counter);
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003182 if (perf_counter_overflow(counter, nmi, data))
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02003183 /* soft-disable the counter */
3184 ;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003185}
3186
Paul Mackerras880ca152009-06-01 17:49:14 +10003187static int perf_swcounter_is_counting(struct perf_counter *counter)
3188{
3189 struct perf_counter_context *ctx;
3190 unsigned long flags;
3191 int count;
3192
3193 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
3194 return 1;
3195
3196 if (counter->state != PERF_COUNTER_STATE_INACTIVE)
3197 return 0;
3198
3199 /*
3200 * If the counter is inactive, it could be just because
3201 * its task is scheduled out, or because it's in a group
3202 * which could not go on the PMU. We want to count in
3203 * the first case but not the second. If the context is
3204 * currently active then an inactive software counter must
3205 * be the second case. If it's not currently active then
3206 * we need to know whether the counter was active when the
3207 * context was last active, which we can determine by
3208 * comparing counter->tstamp_stopped with ctx->time.
3209 *
3210 * We are within an RCU read-side critical section,
3211 * which protects the existence of *ctx.
3212 */
3213 ctx = counter->ctx;
3214 spin_lock_irqsave(&ctx->lock, flags);
3215 count = 1;
3216 /* Re-check state now we have the lock */
3217 if (counter->state < PERF_COUNTER_STATE_INACTIVE ||
3218 counter->ctx->is_active ||
3219 counter->tstamp_stopped < ctx->time)
3220 count = 0;
3221 spin_unlock_irqrestore(&ctx->lock, flags);
3222 return count;
3223}
3224
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003225static int perf_swcounter_match(struct perf_counter *counter,
Peter Zijlstra1c432d82009-06-11 13:19:29 +02003226 enum perf_type_id type,
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003227 u32 event, struct pt_regs *regs)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003228{
Paul Mackerras880ca152009-06-01 17:49:14 +10003229 if (!perf_swcounter_is_counting(counter))
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003230 return 0;
3231
Ingo Molnara21ca2c2009-06-06 09:58:57 +02003232 if (counter->attr.type != type)
3233 return 0;
3234 if (counter->attr.config != event)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003235 return 0;
3236
Paul Mackerras3f731ca2009-06-01 17:52:30 +10003237 if (regs) {
Peter Zijlstra0d486962009-06-02 19:22:16 +02003238 if (counter->attr.exclude_user && user_mode(regs))
Paul Mackerras3f731ca2009-06-01 17:52:30 +10003239 return 0;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003240
Peter Zijlstra0d486962009-06-02 19:22:16 +02003241 if (counter->attr.exclude_kernel && !user_mode(regs))
Paul Mackerras3f731ca2009-06-01 17:52:30 +10003242 return 0;
3243 }
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003244
3245 return 1;
3246}
3247
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003248static void perf_swcounter_add(struct perf_counter *counter, u64 nr,
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003249 int nmi, struct perf_sample_data *data)
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003250{
3251 int neg = atomic64_add_negative(nr, &counter->hw.count);
Ingo Molnar22a4f652009-06-01 10:13:37 +02003252
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003253 if (counter->hw.sample_period && !neg && data->regs)
3254 perf_swcounter_overflow(counter, nmi, data);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003255}
3256
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003257static void perf_swcounter_ctx_event(struct perf_counter_context *ctx,
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003258 enum perf_type_id type,
3259 u32 event, u64 nr, int nmi,
3260 struct perf_sample_data *data)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003261{
3262 struct perf_counter *counter;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003263
Peter Zijlstra01ef09d2009-03-19 20:26:11 +01003264 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003265 return;
3266
Peter Zijlstra592903c2009-03-13 12:21:36 +01003267 rcu_read_lock();
3268 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003269 if (perf_swcounter_match(counter, type, event, data->regs))
3270 perf_swcounter_add(counter, nr, nmi, data);
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003271 }
Peter Zijlstra592903c2009-03-13 12:21:36 +01003272 rcu_read_unlock();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003273}
3274
Peter Zijlstra96f6d442009-03-23 18:22:07 +01003275static int *perf_swcounter_recursion_context(struct perf_cpu_context *cpuctx)
3276{
3277 if (in_nmi())
3278 return &cpuctx->recursion[3];
3279
3280 if (in_irq())
3281 return &cpuctx->recursion[2];
3282
3283 if (in_softirq())
3284 return &cpuctx->recursion[1];
3285
3286 return &cpuctx->recursion[0];
3287}
3288
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003289static void do_perf_swcounter_event(enum perf_type_id type, u32 event,
3290 u64 nr, int nmi,
3291 struct perf_sample_data *data)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003292{
3293 struct perf_cpu_context *cpuctx = &get_cpu_var(perf_cpu_context);
Peter Zijlstra96f6d442009-03-23 18:22:07 +01003294 int *recursion = perf_swcounter_recursion_context(cpuctx);
Peter Zijlstra665c2142009-05-29 14:51:57 +02003295 struct perf_counter_context *ctx;
Peter Zijlstra96f6d442009-03-23 18:22:07 +01003296
3297 if (*recursion)
3298 goto out;
3299
3300 (*recursion)++;
3301 barrier();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003302
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003303 perf_swcounter_ctx_event(&cpuctx->ctx, type, event,
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003304 nr, nmi, data);
Peter Zijlstra665c2142009-05-29 14:51:57 +02003305 rcu_read_lock();
3306 /*
3307 * doesn't really matter which of the child contexts the
3308 * events ends up in.
3309 */
3310 ctx = rcu_dereference(current->perf_counter_ctxp);
3311 if (ctx)
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003312 perf_swcounter_ctx_event(ctx, type, event, nr, nmi, data);
Peter Zijlstra665c2142009-05-29 14:51:57 +02003313 rcu_read_unlock();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003314
Peter Zijlstra96f6d442009-03-23 18:22:07 +01003315 barrier();
3316 (*recursion)--;
3317
3318out:
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003319 put_cpu_var(perf_cpu_context);
3320}
3321
Peter Zijlstraf29ac752009-06-19 18:27:26 +02003322void __perf_swcounter_event(u32 event, u64 nr, int nmi,
3323 struct pt_regs *regs, u64 addr)
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003324{
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003325 struct perf_sample_data data = {
3326 .regs = regs,
3327 .addr = addr,
3328 };
3329
3330 do_perf_swcounter_event(PERF_TYPE_SOFTWARE, event, nr, nmi, &data);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003331}
3332
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003333static void perf_swcounter_read(struct perf_counter *counter)
3334{
3335 perf_swcounter_update(counter);
3336}
3337
3338static int perf_swcounter_enable(struct perf_counter *counter)
3339{
3340 perf_swcounter_set_period(counter);
3341 return 0;
3342}
3343
3344static void perf_swcounter_disable(struct perf_counter *counter)
3345{
3346 perf_swcounter_update(counter);
3347}
3348
Robert Richter4aeb0b42009-04-29 12:47:03 +02003349static const struct pmu perf_ops_generic = {
Peter Zijlstraac17dc82009-03-13 12:21:34 +01003350 .enable = perf_swcounter_enable,
3351 .disable = perf_swcounter_disable,
3352 .read = perf_swcounter_read,
3353};
3354
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003355/*
3356 * Software counter: cpu wall time clock
3357 */
3358
Paul Mackerras9abf8a02009-01-09 16:26:43 +11003359static void cpu_clock_perf_counter_update(struct perf_counter *counter)
3360{
3361 int cpu = raw_smp_processor_id();
3362 s64 prev;
3363 u64 now;
3364
3365 now = cpu_clock(cpu);
3366 prev = atomic64_read(&counter->hw.prev_count);
3367 atomic64_set(&counter->hw.prev_count, now);
3368 atomic64_add(now - prev, &counter->count);
3369}
3370
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003371static int cpu_clock_perf_counter_enable(struct perf_counter *counter)
3372{
3373 struct hw_perf_counter *hwc = &counter->hw;
3374 int cpu = raw_smp_processor_id();
3375
3376 atomic64_set(&hwc->prev_count, cpu_clock(cpu));
Peter Zijlstra039fc912009-03-13 16:43:47 +01003377 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
3378 hwc->hrtimer.function = perf_swcounter_hrtimer;
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003379 if (hwc->sample_period) {
3380 u64 period = max_t(u64, 10000, hwc->sample_period);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003381 __hrtimer_start_range_ns(&hwc->hrtimer,
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003382 ns_to_ktime(period), 0,
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003383 HRTIMER_MODE_REL, 0);
3384 }
3385
3386 return 0;
3387}
3388
Ingo Molnar5c92d122008-12-11 13:21:10 +01003389static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
3390{
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003391 if (counter->hw.sample_period)
Peter Zijlstrab986d7e2009-05-20 12:21:21 +02003392 hrtimer_cancel(&counter->hw.hrtimer);
Paul Mackerras9abf8a02009-01-09 16:26:43 +11003393 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01003394}
3395
3396static void cpu_clock_perf_counter_read(struct perf_counter *counter)
3397{
Paul Mackerras9abf8a02009-01-09 16:26:43 +11003398 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01003399}
3400
Robert Richter4aeb0b42009-04-29 12:47:03 +02003401static const struct pmu perf_ops_cpu_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +01003402 .enable = cpu_clock_perf_counter_enable,
3403 .disable = cpu_clock_perf_counter_disable,
3404 .read = cpu_clock_perf_counter_read,
Ingo Molnar5c92d122008-12-11 13:21:10 +01003405};
3406
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01003407/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003408 * Software counter: task time clock
3409 */
3410
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02003411static void task_clock_perf_counter_update(struct perf_counter *counter, u64 now)
Ingo Molnarbae43c92008-12-11 14:03:20 +01003412{
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02003413 u64 prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003414 s64 delta;
Ingo Molnarbae43c92008-12-11 14:03:20 +01003415
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02003416 prev = atomic64_xchg(&counter->hw.prev_count, now);
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003417 delta = now - prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003418 atomic64_add(delta, &counter->count);
Ingo Molnarbae43c92008-12-11 14:03:20 +01003419}
3420
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01003421static int task_clock_perf_counter_enable(struct perf_counter *counter)
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003422{
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003423 struct hw_perf_counter *hwc = &counter->hw;
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02003424 u64 now;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003425
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02003426 now = counter->ctx->time;
3427
3428 atomic64_set(&hwc->prev_count, now);
Peter Zijlstra039fc912009-03-13 16:43:47 +01003429 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
3430 hwc->hrtimer.function = perf_swcounter_hrtimer;
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003431 if (hwc->sample_period) {
3432 u64 period = max_t(u64, 10000, hwc->sample_period);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003433 __hrtimer_start_range_ns(&hwc->hrtimer,
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003434 ns_to_ktime(period), 0,
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003435 HRTIMER_MODE_REL, 0);
3436 }
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01003437
3438 return 0;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003439}
3440
3441static void task_clock_perf_counter_disable(struct perf_counter *counter)
3442{
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003443 if (counter->hw.sample_period)
Peter Zijlstrab986d7e2009-05-20 12:21:21 +02003444 hrtimer_cancel(&counter->hw.hrtimer);
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02003445 task_clock_perf_counter_update(counter, counter->ctx->time);
3446
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003447}
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01003448
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003449static void task_clock_perf_counter_read(struct perf_counter *counter)
3450{
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02003451 u64 time;
3452
3453 if (!in_nmi()) {
3454 update_context_time(counter->ctx);
3455 time = counter->ctx->time;
3456 } else {
3457 u64 now = perf_clock();
3458 u64 delta = now - counter->ctx->timestamp;
3459 time = counter->ctx->time + delta;
3460 }
3461
3462 task_clock_perf_counter_update(counter, time);
Ingo Molnarbae43c92008-12-11 14:03:20 +01003463}
3464
Robert Richter4aeb0b42009-04-29 12:47:03 +02003465static const struct pmu perf_ops_task_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +01003466 .enable = task_clock_perf_counter_enable,
3467 .disable = task_clock_perf_counter_disable,
3468 .read = task_clock_perf_counter_read,
Ingo Molnarbae43c92008-12-11 14:03:20 +01003469};
3470
Peter Zijlstrae077df42009-03-19 20:26:17 +01003471#ifdef CONFIG_EVENT_PROFILE
3472void perf_tpcounter_event(int event_id)
3473{
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003474 struct perf_sample_data data = {
3475 .regs = get_irq_regs();
3476 .addr = 0,
3477 };
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003478
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003479 if (!data.regs)
3480 data.regs = task_pt_regs(current);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003481
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003482 do_perf_swcounter_event(PERF_TYPE_TRACEPOINT, event_id, 1, 1, &data);
Peter Zijlstrae077df42009-03-19 20:26:17 +01003483}
Steven Whitehouseff7b1b42009-04-15 16:55:05 +01003484EXPORT_SYMBOL_GPL(perf_tpcounter_event);
Peter Zijlstrae077df42009-03-19 20:26:17 +01003485
3486extern int ftrace_profile_enable(int);
3487extern void ftrace_profile_disable(int);
3488
3489static void tp_perf_counter_destroy(struct perf_counter *counter)
3490{
Peter Zijlstra0d486962009-06-02 19:22:16 +02003491 ftrace_profile_disable(perf_event_id(&counter->attr));
Peter Zijlstrae077df42009-03-19 20:26:17 +01003492}
3493
Robert Richter4aeb0b42009-04-29 12:47:03 +02003494static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
Peter Zijlstrae077df42009-03-19 20:26:17 +01003495{
Peter Zijlstra0d486962009-06-02 19:22:16 +02003496 int event_id = perf_event_id(&counter->attr);
Peter Zijlstrae077df42009-03-19 20:26:17 +01003497 int ret;
3498
3499 ret = ftrace_profile_enable(event_id);
3500 if (ret)
3501 return NULL;
3502
3503 counter->destroy = tp_perf_counter_destroy;
3504
3505 return &perf_ops_generic;
3506}
3507#else
Robert Richter4aeb0b42009-04-29 12:47:03 +02003508static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
Peter Zijlstrae077df42009-03-19 20:26:17 +01003509{
3510 return NULL;
3511}
3512#endif
3513
Peter Zijlstraf29ac752009-06-19 18:27:26 +02003514atomic_t perf_swcounter_enabled[PERF_COUNT_SW_MAX];
3515
3516static void sw_perf_counter_destroy(struct perf_counter *counter)
3517{
3518 u64 event = counter->attr.config;
3519
Peter Zijlstraf3440112009-06-22 13:58:35 +02003520 WARN_ON(counter->parent);
3521
Peter Zijlstraf29ac752009-06-19 18:27:26 +02003522 atomic_dec(&perf_swcounter_enabled[event]);
3523}
3524
Robert Richter4aeb0b42009-04-29 12:47:03 +02003525static const struct pmu *sw_perf_counter_init(struct perf_counter *counter)
Ingo Molnar5c92d122008-12-11 13:21:10 +01003526{
Robert Richter4aeb0b42009-04-29 12:47:03 +02003527 const struct pmu *pmu = NULL;
Peter Zijlstraf29ac752009-06-19 18:27:26 +02003528 u64 event = counter->attr.config;
Ingo Molnar5c92d122008-12-11 13:21:10 +01003529
Paul Mackerras0475f9e2009-02-11 14:35:35 +11003530 /*
3531 * Software counters (currently) can't in general distinguish
3532 * between user, kernel and hypervisor events.
3533 * However, context switches and cpu migrations are considered
3534 * to be kernel events, and page faults are never hypervisor
3535 * events.
3536 */
Peter Zijlstraf29ac752009-06-19 18:27:26 +02003537 switch (event) {
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +02003538 case PERF_COUNT_SW_CPU_CLOCK:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003539 pmu = &perf_ops_cpu_clock;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003540
Ingo Molnar5c92d122008-12-11 13:21:10 +01003541 break;
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +02003542 case PERF_COUNT_SW_TASK_CLOCK:
Paul Mackerras23a185c2009-02-09 22:42:47 +11003543 /*
3544 * If the user instantiates this as a per-cpu counter,
3545 * use the cpu_clock counter instead.
3546 */
3547 if (counter->ctx->task)
Robert Richter4aeb0b42009-04-29 12:47:03 +02003548 pmu = &perf_ops_task_clock;
Paul Mackerras23a185c2009-02-09 22:42:47 +11003549 else
Robert Richter4aeb0b42009-04-29 12:47:03 +02003550 pmu = &perf_ops_cpu_clock;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003551
Ingo Molnarbae43c92008-12-11 14:03:20 +01003552 break;
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +02003553 case PERF_COUNT_SW_PAGE_FAULTS:
3554 case PERF_COUNT_SW_PAGE_FAULTS_MIN:
3555 case PERF_COUNT_SW_PAGE_FAULTS_MAJ:
3556 case PERF_COUNT_SW_CONTEXT_SWITCHES:
3557 case PERF_COUNT_SW_CPU_MIGRATIONS:
Peter Zijlstraf3440112009-06-22 13:58:35 +02003558 if (!counter->parent) {
3559 atomic_inc(&perf_swcounter_enabled[event]);
3560 counter->destroy = sw_perf_counter_destroy;
3561 }
Paul Mackerras3f731ca2009-06-01 17:52:30 +10003562 pmu = &perf_ops_generic;
Ingo Molnar6c594c22008-12-14 12:34:15 +01003563 break;
Ingo Molnar5c92d122008-12-11 13:21:10 +01003564 }
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003565
Robert Richter4aeb0b42009-04-29 12:47:03 +02003566 return pmu;
Ingo Molnar5c92d122008-12-11 13:21:10 +01003567}
3568
Thomas Gleixner0793a612008-12-04 20:12:29 +01003569/*
3570 * Allocate and initialize a counter structure
3571 */
3572static struct perf_counter *
Peter Zijlstra0d486962009-06-02 19:22:16 +02003573perf_counter_alloc(struct perf_counter_attr *attr,
Ingo Molnar04289bb2008-12-11 08:38:42 +01003574 int cpu,
Paul Mackerras23a185c2009-02-09 22:42:47 +11003575 struct perf_counter_context *ctx,
Ingo Molnar9b51f662008-12-12 13:49:45 +01003576 struct perf_counter *group_leader,
Peter Zijlstrab84fbc92009-06-22 13:57:40 +02003577 struct perf_counter *parent_counter,
Ingo Molnar9b51f662008-12-12 13:49:45 +01003578 gfp_t gfpflags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003579{
Robert Richter4aeb0b42009-04-29 12:47:03 +02003580 const struct pmu *pmu;
Ingo Molnar621a01e2008-12-11 12:46:46 +01003581 struct perf_counter *counter;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003582 struct hw_perf_counter *hwc;
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003583 long err;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003584
Ingo Molnar9b51f662008-12-12 13:49:45 +01003585 counter = kzalloc(sizeof(*counter), gfpflags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003586 if (!counter)
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003587 return ERR_PTR(-ENOMEM);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003588
Ingo Molnar04289bb2008-12-11 08:38:42 +01003589 /*
3590 * Single counters are their own group leaders, with an
3591 * empty sibling list:
3592 */
3593 if (!group_leader)
3594 group_leader = counter;
3595
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003596 mutex_init(&counter->child_mutex);
3597 INIT_LIST_HEAD(&counter->child_list);
3598
Ingo Molnar04289bb2008-12-11 08:38:42 +01003599 INIT_LIST_HEAD(&counter->list_entry);
Peter Zijlstra592903c2009-03-13 12:21:36 +01003600 INIT_LIST_HEAD(&counter->event_entry);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003601 INIT_LIST_HEAD(&counter->sibling_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003602 init_waitqueue_head(&counter->waitq);
3603
Peter Zijlstra7b732a72009-03-23 18:22:10 +01003604 mutex_init(&counter->mmap_mutex);
3605
Peter Zijlstraa96bbc12009-06-03 14:01:36 +02003606 counter->cpu = cpu;
Peter Zijlstra0d486962009-06-02 19:22:16 +02003607 counter->attr = *attr;
Peter Zijlstraa96bbc12009-06-03 14:01:36 +02003608 counter->group_leader = group_leader;
3609 counter->pmu = NULL;
3610 counter->ctx = ctx;
3611 counter->oncpu = -1;
Ingo Molnar329d8762009-05-26 08:10:00 +02003612
Peter Zijlstrab84fbc92009-06-22 13:57:40 +02003613 counter->parent = parent_counter;
3614
Peter Zijlstraa96bbc12009-06-03 14:01:36 +02003615 counter->ns = get_pid_ns(current->nsproxy->pid_ns);
3616 counter->id = atomic64_inc_return(&perf_counter_id);
3617
3618 counter->state = PERF_COUNTER_STATE_INACTIVE;
3619
Peter Zijlstra0d486962009-06-02 19:22:16 +02003620 if (attr->disabled)
Ingo Molnara86ed502008-12-17 00:43:10 +01003621 counter->state = PERF_COUNTER_STATE_OFF;
3622
Robert Richter4aeb0b42009-04-29 12:47:03 +02003623 pmu = NULL;
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003624
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003625 hwc = &counter->hw;
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02003626 hwc->sample_period = attr->sample_period;
Peter Zijlstra0d486962009-06-02 19:22:16 +02003627 if (attr->freq && attr->sample_freq)
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02003628 hwc->sample_period = 1;
3629
3630 atomic64_set(&hwc->period_left, hwc->sample_period);
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003631
Peter Zijlstra2023b352009-05-05 17:50:26 +02003632 /*
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003633 * we currently do not support PERF_SAMPLE_GROUP on inherited counters
Peter Zijlstra2023b352009-05-05 17:50:26 +02003634 */
Peter Zijlstra0d486962009-06-02 19:22:16 +02003635 if (attr->inherit && (attr->sample_type & PERF_SAMPLE_GROUP))
Peter Zijlstra2023b352009-05-05 17:50:26 +02003636 goto done;
3637
Ingo Molnara21ca2c2009-06-06 09:58:57 +02003638 switch (attr->type) {
Peter Zijlstra081fad82009-06-11 17:57:21 +02003639 case PERF_TYPE_RAW:
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003640 case PERF_TYPE_HARDWARE:
Ingo Molnar8326f442009-06-05 20:22:46 +02003641 case PERF_TYPE_HW_CACHE:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003642 pmu = hw_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003643 break;
3644
3645 case PERF_TYPE_SOFTWARE:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003646 pmu = sw_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003647 break;
3648
3649 case PERF_TYPE_TRACEPOINT:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003650 pmu = tp_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003651 break;
Peter Zijlstra974802e2009-06-12 12:46:55 +02003652
3653 default:
3654 break;
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003655 }
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01003656done:
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003657 err = 0;
Robert Richter4aeb0b42009-04-29 12:47:03 +02003658 if (!pmu)
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003659 err = -EINVAL;
Robert Richter4aeb0b42009-04-29 12:47:03 +02003660 else if (IS_ERR(pmu))
3661 err = PTR_ERR(pmu);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003662
3663 if (err) {
Peter Zijlstraa96bbc12009-06-03 14:01:36 +02003664 if (counter->ns)
3665 put_pid_ns(counter->ns);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003666 kfree(counter);
3667 return ERR_PTR(err);
3668 }
3669
Robert Richter4aeb0b42009-04-29 12:47:03 +02003670 counter->pmu = pmu;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003671
Peter Zijlstraf3440112009-06-22 13:58:35 +02003672 if (!counter->parent) {
3673 atomic_inc(&nr_counters);
3674 if (counter->attr.mmap)
3675 atomic_inc(&nr_mmap_counters);
3676 if (counter->attr.comm)
3677 atomic_inc(&nr_comm_counters);
3678 }
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02003679
Thomas Gleixner0793a612008-12-04 20:12:29 +01003680 return counter;
3681}
3682
Peter Zijlstra974802e2009-06-12 12:46:55 +02003683static int perf_copy_attr(struct perf_counter_attr __user *uattr,
3684 struct perf_counter_attr *attr)
3685{
3686 int ret;
3687 u32 size;
3688
3689 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
3690 return -EFAULT;
3691
3692 /*
3693 * zero the full structure, so that a short copy will be nice.
3694 */
3695 memset(attr, 0, sizeof(*attr));
3696
3697 ret = get_user(size, &uattr->size);
3698 if (ret)
3699 return ret;
3700
3701 if (size > PAGE_SIZE) /* silly large */
3702 goto err_size;
3703
3704 if (!size) /* abi compat */
3705 size = PERF_ATTR_SIZE_VER0;
3706
3707 if (size < PERF_ATTR_SIZE_VER0)
3708 goto err_size;
3709
3710 /*
3711 * If we're handed a bigger struct than we know of,
3712 * ensure all the unknown bits are 0.
3713 */
3714 if (size > sizeof(*attr)) {
3715 unsigned long val;
3716 unsigned long __user *addr;
3717 unsigned long __user *end;
3718
3719 addr = PTR_ALIGN((void __user *)uattr + sizeof(*attr),
3720 sizeof(unsigned long));
3721 end = PTR_ALIGN((void __user *)uattr + size,
3722 sizeof(unsigned long));
3723
3724 for (; addr < end; addr += sizeof(unsigned long)) {
3725 ret = get_user(val, addr);
3726 if (ret)
3727 return ret;
3728 if (val)
3729 goto err_size;
3730 }
3731 }
3732
3733 ret = copy_from_user(attr, uattr, size);
3734 if (ret)
3735 return -EFAULT;
3736
3737 /*
3738 * If the type exists, the corresponding creation will verify
3739 * the attr->config.
3740 */
3741 if (attr->type >= PERF_TYPE_MAX)
3742 return -EINVAL;
3743
3744 if (attr->__reserved_1 || attr->__reserved_2 || attr->__reserved_3)
3745 return -EINVAL;
3746
3747 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
3748 return -EINVAL;
3749
3750 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
3751 return -EINVAL;
3752
3753out:
3754 return ret;
3755
3756err_size:
3757 put_user(sizeof(*attr), &uattr->size);
3758 ret = -E2BIG;
3759 goto out;
3760}
3761
Thomas Gleixner0793a612008-12-04 20:12:29 +01003762/**
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003763 * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
Ingo Molnar9f66a382008-12-10 12:33:23 +01003764 *
Peter Zijlstra0d486962009-06-02 19:22:16 +02003765 * @attr_uptr: event type attributes for monitoring/sampling
Thomas Gleixner0793a612008-12-04 20:12:29 +01003766 * @pid: target pid
Ingo Molnar9f66a382008-12-10 12:33:23 +01003767 * @cpu: target cpu
3768 * @group_fd: group leader counter fd
Thomas Gleixner0793a612008-12-04 20:12:29 +01003769 */
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003770SYSCALL_DEFINE5(perf_counter_open,
Peter Zijlstra974802e2009-06-12 12:46:55 +02003771 struct perf_counter_attr __user *, attr_uptr,
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003772 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003773{
Ingo Molnar04289bb2008-12-11 08:38:42 +01003774 struct perf_counter *counter, *group_leader;
Peter Zijlstra0d486962009-06-02 19:22:16 +02003775 struct perf_counter_attr attr;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003776 struct perf_counter_context *ctx;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003777 struct file *counter_file = NULL;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003778 struct file *group_file = NULL;
3779 int fput_needed = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003780 int fput_needed2 = 0;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003781 int ret;
3782
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003783 /* for future expandability... */
3784 if (flags)
3785 return -EINVAL;
3786
Peter Zijlstra974802e2009-06-12 12:46:55 +02003787 ret = perf_copy_attr(attr_uptr, &attr);
3788 if (ret)
3789 return ret;
Thomas Gleixnereab656a2008-12-08 19:26:59 +01003790
Peter Zijlstra07647712009-06-11 11:18:36 +02003791 if (!attr.exclude_kernel) {
3792 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
3793 return -EACCES;
3794 }
3795
Peter Zijlstradf58ab22009-06-11 11:25:05 +02003796 if (attr.freq) {
3797 if (attr.sample_freq > sysctl_perf_counter_sample_rate)
3798 return -EINVAL;
3799 }
3800
Ingo Molnar04289bb2008-12-11 08:38:42 +01003801 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01003802 * Get the target context (task or percpu):
3803 */
3804 ctx = find_get_context(pid, cpu);
3805 if (IS_ERR(ctx))
3806 return PTR_ERR(ctx);
3807
3808 /*
3809 * Look up the group leader (we will attach this counter to it):
Ingo Molnar04289bb2008-12-11 08:38:42 +01003810 */
3811 group_leader = NULL;
3812 if (group_fd != -1) {
3813 ret = -EINVAL;
3814 group_file = fget_light(group_fd, &fput_needed);
3815 if (!group_file)
Ingo Molnarccff2862008-12-11 11:26:29 +01003816 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003817 if (group_file->f_op != &perf_fops)
Ingo Molnarccff2862008-12-11 11:26:29 +01003818 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003819
3820 group_leader = group_file->private_data;
3821 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01003822 * Do not allow a recursive hierarchy (this new sibling
3823 * becoming part of another group-sibling):
Ingo Molnar04289bb2008-12-11 08:38:42 +01003824 */
Ingo Molnarccff2862008-12-11 11:26:29 +01003825 if (group_leader->group_leader != group_leader)
3826 goto err_put_context;
3827 /*
3828 * Do not allow to attach to a group in a different
3829 * task or CPU context:
3830 */
3831 if (group_leader->ctx != ctx)
3832 goto err_put_context;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11003833 /*
3834 * Only a group leader can be exclusive or pinned
3835 */
Peter Zijlstra0d486962009-06-02 19:22:16 +02003836 if (attr.exclusive || attr.pinned)
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11003837 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003838 }
3839
Peter Zijlstra0d486962009-06-02 19:22:16 +02003840 counter = perf_counter_alloc(&attr, cpu, ctx, group_leader,
Peter Zijlstrab84fbc92009-06-22 13:57:40 +02003841 NULL, GFP_KERNEL);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003842 ret = PTR_ERR(counter);
3843 if (IS_ERR(counter))
Thomas Gleixner0793a612008-12-04 20:12:29 +01003844 goto err_put_context;
3845
Thomas Gleixner0793a612008-12-04 20:12:29 +01003846 ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
3847 if (ret < 0)
Ingo Molnar9b51f662008-12-12 13:49:45 +01003848 goto err_free_put_context;
3849
3850 counter_file = fget_light(ret, &fput_needed2);
3851 if (!counter_file)
3852 goto err_free_put_context;
3853
3854 counter->filp = counter_file;
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003855 WARN_ON_ONCE(ctx->parent_ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003856 mutex_lock(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003857 perf_install_in_context(ctx, counter, cpu);
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003858 ++ctx->generation;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003859 mutex_unlock(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003860
Peter Zijlstra082ff5a2009-05-23 18:29:00 +02003861 counter->owner = current;
3862 get_task_struct(current);
3863 mutex_lock(&current->perf_counter_mutex);
3864 list_add_tail(&counter->owner_entry, &current->perf_counter_list);
3865 mutex_unlock(&current->perf_counter_mutex);
3866
Ingo Molnar9b51f662008-12-12 13:49:45 +01003867 fput_light(counter_file, fput_needed2);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003868
Ingo Molnar04289bb2008-12-11 08:38:42 +01003869out_fput:
3870 fput_light(group_file, fput_needed);
3871
Thomas Gleixner0793a612008-12-04 20:12:29 +01003872 return ret;
3873
Ingo Molnar9b51f662008-12-12 13:49:45 +01003874err_free_put_context:
Thomas Gleixner0793a612008-12-04 20:12:29 +01003875 kfree(counter);
3876
3877err_put_context:
Paul Mackerrasc93f7662009-05-28 22:18:17 +10003878 put_ctx(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003879
Ingo Molnar04289bb2008-12-11 08:38:42 +01003880 goto out_fput;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003881}
3882
Ingo Molnar9b51f662008-12-12 13:49:45 +01003883/*
Ingo Molnar9b51f662008-12-12 13:49:45 +01003884 * inherit a counter from parent task to child task:
3885 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11003886static struct perf_counter *
Ingo Molnar9b51f662008-12-12 13:49:45 +01003887inherit_counter(struct perf_counter *parent_counter,
3888 struct task_struct *parent,
3889 struct perf_counter_context *parent_ctx,
3890 struct task_struct *child,
Paul Mackerrasd859e292009-01-17 18:10:22 +11003891 struct perf_counter *group_leader,
Ingo Molnar9b51f662008-12-12 13:49:45 +01003892 struct perf_counter_context *child_ctx)
3893{
3894 struct perf_counter *child_counter;
3895
Paul Mackerrasd859e292009-01-17 18:10:22 +11003896 /*
3897 * Instead of creating recursive hierarchies of counters,
3898 * we link inherited counters back to the original parent,
3899 * which has a filp for sure, which we use as the reference
3900 * count:
3901 */
3902 if (parent_counter->parent)
3903 parent_counter = parent_counter->parent;
3904
Peter Zijlstra0d486962009-06-02 19:22:16 +02003905 child_counter = perf_counter_alloc(&parent_counter->attr,
Paul Mackerras23a185c2009-02-09 22:42:47 +11003906 parent_counter->cpu, child_ctx,
Peter Zijlstrab84fbc92009-06-22 13:57:40 +02003907 group_leader, parent_counter,
3908 GFP_KERNEL);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003909 if (IS_ERR(child_counter))
3910 return child_counter;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10003911 get_ctx(child_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003912
3913 /*
Paul Mackerras564c2b22009-05-22 14:27:22 +10003914 * Make the child state follow the state of the parent counter,
Peter Zijlstra0d486962009-06-02 19:22:16 +02003915 * not its attr.disabled bit. We hold the parent's mutex,
Ingo Molnar22a4f652009-06-01 10:13:37 +02003916 * so we won't race with perf_counter_{en, dis}able_family.
Paul Mackerras564c2b22009-05-22 14:27:22 +10003917 */
3918 if (parent_counter->state >= PERF_COUNTER_STATE_INACTIVE)
3919 child_counter->state = PERF_COUNTER_STATE_INACTIVE;
3920 else
3921 child_counter->state = PERF_COUNTER_STATE_OFF;
3922
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02003923 if (parent_counter->attr.freq)
3924 child_counter->hw.sample_period = parent_counter->hw.sample_period;
3925
Paul Mackerras564c2b22009-05-22 14:27:22 +10003926 /*
Ingo Molnar9b51f662008-12-12 13:49:45 +01003927 * Link it up in the child's context:
3928 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +11003929 add_counter_to_ctx(child_counter, child_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003930
Ingo Molnar9b51f662008-12-12 13:49:45 +01003931 /*
3932 * Get a reference to the parent filp - we will fput it
3933 * when the child counter exits. This is safe to do because
3934 * we are in the parent and we know that the filp still
3935 * exists and has a nonzero count:
3936 */
3937 atomic_long_inc(&parent_counter->filp->f_count);
3938
Paul Mackerrasd859e292009-01-17 18:10:22 +11003939 /*
3940 * Link this into the parent counter's child list
3941 */
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003942 WARN_ON_ONCE(parent_counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003943 mutex_lock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003944 list_add_tail(&child_counter->child_list, &parent_counter->child_list);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003945 mutex_unlock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003946
3947 return child_counter;
3948}
3949
3950static int inherit_group(struct perf_counter *parent_counter,
3951 struct task_struct *parent,
3952 struct perf_counter_context *parent_ctx,
3953 struct task_struct *child,
3954 struct perf_counter_context *child_ctx)
3955{
3956 struct perf_counter *leader;
3957 struct perf_counter *sub;
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003958 struct perf_counter *child_ctr;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003959
3960 leader = inherit_counter(parent_counter, parent, parent_ctx,
3961 child, NULL, child_ctx);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003962 if (IS_ERR(leader))
3963 return PTR_ERR(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003964 list_for_each_entry(sub, &parent_counter->sibling_list, list_entry) {
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003965 child_ctr = inherit_counter(sub, parent, parent_ctx,
3966 child, leader, child_ctx);
3967 if (IS_ERR(child_ctr))
3968 return PTR_ERR(child_ctr);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003969 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01003970 return 0;
3971}
3972
Paul Mackerrasd859e292009-01-17 18:10:22 +11003973static void sync_child_counter(struct perf_counter *child_counter,
3974 struct perf_counter *parent_counter)
3975{
Peter Zijlstra8bc20952009-05-15 20:45:59 +02003976 u64 child_val;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003977
Paul Mackerrasd859e292009-01-17 18:10:22 +11003978 child_val = atomic64_read(&child_counter->count);
3979
3980 /*
3981 * Add back the child's count to the parent's count:
3982 */
3983 atomic64_add(child_val, &parent_counter->count);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11003984 atomic64_add(child_counter->total_time_enabled,
3985 &parent_counter->child_total_time_enabled);
3986 atomic64_add(child_counter->total_time_running,
3987 &parent_counter->child_total_time_running);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003988
3989 /*
3990 * Remove this counter from the parent's list
3991 */
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003992 WARN_ON_ONCE(parent_counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003993 mutex_lock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003994 list_del_init(&child_counter->child_list);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003995 mutex_unlock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003996
3997 /*
3998 * Release the parent counter, if this was the last
3999 * reference to it.
4000 */
4001 fput(parent_counter->filp);
4002}
4003
Ingo Molnar9b51f662008-12-12 13:49:45 +01004004static void
Peter Zijlstrabbbee902009-05-29 14:25:58 +02004005__perf_counter_exit_task(struct perf_counter *child_counter,
Ingo Molnar9b51f662008-12-12 13:49:45 +01004006 struct perf_counter_context *child_ctx)
4007{
4008 struct perf_counter *parent_counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004009
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004010 update_counter_times(child_counter);
Peter Zijlstraaa9c67f2009-05-23 18:28:59 +02004011 perf_counter_remove_from_context(child_counter);
Ingo Molnar0cc0c022008-12-14 23:20:36 +01004012
Ingo Molnar9b51f662008-12-12 13:49:45 +01004013 parent_counter = child_counter->parent;
4014 /*
4015 * It can happen that parent exits first, and has counters
4016 * that are still around due to the child reference. These
4017 * counters need to be zapped - but otherwise linger.
4018 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11004019 if (parent_counter) {
4020 sync_child_counter(child_counter, parent_counter);
Peter Zijlstraf1600952009-03-19 20:26:16 +01004021 free_counter(child_counter);
Paul Mackerras4bcf3492009-02-11 13:53:19 +01004022 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01004023}
4024
4025/*
Paul Mackerrasd859e292009-01-17 18:10:22 +11004026 * When a child task exits, feed back counter values to parent counters.
Ingo Molnar9b51f662008-12-12 13:49:45 +01004027 */
4028void perf_counter_exit_task(struct task_struct *child)
4029{
4030 struct perf_counter *child_counter, *tmp;
4031 struct perf_counter_context *child_ctx;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004032 unsigned long flags;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004033
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004034 if (likely(!child->perf_counter_ctxp))
Ingo Molnar9b51f662008-12-12 13:49:45 +01004035 return;
4036
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004037 local_irq_save(flags);
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004038 /*
4039 * We can't reschedule here because interrupts are disabled,
4040 * and either child is current or it is a task that can't be
4041 * scheduled, so we are now safe from rescheduling changing
4042 * our context.
4043 */
4044 child_ctx = child->perf_counter_ctxp;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004045 __perf_counter_task_sched_out(child_ctx);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10004046
4047 /*
4048 * Take the context lock here so that if find_get_context is
4049 * reading child->perf_counter_ctxp, we wait until it has
4050 * incremented the context's refcount before we do put_ctx below.
4051 */
4052 spin_lock(&child_ctx->lock);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004053 child->perf_counter_ctxp = NULL;
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004054 if (child_ctx->parent_ctx) {
4055 /*
4056 * This context is a clone; unclone it so it can't get
4057 * swapped to another process while we're removing all
4058 * the counters from it.
4059 */
4060 put_ctx(child_ctx->parent_ctx);
4061 child_ctx->parent_ctx = NULL;
4062 }
Paul Mackerrasc93f7662009-05-28 22:18:17 +10004063 spin_unlock(&child_ctx->lock);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004064 local_irq_restore(flags);
4065
Peter Zijlstra66fff222009-06-10 22:53:37 +02004066 /*
4067 * We can recurse on the same lock type through:
4068 *
4069 * __perf_counter_exit_task()
4070 * sync_child_counter()
4071 * fput(parent_counter->filp)
4072 * perf_release()
4073 * mutex_lock(&ctx->mutex)
4074 *
4075 * But since its the parent context it won't be the same instance.
4076 */
4077 mutex_lock_nested(&child_ctx->mutex, SINGLE_DEPTH_NESTING);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004078
Peter Zijlstra8bc20952009-05-15 20:45:59 +02004079again:
Ingo Molnar9b51f662008-12-12 13:49:45 +01004080 list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list,
4081 list_entry)
Peter Zijlstrabbbee902009-05-29 14:25:58 +02004082 __perf_counter_exit_task(child_counter, child_ctx);
Peter Zijlstra8bc20952009-05-15 20:45:59 +02004083
4084 /*
4085 * If the last counter was a group counter, it will have appended all
4086 * its siblings to the list, but we obtained 'tmp' before that which
4087 * will still point to the list head terminating the iteration.
4088 */
4089 if (!list_empty(&child_ctx->counter_list))
4090 goto again;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004091
4092 mutex_unlock(&child_ctx->mutex);
4093
4094 put_ctx(child_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01004095}
4096
4097/*
Peter Zijlstrabbbee902009-05-29 14:25:58 +02004098 * free an unexposed, unused context as created by inheritance by
4099 * init_task below, used by fork() in case of fail.
4100 */
4101void perf_counter_free_task(struct task_struct *task)
4102{
4103 struct perf_counter_context *ctx = task->perf_counter_ctxp;
4104 struct perf_counter *counter, *tmp;
4105
4106 if (!ctx)
4107 return;
4108
4109 mutex_lock(&ctx->mutex);
4110again:
4111 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry) {
4112 struct perf_counter *parent = counter->parent;
4113
4114 if (WARN_ON_ONCE(!parent))
4115 continue;
4116
4117 mutex_lock(&parent->child_mutex);
4118 list_del_init(&counter->child_list);
4119 mutex_unlock(&parent->child_mutex);
4120
4121 fput(parent->filp);
4122
4123 list_del_counter(counter, ctx);
4124 free_counter(counter);
4125 }
4126
4127 if (!list_empty(&ctx->counter_list))
4128 goto again;
4129
4130 mutex_unlock(&ctx->mutex);
4131
4132 put_ctx(ctx);
4133}
4134
4135/*
Ingo Molnar9b51f662008-12-12 13:49:45 +01004136 * Initialize the perf_counter context in task_struct
4137 */
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02004138int perf_counter_init_task(struct task_struct *child)
Ingo Molnar9b51f662008-12-12 13:49:45 +01004139{
4140 struct perf_counter_context *child_ctx, *parent_ctx;
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004141 struct perf_counter_context *cloned_ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +11004142 struct perf_counter *counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004143 struct task_struct *parent = current;
Paul Mackerras564c2b22009-05-22 14:27:22 +10004144 int inherited_all = 1;
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02004145 int ret = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004146
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004147 child->perf_counter_ctxp = NULL;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004148
Peter Zijlstra082ff5a2009-05-23 18:29:00 +02004149 mutex_init(&child->perf_counter_mutex);
4150 INIT_LIST_HEAD(&child->perf_counter_list);
4151
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004152 if (likely(!parent->perf_counter_ctxp))
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02004153 return 0;
4154
Ingo Molnar9b51f662008-12-12 13:49:45 +01004155 /*
4156 * This is executed from the parent task context, so inherit
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004157 * counters that have been marked for cloning.
4158 * First allocate and initialize a context for the child.
Ingo Molnar9b51f662008-12-12 13:49:45 +01004159 */
4160
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004161 child_ctx = kmalloc(sizeof(struct perf_counter_context), GFP_KERNEL);
4162 if (!child_ctx)
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02004163 return -ENOMEM;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004164
4165 __perf_counter_init_context(child_ctx, child);
4166 child->perf_counter_ctxp = child_ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10004167 get_task_struct(child);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004168
Ingo Molnar9b51f662008-12-12 13:49:45 +01004169 /*
Paul Mackerras25346b932009-06-01 17:48:12 +10004170 * If the parent's context is a clone, pin it so it won't get
4171 * swapped under us.
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004172 */
Paul Mackerras25346b932009-06-01 17:48:12 +10004173 parent_ctx = perf_pin_task_context(parent);
4174
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004175 /*
4176 * No need to check if parent_ctx != NULL here; since we saw
4177 * it non-NULL earlier, the only reason for it to become NULL
4178 * is if we exit, and since we're currently in the middle of
4179 * a fork we can't be exiting at the same time.
4180 */
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004181
4182 /*
Ingo Molnar9b51f662008-12-12 13:49:45 +01004183 * Lock the parent list. No need to lock the child - not PID
4184 * hashed yet and not running, so nobody can access it.
4185 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11004186 mutex_lock(&parent_ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01004187
4188 /*
4189 * We dont have to disable NMIs - we are only looking at
4190 * the list, not manipulating it:
4191 */
Peter Zijlstrad7b629a2009-05-20 12:21:19 +02004192 list_for_each_entry_rcu(counter, &parent_ctx->event_list, event_entry) {
4193 if (counter != counter->group_leader)
4194 continue;
4195
Peter Zijlstra0d486962009-06-02 19:22:16 +02004196 if (!counter->attr.inherit) {
Paul Mackerras564c2b22009-05-22 14:27:22 +10004197 inherited_all = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004198 continue;
Paul Mackerras564c2b22009-05-22 14:27:22 +10004199 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01004200
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02004201 ret = inherit_group(counter, parent, parent_ctx,
4202 child, child_ctx);
4203 if (ret) {
Paul Mackerras564c2b22009-05-22 14:27:22 +10004204 inherited_all = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004205 break;
Paul Mackerras564c2b22009-05-22 14:27:22 +10004206 }
4207 }
4208
4209 if (inherited_all) {
4210 /*
4211 * Mark the child context as a clone of the parent
4212 * context, or of whatever the parent is a clone of.
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004213 * Note that if the parent is a clone, it could get
4214 * uncloned at any point, but that doesn't matter
4215 * because the list of counters and the generation
4216 * count can't have changed since we took the mutex.
Paul Mackerras564c2b22009-05-22 14:27:22 +10004217 */
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004218 cloned_ctx = rcu_dereference(parent_ctx->parent_ctx);
4219 if (cloned_ctx) {
4220 child_ctx->parent_ctx = cloned_ctx;
Paul Mackerras25346b932009-06-01 17:48:12 +10004221 child_ctx->parent_gen = parent_ctx->parent_gen;
Paul Mackerras564c2b22009-05-22 14:27:22 +10004222 } else {
4223 child_ctx->parent_ctx = parent_ctx;
4224 child_ctx->parent_gen = parent_ctx->generation;
4225 }
4226 get_ctx(child_ctx->parent_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01004227 }
4228
Paul Mackerrasd859e292009-01-17 18:10:22 +11004229 mutex_unlock(&parent_ctx->mutex);
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02004230
Paul Mackerras25346b932009-06-01 17:48:12 +10004231 perf_unpin_context(parent_ctx);
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004232
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02004233 return ret;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004234}
4235
Ingo Molnar04289bb2008-12-11 08:38:42 +01004236static void __cpuinit perf_counter_init_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01004237{
Ingo Molnar04289bb2008-12-11 08:38:42 +01004238 struct perf_cpu_context *cpuctx;
Thomas Gleixner0793a612008-12-04 20:12:29 +01004239
Ingo Molnar04289bb2008-12-11 08:38:42 +01004240 cpuctx = &per_cpu(perf_cpu_context, cpu);
4241 __perf_counter_init_context(&cpuctx->ctx, NULL);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004242
Ingo Molnar1dce8d92009-05-04 19:23:18 +02004243 spin_lock(&perf_resource_lock);
Ingo Molnar04289bb2008-12-11 08:38:42 +01004244 cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu;
Ingo Molnar1dce8d92009-05-04 19:23:18 +02004245 spin_unlock(&perf_resource_lock);
Ingo Molnar04289bb2008-12-11 08:38:42 +01004246
Paul Mackerras01d02872009-01-14 13:44:19 +11004247 hw_perf_counter_setup(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004248}
4249
4250#ifdef CONFIG_HOTPLUG_CPU
Ingo Molnar04289bb2008-12-11 08:38:42 +01004251static void __perf_counter_exit_cpu(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01004252{
4253 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
4254 struct perf_counter_context *ctx = &cpuctx->ctx;
4255 struct perf_counter *counter, *tmp;
4256
Ingo Molnar04289bb2008-12-11 08:38:42 +01004257 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry)
4258 __perf_counter_remove_from_context(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004259}
Ingo Molnar04289bb2008-12-11 08:38:42 +01004260static void perf_counter_exit_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01004261{
Paul Mackerrasd859e292009-01-17 18:10:22 +11004262 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
4263 struct perf_counter_context *ctx = &cpuctx->ctx;
4264
4265 mutex_lock(&ctx->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01004266 smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1);
Paul Mackerrasd859e292009-01-17 18:10:22 +11004267 mutex_unlock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004268}
4269#else
Ingo Molnar04289bb2008-12-11 08:38:42 +01004270static inline void perf_counter_exit_cpu(int cpu) { }
Thomas Gleixner0793a612008-12-04 20:12:29 +01004271#endif
4272
4273static int __cpuinit
4274perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
4275{
4276 unsigned int cpu = (long)hcpu;
4277
4278 switch (action) {
4279
4280 case CPU_UP_PREPARE:
4281 case CPU_UP_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01004282 perf_counter_init_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004283 break;
4284
4285 case CPU_DOWN_PREPARE:
4286 case CPU_DOWN_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01004287 perf_counter_exit_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004288 break;
4289
4290 default:
4291 break;
4292 }
4293
4294 return NOTIFY_OK;
4295}
4296
Paul Mackerrasf38b0822009-06-02 21:05:16 +10004297/*
4298 * This has to have a higher priority than migration_notifier in sched.c.
4299 */
Thomas Gleixner0793a612008-12-04 20:12:29 +01004300static struct notifier_block __cpuinitdata perf_cpu_nb = {
4301 .notifier_call = perf_cpu_notify,
Paul Mackerrasf38b0822009-06-02 21:05:16 +10004302 .priority = 20,
Thomas Gleixner0793a612008-12-04 20:12:29 +01004303};
4304
Ingo Molnar0d905bc2009-05-04 19:13:30 +02004305void __init perf_counter_init(void)
Thomas Gleixner0793a612008-12-04 20:12:29 +01004306{
4307 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
4308 (void *)(long)smp_processor_id());
4309 register_cpu_notifier(&perf_cpu_nb);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004310}
Thomas Gleixner0793a612008-12-04 20:12:29 +01004311
4312static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
4313{
4314 return sprintf(buf, "%d\n", perf_reserved_percpu);
4315}
4316
4317static ssize_t
4318perf_set_reserve_percpu(struct sysdev_class *class,
4319 const char *buf,
4320 size_t count)
4321{
4322 struct perf_cpu_context *cpuctx;
4323 unsigned long val;
4324 int err, cpu, mpt;
4325
4326 err = strict_strtoul(buf, 10, &val);
4327 if (err)
4328 return err;
4329 if (val > perf_max_counters)
4330 return -EINVAL;
4331
Ingo Molnar1dce8d92009-05-04 19:23:18 +02004332 spin_lock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004333 perf_reserved_percpu = val;
4334 for_each_online_cpu(cpu) {
4335 cpuctx = &per_cpu(perf_cpu_context, cpu);
4336 spin_lock_irq(&cpuctx->ctx.lock);
4337 mpt = min(perf_max_counters - cpuctx->ctx.nr_counters,
4338 perf_max_counters - perf_reserved_percpu);
4339 cpuctx->max_pertask = mpt;
4340 spin_unlock_irq(&cpuctx->ctx.lock);
4341 }
Ingo Molnar1dce8d92009-05-04 19:23:18 +02004342 spin_unlock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004343
4344 return count;
4345}
4346
4347static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
4348{
4349 return sprintf(buf, "%d\n", perf_overcommit);
4350}
4351
4352static ssize_t
4353perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
4354{
4355 unsigned long val;
4356 int err;
4357
4358 err = strict_strtoul(buf, 10, &val);
4359 if (err)
4360 return err;
4361 if (val > 1)
4362 return -EINVAL;
4363
Ingo Molnar1dce8d92009-05-04 19:23:18 +02004364 spin_lock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004365 perf_overcommit = val;
Ingo Molnar1dce8d92009-05-04 19:23:18 +02004366 spin_unlock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004367
4368 return count;
4369}
4370
4371static SYSDEV_CLASS_ATTR(
4372 reserve_percpu,
4373 0644,
4374 perf_show_reserve_percpu,
4375 perf_set_reserve_percpu
4376 );
4377
4378static SYSDEV_CLASS_ATTR(
4379 overcommit,
4380 0644,
4381 perf_show_overcommit,
4382 perf_set_overcommit
4383 );
4384
4385static struct attribute *perfclass_attrs[] = {
4386 &attr_reserve_percpu.attr,
4387 &attr_overcommit.attr,
4388 NULL
4389};
4390
4391static struct attribute_group perfclass_attr_group = {
4392 .attrs = perfclass_attrs,
4393 .name = "perf_counters",
4394};
4395
4396static int __init perf_counter_sysfs_init(void)
4397{
4398 return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
4399 &perfclass_attr_group);
4400}
4401device_initcall(perf_counter_sysfs_init);