blob: 7e9108efd3053e6dafccf81d82e304a7cc776327 [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{
127 atomic_inc(&ctx->refcount);
128}
129
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000130static void free_ctx(struct rcu_head *head)
131{
132 struct perf_counter_context *ctx;
133
134 ctx = container_of(head, struct perf_counter_context, rcu_head);
135 kfree(ctx);
136}
137
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000138static void put_ctx(struct perf_counter_context *ctx)
139{
Paul Mackerras564c2b22009-05-22 14:27:22 +1000140 if (atomic_dec_and_test(&ctx->refcount)) {
141 if (ctx->parent_ctx)
142 put_ctx(ctx->parent_ctx);
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000143 if (ctx->task)
144 put_task_struct(ctx->task);
145 call_rcu(&ctx->rcu_head, free_ctx);
Paul Mackerras564c2b22009-05-22 14:27:22 +1000146 }
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000147}
148
Peter Zijlstrafccc7142009-05-23 18:28:56 +0200149/*
Paul Mackerras25346b932009-06-01 17:48:12 +1000150 * Get the perf_counter_context for a task and lock it.
151 * This has to cope with with the fact that until it is locked,
152 * the context could get moved to another task.
153 */
Ingo Molnar22a4f652009-06-01 10:13:37 +0200154static struct perf_counter_context *
155perf_lock_task_context(struct task_struct *task, unsigned long *flags)
Paul Mackerras25346b932009-06-01 17:48:12 +1000156{
157 struct perf_counter_context *ctx;
158
159 rcu_read_lock();
160 retry:
161 ctx = rcu_dereference(task->perf_counter_ctxp);
162 if (ctx) {
163 /*
164 * If this context is a clone of another, it might
165 * get swapped for another underneath us by
166 * perf_counter_task_sched_out, though the
167 * rcu_read_lock() protects us from any context
168 * getting freed. Lock the context and check if it
169 * got swapped before we could get the lock, and retry
170 * if so. If we locked the right context, then it
171 * can't get swapped on us any more.
172 */
173 spin_lock_irqsave(&ctx->lock, *flags);
174 if (ctx != rcu_dereference(task->perf_counter_ctxp)) {
175 spin_unlock_irqrestore(&ctx->lock, *flags);
176 goto retry;
177 }
178 }
179 rcu_read_unlock();
180 return ctx;
181}
182
183/*
184 * Get the context for a task and increment its pin_count so it
185 * can't get swapped to another task. This also increments its
186 * reference count so that the context can't get freed.
187 */
188static struct perf_counter_context *perf_pin_task_context(struct task_struct *task)
189{
190 struct perf_counter_context *ctx;
191 unsigned long flags;
192
193 ctx = perf_lock_task_context(task, &flags);
194 if (ctx) {
195 ++ctx->pin_count;
196 get_ctx(ctx);
197 spin_unlock_irqrestore(&ctx->lock, flags);
198 }
199 return ctx;
200}
201
202static void perf_unpin_context(struct perf_counter_context *ctx)
203{
204 unsigned long flags;
205
206 spin_lock_irqsave(&ctx->lock, flags);
207 --ctx->pin_count;
208 spin_unlock_irqrestore(&ctx->lock, flags);
209 put_ctx(ctx);
210}
211
212/*
Peter Zijlstrafccc7142009-05-23 18:28:56 +0200213 * Add a counter from the lists for its context.
214 * Must be called with ctx->mutex and ctx->lock held.
215 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100216static void
217list_add_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
218{
219 struct perf_counter *group_leader = counter->group_leader;
220
221 /*
222 * Depending on whether it is a standalone or sibling counter,
223 * add it straight to the context's counter list, or to the group
224 * leader's sibling list:
225 */
Peter Zijlstra3df5eda2009-05-08 18:52:22 +0200226 if (group_leader == counter)
Ingo Molnar04289bb2008-12-11 08:38:42 +0100227 list_add_tail(&counter->list_entry, &ctx->counter_list);
Peter Zijlstra5c148192009-03-25 12:30:23 +0100228 else {
Ingo Molnar04289bb2008-12-11 08:38:42 +0100229 list_add_tail(&counter->list_entry, &group_leader->sibling_list);
Peter Zijlstra5c148192009-03-25 12:30:23 +0100230 group_leader->nr_siblings++;
231 }
Peter Zijlstra592903c2009-03-13 12:21:36 +0100232
233 list_add_rcu(&counter->event_entry, &ctx->event_list);
Peter Zijlstra8bc20952009-05-15 20:45:59 +0200234 ctx->nr_counters++;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100235}
236
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000237/*
238 * Remove a counter from the lists for its context.
Peter Zijlstrafccc7142009-05-23 18:28:56 +0200239 * Must be called with ctx->mutex and ctx->lock held.
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000240 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100241static void
242list_del_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
243{
244 struct perf_counter *sibling, *tmp;
245
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000246 if (list_empty(&counter->list_entry))
247 return;
Peter Zijlstra8bc20952009-05-15 20:45:59 +0200248 ctx->nr_counters--;
249
Ingo Molnar04289bb2008-12-11 08:38:42 +0100250 list_del_init(&counter->list_entry);
Peter Zijlstra592903c2009-03-13 12:21:36 +0100251 list_del_rcu(&counter->event_entry);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100252
Peter Zijlstra5c148192009-03-25 12:30:23 +0100253 if (counter->group_leader != counter)
254 counter->group_leader->nr_siblings--;
255
Ingo Molnar04289bb2008-12-11 08:38:42 +0100256 /*
257 * If this was a group counter with sibling counters then
258 * upgrade the siblings to singleton counters by adding them
259 * to the context list directly:
260 */
261 list_for_each_entry_safe(sibling, tmp,
262 &counter->sibling_list, list_entry) {
263
Peter Zijlstra75564232009-03-13 12:21:29 +0100264 list_move_tail(&sibling->list_entry, &ctx->counter_list);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100265 sibling->group_leader = sibling;
266 }
267}
268
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100269static void
270counter_sched_out(struct perf_counter *counter,
271 struct perf_cpu_context *cpuctx,
272 struct perf_counter_context *ctx)
273{
274 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
275 return;
276
277 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200278 counter->tstamp_stopped = ctx->time;
Robert Richter4aeb0b42009-04-29 12:47:03 +0200279 counter->pmu->disable(counter);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100280 counter->oncpu = -1;
281
282 if (!is_software_counter(counter))
283 cpuctx->active_oncpu--;
284 ctx->nr_active--;
Peter Zijlstra0d486962009-06-02 19:22:16 +0200285 if (counter->attr.exclusive || !cpuctx->active_oncpu)
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100286 cpuctx->exclusive = 0;
287}
288
Paul Mackerrasd859e292009-01-17 18:10:22 +1100289static void
290group_sched_out(struct perf_counter *group_counter,
291 struct perf_cpu_context *cpuctx,
292 struct perf_counter_context *ctx)
293{
294 struct perf_counter *counter;
295
296 if (group_counter->state != PERF_COUNTER_STATE_ACTIVE)
297 return;
298
299 counter_sched_out(group_counter, cpuctx, ctx);
300
301 /*
302 * Schedule out siblings (if any):
303 */
304 list_for_each_entry(counter, &group_counter->sibling_list, list_entry)
305 counter_sched_out(counter, cpuctx, ctx);
306
Peter Zijlstra0d486962009-06-02 19:22:16 +0200307 if (group_counter->attr.exclusive)
Paul Mackerrasd859e292009-01-17 18:10:22 +1100308 cpuctx->exclusive = 0;
309}
310
Thomas Gleixner0793a612008-12-04 20:12:29 +0100311/*
312 * Cross CPU call to remove a performance counter
313 *
314 * We disable the counter on the hardware level first. After that we
315 * remove it from the context list.
316 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100317static void __perf_counter_remove_from_context(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100318{
319 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
320 struct perf_counter *counter = info;
321 struct perf_counter_context *ctx = counter->ctx;
322
323 /*
324 * If this is a task context, we need to check whether it is
325 * the current task context of this cpu. If not it has been
326 * scheduled out before the smp call arrived.
327 */
Peter Zijlstra665c2142009-05-29 14:51:57 +0200328 if (ctx->task && cpuctx->task_ctx != ctx)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100329 return;
330
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200331 spin_lock(&ctx->lock);
Ingo Molnar34adc802009-05-20 20:13:28 +0200332 /*
333 * Protect the list operation against NMI by disabling the
334 * counters on a global level.
335 */
336 perf_disable();
Thomas Gleixner0793a612008-12-04 20:12:29 +0100337
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100338 counter_sched_out(counter, cpuctx, ctx);
339
Ingo Molnar04289bb2008-12-11 08:38:42 +0100340 list_del_counter(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100341
342 if (!ctx->task) {
343 /*
344 * Allow more per task counters with respect to the
345 * reservation:
346 */
347 cpuctx->max_pertask =
348 min(perf_max_counters - ctx->nr_counters,
349 perf_max_counters - perf_reserved_percpu);
350 }
351
Ingo Molnar34adc802009-05-20 20:13:28 +0200352 perf_enable();
Peter Zijlstra665c2142009-05-29 14:51:57 +0200353 spin_unlock(&ctx->lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100354}
355
356
357/*
358 * Remove the counter from a task's (or a CPU's) list of counters.
359 *
Peter Zijlstrafccc7142009-05-23 18:28:56 +0200360 * Must be called with ctx->mutex held.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100361 *
362 * CPU counters are removed with a smp call. For task counters we only
363 * call when the task is on a CPU.
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000364 *
365 * If counter->ctx is a cloned context, callers must make sure that
366 * every task struct that counter->ctx->task could possibly point to
367 * remains valid. This is OK when called from perf_release since
368 * that only calls us on the top-level context, which can't be a clone.
369 * When called from perf_counter_exit_task, it's OK because the
370 * context has been detached from its task.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100371 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100372static void perf_counter_remove_from_context(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100373{
374 struct perf_counter_context *ctx = counter->ctx;
375 struct task_struct *task = ctx->task;
376
377 if (!task) {
378 /*
379 * Per cpu counters are removed via an smp call and
380 * the removal is always sucessful.
381 */
382 smp_call_function_single(counter->cpu,
Ingo Molnar04289bb2008-12-11 08:38:42 +0100383 __perf_counter_remove_from_context,
Thomas Gleixner0793a612008-12-04 20:12:29 +0100384 counter, 1);
385 return;
386 }
387
388retry:
Ingo Molnar04289bb2008-12-11 08:38:42 +0100389 task_oncpu_function_call(task, __perf_counter_remove_from_context,
Thomas Gleixner0793a612008-12-04 20:12:29 +0100390 counter);
391
392 spin_lock_irq(&ctx->lock);
393 /*
394 * If the context is active we need to retry the smp call.
395 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100396 if (ctx->nr_active && !list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100397 spin_unlock_irq(&ctx->lock);
398 goto retry;
399 }
400
401 /*
402 * The lock prevents that this context is scheduled in so we
Ingo Molnar04289bb2008-12-11 08:38:42 +0100403 * can remove the counter safely, if the call above did not
Thomas Gleixner0793a612008-12-04 20:12:29 +0100404 * succeed.
405 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100406 if (!list_empty(&counter->list_entry)) {
Ingo Molnar04289bb2008-12-11 08:38:42 +0100407 list_del_counter(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100408 }
409 spin_unlock_irq(&ctx->lock);
410}
411
Peter Zijlstra4af49982009-04-06 11:45:10 +0200412static inline u64 perf_clock(void)
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100413{
Peter Zijlstra4af49982009-04-06 11:45:10 +0200414 return cpu_clock(smp_processor_id());
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100415}
416
417/*
418 * Update the record of the current time in a context.
419 */
Peter Zijlstra4af49982009-04-06 11:45:10 +0200420static void update_context_time(struct perf_counter_context *ctx)
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100421{
Peter Zijlstra4af49982009-04-06 11:45:10 +0200422 u64 now = perf_clock();
423
424 ctx->time += now - ctx->timestamp;
425 ctx->timestamp = now;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100426}
427
428/*
429 * Update the total_time_enabled and total_time_running fields for a counter.
430 */
431static void update_counter_times(struct perf_counter *counter)
432{
433 struct perf_counter_context *ctx = counter->ctx;
434 u64 run_end;
435
Peter Zijlstra4af49982009-04-06 11:45:10 +0200436 if (counter->state < PERF_COUNTER_STATE_INACTIVE)
437 return;
438
439 counter->total_time_enabled = ctx->time - counter->tstamp_enabled;
440
441 if (counter->state == PERF_COUNTER_STATE_INACTIVE)
442 run_end = counter->tstamp_stopped;
443 else
444 run_end = ctx->time;
445
446 counter->total_time_running = run_end - counter->tstamp_running;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100447}
448
449/*
450 * Update total_time_enabled and total_time_running for all counters in a group.
451 */
452static void update_group_times(struct perf_counter *leader)
453{
454 struct perf_counter *counter;
455
456 update_counter_times(leader);
457 list_for_each_entry(counter, &leader->sibling_list, list_entry)
458 update_counter_times(counter);
459}
460
461/*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100462 * Cross CPU call to disable a performance counter
463 */
464static void __perf_counter_disable(void *info)
465{
466 struct perf_counter *counter = info;
467 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
468 struct perf_counter_context *ctx = counter->ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100469
470 /*
471 * If this is a per-task counter, need to check whether this
472 * counter's task is the current task on this cpu.
473 */
Peter Zijlstra665c2142009-05-29 14:51:57 +0200474 if (ctx->task && cpuctx->task_ctx != ctx)
Paul Mackerrasd859e292009-01-17 18:10:22 +1100475 return;
476
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200477 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100478
479 /*
480 * If the counter is on, turn it off.
481 * If it is in error state, leave it in error state.
482 */
483 if (counter->state >= PERF_COUNTER_STATE_INACTIVE) {
Peter Zijlstra4af49982009-04-06 11:45:10 +0200484 update_context_time(ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100485 update_counter_times(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100486 if (counter == counter->group_leader)
487 group_sched_out(counter, cpuctx, ctx);
488 else
489 counter_sched_out(counter, cpuctx, ctx);
490 counter->state = PERF_COUNTER_STATE_OFF;
491 }
492
Peter Zijlstra665c2142009-05-29 14:51:57 +0200493 spin_unlock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100494}
495
496/*
497 * Disable a counter.
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000498 *
499 * If counter->ctx is a cloned context, callers must make sure that
500 * every task struct that counter->ctx->task could possibly point to
501 * remains valid. This condition is satisifed when called through
502 * perf_counter_for_each_child or perf_counter_for_each because they
503 * hold the top-level counter's child_mutex, so any descendant that
504 * goes to exit will block in sync_child_counter.
505 * When called from perf_pending_counter it's OK because counter->ctx
506 * is the current context on this CPU and preemption is disabled,
507 * hence we can't get into perf_counter_task_sched_out for this context.
Paul Mackerrasd859e292009-01-17 18:10:22 +1100508 */
509static void perf_counter_disable(struct perf_counter *counter)
510{
511 struct perf_counter_context *ctx = counter->ctx;
512 struct task_struct *task = ctx->task;
513
514 if (!task) {
515 /*
516 * Disable the counter on the cpu that it's on
517 */
518 smp_call_function_single(counter->cpu, __perf_counter_disable,
519 counter, 1);
520 return;
521 }
522
523 retry:
524 task_oncpu_function_call(task, __perf_counter_disable, counter);
525
526 spin_lock_irq(&ctx->lock);
527 /*
528 * If the counter is still active, we need to retry the cross-call.
529 */
530 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
531 spin_unlock_irq(&ctx->lock);
532 goto retry;
533 }
534
535 /*
536 * Since we have the lock this context can't be scheduled
537 * in, so we can change the state safely.
538 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100539 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
540 update_counter_times(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100541 counter->state = PERF_COUNTER_STATE_OFF;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100542 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100543
544 spin_unlock_irq(&ctx->lock);
545}
546
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100547static int
548counter_sched_in(struct perf_counter *counter,
549 struct perf_cpu_context *cpuctx,
550 struct perf_counter_context *ctx,
551 int cpu)
552{
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100553 if (counter->state <= PERF_COUNTER_STATE_OFF)
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100554 return 0;
555
556 counter->state = PERF_COUNTER_STATE_ACTIVE;
557 counter->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */
558 /*
559 * The new state must be visible before we turn it on in the hardware:
560 */
561 smp_wmb();
562
Robert Richter4aeb0b42009-04-29 12:47:03 +0200563 if (counter->pmu->enable(counter)) {
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100564 counter->state = PERF_COUNTER_STATE_INACTIVE;
565 counter->oncpu = -1;
566 return -EAGAIN;
567 }
568
Peter Zijlstra4af49982009-04-06 11:45:10 +0200569 counter->tstamp_running += ctx->time - counter->tstamp_stopped;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100570
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100571 if (!is_software_counter(counter))
572 cpuctx->active_oncpu++;
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100573 ctx->nr_active++;
574
Peter Zijlstra0d486962009-06-02 19:22:16 +0200575 if (counter->attr.exclusive)
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100576 cpuctx->exclusive = 1;
577
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100578 return 0;
579}
580
Paul Mackerras6751b712009-05-11 12:08:02 +1000581static int
582group_sched_in(struct perf_counter *group_counter,
583 struct perf_cpu_context *cpuctx,
584 struct perf_counter_context *ctx,
585 int cpu)
586{
587 struct perf_counter *counter, *partial_group;
588 int ret;
589
590 if (group_counter->state == PERF_COUNTER_STATE_OFF)
591 return 0;
592
593 ret = hw_perf_group_sched_in(group_counter, cpuctx, ctx, cpu);
594 if (ret)
595 return ret < 0 ? ret : 0;
596
Paul Mackerras6751b712009-05-11 12:08:02 +1000597 if (counter_sched_in(group_counter, cpuctx, ctx, cpu))
598 return -EAGAIN;
599
600 /*
601 * Schedule in siblings as one group (if any):
602 */
603 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
Paul Mackerras6751b712009-05-11 12:08:02 +1000604 if (counter_sched_in(counter, cpuctx, ctx, cpu)) {
605 partial_group = counter;
606 goto group_error;
607 }
608 }
609
610 return 0;
611
612group_error:
613 /*
614 * Groups can be scheduled in as one unit only, so undo any
615 * partial group before returning:
616 */
617 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
618 if (counter == partial_group)
619 break;
620 counter_sched_out(counter, cpuctx, ctx);
621 }
622 counter_sched_out(group_counter, cpuctx, ctx);
623
624 return -EAGAIN;
625}
626
Thomas Gleixner0793a612008-12-04 20:12:29 +0100627/*
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100628 * Return 1 for a group consisting entirely of software counters,
629 * 0 if the group contains any hardware counters.
630 */
631static int is_software_only_group(struct perf_counter *leader)
632{
633 struct perf_counter *counter;
634
635 if (!is_software_counter(leader))
636 return 0;
Peter Zijlstra5c148192009-03-25 12:30:23 +0100637
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100638 list_for_each_entry(counter, &leader->sibling_list, list_entry)
639 if (!is_software_counter(counter))
640 return 0;
Peter Zijlstra5c148192009-03-25 12:30:23 +0100641
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100642 return 1;
643}
644
645/*
646 * Work out whether we can put this counter group on the CPU now.
647 */
648static int group_can_go_on(struct perf_counter *counter,
649 struct perf_cpu_context *cpuctx,
650 int can_add_hw)
651{
652 /*
653 * Groups consisting entirely of software counters can always go on.
654 */
655 if (is_software_only_group(counter))
656 return 1;
657 /*
658 * If an exclusive group is already on, no other hardware
659 * counters can go on.
660 */
661 if (cpuctx->exclusive)
662 return 0;
663 /*
664 * If this group is exclusive and there are already
665 * counters on the CPU, it can't go on.
666 */
Peter Zijlstra0d486962009-06-02 19:22:16 +0200667 if (counter->attr.exclusive && cpuctx->active_oncpu)
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100668 return 0;
669 /*
670 * Otherwise, try to add it if all previous groups were able
671 * to go on.
672 */
673 return can_add_hw;
674}
675
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100676static void add_counter_to_ctx(struct perf_counter *counter,
677 struct perf_counter_context *ctx)
678{
679 list_add_counter(counter, ctx);
Peter Zijlstra4af49982009-04-06 11:45:10 +0200680 counter->tstamp_enabled = ctx->time;
681 counter->tstamp_running = ctx->time;
682 counter->tstamp_stopped = ctx->time;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100683}
684
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100685/*
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100686 * Cross CPU call to install and enable a performance counter
Peter Zijlstra682076a2009-05-23 18:28:57 +0200687 *
688 * Must be called with ctx->mutex held
Thomas Gleixner0793a612008-12-04 20:12:29 +0100689 */
690static void __perf_install_in_context(void *info)
691{
692 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
693 struct perf_counter *counter = info;
694 struct perf_counter_context *ctx = counter->ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100695 struct perf_counter *leader = counter->group_leader;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100696 int cpu = smp_processor_id();
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100697 int err;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100698
699 /*
700 * If this is a task context, we need to check whether it is
701 * the current task context of this cpu. If not it has been
702 * scheduled out before the smp call arrived.
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000703 * Or possibly this is the right context but it isn't
704 * on this cpu because it had no counters.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100705 */
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000706 if (ctx->task && cpuctx->task_ctx != ctx) {
Peter Zijlstra665c2142009-05-29 14:51:57 +0200707 if (cpuctx->task_ctx || ctx->task != current)
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000708 return;
709 cpuctx->task_ctx = ctx;
710 }
Thomas Gleixner0793a612008-12-04 20:12:29 +0100711
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200712 spin_lock(&ctx->lock);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000713 ctx->is_active = 1;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200714 update_context_time(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100715
716 /*
717 * Protect the list operation against NMI by disabling the
718 * counters on a global level. NOP for non NMI based counters.
719 */
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200720 perf_disable();
Thomas Gleixner0793a612008-12-04 20:12:29 +0100721
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100722 add_counter_to_ctx(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100723
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100724 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100725 * Don't put the counter on if it is disabled or if
726 * it is in a group and the group isn't on.
727 */
728 if (counter->state != PERF_COUNTER_STATE_INACTIVE ||
729 (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE))
730 goto unlock;
731
732 /*
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100733 * An exclusive counter can't go on if there are already active
734 * hardware counters, and no hardware counter can go on if there
735 * is already an exclusive counter on.
736 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100737 if (!group_can_go_on(counter, cpuctx, 1))
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100738 err = -EEXIST;
739 else
740 err = counter_sched_in(counter, cpuctx, ctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100741
Paul Mackerrasd859e292009-01-17 18:10:22 +1100742 if (err) {
743 /*
744 * This counter couldn't go on. If it is in a group
745 * then we have to pull the whole group off.
746 * If the counter group is pinned then put it in error state.
747 */
748 if (leader != counter)
749 group_sched_out(leader, cpuctx, ctx);
Peter Zijlstra0d486962009-06-02 19:22:16 +0200750 if (leader->attr.pinned) {
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100751 update_group_times(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100752 leader->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100753 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100754 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100755
756 if (!err && !ctx->task && cpuctx->max_pertask)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100757 cpuctx->max_pertask--;
758
Paul Mackerrasd859e292009-01-17 18:10:22 +1100759 unlock:
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200760 perf_enable();
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100761
Peter Zijlstra665c2142009-05-29 14:51:57 +0200762 spin_unlock(&ctx->lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100763}
764
765/*
766 * Attach a performance counter to a context
767 *
768 * First we add the counter to the list with the hardware enable bit
769 * in counter->hw_config cleared.
770 *
771 * If the counter is attached to a task which is on a CPU we use a smp
772 * call to enable it in the task context. The task might have been
773 * scheduled away, but we check this in the smp call again.
Paul Mackerrasd859e292009-01-17 18:10:22 +1100774 *
775 * Must be called with ctx->mutex held.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100776 */
777static void
778perf_install_in_context(struct perf_counter_context *ctx,
779 struct perf_counter *counter,
780 int cpu)
781{
782 struct task_struct *task = ctx->task;
783
Thomas Gleixner0793a612008-12-04 20:12:29 +0100784 if (!task) {
785 /*
786 * Per cpu counters are installed via an smp call and
787 * the install is always sucessful.
788 */
789 smp_call_function_single(cpu, __perf_install_in_context,
790 counter, 1);
791 return;
792 }
793
Thomas Gleixner0793a612008-12-04 20:12:29 +0100794retry:
795 task_oncpu_function_call(task, __perf_install_in_context,
796 counter);
797
798 spin_lock_irq(&ctx->lock);
799 /*
Thomas Gleixner0793a612008-12-04 20:12:29 +0100800 * we need to retry the smp call.
801 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100802 if (ctx->is_active && list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100803 spin_unlock_irq(&ctx->lock);
804 goto retry;
805 }
806
807 /*
808 * The lock prevents that this context is scheduled in so we
809 * can add the counter safely, if it the call above did not
810 * succeed.
811 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100812 if (list_empty(&counter->list_entry))
813 add_counter_to_ctx(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100814 spin_unlock_irq(&ctx->lock);
815}
816
Paul Mackerrasd859e292009-01-17 18:10:22 +1100817/*
818 * Cross CPU call to enable a performance counter
819 */
820static void __perf_counter_enable(void *info)
Ingo Molnar04289bb2008-12-11 08:38:42 +0100821{
Paul Mackerrasd859e292009-01-17 18:10:22 +1100822 struct perf_counter *counter = info;
823 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
824 struct perf_counter_context *ctx = counter->ctx;
825 struct perf_counter *leader = counter->group_leader;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100826 int err;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100827
828 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100829 * If this is a per-task counter, need to check whether this
830 * counter's task is the current task on this cpu.
Ingo Molnar04289bb2008-12-11 08:38:42 +0100831 */
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000832 if (ctx->task && cpuctx->task_ctx != ctx) {
Peter Zijlstra665c2142009-05-29 14:51:57 +0200833 if (cpuctx->task_ctx || ctx->task != current)
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000834 return;
835 cpuctx->task_ctx = ctx;
836 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100837
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200838 spin_lock(&ctx->lock);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000839 ctx->is_active = 1;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200840 update_context_time(ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100841
842 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
843 goto unlock;
844 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200845 counter->tstamp_enabled = ctx->time - counter->total_time_enabled;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100846
847 /*
848 * If the counter is in a group and isn't the group leader,
849 * then don't put it on unless the group is on.
850 */
851 if (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE)
852 goto unlock;
853
Paul Mackerrase758a332009-05-12 21:59:01 +1000854 if (!group_can_go_on(counter, cpuctx, 1)) {
Paul Mackerrasd859e292009-01-17 18:10:22 +1100855 err = -EEXIST;
Paul Mackerrase758a332009-05-12 21:59:01 +1000856 } else {
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200857 perf_disable();
Paul Mackerrase758a332009-05-12 21:59:01 +1000858 if (counter == leader)
859 err = group_sched_in(counter, cpuctx, ctx,
860 smp_processor_id());
861 else
862 err = counter_sched_in(counter, cpuctx, ctx,
863 smp_processor_id());
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200864 perf_enable();
Paul Mackerrase758a332009-05-12 21:59:01 +1000865 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100866
867 if (err) {
868 /*
869 * If this counter can't go on and it's part of a
870 * group, then the whole group has to come off.
871 */
872 if (leader != counter)
873 group_sched_out(leader, cpuctx, ctx);
Peter Zijlstra0d486962009-06-02 19:22:16 +0200874 if (leader->attr.pinned) {
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100875 update_group_times(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100876 leader->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100877 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100878 }
879
880 unlock:
Peter Zijlstra665c2142009-05-29 14:51:57 +0200881 spin_unlock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100882}
883
884/*
885 * Enable a counter.
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000886 *
887 * If counter->ctx is a cloned context, callers must make sure that
888 * every task struct that counter->ctx->task could possibly point to
889 * remains valid. This condition is satisfied when called through
890 * perf_counter_for_each_child or perf_counter_for_each as described
891 * for perf_counter_disable.
Paul Mackerrasd859e292009-01-17 18:10:22 +1100892 */
893static void perf_counter_enable(struct perf_counter *counter)
894{
895 struct perf_counter_context *ctx = counter->ctx;
896 struct task_struct *task = ctx->task;
897
898 if (!task) {
899 /*
900 * Enable the counter on the cpu that it's on
901 */
902 smp_call_function_single(counter->cpu, __perf_counter_enable,
903 counter, 1);
904 return;
905 }
906
907 spin_lock_irq(&ctx->lock);
908 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
909 goto out;
910
911 /*
912 * If the counter is in error state, clear that first.
913 * That way, if we see the counter in error state below, we
914 * know that it has gone back into error state, as distinct
915 * from the task having been scheduled away before the
916 * cross-call arrived.
917 */
918 if (counter->state == PERF_COUNTER_STATE_ERROR)
919 counter->state = PERF_COUNTER_STATE_OFF;
920
921 retry:
922 spin_unlock_irq(&ctx->lock);
923 task_oncpu_function_call(task, __perf_counter_enable, counter);
924
925 spin_lock_irq(&ctx->lock);
926
927 /*
928 * If the context is active and the counter is still off,
929 * we need to retry the cross-call.
930 */
931 if (ctx->is_active && counter->state == PERF_COUNTER_STATE_OFF)
932 goto retry;
933
934 /*
935 * Since we have the lock this context can't be scheduled
936 * in, so we can change the state safely.
937 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100938 if (counter->state == PERF_COUNTER_STATE_OFF) {
Paul Mackerrasd859e292009-01-17 18:10:22 +1100939 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200940 counter->tstamp_enabled =
941 ctx->time - counter->total_time_enabled;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100942 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100943 out:
944 spin_unlock_irq(&ctx->lock);
945}
946
Peter Zijlstra2023b352009-05-05 17:50:26 +0200947static int perf_counter_refresh(struct perf_counter *counter, int refresh)
Peter Zijlstra79f14642009-04-06 11:45:07 +0200948{
Peter Zijlstra2023b352009-05-05 17:50:26 +0200949 /*
950 * not supported on inherited counters
951 */
Peter Zijlstra0d486962009-06-02 19:22:16 +0200952 if (counter->attr.inherit)
Peter Zijlstra2023b352009-05-05 17:50:26 +0200953 return -EINVAL;
954
Peter Zijlstra79f14642009-04-06 11:45:07 +0200955 atomic_add(refresh, &counter->event_limit);
956 perf_counter_enable(counter);
Peter Zijlstra2023b352009-05-05 17:50:26 +0200957
958 return 0;
Peter Zijlstra79f14642009-04-06 11:45:07 +0200959}
960
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100961void __perf_counter_sched_out(struct perf_counter_context *ctx,
962 struct perf_cpu_context *cpuctx)
963{
964 struct perf_counter *counter;
965
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100966 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100967 ctx->is_active = 0;
968 if (likely(!ctx->nr_counters))
969 goto out;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200970 update_context_time(ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100971
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200972 perf_disable();
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100973 if (ctx->nr_active) {
Peter Zijlstraafedadf2009-05-20 12:21:22 +0200974 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
975 if (counter != counter->group_leader)
976 counter_sched_out(counter, cpuctx, ctx);
977 else
978 group_sched_out(counter, cpuctx, ctx);
979 }
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100980 }
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200981 perf_enable();
Paul Mackerrasd859e292009-01-17 18:10:22 +1100982 out:
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100983 spin_unlock(&ctx->lock);
984}
985
Thomas Gleixner0793a612008-12-04 20:12:29 +0100986/*
Paul Mackerras564c2b22009-05-22 14:27:22 +1000987 * Test whether two contexts are equivalent, i.e. whether they
988 * have both been cloned from the same version of the same context
989 * and they both have the same number of enabled counters.
990 * If the number of enabled counters is the same, then the set
991 * of enabled counters should be the same, because these are both
992 * inherited contexts, therefore we can't access individual counters
993 * in them directly with an fd; we can only enable/disable all
994 * counters via prctl, or enable/disable all counters in a family
995 * via ioctl, which will have the same effect on both contexts.
996 */
997static int context_equiv(struct perf_counter_context *ctx1,
998 struct perf_counter_context *ctx2)
999{
1000 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001001 && ctx1->parent_gen == ctx2->parent_gen
Paul Mackerras25346b932009-06-01 17:48:12 +10001002 && !ctx1->pin_count && !ctx2->pin_count;
Paul Mackerras564c2b22009-05-22 14:27:22 +10001003}
1004
1005/*
Thomas Gleixner0793a612008-12-04 20:12:29 +01001006 * Called from scheduler to remove the counters of the current task,
1007 * with interrupts disabled.
1008 *
1009 * We stop each counter and update the counter value in counter->count.
1010 *
Ingo Molnar76715812008-12-17 14:20:28 +01001011 * This does not protect us against NMI, but disable()
Thomas Gleixner0793a612008-12-04 20:12:29 +01001012 * sets the disabled bit in the control field of counter _before_
1013 * accessing the counter control register. If a NMI hits, then it will
1014 * not restart the counter.
1015 */
Paul Mackerras564c2b22009-05-22 14:27:22 +10001016void perf_counter_task_sched_out(struct task_struct *task,
1017 struct task_struct *next, int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001018{
1019 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001020 struct perf_counter_context *ctx = task->perf_counter_ctxp;
Paul Mackerras564c2b22009-05-22 14:27:22 +10001021 struct perf_counter_context *next_ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001022 struct perf_counter_context *parent;
Peter Zijlstra4a0deca2009-03-19 20:26:12 +01001023 struct pt_regs *regs;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001024 int do_switch = 1;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001025
Peter Zijlstra10989fb2009-05-25 14:45:28 +02001026 regs = task_pt_regs(task);
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +02001027 perf_swcounter_event(PERF_COUNT_SW_CONTEXT_SWITCHES, 1, 1, regs, 0);
Peter Zijlstra10989fb2009-05-25 14:45:28 +02001028
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001029 if (likely(!ctx || !cpuctx->task_ctx))
Thomas Gleixner0793a612008-12-04 20:12:29 +01001030 return;
1031
Peter Zijlstrabce379b2009-04-06 11:45:13 +02001032 update_context_time(ctx);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001033
1034 rcu_read_lock();
1035 parent = rcu_dereference(ctx->parent_ctx);
Paul Mackerras564c2b22009-05-22 14:27:22 +10001036 next_ctx = next->perf_counter_ctxp;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001037 if (parent && next_ctx &&
1038 rcu_dereference(next_ctx->parent_ctx) == parent) {
1039 /*
1040 * Looks like the two contexts are clones, so we might be
1041 * able to optimize the context switch. We lock both
1042 * contexts and check that they are clones under the
1043 * lock (including re-checking that neither has been
1044 * uncloned in the meantime). It doesn't matter which
1045 * order we take the locks because no other cpu could
1046 * be trying to lock both of these tasks.
1047 */
1048 spin_lock(&ctx->lock);
1049 spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
1050 if (context_equiv(ctx, next_ctx)) {
Peter Zijlstra665c2142009-05-29 14:51:57 +02001051 /*
1052 * XXX do we need a memory barrier of sorts
1053 * wrt to rcu_dereference() of perf_counter_ctxp
1054 */
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001055 task->perf_counter_ctxp = next_ctx;
1056 next->perf_counter_ctxp = ctx;
1057 ctx->task = next;
1058 next_ctx->task = task;
1059 do_switch = 0;
1060 }
1061 spin_unlock(&next_ctx->lock);
1062 spin_unlock(&ctx->lock);
Paul Mackerras564c2b22009-05-22 14:27:22 +10001063 }
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001064 rcu_read_unlock();
Paul Mackerras564c2b22009-05-22 14:27:22 +10001065
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001066 if (do_switch) {
1067 __perf_counter_sched_out(ctx, cpuctx);
1068 cpuctx->task_ctx = NULL;
1069 }
Thomas Gleixner0793a612008-12-04 20:12:29 +01001070}
1071
Peter Zijlstra665c2142009-05-29 14:51:57 +02001072/*
1073 * Called with IRQs disabled
1074 */
Paul Mackerrasa08b1592009-05-11 15:46:10 +10001075static void __perf_counter_task_sched_out(struct perf_counter_context *ctx)
1076{
1077 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1078
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001079 if (!cpuctx->task_ctx)
1080 return;
Ingo Molnar012b84d2009-05-17 11:08:41 +02001081
1082 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
1083 return;
1084
Paul Mackerrasa08b1592009-05-11 15:46:10 +10001085 __perf_counter_sched_out(ctx, cpuctx);
1086 cpuctx->task_ctx = NULL;
1087}
1088
Peter Zijlstra665c2142009-05-29 14:51:57 +02001089/*
1090 * Called with IRQs disabled
1091 */
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001092static void perf_counter_cpu_sched_out(struct perf_cpu_context *cpuctx)
Ingo Molnar04289bb2008-12-11 08:38:42 +01001093{
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001094 __perf_counter_sched_out(&cpuctx->ctx, cpuctx);
Ingo Molnar04289bb2008-12-11 08:38:42 +01001095}
1096
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001097static void
1098__perf_counter_sched_in(struct perf_counter_context *ctx,
1099 struct perf_cpu_context *cpuctx, int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001100{
Thomas Gleixner0793a612008-12-04 20:12:29 +01001101 struct perf_counter *counter;
Paul Mackerrasdd0e6ba2009-01-12 15:11:00 +11001102 int can_add_hw = 1;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001103
Thomas Gleixner0793a612008-12-04 20:12:29 +01001104 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001105 ctx->is_active = 1;
1106 if (likely(!ctx->nr_counters))
1107 goto out;
1108
Peter Zijlstra4af49982009-04-06 11:45:10 +02001109 ctx->timestamp = perf_clock();
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001110
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001111 perf_disable();
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001112
1113 /*
1114 * First go through the list and put on any pinned groups
1115 * in order to give them the best chance of going on.
1116 */
Ingo Molnar04289bb2008-12-11 08:38:42 +01001117 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001118 if (counter->state <= PERF_COUNTER_STATE_OFF ||
Peter Zijlstra0d486962009-06-02 19:22:16 +02001119 !counter->attr.pinned)
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001120 continue;
1121 if (counter->cpu != -1 && counter->cpu != cpu)
1122 continue;
1123
Peter Zijlstraafedadf2009-05-20 12:21:22 +02001124 if (counter != counter->group_leader)
1125 counter_sched_in(counter, cpuctx, ctx, cpu);
1126 else {
1127 if (group_can_go_on(counter, cpuctx, 1))
1128 group_sched_in(counter, cpuctx, ctx, cpu);
1129 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001130
1131 /*
1132 * If this pinned group hasn't been scheduled,
1133 * put it in error state.
1134 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001135 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
1136 update_group_times(counter);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001137 counter->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001138 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001139 }
1140
1141 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1142 /*
1143 * Ignore counters in OFF or ERROR state, and
1144 * ignore pinned counters since we did them already.
1145 */
1146 if (counter->state <= PERF_COUNTER_STATE_OFF ||
Peter Zijlstra0d486962009-06-02 19:22:16 +02001147 counter->attr.pinned)
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001148 continue;
1149
Ingo Molnar04289bb2008-12-11 08:38:42 +01001150 /*
1151 * Listen to the 'cpu' scheduling filter constraint
1152 * of counters:
1153 */
Thomas Gleixner0793a612008-12-04 20:12:29 +01001154 if (counter->cpu != -1 && counter->cpu != cpu)
1155 continue;
1156
Peter Zijlstraafedadf2009-05-20 12:21:22 +02001157 if (counter != counter->group_leader) {
1158 if (counter_sched_in(counter, cpuctx, ctx, cpu))
Paul Mackerrasdd0e6ba2009-01-12 15:11:00 +11001159 can_add_hw = 0;
Peter Zijlstraafedadf2009-05-20 12:21:22 +02001160 } else {
1161 if (group_can_go_on(counter, cpuctx, can_add_hw)) {
1162 if (group_sched_in(counter, cpuctx, ctx, cpu))
1163 can_add_hw = 0;
1164 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001165 }
Thomas Gleixner0793a612008-12-04 20:12:29 +01001166 }
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001167 perf_enable();
Paul Mackerrasd859e292009-01-17 18:10:22 +11001168 out:
Thomas Gleixner0793a612008-12-04 20:12:29 +01001169 spin_unlock(&ctx->lock);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001170}
Ingo Molnar04289bb2008-12-11 08:38:42 +01001171
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001172/*
1173 * Called from scheduler to add the counters of the current task
1174 * with interrupts disabled.
1175 *
1176 * We restore the counter value and then enable it.
1177 *
1178 * This does not protect us against NMI, but enable()
1179 * sets the enabled bit in the control field of counter _before_
1180 * accessing the counter control register. If a NMI hits, then it will
1181 * keep the counter running.
1182 */
1183void perf_counter_task_sched_in(struct task_struct *task, int cpu)
1184{
1185 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001186 struct perf_counter_context *ctx = task->perf_counter_ctxp;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001187
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001188 if (likely(!ctx))
1189 return;
Paul Mackerras564c2b22009-05-22 14:27:22 +10001190 if (cpuctx->task_ctx == ctx)
1191 return;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001192 __perf_counter_sched_in(ctx, cpuctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001193 cpuctx->task_ctx = ctx;
1194}
1195
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001196static void perf_counter_cpu_sched_in(struct perf_cpu_context *cpuctx, int cpu)
1197{
1198 struct perf_counter_context *ctx = &cpuctx->ctx;
1199
1200 __perf_counter_sched_in(ctx, cpuctx, cpu);
1201}
1202
Peter Zijlstraa78ac322009-05-25 17:39:05 +02001203#define MAX_INTERRUPTS (~0ULL)
1204
1205static void perf_log_throttle(struct perf_counter *counter, int enable);
Peter Zijlstra26b119b2009-05-20 12:21:20 +02001206static void perf_log_period(struct perf_counter *counter, u64 period);
1207
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001208static void perf_adjust_period(struct perf_counter *counter, u64 events)
1209{
1210 struct hw_perf_counter *hwc = &counter->hw;
1211 u64 period, sample_period;
1212 s64 delta;
1213
1214 events *= hwc->sample_period;
1215 period = div64_u64(events, counter->attr.sample_freq);
1216
1217 delta = (s64)(period - hwc->sample_period);
1218 delta = (delta + 7) / 8; /* low pass filter */
1219
1220 sample_period = hwc->sample_period + delta;
1221
1222 if (!sample_period)
1223 sample_period = 1;
1224
1225 perf_log_period(counter, sample_period);
1226
1227 hwc->sample_period = sample_period;
1228}
1229
1230static void perf_ctx_adjust_freq(struct perf_counter_context *ctx)
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001231{
1232 struct perf_counter *counter;
Peter Zijlstra6a24ed6c2009-06-05 18:01:29 +02001233 struct hw_perf_counter *hwc;
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001234 u64 interrupts, freq;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001235
1236 spin_lock(&ctx->lock);
1237 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1238 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
1239 continue;
1240
Peter Zijlstra6a24ed6c2009-06-05 18:01:29 +02001241 hwc = &counter->hw;
1242
1243 interrupts = hwc->interrupts;
1244 hwc->interrupts = 0;
Peter Zijlstraa78ac322009-05-25 17:39:05 +02001245
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001246 /*
1247 * unthrottle counters on the tick
1248 */
Peter Zijlstraa78ac322009-05-25 17:39:05 +02001249 if (interrupts == MAX_INTERRUPTS) {
1250 perf_log_throttle(counter, 1);
1251 counter->pmu->unthrottle(counter);
Peter Zijlstradf58ab22009-06-11 11:25:05 +02001252 interrupts = 2*sysctl_perf_counter_sample_rate/HZ;
Peter Zijlstraa78ac322009-05-25 17:39:05 +02001253 }
1254
Peter Zijlstra0d486962009-06-02 19:22:16 +02001255 if (!counter->attr.freq || !counter->attr.sample_freq)
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001256 continue;
1257
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001258 /*
1259 * if the specified freq < HZ then we need to skip ticks
1260 */
Peter Zijlstra6a24ed6c2009-06-05 18:01:29 +02001261 if (counter->attr.sample_freq < HZ) {
1262 freq = counter->attr.sample_freq;
1263
1264 hwc->freq_count += freq;
1265 hwc->freq_interrupts += interrupts;
1266
1267 if (hwc->freq_count < HZ)
1268 continue;
1269
1270 interrupts = hwc->freq_interrupts;
1271 hwc->freq_interrupts = 0;
1272 hwc->freq_count -= HZ;
1273 } else
1274 freq = HZ;
1275
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001276 perf_adjust_period(counter, freq * interrupts);
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001277
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001278 /*
1279 * In order to avoid being stalled by an (accidental) huge
1280 * sample period, force reset the sample period if we didn't
1281 * get any events in this freq period.
1282 */
1283 if (!interrupts) {
1284 perf_disable();
1285 counter->pmu->disable(counter);
Paul Mackerras87847b82009-06-13 17:06:50 +10001286 atomic64_set(&hwc->period_left, 0);
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001287 counter->pmu->enable(counter);
1288 perf_enable();
1289 }
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001290 }
1291 spin_unlock(&ctx->lock);
1292}
1293
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001294/*
1295 * Round-robin a context's counters:
1296 */
1297static void rotate_ctx(struct perf_counter_context *ctx)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001298{
Thomas Gleixner0793a612008-12-04 20:12:29 +01001299 struct perf_counter *counter;
1300
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001301 if (!ctx->nr_counters)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001302 return;
1303
Thomas Gleixner0793a612008-12-04 20:12:29 +01001304 spin_lock(&ctx->lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001305 /*
Ingo Molnar04289bb2008-12-11 08:38:42 +01001306 * Rotate the first entry last (works just fine for group counters too):
Thomas Gleixner0793a612008-12-04 20:12:29 +01001307 */
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001308 perf_disable();
Ingo Molnar04289bb2008-12-11 08:38:42 +01001309 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Peter Zijlstra75564232009-03-13 12:21:29 +01001310 list_move_tail(&counter->list_entry, &ctx->counter_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001311 break;
1312 }
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001313 perf_enable();
Thomas Gleixner0793a612008-12-04 20:12:29 +01001314
1315 spin_unlock(&ctx->lock);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001316}
Thomas Gleixner0793a612008-12-04 20:12:29 +01001317
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001318void perf_counter_task_tick(struct task_struct *curr, int cpu)
1319{
Peter Zijlstra7fc23a52009-05-08 18:52:21 +02001320 struct perf_cpu_context *cpuctx;
1321 struct perf_counter_context *ctx;
1322
1323 if (!atomic_read(&nr_counters))
1324 return;
1325
1326 cpuctx = &per_cpu(perf_cpu_context, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001327 ctx = curr->perf_counter_ctxp;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001328
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001329 perf_ctx_adjust_freq(&cpuctx->ctx);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001330 if (ctx)
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001331 perf_ctx_adjust_freq(ctx);
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001332
Ingo Molnarb82914c2009-05-04 18:54:32 +02001333 perf_counter_cpu_sched_out(cpuctx);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001334 if (ctx)
1335 __perf_counter_task_sched_out(ctx);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001336
Ingo Molnarb82914c2009-05-04 18:54:32 +02001337 rotate_ctx(&cpuctx->ctx);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001338 if (ctx)
1339 rotate_ctx(ctx);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001340
Ingo Molnarb82914c2009-05-04 18:54:32 +02001341 perf_counter_cpu_sched_in(cpuctx, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001342 if (ctx)
1343 perf_counter_task_sched_in(curr, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001344}
1345
1346/*
Thomas Gleixner0793a612008-12-04 20:12:29 +01001347 * Cross CPU call to read the hardware counter
1348 */
Ingo Molnar76715812008-12-17 14:20:28 +01001349static void __read(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001350{
Ingo Molnar621a01e2008-12-11 12:46:46 +01001351 struct perf_counter *counter = info;
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001352 struct perf_counter_context *ctx = counter->ctx;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001353 unsigned long flags;
Ingo Molnar621a01e2008-12-11 12:46:46 +01001354
Peter Zijlstra849691a2009-04-06 11:45:12 +02001355 local_irq_save(flags);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001356 if (ctx->is_active)
Peter Zijlstra4af49982009-04-06 11:45:10 +02001357 update_context_time(ctx);
Robert Richter4aeb0b42009-04-29 12:47:03 +02001358 counter->pmu->read(counter);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001359 update_counter_times(counter);
Peter Zijlstra849691a2009-04-06 11:45:12 +02001360 local_irq_restore(flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001361}
1362
Ingo Molnar04289bb2008-12-11 08:38:42 +01001363static u64 perf_counter_read(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001364{
1365 /*
1366 * If counter is enabled and currently active on a CPU, update the
1367 * value in the counter structure:
1368 */
Ingo Molnar6a930702008-12-11 15:17:03 +01001369 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
Thomas Gleixner0793a612008-12-04 20:12:29 +01001370 smp_call_function_single(counter->oncpu,
Ingo Molnar76715812008-12-17 14:20:28 +01001371 __read, counter, 1);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001372 } else if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
1373 update_counter_times(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001374 }
1375
Ingo Molnaree060942008-12-13 09:00:03 +01001376 return atomic64_read(&counter->count);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001377}
1378
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001379/*
1380 * Initialize the perf_counter context in a task_struct:
1381 */
1382static void
1383__perf_counter_init_context(struct perf_counter_context *ctx,
1384 struct task_struct *task)
1385{
1386 memset(ctx, 0, sizeof(*ctx));
1387 spin_lock_init(&ctx->lock);
1388 mutex_init(&ctx->mutex);
1389 INIT_LIST_HEAD(&ctx->counter_list);
1390 INIT_LIST_HEAD(&ctx->event_list);
1391 atomic_set(&ctx->refcount, 1);
1392 ctx->task = task;
1393}
1394
Thomas Gleixner0793a612008-12-04 20:12:29 +01001395static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
1396{
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001397 struct perf_counter_context *parent_ctx;
Ingo Molnar22a4f652009-06-01 10:13:37 +02001398 struct perf_counter_context *ctx;
1399 struct perf_cpu_context *cpuctx;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001400 struct task_struct *task;
Paul Mackerras25346b932009-06-01 17:48:12 +10001401 unsigned long flags;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001402 int err;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001403
1404 /*
1405 * If cpu is not a wildcard then this is a percpu counter:
1406 */
1407 if (cpu != -1) {
1408 /* Must be root to operate on a CPU counter: */
Peter Zijlstra07647712009-06-11 11:18:36 +02001409 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
Thomas Gleixner0793a612008-12-04 20:12:29 +01001410 return ERR_PTR(-EACCES);
1411
1412 if (cpu < 0 || cpu > num_possible_cpus())
1413 return ERR_PTR(-EINVAL);
1414
1415 /*
1416 * We could be clever and allow to attach a counter to an
1417 * offline CPU and activate it when the CPU comes up, but
1418 * that's for later.
1419 */
1420 if (!cpu_isset(cpu, cpu_online_map))
1421 return ERR_PTR(-ENODEV);
1422
1423 cpuctx = &per_cpu(perf_cpu_context, cpu);
1424 ctx = &cpuctx->ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001425 get_ctx(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001426
Thomas Gleixner0793a612008-12-04 20:12:29 +01001427 return ctx;
1428 }
1429
1430 rcu_read_lock();
1431 if (!pid)
1432 task = current;
1433 else
1434 task = find_task_by_vpid(pid);
1435 if (task)
1436 get_task_struct(task);
1437 rcu_read_unlock();
1438
1439 if (!task)
1440 return ERR_PTR(-ESRCH);
1441
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001442 /*
1443 * Can't attach counters to a dying task.
1444 */
1445 err = -ESRCH;
1446 if (task->flags & PF_EXITING)
1447 goto errout;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001448
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001449 /* Reuse ptrace permission checks for now. */
1450 err = -EACCES;
1451 if (!ptrace_may_access(task, PTRACE_MODE_READ))
1452 goto errout;
1453
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001454 retry:
Paul Mackerras25346b932009-06-01 17:48:12 +10001455 ctx = perf_lock_task_context(task, &flags);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001456 if (ctx) {
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001457 parent_ctx = ctx->parent_ctx;
1458 if (parent_ctx) {
1459 put_ctx(parent_ctx);
1460 ctx->parent_ctx = NULL; /* no longer a clone */
1461 }
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001462 /*
1463 * Get an extra reference before dropping the lock so that
1464 * this context won't get freed if the task exits.
1465 */
1466 get_ctx(ctx);
Paul Mackerras25346b932009-06-01 17:48:12 +10001467 spin_unlock_irqrestore(&ctx->lock, flags);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001468 }
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001469
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001470 if (!ctx) {
1471 ctx = kmalloc(sizeof(struct perf_counter_context), GFP_KERNEL);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001472 err = -ENOMEM;
1473 if (!ctx)
1474 goto errout;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001475 __perf_counter_init_context(ctx, task);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001476 get_ctx(ctx);
1477 if (cmpxchg(&task->perf_counter_ctxp, NULL, ctx)) {
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001478 /*
1479 * We raced with some other task; use
1480 * the context they set.
1481 */
1482 kfree(ctx);
Paul Mackerras25346b932009-06-01 17:48:12 +10001483 goto retry;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001484 }
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001485 get_task_struct(task);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001486 }
1487
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001488 put_task_struct(task);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001489 return ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001490
1491 errout:
1492 put_task_struct(task);
1493 return ERR_PTR(err);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001494}
1495
Peter Zijlstra592903c2009-03-13 12:21:36 +01001496static void free_counter_rcu(struct rcu_head *head)
1497{
1498 struct perf_counter *counter;
1499
1500 counter = container_of(head, struct perf_counter, rcu_head);
Peter Zijlstra709e50c2009-06-02 14:13:15 +02001501 if (counter->ns)
1502 put_pid_ns(counter->ns);
Peter Zijlstra592903c2009-03-13 12:21:36 +01001503 kfree(counter);
1504}
1505
Peter Zijlstra925d5192009-03-30 19:07:02 +02001506static void perf_pending_sync(struct perf_counter *counter);
1507
Peter Zijlstraf1600952009-03-19 20:26:16 +01001508static void free_counter(struct perf_counter *counter)
1509{
Peter Zijlstra925d5192009-03-30 19:07:02 +02001510 perf_pending_sync(counter);
1511
Peter Zijlstra7fc23a52009-05-08 18:52:21 +02001512 atomic_dec(&nr_counters);
Peter Zijlstra0d486962009-06-02 19:22:16 +02001513 if (counter->attr.mmap)
Peter Zijlstra60313eb2009-06-04 16:53:44 +02001514 atomic_dec(&nr_mmap_counters);
Peter Zijlstra0d486962009-06-02 19:22:16 +02001515 if (counter->attr.comm)
Peter Zijlstra60313eb2009-06-04 16:53:44 +02001516 atomic_dec(&nr_comm_counters);
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02001517
Peter Zijlstrae077df42009-03-19 20:26:17 +01001518 if (counter->destroy)
1519 counter->destroy(counter);
1520
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001521 put_ctx(counter->ctx);
Peter Zijlstraf1600952009-03-19 20:26:16 +01001522 call_rcu(&counter->rcu_head, free_counter_rcu);
1523}
1524
Thomas Gleixner0793a612008-12-04 20:12:29 +01001525/*
1526 * Called when the last reference to the file is gone.
1527 */
1528static int perf_release(struct inode *inode, struct file *file)
1529{
1530 struct perf_counter *counter = file->private_data;
1531 struct perf_counter_context *ctx = counter->ctx;
1532
1533 file->private_data = NULL;
1534
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001535 WARN_ON_ONCE(ctx->parent_ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001536 mutex_lock(&ctx->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01001537 perf_counter_remove_from_context(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001538 mutex_unlock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001539
Peter Zijlstra082ff5a2009-05-23 18:29:00 +02001540 mutex_lock(&counter->owner->perf_counter_mutex);
1541 list_del_init(&counter->owner_entry);
1542 mutex_unlock(&counter->owner->perf_counter_mutex);
1543 put_task_struct(counter->owner);
1544
Peter Zijlstraf1600952009-03-19 20:26:16 +01001545 free_counter(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001546
1547 return 0;
1548}
1549
1550/*
1551 * Read the performance counter - simple non blocking version for now
1552 */
1553static ssize_t
1554perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
1555{
Marti Raudseppd5e8da62009-06-13 02:35:01 +03001556 u64 values[4];
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001557 int n;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001558
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001559 /*
1560 * Return end-of-file for a read on a counter that is in
1561 * error state (i.e. because it was pinned but it couldn't be
1562 * scheduled on to the CPU at some point).
1563 */
1564 if (counter->state == PERF_COUNTER_STATE_ERROR)
1565 return 0;
1566
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001567 WARN_ON_ONCE(counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001568 mutex_lock(&counter->child_mutex);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001569 values[0] = perf_counter_read(counter);
1570 n = 1;
Peter Zijlstra0d486962009-06-02 19:22:16 +02001571 if (counter->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001572 values[n++] = counter->total_time_enabled +
1573 atomic64_read(&counter->child_total_time_enabled);
Peter Zijlstra0d486962009-06-02 19:22:16 +02001574 if (counter->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001575 values[n++] = counter->total_time_running +
1576 atomic64_read(&counter->child_total_time_running);
Peter Zijlstra0d486962009-06-02 19:22:16 +02001577 if (counter->attr.read_format & PERF_FORMAT_ID)
Peter Zijlstra8e5799b2009-06-02 15:08:15 +02001578 values[n++] = counter->id;
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001579 mutex_unlock(&counter->child_mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001580
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001581 if (count < n * sizeof(u64))
1582 return -EINVAL;
1583 count = n * sizeof(u64);
1584
1585 if (copy_to_user(buf, values, count))
1586 return -EFAULT;
1587
1588 return count;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001589}
1590
1591static ssize_t
Thomas Gleixner0793a612008-12-04 20:12:29 +01001592perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
1593{
1594 struct perf_counter *counter = file->private_data;
1595
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001596 return perf_read_hw(counter, buf, count);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001597}
1598
1599static unsigned int perf_poll(struct file *file, poll_table *wait)
1600{
1601 struct perf_counter *counter = file->private_data;
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001602 struct perf_mmap_data *data;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001603 unsigned int events = POLL_HUP;
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001604
1605 rcu_read_lock();
1606 data = rcu_dereference(counter->data);
1607 if (data)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001608 events = atomic_xchg(&data->poll, 0);
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001609 rcu_read_unlock();
Thomas Gleixner0793a612008-12-04 20:12:29 +01001610
1611 poll_wait(file, &counter->waitq, wait);
1612
Thomas Gleixner0793a612008-12-04 20:12:29 +01001613 return events;
1614}
1615
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001616static void perf_counter_reset(struct perf_counter *counter)
1617{
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001618 (void)perf_counter_read(counter);
Paul Mackerras615a3f12009-05-11 15:50:21 +10001619 atomic64_set(&counter->count, 0);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001620 perf_counter_update_userpage(counter);
1621}
1622
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001623/*
1624 * Holding the top-level counter's child_mutex means that any
1625 * descendant process that has inherited this counter will block
1626 * in sync_child_counter if it goes to exit, thus satisfying the
1627 * task existence requirements of perf_counter_enable/disable.
1628 */
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001629static void perf_counter_for_each_child(struct perf_counter *counter,
1630 void (*func)(struct perf_counter *))
1631{
1632 struct perf_counter *child;
1633
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001634 WARN_ON_ONCE(counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001635 mutex_lock(&counter->child_mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001636 func(counter);
1637 list_for_each_entry(child, &counter->child_list, child_list)
1638 func(child);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001639 mutex_unlock(&counter->child_mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001640}
1641
1642static void perf_counter_for_each(struct perf_counter *counter,
1643 void (*func)(struct perf_counter *))
1644{
Peter Zijlstra75f937f2009-06-15 15:05:12 +02001645 struct perf_counter_context *ctx = counter->ctx;
1646 struct perf_counter *sibling;
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001647
Peter Zijlstra75f937f2009-06-15 15:05:12 +02001648 WARN_ON_ONCE(ctx->parent_ctx);
1649 mutex_lock(&ctx->mutex);
1650 counter = counter->group_leader;
1651
1652 perf_counter_for_each_child(counter, func);
1653 func(counter);
1654 list_for_each_entry(sibling, &counter->sibling_list, list_entry)
1655 perf_counter_for_each_child(counter, func);
1656 mutex_unlock(&ctx->mutex);
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001657}
1658
Peter Zijlstra08247e32009-06-02 16:46:57 +02001659static int perf_counter_period(struct perf_counter *counter, u64 __user *arg)
1660{
1661 struct perf_counter_context *ctx = counter->ctx;
1662 unsigned long size;
1663 int ret = 0;
1664 u64 value;
1665
Peter Zijlstra0d486962009-06-02 19:22:16 +02001666 if (!counter->attr.sample_period)
Peter Zijlstra08247e32009-06-02 16:46:57 +02001667 return -EINVAL;
1668
1669 size = copy_from_user(&value, arg, sizeof(value));
1670 if (size != sizeof(value))
1671 return -EFAULT;
1672
1673 if (!value)
1674 return -EINVAL;
1675
1676 spin_lock_irq(&ctx->lock);
Peter Zijlstra0d486962009-06-02 19:22:16 +02001677 if (counter->attr.freq) {
Peter Zijlstradf58ab22009-06-11 11:25:05 +02001678 if (value > sysctl_perf_counter_sample_rate) {
Peter Zijlstra08247e32009-06-02 16:46:57 +02001679 ret = -EINVAL;
1680 goto unlock;
1681 }
1682
Peter Zijlstra0d486962009-06-02 19:22:16 +02001683 counter->attr.sample_freq = value;
Peter Zijlstra08247e32009-06-02 16:46:57 +02001684 } else {
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001685 perf_log_period(counter, value);
1686
Peter Zijlstra0d486962009-06-02 19:22:16 +02001687 counter->attr.sample_period = value;
Peter Zijlstra08247e32009-06-02 16:46:57 +02001688 counter->hw.sample_period = value;
Peter Zijlstra08247e32009-06-02 16:46:57 +02001689 }
1690unlock:
1691 spin_unlock_irq(&ctx->lock);
1692
1693 return ret;
1694}
1695
Paul Mackerrasd859e292009-01-17 18:10:22 +11001696static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1697{
1698 struct perf_counter *counter = file->private_data;
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001699 void (*func)(struct perf_counter *);
1700 u32 flags = arg;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001701
1702 switch (cmd) {
1703 case PERF_COUNTER_IOC_ENABLE:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001704 func = perf_counter_enable;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001705 break;
1706 case PERF_COUNTER_IOC_DISABLE:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001707 func = perf_counter_disable;
Peter Zijlstra79f14642009-04-06 11:45:07 +02001708 break;
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001709 case PERF_COUNTER_IOC_RESET:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001710 func = perf_counter_reset;
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001711 break;
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001712
1713 case PERF_COUNTER_IOC_REFRESH:
1714 return perf_counter_refresh(counter, arg);
Peter Zijlstra08247e32009-06-02 16:46:57 +02001715
1716 case PERF_COUNTER_IOC_PERIOD:
1717 return perf_counter_period(counter, (u64 __user *)arg);
1718
Paul Mackerrasd859e292009-01-17 18:10:22 +11001719 default:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001720 return -ENOTTY;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001721 }
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001722
1723 if (flags & PERF_IOC_FLAG_GROUP)
1724 perf_counter_for_each(counter, func);
1725 else
1726 perf_counter_for_each_child(counter, func);
1727
1728 return 0;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001729}
1730
Peter Zijlstra771d7cd2009-05-25 14:45:26 +02001731int perf_counter_task_enable(void)
1732{
1733 struct perf_counter *counter;
1734
1735 mutex_lock(&current->perf_counter_mutex);
1736 list_for_each_entry(counter, &current->perf_counter_list, owner_entry)
1737 perf_counter_for_each_child(counter, perf_counter_enable);
1738 mutex_unlock(&current->perf_counter_mutex);
1739
1740 return 0;
1741}
1742
1743int perf_counter_task_disable(void)
1744{
1745 struct perf_counter *counter;
1746
1747 mutex_lock(&current->perf_counter_mutex);
1748 list_for_each_entry(counter, &current->perf_counter_list, owner_entry)
1749 perf_counter_for_each_child(counter, perf_counter_disable);
1750 mutex_unlock(&current->perf_counter_mutex);
1751
1752 return 0;
1753}
1754
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001755/*
1756 * Callers need to ensure there can be no nesting of this function, otherwise
1757 * the seqlock logic goes bad. We can not serialize this because the arch
1758 * code calls this from NMI context.
1759 */
1760void perf_counter_update_userpage(struct perf_counter *counter)
Paul Mackerras37d81822009-03-23 18:22:08 +01001761{
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001762 struct perf_counter_mmap_page *userpg;
Ingo Molnar22a4f652009-06-01 10:13:37 +02001763 struct perf_mmap_data *data;
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001764
1765 rcu_read_lock();
1766 data = rcu_dereference(counter->data);
1767 if (!data)
1768 goto unlock;
1769
1770 userpg = data->user_page;
Paul Mackerras37d81822009-03-23 18:22:08 +01001771
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001772 /*
1773 * Disable preemption so as to not let the corresponding user-space
1774 * spin too long if we get preempted.
1775 */
1776 preempt_disable();
Paul Mackerras37d81822009-03-23 18:22:08 +01001777 ++userpg->lock;
Peter Zijlstra92f22a32009-04-02 11:12:04 +02001778 barrier();
Paul Mackerras37d81822009-03-23 18:22:08 +01001779 userpg->index = counter->hw.idx;
1780 userpg->offset = atomic64_read(&counter->count);
1781 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
1782 userpg->offset -= atomic64_read(&counter->hw.prev_count);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001783
Peter Zijlstra92f22a32009-04-02 11:12:04 +02001784 barrier();
Paul Mackerras37d81822009-03-23 18:22:08 +01001785 ++userpg->lock;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001786 preempt_enable();
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001787unlock:
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001788 rcu_read_unlock();
Paul Mackerras37d81822009-03-23 18:22:08 +01001789}
1790
1791static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1792{
1793 struct perf_counter *counter = vma->vm_file->private_data;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001794 struct perf_mmap_data *data;
1795 int ret = VM_FAULT_SIGBUS;
Paul Mackerras37d81822009-03-23 18:22:08 +01001796
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01001797 if (vmf->flags & FAULT_FLAG_MKWRITE) {
1798 if (vmf->pgoff == 0)
1799 ret = 0;
1800 return ret;
1801 }
1802
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001803 rcu_read_lock();
1804 data = rcu_dereference(counter->data);
1805 if (!data)
1806 goto unlock;
Paul Mackerras37d81822009-03-23 18:22:08 +01001807
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001808 if (vmf->pgoff == 0) {
1809 vmf->page = virt_to_page(data->user_page);
1810 } else {
1811 int nr = vmf->pgoff - 1;
1812
1813 if ((unsigned)nr > data->nr_pages)
1814 goto unlock;
1815
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01001816 if (vmf->flags & FAULT_FLAG_WRITE)
1817 goto unlock;
1818
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001819 vmf->page = virt_to_page(data->data_pages[nr]);
1820 }
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01001821
Paul Mackerras37d81822009-03-23 18:22:08 +01001822 get_page(vmf->page);
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01001823 vmf->page->mapping = vma->vm_file->f_mapping;
1824 vmf->page->index = vmf->pgoff;
1825
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001826 ret = 0;
1827unlock:
1828 rcu_read_unlock();
1829
1830 return ret;
1831}
1832
1833static int perf_mmap_data_alloc(struct perf_counter *counter, int nr_pages)
1834{
1835 struct perf_mmap_data *data;
1836 unsigned long size;
1837 int i;
1838
1839 WARN_ON(atomic_read(&counter->mmap_count));
1840
1841 size = sizeof(struct perf_mmap_data);
1842 size += nr_pages * sizeof(void *);
1843
1844 data = kzalloc(size, GFP_KERNEL);
1845 if (!data)
1846 goto fail;
1847
1848 data->user_page = (void *)get_zeroed_page(GFP_KERNEL);
1849 if (!data->user_page)
1850 goto fail_user_page;
1851
1852 for (i = 0; i < nr_pages; i++) {
1853 data->data_pages[i] = (void *)get_zeroed_page(GFP_KERNEL);
1854 if (!data->data_pages[i])
1855 goto fail_data_pages;
1856 }
1857
1858 data->nr_pages = nr_pages;
Peter Zijlstra22c15582009-05-05 17:50:25 +02001859 atomic_set(&data->lock, -1);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001860
1861 rcu_assign_pointer(counter->data, data);
1862
Paul Mackerras37d81822009-03-23 18:22:08 +01001863 return 0;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001864
1865fail_data_pages:
1866 for (i--; i >= 0; i--)
1867 free_page((unsigned long)data->data_pages[i]);
1868
1869 free_page((unsigned long)data->user_page);
1870
1871fail_user_page:
1872 kfree(data);
1873
1874fail:
1875 return -ENOMEM;
1876}
1877
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01001878static void perf_mmap_free_page(unsigned long addr)
1879{
1880 struct page *page = virt_to_page(addr);
1881
1882 page->mapping = NULL;
1883 __free_page(page);
1884}
1885
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001886static void __perf_mmap_data_free(struct rcu_head *rcu_head)
1887{
Ingo Molnar22a4f652009-06-01 10:13:37 +02001888 struct perf_mmap_data *data;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001889 int i;
1890
Ingo Molnar22a4f652009-06-01 10:13:37 +02001891 data = container_of(rcu_head, struct perf_mmap_data, rcu_head);
1892
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01001893 perf_mmap_free_page((unsigned long)data->user_page);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001894 for (i = 0; i < data->nr_pages; i++)
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01001895 perf_mmap_free_page((unsigned long)data->data_pages[i]);
1896
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001897 kfree(data);
1898}
1899
1900static void perf_mmap_data_free(struct perf_counter *counter)
1901{
1902 struct perf_mmap_data *data = counter->data;
1903
1904 WARN_ON(atomic_read(&counter->mmap_count));
1905
1906 rcu_assign_pointer(counter->data, NULL);
1907 call_rcu(&data->rcu_head, __perf_mmap_data_free);
1908}
1909
1910static void perf_mmap_open(struct vm_area_struct *vma)
1911{
1912 struct perf_counter *counter = vma->vm_file->private_data;
1913
1914 atomic_inc(&counter->mmap_count);
1915}
1916
1917static void perf_mmap_close(struct vm_area_struct *vma)
1918{
1919 struct perf_counter *counter = vma->vm_file->private_data;
1920
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001921 WARN_ON_ONCE(counter->ctx->parent_ctx);
Ingo Molnar22a4f652009-06-01 10:13:37 +02001922 if (atomic_dec_and_mutex_lock(&counter->mmap_count, &counter->mmap_mutex)) {
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001923 struct user_struct *user = current_user();
1924
1925 atomic_long_sub(counter->data->nr_pages + 1, &user->locked_vm);
Peter Zijlstrac5078f72009-05-05 17:50:24 +02001926 vma->vm_mm->locked_vm -= counter->data->nr_locked;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001927 perf_mmap_data_free(counter);
1928 mutex_unlock(&counter->mmap_mutex);
1929 }
Paul Mackerras37d81822009-03-23 18:22:08 +01001930}
1931
1932static struct vm_operations_struct perf_mmap_vmops = {
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01001933 .open = perf_mmap_open,
1934 .close = perf_mmap_close,
1935 .fault = perf_mmap_fault,
1936 .page_mkwrite = perf_mmap_fault,
Paul Mackerras37d81822009-03-23 18:22:08 +01001937};
1938
1939static int perf_mmap(struct file *file, struct vm_area_struct *vma)
1940{
1941 struct perf_counter *counter = file->private_data;
Ingo Molnar22a4f652009-06-01 10:13:37 +02001942 unsigned long user_locked, user_lock_limit;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001943 struct user_struct *user = current_user();
Ingo Molnar22a4f652009-06-01 10:13:37 +02001944 unsigned long locked, lock_limit;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001945 unsigned long vma_size;
1946 unsigned long nr_pages;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001947 long user_extra, extra;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001948 int ret = 0;
Paul Mackerras37d81822009-03-23 18:22:08 +01001949
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01001950 if (!(vma->vm_flags & VM_SHARED))
Paul Mackerras37d81822009-03-23 18:22:08 +01001951 return -EINVAL;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001952
1953 vma_size = vma->vm_end - vma->vm_start;
1954 nr_pages = (vma_size / PAGE_SIZE) - 1;
1955
Peter Zijlstra7730d862009-03-25 12:48:31 +01001956 /*
1957 * If we have data pages ensure they're a power-of-two number, so we
1958 * can do bitmasks instead of modulo.
1959 */
1960 if (nr_pages != 0 && !is_power_of_2(nr_pages))
Paul Mackerras37d81822009-03-23 18:22:08 +01001961 return -EINVAL;
1962
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001963 if (vma_size != PAGE_SIZE * (1 + nr_pages))
Paul Mackerras37d81822009-03-23 18:22:08 +01001964 return -EINVAL;
1965
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001966 if (vma->vm_pgoff != 0)
1967 return -EINVAL;
Paul Mackerras37d81822009-03-23 18:22:08 +01001968
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001969 WARN_ON_ONCE(counter->ctx->parent_ctx);
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001970 mutex_lock(&counter->mmap_mutex);
1971 if (atomic_inc_not_zero(&counter->mmap_count)) {
1972 if (nr_pages != counter->data->nr_pages)
1973 ret = -EINVAL;
1974 goto unlock;
1975 }
1976
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001977 user_extra = nr_pages + 1;
1978 user_lock_limit = sysctl_perf_counter_mlock >> (PAGE_SHIFT - 10);
Ingo Molnara3862d32009-05-24 09:02:37 +02001979
1980 /*
1981 * Increase the limit linearly with more CPUs:
1982 */
1983 user_lock_limit *= num_online_cpus();
1984
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001985 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
Peter Zijlstrac5078f72009-05-05 17:50:24 +02001986
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001987 extra = 0;
1988 if (user_locked > user_lock_limit)
1989 extra = user_locked - user_lock_limit;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001990
1991 lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
1992 lock_limit >>= PAGE_SHIFT;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001993 locked = vma->vm_mm->locked_vm + extra;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001994
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001995 if ((locked > lock_limit) && !capable(CAP_IPC_LOCK)) {
1996 ret = -EPERM;
1997 goto unlock;
1998 }
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001999
2000 WARN_ON(counter->data);
2001 ret = perf_mmap_data_alloc(counter, nr_pages);
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02002002 if (ret)
2003 goto unlock;
2004
2005 atomic_set(&counter->mmap_count, 1);
Peter Zijlstra789f90f2009-05-15 15:19:27 +02002006 atomic_long_add(user_extra, &user->locked_vm);
Peter Zijlstrac5078f72009-05-05 17:50:24 +02002007 vma->vm_mm->locked_vm += extra;
2008 counter->data->nr_locked = extra;
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002009 if (vma->vm_flags & VM_WRITE)
2010 counter->data->writable = 1;
2011
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02002012unlock:
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002013 mutex_unlock(&counter->mmap_mutex);
Paul Mackerras37d81822009-03-23 18:22:08 +01002014
Paul Mackerras37d81822009-03-23 18:22:08 +01002015 vma->vm_flags |= VM_RESERVED;
2016 vma->vm_ops = &perf_mmap_vmops;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002017
2018 return ret;
Paul Mackerras37d81822009-03-23 18:22:08 +01002019}
2020
Peter Zijlstra3c446b3d2009-04-06 11:45:01 +02002021static int perf_fasync(int fd, struct file *filp, int on)
2022{
Peter Zijlstra3c446b3d2009-04-06 11:45:01 +02002023 struct inode *inode = filp->f_path.dentry->d_inode;
Ingo Molnar22a4f652009-06-01 10:13:37 +02002024 struct perf_counter *counter = filp->private_data;
Peter Zijlstra3c446b3d2009-04-06 11:45:01 +02002025 int retval;
2026
2027 mutex_lock(&inode->i_mutex);
2028 retval = fasync_helper(fd, filp, on, &counter->fasync);
2029 mutex_unlock(&inode->i_mutex);
2030
2031 if (retval < 0)
2032 return retval;
2033
2034 return 0;
2035}
2036
Thomas Gleixner0793a612008-12-04 20:12:29 +01002037static const struct file_operations perf_fops = {
2038 .release = perf_release,
2039 .read = perf_read,
2040 .poll = perf_poll,
Paul Mackerrasd859e292009-01-17 18:10:22 +11002041 .unlocked_ioctl = perf_ioctl,
2042 .compat_ioctl = perf_ioctl,
Paul Mackerras37d81822009-03-23 18:22:08 +01002043 .mmap = perf_mmap,
Peter Zijlstra3c446b3d2009-04-06 11:45:01 +02002044 .fasync = perf_fasync,
Thomas Gleixner0793a612008-12-04 20:12:29 +01002045};
2046
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002047/*
Peter Zijlstra925d5192009-03-30 19:07:02 +02002048 * Perf counter wakeup
2049 *
2050 * If there's data, ensure we set the poll() state and publish everything
2051 * to user-space before waking everybody up.
2052 */
2053
2054void perf_counter_wakeup(struct perf_counter *counter)
2055{
Peter Zijlstra925d5192009-03-30 19:07:02 +02002056 wake_up_all(&counter->waitq);
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002057
2058 if (counter->pending_kill) {
2059 kill_fasync(&counter->fasync, SIGIO, counter->pending_kill);
2060 counter->pending_kill = 0;
2061 }
Peter Zijlstra925d5192009-03-30 19:07:02 +02002062}
2063
2064/*
2065 * Pending wakeups
2066 *
2067 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
2068 *
2069 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
2070 * single linked list and use cmpxchg() to add entries lockless.
2071 */
2072
Peter Zijlstra79f14642009-04-06 11:45:07 +02002073static void perf_pending_counter(struct perf_pending_entry *entry)
2074{
2075 struct perf_counter *counter = container_of(entry,
2076 struct perf_counter, pending);
2077
2078 if (counter->pending_disable) {
2079 counter->pending_disable = 0;
2080 perf_counter_disable(counter);
2081 }
2082
2083 if (counter->pending_wakeup) {
2084 counter->pending_wakeup = 0;
2085 perf_counter_wakeup(counter);
2086 }
2087}
2088
Peter Zijlstra671dec52009-04-06 11:45:02 +02002089#define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
Peter Zijlstra925d5192009-03-30 19:07:02 +02002090
Peter Zijlstra671dec52009-04-06 11:45:02 +02002091static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = {
Peter Zijlstra925d5192009-03-30 19:07:02 +02002092 PENDING_TAIL,
2093};
2094
Peter Zijlstra671dec52009-04-06 11:45:02 +02002095static void perf_pending_queue(struct perf_pending_entry *entry,
2096 void (*func)(struct perf_pending_entry *))
Peter Zijlstra925d5192009-03-30 19:07:02 +02002097{
Peter Zijlstra671dec52009-04-06 11:45:02 +02002098 struct perf_pending_entry **head;
Peter Zijlstra925d5192009-03-30 19:07:02 +02002099
Peter Zijlstra671dec52009-04-06 11:45:02 +02002100 if (cmpxchg(&entry->next, NULL, PENDING_TAIL) != NULL)
Peter Zijlstra925d5192009-03-30 19:07:02 +02002101 return;
2102
Peter Zijlstra671dec52009-04-06 11:45:02 +02002103 entry->func = func;
2104
2105 head = &get_cpu_var(perf_pending_head);
Peter Zijlstra925d5192009-03-30 19:07:02 +02002106
2107 do {
Peter Zijlstra671dec52009-04-06 11:45:02 +02002108 entry->next = *head;
2109 } while (cmpxchg(head, entry->next, entry) != entry->next);
Peter Zijlstra925d5192009-03-30 19:07:02 +02002110
2111 set_perf_counter_pending();
2112
Peter Zijlstra671dec52009-04-06 11:45:02 +02002113 put_cpu_var(perf_pending_head);
Peter Zijlstra925d5192009-03-30 19:07:02 +02002114}
2115
2116static int __perf_pending_run(void)
2117{
Peter Zijlstra671dec52009-04-06 11:45:02 +02002118 struct perf_pending_entry *list;
Peter Zijlstra925d5192009-03-30 19:07:02 +02002119 int nr = 0;
2120
Peter Zijlstra671dec52009-04-06 11:45:02 +02002121 list = xchg(&__get_cpu_var(perf_pending_head), PENDING_TAIL);
Peter Zijlstra925d5192009-03-30 19:07:02 +02002122 while (list != PENDING_TAIL) {
Peter Zijlstra671dec52009-04-06 11:45:02 +02002123 void (*func)(struct perf_pending_entry *);
2124 struct perf_pending_entry *entry = list;
Peter Zijlstra925d5192009-03-30 19:07:02 +02002125
2126 list = list->next;
2127
Peter Zijlstra671dec52009-04-06 11:45:02 +02002128 func = entry->func;
2129 entry->next = NULL;
Peter Zijlstra925d5192009-03-30 19:07:02 +02002130 /*
2131 * Ensure we observe the unqueue before we issue the wakeup,
2132 * so that we won't be waiting forever.
2133 * -- see perf_not_pending().
2134 */
2135 smp_wmb();
2136
Peter Zijlstra671dec52009-04-06 11:45:02 +02002137 func(entry);
Peter Zijlstra925d5192009-03-30 19:07:02 +02002138 nr++;
2139 }
2140
2141 return nr;
2142}
2143
2144static inline int perf_not_pending(struct perf_counter *counter)
2145{
2146 /*
2147 * If we flush on whatever cpu we run, there is a chance we don't
2148 * need to wait.
2149 */
2150 get_cpu();
2151 __perf_pending_run();
2152 put_cpu();
2153
2154 /*
2155 * Ensure we see the proper queue state before going to sleep
2156 * so that we do not miss the wakeup. -- see perf_pending_handle()
2157 */
2158 smp_rmb();
Peter Zijlstra671dec52009-04-06 11:45:02 +02002159 return counter->pending.next == NULL;
Peter Zijlstra925d5192009-03-30 19:07:02 +02002160}
2161
2162static void perf_pending_sync(struct perf_counter *counter)
2163{
2164 wait_event(counter->waitq, perf_not_pending(counter));
2165}
2166
2167void perf_counter_do_pending(void)
2168{
2169 __perf_pending_run();
2170}
2171
2172/*
Peter Zijlstra394ee072009-03-30 19:07:14 +02002173 * Callchain support -- arch specific
2174 */
2175
Peter Zijlstra9c03d882009-04-06 11:45:00 +02002176__weak struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
Peter Zijlstra394ee072009-03-30 19:07:14 +02002177{
2178 return NULL;
2179}
2180
2181/*
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002182 * Output
2183 */
2184
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002185struct perf_output_handle {
2186 struct perf_counter *counter;
2187 struct perf_mmap_data *data;
Peter Zijlstra8e3747c2009-06-02 16:16:02 +02002188 unsigned long head;
2189 unsigned long offset;
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002190 int nmi;
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002191 int sample;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002192 int locked;
2193 unsigned long flags;
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002194};
2195
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002196static bool perf_output_space(struct perf_mmap_data *data,
2197 unsigned int offset, unsigned int head)
2198{
2199 unsigned long tail;
2200 unsigned long mask;
2201
2202 if (!data->writable)
2203 return true;
2204
2205 mask = (data->nr_pages << PAGE_SHIFT) - 1;
2206 /*
2207 * Userspace could choose to issue a mb() before updating the tail
2208 * pointer. So that all reads will be completed before the write is
2209 * issued.
2210 */
2211 tail = ACCESS_ONCE(data->user_page->data_tail);
2212 smp_rmb();
2213
2214 offset = (offset - tail) & mask;
2215 head = (head - tail) & mask;
2216
2217 if ((int)(head - offset) < 0)
2218 return false;
2219
2220 return true;
2221}
2222
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002223static void perf_output_wakeup(struct perf_output_handle *handle)
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002224{
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002225 atomic_set(&handle->data->poll, POLL_IN);
2226
Peter Zijlstra671dec52009-04-06 11:45:02 +02002227 if (handle->nmi) {
Peter Zijlstra79f14642009-04-06 11:45:07 +02002228 handle->counter->pending_wakeup = 1;
Peter Zijlstra671dec52009-04-06 11:45:02 +02002229 perf_pending_queue(&handle->counter->pending,
Peter Zijlstra79f14642009-04-06 11:45:07 +02002230 perf_pending_counter);
Peter Zijlstra671dec52009-04-06 11:45:02 +02002231 } else
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002232 perf_counter_wakeup(handle->counter);
2233}
2234
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002235/*
2236 * Curious locking construct.
2237 *
2238 * We need to ensure a later event doesn't publish a head when a former
2239 * event isn't done writing. However since we need to deal with NMIs we
2240 * cannot fully serialize things.
2241 *
2242 * What we do is serialize between CPUs so we only have to deal with NMI
2243 * nesting on a single CPU.
2244 *
2245 * We only publish the head (and generate a wakeup) when the outer-most
2246 * event completes.
2247 */
2248static void perf_output_lock(struct perf_output_handle *handle)
2249{
2250 struct perf_mmap_data *data = handle->data;
2251 int cpu;
2252
2253 handle->locked = 0;
2254
2255 local_irq_save(handle->flags);
2256 cpu = smp_processor_id();
2257
2258 if (in_nmi() && atomic_read(&data->lock) == cpu)
2259 return;
2260
Peter Zijlstra22c15582009-05-05 17:50:25 +02002261 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002262 cpu_relax();
2263
2264 handle->locked = 1;
2265}
2266
2267static void perf_output_unlock(struct perf_output_handle *handle)
2268{
2269 struct perf_mmap_data *data = handle->data;
Peter Zijlstra8e3747c2009-06-02 16:16:02 +02002270 unsigned long head;
2271 int cpu;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002272
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002273 data->done_head = data->head;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002274
2275 if (!handle->locked)
2276 goto out;
2277
2278again:
2279 /*
2280 * The xchg implies a full barrier that ensures all writes are done
2281 * before we publish the new head, matched by a rmb() in userspace when
2282 * reading this position.
2283 */
Peter Zijlstra8e3747c2009-06-02 16:16:02 +02002284 while ((head = atomic_long_xchg(&data->done_head, 0)))
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002285 data->user_page->data_head = head;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002286
2287 /*
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002288 * NMI can happen here, which means we can miss a done_head update.
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002289 */
2290
Peter Zijlstra22c15582009-05-05 17:50:25 +02002291 cpu = atomic_xchg(&data->lock, -1);
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002292 WARN_ON_ONCE(cpu != smp_processor_id());
2293
2294 /*
2295 * Therefore we have to validate we did not indeed do so.
2296 */
Peter Zijlstra8e3747c2009-06-02 16:16:02 +02002297 if (unlikely(atomic_long_read(&data->done_head))) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002298 /*
2299 * Since we had it locked, we can lock it again.
2300 */
Peter Zijlstra22c15582009-05-05 17:50:25 +02002301 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002302 cpu_relax();
2303
2304 goto again;
2305 }
2306
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002307 if (atomic_xchg(&data->wakeup, 0))
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002308 perf_output_wakeup(handle);
2309out:
2310 local_irq_restore(handle->flags);
2311}
2312
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002313static void perf_output_copy(struct perf_output_handle *handle,
Peter Zijlstra089dd792009-06-05 14:04:55 +02002314 const void *buf, unsigned int len)
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002315{
2316 unsigned int pages_mask;
2317 unsigned int offset;
2318 unsigned int size;
2319 void **pages;
2320
2321 offset = handle->offset;
2322 pages_mask = handle->data->nr_pages - 1;
2323 pages = handle->data->data_pages;
2324
2325 do {
2326 unsigned int page_offset;
2327 int nr;
2328
2329 nr = (offset >> PAGE_SHIFT) & pages_mask;
2330 page_offset = offset & (PAGE_SIZE - 1);
2331 size = min_t(unsigned int, PAGE_SIZE - page_offset, len);
2332
2333 memcpy(pages[nr] + page_offset, buf, size);
2334
2335 len -= size;
2336 buf += size;
2337 offset += size;
2338 } while (len);
2339
2340 handle->offset = offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01002341
Peter Zijlstra53020fe2009-05-13 21:26:19 +02002342 /*
2343 * Check we didn't copy past our reservation window, taking the
2344 * possible unsigned int wrap into account.
2345 */
Peter Zijlstra8e3747c2009-06-02 16:16:02 +02002346 WARN_ON_ONCE(((long)(handle->head - handle->offset)) < 0);
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002347}
2348
Peter Zijlstra5c148192009-03-25 12:30:23 +01002349#define perf_output_put(handle, x) \
2350 perf_output_copy((handle), &(x), sizeof(x))
2351
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002352static int perf_output_begin(struct perf_output_handle *handle,
2353 struct perf_counter *counter, unsigned int size,
2354 int nmi, int sample)
2355{
2356 struct perf_mmap_data *data;
2357 unsigned int offset, head;
2358 int have_lost;
2359 struct {
2360 struct perf_event_header header;
2361 u64 id;
2362 u64 lost;
2363 } lost_event;
2364
2365 /*
2366 * For inherited counters we send all the output towards the parent.
2367 */
2368 if (counter->parent)
2369 counter = counter->parent;
2370
2371 rcu_read_lock();
2372 data = rcu_dereference(counter->data);
2373 if (!data)
2374 goto out;
2375
2376 handle->data = data;
2377 handle->counter = counter;
2378 handle->nmi = nmi;
2379 handle->sample = sample;
2380
2381 if (!data->nr_pages)
2382 goto fail;
2383
2384 have_lost = atomic_read(&data->lost);
2385 if (have_lost)
2386 size += sizeof(lost_event);
2387
2388 perf_output_lock(handle);
2389
2390 do {
2391 offset = head = atomic_long_read(&data->head);
2392 head += size;
2393 if (unlikely(!perf_output_space(data, offset, head)))
2394 goto fail;
2395 } while (atomic_long_cmpxchg(&data->head, offset, head) != offset);
2396
2397 handle->offset = offset;
2398 handle->head = head;
2399
2400 if ((offset >> PAGE_SHIFT) != (head >> PAGE_SHIFT))
2401 atomic_set(&data->wakeup, 1);
2402
2403 if (have_lost) {
2404 lost_event.header.type = PERF_EVENT_LOST;
2405 lost_event.header.misc = 0;
2406 lost_event.header.size = sizeof(lost_event);
2407 lost_event.id = counter->id;
2408 lost_event.lost = atomic_xchg(&data->lost, 0);
2409
2410 perf_output_put(handle, lost_event);
2411 }
2412
2413 return 0;
2414
2415fail:
2416 atomic_inc(&data->lost);
2417 perf_output_unlock(handle);
2418out:
2419 rcu_read_unlock();
2420
2421 return -ENOSPC;
2422}
2423
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002424static void perf_output_end(struct perf_output_handle *handle)
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002425{
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002426 struct perf_counter *counter = handle->counter;
2427 struct perf_mmap_data *data = handle->data;
2428
Peter Zijlstra0d486962009-06-02 19:22:16 +02002429 int wakeup_events = counter->attr.wakeup_events;
Peter Zijlstrac4578102009-04-02 11:12:01 +02002430
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002431 if (handle->sample && wakeup_events) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002432 int events = atomic_inc_return(&data->events);
Peter Zijlstrac4578102009-04-02 11:12:01 +02002433 if (events >= wakeup_events) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002434 atomic_sub(wakeup_events, &data->events);
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002435 atomic_set(&data->wakeup, 1);
Peter Zijlstrac4578102009-04-02 11:12:01 +02002436 }
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002437 }
2438
2439 perf_output_unlock(handle);
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002440 rcu_read_unlock();
2441}
2442
Peter Zijlstra709e50c2009-06-02 14:13:15 +02002443static u32 perf_counter_pid(struct perf_counter *counter, struct task_struct *p)
2444{
2445 /*
2446 * only top level counters have the pid namespace they were created in
2447 */
2448 if (counter->parent)
2449 counter = counter->parent;
2450
2451 return task_tgid_nr_ns(p, counter->ns);
2452}
2453
2454static u32 perf_counter_tid(struct perf_counter *counter, struct task_struct *p)
2455{
2456 /*
2457 * only top level counters have the pid namespace they were created in
2458 */
2459 if (counter->parent)
2460 counter = counter->parent;
2461
2462 return task_pid_nr_ns(p, counter->ns);
2463}
2464
Peter Zijlstradf1a1322009-06-10 21:02:22 +02002465static void perf_counter_output(struct perf_counter *counter, int nmi,
2466 struct perf_sample_data *data)
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002467{
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002468 int ret;
Peter Zijlstra0d486962009-06-02 19:22:16 +02002469 u64 sample_type = counter->attr.sample_type;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002470 struct perf_output_handle handle;
2471 struct perf_event_header header;
2472 u64 ip;
Peter Zijlstra5c148192009-03-25 12:30:23 +01002473 struct {
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002474 u32 pid, tid;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002475 } tid_entry;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002476 struct {
Peter Zijlstra8e5799b2009-06-02 15:08:15 +02002477 u64 id;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002478 u64 counter;
2479 } group_entry;
Peter Zijlstra394ee072009-03-30 19:07:14 +02002480 struct perf_callchain_entry *callchain = NULL;
2481 int callchain_size = 0;
Peter Zijlstra339f7c92009-04-06 11:45:06 +02002482 u64 time;
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002483 struct {
2484 u32 cpu, reserved;
2485 } cpu_entry;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002486
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02002487 header.type = 0;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002488 header.size = sizeof(header);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002489
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02002490 header.misc = PERF_EVENT_MISC_OVERFLOW;
Peter Zijlstradf1a1322009-06-10 21:02:22 +02002491 header.misc |= perf_misc_flags(data->regs);
Peter Zijlstra6fab0192009-04-08 15:01:26 +02002492
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002493 if (sample_type & PERF_SAMPLE_IP) {
Peter Zijlstradf1a1322009-06-10 21:02:22 +02002494 ip = perf_instruction_pointer(data->regs);
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002495 header.type |= PERF_SAMPLE_IP;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002496 header.size += sizeof(ip);
2497 }
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002498
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002499 if (sample_type & PERF_SAMPLE_TID) {
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002500 /* namespace issues */
Peter Zijlstra709e50c2009-06-02 14:13:15 +02002501 tid_entry.pid = perf_counter_pid(counter, current);
2502 tid_entry.tid = perf_counter_tid(counter, current);
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002503
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002504 header.type |= PERF_SAMPLE_TID;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002505 header.size += sizeof(tid_entry);
2506 }
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002507
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002508 if (sample_type & PERF_SAMPLE_TIME) {
Peter Zijlstra4d855452009-04-08 15:01:32 +02002509 /*
2510 * Maybe do better on x86 and provide cpu_clock_nmi()
2511 */
2512 time = sched_clock();
2513
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002514 header.type |= PERF_SAMPLE_TIME;
Peter Zijlstra4d855452009-04-08 15:01:32 +02002515 header.size += sizeof(u64);
2516 }
2517
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002518 if (sample_type & PERF_SAMPLE_ADDR) {
2519 header.type |= PERF_SAMPLE_ADDR;
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002520 header.size += sizeof(u64);
2521 }
2522
Peter Zijlstraac4bcf82009-06-05 14:44:52 +02002523 if (sample_type & PERF_SAMPLE_ID) {
2524 header.type |= PERF_SAMPLE_ID;
Peter Zijlstraa85f61a2009-05-08 18:52:23 +02002525 header.size += sizeof(u64);
2526 }
2527
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002528 if (sample_type & PERF_SAMPLE_CPU) {
2529 header.type |= PERF_SAMPLE_CPU;
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002530 header.size += sizeof(cpu_entry);
2531
2532 cpu_entry.cpu = raw_smp_processor_id();
2533 }
2534
Peter Zijlstra689802b2009-06-05 15:05:43 +02002535 if (sample_type & PERF_SAMPLE_PERIOD) {
2536 header.type |= PERF_SAMPLE_PERIOD;
2537 header.size += sizeof(u64);
2538 }
2539
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002540 if (sample_type & PERF_SAMPLE_GROUP) {
2541 header.type |= PERF_SAMPLE_GROUP;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002542 header.size += sizeof(u64) +
2543 counter->nr_siblings * sizeof(group_entry);
2544 }
2545
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002546 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
Peter Zijlstradf1a1322009-06-10 21:02:22 +02002547 callchain = perf_callchain(data->regs);
Peter Zijlstra394ee072009-03-30 19:07:14 +02002548
2549 if (callchain) {
Peter Zijlstra9c03d882009-04-06 11:45:00 +02002550 callchain_size = (1 + callchain->nr) * sizeof(u64);
Peter Zijlstra394ee072009-03-30 19:07:14 +02002551
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002552 header.type |= PERF_SAMPLE_CALLCHAIN;
Peter Zijlstra394ee072009-03-30 19:07:14 +02002553 header.size += callchain_size;
2554 }
2555 }
2556
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002557 ret = perf_output_begin(&handle, counter, header.size, nmi, 1);
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002558 if (ret)
2559 return;
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002560
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002561 perf_output_put(&handle, header);
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002562
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002563 if (sample_type & PERF_SAMPLE_IP)
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002564 perf_output_put(&handle, ip);
2565
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002566 if (sample_type & PERF_SAMPLE_TID)
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002567 perf_output_put(&handle, tid_entry);
2568
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002569 if (sample_type & PERF_SAMPLE_TIME)
Peter Zijlstra4d855452009-04-08 15:01:32 +02002570 perf_output_put(&handle, time);
2571
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002572 if (sample_type & PERF_SAMPLE_ADDR)
Peter Zijlstradf1a1322009-06-10 21:02:22 +02002573 perf_output_put(&handle, data->addr);
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002574
Peter Zijlstraac4bcf82009-06-05 14:44:52 +02002575 if (sample_type & PERF_SAMPLE_ID)
2576 perf_output_put(&handle, counter->id);
Peter Zijlstraa85f61a2009-05-08 18:52:23 +02002577
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002578 if (sample_type & PERF_SAMPLE_CPU)
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002579 perf_output_put(&handle, cpu_entry);
2580
Peter Zijlstra689802b2009-06-05 15:05:43 +02002581 if (sample_type & PERF_SAMPLE_PERIOD)
Peter Zijlstra9e350de2009-06-10 21:34:59 +02002582 perf_output_put(&handle, data->period);
Peter Zijlstra689802b2009-06-05 15:05:43 +02002583
Peter Zijlstra2023b352009-05-05 17:50:26 +02002584 /*
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002585 * XXX PERF_SAMPLE_GROUP vs inherited counters seems difficult.
Peter Zijlstra2023b352009-05-05 17:50:26 +02002586 */
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002587 if (sample_type & PERF_SAMPLE_GROUP) {
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002588 struct perf_counter *leader, *sub;
2589 u64 nr = counter->nr_siblings;
2590
2591 perf_output_put(&handle, nr);
2592
2593 leader = counter->group_leader;
2594 list_for_each_entry(sub, &leader->sibling_list, list_entry) {
2595 if (sub != counter)
Robert Richter4aeb0b42009-04-29 12:47:03 +02002596 sub->pmu->read(sub);
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002597
Peter Zijlstra8e5799b2009-06-02 15:08:15 +02002598 group_entry.id = sub->id;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002599 group_entry.counter = atomic64_read(&sub->count);
2600
2601 perf_output_put(&handle, group_entry);
2602 }
2603 }
2604
Peter Zijlstra394ee072009-03-30 19:07:14 +02002605 if (callchain)
2606 perf_output_copy(&handle, callchain, callchain_size);
2607
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002608 perf_output_end(&handle);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002609}
2610
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002611/*
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002612 * fork tracking
2613 */
2614
2615struct perf_fork_event {
2616 struct task_struct *task;
2617
2618 struct {
2619 struct perf_event_header header;
2620
2621 u32 pid;
2622 u32 ppid;
2623 } event;
2624};
2625
2626static void perf_counter_fork_output(struct perf_counter *counter,
2627 struct perf_fork_event *fork_event)
2628{
2629 struct perf_output_handle handle;
2630 int size = fork_event->event.header.size;
2631 struct task_struct *task = fork_event->task;
2632 int ret = perf_output_begin(&handle, counter, size, 0, 0);
2633
2634 if (ret)
2635 return;
2636
2637 fork_event->event.pid = perf_counter_pid(counter, task);
2638 fork_event->event.ppid = perf_counter_pid(counter, task->real_parent);
2639
2640 perf_output_put(&handle, fork_event->event);
2641 perf_output_end(&handle);
2642}
2643
2644static int perf_counter_fork_match(struct perf_counter *counter)
2645{
Peter Zijlstrad99e9442009-06-04 17:08:58 +02002646 if (counter->attr.comm || counter->attr.mmap)
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002647 return 1;
2648
2649 return 0;
2650}
2651
2652static void perf_counter_fork_ctx(struct perf_counter_context *ctx,
2653 struct perf_fork_event *fork_event)
2654{
2655 struct perf_counter *counter;
2656
2657 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2658 return;
2659
2660 rcu_read_lock();
2661 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2662 if (perf_counter_fork_match(counter))
2663 perf_counter_fork_output(counter, fork_event);
2664 }
2665 rcu_read_unlock();
2666}
2667
2668static void perf_counter_fork_event(struct perf_fork_event *fork_event)
2669{
2670 struct perf_cpu_context *cpuctx;
2671 struct perf_counter_context *ctx;
2672
2673 cpuctx = &get_cpu_var(perf_cpu_context);
2674 perf_counter_fork_ctx(&cpuctx->ctx, fork_event);
2675 put_cpu_var(perf_cpu_context);
2676
2677 rcu_read_lock();
2678 /*
2679 * doesn't really matter which of the child contexts the
2680 * events ends up in.
2681 */
2682 ctx = rcu_dereference(current->perf_counter_ctxp);
2683 if (ctx)
2684 perf_counter_fork_ctx(ctx, fork_event);
2685 rcu_read_unlock();
2686}
2687
2688void perf_counter_fork(struct task_struct *task)
2689{
2690 struct perf_fork_event fork_event;
2691
2692 if (!atomic_read(&nr_comm_counters) &&
Peter Zijlstrad99e9442009-06-04 17:08:58 +02002693 !atomic_read(&nr_mmap_counters))
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002694 return;
2695
2696 fork_event = (struct perf_fork_event){
2697 .task = task,
2698 .event = {
2699 .header = {
2700 .type = PERF_EVENT_FORK,
2701 .size = sizeof(fork_event.event),
2702 },
2703 },
2704 };
2705
2706 perf_counter_fork_event(&fork_event);
2707}
2708
2709/*
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002710 * comm tracking
2711 */
2712
2713struct perf_comm_event {
Ingo Molnar22a4f652009-06-01 10:13:37 +02002714 struct task_struct *task;
2715 char *comm;
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002716 int comm_size;
2717
2718 struct {
2719 struct perf_event_header header;
2720
2721 u32 pid;
2722 u32 tid;
2723 } event;
2724};
2725
2726static void perf_counter_comm_output(struct perf_counter *counter,
2727 struct perf_comm_event *comm_event)
2728{
2729 struct perf_output_handle handle;
2730 int size = comm_event->event.header.size;
2731 int ret = perf_output_begin(&handle, counter, size, 0, 0);
2732
2733 if (ret)
2734 return;
2735
Peter Zijlstra709e50c2009-06-02 14:13:15 +02002736 comm_event->event.pid = perf_counter_pid(counter, comm_event->task);
2737 comm_event->event.tid = perf_counter_tid(counter, comm_event->task);
2738
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002739 perf_output_put(&handle, comm_event->event);
2740 perf_output_copy(&handle, comm_event->comm,
2741 comm_event->comm_size);
2742 perf_output_end(&handle);
2743}
2744
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002745static int perf_counter_comm_match(struct perf_counter *counter)
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002746{
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002747 if (counter->attr.comm)
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002748 return 1;
2749
2750 return 0;
2751}
2752
2753static void perf_counter_comm_ctx(struct perf_counter_context *ctx,
2754 struct perf_comm_event *comm_event)
2755{
2756 struct perf_counter *counter;
2757
2758 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2759 return;
2760
2761 rcu_read_lock();
2762 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002763 if (perf_counter_comm_match(counter))
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002764 perf_counter_comm_output(counter, comm_event);
2765 }
2766 rcu_read_unlock();
2767}
2768
2769static void perf_counter_comm_event(struct perf_comm_event *comm_event)
2770{
2771 struct perf_cpu_context *cpuctx;
Peter Zijlstra665c2142009-05-29 14:51:57 +02002772 struct perf_counter_context *ctx;
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002773 unsigned int size;
2774 char *comm = comm_event->task->comm;
2775
Ingo Molnar888fcee2009-04-09 09:48:22 +02002776 size = ALIGN(strlen(comm)+1, sizeof(u64));
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002777
2778 comm_event->comm = comm;
2779 comm_event->comm_size = size;
2780
2781 comm_event->event.header.size = sizeof(comm_event->event) + size;
2782
2783 cpuctx = &get_cpu_var(perf_cpu_context);
2784 perf_counter_comm_ctx(&cpuctx->ctx, comm_event);
2785 put_cpu_var(perf_cpu_context);
Peter Zijlstra665c2142009-05-29 14:51:57 +02002786
2787 rcu_read_lock();
2788 /*
2789 * doesn't really matter which of the child contexts the
2790 * events ends up in.
2791 */
2792 ctx = rcu_dereference(current->perf_counter_ctxp);
2793 if (ctx)
2794 perf_counter_comm_ctx(ctx, comm_event);
2795 rcu_read_unlock();
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002796}
2797
2798void perf_counter_comm(struct task_struct *task)
2799{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002800 struct perf_comm_event comm_event;
2801
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002802 if (!atomic_read(&nr_comm_counters))
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002803 return;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10002804
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002805 comm_event = (struct perf_comm_event){
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002806 .task = task,
2807 .event = {
2808 .header = { .type = PERF_EVENT_COMM, },
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002809 },
2810 };
2811
2812 perf_counter_comm_event(&comm_event);
2813}
2814
2815/*
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002816 * mmap tracking
2817 */
2818
2819struct perf_mmap_event {
Peter Zijlstra089dd792009-06-05 14:04:55 +02002820 struct vm_area_struct *vma;
2821
2822 const char *file_name;
2823 int file_size;
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002824
2825 struct {
2826 struct perf_event_header header;
2827
2828 u32 pid;
2829 u32 tid;
2830 u64 start;
2831 u64 len;
2832 u64 pgoff;
2833 } event;
2834};
2835
2836static void perf_counter_mmap_output(struct perf_counter *counter,
2837 struct perf_mmap_event *mmap_event)
2838{
2839 struct perf_output_handle handle;
2840 int size = mmap_event->event.header.size;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002841 int ret = perf_output_begin(&handle, counter, size, 0, 0);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002842
2843 if (ret)
2844 return;
2845
Peter Zijlstra709e50c2009-06-02 14:13:15 +02002846 mmap_event->event.pid = perf_counter_pid(counter, current);
2847 mmap_event->event.tid = perf_counter_tid(counter, current);
2848
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002849 perf_output_put(&handle, mmap_event->event);
2850 perf_output_copy(&handle, mmap_event->file_name,
2851 mmap_event->file_size);
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002852 perf_output_end(&handle);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002853}
2854
2855static int perf_counter_mmap_match(struct perf_counter *counter,
2856 struct perf_mmap_event *mmap_event)
2857{
Peter Zijlstrad99e9442009-06-04 17:08:58 +02002858 if (counter->attr.mmap)
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002859 return 1;
2860
2861 return 0;
2862}
2863
2864static void perf_counter_mmap_ctx(struct perf_counter_context *ctx,
2865 struct perf_mmap_event *mmap_event)
2866{
2867 struct perf_counter *counter;
2868
2869 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2870 return;
2871
2872 rcu_read_lock();
2873 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2874 if (perf_counter_mmap_match(counter, mmap_event))
2875 perf_counter_mmap_output(counter, mmap_event);
2876 }
2877 rcu_read_unlock();
2878}
2879
2880static void perf_counter_mmap_event(struct perf_mmap_event *mmap_event)
2881{
2882 struct perf_cpu_context *cpuctx;
Peter Zijlstra665c2142009-05-29 14:51:57 +02002883 struct perf_counter_context *ctx;
Peter Zijlstra089dd792009-06-05 14:04:55 +02002884 struct vm_area_struct *vma = mmap_event->vma;
2885 struct file *file = vma->vm_file;
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002886 unsigned int size;
2887 char tmp[16];
2888 char *buf = NULL;
Peter Zijlstra089dd792009-06-05 14:04:55 +02002889 const char *name;
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002890
2891 if (file) {
2892 buf = kzalloc(PATH_MAX, GFP_KERNEL);
2893 if (!buf) {
2894 name = strncpy(tmp, "//enomem", sizeof(tmp));
2895 goto got_name;
2896 }
Peter Zijlstrad3d21c42009-04-09 10:53:46 +02002897 name = d_path(&file->f_path, buf, PATH_MAX);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002898 if (IS_ERR(name)) {
2899 name = strncpy(tmp, "//toolong", sizeof(tmp));
2900 goto got_name;
2901 }
2902 } else {
Peter Zijlstra089dd792009-06-05 14:04:55 +02002903 name = arch_vma_name(mmap_event->vma);
2904 if (name)
2905 goto got_name;
2906
2907 if (!vma->vm_mm) {
2908 name = strncpy(tmp, "[vdso]", sizeof(tmp));
2909 goto got_name;
2910 }
2911
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002912 name = strncpy(tmp, "//anon", sizeof(tmp));
2913 goto got_name;
2914 }
2915
2916got_name:
Ingo Molnar888fcee2009-04-09 09:48:22 +02002917 size = ALIGN(strlen(name)+1, sizeof(u64));
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002918
2919 mmap_event->file_name = name;
2920 mmap_event->file_size = size;
2921
2922 mmap_event->event.header.size = sizeof(mmap_event->event) + size;
2923
2924 cpuctx = &get_cpu_var(perf_cpu_context);
2925 perf_counter_mmap_ctx(&cpuctx->ctx, mmap_event);
2926 put_cpu_var(perf_cpu_context);
2927
Peter Zijlstra665c2142009-05-29 14:51:57 +02002928 rcu_read_lock();
2929 /*
2930 * doesn't really matter which of the child contexts the
2931 * events ends up in.
2932 */
2933 ctx = rcu_dereference(current->perf_counter_ctxp);
2934 if (ctx)
2935 perf_counter_mmap_ctx(ctx, mmap_event);
2936 rcu_read_unlock();
2937
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002938 kfree(buf);
2939}
2940
Peter Zijlstra089dd792009-06-05 14:04:55 +02002941void __perf_counter_mmap(struct vm_area_struct *vma)
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002942{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002943 struct perf_mmap_event mmap_event;
2944
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002945 if (!atomic_read(&nr_mmap_counters))
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002946 return;
2947
2948 mmap_event = (struct perf_mmap_event){
Peter Zijlstra089dd792009-06-05 14:04:55 +02002949 .vma = vma,
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002950 .event = {
2951 .header = { .type = PERF_EVENT_MMAP, },
Peter Zijlstra089dd792009-06-05 14:04:55 +02002952 .start = vma->vm_start,
2953 .len = vma->vm_end - vma->vm_start,
2954 .pgoff = vma->vm_pgoff,
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002955 },
2956 };
2957
2958 perf_counter_mmap_event(&mmap_event);
2959}
2960
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002961/*
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002962 * Log sample_period changes so that analyzing tools can re-normalize the
Peter Zijlstrae220d2d2009-05-23 18:28:55 +02002963 * event flow.
Peter Zijlstra26b119b2009-05-20 12:21:20 +02002964 */
2965
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02002966struct freq_event {
2967 struct perf_event_header header;
2968 u64 time;
2969 u64 id;
2970 u64 period;
2971};
2972
Peter Zijlstra26b119b2009-05-20 12:21:20 +02002973static void perf_log_period(struct perf_counter *counter, u64 period)
2974{
2975 struct perf_output_handle handle;
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02002976 struct freq_event event;
Peter Zijlstra26b119b2009-05-20 12:21:20 +02002977 int ret;
2978
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02002979 if (counter->hw.sample_period == period)
2980 return;
2981
2982 if (counter->attr.sample_type & PERF_SAMPLE_PERIOD)
2983 return;
2984
2985 event = (struct freq_event) {
Peter Zijlstra26b119b2009-05-20 12:21:20 +02002986 .header = {
2987 .type = PERF_EVENT_PERIOD,
2988 .misc = 0,
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02002989 .size = sizeof(event),
Peter Zijlstra26b119b2009-05-20 12:21:20 +02002990 },
2991 .time = sched_clock(),
Peter Zijlstra689802b2009-06-05 15:05:43 +02002992 .id = counter->id,
Peter Zijlstra26b119b2009-05-20 12:21:20 +02002993 .period = period,
2994 };
2995
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02002996 ret = perf_output_begin(&handle, counter, sizeof(event), 1, 0);
Peter Zijlstra26b119b2009-05-20 12:21:20 +02002997 if (ret)
2998 return;
2999
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02003000 perf_output_put(&handle, event);
Peter Zijlstra26b119b2009-05-20 12:21:20 +02003001 perf_output_end(&handle);
3002}
3003
3004/*
Peter Zijlstraa78ac322009-05-25 17:39:05 +02003005 * IRQ throttle logging
3006 */
3007
3008static void perf_log_throttle(struct perf_counter *counter, int enable)
3009{
3010 struct perf_output_handle handle;
3011 int ret;
3012
3013 struct {
3014 struct perf_event_header header;
3015 u64 time;
Peter Zijlstracca3f452009-06-11 14:57:55 +02003016 u64 id;
Peter Zijlstraa78ac322009-05-25 17:39:05 +02003017 } throttle_event = {
3018 .header = {
3019 .type = PERF_EVENT_THROTTLE + 1,
3020 .misc = 0,
3021 .size = sizeof(throttle_event),
3022 },
Peter Zijlstracca3f452009-06-11 14:57:55 +02003023 .time = sched_clock(),
3024 .id = counter->id,
Peter Zijlstraa78ac322009-05-25 17:39:05 +02003025 };
3026
Ingo Molnar0127c3e2009-05-25 22:03:26 +02003027 ret = perf_output_begin(&handle, counter, sizeof(throttle_event), 1, 0);
Peter Zijlstraa78ac322009-05-25 17:39:05 +02003028 if (ret)
3029 return;
3030
3031 perf_output_put(&handle, throttle_event);
3032 perf_output_end(&handle);
3033}
3034
3035/*
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01003036 * Generic counter overflow handling, sampling.
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02003037 */
3038
Peter Zijlstradf1a1322009-06-10 21:02:22 +02003039int perf_counter_overflow(struct perf_counter *counter, int nmi,
3040 struct perf_sample_data *data)
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02003041{
Peter Zijlstra79f14642009-04-06 11:45:07 +02003042 int events = atomic_read(&counter->event_limit);
Peter Zijlstraa78ac322009-05-25 17:39:05 +02003043 int throttle = counter->pmu->unthrottle != NULL;
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02003044 struct hw_perf_counter *hwc = &counter->hw;
Peter Zijlstra79f14642009-04-06 11:45:07 +02003045 int ret = 0;
3046
Peter Zijlstraa78ac322009-05-25 17:39:05 +02003047 if (!throttle) {
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02003048 hwc->interrupts++;
Ingo Molnar128f0482009-06-03 22:19:36 +02003049 } else {
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02003050 if (hwc->interrupts != MAX_INTERRUPTS) {
3051 hwc->interrupts++;
Peter Zijlstradf58ab22009-06-11 11:25:05 +02003052 if (HZ * hwc->interrupts >
3053 (u64)sysctl_perf_counter_sample_rate) {
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02003054 hwc->interrupts = MAX_INTERRUPTS;
Ingo Molnar128f0482009-06-03 22:19:36 +02003055 perf_log_throttle(counter, 0);
3056 ret = 1;
3057 }
3058 } else {
3059 /*
3060 * Keep re-disabling counters even though on the previous
3061 * pass we disabled it - just in case we raced with a
3062 * sched-in and the counter got enabled again:
3063 */
Peter Zijlstraa78ac322009-05-25 17:39:05 +02003064 ret = 1;
3065 }
3066 }
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003067
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02003068 if (counter->attr.freq) {
3069 u64 now = sched_clock();
3070 s64 delta = now - hwc->freq_stamp;
3071
3072 hwc->freq_stamp = now;
3073
3074 if (delta > 0 && delta < TICK_NSEC)
3075 perf_adjust_period(counter, NSEC_PER_SEC / (int)delta);
3076 }
3077
Peter Zijlstra2023b352009-05-05 17:50:26 +02003078 /*
3079 * XXX event_limit might not quite work as expected on inherited
3080 * counters
3081 */
3082
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02003083 counter->pending_kill = POLL_IN;
Peter Zijlstra79f14642009-04-06 11:45:07 +02003084 if (events && atomic_dec_and_test(&counter->event_limit)) {
3085 ret = 1;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02003086 counter->pending_kill = POLL_HUP;
Peter Zijlstra79f14642009-04-06 11:45:07 +02003087 if (nmi) {
3088 counter->pending_disable = 1;
3089 perf_pending_queue(&counter->pending,
3090 perf_pending_counter);
3091 } else
3092 perf_counter_disable(counter);
3093 }
3094
Peter Zijlstradf1a1322009-06-10 21:02:22 +02003095 perf_counter_output(counter, nmi, data);
Peter Zijlstra79f14642009-04-06 11:45:07 +02003096 return ret;
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02003097}
3098
3099/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003100 * Generic software counter infrastructure
3101 */
3102
3103static void perf_swcounter_update(struct perf_counter *counter)
3104{
3105 struct hw_perf_counter *hwc = &counter->hw;
3106 u64 prev, now;
3107 s64 delta;
3108
3109again:
3110 prev = atomic64_read(&hwc->prev_count);
3111 now = atomic64_read(&hwc->count);
3112 if (atomic64_cmpxchg(&hwc->prev_count, prev, now) != prev)
3113 goto again;
3114
3115 delta = now - prev;
3116
3117 atomic64_add(delta, &counter->count);
3118 atomic64_sub(delta, &hwc->period_left);
3119}
3120
3121static void perf_swcounter_set_period(struct perf_counter *counter)
3122{
3123 struct hw_perf_counter *hwc = &counter->hw;
3124 s64 left = atomic64_read(&hwc->period_left);
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003125 s64 period = hwc->sample_period;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003126
3127 if (unlikely(left <= -period)) {
3128 left = period;
3129 atomic64_set(&hwc->period_left, left);
Peter Zijlstra9e350de2009-06-10 21:34:59 +02003130 hwc->last_period = period;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003131 }
3132
3133 if (unlikely(left <= 0)) {
3134 left += period;
3135 atomic64_add(period, &hwc->period_left);
Peter Zijlstra9e350de2009-06-10 21:34:59 +02003136 hwc->last_period = period;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003137 }
3138
3139 atomic64_set(&hwc->prev_count, -left);
3140 atomic64_set(&hwc->count, -left);
3141}
3142
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003143static enum hrtimer_restart perf_swcounter_hrtimer(struct hrtimer *hrtimer)
3144{
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02003145 enum hrtimer_restart ret = HRTIMER_RESTART;
Peter Zijlstradf1a1322009-06-10 21:02:22 +02003146 struct perf_sample_data data;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003147 struct perf_counter *counter;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003148 u64 period;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003149
3150 counter = container_of(hrtimer, struct perf_counter, hw.hrtimer);
Robert Richter4aeb0b42009-04-29 12:47:03 +02003151 counter->pmu->read(counter);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003152
Peter Zijlstradf1a1322009-06-10 21:02:22 +02003153 data.addr = 0;
3154 data.regs = get_irq_regs();
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003155 /*
3156 * In case we exclude kernel IPs or are somehow not in interrupt
3157 * context, provide the next best thing, the user IP.
3158 */
Peter Zijlstradf1a1322009-06-10 21:02:22 +02003159 if ((counter->attr.exclude_kernel || !data.regs) &&
Peter Zijlstra0d486962009-06-02 19:22:16 +02003160 !counter->attr.exclude_user)
Peter Zijlstradf1a1322009-06-10 21:02:22 +02003161 data.regs = task_pt_regs(current);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003162
Peter Zijlstradf1a1322009-06-10 21:02:22 +02003163 if (data.regs) {
3164 if (perf_counter_overflow(counter, 0, &data))
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02003165 ret = HRTIMER_NORESTART;
3166 }
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003167
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003168 period = max_t(u64, 10000, counter->hw.sample_period);
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003169 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003170
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02003171 return ret;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003172}
3173
3174static void perf_swcounter_overflow(struct perf_counter *counter,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003175 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003176{
Peter Zijlstradf1a1322009-06-10 21:02:22 +02003177 struct perf_sample_data data = {
Peter Zijlstra9e350de2009-06-10 21:34:59 +02003178 .regs = regs,
3179 .addr = addr,
3180 .period = counter->hw.last_period,
Peter Zijlstradf1a1322009-06-10 21:02:22 +02003181 };
3182
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003183 perf_swcounter_update(counter);
3184 perf_swcounter_set_period(counter);
Peter Zijlstradf1a1322009-06-10 21:02:22 +02003185 if (perf_counter_overflow(counter, nmi, &data))
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02003186 /* soft-disable the counter */
3187 ;
3188
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003189}
3190
Paul Mackerras880ca152009-06-01 17:49:14 +10003191static int perf_swcounter_is_counting(struct perf_counter *counter)
3192{
3193 struct perf_counter_context *ctx;
3194 unsigned long flags;
3195 int count;
3196
3197 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
3198 return 1;
3199
3200 if (counter->state != PERF_COUNTER_STATE_INACTIVE)
3201 return 0;
3202
3203 /*
3204 * If the counter is inactive, it could be just because
3205 * its task is scheduled out, or because it's in a group
3206 * which could not go on the PMU. We want to count in
3207 * the first case but not the second. If the context is
3208 * currently active then an inactive software counter must
3209 * be the second case. If it's not currently active then
3210 * we need to know whether the counter was active when the
3211 * context was last active, which we can determine by
3212 * comparing counter->tstamp_stopped with ctx->time.
3213 *
3214 * We are within an RCU read-side critical section,
3215 * which protects the existence of *ctx.
3216 */
3217 ctx = counter->ctx;
3218 spin_lock_irqsave(&ctx->lock, flags);
3219 count = 1;
3220 /* Re-check state now we have the lock */
3221 if (counter->state < PERF_COUNTER_STATE_INACTIVE ||
3222 counter->ctx->is_active ||
3223 counter->tstamp_stopped < ctx->time)
3224 count = 0;
3225 spin_unlock_irqrestore(&ctx->lock, flags);
3226 return count;
3227}
3228
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003229static int perf_swcounter_match(struct perf_counter *counter,
Peter Zijlstra1c432d82009-06-11 13:19:29 +02003230 enum perf_type_id type,
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003231 u32 event, struct pt_regs *regs)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003232{
Paul Mackerras880ca152009-06-01 17:49:14 +10003233 if (!perf_swcounter_is_counting(counter))
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003234 return 0;
3235
Ingo Molnara21ca2c2009-06-06 09:58:57 +02003236 if (counter->attr.type != type)
3237 return 0;
3238 if (counter->attr.config != event)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003239 return 0;
3240
Paul Mackerras3f731ca2009-06-01 17:52:30 +10003241 if (regs) {
Peter Zijlstra0d486962009-06-02 19:22:16 +02003242 if (counter->attr.exclude_user && user_mode(regs))
Paul Mackerras3f731ca2009-06-01 17:52:30 +10003243 return 0;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003244
Peter Zijlstra0d486962009-06-02 19:22:16 +02003245 if (counter->attr.exclude_kernel && !user_mode(regs))
Paul Mackerras3f731ca2009-06-01 17:52:30 +10003246 return 0;
3247 }
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003248
3249 return 1;
3250}
3251
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003252static void perf_swcounter_add(struct perf_counter *counter, u64 nr,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003253 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003254{
3255 int neg = atomic64_add_negative(nr, &counter->hw.count);
Ingo Molnar22a4f652009-06-01 10:13:37 +02003256
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003257 if (counter->hw.sample_period && !neg && regs)
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003258 perf_swcounter_overflow(counter, nmi, regs, addr);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003259}
3260
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003261static void perf_swcounter_ctx_event(struct perf_counter_context *ctx,
Peter Zijlstra1c432d82009-06-11 13:19:29 +02003262 enum perf_type_id type, u32 event,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003263 u64 nr, int nmi, struct pt_regs *regs,
3264 u64 addr)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003265{
3266 struct perf_counter *counter;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003267
Peter Zijlstra01ef09d2009-03-19 20:26:11 +01003268 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003269 return;
3270
Peter Zijlstra592903c2009-03-13 12:21:36 +01003271 rcu_read_lock();
3272 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003273 if (perf_swcounter_match(counter, type, event, regs))
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003274 perf_swcounter_add(counter, nr, nmi, regs, addr);
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003275 }
Peter Zijlstra592903c2009-03-13 12:21:36 +01003276 rcu_read_unlock();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003277}
3278
Peter Zijlstra96f6d442009-03-23 18:22:07 +01003279static int *perf_swcounter_recursion_context(struct perf_cpu_context *cpuctx)
3280{
3281 if (in_nmi())
3282 return &cpuctx->recursion[3];
3283
3284 if (in_irq())
3285 return &cpuctx->recursion[2];
3286
3287 if (in_softirq())
3288 return &cpuctx->recursion[1];
3289
3290 return &cpuctx->recursion[0];
3291}
3292
Peter Zijlstra1c432d82009-06-11 13:19:29 +02003293static void __perf_swcounter_event(enum perf_type_id type, u32 event,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003294 u64 nr, int nmi, struct pt_regs *regs,
3295 u64 addr)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003296{
3297 struct perf_cpu_context *cpuctx = &get_cpu_var(perf_cpu_context);
Peter Zijlstra96f6d442009-03-23 18:22:07 +01003298 int *recursion = perf_swcounter_recursion_context(cpuctx);
Peter Zijlstra665c2142009-05-29 14:51:57 +02003299 struct perf_counter_context *ctx;
Peter Zijlstra96f6d442009-03-23 18:22:07 +01003300
3301 if (*recursion)
3302 goto out;
3303
3304 (*recursion)++;
3305 barrier();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003306
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003307 perf_swcounter_ctx_event(&cpuctx->ctx, type, event,
3308 nr, nmi, regs, addr);
Peter Zijlstra665c2142009-05-29 14:51:57 +02003309 rcu_read_lock();
3310 /*
3311 * doesn't really matter which of the child contexts the
3312 * events ends up in.
3313 */
3314 ctx = rcu_dereference(current->perf_counter_ctxp);
3315 if (ctx)
3316 perf_swcounter_ctx_event(ctx, type, event, nr, nmi, regs, addr);
3317 rcu_read_unlock();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003318
Peter Zijlstra96f6d442009-03-23 18:22:07 +01003319 barrier();
3320 (*recursion)--;
3321
3322out:
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003323 put_cpu_var(perf_cpu_context);
3324}
3325
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003326void
3327perf_swcounter_event(u32 event, u64 nr, int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003328{
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003329 __perf_swcounter_event(PERF_TYPE_SOFTWARE, event, nr, nmi, regs, addr);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003330}
3331
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003332static void perf_swcounter_read(struct perf_counter *counter)
3333{
3334 perf_swcounter_update(counter);
3335}
3336
3337static int perf_swcounter_enable(struct perf_counter *counter)
3338{
3339 perf_swcounter_set_period(counter);
3340 return 0;
3341}
3342
3343static void perf_swcounter_disable(struct perf_counter *counter)
3344{
3345 perf_swcounter_update(counter);
3346}
3347
Robert Richter4aeb0b42009-04-29 12:47:03 +02003348static const struct pmu perf_ops_generic = {
Peter Zijlstraac17dc82009-03-13 12:21:34 +01003349 .enable = perf_swcounter_enable,
3350 .disable = perf_swcounter_disable,
3351 .read = perf_swcounter_read,
3352};
3353
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003354/*
3355 * Software counter: cpu wall time clock
3356 */
3357
Paul Mackerras9abf8a02009-01-09 16:26:43 +11003358static void cpu_clock_perf_counter_update(struct perf_counter *counter)
3359{
3360 int cpu = raw_smp_processor_id();
3361 s64 prev;
3362 u64 now;
3363
3364 now = cpu_clock(cpu);
3365 prev = atomic64_read(&counter->hw.prev_count);
3366 atomic64_set(&counter->hw.prev_count, now);
3367 atomic64_add(now - prev, &counter->count);
3368}
3369
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003370static int cpu_clock_perf_counter_enable(struct perf_counter *counter)
3371{
3372 struct hw_perf_counter *hwc = &counter->hw;
3373 int cpu = raw_smp_processor_id();
3374
3375 atomic64_set(&hwc->prev_count, cpu_clock(cpu));
Peter Zijlstra039fc912009-03-13 16:43:47 +01003376 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
3377 hwc->hrtimer.function = perf_swcounter_hrtimer;
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003378 if (hwc->sample_period) {
3379 u64 period = max_t(u64, 10000, hwc->sample_period);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003380 __hrtimer_start_range_ns(&hwc->hrtimer,
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003381 ns_to_ktime(period), 0,
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003382 HRTIMER_MODE_REL, 0);
3383 }
3384
3385 return 0;
3386}
3387
Ingo Molnar5c92d122008-12-11 13:21:10 +01003388static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
3389{
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003390 if (counter->hw.sample_period)
Peter Zijlstrab986d7e2009-05-20 12:21:21 +02003391 hrtimer_cancel(&counter->hw.hrtimer);
Paul Mackerras9abf8a02009-01-09 16:26:43 +11003392 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01003393}
3394
3395static void cpu_clock_perf_counter_read(struct perf_counter *counter)
3396{
Paul Mackerras9abf8a02009-01-09 16:26:43 +11003397 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01003398}
3399
Robert Richter4aeb0b42009-04-29 12:47:03 +02003400static const struct pmu perf_ops_cpu_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +01003401 .enable = cpu_clock_perf_counter_enable,
3402 .disable = cpu_clock_perf_counter_disable,
3403 .read = cpu_clock_perf_counter_read,
Ingo Molnar5c92d122008-12-11 13:21:10 +01003404};
3405
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01003406/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003407 * Software counter: task time clock
3408 */
3409
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02003410static void task_clock_perf_counter_update(struct perf_counter *counter, u64 now)
Ingo Molnarbae43c92008-12-11 14:03:20 +01003411{
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02003412 u64 prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003413 s64 delta;
Ingo Molnarbae43c92008-12-11 14:03:20 +01003414
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02003415 prev = atomic64_xchg(&counter->hw.prev_count, now);
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003416 delta = now - prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003417 atomic64_add(delta, &counter->count);
Ingo Molnarbae43c92008-12-11 14:03:20 +01003418}
3419
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01003420static int task_clock_perf_counter_enable(struct perf_counter *counter)
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003421{
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003422 struct hw_perf_counter *hwc = &counter->hw;
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02003423 u64 now;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003424
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02003425 now = counter->ctx->time;
3426
3427 atomic64_set(&hwc->prev_count, now);
Peter Zijlstra039fc912009-03-13 16:43:47 +01003428 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
3429 hwc->hrtimer.function = perf_swcounter_hrtimer;
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003430 if (hwc->sample_period) {
3431 u64 period = max_t(u64, 10000, hwc->sample_period);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003432 __hrtimer_start_range_ns(&hwc->hrtimer,
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003433 ns_to_ktime(period), 0,
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003434 HRTIMER_MODE_REL, 0);
3435 }
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01003436
3437 return 0;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003438}
3439
3440static void task_clock_perf_counter_disable(struct perf_counter *counter)
3441{
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003442 if (counter->hw.sample_period)
Peter Zijlstrab986d7e2009-05-20 12:21:21 +02003443 hrtimer_cancel(&counter->hw.hrtimer);
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02003444 task_clock_perf_counter_update(counter, counter->ctx->time);
3445
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003446}
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01003447
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003448static void task_clock_perf_counter_read(struct perf_counter *counter)
3449{
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02003450 u64 time;
3451
3452 if (!in_nmi()) {
3453 update_context_time(counter->ctx);
3454 time = counter->ctx->time;
3455 } else {
3456 u64 now = perf_clock();
3457 u64 delta = now - counter->ctx->timestamp;
3458 time = counter->ctx->time + delta;
3459 }
3460
3461 task_clock_perf_counter_update(counter, time);
Ingo Molnarbae43c92008-12-11 14:03:20 +01003462}
3463
Robert Richter4aeb0b42009-04-29 12:47:03 +02003464static const struct pmu perf_ops_task_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +01003465 .enable = task_clock_perf_counter_enable,
3466 .disable = task_clock_perf_counter_disable,
3467 .read = task_clock_perf_counter_read,
Ingo Molnarbae43c92008-12-11 14:03:20 +01003468};
3469
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003470/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003471 * Software counter: cpu migrations
3472 */
Paul Mackerras3f731ca2009-06-01 17:52:30 +10003473void perf_counter_task_migration(struct task_struct *task, int cpu)
Ingo Molnar6c594c22008-12-14 12:34:15 +01003474{
Paul Mackerras3f731ca2009-06-01 17:52:30 +10003475 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
3476 struct perf_counter_context *ctx;
Paul Mackerras23a185c2009-02-09 22:42:47 +11003477
Paul Mackerras3f731ca2009-06-01 17:52:30 +10003478 perf_swcounter_ctx_event(&cpuctx->ctx, PERF_TYPE_SOFTWARE,
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +02003479 PERF_COUNT_SW_CPU_MIGRATIONS,
Paul Mackerras3f731ca2009-06-01 17:52:30 +10003480 1, 1, NULL, 0);
3481
3482 ctx = perf_pin_task_context(task);
3483 if (ctx) {
3484 perf_swcounter_ctx_event(ctx, PERF_TYPE_SOFTWARE,
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +02003485 PERF_COUNT_SW_CPU_MIGRATIONS,
Paul Mackerras3f731ca2009-06-01 17:52:30 +10003486 1, 1, NULL, 0);
3487 perf_unpin_context(ctx);
3488 }
Ingo Molnar6c594c22008-12-14 12:34:15 +01003489}
3490
Peter Zijlstrae077df42009-03-19 20:26:17 +01003491#ifdef CONFIG_EVENT_PROFILE
3492void perf_tpcounter_event(int event_id)
3493{
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003494 struct pt_regs *regs = get_irq_regs();
3495
3496 if (!regs)
3497 regs = task_pt_regs(current);
3498
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003499 __perf_swcounter_event(PERF_TYPE_TRACEPOINT, event_id, 1, 1, regs, 0);
Peter Zijlstrae077df42009-03-19 20:26:17 +01003500}
Steven Whitehouseff7b1b42009-04-15 16:55:05 +01003501EXPORT_SYMBOL_GPL(perf_tpcounter_event);
Peter Zijlstrae077df42009-03-19 20:26:17 +01003502
3503extern int ftrace_profile_enable(int);
3504extern void ftrace_profile_disable(int);
3505
3506static void tp_perf_counter_destroy(struct perf_counter *counter)
3507{
Peter Zijlstra0d486962009-06-02 19:22:16 +02003508 ftrace_profile_disable(perf_event_id(&counter->attr));
Peter Zijlstrae077df42009-03-19 20:26:17 +01003509}
3510
Robert Richter4aeb0b42009-04-29 12:47:03 +02003511static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
Peter Zijlstrae077df42009-03-19 20:26:17 +01003512{
Peter Zijlstra0d486962009-06-02 19:22:16 +02003513 int event_id = perf_event_id(&counter->attr);
Peter Zijlstrae077df42009-03-19 20:26:17 +01003514 int ret;
3515
3516 ret = ftrace_profile_enable(event_id);
3517 if (ret)
3518 return NULL;
3519
3520 counter->destroy = tp_perf_counter_destroy;
3521
3522 return &perf_ops_generic;
3523}
3524#else
Robert Richter4aeb0b42009-04-29 12:47:03 +02003525static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
Peter Zijlstrae077df42009-03-19 20:26:17 +01003526{
3527 return NULL;
3528}
3529#endif
3530
Robert Richter4aeb0b42009-04-29 12:47:03 +02003531static const struct pmu *sw_perf_counter_init(struct perf_counter *counter)
Ingo Molnar5c92d122008-12-11 13:21:10 +01003532{
Robert Richter4aeb0b42009-04-29 12:47:03 +02003533 const struct pmu *pmu = NULL;
Ingo Molnar5c92d122008-12-11 13:21:10 +01003534
Paul Mackerras0475f9e2009-02-11 14:35:35 +11003535 /*
3536 * Software counters (currently) can't in general distinguish
3537 * between user, kernel and hypervisor events.
3538 * However, context switches and cpu migrations are considered
3539 * to be kernel events, and page faults are never hypervisor
3540 * events.
3541 */
Ingo Molnara21ca2c2009-06-06 09:58:57 +02003542 switch (counter->attr.config) {
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +02003543 case PERF_COUNT_SW_CPU_CLOCK:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003544 pmu = &perf_ops_cpu_clock;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003545
Ingo Molnar5c92d122008-12-11 13:21:10 +01003546 break;
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +02003547 case PERF_COUNT_SW_TASK_CLOCK:
Paul Mackerras23a185c2009-02-09 22:42:47 +11003548 /*
3549 * If the user instantiates this as a per-cpu counter,
3550 * use the cpu_clock counter instead.
3551 */
3552 if (counter->ctx->task)
Robert Richter4aeb0b42009-04-29 12:47:03 +02003553 pmu = &perf_ops_task_clock;
Paul Mackerras23a185c2009-02-09 22:42:47 +11003554 else
Robert Richter4aeb0b42009-04-29 12:47:03 +02003555 pmu = &perf_ops_cpu_clock;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003556
Ingo Molnarbae43c92008-12-11 14:03:20 +01003557 break;
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +02003558 case PERF_COUNT_SW_PAGE_FAULTS:
3559 case PERF_COUNT_SW_PAGE_FAULTS_MIN:
3560 case PERF_COUNT_SW_PAGE_FAULTS_MAJ:
3561 case PERF_COUNT_SW_CONTEXT_SWITCHES:
3562 case PERF_COUNT_SW_CPU_MIGRATIONS:
Paul Mackerras3f731ca2009-06-01 17:52:30 +10003563 pmu = &perf_ops_generic;
Ingo Molnar6c594c22008-12-14 12:34:15 +01003564 break;
Ingo Molnar5c92d122008-12-11 13:21:10 +01003565 }
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003566
Robert Richter4aeb0b42009-04-29 12:47:03 +02003567 return pmu;
Ingo Molnar5c92d122008-12-11 13:21:10 +01003568}
3569
Thomas Gleixner0793a612008-12-04 20:12:29 +01003570/*
3571 * Allocate and initialize a counter structure
3572 */
3573static struct perf_counter *
Peter Zijlstra0d486962009-06-02 19:22:16 +02003574perf_counter_alloc(struct perf_counter_attr *attr,
Ingo Molnar04289bb2008-12-11 08:38:42 +01003575 int cpu,
Paul Mackerras23a185c2009-02-09 22:42:47 +11003576 struct perf_counter_context *ctx,
Ingo Molnar9b51f662008-12-12 13:49:45 +01003577 struct perf_counter *group_leader,
3578 gfp_t gfpflags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003579{
Robert Richter4aeb0b42009-04-29 12:47:03 +02003580 const struct pmu *pmu;
Ingo Molnar621a01e2008-12-11 12:46:46 +01003581 struct perf_counter *counter;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003582 struct hw_perf_counter *hwc;
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003583 long err;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003584
Ingo Molnar9b51f662008-12-12 13:49:45 +01003585 counter = kzalloc(sizeof(*counter), gfpflags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003586 if (!counter)
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003587 return ERR_PTR(-ENOMEM);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003588
Ingo Molnar04289bb2008-12-11 08:38:42 +01003589 /*
3590 * Single counters are their own group leaders, with an
3591 * empty sibling list:
3592 */
3593 if (!group_leader)
3594 group_leader = counter;
3595
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003596 mutex_init(&counter->child_mutex);
3597 INIT_LIST_HEAD(&counter->child_list);
3598
Ingo Molnar04289bb2008-12-11 08:38:42 +01003599 INIT_LIST_HEAD(&counter->list_entry);
Peter Zijlstra592903c2009-03-13 12:21:36 +01003600 INIT_LIST_HEAD(&counter->event_entry);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003601 INIT_LIST_HEAD(&counter->sibling_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003602 init_waitqueue_head(&counter->waitq);
3603
Peter Zijlstra7b732a72009-03-23 18:22:10 +01003604 mutex_init(&counter->mmap_mutex);
3605
Peter Zijlstraa96bbc12009-06-03 14:01:36 +02003606 counter->cpu = cpu;
Peter Zijlstra0d486962009-06-02 19:22:16 +02003607 counter->attr = *attr;
Peter Zijlstraa96bbc12009-06-03 14:01:36 +02003608 counter->group_leader = group_leader;
3609 counter->pmu = NULL;
3610 counter->ctx = ctx;
3611 counter->oncpu = -1;
Ingo Molnar329d8762009-05-26 08:10:00 +02003612
Peter Zijlstraa96bbc12009-06-03 14:01:36 +02003613 counter->ns = get_pid_ns(current->nsproxy->pid_ns);
3614 counter->id = atomic64_inc_return(&perf_counter_id);
3615
3616 counter->state = PERF_COUNTER_STATE_INACTIVE;
3617
Peter Zijlstra0d486962009-06-02 19:22:16 +02003618 if (attr->disabled)
Ingo Molnara86ed502008-12-17 00:43:10 +01003619 counter->state = PERF_COUNTER_STATE_OFF;
3620
Robert Richter4aeb0b42009-04-29 12:47:03 +02003621 pmu = NULL;
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003622
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003623 hwc = &counter->hw;
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02003624 hwc->sample_period = attr->sample_period;
Peter Zijlstra0d486962009-06-02 19:22:16 +02003625 if (attr->freq && attr->sample_freq)
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02003626 hwc->sample_period = 1;
3627
3628 atomic64_set(&hwc->period_left, hwc->sample_period);
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003629
Peter Zijlstra2023b352009-05-05 17:50:26 +02003630 /*
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003631 * we currently do not support PERF_SAMPLE_GROUP on inherited counters
Peter Zijlstra2023b352009-05-05 17:50:26 +02003632 */
Peter Zijlstra0d486962009-06-02 19:22:16 +02003633 if (attr->inherit && (attr->sample_type & PERF_SAMPLE_GROUP))
Peter Zijlstra2023b352009-05-05 17:50:26 +02003634 goto done;
3635
Ingo Molnara21ca2c2009-06-06 09:58:57 +02003636 switch (attr->type) {
Peter Zijlstra081fad82009-06-11 17:57:21 +02003637 case PERF_TYPE_RAW:
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003638 case PERF_TYPE_HARDWARE:
Ingo Molnar8326f442009-06-05 20:22:46 +02003639 case PERF_TYPE_HW_CACHE:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003640 pmu = hw_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003641 break;
3642
3643 case PERF_TYPE_SOFTWARE:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003644 pmu = sw_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003645 break;
3646
3647 case PERF_TYPE_TRACEPOINT:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003648 pmu = tp_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003649 break;
Peter Zijlstra974802e2009-06-12 12:46:55 +02003650
3651 default:
3652 break;
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003653 }
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01003654done:
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003655 err = 0;
Robert Richter4aeb0b42009-04-29 12:47:03 +02003656 if (!pmu)
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003657 err = -EINVAL;
Robert Richter4aeb0b42009-04-29 12:47:03 +02003658 else if (IS_ERR(pmu))
3659 err = PTR_ERR(pmu);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003660
3661 if (err) {
Peter Zijlstraa96bbc12009-06-03 14:01:36 +02003662 if (counter->ns)
3663 put_pid_ns(counter->ns);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003664 kfree(counter);
3665 return ERR_PTR(err);
3666 }
3667
Robert Richter4aeb0b42009-04-29 12:47:03 +02003668 counter->pmu = pmu;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003669
Peter Zijlstra7fc23a52009-05-08 18:52:21 +02003670 atomic_inc(&nr_counters);
Peter Zijlstra0d486962009-06-02 19:22:16 +02003671 if (counter->attr.mmap)
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003672 atomic_inc(&nr_mmap_counters);
Peter Zijlstra0d486962009-06-02 19:22:16 +02003673 if (counter->attr.comm)
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003674 atomic_inc(&nr_comm_counters);
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02003675
Thomas Gleixner0793a612008-12-04 20:12:29 +01003676 return counter;
3677}
3678
Peter Zijlstra974802e2009-06-12 12:46:55 +02003679static int perf_copy_attr(struct perf_counter_attr __user *uattr,
3680 struct perf_counter_attr *attr)
3681{
3682 int ret;
3683 u32 size;
3684
3685 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
3686 return -EFAULT;
3687
3688 /*
3689 * zero the full structure, so that a short copy will be nice.
3690 */
3691 memset(attr, 0, sizeof(*attr));
3692
3693 ret = get_user(size, &uattr->size);
3694 if (ret)
3695 return ret;
3696
3697 if (size > PAGE_SIZE) /* silly large */
3698 goto err_size;
3699
3700 if (!size) /* abi compat */
3701 size = PERF_ATTR_SIZE_VER0;
3702
3703 if (size < PERF_ATTR_SIZE_VER0)
3704 goto err_size;
3705
3706 /*
3707 * If we're handed a bigger struct than we know of,
3708 * ensure all the unknown bits are 0.
3709 */
3710 if (size > sizeof(*attr)) {
3711 unsigned long val;
3712 unsigned long __user *addr;
3713 unsigned long __user *end;
3714
3715 addr = PTR_ALIGN((void __user *)uattr + sizeof(*attr),
3716 sizeof(unsigned long));
3717 end = PTR_ALIGN((void __user *)uattr + size,
3718 sizeof(unsigned long));
3719
3720 for (; addr < end; addr += sizeof(unsigned long)) {
3721 ret = get_user(val, addr);
3722 if (ret)
3723 return ret;
3724 if (val)
3725 goto err_size;
3726 }
3727 }
3728
3729 ret = copy_from_user(attr, uattr, size);
3730 if (ret)
3731 return -EFAULT;
3732
3733 /*
3734 * If the type exists, the corresponding creation will verify
3735 * the attr->config.
3736 */
3737 if (attr->type >= PERF_TYPE_MAX)
3738 return -EINVAL;
3739
3740 if (attr->__reserved_1 || attr->__reserved_2 || attr->__reserved_3)
3741 return -EINVAL;
3742
3743 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
3744 return -EINVAL;
3745
3746 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
3747 return -EINVAL;
3748
3749out:
3750 return ret;
3751
3752err_size:
3753 put_user(sizeof(*attr), &uattr->size);
3754 ret = -E2BIG;
3755 goto out;
3756}
3757
Thomas Gleixner0793a612008-12-04 20:12:29 +01003758/**
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003759 * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
Ingo Molnar9f66a382008-12-10 12:33:23 +01003760 *
Peter Zijlstra0d486962009-06-02 19:22:16 +02003761 * @attr_uptr: event type attributes for monitoring/sampling
Thomas Gleixner0793a612008-12-04 20:12:29 +01003762 * @pid: target pid
Ingo Molnar9f66a382008-12-10 12:33:23 +01003763 * @cpu: target cpu
3764 * @group_fd: group leader counter fd
Thomas Gleixner0793a612008-12-04 20:12:29 +01003765 */
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003766SYSCALL_DEFINE5(perf_counter_open,
Peter Zijlstra974802e2009-06-12 12:46:55 +02003767 struct perf_counter_attr __user *, attr_uptr,
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003768 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003769{
Ingo Molnar04289bb2008-12-11 08:38:42 +01003770 struct perf_counter *counter, *group_leader;
Peter Zijlstra0d486962009-06-02 19:22:16 +02003771 struct perf_counter_attr attr;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003772 struct perf_counter_context *ctx;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003773 struct file *counter_file = NULL;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003774 struct file *group_file = NULL;
3775 int fput_needed = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003776 int fput_needed2 = 0;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003777 int ret;
3778
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003779 /* for future expandability... */
3780 if (flags)
3781 return -EINVAL;
3782
Peter Zijlstra974802e2009-06-12 12:46:55 +02003783 ret = perf_copy_attr(attr_uptr, &attr);
3784 if (ret)
3785 return ret;
Thomas Gleixnereab656a2008-12-08 19:26:59 +01003786
Peter Zijlstra07647712009-06-11 11:18:36 +02003787 if (!attr.exclude_kernel) {
3788 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
3789 return -EACCES;
3790 }
3791
Peter Zijlstradf58ab22009-06-11 11:25:05 +02003792 if (attr.freq) {
3793 if (attr.sample_freq > sysctl_perf_counter_sample_rate)
3794 return -EINVAL;
3795 }
3796
Ingo Molnar04289bb2008-12-11 08:38:42 +01003797 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01003798 * Get the target context (task or percpu):
3799 */
3800 ctx = find_get_context(pid, cpu);
3801 if (IS_ERR(ctx))
3802 return PTR_ERR(ctx);
3803
3804 /*
3805 * Look up the group leader (we will attach this counter to it):
Ingo Molnar04289bb2008-12-11 08:38:42 +01003806 */
3807 group_leader = NULL;
3808 if (group_fd != -1) {
3809 ret = -EINVAL;
3810 group_file = fget_light(group_fd, &fput_needed);
3811 if (!group_file)
Ingo Molnarccff2862008-12-11 11:26:29 +01003812 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003813 if (group_file->f_op != &perf_fops)
Ingo Molnarccff2862008-12-11 11:26:29 +01003814 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003815
3816 group_leader = group_file->private_data;
3817 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01003818 * Do not allow a recursive hierarchy (this new sibling
3819 * becoming part of another group-sibling):
Ingo Molnar04289bb2008-12-11 08:38:42 +01003820 */
Ingo Molnarccff2862008-12-11 11:26:29 +01003821 if (group_leader->group_leader != group_leader)
3822 goto err_put_context;
3823 /*
3824 * Do not allow to attach to a group in a different
3825 * task or CPU context:
3826 */
3827 if (group_leader->ctx != ctx)
3828 goto err_put_context;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11003829 /*
3830 * Only a group leader can be exclusive or pinned
3831 */
Peter Zijlstra0d486962009-06-02 19:22:16 +02003832 if (attr.exclusive || attr.pinned)
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11003833 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003834 }
3835
Peter Zijlstra0d486962009-06-02 19:22:16 +02003836 counter = perf_counter_alloc(&attr, cpu, ctx, group_leader,
Paul Mackerras23a185c2009-02-09 22:42:47 +11003837 GFP_KERNEL);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003838 ret = PTR_ERR(counter);
3839 if (IS_ERR(counter))
Thomas Gleixner0793a612008-12-04 20:12:29 +01003840 goto err_put_context;
3841
Thomas Gleixner0793a612008-12-04 20:12:29 +01003842 ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
3843 if (ret < 0)
Ingo Molnar9b51f662008-12-12 13:49:45 +01003844 goto err_free_put_context;
3845
3846 counter_file = fget_light(ret, &fput_needed2);
3847 if (!counter_file)
3848 goto err_free_put_context;
3849
3850 counter->filp = counter_file;
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003851 WARN_ON_ONCE(ctx->parent_ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003852 mutex_lock(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003853 perf_install_in_context(ctx, counter, cpu);
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003854 ++ctx->generation;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003855 mutex_unlock(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003856
Peter Zijlstra082ff5a2009-05-23 18:29:00 +02003857 counter->owner = current;
3858 get_task_struct(current);
3859 mutex_lock(&current->perf_counter_mutex);
3860 list_add_tail(&counter->owner_entry, &current->perf_counter_list);
3861 mutex_unlock(&current->perf_counter_mutex);
3862
Ingo Molnar9b51f662008-12-12 13:49:45 +01003863 fput_light(counter_file, fput_needed2);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003864
Ingo Molnar04289bb2008-12-11 08:38:42 +01003865out_fput:
3866 fput_light(group_file, fput_needed);
3867
Thomas Gleixner0793a612008-12-04 20:12:29 +01003868 return ret;
3869
Ingo Molnar9b51f662008-12-12 13:49:45 +01003870err_free_put_context:
Thomas Gleixner0793a612008-12-04 20:12:29 +01003871 kfree(counter);
3872
3873err_put_context:
Paul Mackerrasc93f7662009-05-28 22:18:17 +10003874 put_ctx(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003875
Ingo Molnar04289bb2008-12-11 08:38:42 +01003876 goto out_fput;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003877}
3878
Ingo Molnar9b51f662008-12-12 13:49:45 +01003879/*
Ingo Molnar9b51f662008-12-12 13:49:45 +01003880 * inherit a counter from parent task to child task:
3881 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11003882static struct perf_counter *
Ingo Molnar9b51f662008-12-12 13:49:45 +01003883inherit_counter(struct perf_counter *parent_counter,
3884 struct task_struct *parent,
3885 struct perf_counter_context *parent_ctx,
3886 struct task_struct *child,
Paul Mackerrasd859e292009-01-17 18:10:22 +11003887 struct perf_counter *group_leader,
Ingo Molnar9b51f662008-12-12 13:49:45 +01003888 struct perf_counter_context *child_ctx)
3889{
3890 struct perf_counter *child_counter;
3891
Paul Mackerrasd859e292009-01-17 18:10:22 +11003892 /*
3893 * Instead of creating recursive hierarchies of counters,
3894 * we link inherited counters back to the original parent,
3895 * which has a filp for sure, which we use as the reference
3896 * count:
3897 */
3898 if (parent_counter->parent)
3899 parent_counter = parent_counter->parent;
3900
Peter Zijlstra0d486962009-06-02 19:22:16 +02003901 child_counter = perf_counter_alloc(&parent_counter->attr,
Paul Mackerras23a185c2009-02-09 22:42:47 +11003902 parent_counter->cpu, child_ctx,
3903 group_leader, GFP_KERNEL);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003904 if (IS_ERR(child_counter))
3905 return child_counter;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10003906 get_ctx(child_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003907
3908 /*
Paul Mackerras564c2b22009-05-22 14:27:22 +10003909 * Make the child state follow the state of the parent counter,
Peter Zijlstra0d486962009-06-02 19:22:16 +02003910 * not its attr.disabled bit. We hold the parent's mutex,
Ingo Molnar22a4f652009-06-01 10:13:37 +02003911 * so we won't race with perf_counter_{en, dis}able_family.
Paul Mackerras564c2b22009-05-22 14:27:22 +10003912 */
3913 if (parent_counter->state >= PERF_COUNTER_STATE_INACTIVE)
3914 child_counter->state = PERF_COUNTER_STATE_INACTIVE;
3915 else
3916 child_counter->state = PERF_COUNTER_STATE_OFF;
3917
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02003918 if (parent_counter->attr.freq)
3919 child_counter->hw.sample_period = parent_counter->hw.sample_period;
3920
Paul Mackerras564c2b22009-05-22 14:27:22 +10003921 /*
Ingo Molnar9b51f662008-12-12 13:49:45 +01003922 * Link it up in the child's context:
3923 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +11003924 add_counter_to_ctx(child_counter, child_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003925
3926 child_counter->parent = parent_counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003927 /*
3928 * inherit into child's child as well:
3929 */
Peter Zijlstra0d486962009-06-02 19:22:16 +02003930 child_counter->attr.inherit = 1;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003931
3932 /*
3933 * Get a reference to the parent filp - we will fput it
3934 * when the child counter exits. This is safe to do because
3935 * we are in the parent and we know that the filp still
3936 * exists and has a nonzero count:
3937 */
3938 atomic_long_inc(&parent_counter->filp->f_count);
3939
Paul Mackerrasd859e292009-01-17 18:10:22 +11003940 /*
3941 * Link this into the parent counter's child list
3942 */
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003943 WARN_ON_ONCE(parent_counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003944 mutex_lock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003945 list_add_tail(&child_counter->child_list, &parent_counter->child_list);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003946 mutex_unlock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003947
3948 return child_counter;
3949}
3950
3951static int inherit_group(struct perf_counter *parent_counter,
3952 struct task_struct *parent,
3953 struct perf_counter_context *parent_ctx,
3954 struct task_struct *child,
3955 struct perf_counter_context *child_ctx)
3956{
3957 struct perf_counter *leader;
3958 struct perf_counter *sub;
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003959 struct perf_counter *child_ctr;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003960
3961 leader = inherit_counter(parent_counter, parent, parent_ctx,
3962 child, NULL, child_ctx);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003963 if (IS_ERR(leader))
3964 return PTR_ERR(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003965 list_for_each_entry(sub, &parent_counter->sibling_list, list_entry) {
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003966 child_ctr = inherit_counter(sub, parent, parent_ctx,
3967 child, leader, child_ctx);
3968 if (IS_ERR(child_ctr))
3969 return PTR_ERR(child_ctr);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003970 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01003971 return 0;
3972}
3973
Paul Mackerrasd859e292009-01-17 18:10:22 +11003974static void sync_child_counter(struct perf_counter *child_counter,
3975 struct perf_counter *parent_counter)
3976{
Peter Zijlstra8bc20952009-05-15 20:45:59 +02003977 u64 child_val;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003978
Paul Mackerrasd859e292009-01-17 18:10:22 +11003979 child_val = atomic64_read(&child_counter->count);
3980
3981 /*
3982 * Add back the child's count to the parent's count:
3983 */
3984 atomic64_add(child_val, &parent_counter->count);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11003985 atomic64_add(child_counter->total_time_enabled,
3986 &parent_counter->child_total_time_enabled);
3987 atomic64_add(child_counter->total_time_running,
3988 &parent_counter->child_total_time_running);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003989
3990 /*
3991 * Remove this counter from the parent's list
3992 */
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003993 WARN_ON_ONCE(parent_counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003994 mutex_lock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003995 list_del_init(&child_counter->child_list);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003996 mutex_unlock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003997
3998 /*
3999 * Release the parent counter, if this was the last
4000 * reference to it.
4001 */
4002 fput(parent_counter->filp);
4003}
4004
Ingo Molnar9b51f662008-12-12 13:49:45 +01004005static void
Peter Zijlstrabbbee902009-05-29 14:25:58 +02004006__perf_counter_exit_task(struct perf_counter *child_counter,
Ingo Molnar9b51f662008-12-12 13:49:45 +01004007 struct perf_counter_context *child_ctx)
4008{
4009 struct perf_counter *parent_counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004010
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004011 update_counter_times(child_counter);
Peter Zijlstraaa9c67f2009-05-23 18:28:59 +02004012 perf_counter_remove_from_context(child_counter);
Ingo Molnar0cc0c022008-12-14 23:20:36 +01004013
Ingo Molnar9b51f662008-12-12 13:49:45 +01004014 parent_counter = child_counter->parent;
4015 /*
4016 * It can happen that parent exits first, and has counters
4017 * that are still around due to the child reference. These
4018 * counters need to be zapped - but otherwise linger.
4019 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11004020 if (parent_counter) {
4021 sync_child_counter(child_counter, parent_counter);
Peter Zijlstraf1600952009-03-19 20:26:16 +01004022 free_counter(child_counter);
Paul Mackerras4bcf3492009-02-11 13:53:19 +01004023 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01004024}
4025
4026/*
Paul Mackerrasd859e292009-01-17 18:10:22 +11004027 * When a child task exits, feed back counter values to parent counters.
Ingo Molnar9b51f662008-12-12 13:49:45 +01004028 */
4029void perf_counter_exit_task(struct task_struct *child)
4030{
4031 struct perf_counter *child_counter, *tmp;
4032 struct perf_counter_context *child_ctx;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004033 unsigned long flags;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004034
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004035 if (likely(!child->perf_counter_ctxp))
Ingo Molnar9b51f662008-12-12 13:49:45 +01004036 return;
4037
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004038 local_irq_save(flags);
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004039 /*
4040 * We can't reschedule here because interrupts are disabled,
4041 * and either child is current or it is a task that can't be
4042 * scheduled, so we are now safe from rescheduling changing
4043 * our context.
4044 */
4045 child_ctx = child->perf_counter_ctxp;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004046 __perf_counter_task_sched_out(child_ctx);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10004047
4048 /*
4049 * Take the context lock here so that if find_get_context is
4050 * reading child->perf_counter_ctxp, we wait until it has
4051 * incremented the context's refcount before we do put_ctx below.
4052 */
4053 spin_lock(&child_ctx->lock);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004054 child->perf_counter_ctxp = NULL;
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004055 if (child_ctx->parent_ctx) {
4056 /*
4057 * This context is a clone; unclone it so it can't get
4058 * swapped to another process while we're removing all
4059 * the counters from it.
4060 */
4061 put_ctx(child_ctx->parent_ctx);
4062 child_ctx->parent_ctx = NULL;
4063 }
Paul Mackerrasc93f7662009-05-28 22:18:17 +10004064 spin_unlock(&child_ctx->lock);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004065 local_irq_restore(flags);
4066
Peter Zijlstra66fff222009-06-10 22:53:37 +02004067 /*
4068 * We can recurse on the same lock type through:
4069 *
4070 * __perf_counter_exit_task()
4071 * sync_child_counter()
4072 * fput(parent_counter->filp)
4073 * perf_release()
4074 * mutex_lock(&ctx->mutex)
4075 *
4076 * But since its the parent context it won't be the same instance.
4077 */
4078 mutex_lock_nested(&child_ctx->mutex, SINGLE_DEPTH_NESTING);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004079
Peter Zijlstra8bc20952009-05-15 20:45:59 +02004080again:
Ingo Molnar9b51f662008-12-12 13:49:45 +01004081 list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list,
4082 list_entry)
Peter Zijlstrabbbee902009-05-29 14:25:58 +02004083 __perf_counter_exit_task(child_counter, child_ctx);
Peter Zijlstra8bc20952009-05-15 20:45:59 +02004084
4085 /*
4086 * If the last counter was a group counter, it will have appended all
4087 * its siblings to the list, but we obtained 'tmp' before that which
4088 * will still point to the list head terminating the iteration.
4089 */
4090 if (!list_empty(&child_ctx->counter_list))
4091 goto again;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004092
4093 mutex_unlock(&child_ctx->mutex);
4094
4095 put_ctx(child_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01004096}
4097
4098/*
Peter Zijlstrabbbee902009-05-29 14:25:58 +02004099 * free an unexposed, unused context as created by inheritance by
4100 * init_task below, used by fork() in case of fail.
4101 */
4102void perf_counter_free_task(struct task_struct *task)
4103{
4104 struct perf_counter_context *ctx = task->perf_counter_ctxp;
4105 struct perf_counter *counter, *tmp;
4106
4107 if (!ctx)
4108 return;
4109
4110 mutex_lock(&ctx->mutex);
4111again:
4112 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry) {
4113 struct perf_counter *parent = counter->parent;
4114
4115 if (WARN_ON_ONCE(!parent))
4116 continue;
4117
4118 mutex_lock(&parent->child_mutex);
4119 list_del_init(&counter->child_list);
4120 mutex_unlock(&parent->child_mutex);
4121
4122 fput(parent->filp);
4123
4124 list_del_counter(counter, ctx);
4125 free_counter(counter);
4126 }
4127
4128 if (!list_empty(&ctx->counter_list))
4129 goto again;
4130
4131 mutex_unlock(&ctx->mutex);
4132
4133 put_ctx(ctx);
4134}
4135
4136/*
Ingo Molnar9b51f662008-12-12 13:49:45 +01004137 * Initialize the perf_counter context in task_struct
4138 */
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02004139int perf_counter_init_task(struct task_struct *child)
Ingo Molnar9b51f662008-12-12 13:49:45 +01004140{
4141 struct perf_counter_context *child_ctx, *parent_ctx;
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004142 struct perf_counter_context *cloned_ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +11004143 struct perf_counter *counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004144 struct task_struct *parent = current;
Paul Mackerras564c2b22009-05-22 14:27:22 +10004145 int inherited_all = 1;
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02004146 int ret = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004147
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004148 child->perf_counter_ctxp = NULL;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004149
Peter Zijlstra082ff5a2009-05-23 18:29:00 +02004150 mutex_init(&child->perf_counter_mutex);
4151 INIT_LIST_HEAD(&child->perf_counter_list);
4152
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004153 if (likely(!parent->perf_counter_ctxp))
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02004154 return 0;
4155
Ingo Molnar9b51f662008-12-12 13:49:45 +01004156 /*
4157 * This is executed from the parent task context, so inherit
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004158 * counters that have been marked for cloning.
4159 * First allocate and initialize a context for the child.
Ingo Molnar9b51f662008-12-12 13:49:45 +01004160 */
4161
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004162 child_ctx = kmalloc(sizeof(struct perf_counter_context), GFP_KERNEL);
4163 if (!child_ctx)
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02004164 return -ENOMEM;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004165
4166 __perf_counter_init_context(child_ctx, child);
4167 child->perf_counter_ctxp = child_ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10004168 get_task_struct(child);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004169
Ingo Molnar9b51f662008-12-12 13:49:45 +01004170 /*
Paul Mackerras25346b932009-06-01 17:48:12 +10004171 * If the parent's context is a clone, pin it so it won't get
4172 * swapped under us.
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004173 */
Paul Mackerras25346b932009-06-01 17:48:12 +10004174 parent_ctx = perf_pin_task_context(parent);
4175
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004176 /*
4177 * No need to check if parent_ctx != NULL here; since we saw
4178 * it non-NULL earlier, the only reason for it to become NULL
4179 * is if we exit, and since we're currently in the middle of
4180 * a fork we can't be exiting at the same time.
4181 */
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004182
4183 /*
Ingo Molnar9b51f662008-12-12 13:49:45 +01004184 * Lock the parent list. No need to lock the child - not PID
4185 * hashed yet and not running, so nobody can access it.
4186 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11004187 mutex_lock(&parent_ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01004188
4189 /*
4190 * We dont have to disable NMIs - we are only looking at
4191 * the list, not manipulating it:
4192 */
Peter Zijlstrad7b629a2009-05-20 12:21:19 +02004193 list_for_each_entry_rcu(counter, &parent_ctx->event_list, event_entry) {
4194 if (counter != counter->group_leader)
4195 continue;
4196
Peter Zijlstra0d486962009-06-02 19:22:16 +02004197 if (!counter->attr.inherit) {
Paul Mackerras564c2b22009-05-22 14:27:22 +10004198 inherited_all = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004199 continue;
Paul Mackerras564c2b22009-05-22 14:27:22 +10004200 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01004201
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02004202 ret = inherit_group(counter, parent, parent_ctx,
4203 child, child_ctx);
4204 if (ret) {
Paul Mackerras564c2b22009-05-22 14:27:22 +10004205 inherited_all = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004206 break;
Paul Mackerras564c2b22009-05-22 14:27:22 +10004207 }
4208 }
4209
4210 if (inherited_all) {
4211 /*
4212 * Mark the child context as a clone of the parent
4213 * context, or of whatever the parent is a clone of.
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004214 * Note that if the parent is a clone, it could get
4215 * uncloned at any point, but that doesn't matter
4216 * because the list of counters and the generation
4217 * count can't have changed since we took the mutex.
Paul Mackerras564c2b22009-05-22 14:27:22 +10004218 */
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004219 cloned_ctx = rcu_dereference(parent_ctx->parent_ctx);
4220 if (cloned_ctx) {
4221 child_ctx->parent_ctx = cloned_ctx;
Paul Mackerras25346b932009-06-01 17:48:12 +10004222 child_ctx->parent_gen = parent_ctx->parent_gen;
Paul Mackerras564c2b22009-05-22 14:27:22 +10004223 } else {
4224 child_ctx->parent_ctx = parent_ctx;
4225 child_ctx->parent_gen = parent_ctx->generation;
4226 }
4227 get_ctx(child_ctx->parent_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01004228 }
4229
Paul Mackerrasd859e292009-01-17 18:10:22 +11004230 mutex_unlock(&parent_ctx->mutex);
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02004231
Paul Mackerras25346b932009-06-01 17:48:12 +10004232 perf_unpin_context(parent_ctx);
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004233
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02004234 return ret;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004235}
4236
Ingo Molnar04289bb2008-12-11 08:38:42 +01004237static void __cpuinit perf_counter_init_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01004238{
Ingo Molnar04289bb2008-12-11 08:38:42 +01004239 struct perf_cpu_context *cpuctx;
Thomas Gleixner0793a612008-12-04 20:12:29 +01004240
Ingo Molnar04289bb2008-12-11 08:38:42 +01004241 cpuctx = &per_cpu(perf_cpu_context, cpu);
4242 __perf_counter_init_context(&cpuctx->ctx, NULL);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004243
Ingo Molnar1dce8d92009-05-04 19:23:18 +02004244 spin_lock(&perf_resource_lock);
Ingo Molnar04289bb2008-12-11 08:38:42 +01004245 cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu;
Ingo Molnar1dce8d92009-05-04 19:23:18 +02004246 spin_unlock(&perf_resource_lock);
Ingo Molnar04289bb2008-12-11 08:38:42 +01004247
Paul Mackerras01d02872009-01-14 13:44:19 +11004248 hw_perf_counter_setup(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004249}
4250
4251#ifdef CONFIG_HOTPLUG_CPU
Ingo Molnar04289bb2008-12-11 08:38:42 +01004252static void __perf_counter_exit_cpu(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01004253{
4254 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
4255 struct perf_counter_context *ctx = &cpuctx->ctx;
4256 struct perf_counter *counter, *tmp;
4257
Ingo Molnar04289bb2008-12-11 08:38:42 +01004258 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry)
4259 __perf_counter_remove_from_context(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004260}
Ingo Molnar04289bb2008-12-11 08:38:42 +01004261static void perf_counter_exit_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01004262{
Paul Mackerrasd859e292009-01-17 18:10:22 +11004263 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
4264 struct perf_counter_context *ctx = &cpuctx->ctx;
4265
4266 mutex_lock(&ctx->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01004267 smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1);
Paul Mackerrasd859e292009-01-17 18:10:22 +11004268 mutex_unlock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004269}
4270#else
Ingo Molnar04289bb2008-12-11 08:38:42 +01004271static inline void perf_counter_exit_cpu(int cpu) { }
Thomas Gleixner0793a612008-12-04 20:12:29 +01004272#endif
4273
4274static int __cpuinit
4275perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
4276{
4277 unsigned int cpu = (long)hcpu;
4278
4279 switch (action) {
4280
4281 case CPU_UP_PREPARE:
4282 case CPU_UP_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01004283 perf_counter_init_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004284 break;
4285
4286 case CPU_DOWN_PREPARE:
4287 case CPU_DOWN_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01004288 perf_counter_exit_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004289 break;
4290
4291 default:
4292 break;
4293 }
4294
4295 return NOTIFY_OK;
4296}
4297
Paul Mackerrasf38b0822009-06-02 21:05:16 +10004298/*
4299 * This has to have a higher priority than migration_notifier in sched.c.
4300 */
Thomas Gleixner0793a612008-12-04 20:12:29 +01004301static struct notifier_block __cpuinitdata perf_cpu_nb = {
4302 .notifier_call = perf_cpu_notify,
Paul Mackerrasf38b0822009-06-02 21:05:16 +10004303 .priority = 20,
Thomas Gleixner0793a612008-12-04 20:12:29 +01004304};
4305
Ingo Molnar0d905bc2009-05-04 19:13:30 +02004306void __init perf_counter_init(void)
Thomas Gleixner0793a612008-12-04 20:12:29 +01004307{
4308 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
4309 (void *)(long)smp_processor_id());
4310 register_cpu_notifier(&perf_cpu_nb);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004311}
Thomas Gleixner0793a612008-12-04 20:12:29 +01004312
4313static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
4314{
4315 return sprintf(buf, "%d\n", perf_reserved_percpu);
4316}
4317
4318static ssize_t
4319perf_set_reserve_percpu(struct sysdev_class *class,
4320 const char *buf,
4321 size_t count)
4322{
4323 struct perf_cpu_context *cpuctx;
4324 unsigned long val;
4325 int err, cpu, mpt;
4326
4327 err = strict_strtoul(buf, 10, &val);
4328 if (err)
4329 return err;
4330 if (val > perf_max_counters)
4331 return -EINVAL;
4332
Ingo Molnar1dce8d92009-05-04 19:23:18 +02004333 spin_lock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004334 perf_reserved_percpu = val;
4335 for_each_online_cpu(cpu) {
4336 cpuctx = &per_cpu(perf_cpu_context, cpu);
4337 spin_lock_irq(&cpuctx->ctx.lock);
4338 mpt = min(perf_max_counters - cpuctx->ctx.nr_counters,
4339 perf_max_counters - perf_reserved_percpu);
4340 cpuctx->max_pertask = mpt;
4341 spin_unlock_irq(&cpuctx->ctx.lock);
4342 }
Ingo Molnar1dce8d92009-05-04 19:23:18 +02004343 spin_unlock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004344
4345 return count;
4346}
4347
4348static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
4349{
4350 return sprintf(buf, "%d\n", perf_overcommit);
4351}
4352
4353static ssize_t
4354perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
4355{
4356 unsigned long val;
4357 int err;
4358
4359 err = strict_strtoul(buf, 10, &val);
4360 if (err)
4361 return err;
4362 if (val > 1)
4363 return -EINVAL;
4364
Ingo Molnar1dce8d92009-05-04 19:23:18 +02004365 spin_lock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004366 perf_overcommit = val;
Ingo Molnar1dce8d92009-05-04 19:23:18 +02004367 spin_unlock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004368
4369 return count;
4370}
4371
4372static SYSDEV_CLASS_ATTR(
4373 reserve_percpu,
4374 0644,
4375 perf_show_reserve_percpu,
4376 perf_set_reserve_percpu
4377 );
4378
4379static SYSDEV_CLASS_ATTR(
4380 overcommit,
4381 0644,
4382 perf_show_overcommit,
4383 perf_set_overcommit
4384 );
4385
4386static struct attribute *perfclass_attrs[] = {
4387 &attr_reserve_percpu.attr,
4388 &attr_overcommit.attr,
4389 NULL
4390};
4391
4392static struct attribute_group perfclass_attr_group = {
4393 .attrs = perfclass_attrs,
4394 .name = "perf_counters",
4395};
4396
4397static int __init perf_counter_sysfs_init(void)
4398{
4399 return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
4400 &perfclass_attr_group);
4401}
4402device_initcall(perf_counter_sysfs_init);