blob: b8c6b97a20a32f82e45f2c74b1e1ac615ee65290 [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 Zijlstra9f498cc2009-07-23 14:46:33 +020045static atomic_t nr_task_counters __read_mostly;
Peter Zijlstra9ee318a2009-04-09 10:53:44 +020046
Peter Zijlstra07647712009-06-11 11:18:36 +020047/*
Peter Zijlstradf58ab22009-06-11 11:25:05 +020048 * perf counter paranoia level:
49 * 0 - not paranoid
50 * 1 - disallow cpu counters to unpriv
51 * 2 - disallow kernel profiling to unpriv
Peter Zijlstra07647712009-06-11 11:18:36 +020052 */
Peter Zijlstradf58ab22009-06-11 11:25:05 +020053int sysctl_perf_counter_paranoid __read_mostly;
Peter Zijlstra07647712009-06-11 11:18:36 +020054
55static inline bool perf_paranoid_cpu(void)
56{
57 return sysctl_perf_counter_paranoid > 0;
58}
59
60static inline bool perf_paranoid_kernel(void)
61{
62 return sysctl_perf_counter_paranoid > 1;
63}
64
Peter Zijlstra789f90f2009-05-15 15:19:27 +020065int sysctl_perf_counter_mlock __read_mostly = 512; /* 'free' kb per user */
Peter Zijlstradf58ab22009-06-11 11:25:05 +020066
67/*
68 * max perf counter sample rate
69 */
70int sysctl_perf_counter_sample_rate __read_mostly = 100000;
Peter Zijlstra1ccd1542009-04-09 10:53:45 +020071
Peter Zijlstraa96bbc12009-06-03 14:01:36 +020072static atomic64_t perf_counter_id;
73
Thomas Gleixner0793a612008-12-04 20:12:29 +010074/*
Ingo Molnar1dce8d92009-05-04 19:23:18 +020075 * Lock for (sysadmin-configurable) counter reservations:
Thomas Gleixner0793a612008-12-04 20:12:29 +010076 */
Ingo Molnar1dce8d92009-05-04 19:23:18 +020077static DEFINE_SPINLOCK(perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +010078
79/*
80 * Architecture provided APIs - weak aliases:
81 */
Robert Richter4aeb0b42009-04-29 12:47:03 +020082extern __weak const struct pmu *hw_perf_counter_init(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +010083{
Paul Mackerrasff6f0542009-01-09 16:19:25 +110084 return NULL;
Thomas Gleixner0793a612008-12-04 20:12:29 +010085}
86
Peter Zijlstra9e35ad32009-05-13 16:21:38 +020087void __weak hw_perf_disable(void) { barrier(); }
88void __weak hw_perf_enable(void) { barrier(); }
89
Paul Mackerras01d02872009-01-14 13:44:19 +110090void __weak hw_perf_counter_setup(int cpu) { barrier(); }
Ingo Molnar28402972009-08-13 10:13:22 +020091void __weak hw_perf_counter_setup_online(int cpu) { barrier(); }
Ingo Molnar22a4f652009-06-01 10:13:37 +020092
93int __weak
94hw_perf_group_sched_in(struct perf_counter *group_leader,
Paul Mackerras3cbed422009-01-09 16:43:42 +110095 struct perf_cpu_context *cpuctx,
96 struct perf_counter_context *ctx, int cpu)
97{
98 return 0;
99}
Thomas Gleixner0793a612008-12-04 20:12:29 +0100100
Paul Mackerras4eb96fc2009-01-09 17:24:34 +1100101void __weak perf_counter_print_debug(void) { }
102
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200103static DEFINE_PER_CPU(int, disable_count);
104
105void __perf_disable(void)
106{
107 __get_cpu_var(disable_count)++;
108}
109
110bool __perf_enable(void)
111{
112 return !--__get_cpu_var(disable_count);
113}
114
115void perf_disable(void)
116{
117 __perf_disable();
118 hw_perf_disable();
119}
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200120
121void perf_enable(void)
122{
123 if (__perf_enable())
124 hw_perf_enable();
125}
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200126
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000127static void get_ctx(struct perf_counter_context *ctx)
128{
Peter Zijlstrae5289d42009-06-19 13:22:51 +0200129 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000130}
131
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000132static void free_ctx(struct rcu_head *head)
133{
134 struct perf_counter_context *ctx;
135
136 ctx = container_of(head, struct perf_counter_context, rcu_head);
137 kfree(ctx);
138}
139
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000140static void put_ctx(struct perf_counter_context *ctx)
141{
Paul Mackerras564c2b22009-05-22 14:27:22 +1000142 if (atomic_dec_and_test(&ctx->refcount)) {
143 if (ctx->parent_ctx)
144 put_ctx(ctx->parent_ctx);
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000145 if (ctx->task)
146 put_task_struct(ctx->task);
147 call_rcu(&ctx->rcu_head, free_ctx);
Paul Mackerras564c2b22009-05-22 14:27:22 +1000148 }
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000149}
150
Peter Zijlstra71a851b2009-07-10 09:06:56 +0200151static void unclone_ctx(struct perf_counter_context *ctx)
152{
153 if (ctx->parent_ctx) {
154 put_ctx(ctx->parent_ctx);
155 ctx->parent_ctx = NULL;
156 }
157}
158
Peter Zijlstrafccc7142009-05-23 18:28:56 +0200159/*
Peter Zijlstra7f453c22009-07-21 13:19:40 +0200160 * If we inherit counters we want to return the parent counter id
161 * to userspace.
162 */
163static u64 primary_counter_id(struct perf_counter *counter)
164{
165 u64 id = counter->id;
166
167 if (counter->parent)
168 id = counter->parent->id;
169
170 return id;
171}
172
Peter Zijlstrafccc7142009-05-23 18:28:56 +0200173/*
Paul Mackerras25346b92009-06-01 17:48:12 +1000174 * Get the perf_counter_context for a task and lock it.
175 * This has to cope with with the fact that until it is locked,
176 * the context could get moved to another task.
177 */
Ingo Molnar22a4f652009-06-01 10:13:37 +0200178static struct perf_counter_context *
179perf_lock_task_context(struct task_struct *task, unsigned long *flags)
Paul Mackerras25346b92009-06-01 17:48:12 +1000180{
181 struct perf_counter_context *ctx;
182
183 rcu_read_lock();
184 retry:
185 ctx = rcu_dereference(task->perf_counter_ctxp);
186 if (ctx) {
187 /*
188 * If this context is a clone of another, it might
189 * get swapped for another underneath us by
190 * perf_counter_task_sched_out, though the
191 * rcu_read_lock() protects us from any context
192 * getting freed. Lock the context and check if it
193 * got swapped before we could get the lock, and retry
194 * if so. If we locked the right context, then it
195 * can't get swapped on us any more.
196 */
197 spin_lock_irqsave(&ctx->lock, *flags);
198 if (ctx != rcu_dereference(task->perf_counter_ctxp)) {
199 spin_unlock_irqrestore(&ctx->lock, *flags);
200 goto retry;
201 }
Peter Zijlstrab49a9e72009-06-19 17:39:33 +0200202
203 if (!atomic_inc_not_zero(&ctx->refcount)) {
204 spin_unlock_irqrestore(&ctx->lock, *flags);
205 ctx = NULL;
206 }
Paul Mackerras25346b92009-06-01 17:48:12 +1000207 }
208 rcu_read_unlock();
209 return ctx;
210}
211
212/*
213 * Get the context for a task and increment its pin_count so it
214 * can't get swapped to another task. This also increments its
215 * reference count so that the context can't get freed.
216 */
217static struct perf_counter_context *perf_pin_task_context(struct task_struct *task)
218{
219 struct perf_counter_context *ctx;
220 unsigned long flags;
221
222 ctx = perf_lock_task_context(task, &flags);
223 if (ctx) {
224 ++ctx->pin_count;
Paul Mackerras25346b92009-06-01 17:48:12 +1000225 spin_unlock_irqrestore(&ctx->lock, flags);
226 }
227 return ctx;
228}
229
230static void perf_unpin_context(struct perf_counter_context *ctx)
231{
232 unsigned long flags;
233
234 spin_lock_irqsave(&ctx->lock, flags);
235 --ctx->pin_count;
236 spin_unlock_irqrestore(&ctx->lock, flags);
237 put_ctx(ctx);
238}
239
240/*
Peter Zijlstrafccc7142009-05-23 18:28:56 +0200241 * Add a counter from the lists for its context.
242 * Must be called with ctx->mutex and ctx->lock held.
243 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100244static void
245list_add_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
246{
247 struct perf_counter *group_leader = counter->group_leader;
248
249 /*
250 * Depending on whether it is a standalone or sibling counter,
251 * add it straight to the context's counter list, or to the group
252 * leader's sibling list:
253 */
Peter Zijlstra3df5eda2009-05-08 18:52:22 +0200254 if (group_leader == counter)
Ingo Molnar04289bb2008-12-11 08:38:42 +0100255 list_add_tail(&counter->list_entry, &ctx->counter_list);
Peter Zijlstra5c148192009-03-25 12:30:23 +0100256 else {
Ingo Molnar04289bb2008-12-11 08:38:42 +0100257 list_add_tail(&counter->list_entry, &group_leader->sibling_list);
Peter Zijlstra5c148192009-03-25 12:30:23 +0100258 group_leader->nr_siblings++;
259 }
Peter Zijlstra592903c2009-03-13 12:21:36 +0100260
261 list_add_rcu(&counter->event_entry, &ctx->event_list);
Peter Zijlstra8bc20952009-05-15 20:45:59 +0200262 ctx->nr_counters++;
Peter Zijlstrabfbd3382009-06-24 21:11:59 +0200263 if (counter->attr.inherit_stat)
264 ctx->nr_stat++;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100265}
266
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000267/*
268 * Remove a counter from the lists for its context.
Peter Zijlstrafccc7142009-05-23 18:28:56 +0200269 * Must be called with ctx->mutex and ctx->lock held.
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000270 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100271static void
272list_del_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
273{
274 struct perf_counter *sibling, *tmp;
275
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000276 if (list_empty(&counter->list_entry))
277 return;
Peter Zijlstra8bc20952009-05-15 20:45:59 +0200278 ctx->nr_counters--;
Peter Zijlstrabfbd3382009-06-24 21:11:59 +0200279 if (counter->attr.inherit_stat)
280 ctx->nr_stat--;
Peter Zijlstra8bc20952009-05-15 20:45:59 +0200281
Ingo Molnar04289bb2008-12-11 08:38:42 +0100282 list_del_init(&counter->list_entry);
Peter Zijlstra592903c2009-03-13 12:21:36 +0100283 list_del_rcu(&counter->event_entry);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100284
Peter Zijlstra5c148192009-03-25 12:30:23 +0100285 if (counter->group_leader != counter)
286 counter->group_leader->nr_siblings--;
287
Ingo Molnar04289bb2008-12-11 08:38:42 +0100288 /*
289 * If this was a group counter with sibling counters then
290 * upgrade the siblings to singleton counters by adding them
291 * to the context list directly:
292 */
293 list_for_each_entry_safe(sibling, tmp,
294 &counter->sibling_list, list_entry) {
295
Peter Zijlstra75564232009-03-13 12:21:29 +0100296 list_move_tail(&sibling->list_entry, &ctx->counter_list);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100297 sibling->group_leader = sibling;
298 }
299}
300
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100301static void
302counter_sched_out(struct perf_counter *counter,
303 struct perf_cpu_context *cpuctx,
304 struct perf_counter_context *ctx)
305{
306 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
307 return;
308
309 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200310 counter->tstamp_stopped = ctx->time;
Robert Richter4aeb0b42009-04-29 12:47:03 +0200311 counter->pmu->disable(counter);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100312 counter->oncpu = -1;
313
314 if (!is_software_counter(counter))
315 cpuctx->active_oncpu--;
316 ctx->nr_active--;
Peter Zijlstra0d486962009-06-02 19:22:16 +0200317 if (counter->attr.exclusive || !cpuctx->active_oncpu)
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100318 cpuctx->exclusive = 0;
319}
320
Paul Mackerrasd859e292009-01-17 18:10:22 +1100321static void
322group_sched_out(struct perf_counter *group_counter,
323 struct perf_cpu_context *cpuctx,
324 struct perf_counter_context *ctx)
325{
326 struct perf_counter *counter;
327
328 if (group_counter->state != PERF_COUNTER_STATE_ACTIVE)
329 return;
330
331 counter_sched_out(group_counter, cpuctx, ctx);
332
333 /*
334 * Schedule out siblings (if any):
335 */
336 list_for_each_entry(counter, &group_counter->sibling_list, list_entry)
337 counter_sched_out(counter, cpuctx, ctx);
338
Peter Zijlstra0d486962009-06-02 19:22:16 +0200339 if (group_counter->attr.exclusive)
Paul Mackerrasd859e292009-01-17 18:10:22 +1100340 cpuctx->exclusive = 0;
341}
342
Thomas Gleixner0793a612008-12-04 20:12:29 +0100343/*
344 * Cross CPU call to remove a performance counter
345 *
346 * We disable the counter on the hardware level first. After that we
347 * remove it from the context list.
348 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100349static void __perf_counter_remove_from_context(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100350{
351 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
352 struct perf_counter *counter = info;
353 struct perf_counter_context *ctx = counter->ctx;
354
355 /*
356 * If this is a task context, we need to check whether it is
357 * the current task context of this cpu. If not it has been
358 * scheduled out before the smp call arrived.
359 */
Peter Zijlstra665c2142009-05-29 14:51:57 +0200360 if (ctx->task && cpuctx->task_ctx != ctx)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100361 return;
362
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200363 spin_lock(&ctx->lock);
Ingo Molnar34adc802009-05-20 20:13:28 +0200364 /*
365 * Protect the list operation against NMI by disabling the
366 * counters on a global level.
367 */
368 perf_disable();
Thomas Gleixner0793a612008-12-04 20:12:29 +0100369
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100370 counter_sched_out(counter, cpuctx, ctx);
371
Ingo Molnar04289bb2008-12-11 08:38:42 +0100372 list_del_counter(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100373
374 if (!ctx->task) {
375 /*
376 * Allow more per task counters with respect to the
377 * reservation:
378 */
379 cpuctx->max_pertask =
380 min(perf_max_counters - ctx->nr_counters,
381 perf_max_counters - perf_reserved_percpu);
382 }
383
Ingo Molnar34adc802009-05-20 20:13:28 +0200384 perf_enable();
Peter Zijlstra665c2142009-05-29 14:51:57 +0200385 spin_unlock(&ctx->lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100386}
387
388
389/*
390 * Remove the counter from a task's (or a CPU's) list of counters.
391 *
Peter Zijlstrafccc7142009-05-23 18:28:56 +0200392 * Must be called with ctx->mutex held.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100393 *
394 * CPU counters are removed with a smp call. For task counters we only
395 * call when the task is on a CPU.
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000396 *
397 * If counter->ctx is a cloned context, callers must make sure that
398 * every task struct that counter->ctx->task could possibly point to
399 * remains valid. This is OK when called from perf_release since
400 * that only calls us on the top-level context, which can't be a clone.
401 * When called from perf_counter_exit_task, it's OK because the
402 * context has been detached from its task.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100403 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100404static void perf_counter_remove_from_context(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100405{
406 struct perf_counter_context *ctx = counter->ctx;
407 struct task_struct *task = ctx->task;
408
409 if (!task) {
410 /*
411 * Per cpu counters are removed via an smp call and
412 * the removal is always sucessful.
413 */
414 smp_call_function_single(counter->cpu,
Ingo Molnar04289bb2008-12-11 08:38:42 +0100415 __perf_counter_remove_from_context,
Thomas Gleixner0793a612008-12-04 20:12:29 +0100416 counter, 1);
417 return;
418 }
419
420retry:
Ingo Molnar04289bb2008-12-11 08:38:42 +0100421 task_oncpu_function_call(task, __perf_counter_remove_from_context,
Thomas Gleixner0793a612008-12-04 20:12:29 +0100422 counter);
423
424 spin_lock_irq(&ctx->lock);
425 /*
426 * If the context is active we need to retry the smp call.
427 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100428 if (ctx->nr_active && !list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100429 spin_unlock_irq(&ctx->lock);
430 goto retry;
431 }
432
433 /*
434 * The lock prevents that this context is scheduled in so we
Ingo Molnar04289bb2008-12-11 08:38:42 +0100435 * can remove the counter safely, if the call above did not
Thomas Gleixner0793a612008-12-04 20:12:29 +0100436 * succeed.
437 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100438 if (!list_empty(&counter->list_entry)) {
Ingo Molnar04289bb2008-12-11 08:38:42 +0100439 list_del_counter(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100440 }
441 spin_unlock_irq(&ctx->lock);
442}
443
Peter Zijlstra4af49982009-04-06 11:45:10 +0200444static inline u64 perf_clock(void)
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100445{
Peter Zijlstra4af49982009-04-06 11:45:10 +0200446 return cpu_clock(smp_processor_id());
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100447}
448
449/*
450 * Update the record of the current time in a context.
451 */
Peter Zijlstra4af49982009-04-06 11:45:10 +0200452static void update_context_time(struct perf_counter_context *ctx)
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100453{
Peter Zijlstra4af49982009-04-06 11:45:10 +0200454 u64 now = perf_clock();
455
456 ctx->time += now - ctx->timestamp;
457 ctx->timestamp = now;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100458}
459
460/*
461 * Update the total_time_enabled and total_time_running fields for a counter.
462 */
463static void update_counter_times(struct perf_counter *counter)
464{
465 struct perf_counter_context *ctx = counter->ctx;
466 u64 run_end;
467
Peter Zijlstra4af49982009-04-06 11:45:10 +0200468 if (counter->state < PERF_COUNTER_STATE_INACTIVE)
469 return;
470
471 counter->total_time_enabled = ctx->time - counter->tstamp_enabled;
472
473 if (counter->state == PERF_COUNTER_STATE_INACTIVE)
474 run_end = counter->tstamp_stopped;
475 else
476 run_end = ctx->time;
477
478 counter->total_time_running = run_end - counter->tstamp_running;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100479}
480
481/*
482 * Update total_time_enabled and total_time_running for all counters in a group.
483 */
484static void update_group_times(struct perf_counter *leader)
485{
486 struct perf_counter *counter;
487
488 update_counter_times(leader);
489 list_for_each_entry(counter, &leader->sibling_list, list_entry)
490 update_counter_times(counter);
491}
492
493/*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100494 * Cross CPU call to disable a performance counter
495 */
496static void __perf_counter_disable(void *info)
497{
498 struct perf_counter *counter = info;
499 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
500 struct perf_counter_context *ctx = counter->ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100501
502 /*
503 * If this is a per-task counter, need to check whether this
504 * counter's task is the current task on this cpu.
505 */
Peter Zijlstra665c2142009-05-29 14:51:57 +0200506 if (ctx->task && cpuctx->task_ctx != ctx)
Paul Mackerrasd859e292009-01-17 18:10:22 +1100507 return;
508
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200509 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100510
511 /*
512 * If the counter is on, turn it off.
513 * If it is in error state, leave it in error state.
514 */
515 if (counter->state >= PERF_COUNTER_STATE_INACTIVE) {
Peter Zijlstra4af49982009-04-06 11:45:10 +0200516 update_context_time(ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100517 update_counter_times(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100518 if (counter == counter->group_leader)
519 group_sched_out(counter, cpuctx, ctx);
520 else
521 counter_sched_out(counter, cpuctx, ctx);
522 counter->state = PERF_COUNTER_STATE_OFF;
523 }
524
Peter Zijlstra665c2142009-05-29 14:51:57 +0200525 spin_unlock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100526}
527
528/*
529 * Disable a counter.
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000530 *
531 * If counter->ctx is a cloned context, callers must make sure that
532 * every task struct that counter->ctx->task could possibly point to
533 * remains valid. This condition is satisifed when called through
534 * perf_counter_for_each_child or perf_counter_for_each because they
535 * hold the top-level counter's child_mutex, so any descendant that
536 * goes to exit will block in sync_child_counter.
537 * When called from perf_pending_counter it's OK because counter->ctx
538 * is the current context on this CPU and preemption is disabled,
539 * hence we can't get into perf_counter_task_sched_out for this context.
Paul Mackerrasd859e292009-01-17 18:10:22 +1100540 */
541static void perf_counter_disable(struct perf_counter *counter)
542{
543 struct perf_counter_context *ctx = counter->ctx;
544 struct task_struct *task = ctx->task;
545
546 if (!task) {
547 /*
548 * Disable the counter on the cpu that it's on
549 */
550 smp_call_function_single(counter->cpu, __perf_counter_disable,
551 counter, 1);
552 return;
553 }
554
555 retry:
556 task_oncpu_function_call(task, __perf_counter_disable, counter);
557
558 spin_lock_irq(&ctx->lock);
559 /*
560 * If the counter is still active, we need to retry the cross-call.
561 */
562 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
563 spin_unlock_irq(&ctx->lock);
564 goto retry;
565 }
566
567 /*
568 * Since we have the lock this context can't be scheduled
569 * in, so we can change the state safely.
570 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100571 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
572 update_counter_times(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100573 counter->state = PERF_COUNTER_STATE_OFF;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100574 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100575
576 spin_unlock_irq(&ctx->lock);
577}
578
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100579static int
580counter_sched_in(struct perf_counter *counter,
581 struct perf_cpu_context *cpuctx,
582 struct perf_counter_context *ctx,
583 int cpu)
584{
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100585 if (counter->state <= PERF_COUNTER_STATE_OFF)
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100586 return 0;
587
588 counter->state = PERF_COUNTER_STATE_ACTIVE;
589 counter->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */
590 /*
591 * The new state must be visible before we turn it on in the hardware:
592 */
593 smp_wmb();
594
Robert Richter4aeb0b42009-04-29 12:47:03 +0200595 if (counter->pmu->enable(counter)) {
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100596 counter->state = PERF_COUNTER_STATE_INACTIVE;
597 counter->oncpu = -1;
598 return -EAGAIN;
599 }
600
Peter Zijlstra4af49982009-04-06 11:45:10 +0200601 counter->tstamp_running += ctx->time - counter->tstamp_stopped;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100602
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100603 if (!is_software_counter(counter))
604 cpuctx->active_oncpu++;
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100605 ctx->nr_active++;
606
Peter Zijlstra0d486962009-06-02 19:22:16 +0200607 if (counter->attr.exclusive)
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100608 cpuctx->exclusive = 1;
609
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100610 return 0;
611}
612
Paul Mackerras6751b712009-05-11 12:08:02 +1000613static int
614group_sched_in(struct perf_counter *group_counter,
615 struct perf_cpu_context *cpuctx,
616 struct perf_counter_context *ctx,
617 int cpu)
618{
619 struct perf_counter *counter, *partial_group;
620 int ret;
621
622 if (group_counter->state == PERF_COUNTER_STATE_OFF)
623 return 0;
624
625 ret = hw_perf_group_sched_in(group_counter, cpuctx, ctx, cpu);
626 if (ret)
627 return ret < 0 ? ret : 0;
628
Paul Mackerras6751b712009-05-11 12:08:02 +1000629 if (counter_sched_in(group_counter, cpuctx, ctx, cpu))
630 return -EAGAIN;
631
632 /*
633 * Schedule in siblings as one group (if any):
634 */
635 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
Paul Mackerras6751b712009-05-11 12:08:02 +1000636 if (counter_sched_in(counter, cpuctx, ctx, cpu)) {
637 partial_group = counter;
638 goto group_error;
639 }
640 }
641
642 return 0;
643
644group_error:
645 /*
646 * Groups can be scheduled in as one unit only, so undo any
647 * partial group before returning:
648 */
649 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
650 if (counter == partial_group)
651 break;
652 counter_sched_out(counter, cpuctx, ctx);
653 }
654 counter_sched_out(group_counter, cpuctx, ctx);
655
656 return -EAGAIN;
657}
658
Thomas Gleixner0793a612008-12-04 20:12:29 +0100659/*
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100660 * Return 1 for a group consisting entirely of software counters,
661 * 0 if the group contains any hardware counters.
662 */
663static int is_software_only_group(struct perf_counter *leader)
664{
665 struct perf_counter *counter;
666
667 if (!is_software_counter(leader))
668 return 0;
Peter Zijlstra5c148192009-03-25 12:30:23 +0100669
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100670 list_for_each_entry(counter, &leader->sibling_list, list_entry)
671 if (!is_software_counter(counter))
672 return 0;
Peter Zijlstra5c148192009-03-25 12:30:23 +0100673
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100674 return 1;
675}
676
677/*
678 * Work out whether we can put this counter group on the CPU now.
679 */
680static int group_can_go_on(struct perf_counter *counter,
681 struct perf_cpu_context *cpuctx,
682 int can_add_hw)
683{
684 /*
685 * Groups consisting entirely of software counters can always go on.
686 */
687 if (is_software_only_group(counter))
688 return 1;
689 /*
690 * If an exclusive group is already on, no other hardware
691 * counters can go on.
692 */
693 if (cpuctx->exclusive)
694 return 0;
695 /*
696 * If this group is exclusive and there are already
697 * counters on the CPU, it can't go on.
698 */
Peter Zijlstra0d486962009-06-02 19:22:16 +0200699 if (counter->attr.exclusive && cpuctx->active_oncpu)
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100700 return 0;
701 /*
702 * Otherwise, try to add it if all previous groups were able
703 * to go on.
704 */
705 return can_add_hw;
706}
707
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100708static void add_counter_to_ctx(struct perf_counter *counter,
709 struct perf_counter_context *ctx)
710{
711 list_add_counter(counter, ctx);
Peter Zijlstra4af49982009-04-06 11:45:10 +0200712 counter->tstamp_enabled = ctx->time;
713 counter->tstamp_running = ctx->time;
714 counter->tstamp_stopped = ctx->time;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100715}
716
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100717/*
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100718 * Cross CPU call to install and enable a performance counter
Peter Zijlstra682076a2009-05-23 18:28:57 +0200719 *
720 * Must be called with ctx->mutex held
Thomas Gleixner0793a612008-12-04 20:12:29 +0100721 */
722static void __perf_install_in_context(void *info)
723{
724 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
725 struct perf_counter *counter = info;
726 struct perf_counter_context *ctx = counter->ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100727 struct perf_counter *leader = counter->group_leader;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100728 int cpu = smp_processor_id();
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100729 int err;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100730
731 /*
732 * If this is a task context, we need to check whether it is
733 * the current task context of this cpu. If not it has been
734 * scheduled out before the smp call arrived.
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000735 * Or possibly this is the right context but it isn't
736 * on this cpu because it had no counters.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100737 */
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000738 if (ctx->task && cpuctx->task_ctx != ctx) {
Peter Zijlstra665c2142009-05-29 14:51:57 +0200739 if (cpuctx->task_ctx || ctx->task != current)
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000740 return;
741 cpuctx->task_ctx = ctx;
742 }
Thomas Gleixner0793a612008-12-04 20:12:29 +0100743
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200744 spin_lock(&ctx->lock);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000745 ctx->is_active = 1;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200746 update_context_time(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100747
748 /*
749 * Protect the list operation against NMI by disabling the
750 * counters on a global level. NOP for non NMI based counters.
751 */
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200752 perf_disable();
Thomas Gleixner0793a612008-12-04 20:12:29 +0100753
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100754 add_counter_to_ctx(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100755
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100756 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100757 * Don't put the counter on if it is disabled or if
758 * it is in a group and the group isn't on.
759 */
760 if (counter->state != PERF_COUNTER_STATE_INACTIVE ||
761 (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE))
762 goto unlock;
763
764 /*
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100765 * An exclusive counter can't go on if there are already active
766 * hardware counters, and no hardware counter can go on if there
767 * is already an exclusive counter on.
768 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100769 if (!group_can_go_on(counter, cpuctx, 1))
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100770 err = -EEXIST;
771 else
772 err = counter_sched_in(counter, cpuctx, ctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100773
Paul Mackerrasd859e292009-01-17 18:10:22 +1100774 if (err) {
775 /*
776 * This counter couldn't go on. If it is in a group
777 * then we have to pull the whole group off.
778 * If the counter group is pinned then put it in error state.
779 */
780 if (leader != counter)
781 group_sched_out(leader, cpuctx, ctx);
Peter Zijlstra0d486962009-06-02 19:22:16 +0200782 if (leader->attr.pinned) {
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100783 update_group_times(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100784 leader->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100785 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100786 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100787
788 if (!err && !ctx->task && cpuctx->max_pertask)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100789 cpuctx->max_pertask--;
790
Paul Mackerrasd859e292009-01-17 18:10:22 +1100791 unlock:
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200792 perf_enable();
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100793
Peter Zijlstra665c2142009-05-29 14:51:57 +0200794 spin_unlock(&ctx->lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100795}
796
797/*
798 * Attach a performance counter to a context
799 *
800 * First we add the counter to the list with the hardware enable bit
801 * in counter->hw_config cleared.
802 *
803 * If the counter is attached to a task which is on a CPU we use a smp
804 * call to enable it in the task context. The task might have been
805 * scheduled away, but we check this in the smp call again.
Paul Mackerrasd859e292009-01-17 18:10:22 +1100806 *
807 * Must be called with ctx->mutex held.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100808 */
809static void
810perf_install_in_context(struct perf_counter_context *ctx,
811 struct perf_counter *counter,
812 int cpu)
813{
814 struct task_struct *task = ctx->task;
815
Thomas Gleixner0793a612008-12-04 20:12:29 +0100816 if (!task) {
817 /*
818 * Per cpu counters are installed via an smp call and
819 * the install is always sucessful.
820 */
821 smp_call_function_single(cpu, __perf_install_in_context,
822 counter, 1);
823 return;
824 }
825
Thomas Gleixner0793a612008-12-04 20:12:29 +0100826retry:
827 task_oncpu_function_call(task, __perf_install_in_context,
828 counter);
829
830 spin_lock_irq(&ctx->lock);
831 /*
Thomas Gleixner0793a612008-12-04 20:12:29 +0100832 * we need to retry the smp call.
833 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100834 if (ctx->is_active && list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100835 spin_unlock_irq(&ctx->lock);
836 goto retry;
837 }
838
839 /*
840 * The lock prevents that this context is scheduled in so we
841 * can add the counter safely, if it the call above did not
842 * succeed.
843 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100844 if (list_empty(&counter->list_entry))
845 add_counter_to_ctx(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100846 spin_unlock_irq(&ctx->lock);
847}
848
Paul Mackerrasd859e292009-01-17 18:10:22 +1100849/*
850 * Cross CPU call to enable a performance counter
851 */
852static void __perf_counter_enable(void *info)
Ingo Molnar04289bb2008-12-11 08:38:42 +0100853{
Paul Mackerrasd859e292009-01-17 18:10:22 +1100854 struct perf_counter *counter = info;
855 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
856 struct perf_counter_context *ctx = counter->ctx;
857 struct perf_counter *leader = counter->group_leader;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100858 int err;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100859
860 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100861 * If this is a per-task counter, need to check whether this
862 * counter's task is the current task on this cpu.
Ingo Molnar04289bb2008-12-11 08:38:42 +0100863 */
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000864 if (ctx->task && cpuctx->task_ctx != ctx) {
Peter Zijlstra665c2142009-05-29 14:51:57 +0200865 if (cpuctx->task_ctx || ctx->task != current)
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000866 return;
867 cpuctx->task_ctx = ctx;
868 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100869
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200870 spin_lock(&ctx->lock);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000871 ctx->is_active = 1;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200872 update_context_time(ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100873
874 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
875 goto unlock;
876 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200877 counter->tstamp_enabled = ctx->time - counter->total_time_enabled;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100878
879 /*
880 * If the counter is in a group and isn't the group leader,
881 * then don't put it on unless the group is on.
882 */
883 if (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE)
884 goto unlock;
885
Paul Mackerrase758a332009-05-12 21:59:01 +1000886 if (!group_can_go_on(counter, cpuctx, 1)) {
Paul Mackerrasd859e292009-01-17 18:10:22 +1100887 err = -EEXIST;
Paul Mackerrase758a332009-05-12 21:59:01 +1000888 } else {
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200889 perf_disable();
Paul Mackerrase758a332009-05-12 21:59:01 +1000890 if (counter == leader)
891 err = group_sched_in(counter, cpuctx, ctx,
892 smp_processor_id());
893 else
894 err = counter_sched_in(counter, cpuctx, ctx,
895 smp_processor_id());
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200896 perf_enable();
Paul Mackerrase758a332009-05-12 21:59:01 +1000897 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100898
899 if (err) {
900 /*
901 * If this counter can't go on and it's part of a
902 * group, then the whole group has to come off.
903 */
904 if (leader != counter)
905 group_sched_out(leader, cpuctx, ctx);
Peter Zijlstra0d486962009-06-02 19:22:16 +0200906 if (leader->attr.pinned) {
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100907 update_group_times(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100908 leader->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100909 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100910 }
911
912 unlock:
Peter Zijlstra665c2142009-05-29 14:51:57 +0200913 spin_unlock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100914}
915
916/*
917 * Enable a counter.
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000918 *
919 * If counter->ctx is a cloned context, callers must make sure that
920 * every task struct that counter->ctx->task could possibly point to
921 * remains valid. This condition is satisfied when called through
922 * perf_counter_for_each_child or perf_counter_for_each as described
923 * for perf_counter_disable.
Paul Mackerrasd859e292009-01-17 18:10:22 +1100924 */
925static void perf_counter_enable(struct perf_counter *counter)
926{
927 struct perf_counter_context *ctx = counter->ctx;
928 struct task_struct *task = ctx->task;
929
930 if (!task) {
931 /*
932 * Enable the counter on the cpu that it's on
933 */
934 smp_call_function_single(counter->cpu, __perf_counter_enable,
935 counter, 1);
936 return;
937 }
938
939 spin_lock_irq(&ctx->lock);
940 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
941 goto out;
942
943 /*
944 * If the counter is in error state, clear that first.
945 * That way, if we see the counter in error state below, we
946 * know that it has gone back into error state, as distinct
947 * from the task having been scheduled away before the
948 * cross-call arrived.
949 */
950 if (counter->state == PERF_COUNTER_STATE_ERROR)
951 counter->state = PERF_COUNTER_STATE_OFF;
952
953 retry:
954 spin_unlock_irq(&ctx->lock);
955 task_oncpu_function_call(task, __perf_counter_enable, counter);
956
957 spin_lock_irq(&ctx->lock);
958
959 /*
960 * If the context is active and the counter is still off,
961 * we need to retry the cross-call.
962 */
963 if (ctx->is_active && counter->state == PERF_COUNTER_STATE_OFF)
964 goto retry;
965
966 /*
967 * Since we have the lock this context can't be scheduled
968 * in, so we can change the state safely.
969 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100970 if (counter->state == PERF_COUNTER_STATE_OFF) {
Paul Mackerrasd859e292009-01-17 18:10:22 +1100971 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200972 counter->tstamp_enabled =
973 ctx->time - counter->total_time_enabled;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100974 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100975 out:
976 spin_unlock_irq(&ctx->lock);
977}
978
Peter Zijlstra2023b352009-05-05 17:50:26 +0200979static int perf_counter_refresh(struct perf_counter *counter, int refresh)
Peter Zijlstra79f14642009-04-06 11:45:07 +0200980{
Peter Zijlstra2023b352009-05-05 17:50:26 +0200981 /*
982 * not supported on inherited counters
983 */
Peter Zijlstra0d486962009-06-02 19:22:16 +0200984 if (counter->attr.inherit)
Peter Zijlstra2023b352009-05-05 17:50:26 +0200985 return -EINVAL;
986
Peter Zijlstra79f14642009-04-06 11:45:07 +0200987 atomic_add(refresh, &counter->event_limit);
988 perf_counter_enable(counter);
Peter Zijlstra2023b352009-05-05 17:50:26 +0200989
990 return 0;
Peter Zijlstra79f14642009-04-06 11:45:07 +0200991}
992
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100993void __perf_counter_sched_out(struct perf_counter_context *ctx,
994 struct perf_cpu_context *cpuctx)
995{
996 struct perf_counter *counter;
997
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100998 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100999 ctx->is_active = 0;
1000 if (likely(!ctx->nr_counters))
1001 goto out;
Peter Zijlstra4af49982009-04-06 11:45:10 +02001002 update_context_time(ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001003
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001004 perf_disable();
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001005 if (ctx->nr_active) {
Peter Zijlstraafedadf2009-05-20 12:21:22 +02001006 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1007 if (counter != counter->group_leader)
1008 counter_sched_out(counter, cpuctx, ctx);
1009 else
1010 group_sched_out(counter, cpuctx, ctx);
1011 }
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001012 }
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001013 perf_enable();
Paul Mackerrasd859e292009-01-17 18:10:22 +11001014 out:
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001015 spin_unlock(&ctx->lock);
1016}
1017
Thomas Gleixner0793a612008-12-04 20:12:29 +01001018/*
Paul Mackerras564c2b22009-05-22 14:27:22 +10001019 * Test whether two contexts are equivalent, i.e. whether they
1020 * have both been cloned from the same version of the same context
1021 * and they both have the same number of enabled counters.
1022 * If the number of enabled counters is the same, then the set
1023 * of enabled counters should be the same, because these are both
1024 * inherited contexts, therefore we can't access individual counters
1025 * in them directly with an fd; we can only enable/disable all
1026 * counters via prctl, or enable/disable all counters in a family
1027 * via ioctl, which will have the same effect on both contexts.
1028 */
1029static int context_equiv(struct perf_counter_context *ctx1,
1030 struct perf_counter_context *ctx2)
1031{
1032 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001033 && ctx1->parent_gen == ctx2->parent_gen
Paul Mackerras25346b92009-06-01 17:48:12 +10001034 && !ctx1->pin_count && !ctx2->pin_count;
Paul Mackerras564c2b22009-05-22 14:27:22 +10001035}
1036
Peter Zijlstrabfbd3382009-06-24 21:11:59 +02001037static void __perf_counter_read(void *counter);
1038
1039static void __perf_counter_sync_stat(struct perf_counter *counter,
1040 struct perf_counter *next_counter)
1041{
1042 u64 value;
1043
1044 if (!counter->attr.inherit_stat)
1045 return;
1046
1047 /*
1048 * Update the counter value, we cannot use perf_counter_read()
1049 * because we're in the middle of a context switch and have IRQs
1050 * disabled, which upsets smp_call_function_single(), however
1051 * we know the counter must be on the current CPU, therefore we
1052 * don't need to use it.
1053 */
1054 switch (counter->state) {
1055 case PERF_COUNTER_STATE_ACTIVE:
1056 __perf_counter_read(counter);
1057 break;
1058
1059 case PERF_COUNTER_STATE_INACTIVE:
1060 update_counter_times(counter);
1061 break;
1062
1063 default:
1064 break;
1065 }
1066
1067 /*
1068 * In order to keep per-task stats reliable we need to flip the counter
1069 * values when we flip the contexts.
1070 */
1071 value = atomic64_read(&next_counter->count);
1072 value = atomic64_xchg(&counter->count, value);
1073 atomic64_set(&next_counter->count, value);
1074
Peter Zijlstra19d2e752009-06-26 13:10:23 +02001075 swap(counter->total_time_enabled, next_counter->total_time_enabled);
1076 swap(counter->total_time_running, next_counter->total_time_running);
1077
Peter Zijlstrabfbd3382009-06-24 21:11:59 +02001078 /*
Peter Zijlstra19d2e752009-06-26 13:10:23 +02001079 * Since we swizzled the values, update the user visible data too.
Peter Zijlstrabfbd3382009-06-24 21:11:59 +02001080 */
Peter Zijlstra19d2e752009-06-26 13:10:23 +02001081 perf_counter_update_userpage(counter);
1082 perf_counter_update_userpage(next_counter);
Peter Zijlstrabfbd3382009-06-24 21:11:59 +02001083}
1084
1085#define list_next_entry(pos, member) \
1086 list_entry(pos->member.next, typeof(*pos), member)
1087
1088static void perf_counter_sync_stat(struct perf_counter_context *ctx,
1089 struct perf_counter_context *next_ctx)
1090{
1091 struct perf_counter *counter, *next_counter;
1092
1093 if (!ctx->nr_stat)
1094 return;
1095
1096 counter = list_first_entry(&ctx->event_list,
1097 struct perf_counter, event_entry);
1098
1099 next_counter = list_first_entry(&next_ctx->event_list,
1100 struct perf_counter, event_entry);
1101
1102 while (&counter->event_entry != &ctx->event_list &&
1103 &next_counter->event_entry != &next_ctx->event_list) {
1104
1105 __perf_counter_sync_stat(counter, next_counter);
1106
1107 counter = list_next_entry(counter, event_entry);
Peter Zijlstra10545982009-08-06 18:06:26 +02001108 next_counter = list_next_entry(next_counter, event_entry);
Peter Zijlstrabfbd3382009-06-24 21:11:59 +02001109 }
1110}
1111
Paul Mackerras564c2b22009-05-22 14:27:22 +10001112/*
Thomas Gleixner0793a612008-12-04 20:12:29 +01001113 * Called from scheduler to remove the counters of the current task,
1114 * with interrupts disabled.
1115 *
1116 * We stop each counter and update the counter value in counter->count.
1117 *
Ingo Molnar76715812008-12-17 14:20:28 +01001118 * This does not protect us against NMI, but disable()
Thomas Gleixner0793a612008-12-04 20:12:29 +01001119 * sets the disabled bit in the control field of counter _before_
1120 * accessing the counter control register. If a NMI hits, then it will
1121 * not restart the counter.
1122 */
Paul Mackerras564c2b22009-05-22 14:27:22 +10001123void perf_counter_task_sched_out(struct task_struct *task,
1124 struct task_struct *next, int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001125{
1126 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001127 struct perf_counter_context *ctx = task->perf_counter_ctxp;
Paul Mackerras564c2b22009-05-22 14:27:22 +10001128 struct perf_counter_context *next_ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001129 struct perf_counter_context *parent;
Peter Zijlstra4a0deca2009-03-19 20:26:12 +01001130 struct pt_regs *regs;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001131 int do_switch = 1;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001132
Peter Zijlstra10989fb2009-05-25 14:45:28 +02001133 regs = task_pt_regs(task);
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +02001134 perf_swcounter_event(PERF_COUNT_SW_CONTEXT_SWITCHES, 1, 1, regs, 0);
Peter Zijlstra10989fb2009-05-25 14:45:28 +02001135
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001136 if (likely(!ctx || !cpuctx->task_ctx))
Thomas Gleixner0793a612008-12-04 20:12:29 +01001137 return;
1138
Peter Zijlstrabce379b2009-04-06 11:45:13 +02001139 update_context_time(ctx);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001140
1141 rcu_read_lock();
1142 parent = rcu_dereference(ctx->parent_ctx);
Paul Mackerras564c2b22009-05-22 14:27:22 +10001143 next_ctx = next->perf_counter_ctxp;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001144 if (parent && next_ctx &&
1145 rcu_dereference(next_ctx->parent_ctx) == parent) {
1146 /*
1147 * Looks like the two contexts are clones, so we might be
1148 * able to optimize the context switch. We lock both
1149 * contexts and check that they are clones under the
1150 * lock (including re-checking that neither has been
1151 * uncloned in the meantime). It doesn't matter which
1152 * order we take the locks because no other cpu could
1153 * be trying to lock both of these tasks.
1154 */
1155 spin_lock(&ctx->lock);
1156 spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
1157 if (context_equiv(ctx, next_ctx)) {
Peter Zijlstra665c2142009-05-29 14:51:57 +02001158 /*
1159 * XXX do we need a memory barrier of sorts
1160 * wrt to rcu_dereference() of perf_counter_ctxp
1161 */
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001162 task->perf_counter_ctxp = next_ctx;
1163 next->perf_counter_ctxp = ctx;
1164 ctx->task = next;
1165 next_ctx->task = task;
1166 do_switch = 0;
Peter Zijlstrabfbd3382009-06-24 21:11:59 +02001167
1168 perf_counter_sync_stat(ctx, next_ctx);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001169 }
1170 spin_unlock(&next_ctx->lock);
1171 spin_unlock(&ctx->lock);
Paul Mackerras564c2b22009-05-22 14:27:22 +10001172 }
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001173 rcu_read_unlock();
Paul Mackerras564c2b22009-05-22 14:27:22 +10001174
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001175 if (do_switch) {
1176 __perf_counter_sched_out(ctx, cpuctx);
1177 cpuctx->task_ctx = NULL;
1178 }
Thomas Gleixner0793a612008-12-04 20:12:29 +01001179}
1180
Peter Zijlstra665c2142009-05-29 14:51:57 +02001181/*
1182 * Called with IRQs disabled
1183 */
Paul Mackerrasa08b1592009-05-11 15:46:10 +10001184static void __perf_counter_task_sched_out(struct perf_counter_context *ctx)
1185{
1186 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1187
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001188 if (!cpuctx->task_ctx)
1189 return;
Ingo Molnar012b84d2009-05-17 11:08:41 +02001190
1191 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
1192 return;
1193
Paul Mackerrasa08b1592009-05-11 15:46:10 +10001194 __perf_counter_sched_out(ctx, cpuctx);
1195 cpuctx->task_ctx = NULL;
1196}
1197
Peter Zijlstra665c2142009-05-29 14:51:57 +02001198/*
1199 * Called with IRQs disabled
1200 */
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001201static void perf_counter_cpu_sched_out(struct perf_cpu_context *cpuctx)
Ingo Molnar04289bb2008-12-11 08:38:42 +01001202{
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001203 __perf_counter_sched_out(&cpuctx->ctx, cpuctx);
Ingo Molnar04289bb2008-12-11 08:38:42 +01001204}
1205
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001206static void
1207__perf_counter_sched_in(struct perf_counter_context *ctx,
1208 struct perf_cpu_context *cpuctx, int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001209{
Thomas Gleixner0793a612008-12-04 20:12:29 +01001210 struct perf_counter *counter;
Paul Mackerrasdd0e6ba2009-01-12 15:11:00 +11001211 int can_add_hw = 1;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001212
Thomas Gleixner0793a612008-12-04 20:12:29 +01001213 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001214 ctx->is_active = 1;
1215 if (likely(!ctx->nr_counters))
1216 goto out;
1217
Peter Zijlstra4af49982009-04-06 11:45:10 +02001218 ctx->timestamp = perf_clock();
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001219
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001220 perf_disable();
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001221
1222 /*
1223 * First go through the list and put on any pinned groups
1224 * in order to give them the best chance of going on.
1225 */
Ingo Molnar04289bb2008-12-11 08:38:42 +01001226 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001227 if (counter->state <= PERF_COUNTER_STATE_OFF ||
Peter Zijlstra0d486962009-06-02 19:22:16 +02001228 !counter->attr.pinned)
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001229 continue;
1230 if (counter->cpu != -1 && counter->cpu != cpu)
1231 continue;
1232
Peter Zijlstraafedadf2009-05-20 12:21:22 +02001233 if (counter != counter->group_leader)
1234 counter_sched_in(counter, cpuctx, ctx, cpu);
1235 else {
1236 if (group_can_go_on(counter, cpuctx, 1))
1237 group_sched_in(counter, cpuctx, ctx, cpu);
1238 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001239
1240 /*
1241 * If this pinned group hasn't been scheduled,
1242 * put it in error state.
1243 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001244 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
1245 update_group_times(counter);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001246 counter->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001247 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001248 }
1249
1250 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1251 /*
1252 * Ignore counters in OFF or ERROR state, and
1253 * ignore pinned counters since we did them already.
1254 */
1255 if (counter->state <= PERF_COUNTER_STATE_OFF ||
Peter Zijlstra0d486962009-06-02 19:22:16 +02001256 counter->attr.pinned)
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001257 continue;
1258
Ingo Molnar04289bb2008-12-11 08:38:42 +01001259 /*
1260 * Listen to the 'cpu' scheduling filter constraint
1261 * of counters:
1262 */
Thomas Gleixner0793a612008-12-04 20:12:29 +01001263 if (counter->cpu != -1 && counter->cpu != cpu)
1264 continue;
1265
Peter Zijlstraafedadf2009-05-20 12:21:22 +02001266 if (counter != counter->group_leader) {
1267 if (counter_sched_in(counter, cpuctx, ctx, cpu))
Paul Mackerrasdd0e6ba2009-01-12 15:11:00 +11001268 can_add_hw = 0;
Peter Zijlstraafedadf2009-05-20 12:21:22 +02001269 } else {
1270 if (group_can_go_on(counter, cpuctx, can_add_hw)) {
1271 if (group_sched_in(counter, cpuctx, ctx, cpu))
1272 can_add_hw = 0;
1273 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001274 }
Thomas Gleixner0793a612008-12-04 20:12:29 +01001275 }
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001276 perf_enable();
Paul Mackerrasd859e292009-01-17 18:10:22 +11001277 out:
Thomas Gleixner0793a612008-12-04 20:12:29 +01001278 spin_unlock(&ctx->lock);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001279}
Ingo Molnar04289bb2008-12-11 08:38:42 +01001280
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001281/*
1282 * Called from scheduler to add the counters of the current task
1283 * with interrupts disabled.
1284 *
1285 * We restore the counter value and then enable it.
1286 *
1287 * This does not protect us against NMI, but enable()
1288 * sets the enabled bit in the control field of counter _before_
1289 * accessing the counter control register. If a NMI hits, then it will
1290 * keep the counter running.
1291 */
1292void perf_counter_task_sched_in(struct task_struct *task, int cpu)
1293{
1294 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001295 struct perf_counter_context *ctx = task->perf_counter_ctxp;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001296
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001297 if (likely(!ctx))
1298 return;
Paul Mackerras564c2b22009-05-22 14:27:22 +10001299 if (cpuctx->task_ctx == ctx)
1300 return;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001301 __perf_counter_sched_in(ctx, cpuctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001302 cpuctx->task_ctx = ctx;
1303}
1304
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001305static void perf_counter_cpu_sched_in(struct perf_cpu_context *cpuctx, int cpu)
1306{
1307 struct perf_counter_context *ctx = &cpuctx->ctx;
1308
1309 __perf_counter_sched_in(ctx, cpuctx, cpu);
1310}
1311
Peter Zijlstraa78ac322009-05-25 17:39:05 +02001312#define MAX_INTERRUPTS (~0ULL)
1313
1314static void perf_log_throttle(struct perf_counter *counter, int enable);
Peter Zijlstra26b119b2009-05-20 12:21:20 +02001315
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001316static void perf_adjust_period(struct perf_counter *counter, u64 events)
1317{
1318 struct hw_perf_counter *hwc = &counter->hw;
1319 u64 period, sample_period;
1320 s64 delta;
1321
1322 events *= hwc->sample_period;
1323 period = div64_u64(events, counter->attr.sample_freq);
1324
1325 delta = (s64)(period - hwc->sample_period);
1326 delta = (delta + 7) / 8; /* low pass filter */
1327
1328 sample_period = hwc->sample_period + delta;
1329
1330 if (!sample_period)
1331 sample_period = 1;
1332
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001333 hwc->sample_period = sample_period;
1334}
1335
1336static void perf_ctx_adjust_freq(struct perf_counter_context *ctx)
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001337{
1338 struct perf_counter *counter;
Peter Zijlstra6a24ed6c2009-06-05 18:01:29 +02001339 struct hw_perf_counter *hwc;
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001340 u64 interrupts, freq;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001341
1342 spin_lock(&ctx->lock);
1343 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1344 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
1345 continue;
1346
Peter Zijlstra6a24ed6c2009-06-05 18:01:29 +02001347 hwc = &counter->hw;
1348
1349 interrupts = hwc->interrupts;
1350 hwc->interrupts = 0;
Peter Zijlstraa78ac322009-05-25 17:39:05 +02001351
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001352 /*
1353 * unthrottle counters on the tick
1354 */
Peter Zijlstraa78ac322009-05-25 17:39:05 +02001355 if (interrupts == MAX_INTERRUPTS) {
1356 perf_log_throttle(counter, 1);
1357 counter->pmu->unthrottle(counter);
Peter Zijlstradf58ab22009-06-11 11:25:05 +02001358 interrupts = 2*sysctl_perf_counter_sample_rate/HZ;
Peter Zijlstraa78ac322009-05-25 17:39:05 +02001359 }
1360
Peter Zijlstra0d486962009-06-02 19:22:16 +02001361 if (!counter->attr.freq || !counter->attr.sample_freq)
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001362 continue;
1363
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001364 /*
1365 * if the specified freq < HZ then we need to skip ticks
1366 */
Peter Zijlstra6a24ed6c2009-06-05 18:01:29 +02001367 if (counter->attr.sample_freq < HZ) {
1368 freq = counter->attr.sample_freq;
1369
1370 hwc->freq_count += freq;
1371 hwc->freq_interrupts += interrupts;
1372
1373 if (hwc->freq_count < HZ)
1374 continue;
1375
1376 interrupts = hwc->freq_interrupts;
1377 hwc->freq_interrupts = 0;
1378 hwc->freq_count -= HZ;
1379 } else
1380 freq = HZ;
1381
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001382 perf_adjust_period(counter, freq * interrupts);
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001383
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001384 /*
1385 * In order to avoid being stalled by an (accidental) huge
1386 * sample period, force reset the sample period if we didn't
1387 * get any events in this freq period.
1388 */
1389 if (!interrupts) {
1390 perf_disable();
1391 counter->pmu->disable(counter);
Paul Mackerras87847b82009-06-13 17:06:50 +10001392 atomic64_set(&hwc->period_left, 0);
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001393 counter->pmu->enable(counter);
1394 perf_enable();
1395 }
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001396 }
1397 spin_unlock(&ctx->lock);
1398}
1399
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001400/*
1401 * Round-robin a context's counters:
1402 */
1403static void rotate_ctx(struct perf_counter_context *ctx)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001404{
Thomas Gleixner0793a612008-12-04 20:12:29 +01001405 struct perf_counter *counter;
1406
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001407 if (!ctx->nr_counters)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001408 return;
1409
Thomas Gleixner0793a612008-12-04 20:12:29 +01001410 spin_lock(&ctx->lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001411 /*
Ingo Molnar04289bb2008-12-11 08:38:42 +01001412 * Rotate the first entry last (works just fine for group counters too):
Thomas Gleixner0793a612008-12-04 20:12:29 +01001413 */
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001414 perf_disable();
Ingo Molnar04289bb2008-12-11 08:38:42 +01001415 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Peter Zijlstra75564232009-03-13 12:21:29 +01001416 list_move_tail(&counter->list_entry, &ctx->counter_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001417 break;
1418 }
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001419 perf_enable();
Thomas Gleixner0793a612008-12-04 20:12:29 +01001420
1421 spin_unlock(&ctx->lock);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001422}
Thomas Gleixner0793a612008-12-04 20:12:29 +01001423
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001424void perf_counter_task_tick(struct task_struct *curr, int cpu)
1425{
Peter Zijlstra7fc23a52009-05-08 18:52:21 +02001426 struct perf_cpu_context *cpuctx;
1427 struct perf_counter_context *ctx;
1428
1429 if (!atomic_read(&nr_counters))
1430 return;
1431
1432 cpuctx = &per_cpu(perf_cpu_context, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001433 ctx = curr->perf_counter_ctxp;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001434
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001435 perf_ctx_adjust_freq(&cpuctx->ctx);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001436 if (ctx)
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001437 perf_ctx_adjust_freq(ctx);
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001438
Ingo Molnarb82914c2009-05-04 18:54:32 +02001439 perf_counter_cpu_sched_out(cpuctx);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001440 if (ctx)
1441 __perf_counter_task_sched_out(ctx);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001442
Ingo Molnarb82914c2009-05-04 18:54:32 +02001443 rotate_ctx(&cpuctx->ctx);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001444 if (ctx)
1445 rotate_ctx(ctx);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001446
Ingo Molnarb82914c2009-05-04 18:54:32 +02001447 perf_counter_cpu_sched_in(cpuctx, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001448 if (ctx)
1449 perf_counter_task_sched_in(curr, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001450}
1451
1452/*
Paul Mackerras57e79862009-06-30 16:07:19 +10001453 * Enable all of a task's counters that have been marked enable-on-exec.
1454 * This expects task == current.
1455 */
1456static void perf_counter_enable_on_exec(struct task_struct *task)
1457{
1458 struct perf_counter_context *ctx;
1459 struct perf_counter *counter;
1460 unsigned long flags;
1461 int enabled = 0;
1462
1463 local_irq_save(flags);
1464 ctx = task->perf_counter_ctxp;
1465 if (!ctx || !ctx->nr_counters)
1466 goto out;
1467
1468 __perf_counter_task_sched_out(ctx);
1469
1470 spin_lock(&ctx->lock);
1471
1472 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1473 if (!counter->attr.enable_on_exec)
1474 continue;
1475 counter->attr.enable_on_exec = 0;
1476 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
1477 continue;
1478 counter->state = PERF_COUNTER_STATE_INACTIVE;
1479 counter->tstamp_enabled =
1480 ctx->time - counter->total_time_enabled;
1481 enabled = 1;
1482 }
1483
1484 /*
1485 * Unclone this context if we enabled any counter.
1486 */
Peter Zijlstra71a851b2009-07-10 09:06:56 +02001487 if (enabled)
1488 unclone_ctx(ctx);
Paul Mackerras57e79862009-06-30 16:07:19 +10001489
1490 spin_unlock(&ctx->lock);
1491
1492 perf_counter_task_sched_in(task, smp_processor_id());
1493 out:
1494 local_irq_restore(flags);
1495}
1496
1497/*
Thomas Gleixner0793a612008-12-04 20:12:29 +01001498 * Cross CPU call to read the hardware counter
1499 */
Peter Zijlstrabfbd3382009-06-24 21:11:59 +02001500static void __perf_counter_read(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001501{
Ingo Molnar621a01e2008-12-11 12:46:46 +01001502 struct perf_counter *counter = info;
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001503 struct perf_counter_context *ctx = counter->ctx;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001504 unsigned long flags;
Ingo Molnar621a01e2008-12-11 12:46:46 +01001505
Peter Zijlstra849691a2009-04-06 11:45:12 +02001506 local_irq_save(flags);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001507 if (ctx->is_active)
Peter Zijlstra4af49982009-04-06 11:45:10 +02001508 update_context_time(ctx);
Robert Richter4aeb0b42009-04-29 12:47:03 +02001509 counter->pmu->read(counter);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001510 update_counter_times(counter);
Peter Zijlstra849691a2009-04-06 11:45:12 +02001511 local_irq_restore(flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001512}
1513
Ingo Molnar04289bb2008-12-11 08:38:42 +01001514static u64 perf_counter_read(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001515{
1516 /*
1517 * If counter is enabled and currently active on a CPU, update the
1518 * value in the counter structure:
1519 */
Ingo Molnar6a930702008-12-11 15:17:03 +01001520 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
Thomas Gleixner0793a612008-12-04 20:12:29 +01001521 smp_call_function_single(counter->oncpu,
Peter Zijlstrabfbd3382009-06-24 21:11:59 +02001522 __perf_counter_read, counter, 1);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001523 } else if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
1524 update_counter_times(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001525 }
1526
Ingo Molnaree060942008-12-13 09:00:03 +01001527 return atomic64_read(&counter->count);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001528}
1529
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001530/*
1531 * Initialize the perf_counter context in a task_struct:
1532 */
1533static void
1534__perf_counter_init_context(struct perf_counter_context *ctx,
1535 struct task_struct *task)
1536{
1537 memset(ctx, 0, sizeof(*ctx));
1538 spin_lock_init(&ctx->lock);
1539 mutex_init(&ctx->mutex);
1540 INIT_LIST_HEAD(&ctx->counter_list);
1541 INIT_LIST_HEAD(&ctx->event_list);
1542 atomic_set(&ctx->refcount, 1);
1543 ctx->task = task;
1544}
1545
Thomas Gleixner0793a612008-12-04 20:12:29 +01001546static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
1547{
Ingo Molnar22a4f652009-06-01 10:13:37 +02001548 struct perf_counter_context *ctx;
1549 struct perf_cpu_context *cpuctx;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001550 struct task_struct *task;
Paul Mackerras25346b92009-06-01 17:48:12 +10001551 unsigned long flags;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001552 int err;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001553
1554 /*
1555 * If cpu is not a wildcard then this is a percpu counter:
1556 */
1557 if (cpu != -1) {
1558 /* Must be root to operate on a CPU counter: */
Peter Zijlstra07647712009-06-11 11:18:36 +02001559 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
Thomas Gleixner0793a612008-12-04 20:12:29 +01001560 return ERR_PTR(-EACCES);
1561
1562 if (cpu < 0 || cpu > num_possible_cpus())
1563 return ERR_PTR(-EINVAL);
1564
1565 /*
1566 * We could be clever and allow to attach a counter to an
1567 * offline CPU and activate it when the CPU comes up, but
1568 * that's for later.
1569 */
1570 if (!cpu_isset(cpu, cpu_online_map))
1571 return ERR_PTR(-ENODEV);
1572
1573 cpuctx = &per_cpu(perf_cpu_context, cpu);
1574 ctx = &cpuctx->ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001575 get_ctx(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001576
Thomas Gleixner0793a612008-12-04 20:12:29 +01001577 return ctx;
1578 }
1579
1580 rcu_read_lock();
1581 if (!pid)
1582 task = current;
1583 else
1584 task = find_task_by_vpid(pid);
1585 if (task)
1586 get_task_struct(task);
1587 rcu_read_unlock();
1588
1589 if (!task)
1590 return ERR_PTR(-ESRCH);
1591
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001592 /*
1593 * Can't attach counters to a dying task.
1594 */
1595 err = -ESRCH;
1596 if (task->flags & PF_EXITING)
1597 goto errout;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001598
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001599 /* Reuse ptrace permission checks for now. */
1600 err = -EACCES;
1601 if (!ptrace_may_access(task, PTRACE_MODE_READ))
1602 goto errout;
1603
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001604 retry:
Paul Mackerras25346b92009-06-01 17:48:12 +10001605 ctx = perf_lock_task_context(task, &flags);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001606 if (ctx) {
Peter Zijlstra71a851b2009-07-10 09:06:56 +02001607 unclone_ctx(ctx);
Paul Mackerras25346b92009-06-01 17:48:12 +10001608 spin_unlock_irqrestore(&ctx->lock, flags);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001609 }
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001610
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001611 if (!ctx) {
1612 ctx = kmalloc(sizeof(struct perf_counter_context), GFP_KERNEL);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001613 err = -ENOMEM;
1614 if (!ctx)
1615 goto errout;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001616 __perf_counter_init_context(ctx, task);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001617 get_ctx(ctx);
1618 if (cmpxchg(&task->perf_counter_ctxp, NULL, ctx)) {
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001619 /*
1620 * We raced with some other task; use
1621 * the context they set.
1622 */
1623 kfree(ctx);
Paul Mackerras25346b92009-06-01 17:48:12 +10001624 goto retry;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001625 }
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001626 get_task_struct(task);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001627 }
1628
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001629 put_task_struct(task);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001630 return ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001631
1632 errout:
1633 put_task_struct(task);
1634 return ERR_PTR(err);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001635}
1636
Peter Zijlstra592903c2009-03-13 12:21:36 +01001637static void free_counter_rcu(struct rcu_head *head)
1638{
1639 struct perf_counter *counter;
1640
1641 counter = container_of(head, struct perf_counter, rcu_head);
Peter Zijlstra709e50c2009-06-02 14:13:15 +02001642 if (counter->ns)
1643 put_pid_ns(counter->ns);
Peter Zijlstra592903c2009-03-13 12:21:36 +01001644 kfree(counter);
1645}
1646
Peter Zijlstra925d5192009-03-30 19:07:02 +02001647static void perf_pending_sync(struct perf_counter *counter);
1648
Peter Zijlstraf1600952009-03-19 20:26:16 +01001649static void free_counter(struct perf_counter *counter)
1650{
Peter Zijlstra925d5192009-03-30 19:07:02 +02001651 perf_pending_sync(counter);
1652
Peter Zijlstraf3440112009-06-22 13:58:35 +02001653 if (!counter->parent) {
1654 atomic_dec(&nr_counters);
1655 if (counter->attr.mmap)
1656 atomic_dec(&nr_mmap_counters);
1657 if (counter->attr.comm)
1658 atomic_dec(&nr_comm_counters);
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02001659 if (counter->attr.task)
1660 atomic_dec(&nr_task_counters);
Peter Zijlstraf3440112009-06-22 13:58:35 +02001661 }
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02001662
Peter Zijlstrae077df42009-03-19 20:26:17 +01001663 if (counter->destroy)
1664 counter->destroy(counter);
1665
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001666 put_ctx(counter->ctx);
Peter Zijlstraf1600952009-03-19 20:26:16 +01001667 call_rcu(&counter->rcu_head, free_counter_rcu);
1668}
1669
Thomas Gleixner0793a612008-12-04 20:12:29 +01001670/*
1671 * Called when the last reference to the file is gone.
1672 */
1673static int perf_release(struct inode *inode, struct file *file)
1674{
1675 struct perf_counter *counter = file->private_data;
1676 struct perf_counter_context *ctx = counter->ctx;
1677
1678 file->private_data = NULL;
1679
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001680 WARN_ON_ONCE(ctx->parent_ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001681 mutex_lock(&ctx->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01001682 perf_counter_remove_from_context(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001683 mutex_unlock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001684
Peter Zijlstra082ff5a2009-05-23 18:29:00 +02001685 mutex_lock(&counter->owner->perf_counter_mutex);
1686 list_del_init(&counter->owner_entry);
1687 mutex_unlock(&counter->owner->perf_counter_mutex);
1688 put_task_struct(counter->owner);
1689
Peter Zijlstraf1600952009-03-19 20:26:16 +01001690 free_counter(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001691
1692 return 0;
1693}
1694
Peter Zijlstra3dab77f2009-08-13 11:47:53 +02001695static int perf_counter_read_size(struct perf_counter *counter)
1696{
1697 int entry = sizeof(u64); /* value */
1698 int size = 0;
1699 int nr = 1;
1700
1701 if (counter->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1702 size += sizeof(u64);
1703
1704 if (counter->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1705 size += sizeof(u64);
1706
1707 if (counter->attr.read_format & PERF_FORMAT_ID)
1708 entry += sizeof(u64);
1709
1710 if (counter->attr.read_format & PERF_FORMAT_GROUP) {
1711 nr += counter->group_leader->nr_siblings;
1712 size += sizeof(u64);
1713 }
1714
1715 size += entry * nr;
1716
1717 return size;
1718}
1719
1720static u64 perf_counter_read_value(struct perf_counter *counter)
Peter Zijlstrae53c0992009-07-24 14:42:10 +02001721{
1722 struct perf_counter *child;
1723 u64 total = 0;
1724
1725 total += perf_counter_read(counter);
1726 list_for_each_entry(child, &counter->child_list, child_list)
1727 total += perf_counter_read(child);
1728
1729 return total;
1730}
1731
Peter Zijlstra3dab77f2009-08-13 11:47:53 +02001732static int perf_counter_read_entry(struct perf_counter *counter,
1733 u64 read_format, char __user *buf)
1734{
1735 int n = 0, count = 0;
1736 u64 values[2];
1737
1738 values[n++] = perf_counter_read_value(counter);
1739 if (read_format & PERF_FORMAT_ID)
1740 values[n++] = primary_counter_id(counter);
1741
1742 count = n * sizeof(u64);
1743
1744 if (copy_to_user(buf, values, count))
1745 return -EFAULT;
1746
1747 return count;
1748}
1749
1750static int perf_counter_read_group(struct perf_counter *counter,
1751 u64 read_format, char __user *buf)
1752{
1753 struct perf_counter *leader = counter->group_leader, *sub;
1754 int n = 0, size = 0, err = -EFAULT;
1755 u64 values[3];
1756
1757 values[n++] = 1 + leader->nr_siblings;
1758 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
1759 values[n++] = leader->total_time_enabled +
1760 atomic64_read(&leader->child_total_time_enabled);
1761 }
1762 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
1763 values[n++] = leader->total_time_running +
1764 atomic64_read(&leader->child_total_time_running);
1765 }
1766
1767 size = n * sizeof(u64);
1768
1769 if (copy_to_user(buf, values, size))
1770 return -EFAULT;
1771
1772 err = perf_counter_read_entry(leader, read_format, buf + size);
1773 if (err < 0)
1774 return err;
1775
1776 size += err;
1777
1778 list_for_each_entry(sub, &leader->sibling_list, list_entry) {
1779 err = perf_counter_read_entry(counter, read_format,
1780 buf + size);
1781 if (err < 0)
1782 return err;
1783
1784 size += err;
1785 }
1786
1787 return size;
1788}
1789
1790static int perf_counter_read_one(struct perf_counter *counter,
1791 u64 read_format, char __user *buf)
1792{
1793 u64 values[4];
1794 int n = 0;
1795
1796 values[n++] = perf_counter_read_value(counter);
1797 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
1798 values[n++] = counter->total_time_enabled +
1799 atomic64_read(&counter->child_total_time_enabled);
1800 }
1801 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
1802 values[n++] = counter->total_time_running +
1803 atomic64_read(&counter->child_total_time_running);
1804 }
1805 if (read_format & PERF_FORMAT_ID)
1806 values[n++] = primary_counter_id(counter);
1807
1808 if (copy_to_user(buf, values, n * sizeof(u64)))
1809 return -EFAULT;
1810
1811 return n * sizeof(u64);
1812}
1813
Thomas Gleixner0793a612008-12-04 20:12:29 +01001814/*
1815 * Read the performance counter - simple non blocking version for now
1816 */
1817static ssize_t
1818perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
1819{
Peter Zijlstra3dab77f2009-08-13 11:47:53 +02001820 u64 read_format = counter->attr.read_format;
1821 int ret;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001822
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001823 /*
1824 * Return end-of-file for a read on a counter that is in
1825 * error state (i.e. because it was pinned but it couldn't be
1826 * scheduled on to the CPU at some point).
1827 */
1828 if (counter->state == PERF_COUNTER_STATE_ERROR)
1829 return 0;
1830
Peter Zijlstra3dab77f2009-08-13 11:47:53 +02001831 if (count < perf_counter_read_size(counter))
1832 return -ENOSPC;
1833
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001834 WARN_ON_ONCE(counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001835 mutex_lock(&counter->child_mutex);
Peter Zijlstra3dab77f2009-08-13 11:47:53 +02001836 if (read_format & PERF_FORMAT_GROUP)
1837 ret = perf_counter_read_group(counter, read_format, buf);
1838 else
1839 ret = perf_counter_read_one(counter, read_format, buf);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001840 mutex_unlock(&counter->child_mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001841
Peter Zijlstra3dab77f2009-08-13 11:47:53 +02001842 return ret;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001843}
1844
1845static ssize_t
Thomas Gleixner0793a612008-12-04 20:12:29 +01001846perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
1847{
1848 struct perf_counter *counter = file->private_data;
1849
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001850 return perf_read_hw(counter, buf, count);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001851}
1852
1853static unsigned int perf_poll(struct file *file, poll_table *wait)
1854{
1855 struct perf_counter *counter = file->private_data;
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001856 struct perf_mmap_data *data;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001857 unsigned int events = POLL_HUP;
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001858
1859 rcu_read_lock();
1860 data = rcu_dereference(counter->data);
1861 if (data)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001862 events = atomic_xchg(&data->poll, 0);
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001863 rcu_read_unlock();
Thomas Gleixner0793a612008-12-04 20:12:29 +01001864
1865 poll_wait(file, &counter->waitq, wait);
1866
Thomas Gleixner0793a612008-12-04 20:12:29 +01001867 return events;
1868}
1869
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001870static void perf_counter_reset(struct perf_counter *counter)
1871{
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001872 (void)perf_counter_read(counter);
Paul Mackerras615a3f12009-05-11 15:50:21 +10001873 atomic64_set(&counter->count, 0);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001874 perf_counter_update_userpage(counter);
1875}
1876
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001877/*
1878 * Holding the top-level counter's child_mutex means that any
1879 * descendant process that has inherited this counter will block
1880 * in sync_child_counter if it goes to exit, thus satisfying the
1881 * task existence requirements of perf_counter_enable/disable.
1882 */
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001883static void perf_counter_for_each_child(struct perf_counter *counter,
1884 void (*func)(struct perf_counter *))
1885{
1886 struct perf_counter *child;
1887
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001888 WARN_ON_ONCE(counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001889 mutex_lock(&counter->child_mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001890 func(counter);
1891 list_for_each_entry(child, &counter->child_list, child_list)
1892 func(child);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001893 mutex_unlock(&counter->child_mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001894}
1895
1896static void perf_counter_for_each(struct perf_counter *counter,
1897 void (*func)(struct perf_counter *))
1898{
Peter Zijlstra75f937f2009-06-15 15:05:12 +02001899 struct perf_counter_context *ctx = counter->ctx;
1900 struct perf_counter *sibling;
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001901
Peter Zijlstra75f937f2009-06-15 15:05:12 +02001902 WARN_ON_ONCE(ctx->parent_ctx);
1903 mutex_lock(&ctx->mutex);
1904 counter = counter->group_leader;
1905
1906 perf_counter_for_each_child(counter, func);
1907 func(counter);
1908 list_for_each_entry(sibling, &counter->sibling_list, list_entry)
1909 perf_counter_for_each_child(counter, func);
1910 mutex_unlock(&ctx->mutex);
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001911}
1912
Peter Zijlstra08247e32009-06-02 16:46:57 +02001913static int perf_counter_period(struct perf_counter *counter, u64 __user *arg)
1914{
1915 struct perf_counter_context *ctx = counter->ctx;
1916 unsigned long size;
1917 int ret = 0;
1918 u64 value;
1919
Peter Zijlstra0d486962009-06-02 19:22:16 +02001920 if (!counter->attr.sample_period)
Peter Zijlstra08247e32009-06-02 16:46:57 +02001921 return -EINVAL;
1922
1923 size = copy_from_user(&value, arg, sizeof(value));
1924 if (size != sizeof(value))
1925 return -EFAULT;
1926
1927 if (!value)
1928 return -EINVAL;
1929
1930 spin_lock_irq(&ctx->lock);
Peter Zijlstra0d486962009-06-02 19:22:16 +02001931 if (counter->attr.freq) {
Peter Zijlstradf58ab22009-06-11 11:25:05 +02001932 if (value > sysctl_perf_counter_sample_rate) {
Peter Zijlstra08247e32009-06-02 16:46:57 +02001933 ret = -EINVAL;
1934 goto unlock;
1935 }
1936
Peter Zijlstra0d486962009-06-02 19:22:16 +02001937 counter->attr.sample_freq = value;
Peter Zijlstra08247e32009-06-02 16:46:57 +02001938 } else {
Peter Zijlstra0d486962009-06-02 19:22:16 +02001939 counter->attr.sample_period = value;
Peter Zijlstra08247e32009-06-02 16:46:57 +02001940 counter->hw.sample_period = value;
Peter Zijlstra08247e32009-06-02 16:46:57 +02001941 }
1942unlock:
1943 spin_unlock_irq(&ctx->lock);
1944
1945 return ret;
1946}
1947
Paul Mackerrasd859e292009-01-17 18:10:22 +11001948static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1949{
1950 struct perf_counter *counter = file->private_data;
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001951 void (*func)(struct perf_counter *);
1952 u32 flags = arg;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001953
1954 switch (cmd) {
1955 case PERF_COUNTER_IOC_ENABLE:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001956 func = perf_counter_enable;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001957 break;
1958 case PERF_COUNTER_IOC_DISABLE:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001959 func = perf_counter_disable;
Peter Zijlstra79f14642009-04-06 11:45:07 +02001960 break;
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001961 case PERF_COUNTER_IOC_RESET:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001962 func = perf_counter_reset;
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001963 break;
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001964
1965 case PERF_COUNTER_IOC_REFRESH:
1966 return perf_counter_refresh(counter, arg);
Peter Zijlstra08247e32009-06-02 16:46:57 +02001967
1968 case PERF_COUNTER_IOC_PERIOD:
1969 return perf_counter_period(counter, (u64 __user *)arg);
1970
Paul Mackerrasd859e292009-01-17 18:10:22 +11001971 default:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001972 return -ENOTTY;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001973 }
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001974
1975 if (flags & PERF_IOC_FLAG_GROUP)
1976 perf_counter_for_each(counter, func);
1977 else
1978 perf_counter_for_each_child(counter, func);
1979
1980 return 0;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001981}
1982
Peter Zijlstra771d7cd2009-05-25 14:45:26 +02001983int perf_counter_task_enable(void)
1984{
1985 struct perf_counter *counter;
1986
1987 mutex_lock(&current->perf_counter_mutex);
1988 list_for_each_entry(counter, &current->perf_counter_list, owner_entry)
1989 perf_counter_for_each_child(counter, perf_counter_enable);
1990 mutex_unlock(&current->perf_counter_mutex);
1991
1992 return 0;
1993}
1994
1995int perf_counter_task_disable(void)
1996{
1997 struct perf_counter *counter;
1998
1999 mutex_lock(&current->perf_counter_mutex);
2000 list_for_each_entry(counter, &current->perf_counter_list, owner_entry)
2001 perf_counter_for_each_child(counter, perf_counter_disable);
2002 mutex_unlock(&current->perf_counter_mutex);
2003
2004 return 0;
2005}
2006
Peter Zijlstra194002b2009-06-22 16:35:24 +02002007static int perf_counter_index(struct perf_counter *counter)
2008{
2009 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
2010 return 0;
2011
2012 return counter->hw.idx + 1 - PERF_COUNTER_INDEX_OFFSET;
2013}
2014
Peter Zijlstra38ff6672009-03-30 19:07:03 +02002015/*
2016 * Callers need to ensure there can be no nesting of this function, otherwise
2017 * the seqlock logic goes bad. We can not serialize this because the arch
2018 * code calls this from NMI context.
2019 */
2020void perf_counter_update_userpage(struct perf_counter *counter)
Paul Mackerras37d81822009-03-23 18:22:08 +01002021{
Peter Zijlstra38ff6672009-03-30 19:07:03 +02002022 struct perf_counter_mmap_page *userpg;
Ingo Molnar22a4f652009-06-01 10:13:37 +02002023 struct perf_mmap_data *data;
Peter Zijlstra38ff6672009-03-30 19:07:03 +02002024
2025 rcu_read_lock();
2026 data = rcu_dereference(counter->data);
2027 if (!data)
2028 goto unlock;
2029
2030 userpg = data->user_page;
Paul Mackerras37d81822009-03-23 18:22:08 +01002031
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002032 /*
2033 * Disable preemption so as to not let the corresponding user-space
2034 * spin too long if we get preempted.
2035 */
2036 preempt_disable();
Paul Mackerras37d81822009-03-23 18:22:08 +01002037 ++userpg->lock;
Peter Zijlstra92f22a32009-04-02 11:12:04 +02002038 barrier();
Peter Zijlstra194002b2009-06-22 16:35:24 +02002039 userpg->index = perf_counter_index(counter);
Paul Mackerras37d81822009-03-23 18:22:08 +01002040 userpg->offset = atomic64_read(&counter->count);
2041 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
2042 userpg->offset -= atomic64_read(&counter->hw.prev_count);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002043
Peter Zijlstra7f8b4e42009-06-22 14:34:35 +02002044 userpg->time_enabled = counter->total_time_enabled +
2045 atomic64_read(&counter->child_total_time_enabled);
2046
2047 userpg->time_running = counter->total_time_running +
2048 atomic64_read(&counter->child_total_time_running);
2049
Peter Zijlstra92f22a32009-04-02 11:12:04 +02002050 barrier();
Paul Mackerras37d81822009-03-23 18:22:08 +01002051 ++userpg->lock;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002052 preempt_enable();
Peter Zijlstra38ff6672009-03-30 19:07:03 +02002053unlock:
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002054 rcu_read_unlock();
Paul Mackerras37d81822009-03-23 18:22:08 +01002055}
2056
2057static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
2058{
2059 struct perf_counter *counter = vma->vm_file->private_data;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002060 struct perf_mmap_data *data;
2061 int ret = VM_FAULT_SIGBUS;
Paul Mackerras37d81822009-03-23 18:22:08 +01002062
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002063 if (vmf->flags & FAULT_FLAG_MKWRITE) {
2064 if (vmf->pgoff == 0)
2065 ret = 0;
2066 return ret;
2067 }
2068
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002069 rcu_read_lock();
2070 data = rcu_dereference(counter->data);
2071 if (!data)
2072 goto unlock;
Paul Mackerras37d81822009-03-23 18:22:08 +01002073
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002074 if (vmf->pgoff == 0) {
2075 vmf->page = virt_to_page(data->user_page);
2076 } else {
2077 int nr = vmf->pgoff - 1;
2078
2079 if ((unsigned)nr > data->nr_pages)
2080 goto unlock;
2081
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002082 if (vmf->flags & FAULT_FLAG_WRITE)
2083 goto unlock;
2084
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002085 vmf->page = virt_to_page(data->data_pages[nr]);
2086 }
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002087
Paul Mackerras37d81822009-03-23 18:22:08 +01002088 get_page(vmf->page);
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002089 vmf->page->mapping = vma->vm_file->f_mapping;
2090 vmf->page->index = vmf->pgoff;
2091
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002092 ret = 0;
2093unlock:
2094 rcu_read_unlock();
2095
2096 return ret;
2097}
2098
2099static int perf_mmap_data_alloc(struct perf_counter *counter, int nr_pages)
2100{
2101 struct perf_mmap_data *data;
2102 unsigned long size;
2103 int i;
2104
2105 WARN_ON(atomic_read(&counter->mmap_count));
2106
2107 size = sizeof(struct perf_mmap_data);
2108 size += nr_pages * sizeof(void *);
2109
2110 data = kzalloc(size, GFP_KERNEL);
2111 if (!data)
2112 goto fail;
2113
2114 data->user_page = (void *)get_zeroed_page(GFP_KERNEL);
2115 if (!data->user_page)
2116 goto fail_user_page;
2117
2118 for (i = 0; i < nr_pages; i++) {
2119 data->data_pages[i] = (void *)get_zeroed_page(GFP_KERNEL);
2120 if (!data->data_pages[i])
2121 goto fail_data_pages;
2122 }
2123
2124 data->nr_pages = nr_pages;
Peter Zijlstra22c15582009-05-05 17:50:25 +02002125 atomic_set(&data->lock, -1);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002126
2127 rcu_assign_pointer(counter->data, data);
2128
Paul Mackerras37d81822009-03-23 18:22:08 +01002129 return 0;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002130
2131fail_data_pages:
2132 for (i--; i >= 0; i--)
2133 free_page((unsigned long)data->data_pages[i]);
2134
2135 free_page((unsigned long)data->user_page);
2136
2137fail_user_page:
2138 kfree(data);
2139
2140fail:
2141 return -ENOMEM;
2142}
2143
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002144static void perf_mmap_free_page(unsigned long addr)
2145{
Kevin Cernekee5bfd7562009-07-05 12:08:19 -07002146 struct page *page = virt_to_page((void *)addr);
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002147
2148 page->mapping = NULL;
2149 __free_page(page);
2150}
2151
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002152static void __perf_mmap_data_free(struct rcu_head *rcu_head)
2153{
Ingo Molnar22a4f652009-06-01 10:13:37 +02002154 struct perf_mmap_data *data;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002155 int i;
2156
Ingo Molnar22a4f652009-06-01 10:13:37 +02002157 data = container_of(rcu_head, struct perf_mmap_data, rcu_head);
2158
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002159 perf_mmap_free_page((unsigned long)data->user_page);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002160 for (i = 0; i < data->nr_pages; i++)
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002161 perf_mmap_free_page((unsigned long)data->data_pages[i]);
2162
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002163 kfree(data);
2164}
2165
2166static void perf_mmap_data_free(struct perf_counter *counter)
2167{
2168 struct perf_mmap_data *data = counter->data;
2169
2170 WARN_ON(atomic_read(&counter->mmap_count));
2171
2172 rcu_assign_pointer(counter->data, NULL);
2173 call_rcu(&data->rcu_head, __perf_mmap_data_free);
2174}
2175
2176static void perf_mmap_open(struct vm_area_struct *vma)
2177{
2178 struct perf_counter *counter = vma->vm_file->private_data;
2179
2180 atomic_inc(&counter->mmap_count);
2181}
2182
2183static void perf_mmap_close(struct vm_area_struct *vma)
2184{
2185 struct perf_counter *counter = vma->vm_file->private_data;
2186
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10002187 WARN_ON_ONCE(counter->ctx->parent_ctx);
Ingo Molnar22a4f652009-06-01 10:13:37 +02002188 if (atomic_dec_and_mutex_lock(&counter->mmap_count, &counter->mmap_mutex)) {
Peter Zijlstra789f90f2009-05-15 15:19:27 +02002189 struct user_struct *user = current_user();
2190
2191 atomic_long_sub(counter->data->nr_pages + 1, &user->locked_vm);
Peter Zijlstrac5078f72009-05-05 17:50:24 +02002192 vma->vm_mm->locked_vm -= counter->data->nr_locked;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002193 perf_mmap_data_free(counter);
2194 mutex_unlock(&counter->mmap_mutex);
2195 }
Paul Mackerras37d81822009-03-23 18:22:08 +01002196}
2197
2198static struct vm_operations_struct perf_mmap_vmops = {
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002199 .open = perf_mmap_open,
2200 .close = perf_mmap_close,
2201 .fault = perf_mmap_fault,
2202 .page_mkwrite = perf_mmap_fault,
Paul Mackerras37d81822009-03-23 18:22:08 +01002203};
2204
2205static int perf_mmap(struct file *file, struct vm_area_struct *vma)
2206{
2207 struct perf_counter *counter = file->private_data;
Ingo Molnar22a4f652009-06-01 10:13:37 +02002208 unsigned long user_locked, user_lock_limit;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02002209 struct user_struct *user = current_user();
Ingo Molnar22a4f652009-06-01 10:13:37 +02002210 unsigned long locked, lock_limit;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002211 unsigned long vma_size;
2212 unsigned long nr_pages;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02002213 long user_extra, extra;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002214 int ret = 0;
Paul Mackerras37d81822009-03-23 18:22:08 +01002215
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002216 if (!(vma->vm_flags & VM_SHARED))
Paul Mackerras37d81822009-03-23 18:22:08 +01002217 return -EINVAL;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002218
2219 vma_size = vma->vm_end - vma->vm_start;
2220 nr_pages = (vma_size / PAGE_SIZE) - 1;
2221
Peter Zijlstra7730d862009-03-25 12:48:31 +01002222 /*
2223 * If we have data pages ensure they're a power-of-two number, so we
2224 * can do bitmasks instead of modulo.
2225 */
2226 if (nr_pages != 0 && !is_power_of_2(nr_pages))
Paul Mackerras37d81822009-03-23 18:22:08 +01002227 return -EINVAL;
2228
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002229 if (vma_size != PAGE_SIZE * (1 + nr_pages))
Paul Mackerras37d81822009-03-23 18:22:08 +01002230 return -EINVAL;
2231
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002232 if (vma->vm_pgoff != 0)
2233 return -EINVAL;
Paul Mackerras37d81822009-03-23 18:22:08 +01002234
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10002235 WARN_ON_ONCE(counter->ctx->parent_ctx);
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02002236 mutex_lock(&counter->mmap_mutex);
2237 if (atomic_inc_not_zero(&counter->mmap_count)) {
2238 if (nr_pages != counter->data->nr_pages)
2239 ret = -EINVAL;
2240 goto unlock;
2241 }
2242
Peter Zijlstra789f90f2009-05-15 15:19:27 +02002243 user_extra = nr_pages + 1;
2244 user_lock_limit = sysctl_perf_counter_mlock >> (PAGE_SHIFT - 10);
Ingo Molnara3862d32009-05-24 09:02:37 +02002245
2246 /*
2247 * Increase the limit linearly with more CPUs:
2248 */
2249 user_lock_limit *= num_online_cpus();
2250
Peter Zijlstra789f90f2009-05-15 15:19:27 +02002251 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
Peter Zijlstrac5078f72009-05-05 17:50:24 +02002252
Peter Zijlstra789f90f2009-05-15 15:19:27 +02002253 extra = 0;
2254 if (user_locked > user_lock_limit)
2255 extra = user_locked - user_lock_limit;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002256
2257 lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
2258 lock_limit >>= PAGE_SHIFT;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02002259 locked = vma->vm_mm->locked_vm + extra;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002260
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02002261 if ((locked > lock_limit) && !capable(CAP_IPC_LOCK)) {
2262 ret = -EPERM;
2263 goto unlock;
2264 }
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002265
2266 WARN_ON(counter->data);
2267 ret = perf_mmap_data_alloc(counter, nr_pages);
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02002268 if (ret)
2269 goto unlock;
2270
2271 atomic_set(&counter->mmap_count, 1);
Peter Zijlstra789f90f2009-05-15 15:19:27 +02002272 atomic_long_add(user_extra, &user->locked_vm);
Peter Zijlstrac5078f72009-05-05 17:50:24 +02002273 vma->vm_mm->locked_vm += extra;
2274 counter->data->nr_locked = extra;
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002275 if (vma->vm_flags & VM_WRITE)
2276 counter->data->writable = 1;
2277
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02002278unlock:
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002279 mutex_unlock(&counter->mmap_mutex);
Paul Mackerras37d81822009-03-23 18:22:08 +01002280
Paul Mackerras37d81822009-03-23 18:22:08 +01002281 vma->vm_flags |= VM_RESERVED;
2282 vma->vm_ops = &perf_mmap_vmops;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002283
2284 return ret;
Paul Mackerras37d81822009-03-23 18:22:08 +01002285}
2286
Peter Zijlstra3c446b32009-04-06 11:45:01 +02002287static int perf_fasync(int fd, struct file *filp, int on)
2288{
Peter Zijlstra3c446b32009-04-06 11:45:01 +02002289 struct inode *inode = filp->f_path.dentry->d_inode;
Ingo Molnar22a4f652009-06-01 10:13:37 +02002290 struct perf_counter *counter = filp->private_data;
Peter Zijlstra3c446b32009-04-06 11:45:01 +02002291 int retval;
2292
2293 mutex_lock(&inode->i_mutex);
2294 retval = fasync_helper(fd, filp, on, &counter->fasync);
2295 mutex_unlock(&inode->i_mutex);
2296
2297 if (retval < 0)
2298 return retval;
2299
2300 return 0;
2301}
2302
Thomas Gleixner0793a612008-12-04 20:12:29 +01002303static const struct file_operations perf_fops = {
2304 .release = perf_release,
2305 .read = perf_read,
2306 .poll = perf_poll,
Paul Mackerrasd859e292009-01-17 18:10:22 +11002307 .unlocked_ioctl = perf_ioctl,
2308 .compat_ioctl = perf_ioctl,
Paul Mackerras37d81822009-03-23 18:22:08 +01002309 .mmap = perf_mmap,
Peter Zijlstra3c446b32009-04-06 11:45:01 +02002310 .fasync = perf_fasync,
Thomas Gleixner0793a612008-12-04 20:12:29 +01002311};
2312
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002313/*
Peter Zijlstra925d5192009-03-30 19:07:02 +02002314 * Perf counter wakeup
2315 *
2316 * If there's data, ensure we set the poll() state and publish everything
2317 * to user-space before waking everybody up.
2318 */
2319
2320void perf_counter_wakeup(struct perf_counter *counter)
2321{
Peter Zijlstra925d5192009-03-30 19:07:02 +02002322 wake_up_all(&counter->waitq);
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002323
2324 if (counter->pending_kill) {
2325 kill_fasync(&counter->fasync, SIGIO, counter->pending_kill);
2326 counter->pending_kill = 0;
2327 }
Peter Zijlstra925d5192009-03-30 19:07:02 +02002328}
2329
2330/*
2331 * Pending wakeups
2332 *
2333 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
2334 *
2335 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
2336 * single linked list and use cmpxchg() to add entries lockless.
2337 */
2338
Peter Zijlstra79f14642009-04-06 11:45:07 +02002339static void perf_pending_counter(struct perf_pending_entry *entry)
2340{
2341 struct perf_counter *counter = container_of(entry,
2342 struct perf_counter, pending);
2343
2344 if (counter->pending_disable) {
2345 counter->pending_disable = 0;
2346 perf_counter_disable(counter);
2347 }
2348
2349 if (counter->pending_wakeup) {
2350 counter->pending_wakeup = 0;
2351 perf_counter_wakeup(counter);
2352 }
2353}
2354
Peter Zijlstra671dec52009-04-06 11:45:02 +02002355#define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
Peter Zijlstra925d5192009-03-30 19:07:02 +02002356
Peter Zijlstra671dec52009-04-06 11:45:02 +02002357static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = {
Peter Zijlstra925d5192009-03-30 19:07:02 +02002358 PENDING_TAIL,
2359};
2360
Peter Zijlstra671dec52009-04-06 11:45:02 +02002361static void perf_pending_queue(struct perf_pending_entry *entry,
2362 void (*func)(struct perf_pending_entry *))
Peter Zijlstra925d5192009-03-30 19:07:02 +02002363{
Peter Zijlstra671dec52009-04-06 11:45:02 +02002364 struct perf_pending_entry **head;
Peter Zijlstra925d5192009-03-30 19:07:02 +02002365
Peter Zijlstra671dec52009-04-06 11:45:02 +02002366 if (cmpxchg(&entry->next, NULL, PENDING_TAIL) != NULL)
Peter Zijlstra925d5192009-03-30 19:07:02 +02002367 return;
2368
Peter Zijlstra671dec52009-04-06 11:45:02 +02002369 entry->func = func;
2370
2371 head = &get_cpu_var(perf_pending_head);
Peter Zijlstra925d5192009-03-30 19:07:02 +02002372
2373 do {
Peter Zijlstra671dec52009-04-06 11:45:02 +02002374 entry->next = *head;
2375 } while (cmpxchg(head, entry->next, entry) != entry->next);
Peter Zijlstra925d5192009-03-30 19:07:02 +02002376
2377 set_perf_counter_pending();
2378
Peter Zijlstra671dec52009-04-06 11:45:02 +02002379 put_cpu_var(perf_pending_head);
Peter Zijlstra925d5192009-03-30 19:07:02 +02002380}
2381
2382static int __perf_pending_run(void)
2383{
Peter Zijlstra671dec52009-04-06 11:45:02 +02002384 struct perf_pending_entry *list;
Peter Zijlstra925d5192009-03-30 19:07:02 +02002385 int nr = 0;
2386
Peter Zijlstra671dec52009-04-06 11:45:02 +02002387 list = xchg(&__get_cpu_var(perf_pending_head), PENDING_TAIL);
Peter Zijlstra925d5192009-03-30 19:07:02 +02002388 while (list != PENDING_TAIL) {
Peter Zijlstra671dec52009-04-06 11:45:02 +02002389 void (*func)(struct perf_pending_entry *);
2390 struct perf_pending_entry *entry = list;
Peter Zijlstra925d5192009-03-30 19:07:02 +02002391
2392 list = list->next;
2393
Peter Zijlstra671dec52009-04-06 11:45:02 +02002394 func = entry->func;
2395 entry->next = NULL;
Peter Zijlstra925d5192009-03-30 19:07:02 +02002396 /*
2397 * Ensure we observe the unqueue before we issue the wakeup,
2398 * so that we won't be waiting forever.
2399 * -- see perf_not_pending().
2400 */
2401 smp_wmb();
2402
Peter Zijlstra671dec52009-04-06 11:45:02 +02002403 func(entry);
Peter Zijlstra925d5192009-03-30 19:07:02 +02002404 nr++;
2405 }
2406
2407 return nr;
2408}
2409
2410static inline int perf_not_pending(struct perf_counter *counter)
2411{
2412 /*
2413 * If we flush on whatever cpu we run, there is a chance we don't
2414 * need to wait.
2415 */
2416 get_cpu();
2417 __perf_pending_run();
2418 put_cpu();
2419
2420 /*
2421 * Ensure we see the proper queue state before going to sleep
2422 * so that we do not miss the wakeup. -- see perf_pending_handle()
2423 */
2424 smp_rmb();
Peter Zijlstra671dec52009-04-06 11:45:02 +02002425 return counter->pending.next == NULL;
Peter Zijlstra925d5192009-03-30 19:07:02 +02002426}
2427
2428static void perf_pending_sync(struct perf_counter *counter)
2429{
2430 wait_event(counter->waitq, perf_not_pending(counter));
2431}
2432
2433void perf_counter_do_pending(void)
2434{
2435 __perf_pending_run();
2436}
2437
2438/*
Peter Zijlstra394ee072009-03-30 19:07:14 +02002439 * Callchain support -- arch specific
2440 */
2441
Peter Zijlstra9c03d882009-04-06 11:45:00 +02002442__weak struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
Peter Zijlstra394ee072009-03-30 19:07:14 +02002443{
2444 return NULL;
2445}
2446
2447/*
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002448 * Output
2449 */
2450
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002451struct perf_output_handle {
2452 struct perf_counter *counter;
2453 struct perf_mmap_data *data;
Peter Zijlstra8e3747c2009-06-02 16:16:02 +02002454 unsigned long head;
2455 unsigned long offset;
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002456 int nmi;
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002457 int sample;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002458 int locked;
2459 unsigned long flags;
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002460};
2461
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002462static bool perf_output_space(struct perf_mmap_data *data,
2463 unsigned int offset, unsigned int head)
2464{
2465 unsigned long tail;
2466 unsigned long mask;
2467
2468 if (!data->writable)
2469 return true;
2470
2471 mask = (data->nr_pages << PAGE_SHIFT) - 1;
2472 /*
2473 * Userspace could choose to issue a mb() before updating the tail
2474 * pointer. So that all reads will be completed before the write is
2475 * issued.
2476 */
2477 tail = ACCESS_ONCE(data->user_page->data_tail);
2478 smp_rmb();
2479
2480 offset = (offset - tail) & mask;
2481 head = (head - tail) & mask;
2482
2483 if ((int)(head - offset) < 0)
2484 return false;
2485
2486 return true;
2487}
2488
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002489static void perf_output_wakeup(struct perf_output_handle *handle)
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002490{
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002491 atomic_set(&handle->data->poll, POLL_IN);
2492
Peter Zijlstra671dec52009-04-06 11:45:02 +02002493 if (handle->nmi) {
Peter Zijlstra79f14642009-04-06 11:45:07 +02002494 handle->counter->pending_wakeup = 1;
Peter Zijlstra671dec52009-04-06 11:45:02 +02002495 perf_pending_queue(&handle->counter->pending,
Peter Zijlstra79f14642009-04-06 11:45:07 +02002496 perf_pending_counter);
Peter Zijlstra671dec52009-04-06 11:45:02 +02002497 } else
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002498 perf_counter_wakeup(handle->counter);
2499}
2500
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002501/*
2502 * Curious locking construct.
2503 *
2504 * We need to ensure a later event doesn't publish a head when a former
2505 * event isn't done writing. However since we need to deal with NMIs we
2506 * cannot fully serialize things.
2507 *
2508 * What we do is serialize between CPUs so we only have to deal with NMI
2509 * nesting on a single CPU.
2510 *
2511 * We only publish the head (and generate a wakeup) when the outer-most
2512 * event completes.
2513 */
2514static void perf_output_lock(struct perf_output_handle *handle)
2515{
2516 struct perf_mmap_data *data = handle->data;
2517 int cpu;
2518
2519 handle->locked = 0;
2520
2521 local_irq_save(handle->flags);
2522 cpu = smp_processor_id();
2523
2524 if (in_nmi() && atomic_read(&data->lock) == cpu)
2525 return;
2526
Peter Zijlstra22c15582009-05-05 17:50:25 +02002527 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002528 cpu_relax();
2529
2530 handle->locked = 1;
2531}
2532
2533static void perf_output_unlock(struct perf_output_handle *handle)
2534{
2535 struct perf_mmap_data *data = handle->data;
Peter Zijlstra8e3747c2009-06-02 16:16:02 +02002536 unsigned long head;
2537 int cpu;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002538
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002539 data->done_head = data->head;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002540
2541 if (!handle->locked)
2542 goto out;
2543
2544again:
2545 /*
2546 * The xchg implies a full barrier that ensures all writes are done
2547 * before we publish the new head, matched by a rmb() in userspace when
2548 * reading this position.
2549 */
Peter Zijlstra8e3747c2009-06-02 16:16:02 +02002550 while ((head = atomic_long_xchg(&data->done_head, 0)))
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002551 data->user_page->data_head = head;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002552
2553 /*
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002554 * NMI can happen here, which means we can miss a done_head update.
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002555 */
2556
Peter Zijlstra22c15582009-05-05 17:50:25 +02002557 cpu = atomic_xchg(&data->lock, -1);
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002558 WARN_ON_ONCE(cpu != smp_processor_id());
2559
2560 /*
2561 * Therefore we have to validate we did not indeed do so.
2562 */
Peter Zijlstra8e3747c2009-06-02 16:16:02 +02002563 if (unlikely(atomic_long_read(&data->done_head))) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002564 /*
2565 * Since we had it locked, we can lock it again.
2566 */
Peter Zijlstra22c15582009-05-05 17:50:25 +02002567 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002568 cpu_relax();
2569
2570 goto again;
2571 }
2572
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002573 if (atomic_xchg(&data->wakeup, 0))
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002574 perf_output_wakeup(handle);
2575out:
2576 local_irq_restore(handle->flags);
2577}
2578
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002579static void perf_output_copy(struct perf_output_handle *handle,
Peter Zijlstra089dd792009-06-05 14:04:55 +02002580 const void *buf, unsigned int len)
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002581{
2582 unsigned int pages_mask;
2583 unsigned int offset;
2584 unsigned int size;
2585 void **pages;
2586
2587 offset = handle->offset;
2588 pages_mask = handle->data->nr_pages - 1;
2589 pages = handle->data->data_pages;
2590
2591 do {
2592 unsigned int page_offset;
2593 int nr;
2594
2595 nr = (offset >> PAGE_SHIFT) & pages_mask;
2596 page_offset = offset & (PAGE_SIZE - 1);
2597 size = min_t(unsigned int, PAGE_SIZE - page_offset, len);
2598
2599 memcpy(pages[nr] + page_offset, buf, size);
2600
2601 len -= size;
2602 buf += size;
2603 offset += size;
2604 } while (len);
2605
2606 handle->offset = offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01002607
Peter Zijlstra53020fe2009-05-13 21:26:19 +02002608 /*
2609 * Check we didn't copy past our reservation window, taking the
2610 * possible unsigned int wrap into account.
2611 */
Peter Zijlstra8e3747c2009-06-02 16:16:02 +02002612 WARN_ON_ONCE(((long)(handle->head - handle->offset)) < 0);
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002613}
2614
Peter Zijlstra5c148192009-03-25 12:30:23 +01002615#define perf_output_put(handle, x) \
2616 perf_output_copy((handle), &(x), sizeof(x))
2617
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002618static int perf_output_begin(struct perf_output_handle *handle,
2619 struct perf_counter *counter, unsigned int size,
2620 int nmi, int sample)
2621{
2622 struct perf_mmap_data *data;
2623 unsigned int offset, head;
2624 int have_lost;
2625 struct {
2626 struct perf_event_header header;
2627 u64 id;
2628 u64 lost;
2629 } lost_event;
2630
2631 /*
2632 * For inherited counters we send all the output towards the parent.
2633 */
2634 if (counter->parent)
2635 counter = counter->parent;
2636
2637 rcu_read_lock();
2638 data = rcu_dereference(counter->data);
2639 if (!data)
2640 goto out;
2641
2642 handle->data = data;
2643 handle->counter = counter;
2644 handle->nmi = nmi;
2645 handle->sample = sample;
2646
2647 if (!data->nr_pages)
2648 goto fail;
2649
2650 have_lost = atomic_read(&data->lost);
2651 if (have_lost)
2652 size += sizeof(lost_event);
2653
2654 perf_output_lock(handle);
2655
2656 do {
2657 offset = head = atomic_long_read(&data->head);
2658 head += size;
2659 if (unlikely(!perf_output_space(data, offset, head)))
2660 goto fail;
2661 } while (atomic_long_cmpxchg(&data->head, offset, head) != offset);
2662
2663 handle->offset = offset;
2664 handle->head = head;
2665
2666 if ((offset >> PAGE_SHIFT) != (head >> PAGE_SHIFT))
2667 atomic_set(&data->wakeup, 1);
2668
2669 if (have_lost) {
2670 lost_event.header.type = PERF_EVENT_LOST;
2671 lost_event.header.misc = 0;
2672 lost_event.header.size = sizeof(lost_event);
2673 lost_event.id = counter->id;
2674 lost_event.lost = atomic_xchg(&data->lost, 0);
2675
2676 perf_output_put(handle, lost_event);
2677 }
2678
2679 return 0;
2680
2681fail:
2682 atomic_inc(&data->lost);
2683 perf_output_unlock(handle);
2684out:
2685 rcu_read_unlock();
2686
2687 return -ENOSPC;
2688}
2689
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002690static void perf_output_end(struct perf_output_handle *handle)
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002691{
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002692 struct perf_counter *counter = handle->counter;
2693 struct perf_mmap_data *data = handle->data;
2694
Peter Zijlstra0d486962009-06-02 19:22:16 +02002695 int wakeup_events = counter->attr.wakeup_events;
Peter Zijlstrac4578102009-04-02 11:12:01 +02002696
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002697 if (handle->sample && wakeup_events) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002698 int events = atomic_inc_return(&data->events);
Peter Zijlstrac4578102009-04-02 11:12:01 +02002699 if (events >= wakeup_events) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002700 atomic_sub(wakeup_events, &data->events);
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002701 atomic_set(&data->wakeup, 1);
Peter Zijlstrac4578102009-04-02 11:12:01 +02002702 }
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002703 }
2704
2705 perf_output_unlock(handle);
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002706 rcu_read_unlock();
2707}
2708
Peter Zijlstra709e50c2009-06-02 14:13:15 +02002709static u32 perf_counter_pid(struct perf_counter *counter, struct task_struct *p)
2710{
2711 /*
2712 * only top level counters have the pid namespace they were created in
2713 */
2714 if (counter->parent)
2715 counter = counter->parent;
2716
2717 return task_tgid_nr_ns(p, counter->ns);
2718}
2719
2720static u32 perf_counter_tid(struct perf_counter *counter, struct task_struct *p)
2721{
2722 /*
2723 * only top level counters have the pid namespace they were created in
2724 */
2725 if (counter->parent)
2726 counter = counter->parent;
2727
2728 return task_pid_nr_ns(p, counter->ns);
2729}
2730
Peter Zijlstra3dab77f2009-08-13 11:47:53 +02002731static void perf_output_read_one(struct perf_output_handle *handle,
2732 struct perf_counter *counter)
2733{
2734 u64 read_format = counter->attr.read_format;
2735 u64 values[4];
2736 int n = 0;
2737
2738 values[n++] = atomic64_read(&counter->count);
2739 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
2740 values[n++] = counter->total_time_enabled +
2741 atomic64_read(&counter->child_total_time_enabled);
2742 }
2743 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
2744 values[n++] = counter->total_time_running +
2745 atomic64_read(&counter->child_total_time_running);
2746 }
2747 if (read_format & PERF_FORMAT_ID)
2748 values[n++] = primary_counter_id(counter);
2749
2750 perf_output_copy(handle, values, n * sizeof(u64));
2751}
2752
2753/*
2754 * XXX PERF_FORMAT_GROUP vs inherited counters seems difficult.
2755 */
2756static void perf_output_read_group(struct perf_output_handle *handle,
2757 struct perf_counter *counter)
2758{
2759 struct perf_counter *leader = counter->group_leader, *sub;
2760 u64 read_format = counter->attr.read_format;
2761 u64 values[5];
2762 int n = 0;
2763
2764 values[n++] = 1 + leader->nr_siblings;
2765
2766 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2767 values[n++] = leader->total_time_enabled;
2768
2769 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2770 values[n++] = leader->total_time_running;
2771
2772 if (leader != counter)
2773 leader->pmu->read(leader);
2774
2775 values[n++] = atomic64_read(&leader->count);
2776 if (read_format & PERF_FORMAT_ID)
2777 values[n++] = primary_counter_id(leader);
2778
2779 perf_output_copy(handle, values, n * sizeof(u64));
2780
2781 list_for_each_entry(sub, &leader->sibling_list, list_entry) {
2782 n = 0;
2783
2784 if (sub != counter)
2785 sub->pmu->read(sub);
2786
2787 values[n++] = atomic64_read(&sub->count);
2788 if (read_format & PERF_FORMAT_ID)
2789 values[n++] = primary_counter_id(sub);
2790
2791 perf_output_copy(handle, values, n * sizeof(u64));
2792 }
2793}
2794
2795static void perf_output_read(struct perf_output_handle *handle,
2796 struct perf_counter *counter)
2797{
2798 if (counter->attr.read_format & PERF_FORMAT_GROUP)
2799 perf_output_read_group(handle, counter);
2800 else
2801 perf_output_read_one(handle, counter);
2802}
2803
Ingo Molnar28402972009-08-13 10:13:22 +02002804void perf_counter_output(struct perf_counter *counter, int nmi,
Peter Zijlstradf1a1322009-06-10 21:02:22 +02002805 struct perf_sample_data *data)
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002806{
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002807 int ret;
Peter Zijlstra0d486962009-06-02 19:22:16 +02002808 u64 sample_type = counter->attr.sample_type;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002809 struct perf_output_handle handle;
2810 struct perf_event_header header;
2811 u64 ip;
Peter Zijlstra5c148192009-03-25 12:30:23 +01002812 struct {
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002813 u32 pid, tid;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002814 } tid_entry;
Peter Zijlstra394ee072009-03-30 19:07:14 +02002815 struct perf_callchain_entry *callchain = NULL;
2816 int callchain_size = 0;
Peter Zijlstra339f7c92009-04-06 11:45:06 +02002817 u64 time;
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002818 struct {
2819 u32 cpu, reserved;
2820 } cpu_entry;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002821
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02002822 header.type = PERF_EVENT_SAMPLE;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002823 header.size = sizeof(header);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002824
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02002825 header.misc = 0;
Peter Zijlstradf1a1322009-06-10 21:02:22 +02002826 header.misc |= perf_misc_flags(data->regs);
Peter Zijlstra6fab0192009-04-08 15:01:26 +02002827
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002828 if (sample_type & PERF_SAMPLE_IP) {
Peter Zijlstradf1a1322009-06-10 21:02:22 +02002829 ip = perf_instruction_pointer(data->regs);
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002830 header.size += sizeof(ip);
2831 }
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002832
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002833 if (sample_type & PERF_SAMPLE_TID) {
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002834 /* namespace issues */
Peter Zijlstra709e50c2009-06-02 14:13:15 +02002835 tid_entry.pid = perf_counter_pid(counter, current);
2836 tid_entry.tid = perf_counter_tid(counter, current);
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002837
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002838 header.size += sizeof(tid_entry);
2839 }
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002840
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002841 if (sample_type & PERF_SAMPLE_TIME) {
Peter Zijlstra4d855452009-04-08 15:01:32 +02002842 /*
2843 * Maybe do better on x86 and provide cpu_clock_nmi()
2844 */
2845 time = sched_clock();
2846
Peter Zijlstra4d855452009-04-08 15:01:32 +02002847 header.size += sizeof(u64);
2848 }
2849
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02002850 if (sample_type & PERF_SAMPLE_ADDR)
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002851 header.size += sizeof(u64);
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002852
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02002853 if (sample_type & PERF_SAMPLE_ID)
Peter Zijlstraa85f61a2009-05-08 18:52:23 +02002854 header.size += sizeof(u64);
Peter Zijlstraa85f61a2009-05-08 18:52:23 +02002855
Peter Zijlstra7f453c22009-07-21 13:19:40 +02002856 if (sample_type & PERF_SAMPLE_STREAM_ID)
2857 header.size += sizeof(u64);
2858
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002859 if (sample_type & PERF_SAMPLE_CPU) {
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002860 header.size += sizeof(cpu_entry);
2861
2862 cpu_entry.cpu = raw_smp_processor_id();
Arjan van de Ven0dc3d522009-07-21 00:55:05 -07002863 cpu_entry.reserved = 0;
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002864 }
2865
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02002866 if (sample_type & PERF_SAMPLE_PERIOD)
Peter Zijlstra689802b2009-06-05 15:05:43 +02002867 header.size += sizeof(u64);
Peter Zijlstra689802b2009-06-05 15:05:43 +02002868
Peter Zijlstra3dab77f2009-08-13 11:47:53 +02002869 if (sample_type & PERF_SAMPLE_READ)
2870 header.size += perf_counter_read_size(counter);
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002871
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002872 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
Peter Zijlstradf1a1322009-06-10 21:02:22 +02002873 callchain = perf_callchain(data->regs);
Peter Zijlstra394ee072009-03-30 19:07:14 +02002874
2875 if (callchain) {
Peter Zijlstra9c03d882009-04-06 11:45:00 +02002876 callchain_size = (1 + callchain->nr) * sizeof(u64);
Peter Zijlstra394ee072009-03-30 19:07:14 +02002877 header.size += callchain_size;
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02002878 } else
2879 header.size += sizeof(u64);
Peter Zijlstra394ee072009-03-30 19:07:14 +02002880 }
2881
Frederic Weisbecker3a43ce62009-08-08 04:26:37 +02002882 if (sample_type & PERF_SAMPLE_RAW) {
Peter Zijlstraa0445602009-08-10 11:16:52 +02002883 int size = sizeof(u32);
2884
2885 if (data->raw)
2886 size += data->raw->size;
2887 else
2888 size += sizeof(u32);
2889
2890 WARN_ON_ONCE(size & (sizeof(u64)-1));
2891 header.size += size;
Frederic Weisbeckerf413cdb2009-08-07 01:25:54 +02002892 }
2893
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002894 ret = perf_output_begin(&handle, counter, header.size, nmi, 1);
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002895 if (ret)
2896 return;
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002897
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002898 perf_output_put(&handle, header);
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002899
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002900 if (sample_type & PERF_SAMPLE_IP)
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002901 perf_output_put(&handle, ip);
2902
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002903 if (sample_type & PERF_SAMPLE_TID)
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002904 perf_output_put(&handle, tid_entry);
2905
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002906 if (sample_type & PERF_SAMPLE_TIME)
Peter Zijlstra4d855452009-04-08 15:01:32 +02002907 perf_output_put(&handle, time);
2908
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002909 if (sample_type & PERF_SAMPLE_ADDR)
Peter Zijlstradf1a1322009-06-10 21:02:22 +02002910 perf_output_put(&handle, data->addr);
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002911
Peter Zijlstra7f453c22009-07-21 13:19:40 +02002912 if (sample_type & PERF_SAMPLE_ID) {
2913 u64 id = primary_counter_id(counter);
2914
2915 perf_output_put(&handle, id);
2916 }
2917
2918 if (sample_type & PERF_SAMPLE_STREAM_ID)
Peter Zijlstraac4bcf82009-06-05 14:44:52 +02002919 perf_output_put(&handle, counter->id);
Peter Zijlstraa85f61a2009-05-08 18:52:23 +02002920
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002921 if (sample_type & PERF_SAMPLE_CPU)
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002922 perf_output_put(&handle, cpu_entry);
2923
Peter Zijlstra689802b2009-06-05 15:05:43 +02002924 if (sample_type & PERF_SAMPLE_PERIOD)
Peter Zijlstra9e350de2009-06-10 21:34:59 +02002925 perf_output_put(&handle, data->period);
Peter Zijlstra689802b2009-06-05 15:05:43 +02002926
Peter Zijlstra3dab77f2009-08-13 11:47:53 +02002927 if (sample_type & PERF_SAMPLE_READ)
2928 perf_output_read(&handle, counter);
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002929
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02002930 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
2931 if (callchain)
2932 perf_output_copy(&handle, callchain, callchain_size);
2933 else {
2934 u64 nr = 0;
2935 perf_output_put(&handle, nr);
2936 }
2937 }
Peter Zijlstra394ee072009-03-30 19:07:14 +02002938
Peter Zijlstraa0445602009-08-10 11:16:52 +02002939 if (sample_type & PERF_SAMPLE_RAW) {
2940 if (data->raw) {
2941 perf_output_put(&handle, data->raw->size);
2942 perf_output_copy(&handle, data->raw->data, data->raw->size);
2943 } else {
2944 struct {
2945 u32 size;
2946 u32 data;
2947 } raw = {
2948 .size = sizeof(u32),
2949 .data = 0,
2950 };
2951 perf_output_put(&handle, raw);
2952 }
2953 }
Frederic Weisbeckerf413cdb2009-08-07 01:25:54 +02002954
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002955 perf_output_end(&handle);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002956}
2957
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002958/*
Peter Zijlstra38b200d2009-06-23 20:13:11 +02002959 * read event
2960 */
2961
2962struct perf_read_event {
2963 struct perf_event_header header;
2964
2965 u32 pid;
2966 u32 tid;
Peter Zijlstra38b200d2009-06-23 20:13:11 +02002967};
2968
2969static void
2970perf_counter_read_event(struct perf_counter *counter,
2971 struct task_struct *task)
2972{
2973 struct perf_output_handle handle;
2974 struct perf_read_event event = {
2975 .header = {
2976 .type = PERF_EVENT_READ,
2977 .misc = 0,
Peter Zijlstra3dab77f2009-08-13 11:47:53 +02002978 .size = sizeof(event) + perf_counter_read_size(counter),
Peter Zijlstra38b200d2009-06-23 20:13:11 +02002979 },
2980 .pid = perf_counter_pid(counter, task),
2981 .tid = perf_counter_tid(counter, task),
Peter Zijlstra38b200d2009-06-23 20:13:11 +02002982 };
Peter Zijlstra3dab77f2009-08-13 11:47:53 +02002983 int ret;
Peter Zijlstra38b200d2009-06-23 20:13:11 +02002984
2985 ret = perf_output_begin(&handle, counter, event.header.size, 0, 0);
2986 if (ret)
2987 return;
2988
Peter Zijlstra3dab77f2009-08-13 11:47:53 +02002989 perf_output_put(&handle, event);
2990 perf_output_read(&handle, counter);
2991
Peter Zijlstra38b200d2009-06-23 20:13:11 +02002992 perf_output_end(&handle);
2993}
2994
2995/*
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02002996 * task tracking -- fork/exit
2997 *
2998 * enabled by: attr.comm | attr.mmap | attr.task
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002999 */
3000
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02003001struct perf_task_event {
Peter Zijlstra3a80b4a2009-08-07 19:49:01 +02003002 struct task_struct *task;
3003 struct perf_counter_context *task_ctx;
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003004
3005 struct {
3006 struct perf_event_header header;
3007
3008 u32 pid;
3009 u32 ppid;
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02003010 u32 tid;
3011 u32 ptid;
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003012 } event;
3013};
3014
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02003015static void perf_counter_task_output(struct perf_counter *counter,
3016 struct perf_task_event *task_event)
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003017{
3018 struct perf_output_handle handle;
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02003019 int size = task_event->event.header.size;
3020 struct task_struct *task = task_event->task;
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003021 int ret = perf_output_begin(&handle, counter, size, 0, 0);
3022
3023 if (ret)
3024 return;
3025
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02003026 task_event->event.pid = perf_counter_pid(counter, task);
3027 task_event->event.ppid = perf_counter_pid(counter, task->real_parent);
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003028
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02003029 task_event->event.tid = perf_counter_tid(counter, task);
3030 task_event->event.ptid = perf_counter_tid(counter, task->real_parent);
3031
3032 perf_output_put(&handle, task_event->event);
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003033 perf_output_end(&handle);
3034}
3035
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02003036static int perf_counter_task_match(struct perf_counter *counter)
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003037{
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02003038 if (counter->attr.comm || counter->attr.mmap || counter->attr.task)
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003039 return 1;
3040
3041 return 0;
3042}
3043
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02003044static void perf_counter_task_ctx(struct perf_counter_context *ctx,
3045 struct perf_task_event *task_event)
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003046{
3047 struct perf_counter *counter;
3048
3049 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
3050 return;
3051
3052 rcu_read_lock();
3053 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02003054 if (perf_counter_task_match(counter))
3055 perf_counter_task_output(counter, task_event);
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003056 }
3057 rcu_read_unlock();
3058}
3059
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02003060static void perf_counter_task_event(struct perf_task_event *task_event)
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003061{
3062 struct perf_cpu_context *cpuctx;
Peter Zijlstra3a80b4a2009-08-07 19:49:01 +02003063 struct perf_counter_context *ctx = task_event->task_ctx;
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003064
3065 cpuctx = &get_cpu_var(perf_cpu_context);
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02003066 perf_counter_task_ctx(&cpuctx->ctx, task_event);
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003067 put_cpu_var(perf_cpu_context);
3068
3069 rcu_read_lock();
Peter Zijlstra3a80b4a2009-08-07 19:49:01 +02003070 if (!ctx)
3071 ctx = rcu_dereference(task_event->task->perf_counter_ctxp);
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003072 if (ctx)
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02003073 perf_counter_task_ctx(ctx, task_event);
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003074 rcu_read_unlock();
3075}
3076
Peter Zijlstra3a80b4a2009-08-07 19:49:01 +02003077static void perf_counter_task(struct task_struct *task,
3078 struct perf_counter_context *task_ctx,
3079 int new)
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02003080{
3081 struct perf_task_event task_event;
3082
3083 if (!atomic_read(&nr_comm_counters) &&
3084 !atomic_read(&nr_mmap_counters) &&
3085 !atomic_read(&nr_task_counters))
3086 return;
3087
3088 task_event = (struct perf_task_event){
Peter Zijlstra3a80b4a2009-08-07 19:49:01 +02003089 .task = task,
3090 .task_ctx = task_ctx,
3091 .event = {
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02003092 .header = {
3093 .type = new ? PERF_EVENT_FORK : PERF_EVENT_EXIT,
3094 .misc = 0,
3095 .size = sizeof(task_event.event),
3096 },
3097 /* .pid */
3098 /* .ppid */
3099 /* .tid */
3100 /* .ptid */
3101 },
3102 };
3103
3104 perf_counter_task_event(&task_event);
3105}
3106
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003107void perf_counter_fork(struct task_struct *task)
3108{
Peter Zijlstra3a80b4a2009-08-07 19:49:01 +02003109 perf_counter_task(task, NULL, 1);
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003110}
3111
3112/*
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02003113 * comm tracking
3114 */
3115
3116struct perf_comm_event {
Ingo Molnar22a4f652009-06-01 10:13:37 +02003117 struct task_struct *task;
3118 char *comm;
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02003119 int comm_size;
3120
3121 struct {
3122 struct perf_event_header header;
3123
3124 u32 pid;
3125 u32 tid;
3126 } event;
3127};
3128
3129static void perf_counter_comm_output(struct perf_counter *counter,
3130 struct perf_comm_event *comm_event)
3131{
3132 struct perf_output_handle handle;
3133 int size = comm_event->event.header.size;
3134 int ret = perf_output_begin(&handle, counter, size, 0, 0);
3135
3136 if (ret)
3137 return;
3138
Peter Zijlstra709e50c2009-06-02 14:13:15 +02003139 comm_event->event.pid = perf_counter_pid(counter, comm_event->task);
3140 comm_event->event.tid = perf_counter_tid(counter, comm_event->task);
3141
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02003142 perf_output_put(&handle, comm_event->event);
3143 perf_output_copy(&handle, comm_event->comm,
3144 comm_event->comm_size);
3145 perf_output_end(&handle);
3146}
3147
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003148static int perf_counter_comm_match(struct perf_counter *counter)
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02003149{
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003150 if (counter->attr.comm)
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02003151 return 1;
3152
3153 return 0;
3154}
3155
3156static void perf_counter_comm_ctx(struct perf_counter_context *ctx,
3157 struct perf_comm_event *comm_event)
3158{
3159 struct perf_counter *counter;
3160
3161 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
3162 return;
3163
3164 rcu_read_lock();
3165 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003166 if (perf_counter_comm_match(counter))
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02003167 perf_counter_comm_output(counter, comm_event);
3168 }
3169 rcu_read_unlock();
3170}
3171
3172static void perf_counter_comm_event(struct perf_comm_event *comm_event)
3173{
3174 struct perf_cpu_context *cpuctx;
Peter Zijlstra665c2142009-05-29 14:51:57 +02003175 struct perf_counter_context *ctx;
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02003176 unsigned int size;
Anton Blanchard413ee3b2009-07-16 15:15:52 +02003177 char comm[TASK_COMM_LEN];
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02003178
Anton Blanchard413ee3b2009-07-16 15:15:52 +02003179 memset(comm, 0, sizeof(comm));
3180 strncpy(comm, comm_event->task->comm, sizeof(comm));
Ingo Molnar888fcee2009-04-09 09:48:22 +02003181 size = ALIGN(strlen(comm)+1, sizeof(u64));
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02003182
3183 comm_event->comm = comm;
3184 comm_event->comm_size = size;
3185
3186 comm_event->event.header.size = sizeof(comm_event->event) + size;
3187
3188 cpuctx = &get_cpu_var(perf_cpu_context);
3189 perf_counter_comm_ctx(&cpuctx->ctx, comm_event);
3190 put_cpu_var(perf_cpu_context);
Peter Zijlstra665c2142009-05-29 14:51:57 +02003191
3192 rcu_read_lock();
3193 /*
3194 * doesn't really matter which of the child contexts the
3195 * events ends up in.
3196 */
3197 ctx = rcu_dereference(current->perf_counter_ctxp);
3198 if (ctx)
3199 perf_counter_comm_ctx(ctx, comm_event);
3200 rcu_read_unlock();
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02003201}
3202
3203void perf_counter_comm(struct task_struct *task)
3204{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02003205 struct perf_comm_event comm_event;
3206
Paul Mackerras57e79862009-06-30 16:07:19 +10003207 if (task->perf_counter_ctxp)
3208 perf_counter_enable_on_exec(task);
3209
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003210 if (!atomic_read(&nr_comm_counters))
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02003211 return;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003212
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02003213 comm_event = (struct perf_comm_event){
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02003214 .task = task,
Peter Zijlstra573402d2009-07-22 11:13:50 +02003215 /* .comm */
3216 /* .comm_size */
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02003217 .event = {
Peter Zijlstra573402d2009-07-22 11:13:50 +02003218 .header = {
3219 .type = PERF_EVENT_COMM,
3220 .misc = 0,
3221 /* .size */
3222 },
3223 /* .pid */
3224 /* .tid */
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02003225 },
3226 };
3227
3228 perf_counter_comm_event(&comm_event);
3229}
3230
3231/*
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003232 * mmap tracking
3233 */
3234
3235struct perf_mmap_event {
Peter Zijlstra089dd792009-06-05 14:04:55 +02003236 struct vm_area_struct *vma;
3237
3238 const char *file_name;
3239 int file_size;
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003240
3241 struct {
3242 struct perf_event_header header;
3243
3244 u32 pid;
3245 u32 tid;
3246 u64 start;
3247 u64 len;
3248 u64 pgoff;
3249 } event;
3250};
3251
3252static void perf_counter_mmap_output(struct perf_counter *counter,
3253 struct perf_mmap_event *mmap_event)
3254{
3255 struct perf_output_handle handle;
3256 int size = mmap_event->event.header.size;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02003257 int ret = perf_output_begin(&handle, counter, size, 0, 0);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003258
3259 if (ret)
3260 return;
3261
Peter Zijlstra709e50c2009-06-02 14:13:15 +02003262 mmap_event->event.pid = perf_counter_pid(counter, current);
3263 mmap_event->event.tid = perf_counter_tid(counter, current);
3264
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003265 perf_output_put(&handle, mmap_event->event);
3266 perf_output_copy(&handle, mmap_event->file_name,
3267 mmap_event->file_size);
Peter Zijlstra78d613e2009-03-30 19:07:11 +02003268 perf_output_end(&handle);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003269}
3270
3271static int perf_counter_mmap_match(struct perf_counter *counter,
3272 struct perf_mmap_event *mmap_event)
3273{
Peter Zijlstrad99e9442009-06-04 17:08:58 +02003274 if (counter->attr.mmap)
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003275 return 1;
3276
3277 return 0;
3278}
3279
3280static void perf_counter_mmap_ctx(struct perf_counter_context *ctx,
3281 struct perf_mmap_event *mmap_event)
3282{
3283 struct perf_counter *counter;
3284
3285 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
3286 return;
3287
3288 rcu_read_lock();
3289 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
3290 if (perf_counter_mmap_match(counter, mmap_event))
3291 perf_counter_mmap_output(counter, mmap_event);
3292 }
3293 rcu_read_unlock();
3294}
3295
3296static void perf_counter_mmap_event(struct perf_mmap_event *mmap_event)
3297{
3298 struct perf_cpu_context *cpuctx;
Peter Zijlstra665c2142009-05-29 14:51:57 +02003299 struct perf_counter_context *ctx;
Peter Zijlstra089dd792009-06-05 14:04:55 +02003300 struct vm_area_struct *vma = mmap_event->vma;
3301 struct file *file = vma->vm_file;
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003302 unsigned int size;
3303 char tmp[16];
3304 char *buf = NULL;
Peter Zijlstra089dd792009-06-05 14:04:55 +02003305 const char *name;
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003306
Anton Blanchard413ee3b2009-07-16 15:15:52 +02003307 memset(tmp, 0, sizeof(tmp));
3308
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003309 if (file) {
Anton Blanchard413ee3b2009-07-16 15:15:52 +02003310 /*
3311 * d_path works from the end of the buffer backwards, so we
3312 * need to add enough zero bytes after the string to handle
3313 * the 64bit alignment we do later.
3314 */
3315 buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003316 if (!buf) {
3317 name = strncpy(tmp, "//enomem", sizeof(tmp));
3318 goto got_name;
3319 }
Peter Zijlstrad3d21c42009-04-09 10:53:46 +02003320 name = d_path(&file->f_path, buf, PATH_MAX);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003321 if (IS_ERR(name)) {
3322 name = strncpy(tmp, "//toolong", sizeof(tmp));
3323 goto got_name;
3324 }
3325 } else {
Anton Blanchard413ee3b2009-07-16 15:15:52 +02003326 if (arch_vma_name(mmap_event->vma)) {
3327 name = strncpy(tmp, arch_vma_name(mmap_event->vma),
3328 sizeof(tmp));
Peter Zijlstra089dd792009-06-05 14:04:55 +02003329 goto got_name;
Anton Blanchard413ee3b2009-07-16 15:15:52 +02003330 }
Peter Zijlstra089dd792009-06-05 14:04:55 +02003331
3332 if (!vma->vm_mm) {
3333 name = strncpy(tmp, "[vdso]", sizeof(tmp));
3334 goto got_name;
3335 }
3336
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003337 name = strncpy(tmp, "//anon", sizeof(tmp));
3338 goto got_name;
3339 }
3340
3341got_name:
Ingo Molnar888fcee2009-04-09 09:48:22 +02003342 size = ALIGN(strlen(name)+1, sizeof(u64));
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003343
3344 mmap_event->file_name = name;
3345 mmap_event->file_size = size;
3346
3347 mmap_event->event.header.size = sizeof(mmap_event->event) + size;
3348
3349 cpuctx = &get_cpu_var(perf_cpu_context);
3350 perf_counter_mmap_ctx(&cpuctx->ctx, mmap_event);
3351 put_cpu_var(perf_cpu_context);
3352
Peter Zijlstra665c2142009-05-29 14:51:57 +02003353 rcu_read_lock();
3354 /*
3355 * doesn't really matter which of the child contexts the
3356 * events ends up in.
3357 */
3358 ctx = rcu_dereference(current->perf_counter_ctxp);
3359 if (ctx)
3360 perf_counter_mmap_ctx(ctx, mmap_event);
3361 rcu_read_unlock();
3362
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003363 kfree(buf);
3364}
3365
Peter Zijlstra089dd792009-06-05 14:04:55 +02003366void __perf_counter_mmap(struct vm_area_struct *vma)
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003367{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02003368 struct perf_mmap_event mmap_event;
3369
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003370 if (!atomic_read(&nr_mmap_counters))
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02003371 return;
3372
3373 mmap_event = (struct perf_mmap_event){
Peter Zijlstra089dd792009-06-05 14:04:55 +02003374 .vma = vma,
Peter Zijlstra573402d2009-07-22 11:13:50 +02003375 /* .file_name */
3376 /* .file_size */
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003377 .event = {
Peter Zijlstra573402d2009-07-22 11:13:50 +02003378 .header = {
3379 .type = PERF_EVENT_MMAP,
3380 .misc = 0,
3381 /* .size */
3382 },
3383 /* .pid */
3384 /* .tid */
Peter Zijlstra089dd792009-06-05 14:04:55 +02003385 .start = vma->vm_start,
3386 .len = vma->vm_end - vma->vm_start,
3387 .pgoff = vma->vm_pgoff,
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003388 },
3389 };
3390
3391 perf_counter_mmap_event(&mmap_event);
3392}
3393
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003394/*
Peter Zijlstraa78ac322009-05-25 17:39:05 +02003395 * IRQ throttle logging
3396 */
3397
3398static void perf_log_throttle(struct perf_counter *counter, int enable)
3399{
3400 struct perf_output_handle handle;
3401 int ret;
3402
3403 struct {
3404 struct perf_event_header header;
3405 u64 time;
Peter Zijlstracca3f452009-06-11 14:57:55 +02003406 u64 id;
Peter Zijlstra7f453c22009-07-21 13:19:40 +02003407 u64 stream_id;
Peter Zijlstraa78ac322009-05-25 17:39:05 +02003408 } throttle_event = {
3409 .header = {
Anton Blanchard966ee4d2009-07-22 23:05:46 +10003410 .type = PERF_EVENT_THROTTLE,
Peter Zijlstraa78ac322009-05-25 17:39:05 +02003411 .misc = 0,
3412 .size = sizeof(throttle_event),
3413 },
Peter Zijlstra7f453c22009-07-21 13:19:40 +02003414 .time = sched_clock(),
3415 .id = primary_counter_id(counter),
3416 .stream_id = counter->id,
Peter Zijlstraa78ac322009-05-25 17:39:05 +02003417 };
3418
Anton Blanchard966ee4d2009-07-22 23:05:46 +10003419 if (enable)
3420 throttle_event.header.type = PERF_EVENT_UNTHROTTLE;
3421
Ingo Molnar0127c3e2009-05-25 22:03:26 +02003422 ret = perf_output_begin(&handle, counter, sizeof(throttle_event), 1, 0);
Peter Zijlstraa78ac322009-05-25 17:39:05 +02003423 if (ret)
3424 return;
3425
3426 perf_output_put(&handle, throttle_event);
3427 perf_output_end(&handle);
3428}
3429
3430/*
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01003431 * Generic counter overflow handling, sampling.
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02003432 */
3433
Peter Zijlstradf1a1322009-06-10 21:02:22 +02003434int perf_counter_overflow(struct perf_counter *counter, int nmi,
3435 struct perf_sample_data *data)
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02003436{
Peter Zijlstra79f14642009-04-06 11:45:07 +02003437 int events = atomic_read(&counter->event_limit);
Peter Zijlstraa78ac322009-05-25 17:39:05 +02003438 int throttle = counter->pmu->unthrottle != NULL;
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02003439 struct hw_perf_counter *hwc = &counter->hw;
Peter Zijlstra79f14642009-04-06 11:45:07 +02003440 int ret = 0;
3441
Peter Zijlstraa78ac322009-05-25 17:39:05 +02003442 if (!throttle) {
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02003443 hwc->interrupts++;
Ingo Molnar128f0482009-06-03 22:19:36 +02003444 } else {
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02003445 if (hwc->interrupts != MAX_INTERRUPTS) {
3446 hwc->interrupts++;
Peter Zijlstradf58ab22009-06-11 11:25:05 +02003447 if (HZ * hwc->interrupts >
3448 (u64)sysctl_perf_counter_sample_rate) {
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02003449 hwc->interrupts = MAX_INTERRUPTS;
Ingo Molnar128f0482009-06-03 22:19:36 +02003450 perf_log_throttle(counter, 0);
3451 ret = 1;
3452 }
3453 } else {
3454 /*
3455 * Keep re-disabling counters even though on the previous
3456 * pass we disabled it - just in case we raced with a
3457 * sched-in and the counter got enabled again:
3458 */
Peter Zijlstraa78ac322009-05-25 17:39:05 +02003459 ret = 1;
3460 }
3461 }
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003462
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02003463 if (counter->attr.freq) {
3464 u64 now = sched_clock();
3465 s64 delta = now - hwc->freq_stamp;
3466
3467 hwc->freq_stamp = now;
3468
3469 if (delta > 0 && delta < TICK_NSEC)
3470 perf_adjust_period(counter, NSEC_PER_SEC / (int)delta);
3471 }
3472
Peter Zijlstra2023b352009-05-05 17:50:26 +02003473 /*
3474 * XXX event_limit might not quite work as expected on inherited
3475 * counters
3476 */
3477
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02003478 counter->pending_kill = POLL_IN;
Peter Zijlstra79f14642009-04-06 11:45:07 +02003479 if (events && atomic_dec_and_test(&counter->event_limit)) {
3480 ret = 1;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02003481 counter->pending_kill = POLL_HUP;
Peter Zijlstra79f14642009-04-06 11:45:07 +02003482 if (nmi) {
3483 counter->pending_disable = 1;
3484 perf_pending_queue(&counter->pending,
3485 perf_pending_counter);
3486 } else
3487 perf_counter_disable(counter);
3488 }
3489
Peter Zijlstradf1a1322009-06-10 21:02:22 +02003490 perf_counter_output(counter, nmi, data);
Peter Zijlstra79f14642009-04-06 11:45:07 +02003491 return ret;
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02003492}
3493
3494/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003495 * Generic software counter infrastructure
3496 */
3497
Peter Zijlstra7b4b6652009-07-22 09:29:32 +02003498/*
3499 * We directly increment counter->count and keep a second value in
3500 * counter->hw.period_left to count intervals. This period counter
3501 * is kept in the range [-sample_period, 0] so that we can use the
3502 * sign as trigger.
3503 */
3504
3505static u64 perf_swcounter_set_period(struct perf_counter *counter)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003506{
3507 struct hw_perf_counter *hwc = &counter->hw;
Peter Zijlstra7b4b6652009-07-22 09:29:32 +02003508 u64 period = hwc->last_period;
3509 u64 nr, offset;
3510 s64 old, val;
3511
3512 hwc->last_period = hwc->sample_period;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003513
3514again:
Peter Zijlstra7b4b6652009-07-22 09:29:32 +02003515 old = val = atomic64_read(&hwc->period_left);
3516 if (val < 0)
3517 return 0;
3518
3519 nr = div64_u64(period + val, period);
3520 offset = nr * period;
3521 val -= offset;
3522 if (atomic64_cmpxchg(&hwc->period_left, old, val) != old)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003523 goto again;
3524
Peter Zijlstra7b4b6652009-07-22 09:29:32 +02003525 return nr;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003526}
3527
3528static void perf_swcounter_overflow(struct perf_counter *counter,
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003529 int nmi, struct perf_sample_data *data)
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003530{
Peter Zijlstra7b4b6652009-07-22 09:29:32 +02003531 struct hw_perf_counter *hwc = &counter->hw;
3532 u64 overflow;
Peter Zijlstradf1a1322009-06-10 21:02:22 +02003533
Peter Zijlstra7b4b6652009-07-22 09:29:32 +02003534 data->period = counter->hw.last_period;
3535 overflow = perf_swcounter_set_period(counter);
3536
3537 if (hwc->interrupts == MAX_INTERRUPTS)
3538 return;
3539
3540 for (; overflow; overflow--) {
3541 if (perf_counter_overflow(counter, nmi, data)) {
3542 /*
3543 * We inhibit the overflow from happening when
3544 * hwc->interrupts == MAX_INTERRUPTS.
3545 */
3546 break;
3547 }
3548 }
3549}
3550
3551static void perf_swcounter_unthrottle(struct perf_counter *counter)
3552{
3553 /*
3554 * Nothing to do, we already reset hwc->interrupts.
3555 */
3556}
3557
3558static void perf_swcounter_add(struct perf_counter *counter, u64 nr,
3559 int nmi, struct perf_sample_data *data)
3560{
3561 struct hw_perf_counter *hwc = &counter->hw;
3562
3563 atomic64_add(nr, &counter->count);
3564
3565 if (!hwc->sample_period)
3566 return;
3567
3568 if (!data->regs)
3569 return;
3570
3571 if (!atomic64_add_negative(nr, &hwc->period_left))
3572 perf_swcounter_overflow(counter, nmi, data);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003573}
3574
Paul Mackerras880ca152009-06-01 17:49:14 +10003575static int perf_swcounter_is_counting(struct perf_counter *counter)
3576{
Peter Zijlstrabcfc2602009-08-13 09:51:55 +02003577 /*
3578 * The counter is active, we're good!
3579 */
Paul Mackerras880ca152009-06-01 17:49:14 +10003580 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
3581 return 1;
3582
Peter Zijlstrabcfc2602009-08-13 09:51:55 +02003583 /*
3584 * The counter is off/error, not counting.
3585 */
Paul Mackerras880ca152009-06-01 17:49:14 +10003586 if (counter->state != PERF_COUNTER_STATE_INACTIVE)
3587 return 0;
3588
3589 /*
Peter Zijlstrabcfc2602009-08-13 09:51:55 +02003590 * The counter is inactive, if the context is active
3591 * we're part of a group that didn't make it on the 'pmu',
3592 * not counting.
Paul Mackerras880ca152009-06-01 17:49:14 +10003593 */
Peter Zijlstrabcfc2602009-08-13 09:51:55 +02003594 if (counter->ctx->is_active)
3595 return 0;
3596
3597 /*
3598 * We're inactive and the context is too, this means the
3599 * task is scheduled out, we're counting events that happen
3600 * to us, like migration events.
3601 */
3602 return 1;
Paul Mackerras880ca152009-06-01 17:49:14 +10003603}
3604
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003605static int perf_swcounter_match(struct perf_counter *counter,
Peter Zijlstra1c432d82009-06-11 13:19:29 +02003606 enum perf_type_id type,
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003607 u32 event, struct pt_regs *regs)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003608{
Paul Mackerras880ca152009-06-01 17:49:14 +10003609 if (!perf_swcounter_is_counting(counter))
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003610 return 0;
3611
Ingo Molnara21ca2c2009-06-06 09:58:57 +02003612 if (counter->attr.type != type)
3613 return 0;
3614 if (counter->attr.config != event)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003615 return 0;
3616
Paul Mackerras3f731ca2009-06-01 17:52:30 +10003617 if (regs) {
Peter Zijlstra0d486962009-06-02 19:22:16 +02003618 if (counter->attr.exclude_user && user_mode(regs))
Paul Mackerras3f731ca2009-06-01 17:52:30 +10003619 return 0;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003620
Peter Zijlstra0d486962009-06-02 19:22:16 +02003621 if (counter->attr.exclude_kernel && !user_mode(regs))
Paul Mackerras3f731ca2009-06-01 17:52:30 +10003622 return 0;
3623 }
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003624
3625 return 1;
3626}
3627
3628static void perf_swcounter_ctx_event(struct perf_counter_context *ctx,
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003629 enum perf_type_id type,
3630 u32 event, u64 nr, int nmi,
3631 struct perf_sample_data *data)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003632{
3633 struct perf_counter *counter;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003634
Peter Zijlstra01ef09d2009-03-19 20:26:11 +01003635 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003636 return;
3637
Peter Zijlstra592903c2009-03-13 12:21:36 +01003638 rcu_read_lock();
3639 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003640 if (perf_swcounter_match(counter, type, event, data->regs))
3641 perf_swcounter_add(counter, nr, nmi, data);
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003642 }
Peter Zijlstra592903c2009-03-13 12:21:36 +01003643 rcu_read_unlock();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003644}
3645
Peter Zijlstra96f6d442009-03-23 18:22:07 +01003646static int *perf_swcounter_recursion_context(struct perf_cpu_context *cpuctx)
3647{
3648 if (in_nmi())
3649 return &cpuctx->recursion[3];
3650
3651 if (in_irq())
3652 return &cpuctx->recursion[2];
3653
3654 if (in_softirq())
3655 return &cpuctx->recursion[1];
3656
3657 return &cpuctx->recursion[0];
3658}
3659
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003660static void do_perf_swcounter_event(enum perf_type_id type, u32 event,
3661 u64 nr, int nmi,
3662 struct perf_sample_data *data)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003663{
3664 struct perf_cpu_context *cpuctx = &get_cpu_var(perf_cpu_context);
Peter Zijlstra96f6d442009-03-23 18:22:07 +01003665 int *recursion = perf_swcounter_recursion_context(cpuctx);
Peter Zijlstra665c2142009-05-29 14:51:57 +02003666 struct perf_counter_context *ctx;
Peter Zijlstra96f6d442009-03-23 18:22:07 +01003667
3668 if (*recursion)
3669 goto out;
3670
3671 (*recursion)++;
3672 barrier();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003673
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003674 perf_swcounter_ctx_event(&cpuctx->ctx, type, event,
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003675 nr, nmi, data);
Peter Zijlstra665c2142009-05-29 14:51:57 +02003676 rcu_read_lock();
3677 /*
3678 * doesn't really matter which of the child contexts the
3679 * events ends up in.
3680 */
3681 ctx = rcu_dereference(current->perf_counter_ctxp);
3682 if (ctx)
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003683 perf_swcounter_ctx_event(ctx, type, event, nr, nmi, data);
Peter Zijlstra665c2142009-05-29 14:51:57 +02003684 rcu_read_unlock();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003685
Peter Zijlstra96f6d442009-03-23 18:22:07 +01003686 barrier();
3687 (*recursion)--;
3688
3689out:
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003690 put_cpu_var(perf_cpu_context);
3691}
3692
Peter Zijlstraf29ac752009-06-19 18:27:26 +02003693void __perf_swcounter_event(u32 event, u64 nr, int nmi,
3694 struct pt_regs *regs, u64 addr)
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003695{
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003696 struct perf_sample_data data = {
3697 .regs = regs,
3698 .addr = addr,
3699 };
3700
3701 do_perf_swcounter_event(PERF_TYPE_SOFTWARE, event, nr, nmi, &data);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003702}
3703
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003704static void perf_swcounter_read(struct perf_counter *counter)
3705{
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003706}
3707
3708static int perf_swcounter_enable(struct perf_counter *counter)
3709{
Peter Zijlstra7b4b6652009-07-22 09:29:32 +02003710 struct hw_perf_counter *hwc = &counter->hw;
3711
3712 if (hwc->sample_period) {
3713 hwc->last_period = hwc->sample_period;
3714 perf_swcounter_set_period(counter);
3715 }
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003716 return 0;
3717}
3718
3719static void perf_swcounter_disable(struct perf_counter *counter)
3720{
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003721}
3722
Robert Richter4aeb0b42009-04-29 12:47:03 +02003723static const struct pmu perf_ops_generic = {
Peter Zijlstraac17dc82009-03-13 12:21:34 +01003724 .enable = perf_swcounter_enable,
3725 .disable = perf_swcounter_disable,
3726 .read = perf_swcounter_read,
Peter Zijlstra7b4b6652009-07-22 09:29:32 +02003727 .unthrottle = perf_swcounter_unthrottle,
Peter Zijlstraac17dc82009-03-13 12:21:34 +01003728};
3729
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003730/*
Peter Zijlstra7b4b6652009-07-22 09:29:32 +02003731 * hrtimer based swcounter callback
3732 */
3733
3734static enum hrtimer_restart perf_swcounter_hrtimer(struct hrtimer *hrtimer)
3735{
3736 enum hrtimer_restart ret = HRTIMER_RESTART;
3737 struct perf_sample_data data;
3738 struct perf_counter *counter;
3739 u64 period;
3740
3741 counter = container_of(hrtimer, struct perf_counter, hw.hrtimer);
3742 counter->pmu->read(counter);
3743
3744 data.addr = 0;
3745 data.regs = get_irq_regs();
3746 /*
3747 * In case we exclude kernel IPs or are somehow not in interrupt
3748 * context, provide the next best thing, the user IP.
3749 */
3750 if ((counter->attr.exclude_kernel || !data.regs) &&
3751 !counter->attr.exclude_user)
3752 data.regs = task_pt_regs(current);
3753
3754 if (data.regs) {
3755 if (perf_counter_overflow(counter, 0, &data))
3756 ret = HRTIMER_NORESTART;
3757 }
3758
3759 period = max_t(u64, 10000, counter->hw.sample_period);
3760 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
3761
3762 return ret;
3763}
3764
3765/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003766 * Software counter: cpu wall time clock
3767 */
3768
Paul Mackerras9abf8a02009-01-09 16:26:43 +11003769static void cpu_clock_perf_counter_update(struct perf_counter *counter)
3770{
3771 int cpu = raw_smp_processor_id();
3772 s64 prev;
3773 u64 now;
3774
3775 now = cpu_clock(cpu);
3776 prev = atomic64_read(&counter->hw.prev_count);
3777 atomic64_set(&counter->hw.prev_count, now);
3778 atomic64_add(now - prev, &counter->count);
3779}
3780
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003781static int cpu_clock_perf_counter_enable(struct perf_counter *counter)
3782{
3783 struct hw_perf_counter *hwc = &counter->hw;
3784 int cpu = raw_smp_processor_id();
3785
3786 atomic64_set(&hwc->prev_count, cpu_clock(cpu));
Peter Zijlstra039fc912009-03-13 16:43:47 +01003787 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
3788 hwc->hrtimer.function = perf_swcounter_hrtimer;
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003789 if (hwc->sample_period) {
3790 u64 period = max_t(u64, 10000, hwc->sample_period);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003791 __hrtimer_start_range_ns(&hwc->hrtimer,
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003792 ns_to_ktime(period), 0,
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003793 HRTIMER_MODE_REL, 0);
3794 }
3795
3796 return 0;
3797}
3798
Ingo Molnar5c92d122008-12-11 13:21:10 +01003799static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
3800{
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003801 if (counter->hw.sample_period)
Peter Zijlstrab986d7e2009-05-20 12:21:21 +02003802 hrtimer_cancel(&counter->hw.hrtimer);
Paul Mackerras9abf8a02009-01-09 16:26:43 +11003803 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01003804}
3805
3806static void cpu_clock_perf_counter_read(struct perf_counter *counter)
3807{
Paul Mackerras9abf8a02009-01-09 16:26:43 +11003808 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01003809}
3810
Robert Richter4aeb0b42009-04-29 12:47:03 +02003811static const struct pmu perf_ops_cpu_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +01003812 .enable = cpu_clock_perf_counter_enable,
3813 .disable = cpu_clock_perf_counter_disable,
3814 .read = cpu_clock_perf_counter_read,
Ingo Molnar5c92d122008-12-11 13:21:10 +01003815};
3816
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01003817/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003818 * Software counter: task time clock
3819 */
3820
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02003821static void task_clock_perf_counter_update(struct perf_counter *counter, u64 now)
Ingo Molnarbae43c92008-12-11 14:03:20 +01003822{
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02003823 u64 prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003824 s64 delta;
Ingo Molnarbae43c92008-12-11 14:03:20 +01003825
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02003826 prev = atomic64_xchg(&counter->hw.prev_count, now);
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003827 delta = now - prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003828 atomic64_add(delta, &counter->count);
Ingo Molnarbae43c92008-12-11 14:03:20 +01003829}
3830
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01003831static int task_clock_perf_counter_enable(struct perf_counter *counter)
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003832{
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003833 struct hw_perf_counter *hwc = &counter->hw;
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02003834 u64 now;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003835
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02003836 now = counter->ctx->time;
3837
3838 atomic64_set(&hwc->prev_count, now);
Peter Zijlstra039fc912009-03-13 16:43:47 +01003839 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
3840 hwc->hrtimer.function = perf_swcounter_hrtimer;
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003841 if (hwc->sample_period) {
3842 u64 period = max_t(u64, 10000, hwc->sample_period);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003843 __hrtimer_start_range_ns(&hwc->hrtimer,
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003844 ns_to_ktime(period), 0,
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003845 HRTIMER_MODE_REL, 0);
3846 }
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01003847
3848 return 0;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003849}
3850
3851static void task_clock_perf_counter_disable(struct perf_counter *counter)
3852{
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003853 if (counter->hw.sample_period)
Peter Zijlstrab986d7e2009-05-20 12:21:21 +02003854 hrtimer_cancel(&counter->hw.hrtimer);
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02003855 task_clock_perf_counter_update(counter, counter->ctx->time);
3856
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003857}
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01003858
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003859static void task_clock_perf_counter_read(struct perf_counter *counter)
3860{
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02003861 u64 time;
3862
3863 if (!in_nmi()) {
3864 update_context_time(counter->ctx);
3865 time = counter->ctx->time;
3866 } else {
3867 u64 now = perf_clock();
3868 u64 delta = now - counter->ctx->timestamp;
3869 time = counter->ctx->time + delta;
3870 }
3871
3872 task_clock_perf_counter_update(counter, time);
Ingo Molnarbae43c92008-12-11 14:03:20 +01003873}
3874
Robert Richter4aeb0b42009-04-29 12:47:03 +02003875static const struct pmu perf_ops_task_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +01003876 .enable = task_clock_perf_counter_enable,
3877 .disable = task_clock_perf_counter_disable,
3878 .read = task_clock_perf_counter_read,
Ingo Molnarbae43c92008-12-11 14:03:20 +01003879};
3880
Peter Zijlstrae077df42009-03-19 20:26:17 +01003881#ifdef CONFIG_EVENT_PROFILE
Frederic Weisbeckerf413cdb2009-08-07 01:25:54 +02003882void perf_tpcounter_event(int event_id, u64 addr, u64 count, void *record,
3883 int entry_size)
Peter Zijlstrae077df42009-03-19 20:26:17 +01003884{
Frederic Weisbecker3a43ce62009-08-08 04:26:37 +02003885 struct perf_raw_record raw = {
Frederic Weisbeckerf413cdb2009-08-07 01:25:54 +02003886 .size = entry_size,
Frederic Weisbecker3a43ce62009-08-08 04:26:37 +02003887 .data = record,
Frederic Weisbeckerf413cdb2009-08-07 01:25:54 +02003888 };
3889
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003890 struct perf_sample_data data = {
Chris Wilsond4d7d0b2009-07-06 09:31:33 +01003891 .regs = get_irq_regs(),
Peter Zijlstra3a659302009-07-21 17:34:57 +02003892 .addr = addr,
Frederic Weisbecker3a43ce62009-08-08 04:26:37 +02003893 .raw = &raw,
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003894 };
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003895
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003896 if (!data.regs)
3897 data.regs = task_pt_regs(current);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003898
Peter Zijlstra3a659302009-07-21 17:34:57 +02003899 do_perf_swcounter_event(PERF_TYPE_TRACEPOINT, event_id, count, 1, &data);
Peter Zijlstrae077df42009-03-19 20:26:17 +01003900}
Steven Whitehouseff7b1b42009-04-15 16:55:05 +01003901EXPORT_SYMBOL_GPL(perf_tpcounter_event);
Peter Zijlstrae077df42009-03-19 20:26:17 +01003902
3903extern int ftrace_profile_enable(int);
3904extern void ftrace_profile_disable(int);
3905
3906static void tp_perf_counter_destroy(struct perf_counter *counter)
3907{
Chris Wilsond4d7d0b2009-07-06 09:31:33 +01003908 ftrace_profile_disable(counter->attr.config);
Peter Zijlstrae077df42009-03-19 20:26:17 +01003909}
3910
Robert Richter4aeb0b42009-04-29 12:47:03 +02003911static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
Peter Zijlstrae077df42009-03-19 20:26:17 +01003912{
Peter Zijlstraa4e95fc2009-08-10 11:20:12 +02003913 /*
3914 * Raw tracepoint data is a severe data leak, only allow root to
3915 * have these.
3916 */
3917 if ((counter->attr.sample_type & PERF_SAMPLE_RAW) &&
3918 !capable(CAP_SYS_ADMIN))
3919 return ERR_PTR(-EPERM);
3920
Chris Wilsond4d7d0b2009-07-06 09:31:33 +01003921 if (ftrace_profile_enable(counter->attr.config))
Peter Zijlstrae077df42009-03-19 20:26:17 +01003922 return NULL;
3923
3924 counter->destroy = tp_perf_counter_destroy;
3925
3926 return &perf_ops_generic;
3927}
3928#else
Robert Richter4aeb0b42009-04-29 12:47:03 +02003929static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
Peter Zijlstrae077df42009-03-19 20:26:17 +01003930{
3931 return NULL;
3932}
3933#endif
3934
Peter Zijlstraf29ac752009-06-19 18:27:26 +02003935atomic_t perf_swcounter_enabled[PERF_COUNT_SW_MAX];
3936
3937static void sw_perf_counter_destroy(struct perf_counter *counter)
3938{
3939 u64 event = counter->attr.config;
3940
Peter Zijlstraf3440112009-06-22 13:58:35 +02003941 WARN_ON(counter->parent);
3942
Peter Zijlstraf29ac752009-06-19 18:27:26 +02003943 atomic_dec(&perf_swcounter_enabled[event]);
3944}
3945
Robert Richter4aeb0b42009-04-29 12:47:03 +02003946static const struct pmu *sw_perf_counter_init(struct perf_counter *counter)
Ingo Molnar5c92d122008-12-11 13:21:10 +01003947{
Robert Richter4aeb0b42009-04-29 12:47:03 +02003948 const struct pmu *pmu = NULL;
Peter Zijlstraf29ac752009-06-19 18:27:26 +02003949 u64 event = counter->attr.config;
Ingo Molnar5c92d122008-12-11 13:21:10 +01003950
Paul Mackerras0475f9e2009-02-11 14:35:35 +11003951 /*
3952 * Software counters (currently) can't in general distinguish
3953 * between user, kernel and hypervisor events.
3954 * However, context switches and cpu migrations are considered
3955 * to be kernel events, and page faults are never hypervisor
3956 * events.
3957 */
Peter Zijlstraf29ac752009-06-19 18:27:26 +02003958 switch (event) {
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +02003959 case PERF_COUNT_SW_CPU_CLOCK:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003960 pmu = &perf_ops_cpu_clock;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003961
Ingo Molnar5c92d122008-12-11 13:21:10 +01003962 break;
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +02003963 case PERF_COUNT_SW_TASK_CLOCK:
Paul Mackerras23a185c2009-02-09 22:42:47 +11003964 /*
3965 * If the user instantiates this as a per-cpu counter,
3966 * use the cpu_clock counter instead.
3967 */
3968 if (counter->ctx->task)
Robert Richter4aeb0b42009-04-29 12:47:03 +02003969 pmu = &perf_ops_task_clock;
Paul Mackerras23a185c2009-02-09 22:42:47 +11003970 else
Robert Richter4aeb0b42009-04-29 12:47:03 +02003971 pmu = &perf_ops_cpu_clock;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003972
Ingo Molnarbae43c92008-12-11 14:03:20 +01003973 break;
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +02003974 case PERF_COUNT_SW_PAGE_FAULTS:
3975 case PERF_COUNT_SW_PAGE_FAULTS_MIN:
3976 case PERF_COUNT_SW_PAGE_FAULTS_MAJ:
3977 case PERF_COUNT_SW_CONTEXT_SWITCHES:
3978 case PERF_COUNT_SW_CPU_MIGRATIONS:
Peter Zijlstraf3440112009-06-22 13:58:35 +02003979 if (!counter->parent) {
3980 atomic_inc(&perf_swcounter_enabled[event]);
3981 counter->destroy = sw_perf_counter_destroy;
3982 }
Paul Mackerras3f731ca2009-06-01 17:52:30 +10003983 pmu = &perf_ops_generic;
Ingo Molnar6c594c22008-12-14 12:34:15 +01003984 break;
Ingo Molnar5c92d122008-12-11 13:21:10 +01003985 }
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003986
Robert Richter4aeb0b42009-04-29 12:47:03 +02003987 return pmu;
Ingo Molnar5c92d122008-12-11 13:21:10 +01003988}
3989
Thomas Gleixner0793a612008-12-04 20:12:29 +01003990/*
3991 * Allocate and initialize a counter structure
3992 */
3993static struct perf_counter *
Peter Zijlstra0d486962009-06-02 19:22:16 +02003994perf_counter_alloc(struct perf_counter_attr *attr,
Ingo Molnar04289bb2008-12-11 08:38:42 +01003995 int cpu,
Paul Mackerras23a185c2009-02-09 22:42:47 +11003996 struct perf_counter_context *ctx,
Ingo Molnar9b51f662008-12-12 13:49:45 +01003997 struct perf_counter *group_leader,
Peter Zijlstrab84fbc92009-06-22 13:57:40 +02003998 struct perf_counter *parent_counter,
Ingo Molnar9b51f662008-12-12 13:49:45 +01003999 gfp_t gfpflags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01004000{
Robert Richter4aeb0b42009-04-29 12:47:03 +02004001 const struct pmu *pmu;
Ingo Molnar621a01e2008-12-11 12:46:46 +01004002 struct perf_counter *counter;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02004003 struct hw_perf_counter *hwc;
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02004004 long err;
Thomas Gleixner0793a612008-12-04 20:12:29 +01004005
Ingo Molnar9b51f662008-12-12 13:49:45 +01004006 counter = kzalloc(sizeof(*counter), gfpflags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004007 if (!counter)
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02004008 return ERR_PTR(-ENOMEM);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004009
Ingo Molnar04289bb2008-12-11 08:38:42 +01004010 /*
4011 * Single counters are their own group leaders, with an
4012 * empty sibling list:
4013 */
4014 if (!group_leader)
4015 group_leader = counter;
4016
Peter Zijlstrafccc7142009-05-23 18:28:56 +02004017 mutex_init(&counter->child_mutex);
4018 INIT_LIST_HEAD(&counter->child_list);
4019
Ingo Molnar04289bb2008-12-11 08:38:42 +01004020 INIT_LIST_HEAD(&counter->list_entry);
Peter Zijlstra592903c2009-03-13 12:21:36 +01004021 INIT_LIST_HEAD(&counter->event_entry);
Ingo Molnar04289bb2008-12-11 08:38:42 +01004022 INIT_LIST_HEAD(&counter->sibling_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004023 init_waitqueue_head(&counter->waitq);
4024
Peter Zijlstra7b732a72009-03-23 18:22:10 +01004025 mutex_init(&counter->mmap_mutex);
4026
Peter Zijlstraa96bbc12009-06-03 14:01:36 +02004027 counter->cpu = cpu;
Peter Zijlstra0d486962009-06-02 19:22:16 +02004028 counter->attr = *attr;
Peter Zijlstraa96bbc12009-06-03 14:01:36 +02004029 counter->group_leader = group_leader;
4030 counter->pmu = NULL;
4031 counter->ctx = ctx;
4032 counter->oncpu = -1;
Ingo Molnar329d8762009-05-26 08:10:00 +02004033
Peter Zijlstrab84fbc92009-06-22 13:57:40 +02004034 counter->parent = parent_counter;
4035
Peter Zijlstraa96bbc12009-06-03 14:01:36 +02004036 counter->ns = get_pid_ns(current->nsproxy->pid_ns);
4037 counter->id = atomic64_inc_return(&perf_counter_id);
4038
4039 counter->state = PERF_COUNTER_STATE_INACTIVE;
4040
Peter Zijlstra0d486962009-06-02 19:22:16 +02004041 if (attr->disabled)
Ingo Molnara86ed502008-12-17 00:43:10 +01004042 counter->state = PERF_COUNTER_STATE_OFF;
4043
Robert Richter4aeb0b42009-04-29 12:47:03 +02004044 pmu = NULL;
Peter Zijlstrab8e83512009-03-19 20:26:18 +01004045
Peter Zijlstra60db5e02009-05-15 15:19:28 +02004046 hwc = &counter->hw;
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02004047 hwc->sample_period = attr->sample_period;
Peter Zijlstra0d486962009-06-02 19:22:16 +02004048 if (attr->freq && attr->sample_freq)
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02004049 hwc->sample_period = 1;
4050
4051 atomic64_set(&hwc->period_left, hwc->sample_period);
Peter Zijlstra60db5e02009-05-15 15:19:28 +02004052
Peter Zijlstra2023b352009-05-05 17:50:26 +02004053 /*
Peter Zijlstra3dab77f2009-08-13 11:47:53 +02004054 * we currently do not support PERF_FORMAT_GROUP on inherited counters
Peter Zijlstra2023b352009-05-05 17:50:26 +02004055 */
Peter Zijlstra3dab77f2009-08-13 11:47:53 +02004056 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
Peter Zijlstra2023b352009-05-05 17:50:26 +02004057 goto done;
4058
Ingo Molnara21ca2c2009-06-06 09:58:57 +02004059 switch (attr->type) {
Peter Zijlstra081fad82009-06-11 17:57:21 +02004060 case PERF_TYPE_RAW:
Peter Zijlstrab8e83512009-03-19 20:26:18 +01004061 case PERF_TYPE_HARDWARE:
Ingo Molnar8326f442009-06-05 20:22:46 +02004062 case PERF_TYPE_HW_CACHE:
Robert Richter4aeb0b42009-04-29 12:47:03 +02004063 pmu = hw_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01004064 break;
4065
4066 case PERF_TYPE_SOFTWARE:
Robert Richter4aeb0b42009-04-29 12:47:03 +02004067 pmu = sw_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01004068 break;
4069
4070 case PERF_TYPE_TRACEPOINT:
Robert Richter4aeb0b42009-04-29 12:47:03 +02004071 pmu = tp_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01004072 break;
Peter Zijlstra974802e2009-06-12 12:46:55 +02004073
4074 default:
4075 break;
Peter Zijlstrab8e83512009-03-19 20:26:18 +01004076 }
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01004077done:
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02004078 err = 0;
Robert Richter4aeb0b42009-04-29 12:47:03 +02004079 if (!pmu)
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02004080 err = -EINVAL;
Robert Richter4aeb0b42009-04-29 12:47:03 +02004081 else if (IS_ERR(pmu))
4082 err = PTR_ERR(pmu);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02004083
4084 if (err) {
Peter Zijlstraa96bbc12009-06-03 14:01:36 +02004085 if (counter->ns)
4086 put_pid_ns(counter->ns);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02004087 kfree(counter);
4088 return ERR_PTR(err);
4089 }
4090
Robert Richter4aeb0b42009-04-29 12:47:03 +02004091 counter->pmu = pmu;
Thomas Gleixner0793a612008-12-04 20:12:29 +01004092
Peter Zijlstraf3440112009-06-22 13:58:35 +02004093 if (!counter->parent) {
4094 atomic_inc(&nr_counters);
4095 if (counter->attr.mmap)
4096 atomic_inc(&nr_mmap_counters);
4097 if (counter->attr.comm)
4098 atomic_inc(&nr_comm_counters);
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02004099 if (counter->attr.task)
4100 atomic_inc(&nr_task_counters);
Peter Zijlstraf3440112009-06-22 13:58:35 +02004101 }
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02004102
Thomas Gleixner0793a612008-12-04 20:12:29 +01004103 return counter;
4104}
4105
Peter Zijlstra974802e2009-06-12 12:46:55 +02004106static int perf_copy_attr(struct perf_counter_attr __user *uattr,
4107 struct perf_counter_attr *attr)
4108{
4109 int ret;
4110 u32 size;
4111
4112 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
4113 return -EFAULT;
4114
4115 /*
4116 * zero the full structure, so that a short copy will be nice.
4117 */
4118 memset(attr, 0, sizeof(*attr));
4119
4120 ret = get_user(size, &uattr->size);
4121 if (ret)
4122 return ret;
4123
4124 if (size > PAGE_SIZE) /* silly large */
4125 goto err_size;
4126
4127 if (!size) /* abi compat */
4128 size = PERF_ATTR_SIZE_VER0;
4129
4130 if (size < PERF_ATTR_SIZE_VER0)
4131 goto err_size;
4132
4133 /*
4134 * If we're handed a bigger struct than we know of,
4135 * ensure all the unknown bits are 0.
4136 */
4137 if (size > sizeof(*attr)) {
4138 unsigned long val;
4139 unsigned long __user *addr;
4140 unsigned long __user *end;
4141
4142 addr = PTR_ALIGN((void __user *)uattr + sizeof(*attr),
4143 sizeof(unsigned long));
4144 end = PTR_ALIGN((void __user *)uattr + size,
4145 sizeof(unsigned long));
4146
4147 for (; addr < end; addr += sizeof(unsigned long)) {
4148 ret = get_user(val, addr);
4149 if (ret)
4150 return ret;
4151 if (val)
4152 goto err_size;
4153 }
4154 }
4155
4156 ret = copy_from_user(attr, uattr, size);
4157 if (ret)
4158 return -EFAULT;
4159
4160 /*
4161 * If the type exists, the corresponding creation will verify
4162 * the attr->config.
4163 */
4164 if (attr->type >= PERF_TYPE_MAX)
4165 return -EINVAL;
4166
4167 if (attr->__reserved_1 || attr->__reserved_2 || attr->__reserved_3)
4168 return -EINVAL;
4169
4170 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
4171 return -EINVAL;
4172
4173 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
4174 return -EINVAL;
4175
4176out:
4177 return ret;
4178
4179err_size:
4180 put_user(sizeof(*attr), &uattr->size);
4181 ret = -E2BIG;
4182 goto out;
4183}
4184
Thomas Gleixner0793a612008-12-04 20:12:29 +01004185/**
Paul Mackerras2743a5b2009-03-04 20:36:51 +11004186 * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
Ingo Molnar9f66a382008-12-10 12:33:23 +01004187 *
Peter Zijlstra0d486962009-06-02 19:22:16 +02004188 * @attr_uptr: event type attributes for monitoring/sampling
Thomas Gleixner0793a612008-12-04 20:12:29 +01004189 * @pid: target pid
Ingo Molnar9f66a382008-12-10 12:33:23 +01004190 * @cpu: target cpu
4191 * @group_fd: group leader counter fd
Thomas Gleixner0793a612008-12-04 20:12:29 +01004192 */
Paul Mackerras2743a5b2009-03-04 20:36:51 +11004193SYSCALL_DEFINE5(perf_counter_open,
Peter Zijlstra974802e2009-06-12 12:46:55 +02004194 struct perf_counter_attr __user *, attr_uptr,
Paul Mackerras2743a5b2009-03-04 20:36:51 +11004195 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01004196{
Ingo Molnar04289bb2008-12-11 08:38:42 +01004197 struct perf_counter *counter, *group_leader;
Peter Zijlstra0d486962009-06-02 19:22:16 +02004198 struct perf_counter_attr attr;
Ingo Molnar04289bb2008-12-11 08:38:42 +01004199 struct perf_counter_context *ctx;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004200 struct file *counter_file = NULL;
Ingo Molnar04289bb2008-12-11 08:38:42 +01004201 struct file *group_file = NULL;
4202 int fput_needed = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004203 int fput_needed2 = 0;
Thomas Gleixner0793a612008-12-04 20:12:29 +01004204 int ret;
4205
Paul Mackerras2743a5b2009-03-04 20:36:51 +11004206 /* for future expandability... */
4207 if (flags)
4208 return -EINVAL;
4209
Peter Zijlstra974802e2009-06-12 12:46:55 +02004210 ret = perf_copy_attr(attr_uptr, &attr);
4211 if (ret)
4212 return ret;
Thomas Gleixnereab656a2008-12-08 19:26:59 +01004213
Peter Zijlstra07647712009-06-11 11:18:36 +02004214 if (!attr.exclude_kernel) {
4215 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
4216 return -EACCES;
4217 }
4218
Peter Zijlstradf58ab22009-06-11 11:25:05 +02004219 if (attr.freq) {
4220 if (attr.sample_freq > sysctl_perf_counter_sample_rate)
4221 return -EINVAL;
4222 }
4223
Ingo Molnar04289bb2008-12-11 08:38:42 +01004224 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01004225 * Get the target context (task or percpu):
4226 */
4227 ctx = find_get_context(pid, cpu);
4228 if (IS_ERR(ctx))
4229 return PTR_ERR(ctx);
4230
4231 /*
4232 * Look up the group leader (we will attach this counter to it):
Ingo Molnar04289bb2008-12-11 08:38:42 +01004233 */
4234 group_leader = NULL;
4235 if (group_fd != -1) {
4236 ret = -EINVAL;
4237 group_file = fget_light(group_fd, &fput_needed);
4238 if (!group_file)
Ingo Molnarccff2862008-12-11 11:26:29 +01004239 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01004240 if (group_file->f_op != &perf_fops)
Ingo Molnarccff2862008-12-11 11:26:29 +01004241 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01004242
4243 group_leader = group_file->private_data;
4244 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01004245 * Do not allow a recursive hierarchy (this new sibling
4246 * becoming part of another group-sibling):
Ingo Molnar04289bb2008-12-11 08:38:42 +01004247 */
Ingo Molnarccff2862008-12-11 11:26:29 +01004248 if (group_leader->group_leader != group_leader)
4249 goto err_put_context;
4250 /*
4251 * Do not allow to attach to a group in a different
4252 * task or CPU context:
4253 */
4254 if (group_leader->ctx != ctx)
4255 goto err_put_context;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11004256 /*
4257 * Only a group leader can be exclusive or pinned
4258 */
Peter Zijlstra0d486962009-06-02 19:22:16 +02004259 if (attr.exclusive || attr.pinned)
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11004260 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01004261 }
4262
Peter Zijlstra0d486962009-06-02 19:22:16 +02004263 counter = perf_counter_alloc(&attr, cpu, ctx, group_leader,
Peter Zijlstrab84fbc92009-06-22 13:57:40 +02004264 NULL, GFP_KERNEL);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02004265 ret = PTR_ERR(counter);
4266 if (IS_ERR(counter))
Thomas Gleixner0793a612008-12-04 20:12:29 +01004267 goto err_put_context;
4268
Thomas Gleixner0793a612008-12-04 20:12:29 +01004269 ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
4270 if (ret < 0)
Ingo Molnar9b51f662008-12-12 13:49:45 +01004271 goto err_free_put_context;
4272
4273 counter_file = fget_light(ret, &fput_needed2);
4274 if (!counter_file)
4275 goto err_free_put_context;
4276
4277 counter->filp = counter_file;
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004278 WARN_ON_ONCE(ctx->parent_ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +11004279 mutex_lock(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01004280 perf_install_in_context(ctx, counter, cpu);
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004281 ++ctx->generation;
Paul Mackerrasd859e292009-01-17 18:10:22 +11004282 mutex_unlock(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01004283
Peter Zijlstra082ff5a2009-05-23 18:29:00 +02004284 counter->owner = current;
4285 get_task_struct(current);
4286 mutex_lock(&current->perf_counter_mutex);
4287 list_add_tail(&counter->owner_entry, &current->perf_counter_list);
4288 mutex_unlock(&current->perf_counter_mutex);
4289
Ingo Molnar9b51f662008-12-12 13:49:45 +01004290 fput_light(counter_file, fput_needed2);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004291
Ingo Molnar04289bb2008-12-11 08:38:42 +01004292out_fput:
4293 fput_light(group_file, fput_needed);
4294
Thomas Gleixner0793a612008-12-04 20:12:29 +01004295 return ret;
4296
Ingo Molnar9b51f662008-12-12 13:49:45 +01004297err_free_put_context:
Thomas Gleixner0793a612008-12-04 20:12:29 +01004298 kfree(counter);
4299
4300err_put_context:
Paul Mackerrasc93f7662009-05-28 22:18:17 +10004301 put_ctx(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004302
Ingo Molnar04289bb2008-12-11 08:38:42 +01004303 goto out_fput;
Thomas Gleixner0793a612008-12-04 20:12:29 +01004304}
4305
Ingo Molnar9b51f662008-12-12 13:49:45 +01004306/*
Ingo Molnar9b51f662008-12-12 13:49:45 +01004307 * inherit a counter from parent task to child task:
4308 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11004309static struct perf_counter *
Ingo Molnar9b51f662008-12-12 13:49:45 +01004310inherit_counter(struct perf_counter *parent_counter,
4311 struct task_struct *parent,
4312 struct perf_counter_context *parent_ctx,
4313 struct task_struct *child,
Paul Mackerrasd859e292009-01-17 18:10:22 +11004314 struct perf_counter *group_leader,
Ingo Molnar9b51f662008-12-12 13:49:45 +01004315 struct perf_counter_context *child_ctx)
4316{
4317 struct perf_counter *child_counter;
4318
Paul Mackerrasd859e292009-01-17 18:10:22 +11004319 /*
4320 * Instead of creating recursive hierarchies of counters,
4321 * we link inherited counters back to the original parent,
4322 * which has a filp for sure, which we use as the reference
4323 * count:
4324 */
4325 if (parent_counter->parent)
4326 parent_counter = parent_counter->parent;
4327
Peter Zijlstra0d486962009-06-02 19:22:16 +02004328 child_counter = perf_counter_alloc(&parent_counter->attr,
Paul Mackerras23a185c2009-02-09 22:42:47 +11004329 parent_counter->cpu, child_ctx,
Peter Zijlstrab84fbc92009-06-22 13:57:40 +02004330 group_leader, parent_counter,
4331 GFP_KERNEL);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02004332 if (IS_ERR(child_counter))
4333 return child_counter;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10004334 get_ctx(child_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01004335
4336 /*
Paul Mackerras564c2b22009-05-22 14:27:22 +10004337 * Make the child state follow the state of the parent counter,
Peter Zijlstra0d486962009-06-02 19:22:16 +02004338 * not its attr.disabled bit. We hold the parent's mutex,
Ingo Molnar22a4f652009-06-01 10:13:37 +02004339 * so we won't race with perf_counter_{en, dis}able_family.
Paul Mackerras564c2b22009-05-22 14:27:22 +10004340 */
4341 if (parent_counter->state >= PERF_COUNTER_STATE_INACTIVE)
4342 child_counter->state = PERF_COUNTER_STATE_INACTIVE;
4343 else
4344 child_counter->state = PERF_COUNTER_STATE_OFF;
4345
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02004346 if (parent_counter->attr.freq)
4347 child_counter->hw.sample_period = parent_counter->hw.sample_period;
4348
Paul Mackerras564c2b22009-05-22 14:27:22 +10004349 /*
Ingo Molnar9b51f662008-12-12 13:49:45 +01004350 * Link it up in the child's context:
4351 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +11004352 add_counter_to_ctx(child_counter, child_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01004353
Ingo Molnar9b51f662008-12-12 13:49:45 +01004354 /*
4355 * Get a reference to the parent filp - we will fput it
4356 * when the child counter exits. This is safe to do because
4357 * we are in the parent and we know that the filp still
4358 * exists and has a nonzero count:
4359 */
4360 atomic_long_inc(&parent_counter->filp->f_count);
4361
Paul Mackerrasd859e292009-01-17 18:10:22 +11004362 /*
4363 * Link this into the parent counter's child list
4364 */
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004365 WARN_ON_ONCE(parent_counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02004366 mutex_lock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11004367 list_add_tail(&child_counter->child_list, &parent_counter->child_list);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02004368 mutex_unlock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11004369
4370 return child_counter;
4371}
4372
4373static int inherit_group(struct perf_counter *parent_counter,
4374 struct task_struct *parent,
4375 struct perf_counter_context *parent_ctx,
4376 struct task_struct *child,
4377 struct perf_counter_context *child_ctx)
4378{
4379 struct perf_counter *leader;
4380 struct perf_counter *sub;
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02004381 struct perf_counter *child_ctr;
Paul Mackerrasd859e292009-01-17 18:10:22 +11004382
4383 leader = inherit_counter(parent_counter, parent, parent_ctx,
4384 child, NULL, child_ctx);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02004385 if (IS_ERR(leader))
4386 return PTR_ERR(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +11004387 list_for_each_entry(sub, &parent_counter->sibling_list, list_entry) {
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02004388 child_ctr = inherit_counter(sub, parent, parent_ctx,
4389 child, leader, child_ctx);
4390 if (IS_ERR(child_ctr))
4391 return PTR_ERR(child_ctr);
Paul Mackerrasd859e292009-01-17 18:10:22 +11004392 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01004393 return 0;
4394}
4395
Paul Mackerrasd859e292009-01-17 18:10:22 +11004396static void sync_child_counter(struct perf_counter *child_counter,
Peter Zijlstra38b200d2009-06-23 20:13:11 +02004397 struct task_struct *child)
Paul Mackerrasd859e292009-01-17 18:10:22 +11004398{
Peter Zijlstra38b200d2009-06-23 20:13:11 +02004399 struct perf_counter *parent_counter = child_counter->parent;
Peter Zijlstra8bc20952009-05-15 20:45:59 +02004400 u64 child_val;
Paul Mackerrasd859e292009-01-17 18:10:22 +11004401
Peter Zijlstrabfbd3382009-06-24 21:11:59 +02004402 if (child_counter->attr.inherit_stat)
4403 perf_counter_read_event(child_counter, child);
Peter Zijlstra38b200d2009-06-23 20:13:11 +02004404
Paul Mackerrasd859e292009-01-17 18:10:22 +11004405 child_val = atomic64_read(&child_counter->count);
4406
4407 /*
4408 * Add back the child's count to the parent's count:
4409 */
4410 atomic64_add(child_val, &parent_counter->count);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11004411 atomic64_add(child_counter->total_time_enabled,
4412 &parent_counter->child_total_time_enabled);
4413 atomic64_add(child_counter->total_time_running,
4414 &parent_counter->child_total_time_running);
Paul Mackerrasd859e292009-01-17 18:10:22 +11004415
4416 /*
4417 * Remove this counter from the parent's list
4418 */
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004419 WARN_ON_ONCE(parent_counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02004420 mutex_lock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11004421 list_del_init(&child_counter->child_list);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02004422 mutex_unlock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11004423
4424 /*
4425 * Release the parent counter, if this was the last
4426 * reference to it.
4427 */
4428 fput(parent_counter->filp);
4429}
4430
Ingo Molnar9b51f662008-12-12 13:49:45 +01004431static void
Peter Zijlstrabbbee902009-05-29 14:25:58 +02004432__perf_counter_exit_task(struct perf_counter *child_counter,
Peter Zijlstra38b200d2009-06-23 20:13:11 +02004433 struct perf_counter_context *child_ctx,
4434 struct task_struct *child)
Ingo Molnar9b51f662008-12-12 13:49:45 +01004435{
4436 struct perf_counter *parent_counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004437
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004438 update_counter_times(child_counter);
Peter Zijlstraaa9c67f2009-05-23 18:28:59 +02004439 perf_counter_remove_from_context(child_counter);
Ingo Molnar0cc0c022008-12-14 23:20:36 +01004440
Ingo Molnar9b51f662008-12-12 13:49:45 +01004441 parent_counter = child_counter->parent;
4442 /*
4443 * It can happen that parent exits first, and has counters
4444 * that are still around due to the child reference. These
4445 * counters need to be zapped - but otherwise linger.
4446 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11004447 if (parent_counter) {
Peter Zijlstra38b200d2009-06-23 20:13:11 +02004448 sync_child_counter(child_counter, child);
Peter Zijlstraf1600952009-03-19 20:26:16 +01004449 free_counter(child_counter);
Paul Mackerras4bcf3492009-02-11 13:53:19 +01004450 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01004451}
4452
4453/*
Paul Mackerrasd859e292009-01-17 18:10:22 +11004454 * When a child task exits, feed back counter values to parent counters.
Ingo Molnar9b51f662008-12-12 13:49:45 +01004455 */
4456void perf_counter_exit_task(struct task_struct *child)
4457{
4458 struct perf_counter *child_counter, *tmp;
4459 struct perf_counter_context *child_ctx;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004460 unsigned long flags;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004461
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02004462 if (likely(!child->perf_counter_ctxp)) {
Peter Zijlstra3a80b4a2009-08-07 19:49:01 +02004463 perf_counter_task(child, NULL, 0);
Ingo Molnar9b51f662008-12-12 13:49:45 +01004464 return;
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02004465 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01004466
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004467 local_irq_save(flags);
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004468 /*
4469 * We can't reschedule here because interrupts are disabled,
4470 * and either child is current or it is a task that can't be
4471 * scheduled, so we are now safe from rescheduling changing
4472 * our context.
4473 */
4474 child_ctx = child->perf_counter_ctxp;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004475 __perf_counter_task_sched_out(child_ctx);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10004476
4477 /*
4478 * Take the context lock here so that if find_get_context is
4479 * reading child->perf_counter_ctxp, we wait until it has
4480 * incremented the context's refcount before we do put_ctx below.
4481 */
4482 spin_lock(&child_ctx->lock);
Peter Zijlstra3a80b4a2009-08-07 19:49:01 +02004483 child->perf_counter_ctxp = NULL;
Peter Zijlstra71a851b2009-07-10 09:06:56 +02004484 /*
4485 * If this context is a clone; unclone it so it can't get
4486 * swapped to another process while we're removing all
4487 * the counters from it.
4488 */
4489 unclone_ctx(child_ctx);
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02004490 spin_unlock_irqrestore(&child_ctx->lock, flags);
4491
4492 /*
4493 * Report the task dead after unscheduling the counters so that we
4494 * won't get any samples after PERF_EVENT_EXIT. We can however still
4495 * get a few PERF_EVENT_READ events.
4496 */
Peter Zijlstra3a80b4a2009-08-07 19:49:01 +02004497 perf_counter_task(child, child_ctx, 0);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004498
Peter Zijlstra66fff222009-06-10 22:53:37 +02004499 /*
4500 * We can recurse on the same lock type through:
4501 *
4502 * __perf_counter_exit_task()
4503 * sync_child_counter()
4504 * fput(parent_counter->filp)
4505 * perf_release()
4506 * mutex_lock(&ctx->mutex)
4507 *
4508 * But since its the parent context it won't be the same instance.
4509 */
4510 mutex_lock_nested(&child_ctx->mutex, SINGLE_DEPTH_NESTING);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004511
Peter Zijlstra8bc20952009-05-15 20:45:59 +02004512again:
Ingo Molnar9b51f662008-12-12 13:49:45 +01004513 list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list,
4514 list_entry)
Peter Zijlstra38b200d2009-06-23 20:13:11 +02004515 __perf_counter_exit_task(child_counter, child_ctx, child);
Peter Zijlstra8bc20952009-05-15 20:45:59 +02004516
4517 /*
4518 * If the last counter was a group counter, it will have appended all
4519 * its siblings to the list, but we obtained 'tmp' before that which
4520 * will still point to the list head terminating the iteration.
4521 */
4522 if (!list_empty(&child_ctx->counter_list))
4523 goto again;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004524
4525 mutex_unlock(&child_ctx->mutex);
4526
4527 put_ctx(child_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01004528}
4529
4530/*
Peter Zijlstrabbbee902009-05-29 14:25:58 +02004531 * free an unexposed, unused context as created by inheritance by
4532 * init_task below, used by fork() in case of fail.
4533 */
4534void perf_counter_free_task(struct task_struct *task)
4535{
4536 struct perf_counter_context *ctx = task->perf_counter_ctxp;
4537 struct perf_counter *counter, *tmp;
4538
4539 if (!ctx)
4540 return;
4541
4542 mutex_lock(&ctx->mutex);
4543again:
4544 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry) {
4545 struct perf_counter *parent = counter->parent;
4546
4547 if (WARN_ON_ONCE(!parent))
4548 continue;
4549
4550 mutex_lock(&parent->child_mutex);
4551 list_del_init(&counter->child_list);
4552 mutex_unlock(&parent->child_mutex);
4553
4554 fput(parent->filp);
4555
4556 list_del_counter(counter, ctx);
4557 free_counter(counter);
4558 }
4559
4560 if (!list_empty(&ctx->counter_list))
4561 goto again;
4562
4563 mutex_unlock(&ctx->mutex);
4564
4565 put_ctx(ctx);
4566}
4567
4568/*
Ingo Molnar9b51f662008-12-12 13:49:45 +01004569 * Initialize the perf_counter context in task_struct
4570 */
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02004571int perf_counter_init_task(struct task_struct *child)
Ingo Molnar9b51f662008-12-12 13:49:45 +01004572{
4573 struct perf_counter_context *child_ctx, *parent_ctx;
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004574 struct perf_counter_context *cloned_ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +11004575 struct perf_counter *counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004576 struct task_struct *parent = current;
Paul Mackerras564c2b22009-05-22 14:27:22 +10004577 int inherited_all = 1;
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02004578 int ret = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004579
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004580 child->perf_counter_ctxp = NULL;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004581
Peter Zijlstra082ff5a2009-05-23 18:29:00 +02004582 mutex_init(&child->perf_counter_mutex);
4583 INIT_LIST_HEAD(&child->perf_counter_list);
4584
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004585 if (likely(!parent->perf_counter_ctxp))
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02004586 return 0;
4587
Ingo Molnar9b51f662008-12-12 13:49:45 +01004588 /*
4589 * This is executed from the parent task context, so inherit
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004590 * counters that have been marked for cloning.
4591 * First allocate and initialize a context for the child.
Ingo Molnar9b51f662008-12-12 13:49:45 +01004592 */
4593
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004594 child_ctx = kmalloc(sizeof(struct perf_counter_context), GFP_KERNEL);
4595 if (!child_ctx)
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02004596 return -ENOMEM;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004597
4598 __perf_counter_init_context(child_ctx, child);
4599 child->perf_counter_ctxp = child_ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10004600 get_task_struct(child);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004601
Ingo Molnar9b51f662008-12-12 13:49:45 +01004602 /*
Paul Mackerras25346b92009-06-01 17:48:12 +10004603 * If the parent's context is a clone, pin it so it won't get
4604 * swapped under us.
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004605 */
Paul Mackerras25346b92009-06-01 17:48:12 +10004606 parent_ctx = perf_pin_task_context(parent);
4607
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004608 /*
4609 * No need to check if parent_ctx != NULL here; since we saw
4610 * it non-NULL earlier, the only reason for it to become NULL
4611 * is if we exit, and since we're currently in the middle of
4612 * a fork we can't be exiting at the same time.
4613 */
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004614
4615 /*
Ingo Molnar9b51f662008-12-12 13:49:45 +01004616 * Lock the parent list. No need to lock the child - not PID
4617 * hashed yet and not running, so nobody can access it.
4618 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11004619 mutex_lock(&parent_ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01004620
4621 /*
4622 * We dont have to disable NMIs - we are only looking at
4623 * the list, not manipulating it:
4624 */
Peter Zijlstrad7b629a2009-05-20 12:21:19 +02004625 list_for_each_entry_rcu(counter, &parent_ctx->event_list, event_entry) {
4626 if (counter != counter->group_leader)
4627 continue;
4628
Peter Zijlstra0d486962009-06-02 19:22:16 +02004629 if (!counter->attr.inherit) {
Paul Mackerras564c2b22009-05-22 14:27:22 +10004630 inherited_all = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004631 continue;
Paul Mackerras564c2b22009-05-22 14:27:22 +10004632 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01004633
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02004634 ret = inherit_group(counter, parent, parent_ctx,
4635 child, child_ctx);
4636 if (ret) {
Paul Mackerras564c2b22009-05-22 14:27:22 +10004637 inherited_all = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004638 break;
Paul Mackerras564c2b22009-05-22 14:27:22 +10004639 }
4640 }
4641
4642 if (inherited_all) {
4643 /*
4644 * Mark the child context as a clone of the parent
4645 * context, or of whatever the parent is a clone of.
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004646 * Note that if the parent is a clone, it could get
4647 * uncloned at any point, but that doesn't matter
4648 * because the list of counters and the generation
4649 * count can't have changed since we took the mutex.
Paul Mackerras564c2b22009-05-22 14:27:22 +10004650 */
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004651 cloned_ctx = rcu_dereference(parent_ctx->parent_ctx);
4652 if (cloned_ctx) {
4653 child_ctx->parent_ctx = cloned_ctx;
Paul Mackerras25346b92009-06-01 17:48:12 +10004654 child_ctx->parent_gen = parent_ctx->parent_gen;
Paul Mackerras564c2b22009-05-22 14:27:22 +10004655 } else {
4656 child_ctx->parent_ctx = parent_ctx;
4657 child_ctx->parent_gen = parent_ctx->generation;
4658 }
4659 get_ctx(child_ctx->parent_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01004660 }
4661
Paul Mackerrasd859e292009-01-17 18:10:22 +11004662 mutex_unlock(&parent_ctx->mutex);
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02004663
Paul Mackerras25346b92009-06-01 17:48:12 +10004664 perf_unpin_context(parent_ctx);
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004665
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02004666 return ret;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004667}
4668
Ingo Molnar04289bb2008-12-11 08:38:42 +01004669static void __cpuinit perf_counter_init_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01004670{
Ingo Molnar04289bb2008-12-11 08:38:42 +01004671 struct perf_cpu_context *cpuctx;
Thomas Gleixner0793a612008-12-04 20:12:29 +01004672
Ingo Molnar04289bb2008-12-11 08:38:42 +01004673 cpuctx = &per_cpu(perf_cpu_context, cpu);
4674 __perf_counter_init_context(&cpuctx->ctx, NULL);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004675
Ingo Molnar1dce8d92009-05-04 19:23:18 +02004676 spin_lock(&perf_resource_lock);
Ingo Molnar04289bb2008-12-11 08:38:42 +01004677 cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu;
Ingo Molnar1dce8d92009-05-04 19:23:18 +02004678 spin_unlock(&perf_resource_lock);
Ingo Molnar04289bb2008-12-11 08:38:42 +01004679
Paul Mackerras01d02872009-01-14 13:44:19 +11004680 hw_perf_counter_setup(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004681}
4682
4683#ifdef CONFIG_HOTPLUG_CPU
Ingo Molnar04289bb2008-12-11 08:38:42 +01004684static void __perf_counter_exit_cpu(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01004685{
4686 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
4687 struct perf_counter_context *ctx = &cpuctx->ctx;
4688 struct perf_counter *counter, *tmp;
4689
Ingo Molnar04289bb2008-12-11 08:38:42 +01004690 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry)
4691 __perf_counter_remove_from_context(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004692}
Ingo Molnar04289bb2008-12-11 08:38:42 +01004693static void perf_counter_exit_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01004694{
Paul Mackerrasd859e292009-01-17 18:10:22 +11004695 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
4696 struct perf_counter_context *ctx = &cpuctx->ctx;
4697
4698 mutex_lock(&ctx->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01004699 smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1);
Paul Mackerrasd859e292009-01-17 18:10:22 +11004700 mutex_unlock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004701}
4702#else
Ingo Molnar04289bb2008-12-11 08:38:42 +01004703static inline void perf_counter_exit_cpu(int cpu) { }
Thomas Gleixner0793a612008-12-04 20:12:29 +01004704#endif
4705
4706static int __cpuinit
4707perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
4708{
4709 unsigned int cpu = (long)hcpu;
4710
4711 switch (action) {
4712
4713 case CPU_UP_PREPARE:
4714 case CPU_UP_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01004715 perf_counter_init_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004716 break;
4717
Ingo Molnar28402972009-08-13 10:13:22 +02004718 case CPU_ONLINE:
4719 case CPU_ONLINE_FROZEN:
4720 hw_perf_counter_setup_online(cpu);
4721 break;
4722
Thomas Gleixner0793a612008-12-04 20:12:29 +01004723 case CPU_DOWN_PREPARE:
4724 case CPU_DOWN_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01004725 perf_counter_exit_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004726 break;
4727
4728 default:
4729 break;
4730 }
4731
4732 return NOTIFY_OK;
4733}
4734
Paul Mackerrasf38b0822009-06-02 21:05:16 +10004735/*
4736 * This has to have a higher priority than migration_notifier in sched.c.
4737 */
Thomas Gleixner0793a612008-12-04 20:12:29 +01004738static struct notifier_block __cpuinitdata perf_cpu_nb = {
4739 .notifier_call = perf_cpu_notify,
Paul Mackerrasf38b0822009-06-02 21:05:16 +10004740 .priority = 20,
Thomas Gleixner0793a612008-12-04 20:12:29 +01004741};
4742
Ingo Molnar0d905bc2009-05-04 19:13:30 +02004743void __init perf_counter_init(void)
Thomas Gleixner0793a612008-12-04 20:12:29 +01004744{
4745 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
4746 (void *)(long)smp_processor_id());
Ingo Molnar28402972009-08-13 10:13:22 +02004747 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_ONLINE,
4748 (void *)(long)smp_processor_id());
Thomas Gleixner0793a612008-12-04 20:12:29 +01004749 register_cpu_notifier(&perf_cpu_nb);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004750}
Thomas Gleixner0793a612008-12-04 20:12:29 +01004751
4752static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
4753{
4754 return sprintf(buf, "%d\n", perf_reserved_percpu);
4755}
4756
4757static ssize_t
4758perf_set_reserve_percpu(struct sysdev_class *class,
4759 const char *buf,
4760 size_t count)
4761{
4762 struct perf_cpu_context *cpuctx;
4763 unsigned long val;
4764 int err, cpu, mpt;
4765
4766 err = strict_strtoul(buf, 10, &val);
4767 if (err)
4768 return err;
4769 if (val > perf_max_counters)
4770 return -EINVAL;
4771
Ingo Molnar1dce8d92009-05-04 19:23:18 +02004772 spin_lock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004773 perf_reserved_percpu = val;
4774 for_each_online_cpu(cpu) {
4775 cpuctx = &per_cpu(perf_cpu_context, cpu);
4776 spin_lock_irq(&cpuctx->ctx.lock);
4777 mpt = min(perf_max_counters - cpuctx->ctx.nr_counters,
4778 perf_max_counters - perf_reserved_percpu);
4779 cpuctx->max_pertask = mpt;
4780 spin_unlock_irq(&cpuctx->ctx.lock);
4781 }
Ingo Molnar1dce8d92009-05-04 19:23:18 +02004782 spin_unlock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004783
4784 return count;
4785}
4786
4787static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
4788{
4789 return sprintf(buf, "%d\n", perf_overcommit);
4790}
4791
4792static ssize_t
4793perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
4794{
4795 unsigned long val;
4796 int err;
4797
4798 err = strict_strtoul(buf, 10, &val);
4799 if (err)
4800 return err;
4801 if (val > 1)
4802 return -EINVAL;
4803
Ingo Molnar1dce8d92009-05-04 19:23:18 +02004804 spin_lock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004805 perf_overcommit = val;
Ingo Molnar1dce8d92009-05-04 19:23:18 +02004806 spin_unlock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004807
4808 return count;
4809}
4810
4811static SYSDEV_CLASS_ATTR(
4812 reserve_percpu,
4813 0644,
4814 perf_show_reserve_percpu,
4815 perf_set_reserve_percpu
4816 );
4817
4818static SYSDEV_CLASS_ATTR(
4819 overcommit,
4820 0644,
4821 perf_show_overcommit,
4822 perf_set_overcommit
4823 );
4824
4825static struct attribute *perfclass_attrs[] = {
4826 &attr_reserve_percpu.attr,
4827 &attr_overcommit.attr,
4828 NULL
4829};
4830
4831static struct attribute_group perfclass_attr_group = {
4832 .attrs = perfclass_attrs,
4833 .name = "perf_counters",
4834};
4835
4836static int __init perf_counter_sysfs_init(void)
4837{
4838 return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
4839 &perfclass_attr_group);
4840}
4841device_initcall(perf_counter_sysfs_init);