blob: 02994a719e2711f4a09de6b96f02d87ec92899af [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 Mackerras25346b92009-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 Mackerras25346b92009-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 Mackerras25346b92009-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 Mackerras25346b92009-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 Mackerras25346b92009-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 Mackerras25346b92009-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 Mackerras25346b92009-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 Mackerras25346b92009-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 Mackerras25346b92009-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 Zijlstra194002b2009-06-22 16:35:24 +02001756static int perf_counter_index(struct perf_counter *counter)
1757{
1758 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
1759 return 0;
1760
1761 return counter->hw.idx + 1 - PERF_COUNTER_INDEX_OFFSET;
1762}
1763
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001764/*
1765 * Callers need to ensure there can be no nesting of this function, otherwise
1766 * the seqlock logic goes bad. We can not serialize this because the arch
1767 * code calls this from NMI context.
1768 */
1769void perf_counter_update_userpage(struct perf_counter *counter)
Paul Mackerras37d81822009-03-23 18:22:08 +01001770{
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001771 struct perf_counter_mmap_page *userpg;
Ingo Molnar22a4f652009-06-01 10:13:37 +02001772 struct perf_mmap_data *data;
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001773
1774 rcu_read_lock();
1775 data = rcu_dereference(counter->data);
1776 if (!data)
1777 goto unlock;
1778
1779 userpg = data->user_page;
Paul Mackerras37d81822009-03-23 18:22:08 +01001780
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001781 /*
1782 * Disable preemption so as to not let the corresponding user-space
1783 * spin too long if we get preempted.
1784 */
1785 preempt_disable();
Paul Mackerras37d81822009-03-23 18:22:08 +01001786 ++userpg->lock;
Peter Zijlstra92f22a32009-04-02 11:12:04 +02001787 barrier();
Peter Zijlstra194002b2009-06-22 16:35:24 +02001788 userpg->index = perf_counter_index(counter);
Paul Mackerras37d81822009-03-23 18:22:08 +01001789 userpg->offset = atomic64_read(&counter->count);
1790 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
1791 userpg->offset -= atomic64_read(&counter->hw.prev_count);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001792
Peter Zijlstra7f8b4e42009-06-22 14:34:35 +02001793 userpg->time_enabled = counter->total_time_enabled +
1794 atomic64_read(&counter->child_total_time_enabled);
1795
1796 userpg->time_running = counter->total_time_running +
1797 atomic64_read(&counter->child_total_time_running);
1798
Peter Zijlstra92f22a32009-04-02 11:12:04 +02001799 barrier();
Paul Mackerras37d81822009-03-23 18:22:08 +01001800 ++userpg->lock;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001801 preempt_enable();
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001802unlock:
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001803 rcu_read_unlock();
Paul Mackerras37d81822009-03-23 18:22:08 +01001804}
1805
1806static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1807{
1808 struct perf_counter *counter = vma->vm_file->private_data;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001809 struct perf_mmap_data *data;
1810 int ret = VM_FAULT_SIGBUS;
Paul Mackerras37d81822009-03-23 18:22:08 +01001811
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01001812 if (vmf->flags & FAULT_FLAG_MKWRITE) {
1813 if (vmf->pgoff == 0)
1814 ret = 0;
1815 return ret;
1816 }
1817
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001818 rcu_read_lock();
1819 data = rcu_dereference(counter->data);
1820 if (!data)
1821 goto unlock;
Paul Mackerras37d81822009-03-23 18:22:08 +01001822
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001823 if (vmf->pgoff == 0) {
1824 vmf->page = virt_to_page(data->user_page);
1825 } else {
1826 int nr = vmf->pgoff - 1;
1827
1828 if ((unsigned)nr > data->nr_pages)
1829 goto unlock;
1830
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01001831 if (vmf->flags & FAULT_FLAG_WRITE)
1832 goto unlock;
1833
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001834 vmf->page = virt_to_page(data->data_pages[nr]);
1835 }
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01001836
Paul Mackerras37d81822009-03-23 18:22:08 +01001837 get_page(vmf->page);
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01001838 vmf->page->mapping = vma->vm_file->f_mapping;
1839 vmf->page->index = vmf->pgoff;
1840
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001841 ret = 0;
1842unlock:
1843 rcu_read_unlock();
1844
1845 return ret;
1846}
1847
1848static int perf_mmap_data_alloc(struct perf_counter *counter, int nr_pages)
1849{
1850 struct perf_mmap_data *data;
1851 unsigned long size;
1852 int i;
1853
1854 WARN_ON(atomic_read(&counter->mmap_count));
1855
1856 size = sizeof(struct perf_mmap_data);
1857 size += nr_pages * sizeof(void *);
1858
1859 data = kzalloc(size, GFP_KERNEL);
1860 if (!data)
1861 goto fail;
1862
1863 data->user_page = (void *)get_zeroed_page(GFP_KERNEL);
1864 if (!data->user_page)
1865 goto fail_user_page;
1866
1867 for (i = 0; i < nr_pages; i++) {
1868 data->data_pages[i] = (void *)get_zeroed_page(GFP_KERNEL);
1869 if (!data->data_pages[i])
1870 goto fail_data_pages;
1871 }
1872
1873 data->nr_pages = nr_pages;
Peter Zijlstra22c15582009-05-05 17:50:25 +02001874 atomic_set(&data->lock, -1);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001875
1876 rcu_assign_pointer(counter->data, data);
1877
Paul Mackerras37d81822009-03-23 18:22:08 +01001878 return 0;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001879
1880fail_data_pages:
1881 for (i--; i >= 0; i--)
1882 free_page((unsigned long)data->data_pages[i]);
1883
1884 free_page((unsigned long)data->user_page);
1885
1886fail_user_page:
1887 kfree(data);
1888
1889fail:
1890 return -ENOMEM;
1891}
1892
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01001893static void perf_mmap_free_page(unsigned long addr)
1894{
1895 struct page *page = virt_to_page(addr);
1896
1897 page->mapping = NULL;
1898 __free_page(page);
1899}
1900
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001901static void __perf_mmap_data_free(struct rcu_head *rcu_head)
1902{
Ingo Molnar22a4f652009-06-01 10:13:37 +02001903 struct perf_mmap_data *data;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001904 int i;
1905
Ingo Molnar22a4f652009-06-01 10:13:37 +02001906 data = container_of(rcu_head, struct perf_mmap_data, rcu_head);
1907
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01001908 perf_mmap_free_page((unsigned long)data->user_page);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001909 for (i = 0; i < data->nr_pages; i++)
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01001910 perf_mmap_free_page((unsigned long)data->data_pages[i]);
1911
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001912 kfree(data);
1913}
1914
1915static void perf_mmap_data_free(struct perf_counter *counter)
1916{
1917 struct perf_mmap_data *data = counter->data;
1918
1919 WARN_ON(atomic_read(&counter->mmap_count));
1920
1921 rcu_assign_pointer(counter->data, NULL);
1922 call_rcu(&data->rcu_head, __perf_mmap_data_free);
1923}
1924
1925static void perf_mmap_open(struct vm_area_struct *vma)
1926{
1927 struct perf_counter *counter = vma->vm_file->private_data;
1928
1929 atomic_inc(&counter->mmap_count);
1930}
1931
1932static void perf_mmap_close(struct vm_area_struct *vma)
1933{
1934 struct perf_counter *counter = vma->vm_file->private_data;
1935
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001936 WARN_ON_ONCE(counter->ctx->parent_ctx);
Ingo Molnar22a4f652009-06-01 10:13:37 +02001937 if (atomic_dec_and_mutex_lock(&counter->mmap_count, &counter->mmap_mutex)) {
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001938 struct user_struct *user = current_user();
1939
1940 atomic_long_sub(counter->data->nr_pages + 1, &user->locked_vm);
Peter Zijlstrac5078f72009-05-05 17:50:24 +02001941 vma->vm_mm->locked_vm -= counter->data->nr_locked;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001942 perf_mmap_data_free(counter);
1943 mutex_unlock(&counter->mmap_mutex);
1944 }
Paul Mackerras37d81822009-03-23 18:22:08 +01001945}
1946
1947static struct vm_operations_struct perf_mmap_vmops = {
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01001948 .open = perf_mmap_open,
1949 .close = perf_mmap_close,
1950 .fault = perf_mmap_fault,
1951 .page_mkwrite = perf_mmap_fault,
Paul Mackerras37d81822009-03-23 18:22:08 +01001952};
1953
1954static int perf_mmap(struct file *file, struct vm_area_struct *vma)
1955{
1956 struct perf_counter *counter = file->private_data;
Ingo Molnar22a4f652009-06-01 10:13:37 +02001957 unsigned long user_locked, user_lock_limit;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001958 struct user_struct *user = current_user();
Ingo Molnar22a4f652009-06-01 10:13:37 +02001959 unsigned long locked, lock_limit;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001960 unsigned long vma_size;
1961 unsigned long nr_pages;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001962 long user_extra, extra;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001963 int ret = 0;
Paul Mackerras37d81822009-03-23 18:22:08 +01001964
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01001965 if (!(vma->vm_flags & VM_SHARED))
Paul Mackerras37d81822009-03-23 18:22:08 +01001966 return -EINVAL;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001967
1968 vma_size = vma->vm_end - vma->vm_start;
1969 nr_pages = (vma_size / PAGE_SIZE) - 1;
1970
Peter Zijlstra7730d862009-03-25 12:48:31 +01001971 /*
1972 * If we have data pages ensure they're a power-of-two number, so we
1973 * can do bitmasks instead of modulo.
1974 */
1975 if (nr_pages != 0 && !is_power_of_2(nr_pages))
Paul Mackerras37d81822009-03-23 18:22:08 +01001976 return -EINVAL;
1977
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001978 if (vma_size != PAGE_SIZE * (1 + nr_pages))
Paul Mackerras37d81822009-03-23 18:22:08 +01001979 return -EINVAL;
1980
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001981 if (vma->vm_pgoff != 0)
1982 return -EINVAL;
Paul Mackerras37d81822009-03-23 18:22:08 +01001983
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001984 WARN_ON_ONCE(counter->ctx->parent_ctx);
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001985 mutex_lock(&counter->mmap_mutex);
1986 if (atomic_inc_not_zero(&counter->mmap_count)) {
1987 if (nr_pages != counter->data->nr_pages)
1988 ret = -EINVAL;
1989 goto unlock;
1990 }
1991
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001992 user_extra = nr_pages + 1;
1993 user_lock_limit = sysctl_perf_counter_mlock >> (PAGE_SHIFT - 10);
Ingo Molnara3862d32009-05-24 09:02:37 +02001994
1995 /*
1996 * Increase the limit linearly with more CPUs:
1997 */
1998 user_lock_limit *= num_online_cpus();
1999
Peter Zijlstra789f90f2009-05-15 15:19:27 +02002000 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
Peter Zijlstrac5078f72009-05-05 17:50:24 +02002001
Peter Zijlstra789f90f2009-05-15 15:19:27 +02002002 extra = 0;
2003 if (user_locked > user_lock_limit)
2004 extra = user_locked - user_lock_limit;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002005
2006 lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
2007 lock_limit >>= PAGE_SHIFT;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02002008 locked = vma->vm_mm->locked_vm + extra;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002009
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02002010 if ((locked > lock_limit) && !capable(CAP_IPC_LOCK)) {
2011 ret = -EPERM;
2012 goto unlock;
2013 }
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002014
2015 WARN_ON(counter->data);
2016 ret = perf_mmap_data_alloc(counter, nr_pages);
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02002017 if (ret)
2018 goto unlock;
2019
2020 atomic_set(&counter->mmap_count, 1);
Peter Zijlstra789f90f2009-05-15 15:19:27 +02002021 atomic_long_add(user_extra, &user->locked_vm);
Peter Zijlstrac5078f72009-05-05 17:50:24 +02002022 vma->vm_mm->locked_vm += extra;
2023 counter->data->nr_locked = extra;
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002024 if (vma->vm_flags & VM_WRITE)
2025 counter->data->writable = 1;
2026
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02002027unlock:
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002028 mutex_unlock(&counter->mmap_mutex);
Paul Mackerras37d81822009-03-23 18:22:08 +01002029
Paul Mackerras37d81822009-03-23 18:22:08 +01002030 vma->vm_flags |= VM_RESERVED;
2031 vma->vm_ops = &perf_mmap_vmops;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002032
2033 return ret;
Paul Mackerras37d81822009-03-23 18:22:08 +01002034}
2035
Peter Zijlstra3c446b32009-04-06 11:45:01 +02002036static int perf_fasync(int fd, struct file *filp, int on)
2037{
Peter Zijlstra3c446b32009-04-06 11:45:01 +02002038 struct inode *inode = filp->f_path.dentry->d_inode;
Ingo Molnar22a4f652009-06-01 10:13:37 +02002039 struct perf_counter *counter = filp->private_data;
Peter Zijlstra3c446b32009-04-06 11:45:01 +02002040 int retval;
2041
2042 mutex_lock(&inode->i_mutex);
2043 retval = fasync_helper(fd, filp, on, &counter->fasync);
2044 mutex_unlock(&inode->i_mutex);
2045
2046 if (retval < 0)
2047 return retval;
2048
2049 return 0;
2050}
2051
Thomas Gleixner0793a612008-12-04 20:12:29 +01002052static const struct file_operations perf_fops = {
2053 .release = perf_release,
2054 .read = perf_read,
2055 .poll = perf_poll,
Paul Mackerrasd859e292009-01-17 18:10:22 +11002056 .unlocked_ioctl = perf_ioctl,
2057 .compat_ioctl = perf_ioctl,
Paul Mackerras37d81822009-03-23 18:22:08 +01002058 .mmap = perf_mmap,
Peter Zijlstra3c446b32009-04-06 11:45:01 +02002059 .fasync = perf_fasync,
Thomas Gleixner0793a612008-12-04 20:12:29 +01002060};
2061
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002062/*
Peter Zijlstra925d5192009-03-30 19:07:02 +02002063 * Perf counter wakeup
2064 *
2065 * If there's data, ensure we set the poll() state and publish everything
2066 * to user-space before waking everybody up.
2067 */
2068
2069void perf_counter_wakeup(struct perf_counter *counter)
2070{
Peter Zijlstra925d5192009-03-30 19:07:02 +02002071 wake_up_all(&counter->waitq);
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002072
2073 if (counter->pending_kill) {
2074 kill_fasync(&counter->fasync, SIGIO, counter->pending_kill);
2075 counter->pending_kill = 0;
2076 }
Peter Zijlstra925d5192009-03-30 19:07:02 +02002077}
2078
2079/*
2080 * Pending wakeups
2081 *
2082 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
2083 *
2084 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
2085 * single linked list and use cmpxchg() to add entries lockless.
2086 */
2087
Peter Zijlstra79f14642009-04-06 11:45:07 +02002088static void perf_pending_counter(struct perf_pending_entry *entry)
2089{
2090 struct perf_counter *counter = container_of(entry,
2091 struct perf_counter, pending);
2092
2093 if (counter->pending_disable) {
2094 counter->pending_disable = 0;
2095 perf_counter_disable(counter);
2096 }
2097
2098 if (counter->pending_wakeup) {
2099 counter->pending_wakeup = 0;
2100 perf_counter_wakeup(counter);
2101 }
2102}
2103
Peter Zijlstra671dec52009-04-06 11:45:02 +02002104#define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
Peter Zijlstra925d5192009-03-30 19:07:02 +02002105
Peter Zijlstra671dec52009-04-06 11:45:02 +02002106static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = {
Peter Zijlstra925d5192009-03-30 19:07:02 +02002107 PENDING_TAIL,
2108};
2109
Peter Zijlstra671dec52009-04-06 11:45:02 +02002110static void perf_pending_queue(struct perf_pending_entry *entry,
2111 void (*func)(struct perf_pending_entry *))
Peter Zijlstra925d5192009-03-30 19:07:02 +02002112{
Peter Zijlstra671dec52009-04-06 11:45:02 +02002113 struct perf_pending_entry **head;
Peter Zijlstra925d5192009-03-30 19:07:02 +02002114
Peter Zijlstra671dec52009-04-06 11:45:02 +02002115 if (cmpxchg(&entry->next, NULL, PENDING_TAIL) != NULL)
Peter Zijlstra925d5192009-03-30 19:07:02 +02002116 return;
2117
Peter Zijlstra671dec52009-04-06 11:45:02 +02002118 entry->func = func;
2119
2120 head = &get_cpu_var(perf_pending_head);
Peter Zijlstra925d5192009-03-30 19:07:02 +02002121
2122 do {
Peter Zijlstra671dec52009-04-06 11:45:02 +02002123 entry->next = *head;
2124 } while (cmpxchg(head, entry->next, entry) != entry->next);
Peter Zijlstra925d5192009-03-30 19:07:02 +02002125
2126 set_perf_counter_pending();
2127
Peter Zijlstra671dec52009-04-06 11:45:02 +02002128 put_cpu_var(perf_pending_head);
Peter Zijlstra925d5192009-03-30 19:07:02 +02002129}
2130
2131static int __perf_pending_run(void)
2132{
Peter Zijlstra671dec52009-04-06 11:45:02 +02002133 struct perf_pending_entry *list;
Peter Zijlstra925d5192009-03-30 19:07:02 +02002134 int nr = 0;
2135
Peter Zijlstra671dec52009-04-06 11:45:02 +02002136 list = xchg(&__get_cpu_var(perf_pending_head), PENDING_TAIL);
Peter Zijlstra925d5192009-03-30 19:07:02 +02002137 while (list != PENDING_TAIL) {
Peter Zijlstra671dec52009-04-06 11:45:02 +02002138 void (*func)(struct perf_pending_entry *);
2139 struct perf_pending_entry *entry = list;
Peter Zijlstra925d5192009-03-30 19:07:02 +02002140
2141 list = list->next;
2142
Peter Zijlstra671dec52009-04-06 11:45:02 +02002143 func = entry->func;
2144 entry->next = NULL;
Peter Zijlstra925d5192009-03-30 19:07:02 +02002145 /*
2146 * Ensure we observe the unqueue before we issue the wakeup,
2147 * so that we won't be waiting forever.
2148 * -- see perf_not_pending().
2149 */
2150 smp_wmb();
2151
Peter Zijlstra671dec52009-04-06 11:45:02 +02002152 func(entry);
Peter Zijlstra925d5192009-03-30 19:07:02 +02002153 nr++;
2154 }
2155
2156 return nr;
2157}
2158
2159static inline int perf_not_pending(struct perf_counter *counter)
2160{
2161 /*
2162 * If we flush on whatever cpu we run, there is a chance we don't
2163 * need to wait.
2164 */
2165 get_cpu();
2166 __perf_pending_run();
2167 put_cpu();
2168
2169 /*
2170 * Ensure we see the proper queue state before going to sleep
2171 * so that we do not miss the wakeup. -- see perf_pending_handle()
2172 */
2173 smp_rmb();
Peter Zijlstra671dec52009-04-06 11:45:02 +02002174 return counter->pending.next == NULL;
Peter Zijlstra925d5192009-03-30 19:07:02 +02002175}
2176
2177static void perf_pending_sync(struct perf_counter *counter)
2178{
2179 wait_event(counter->waitq, perf_not_pending(counter));
2180}
2181
2182void perf_counter_do_pending(void)
2183{
2184 __perf_pending_run();
2185}
2186
2187/*
Peter Zijlstra394ee072009-03-30 19:07:14 +02002188 * Callchain support -- arch specific
2189 */
2190
Peter Zijlstra9c03d882009-04-06 11:45:00 +02002191__weak struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
Peter Zijlstra394ee072009-03-30 19:07:14 +02002192{
2193 return NULL;
2194}
2195
2196/*
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002197 * Output
2198 */
2199
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002200struct perf_output_handle {
2201 struct perf_counter *counter;
2202 struct perf_mmap_data *data;
Peter Zijlstra8e3747c2009-06-02 16:16:02 +02002203 unsigned long head;
2204 unsigned long offset;
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002205 int nmi;
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002206 int sample;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002207 int locked;
2208 unsigned long flags;
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002209};
2210
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002211static bool perf_output_space(struct perf_mmap_data *data,
2212 unsigned int offset, unsigned int head)
2213{
2214 unsigned long tail;
2215 unsigned long mask;
2216
2217 if (!data->writable)
2218 return true;
2219
2220 mask = (data->nr_pages << PAGE_SHIFT) - 1;
2221 /*
2222 * Userspace could choose to issue a mb() before updating the tail
2223 * pointer. So that all reads will be completed before the write is
2224 * issued.
2225 */
2226 tail = ACCESS_ONCE(data->user_page->data_tail);
2227 smp_rmb();
2228
2229 offset = (offset - tail) & mask;
2230 head = (head - tail) & mask;
2231
2232 if ((int)(head - offset) < 0)
2233 return false;
2234
2235 return true;
2236}
2237
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002238static void perf_output_wakeup(struct perf_output_handle *handle)
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002239{
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002240 atomic_set(&handle->data->poll, POLL_IN);
2241
Peter Zijlstra671dec52009-04-06 11:45:02 +02002242 if (handle->nmi) {
Peter Zijlstra79f14642009-04-06 11:45:07 +02002243 handle->counter->pending_wakeup = 1;
Peter Zijlstra671dec52009-04-06 11:45:02 +02002244 perf_pending_queue(&handle->counter->pending,
Peter Zijlstra79f14642009-04-06 11:45:07 +02002245 perf_pending_counter);
Peter Zijlstra671dec52009-04-06 11:45:02 +02002246 } else
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002247 perf_counter_wakeup(handle->counter);
2248}
2249
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002250/*
2251 * Curious locking construct.
2252 *
2253 * We need to ensure a later event doesn't publish a head when a former
2254 * event isn't done writing. However since we need to deal with NMIs we
2255 * cannot fully serialize things.
2256 *
2257 * What we do is serialize between CPUs so we only have to deal with NMI
2258 * nesting on a single CPU.
2259 *
2260 * We only publish the head (and generate a wakeup) when the outer-most
2261 * event completes.
2262 */
2263static void perf_output_lock(struct perf_output_handle *handle)
2264{
2265 struct perf_mmap_data *data = handle->data;
2266 int cpu;
2267
2268 handle->locked = 0;
2269
2270 local_irq_save(handle->flags);
2271 cpu = smp_processor_id();
2272
2273 if (in_nmi() && atomic_read(&data->lock) == cpu)
2274 return;
2275
Peter Zijlstra22c15582009-05-05 17:50:25 +02002276 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002277 cpu_relax();
2278
2279 handle->locked = 1;
2280}
2281
2282static void perf_output_unlock(struct perf_output_handle *handle)
2283{
2284 struct perf_mmap_data *data = handle->data;
Peter Zijlstra8e3747c2009-06-02 16:16:02 +02002285 unsigned long head;
2286 int cpu;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002287
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002288 data->done_head = data->head;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002289
2290 if (!handle->locked)
2291 goto out;
2292
2293again:
2294 /*
2295 * The xchg implies a full barrier that ensures all writes are done
2296 * before we publish the new head, matched by a rmb() in userspace when
2297 * reading this position.
2298 */
Peter Zijlstra8e3747c2009-06-02 16:16:02 +02002299 while ((head = atomic_long_xchg(&data->done_head, 0)))
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002300 data->user_page->data_head = head;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002301
2302 /*
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002303 * NMI can happen here, which means we can miss a done_head update.
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002304 */
2305
Peter Zijlstra22c15582009-05-05 17:50:25 +02002306 cpu = atomic_xchg(&data->lock, -1);
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002307 WARN_ON_ONCE(cpu != smp_processor_id());
2308
2309 /*
2310 * Therefore we have to validate we did not indeed do so.
2311 */
Peter Zijlstra8e3747c2009-06-02 16:16:02 +02002312 if (unlikely(atomic_long_read(&data->done_head))) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002313 /*
2314 * Since we had it locked, we can lock it again.
2315 */
Peter Zijlstra22c15582009-05-05 17:50:25 +02002316 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002317 cpu_relax();
2318
2319 goto again;
2320 }
2321
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002322 if (atomic_xchg(&data->wakeup, 0))
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002323 perf_output_wakeup(handle);
2324out:
2325 local_irq_restore(handle->flags);
2326}
2327
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002328static void perf_output_copy(struct perf_output_handle *handle,
Peter Zijlstra089dd792009-06-05 14:04:55 +02002329 const void *buf, unsigned int len)
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002330{
2331 unsigned int pages_mask;
2332 unsigned int offset;
2333 unsigned int size;
2334 void **pages;
2335
2336 offset = handle->offset;
2337 pages_mask = handle->data->nr_pages - 1;
2338 pages = handle->data->data_pages;
2339
2340 do {
2341 unsigned int page_offset;
2342 int nr;
2343
2344 nr = (offset >> PAGE_SHIFT) & pages_mask;
2345 page_offset = offset & (PAGE_SIZE - 1);
2346 size = min_t(unsigned int, PAGE_SIZE - page_offset, len);
2347
2348 memcpy(pages[nr] + page_offset, buf, size);
2349
2350 len -= size;
2351 buf += size;
2352 offset += size;
2353 } while (len);
2354
2355 handle->offset = offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01002356
Peter Zijlstra53020fe2009-05-13 21:26:19 +02002357 /*
2358 * Check we didn't copy past our reservation window, taking the
2359 * possible unsigned int wrap into account.
2360 */
Peter Zijlstra8e3747c2009-06-02 16:16:02 +02002361 WARN_ON_ONCE(((long)(handle->head - handle->offset)) < 0);
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002362}
2363
Peter Zijlstra5c148192009-03-25 12:30:23 +01002364#define perf_output_put(handle, x) \
2365 perf_output_copy((handle), &(x), sizeof(x))
2366
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002367static int perf_output_begin(struct perf_output_handle *handle,
2368 struct perf_counter *counter, unsigned int size,
2369 int nmi, int sample)
2370{
2371 struct perf_mmap_data *data;
2372 unsigned int offset, head;
2373 int have_lost;
2374 struct {
2375 struct perf_event_header header;
2376 u64 id;
2377 u64 lost;
2378 } lost_event;
2379
2380 /*
2381 * For inherited counters we send all the output towards the parent.
2382 */
2383 if (counter->parent)
2384 counter = counter->parent;
2385
2386 rcu_read_lock();
2387 data = rcu_dereference(counter->data);
2388 if (!data)
2389 goto out;
2390
2391 handle->data = data;
2392 handle->counter = counter;
2393 handle->nmi = nmi;
2394 handle->sample = sample;
2395
2396 if (!data->nr_pages)
2397 goto fail;
2398
2399 have_lost = atomic_read(&data->lost);
2400 if (have_lost)
2401 size += sizeof(lost_event);
2402
2403 perf_output_lock(handle);
2404
2405 do {
2406 offset = head = atomic_long_read(&data->head);
2407 head += size;
2408 if (unlikely(!perf_output_space(data, offset, head)))
2409 goto fail;
2410 } while (atomic_long_cmpxchg(&data->head, offset, head) != offset);
2411
2412 handle->offset = offset;
2413 handle->head = head;
2414
2415 if ((offset >> PAGE_SHIFT) != (head >> PAGE_SHIFT))
2416 atomic_set(&data->wakeup, 1);
2417
2418 if (have_lost) {
2419 lost_event.header.type = PERF_EVENT_LOST;
2420 lost_event.header.misc = 0;
2421 lost_event.header.size = sizeof(lost_event);
2422 lost_event.id = counter->id;
2423 lost_event.lost = atomic_xchg(&data->lost, 0);
2424
2425 perf_output_put(handle, lost_event);
2426 }
2427
2428 return 0;
2429
2430fail:
2431 atomic_inc(&data->lost);
2432 perf_output_unlock(handle);
2433out:
2434 rcu_read_unlock();
2435
2436 return -ENOSPC;
2437}
2438
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002439static void perf_output_end(struct perf_output_handle *handle)
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002440{
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002441 struct perf_counter *counter = handle->counter;
2442 struct perf_mmap_data *data = handle->data;
2443
Peter Zijlstra0d486962009-06-02 19:22:16 +02002444 int wakeup_events = counter->attr.wakeup_events;
Peter Zijlstrac4578102009-04-02 11:12:01 +02002445
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002446 if (handle->sample && wakeup_events) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002447 int events = atomic_inc_return(&data->events);
Peter Zijlstrac4578102009-04-02 11:12:01 +02002448 if (events >= wakeup_events) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002449 atomic_sub(wakeup_events, &data->events);
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002450 atomic_set(&data->wakeup, 1);
Peter Zijlstrac4578102009-04-02 11:12:01 +02002451 }
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002452 }
2453
2454 perf_output_unlock(handle);
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002455 rcu_read_unlock();
2456}
2457
Peter Zijlstra709e50c2009-06-02 14:13:15 +02002458static u32 perf_counter_pid(struct perf_counter *counter, struct task_struct *p)
2459{
2460 /*
2461 * only top level counters have the pid namespace they were created in
2462 */
2463 if (counter->parent)
2464 counter = counter->parent;
2465
2466 return task_tgid_nr_ns(p, counter->ns);
2467}
2468
2469static u32 perf_counter_tid(struct perf_counter *counter, struct task_struct *p)
2470{
2471 /*
2472 * only top level counters have the pid namespace they were created in
2473 */
2474 if (counter->parent)
2475 counter = counter->parent;
2476
2477 return task_pid_nr_ns(p, counter->ns);
2478}
2479
Peter Zijlstradf1a1322009-06-10 21:02:22 +02002480static void perf_counter_output(struct perf_counter *counter, int nmi,
2481 struct perf_sample_data *data)
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002482{
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002483 int ret;
Peter Zijlstra0d486962009-06-02 19:22:16 +02002484 u64 sample_type = counter->attr.sample_type;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002485 struct perf_output_handle handle;
2486 struct perf_event_header header;
2487 u64 ip;
Peter Zijlstra5c148192009-03-25 12:30:23 +01002488 struct {
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002489 u32 pid, tid;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002490 } tid_entry;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002491 struct {
Peter Zijlstra8e5799b2009-06-02 15:08:15 +02002492 u64 id;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002493 u64 counter;
2494 } group_entry;
Peter Zijlstra394ee072009-03-30 19:07:14 +02002495 struct perf_callchain_entry *callchain = NULL;
2496 int callchain_size = 0;
Peter Zijlstra339f7c92009-04-06 11:45:06 +02002497 u64 time;
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002498 struct {
2499 u32 cpu, reserved;
2500 } cpu_entry;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002501
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02002502 header.type = 0;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002503 header.size = sizeof(header);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002504
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02002505 header.misc = PERF_EVENT_MISC_OVERFLOW;
Peter Zijlstradf1a1322009-06-10 21:02:22 +02002506 header.misc |= perf_misc_flags(data->regs);
Peter Zijlstra6fab0192009-04-08 15:01:26 +02002507
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002508 if (sample_type & PERF_SAMPLE_IP) {
Peter Zijlstradf1a1322009-06-10 21:02:22 +02002509 ip = perf_instruction_pointer(data->regs);
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002510 header.type |= PERF_SAMPLE_IP;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002511 header.size += sizeof(ip);
2512 }
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002513
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002514 if (sample_type & PERF_SAMPLE_TID) {
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002515 /* namespace issues */
Peter Zijlstra709e50c2009-06-02 14:13:15 +02002516 tid_entry.pid = perf_counter_pid(counter, current);
2517 tid_entry.tid = perf_counter_tid(counter, current);
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002518
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002519 header.type |= PERF_SAMPLE_TID;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002520 header.size += sizeof(tid_entry);
2521 }
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002522
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002523 if (sample_type & PERF_SAMPLE_TIME) {
Peter Zijlstra4d855452009-04-08 15:01:32 +02002524 /*
2525 * Maybe do better on x86 and provide cpu_clock_nmi()
2526 */
2527 time = sched_clock();
2528
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002529 header.type |= PERF_SAMPLE_TIME;
Peter Zijlstra4d855452009-04-08 15:01:32 +02002530 header.size += sizeof(u64);
2531 }
2532
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002533 if (sample_type & PERF_SAMPLE_ADDR) {
2534 header.type |= PERF_SAMPLE_ADDR;
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002535 header.size += sizeof(u64);
2536 }
2537
Peter Zijlstraac4bcf82009-06-05 14:44:52 +02002538 if (sample_type & PERF_SAMPLE_ID) {
2539 header.type |= PERF_SAMPLE_ID;
Peter Zijlstraa85f61a2009-05-08 18:52:23 +02002540 header.size += sizeof(u64);
2541 }
2542
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002543 if (sample_type & PERF_SAMPLE_CPU) {
2544 header.type |= PERF_SAMPLE_CPU;
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002545 header.size += sizeof(cpu_entry);
2546
2547 cpu_entry.cpu = raw_smp_processor_id();
2548 }
2549
Peter Zijlstra689802b2009-06-05 15:05:43 +02002550 if (sample_type & PERF_SAMPLE_PERIOD) {
2551 header.type |= PERF_SAMPLE_PERIOD;
2552 header.size += sizeof(u64);
2553 }
2554
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002555 if (sample_type & PERF_SAMPLE_GROUP) {
2556 header.type |= PERF_SAMPLE_GROUP;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002557 header.size += sizeof(u64) +
2558 counter->nr_siblings * sizeof(group_entry);
2559 }
2560
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002561 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
Peter Zijlstradf1a1322009-06-10 21:02:22 +02002562 callchain = perf_callchain(data->regs);
Peter Zijlstra394ee072009-03-30 19:07:14 +02002563
2564 if (callchain) {
Peter Zijlstra9c03d882009-04-06 11:45:00 +02002565 callchain_size = (1 + callchain->nr) * sizeof(u64);
Peter Zijlstra394ee072009-03-30 19:07:14 +02002566
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002567 header.type |= PERF_SAMPLE_CALLCHAIN;
Peter Zijlstra394ee072009-03-30 19:07:14 +02002568 header.size += callchain_size;
2569 }
2570 }
2571
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002572 ret = perf_output_begin(&handle, counter, header.size, nmi, 1);
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002573 if (ret)
2574 return;
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002575
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002576 perf_output_put(&handle, header);
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002577
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002578 if (sample_type & PERF_SAMPLE_IP)
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002579 perf_output_put(&handle, ip);
2580
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002581 if (sample_type & PERF_SAMPLE_TID)
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002582 perf_output_put(&handle, tid_entry);
2583
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002584 if (sample_type & PERF_SAMPLE_TIME)
Peter Zijlstra4d855452009-04-08 15:01:32 +02002585 perf_output_put(&handle, time);
2586
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002587 if (sample_type & PERF_SAMPLE_ADDR)
Peter Zijlstradf1a1322009-06-10 21:02:22 +02002588 perf_output_put(&handle, data->addr);
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002589
Peter Zijlstraac4bcf82009-06-05 14:44:52 +02002590 if (sample_type & PERF_SAMPLE_ID)
2591 perf_output_put(&handle, counter->id);
Peter Zijlstraa85f61a2009-05-08 18:52:23 +02002592
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002593 if (sample_type & PERF_SAMPLE_CPU)
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002594 perf_output_put(&handle, cpu_entry);
2595
Peter Zijlstra689802b2009-06-05 15:05:43 +02002596 if (sample_type & PERF_SAMPLE_PERIOD)
Peter Zijlstra9e350de2009-06-10 21:34:59 +02002597 perf_output_put(&handle, data->period);
Peter Zijlstra689802b2009-06-05 15:05:43 +02002598
Peter Zijlstra2023b352009-05-05 17:50:26 +02002599 /*
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002600 * XXX PERF_SAMPLE_GROUP vs inherited counters seems difficult.
Peter Zijlstra2023b352009-05-05 17:50:26 +02002601 */
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002602 if (sample_type & PERF_SAMPLE_GROUP) {
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002603 struct perf_counter *leader, *sub;
2604 u64 nr = counter->nr_siblings;
2605
2606 perf_output_put(&handle, nr);
2607
2608 leader = counter->group_leader;
2609 list_for_each_entry(sub, &leader->sibling_list, list_entry) {
2610 if (sub != counter)
Robert Richter4aeb0b42009-04-29 12:47:03 +02002611 sub->pmu->read(sub);
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002612
Peter Zijlstra8e5799b2009-06-02 15:08:15 +02002613 group_entry.id = sub->id;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002614 group_entry.counter = atomic64_read(&sub->count);
2615
2616 perf_output_put(&handle, group_entry);
2617 }
2618 }
2619
Peter Zijlstra394ee072009-03-30 19:07:14 +02002620 if (callchain)
2621 perf_output_copy(&handle, callchain, callchain_size);
2622
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002623 perf_output_end(&handle);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002624}
2625
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002626/*
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002627 * fork tracking
2628 */
2629
2630struct perf_fork_event {
2631 struct task_struct *task;
2632
2633 struct {
2634 struct perf_event_header header;
2635
2636 u32 pid;
2637 u32 ppid;
2638 } event;
2639};
2640
2641static void perf_counter_fork_output(struct perf_counter *counter,
2642 struct perf_fork_event *fork_event)
2643{
2644 struct perf_output_handle handle;
2645 int size = fork_event->event.header.size;
2646 struct task_struct *task = fork_event->task;
2647 int ret = perf_output_begin(&handle, counter, size, 0, 0);
2648
2649 if (ret)
2650 return;
2651
2652 fork_event->event.pid = perf_counter_pid(counter, task);
2653 fork_event->event.ppid = perf_counter_pid(counter, task->real_parent);
2654
2655 perf_output_put(&handle, fork_event->event);
2656 perf_output_end(&handle);
2657}
2658
2659static int perf_counter_fork_match(struct perf_counter *counter)
2660{
Peter Zijlstrad99e9442009-06-04 17:08:58 +02002661 if (counter->attr.comm || counter->attr.mmap)
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002662 return 1;
2663
2664 return 0;
2665}
2666
2667static void perf_counter_fork_ctx(struct perf_counter_context *ctx,
2668 struct perf_fork_event *fork_event)
2669{
2670 struct perf_counter *counter;
2671
2672 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2673 return;
2674
2675 rcu_read_lock();
2676 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2677 if (perf_counter_fork_match(counter))
2678 perf_counter_fork_output(counter, fork_event);
2679 }
2680 rcu_read_unlock();
2681}
2682
2683static void perf_counter_fork_event(struct perf_fork_event *fork_event)
2684{
2685 struct perf_cpu_context *cpuctx;
2686 struct perf_counter_context *ctx;
2687
2688 cpuctx = &get_cpu_var(perf_cpu_context);
2689 perf_counter_fork_ctx(&cpuctx->ctx, fork_event);
2690 put_cpu_var(perf_cpu_context);
2691
2692 rcu_read_lock();
2693 /*
2694 * doesn't really matter which of the child contexts the
2695 * events ends up in.
2696 */
2697 ctx = rcu_dereference(current->perf_counter_ctxp);
2698 if (ctx)
2699 perf_counter_fork_ctx(ctx, fork_event);
2700 rcu_read_unlock();
2701}
2702
2703void perf_counter_fork(struct task_struct *task)
2704{
2705 struct perf_fork_event fork_event;
2706
2707 if (!atomic_read(&nr_comm_counters) &&
Peter Zijlstrad99e9442009-06-04 17:08:58 +02002708 !atomic_read(&nr_mmap_counters))
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002709 return;
2710
2711 fork_event = (struct perf_fork_event){
2712 .task = task,
2713 .event = {
2714 .header = {
2715 .type = PERF_EVENT_FORK,
2716 .size = sizeof(fork_event.event),
2717 },
2718 },
2719 };
2720
2721 perf_counter_fork_event(&fork_event);
2722}
2723
2724/*
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002725 * comm tracking
2726 */
2727
2728struct perf_comm_event {
Ingo Molnar22a4f652009-06-01 10:13:37 +02002729 struct task_struct *task;
2730 char *comm;
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002731 int comm_size;
2732
2733 struct {
2734 struct perf_event_header header;
2735
2736 u32 pid;
2737 u32 tid;
2738 } event;
2739};
2740
2741static void perf_counter_comm_output(struct perf_counter *counter,
2742 struct perf_comm_event *comm_event)
2743{
2744 struct perf_output_handle handle;
2745 int size = comm_event->event.header.size;
2746 int ret = perf_output_begin(&handle, counter, size, 0, 0);
2747
2748 if (ret)
2749 return;
2750
Peter Zijlstra709e50c2009-06-02 14:13:15 +02002751 comm_event->event.pid = perf_counter_pid(counter, comm_event->task);
2752 comm_event->event.tid = perf_counter_tid(counter, comm_event->task);
2753
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002754 perf_output_put(&handle, comm_event->event);
2755 perf_output_copy(&handle, comm_event->comm,
2756 comm_event->comm_size);
2757 perf_output_end(&handle);
2758}
2759
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002760static int perf_counter_comm_match(struct perf_counter *counter)
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002761{
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002762 if (counter->attr.comm)
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002763 return 1;
2764
2765 return 0;
2766}
2767
2768static void perf_counter_comm_ctx(struct perf_counter_context *ctx,
2769 struct perf_comm_event *comm_event)
2770{
2771 struct perf_counter *counter;
2772
2773 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2774 return;
2775
2776 rcu_read_lock();
2777 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002778 if (perf_counter_comm_match(counter))
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002779 perf_counter_comm_output(counter, comm_event);
2780 }
2781 rcu_read_unlock();
2782}
2783
2784static void perf_counter_comm_event(struct perf_comm_event *comm_event)
2785{
2786 struct perf_cpu_context *cpuctx;
Peter Zijlstra665c2142009-05-29 14:51:57 +02002787 struct perf_counter_context *ctx;
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002788 unsigned int size;
2789 char *comm = comm_event->task->comm;
2790
Ingo Molnar888fcee2009-04-09 09:48:22 +02002791 size = ALIGN(strlen(comm)+1, sizeof(u64));
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002792
2793 comm_event->comm = comm;
2794 comm_event->comm_size = size;
2795
2796 comm_event->event.header.size = sizeof(comm_event->event) + size;
2797
2798 cpuctx = &get_cpu_var(perf_cpu_context);
2799 perf_counter_comm_ctx(&cpuctx->ctx, comm_event);
2800 put_cpu_var(perf_cpu_context);
Peter Zijlstra665c2142009-05-29 14:51:57 +02002801
2802 rcu_read_lock();
2803 /*
2804 * doesn't really matter which of the child contexts the
2805 * events ends up in.
2806 */
2807 ctx = rcu_dereference(current->perf_counter_ctxp);
2808 if (ctx)
2809 perf_counter_comm_ctx(ctx, comm_event);
2810 rcu_read_unlock();
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002811}
2812
2813void perf_counter_comm(struct task_struct *task)
2814{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002815 struct perf_comm_event comm_event;
2816
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002817 if (!atomic_read(&nr_comm_counters))
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002818 return;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10002819
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002820 comm_event = (struct perf_comm_event){
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002821 .task = task,
2822 .event = {
2823 .header = { .type = PERF_EVENT_COMM, },
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002824 },
2825 };
2826
2827 perf_counter_comm_event(&comm_event);
2828}
2829
2830/*
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002831 * mmap tracking
2832 */
2833
2834struct perf_mmap_event {
Peter Zijlstra089dd792009-06-05 14:04:55 +02002835 struct vm_area_struct *vma;
2836
2837 const char *file_name;
2838 int file_size;
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002839
2840 struct {
2841 struct perf_event_header header;
2842
2843 u32 pid;
2844 u32 tid;
2845 u64 start;
2846 u64 len;
2847 u64 pgoff;
2848 } event;
2849};
2850
2851static void perf_counter_mmap_output(struct perf_counter *counter,
2852 struct perf_mmap_event *mmap_event)
2853{
2854 struct perf_output_handle handle;
2855 int size = mmap_event->event.header.size;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002856 int ret = perf_output_begin(&handle, counter, size, 0, 0);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002857
2858 if (ret)
2859 return;
2860
Peter Zijlstra709e50c2009-06-02 14:13:15 +02002861 mmap_event->event.pid = perf_counter_pid(counter, current);
2862 mmap_event->event.tid = perf_counter_tid(counter, current);
2863
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002864 perf_output_put(&handle, mmap_event->event);
2865 perf_output_copy(&handle, mmap_event->file_name,
2866 mmap_event->file_size);
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002867 perf_output_end(&handle);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002868}
2869
2870static int perf_counter_mmap_match(struct perf_counter *counter,
2871 struct perf_mmap_event *mmap_event)
2872{
Peter Zijlstrad99e9442009-06-04 17:08:58 +02002873 if (counter->attr.mmap)
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002874 return 1;
2875
2876 return 0;
2877}
2878
2879static void perf_counter_mmap_ctx(struct perf_counter_context *ctx,
2880 struct perf_mmap_event *mmap_event)
2881{
2882 struct perf_counter *counter;
2883
2884 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2885 return;
2886
2887 rcu_read_lock();
2888 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2889 if (perf_counter_mmap_match(counter, mmap_event))
2890 perf_counter_mmap_output(counter, mmap_event);
2891 }
2892 rcu_read_unlock();
2893}
2894
2895static void perf_counter_mmap_event(struct perf_mmap_event *mmap_event)
2896{
2897 struct perf_cpu_context *cpuctx;
Peter Zijlstra665c2142009-05-29 14:51:57 +02002898 struct perf_counter_context *ctx;
Peter Zijlstra089dd792009-06-05 14:04:55 +02002899 struct vm_area_struct *vma = mmap_event->vma;
2900 struct file *file = vma->vm_file;
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002901 unsigned int size;
2902 char tmp[16];
2903 char *buf = NULL;
Peter Zijlstra089dd792009-06-05 14:04:55 +02002904 const char *name;
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002905
2906 if (file) {
2907 buf = kzalloc(PATH_MAX, GFP_KERNEL);
2908 if (!buf) {
2909 name = strncpy(tmp, "//enomem", sizeof(tmp));
2910 goto got_name;
2911 }
Peter Zijlstrad3d21c42009-04-09 10:53:46 +02002912 name = d_path(&file->f_path, buf, PATH_MAX);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002913 if (IS_ERR(name)) {
2914 name = strncpy(tmp, "//toolong", sizeof(tmp));
2915 goto got_name;
2916 }
2917 } else {
Peter Zijlstra089dd792009-06-05 14:04:55 +02002918 name = arch_vma_name(mmap_event->vma);
2919 if (name)
2920 goto got_name;
2921
2922 if (!vma->vm_mm) {
2923 name = strncpy(tmp, "[vdso]", sizeof(tmp));
2924 goto got_name;
2925 }
2926
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002927 name = strncpy(tmp, "//anon", sizeof(tmp));
2928 goto got_name;
2929 }
2930
2931got_name:
Ingo Molnar888fcee2009-04-09 09:48:22 +02002932 size = ALIGN(strlen(name)+1, sizeof(u64));
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002933
2934 mmap_event->file_name = name;
2935 mmap_event->file_size = size;
2936
2937 mmap_event->event.header.size = sizeof(mmap_event->event) + size;
2938
2939 cpuctx = &get_cpu_var(perf_cpu_context);
2940 perf_counter_mmap_ctx(&cpuctx->ctx, mmap_event);
2941 put_cpu_var(perf_cpu_context);
2942
Peter Zijlstra665c2142009-05-29 14:51:57 +02002943 rcu_read_lock();
2944 /*
2945 * doesn't really matter which of the child contexts the
2946 * events ends up in.
2947 */
2948 ctx = rcu_dereference(current->perf_counter_ctxp);
2949 if (ctx)
2950 perf_counter_mmap_ctx(ctx, mmap_event);
2951 rcu_read_unlock();
2952
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002953 kfree(buf);
2954}
2955
Peter Zijlstra089dd792009-06-05 14:04:55 +02002956void __perf_counter_mmap(struct vm_area_struct *vma)
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002957{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002958 struct perf_mmap_event mmap_event;
2959
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002960 if (!atomic_read(&nr_mmap_counters))
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002961 return;
2962
2963 mmap_event = (struct perf_mmap_event){
Peter Zijlstra089dd792009-06-05 14:04:55 +02002964 .vma = vma,
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002965 .event = {
2966 .header = { .type = PERF_EVENT_MMAP, },
Peter Zijlstra089dd792009-06-05 14:04:55 +02002967 .start = vma->vm_start,
2968 .len = vma->vm_end - vma->vm_start,
2969 .pgoff = vma->vm_pgoff,
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002970 },
2971 };
2972
2973 perf_counter_mmap_event(&mmap_event);
2974}
2975
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002976/*
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002977 * Log sample_period changes so that analyzing tools can re-normalize the
Peter Zijlstrae220d2d2009-05-23 18:28:55 +02002978 * event flow.
Peter Zijlstra26b119b2009-05-20 12:21:20 +02002979 */
2980
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02002981struct freq_event {
2982 struct perf_event_header header;
2983 u64 time;
2984 u64 id;
2985 u64 period;
2986};
2987
Peter Zijlstra26b119b2009-05-20 12:21:20 +02002988static void perf_log_period(struct perf_counter *counter, u64 period)
2989{
2990 struct perf_output_handle handle;
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02002991 struct freq_event event;
Peter Zijlstra26b119b2009-05-20 12:21:20 +02002992 int ret;
2993
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02002994 if (counter->hw.sample_period == period)
2995 return;
2996
2997 if (counter->attr.sample_type & PERF_SAMPLE_PERIOD)
2998 return;
2999
3000 event = (struct freq_event) {
Peter Zijlstra26b119b2009-05-20 12:21:20 +02003001 .header = {
3002 .type = PERF_EVENT_PERIOD,
3003 .misc = 0,
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02003004 .size = sizeof(event),
Peter Zijlstra26b119b2009-05-20 12:21:20 +02003005 },
3006 .time = sched_clock(),
Peter Zijlstra689802b2009-06-05 15:05:43 +02003007 .id = counter->id,
Peter Zijlstra26b119b2009-05-20 12:21:20 +02003008 .period = period,
3009 };
3010
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02003011 ret = perf_output_begin(&handle, counter, sizeof(event), 1, 0);
Peter Zijlstra26b119b2009-05-20 12:21:20 +02003012 if (ret)
3013 return;
3014
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02003015 perf_output_put(&handle, event);
Peter Zijlstra26b119b2009-05-20 12:21:20 +02003016 perf_output_end(&handle);
3017}
3018
3019/*
Peter Zijlstraa78ac322009-05-25 17:39:05 +02003020 * IRQ throttle logging
3021 */
3022
3023static void perf_log_throttle(struct perf_counter *counter, int enable)
3024{
3025 struct perf_output_handle handle;
3026 int ret;
3027
3028 struct {
3029 struct perf_event_header header;
3030 u64 time;
Peter Zijlstracca3f452009-06-11 14:57:55 +02003031 u64 id;
Peter Zijlstraa78ac322009-05-25 17:39:05 +02003032 } throttle_event = {
3033 .header = {
3034 .type = PERF_EVENT_THROTTLE + 1,
3035 .misc = 0,
3036 .size = sizeof(throttle_event),
3037 },
Peter Zijlstracca3f452009-06-11 14:57:55 +02003038 .time = sched_clock(),
3039 .id = counter->id,
Peter Zijlstraa78ac322009-05-25 17:39:05 +02003040 };
3041
Ingo Molnar0127c3e2009-05-25 22:03:26 +02003042 ret = perf_output_begin(&handle, counter, sizeof(throttle_event), 1, 0);
Peter Zijlstraa78ac322009-05-25 17:39:05 +02003043 if (ret)
3044 return;
3045
3046 perf_output_put(&handle, throttle_event);
3047 perf_output_end(&handle);
3048}
3049
3050/*
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01003051 * Generic counter overflow handling, sampling.
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02003052 */
3053
Peter Zijlstradf1a1322009-06-10 21:02:22 +02003054int perf_counter_overflow(struct perf_counter *counter, int nmi,
3055 struct perf_sample_data *data)
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02003056{
Peter Zijlstra79f14642009-04-06 11:45:07 +02003057 int events = atomic_read(&counter->event_limit);
Peter Zijlstraa78ac322009-05-25 17:39:05 +02003058 int throttle = counter->pmu->unthrottle != NULL;
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02003059 struct hw_perf_counter *hwc = &counter->hw;
Peter Zijlstra79f14642009-04-06 11:45:07 +02003060 int ret = 0;
3061
Peter Zijlstraa78ac322009-05-25 17:39:05 +02003062 if (!throttle) {
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02003063 hwc->interrupts++;
Ingo Molnar128f0482009-06-03 22:19:36 +02003064 } else {
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02003065 if (hwc->interrupts != MAX_INTERRUPTS) {
3066 hwc->interrupts++;
Peter Zijlstradf58ab22009-06-11 11:25:05 +02003067 if (HZ * hwc->interrupts >
3068 (u64)sysctl_perf_counter_sample_rate) {
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02003069 hwc->interrupts = MAX_INTERRUPTS;
Ingo Molnar128f0482009-06-03 22:19:36 +02003070 perf_log_throttle(counter, 0);
3071 ret = 1;
3072 }
3073 } else {
3074 /*
3075 * Keep re-disabling counters even though on the previous
3076 * pass we disabled it - just in case we raced with a
3077 * sched-in and the counter got enabled again:
3078 */
Peter Zijlstraa78ac322009-05-25 17:39:05 +02003079 ret = 1;
3080 }
3081 }
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003082
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02003083 if (counter->attr.freq) {
3084 u64 now = sched_clock();
3085 s64 delta = now - hwc->freq_stamp;
3086
3087 hwc->freq_stamp = now;
3088
3089 if (delta > 0 && delta < TICK_NSEC)
3090 perf_adjust_period(counter, NSEC_PER_SEC / (int)delta);
3091 }
3092
Peter Zijlstra2023b352009-05-05 17:50:26 +02003093 /*
3094 * XXX event_limit might not quite work as expected on inherited
3095 * counters
3096 */
3097
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02003098 counter->pending_kill = POLL_IN;
Peter Zijlstra79f14642009-04-06 11:45:07 +02003099 if (events && atomic_dec_and_test(&counter->event_limit)) {
3100 ret = 1;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02003101 counter->pending_kill = POLL_HUP;
Peter Zijlstra79f14642009-04-06 11:45:07 +02003102 if (nmi) {
3103 counter->pending_disable = 1;
3104 perf_pending_queue(&counter->pending,
3105 perf_pending_counter);
3106 } else
3107 perf_counter_disable(counter);
3108 }
3109
Peter Zijlstradf1a1322009-06-10 21:02:22 +02003110 perf_counter_output(counter, nmi, data);
Peter Zijlstra79f14642009-04-06 11:45:07 +02003111 return ret;
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02003112}
3113
3114/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003115 * Generic software counter infrastructure
3116 */
3117
3118static void perf_swcounter_update(struct perf_counter *counter)
3119{
3120 struct hw_perf_counter *hwc = &counter->hw;
3121 u64 prev, now;
3122 s64 delta;
3123
3124again:
3125 prev = atomic64_read(&hwc->prev_count);
3126 now = atomic64_read(&hwc->count);
3127 if (atomic64_cmpxchg(&hwc->prev_count, prev, now) != prev)
3128 goto again;
3129
3130 delta = now - prev;
3131
3132 atomic64_add(delta, &counter->count);
3133 atomic64_sub(delta, &hwc->period_left);
3134}
3135
3136static void perf_swcounter_set_period(struct perf_counter *counter)
3137{
3138 struct hw_perf_counter *hwc = &counter->hw;
3139 s64 left = atomic64_read(&hwc->period_left);
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003140 s64 period = hwc->sample_period;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003141
3142 if (unlikely(left <= -period)) {
3143 left = period;
3144 atomic64_set(&hwc->period_left, left);
Peter Zijlstra9e350de2009-06-10 21:34:59 +02003145 hwc->last_period = period;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003146 }
3147
3148 if (unlikely(left <= 0)) {
3149 left += period;
3150 atomic64_add(period, &hwc->period_left);
Peter Zijlstra9e350de2009-06-10 21:34:59 +02003151 hwc->last_period = period;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003152 }
3153
3154 atomic64_set(&hwc->prev_count, -left);
3155 atomic64_set(&hwc->count, -left);
3156}
3157
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003158static enum hrtimer_restart perf_swcounter_hrtimer(struct hrtimer *hrtimer)
3159{
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02003160 enum hrtimer_restart ret = HRTIMER_RESTART;
Peter Zijlstradf1a1322009-06-10 21:02:22 +02003161 struct perf_sample_data data;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003162 struct perf_counter *counter;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003163 u64 period;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003164
3165 counter = container_of(hrtimer, struct perf_counter, hw.hrtimer);
Robert Richter4aeb0b42009-04-29 12:47:03 +02003166 counter->pmu->read(counter);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003167
Peter Zijlstradf1a1322009-06-10 21:02:22 +02003168 data.addr = 0;
3169 data.regs = get_irq_regs();
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003170 /*
3171 * In case we exclude kernel IPs or are somehow not in interrupt
3172 * context, provide the next best thing, the user IP.
3173 */
Peter Zijlstradf1a1322009-06-10 21:02:22 +02003174 if ((counter->attr.exclude_kernel || !data.regs) &&
Peter Zijlstra0d486962009-06-02 19:22:16 +02003175 !counter->attr.exclude_user)
Peter Zijlstradf1a1322009-06-10 21:02:22 +02003176 data.regs = task_pt_regs(current);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003177
Peter Zijlstradf1a1322009-06-10 21:02:22 +02003178 if (data.regs) {
3179 if (perf_counter_overflow(counter, 0, &data))
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02003180 ret = HRTIMER_NORESTART;
3181 }
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003182
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003183 period = max_t(u64, 10000, counter->hw.sample_period);
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003184 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003185
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02003186 return ret;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003187}
3188
3189static void perf_swcounter_overflow(struct perf_counter *counter,
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003190 int nmi, struct perf_sample_data *data)
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003191{
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003192 data->period = counter->hw.last_period;
Peter Zijlstradf1a1322009-06-10 21:02:22 +02003193
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003194 perf_swcounter_update(counter);
3195 perf_swcounter_set_period(counter);
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003196 if (perf_counter_overflow(counter, nmi, data))
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02003197 /* soft-disable the counter */
3198 ;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003199}
3200
Paul Mackerras880ca152009-06-01 17:49:14 +10003201static int perf_swcounter_is_counting(struct perf_counter *counter)
3202{
3203 struct perf_counter_context *ctx;
3204 unsigned long flags;
3205 int count;
3206
3207 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
3208 return 1;
3209
3210 if (counter->state != PERF_COUNTER_STATE_INACTIVE)
3211 return 0;
3212
3213 /*
3214 * If the counter is inactive, it could be just because
3215 * its task is scheduled out, or because it's in a group
3216 * which could not go on the PMU. We want to count in
3217 * the first case but not the second. If the context is
3218 * currently active then an inactive software counter must
3219 * be the second case. If it's not currently active then
3220 * we need to know whether the counter was active when the
3221 * context was last active, which we can determine by
3222 * comparing counter->tstamp_stopped with ctx->time.
3223 *
3224 * We are within an RCU read-side critical section,
3225 * which protects the existence of *ctx.
3226 */
3227 ctx = counter->ctx;
3228 spin_lock_irqsave(&ctx->lock, flags);
3229 count = 1;
3230 /* Re-check state now we have the lock */
3231 if (counter->state < PERF_COUNTER_STATE_INACTIVE ||
3232 counter->ctx->is_active ||
3233 counter->tstamp_stopped < ctx->time)
3234 count = 0;
3235 spin_unlock_irqrestore(&ctx->lock, flags);
3236 return count;
3237}
3238
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003239static int perf_swcounter_match(struct perf_counter *counter,
Peter Zijlstra1c432d82009-06-11 13:19:29 +02003240 enum perf_type_id type,
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003241 u32 event, struct pt_regs *regs)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003242{
Paul Mackerras880ca152009-06-01 17:49:14 +10003243 if (!perf_swcounter_is_counting(counter))
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003244 return 0;
3245
Ingo Molnara21ca2c2009-06-06 09:58:57 +02003246 if (counter->attr.type != type)
3247 return 0;
3248 if (counter->attr.config != event)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003249 return 0;
3250
Paul Mackerras3f731ca2009-06-01 17:52:30 +10003251 if (regs) {
Peter Zijlstra0d486962009-06-02 19:22:16 +02003252 if (counter->attr.exclude_user && user_mode(regs))
Paul Mackerras3f731ca2009-06-01 17:52:30 +10003253 return 0;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003254
Peter Zijlstra0d486962009-06-02 19:22:16 +02003255 if (counter->attr.exclude_kernel && !user_mode(regs))
Paul Mackerras3f731ca2009-06-01 17:52:30 +10003256 return 0;
3257 }
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003258
3259 return 1;
3260}
3261
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003262static void perf_swcounter_add(struct perf_counter *counter, u64 nr,
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003263 int nmi, struct perf_sample_data *data)
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003264{
3265 int neg = atomic64_add_negative(nr, &counter->hw.count);
Ingo Molnar22a4f652009-06-01 10:13:37 +02003266
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003267 if (counter->hw.sample_period && !neg && data->regs)
3268 perf_swcounter_overflow(counter, nmi, data);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003269}
3270
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003271static void perf_swcounter_ctx_event(struct perf_counter_context *ctx,
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003272 enum perf_type_id type,
3273 u32 event, u64 nr, int nmi,
3274 struct perf_sample_data *data)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003275{
3276 struct perf_counter *counter;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003277
Peter Zijlstra01ef09d2009-03-19 20:26:11 +01003278 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003279 return;
3280
Peter Zijlstra592903c2009-03-13 12:21:36 +01003281 rcu_read_lock();
3282 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003283 if (perf_swcounter_match(counter, type, event, data->regs))
3284 perf_swcounter_add(counter, nr, nmi, data);
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003285 }
Peter Zijlstra592903c2009-03-13 12:21:36 +01003286 rcu_read_unlock();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003287}
3288
Peter Zijlstra96f6d442009-03-23 18:22:07 +01003289static int *perf_swcounter_recursion_context(struct perf_cpu_context *cpuctx)
3290{
3291 if (in_nmi())
3292 return &cpuctx->recursion[3];
3293
3294 if (in_irq())
3295 return &cpuctx->recursion[2];
3296
3297 if (in_softirq())
3298 return &cpuctx->recursion[1];
3299
3300 return &cpuctx->recursion[0];
3301}
3302
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003303static void do_perf_swcounter_event(enum perf_type_id type, u32 event,
3304 u64 nr, int nmi,
3305 struct perf_sample_data *data)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003306{
3307 struct perf_cpu_context *cpuctx = &get_cpu_var(perf_cpu_context);
Peter Zijlstra96f6d442009-03-23 18:22:07 +01003308 int *recursion = perf_swcounter_recursion_context(cpuctx);
Peter Zijlstra665c2142009-05-29 14:51:57 +02003309 struct perf_counter_context *ctx;
Peter Zijlstra96f6d442009-03-23 18:22:07 +01003310
3311 if (*recursion)
3312 goto out;
3313
3314 (*recursion)++;
3315 barrier();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003316
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003317 perf_swcounter_ctx_event(&cpuctx->ctx, type, event,
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003318 nr, nmi, data);
Peter Zijlstra665c2142009-05-29 14:51:57 +02003319 rcu_read_lock();
3320 /*
3321 * doesn't really matter which of the child contexts the
3322 * events ends up in.
3323 */
3324 ctx = rcu_dereference(current->perf_counter_ctxp);
3325 if (ctx)
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003326 perf_swcounter_ctx_event(ctx, type, event, nr, nmi, data);
Peter Zijlstra665c2142009-05-29 14:51:57 +02003327 rcu_read_unlock();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003328
Peter Zijlstra96f6d442009-03-23 18:22:07 +01003329 barrier();
3330 (*recursion)--;
3331
3332out:
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003333 put_cpu_var(perf_cpu_context);
3334}
3335
Peter Zijlstraf29ac752009-06-19 18:27:26 +02003336void __perf_swcounter_event(u32 event, u64 nr, int nmi,
3337 struct pt_regs *regs, u64 addr)
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003338{
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003339 struct perf_sample_data data = {
3340 .regs = regs,
3341 .addr = addr,
3342 };
3343
3344 do_perf_swcounter_event(PERF_TYPE_SOFTWARE, event, nr, nmi, &data);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003345}
3346
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003347static void perf_swcounter_read(struct perf_counter *counter)
3348{
3349 perf_swcounter_update(counter);
3350}
3351
3352static int perf_swcounter_enable(struct perf_counter *counter)
3353{
3354 perf_swcounter_set_period(counter);
3355 return 0;
3356}
3357
3358static void perf_swcounter_disable(struct perf_counter *counter)
3359{
3360 perf_swcounter_update(counter);
3361}
3362
Robert Richter4aeb0b42009-04-29 12:47:03 +02003363static const struct pmu perf_ops_generic = {
Peter Zijlstraac17dc82009-03-13 12:21:34 +01003364 .enable = perf_swcounter_enable,
3365 .disable = perf_swcounter_disable,
3366 .read = perf_swcounter_read,
3367};
3368
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003369/*
3370 * Software counter: cpu wall time clock
3371 */
3372
Paul Mackerras9abf8a02009-01-09 16:26:43 +11003373static void cpu_clock_perf_counter_update(struct perf_counter *counter)
3374{
3375 int cpu = raw_smp_processor_id();
3376 s64 prev;
3377 u64 now;
3378
3379 now = cpu_clock(cpu);
3380 prev = atomic64_read(&counter->hw.prev_count);
3381 atomic64_set(&counter->hw.prev_count, now);
3382 atomic64_add(now - prev, &counter->count);
3383}
3384
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003385static int cpu_clock_perf_counter_enable(struct perf_counter *counter)
3386{
3387 struct hw_perf_counter *hwc = &counter->hw;
3388 int cpu = raw_smp_processor_id();
3389
3390 atomic64_set(&hwc->prev_count, cpu_clock(cpu));
Peter Zijlstra039fc912009-03-13 16:43:47 +01003391 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
3392 hwc->hrtimer.function = perf_swcounter_hrtimer;
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003393 if (hwc->sample_period) {
3394 u64 period = max_t(u64, 10000, hwc->sample_period);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003395 __hrtimer_start_range_ns(&hwc->hrtimer,
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003396 ns_to_ktime(period), 0,
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003397 HRTIMER_MODE_REL, 0);
3398 }
3399
3400 return 0;
3401}
3402
Ingo Molnar5c92d122008-12-11 13:21:10 +01003403static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
3404{
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003405 if (counter->hw.sample_period)
Peter Zijlstrab986d7e2009-05-20 12:21:21 +02003406 hrtimer_cancel(&counter->hw.hrtimer);
Paul Mackerras9abf8a02009-01-09 16:26:43 +11003407 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01003408}
3409
3410static void cpu_clock_perf_counter_read(struct perf_counter *counter)
3411{
Paul Mackerras9abf8a02009-01-09 16:26:43 +11003412 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01003413}
3414
Robert Richter4aeb0b42009-04-29 12:47:03 +02003415static const struct pmu perf_ops_cpu_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +01003416 .enable = cpu_clock_perf_counter_enable,
3417 .disable = cpu_clock_perf_counter_disable,
3418 .read = cpu_clock_perf_counter_read,
Ingo Molnar5c92d122008-12-11 13:21:10 +01003419};
3420
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01003421/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003422 * Software counter: task time clock
3423 */
3424
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02003425static void task_clock_perf_counter_update(struct perf_counter *counter, u64 now)
Ingo Molnarbae43c92008-12-11 14:03:20 +01003426{
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02003427 u64 prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003428 s64 delta;
Ingo Molnarbae43c92008-12-11 14:03:20 +01003429
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02003430 prev = atomic64_xchg(&counter->hw.prev_count, now);
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003431 delta = now - prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003432 atomic64_add(delta, &counter->count);
Ingo Molnarbae43c92008-12-11 14:03:20 +01003433}
3434
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01003435static int task_clock_perf_counter_enable(struct perf_counter *counter)
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003436{
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003437 struct hw_perf_counter *hwc = &counter->hw;
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02003438 u64 now;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003439
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02003440 now = counter->ctx->time;
3441
3442 atomic64_set(&hwc->prev_count, now);
Peter Zijlstra039fc912009-03-13 16:43:47 +01003443 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
3444 hwc->hrtimer.function = perf_swcounter_hrtimer;
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003445 if (hwc->sample_period) {
3446 u64 period = max_t(u64, 10000, hwc->sample_period);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003447 __hrtimer_start_range_ns(&hwc->hrtimer,
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003448 ns_to_ktime(period), 0,
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003449 HRTIMER_MODE_REL, 0);
3450 }
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01003451
3452 return 0;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003453}
3454
3455static void task_clock_perf_counter_disable(struct perf_counter *counter)
3456{
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003457 if (counter->hw.sample_period)
Peter Zijlstrab986d7e2009-05-20 12:21:21 +02003458 hrtimer_cancel(&counter->hw.hrtimer);
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02003459 task_clock_perf_counter_update(counter, counter->ctx->time);
3460
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003461}
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01003462
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003463static void task_clock_perf_counter_read(struct perf_counter *counter)
3464{
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02003465 u64 time;
3466
3467 if (!in_nmi()) {
3468 update_context_time(counter->ctx);
3469 time = counter->ctx->time;
3470 } else {
3471 u64 now = perf_clock();
3472 u64 delta = now - counter->ctx->timestamp;
3473 time = counter->ctx->time + delta;
3474 }
3475
3476 task_clock_perf_counter_update(counter, time);
Ingo Molnarbae43c92008-12-11 14:03:20 +01003477}
3478
Robert Richter4aeb0b42009-04-29 12:47:03 +02003479static const struct pmu perf_ops_task_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +01003480 .enable = task_clock_perf_counter_enable,
3481 .disable = task_clock_perf_counter_disable,
3482 .read = task_clock_perf_counter_read,
Ingo Molnarbae43c92008-12-11 14:03:20 +01003483};
3484
Peter Zijlstrae077df42009-03-19 20:26:17 +01003485#ifdef CONFIG_EVENT_PROFILE
3486void perf_tpcounter_event(int event_id)
3487{
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003488 struct perf_sample_data data = {
3489 .regs = get_irq_regs();
3490 .addr = 0,
3491 };
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003492
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003493 if (!data.regs)
3494 data.regs = task_pt_regs(current);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003495
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003496 do_perf_swcounter_event(PERF_TYPE_TRACEPOINT, event_id, 1, 1, &data);
Peter Zijlstrae077df42009-03-19 20:26:17 +01003497}
Steven Whitehouseff7b1b42009-04-15 16:55:05 +01003498EXPORT_SYMBOL_GPL(perf_tpcounter_event);
Peter Zijlstrae077df42009-03-19 20:26:17 +01003499
3500extern int ftrace_profile_enable(int);
3501extern void ftrace_profile_disable(int);
3502
3503static void tp_perf_counter_destroy(struct perf_counter *counter)
3504{
Peter Zijlstra0d486962009-06-02 19:22:16 +02003505 ftrace_profile_disable(perf_event_id(&counter->attr));
Peter Zijlstrae077df42009-03-19 20:26:17 +01003506}
3507
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{
Peter Zijlstra0d486962009-06-02 19:22:16 +02003510 int event_id = perf_event_id(&counter->attr);
Peter Zijlstrae077df42009-03-19 20:26:17 +01003511 int ret;
3512
3513 ret = ftrace_profile_enable(event_id);
3514 if (ret)
3515 return NULL;
3516
3517 counter->destroy = tp_perf_counter_destroy;
3518
3519 return &perf_ops_generic;
3520}
3521#else
Robert Richter4aeb0b42009-04-29 12:47:03 +02003522static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
Peter Zijlstrae077df42009-03-19 20:26:17 +01003523{
3524 return NULL;
3525}
3526#endif
3527
Peter Zijlstraf29ac752009-06-19 18:27:26 +02003528atomic_t perf_swcounter_enabled[PERF_COUNT_SW_MAX];
3529
3530static void sw_perf_counter_destroy(struct perf_counter *counter)
3531{
3532 u64 event = counter->attr.config;
3533
Peter Zijlstraf3440112009-06-22 13:58:35 +02003534 WARN_ON(counter->parent);
3535
Peter Zijlstraf29ac752009-06-19 18:27:26 +02003536 atomic_dec(&perf_swcounter_enabled[event]);
3537}
3538
Robert Richter4aeb0b42009-04-29 12:47:03 +02003539static const struct pmu *sw_perf_counter_init(struct perf_counter *counter)
Ingo Molnar5c92d122008-12-11 13:21:10 +01003540{
Robert Richter4aeb0b42009-04-29 12:47:03 +02003541 const struct pmu *pmu = NULL;
Peter Zijlstraf29ac752009-06-19 18:27:26 +02003542 u64 event = counter->attr.config;
Ingo Molnar5c92d122008-12-11 13:21:10 +01003543
Paul Mackerras0475f9e2009-02-11 14:35:35 +11003544 /*
3545 * Software counters (currently) can't in general distinguish
3546 * between user, kernel and hypervisor events.
3547 * However, context switches and cpu migrations are considered
3548 * to be kernel events, and page faults are never hypervisor
3549 * events.
3550 */
Peter Zijlstraf29ac752009-06-19 18:27:26 +02003551 switch (event) {
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +02003552 case PERF_COUNT_SW_CPU_CLOCK:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003553 pmu = &perf_ops_cpu_clock;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003554
Ingo Molnar5c92d122008-12-11 13:21:10 +01003555 break;
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +02003556 case PERF_COUNT_SW_TASK_CLOCK:
Paul Mackerras23a185c2009-02-09 22:42:47 +11003557 /*
3558 * If the user instantiates this as a per-cpu counter,
3559 * use the cpu_clock counter instead.
3560 */
3561 if (counter->ctx->task)
Robert Richter4aeb0b42009-04-29 12:47:03 +02003562 pmu = &perf_ops_task_clock;
Paul Mackerras23a185c2009-02-09 22:42:47 +11003563 else
Robert Richter4aeb0b42009-04-29 12:47:03 +02003564 pmu = &perf_ops_cpu_clock;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003565
Ingo Molnarbae43c92008-12-11 14:03:20 +01003566 break;
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +02003567 case PERF_COUNT_SW_PAGE_FAULTS:
3568 case PERF_COUNT_SW_PAGE_FAULTS_MIN:
3569 case PERF_COUNT_SW_PAGE_FAULTS_MAJ:
3570 case PERF_COUNT_SW_CONTEXT_SWITCHES:
3571 case PERF_COUNT_SW_CPU_MIGRATIONS:
Peter Zijlstraf3440112009-06-22 13:58:35 +02003572 if (!counter->parent) {
3573 atomic_inc(&perf_swcounter_enabled[event]);
3574 counter->destroy = sw_perf_counter_destroy;
3575 }
Paul Mackerras3f731ca2009-06-01 17:52:30 +10003576 pmu = &perf_ops_generic;
Ingo Molnar6c594c22008-12-14 12:34:15 +01003577 break;
Ingo Molnar5c92d122008-12-11 13:21:10 +01003578 }
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003579
Robert Richter4aeb0b42009-04-29 12:47:03 +02003580 return pmu;
Ingo Molnar5c92d122008-12-11 13:21:10 +01003581}
3582
Thomas Gleixner0793a612008-12-04 20:12:29 +01003583/*
3584 * Allocate and initialize a counter structure
3585 */
3586static struct perf_counter *
Peter Zijlstra0d486962009-06-02 19:22:16 +02003587perf_counter_alloc(struct perf_counter_attr *attr,
Ingo Molnar04289bb2008-12-11 08:38:42 +01003588 int cpu,
Paul Mackerras23a185c2009-02-09 22:42:47 +11003589 struct perf_counter_context *ctx,
Ingo Molnar9b51f662008-12-12 13:49:45 +01003590 struct perf_counter *group_leader,
Peter Zijlstrab84fbc92009-06-22 13:57:40 +02003591 struct perf_counter *parent_counter,
Ingo Molnar9b51f662008-12-12 13:49:45 +01003592 gfp_t gfpflags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003593{
Robert Richter4aeb0b42009-04-29 12:47:03 +02003594 const struct pmu *pmu;
Ingo Molnar621a01e2008-12-11 12:46:46 +01003595 struct perf_counter *counter;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003596 struct hw_perf_counter *hwc;
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003597 long err;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003598
Ingo Molnar9b51f662008-12-12 13:49:45 +01003599 counter = kzalloc(sizeof(*counter), gfpflags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003600 if (!counter)
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003601 return ERR_PTR(-ENOMEM);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003602
Ingo Molnar04289bb2008-12-11 08:38:42 +01003603 /*
3604 * Single counters are their own group leaders, with an
3605 * empty sibling list:
3606 */
3607 if (!group_leader)
3608 group_leader = counter;
3609
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003610 mutex_init(&counter->child_mutex);
3611 INIT_LIST_HEAD(&counter->child_list);
3612
Ingo Molnar04289bb2008-12-11 08:38:42 +01003613 INIT_LIST_HEAD(&counter->list_entry);
Peter Zijlstra592903c2009-03-13 12:21:36 +01003614 INIT_LIST_HEAD(&counter->event_entry);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003615 INIT_LIST_HEAD(&counter->sibling_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003616 init_waitqueue_head(&counter->waitq);
3617
Peter Zijlstra7b732a72009-03-23 18:22:10 +01003618 mutex_init(&counter->mmap_mutex);
3619
Peter Zijlstraa96bbc12009-06-03 14:01:36 +02003620 counter->cpu = cpu;
Peter Zijlstra0d486962009-06-02 19:22:16 +02003621 counter->attr = *attr;
Peter Zijlstraa96bbc12009-06-03 14:01:36 +02003622 counter->group_leader = group_leader;
3623 counter->pmu = NULL;
3624 counter->ctx = ctx;
3625 counter->oncpu = -1;
Ingo Molnar329d8762009-05-26 08:10:00 +02003626
Peter Zijlstrab84fbc92009-06-22 13:57:40 +02003627 counter->parent = parent_counter;
3628
Peter Zijlstraa96bbc12009-06-03 14:01:36 +02003629 counter->ns = get_pid_ns(current->nsproxy->pid_ns);
3630 counter->id = atomic64_inc_return(&perf_counter_id);
3631
3632 counter->state = PERF_COUNTER_STATE_INACTIVE;
3633
Peter Zijlstra0d486962009-06-02 19:22:16 +02003634 if (attr->disabled)
Ingo Molnara86ed502008-12-17 00:43:10 +01003635 counter->state = PERF_COUNTER_STATE_OFF;
3636
Robert Richter4aeb0b42009-04-29 12:47:03 +02003637 pmu = NULL;
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003638
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003639 hwc = &counter->hw;
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02003640 hwc->sample_period = attr->sample_period;
Peter Zijlstra0d486962009-06-02 19:22:16 +02003641 if (attr->freq && attr->sample_freq)
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02003642 hwc->sample_period = 1;
3643
3644 atomic64_set(&hwc->period_left, hwc->sample_period);
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003645
Peter Zijlstra2023b352009-05-05 17:50:26 +02003646 /*
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003647 * we currently do not support PERF_SAMPLE_GROUP on inherited counters
Peter Zijlstra2023b352009-05-05 17:50:26 +02003648 */
Peter Zijlstra0d486962009-06-02 19:22:16 +02003649 if (attr->inherit && (attr->sample_type & PERF_SAMPLE_GROUP))
Peter Zijlstra2023b352009-05-05 17:50:26 +02003650 goto done;
3651
Ingo Molnara21ca2c2009-06-06 09:58:57 +02003652 switch (attr->type) {
Peter Zijlstra081fad82009-06-11 17:57:21 +02003653 case PERF_TYPE_RAW:
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003654 case PERF_TYPE_HARDWARE:
Ingo Molnar8326f442009-06-05 20:22:46 +02003655 case PERF_TYPE_HW_CACHE:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003656 pmu = hw_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003657 break;
3658
3659 case PERF_TYPE_SOFTWARE:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003660 pmu = sw_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003661 break;
3662
3663 case PERF_TYPE_TRACEPOINT:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003664 pmu = tp_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003665 break;
Peter Zijlstra974802e2009-06-12 12:46:55 +02003666
3667 default:
3668 break;
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003669 }
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01003670done:
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003671 err = 0;
Robert Richter4aeb0b42009-04-29 12:47:03 +02003672 if (!pmu)
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003673 err = -EINVAL;
Robert Richter4aeb0b42009-04-29 12:47:03 +02003674 else if (IS_ERR(pmu))
3675 err = PTR_ERR(pmu);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003676
3677 if (err) {
Peter Zijlstraa96bbc12009-06-03 14:01:36 +02003678 if (counter->ns)
3679 put_pid_ns(counter->ns);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003680 kfree(counter);
3681 return ERR_PTR(err);
3682 }
3683
Robert Richter4aeb0b42009-04-29 12:47:03 +02003684 counter->pmu = pmu;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003685
Peter Zijlstraf3440112009-06-22 13:58:35 +02003686 if (!counter->parent) {
3687 atomic_inc(&nr_counters);
3688 if (counter->attr.mmap)
3689 atomic_inc(&nr_mmap_counters);
3690 if (counter->attr.comm)
3691 atomic_inc(&nr_comm_counters);
3692 }
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02003693
Thomas Gleixner0793a612008-12-04 20:12:29 +01003694 return counter;
3695}
3696
Peter Zijlstra974802e2009-06-12 12:46:55 +02003697static int perf_copy_attr(struct perf_counter_attr __user *uattr,
3698 struct perf_counter_attr *attr)
3699{
3700 int ret;
3701 u32 size;
3702
3703 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
3704 return -EFAULT;
3705
3706 /*
3707 * zero the full structure, so that a short copy will be nice.
3708 */
3709 memset(attr, 0, sizeof(*attr));
3710
3711 ret = get_user(size, &uattr->size);
3712 if (ret)
3713 return ret;
3714
3715 if (size > PAGE_SIZE) /* silly large */
3716 goto err_size;
3717
3718 if (!size) /* abi compat */
3719 size = PERF_ATTR_SIZE_VER0;
3720
3721 if (size < PERF_ATTR_SIZE_VER0)
3722 goto err_size;
3723
3724 /*
3725 * If we're handed a bigger struct than we know of,
3726 * ensure all the unknown bits are 0.
3727 */
3728 if (size > sizeof(*attr)) {
3729 unsigned long val;
3730 unsigned long __user *addr;
3731 unsigned long __user *end;
3732
3733 addr = PTR_ALIGN((void __user *)uattr + sizeof(*attr),
3734 sizeof(unsigned long));
3735 end = PTR_ALIGN((void __user *)uattr + size,
3736 sizeof(unsigned long));
3737
3738 for (; addr < end; addr += sizeof(unsigned long)) {
3739 ret = get_user(val, addr);
3740 if (ret)
3741 return ret;
3742 if (val)
3743 goto err_size;
3744 }
3745 }
3746
3747 ret = copy_from_user(attr, uattr, size);
3748 if (ret)
3749 return -EFAULT;
3750
3751 /*
3752 * If the type exists, the corresponding creation will verify
3753 * the attr->config.
3754 */
3755 if (attr->type >= PERF_TYPE_MAX)
3756 return -EINVAL;
3757
3758 if (attr->__reserved_1 || attr->__reserved_2 || attr->__reserved_3)
3759 return -EINVAL;
3760
3761 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
3762 return -EINVAL;
3763
3764 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
3765 return -EINVAL;
3766
3767out:
3768 return ret;
3769
3770err_size:
3771 put_user(sizeof(*attr), &uattr->size);
3772 ret = -E2BIG;
3773 goto out;
3774}
3775
Thomas Gleixner0793a612008-12-04 20:12:29 +01003776/**
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003777 * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
Ingo Molnar9f66a382008-12-10 12:33:23 +01003778 *
Peter Zijlstra0d486962009-06-02 19:22:16 +02003779 * @attr_uptr: event type attributes for monitoring/sampling
Thomas Gleixner0793a612008-12-04 20:12:29 +01003780 * @pid: target pid
Ingo Molnar9f66a382008-12-10 12:33:23 +01003781 * @cpu: target cpu
3782 * @group_fd: group leader counter fd
Thomas Gleixner0793a612008-12-04 20:12:29 +01003783 */
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003784SYSCALL_DEFINE5(perf_counter_open,
Peter Zijlstra974802e2009-06-12 12:46:55 +02003785 struct perf_counter_attr __user *, attr_uptr,
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003786 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003787{
Ingo Molnar04289bb2008-12-11 08:38:42 +01003788 struct perf_counter *counter, *group_leader;
Peter Zijlstra0d486962009-06-02 19:22:16 +02003789 struct perf_counter_attr attr;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003790 struct perf_counter_context *ctx;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003791 struct file *counter_file = NULL;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003792 struct file *group_file = NULL;
3793 int fput_needed = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003794 int fput_needed2 = 0;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003795 int ret;
3796
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003797 /* for future expandability... */
3798 if (flags)
3799 return -EINVAL;
3800
Peter Zijlstra974802e2009-06-12 12:46:55 +02003801 ret = perf_copy_attr(attr_uptr, &attr);
3802 if (ret)
3803 return ret;
Thomas Gleixnereab656a2008-12-08 19:26:59 +01003804
Peter Zijlstra07647712009-06-11 11:18:36 +02003805 if (!attr.exclude_kernel) {
3806 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
3807 return -EACCES;
3808 }
3809
Peter Zijlstradf58ab22009-06-11 11:25:05 +02003810 if (attr.freq) {
3811 if (attr.sample_freq > sysctl_perf_counter_sample_rate)
3812 return -EINVAL;
3813 }
3814
Ingo Molnar04289bb2008-12-11 08:38:42 +01003815 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01003816 * Get the target context (task or percpu):
3817 */
3818 ctx = find_get_context(pid, cpu);
3819 if (IS_ERR(ctx))
3820 return PTR_ERR(ctx);
3821
3822 /*
3823 * Look up the group leader (we will attach this counter to it):
Ingo Molnar04289bb2008-12-11 08:38:42 +01003824 */
3825 group_leader = NULL;
3826 if (group_fd != -1) {
3827 ret = -EINVAL;
3828 group_file = fget_light(group_fd, &fput_needed);
3829 if (!group_file)
Ingo Molnarccff2862008-12-11 11:26:29 +01003830 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003831 if (group_file->f_op != &perf_fops)
Ingo Molnarccff2862008-12-11 11:26:29 +01003832 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003833
3834 group_leader = group_file->private_data;
3835 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01003836 * Do not allow a recursive hierarchy (this new sibling
3837 * becoming part of another group-sibling):
Ingo Molnar04289bb2008-12-11 08:38:42 +01003838 */
Ingo Molnarccff2862008-12-11 11:26:29 +01003839 if (group_leader->group_leader != group_leader)
3840 goto err_put_context;
3841 /*
3842 * Do not allow to attach to a group in a different
3843 * task or CPU context:
3844 */
3845 if (group_leader->ctx != ctx)
3846 goto err_put_context;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11003847 /*
3848 * Only a group leader can be exclusive or pinned
3849 */
Peter Zijlstra0d486962009-06-02 19:22:16 +02003850 if (attr.exclusive || attr.pinned)
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11003851 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003852 }
3853
Peter Zijlstra0d486962009-06-02 19:22:16 +02003854 counter = perf_counter_alloc(&attr, cpu, ctx, group_leader,
Peter Zijlstrab84fbc92009-06-22 13:57:40 +02003855 NULL, GFP_KERNEL);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003856 ret = PTR_ERR(counter);
3857 if (IS_ERR(counter))
Thomas Gleixner0793a612008-12-04 20:12:29 +01003858 goto err_put_context;
3859
Thomas Gleixner0793a612008-12-04 20:12:29 +01003860 ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
3861 if (ret < 0)
Ingo Molnar9b51f662008-12-12 13:49:45 +01003862 goto err_free_put_context;
3863
3864 counter_file = fget_light(ret, &fput_needed2);
3865 if (!counter_file)
3866 goto err_free_put_context;
3867
3868 counter->filp = counter_file;
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003869 WARN_ON_ONCE(ctx->parent_ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003870 mutex_lock(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003871 perf_install_in_context(ctx, counter, cpu);
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003872 ++ctx->generation;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003873 mutex_unlock(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003874
Peter Zijlstra082ff5a2009-05-23 18:29:00 +02003875 counter->owner = current;
3876 get_task_struct(current);
3877 mutex_lock(&current->perf_counter_mutex);
3878 list_add_tail(&counter->owner_entry, &current->perf_counter_list);
3879 mutex_unlock(&current->perf_counter_mutex);
3880
Ingo Molnar9b51f662008-12-12 13:49:45 +01003881 fput_light(counter_file, fput_needed2);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003882
Ingo Molnar04289bb2008-12-11 08:38:42 +01003883out_fput:
3884 fput_light(group_file, fput_needed);
3885
Thomas Gleixner0793a612008-12-04 20:12:29 +01003886 return ret;
3887
Ingo Molnar9b51f662008-12-12 13:49:45 +01003888err_free_put_context:
Thomas Gleixner0793a612008-12-04 20:12:29 +01003889 kfree(counter);
3890
3891err_put_context:
Paul Mackerrasc93f7662009-05-28 22:18:17 +10003892 put_ctx(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003893
Ingo Molnar04289bb2008-12-11 08:38:42 +01003894 goto out_fput;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003895}
3896
Ingo Molnar9b51f662008-12-12 13:49:45 +01003897/*
Ingo Molnar9b51f662008-12-12 13:49:45 +01003898 * inherit a counter from parent task to child task:
3899 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11003900static struct perf_counter *
Ingo Molnar9b51f662008-12-12 13:49:45 +01003901inherit_counter(struct perf_counter *parent_counter,
3902 struct task_struct *parent,
3903 struct perf_counter_context *parent_ctx,
3904 struct task_struct *child,
Paul Mackerrasd859e292009-01-17 18:10:22 +11003905 struct perf_counter *group_leader,
Ingo Molnar9b51f662008-12-12 13:49:45 +01003906 struct perf_counter_context *child_ctx)
3907{
3908 struct perf_counter *child_counter;
3909
Paul Mackerrasd859e292009-01-17 18:10:22 +11003910 /*
3911 * Instead of creating recursive hierarchies of counters,
3912 * we link inherited counters back to the original parent,
3913 * which has a filp for sure, which we use as the reference
3914 * count:
3915 */
3916 if (parent_counter->parent)
3917 parent_counter = parent_counter->parent;
3918
Peter Zijlstra0d486962009-06-02 19:22:16 +02003919 child_counter = perf_counter_alloc(&parent_counter->attr,
Paul Mackerras23a185c2009-02-09 22:42:47 +11003920 parent_counter->cpu, child_ctx,
Peter Zijlstrab84fbc92009-06-22 13:57:40 +02003921 group_leader, parent_counter,
3922 GFP_KERNEL);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003923 if (IS_ERR(child_counter))
3924 return child_counter;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10003925 get_ctx(child_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003926
3927 /*
Paul Mackerras564c2b22009-05-22 14:27:22 +10003928 * Make the child state follow the state of the parent counter,
Peter Zijlstra0d486962009-06-02 19:22:16 +02003929 * not its attr.disabled bit. We hold the parent's mutex,
Ingo Molnar22a4f652009-06-01 10:13:37 +02003930 * so we won't race with perf_counter_{en, dis}able_family.
Paul Mackerras564c2b22009-05-22 14:27:22 +10003931 */
3932 if (parent_counter->state >= PERF_COUNTER_STATE_INACTIVE)
3933 child_counter->state = PERF_COUNTER_STATE_INACTIVE;
3934 else
3935 child_counter->state = PERF_COUNTER_STATE_OFF;
3936
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02003937 if (parent_counter->attr.freq)
3938 child_counter->hw.sample_period = parent_counter->hw.sample_period;
3939
Paul Mackerras564c2b22009-05-22 14:27:22 +10003940 /*
Ingo Molnar9b51f662008-12-12 13:49:45 +01003941 * Link it up in the child's context:
3942 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +11003943 add_counter_to_ctx(child_counter, child_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003944
Ingo Molnar9b51f662008-12-12 13:49:45 +01003945 /*
3946 * Get a reference to the parent filp - we will fput it
3947 * when the child counter exits. This is safe to do because
3948 * we are in the parent and we know that the filp still
3949 * exists and has a nonzero count:
3950 */
3951 atomic_long_inc(&parent_counter->filp->f_count);
3952
Paul Mackerrasd859e292009-01-17 18:10:22 +11003953 /*
3954 * Link this into the parent counter's child list
3955 */
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003956 WARN_ON_ONCE(parent_counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003957 mutex_lock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003958 list_add_tail(&child_counter->child_list, &parent_counter->child_list);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003959 mutex_unlock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003960
3961 return child_counter;
3962}
3963
3964static int inherit_group(struct perf_counter *parent_counter,
3965 struct task_struct *parent,
3966 struct perf_counter_context *parent_ctx,
3967 struct task_struct *child,
3968 struct perf_counter_context *child_ctx)
3969{
3970 struct perf_counter *leader;
3971 struct perf_counter *sub;
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003972 struct perf_counter *child_ctr;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003973
3974 leader = inherit_counter(parent_counter, parent, parent_ctx,
3975 child, NULL, child_ctx);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003976 if (IS_ERR(leader))
3977 return PTR_ERR(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003978 list_for_each_entry(sub, &parent_counter->sibling_list, list_entry) {
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003979 child_ctr = inherit_counter(sub, parent, parent_ctx,
3980 child, leader, child_ctx);
3981 if (IS_ERR(child_ctr))
3982 return PTR_ERR(child_ctr);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003983 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01003984 return 0;
3985}
3986
Paul Mackerrasd859e292009-01-17 18:10:22 +11003987static void sync_child_counter(struct perf_counter *child_counter,
3988 struct perf_counter *parent_counter)
3989{
Peter Zijlstra8bc20952009-05-15 20:45:59 +02003990 u64 child_val;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003991
Paul Mackerrasd859e292009-01-17 18:10:22 +11003992 child_val = atomic64_read(&child_counter->count);
3993
3994 /*
3995 * Add back the child's count to the parent's count:
3996 */
3997 atomic64_add(child_val, &parent_counter->count);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11003998 atomic64_add(child_counter->total_time_enabled,
3999 &parent_counter->child_total_time_enabled);
4000 atomic64_add(child_counter->total_time_running,
4001 &parent_counter->child_total_time_running);
Paul Mackerrasd859e292009-01-17 18:10:22 +11004002
4003 /*
4004 * Remove this counter from the parent's list
4005 */
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004006 WARN_ON_ONCE(parent_counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02004007 mutex_lock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11004008 list_del_init(&child_counter->child_list);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02004009 mutex_unlock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11004010
4011 /*
4012 * Release the parent counter, if this was the last
4013 * reference to it.
4014 */
4015 fput(parent_counter->filp);
4016}
4017
Ingo Molnar9b51f662008-12-12 13:49:45 +01004018static void
Peter Zijlstrabbbee902009-05-29 14:25:58 +02004019__perf_counter_exit_task(struct perf_counter *child_counter,
Ingo Molnar9b51f662008-12-12 13:49:45 +01004020 struct perf_counter_context *child_ctx)
4021{
4022 struct perf_counter *parent_counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004023
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004024 update_counter_times(child_counter);
Peter Zijlstraaa9c67f2009-05-23 18:28:59 +02004025 perf_counter_remove_from_context(child_counter);
Ingo Molnar0cc0c022008-12-14 23:20:36 +01004026
Ingo Molnar9b51f662008-12-12 13:49:45 +01004027 parent_counter = child_counter->parent;
4028 /*
4029 * It can happen that parent exits first, and has counters
4030 * that are still around due to the child reference. These
4031 * counters need to be zapped - but otherwise linger.
4032 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11004033 if (parent_counter) {
4034 sync_child_counter(child_counter, parent_counter);
Peter Zijlstraf1600952009-03-19 20:26:16 +01004035 free_counter(child_counter);
Paul Mackerras4bcf3492009-02-11 13:53:19 +01004036 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01004037}
4038
4039/*
Paul Mackerrasd859e292009-01-17 18:10:22 +11004040 * When a child task exits, feed back counter values to parent counters.
Ingo Molnar9b51f662008-12-12 13:49:45 +01004041 */
4042void perf_counter_exit_task(struct task_struct *child)
4043{
4044 struct perf_counter *child_counter, *tmp;
4045 struct perf_counter_context *child_ctx;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004046 unsigned long flags;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004047
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004048 if (likely(!child->perf_counter_ctxp))
Ingo Molnar9b51f662008-12-12 13:49:45 +01004049 return;
4050
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004051 local_irq_save(flags);
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004052 /*
4053 * We can't reschedule here because interrupts are disabled,
4054 * and either child is current or it is a task that can't be
4055 * scheduled, so we are now safe from rescheduling changing
4056 * our context.
4057 */
4058 child_ctx = child->perf_counter_ctxp;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004059 __perf_counter_task_sched_out(child_ctx);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10004060
4061 /*
4062 * Take the context lock here so that if find_get_context is
4063 * reading child->perf_counter_ctxp, we wait until it has
4064 * incremented the context's refcount before we do put_ctx below.
4065 */
4066 spin_lock(&child_ctx->lock);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004067 child->perf_counter_ctxp = NULL;
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004068 if (child_ctx->parent_ctx) {
4069 /*
4070 * This context is a clone; unclone it so it can't get
4071 * swapped to another process while we're removing all
4072 * the counters from it.
4073 */
4074 put_ctx(child_ctx->parent_ctx);
4075 child_ctx->parent_ctx = NULL;
4076 }
Paul Mackerrasc93f7662009-05-28 22:18:17 +10004077 spin_unlock(&child_ctx->lock);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004078 local_irq_restore(flags);
4079
Peter Zijlstra66fff222009-06-10 22:53:37 +02004080 /*
4081 * We can recurse on the same lock type through:
4082 *
4083 * __perf_counter_exit_task()
4084 * sync_child_counter()
4085 * fput(parent_counter->filp)
4086 * perf_release()
4087 * mutex_lock(&ctx->mutex)
4088 *
4089 * But since its the parent context it won't be the same instance.
4090 */
4091 mutex_lock_nested(&child_ctx->mutex, SINGLE_DEPTH_NESTING);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004092
Peter Zijlstra8bc20952009-05-15 20:45:59 +02004093again:
Ingo Molnar9b51f662008-12-12 13:49:45 +01004094 list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list,
4095 list_entry)
Peter Zijlstrabbbee902009-05-29 14:25:58 +02004096 __perf_counter_exit_task(child_counter, child_ctx);
Peter Zijlstra8bc20952009-05-15 20:45:59 +02004097
4098 /*
4099 * If the last counter was a group counter, it will have appended all
4100 * its siblings to the list, but we obtained 'tmp' before that which
4101 * will still point to the list head terminating the iteration.
4102 */
4103 if (!list_empty(&child_ctx->counter_list))
4104 goto again;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004105
4106 mutex_unlock(&child_ctx->mutex);
4107
4108 put_ctx(child_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01004109}
4110
4111/*
Peter Zijlstrabbbee902009-05-29 14:25:58 +02004112 * free an unexposed, unused context as created by inheritance by
4113 * init_task below, used by fork() in case of fail.
4114 */
4115void perf_counter_free_task(struct task_struct *task)
4116{
4117 struct perf_counter_context *ctx = task->perf_counter_ctxp;
4118 struct perf_counter *counter, *tmp;
4119
4120 if (!ctx)
4121 return;
4122
4123 mutex_lock(&ctx->mutex);
4124again:
4125 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry) {
4126 struct perf_counter *parent = counter->parent;
4127
4128 if (WARN_ON_ONCE(!parent))
4129 continue;
4130
4131 mutex_lock(&parent->child_mutex);
4132 list_del_init(&counter->child_list);
4133 mutex_unlock(&parent->child_mutex);
4134
4135 fput(parent->filp);
4136
4137 list_del_counter(counter, ctx);
4138 free_counter(counter);
4139 }
4140
4141 if (!list_empty(&ctx->counter_list))
4142 goto again;
4143
4144 mutex_unlock(&ctx->mutex);
4145
4146 put_ctx(ctx);
4147}
4148
4149/*
Ingo Molnar9b51f662008-12-12 13:49:45 +01004150 * Initialize the perf_counter context in task_struct
4151 */
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02004152int perf_counter_init_task(struct task_struct *child)
Ingo Molnar9b51f662008-12-12 13:49:45 +01004153{
4154 struct perf_counter_context *child_ctx, *parent_ctx;
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004155 struct perf_counter_context *cloned_ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +11004156 struct perf_counter *counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004157 struct task_struct *parent = current;
Paul Mackerras564c2b22009-05-22 14:27:22 +10004158 int inherited_all = 1;
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02004159 int ret = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004160
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004161 child->perf_counter_ctxp = NULL;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004162
Peter Zijlstra082ff5a2009-05-23 18:29:00 +02004163 mutex_init(&child->perf_counter_mutex);
4164 INIT_LIST_HEAD(&child->perf_counter_list);
4165
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004166 if (likely(!parent->perf_counter_ctxp))
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02004167 return 0;
4168
Ingo Molnar9b51f662008-12-12 13:49:45 +01004169 /*
4170 * This is executed from the parent task context, so inherit
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004171 * counters that have been marked for cloning.
4172 * First allocate and initialize a context for the child.
Ingo Molnar9b51f662008-12-12 13:49:45 +01004173 */
4174
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004175 child_ctx = kmalloc(sizeof(struct perf_counter_context), GFP_KERNEL);
4176 if (!child_ctx)
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02004177 return -ENOMEM;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004178
4179 __perf_counter_init_context(child_ctx, child);
4180 child->perf_counter_ctxp = child_ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10004181 get_task_struct(child);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004182
Ingo Molnar9b51f662008-12-12 13:49:45 +01004183 /*
Paul Mackerras25346b92009-06-01 17:48:12 +10004184 * If the parent's context is a clone, pin it so it won't get
4185 * swapped under us.
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004186 */
Paul Mackerras25346b92009-06-01 17:48:12 +10004187 parent_ctx = perf_pin_task_context(parent);
4188
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004189 /*
4190 * No need to check if parent_ctx != NULL here; since we saw
4191 * it non-NULL earlier, the only reason for it to become NULL
4192 * is if we exit, and since we're currently in the middle of
4193 * a fork we can't be exiting at the same time.
4194 */
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004195
4196 /*
Ingo Molnar9b51f662008-12-12 13:49:45 +01004197 * Lock the parent list. No need to lock the child - not PID
4198 * hashed yet and not running, so nobody can access it.
4199 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11004200 mutex_lock(&parent_ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01004201
4202 /*
4203 * We dont have to disable NMIs - we are only looking at
4204 * the list, not manipulating it:
4205 */
Peter Zijlstrad7b629a2009-05-20 12:21:19 +02004206 list_for_each_entry_rcu(counter, &parent_ctx->event_list, event_entry) {
4207 if (counter != counter->group_leader)
4208 continue;
4209
Peter Zijlstra0d486962009-06-02 19:22:16 +02004210 if (!counter->attr.inherit) {
Paul Mackerras564c2b22009-05-22 14:27:22 +10004211 inherited_all = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004212 continue;
Paul Mackerras564c2b22009-05-22 14:27:22 +10004213 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01004214
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02004215 ret = inherit_group(counter, parent, parent_ctx,
4216 child, child_ctx);
4217 if (ret) {
Paul Mackerras564c2b22009-05-22 14:27:22 +10004218 inherited_all = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004219 break;
Paul Mackerras564c2b22009-05-22 14:27:22 +10004220 }
4221 }
4222
4223 if (inherited_all) {
4224 /*
4225 * Mark the child context as a clone of the parent
4226 * context, or of whatever the parent is a clone of.
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004227 * Note that if the parent is a clone, it could get
4228 * uncloned at any point, but that doesn't matter
4229 * because the list of counters and the generation
4230 * count can't have changed since we took the mutex.
Paul Mackerras564c2b22009-05-22 14:27:22 +10004231 */
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004232 cloned_ctx = rcu_dereference(parent_ctx->parent_ctx);
4233 if (cloned_ctx) {
4234 child_ctx->parent_ctx = cloned_ctx;
Paul Mackerras25346b92009-06-01 17:48:12 +10004235 child_ctx->parent_gen = parent_ctx->parent_gen;
Paul Mackerras564c2b22009-05-22 14:27:22 +10004236 } else {
4237 child_ctx->parent_ctx = parent_ctx;
4238 child_ctx->parent_gen = parent_ctx->generation;
4239 }
4240 get_ctx(child_ctx->parent_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01004241 }
4242
Paul Mackerrasd859e292009-01-17 18:10:22 +11004243 mutex_unlock(&parent_ctx->mutex);
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02004244
Paul Mackerras25346b92009-06-01 17:48:12 +10004245 perf_unpin_context(parent_ctx);
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004246
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02004247 return ret;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004248}
4249
Ingo Molnar04289bb2008-12-11 08:38:42 +01004250static void __cpuinit perf_counter_init_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01004251{
Ingo Molnar04289bb2008-12-11 08:38:42 +01004252 struct perf_cpu_context *cpuctx;
Thomas Gleixner0793a612008-12-04 20:12:29 +01004253
Ingo Molnar04289bb2008-12-11 08:38:42 +01004254 cpuctx = &per_cpu(perf_cpu_context, cpu);
4255 __perf_counter_init_context(&cpuctx->ctx, NULL);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004256
Ingo Molnar1dce8d92009-05-04 19:23:18 +02004257 spin_lock(&perf_resource_lock);
Ingo Molnar04289bb2008-12-11 08:38:42 +01004258 cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu;
Ingo Molnar1dce8d92009-05-04 19:23:18 +02004259 spin_unlock(&perf_resource_lock);
Ingo Molnar04289bb2008-12-11 08:38:42 +01004260
Paul Mackerras01d02872009-01-14 13:44:19 +11004261 hw_perf_counter_setup(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004262}
4263
4264#ifdef CONFIG_HOTPLUG_CPU
Ingo Molnar04289bb2008-12-11 08:38:42 +01004265static void __perf_counter_exit_cpu(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01004266{
4267 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
4268 struct perf_counter_context *ctx = &cpuctx->ctx;
4269 struct perf_counter *counter, *tmp;
4270
Ingo Molnar04289bb2008-12-11 08:38:42 +01004271 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry)
4272 __perf_counter_remove_from_context(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004273}
Ingo Molnar04289bb2008-12-11 08:38:42 +01004274static void perf_counter_exit_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01004275{
Paul Mackerrasd859e292009-01-17 18:10:22 +11004276 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
4277 struct perf_counter_context *ctx = &cpuctx->ctx;
4278
4279 mutex_lock(&ctx->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01004280 smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1);
Paul Mackerrasd859e292009-01-17 18:10:22 +11004281 mutex_unlock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004282}
4283#else
Ingo Molnar04289bb2008-12-11 08:38:42 +01004284static inline void perf_counter_exit_cpu(int cpu) { }
Thomas Gleixner0793a612008-12-04 20:12:29 +01004285#endif
4286
4287static int __cpuinit
4288perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
4289{
4290 unsigned int cpu = (long)hcpu;
4291
4292 switch (action) {
4293
4294 case CPU_UP_PREPARE:
4295 case CPU_UP_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01004296 perf_counter_init_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004297 break;
4298
4299 case CPU_DOWN_PREPARE:
4300 case CPU_DOWN_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01004301 perf_counter_exit_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004302 break;
4303
4304 default:
4305 break;
4306 }
4307
4308 return NOTIFY_OK;
4309}
4310
Paul Mackerrasf38b0822009-06-02 21:05:16 +10004311/*
4312 * This has to have a higher priority than migration_notifier in sched.c.
4313 */
Thomas Gleixner0793a612008-12-04 20:12:29 +01004314static struct notifier_block __cpuinitdata perf_cpu_nb = {
4315 .notifier_call = perf_cpu_notify,
Paul Mackerrasf38b0822009-06-02 21:05:16 +10004316 .priority = 20,
Thomas Gleixner0793a612008-12-04 20:12:29 +01004317};
4318
Ingo Molnar0d905bc2009-05-04 19:13:30 +02004319void __init perf_counter_init(void)
Thomas Gleixner0793a612008-12-04 20:12:29 +01004320{
4321 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
4322 (void *)(long)smp_processor_id());
4323 register_cpu_notifier(&perf_cpu_nb);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004324}
Thomas Gleixner0793a612008-12-04 20:12:29 +01004325
4326static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
4327{
4328 return sprintf(buf, "%d\n", perf_reserved_percpu);
4329}
4330
4331static ssize_t
4332perf_set_reserve_percpu(struct sysdev_class *class,
4333 const char *buf,
4334 size_t count)
4335{
4336 struct perf_cpu_context *cpuctx;
4337 unsigned long val;
4338 int err, cpu, mpt;
4339
4340 err = strict_strtoul(buf, 10, &val);
4341 if (err)
4342 return err;
4343 if (val > perf_max_counters)
4344 return -EINVAL;
4345
Ingo Molnar1dce8d92009-05-04 19:23:18 +02004346 spin_lock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004347 perf_reserved_percpu = val;
4348 for_each_online_cpu(cpu) {
4349 cpuctx = &per_cpu(perf_cpu_context, cpu);
4350 spin_lock_irq(&cpuctx->ctx.lock);
4351 mpt = min(perf_max_counters - cpuctx->ctx.nr_counters,
4352 perf_max_counters - perf_reserved_percpu);
4353 cpuctx->max_pertask = mpt;
4354 spin_unlock_irq(&cpuctx->ctx.lock);
4355 }
Ingo Molnar1dce8d92009-05-04 19:23:18 +02004356 spin_unlock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004357
4358 return count;
4359}
4360
4361static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
4362{
4363 return sprintf(buf, "%d\n", perf_overcommit);
4364}
4365
4366static ssize_t
4367perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
4368{
4369 unsigned long val;
4370 int err;
4371
4372 err = strict_strtoul(buf, 10, &val);
4373 if (err)
4374 return err;
4375 if (val > 1)
4376 return -EINVAL;
4377
Ingo Molnar1dce8d92009-05-04 19:23:18 +02004378 spin_lock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004379 perf_overcommit = val;
Ingo Molnar1dce8d92009-05-04 19:23:18 +02004380 spin_unlock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004381
4382 return count;
4383}
4384
4385static SYSDEV_CLASS_ATTR(
4386 reserve_percpu,
4387 0644,
4388 perf_show_reserve_percpu,
4389 perf_set_reserve_percpu
4390 );
4391
4392static SYSDEV_CLASS_ATTR(
4393 overcommit,
4394 0644,
4395 perf_show_overcommit,
4396 perf_set_overcommit
4397 );
4398
4399static struct attribute *perfclass_attrs[] = {
4400 &attr_reserve_percpu.attr,
4401 &attr_overcommit.attr,
4402 NULL
4403};
4404
4405static struct attribute_group perfclass_attr_group = {
4406 .attrs = perfclass_attrs,
4407 .name = "perf_counters",
4408};
4409
4410static int __init perf_counter_sysfs_init(void)
4411{
4412 return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
4413 &perfclass_attr_group);
4414}
4415device_initcall(perf_counter_sysfs_init);