blob: 8bf997d86bf4f4fc856f167d7e006ad9d7ebd36a [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 Zijlstra71a851b2009-07-10 09:06:56 +0200149static void unclone_ctx(struct perf_counter_context *ctx)
150{
151 if (ctx->parent_ctx) {
152 put_ctx(ctx->parent_ctx);
153 ctx->parent_ctx = NULL;
154 }
155}
156
Peter Zijlstrafccc7142009-05-23 18:28:56 +0200157/*
Paul Mackerras25346b932009-06-01 17:48:12 +1000158 * Get the perf_counter_context for a task and lock it.
159 * This has to cope with with the fact that until it is locked,
160 * the context could get moved to another task.
161 */
Ingo Molnar22a4f652009-06-01 10:13:37 +0200162static struct perf_counter_context *
163perf_lock_task_context(struct task_struct *task, unsigned long *flags)
Paul Mackerras25346b932009-06-01 17:48:12 +1000164{
165 struct perf_counter_context *ctx;
166
167 rcu_read_lock();
168 retry:
169 ctx = rcu_dereference(task->perf_counter_ctxp);
170 if (ctx) {
171 /*
172 * If this context is a clone of another, it might
173 * get swapped for another underneath us by
174 * perf_counter_task_sched_out, though the
175 * rcu_read_lock() protects us from any context
176 * getting freed. Lock the context and check if it
177 * got swapped before we could get the lock, and retry
178 * if so. If we locked the right context, then it
179 * can't get swapped on us any more.
180 */
181 spin_lock_irqsave(&ctx->lock, *flags);
182 if (ctx != rcu_dereference(task->perf_counter_ctxp)) {
183 spin_unlock_irqrestore(&ctx->lock, *flags);
184 goto retry;
185 }
Peter Zijlstrab49a9e72009-06-19 17:39:33 +0200186
187 if (!atomic_inc_not_zero(&ctx->refcount)) {
188 spin_unlock_irqrestore(&ctx->lock, *flags);
189 ctx = NULL;
190 }
Paul Mackerras25346b932009-06-01 17:48:12 +1000191 }
192 rcu_read_unlock();
193 return ctx;
194}
195
196/*
197 * Get the context for a task and increment its pin_count so it
198 * can't get swapped to another task. This also increments its
199 * reference count so that the context can't get freed.
200 */
201static struct perf_counter_context *perf_pin_task_context(struct task_struct *task)
202{
203 struct perf_counter_context *ctx;
204 unsigned long flags;
205
206 ctx = perf_lock_task_context(task, &flags);
207 if (ctx) {
208 ++ctx->pin_count;
Paul Mackerras25346b932009-06-01 17:48:12 +1000209 spin_unlock_irqrestore(&ctx->lock, flags);
210 }
211 return ctx;
212}
213
214static void perf_unpin_context(struct perf_counter_context *ctx)
215{
216 unsigned long flags;
217
218 spin_lock_irqsave(&ctx->lock, flags);
219 --ctx->pin_count;
220 spin_unlock_irqrestore(&ctx->lock, flags);
221 put_ctx(ctx);
222}
223
224/*
Peter Zijlstrafccc7142009-05-23 18:28:56 +0200225 * Add a counter from the lists for its context.
226 * Must be called with ctx->mutex and ctx->lock held.
227 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100228static void
229list_add_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
230{
231 struct perf_counter *group_leader = counter->group_leader;
232
233 /*
234 * Depending on whether it is a standalone or sibling counter,
235 * add it straight to the context's counter list, or to the group
236 * leader's sibling list:
237 */
Peter Zijlstra3df5eda2009-05-08 18:52:22 +0200238 if (group_leader == counter)
Ingo Molnar04289bb2008-12-11 08:38:42 +0100239 list_add_tail(&counter->list_entry, &ctx->counter_list);
Peter Zijlstra5c148192009-03-25 12:30:23 +0100240 else {
Ingo Molnar04289bb2008-12-11 08:38:42 +0100241 list_add_tail(&counter->list_entry, &group_leader->sibling_list);
Peter Zijlstra5c148192009-03-25 12:30:23 +0100242 group_leader->nr_siblings++;
243 }
Peter Zijlstra592903c2009-03-13 12:21:36 +0100244
245 list_add_rcu(&counter->event_entry, &ctx->event_list);
Peter Zijlstra8bc20952009-05-15 20:45:59 +0200246 ctx->nr_counters++;
Peter Zijlstrabfbd3382009-06-24 21:11:59 +0200247 if (counter->attr.inherit_stat)
248 ctx->nr_stat++;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100249}
250
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000251/*
252 * Remove a counter from the lists for its context.
Peter Zijlstrafccc7142009-05-23 18:28:56 +0200253 * Must be called with ctx->mutex and ctx->lock held.
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000254 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100255static void
256list_del_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
257{
258 struct perf_counter *sibling, *tmp;
259
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000260 if (list_empty(&counter->list_entry))
261 return;
Peter Zijlstra8bc20952009-05-15 20:45:59 +0200262 ctx->nr_counters--;
Peter Zijlstrabfbd3382009-06-24 21:11:59 +0200263 if (counter->attr.inherit_stat)
264 ctx->nr_stat--;
Peter Zijlstra8bc20952009-05-15 20:45:59 +0200265
Ingo Molnar04289bb2008-12-11 08:38:42 +0100266 list_del_init(&counter->list_entry);
Peter Zijlstra592903c2009-03-13 12:21:36 +0100267 list_del_rcu(&counter->event_entry);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100268
Peter Zijlstra5c148192009-03-25 12:30:23 +0100269 if (counter->group_leader != counter)
270 counter->group_leader->nr_siblings--;
271
Ingo Molnar04289bb2008-12-11 08:38:42 +0100272 /*
273 * If this was a group counter with sibling counters then
274 * upgrade the siblings to singleton counters by adding them
275 * to the context list directly:
276 */
277 list_for_each_entry_safe(sibling, tmp,
278 &counter->sibling_list, list_entry) {
279
Peter Zijlstra75564232009-03-13 12:21:29 +0100280 list_move_tail(&sibling->list_entry, &ctx->counter_list);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100281 sibling->group_leader = sibling;
282 }
283}
284
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100285static void
286counter_sched_out(struct perf_counter *counter,
287 struct perf_cpu_context *cpuctx,
288 struct perf_counter_context *ctx)
289{
290 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
291 return;
292
293 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200294 counter->tstamp_stopped = ctx->time;
Robert Richter4aeb0b42009-04-29 12:47:03 +0200295 counter->pmu->disable(counter);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100296 counter->oncpu = -1;
297
298 if (!is_software_counter(counter))
299 cpuctx->active_oncpu--;
300 ctx->nr_active--;
Peter Zijlstra0d486962009-06-02 19:22:16 +0200301 if (counter->attr.exclusive || !cpuctx->active_oncpu)
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100302 cpuctx->exclusive = 0;
303}
304
Paul Mackerrasd859e292009-01-17 18:10:22 +1100305static void
306group_sched_out(struct perf_counter *group_counter,
307 struct perf_cpu_context *cpuctx,
308 struct perf_counter_context *ctx)
309{
310 struct perf_counter *counter;
311
312 if (group_counter->state != PERF_COUNTER_STATE_ACTIVE)
313 return;
314
315 counter_sched_out(group_counter, cpuctx, ctx);
316
317 /*
318 * Schedule out siblings (if any):
319 */
320 list_for_each_entry(counter, &group_counter->sibling_list, list_entry)
321 counter_sched_out(counter, cpuctx, ctx);
322
Peter Zijlstra0d486962009-06-02 19:22:16 +0200323 if (group_counter->attr.exclusive)
Paul Mackerrasd859e292009-01-17 18:10:22 +1100324 cpuctx->exclusive = 0;
325}
326
Thomas Gleixner0793a612008-12-04 20:12:29 +0100327/*
328 * Cross CPU call to remove a performance counter
329 *
330 * We disable the counter on the hardware level first. After that we
331 * remove it from the context list.
332 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100333static void __perf_counter_remove_from_context(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100334{
335 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
336 struct perf_counter *counter = info;
337 struct perf_counter_context *ctx = counter->ctx;
338
339 /*
340 * If this is a task context, we need to check whether it is
341 * the current task context of this cpu. If not it has been
342 * scheduled out before the smp call arrived.
343 */
Peter Zijlstra665c2142009-05-29 14:51:57 +0200344 if (ctx->task && cpuctx->task_ctx != ctx)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100345 return;
346
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200347 spin_lock(&ctx->lock);
Ingo Molnar34adc802009-05-20 20:13:28 +0200348 /*
349 * Protect the list operation against NMI by disabling the
350 * counters on a global level.
351 */
352 perf_disable();
Thomas Gleixner0793a612008-12-04 20:12:29 +0100353
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100354 counter_sched_out(counter, cpuctx, ctx);
355
Ingo Molnar04289bb2008-12-11 08:38:42 +0100356 list_del_counter(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100357
358 if (!ctx->task) {
359 /*
360 * Allow more per task counters with respect to the
361 * reservation:
362 */
363 cpuctx->max_pertask =
364 min(perf_max_counters - ctx->nr_counters,
365 perf_max_counters - perf_reserved_percpu);
366 }
367
Ingo Molnar34adc802009-05-20 20:13:28 +0200368 perf_enable();
Peter Zijlstra665c2142009-05-29 14:51:57 +0200369 spin_unlock(&ctx->lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100370}
371
372
373/*
374 * Remove the counter from a task's (or a CPU's) list of counters.
375 *
Peter Zijlstrafccc7142009-05-23 18:28:56 +0200376 * Must be called with ctx->mutex held.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100377 *
378 * CPU counters are removed with a smp call. For task counters we only
379 * call when the task is on a CPU.
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000380 *
381 * If counter->ctx is a cloned context, callers must make sure that
382 * every task struct that counter->ctx->task could possibly point to
383 * remains valid. This is OK when called from perf_release since
384 * that only calls us on the top-level context, which can't be a clone.
385 * When called from perf_counter_exit_task, it's OK because the
386 * context has been detached from its task.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100387 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100388static void perf_counter_remove_from_context(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100389{
390 struct perf_counter_context *ctx = counter->ctx;
391 struct task_struct *task = ctx->task;
392
393 if (!task) {
394 /*
395 * Per cpu counters are removed via an smp call and
396 * the removal is always sucessful.
397 */
398 smp_call_function_single(counter->cpu,
Ingo Molnar04289bb2008-12-11 08:38:42 +0100399 __perf_counter_remove_from_context,
Thomas Gleixner0793a612008-12-04 20:12:29 +0100400 counter, 1);
401 return;
402 }
403
404retry:
Ingo Molnar04289bb2008-12-11 08:38:42 +0100405 task_oncpu_function_call(task, __perf_counter_remove_from_context,
Thomas Gleixner0793a612008-12-04 20:12:29 +0100406 counter);
407
408 spin_lock_irq(&ctx->lock);
409 /*
410 * If the context is active we need to retry the smp call.
411 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100412 if (ctx->nr_active && !list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100413 spin_unlock_irq(&ctx->lock);
414 goto retry;
415 }
416
417 /*
418 * The lock prevents that this context is scheduled in so we
Ingo Molnar04289bb2008-12-11 08:38:42 +0100419 * can remove the counter safely, if the call above did not
Thomas Gleixner0793a612008-12-04 20:12:29 +0100420 * succeed.
421 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100422 if (!list_empty(&counter->list_entry)) {
Ingo Molnar04289bb2008-12-11 08:38:42 +0100423 list_del_counter(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100424 }
425 spin_unlock_irq(&ctx->lock);
426}
427
Peter Zijlstra4af49982009-04-06 11:45:10 +0200428static inline u64 perf_clock(void)
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100429{
Peter Zijlstra4af49982009-04-06 11:45:10 +0200430 return cpu_clock(smp_processor_id());
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100431}
432
433/*
434 * Update the record of the current time in a context.
435 */
Peter Zijlstra4af49982009-04-06 11:45:10 +0200436static void update_context_time(struct perf_counter_context *ctx)
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100437{
Peter Zijlstra4af49982009-04-06 11:45:10 +0200438 u64 now = perf_clock();
439
440 ctx->time += now - ctx->timestamp;
441 ctx->timestamp = now;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100442}
443
444/*
445 * Update the total_time_enabled and total_time_running fields for a counter.
446 */
447static void update_counter_times(struct perf_counter *counter)
448{
449 struct perf_counter_context *ctx = counter->ctx;
450 u64 run_end;
451
Peter Zijlstra4af49982009-04-06 11:45:10 +0200452 if (counter->state < PERF_COUNTER_STATE_INACTIVE)
453 return;
454
455 counter->total_time_enabled = ctx->time - counter->tstamp_enabled;
456
457 if (counter->state == PERF_COUNTER_STATE_INACTIVE)
458 run_end = counter->tstamp_stopped;
459 else
460 run_end = ctx->time;
461
462 counter->total_time_running = run_end - counter->tstamp_running;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100463}
464
465/*
466 * Update total_time_enabled and total_time_running for all counters in a group.
467 */
468static void update_group_times(struct perf_counter *leader)
469{
470 struct perf_counter *counter;
471
472 update_counter_times(leader);
473 list_for_each_entry(counter, &leader->sibling_list, list_entry)
474 update_counter_times(counter);
475}
476
477/*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100478 * Cross CPU call to disable a performance counter
479 */
480static void __perf_counter_disable(void *info)
481{
482 struct perf_counter *counter = info;
483 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
484 struct perf_counter_context *ctx = counter->ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100485
486 /*
487 * If this is a per-task counter, need to check whether this
488 * counter's task is the current task on this cpu.
489 */
Peter Zijlstra665c2142009-05-29 14:51:57 +0200490 if (ctx->task && cpuctx->task_ctx != ctx)
Paul Mackerrasd859e292009-01-17 18:10:22 +1100491 return;
492
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200493 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100494
495 /*
496 * If the counter is on, turn it off.
497 * If it is in error state, leave it in error state.
498 */
499 if (counter->state >= PERF_COUNTER_STATE_INACTIVE) {
Peter Zijlstra4af49982009-04-06 11:45:10 +0200500 update_context_time(ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100501 update_counter_times(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100502 if (counter == counter->group_leader)
503 group_sched_out(counter, cpuctx, ctx);
504 else
505 counter_sched_out(counter, cpuctx, ctx);
506 counter->state = PERF_COUNTER_STATE_OFF;
507 }
508
Peter Zijlstra665c2142009-05-29 14:51:57 +0200509 spin_unlock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100510}
511
512/*
513 * Disable a counter.
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000514 *
515 * If counter->ctx is a cloned context, callers must make sure that
516 * every task struct that counter->ctx->task could possibly point to
517 * remains valid. This condition is satisifed when called through
518 * perf_counter_for_each_child or perf_counter_for_each because they
519 * hold the top-level counter's child_mutex, so any descendant that
520 * goes to exit will block in sync_child_counter.
521 * When called from perf_pending_counter it's OK because counter->ctx
522 * is the current context on this CPU and preemption is disabled,
523 * hence we can't get into perf_counter_task_sched_out for this context.
Paul Mackerrasd859e292009-01-17 18:10:22 +1100524 */
525static void perf_counter_disable(struct perf_counter *counter)
526{
527 struct perf_counter_context *ctx = counter->ctx;
528 struct task_struct *task = ctx->task;
529
530 if (!task) {
531 /*
532 * Disable the counter on the cpu that it's on
533 */
534 smp_call_function_single(counter->cpu, __perf_counter_disable,
535 counter, 1);
536 return;
537 }
538
539 retry:
540 task_oncpu_function_call(task, __perf_counter_disable, counter);
541
542 spin_lock_irq(&ctx->lock);
543 /*
544 * If the counter is still active, we need to retry the cross-call.
545 */
546 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
547 spin_unlock_irq(&ctx->lock);
548 goto retry;
549 }
550
551 /*
552 * Since we have the lock this context can't be scheduled
553 * in, so we can change the state safely.
554 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100555 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
556 update_counter_times(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100557 counter->state = PERF_COUNTER_STATE_OFF;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100558 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100559
560 spin_unlock_irq(&ctx->lock);
561}
562
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100563static int
564counter_sched_in(struct perf_counter *counter,
565 struct perf_cpu_context *cpuctx,
566 struct perf_counter_context *ctx,
567 int cpu)
568{
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100569 if (counter->state <= PERF_COUNTER_STATE_OFF)
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100570 return 0;
571
572 counter->state = PERF_COUNTER_STATE_ACTIVE;
573 counter->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */
574 /*
575 * The new state must be visible before we turn it on in the hardware:
576 */
577 smp_wmb();
578
Robert Richter4aeb0b42009-04-29 12:47:03 +0200579 if (counter->pmu->enable(counter)) {
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100580 counter->state = PERF_COUNTER_STATE_INACTIVE;
581 counter->oncpu = -1;
582 return -EAGAIN;
583 }
584
Peter Zijlstra4af49982009-04-06 11:45:10 +0200585 counter->tstamp_running += ctx->time - counter->tstamp_stopped;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100586
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100587 if (!is_software_counter(counter))
588 cpuctx->active_oncpu++;
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100589 ctx->nr_active++;
590
Peter Zijlstra0d486962009-06-02 19:22:16 +0200591 if (counter->attr.exclusive)
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100592 cpuctx->exclusive = 1;
593
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100594 return 0;
595}
596
Paul Mackerras6751b712009-05-11 12:08:02 +1000597static int
598group_sched_in(struct perf_counter *group_counter,
599 struct perf_cpu_context *cpuctx,
600 struct perf_counter_context *ctx,
601 int cpu)
602{
603 struct perf_counter *counter, *partial_group;
604 int ret;
605
606 if (group_counter->state == PERF_COUNTER_STATE_OFF)
607 return 0;
608
609 ret = hw_perf_group_sched_in(group_counter, cpuctx, ctx, cpu);
610 if (ret)
611 return ret < 0 ? ret : 0;
612
Paul Mackerras6751b712009-05-11 12:08:02 +1000613 if (counter_sched_in(group_counter, cpuctx, ctx, cpu))
614 return -EAGAIN;
615
616 /*
617 * Schedule in siblings as one group (if any):
618 */
619 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
Paul Mackerras6751b712009-05-11 12:08:02 +1000620 if (counter_sched_in(counter, cpuctx, ctx, cpu)) {
621 partial_group = counter;
622 goto group_error;
623 }
624 }
625
626 return 0;
627
628group_error:
629 /*
630 * Groups can be scheduled in as one unit only, so undo any
631 * partial group before returning:
632 */
633 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
634 if (counter == partial_group)
635 break;
636 counter_sched_out(counter, cpuctx, ctx);
637 }
638 counter_sched_out(group_counter, cpuctx, ctx);
639
640 return -EAGAIN;
641}
642
Thomas Gleixner0793a612008-12-04 20:12:29 +0100643/*
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100644 * Return 1 for a group consisting entirely of software counters,
645 * 0 if the group contains any hardware counters.
646 */
647static int is_software_only_group(struct perf_counter *leader)
648{
649 struct perf_counter *counter;
650
651 if (!is_software_counter(leader))
652 return 0;
Peter Zijlstra5c148192009-03-25 12:30:23 +0100653
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100654 list_for_each_entry(counter, &leader->sibling_list, list_entry)
655 if (!is_software_counter(counter))
656 return 0;
Peter Zijlstra5c148192009-03-25 12:30:23 +0100657
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100658 return 1;
659}
660
661/*
662 * Work out whether we can put this counter group on the CPU now.
663 */
664static int group_can_go_on(struct perf_counter *counter,
665 struct perf_cpu_context *cpuctx,
666 int can_add_hw)
667{
668 /*
669 * Groups consisting entirely of software counters can always go on.
670 */
671 if (is_software_only_group(counter))
672 return 1;
673 /*
674 * If an exclusive group is already on, no other hardware
675 * counters can go on.
676 */
677 if (cpuctx->exclusive)
678 return 0;
679 /*
680 * If this group is exclusive and there are already
681 * counters on the CPU, it can't go on.
682 */
Peter Zijlstra0d486962009-06-02 19:22:16 +0200683 if (counter->attr.exclusive && cpuctx->active_oncpu)
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100684 return 0;
685 /*
686 * Otherwise, try to add it if all previous groups were able
687 * to go on.
688 */
689 return can_add_hw;
690}
691
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100692static void add_counter_to_ctx(struct perf_counter *counter,
693 struct perf_counter_context *ctx)
694{
695 list_add_counter(counter, ctx);
Peter Zijlstra4af49982009-04-06 11:45:10 +0200696 counter->tstamp_enabled = ctx->time;
697 counter->tstamp_running = ctx->time;
698 counter->tstamp_stopped = ctx->time;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100699}
700
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100701/*
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100702 * Cross CPU call to install and enable a performance counter
Peter Zijlstra682076a2009-05-23 18:28:57 +0200703 *
704 * Must be called with ctx->mutex held
Thomas Gleixner0793a612008-12-04 20:12:29 +0100705 */
706static void __perf_install_in_context(void *info)
707{
708 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
709 struct perf_counter *counter = info;
710 struct perf_counter_context *ctx = counter->ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100711 struct perf_counter *leader = counter->group_leader;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100712 int cpu = smp_processor_id();
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100713 int err;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100714
715 /*
716 * If this is a task context, we need to check whether it is
717 * the current task context of this cpu. If not it has been
718 * scheduled out before the smp call arrived.
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000719 * Or possibly this is the right context but it isn't
720 * on this cpu because it had no counters.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100721 */
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000722 if (ctx->task && cpuctx->task_ctx != ctx) {
Peter Zijlstra665c2142009-05-29 14:51:57 +0200723 if (cpuctx->task_ctx || ctx->task != current)
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000724 return;
725 cpuctx->task_ctx = ctx;
726 }
Thomas Gleixner0793a612008-12-04 20:12:29 +0100727
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200728 spin_lock(&ctx->lock);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000729 ctx->is_active = 1;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200730 update_context_time(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100731
732 /*
733 * Protect the list operation against NMI by disabling the
734 * counters on a global level. NOP for non NMI based counters.
735 */
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200736 perf_disable();
Thomas Gleixner0793a612008-12-04 20:12:29 +0100737
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100738 add_counter_to_ctx(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100739
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100740 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100741 * Don't put the counter on if it is disabled or if
742 * it is in a group and the group isn't on.
743 */
744 if (counter->state != PERF_COUNTER_STATE_INACTIVE ||
745 (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE))
746 goto unlock;
747
748 /*
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100749 * An exclusive counter can't go on if there are already active
750 * hardware counters, and no hardware counter can go on if there
751 * is already an exclusive counter on.
752 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100753 if (!group_can_go_on(counter, cpuctx, 1))
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100754 err = -EEXIST;
755 else
756 err = counter_sched_in(counter, cpuctx, ctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100757
Paul Mackerrasd859e292009-01-17 18:10:22 +1100758 if (err) {
759 /*
760 * This counter couldn't go on. If it is in a group
761 * then we have to pull the whole group off.
762 * If the counter group is pinned then put it in error state.
763 */
764 if (leader != counter)
765 group_sched_out(leader, cpuctx, ctx);
Peter Zijlstra0d486962009-06-02 19:22:16 +0200766 if (leader->attr.pinned) {
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100767 update_group_times(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100768 leader->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100769 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100770 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100771
772 if (!err && !ctx->task && cpuctx->max_pertask)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100773 cpuctx->max_pertask--;
774
Paul Mackerrasd859e292009-01-17 18:10:22 +1100775 unlock:
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200776 perf_enable();
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100777
Peter Zijlstra665c2142009-05-29 14:51:57 +0200778 spin_unlock(&ctx->lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100779}
780
781/*
782 * Attach a performance counter to a context
783 *
784 * First we add the counter to the list with the hardware enable bit
785 * in counter->hw_config cleared.
786 *
787 * If the counter is attached to a task which is on a CPU we use a smp
788 * call to enable it in the task context. The task might have been
789 * scheduled away, but we check this in the smp call again.
Paul Mackerrasd859e292009-01-17 18:10:22 +1100790 *
791 * Must be called with ctx->mutex held.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100792 */
793static void
794perf_install_in_context(struct perf_counter_context *ctx,
795 struct perf_counter *counter,
796 int cpu)
797{
798 struct task_struct *task = ctx->task;
799
Thomas Gleixner0793a612008-12-04 20:12:29 +0100800 if (!task) {
801 /*
802 * Per cpu counters are installed via an smp call and
803 * the install is always sucessful.
804 */
805 smp_call_function_single(cpu, __perf_install_in_context,
806 counter, 1);
807 return;
808 }
809
Thomas Gleixner0793a612008-12-04 20:12:29 +0100810retry:
811 task_oncpu_function_call(task, __perf_install_in_context,
812 counter);
813
814 spin_lock_irq(&ctx->lock);
815 /*
Thomas Gleixner0793a612008-12-04 20:12:29 +0100816 * we need to retry the smp call.
817 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100818 if (ctx->is_active && list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100819 spin_unlock_irq(&ctx->lock);
820 goto retry;
821 }
822
823 /*
824 * The lock prevents that this context is scheduled in so we
825 * can add the counter safely, if it the call above did not
826 * succeed.
827 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100828 if (list_empty(&counter->list_entry))
829 add_counter_to_ctx(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100830 spin_unlock_irq(&ctx->lock);
831}
832
Paul Mackerrasd859e292009-01-17 18:10:22 +1100833/*
834 * Cross CPU call to enable a performance counter
835 */
836static void __perf_counter_enable(void *info)
Ingo Molnar04289bb2008-12-11 08:38:42 +0100837{
Paul Mackerrasd859e292009-01-17 18:10:22 +1100838 struct perf_counter *counter = info;
839 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
840 struct perf_counter_context *ctx = counter->ctx;
841 struct perf_counter *leader = counter->group_leader;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100842 int err;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100843
844 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100845 * If this is a per-task counter, need to check whether this
846 * counter's task is the current task on this cpu.
Ingo Molnar04289bb2008-12-11 08:38:42 +0100847 */
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000848 if (ctx->task && cpuctx->task_ctx != ctx) {
Peter Zijlstra665c2142009-05-29 14:51:57 +0200849 if (cpuctx->task_ctx || ctx->task != current)
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000850 return;
851 cpuctx->task_ctx = ctx;
852 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100853
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200854 spin_lock(&ctx->lock);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000855 ctx->is_active = 1;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200856 update_context_time(ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100857
858 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
859 goto unlock;
860 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200861 counter->tstamp_enabled = ctx->time - counter->total_time_enabled;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100862
863 /*
864 * If the counter is in a group and isn't the group leader,
865 * then don't put it on unless the group is on.
866 */
867 if (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE)
868 goto unlock;
869
Paul Mackerrase758a332009-05-12 21:59:01 +1000870 if (!group_can_go_on(counter, cpuctx, 1)) {
Paul Mackerrasd859e292009-01-17 18:10:22 +1100871 err = -EEXIST;
Paul Mackerrase758a332009-05-12 21:59:01 +1000872 } else {
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200873 perf_disable();
Paul Mackerrase758a332009-05-12 21:59:01 +1000874 if (counter == leader)
875 err = group_sched_in(counter, cpuctx, ctx,
876 smp_processor_id());
877 else
878 err = counter_sched_in(counter, cpuctx, ctx,
879 smp_processor_id());
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200880 perf_enable();
Paul Mackerrase758a332009-05-12 21:59:01 +1000881 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100882
883 if (err) {
884 /*
885 * If this counter can't go on and it's part of a
886 * group, then the whole group has to come off.
887 */
888 if (leader != counter)
889 group_sched_out(leader, cpuctx, ctx);
Peter Zijlstra0d486962009-06-02 19:22:16 +0200890 if (leader->attr.pinned) {
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100891 update_group_times(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100892 leader->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100893 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100894 }
895
896 unlock:
Peter Zijlstra665c2142009-05-29 14:51:57 +0200897 spin_unlock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100898}
899
900/*
901 * Enable a counter.
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000902 *
903 * If counter->ctx is a cloned context, callers must make sure that
904 * every task struct that counter->ctx->task could possibly point to
905 * remains valid. This condition is satisfied when called through
906 * perf_counter_for_each_child or perf_counter_for_each as described
907 * for perf_counter_disable.
Paul Mackerrasd859e292009-01-17 18:10:22 +1100908 */
909static void perf_counter_enable(struct perf_counter *counter)
910{
911 struct perf_counter_context *ctx = counter->ctx;
912 struct task_struct *task = ctx->task;
913
914 if (!task) {
915 /*
916 * Enable the counter on the cpu that it's on
917 */
918 smp_call_function_single(counter->cpu, __perf_counter_enable,
919 counter, 1);
920 return;
921 }
922
923 spin_lock_irq(&ctx->lock);
924 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
925 goto out;
926
927 /*
928 * If the counter is in error state, clear that first.
929 * That way, if we see the counter in error state below, we
930 * know that it has gone back into error state, as distinct
931 * from the task having been scheduled away before the
932 * cross-call arrived.
933 */
934 if (counter->state == PERF_COUNTER_STATE_ERROR)
935 counter->state = PERF_COUNTER_STATE_OFF;
936
937 retry:
938 spin_unlock_irq(&ctx->lock);
939 task_oncpu_function_call(task, __perf_counter_enable, counter);
940
941 spin_lock_irq(&ctx->lock);
942
943 /*
944 * If the context is active and the counter is still off,
945 * we need to retry the cross-call.
946 */
947 if (ctx->is_active && counter->state == PERF_COUNTER_STATE_OFF)
948 goto retry;
949
950 /*
951 * Since we have the lock this context can't be scheduled
952 * in, so we can change the state safely.
953 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100954 if (counter->state == PERF_COUNTER_STATE_OFF) {
Paul Mackerrasd859e292009-01-17 18:10:22 +1100955 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200956 counter->tstamp_enabled =
957 ctx->time - counter->total_time_enabled;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100958 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100959 out:
960 spin_unlock_irq(&ctx->lock);
961}
962
Peter Zijlstra2023b352009-05-05 17:50:26 +0200963static int perf_counter_refresh(struct perf_counter *counter, int refresh)
Peter Zijlstra79f14642009-04-06 11:45:07 +0200964{
Peter Zijlstra2023b352009-05-05 17:50:26 +0200965 /*
966 * not supported on inherited counters
967 */
Peter Zijlstra0d486962009-06-02 19:22:16 +0200968 if (counter->attr.inherit)
Peter Zijlstra2023b352009-05-05 17:50:26 +0200969 return -EINVAL;
970
Peter Zijlstra79f14642009-04-06 11:45:07 +0200971 atomic_add(refresh, &counter->event_limit);
972 perf_counter_enable(counter);
Peter Zijlstra2023b352009-05-05 17:50:26 +0200973
974 return 0;
Peter Zijlstra79f14642009-04-06 11:45:07 +0200975}
976
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100977void __perf_counter_sched_out(struct perf_counter_context *ctx,
978 struct perf_cpu_context *cpuctx)
979{
980 struct perf_counter *counter;
981
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100982 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100983 ctx->is_active = 0;
984 if (likely(!ctx->nr_counters))
985 goto out;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200986 update_context_time(ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100987
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200988 perf_disable();
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100989 if (ctx->nr_active) {
Peter Zijlstraafedadf2009-05-20 12:21:22 +0200990 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
991 if (counter != counter->group_leader)
992 counter_sched_out(counter, cpuctx, ctx);
993 else
994 group_sched_out(counter, cpuctx, ctx);
995 }
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100996 }
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200997 perf_enable();
Paul Mackerrasd859e292009-01-17 18:10:22 +1100998 out:
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100999 spin_unlock(&ctx->lock);
1000}
1001
Thomas Gleixner0793a612008-12-04 20:12:29 +01001002/*
Paul Mackerras564c2b22009-05-22 14:27:22 +10001003 * Test whether two contexts are equivalent, i.e. whether they
1004 * have both been cloned from the same version of the same context
1005 * and they both have the same number of enabled counters.
1006 * If the number of enabled counters is the same, then the set
1007 * of enabled counters should be the same, because these are both
1008 * inherited contexts, therefore we can't access individual counters
1009 * in them directly with an fd; we can only enable/disable all
1010 * counters via prctl, or enable/disable all counters in a family
1011 * via ioctl, which will have the same effect on both contexts.
1012 */
1013static int context_equiv(struct perf_counter_context *ctx1,
1014 struct perf_counter_context *ctx2)
1015{
1016 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001017 && ctx1->parent_gen == ctx2->parent_gen
Paul Mackerras25346b932009-06-01 17:48:12 +10001018 && !ctx1->pin_count && !ctx2->pin_count;
Paul Mackerras564c2b22009-05-22 14:27:22 +10001019}
1020
Peter Zijlstrabfbd3382009-06-24 21:11:59 +02001021static void __perf_counter_read(void *counter);
1022
1023static void __perf_counter_sync_stat(struct perf_counter *counter,
1024 struct perf_counter *next_counter)
1025{
1026 u64 value;
1027
1028 if (!counter->attr.inherit_stat)
1029 return;
1030
1031 /*
1032 * Update the counter value, we cannot use perf_counter_read()
1033 * because we're in the middle of a context switch and have IRQs
1034 * disabled, which upsets smp_call_function_single(), however
1035 * we know the counter must be on the current CPU, therefore we
1036 * don't need to use it.
1037 */
1038 switch (counter->state) {
1039 case PERF_COUNTER_STATE_ACTIVE:
1040 __perf_counter_read(counter);
1041 break;
1042
1043 case PERF_COUNTER_STATE_INACTIVE:
1044 update_counter_times(counter);
1045 break;
1046
1047 default:
1048 break;
1049 }
1050
1051 /*
1052 * In order to keep per-task stats reliable we need to flip the counter
1053 * values when we flip the contexts.
1054 */
1055 value = atomic64_read(&next_counter->count);
1056 value = atomic64_xchg(&counter->count, value);
1057 atomic64_set(&next_counter->count, value);
1058
Peter Zijlstra19d2e752009-06-26 13:10:23 +02001059 swap(counter->total_time_enabled, next_counter->total_time_enabled);
1060 swap(counter->total_time_running, next_counter->total_time_running);
1061
Peter Zijlstrabfbd3382009-06-24 21:11:59 +02001062 /*
Peter Zijlstra19d2e752009-06-26 13:10:23 +02001063 * Since we swizzled the values, update the user visible data too.
Peter Zijlstrabfbd3382009-06-24 21:11:59 +02001064 */
Peter Zijlstra19d2e752009-06-26 13:10:23 +02001065 perf_counter_update_userpage(counter);
1066 perf_counter_update_userpage(next_counter);
Peter Zijlstrabfbd3382009-06-24 21:11:59 +02001067}
1068
1069#define list_next_entry(pos, member) \
1070 list_entry(pos->member.next, typeof(*pos), member)
1071
1072static void perf_counter_sync_stat(struct perf_counter_context *ctx,
1073 struct perf_counter_context *next_ctx)
1074{
1075 struct perf_counter *counter, *next_counter;
1076
1077 if (!ctx->nr_stat)
1078 return;
1079
1080 counter = list_first_entry(&ctx->event_list,
1081 struct perf_counter, event_entry);
1082
1083 next_counter = list_first_entry(&next_ctx->event_list,
1084 struct perf_counter, event_entry);
1085
1086 while (&counter->event_entry != &ctx->event_list &&
1087 &next_counter->event_entry != &next_ctx->event_list) {
1088
1089 __perf_counter_sync_stat(counter, next_counter);
1090
1091 counter = list_next_entry(counter, event_entry);
1092 next_counter = list_next_entry(counter, event_entry);
1093 }
1094}
1095
Paul Mackerras564c2b22009-05-22 14:27:22 +10001096/*
Thomas Gleixner0793a612008-12-04 20:12:29 +01001097 * Called from scheduler to remove the counters of the current task,
1098 * with interrupts disabled.
1099 *
1100 * We stop each counter and update the counter value in counter->count.
1101 *
Ingo Molnar76715812008-12-17 14:20:28 +01001102 * This does not protect us against NMI, but disable()
Thomas Gleixner0793a612008-12-04 20:12:29 +01001103 * sets the disabled bit in the control field of counter _before_
1104 * accessing the counter control register. If a NMI hits, then it will
1105 * not restart the counter.
1106 */
Paul Mackerras564c2b22009-05-22 14:27:22 +10001107void perf_counter_task_sched_out(struct task_struct *task,
1108 struct task_struct *next, int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001109{
1110 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001111 struct perf_counter_context *ctx = task->perf_counter_ctxp;
Paul Mackerras564c2b22009-05-22 14:27:22 +10001112 struct perf_counter_context *next_ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001113 struct perf_counter_context *parent;
Peter Zijlstra4a0deca2009-03-19 20:26:12 +01001114 struct pt_regs *regs;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001115 int do_switch = 1;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001116
Peter Zijlstra10989fb2009-05-25 14:45:28 +02001117 regs = task_pt_regs(task);
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +02001118 perf_swcounter_event(PERF_COUNT_SW_CONTEXT_SWITCHES, 1, 1, regs, 0);
Peter Zijlstra10989fb2009-05-25 14:45:28 +02001119
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001120 if (likely(!ctx || !cpuctx->task_ctx))
Thomas Gleixner0793a612008-12-04 20:12:29 +01001121 return;
1122
Peter Zijlstrabce379b2009-04-06 11:45:13 +02001123 update_context_time(ctx);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001124
1125 rcu_read_lock();
1126 parent = rcu_dereference(ctx->parent_ctx);
Paul Mackerras564c2b22009-05-22 14:27:22 +10001127 next_ctx = next->perf_counter_ctxp;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001128 if (parent && next_ctx &&
1129 rcu_dereference(next_ctx->parent_ctx) == parent) {
1130 /*
1131 * Looks like the two contexts are clones, so we might be
1132 * able to optimize the context switch. We lock both
1133 * contexts and check that they are clones under the
1134 * lock (including re-checking that neither has been
1135 * uncloned in the meantime). It doesn't matter which
1136 * order we take the locks because no other cpu could
1137 * be trying to lock both of these tasks.
1138 */
1139 spin_lock(&ctx->lock);
1140 spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
1141 if (context_equiv(ctx, next_ctx)) {
Peter Zijlstra665c2142009-05-29 14:51:57 +02001142 /*
1143 * XXX do we need a memory barrier of sorts
1144 * wrt to rcu_dereference() of perf_counter_ctxp
1145 */
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001146 task->perf_counter_ctxp = next_ctx;
1147 next->perf_counter_ctxp = ctx;
1148 ctx->task = next;
1149 next_ctx->task = task;
1150 do_switch = 0;
Peter Zijlstrabfbd3382009-06-24 21:11:59 +02001151
1152 perf_counter_sync_stat(ctx, next_ctx);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001153 }
1154 spin_unlock(&next_ctx->lock);
1155 spin_unlock(&ctx->lock);
Paul Mackerras564c2b22009-05-22 14:27:22 +10001156 }
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001157 rcu_read_unlock();
Paul Mackerras564c2b22009-05-22 14:27:22 +10001158
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001159 if (do_switch) {
1160 __perf_counter_sched_out(ctx, cpuctx);
1161 cpuctx->task_ctx = NULL;
1162 }
Thomas Gleixner0793a612008-12-04 20:12:29 +01001163}
1164
Peter Zijlstra665c2142009-05-29 14:51:57 +02001165/*
1166 * Called with IRQs disabled
1167 */
Paul Mackerrasa08b1592009-05-11 15:46:10 +10001168static void __perf_counter_task_sched_out(struct perf_counter_context *ctx)
1169{
1170 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1171
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001172 if (!cpuctx->task_ctx)
1173 return;
Ingo Molnar012b84d2009-05-17 11:08:41 +02001174
1175 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
1176 return;
1177
Paul Mackerrasa08b1592009-05-11 15:46:10 +10001178 __perf_counter_sched_out(ctx, cpuctx);
1179 cpuctx->task_ctx = NULL;
1180}
1181
Peter Zijlstra665c2142009-05-29 14:51:57 +02001182/*
1183 * Called with IRQs disabled
1184 */
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001185static void perf_counter_cpu_sched_out(struct perf_cpu_context *cpuctx)
Ingo Molnar04289bb2008-12-11 08:38:42 +01001186{
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001187 __perf_counter_sched_out(&cpuctx->ctx, cpuctx);
Ingo Molnar04289bb2008-12-11 08:38:42 +01001188}
1189
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001190static void
1191__perf_counter_sched_in(struct perf_counter_context *ctx,
1192 struct perf_cpu_context *cpuctx, int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001193{
Thomas Gleixner0793a612008-12-04 20:12:29 +01001194 struct perf_counter *counter;
Paul Mackerrasdd0e6ba2009-01-12 15:11:00 +11001195 int can_add_hw = 1;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001196
Thomas Gleixner0793a612008-12-04 20:12:29 +01001197 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001198 ctx->is_active = 1;
1199 if (likely(!ctx->nr_counters))
1200 goto out;
1201
Peter Zijlstra4af49982009-04-06 11:45:10 +02001202 ctx->timestamp = perf_clock();
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001203
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001204 perf_disable();
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001205
1206 /*
1207 * First go through the list and put on any pinned groups
1208 * in order to give them the best chance of going on.
1209 */
Ingo Molnar04289bb2008-12-11 08:38:42 +01001210 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001211 if (counter->state <= PERF_COUNTER_STATE_OFF ||
Peter Zijlstra0d486962009-06-02 19:22:16 +02001212 !counter->attr.pinned)
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001213 continue;
1214 if (counter->cpu != -1 && counter->cpu != cpu)
1215 continue;
1216
Peter Zijlstraafedadf2009-05-20 12:21:22 +02001217 if (counter != counter->group_leader)
1218 counter_sched_in(counter, cpuctx, ctx, cpu);
1219 else {
1220 if (group_can_go_on(counter, cpuctx, 1))
1221 group_sched_in(counter, cpuctx, ctx, cpu);
1222 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001223
1224 /*
1225 * If this pinned group hasn't been scheduled,
1226 * put it in error state.
1227 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001228 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
1229 update_group_times(counter);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001230 counter->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001231 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001232 }
1233
1234 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1235 /*
1236 * Ignore counters in OFF or ERROR state, and
1237 * ignore pinned counters since we did them already.
1238 */
1239 if (counter->state <= PERF_COUNTER_STATE_OFF ||
Peter Zijlstra0d486962009-06-02 19:22:16 +02001240 counter->attr.pinned)
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001241 continue;
1242
Ingo Molnar04289bb2008-12-11 08:38:42 +01001243 /*
1244 * Listen to the 'cpu' scheduling filter constraint
1245 * of counters:
1246 */
Thomas Gleixner0793a612008-12-04 20:12:29 +01001247 if (counter->cpu != -1 && counter->cpu != cpu)
1248 continue;
1249
Peter Zijlstraafedadf2009-05-20 12:21:22 +02001250 if (counter != counter->group_leader) {
1251 if (counter_sched_in(counter, cpuctx, ctx, cpu))
Paul Mackerrasdd0e6ba2009-01-12 15:11:00 +11001252 can_add_hw = 0;
Peter Zijlstraafedadf2009-05-20 12:21:22 +02001253 } else {
1254 if (group_can_go_on(counter, cpuctx, can_add_hw)) {
1255 if (group_sched_in(counter, cpuctx, ctx, cpu))
1256 can_add_hw = 0;
1257 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001258 }
Thomas Gleixner0793a612008-12-04 20:12:29 +01001259 }
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001260 perf_enable();
Paul Mackerrasd859e292009-01-17 18:10:22 +11001261 out:
Thomas Gleixner0793a612008-12-04 20:12:29 +01001262 spin_unlock(&ctx->lock);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001263}
Ingo Molnar04289bb2008-12-11 08:38:42 +01001264
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001265/*
1266 * Called from scheduler to add the counters of the current task
1267 * with interrupts disabled.
1268 *
1269 * We restore the counter value and then enable it.
1270 *
1271 * This does not protect us against NMI, but enable()
1272 * sets the enabled bit in the control field of counter _before_
1273 * accessing the counter control register. If a NMI hits, then it will
1274 * keep the counter running.
1275 */
1276void perf_counter_task_sched_in(struct task_struct *task, int cpu)
1277{
1278 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001279 struct perf_counter_context *ctx = task->perf_counter_ctxp;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001280
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001281 if (likely(!ctx))
1282 return;
Paul Mackerras564c2b22009-05-22 14:27:22 +10001283 if (cpuctx->task_ctx == ctx)
1284 return;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001285 __perf_counter_sched_in(ctx, cpuctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001286 cpuctx->task_ctx = ctx;
1287}
1288
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001289static void perf_counter_cpu_sched_in(struct perf_cpu_context *cpuctx, int cpu)
1290{
1291 struct perf_counter_context *ctx = &cpuctx->ctx;
1292
1293 __perf_counter_sched_in(ctx, cpuctx, cpu);
1294}
1295
Peter Zijlstraa78ac322009-05-25 17:39:05 +02001296#define MAX_INTERRUPTS (~0ULL)
1297
1298static void perf_log_throttle(struct perf_counter *counter, int enable);
Peter Zijlstra26b119b2009-05-20 12:21:20 +02001299static void perf_log_period(struct perf_counter *counter, u64 period);
1300
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001301static void perf_adjust_period(struct perf_counter *counter, u64 events)
1302{
1303 struct hw_perf_counter *hwc = &counter->hw;
1304 u64 period, sample_period;
1305 s64 delta;
1306
1307 events *= hwc->sample_period;
1308 period = div64_u64(events, counter->attr.sample_freq);
1309
1310 delta = (s64)(period - hwc->sample_period);
1311 delta = (delta + 7) / 8; /* low pass filter */
1312
1313 sample_period = hwc->sample_period + delta;
1314
1315 if (!sample_period)
1316 sample_period = 1;
1317
1318 perf_log_period(counter, sample_period);
1319
1320 hwc->sample_period = sample_period;
1321}
1322
1323static void perf_ctx_adjust_freq(struct perf_counter_context *ctx)
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001324{
1325 struct perf_counter *counter;
Peter Zijlstra6a24ed6c2009-06-05 18:01:29 +02001326 struct hw_perf_counter *hwc;
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001327 u64 interrupts, freq;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001328
1329 spin_lock(&ctx->lock);
1330 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1331 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
1332 continue;
1333
Peter Zijlstra6a24ed6c2009-06-05 18:01:29 +02001334 hwc = &counter->hw;
1335
1336 interrupts = hwc->interrupts;
1337 hwc->interrupts = 0;
Peter Zijlstraa78ac322009-05-25 17:39:05 +02001338
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001339 /*
1340 * unthrottle counters on the tick
1341 */
Peter Zijlstraa78ac322009-05-25 17:39:05 +02001342 if (interrupts == MAX_INTERRUPTS) {
1343 perf_log_throttle(counter, 1);
1344 counter->pmu->unthrottle(counter);
Peter Zijlstradf58ab22009-06-11 11:25:05 +02001345 interrupts = 2*sysctl_perf_counter_sample_rate/HZ;
Peter Zijlstraa78ac322009-05-25 17:39:05 +02001346 }
1347
Peter Zijlstra0d486962009-06-02 19:22:16 +02001348 if (!counter->attr.freq || !counter->attr.sample_freq)
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001349 continue;
1350
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001351 /*
1352 * if the specified freq < HZ then we need to skip ticks
1353 */
Peter Zijlstra6a24ed6c2009-06-05 18:01:29 +02001354 if (counter->attr.sample_freq < HZ) {
1355 freq = counter->attr.sample_freq;
1356
1357 hwc->freq_count += freq;
1358 hwc->freq_interrupts += interrupts;
1359
1360 if (hwc->freq_count < HZ)
1361 continue;
1362
1363 interrupts = hwc->freq_interrupts;
1364 hwc->freq_interrupts = 0;
1365 hwc->freq_count -= HZ;
1366 } else
1367 freq = HZ;
1368
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001369 perf_adjust_period(counter, freq * interrupts);
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001370
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001371 /*
1372 * In order to avoid being stalled by an (accidental) huge
1373 * sample period, force reset the sample period if we didn't
1374 * get any events in this freq period.
1375 */
1376 if (!interrupts) {
1377 perf_disable();
1378 counter->pmu->disable(counter);
Paul Mackerras87847b82009-06-13 17:06:50 +10001379 atomic64_set(&hwc->period_left, 0);
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001380 counter->pmu->enable(counter);
1381 perf_enable();
1382 }
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001383 }
1384 spin_unlock(&ctx->lock);
1385}
1386
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001387/*
1388 * Round-robin a context's counters:
1389 */
1390static void rotate_ctx(struct perf_counter_context *ctx)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001391{
Thomas Gleixner0793a612008-12-04 20:12:29 +01001392 struct perf_counter *counter;
1393
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001394 if (!ctx->nr_counters)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001395 return;
1396
Thomas Gleixner0793a612008-12-04 20:12:29 +01001397 spin_lock(&ctx->lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001398 /*
Ingo Molnar04289bb2008-12-11 08:38:42 +01001399 * Rotate the first entry last (works just fine for group counters too):
Thomas Gleixner0793a612008-12-04 20:12:29 +01001400 */
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001401 perf_disable();
Ingo Molnar04289bb2008-12-11 08:38:42 +01001402 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Peter Zijlstra75564232009-03-13 12:21:29 +01001403 list_move_tail(&counter->list_entry, &ctx->counter_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001404 break;
1405 }
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001406 perf_enable();
Thomas Gleixner0793a612008-12-04 20:12:29 +01001407
1408 spin_unlock(&ctx->lock);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001409}
Thomas Gleixner0793a612008-12-04 20:12:29 +01001410
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001411void perf_counter_task_tick(struct task_struct *curr, int cpu)
1412{
Peter Zijlstra7fc23a52009-05-08 18:52:21 +02001413 struct perf_cpu_context *cpuctx;
1414 struct perf_counter_context *ctx;
1415
1416 if (!atomic_read(&nr_counters))
1417 return;
1418
1419 cpuctx = &per_cpu(perf_cpu_context, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001420 ctx = curr->perf_counter_ctxp;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001421
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001422 perf_ctx_adjust_freq(&cpuctx->ctx);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001423 if (ctx)
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001424 perf_ctx_adjust_freq(ctx);
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001425
Ingo Molnarb82914c2009-05-04 18:54:32 +02001426 perf_counter_cpu_sched_out(cpuctx);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001427 if (ctx)
1428 __perf_counter_task_sched_out(ctx);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001429
Ingo Molnarb82914c2009-05-04 18:54:32 +02001430 rotate_ctx(&cpuctx->ctx);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001431 if (ctx)
1432 rotate_ctx(ctx);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001433
Ingo Molnarb82914c2009-05-04 18:54:32 +02001434 perf_counter_cpu_sched_in(cpuctx, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001435 if (ctx)
1436 perf_counter_task_sched_in(curr, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001437}
1438
1439/*
Paul Mackerras57e79862009-06-30 16:07:19 +10001440 * Enable all of a task's counters that have been marked enable-on-exec.
1441 * This expects task == current.
1442 */
1443static void perf_counter_enable_on_exec(struct task_struct *task)
1444{
1445 struct perf_counter_context *ctx;
1446 struct perf_counter *counter;
1447 unsigned long flags;
1448 int enabled = 0;
1449
1450 local_irq_save(flags);
1451 ctx = task->perf_counter_ctxp;
1452 if (!ctx || !ctx->nr_counters)
1453 goto out;
1454
1455 __perf_counter_task_sched_out(ctx);
1456
1457 spin_lock(&ctx->lock);
1458
1459 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1460 if (!counter->attr.enable_on_exec)
1461 continue;
1462 counter->attr.enable_on_exec = 0;
1463 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
1464 continue;
1465 counter->state = PERF_COUNTER_STATE_INACTIVE;
1466 counter->tstamp_enabled =
1467 ctx->time - counter->total_time_enabled;
1468 enabled = 1;
1469 }
1470
1471 /*
1472 * Unclone this context if we enabled any counter.
1473 */
Peter Zijlstra71a851b2009-07-10 09:06:56 +02001474 if (enabled)
1475 unclone_ctx(ctx);
Paul Mackerras57e79862009-06-30 16:07:19 +10001476
1477 spin_unlock(&ctx->lock);
1478
1479 perf_counter_task_sched_in(task, smp_processor_id());
1480 out:
1481 local_irq_restore(flags);
1482}
1483
1484/*
Thomas Gleixner0793a612008-12-04 20:12:29 +01001485 * Cross CPU call to read the hardware counter
1486 */
Peter Zijlstrabfbd3382009-06-24 21:11:59 +02001487static void __perf_counter_read(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001488{
Ingo Molnar621a01e2008-12-11 12:46:46 +01001489 struct perf_counter *counter = info;
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001490 struct perf_counter_context *ctx = counter->ctx;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001491 unsigned long flags;
Ingo Molnar621a01e2008-12-11 12:46:46 +01001492
Peter Zijlstra849691a2009-04-06 11:45:12 +02001493 local_irq_save(flags);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001494 if (ctx->is_active)
Peter Zijlstra4af49982009-04-06 11:45:10 +02001495 update_context_time(ctx);
Robert Richter4aeb0b42009-04-29 12:47:03 +02001496 counter->pmu->read(counter);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001497 update_counter_times(counter);
Peter Zijlstra849691a2009-04-06 11:45:12 +02001498 local_irq_restore(flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001499}
1500
Ingo Molnar04289bb2008-12-11 08:38:42 +01001501static u64 perf_counter_read(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001502{
1503 /*
1504 * If counter is enabled and currently active on a CPU, update the
1505 * value in the counter structure:
1506 */
Ingo Molnar6a930702008-12-11 15:17:03 +01001507 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
Thomas Gleixner0793a612008-12-04 20:12:29 +01001508 smp_call_function_single(counter->oncpu,
Peter Zijlstrabfbd3382009-06-24 21:11:59 +02001509 __perf_counter_read, counter, 1);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001510 } else if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
1511 update_counter_times(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001512 }
1513
Ingo Molnaree060942008-12-13 09:00:03 +01001514 return atomic64_read(&counter->count);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001515}
1516
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001517/*
1518 * Initialize the perf_counter context in a task_struct:
1519 */
1520static void
1521__perf_counter_init_context(struct perf_counter_context *ctx,
1522 struct task_struct *task)
1523{
1524 memset(ctx, 0, sizeof(*ctx));
1525 spin_lock_init(&ctx->lock);
1526 mutex_init(&ctx->mutex);
1527 INIT_LIST_HEAD(&ctx->counter_list);
1528 INIT_LIST_HEAD(&ctx->event_list);
1529 atomic_set(&ctx->refcount, 1);
1530 ctx->task = task;
1531}
1532
Thomas Gleixner0793a612008-12-04 20:12:29 +01001533static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
1534{
Ingo Molnar22a4f652009-06-01 10:13:37 +02001535 struct perf_counter_context *ctx;
1536 struct perf_cpu_context *cpuctx;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001537 struct task_struct *task;
Paul Mackerras25346b932009-06-01 17:48:12 +10001538 unsigned long flags;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001539 int err;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001540
1541 /*
1542 * If cpu is not a wildcard then this is a percpu counter:
1543 */
1544 if (cpu != -1) {
1545 /* Must be root to operate on a CPU counter: */
Peter Zijlstra07647712009-06-11 11:18:36 +02001546 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
Thomas Gleixner0793a612008-12-04 20:12:29 +01001547 return ERR_PTR(-EACCES);
1548
1549 if (cpu < 0 || cpu > num_possible_cpus())
1550 return ERR_PTR(-EINVAL);
1551
1552 /*
1553 * We could be clever and allow to attach a counter to an
1554 * offline CPU and activate it when the CPU comes up, but
1555 * that's for later.
1556 */
1557 if (!cpu_isset(cpu, cpu_online_map))
1558 return ERR_PTR(-ENODEV);
1559
1560 cpuctx = &per_cpu(perf_cpu_context, cpu);
1561 ctx = &cpuctx->ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001562 get_ctx(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001563
Thomas Gleixner0793a612008-12-04 20:12:29 +01001564 return ctx;
1565 }
1566
1567 rcu_read_lock();
1568 if (!pid)
1569 task = current;
1570 else
1571 task = find_task_by_vpid(pid);
1572 if (task)
1573 get_task_struct(task);
1574 rcu_read_unlock();
1575
1576 if (!task)
1577 return ERR_PTR(-ESRCH);
1578
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001579 /*
1580 * Can't attach counters to a dying task.
1581 */
1582 err = -ESRCH;
1583 if (task->flags & PF_EXITING)
1584 goto errout;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001585
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001586 /* Reuse ptrace permission checks for now. */
1587 err = -EACCES;
1588 if (!ptrace_may_access(task, PTRACE_MODE_READ))
1589 goto errout;
1590
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001591 retry:
Paul Mackerras25346b932009-06-01 17:48:12 +10001592 ctx = perf_lock_task_context(task, &flags);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001593 if (ctx) {
Peter Zijlstra71a851b2009-07-10 09:06:56 +02001594 unclone_ctx(ctx);
Paul Mackerras25346b932009-06-01 17:48:12 +10001595 spin_unlock_irqrestore(&ctx->lock, flags);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001596 }
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001597
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001598 if (!ctx) {
1599 ctx = kmalloc(sizeof(struct perf_counter_context), GFP_KERNEL);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001600 err = -ENOMEM;
1601 if (!ctx)
1602 goto errout;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001603 __perf_counter_init_context(ctx, task);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001604 get_ctx(ctx);
1605 if (cmpxchg(&task->perf_counter_ctxp, NULL, ctx)) {
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001606 /*
1607 * We raced with some other task; use
1608 * the context they set.
1609 */
1610 kfree(ctx);
Paul Mackerras25346b932009-06-01 17:48:12 +10001611 goto retry;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001612 }
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001613 get_task_struct(task);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001614 }
1615
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001616 put_task_struct(task);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001617 return ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001618
1619 errout:
1620 put_task_struct(task);
1621 return ERR_PTR(err);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001622}
1623
Peter Zijlstra592903c2009-03-13 12:21:36 +01001624static void free_counter_rcu(struct rcu_head *head)
1625{
1626 struct perf_counter *counter;
1627
1628 counter = container_of(head, struct perf_counter, rcu_head);
Peter Zijlstra709e50c2009-06-02 14:13:15 +02001629 if (counter->ns)
1630 put_pid_ns(counter->ns);
Peter Zijlstra592903c2009-03-13 12:21:36 +01001631 kfree(counter);
1632}
1633
Peter Zijlstra925d5192009-03-30 19:07:02 +02001634static void perf_pending_sync(struct perf_counter *counter);
1635
Peter Zijlstraf1600952009-03-19 20:26:16 +01001636static void free_counter(struct perf_counter *counter)
1637{
Peter Zijlstra925d5192009-03-30 19:07:02 +02001638 perf_pending_sync(counter);
1639
Peter Zijlstraf3440112009-06-22 13:58:35 +02001640 if (!counter->parent) {
1641 atomic_dec(&nr_counters);
1642 if (counter->attr.mmap)
1643 atomic_dec(&nr_mmap_counters);
1644 if (counter->attr.comm)
1645 atomic_dec(&nr_comm_counters);
1646 }
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02001647
Peter Zijlstrae077df42009-03-19 20:26:17 +01001648 if (counter->destroy)
1649 counter->destroy(counter);
1650
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001651 put_ctx(counter->ctx);
Peter Zijlstraf1600952009-03-19 20:26:16 +01001652 call_rcu(&counter->rcu_head, free_counter_rcu);
1653}
1654
Thomas Gleixner0793a612008-12-04 20:12:29 +01001655/*
1656 * Called when the last reference to the file is gone.
1657 */
1658static int perf_release(struct inode *inode, struct file *file)
1659{
1660 struct perf_counter *counter = file->private_data;
1661 struct perf_counter_context *ctx = counter->ctx;
1662
1663 file->private_data = NULL;
1664
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001665 WARN_ON_ONCE(ctx->parent_ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001666 mutex_lock(&ctx->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01001667 perf_counter_remove_from_context(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001668 mutex_unlock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001669
Peter Zijlstra082ff5a2009-05-23 18:29:00 +02001670 mutex_lock(&counter->owner->perf_counter_mutex);
1671 list_del_init(&counter->owner_entry);
1672 mutex_unlock(&counter->owner->perf_counter_mutex);
1673 put_task_struct(counter->owner);
1674
Peter Zijlstraf1600952009-03-19 20:26:16 +01001675 free_counter(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001676
1677 return 0;
1678}
1679
1680/*
1681 * Read the performance counter - simple non blocking version for now
1682 */
1683static ssize_t
1684perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
1685{
Marti Raudseppd5e8da62009-06-13 02:35:01 +03001686 u64 values[4];
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001687 int n;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001688
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001689 /*
1690 * Return end-of-file for a read on a counter that is in
1691 * error state (i.e. because it was pinned but it couldn't be
1692 * scheduled on to the CPU at some point).
1693 */
1694 if (counter->state == PERF_COUNTER_STATE_ERROR)
1695 return 0;
1696
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001697 WARN_ON_ONCE(counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001698 mutex_lock(&counter->child_mutex);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001699 values[0] = perf_counter_read(counter);
1700 n = 1;
Peter Zijlstra0d486962009-06-02 19:22:16 +02001701 if (counter->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001702 values[n++] = counter->total_time_enabled +
1703 atomic64_read(&counter->child_total_time_enabled);
Peter Zijlstra0d486962009-06-02 19:22:16 +02001704 if (counter->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001705 values[n++] = counter->total_time_running +
1706 atomic64_read(&counter->child_total_time_running);
Peter Zijlstra0d486962009-06-02 19:22:16 +02001707 if (counter->attr.read_format & PERF_FORMAT_ID)
Peter Zijlstra8e5799b2009-06-02 15:08:15 +02001708 values[n++] = counter->id;
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001709 mutex_unlock(&counter->child_mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001710
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001711 if (count < n * sizeof(u64))
1712 return -EINVAL;
1713 count = n * sizeof(u64);
1714
1715 if (copy_to_user(buf, values, count))
1716 return -EFAULT;
1717
1718 return count;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001719}
1720
1721static ssize_t
Thomas Gleixner0793a612008-12-04 20:12:29 +01001722perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
1723{
1724 struct perf_counter *counter = file->private_data;
1725
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001726 return perf_read_hw(counter, buf, count);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001727}
1728
1729static unsigned int perf_poll(struct file *file, poll_table *wait)
1730{
1731 struct perf_counter *counter = file->private_data;
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001732 struct perf_mmap_data *data;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001733 unsigned int events = POLL_HUP;
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001734
1735 rcu_read_lock();
1736 data = rcu_dereference(counter->data);
1737 if (data)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001738 events = atomic_xchg(&data->poll, 0);
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001739 rcu_read_unlock();
Thomas Gleixner0793a612008-12-04 20:12:29 +01001740
1741 poll_wait(file, &counter->waitq, wait);
1742
Thomas Gleixner0793a612008-12-04 20:12:29 +01001743 return events;
1744}
1745
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001746static void perf_counter_reset(struct perf_counter *counter)
1747{
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001748 (void)perf_counter_read(counter);
Paul Mackerras615a3f12009-05-11 15:50:21 +10001749 atomic64_set(&counter->count, 0);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001750 perf_counter_update_userpage(counter);
1751}
1752
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001753/*
1754 * Holding the top-level counter's child_mutex means that any
1755 * descendant process that has inherited this counter will block
1756 * in sync_child_counter if it goes to exit, thus satisfying the
1757 * task existence requirements of perf_counter_enable/disable.
1758 */
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001759static void perf_counter_for_each_child(struct perf_counter *counter,
1760 void (*func)(struct perf_counter *))
1761{
1762 struct perf_counter *child;
1763
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001764 WARN_ON_ONCE(counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001765 mutex_lock(&counter->child_mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001766 func(counter);
1767 list_for_each_entry(child, &counter->child_list, child_list)
1768 func(child);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001769 mutex_unlock(&counter->child_mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001770}
1771
1772static void perf_counter_for_each(struct perf_counter *counter,
1773 void (*func)(struct perf_counter *))
1774{
Peter Zijlstra75f937f2009-06-15 15:05:12 +02001775 struct perf_counter_context *ctx = counter->ctx;
1776 struct perf_counter *sibling;
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001777
Peter Zijlstra75f937f2009-06-15 15:05:12 +02001778 WARN_ON_ONCE(ctx->parent_ctx);
1779 mutex_lock(&ctx->mutex);
1780 counter = counter->group_leader;
1781
1782 perf_counter_for_each_child(counter, func);
1783 func(counter);
1784 list_for_each_entry(sibling, &counter->sibling_list, list_entry)
1785 perf_counter_for_each_child(counter, func);
1786 mutex_unlock(&ctx->mutex);
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001787}
1788
Peter Zijlstra08247e32009-06-02 16:46:57 +02001789static int perf_counter_period(struct perf_counter *counter, u64 __user *arg)
1790{
1791 struct perf_counter_context *ctx = counter->ctx;
1792 unsigned long size;
1793 int ret = 0;
1794 u64 value;
1795
Peter Zijlstra0d486962009-06-02 19:22:16 +02001796 if (!counter->attr.sample_period)
Peter Zijlstra08247e32009-06-02 16:46:57 +02001797 return -EINVAL;
1798
1799 size = copy_from_user(&value, arg, sizeof(value));
1800 if (size != sizeof(value))
1801 return -EFAULT;
1802
1803 if (!value)
1804 return -EINVAL;
1805
1806 spin_lock_irq(&ctx->lock);
Peter Zijlstra0d486962009-06-02 19:22:16 +02001807 if (counter->attr.freq) {
Peter Zijlstradf58ab22009-06-11 11:25:05 +02001808 if (value > sysctl_perf_counter_sample_rate) {
Peter Zijlstra08247e32009-06-02 16:46:57 +02001809 ret = -EINVAL;
1810 goto unlock;
1811 }
1812
Peter Zijlstra0d486962009-06-02 19:22:16 +02001813 counter->attr.sample_freq = value;
Peter Zijlstra08247e32009-06-02 16:46:57 +02001814 } else {
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001815 perf_log_period(counter, value);
1816
Peter Zijlstra0d486962009-06-02 19:22:16 +02001817 counter->attr.sample_period = value;
Peter Zijlstra08247e32009-06-02 16:46:57 +02001818 counter->hw.sample_period = value;
Peter Zijlstra08247e32009-06-02 16:46:57 +02001819 }
1820unlock:
1821 spin_unlock_irq(&ctx->lock);
1822
1823 return ret;
1824}
1825
Paul Mackerrasd859e292009-01-17 18:10:22 +11001826static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1827{
1828 struct perf_counter *counter = file->private_data;
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001829 void (*func)(struct perf_counter *);
1830 u32 flags = arg;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001831
1832 switch (cmd) {
1833 case PERF_COUNTER_IOC_ENABLE:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001834 func = perf_counter_enable;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001835 break;
1836 case PERF_COUNTER_IOC_DISABLE:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001837 func = perf_counter_disable;
Peter Zijlstra79f14642009-04-06 11:45:07 +02001838 break;
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001839 case PERF_COUNTER_IOC_RESET:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001840 func = perf_counter_reset;
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001841 break;
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001842
1843 case PERF_COUNTER_IOC_REFRESH:
1844 return perf_counter_refresh(counter, arg);
Peter Zijlstra08247e32009-06-02 16:46:57 +02001845
1846 case PERF_COUNTER_IOC_PERIOD:
1847 return perf_counter_period(counter, (u64 __user *)arg);
1848
Paul Mackerrasd859e292009-01-17 18:10:22 +11001849 default:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001850 return -ENOTTY;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001851 }
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001852
1853 if (flags & PERF_IOC_FLAG_GROUP)
1854 perf_counter_for_each(counter, func);
1855 else
1856 perf_counter_for_each_child(counter, func);
1857
1858 return 0;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001859}
1860
Peter Zijlstra771d7cd2009-05-25 14:45:26 +02001861int perf_counter_task_enable(void)
1862{
1863 struct perf_counter *counter;
1864
1865 mutex_lock(&current->perf_counter_mutex);
1866 list_for_each_entry(counter, &current->perf_counter_list, owner_entry)
1867 perf_counter_for_each_child(counter, perf_counter_enable);
1868 mutex_unlock(&current->perf_counter_mutex);
1869
1870 return 0;
1871}
1872
1873int perf_counter_task_disable(void)
1874{
1875 struct perf_counter *counter;
1876
1877 mutex_lock(&current->perf_counter_mutex);
1878 list_for_each_entry(counter, &current->perf_counter_list, owner_entry)
1879 perf_counter_for_each_child(counter, perf_counter_disable);
1880 mutex_unlock(&current->perf_counter_mutex);
1881
1882 return 0;
1883}
1884
Peter Zijlstra194002b2009-06-22 16:35:24 +02001885static int perf_counter_index(struct perf_counter *counter)
1886{
1887 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
1888 return 0;
1889
1890 return counter->hw.idx + 1 - PERF_COUNTER_INDEX_OFFSET;
1891}
1892
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001893/*
1894 * Callers need to ensure there can be no nesting of this function, otherwise
1895 * the seqlock logic goes bad. We can not serialize this because the arch
1896 * code calls this from NMI context.
1897 */
1898void perf_counter_update_userpage(struct perf_counter *counter)
Paul Mackerras37d81822009-03-23 18:22:08 +01001899{
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001900 struct perf_counter_mmap_page *userpg;
Ingo Molnar22a4f652009-06-01 10:13:37 +02001901 struct perf_mmap_data *data;
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001902
1903 rcu_read_lock();
1904 data = rcu_dereference(counter->data);
1905 if (!data)
1906 goto unlock;
1907
1908 userpg = data->user_page;
Paul Mackerras37d81822009-03-23 18:22:08 +01001909
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001910 /*
1911 * Disable preemption so as to not let the corresponding user-space
1912 * spin too long if we get preempted.
1913 */
1914 preempt_disable();
Paul Mackerras37d81822009-03-23 18:22:08 +01001915 ++userpg->lock;
Peter Zijlstra92f22a32009-04-02 11:12:04 +02001916 barrier();
Peter Zijlstra194002b2009-06-22 16:35:24 +02001917 userpg->index = perf_counter_index(counter);
Paul Mackerras37d81822009-03-23 18:22:08 +01001918 userpg->offset = atomic64_read(&counter->count);
1919 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
1920 userpg->offset -= atomic64_read(&counter->hw.prev_count);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001921
Peter Zijlstra7f8b4e42009-06-22 14:34:35 +02001922 userpg->time_enabled = counter->total_time_enabled +
1923 atomic64_read(&counter->child_total_time_enabled);
1924
1925 userpg->time_running = counter->total_time_running +
1926 atomic64_read(&counter->child_total_time_running);
1927
Peter Zijlstra92f22a32009-04-02 11:12:04 +02001928 barrier();
Paul Mackerras37d81822009-03-23 18:22:08 +01001929 ++userpg->lock;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001930 preempt_enable();
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001931unlock:
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001932 rcu_read_unlock();
Paul Mackerras37d81822009-03-23 18:22:08 +01001933}
1934
1935static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1936{
1937 struct perf_counter *counter = vma->vm_file->private_data;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001938 struct perf_mmap_data *data;
1939 int ret = VM_FAULT_SIGBUS;
Paul Mackerras37d81822009-03-23 18:22:08 +01001940
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01001941 if (vmf->flags & FAULT_FLAG_MKWRITE) {
1942 if (vmf->pgoff == 0)
1943 ret = 0;
1944 return ret;
1945 }
1946
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001947 rcu_read_lock();
1948 data = rcu_dereference(counter->data);
1949 if (!data)
1950 goto unlock;
Paul Mackerras37d81822009-03-23 18:22:08 +01001951
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001952 if (vmf->pgoff == 0) {
1953 vmf->page = virt_to_page(data->user_page);
1954 } else {
1955 int nr = vmf->pgoff - 1;
1956
1957 if ((unsigned)nr > data->nr_pages)
1958 goto unlock;
1959
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01001960 if (vmf->flags & FAULT_FLAG_WRITE)
1961 goto unlock;
1962
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001963 vmf->page = virt_to_page(data->data_pages[nr]);
1964 }
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01001965
Paul Mackerras37d81822009-03-23 18:22:08 +01001966 get_page(vmf->page);
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01001967 vmf->page->mapping = vma->vm_file->f_mapping;
1968 vmf->page->index = vmf->pgoff;
1969
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001970 ret = 0;
1971unlock:
1972 rcu_read_unlock();
1973
1974 return ret;
1975}
1976
1977static int perf_mmap_data_alloc(struct perf_counter *counter, int nr_pages)
1978{
1979 struct perf_mmap_data *data;
1980 unsigned long size;
1981 int i;
1982
1983 WARN_ON(atomic_read(&counter->mmap_count));
1984
1985 size = sizeof(struct perf_mmap_data);
1986 size += nr_pages * sizeof(void *);
1987
1988 data = kzalloc(size, GFP_KERNEL);
1989 if (!data)
1990 goto fail;
1991
1992 data->user_page = (void *)get_zeroed_page(GFP_KERNEL);
1993 if (!data->user_page)
1994 goto fail_user_page;
1995
1996 for (i = 0; i < nr_pages; i++) {
1997 data->data_pages[i] = (void *)get_zeroed_page(GFP_KERNEL);
1998 if (!data->data_pages[i])
1999 goto fail_data_pages;
2000 }
2001
2002 data->nr_pages = nr_pages;
Peter Zijlstra22c15582009-05-05 17:50:25 +02002003 atomic_set(&data->lock, -1);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002004
2005 rcu_assign_pointer(counter->data, data);
2006
Paul Mackerras37d81822009-03-23 18:22:08 +01002007 return 0;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002008
2009fail_data_pages:
2010 for (i--; i >= 0; i--)
2011 free_page((unsigned long)data->data_pages[i]);
2012
2013 free_page((unsigned long)data->user_page);
2014
2015fail_user_page:
2016 kfree(data);
2017
2018fail:
2019 return -ENOMEM;
2020}
2021
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002022static void perf_mmap_free_page(unsigned long addr)
2023{
2024 struct page *page = virt_to_page(addr);
2025
2026 page->mapping = NULL;
2027 __free_page(page);
2028}
2029
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002030static void __perf_mmap_data_free(struct rcu_head *rcu_head)
2031{
Ingo Molnar22a4f652009-06-01 10:13:37 +02002032 struct perf_mmap_data *data;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002033 int i;
2034
Ingo Molnar22a4f652009-06-01 10:13:37 +02002035 data = container_of(rcu_head, struct perf_mmap_data, rcu_head);
2036
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002037 perf_mmap_free_page((unsigned long)data->user_page);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002038 for (i = 0; i < data->nr_pages; i++)
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002039 perf_mmap_free_page((unsigned long)data->data_pages[i]);
2040
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002041 kfree(data);
2042}
2043
2044static void perf_mmap_data_free(struct perf_counter *counter)
2045{
2046 struct perf_mmap_data *data = counter->data;
2047
2048 WARN_ON(atomic_read(&counter->mmap_count));
2049
2050 rcu_assign_pointer(counter->data, NULL);
2051 call_rcu(&data->rcu_head, __perf_mmap_data_free);
2052}
2053
2054static void perf_mmap_open(struct vm_area_struct *vma)
2055{
2056 struct perf_counter *counter = vma->vm_file->private_data;
2057
2058 atomic_inc(&counter->mmap_count);
2059}
2060
2061static void perf_mmap_close(struct vm_area_struct *vma)
2062{
2063 struct perf_counter *counter = vma->vm_file->private_data;
2064
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10002065 WARN_ON_ONCE(counter->ctx->parent_ctx);
Ingo Molnar22a4f652009-06-01 10:13:37 +02002066 if (atomic_dec_and_mutex_lock(&counter->mmap_count, &counter->mmap_mutex)) {
Peter Zijlstra789f90f2009-05-15 15:19:27 +02002067 struct user_struct *user = current_user();
2068
2069 atomic_long_sub(counter->data->nr_pages + 1, &user->locked_vm);
Peter Zijlstrac5078f72009-05-05 17:50:24 +02002070 vma->vm_mm->locked_vm -= counter->data->nr_locked;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002071 perf_mmap_data_free(counter);
2072 mutex_unlock(&counter->mmap_mutex);
2073 }
Paul Mackerras37d81822009-03-23 18:22:08 +01002074}
2075
2076static struct vm_operations_struct perf_mmap_vmops = {
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002077 .open = perf_mmap_open,
2078 .close = perf_mmap_close,
2079 .fault = perf_mmap_fault,
2080 .page_mkwrite = perf_mmap_fault,
Paul Mackerras37d81822009-03-23 18:22:08 +01002081};
2082
2083static int perf_mmap(struct file *file, struct vm_area_struct *vma)
2084{
2085 struct perf_counter *counter = file->private_data;
Ingo Molnar22a4f652009-06-01 10:13:37 +02002086 unsigned long user_locked, user_lock_limit;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02002087 struct user_struct *user = current_user();
Ingo Molnar22a4f652009-06-01 10:13:37 +02002088 unsigned long locked, lock_limit;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002089 unsigned long vma_size;
2090 unsigned long nr_pages;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02002091 long user_extra, extra;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002092 int ret = 0;
Paul Mackerras37d81822009-03-23 18:22:08 +01002093
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002094 if (!(vma->vm_flags & VM_SHARED))
Paul Mackerras37d81822009-03-23 18:22:08 +01002095 return -EINVAL;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002096
2097 vma_size = vma->vm_end - vma->vm_start;
2098 nr_pages = (vma_size / PAGE_SIZE) - 1;
2099
Peter Zijlstra7730d862009-03-25 12:48:31 +01002100 /*
2101 * If we have data pages ensure they're a power-of-two number, so we
2102 * can do bitmasks instead of modulo.
2103 */
2104 if (nr_pages != 0 && !is_power_of_2(nr_pages))
Paul Mackerras37d81822009-03-23 18:22:08 +01002105 return -EINVAL;
2106
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002107 if (vma_size != PAGE_SIZE * (1 + nr_pages))
Paul Mackerras37d81822009-03-23 18:22:08 +01002108 return -EINVAL;
2109
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002110 if (vma->vm_pgoff != 0)
2111 return -EINVAL;
Paul Mackerras37d81822009-03-23 18:22:08 +01002112
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10002113 WARN_ON_ONCE(counter->ctx->parent_ctx);
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02002114 mutex_lock(&counter->mmap_mutex);
2115 if (atomic_inc_not_zero(&counter->mmap_count)) {
2116 if (nr_pages != counter->data->nr_pages)
2117 ret = -EINVAL;
2118 goto unlock;
2119 }
2120
Peter Zijlstra789f90f2009-05-15 15:19:27 +02002121 user_extra = nr_pages + 1;
2122 user_lock_limit = sysctl_perf_counter_mlock >> (PAGE_SHIFT - 10);
Ingo Molnara3862d32009-05-24 09:02:37 +02002123
2124 /*
2125 * Increase the limit linearly with more CPUs:
2126 */
2127 user_lock_limit *= num_online_cpus();
2128
Peter Zijlstra789f90f2009-05-15 15:19:27 +02002129 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
Peter Zijlstrac5078f72009-05-05 17:50:24 +02002130
Peter Zijlstra789f90f2009-05-15 15:19:27 +02002131 extra = 0;
2132 if (user_locked > user_lock_limit)
2133 extra = user_locked - user_lock_limit;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002134
2135 lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
2136 lock_limit >>= PAGE_SHIFT;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02002137 locked = vma->vm_mm->locked_vm + extra;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002138
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02002139 if ((locked > lock_limit) && !capable(CAP_IPC_LOCK)) {
2140 ret = -EPERM;
2141 goto unlock;
2142 }
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002143
2144 WARN_ON(counter->data);
2145 ret = perf_mmap_data_alloc(counter, nr_pages);
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02002146 if (ret)
2147 goto unlock;
2148
2149 atomic_set(&counter->mmap_count, 1);
Peter Zijlstra789f90f2009-05-15 15:19:27 +02002150 atomic_long_add(user_extra, &user->locked_vm);
Peter Zijlstrac5078f72009-05-05 17:50:24 +02002151 vma->vm_mm->locked_vm += extra;
2152 counter->data->nr_locked = extra;
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002153 if (vma->vm_flags & VM_WRITE)
2154 counter->data->writable = 1;
2155
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02002156unlock:
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002157 mutex_unlock(&counter->mmap_mutex);
Paul Mackerras37d81822009-03-23 18:22:08 +01002158
Paul Mackerras37d81822009-03-23 18:22:08 +01002159 vma->vm_flags |= VM_RESERVED;
2160 vma->vm_ops = &perf_mmap_vmops;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002161
2162 return ret;
Paul Mackerras37d81822009-03-23 18:22:08 +01002163}
2164
Peter Zijlstra3c446b3d2009-04-06 11:45:01 +02002165static int perf_fasync(int fd, struct file *filp, int on)
2166{
Peter Zijlstra3c446b3d2009-04-06 11:45:01 +02002167 struct inode *inode = filp->f_path.dentry->d_inode;
Ingo Molnar22a4f652009-06-01 10:13:37 +02002168 struct perf_counter *counter = filp->private_data;
Peter Zijlstra3c446b3d2009-04-06 11:45:01 +02002169 int retval;
2170
2171 mutex_lock(&inode->i_mutex);
2172 retval = fasync_helper(fd, filp, on, &counter->fasync);
2173 mutex_unlock(&inode->i_mutex);
2174
2175 if (retval < 0)
2176 return retval;
2177
2178 return 0;
2179}
2180
Thomas Gleixner0793a612008-12-04 20:12:29 +01002181static const struct file_operations perf_fops = {
2182 .release = perf_release,
2183 .read = perf_read,
2184 .poll = perf_poll,
Paul Mackerrasd859e292009-01-17 18:10:22 +11002185 .unlocked_ioctl = perf_ioctl,
2186 .compat_ioctl = perf_ioctl,
Paul Mackerras37d81822009-03-23 18:22:08 +01002187 .mmap = perf_mmap,
Peter Zijlstra3c446b3d2009-04-06 11:45:01 +02002188 .fasync = perf_fasync,
Thomas Gleixner0793a612008-12-04 20:12:29 +01002189};
2190
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002191/*
Peter Zijlstra925d5192009-03-30 19:07:02 +02002192 * Perf counter wakeup
2193 *
2194 * If there's data, ensure we set the poll() state and publish everything
2195 * to user-space before waking everybody up.
2196 */
2197
2198void perf_counter_wakeup(struct perf_counter *counter)
2199{
Peter Zijlstra925d5192009-03-30 19:07:02 +02002200 wake_up_all(&counter->waitq);
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002201
2202 if (counter->pending_kill) {
2203 kill_fasync(&counter->fasync, SIGIO, counter->pending_kill);
2204 counter->pending_kill = 0;
2205 }
Peter Zijlstra925d5192009-03-30 19:07:02 +02002206}
2207
2208/*
2209 * Pending wakeups
2210 *
2211 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
2212 *
2213 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
2214 * single linked list and use cmpxchg() to add entries lockless.
2215 */
2216
Peter Zijlstra79f14642009-04-06 11:45:07 +02002217static void perf_pending_counter(struct perf_pending_entry *entry)
2218{
2219 struct perf_counter *counter = container_of(entry,
2220 struct perf_counter, pending);
2221
2222 if (counter->pending_disable) {
2223 counter->pending_disable = 0;
2224 perf_counter_disable(counter);
2225 }
2226
2227 if (counter->pending_wakeup) {
2228 counter->pending_wakeup = 0;
2229 perf_counter_wakeup(counter);
2230 }
2231}
2232
Peter Zijlstra671dec52009-04-06 11:45:02 +02002233#define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
Peter Zijlstra925d5192009-03-30 19:07:02 +02002234
Peter Zijlstra671dec52009-04-06 11:45:02 +02002235static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = {
Peter Zijlstra925d5192009-03-30 19:07:02 +02002236 PENDING_TAIL,
2237};
2238
Peter Zijlstra671dec52009-04-06 11:45:02 +02002239static void perf_pending_queue(struct perf_pending_entry *entry,
2240 void (*func)(struct perf_pending_entry *))
Peter Zijlstra925d5192009-03-30 19:07:02 +02002241{
Peter Zijlstra671dec52009-04-06 11:45:02 +02002242 struct perf_pending_entry **head;
Peter Zijlstra925d5192009-03-30 19:07:02 +02002243
Peter Zijlstra671dec52009-04-06 11:45:02 +02002244 if (cmpxchg(&entry->next, NULL, PENDING_TAIL) != NULL)
Peter Zijlstra925d5192009-03-30 19:07:02 +02002245 return;
2246
Peter Zijlstra671dec52009-04-06 11:45:02 +02002247 entry->func = func;
2248
2249 head = &get_cpu_var(perf_pending_head);
Peter Zijlstra925d5192009-03-30 19:07:02 +02002250
2251 do {
Peter Zijlstra671dec52009-04-06 11:45:02 +02002252 entry->next = *head;
2253 } while (cmpxchg(head, entry->next, entry) != entry->next);
Peter Zijlstra925d5192009-03-30 19:07:02 +02002254
2255 set_perf_counter_pending();
2256
Peter Zijlstra671dec52009-04-06 11:45:02 +02002257 put_cpu_var(perf_pending_head);
Peter Zijlstra925d5192009-03-30 19:07:02 +02002258}
2259
2260static int __perf_pending_run(void)
2261{
Peter Zijlstra671dec52009-04-06 11:45:02 +02002262 struct perf_pending_entry *list;
Peter Zijlstra925d5192009-03-30 19:07:02 +02002263 int nr = 0;
2264
Peter Zijlstra671dec52009-04-06 11:45:02 +02002265 list = xchg(&__get_cpu_var(perf_pending_head), PENDING_TAIL);
Peter Zijlstra925d5192009-03-30 19:07:02 +02002266 while (list != PENDING_TAIL) {
Peter Zijlstra671dec52009-04-06 11:45:02 +02002267 void (*func)(struct perf_pending_entry *);
2268 struct perf_pending_entry *entry = list;
Peter Zijlstra925d5192009-03-30 19:07:02 +02002269
2270 list = list->next;
2271
Peter Zijlstra671dec52009-04-06 11:45:02 +02002272 func = entry->func;
2273 entry->next = NULL;
Peter Zijlstra925d5192009-03-30 19:07:02 +02002274 /*
2275 * Ensure we observe the unqueue before we issue the wakeup,
2276 * so that we won't be waiting forever.
2277 * -- see perf_not_pending().
2278 */
2279 smp_wmb();
2280
Peter Zijlstra671dec52009-04-06 11:45:02 +02002281 func(entry);
Peter Zijlstra925d5192009-03-30 19:07:02 +02002282 nr++;
2283 }
2284
2285 return nr;
2286}
2287
2288static inline int perf_not_pending(struct perf_counter *counter)
2289{
2290 /*
2291 * If we flush on whatever cpu we run, there is a chance we don't
2292 * need to wait.
2293 */
2294 get_cpu();
2295 __perf_pending_run();
2296 put_cpu();
2297
2298 /*
2299 * Ensure we see the proper queue state before going to sleep
2300 * so that we do not miss the wakeup. -- see perf_pending_handle()
2301 */
2302 smp_rmb();
Peter Zijlstra671dec52009-04-06 11:45:02 +02002303 return counter->pending.next == NULL;
Peter Zijlstra925d5192009-03-30 19:07:02 +02002304}
2305
2306static void perf_pending_sync(struct perf_counter *counter)
2307{
2308 wait_event(counter->waitq, perf_not_pending(counter));
2309}
2310
2311void perf_counter_do_pending(void)
2312{
2313 __perf_pending_run();
2314}
2315
2316/*
Peter Zijlstra394ee072009-03-30 19:07:14 +02002317 * Callchain support -- arch specific
2318 */
2319
Peter Zijlstra9c03d882009-04-06 11:45:00 +02002320__weak struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
Peter Zijlstra394ee072009-03-30 19:07:14 +02002321{
2322 return NULL;
2323}
2324
2325/*
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002326 * Output
2327 */
2328
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002329struct perf_output_handle {
2330 struct perf_counter *counter;
2331 struct perf_mmap_data *data;
Peter Zijlstra8e3747c2009-06-02 16:16:02 +02002332 unsigned long head;
2333 unsigned long offset;
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002334 int nmi;
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002335 int sample;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002336 int locked;
2337 unsigned long flags;
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002338};
2339
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002340static bool perf_output_space(struct perf_mmap_data *data,
2341 unsigned int offset, unsigned int head)
2342{
2343 unsigned long tail;
2344 unsigned long mask;
2345
2346 if (!data->writable)
2347 return true;
2348
2349 mask = (data->nr_pages << PAGE_SHIFT) - 1;
2350 /*
2351 * Userspace could choose to issue a mb() before updating the tail
2352 * pointer. So that all reads will be completed before the write is
2353 * issued.
2354 */
2355 tail = ACCESS_ONCE(data->user_page->data_tail);
2356 smp_rmb();
2357
2358 offset = (offset - tail) & mask;
2359 head = (head - tail) & mask;
2360
2361 if ((int)(head - offset) < 0)
2362 return false;
2363
2364 return true;
2365}
2366
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002367static void perf_output_wakeup(struct perf_output_handle *handle)
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002368{
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002369 atomic_set(&handle->data->poll, POLL_IN);
2370
Peter Zijlstra671dec52009-04-06 11:45:02 +02002371 if (handle->nmi) {
Peter Zijlstra79f14642009-04-06 11:45:07 +02002372 handle->counter->pending_wakeup = 1;
Peter Zijlstra671dec52009-04-06 11:45:02 +02002373 perf_pending_queue(&handle->counter->pending,
Peter Zijlstra79f14642009-04-06 11:45:07 +02002374 perf_pending_counter);
Peter Zijlstra671dec52009-04-06 11:45:02 +02002375 } else
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002376 perf_counter_wakeup(handle->counter);
2377}
2378
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002379/*
2380 * Curious locking construct.
2381 *
2382 * We need to ensure a later event doesn't publish a head when a former
2383 * event isn't done writing. However since we need to deal with NMIs we
2384 * cannot fully serialize things.
2385 *
2386 * What we do is serialize between CPUs so we only have to deal with NMI
2387 * nesting on a single CPU.
2388 *
2389 * We only publish the head (and generate a wakeup) when the outer-most
2390 * event completes.
2391 */
2392static void perf_output_lock(struct perf_output_handle *handle)
2393{
2394 struct perf_mmap_data *data = handle->data;
2395 int cpu;
2396
2397 handle->locked = 0;
2398
2399 local_irq_save(handle->flags);
2400 cpu = smp_processor_id();
2401
2402 if (in_nmi() && atomic_read(&data->lock) == cpu)
2403 return;
2404
Peter Zijlstra22c15582009-05-05 17:50:25 +02002405 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002406 cpu_relax();
2407
2408 handle->locked = 1;
2409}
2410
2411static void perf_output_unlock(struct perf_output_handle *handle)
2412{
2413 struct perf_mmap_data *data = handle->data;
Peter Zijlstra8e3747c2009-06-02 16:16:02 +02002414 unsigned long head;
2415 int cpu;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002416
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002417 data->done_head = data->head;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002418
2419 if (!handle->locked)
2420 goto out;
2421
2422again:
2423 /*
2424 * The xchg implies a full barrier that ensures all writes are done
2425 * before we publish the new head, matched by a rmb() in userspace when
2426 * reading this position.
2427 */
Peter Zijlstra8e3747c2009-06-02 16:16:02 +02002428 while ((head = atomic_long_xchg(&data->done_head, 0)))
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002429 data->user_page->data_head = head;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002430
2431 /*
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002432 * NMI can happen here, which means we can miss a done_head update.
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002433 */
2434
Peter Zijlstra22c15582009-05-05 17:50:25 +02002435 cpu = atomic_xchg(&data->lock, -1);
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002436 WARN_ON_ONCE(cpu != smp_processor_id());
2437
2438 /*
2439 * Therefore we have to validate we did not indeed do so.
2440 */
Peter Zijlstra8e3747c2009-06-02 16:16:02 +02002441 if (unlikely(atomic_long_read(&data->done_head))) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002442 /*
2443 * Since we had it locked, we can lock it again.
2444 */
Peter Zijlstra22c15582009-05-05 17:50:25 +02002445 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002446 cpu_relax();
2447
2448 goto again;
2449 }
2450
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002451 if (atomic_xchg(&data->wakeup, 0))
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002452 perf_output_wakeup(handle);
2453out:
2454 local_irq_restore(handle->flags);
2455}
2456
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002457static void perf_output_copy(struct perf_output_handle *handle,
Peter Zijlstra089dd792009-06-05 14:04:55 +02002458 const void *buf, unsigned int len)
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002459{
2460 unsigned int pages_mask;
2461 unsigned int offset;
2462 unsigned int size;
2463 void **pages;
2464
2465 offset = handle->offset;
2466 pages_mask = handle->data->nr_pages - 1;
2467 pages = handle->data->data_pages;
2468
2469 do {
2470 unsigned int page_offset;
2471 int nr;
2472
2473 nr = (offset >> PAGE_SHIFT) & pages_mask;
2474 page_offset = offset & (PAGE_SIZE - 1);
2475 size = min_t(unsigned int, PAGE_SIZE - page_offset, len);
2476
2477 memcpy(pages[nr] + page_offset, buf, size);
2478
2479 len -= size;
2480 buf += size;
2481 offset += size;
2482 } while (len);
2483
2484 handle->offset = offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01002485
Peter Zijlstra53020fe2009-05-13 21:26:19 +02002486 /*
2487 * Check we didn't copy past our reservation window, taking the
2488 * possible unsigned int wrap into account.
2489 */
Peter Zijlstra8e3747c2009-06-02 16:16:02 +02002490 WARN_ON_ONCE(((long)(handle->head - handle->offset)) < 0);
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002491}
2492
Peter Zijlstra5c148192009-03-25 12:30:23 +01002493#define perf_output_put(handle, x) \
2494 perf_output_copy((handle), &(x), sizeof(x))
2495
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002496static int perf_output_begin(struct perf_output_handle *handle,
2497 struct perf_counter *counter, unsigned int size,
2498 int nmi, int sample)
2499{
2500 struct perf_mmap_data *data;
2501 unsigned int offset, head;
2502 int have_lost;
2503 struct {
2504 struct perf_event_header header;
2505 u64 id;
2506 u64 lost;
2507 } lost_event;
2508
2509 /*
2510 * For inherited counters we send all the output towards the parent.
2511 */
2512 if (counter->parent)
2513 counter = counter->parent;
2514
2515 rcu_read_lock();
2516 data = rcu_dereference(counter->data);
2517 if (!data)
2518 goto out;
2519
2520 handle->data = data;
2521 handle->counter = counter;
2522 handle->nmi = nmi;
2523 handle->sample = sample;
2524
2525 if (!data->nr_pages)
2526 goto fail;
2527
2528 have_lost = atomic_read(&data->lost);
2529 if (have_lost)
2530 size += sizeof(lost_event);
2531
2532 perf_output_lock(handle);
2533
2534 do {
2535 offset = head = atomic_long_read(&data->head);
2536 head += size;
2537 if (unlikely(!perf_output_space(data, offset, head)))
2538 goto fail;
2539 } while (atomic_long_cmpxchg(&data->head, offset, head) != offset);
2540
2541 handle->offset = offset;
2542 handle->head = head;
2543
2544 if ((offset >> PAGE_SHIFT) != (head >> PAGE_SHIFT))
2545 atomic_set(&data->wakeup, 1);
2546
2547 if (have_lost) {
2548 lost_event.header.type = PERF_EVENT_LOST;
2549 lost_event.header.misc = 0;
2550 lost_event.header.size = sizeof(lost_event);
2551 lost_event.id = counter->id;
2552 lost_event.lost = atomic_xchg(&data->lost, 0);
2553
2554 perf_output_put(handle, lost_event);
2555 }
2556
2557 return 0;
2558
2559fail:
2560 atomic_inc(&data->lost);
2561 perf_output_unlock(handle);
2562out:
2563 rcu_read_unlock();
2564
2565 return -ENOSPC;
2566}
2567
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002568static void perf_output_end(struct perf_output_handle *handle)
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002569{
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002570 struct perf_counter *counter = handle->counter;
2571 struct perf_mmap_data *data = handle->data;
2572
Peter Zijlstra0d486962009-06-02 19:22:16 +02002573 int wakeup_events = counter->attr.wakeup_events;
Peter Zijlstrac4578102009-04-02 11:12:01 +02002574
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002575 if (handle->sample && wakeup_events) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002576 int events = atomic_inc_return(&data->events);
Peter Zijlstrac4578102009-04-02 11:12:01 +02002577 if (events >= wakeup_events) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002578 atomic_sub(wakeup_events, &data->events);
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002579 atomic_set(&data->wakeup, 1);
Peter Zijlstrac4578102009-04-02 11:12:01 +02002580 }
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002581 }
2582
2583 perf_output_unlock(handle);
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002584 rcu_read_unlock();
2585}
2586
Peter Zijlstra709e50c2009-06-02 14:13:15 +02002587static u32 perf_counter_pid(struct perf_counter *counter, struct task_struct *p)
2588{
2589 /*
2590 * only top level counters have the pid namespace they were created in
2591 */
2592 if (counter->parent)
2593 counter = counter->parent;
2594
2595 return task_tgid_nr_ns(p, counter->ns);
2596}
2597
2598static u32 perf_counter_tid(struct perf_counter *counter, struct task_struct *p)
2599{
2600 /*
2601 * only top level counters have the pid namespace they were created in
2602 */
2603 if (counter->parent)
2604 counter = counter->parent;
2605
2606 return task_pid_nr_ns(p, counter->ns);
2607}
2608
Peter Zijlstradf1a1322009-06-10 21:02:22 +02002609static void perf_counter_output(struct perf_counter *counter, int nmi,
2610 struct perf_sample_data *data)
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002611{
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002612 int ret;
Peter Zijlstra0d486962009-06-02 19:22:16 +02002613 u64 sample_type = counter->attr.sample_type;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002614 struct perf_output_handle handle;
2615 struct perf_event_header header;
2616 u64 ip;
Peter Zijlstra5c148192009-03-25 12:30:23 +01002617 struct {
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002618 u32 pid, tid;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002619 } tid_entry;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002620 struct {
Peter Zijlstra8e5799b2009-06-02 15:08:15 +02002621 u64 id;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002622 u64 counter;
2623 } group_entry;
Peter Zijlstra394ee072009-03-30 19:07:14 +02002624 struct perf_callchain_entry *callchain = NULL;
2625 int callchain_size = 0;
Peter Zijlstra339f7c92009-04-06 11:45:06 +02002626 u64 time;
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002627 struct {
2628 u32 cpu, reserved;
2629 } cpu_entry;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002630
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02002631 header.type = PERF_EVENT_SAMPLE;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002632 header.size = sizeof(header);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002633
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02002634 header.misc = 0;
Peter Zijlstradf1a1322009-06-10 21:02:22 +02002635 header.misc |= perf_misc_flags(data->regs);
Peter Zijlstra6fab0192009-04-08 15:01:26 +02002636
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002637 if (sample_type & PERF_SAMPLE_IP) {
Peter Zijlstradf1a1322009-06-10 21:02:22 +02002638 ip = perf_instruction_pointer(data->regs);
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002639 header.size += sizeof(ip);
2640 }
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002641
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002642 if (sample_type & PERF_SAMPLE_TID) {
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002643 /* namespace issues */
Peter Zijlstra709e50c2009-06-02 14:13:15 +02002644 tid_entry.pid = perf_counter_pid(counter, current);
2645 tid_entry.tid = perf_counter_tid(counter, current);
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002646
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002647 header.size += sizeof(tid_entry);
2648 }
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002649
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002650 if (sample_type & PERF_SAMPLE_TIME) {
Peter Zijlstra4d855452009-04-08 15:01:32 +02002651 /*
2652 * Maybe do better on x86 and provide cpu_clock_nmi()
2653 */
2654 time = sched_clock();
2655
Peter Zijlstra4d855452009-04-08 15:01:32 +02002656 header.size += sizeof(u64);
2657 }
2658
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02002659 if (sample_type & PERF_SAMPLE_ADDR)
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002660 header.size += sizeof(u64);
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002661
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02002662 if (sample_type & PERF_SAMPLE_ID)
Peter Zijlstraa85f61a2009-05-08 18:52:23 +02002663 header.size += sizeof(u64);
Peter Zijlstraa85f61a2009-05-08 18:52:23 +02002664
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002665 if (sample_type & PERF_SAMPLE_CPU) {
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002666 header.size += sizeof(cpu_entry);
2667
2668 cpu_entry.cpu = raw_smp_processor_id();
2669 }
2670
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02002671 if (sample_type & PERF_SAMPLE_PERIOD)
Peter Zijlstra689802b2009-06-05 15:05:43 +02002672 header.size += sizeof(u64);
Peter Zijlstra689802b2009-06-05 15:05:43 +02002673
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002674 if (sample_type & PERF_SAMPLE_GROUP) {
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002675 header.size += sizeof(u64) +
2676 counter->nr_siblings * sizeof(group_entry);
2677 }
2678
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002679 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
Peter Zijlstradf1a1322009-06-10 21:02:22 +02002680 callchain = perf_callchain(data->regs);
Peter Zijlstra394ee072009-03-30 19:07:14 +02002681
2682 if (callchain) {
Peter Zijlstra9c03d882009-04-06 11:45:00 +02002683 callchain_size = (1 + callchain->nr) * sizeof(u64);
Peter Zijlstra394ee072009-03-30 19:07:14 +02002684 header.size += callchain_size;
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02002685 } else
2686 header.size += sizeof(u64);
Peter Zijlstra394ee072009-03-30 19:07:14 +02002687 }
2688
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002689 ret = perf_output_begin(&handle, counter, header.size, nmi, 1);
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002690 if (ret)
2691 return;
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002692
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002693 perf_output_put(&handle, header);
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002694
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002695 if (sample_type & PERF_SAMPLE_IP)
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002696 perf_output_put(&handle, ip);
2697
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002698 if (sample_type & PERF_SAMPLE_TID)
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002699 perf_output_put(&handle, tid_entry);
2700
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002701 if (sample_type & PERF_SAMPLE_TIME)
Peter Zijlstra4d855452009-04-08 15:01:32 +02002702 perf_output_put(&handle, time);
2703
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002704 if (sample_type & PERF_SAMPLE_ADDR)
Peter Zijlstradf1a1322009-06-10 21:02:22 +02002705 perf_output_put(&handle, data->addr);
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002706
Peter Zijlstraac4bcf82009-06-05 14:44:52 +02002707 if (sample_type & PERF_SAMPLE_ID)
2708 perf_output_put(&handle, counter->id);
Peter Zijlstraa85f61a2009-05-08 18:52:23 +02002709
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002710 if (sample_type & PERF_SAMPLE_CPU)
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002711 perf_output_put(&handle, cpu_entry);
2712
Peter Zijlstra689802b2009-06-05 15:05:43 +02002713 if (sample_type & PERF_SAMPLE_PERIOD)
Peter Zijlstra9e350de2009-06-10 21:34:59 +02002714 perf_output_put(&handle, data->period);
Peter Zijlstra689802b2009-06-05 15:05:43 +02002715
Peter Zijlstra2023b352009-05-05 17:50:26 +02002716 /*
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002717 * XXX PERF_SAMPLE_GROUP vs inherited counters seems difficult.
Peter Zijlstra2023b352009-05-05 17:50:26 +02002718 */
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002719 if (sample_type & PERF_SAMPLE_GROUP) {
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002720 struct perf_counter *leader, *sub;
2721 u64 nr = counter->nr_siblings;
2722
2723 perf_output_put(&handle, nr);
2724
2725 leader = counter->group_leader;
2726 list_for_each_entry(sub, &leader->sibling_list, list_entry) {
2727 if (sub != counter)
Robert Richter4aeb0b42009-04-29 12:47:03 +02002728 sub->pmu->read(sub);
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002729
Peter Zijlstra8e5799b2009-06-02 15:08:15 +02002730 group_entry.id = sub->id;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002731 group_entry.counter = atomic64_read(&sub->count);
2732
2733 perf_output_put(&handle, group_entry);
2734 }
2735 }
2736
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02002737 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
2738 if (callchain)
2739 perf_output_copy(&handle, callchain, callchain_size);
2740 else {
2741 u64 nr = 0;
2742 perf_output_put(&handle, nr);
2743 }
2744 }
Peter Zijlstra394ee072009-03-30 19:07:14 +02002745
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002746 perf_output_end(&handle);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002747}
2748
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002749/*
Peter Zijlstra38b200d2009-06-23 20:13:11 +02002750 * read event
2751 */
2752
2753struct perf_read_event {
2754 struct perf_event_header header;
2755
2756 u32 pid;
2757 u32 tid;
2758 u64 value;
2759 u64 format[3];
2760};
2761
2762static void
2763perf_counter_read_event(struct perf_counter *counter,
2764 struct task_struct *task)
2765{
2766 struct perf_output_handle handle;
2767 struct perf_read_event event = {
2768 .header = {
2769 .type = PERF_EVENT_READ,
2770 .misc = 0,
2771 .size = sizeof(event) - sizeof(event.format),
2772 },
2773 .pid = perf_counter_pid(counter, task),
2774 .tid = perf_counter_tid(counter, task),
2775 .value = atomic64_read(&counter->count),
2776 };
2777 int ret, i = 0;
2778
2779 if (counter->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
2780 event.header.size += sizeof(u64);
2781 event.format[i++] = counter->total_time_enabled;
2782 }
2783
2784 if (counter->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
2785 event.header.size += sizeof(u64);
2786 event.format[i++] = counter->total_time_running;
2787 }
2788
2789 if (counter->attr.read_format & PERF_FORMAT_ID) {
2790 u64 id;
2791
2792 event.header.size += sizeof(u64);
2793 if (counter->parent)
2794 id = counter->parent->id;
2795 else
2796 id = counter->id;
2797
2798 event.format[i++] = id;
2799 }
2800
2801 ret = perf_output_begin(&handle, counter, event.header.size, 0, 0);
2802 if (ret)
2803 return;
2804
2805 perf_output_copy(&handle, &event, event.header.size);
2806 perf_output_end(&handle);
2807}
2808
2809/*
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002810 * fork tracking
2811 */
2812
2813struct perf_fork_event {
2814 struct task_struct *task;
2815
2816 struct {
2817 struct perf_event_header header;
2818
2819 u32 pid;
2820 u32 ppid;
2821 } event;
2822};
2823
2824static void perf_counter_fork_output(struct perf_counter *counter,
2825 struct perf_fork_event *fork_event)
2826{
2827 struct perf_output_handle handle;
2828 int size = fork_event->event.header.size;
2829 struct task_struct *task = fork_event->task;
2830 int ret = perf_output_begin(&handle, counter, size, 0, 0);
2831
2832 if (ret)
2833 return;
2834
2835 fork_event->event.pid = perf_counter_pid(counter, task);
2836 fork_event->event.ppid = perf_counter_pid(counter, task->real_parent);
2837
2838 perf_output_put(&handle, fork_event->event);
2839 perf_output_end(&handle);
2840}
2841
2842static int perf_counter_fork_match(struct perf_counter *counter)
2843{
Peter Zijlstrad99e9442009-06-04 17:08:58 +02002844 if (counter->attr.comm || counter->attr.mmap)
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002845 return 1;
2846
2847 return 0;
2848}
2849
2850static void perf_counter_fork_ctx(struct perf_counter_context *ctx,
2851 struct perf_fork_event *fork_event)
2852{
2853 struct perf_counter *counter;
2854
2855 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2856 return;
2857
2858 rcu_read_lock();
2859 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2860 if (perf_counter_fork_match(counter))
2861 perf_counter_fork_output(counter, fork_event);
2862 }
2863 rcu_read_unlock();
2864}
2865
2866static void perf_counter_fork_event(struct perf_fork_event *fork_event)
2867{
2868 struct perf_cpu_context *cpuctx;
2869 struct perf_counter_context *ctx;
2870
2871 cpuctx = &get_cpu_var(perf_cpu_context);
2872 perf_counter_fork_ctx(&cpuctx->ctx, fork_event);
2873 put_cpu_var(perf_cpu_context);
2874
2875 rcu_read_lock();
2876 /*
2877 * doesn't really matter which of the child contexts the
2878 * events ends up in.
2879 */
2880 ctx = rcu_dereference(current->perf_counter_ctxp);
2881 if (ctx)
2882 perf_counter_fork_ctx(ctx, fork_event);
2883 rcu_read_unlock();
2884}
2885
2886void perf_counter_fork(struct task_struct *task)
2887{
2888 struct perf_fork_event fork_event;
2889
2890 if (!atomic_read(&nr_comm_counters) &&
Peter Zijlstrad99e9442009-06-04 17:08:58 +02002891 !atomic_read(&nr_mmap_counters))
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002892 return;
2893
2894 fork_event = (struct perf_fork_event){
2895 .task = task,
2896 .event = {
2897 .header = {
2898 .type = PERF_EVENT_FORK,
2899 .size = sizeof(fork_event.event),
2900 },
2901 },
2902 };
2903
2904 perf_counter_fork_event(&fork_event);
2905}
2906
2907/*
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002908 * comm tracking
2909 */
2910
2911struct perf_comm_event {
Ingo Molnar22a4f652009-06-01 10:13:37 +02002912 struct task_struct *task;
2913 char *comm;
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002914 int comm_size;
2915
2916 struct {
2917 struct perf_event_header header;
2918
2919 u32 pid;
2920 u32 tid;
2921 } event;
2922};
2923
2924static void perf_counter_comm_output(struct perf_counter *counter,
2925 struct perf_comm_event *comm_event)
2926{
2927 struct perf_output_handle handle;
2928 int size = comm_event->event.header.size;
2929 int ret = perf_output_begin(&handle, counter, size, 0, 0);
2930
2931 if (ret)
2932 return;
2933
Peter Zijlstra709e50c2009-06-02 14:13:15 +02002934 comm_event->event.pid = perf_counter_pid(counter, comm_event->task);
2935 comm_event->event.tid = perf_counter_tid(counter, comm_event->task);
2936
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002937 perf_output_put(&handle, comm_event->event);
2938 perf_output_copy(&handle, comm_event->comm,
2939 comm_event->comm_size);
2940 perf_output_end(&handle);
2941}
2942
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002943static int perf_counter_comm_match(struct perf_counter *counter)
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002944{
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002945 if (counter->attr.comm)
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002946 return 1;
2947
2948 return 0;
2949}
2950
2951static void perf_counter_comm_ctx(struct perf_counter_context *ctx,
2952 struct perf_comm_event *comm_event)
2953{
2954 struct perf_counter *counter;
2955
2956 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2957 return;
2958
2959 rcu_read_lock();
2960 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002961 if (perf_counter_comm_match(counter))
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002962 perf_counter_comm_output(counter, comm_event);
2963 }
2964 rcu_read_unlock();
2965}
2966
2967static void perf_counter_comm_event(struct perf_comm_event *comm_event)
2968{
2969 struct perf_cpu_context *cpuctx;
Peter Zijlstra665c2142009-05-29 14:51:57 +02002970 struct perf_counter_context *ctx;
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002971 unsigned int size;
2972 char *comm = comm_event->task->comm;
2973
Ingo Molnar888fcee2009-04-09 09:48:22 +02002974 size = ALIGN(strlen(comm)+1, sizeof(u64));
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002975
2976 comm_event->comm = comm;
2977 comm_event->comm_size = size;
2978
2979 comm_event->event.header.size = sizeof(comm_event->event) + size;
2980
2981 cpuctx = &get_cpu_var(perf_cpu_context);
2982 perf_counter_comm_ctx(&cpuctx->ctx, comm_event);
2983 put_cpu_var(perf_cpu_context);
Peter Zijlstra665c2142009-05-29 14:51:57 +02002984
2985 rcu_read_lock();
2986 /*
2987 * doesn't really matter which of the child contexts the
2988 * events ends up in.
2989 */
2990 ctx = rcu_dereference(current->perf_counter_ctxp);
2991 if (ctx)
2992 perf_counter_comm_ctx(ctx, comm_event);
2993 rcu_read_unlock();
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002994}
2995
2996void perf_counter_comm(struct task_struct *task)
2997{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002998 struct perf_comm_event comm_event;
2999
Paul Mackerras57e79862009-06-30 16:07:19 +10003000 if (task->perf_counter_ctxp)
3001 perf_counter_enable_on_exec(task);
3002
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003003 if (!atomic_read(&nr_comm_counters))
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02003004 return;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003005
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02003006 comm_event = (struct perf_comm_event){
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02003007 .task = task,
3008 .event = {
3009 .header = { .type = PERF_EVENT_COMM, },
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02003010 },
3011 };
3012
3013 perf_counter_comm_event(&comm_event);
3014}
3015
3016/*
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003017 * mmap tracking
3018 */
3019
3020struct perf_mmap_event {
Peter Zijlstra089dd792009-06-05 14:04:55 +02003021 struct vm_area_struct *vma;
3022
3023 const char *file_name;
3024 int file_size;
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003025
3026 struct {
3027 struct perf_event_header header;
3028
3029 u32 pid;
3030 u32 tid;
3031 u64 start;
3032 u64 len;
3033 u64 pgoff;
3034 } event;
3035};
3036
3037static void perf_counter_mmap_output(struct perf_counter *counter,
3038 struct perf_mmap_event *mmap_event)
3039{
3040 struct perf_output_handle handle;
3041 int size = mmap_event->event.header.size;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02003042 int ret = perf_output_begin(&handle, counter, size, 0, 0);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003043
3044 if (ret)
3045 return;
3046
Peter Zijlstra709e50c2009-06-02 14:13:15 +02003047 mmap_event->event.pid = perf_counter_pid(counter, current);
3048 mmap_event->event.tid = perf_counter_tid(counter, current);
3049
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003050 perf_output_put(&handle, mmap_event->event);
3051 perf_output_copy(&handle, mmap_event->file_name,
3052 mmap_event->file_size);
Peter Zijlstra78d613e2009-03-30 19:07:11 +02003053 perf_output_end(&handle);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003054}
3055
3056static int perf_counter_mmap_match(struct perf_counter *counter,
3057 struct perf_mmap_event *mmap_event)
3058{
Peter Zijlstrad99e9442009-06-04 17:08:58 +02003059 if (counter->attr.mmap)
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003060 return 1;
3061
3062 return 0;
3063}
3064
3065static void perf_counter_mmap_ctx(struct perf_counter_context *ctx,
3066 struct perf_mmap_event *mmap_event)
3067{
3068 struct perf_counter *counter;
3069
3070 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
3071 return;
3072
3073 rcu_read_lock();
3074 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
3075 if (perf_counter_mmap_match(counter, mmap_event))
3076 perf_counter_mmap_output(counter, mmap_event);
3077 }
3078 rcu_read_unlock();
3079}
3080
3081static void perf_counter_mmap_event(struct perf_mmap_event *mmap_event)
3082{
3083 struct perf_cpu_context *cpuctx;
Peter Zijlstra665c2142009-05-29 14:51:57 +02003084 struct perf_counter_context *ctx;
Peter Zijlstra089dd792009-06-05 14:04:55 +02003085 struct vm_area_struct *vma = mmap_event->vma;
3086 struct file *file = vma->vm_file;
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003087 unsigned int size;
3088 char tmp[16];
3089 char *buf = NULL;
Peter Zijlstra089dd792009-06-05 14:04:55 +02003090 const char *name;
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003091
3092 if (file) {
3093 buf = kzalloc(PATH_MAX, GFP_KERNEL);
3094 if (!buf) {
3095 name = strncpy(tmp, "//enomem", sizeof(tmp));
3096 goto got_name;
3097 }
Peter Zijlstrad3d21c42009-04-09 10:53:46 +02003098 name = d_path(&file->f_path, buf, PATH_MAX);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003099 if (IS_ERR(name)) {
3100 name = strncpy(tmp, "//toolong", sizeof(tmp));
3101 goto got_name;
3102 }
3103 } else {
Peter Zijlstra089dd792009-06-05 14:04:55 +02003104 name = arch_vma_name(mmap_event->vma);
3105 if (name)
3106 goto got_name;
3107
3108 if (!vma->vm_mm) {
3109 name = strncpy(tmp, "[vdso]", sizeof(tmp));
3110 goto got_name;
3111 }
3112
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003113 name = strncpy(tmp, "//anon", sizeof(tmp));
3114 goto got_name;
3115 }
3116
3117got_name:
Ingo Molnar888fcee2009-04-09 09:48:22 +02003118 size = ALIGN(strlen(name)+1, sizeof(u64));
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003119
3120 mmap_event->file_name = name;
3121 mmap_event->file_size = size;
3122
3123 mmap_event->event.header.size = sizeof(mmap_event->event) + size;
3124
3125 cpuctx = &get_cpu_var(perf_cpu_context);
3126 perf_counter_mmap_ctx(&cpuctx->ctx, mmap_event);
3127 put_cpu_var(perf_cpu_context);
3128
Peter Zijlstra665c2142009-05-29 14:51:57 +02003129 rcu_read_lock();
3130 /*
3131 * doesn't really matter which of the child contexts the
3132 * events ends up in.
3133 */
3134 ctx = rcu_dereference(current->perf_counter_ctxp);
3135 if (ctx)
3136 perf_counter_mmap_ctx(ctx, mmap_event);
3137 rcu_read_unlock();
3138
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003139 kfree(buf);
3140}
3141
Peter Zijlstra089dd792009-06-05 14:04:55 +02003142void __perf_counter_mmap(struct vm_area_struct *vma)
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003143{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02003144 struct perf_mmap_event mmap_event;
3145
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003146 if (!atomic_read(&nr_mmap_counters))
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02003147 return;
3148
3149 mmap_event = (struct perf_mmap_event){
Peter Zijlstra089dd792009-06-05 14:04:55 +02003150 .vma = vma,
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003151 .event = {
3152 .header = { .type = PERF_EVENT_MMAP, },
Peter Zijlstra089dd792009-06-05 14:04:55 +02003153 .start = vma->vm_start,
3154 .len = vma->vm_end - vma->vm_start,
3155 .pgoff = vma->vm_pgoff,
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003156 },
3157 };
3158
3159 perf_counter_mmap_event(&mmap_event);
3160}
3161
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003162/*
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003163 * Log sample_period changes so that analyzing tools can re-normalize the
Peter Zijlstrae220d2d2009-05-23 18:28:55 +02003164 * event flow.
Peter Zijlstra26b119b2009-05-20 12:21:20 +02003165 */
3166
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02003167struct freq_event {
3168 struct perf_event_header header;
3169 u64 time;
3170 u64 id;
3171 u64 period;
3172};
3173
Peter Zijlstra26b119b2009-05-20 12:21:20 +02003174static void perf_log_period(struct perf_counter *counter, u64 period)
3175{
3176 struct perf_output_handle handle;
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02003177 struct freq_event event;
Peter Zijlstra26b119b2009-05-20 12:21:20 +02003178 int ret;
3179
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02003180 if (counter->hw.sample_period == period)
3181 return;
3182
3183 if (counter->attr.sample_type & PERF_SAMPLE_PERIOD)
3184 return;
3185
3186 event = (struct freq_event) {
Peter Zijlstra26b119b2009-05-20 12:21:20 +02003187 .header = {
3188 .type = PERF_EVENT_PERIOD,
3189 .misc = 0,
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02003190 .size = sizeof(event),
Peter Zijlstra26b119b2009-05-20 12:21:20 +02003191 },
3192 .time = sched_clock(),
Peter Zijlstra689802b2009-06-05 15:05:43 +02003193 .id = counter->id,
Peter Zijlstra26b119b2009-05-20 12:21:20 +02003194 .period = period,
3195 };
3196
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02003197 ret = perf_output_begin(&handle, counter, sizeof(event), 1, 0);
Peter Zijlstra26b119b2009-05-20 12:21:20 +02003198 if (ret)
3199 return;
3200
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02003201 perf_output_put(&handle, event);
Peter Zijlstra26b119b2009-05-20 12:21:20 +02003202 perf_output_end(&handle);
3203}
3204
3205/*
Peter Zijlstraa78ac322009-05-25 17:39:05 +02003206 * IRQ throttle logging
3207 */
3208
3209static void perf_log_throttle(struct perf_counter *counter, int enable)
3210{
3211 struct perf_output_handle handle;
3212 int ret;
3213
3214 struct {
3215 struct perf_event_header header;
3216 u64 time;
Peter Zijlstracca3f452009-06-11 14:57:55 +02003217 u64 id;
Peter Zijlstraa78ac322009-05-25 17:39:05 +02003218 } throttle_event = {
3219 .header = {
3220 .type = PERF_EVENT_THROTTLE + 1,
3221 .misc = 0,
3222 .size = sizeof(throttle_event),
3223 },
Peter Zijlstracca3f452009-06-11 14:57:55 +02003224 .time = sched_clock(),
3225 .id = counter->id,
Peter Zijlstraa78ac322009-05-25 17:39:05 +02003226 };
3227
Ingo Molnar0127c3e2009-05-25 22:03:26 +02003228 ret = perf_output_begin(&handle, counter, sizeof(throttle_event), 1, 0);
Peter Zijlstraa78ac322009-05-25 17:39:05 +02003229 if (ret)
3230 return;
3231
3232 perf_output_put(&handle, throttle_event);
3233 perf_output_end(&handle);
3234}
3235
3236/*
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01003237 * Generic counter overflow handling, sampling.
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02003238 */
3239
Peter Zijlstradf1a1322009-06-10 21:02:22 +02003240int perf_counter_overflow(struct perf_counter *counter, int nmi,
3241 struct perf_sample_data *data)
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02003242{
Peter Zijlstra79f14642009-04-06 11:45:07 +02003243 int events = atomic_read(&counter->event_limit);
Peter Zijlstraa78ac322009-05-25 17:39:05 +02003244 int throttle = counter->pmu->unthrottle != NULL;
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02003245 struct hw_perf_counter *hwc = &counter->hw;
Peter Zijlstra79f14642009-04-06 11:45:07 +02003246 int ret = 0;
3247
Peter Zijlstraa78ac322009-05-25 17:39:05 +02003248 if (!throttle) {
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02003249 hwc->interrupts++;
Ingo Molnar128f0482009-06-03 22:19:36 +02003250 } else {
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02003251 if (hwc->interrupts != MAX_INTERRUPTS) {
3252 hwc->interrupts++;
Peter Zijlstradf58ab22009-06-11 11:25:05 +02003253 if (HZ * hwc->interrupts >
3254 (u64)sysctl_perf_counter_sample_rate) {
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02003255 hwc->interrupts = MAX_INTERRUPTS;
Ingo Molnar128f0482009-06-03 22:19:36 +02003256 perf_log_throttle(counter, 0);
3257 ret = 1;
3258 }
3259 } else {
3260 /*
3261 * Keep re-disabling counters even though on the previous
3262 * pass we disabled it - just in case we raced with a
3263 * sched-in and the counter got enabled again:
3264 */
Peter Zijlstraa78ac322009-05-25 17:39:05 +02003265 ret = 1;
3266 }
3267 }
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003268
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02003269 if (counter->attr.freq) {
3270 u64 now = sched_clock();
3271 s64 delta = now - hwc->freq_stamp;
3272
3273 hwc->freq_stamp = now;
3274
3275 if (delta > 0 && delta < TICK_NSEC)
3276 perf_adjust_period(counter, NSEC_PER_SEC / (int)delta);
3277 }
3278
Peter Zijlstra2023b352009-05-05 17:50:26 +02003279 /*
3280 * XXX event_limit might not quite work as expected on inherited
3281 * counters
3282 */
3283
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02003284 counter->pending_kill = POLL_IN;
Peter Zijlstra79f14642009-04-06 11:45:07 +02003285 if (events && atomic_dec_and_test(&counter->event_limit)) {
3286 ret = 1;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02003287 counter->pending_kill = POLL_HUP;
Peter Zijlstra79f14642009-04-06 11:45:07 +02003288 if (nmi) {
3289 counter->pending_disable = 1;
3290 perf_pending_queue(&counter->pending,
3291 perf_pending_counter);
3292 } else
3293 perf_counter_disable(counter);
3294 }
3295
Peter Zijlstradf1a1322009-06-10 21:02:22 +02003296 perf_counter_output(counter, nmi, data);
Peter Zijlstra79f14642009-04-06 11:45:07 +02003297 return ret;
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02003298}
3299
3300/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003301 * Generic software counter infrastructure
3302 */
3303
3304static void perf_swcounter_update(struct perf_counter *counter)
3305{
3306 struct hw_perf_counter *hwc = &counter->hw;
3307 u64 prev, now;
3308 s64 delta;
3309
3310again:
3311 prev = atomic64_read(&hwc->prev_count);
3312 now = atomic64_read(&hwc->count);
3313 if (atomic64_cmpxchg(&hwc->prev_count, prev, now) != prev)
3314 goto again;
3315
3316 delta = now - prev;
3317
3318 atomic64_add(delta, &counter->count);
3319 atomic64_sub(delta, &hwc->period_left);
3320}
3321
3322static void perf_swcounter_set_period(struct perf_counter *counter)
3323{
3324 struct hw_perf_counter *hwc = &counter->hw;
3325 s64 left = atomic64_read(&hwc->period_left);
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003326 s64 period = hwc->sample_period;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003327
3328 if (unlikely(left <= -period)) {
3329 left = period;
3330 atomic64_set(&hwc->period_left, left);
Peter Zijlstra9e350de2009-06-10 21:34:59 +02003331 hwc->last_period = period;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003332 }
3333
3334 if (unlikely(left <= 0)) {
3335 left += period;
3336 atomic64_add(period, &hwc->period_left);
Peter Zijlstra9e350de2009-06-10 21:34:59 +02003337 hwc->last_period = period;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003338 }
3339
3340 atomic64_set(&hwc->prev_count, -left);
3341 atomic64_set(&hwc->count, -left);
3342}
3343
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003344static enum hrtimer_restart perf_swcounter_hrtimer(struct hrtimer *hrtimer)
3345{
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02003346 enum hrtimer_restart ret = HRTIMER_RESTART;
Peter Zijlstradf1a1322009-06-10 21:02:22 +02003347 struct perf_sample_data data;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003348 struct perf_counter *counter;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003349 u64 period;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003350
3351 counter = container_of(hrtimer, struct perf_counter, hw.hrtimer);
Robert Richter4aeb0b42009-04-29 12:47:03 +02003352 counter->pmu->read(counter);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003353
Peter Zijlstradf1a1322009-06-10 21:02:22 +02003354 data.addr = 0;
3355 data.regs = get_irq_regs();
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003356 /*
3357 * In case we exclude kernel IPs or are somehow not in interrupt
3358 * context, provide the next best thing, the user IP.
3359 */
Peter Zijlstradf1a1322009-06-10 21:02:22 +02003360 if ((counter->attr.exclude_kernel || !data.regs) &&
Peter Zijlstra0d486962009-06-02 19:22:16 +02003361 !counter->attr.exclude_user)
Peter Zijlstradf1a1322009-06-10 21:02:22 +02003362 data.regs = task_pt_regs(current);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003363
Peter Zijlstradf1a1322009-06-10 21:02:22 +02003364 if (data.regs) {
3365 if (perf_counter_overflow(counter, 0, &data))
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02003366 ret = HRTIMER_NORESTART;
3367 }
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003368
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003369 period = max_t(u64, 10000, counter->hw.sample_period);
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003370 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003371
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02003372 return ret;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003373}
3374
3375static void perf_swcounter_overflow(struct perf_counter *counter,
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003376 int nmi, struct perf_sample_data *data)
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003377{
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003378 data->period = counter->hw.last_period;
Peter Zijlstradf1a1322009-06-10 21:02:22 +02003379
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003380 perf_swcounter_update(counter);
3381 perf_swcounter_set_period(counter);
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003382 if (perf_counter_overflow(counter, nmi, data))
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02003383 /* soft-disable the counter */
3384 ;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003385}
3386
Paul Mackerras880ca152009-06-01 17:49:14 +10003387static int perf_swcounter_is_counting(struct perf_counter *counter)
3388{
3389 struct perf_counter_context *ctx;
3390 unsigned long flags;
3391 int count;
3392
3393 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
3394 return 1;
3395
3396 if (counter->state != PERF_COUNTER_STATE_INACTIVE)
3397 return 0;
3398
3399 /*
3400 * If the counter is inactive, it could be just because
3401 * its task is scheduled out, or because it's in a group
3402 * which could not go on the PMU. We want to count in
3403 * the first case but not the second. If the context is
3404 * currently active then an inactive software counter must
3405 * be the second case. If it's not currently active then
3406 * we need to know whether the counter was active when the
3407 * context was last active, which we can determine by
3408 * comparing counter->tstamp_stopped with ctx->time.
3409 *
3410 * We are within an RCU read-side critical section,
3411 * which protects the existence of *ctx.
3412 */
3413 ctx = counter->ctx;
3414 spin_lock_irqsave(&ctx->lock, flags);
3415 count = 1;
3416 /* Re-check state now we have the lock */
3417 if (counter->state < PERF_COUNTER_STATE_INACTIVE ||
3418 counter->ctx->is_active ||
3419 counter->tstamp_stopped < ctx->time)
3420 count = 0;
3421 spin_unlock_irqrestore(&ctx->lock, flags);
3422 return count;
3423}
3424
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003425static int perf_swcounter_match(struct perf_counter *counter,
Peter Zijlstra1c432d82009-06-11 13:19:29 +02003426 enum perf_type_id type,
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003427 u32 event, struct pt_regs *regs)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003428{
Paul Mackerras880ca152009-06-01 17:49:14 +10003429 if (!perf_swcounter_is_counting(counter))
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003430 return 0;
3431
Ingo Molnara21ca2c2009-06-06 09:58:57 +02003432 if (counter->attr.type != type)
3433 return 0;
3434 if (counter->attr.config != event)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003435 return 0;
3436
Paul Mackerras3f731ca2009-06-01 17:52:30 +10003437 if (regs) {
Peter Zijlstra0d486962009-06-02 19:22:16 +02003438 if (counter->attr.exclude_user && user_mode(regs))
Paul Mackerras3f731ca2009-06-01 17:52:30 +10003439 return 0;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003440
Peter Zijlstra0d486962009-06-02 19:22:16 +02003441 if (counter->attr.exclude_kernel && !user_mode(regs))
Paul Mackerras3f731ca2009-06-01 17:52:30 +10003442 return 0;
3443 }
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003444
3445 return 1;
3446}
3447
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003448static void perf_swcounter_add(struct perf_counter *counter, u64 nr,
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003449 int nmi, struct perf_sample_data *data)
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003450{
3451 int neg = atomic64_add_negative(nr, &counter->hw.count);
Ingo Molnar22a4f652009-06-01 10:13:37 +02003452
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003453 if (counter->hw.sample_period && !neg && data->regs)
3454 perf_swcounter_overflow(counter, nmi, data);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003455}
3456
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003457static void perf_swcounter_ctx_event(struct perf_counter_context *ctx,
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003458 enum perf_type_id type,
3459 u32 event, u64 nr, int nmi,
3460 struct perf_sample_data *data)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003461{
3462 struct perf_counter *counter;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003463
Peter Zijlstra01ef09d2009-03-19 20:26:11 +01003464 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003465 return;
3466
Peter Zijlstra592903c2009-03-13 12:21:36 +01003467 rcu_read_lock();
3468 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003469 if (perf_swcounter_match(counter, type, event, data->regs))
3470 perf_swcounter_add(counter, nr, nmi, data);
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003471 }
Peter Zijlstra592903c2009-03-13 12:21:36 +01003472 rcu_read_unlock();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003473}
3474
Peter Zijlstra96f6d442009-03-23 18:22:07 +01003475static int *perf_swcounter_recursion_context(struct perf_cpu_context *cpuctx)
3476{
3477 if (in_nmi())
3478 return &cpuctx->recursion[3];
3479
3480 if (in_irq())
3481 return &cpuctx->recursion[2];
3482
3483 if (in_softirq())
3484 return &cpuctx->recursion[1];
3485
3486 return &cpuctx->recursion[0];
3487}
3488
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003489static void do_perf_swcounter_event(enum perf_type_id type, u32 event,
3490 u64 nr, int nmi,
3491 struct perf_sample_data *data)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003492{
3493 struct perf_cpu_context *cpuctx = &get_cpu_var(perf_cpu_context);
Peter Zijlstra96f6d442009-03-23 18:22:07 +01003494 int *recursion = perf_swcounter_recursion_context(cpuctx);
Peter Zijlstra665c2142009-05-29 14:51:57 +02003495 struct perf_counter_context *ctx;
Peter Zijlstra96f6d442009-03-23 18:22:07 +01003496
3497 if (*recursion)
3498 goto out;
3499
3500 (*recursion)++;
3501 barrier();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003502
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003503 perf_swcounter_ctx_event(&cpuctx->ctx, type, event,
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003504 nr, nmi, data);
Peter Zijlstra665c2142009-05-29 14:51:57 +02003505 rcu_read_lock();
3506 /*
3507 * doesn't really matter which of the child contexts the
3508 * events ends up in.
3509 */
3510 ctx = rcu_dereference(current->perf_counter_ctxp);
3511 if (ctx)
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003512 perf_swcounter_ctx_event(ctx, type, event, nr, nmi, data);
Peter Zijlstra665c2142009-05-29 14:51:57 +02003513 rcu_read_unlock();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003514
Peter Zijlstra96f6d442009-03-23 18:22:07 +01003515 barrier();
3516 (*recursion)--;
3517
3518out:
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003519 put_cpu_var(perf_cpu_context);
3520}
3521
Peter Zijlstraf29ac752009-06-19 18:27:26 +02003522void __perf_swcounter_event(u32 event, u64 nr, int nmi,
3523 struct pt_regs *regs, u64 addr)
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003524{
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003525 struct perf_sample_data data = {
3526 .regs = regs,
3527 .addr = addr,
3528 };
3529
3530 do_perf_swcounter_event(PERF_TYPE_SOFTWARE, event, nr, nmi, &data);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003531}
3532
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003533static void perf_swcounter_read(struct perf_counter *counter)
3534{
3535 perf_swcounter_update(counter);
3536}
3537
3538static int perf_swcounter_enable(struct perf_counter *counter)
3539{
3540 perf_swcounter_set_period(counter);
3541 return 0;
3542}
3543
3544static void perf_swcounter_disable(struct perf_counter *counter)
3545{
3546 perf_swcounter_update(counter);
3547}
3548
Robert Richter4aeb0b42009-04-29 12:47:03 +02003549static const struct pmu perf_ops_generic = {
Peter Zijlstraac17dc82009-03-13 12:21:34 +01003550 .enable = perf_swcounter_enable,
3551 .disable = perf_swcounter_disable,
3552 .read = perf_swcounter_read,
3553};
3554
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003555/*
3556 * Software counter: cpu wall time clock
3557 */
3558
Paul Mackerras9abf8a02009-01-09 16:26:43 +11003559static void cpu_clock_perf_counter_update(struct perf_counter *counter)
3560{
3561 int cpu = raw_smp_processor_id();
3562 s64 prev;
3563 u64 now;
3564
3565 now = cpu_clock(cpu);
3566 prev = atomic64_read(&counter->hw.prev_count);
3567 atomic64_set(&counter->hw.prev_count, now);
3568 atomic64_add(now - prev, &counter->count);
3569}
3570
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003571static int cpu_clock_perf_counter_enable(struct perf_counter *counter)
3572{
3573 struct hw_perf_counter *hwc = &counter->hw;
3574 int cpu = raw_smp_processor_id();
3575
3576 atomic64_set(&hwc->prev_count, cpu_clock(cpu));
Peter Zijlstra039fc912009-03-13 16:43:47 +01003577 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
3578 hwc->hrtimer.function = perf_swcounter_hrtimer;
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003579 if (hwc->sample_period) {
3580 u64 period = max_t(u64, 10000, hwc->sample_period);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003581 __hrtimer_start_range_ns(&hwc->hrtimer,
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003582 ns_to_ktime(period), 0,
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003583 HRTIMER_MODE_REL, 0);
3584 }
3585
3586 return 0;
3587}
3588
Ingo Molnar5c92d122008-12-11 13:21:10 +01003589static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
3590{
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003591 if (counter->hw.sample_period)
Peter Zijlstrab986d7e2009-05-20 12:21:21 +02003592 hrtimer_cancel(&counter->hw.hrtimer);
Paul Mackerras9abf8a02009-01-09 16:26:43 +11003593 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01003594}
3595
3596static void cpu_clock_perf_counter_read(struct perf_counter *counter)
3597{
Paul Mackerras9abf8a02009-01-09 16:26:43 +11003598 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01003599}
3600
Robert Richter4aeb0b42009-04-29 12:47:03 +02003601static const struct pmu perf_ops_cpu_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +01003602 .enable = cpu_clock_perf_counter_enable,
3603 .disable = cpu_clock_perf_counter_disable,
3604 .read = cpu_clock_perf_counter_read,
Ingo Molnar5c92d122008-12-11 13:21:10 +01003605};
3606
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01003607/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003608 * Software counter: task time clock
3609 */
3610
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02003611static void task_clock_perf_counter_update(struct perf_counter *counter, u64 now)
Ingo Molnarbae43c92008-12-11 14:03:20 +01003612{
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02003613 u64 prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003614 s64 delta;
Ingo Molnarbae43c92008-12-11 14:03:20 +01003615
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02003616 prev = atomic64_xchg(&counter->hw.prev_count, now);
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003617 delta = now - prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003618 atomic64_add(delta, &counter->count);
Ingo Molnarbae43c92008-12-11 14:03:20 +01003619}
3620
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01003621static int task_clock_perf_counter_enable(struct perf_counter *counter)
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003622{
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003623 struct hw_perf_counter *hwc = &counter->hw;
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02003624 u64 now;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003625
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02003626 now = counter->ctx->time;
3627
3628 atomic64_set(&hwc->prev_count, now);
Peter Zijlstra039fc912009-03-13 16:43:47 +01003629 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
3630 hwc->hrtimer.function = perf_swcounter_hrtimer;
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003631 if (hwc->sample_period) {
3632 u64 period = max_t(u64, 10000, hwc->sample_period);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003633 __hrtimer_start_range_ns(&hwc->hrtimer,
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003634 ns_to_ktime(period), 0,
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003635 HRTIMER_MODE_REL, 0);
3636 }
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01003637
3638 return 0;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003639}
3640
3641static void task_clock_perf_counter_disable(struct perf_counter *counter)
3642{
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003643 if (counter->hw.sample_period)
Peter Zijlstrab986d7e2009-05-20 12:21:21 +02003644 hrtimer_cancel(&counter->hw.hrtimer);
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02003645 task_clock_perf_counter_update(counter, counter->ctx->time);
3646
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003647}
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01003648
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003649static void task_clock_perf_counter_read(struct perf_counter *counter)
3650{
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02003651 u64 time;
3652
3653 if (!in_nmi()) {
3654 update_context_time(counter->ctx);
3655 time = counter->ctx->time;
3656 } else {
3657 u64 now = perf_clock();
3658 u64 delta = now - counter->ctx->timestamp;
3659 time = counter->ctx->time + delta;
3660 }
3661
3662 task_clock_perf_counter_update(counter, time);
Ingo Molnarbae43c92008-12-11 14:03:20 +01003663}
3664
Robert Richter4aeb0b42009-04-29 12:47:03 +02003665static const struct pmu perf_ops_task_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +01003666 .enable = task_clock_perf_counter_enable,
3667 .disable = task_clock_perf_counter_disable,
3668 .read = task_clock_perf_counter_read,
Ingo Molnarbae43c92008-12-11 14:03:20 +01003669};
3670
Peter Zijlstrae077df42009-03-19 20:26:17 +01003671#ifdef CONFIG_EVENT_PROFILE
3672void perf_tpcounter_event(int event_id)
3673{
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003674 struct perf_sample_data data = {
3675 .regs = get_irq_regs();
3676 .addr = 0,
3677 };
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003678
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003679 if (!data.regs)
3680 data.regs = task_pt_regs(current);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003681
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003682 do_perf_swcounter_event(PERF_TYPE_TRACEPOINT, event_id, 1, 1, &data);
Peter Zijlstrae077df42009-03-19 20:26:17 +01003683}
Steven Whitehouseff7b1b42009-04-15 16:55:05 +01003684EXPORT_SYMBOL_GPL(perf_tpcounter_event);
Peter Zijlstrae077df42009-03-19 20:26:17 +01003685
3686extern int ftrace_profile_enable(int);
3687extern void ftrace_profile_disable(int);
3688
3689static void tp_perf_counter_destroy(struct perf_counter *counter)
3690{
Peter Zijlstra0d486962009-06-02 19:22:16 +02003691 ftrace_profile_disable(perf_event_id(&counter->attr));
Peter Zijlstrae077df42009-03-19 20:26:17 +01003692}
3693
Robert Richter4aeb0b42009-04-29 12:47:03 +02003694static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
Peter Zijlstrae077df42009-03-19 20:26:17 +01003695{
Peter Zijlstra0d486962009-06-02 19:22:16 +02003696 int event_id = perf_event_id(&counter->attr);
Peter Zijlstrae077df42009-03-19 20:26:17 +01003697 int ret;
3698
3699 ret = ftrace_profile_enable(event_id);
3700 if (ret)
3701 return NULL;
3702
3703 counter->destroy = tp_perf_counter_destroy;
3704
3705 return &perf_ops_generic;
3706}
3707#else
Robert Richter4aeb0b42009-04-29 12:47:03 +02003708static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
Peter Zijlstrae077df42009-03-19 20:26:17 +01003709{
3710 return NULL;
3711}
3712#endif
3713
Peter Zijlstraf29ac752009-06-19 18:27:26 +02003714atomic_t perf_swcounter_enabled[PERF_COUNT_SW_MAX];
3715
3716static void sw_perf_counter_destroy(struct perf_counter *counter)
3717{
3718 u64 event = counter->attr.config;
3719
Peter Zijlstraf3440112009-06-22 13:58:35 +02003720 WARN_ON(counter->parent);
3721
Peter Zijlstraf29ac752009-06-19 18:27:26 +02003722 atomic_dec(&perf_swcounter_enabled[event]);
3723}
3724
Robert Richter4aeb0b42009-04-29 12:47:03 +02003725static const struct pmu *sw_perf_counter_init(struct perf_counter *counter)
Ingo Molnar5c92d122008-12-11 13:21:10 +01003726{
Robert Richter4aeb0b42009-04-29 12:47:03 +02003727 const struct pmu *pmu = NULL;
Peter Zijlstraf29ac752009-06-19 18:27:26 +02003728 u64 event = counter->attr.config;
Ingo Molnar5c92d122008-12-11 13:21:10 +01003729
Paul Mackerras0475f9e2009-02-11 14:35:35 +11003730 /*
3731 * Software counters (currently) can't in general distinguish
3732 * between user, kernel and hypervisor events.
3733 * However, context switches and cpu migrations are considered
3734 * to be kernel events, and page faults are never hypervisor
3735 * events.
3736 */
Peter Zijlstraf29ac752009-06-19 18:27:26 +02003737 switch (event) {
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +02003738 case PERF_COUNT_SW_CPU_CLOCK:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003739 pmu = &perf_ops_cpu_clock;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003740
Ingo Molnar5c92d122008-12-11 13:21:10 +01003741 break;
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +02003742 case PERF_COUNT_SW_TASK_CLOCK:
Paul Mackerras23a185c2009-02-09 22:42:47 +11003743 /*
3744 * If the user instantiates this as a per-cpu counter,
3745 * use the cpu_clock counter instead.
3746 */
3747 if (counter->ctx->task)
Robert Richter4aeb0b42009-04-29 12:47:03 +02003748 pmu = &perf_ops_task_clock;
Paul Mackerras23a185c2009-02-09 22:42:47 +11003749 else
Robert Richter4aeb0b42009-04-29 12:47:03 +02003750 pmu = &perf_ops_cpu_clock;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003751
Ingo Molnarbae43c92008-12-11 14:03:20 +01003752 break;
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +02003753 case PERF_COUNT_SW_PAGE_FAULTS:
3754 case PERF_COUNT_SW_PAGE_FAULTS_MIN:
3755 case PERF_COUNT_SW_PAGE_FAULTS_MAJ:
3756 case PERF_COUNT_SW_CONTEXT_SWITCHES:
3757 case PERF_COUNT_SW_CPU_MIGRATIONS:
Peter Zijlstraf3440112009-06-22 13:58:35 +02003758 if (!counter->parent) {
3759 atomic_inc(&perf_swcounter_enabled[event]);
3760 counter->destroy = sw_perf_counter_destroy;
3761 }
Paul Mackerras3f731ca2009-06-01 17:52:30 +10003762 pmu = &perf_ops_generic;
Ingo Molnar6c594c22008-12-14 12:34:15 +01003763 break;
Ingo Molnar5c92d122008-12-11 13:21:10 +01003764 }
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003765
Robert Richter4aeb0b42009-04-29 12:47:03 +02003766 return pmu;
Ingo Molnar5c92d122008-12-11 13:21:10 +01003767}
3768
Thomas Gleixner0793a612008-12-04 20:12:29 +01003769/*
3770 * Allocate and initialize a counter structure
3771 */
3772static struct perf_counter *
Peter Zijlstra0d486962009-06-02 19:22:16 +02003773perf_counter_alloc(struct perf_counter_attr *attr,
Ingo Molnar04289bb2008-12-11 08:38:42 +01003774 int cpu,
Paul Mackerras23a185c2009-02-09 22:42:47 +11003775 struct perf_counter_context *ctx,
Ingo Molnar9b51f662008-12-12 13:49:45 +01003776 struct perf_counter *group_leader,
Peter Zijlstrab84fbc92009-06-22 13:57:40 +02003777 struct perf_counter *parent_counter,
Ingo Molnar9b51f662008-12-12 13:49:45 +01003778 gfp_t gfpflags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003779{
Robert Richter4aeb0b42009-04-29 12:47:03 +02003780 const struct pmu *pmu;
Ingo Molnar621a01e2008-12-11 12:46:46 +01003781 struct perf_counter *counter;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003782 struct hw_perf_counter *hwc;
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003783 long err;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003784
Ingo Molnar9b51f662008-12-12 13:49:45 +01003785 counter = kzalloc(sizeof(*counter), gfpflags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003786 if (!counter)
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003787 return ERR_PTR(-ENOMEM);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003788
Ingo Molnar04289bb2008-12-11 08:38:42 +01003789 /*
3790 * Single counters are their own group leaders, with an
3791 * empty sibling list:
3792 */
3793 if (!group_leader)
3794 group_leader = counter;
3795
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003796 mutex_init(&counter->child_mutex);
3797 INIT_LIST_HEAD(&counter->child_list);
3798
Ingo Molnar04289bb2008-12-11 08:38:42 +01003799 INIT_LIST_HEAD(&counter->list_entry);
Peter Zijlstra592903c2009-03-13 12:21:36 +01003800 INIT_LIST_HEAD(&counter->event_entry);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003801 INIT_LIST_HEAD(&counter->sibling_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003802 init_waitqueue_head(&counter->waitq);
3803
Peter Zijlstra7b732a72009-03-23 18:22:10 +01003804 mutex_init(&counter->mmap_mutex);
3805
Peter Zijlstraa96bbc12009-06-03 14:01:36 +02003806 counter->cpu = cpu;
Peter Zijlstra0d486962009-06-02 19:22:16 +02003807 counter->attr = *attr;
Peter Zijlstraa96bbc12009-06-03 14:01:36 +02003808 counter->group_leader = group_leader;
3809 counter->pmu = NULL;
3810 counter->ctx = ctx;
3811 counter->oncpu = -1;
Ingo Molnar329d8762009-05-26 08:10:00 +02003812
Peter Zijlstrab84fbc92009-06-22 13:57:40 +02003813 counter->parent = parent_counter;
3814
Peter Zijlstraa96bbc12009-06-03 14:01:36 +02003815 counter->ns = get_pid_ns(current->nsproxy->pid_ns);
3816 counter->id = atomic64_inc_return(&perf_counter_id);
3817
3818 counter->state = PERF_COUNTER_STATE_INACTIVE;
3819
Peter Zijlstra0d486962009-06-02 19:22:16 +02003820 if (attr->disabled)
Ingo Molnara86ed502008-12-17 00:43:10 +01003821 counter->state = PERF_COUNTER_STATE_OFF;
3822
Robert Richter4aeb0b42009-04-29 12:47:03 +02003823 pmu = NULL;
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003824
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003825 hwc = &counter->hw;
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02003826 hwc->sample_period = attr->sample_period;
Peter Zijlstra0d486962009-06-02 19:22:16 +02003827 if (attr->freq && attr->sample_freq)
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02003828 hwc->sample_period = 1;
3829
3830 atomic64_set(&hwc->period_left, hwc->sample_period);
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003831
Peter Zijlstra2023b352009-05-05 17:50:26 +02003832 /*
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003833 * we currently do not support PERF_SAMPLE_GROUP on inherited counters
Peter Zijlstra2023b352009-05-05 17:50:26 +02003834 */
Peter Zijlstra0d486962009-06-02 19:22:16 +02003835 if (attr->inherit && (attr->sample_type & PERF_SAMPLE_GROUP))
Peter Zijlstra2023b352009-05-05 17:50:26 +02003836 goto done;
3837
Ingo Molnara21ca2c2009-06-06 09:58:57 +02003838 switch (attr->type) {
Peter Zijlstra081fad82009-06-11 17:57:21 +02003839 case PERF_TYPE_RAW:
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003840 case PERF_TYPE_HARDWARE:
Ingo Molnar8326f442009-06-05 20:22:46 +02003841 case PERF_TYPE_HW_CACHE:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003842 pmu = hw_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003843 break;
3844
3845 case PERF_TYPE_SOFTWARE:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003846 pmu = sw_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003847 break;
3848
3849 case PERF_TYPE_TRACEPOINT:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003850 pmu = tp_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003851 break;
Peter Zijlstra974802e2009-06-12 12:46:55 +02003852
3853 default:
3854 break;
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003855 }
Peter Zijlstraf4a2deb42009-03-23 18:22:06 +01003856done:
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003857 err = 0;
Robert Richter4aeb0b42009-04-29 12:47:03 +02003858 if (!pmu)
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003859 err = -EINVAL;
Robert Richter4aeb0b42009-04-29 12:47:03 +02003860 else if (IS_ERR(pmu))
3861 err = PTR_ERR(pmu);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003862
3863 if (err) {
Peter Zijlstraa96bbc12009-06-03 14:01:36 +02003864 if (counter->ns)
3865 put_pid_ns(counter->ns);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003866 kfree(counter);
3867 return ERR_PTR(err);
3868 }
3869
Robert Richter4aeb0b42009-04-29 12:47:03 +02003870 counter->pmu = pmu;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003871
Peter Zijlstraf3440112009-06-22 13:58:35 +02003872 if (!counter->parent) {
3873 atomic_inc(&nr_counters);
3874 if (counter->attr.mmap)
3875 atomic_inc(&nr_mmap_counters);
3876 if (counter->attr.comm)
3877 atomic_inc(&nr_comm_counters);
3878 }
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02003879
Thomas Gleixner0793a612008-12-04 20:12:29 +01003880 return counter;
3881}
3882
Peter Zijlstra974802e2009-06-12 12:46:55 +02003883static int perf_copy_attr(struct perf_counter_attr __user *uattr,
3884 struct perf_counter_attr *attr)
3885{
3886 int ret;
3887 u32 size;
3888
3889 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
3890 return -EFAULT;
3891
3892 /*
3893 * zero the full structure, so that a short copy will be nice.
3894 */
3895 memset(attr, 0, sizeof(*attr));
3896
3897 ret = get_user(size, &uattr->size);
3898 if (ret)
3899 return ret;
3900
3901 if (size > PAGE_SIZE) /* silly large */
3902 goto err_size;
3903
3904 if (!size) /* abi compat */
3905 size = PERF_ATTR_SIZE_VER0;
3906
3907 if (size < PERF_ATTR_SIZE_VER0)
3908 goto err_size;
3909
3910 /*
3911 * If we're handed a bigger struct than we know of,
3912 * ensure all the unknown bits are 0.
3913 */
3914 if (size > sizeof(*attr)) {
3915 unsigned long val;
3916 unsigned long __user *addr;
3917 unsigned long __user *end;
3918
3919 addr = PTR_ALIGN((void __user *)uattr + sizeof(*attr),
3920 sizeof(unsigned long));
3921 end = PTR_ALIGN((void __user *)uattr + size,
3922 sizeof(unsigned long));
3923
3924 for (; addr < end; addr += sizeof(unsigned long)) {
3925 ret = get_user(val, addr);
3926 if (ret)
3927 return ret;
3928 if (val)
3929 goto err_size;
3930 }
3931 }
3932
3933 ret = copy_from_user(attr, uattr, size);
3934 if (ret)
3935 return -EFAULT;
3936
3937 /*
3938 * If the type exists, the corresponding creation will verify
3939 * the attr->config.
3940 */
3941 if (attr->type >= PERF_TYPE_MAX)
3942 return -EINVAL;
3943
3944 if (attr->__reserved_1 || attr->__reserved_2 || attr->__reserved_3)
3945 return -EINVAL;
3946
3947 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
3948 return -EINVAL;
3949
3950 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
3951 return -EINVAL;
3952
3953out:
3954 return ret;
3955
3956err_size:
3957 put_user(sizeof(*attr), &uattr->size);
3958 ret = -E2BIG;
3959 goto out;
3960}
3961
Thomas Gleixner0793a612008-12-04 20:12:29 +01003962/**
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003963 * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
Ingo Molnar9f66a382008-12-10 12:33:23 +01003964 *
Peter Zijlstra0d486962009-06-02 19:22:16 +02003965 * @attr_uptr: event type attributes for monitoring/sampling
Thomas Gleixner0793a612008-12-04 20:12:29 +01003966 * @pid: target pid
Ingo Molnar9f66a382008-12-10 12:33:23 +01003967 * @cpu: target cpu
3968 * @group_fd: group leader counter fd
Thomas Gleixner0793a612008-12-04 20:12:29 +01003969 */
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003970SYSCALL_DEFINE5(perf_counter_open,
Peter Zijlstra974802e2009-06-12 12:46:55 +02003971 struct perf_counter_attr __user *, attr_uptr,
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003972 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003973{
Ingo Molnar04289bb2008-12-11 08:38:42 +01003974 struct perf_counter *counter, *group_leader;
Peter Zijlstra0d486962009-06-02 19:22:16 +02003975 struct perf_counter_attr attr;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003976 struct perf_counter_context *ctx;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003977 struct file *counter_file = NULL;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003978 struct file *group_file = NULL;
3979 int fput_needed = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003980 int fput_needed2 = 0;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003981 int ret;
3982
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003983 /* for future expandability... */
3984 if (flags)
3985 return -EINVAL;
3986
Peter Zijlstra974802e2009-06-12 12:46:55 +02003987 ret = perf_copy_attr(attr_uptr, &attr);
3988 if (ret)
3989 return ret;
Thomas Gleixnereab656a2008-12-08 19:26:59 +01003990
Peter Zijlstra07647712009-06-11 11:18:36 +02003991 if (!attr.exclude_kernel) {
3992 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
3993 return -EACCES;
3994 }
3995
Peter Zijlstradf58ab22009-06-11 11:25:05 +02003996 if (attr.freq) {
3997 if (attr.sample_freq > sysctl_perf_counter_sample_rate)
3998 return -EINVAL;
3999 }
4000
Ingo Molnar04289bb2008-12-11 08:38:42 +01004001 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01004002 * Get the target context (task or percpu):
4003 */
4004 ctx = find_get_context(pid, cpu);
4005 if (IS_ERR(ctx))
4006 return PTR_ERR(ctx);
4007
4008 /*
4009 * Look up the group leader (we will attach this counter to it):
Ingo Molnar04289bb2008-12-11 08:38:42 +01004010 */
4011 group_leader = NULL;
4012 if (group_fd != -1) {
4013 ret = -EINVAL;
4014 group_file = fget_light(group_fd, &fput_needed);
4015 if (!group_file)
Ingo Molnarccff2862008-12-11 11:26:29 +01004016 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01004017 if (group_file->f_op != &perf_fops)
Ingo Molnarccff2862008-12-11 11:26:29 +01004018 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01004019
4020 group_leader = group_file->private_data;
4021 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01004022 * Do not allow a recursive hierarchy (this new sibling
4023 * becoming part of another group-sibling):
Ingo Molnar04289bb2008-12-11 08:38:42 +01004024 */
Ingo Molnarccff2862008-12-11 11:26:29 +01004025 if (group_leader->group_leader != group_leader)
4026 goto err_put_context;
4027 /*
4028 * Do not allow to attach to a group in a different
4029 * task or CPU context:
4030 */
4031 if (group_leader->ctx != ctx)
4032 goto err_put_context;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11004033 /*
4034 * Only a group leader can be exclusive or pinned
4035 */
Peter Zijlstra0d486962009-06-02 19:22:16 +02004036 if (attr.exclusive || attr.pinned)
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11004037 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01004038 }
4039
Peter Zijlstra0d486962009-06-02 19:22:16 +02004040 counter = perf_counter_alloc(&attr, cpu, ctx, group_leader,
Peter Zijlstrab84fbc92009-06-22 13:57:40 +02004041 NULL, GFP_KERNEL);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02004042 ret = PTR_ERR(counter);
4043 if (IS_ERR(counter))
Thomas Gleixner0793a612008-12-04 20:12:29 +01004044 goto err_put_context;
4045
Thomas Gleixner0793a612008-12-04 20:12:29 +01004046 ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
4047 if (ret < 0)
Ingo Molnar9b51f662008-12-12 13:49:45 +01004048 goto err_free_put_context;
4049
4050 counter_file = fget_light(ret, &fput_needed2);
4051 if (!counter_file)
4052 goto err_free_put_context;
4053
4054 counter->filp = counter_file;
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004055 WARN_ON_ONCE(ctx->parent_ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +11004056 mutex_lock(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01004057 perf_install_in_context(ctx, counter, cpu);
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004058 ++ctx->generation;
Paul Mackerrasd859e292009-01-17 18:10:22 +11004059 mutex_unlock(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01004060
Peter Zijlstra082ff5a2009-05-23 18:29:00 +02004061 counter->owner = current;
4062 get_task_struct(current);
4063 mutex_lock(&current->perf_counter_mutex);
4064 list_add_tail(&counter->owner_entry, &current->perf_counter_list);
4065 mutex_unlock(&current->perf_counter_mutex);
4066
Ingo Molnar9b51f662008-12-12 13:49:45 +01004067 fput_light(counter_file, fput_needed2);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004068
Ingo Molnar04289bb2008-12-11 08:38:42 +01004069out_fput:
4070 fput_light(group_file, fput_needed);
4071
Thomas Gleixner0793a612008-12-04 20:12:29 +01004072 return ret;
4073
Ingo Molnar9b51f662008-12-12 13:49:45 +01004074err_free_put_context:
Thomas Gleixner0793a612008-12-04 20:12:29 +01004075 kfree(counter);
4076
4077err_put_context:
Paul Mackerrasc93f7662009-05-28 22:18:17 +10004078 put_ctx(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004079
Ingo Molnar04289bb2008-12-11 08:38:42 +01004080 goto out_fput;
Thomas Gleixner0793a612008-12-04 20:12:29 +01004081}
4082
Ingo Molnar9b51f662008-12-12 13:49:45 +01004083/*
Ingo Molnar9b51f662008-12-12 13:49:45 +01004084 * inherit a counter from parent task to child task:
4085 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11004086static struct perf_counter *
Ingo Molnar9b51f662008-12-12 13:49:45 +01004087inherit_counter(struct perf_counter *parent_counter,
4088 struct task_struct *parent,
4089 struct perf_counter_context *parent_ctx,
4090 struct task_struct *child,
Paul Mackerrasd859e292009-01-17 18:10:22 +11004091 struct perf_counter *group_leader,
Ingo Molnar9b51f662008-12-12 13:49:45 +01004092 struct perf_counter_context *child_ctx)
4093{
4094 struct perf_counter *child_counter;
4095
Paul Mackerrasd859e292009-01-17 18:10:22 +11004096 /*
4097 * Instead of creating recursive hierarchies of counters,
4098 * we link inherited counters back to the original parent,
4099 * which has a filp for sure, which we use as the reference
4100 * count:
4101 */
4102 if (parent_counter->parent)
4103 parent_counter = parent_counter->parent;
4104
Peter Zijlstra0d486962009-06-02 19:22:16 +02004105 child_counter = perf_counter_alloc(&parent_counter->attr,
Paul Mackerras23a185c2009-02-09 22:42:47 +11004106 parent_counter->cpu, child_ctx,
Peter Zijlstrab84fbc92009-06-22 13:57:40 +02004107 group_leader, parent_counter,
4108 GFP_KERNEL);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02004109 if (IS_ERR(child_counter))
4110 return child_counter;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10004111 get_ctx(child_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01004112
4113 /*
Paul Mackerras564c2b22009-05-22 14:27:22 +10004114 * Make the child state follow the state of the parent counter,
Peter Zijlstra0d486962009-06-02 19:22:16 +02004115 * not its attr.disabled bit. We hold the parent's mutex,
Ingo Molnar22a4f652009-06-01 10:13:37 +02004116 * so we won't race with perf_counter_{en, dis}able_family.
Paul Mackerras564c2b22009-05-22 14:27:22 +10004117 */
4118 if (parent_counter->state >= PERF_COUNTER_STATE_INACTIVE)
4119 child_counter->state = PERF_COUNTER_STATE_INACTIVE;
4120 else
4121 child_counter->state = PERF_COUNTER_STATE_OFF;
4122
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02004123 if (parent_counter->attr.freq)
4124 child_counter->hw.sample_period = parent_counter->hw.sample_period;
4125
Paul Mackerras564c2b22009-05-22 14:27:22 +10004126 /*
Ingo Molnar9b51f662008-12-12 13:49:45 +01004127 * Link it up in the child's context:
4128 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +11004129 add_counter_to_ctx(child_counter, child_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01004130
Ingo Molnar9b51f662008-12-12 13:49:45 +01004131 /*
4132 * Get a reference to the parent filp - we will fput it
4133 * when the child counter exits. This is safe to do because
4134 * we are in the parent and we know that the filp still
4135 * exists and has a nonzero count:
4136 */
4137 atomic_long_inc(&parent_counter->filp->f_count);
4138
Paul Mackerrasd859e292009-01-17 18:10:22 +11004139 /*
4140 * Link this into the parent counter's child list
4141 */
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004142 WARN_ON_ONCE(parent_counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02004143 mutex_lock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11004144 list_add_tail(&child_counter->child_list, &parent_counter->child_list);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02004145 mutex_unlock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11004146
4147 return child_counter;
4148}
4149
4150static int inherit_group(struct perf_counter *parent_counter,
4151 struct task_struct *parent,
4152 struct perf_counter_context *parent_ctx,
4153 struct task_struct *child,
4154 struct perf_counter_context *child_ctx)
4155{
4156 struct perf_counter *leader;
4157 struct perf_counter *sub;
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02004158 struct perf_counter *child_ctr;
Paul Mackerrasd859e292009-01-17 18:10:22 +11004159
4160 leader = inherit_counter(parent_counter, parent, parent_ctx,
4161 child, NULL, child_ctx);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02004162 if (IS_ERR(leader))
4163 return PTR_ERR(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +11004164 list_for_each_entry(sub, &parent_counter->sibling_list, list_entry) {
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02004165 child_ctr = inherit_counter(sub, parent, parent_ctx,
4166 child, leader, child_ctx);
4167 if (IS_ERR(child_ctr))
4168 return PTR_ERR(child_ctr);
Paul Mackerrasd859e292009-01-17 18:10:22 +11004169 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01004170 return 0;
4171}
4172
Paul Mackerrasd859e292009-01-17 18:10:22 +11004173static void sync_child_counter(struct perf_counter *child_counter,
Peter Zijlstra38b200d2009-06-23 20:13:11 +02004174 struct task_struct *child)
Paul Mackerrasd859e292009-01-17 18:10:22 +11004175{
Peter Zijlstra38b200d2009-06-23 20:13:11 +02004176 struct perf_counter *parent_counter = child_counter->parent;
Peter Zijlstra8bc20952009-05-15 20:45:59 +02004177 u64 child_val;
Paul Mackerrasd859e292009-01-17 18:10:22 +11004178
Peter Zijlstrabfbd3382009-06-24 21:11:59 +02004179 if (child_counter->attr.inherit_stat)
4180 perf_counter_read_event(child_counter, child);
Peter Zijlstra38b200d2009-06-23 20:13:11 +02004181
Paul Mackerrasd859e292009-01-17 18:10:22 +11004182 child_val = atomic64_read(&child_counter->count);
4183
4184 /*
4185 * Add back the child's count to the parent's count:
4186 */
4187 atomic64_add(child_val, &parent_counter->count);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11004188 atomic64_add(child_counter->total_time_enabled,
4189 &parent_counter->child_total_time_enabled);
4190 atomic64_add(child_counter->total_time_running,
4191 &parent_counter->child_total_time_running);
Paul Mackerrasd859e292009-01-17 18:10:22 +11004192
4193 /*
4194 * Remove this counter from the parent's list
4195 */
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004196 WARN_ON_ONCE(parent_counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02004197 mutex_lock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11004198 list_del_init(&child_counter->child_list);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02004199 mutex_unlock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11004200
4201 /*
4202 * Release the parent counter, if this was the last
4203 * reference to it.
4204 */
4205 fput(parent_counter->filp);
4206}
4207
Ingo Molnar9b51f662008-12-12 13:49:45 +01004208static void
Peter Zijlstrabbbee902009-05-29 14:25:58 +02004209__perf_counter_exit_task(struct perf_counter *child_counter,
Peter Zijlstra38b200d2009-06-23 20:13:11 +02004210 struct perf_counter_context *child_ctx,
4211 struct task_struct *child)
Ingo Molnar9b51f662008-12-12 13:49:45 +01004212{
4213 struct perf_counter *parent_counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004214
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004215 update_counter_times(child_counter);
Peter Zijlstraaa9c67f2009-05-23 18:28:59 +02004216 perf_counter_remove_from_context(child_counter);
Ingo Molnar0cc0c022008-12-14 23:20:36 +01004217
Ingo Molnar9b51f662008-12-12 13:49:45 +01004218 parent_counter = child_counter->parent;
4219 /*
4220 * It can happen that parent exits first, and has counters
4221 * that are still around due to the child reference. These
4222 * counters need to be zapped - but otherwise linger.
4223 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11004224 if (parent_counter) {
Peter Zijlstra38b200d2009-06-23 20:13:11 +02004225 sync_child_counter(child_counter, child);
Peter Zijlstraf1600952009-03-19 20:26:16 +01004226 free_counter(child_counter);
Paul Mackerras4bcf3492009-02-11 13:53:19 +01004227 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01004228}
4229
4230/*
Paul Mackerrasd859e292009-01-17 18:10:22 +11004231 * When a child task exits, feed back counter values to parent counters.
Ingo Molnar9b51f662008-12-12 13:49:45 +01004232 */
4233void perf_counter_exit_task(struct task_struct *child)
4234{
4235 struct perf_counter *child_counter, *tmp;
4236 struct perf_counter_context *child_ctx;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004237 unsigned long flags;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004238
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004239 if (likely(!child->perf_counter_ctxp))
Ingo Molnar9b51f662008-12-12 13:49:45 +01004240 return;
4241
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004242 local_irq_save(flags);
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004243 /*
4244 * We can't reschedule here because interrupts are disabled,
4245 * and either child is current or it is a task that can't be
4246 * scheduled, so we are now safe from rescheduling changing
4247 * our context.
4248 */
4249 child_ctx = child->perf_counter_ctxp;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004250 __perf_counter_task_sched_out(child_ctx);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10004251
4252 /*
4253 * Take the context lock here so that if find_get_context is
4254 * reading child->perf_counter_ctxp, we wait until it has
4255 * incremented the context's refcount before we do put_ctx below.
4256 */
4257 spin_lock(&child_ctx->lock);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004258 child->perf_counter_ctxp = NULL;
Peter Zijlstra71a851b2009-07-10 09:06:56 +02004259 /*
4260 * If this context is a clone; unclone it so it can't get
4261 * swapped to another process while we're removing all
4262 * the counters from it.
4263 */
4264 unclone_ctx(child_ctx);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10004265 spin_unlock(&child_ctx->lock);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004266 local_irq_restore(flags);
4267
Peter Zijlstra66fff222009-06-10 22:53:37 +02004268 /*
4269 * We can recurse on the same lock type through:
4270 *
4271 * __perf_counter_exit_task()
4272 * sync_child_counter()
4273 * fput(parent_counter->filp)
4274 * perf_release()
4275 * mutex_lock(&ctx->mutex)
4276 *
4277 * But since its the parent context it won't be the same instance.
4278 */
4279 mutex_lock_nested(&child_ctx->mutex, SINGLE_DEPTH_NESTING);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004280
Peter Zijlstra8bc20952009-05-15 20:45:59 +02004281again:
Ingo Molnar9b51f662008-12-12 13:49:45 +01004282 list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list,
4283 list_entry)
Peter Zijlstra38b200d2009-06-23 20:13:11 +02004284 __perf_counter_exit_task(child_counter, child_ctx, child);
Peter Zijlstra8bc20952009-05-15 20:45:59 +02004285
4286 /*
4287 * If the last counter was a group counter, it will have appended all
4288 * its siblings to the list, but we obtained 'tmp' before that which
4289 * will still point to the list head terminating the iteration.
4290 */
4291 if (!list_empty(&child_ctx->counter_list))
4292 goto again;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004293
4294 mutex_unlock(&child_ctx->mutex);
4295
4296 put_ctx(child_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01004297}
4298
4299/*
Peter Zijlstrabbbee902009-05-29 14:25:58 +02004300 * free an unexposed, unused context as created by inheritance by
4301 * init_task below, used by fork() in case of fail.
4302 */
4303void perf_counter_free_task(struct task_struct *task)
4304{
4305 struct perf_counter_context *ctx = task->perf_counter_ctxp;
4306 struct perf_counter *counter, *tmp;
4307
4308 if (!ctx)
4309 return;
4310
4311 mutex_lock(&ctx->mutex);
4312again:
4313 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry) {
4314 struct perf_counter *parent = counter->parent;
4315
4316 if (WARN_ON_ONCE(!parent))
4317 continue;
4318
4319 mutex_lock(&parent->child_mutex);
4320 list_del_init(&counter->child_list);
4321 mutex_unlock(&parent->child_mutex);
4322
4323 fput(parent->filp);
4324
4325 list_del_counter(counter, ctx);
4326 free_counter(counter);
4327 }
4328
4329 if (!list_empty(&ctx->counter_list))
4330 goto again;
4331
4332 mutex_unlock(&ctx->mutex);
4333
4334 put_ctx(ctx);
4335}
4336
4337/*
Ingo Molnar9b51f662008-12-12 13:49:45 +01004338 * Initialize the perf_counter context in task_struct
4339 */
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02004340int perf_counter_init_task(struct task_struct *child)
Ingo Molnar9b51f662008-12-12 13:49:45 +01004341{
4342 struct perf_counter_context *child_ctx, *parent_ctx;
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004343 struct perf_counter_context *cloned_ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +11004344 struct perf_counter *counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004345 struct task_struct *parent = current;
Paul Mackerras564c2b22009-05-22 14:27:22 +10004346 int inherited_all = 1;
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02004347 int ret = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004348
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004349 child->perf_counter_ctxp = NULL;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004350
Peter Zijlstra082ff5a2009-05-23 18:29:00 +02004351 mutex_init(&child->perf_counter_mutex);
4352 INIT_LIST_HEAD(&child->perf_counter_list);
4353
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004354 if (likely(!parent->perf_counter_ctxp))
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02004355 return 0;
4356
Ingo Molnar9b51f662008-12-12 13:49:45 +01004357 /*
4358 * This is executed from the parent task context, so inherit
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004359 * counters that have been marked for cloning.
4360 * First allocate and initialize a context for the child.
Ingo Molnar9b51f662008-12-12 13:49:45 +01004361 */
4362
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004363 child_ctx = kmalloc(sizeof(struct perf_counter_context), GFP_KERNEL);
4364 if (!child_ctx)
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02004365 return -ENOMEM;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004366
4367 __perf_counter_init_context(child_ctx, child);
4368 child->perf_counter_ctxp = child_ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10004369 get_task_struct(child);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004370
Ingo Molnar9b51f662008-12-12 13:49:45 +01004371 /*
Paul Mackerras25346b932009-06-01 17:48:12 +10004372 * If the parent's context is a clone, pin it so it won't get
4373 * swapped under us.
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004374 */
Paul Mackerras25346b932009-06-01 17:48:12 +10004375 parent_ctx = perf_pin_task_context(parent);
4376
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004377 /*
4378 * No need to check if parent_ctx != NULL here; since we saw
4379 * it non-NULL earlier, the only reason for it to become NULL
4380 * is if we exit, and since we're currently in the middle of
4381 * a fork we can't be exiting at the same time.
4382 */
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004383
4384 /*
Ingo Molnar9b51f662008-12-12 13:49:45 +01004385 * Lock the parent list. No need to lock the child - not PID
4386 * hashed yet and not running, so nobody can access it.
4387 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11004388 mutex_lock(&parent_ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01004389
4390 /*
4391 * We dont have to disable NMIs - we are only looking at
4392 * the list, not manipulating it:
4393 */
Peter Zijlstrad7b629a2009-05-20 12:21:19 +02004394 list_for_each_entry_rcu(counter, &parent_ctx->event_list, event_entry) {
4395 if (counter != counter->group_leader)
4396 continue;
4397
Peter Zijlstra0d486962009-06-02 19:22:16 +02004398 if (!counter->attr.inherit) {
Paul Mackerras564c2b22009-05-22 14:27:22 +10004399 inherited_all = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004400 continue;
Paul Mackerras564c2b22009-05-22 14:27:22 +10004401 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01004402
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02004403 ret = inherit_group(counter, parent, parent_ctx,
4404 child, child_ctx);
4405 if (ret) {
Paul Mackerras564c2b22009-05-22 14:27:22 +10004406 inherited_all = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004407 break;
Paul Mackerras564c2b22009-05-22 14:27:22 +10004408 }
4409 }
4410
4411 if (inherited_all) {
4412 /*
4413 * Mark the child context as a clone of the parent
4414 * context, or of whatever the parent is a clone of.
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004415 * Note that if the parent is a clone, it could get
4416 * uncloned at any point, but that doesn't matter
4417 * because the list of counters and the generation
4418 * count can't have changed since we took the mutex.
Paul Mackerras564c2b22009-05-22 14:27:22 +10004419 */
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004420 cloned_ctx = rcu_dereference(parent_ctx->parent_ctx);
4421 if (cloned_ctx) {
4422 child_ctx->parent_ctx = cloned_ctx;
Paul Mackerras25346b932009-06-01 17:48:12 +10004423 child_ctx->parent_gen = parent_ctx->parent_gen;
Paul Mackerras564c2b22009-05-22 14:27:22 +10004424 } else {
4425 child_ctx->parent_ctx = parent_ctx;
4426 child_ctx->parent_gen = parent_ctx->generation;
4427 }
4428 get_ctx(child_ctx->parent_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01004429 }
4430
Paul Mackerrasd859e292009-01-17 18:10:22 +11004431 mutex_unlock(&parent_ctx->mutex);
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02004432
Paul Mackerras25346b932009-06-01 17:48:12 +10004433 perf_unpin_context(parent_ctx);
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004434
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02004435 return ret;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004436}
4437
Ingo Molnar04289bb2008-12-11 08:38:42 +01004438static void __cpuinit perf_counter_init_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01004439{
Ingo Molnar04289bb2008-12-11 08:38:42 +01004440 struct perf_cpu_context *cpuctx;
Thomas Gleixner0793a612008-12-04 20:12:29 +01004441
Ingo Molnar04289bb2008-12-11 08:38:42 +01004442 cpuctx = &per_cpu(perf_cpu_context, cpu);
4443 __perf_counter_init_context(&cpuctx->ctx, NULL);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004444
Ingo Molnar1dce8d92009-05-04 19:23:18 +02004445 spin_lock(&perf_resource_lock);
Ingo Molnar04289bb2008-12-11 08:38:42 +01004446 cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu;
Ingo Molnar1dce8d92009-05-04 19:23:18 +02004447 spin_unlock(&perf_resource_lock);
Ingo Molnar04289bb2008-12-11 08:38:42 +01004448
Paul Mackerras01d02872009-01-14 13:44:19 +11004449 hw_perf_counter_setup(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004450}
4451
4452#ifdef CONFIG_HOTPLUG_CPU
Ingo Molnar04289bb2008-12-11 08:38:42 +01004453static void __perf_counter_exit_cpu(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01004454{
4455 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
4456 struct perf_counter_context *ctx = &cpuctx->ctx;
4457 struct perf_counter *counter, *tmp;
4458
Ingo Molnar04289bb2008-12-11 08:38:42 +01004459 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry)
4460 __perf_counter_remove_from_context(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004461}
Ingo Molnar04289bb2008-12-11 08:38:42 +01004462static void perf_counter_exit_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01004463{
Paul Mackerrasd859e292009-01-17 18:10:22 +11004464 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
4465 struct perf_counter_context *ctx = &cpuctx->ctx;
4466
4467 mutex_lock(&ctx->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01004468 smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1);
Paul Mackerrasd859e292009-01-17 18:10:22 +11004469 mutex_unlock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004470}
4471#else
Ingo Molnar04289bb2008-12-11 08:38:42 +01004472static inline void perf_counter_exit_cpu(int cpu) { }
Thomas Gleixner0793a612008-12-04 20:12:29 +01004473#endif
4474
4475static int __cpuinit
4476perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
4477{
4478 unsigned int cpu = (long)hcpu;
4479
4480 switch (action) {
4481
4482 case CPU_UP_PREPARE:
4483 case CPU_UP_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01004484 perf_counter_init_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004485 break;
4486
4487 case CPU_DOWN_PREPARE:
4488 case CPU_DOWN_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01004489 perf_counter_exit_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004490 break;
4491
4492 default:
4493 break;
4494 }
4495
4496 return NOTIFY_OK;
4497}
4498
Paul Mackerrasf38b0822009-06-02 21:05:16 +10004499/*
4500 * This has to have a higher priority than migration_notifier in sched.c.
4501 */
Thomas Gleixner0793a612008-12-04 20:12:29 +01004502static struct notifier_block __cpuinitdata perf_cpu_nb = {
4503 .notifier_call = perf_cpu_notify,
Paul Mackerrasf38b0822009-06-02 21:05:16 +10004504 .priority = 20,
Thomas Gleixner0793a612008-12-04 20:12:29 +01004505};
4506
Ingo Molnar0d905bc2009-05-04 19:13:30 +02004507void __init perf_counter_init(void)
Thomas Gleixner0793a612008-12-04 20:12:29 +01004508{
4509 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
4510 (void *)(long)smp_processor_id());
4511 register_cpu_notifier(&perf_cpu_nb);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004512}
Thomas Gleixner0793a612008-12-04 20:12:29 +01004513
4514static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
4515{
4516 return sprintf(buf, "%d\n", perf_reserved_percpu);
4517}
4518
4519static ssize_t
4520perf_set_reserve_percpu(struct sysdev_class *class,
4521 const char *buf,
4522 size_t count)
4523{
4524 struct perf_cpu_context *cpuctx;
4525 unsigned long val;
4526 int err, cpu, mpt;
4527
4528 err = strict_strtoul(buf, 10, &val);
4529 if (err)
4530 return err;
4531 if (val > perf_max_counters)
4532 return -EINVAL;
4533
Ingo Molnar1dce8d92009-05-04 19:23:18 +02004534 spin_lock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004535 perf_reserved_percpu = val;
4536 for_each_online_cpu(cpu) {
4537 cpuctx = &per_cpu(perf_cpu_context, cpu);
4538 spin_lock_irq(&cpuctx->ctx.lock);
4539 mpt = min(perf_max_counters - cpuctx->ctx.nr_counters,
4540 perf_max_counters - perf_reserved_percpu);
4541 cpuctx->max_pertask = mpt;
4542 spin_unlock_irq(&cpuctx->ctx.lock);
4543 }
Ingo Molnar1dce8d92009-05-04 19:23:18 +02004544 spin_unlock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004545
4546 return count;
4547}
4548
4549static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
4550{
4551 return sprintf(buf, "%d\n", perf_overcommit);
4552}
4553
4554static ssize_t
4555perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
4556{
4557 unsigned long val;
4558 int err;
4559
4560 err = strict_strtoul(buf, 10, &val);
4561 if (err)
4562 return err;
4563 if (val > 1)
4564 return -EINVAL;
4565
Ingo Molnar1dce8d92009-05-04 19:23:18 +02004566 spin_lock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004567 perf_overcommit = val;
Ingo Molnar1dce8d92009-05-04 19:23:18 +02004568 spin_unlock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004569
4570 return count;
4571}
4572
4573static SYSDEV_CLASS_ATTR(
4574 reserve_percpu,
4575 0644,
4576 perf_show_reserve_percpu,
4577 perf_set_reserve_percpu
4578 );
4579
4580static SYSDEV_CLASS_ATTR(
4581 overcommit,
4582 0644,
4583 perf_show_overcommit,
4584 perf_set_overcommit
4585 );
4586
4587static struct attribute *perfclass_attrs[] = {
4588 &attr_reserve_percpu.attr,
4589 &attr_overcommit.attr,
4590 NULL
4591};
4592
4593static struct attribute_group perfclass_attr_group = {
4594 .attrs = perfclass_attrs,
4595 .name = "perf_counters",
4596};
4597
4598static int __init perf_counter_sysfs_init(void)
4599{
4600 return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
4601 &perfclass_attr_group);
4602}
4603device_initcall(perf_counter_sysfs_init);