blob: 06bf6a4f260842f20dadd844ffad7fe9d2a03dbb [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 Zijlstra970892a2009-08-13 11:47:54 +0200310 if (counter->pending_disable) {
311 counter->pending_disable = 0;
312 counter->state = PERF_COUNTER_STATE_OFF;
313 }
Peter Zijlstra4af49982009-04-06 11:45:10 +0200314 counter->tstamp_stopped = ctx->time;
Robert Richter4aeb0b42009-04-29 12:47:03 +0200315 counter->pmu->disable(counter);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100316 counter->oncpu = -1;
317
318 if (!is_software_counter(counter))
319 cpuctx->active_oncpu--;
320 ctx->nr_active--;
Peter Zijlstra0d486962009-06-02 19:22:16 +0200321 if (counter->attr.exclusive || !cpuctx->active_oncpu)
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100322 cpuctx->exclusive = 0;
323}
324
Paul Mackerrasd859e292009-01-17 18:10:22 +1100325static void
326group_sched_out(struct perf_counter *group_counter,
327 struct perf_cpu_context *cpuctx,
328 struct perf_counter_context *ctx)
329{
330 struct perf_counter *counter;
331
332 if (group_counter->state != PERF_COUNTER_STATE_ACTIVE)
333 return;
334
335 counter_sched_out(group_counter, cpuctx, ctx);
336
337 /*
338 * Schedule out siblings (if any):
339 */
340 list_for_each_entry(counter, &group_counter->sibling_list, list_entry)
341 counter_sched_out(counter, cpuctx, ctx);
342
Peter Zijlstra0d486962009-06-02 19:22:16 +0200343 if (group_counter->attr.exclusive)
Paul Mackerrasd859e292009-01-17 18:10:22 +1100344 cpuctx->exclusive = 0;
345}
346
Thomas Gleixner0793a612008-12-04 20:12:29 +0100347/*
348 * Cross CPU call to remove a performance counter
349 *
350 * We disable the counter on the hardware level first. After that we
351 * remove it from the context list.
352 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100353static void __perf_counter_remove_from_context(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100354{
355 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
356 struct perf_counter *counter = info;
357 struct perf_counter_context *ctx = counter->ctx;
358
359 /*
360 * If this is a task context, we need to check whether it is
361 * the current task context of this cpu. If not it has been
362 * scheduled out before the smp call arrived.
363 */
Peter Zijlstra665c2142009-05-29 14:51:57 +0200364 if (ctx->task && cpuctx->task_ctx != ctx)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100365 return;
366
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200367 spin_lock(&ctx->lock);
Ingo Molnar34adc802009-05-20 20:13:28 +0200368 /*
369 * Protect the list operation against NMI by disabling the
370 * counters on a global level.
371 */
372 perf_disable();
Thomas Gleixner0793a612008-12-04 20:12:29 +0100373
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100374 counter_sched_out(counter, cpuctx, ctx);
375
Ingo Molnar04289bb2008-12-11 08:38:42 +0100376 list_del_counter(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100377
378 if (!ctx->task) {
379 /*
380 * Allow more per task counters with respect to the
381 * reservation:
382 */
383 cpuctx->max_pertask =
384 min(perf_max_counters - ctx->nr_counters,
385 perf_max_counters - perf_reserved_percpu);
386 }
387
Ingo Molnar34adc802009-05-20 20:13:28 +0200388 perf_enable();
Peter Zijlstra665c2142009-05-29 14:51:57 +0200389 spin_unlock(&ctx->lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100390}
391
392
393/*
394 * Remove the counter from a task's (or a CPU's) list of counters.
395 *
Peter Zijlstrafccc7142009-05-23 18:28:56 +0200396 * Must be called with ctx->mutex held.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100397 *
398 * CPU counters are removed with a smp call. For task counters we only
399 * call when the task is on a CPU.
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000400 *
401 * If counter->ctx is a cloned context, callers must make sure that
402 * every task struct that counter->ctx->task could possibly point to
403 * remains valid. This is OK when called from perf_release since
404 * that only calls us on the top-level context, which can't be a clone.
405 * When called from perf_counter_exit_task, it's OK because the
406 * context has been detached from its task.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100407 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100408static void perf_counter_remove_from_context(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100409{
410 struct perf_counter_context *ctx = counter->ctx;
411 struct task_struct *task = ctx->task;
412
413 if (!task) {
414 /*
415 * Per cpu counters are removed via an smp call and
416 * the removal is always sucessful.
417 */
418 smp_call_function_single(counter->cpu,
Ingo Molnar04289bb2008-12-11 08:38:42 +0100419 __perf_counter_remove_from_context,
Thomas Gleixner0793a612008-12-04 20:12:29 +0100420 counter, 1);
421 return;
422 }
423
424retry:
Ingo Molnar04289bb2008-12-11 08:38:42 +0100425 task_oncpu_function_call(task, __perf_counter_remove_from_context,
Thomas Gleixner0793a612008-12-04 20:12:29 +0100426 counter);
427
428 spin_lock_irq(&ctx->lock);
429 /*
430 * If the context is active we need to retry the smp call.
431 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100432 if (ctx->nr_active && !list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100433 spin_unlock_irq(&ctx->lock);
434 goto retry;
435 }
436
437 /*
438 * The lock prevents that this context is scheduled in so we
Ingo Molnar04289bb2008-12-11 08:38:42 +0100439 * can remove the counter safely, if the call above did not
Thomas Gleixner0793a612008-12-04 20:12:29 +0100440 * succeed.
441 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100442 if (!list_empty(&counter->list_entry)) {
Ingo Molnar04289bb2008-12-11 08:38:42 +0100443 list_del_counter(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100444 }
445 spin_unlock_irq(&ctx->lock);
446}
447
Peter Zijlstra4af49982009-04-06 11:45:10 +0200448static inline u64 perf_clock(void)
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100449{
Peter Zijlstra4af49982009-04-06 11:45:10 +0200450 return cpu_clock(smp_processor_id());
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100451}
452
453/*
454 * Update the record of the current time in a context.
455 */
Peter Zijlstra4af49982009-04-06 11:45:10 +0200456static void update_context_time(struct perf_counter_context *ctx)
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100457{
Peter Zijlstra4af49982009-04-06 11:45:10 +0200458 u64 now = perf_clock();
459
460 ctx->time += now - ctx->timestamp;
461 ctx->timestamp = now;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100462}
463
464/*
465 * Update the total_time_enabled and total_time_running fields for a counter.
466 */
467static void update_counter_times(struct perf_counter *counter)
468{
469 struct perf_counter_context *ctx = counter->ctx;
470 u64 run_end;
471
Paul Mackerrasfa289be2009-08-25 15:17:20 +1000472 if (counter->state < PERF_COUNTER_STATE_INACTIVE ||
473 counter->group_leader->state < PERF_COUNTER_STATE_INACTIVE)
Peter Zijlstra4af49982009-04-06 11:45:10 +0200474 return;
475
476 counter->total_time_enabled = ctx->time - counter->tstamp_enabled;
477
478 if (counter->state == PERF_COUNTER_STATE_INACTIVE)
479 run_end = counter->tstamp_stopped;
480 else
481 run_end = ctx->time;
482
483 counter->total_time_running = run_end - counter->tstamp_running;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100484}
485
486/*
487 * Update total_time_enabled and total_time_running for all counters in a group.
488 */
489static void update_group_times(struct perf_counter *leader)
490{
491 struct perf_counter *counter;
492
493 update_counter_times(leader);
494 list_for_each_entry(counter, &leader->sibling_list, list_entry)
495 update_counter_times(counter);
496}
497
498/*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100499 * Cross CPU call to disable a performance counter
500 */
501static void __perf_counter_disable(void *info)
502{
503 struct perf_counter *counter = info;
504 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
505 struct perf_counter_context *ctx = counter->ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100506
507 /*
508 * If this is a per-task counter, need to check whether this
509 * counter's task is the current task on this cpu.
510 */
Peter Zijlstra665c2142009-05-29 14:51:57 +0200511 if (ctx->task && cpuctx->task_ctx != ctx)
Paul Mackerrasd859e292009-01-17 18:10:22 +1100512 return;
513
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200514 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100515
516 /*
517 * If the counter is on, turn it off.
518 * If it is in error state, leave it in error state.
519 */
520 if (counter->state >= PERF_COUNTER_STATE_INACTIVE) {
Peter Zijlstra4af49982009-04-06 11:45:10 +0200521 update_context_time(ctx);
Paul Mackerrasfa289be2009-08-25 15:17:20 +1000522 update_group_times(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100523 if (counter == counter->group_leader)
524 group_sched_out(counter, cpuctx, ctx);
525 else
526 counter_sched_out(counter, cpuctx, ctx);
527 counter->state = PERF_COUNTER_STATE_OFF;
528 }
529
Peter Zijlstra665c2142009-05-29 14:51:57 +0200530 spin_unlock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100531}
532
533/*
534 * Disable a counter.
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000535 *
536 * If counter->ctx is a cloned context, callers must make sure that
537 * every task struct that counter->ctx->task could possibly point to
538 * remains valid. This condition is satisifed when called through
539 * perf_counter_for_each_child or perf_counter_for_each because they
540 * hold the top-level counter's child_mutex, so any descendant that
541 * goes to exit will block in sync_child_counter.
542 * When called from perf_pending_counter it's OK because counter->ctx
543 * is the current context on this CPU and preemption is disabled,
544 * hence we can't get into perf_counter_task_sched_out for this context.
Paul Mackerrasd859e292009-01-17 18:10:22 +1100545 */
546static void perf_counter_disable(struct perf_counter *counter)
547{
548 struct perf_counter_context *ctx = counter->ctx;
549 struct task_struct *task = ctx->task;
550
551 if (!task) {
552 /*
553 * Disable the counter on the cpu that it's on
554 */
555 smp_call_function_single(counter->cpu, __perf_counter_disable,
556 counter, 1);
557 return;
558 }
559
560 retry:
561 task_oncpu_function_call(task, __perf_counter_disable, counter);
562
563 spin_lock_irq(&ctx->lock);
564 /*
565 * If the counter is still active, we need to retry the cross-call.
566 */
567 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
568 spin_unlock_irq(&ctx->lock);
569 goto retry;
570 }
571
572 /*
573 * Since we have the lock this context can't be scheduled
574 * in, so we can change the state safely.
575 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100576 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
Paul Mackerrasfa289be2009-08-25 15:17:20 +1000577 update_group_times(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100578 counter->state = PERF_COUNTER_STATE_OFF;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100579 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100580
581 spin_unlock_irq(&ctx->lock);
582}
583
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100584static int
585counter_sched_in(struct perf_counter *counter,
586 struct perf_cpu_context *cpuctx,
587 struct perf_counter_context *ctx,
588 int cpu)
589{
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100590 if (counter->state <= PERF_COUNTER_STATE_OFF)
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100591 return 0;
592
593 counter->state = PERF_COUNTER_STATE_ACTIVE;
594 counter->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */
595 /*
596 * The new state must be visible before we turn it on in the hardware:
597 */
598 smp_wmb();
599
Robert Richter4aeb0b42009-04-29 12:47:03 +0200600 if (counter->pmu->enable(counter)) {
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100601 counter->state = PERF_COUNTER_STATE_INACTIVE;
602 counter->oncpu = -1;
603 return -EAGAIN;
604 }
605
Peter Zijlstra4af49982009-04-06 11:45:10 +0200606 counter->tstamp_running += ctx->time - counter->tstamp_stopped;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100607
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100608 if (!is_software_counter(counter))
609 cpuctx->active_oncpu++;
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100610 ctx->nr_active++;
611
Peter Zijlstra0d486962009-06-02 19:22:16 +0200612 if (counter->attr.exclusive)
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100613 cpuctx->exclusive = 1;
614
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100615 return 0;
616}
617
Paul Mackerras6751b712009-05-11 12:08:02 +1000618static int
619group_sched_in(struct perf_counter *group_counter,
620 struct perf_cpu_context *cpuctx,
621 struct perf_counter_context *ctx,
622 int cpu)
623{
624 struct perf_counter *counter, *partial_group;
625 int ret;
626
627 if (group_counter->state == PERF_COUNTER_STATE_OFF)
628 return 0;
629
630 ret = hw_perf_group_sched_in(group_counter, cpuctx, ctx, cpu);
631 if (ret)
632 return ret < 0 ? ret : 0;
633
Paul Mackerras6751b712009-05-11 12:08:02 +1000634 if (counter_sched_in(group_counter, cpuctx, ctx, cpu))
635 return -EAGAIN;
636
637 /*
638 * Schedule in siblings as one group (if any):
639 */
640 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
Paul Mackerras6751b712009-05-11 12:08:02 +1000641 if (counter_sched_in(counter, cpuctx, ctx, cpu)) {
642 partial_group = counter;
643 goto group_error;
644 }
645 }
646
647 return 0;
648
649group_error:
650 /*
651 * Groups can be scheduled in as one unit only, so undo any
652 * partial group before returning:
653 */
654 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
655 if (counter == partial_group)
656 break;
657 counter_sched_out(counter, cpuctx, ctx);
658 }
659 counter_sched_out(group_counter, cpuctx, ctx);
660
661 return -EAGAIN;
662}
663
Thomas Gleixner0793a612008-12-04 20:12:29 +0100664/*
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100665 * Return 1 for a group consisting entirely of software counters,
666 * 0 if the group contains any hardware counters.
667 */
668static int is_software_only_group(struct perf_counter *leader)
669{
670 struct perf_counter *counter;
671
672 if (!is_software_counter(leader))
673 return 0;
Peter Zijlstra5c148192009-03-25 12:30:23 +0100674
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100675 list_for_each_entry(counter, &leader->sibling_list, list_entry)
676 if (!is_software_counter(counter))
677 return 0;
Peter Zijlstra5c148192009-03-25 12:30:23 +0100678
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100679 return 1;
680}
681
682/*
683 * Work out whether we can put this counter group on the CPU now.
684 */
685static int group_can_go_on(struct perf_counter *counter,
686 struct perf_cpu_context *cpuctx,
687 int can_add_hw)
688{
689 /*
690 * Groups consisting entirely of software counters can always go on.
691 */
692 if (is_software_only_group(counter))
693 return 1;
694 /*
695 * If an exclusive group is already on, no other hardware
696 * counters can go on.
697 */
698 if (cpuctx->exclusive)
699 return 0;
700 /*
701 * If this group is exclusive and there are already
702 * counters on the CPU, it can't go on.
703 */
Peter Zijlstra0d486962009-06-02 19:22:16 +0200704 if (counter->attr.exclusive && cpuctx->active_oncpu)
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100705 return 0;
706 /*
707 * Otherwise, try to add it if all previous groups were able
708 * to go on.
709 */
710 return can_add_hw;
711}
712
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100713static void add_counter_to_ctx(struct perf_counter *counter,
714 struct perf_counter_context *ctx)
715{
716 list_add_counter(counter, ctx);
Peter Zijlstra4af49982009-04-06 11:45:10 +0200717 counter->tstamp_enabled = ctx->time;
718 counter->tstamp_running = ctx->time;
719 counter->tstamp_stopped = ctx->time;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100720}
721
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100722/*
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100723 * Cross CPU call to install and enable a performance counter
Peter Zijlstra682076a2009-05-23 18:28:57 +0200724 *
725 * Must be called with ctx->mutex held
Thomas Gleixner0793a612008-12-04 20:12:29 +0100726 */
727static void __perf_install_in_context(void *info)
728{
729 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
730 struct perf_counter *counter = info;
731 struct perf_counter_context *ctx = counter->ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100732 struct perf_counter *leader = counter->group_leader;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100733 int cpu = smp_processor_id();
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100734 int err;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100735
736 /*
737 * If this is a task context, we need to check whether it is
738 * the current task context of this cpu. If not it has been
739 * scheduled out before the smp call arrived.
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000740 * Or possibly this is the right context but it isn't
741 * on this cpu because it had no counters.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100742 */
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000743 if (ctx->task && cpuctx->task_ctx != ctx) {
Peter Zijlstra665c2142009-05-29 14:51:57 +0200744 if (cpuctx->task_ctx || ctx->task != current)
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000745 return;
746 cpuctx->task_ctx = ctx;
747 }
Thomas Gleixner0793a612008-12-04 20:12:29 +0100748
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200749 spin_lock(&ctx->lock);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000750 ctx->is_active = 1;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200751 update_context_time(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100752
753 /*
754 * Protect the list operation against NMI by disabling the
755 * counters on a global level. NOP for non NMI based counters.
756 */
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200757 perf_disable();
Thomas Gleixner0793a612008-12-04 20:12:29 +0100758
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100759 add_counter_to_ctx(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100760
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100761 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100762 * Don't put the counter on if it is disabled or if
763 * it is in a group and the group isn't on.
764 */
765 if (counter->state != PERF_COUNTER_STATE_INACTIVE ||
766 (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE))
767 goto unlock;
768
769 /*
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100770 * An exclusive counter can't go on if there are already active
771 * hardware counters, and no hardware counter can go on if there
772 * is already an exclusive counter on.
773 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100774 if (!group_can_go_on(counter, cpuctx, 1))
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100775 err = -EEXIST;
776 else
777 err = counter_sched_in(counter, cpuctx, ctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100778
Paul Mackerrasd859e292009-01-17 18:10:22 +1100779 if (err) {
780 /*
781 * This counter couldn't go on. If it is in a group
782 * then we have to pull the whole group off.
783 * If the counter group is pinned then put it in error state.
784 */
785 if (leader != counter)
786 group_sched_out(leader, cpuctx, ctx);
Peter Zijlstra0d486962009-06-02 19:22:16 +0200787 if (leader->attr.pinned) {
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100788 update_group_times(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100789 leader->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100790 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100791 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100792
793 if (!err && !ctx->task && cpuctx->max_pertask)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100794 cpuctx->max_pertask--;
795
Paul Mackerrasd859e292009-01-17 18:10:22 +1100796 unlock:
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200797 perf_enable();
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100798
Peter Zijlstra665c2142009-05-29 14:51:57 +0200799 spin_unlock(&ctx->lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100800}
801
802/*
803 * Attach a performance counter to a context
804 *
805 * First we add the counter to the list with the hardware enable bit
806 * in counter->hw_config cleared.
807 *
808 * If the counter is attached to a task which is on a CPU we use a smp
809 * call to enable it in the task context. The task might have been
810 * scheduled away, but we check this in the smp call again.
Paul Mackerrasd859e292009-01-17 18:10:22 +1100811 *
812 * Must be called with ctx->mutex held.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100813 */
814static void
815perf_install_in_context(struct perf_counter_context *ctx,
816 struct perf_counter *counter,
817 int cpu)
818{
819 struct task_struct *task = ctx->task;
820
Thomas Gleixner0793a612008-12-04 20:12:29 +0100821 if (!task) {
822 /*
823 * Per cpu counters are installed via an smp call and
824 * the install is always sucessful.
825 */
826 smp_call_function_single(cpu, __perf_install_in_context,
827 counter, 1);
828 return;
829 }
830
Thomas Gleixner0793a612008-12-04 20:12:29 +0100831retry:
832 task_oncpu_function_call(task, __perf_install_in_context,
833 counter);
834
835 spin_lock_irq(&ctx->lock);
836 /*
Thomas Gleixner0793a612008-12-04 20:12:29 +0100837 * we need to retry the smp call.
838 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100839 if (ctx->is_active && list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100840 spin_unlock_irq(&ctx->lock);
841 goto retry;
842 }
843
844 /*
845 * The lock prevents that this context is scheduled in so we
846 * can add the counter safely, if it the call above did not
847 * succeed.
848 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100849 if (list_empty(&counter->list_entry))
850 add_counter_to_ctx(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100851 spin_unlock_irq(&ctx->lock);
852}
853
Paul Mackerrasd859e292009-01-17 18:10:22 +1100854/*
Paul Mackerrasfa289be2009-08-25 15:17:20 +1000855 * Put a counter into inactive state and update time fields.
856 * Enabling the leader of a group effectively enables all
857 * the group members that aren't explicitly disabled, so we
858 * have to update their ->tstamp_enabled also.
859 * Note: this works for group members as well as group leaders
860 * since the non-leader members' sibling_lists will be empty.
861 */
862static void __perf_counter_mark_enabled(struct perf_counter *counter,
863 struct perf_counter_context *ctx)
864{
865 struct perf_counter *sub;
866
867 counter->state = PERF_COUNTER_STATE_INACTIVE;
868 counter->tstamp_enabled = ctx->time - counter->total_time_enabled;
869 list_for_each_entry(sub, &counter->sibling_list, list_entry)
870 if (sub->state >= PERF_COUNTER_STATE_INACTIVE)
871 sub->tstamp_enabled =
872 ctx->time - sub->total_time_enabled;
873}
874
875/*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100876 * Cross CPU call to enable a performance counter
877 */
878static void __perf_counter_enable(void *info)
Ingo Molnar04289bb2008-12-11 08:38:42 +0100879{
Paul Mackerrasd859e292009-01-17 18:10:22 +1100880 struct perf_counter *counter = info;
881 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
882 struct perf_counter_context *ctx = counter->ctx;
883 struct perf_counter *leader = counter->group_leader;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100884 int err;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100885
886 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100887 * If this is a per-task counter, need to check whether this
888 * counter's task is the current task on this cpu.
Ingo Molnar04289bb2008-12-11 08:38:42 +0100889 */
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000890 if (ctx->task && cpuctx->task_ctx != ctx) {
Peter Zijlstra665c2142009-05-29 14:51:57 +0200891 if (cpuctx->task_ctx || ctx->task != current)
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000892 return;
893 cpuctx->task_ctx = ctx;
894 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100895
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200896 spin_lock(&ctx->lock);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000897 ctx->is_active = 1;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200898 update_context_time(ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100899
900 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
901 goto unlock;
Paul Mackerrasfa289be2009-08-25 15:17:20 +1000902 __perf_counter_mark_enabled(counter, ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100903
904 /*
905 * If the counter is in a group and isn't the group leader,
906 * then don't put it on unless the group is on.
907 */
908 if (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE)
909 goto unlock;
910
Paul Mackerrase758a332009-05-12 21:59:01 +1000911 if (!group_can_go_on(counter, cpuctx, 1)) {
Paul Mackerrasd859e292009-01-17 18:10:22 +1100912 err = -EEXIST;
Paul Mackerrase758a332009-05-12 21:59:01 +1000913 } else {
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200914 perf_disable();
Paul Mackerrase758a332009-05-12 21:59:01 +1000915 if (counter == leader)
916 err = group_sched_in(counter, cpuctx, ctx,
917 smp_processor_id());
918 else
919 err = counter_sched_in(counter, cpuctx, ctx,
920 smp_processor_id());
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200921 perf_enable();
Paul Mackerrase758a332009-05-12 21:59:01 +1000922 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100923
924 if (err) {
925 /*
926 * If this counter can't go on and it's part of a
927 * group, then the whole group has to come off.
928 */
929 if (leader != counter)
930 group_sched_out(leader, cpuctx, ctx);
Peter Zijlstra0d486962009-06-02 19:22:16 +0200931 if (leader->attr.pinned) {
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100932 update_group_times(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100933 leader->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100934 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100935 }
936
937 unlock:
Peter Zijlstra665c2142009-05-29 14:51:57 +0200938 spin_unlock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100939}
940
941/*
942 * Enable a counter.
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000943 *
944 * If counter->ctx is a cloned context, callers must make sure that
945 * every task struct that counter->ctx->task could possibly point to
946 * remains valid. This condition is satisfied when called through
947 * perf_counter_for_each_child or perf_counter_for_each as described
948 * for perf_counter_disable.
Paul Mackerrasd859e292009-01-17 18:10:22 +1100949 */
950static void perf_counter_enable(struct perf_counter *counter)
951{
952 struct perf_counter_context *ctx = counter->ctx;
953 struct task_struct *task = ctx->task;
954
955 if (!task) {
956 /*
957 * Enable the counter on the cpu that it's on
958 */
959 smp_call_function_single(counter->cpu, __perf_counter_enable,
960 counter, 1);
961 return;
962 }
963
964 spin_lock_irq(&ctx->lock);
965 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
966 goto out;
967
968 /*
969 * If the counter is in error state, clear that first.
970 * That way, if we see the counter in error state below, we
971 * know that it has gone back into error state, as distinct
972 * from the task having been scheduled away before the
973 * cross-call arrived.
974 */
975 if (counter->state == PERF_COUNTER_STATE_ERROR)
976 counter->state = PERF_COUNTER_STATE_OFF;
977
978 retry:
979 spin_unlock_irq(&ctx->lock);
980 task_oncpu_function_call(task, __perf_counter_enable, counter);
981
982 spin_lock_irq(&ctx->lock);
983
984 /*
985 * If the context is active and the counter is still off,
986 * we need to retry the cross-call.
987 */
988 if (ctx->is_active && counter->state == PERF_COUNTER_STATE_OFF)
989 goto retry;
990
991 /*
992 * Since we have the lock this context can't be scheduled
993 * in, so we can change the state safely.
994 */
Paul Mackerrasfa289be2009-08-25 15:17:20 +1000995 if (counter->state == PERF_COUNTER_STATE_OFF)
996 __perf_counter_mark_enabled(counter, ctx);
997
Paul Mackerrasd859e292009-01-17 18:10:22 +1100998 out:
999 spin_unlock_irq(&ctx->lock);
1000}
1001
Peter Zijlstra2023b352009-05-05 17:50:26 +02001002static int perf_counter_refresh(struct perf_counter *counter, int refresh)
Peter Zijlstra79f14642009-04-06 11:45:07 +02001003{
Peter Zijlstra2023b352009-05-05 17:50:26 +02001004 /*
1005 * not supported on inherited counters
1006 */
Peter Zijlstra0d486962009-06-02 19:22:16 +02001007 if (counter->attr.inherit)
Peter Zijlstra2023b352009-05-05 17:50:26 +02001008 return -EINVAL;
1009
Peter Zijlstra79f14642009-04-06 11:45:07 +02001010 atomic_add(refresh, &counter->event_limit);
1011 perf_counter_enable(counter);
Peter Zijlstra2023b352009-05-05 17:50:26 +02001012
1013 return 0;
Peter Zijlstra79f14642009-04-06 11:45:07 +02001014}
1015
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001016void __perf_counter_sched_out(struct perf_counter_context *ctx,
1017 struct perf_cpu_context *cpuctx)
1018{
1019 struct perf_counter *counter;
1020
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001021 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001022 ctx->is_active = 0;
1023 if (likely(!ctx->nr_counters))
1024 goto out;
Peter Zijlstra4af49982009-04-06 11:45:10 +02001025 update_context_time(ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001026
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001027 perf_disable();
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001028 if (ctx->nr_active) {
Peter Zijlstraafedadf2009-05-20 12:21:22 +02001029 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1030 if (counter != counter->group_leader)
1031 counter_sched_out(counter, cpuctx, ctx);
1032 else
1033 group_sched_out(counter, cpuctx, ctx);
1034 }
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001035 }
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001036 perf_enable();
Paul Mackerrasd859e292009-01-17 18:10:22 +11001037 out:
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001038 spin_unlock(&ctx->lock);
1039}
1040
Thomas Gleixner0793a612008-12-04 20:12:29 +01001041/*
Paul Mackerras564c2b22009-05-22 14:27:22 +10001042 * Test whether two contexts are equivalent, i.e. whether they
1043 * have both been cloned from the same version of the same context
1044 * and they both have the same number of enabled counters.
1045 * If the number of enabled counters is the same, then the set
1046 * of enabled counters should be the same, because these are both
1047 * inherited contexts, therefore we can't access individual counters
1048 * in them directly with an fd; we can only enable/disable all
1049 * counters via prctl, or enable/disable all counters in a family
1050 * via ioctl, which will have the same effect on both contexts.
1051 */
1052static int context_equiv(struct perf_counter_context *ctx1,
1053 struct perf_counter_context *ctx2)
1054{
1055 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001056 && ctx1->parent_gen == ctx2->parent_gen
Paul Mackerras25346b92009-06-01 17:48:12 +10001057 && !ctx1->pin_count && !ctx2->pin_count;
Paul Mackerras564c2b22009-05-22 14:27:22 +10001058}
1059
Peter Zijlstrabfbd3382009-06-24 21:11:59 +02001060static void __perf_counter_read(void *counter);
1061
1062static void __perf_counter_sync_stat(struct perf_counter *counter,
1063 struct perf_counter *next_counter)
1064{
1065 u64 value;
1066
1067 if (!counter->attr.inherit_stat)
1068 return;
1069
1070 /*
1071 * Update the counter value, we cannot use perf_counter_read()
1072 * because we're in the middle of a context switch and have IRQs
1073 * disabled, which upsets smp_call_function_single(), however
1074 * we know the counter must be on the current CPU, therefore we
1075 * don't need to use it.
1076 */
1077 switch (counter->state) {
1078 case PERF_COUNTER_STATE_ACTIVE:
1079 __perf_counter_read(counter);
1080 break;
1081
1082 case PERF_COUNTER_STATE_INACTIVE:
1083 update_counter_times(counter);
1084 break;
1085
1086 default:
1087 break;
1088 }
1089
1090 /*
1091 * In order to keep per-task stats reliable we need to flip the counter
1092 * values when we flip the contexts.
1093 */
1094 value = atomic64_read(&next_counter->count);
1095 value = atomic64_xchg(&counter->count, value);
1096 atomic64_set(&next_counter->count, value);
1097
Peter Zijlstra19d2e752009-06-26 13:10:23 +02001098 swap(counter->total_time_enabled, next_counter->total_time_enabled);
1099 swap(counter->total_time_running, next_counter->total_time_running);
1100
Peter Zijlstrabfbd3382009-06-24 21:11:59 +02001101 /*
Peter Zijlstra19d2e752009-06-26 13:10:23 +02001102 * Since we swizzled the values, update the user visible data too.
Peter Zijlstrabfbd3382009-06-24 21:11:59 +02001103 */
Peter Zijlstra19d2e752009-06-26 13:10:23 +02001104 perf_counter_update_userpage(counter);
1105 perf_counter_update_userpage(next_counter);
Peter Zijlstrabfbd3382009-06-24 21:11:59 +02001106}
1107
1108#define list_next_entry(pos, member) \
1109 list_entry(pos->member.next, typeof(*pos), member)
1110
1111static void perf_counter_sync_stat(struct perf_counter_context *ctx,
1112 struct perf_counter_context *next_ctx)
1113{
1114 struct perf_counter *counter, *next_counter;
1115
1116 if (!ctx->nr_stat)
1117 return;
1118
1119 counter = list_first_entry(&ctx->event_list,
1120 struct perf_counter, event_entry);
1121
1122 next_counter = list_first_entry(&next_ctx->event_list,
1123 struct perf_counter, event_entry);
1124
1125 while (&counter->event_entry != &ctx->event_list &&
1126 &next_counter->event_entry != &next_ctx->event_list) {
1127
1128 __perf_counter_sync_stat(counter, next_counter);
1129
1130 counter = list_next_entry(counter, event_entry);
Peter Zijlstra10545982009-08-06 18:06:26 +02001131 next_counter = list_next_entry(next_counter, event_entry);
Peter Zijlstrabfbd3382009-06-24 21:11:59 +02001132 }
1133}
1134
Paul Mackerras564c2b22009-05-22 14:27:22 +10001135/*
Thomas Gleixner0793a612008-12-04 20:12:29 +01001136 * Called from scheduler to remove the counters of the current task,
1137 * with interrupts disabled.
1138 *
1139 * We stop each counter and update the counter value in counter->count.
1140 *
Ingo Molnar76715812008-12-17 14:20:28 +01001141 * This does not protect us against NMI, but disable()
Thomas Gleixner0793a612008-12-04 20:12:29 +01001142 * sets the disabled bit in the control field of counter _before_
1143 * accessing the counter control register. If a NMI hits, then it will
1144 * not restart the counter.
1145 */
Paul Mackerras564c2b22009-05-22 14:27:22 +10001146void perf_counter_task_sched_out(struct task_struct *task,
1147 struct task_struct *next, int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001148{
1149 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001150 struct perf_counter_context *ctx = task->perf_counter_ctxp;
Paul Mackerras564c2b22009-05-22 14:27:22 +10001151 struct perf_counter_context *next_ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001152 struct perf_counter_context *parent;
Peter Zijlstra4a0deca2009-03-19 20:26:12 +01001153 struct pt_regs *regs;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001154 int do_switch = 1;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001155
Peter Zijlstra10989fb2009-05-25 14:45:28 +02001156 regs = task_pt_regs(task);
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +02001157 perf_swcounter_event(PERF_COUNT_SW_CONTEXT_SWITCHES, 1, 1, regs, 0);
Peter Zijlstra10989fb2009-05-25 14:45:28 +02001158
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001159 if (likely(!ctx || !cpuctx->task_ctx))
Thomas Gleixner0793a612008-12-04 20:12:29 +01001160 return;
1161
Peter Zijlstrabce379b2009-04-06 11:45:13 +02001162 update_context_time(ctx);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001163
1164 rcu_read_lock();
1165 parent = rcu_dereference(ctx->parent_ctx);
Paul Mackerras564c2b22009-05-22 14:27:22 +10001166 next_ctx = next->perf_counter_ctxp;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001167 if (parent && next_ctx &&
1168 rcu_dereference(next_ctx->parent_ctx) == parent) {
1169 /*
1170 * Looks like the two contexts are clones, so we might be
1171 * able to optimize the context switch. We lock both
1172 * contexts and check that they are clones under the
1173 * lock (including re-checking that neither has been
1174 * uncloned in the meantime). It doesn't matter which
1175 * order we take the locks because no other cpu could
1176 * be trying to lock both of these tasks.
1177 */
1178 spin_lock(&ctx->lock);
1179 spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
1180 if (context_equiv(ctx, next_ctx)) {
Peter Zijlstra665c2142009-05-29 14:51:57 +02001181 /*
1182 * XXX do we need a memory barrier of sorts
1183 * wrt to rcu_dereference() of perf_counter_ctxp
1184 */
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001185 task->perf_counter_ctxp = next_ctx;
1186 next->perf_counter_ctxp = ctx;
1187 ctx->task = next;
1188 next_ctx->task = task;
1189 do_switch = 0;
Peter Zijlstrabfbd3382009-06-24 21:11:59 +02001190
1191 perf_counter_sync_stat(ctx, next_ctx);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001192 }
1193 spin_unlock(&next_ctx->lock);
1194 spin_unlock(&ctx->lock);
Paul Mackerras564c2b22009-05-22 14:27:22 +10001195 }
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001196 rcu_read_unlock();
Paul Mackerras564c2b22009-05-22 14:27:22 +10001197
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001198 if (do_switch) {
1199 __perf_counter_sched_out(ctx, cpuctx);
1200 cpuctx->task_ctx = NULL;
1201 }
Thomas Gleixner0793a612008-12-04 20:12:29 +01001202}
1203
Peter Zijlstra665c2142009-05-29 14:51:57 +02001204/*
1205 * Called with IRQs disabled
1206 */
Paul Mackerrasa08b1592009-05-11 15:46:10 +10001207static void __perf_counter_task_sched_out(struct perf_counter_context *ctx)
1208{
1209 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1210
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001211 if (!cpuctx->task_ctx)
1212 return;
Ingo Molnar012b84d2009-05-17 11:08:41 +02001213
1214 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
1215 return;
1216
Paul Mackerrasa08b1592009-05-11 15:46:10 +10001217 __perf_counter_sched_out(ctx, cpuctx);
1218 cpuctx->task_ctx = NULL;
1219}
1220
Peter Zijlstra665c2142009-05-29 14:51:57 +02001221/*
1222 * Called with IRQs disabled
1223 */
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001224static void perf_counter_cpu_sched_out(struct perf_cpu_context *cpuctx)
Ingo Molnar04289bb2008-12-11 08:38:42 +01001225{
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001226 __perf_counter_sched_out(&cpuctx->ctx, cpuctx);
Ingo Molnar04289bb2008-12-11 08:38:42 +01001227}
1228
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001229static void
1230__perf_counter_sched_in(struct perf_counter_context *ctx,
1231 struct perf_cpu_context *cpuctx, int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001232{
Thomas Gleixner0793a612008-12-04 20:12:29 +01001233 struct perf_counter *counter;
Paul Mackerrasdd0e6ba2009-01-12 15:11:00 +11001234 int can_add_hw = 1;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001235
Thomas Gleixner0793a612008-12-04 20:12:29 +01001236 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001237 ctx->is_active = 1;
1238 if (likely(!ctx->nr_counters))
1239 goto out;
1240
Peter Zijlstra4af49982009-04-06 11:45:10 +02001241 ctx->timestamp = perf_clock();
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001242
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001243 perf_disable();
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001244
1245 /*
1246 * First go through the list and put on any pinned groups
1247 * in order to give them the best chance of going on.
1248 */
Ingo Molnar04289bb2008-12-11 08:38:42 +01001249 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001250 if (counter->state <= PERF_COUNTER_STATE_OFF ||
Peter Zijlstra0d486962009-06-02 19:22:16 +02001251 !counter->attr.pinned)
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001252 continue;
1253 if (counter->cpu != -1 && counter->cpu != cpu)
1254 continue;
1255
Peter Zijlstraafedadf2009-05-20 12:21:22 +02001256 if (counter != counter->group_leader)
1257 counter_sched_in(counter, cpuctx, ctx, cpu);
1258 else {
1259 if (group_can_go_on(counter, cpuctx, 1))
1260 group_sched_in(counter, cpuctx, ctx, cpu);
1261 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001262
1263 /*
1264 * If this pinned group hasn't been scheduled,
1265 * put it in error state.
1266 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001267 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
1268 update_group_times(counter);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001269 counter->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001270 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001271 }
1272
1273 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1274 /*
1275 * Ignore counters in OFF or ERROR state, and
1276 * ignore pinned counters since we did them already.
1277 */
1278 if (counter->state <= PERF_COUNTER_STATE_OFF ||
Peter Zijlstra0d486962009-06-02 19:22:16 +02001279 counter->attr.pinned)
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001280 continue;
1281
Ingo Molnar04289bb2008-12-11 08:38:42 +01001282 /*
1283 * Listen to the 'cpu' scheduling filter constraint
1284 * of counters:
1285 */
Thomas Gleixner0793a612008-12-04 20:12:29 +01001286 if (counter->cpu != -1 && counter->cpu != cpu)
1287 continue;
1288
Peter Zijlstraafedadf2009-05-20 12:21:22 +02001289 if (counter != counter->group_leader) {
1290 if (counter_sched_in(counter, cpuctx, ctx, cpu))
Paul Mackerrasdd0e6ba2009-01-12 15:11:00 +11001291 can_add_hw = 0;
Peter Zijlstraafedadf2009-05-20 12:21:22 +02001292 } else {
1293 if (group_can_go_on(counter, cpuctx, can_add_hw)) {
1294 if (group_sched_in(counter, cpuctx, ctx, cpu))
1295 can_add_hw = 0;
1296 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001297 }
Thomas Gleixner0793a612008-12-04 20:12:29 +01001298 }
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001299 perf_enable();
Paul Mackerrasd859e292009-01-17 18:10:22 +11001300 out:
Thomas Gleixner0793a612008-12-04 20:12:29 +01001301 spin_unlock(&ctx->lock);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001302}
Ingo Molnar04289bb2008-12-11 08:38:42 +01001303
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001304/*
1305 * Called from scheduler to add the counters of the current task
1306 * with interrupts disabled.
1307 *
1308 * We restore the counter value and then enable it.
1309 *
1310 * This does not protect us against NMI, but enable()
1311 * sets the enabled bit in the control field of counter _before_
1312 * accessing the counter control register. If a NMI hits, then it will
1313 * keep the counter running.
1314 */
1315void perf_counter_task_sched_in(struct task_struct *task, int cpu)
1316{
1317 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001318 struct perf_counter_context *ctx = task->perf_counter_ctxp;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001319
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001320 if (likely(!ctx))
1321 return;
Paul Mackerras564c2b22009-05-22 14:27:22 +10001322 if (cpuctx->task_ctx == ctx)
1323 return;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001324 __perf_counter_sched_in(ctx, cpuctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001325 cpuctx->task_ctx = ctx;
1326}
1327
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001328static void perf_counter_cpu_sched_in(struct perf_cpu_context *cpuctx, int cpu)
1329{
1330 struct perf_counter_context *ctx = &cpuctx->ctx;
1331
1332 __perf_counter_sched_in(ctx, cpuctx, cpu);
1333}
1334
Peter Zijlstraa78ac322009-05-25 17:39:05 +02001335#define MAX_INTERRUPTS (~0ULL)
1336
1337static void perf_log_throttle(struct perf_counter *counter, int enable);
Peter Zijlstra26b119b2009-05-20 12:21:20 +02001338
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001339static void perf_adjust_period(struct perf_counter *counter, u64 events)
1340{
1341 struct hw_perf_counter *hwc = &counter->hw;
1342 u64 period, sample_period;
1343 s64 delta;
1344
1345 events *= hwc->sample_period;
1346 period = div64_u64(events, counter->attr.sample_freq);
1347
1348 delta = (s64)(period - hwc->sample_period);
1349 delta = (delta + 7) / 8; /* low pass filter */
1350
1351 sample_period = hwc->sample_period + delta;
1352
1353 if (!sample_period)
1354 sample_period = 1;
1355
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001356 hwc->sample_period = sample_period;
1357}
1358
1359static void perf_ctx_adjust_freq(struct perf_counter_context *ctx)
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001360{
1361 struct perf_counter *counter;
Peter Zijlstra6a24ed6c2009-06-05 18:01:29 +02001362 struct hw_perf_counter *hwc;
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001363 u64 interrupts, freq;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001364
1365 spin_lock(&ctx->lock);
1366 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1367 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
1368 continue;
1369
Peter Zijlstra6a24ed6c2009-06-05 18:01:29 +02001370 hwc = &counter->hw;
1371
1372 interrupts = hwc->interrupts;
1373 hwc->interrupts = 0;
Peter Zijlstraa78ac322009-05-25 17:39:05 +02001374
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001375 /*
1376 * unthrottle counters on the tick
1377 */
Peter Zijlstraa78ac322009-05-25 17:39:05 +02001378 if (interrupts == MAX_INTERRUPTS) {
1379 perf_log_throttle(counter, 1);
1380 counter->pmu->unthrottle(counter);
Peter Zijlstradf58ab22009-06-11 11:25:05 +02001381 interrupts = 2*sysctl_perf_counter_sample_rate/HZ;
Peter Zijlstraa78ac322009-05-25 17:39:05 +02001382 }
1383
Peter Zijlstra0d486962009-06-02 19:22:16 +02001384 if (!counter->attr.freq || !counter->attr.sample_freq)
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001385 continue;
1386
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001387 /*
1388 * if the specified freq < HZ then we need to skip ticks
1389 */
Peter Zijlstra6a24ed6c2009-06-05 18:01:29 +02001390 if (counter->attr.sample_freq < HZ) {
1391 freq = counter->attr.sample_freq;
1392
1393 hwc->freq_count += freq;
1394 hwc->freq_interrupts += interrupts;
1395
1396 if (hwc->freq_count < HZ)
1397 continue;
1398
1399 interrupts = hwc->freq_interrupts;
1400 hwc->freq_interrupts = 0;
1401 hwc->freq_count -= HZ;
1402 } else
1403 freq = HZ;
1404
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001405 perf_adjust_period(counter, freq * interrupts);
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001406
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001407 /*
1408 * In order to avoid being stalled by an (accidental) huge
1409 * sample period, force reset the sample period if we didn't
1410 * get any events in this freq period.
1411 */
1412 if (!interrupts) {
1413 perf_disable();
1414 counter->pmu->disable(counter);
Paul Mackerras87847b82009-06-13 17:06:50 +10001415 atomic64_set(&hwc->period_left, 0);
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001416 counter->pmu->enable(counter);
1417 perf_enable();
1418 }
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001419 }
1420 spin_unlock(&ctx->lock);
1421}
1422
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001423/*
1424 * Round-robin a context's counters:
1425 */
1426static void rotate_ctx(struct perf_counter_context *ctx)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001427{
Thomas Gleixner0793a612008-12-04 20:12:29 +01001428 struct perf_counter *counter;
1429
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001430 if (!ctx->nr_counters)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001431 return;
1432
Thomas Gleixner0793a612008-12-04 20:12:29 +01001433 spin_lock(&ctx->lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001434 /*
Ingo Molnar04289bb2008-12-11 08:38:42 +01001435 * Rotate the first entry last (works just fine for group counters too):
Thomas Gleixner0793a612008-12-04 20:12:29 +01001436 */
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001437 perf_disable();
Ingo Molnar04289bb2008-12-11 08:38:42 +01001438 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Peter Zijlstra75564232009-03-13 12:21:29 +01001439 list_move_tail(&counter->list_entry, &ctx->counter_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001440 break;
1441 }
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001442 perf_enable();
Thomas Gleixner0793a612008-12-04 20:12:29 +01001443
1444 spin_unlock(&ctx->lock);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001445}
Thomas Gleixner0793a612008-12-04 20:12:29 +01001446
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001447void perf_counter_task_tick(struct task_struct *curr, int cpu)
1448{
Peter Zijlstra7fc23a52009-05-08 18:52:21 +02001449 struct perf_cpu_context *cpuctx;
1450 struct perf_counter_context *ctx;
1451
1452 if (!atomic_read(&nr_counters))
1453 return;
1454
1455 cpuctx = &per_cpu(perf_cpu_context, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001456 ctx = curr->perf_counter_ctxp;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001457
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001458 perf_ctx_adjust_freq(&cpuctx->ctx);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001459 if (ctx)
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001460 perf_ctx_adjust_freq(ctx);
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001461
Ingo Molnarb82914c2009-05-04 18:54:32 +02001462 perf_counter_cpu_sched_out(cpuctx);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001463 if (ctx)
1464 __perf_counter_task_sched_out(ctx);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001465
Ingo Molnarb82914c2009-05-04 18:54:32 +02001466 rotate_ctx(&cpuctx->ctx);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001467 if (ctx)
1468 rotate_ctx(ctx);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001469
Ingo Molnarb82914c2009-05-04 18:54:32 +02001470 perf_counter_cpu_sched_in(cpuctx, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001471 if (ctx)
1472 perf_counter_task_sched_in(curr, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001473}
1474
1475/*
Paul Mackerras57e79862009-06-30 16:07:19 +10001476 * Enable all of a task's counters that have been marked enable-on-exec.
1477 * This expects task == current.
1478 */
1479static void perf_counter_enable_on_exec(struct task_struct *task)
1480{
1481 struct perf_counter_context *ctx;
1482 struct perf_counter *counter;
1483 unsigned long flags;
1484 int enabled = 0;
1485
1486 local_irq_save(flags);
1487 ctx = task->perf_counter_ctxp;
1488 if (!ctx || !ctx->nr_counters)
1489 goto out;
1490
1491 __perf_counter_task_sched_out(ctx);
1492
1493 spin_lock(&ctx->lock);
1494
1495 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1496 if (!counter->attr.enable_on_exec)
1497 continue;
1498 counter->attr.enable_on_exec = 0;
1499 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
1500 continue;
Paul Mackerrasfa289be2009-08-25 15:17:20 +10001501 __perf_counter_mark_enabled(counter, ctx);
Paul Mackerras57e79862009-06-30 16:07:19 +10001502 enabled = 1;
1503 }
1504
1505 /*
1506 * Unclone this context if we enabled any counter.
1507 */
Peter Zijlstra71a851b2009-07-10 09:06:56 +02001508 if (enabled)
1509 unclone_ctx(ctx);
Paul Mackerras57e79862009-06-30 16:07:19 +10001510
1511 spin_unlock(&ctx->lock);
1512
1513 perf_counter_task_sched_in(task, smp_processor_id());
1514 out:
1515 local_irq_restore(flags);
1516}
1517
1518/*
Thomas Gleixner0793a612008-12-04 20:12:29 +01001519 * Cross CPU call to read the hardware counter
1520 */
Peter Zijlstrabfbd3382009-06-24 21:11:59 +02001521static void __perf_counter_read(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001522{
Paul Mackerrase1ac3612009-08-14 15:39:10 +10001523 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
Ingo Molnar621a01e2008-12-11 12:46:46 +01001524 struct perf_counter *counter = info;
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001525 struct perf_counter_context *ctx = counter->ctx;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001526 unsigned long flags;
Ingo Molnar621a01e2008-12-11 12:46:46 +01001527
Paul Mackerrase1ac3612009-08-14 15:39:10 +10001528 /*
1529 * If this is a task context, we need to check whether it is
1530 * the current task context of this cpu. If not it has been
1531 * scheduled out before the smp call arrived. In that case
1532 * counter->count would have been updated to a recent sample
1533 * when the counter was scheduled out.
1534 */
1535 if (ctx->task && cpuctx->task_ctx != ctx)
1536 return;
1537
Peter Zijlstra849691a2009-04-06 11:45:12 +02001538 local_irq_save(flags);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001539 if (ctx->is_active)
Peter Zijlstra4af49982009-04-06 11:45:10 +02001540 update_context_time(ctx);
Robert Richter4aeb0b42009-04-29 12:47:03 +02001541 counter->pmu->read(counter);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001542 update_counter_times(counter);
Peter Zijlstra849691a2009-04-06 11:45:12 +02001543 local_irq_restore(flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001544}
1545
Ingo Molnar04289bb2008-12-11 08:38:42 +01001546static u64 perf_counter_read(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001547{
1548 /*
1549 * If counter is enabled and currently active on a CPU, update the
1550 * value in the counter structure:
1551 */
Ingo Molnar6a930702008-12-11 15:17:03 +01001552 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
Thomas Gleixner0793a612008-12-04 20:12:29 +01001553 smp_call_function_single(counter->oncpu,
Peter Zijlstrabfbd3382009-06-24 21:11:59 +02001554 __perf_counter_read, counter, 1);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001555 } else if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
1556 update_counter_times(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001557 }
1558
Ingo Molnaree060942008-12-13 09:00:03 +01001559 return atomic64_read(&counter->count);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001560}
1561
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001562/*
1563 * Initialize the perf_counter context in a task_struct:
1564 */
1565static void
1566__perf_counter_init_context(struct perf_counter_context *ctx,
1567 struct task_struct *task)
1568{
1569 memset(ctx, 0, sizeof(*ctx));
1570 spin_lock_init(&ctx->lock);
1571 mutex_init(&ctx->mutex);
1572 INIT_LIST_HEAD(&ctx->counter_list);
1573 INIT_LIST_HEAD(&ctx->event_list);
1574 atomic_set(&ctx->refcount, 1);
1575 ctx->task = task;
1576}
1577
Thomas Gleixner0793a612008-12-04 20:12:29 +01001578static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
1579{
Ingo Molnar22a4f652009-06-01 10:13:37 +02001580 struct perf_counter_context *ctx;
1581 struct perf_cpu_context *cpuctx;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001582 struct task_struct *task;
Paul Mackerras25346b92009-06-01 17:48:12 +10001583 unsigned long flags;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001584 int err;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001585
1586 /*
1587 * If cpu is not a wildcard then this is a percpu counter:
1588 */
1589 if (cpu != -1) {
1590 /* Must be root to operate on a CPU counter: */
Peter Zijlstra07647712009-06-11 11:18:36 +02001591 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
Thomas Gleixner0793a612008-12-04 20:12:29 +01001592 return ERR_PTR(-EACCES);
1593
1594 if (cpu < 0 || cpu > num_possible_cpus())
1595 return ERR_PTR(-EINVAL);
1596
1597 /*
1598 * We could be clever and allow to attach a counter to an
1599 * offline CPU and activate it when the CPU comes up, but
1600 * that's for later.
1601 */
1602 if (!cpu_isset(cpu, cpu_online_map))
1603 return ERR_PTR(-ENODEV);
1604
1605 cpuctx = &per_cpu(perf_cpu_context, cpu);
1606 ctx = &cpuctx->ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001607 get_ctx(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001608
Thomas Gleixner0793a612008-12-04 20:12:29 +01001609 return ctx;
1610 }
1611
1612 rcu_read_lock();
1613 if (!pid)
1614 task = current;
1615 else
1616 task = find_task_by_vpid(pid);
1617 if (task)
1618 get_task_struct(task);
1619 rcu_read_unlock();
1620
1621 if (!task)
1622 return ERR_PTR(-ESRCH);
1623
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001624 /*
1625 * Can't attach counters to a dying task.
1626 */
1627 err = -ESRCH;
1628 if (task->flags & PF_EXITING)
1629 goto errout;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001630
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001631 /* Reuse ptrace permission checks for now. */
1632 err = -EACCES;
1633 if (!ptrace_may_access(task, PTRACE_MODE_READ))
1634 goto errout;
1635
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001636 retry:
Paul Mackerras25346b92009-06-01 17:48:12 +10001637 ctx = perf_lock_task_context(task, &flags);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001638 if (ctx) {
Peter Zijlstra71a851b2009-07-10 09:06:56 +02001639 unclone_ctx(ctx);
Paul Mackerras25346b92009-06-01 17:48:12 +10001640 spin_unlock_irqrestore(&ctx->lock, flags);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001641 }
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001642
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001643 if (!ctx) {
1644 ctx = kmalloc(sizeof(struct perf_counter_context), GFP_KERNEL);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001645 err = -ENOMEM;
1646 if (!ctx)
1647 goto errout;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001648 __perf_counter_init_context(ctx, task);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001649 get_ctx(ctx);
1650 if (cmpxchg(&task->perf_counter_ctxp, NULL, ctx)) {
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001651 /*
1652 * We raced with some other task; use
1653 * the context they set.
1654 */
1655 kfree(ctx);
Paul Mackerras25346b92009-06-01 17:48:12 +10001656 goto retry;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001657 }
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001658 get_task_struct(task);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001659 }
1660
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001661 put_task_struct(task);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001662 return ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001663
1664 errout:
1665 put_task_struct(task);
1666 return ERR_PTR(err);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001667}
1668
Peter Zijlstra592903c2009-03-13 12:21:36 +01001669static void free_counter_rcu(struct rcu_head *head)
1670{
1671 struct perf_counter *counter;
1672
1673 counter = container_of(head, struct perf_counter, rcu_head);
Peter Zijlstra709e50c2009-06-02 14:13:15 +02001674 if (counter->ns)
1675 put_pid_ns(counter->ns);
Peter Zijlstra592903c2009-03-13 12:21:36 +01001676 kfree(counter);
1677}
1678
Peter Zijlstra925d5192009-03-30 19:07:02 +02001679static void perf_pending_sync(struct perf_counter *counter);
1680
Peter Zijlstraf1600952009-03-19 20:26:16 +01001681static void free_counter(struct perf_counter *counter)
1682{
Peter Zijlstra925d5192009-03-30 19:07:02 +02001683 perf_pending_sync(counter);
1684
Peter Zijlstraf3440112009-06-22 13:58:35 +02001685 if (!counter->parent) {
1686 atomic_dec(&nr_counters);
1687 if (counter->attr.mmap)
1688 atomic_dec(&nr_mmap_counters);
1689 if (counter->attr.comm)
1690 atomic_dec(&nr_comm_counters);
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02001691 if (counter->attr.task)
1692 atomic_dec(&nr_task_counters);
Peter Zijlstraf3440112009-06-22 13:58:35 +02001693 }
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02001694
Peter Zijlstrae077df42009-03-19 20:26:17 +01001695 if (counter->destroy)
1696 counter->destroy(counter);
1697
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001698 put_ctx(counter->ctx);
Peter Zijlstraf1600952009-03-19 20:26:16 +01001699 call_rcu(&counter->rcu_head, free_counter_rcu);
1700}
1701
Thomas Gleixner0793a612008-12-04 20:12:29 +01001702/*
1703 * Called when the last reference to the file is gone.
1704 */
1705static int perf_release(struct inode *inode, struct file *file)
1706{
1707 struct perf_counter *counter = file->private_data;
1708 struct perf_counter_context *ctx = counter->ctx;
1709
1710 file->private_data = NULL;
1711
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001712 WARN_ON_ONCE(ctx->parent_ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001713 mutex_lock(&ctx->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01001714 perf_counter_remove_from_context(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001715 mutex_unlock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001716
Peter Zijlstra082ff5a2009-05-23 18:29:00 +02001717 mutex_lock(&counter->owner->perf_counter_mutex);
1718 list_del_init(&counter->owner_entry);
1719 mutex_unlock(&counter->owner->perf_counter_mutex);
1720 put_task_struct(counter->owner);
1721
Peter Zijlstraf1600952009-03-19 20:26:16 +01001722 free_counter(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001723
1724 return 0;
1725}
1726
Peter Zijlstra3dab77f2009-08-13 11:47:53 +02001727static int perf_counter_read_size(struct perf_counter *counter)
1728{
1729 int entry = sizeof(u64); /* value */
1730 int size = 0;
1731 int nr = 1;
1732
1733 if (counter->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1734 size += sizeof(u64);
1735
1736 if (counter->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1737 size += sizeof(u64);
1738
1739 if (counter->attr.read_format & PERF_FORMAT_ID)
1740 entry += sizeof(u64);
1741
1742 if (counter->attr.read_format & PERF_FORMAT_GROUP) {
1743 nr += counter->group_leader->nr_siblings;
1744 size += sizeof(u64);
1745 }
1746
1747 size += entry * nr;
1748
1749 return size;
1750}
1751
1752static u64 perf_counter_read_value(struct perf_counter *counter)
Peter Zijlstrae53c0992009-07-24 14:42:10 +02001753{
1754 struct perf_counter *child;
1755 u64 total = 0;
1756
1757 total += perf_counter_read(counter);
1758 list_for_each_entry(child, &counter->child_list, child_list)
1759 total += perf_counter_read(child);
1760
1761 return total;
1762}
1763
Peter Zijlstra3dab77f2009-08-13 11:47:53 +02001764static int perf_counter_read_entry(struct perf_counter *counter,
1765 u64 read_format, char __user *buf)
1766{
1767 int n = 0, count = 0;
1768 u64 values[2];
1769
1770 values[n++] = perf_counter_read_value(counter);
1771 if (read_format & PERF_FORMAT_ID)
1772 values[n++] = primary_counter_id(counter);
1773
1774 count = n * sizeof(u64);
1775
1776 if (copy_to_user(buf, values, count))
1777 return -EFAULT;
1778
1779 return count;
1780}
1781
1782static int perf_counter_read_group(struct perf_counter *counter,
1783 u64 read_format, char __user *buf)
1784{
1785 struct perf_counter *leader = counter->group_leader, *sub;
1786 int n = 0, size = 0, err = -EFAULT;
1787 u64 values[3];
1788
1789 values[n++] = 1 + leader->nr_siblings;
1790 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
1791 values[n++] = leader->total_time_enabled +
1792 atomic64_read(&leader->child_total_time_enabled);
1793 }
1794 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
1795 values[n++] = leader->total_time_running +
1796 atomic64_read(&leader->child_total_time_running);
1797 }
1798
1799 size = n * sizeof(u64);
1800
1801 if (copy_to_user(buf, values, size))
1802 return -EFAULT;
1803
1804 err = perf_counter_read_entry(leader, read_format, buf + size);
1805 if (err < 0)
1806 return err;
1807
1808 size += err;
1809
1810 list_for_each_entry(sub, &leader->sibling_list, list_entry) {
Peter Zijlstra4464fca2009-08-21 17:19:36 +02001811 err = perf_counter_read_entry(sub, read_format,
Peter Zijlstra3dab77f2009-08-13 11:47:53 +02001812 buf + size);
1813 if (err < 0)
1814 return err;
1815
1816 size += err;
1817 }
1818
1819 return size;
1820}
1821
1822static int perf_counter_read_one(struct perf_counter *counter,
1823 u64 read_format, char __user *buf)
1824{
1825 u64 values[4];
1826 int n = 0;
1827
1828 values[n++] = perf_counter_read_value(counter);
1829 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
1830 values[n++] = counter->total_time_enabled +
1831 atomic64_read(&counter->child_total_time_enabled);
1832 }
1833 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
1834 values[n++] = counter->total_time_running +
1835 atomic64_read(&counter->child_total_time_running);
1836 }
1837 if (read_format & PERF_FORMAT_ID)
1838 values[n++] = primary_counter_id(counter);
1839
1840 if (copy_to_user(buf, values, n * sizeof(u64)))
1841 return -EFAULT;
1842
1843 return n * sizeof(u64);
1844}
1845
Thomas Gleixner0793a612008-12-04 20:12:29 +01001846/*
1847 * Read the performance counter - simple non blocking version for now
1848 */
1849static ssize_t
1850perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
1851{
Peter Zijlstra3dab77f2009-08-13 11:47:53 +02001852 u64 read_format = counter->attr.read_format;
1853 int ret;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001854
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001855 /*
1856 * Return end-of-file for a read on a counter that is in
1857 * error state (i.e. because it was pinned but it couldn't be
1858 * scheduled on to the CPU at some point).
1859 */
1860 if (counter->state == PERF_COUNTER_STATE_ERROR)
1861 return 0;
1862
Peter Zijlstra3dab77f2009-08-13 11:47:53 +02001863 if (count < perf_counter_read_size(counter))
1864 return -ENOSPC;
1865
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001866 WARN_ON_ONCE(counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001867 mutex_lock(&counter->child_mutex);
Peter Zijlstra3dab77f2009-08-13 11:47:53 +02001868 if (read_format & PERF_FORMAT_GROUP)
1869 ret = perf_counter_read_group(counter, read_format, buf);
1870 else
1871 ret = perf_counter_read_one(counter, read_format, buf);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001872 mutex_unlock(&counter->child_mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001873
Peter Zijlstra3dab77f2009-08-13 11:47:53 +02001874 return ret;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001875}
1876
1877static ssize_t
Thomas Gleixner0793a612008-12-04 20:12:29 +01001878perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
1879{
1880 struct perf_counter *counter = file->private_data;
1881
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001882 return perf_read_hw(counter, buf, count);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001883}
1884
1885static unsigned int perf_poll(struct file *file, poll_table *wait)
1886{
1887 struct perf_counter *counter = file->private_data;
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001888 struct perf_mmap_data *data;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001889 unsigned int events = POLL_HUP;
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001890
1891 rcu_read_lock();
1892 data = rcu_dereference(counter->data);
1893 if (data)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001894 events = atomic_xchg(&data->poll, 0);
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001895 rcu_read_unlock();
Thomas Gleixner0793a612008-12-04 20:12:29 +01001896
1897 poll_wait(file, &counter->waitq, wait);
1898
Thomas Gleixner0793a612008-12-04 20:12:29 +01001899 return events;
1900}
1901
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001902static void perf_counter_reset(struct perf_counter *counter)
1903{
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001904 (void)perf_counter_read(counter);
Paul Mackerras615a3f12009-05-11 15:50:21 +10001905 atomic64_set(&counter->count, 0);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001906 perf_counter_update_userpage(counter);
1907}
1908
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001909/*
1910 * Holding the top-level counter's child_mutex means that any
1911 * descendant process that has inherited this counter will block
1912 * in sync_child_counter if it goes to exit, thus satisfying the
1913 * task existence requirements of perf_counter_enable/disable.
1914 */
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001915static void perf_counter_for_each_child(struct perf_counter *counter,
1916 void (*func)(struct perf_counter *))
1917{
1918 struct perf_counter *child;
1919
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001920 WARN_ON_ONCE(counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001921 mutex_lock(&counter->child_mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001922 func(counter);
1923 list_for_each_entry(child, &counter->child_list, child_list)
1924 func(child);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001925 mutex_unlock(&counter->child_mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001926}
1927
1928static void perf_counter_for_each(struct perf_counter *counter,
1929 void (*func)(struct perf_counter *))
1930{
Peter Zijlstra75f937f2009-06-15 15:05:12 +02001931 struct perf_counter_context *ctx = counter->ctx;
1932 struct perf_counter *sibling;
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001933
Peter Zijlstra75f937f2009-06-15 15:05:12 +02001934 WARN_ON_ONCE(ctx->parent_ctx);
1935 mutex_lock(&ctx->mutex);
1936 counter = counter->group_leader;
1937
1938 perf_counter_for_each_child(counter, func);
1939 func(counter);
1940 list_for_each_entry(sibling, &counter->sibling_list, list_entry)
1941 perf_counter_for_each_child(counter, func);
1942 mutex_unlock(&ctx->mutex);
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001943}
1944
Peter Zijlstra08247e32009-06-02 16:46:57 +02001945static int perf_counter_period(struct perf_counter *counter, u64 __user *arg)
1946{
1947 struct perf_counter_context *ctx = counter->ctx;
1948 unsigned long size;
1949 int ret = 0;
1950 u64 value;
1951
Peter Zijlstra0d486962009-06-02 19:22:16 +02001952 if (!counter->attr.sample_period)
Peter Zijlstra08247e32009-06-02 16:46:57 +02001953 return -EINVAL;
1954
1955 size = copy_from_user(&value, arg, sizeof(value));
1956 if (size != sizeof(value))
1957 return -EFAULT;
1958
1959 if (!value)
1960 return -EINVAL;
1961
1962 spin_lock_irq(&ctx->lock);
Peter Zijlstra0d486962009-06-02 19:22:16 +02001963 if (counter->attr.freq) {
Peter Zijlstradf58ab22009-06-11 11:25:05 +02001964 if (value > sysctl_perf_counter_sample_rate) {
Peter Zijlstra08247e32009-06-02 16:46:57 +02001965 ret = -EINVAL;
1966 goto unlock;
1967 }
1968
Peter Zijlstra0d486962009-06-02 19:22:16 +02001969 counter->attr.sample_freq = value;
Peter Zijlstra08247e32009-06-02 16:46:57 +02001970 } else {
Peter Zijlstra0d486962009-06-02 19:22:16 +02001971 counter->attr.sample_period = value;
Peter Zijlstra08247e32009-06-02 16:46:57 +02001972 counter->hw.sample_period = value;
Peter Zijlstra08247e32009-06-02 16:46:57 +02001973 }
1974unlock:
1975 spin_unlock_irq(&ctx->lock);
1976
1977 return ret;
1978}
1979
Paul Mackerrasd859e292009-01-17 18:10:22 +11001980static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1981{
1982 struct perf_counter *counter = file->private_data;
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001983 void (*func)(struct perf_counter *);
1984 u32 flags = arg;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001985
1986 switch (cmd) {
1987 case PERF_COUNTER_IOC_ENABLE:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001988 func = perf_counter_enable;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001989 break;
1990 case PERF_COUNTER_IOC_DISABLE:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001991 func = perf_counter_disable;
Peter Zijlstra79f14642009-04-06 11:45:07 +02001992 break;
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001993 case PERF_COUNTER_IOC_RESET:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001994 func = perf_counter_reset;
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001995 break;
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001996
1997 case PERF_COUNTER_IOC_REFRESH:
1998 return perf_counter_refresh(counter, arg);
Peter Zijlstra08247e32009-06-02 16:46:57 +02001999
2000 case PERF_COUNTER_IOC_PERIOD:
2001 return perf_counter_period(counter, (u64 __user *)arg);
2002
Paul Mackerrasd859e292009-01-17 18:10:22 +11002003 default:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02002004 return -ENOTTY;
Paul Mackerrasd859e292009-01-17 18:10:22 +11002005 }
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02002006
2007 if (flags & PERF_IOC_FLAG_GROUP)
2008 perf_counter_for_each(counter, func);
2009 else
2010 perf_counter_for_each_child(counter, func);
2011
2012 return 0;
Paul Mackerrasd859e292009-01-17 18:10:22 +11002013}
2014
Peter Zijlstra771d7cd2009-05-25 14:45:26 +02002015int perf_counter_task_enable(void)
2016{
2017 struct perf_counter *counter;
2018
2019 mutex_lock(&current->perf_counter_mutex);
2020 list_for_each_entry(counter, &current->perf_counter_list, owner_entry)
2021 perf_counter_for_each_child(counter, perf_counter_enable);
2022 mutex_unlock(&current->perf_counter_mutex);
2023
2024 return 0;
2025}
2026
2027int perf_counter_task_disable(void)
2028{
2029 struct perf_counter *counter;
2030
2031 mutex_lock(&current->perf_counter_mutex);
2032 list_for_each_entry(counter, &current->perf_counter_list, owner_entry)
2033 perf_counter_for_each_child(counter, perf_counter_disable);
2034 mutex_unlock(&current->perf_counter_mutex);
2035
2036 return 0;
2037}
2038
Ingo Molnarf738eb12009-08-18 11:32:24 +02002039#ifndef PERF_COUNTER_INDEX_OFFSET
2040# define PERF_COUNTER_INDEX_OFFSET 0
2041#endif
2042
Peter Zijlstra194002b2009-06-22 16:35:24 +02002043static int perf_counter_index(struct perf_counter *counter)
2044{
2045 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
2046 return 0;
2047
2048 return counter->hw.idx + 1 - PERF_COUNTER_INDEX_OFFSET;
2049}
2050
Peter Zijlstra38ff6672009-03-30 19:07:03 +02002051/*
2052 * Callers need to ensure there can be no nesting of this function, otherwise
2053 * the seqlock logic goes bad. We can not serialize this because the arch
2054 * code calls this from NMI context.
2055 */
2056void perf_counter_update_userpage(struct perf_counter *counter)
Paul Mackerras37d81822009-03-23 18:22:08 +01002057{
Peter Zijlstra38ff6672009-03-30 19:07:03 +02002058 struct perf_counter_mmap_page *userpg;
Ingo Molnar22a4f652009-06-01 10:13:37 +02002059 struct perf_mmap_data *data;
Peter Zijlstra38ff6672009-03-30 19:07:03 +02002060
2061 rcu_read_lock();
2062 data = rcu_dereference(counter->data);
2063 if (!data)
2064 goto unlock;
2065
2066 userpg = data->user_page;
Paul Mackerras37d81822009-03-23 18:22:08 +01002067
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002068 /*
2069 * Disable preemption so as to not let the corresponding user-space
2070 * spin too long if we get preempted.
2071 */
2072 preempt_disable();
Paul Mackerras37d81822009-03-23 18:22:08 +01002073 ++userpg->lock;
Peter Zijlstra92f22a32009-04-02 11:12:04 +02002074 barrier();
Peter Zijlstra194002b2009-06-22 16:35:24 +02002075 userpg->index = perf_counter_index(counter);
Paul Mackerras37d81822009-03-23 18:22:08 +01002076 userpg->offset = atomic64_read(&counter->count);
2077 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
2078 userpg->offset -= atomic64_read(&counter->hw.prev_count);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002079
Peter Zijlstra7f8b4e42009-06-22 14:34:35 +02002080 userpg->time_enabled = counter->total_time_enabled +
2081 atomic64_read(&counter->child_total_time_enabled);
2082
2083 userpg->time_running = counter->total_time_running +
2084 atomic64_read(&counter->child_total_time_running);
2085
Peter Zijlstra92f22a32009-04-02 11:12:04 +02002086 barrier();
Paul Mackerras37d81822009-03-23 18:22:08 +01002087 ++userpg->lock;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002088 preempt_enable();
Peter Zijlstra38ff6672009-03-30 19:07:03 +02002089unlock:
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002090 rcu_read_unlock();
Paul Mackerras37d81822009-03-23 18:22:08 +01002091}
2092
2093static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
2094{
2095 struct perf_counter *counter = vma->vm_file->private_data;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002096 struct perf_mmap_data *data;
2097 int ret = VM_FAULT_SIGBUS;
Paul Mackerras37d81822009-03-23 18:22:08 +01002098
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002099 if (vmf->flags & FAULT_FLAG_MKWRITE) {
2100 if (vmf->pgoff == 0)
2101 ret = 0;
2102 return ret;
2103 }
2104
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002105 rcu_read_lock();
2106 data = rcu_dereference(counter->data);
2107 if (!data)
2108 goto unlock;
Paul Mackerras37d81822009-03-23 18:22:08 +01002109
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002110 if (vmf->pgoff == 0) {
2111 vmf->page = virt_to_page(data->user_page);
2112 } else {
2113 int nr = vmf->pgoff - 1;
2114
2115 if ((unsigned)nr > data->nr_pages)
2116 goto unlock;
2117
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002118 if (vmf->flags & FAULT_FLAG_WRITE)
2119 goto unlock;
2120
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002121 vmf->page = virt_to_page(data->data_pages[nr]);
2122 }
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002123
Paul Mackerras37d81822009-03-23 18:22:08 +01002124 get_page(vmf->page);
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002125 vmf->page->mapping = vma->vm_file->f_mapping;
2126 vmf->page->index = vmf->pgoff;
2127
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002128 ret = 0;
2129unlock:
2130 rcu_read_unlock();
2131
2132 return ret;
2133}
2134
2135static int perf_mmap_data_alloc(struct perf_counter *counter, int nr_pages)
2136{
2137 struct perf_mmap_data *data;
2138 unsigned long size;
2139 int i;
2140
2141 WARN_ON(atomic_read(&counter->mmap_count));
2142
2143 size = sizeof(struct perf_mmap_data);
2144 size += nr_pages * sizeof(void *);
2145
2146 data = kzalloc(size, GFP_KERNEL);
2147 if (!data)
2148 goto fail;
2149
2150 data->user_page = (void *)get_zeroed_page(GFP_KERNEL);
2151 if (!data->user_page)
2152 goto fail_user_page;
2153
2154 for (i = 0; i < nr_pages; i++) {
2155 data->data_pages[i] = (void *)get_zeroed_page(GFP_KERNEL);
2156 if (!data->data_pages[i])
2157 goto fail_data_pages;
2158 }
2159
2160 data->nr_pages = nr_pages;
Peter Zijlstra22c15582009-05-05 17:50:25 +02002161 atomic_set(&data->lock, -1);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002162
2163 rcu_assign_pointer(counter->data, data);
2164
Paul Mackerras37d81822009-03-23 18:22:08 +01002165 return 0;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002166
2167fail_data_pages:
2168 for (i--; i >= 0; i--)
2169 free_page((unsigned long)data->data_pages[i]);
2170
2171 free_page((unsigned long)data->user_page);
2172
2173fail_user_page:
2174 kfree(data);
2175
2176fail:
2177 return -ENOMEM;
2178}
2179
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002180static void perf_mmap_free_page(unsigned long addr)
2181{
Kevin Cernekee5bfd7562009-07-05 12:08:19 -07002182 struct page *page = virt_to_page((void *)addr);
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002183
2184 page->mapping = NULL;
2185 __free_page(page);
2186}
2187
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002188static void __perf_mmap_data_free(struct rcu_head *rcu_head)
2189{
Ingo Molnar22a4f652009-06-01 10:13:37 +02002190 struct perf_mmap_data *data;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002191 int i;
2192
Ingo Molnar22a4f652009-06-01 10:13:37 +02002193 data = container_of(rcu_head, struct perf_mmap_data, rcu_head);
2194
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002195 perf_mmap_free_page((unsigned long)data->user_page);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002196 for (i = 0; i < data->nr_pages; i++)
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002197 perf_mmap_free_page((unsigned long)data->data_pages[i]);
2198
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002199 kfree(data);
2200}
2201
2202static void perf_mmap_data_free(struct perf_counter *counter)
2203{
2204 struct perf_mmap_data *data = counter->data;
2205
2206 WARN_ON(atomic_read(&counter->mmap_count));
2207
2208 rcu_assign_pointer(counter->data, NULL);
2209 call_rcu(&data->rcu_head, __perf_mmap_data_free);
2210}
2211
2212static void perf_mmap_open(struct vm_area_struct *vma)
2213{
2214 struct perf_counter *counter = vma->vm_file->private_data;
2215
2216 atomic_inc(&counter->mmap_count);
2217}
2218
2219static void perf_mmap_close(struct vm_area_struct *vma)
2220{
2221 struct perf_counter *counter = vma->vm_file->private_data;
2222
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10002223 WARN_ON_ONCE(counter->ctx->parent_ctx);
Ingo Molnar22a4f652009-06-01 10:13:37 +02002224 if (atomic_dec_and_mutex_lock(&counter->mmap_count, &counter->mmap_mutex)) {
Peter Zijlstra789f90f2009-05-15 15:19:27 +02002225 struct user_struct *user = current_user();
2226
2227 atomic_long_sub(counter->data->nr_pages + 1, &user->locked_vm);
Peter Zijlstrac5078f72009-05-05 17:50:24 +02002228 vma->vm_mm->locked_vm -= counter->data->nr_locked;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002229 perf_mmap_data_free(counter);
2230 mutex_unlock(&counter->mmap_mutex);
2231 }
Paul Mackerras37d81822009-03-23 18:22:08 +01002232}
2233
2234static struct vm_operations_struct perf_mmap_vmops = {
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002235 .open = perf_mmap_open,
2236 .close = perf_mmap_close,
2237 .fault = perf_mmap_fault,
2238 .page_mkwrite = perf_mmap_fault,
Paul Mackerras37d81822009-03-23 18:22:08 +01002239};
2240
2241static int perf_mmap(struct file *file, struct vm_area_struct *vma)
2242{
2243 struct perf_counter *counter = file->private_data;
Ingo Molnar22a4f652009-06-01 10:13:37 +02002244 unsigned long user_locked, user_lock_limit;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02002245 struct user_struct *user = current_user();
Ingo Molnar22a4f652009-06-01 10:13:37 +02002246 unsigned long locked, lock_limit;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002247 unsigned long vma_size;
2248 unsigned long nr_pages;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02002249 long user_extra, extra;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002250 int ret = 0;
Paul Mackerras37d81822009-03-23 18:22:08 +01002251
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002252 if (!(vma->vm_flags & VM_SHARED))
Paul Mackerras37d81822009-03-23 18:22:08 +01002253 return -EINVAL;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002254
2255 vma_size = vma->vm_end - vma->vm_start;
2256 nr_pages = (vma_size / PAGE_SIZE) - 1;
2257
Peter Zijlstra7730d862009-03-25 12:48:31 +01002258 /*
2259 * If we have data pages ensure they're a power-of-two number, so we
2260 * can do bitmasks instead of modulo.
2261 */
2262 if (nr_pages != 0 && !is_power_of_2(nr_pages))
Paul Mackerras37d81822009-03-23 18:22:08 +01002263 return -EINVAL;
2264
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002265 if (vma_size != PAGE_SIZE * (1 + nr_pages))
Paul Mackerras37d81822009-03-23 18:22:08 +01002266 return -EINVAL;
2267
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002268 if (vma->vm_pgoff != 0)
2269 return -EINVAL;
Paul Mackerras37d81822009-03-23 18:22:08 +01002270
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10002271 WARN_ON_ONCE(counter->ctx->parent_ctx);
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02002272 mutex_lock(&counter->mmap_mutex);
2273 if (atomic_inc_not_zero(&counter->mmap_count)) {
2274 if (nr_pages != counter->data->nr_pages)
2275 ret = -EINVAL;
2276 goto unlock;
2277 }
2278
Peter Zijlstra789f90f2009-05-15 15:19:27 +02002279 user_extra = nr_pages + 1;
2280 user_lock_limit = sysctl_perf_counter_mlock >> (PAGE_SHIFT - 10);
Ingo Molnara3862d32009-05-24 09:02:37 +02002281
2282 /*
2283 * Increase the limit linearly with more CPUs:
2284 */
2285 user_lock_limit *= num_online_cpus();
2286
Peter Zijlstra789f90f2009-05-15 15:19:27 +02002287 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
Peter Zijlstrac5078f72009-05-05 17:50:24 +02002288
Peter Zijlstra789f90f2009-05-15 15:19:27 +02002289 extra = 0;
2290 if (user_locked > user_lock_limit)
2291 extra = user_locked - user_lock_limit;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002292
2293 lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
2294 lock_limit >>= PAGE_SHIFT;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02002295 locked = vma->vm_mm->locked_vm + extra;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002296
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02002297 if ((locked > lock_limit) && !capable(CAP_IPC_LOCK)) {
2298 ret = -EPERM;
2299 goto unlock;
2300 }
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002301
2302 WARN_ON(counter->data);
2303 ret = perf_mmap_data_alloc(counter, nr_pages);
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02002304 if (ret)
2305 goto unlock;
2306
2307 atomic_set(&counter->mmap_count, 1);
Peter Zijlstra789f90f2009-05-15 15:19:27 +02002308 atomic_long_add(user_extra, &user->locked_vm);
Peter Zijlstrac5078f72009-05-05 17:50:24 +02002309 vma->vm_mm->locked_vm += extra;
2310 counter->data->nr_locked = extra;
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002311 if (vma->vm_flags & VM_WRITE)
2312 counter->data->writable = 1;
2313
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02002314unlock:
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002315 mutex_unlock(&counter->mmap_mutex);
Paul Mackerras37d81822009-03-23 18:22:08 +01002316
Paul Mackerras37d81822009-03-23 18:22:08 +01002317 vma->vm_flags |= VM_RESERVED;
2318 vma->vm_ops = &perf_mmap_vmops;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002319
2320 return ret;
Paul Mackerras37d81822009-03-23 18:22:08 +01002321}
2322
Peter Zijlstra3c446b32009-04-06 11:45:01 +02002323static int perf_fasync(int fd, struct file *filp, int on)
2324{
Peter Zijlstra3c446b32009-04-06 11:45:01 +02002325 struct inode *inode = filp->f_path.dentry->d_inode;
Ingo Molnar22a4f652009-06-01 10:13:37 +02002326 struct perf_counter *counter = filp->private_data;
Peter Zijlstra3c446b32009-04-06 11:45:01 +02002327 int retval;
2328
2329 mutex_lock(&inode->i_mutex);
2330 retval = fasync_helper(fd, filp, on, &counter->fasync);
2331 mutex_unlock(&inode->i_mutex);
2332
2333 if (retval < 0)
2334 return retval;
2335
2336 return 0;
2337}
2338
Thomas Gleixner0793a612008-12-04 20:12:29 +01002339static const struct file_operations perf_fops = {
2340 .release = perf_release,
2341 .read = perf_read,
2342 .poll = perf_poll,
Paul Mackerrasd859e292009-01-17 18:10:22 +11002343 .unlocked_ioctl = perf_ioctl,
2344 .compat_ioctl = perf_ioctl,
Paul Mackerras37d81822009-03-23 18:22:08 +01002345 .mmap = perf_mmap,
Peter Zijlstra3c446b32009-04-06 11:45:01 +02002346 .fasync = perf_fasync,
Thomas Gleixner0793a612008-12-04 20:12:29 +01002347};
2348
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002349/*
Peter Zijlstra925d5192009-03-30 19:07:02 +02002350 * Perf counter wakeup
2351 *
2352 * If there's data, ensure we set the poll() state and publish everything
2353 * to user-space before waking everybody up.
2354 */
2355
2356void perf_counter_wakeup(struct perf_counter *counter)
2357{
Peter Zijlstra925d5192009-03-30 19:07:02 +02002358 wake_up_all(&counter->waitq);
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002359
2360 if (counter->pending_kill) {
2361 kill_fasync(&counter->fasync, SIGIO, counter->pending_kill);
2362 counter->pending_kill = 0;
2363 }
Peter Zijlstra925d5192009-03-30 19:07:02 +02002364}
2365
2366/*
2367 * Pending wakeups
2368 *
2369 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
2370 *
2371 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
2372 * single linked list and use cmpxchg() to add entries lockless.
2373 */
2374
Peter Zijlstra79f14642009-04-06 11:45:07 +02002375static void perf_pending_counter(struct perf_pending_entry *entry)
2376{
2377 struct perf_counter *counter = container_of(entry,
2378 struct perf_counter, pending);
2379
2380 if (counter->pending_disable) {
2381 counter->pending_disable = 0;
Peter Zijlstra970892a2009-08-13 11:47:54 +02002382 __perf_counter_disable(counter);
Peter Zijlstra79f14642009-04-06 11:45:07 +02002383 }
2384
2385 if (counter->pending_wakeup) {
2386 counter->pending_wakeup = 0;
2387 perf_counter_wakeup(counter);
2388 }
2389}
2390
Peter Zijlstra671dec52009-04-06 11:45:02 +02002391#define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
Peter Zijlstra925d5192009-03-30 19:07:02 +02002392
Peter Zijlstra671dec52009-04-06 11:45:02 +02002393static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = {
Peter Zijlstra925d5192009-03-30 19:07:02 +02002394 PENDING_TAIL,
2395};
2396
Peter Zijlstra671dec52009-04-06 11:45:02 +02002397static void perf_pending_queue(struct perf_pending_entry *entry,
2398 void (*func)(struct perf_pending_entry *))
Peter Zijlstra925d5192009-03-30 19:07:02 +02002399{
Peter Zijlstra671dec52009-04-06 11:45:02 +02002400 struct perf_pending_entry **head;
Peter Zijlstra925d5192009-03-30 19:07:02 +02002401
Peter Zijlstra671dec52009-04-06 11:45:02 +02002402 if (cmpxchg(&entry->next, NULL, PENDING_TAIL) != NULL)
Peter Zijlstra925d5192009-03-30 19:07:02 +02002403 return;
2404
Peter Zijlstra671dec52009-04-06 11:45:02 +02002405 entry->func = func;
2406
2407 head = &get_cpu_var(perf_pending_head);
Peter Zijlstra925d5192009-03-30 19:07:02 +02002408
2409 do {
Peter Zijlstra671dec52009-04-06 11:45:02 +02002410 entry->next = *head;
2411 } while (cmpxchg(head, entry->next, entry) != entry->next);
Peter Zijlstra925d5192009-03-30 19:07:02 +02002412
2413 set_perf_counter_pending();
2414
Peter Zijlstra671dec52009-04-06 11:45:02 +02002415 put_cpu_var(perf_pending_head);
Peter Zijlstra925d5192009-03-30 19:07:02 +02002416}
2417
2418static int __perf_pending_run(void)
2419{
Peter Zijlstra671dec52009-04-06 11:45:02 +02002420 struct perf_pending_entry *list;
Peter Zijlstra925d5192009-03-30 19:07:02 +02002421 int nr = 0;
2422
Peter Zijlstra671dec52009-04-06 11:45:02 +02002423 list = xchg(&__get_cpu_var(perf_pending_head), PENDING_TAIL);
Peter Zijlstra925d5192009-03-30 19:07:02 +02002424 while (list != PENDING_TAIL) {
Peter Zijlstra671dec52009-04-06 11:45:02 +02002425 void (*func)(struct perf_pending_entry *);
2426 struct perf_pending_entry *entry = list;
Peter Zijlstra925d5192009-03-30 19:07:02 +02002427
2428 list = list->next;
2429
Peter Zijlstra671dec52009-04-06 11:45:02 +02002430 func = entry->func;
2431 entry->next = NULL;
Peter Zijlstra925d5192009-03-30 19:07:02 +02002432 /*
2433 * Ensure we observe the unqueue before we issue the wakeup,
2434 * so that we won't be waiting forever.
2435 * -- see perf_not_pending().
2436 */
2437 smp_wmb();
2438
Peter Zijlstra671dec52009-04-06 11:45:02 +02002439 func(entry);
Peter Zijlstra925d5192009-03-30 19:07:02 +02002440 nr++;
2441 }
2442
2443 return nr;
2444}
2445
2446static inline int perf_not_pending(struct perf_counter *counter)
2447{
2448 /*
2449 * If we flush on whatever cpu we run, there is a chance we don't
2450 * need to wait.
2451 */
2452 get_cpu();
2453 __perf_pending_run();
2454 put_cpu();
2455
2456 /*
2457 * Ensure we see the proper queue state before going to sleep
2458 * so that we do not miss the wakeup. -- see perf_pending_handle()
2459 */
2460 smp_rmb();
Peter Zijlstra671dec52009-04-06 11:45:02 +02002461 return counter->pending.next == NULL;
Peter Zijlstra925d5192009-03-30 19:07:02 +02002462}
2463
2464static void perf_pending_sync(struct perf_counter *counter)
2465{
2466 wait_event(counter->waitq, perf_not_pending(counter));
2467}
2468
2469void perf_counter_do_pending(void)
2470{
2471 __perf_pending_run();
2472}
2473
2474/*
Peter Zijlstra394ee072009-03-30 19:07:14 +02002475 * Callchain support -- arch specific
2476 */
2477
Peter Zijlstra9c03d882009-04-06 11:45:00 +02002478__weak struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
Peter Zijlstra394ee072009-03-30 19:07:14 +02002479{
2480 return NULL;
2481}
2482
2483/*
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002484 * Output
2485 */
2486
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002487struct perf_output_handle {
2488 struct perf_counter *counter;
2489 struct perf_mmap_data *data;
Peter Zijlstra8e3747c2009-06-02 16:16:02 +02002490 unsigned long head;
2491 unsigned long offset;
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002492 int nmi;
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002493 int sample;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002494 int locked;
2495 unsigned long flags;
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002496};
2497
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002498static bool perf_output_space(struct perf_mmap_data *data,
2499 unsigned int offset, unsigned int head)
2500{
2501 unsigned long tail;
2502 unsigned long mask;
2503
2504 if (!data->writable)
2505 return true;
2506
2507 mask = (data->nr_pages << PAGE_SHIFT) - 1;
2508 /*
2509 * Userspace could choose to issue a mb() before updating the tail
2510 * pointer. So that all reads will be completed before the write is
2511 * issued.
2512 */
2513 tail = ACCESS_ONCE(data->user_page->data_tail);
2514 smp_rmb();
2515
2516 offset = (offset - tail) & mask;
2517 head = (head - tail) & mask;
2518
2519 if ((int)(head - offset) < 0)
2520 return false;
2521
2522 return true;
2523}
2524
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002525static void perf_output_wakeup(struct perf_output_handle *handle)
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002526{
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002527 atomic_set(&handle->data->poll, POLL_IN);
2528
Peter Zijlstra671dec52009-04-06 11:45:02 +02002529 if (handle->nmi) {
Peter Zijlstra79f14642009-04-06 11:45:07 +02002530 handle->counter->pending_wakeup = 1;
Peter Zijlstra671dec52009-04-06 11:45:02 +02002531 perf_pending_queue(&handle->counter->pending,
Peter Zijlstra79f14642009-04-06 11:45:07 +02002532 perf_pending_counter);
Peter Zijlstra671dec52009-04-06 11:45:02 +02002533 } else
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002534 perf_counter_wakeup(handle->counter);
2535}
2536
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002537/*
2538 * Curious locking construct.
2539 *
2540 * We need to ensure a later event doesn't publish a head when a former
2541 * event isn't done writing. However since we need to deal with NMIs we
2542 * cannot fully serialize things.
2543 *
2544 * What we do is serialize between CPUs so we only have to deal with NMI
2545 * nesting on a single CPU.
2546 *
2547 * We only publish the head (and generate a wakeup) when the outer-most
2548 * event completes.
2549 */
2550static void perf_output_lock(struct perf_output_handle *handle)
2551{
2552 struct perf_mmap_data *data = handle->data;
2553 int cpu;
2554
2555 handle->locked = 0;
2556
2557 local_irq_save(handle->flags);
2558 cpu = smp_processor_id();
2559
2560 if (in_nmi() && atomic_read(&data->lock) == cpu)
2561 return;
2562
Peter Zijlstra22c15582009-05-05 17:50:25 +02002563 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002564 cpu_relax();
2565
2566 handle->locked = 1;
2567}
2568
2569static void perf_output_unlock(struct perf_output_handle *handle)
2570{
2571 struct perf_mmap_data *data = handle->data;
Peter Zijlstra8e3747c2009-06-02 16:16:02 +02002572 unsigned long head;
2573 int cpu;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002574
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002575 data->done_head = data->head;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002576
2577 if (!handle->locked)
2578 goto out;
2579
2580again:
2581 /*
2582 * The xchg implies a full barrier that ensures all writes are done
2583 * before we publish the new head, matched by a rmb() in userspace when
2584 * reading this position.
2585 */
Peter Zijlstra8e3747c2009-06-02 16:16:02 +02002586 while ((head = atomic_long_xchg(&data->done_head, 0)))
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002587 data->user_page->data_head = head;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002588
2589 /*
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002590 * NMI can happen here, which means we can miss a done_head update.
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002591 */
2592
Peter Zijlstra22c15582009-05-05 17:50:25 +02002593 cpu = atomic_xchg(&data->lock, -1);
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002594 WARN_ON_ONCE(cpu != smp_processor_id());
2595
2596 /*
2597 * Therefore we have to validate we did not indeed do so.
2598 */
Peter Zijlstra8e3747c2009-06-02 16:16:02 +02002599 if (unlikely(atomic_long_read(&data->done_head))) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002600 /*
2601 * Since we had it locked, we can lock it again.
2602 */
Peter Zijlstra22c15582009-05-05 17:50:25 +02002603 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002604 cpu_relax();
2605
2606 goto again;
2607 }
2608
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002609 if (atomic_xchg(&data->wakeup, 0))
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002610 perf_output_wakeup(handle);
2611out:
2612 local_irq_restore(handle->flags);
2613}
2614
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002615static void perf_output_copy(struct perf_output_handle *handle,
Peter Zijlstra089dd792009-06-05 14:04:55 +02002616 const void *buf, unsigned int len)
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002617{
2618 unsigned int pages_mask;
2619 unsigned int offset;
2620 unsigned int size;
2621 void **pages;
2622
2623 offset = handle->offset;
2624 pages_mask = handle->data->nr_pages - 1;
2625 pages = handle->data->data_pages;
2626
2627 do {
2628 unsigned int page_offset;
2629 int nr;
2630
2631 nr = (offset >> PAGE_SHIFT) & pages_mask;
2632 page_offset = offset & (PAGE_SIZE - 1);
2633 size = min_t(unsigned int, PAGE_SIZE - page_offset, len);
2634
2635 memcpy(pages[nr] + page_offset, buf, size);
2636
2637 len -= size;
2638 buf += size;
2639 offset += size;
2640 } while (len);
2641
2642 handle->offset = offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01002643
Peter Zijlstra53020fe2009-05-13 21:26:19 +02002644 /*
2645 * Check we didn't copy past our reservation window, taking the
2646 * possible unsigned int wrap into account.
2647 */
Peter Zijlstra8e3747c2009-06-02 16:16:02 +02002648 WARN_ON_ONCE(((long)(handle->head - handle->offset)) < 0);
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002649}
2650
Peter Zijlstra5c148192009-03-25 12:30:23 +01002651#define perf_output_put(handle, x) \
2652 perf_output_copy((handle), &(x), sizeof(x))
2653
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002654static int perf_output_begin(struct perf_output_handle *handle,
2655 struct perf_counter *counter, unsigned int size,
2656 int nmi, int sample)
2657{
2658 struct perf_mmap_data *data;
2659 unsigned int offset, head;
2660 int have_lost;
2661 struct {
2662 struct perf_event_header header;
2663 u64 id;
2664 u64 lost;
2665 } lost_event;
2666
2667 /*
2668 * For inherited counters we send all the output towards the parent.
2669 */
2670 if (counter->parent)
2671 counter = counter->parent;
2672
2673 rcu_read_lock();
2674 data = rcu_dereference(counter->data);
2675 if (!data)
2676 goto out;
2677
2678 handle->data = data;
2679 handle->counter = counter;
2680 handle->nmi = nmi;
2681 handle->sample = sample;
2682
2683 if (!data->nr_pages)
2684 goto fail;
2685
2686 have_lost = atomic_read(&data->lost);
2687 if (have_lost)
2688 size += sizeof(lost_event);
2689
2690 perf_output_lock(handle);
2691
2692 do {
2693 offset = head = atomic_long_read(&data->head);
2694 head += size;
2695 if (unlikely(!perf_output_space(data, offset, head)))
2696 goto fail;
2697 } while (atomic_long_cmpxchg(&data->head, offset, head) != offset);
2698
2699 handle->offset = offset;
2700 handle->head = head;
2701
2702 if ((offset >> PAGE_SHIFT) != (head >> PAGE_SHIFT))
2703 atomic_set(&data->wakeup, 1);
2704
2705 if (have_lost) {
2706 lost_event.header.type = PERF_EVENT_LOST;
2707 lost_event.header.misc = 0;
2708 lost_event.header.size = sizeof(lost_event);
2709 lost_event.id = counter->id;
2710 lost_event.lost = atomic_xchg(&data->lost, 0);
2711
2712 perf_output_put(handle, lost_event);
2713 }
2714
2715 return 0;
2716
2717fail:
2718 atomic_inc(&data->lost);
2719 perf_output_unlock(handle);
2720out:
2721 rcu_read_unlock();
2722
2723 return -ENOSPC;
2724}
2725
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002726static void perf_output_end(struct perf_output_handle *handle)
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002727{
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002728 struct perf_counter *counter = handle->counter;
2729 struct perf_mmap_data *data = handle->data;
2730
Peter Zijlstra0d486962009-06-02 19:22:16 +02002731 int wakeup_events = counter->attr.wakeup_events;
Peter Zijlstrac4578102009-04-02 11:12:01 +02002732
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002733 if (handle->sample && wakeup_events) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002734 int events = atomic_inc_return(&data->events);
Peter Zijlstrac4578102009-04-02 11:12:01 +02002735 if (events >= wakeup_events) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002736 atomic_sub(wakeup_events, &data->events);
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002737 atomic_set(&data->wakeup, 1);
Peter Zijlstrac4578102009-04-02 11:12:01 +02002738 }
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002739 }
2740
2741 perf_output_unlock(handle);
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002742 rcu_read_unlock();
2743}
2744
Peter Zijlstra709e50c2009-06-02 14:13:15 +02002745static u32 perf_counter_pid(struct perf_counter *counter, struct task_struct *p)
2746{
2747 /*
2748 * only top level counters have the pid namespace they were created in
2749 */
2750 if (counter->parent)
2751 counter = counter->parent;
2752
2753 return task_tgid_nr_ns(p, counter->ns);
2754}
2755
2756static u32 perf_counter_tid(struct perf_counter *counter, struct task_struct *p)
2757{
2758 /*
2759 * only top level counters have the pid namespace they were created in
2760 */
2761 if (counter->parent)
2762 counter = counter->parent;
2763
2764 return task_pid_nr_ns(p, counter->ns);
2765}
2766
Peter Zijlstra3dab77f2009-08-13 11:47:53 +02002767static void perf_output_read_one(struct perf_output_handle *handle,
2768 struct perf_counter *counter)
2769{
2770 u64 read_format = counter->attr.read_format;
2771 u64 values[4];
2772 int n = 0;
2773
2774 values[n++] = atomic64_read(&counter->count);
2775 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
2776 values[n++] = counter->total_time_enabled +
2777 atomic64_read(&counter->child_total_time_enabled);
2778 }
2779 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
2780 values[n++] = counter->total_time_running +
2781 atomic64_read(&counter->child_total_time_running);
2782 }
2783 if (read_format & PERF_FORMAT_ID)
2784 values[n++] = primary_counter_id(counter);
2785
2786 perf_output_copy(handle, values, n * sizeof(u64));
2787}
2788
2789/*
2790 * XXX PERF_FORMAT_GROUP vs inherited counters seems difficult.
2791 */
2792static void perf_output_read_group(struct perf_output_handle *handle,
2793 struct perf_counter *counter)
2794{
2795 struct perf_counter *leader = counter->group_leader, *sub;
2796 u64 read_format = counter->attr.read_format;
2797 u64 values[5];
2798 int n = 0;
2799
2800 values[n++] = 1 + leader->nr_siblings;
2801
2802 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2803 values[n++] = leader->total_time_enabled;
2804
2805 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2806 values[n++] = leader->total_time_running;
2807
2808 if (leader != counter)
2809 leader->pmu->read(leader);
2810
2811 values[n++] = atomic64_read(&leader->count);
2812 if (read_format & PERF_FORMAT_ID)
2813 values[n++] = primary_counter_id(leader);
2814
2815 perf_output_copy(handle, values, n * sizeof(u64));
2816
2817 list_for_each_entry(sub, &leader->sibling_list, list_entry) {
2818 n = 0;
2819
2820 if (sub != counter)
2821 sub->pmu->read(sub);
2822
2823 values[n++] = atomic64_read(&sub->count);
2824 if (read_format & PERF_FORMAT_ID)
2825 values[n++] = primary_counter_id(sub);
2826
2827 perf_output_copy(handle, values, n * sizeof(u64));
2828 }
2829}
2830
2831static void perf_output_read(struct perf_output_handle *handle,
2832 struct perf_counter *counter)
2833{
2834 if (counter->attr.read_format & PERF_FORMAT_GROUP)
2835 perf_output_read_group(handle, counter);
2836 else
2837 perf_output_read_one(handle, counter);
2838}
2839
Ingo Molnar28402972009-08-13 10:13:22 +02002840void perf_counter_output(struct perf_counter *counter, int nmi,
Peter Zijlstradf1a1322009-06-10 21:02:22 +02002841 struct perf_sample_data *data)
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002842{
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002843 int ret;
Peter Zijlstra0d486962009-06-02 19:22:16 +02002844 u64 sample_type = counter->attr.sample_type;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002845 struct perf_output_handle handle;
2846 struct perf_event_header header;
2847 u64 ip;
Peter Zijlstra5c148192009-03-25 12:30:23 +01002848 struct {
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002849 u32 pid, tid;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002850 } tid_entry;
Peter Zijlstra394ee072009-03-30 19:07:14 +02002851 struct perf_callchain_entry *callchain = NULL;
2852 int callchain_size = 0;
Peter Zijlstra339f7c92009-04-06 11:45:06 +02002853 u64 time;
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002854 struct {
2855 u32 cpu, reserved;
2856 } cpu_entry;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002857
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02002858 header.type = PERF_EVENT_SAMPLE;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002859 header.size = sizeof(header);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002860
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02002861 header.misc = 0;
Peter Zijlstradf1a1322009-06-10 21:02:22 +02002862 header.misc |= perf_misc_flags(data->regs);
Peter Zijlstra6fab0192009-04-08 15:01:26 +02002863
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002864 if (sample_type & PERF_SAMPLE_IP) {
Peter Zijlstradf1a1322009-06-10 21:02:22 +02002865 ip = perf_instruction_pointer(data->regs);
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002866 header.size += sizeof(ip);
2867 }
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002868
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002869 if (sample_type & PERF_SAMPLE_TID) {
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002870 /* namespace issues */
Peter Zijlstra709e50c2009-06-02 14:13:15 +02002871 tid_entry.pid = perf_counter_pid(counter, current);
2872 tid_entry.tid = perf_counter_tid(counter, current);
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002873
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002874 header.size += sizeof(tid_entry);
2875 }
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002876
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002877 if (sample_type & PERF_SAMPLE_TIME) {
Peter Zijlstra4d855452009-04-08 15:01:32 +02002878 /*
2879 * Maybe do better on x86 and provide cpu_clock_nmi()
2880 */
2881 time = sched_clock();
2882
Peter Zijlstra4d855452009-04-08 15:01:32 +02002883 header.size += sizeof(u64);
2884 }
2885
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02002886 if (sample_type & PERF_SAMPLE_ADDR)
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002887 header.size += sizeof(u64);
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002888
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02002889 if (sample_type & PERF_SAMPLE_ID)
Peter Zijlstraa85f61a2009-05-08 18:52:23 +02002890 header.size += sizeof(u64);
Peter Zijlstraa85f61a2009-05-08 18:52:23 +02002891
Peter Zijlstra7f453c22009-07-21 13:19:40 +02002892 if (sample_type & PERF_SAMPLE_STREAM_ID)
2893 header.size += sizeof(u64);
2894
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002895 if (sample_type & PERF_SAMPLE_CPU) {
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002896 header.size += sizeof(cpu_entry);
2897
2898 cpu_entry.cpu = raw_smp_processor_id();
Arjan van de Ven0dc3d522009-07-21 00:55:05 -07002899 cpu_entry.reserved = 0;
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002900 }
2901
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02002902 if (sample_type & PERF_SAMPLE_PERIOD)
Peter Zijlstra689802b2009-06-05 15:05:43 +02002903 header.size += sizeof(u64);
Peter Zijlstra689802b2009-06-05 15:05:43 +02002904
Peter Zijlstra3dab77f2009-08-13 11:47:53 +02002905 if (sample_type & PERF_SAMPLE_READ)
2906 header.size += perf_counter_read_size(counter);
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002907
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002908 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
Peter Zijlstradf1a1322009-06-10 21:02:22 +02002909 callchain = perf_callchain(data->regs);
Peter Zijlstra394ee072009-03-30 19:07:14 +02002910
2911 if (callchain) {
Peter Zijlstra9c03d882009-04-06 11:45:00 +02002912 callchain_size = (1 + callchain->nr) * sizeof(u64);
Peter Zijlstra394ee072009-03-30 19:07:14 +02002913 header.size += callchain_size;
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02002914 } else
2915 header.size += sizeof(u64);
Peter Zijlstra394ee072009-03-30 19:07:14 +02002916 }
2917
Frederic Weisbecker3a43ce62009-08-08 04:26:37 +02002918 if (sample_type & PERF_SAMPLE_RAW) {
Peter Zijlstraa0445602009-08-10 11:16:52 +02002919 int size = sizeof(u32);
2920
2921 if (data->raw)
2922 size += data->raw->size;
2923 else
2924 size += sizeof(u32);
2925
2926 WARN_ON_ONCE(size & (sizeof(u64)-1));
2927 header.size += size;
Frederic Weisbeckerf413cdb2009-08-07 01:25:54 +02002928 }
2929
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002930 ret = perf_output_begin(&handle, counter, header.size, nmi, 1);
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002931 if (ret)
2932 return;
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002933
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002934 perf_output_put(&handle, header);
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002935
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002936 if (sample_type & PERF_SAMPLE_IP)
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002937 perf_output_put(&handle, ip);
2938
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002939 if (sample_type & PERF_SAMPLE_TID)
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002940 perf_output_put(&handle, tid_entry);
2941
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002942 if (sample_type & PERF_SAMPLE_TIME)
Peter Zijlstra4d855452009-04-08 15:01:32 +02002943 perf_output_put(&handle, time);
2944
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002945 if (sample_type & PERF_SAMPLE_ADDR)
Peter Zijlstradf1a1322009-06-10 21:02:22 +02002946 perf_output_put(&handle, data->addr);
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002947
Peter Zijlstra7f453c22009-07-21 13:19:40 +02002948 if (sample_type & PERF_SAMPLE_ID) {
2949 u64 id = primary_counter_id(counter);
2950
2951 perf_output_put(&handle, id);
2952 }
2953
2954 if (sample_type & PERF_SAMPLE_STREAM_ID)
Peter Zijlstraac4bcf82009-06-05 14:44:52 +02002955 perf_output_put(&handle, counter->id);
Peter Zijlstraa85f61a2009-05-08 18:52:23 +02002956
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002957 if (sample_type & PERF_SAMPLE_CPU)
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002958 perf_output_put(&handle, cpu_entry);
2959
Peter Zijlstra689802b2009-06-05 15:05:43 +02002960 if (sample_type & PERF_SAMPLE_PERIOD)
Peter Zijlstra9e350de2009-06-10 21:34:59 +02002961 perf_output_put(&handle, data->period);
Peter Zijlstra689802b2009-06-05 15:05:43 +02002962
Peter Zijlstra3dab77f2009-08-13 11:47:53 +02002963 if (sample_type & PERF_SAMPLE_READ)
2964 perf_output_read(&handle, counter);
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002965
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02002966 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
2967 if (callchain)
2968 perf_output_copy(&handle, callchain, callchain_size);
2969 else {
2970 u64 nr = 0;
2971 perf_output_put(&handle, nr);
2972 }
2973 }
Peter Zijlstra394ee072009-03-30 19:07:14 +02002974
Peter Zijlstraa0445602009-08-10 11:16:52 +02002975 if (sample_type & PERF_SAMPLE_RAW) {
2976 if (data->raw) {
2977 perf_output_put(&handle, data->raw->size);
2978 perf_output_copy(&handle, data->raw->data, data->raw->size);
2979 } else {
2980 struct {
2981 u32 size;
2982 u32 data;
2983 } raw = {
2984 .size = sizeof(u32),
2985 .data = 0,
2986 };
2987 perf_output_put(&handle, raw);
2988 }
2989 }
Frederic Weisbeckerf413cdb2009-08-07 01:25:54 +02002990
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002991 perf_output_end(&handle);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002992}
2993
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002994/*
Peter Zijlstra38b200d2009-06-23 20:13:11 +02002995 * read event
2996 */
2997
2998struct perf_read_event {
2999 struct perf_event_header header;
3000
3001 u32 pid;
3002 u32 tid;
Peter Zijlstra38b200d2009-06-23 20:13:11 +02003003};
3004
3005static void
3006perf_counter_read_event(struct perf_counter *counter,
3007 struct task_struct *task)
3008{
3009 struct perf_output_handle handle;
3010 struct perf_read_event event = {
3011 .header = {
3012 .type = PERF_EVENT_READ,
3013 .misc = 0,
Peter Zijlstra3dab77f2009-08-13 11:47:53 +02003014 .size = sizeof(event) + perf_counter_read_size(counter),
Peter Zijlstra38b200d2009-06-23 20:13:11 +02003015 },
3016 .pid = perf_counter_pid(counter, task),
3017 .tid = perf_counter_tid(counter, task),
Peter Zijlstra38b200d2009-06-23 20:13:11 +02003018 };
Peter Zijlstra3dab77f2009-08-13 11:47:53 +02003019 int ret;
Peter Zijlstra38b200d2009-06-23 20:13:11 +02003020
3021 ret = perf_output_begin(&handle, counter, event.header.size, 0, 0);
3022 if (ret)
3023 return;
3024
Peter Zijlstra3dab77f2009-08-13 11:47:53 +02003025 perf_output_put(&handle, event);
3026 perf_output_read(&handle, counter);
3027
Peter Zijlstra38b200d2009-06-23 20:13:11 +02003028 perf_output_end(&handle);
3029}
3030
3031/*
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02003032 * task tracking -- fork/exit
3033 *
3034 * enabled by: attr.comm | attr.mmap | attr.task
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003035 */
3036
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02003037struct perf_task_event {
Peter Zijlstra3a80b4a2009-08-07 19:49:01 +02003038 struct task_struct *task;
3039 struct perf_counter_context *task_ctx;
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003040
3041 struct {
3042 struct perf_event_header header;
3043
3044 u32 pid;
3045 u32 ppid;
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02003046 u32 tid;
3047 u32 ptid;
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003048 } event;
3049};
3050
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02003051static void perf_counter_task_output(struct perf_counter *counter,
3052 struct perf_task_event *task_event)
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003053{
3054 struct perf_output_handle handle;
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02003055 int size = task_event->event.header.size;
3056 struct task_struct *task = task_event->task;
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003057 int ret = perf_output_begin(&handle, counter, size, 0, 0);
3058
3059 if (ret)
3060 return;
3061
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02003062 task_event->event.pid = perf_counter_pid(counter, task);
Peter Zijlstra94d5d1b2009-08-13 16:14:42 +02003063 task_event->event.ppid = perf_counter_pid(counter, current);
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003064
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02003065 task_event->event.tid = perf_counter_tid(counter, task);
Peter Zijlstra94d5d1b2009-08-13 16:14:42 +02003066 task_event->event.ptid = perf_counter_tid(counter, current);
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02003067
3068 perf_output_put(&handle, task_event->event);
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003069 perf_output_end(&handle);
3070}
3071
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02003072static int perf_counter_task_match(struct perf_counter *counter)
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003073{
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02003074 if (counter->attr.comm || counter->attr.mmap || counter->attr.task)
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003075 return 1;
3076
3077 return 0;
3078}
3079
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02003080static void perf_counter_task_ctx(struct perf_counter_context *ctx,
3081 struct perf_task_event *task_event)
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003082{
3083 struct perf_counter *counter;
3084
3085 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
3086 return;
3087
3088 rcu_read_lock();
3089 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02003090 if (perf_counter_task_match(counter))
3091 perf_counter_task_output(counter, task_event);
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003092 }
3093 rcu_read_unlock();
3094}
3095
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02003096static void perf_counter_task_event(struct perf_task_event *task_event)
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003097{
3098 struct perf_cpu_context *cpuctx;
Peter Zijlstra3a80b4a2009-08-07 19:49:01 +02003099 struct perf_counter_context *ctx = task_event->task_ctx;
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003100
3101 cpuctx = &get_cpu_var(perf_cpu_context);
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02003102 perf_counter_task_ctx(&cpuctx->ctx, task_event);
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003103 put_cpu_var(perf_cpu_context);
3104
3105 rcu_read_lock();
Peter Zijlstra3a80b4a2009-08-07 19:49:01 +02003106 if (!ctx)
3107 ctx = rcu_dereference(task_event->task->perf_counter_ctxp);
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003108 if (ctx)
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02003109 perf_counter_task_ctx(ctx, task_event);
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003110 rcu_read_unlock();
3111}
3112
Peter Zijlstra3a80b4a2009-08-07 19:49:01 +02003113static void perf_counter_task(struct task_struct *task,
3114 struct perf_counter_context *task_ctx,
3115 int new)
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02003116{
3117 struct perf_task_event task_event;
3118
3119 if (!atomic_read(&nr_comm_counters) &&
3120 !atomic_read(&nr_mmap_counters) &&
3121 !atomic_read(&nr_task_counters))
3122 return;
3123
3124 task_event = (struct perf_task_event){
Peter Zijlstra3a80b4a2009-08-07 19:49:01 +02003125 .task = task,
3126 .task_ctx = task_ctx,
3127 .event = {
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02003128 .header = {
3129 .type = new ? PERF_EVENT_FORK : PERF_EVENT_EXIT,
3130 .misc = 0,
3131 .size = sizeof(task_event.event),
3132 },
3133 /* .pid */
3134 /* .ppid */
3135 /* .tid */
3136 /* .ptid */
3137 },
3138 };
3139
3140 perf_counter_task_event(&task_event);
3141}
3142
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003143void perf_counter_fork(struct task_struct *task)
3144{
Peter Zijlstra3a80b4a2009-08-07 19:49:01 +02003145 perf_counter_task(task, NULL, 1);
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003146}
3147
3148/*
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02003149 * comm tracking
3150 */
3151
3152struct perf_comm_event {
Ingo Molnar22a4f652009-06-01 10:13:37 +02003153 struct task_struct *task;
3154 char *comm;
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02003155 int comm_size;
3156
3157 struct {
3158 struct perf_event_header header;
3159
3160 u32 pid;
3161 u32 tid;
3162 } event;
3163};
3164
3165static void perf_counter_comm_output(struct perf_counter *counter,
3166 struct perf_comm_event *comm_event)
3167{
3168 struct perf_output_handle handle;
3169 int size = comm_event->event.header.size;
3170 int ret = perf_output_begin(&handle, counter, size, 0, 0);
3171
3172 if (ret)
3173 return;
3174
Peter Zijlstra709e50c2009-06-02 14:13:15 +02003175 comm_event->event.pid = perf_counter_pid(counter, comm_event->task);
3176 comm_event->event.tid = perf_counter_tid(counter, comm_event->task);
3177
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02003178 perf_output_put(&handle, comm_event->event);
3179 perf_output_copy(&handle, comm_event->comm,
3180 comm_event->comm_size);
3181 perf_output_end(&handle);
3182}
3183
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003184static int perf_counter_comm_match(struct perf_counter *counter)
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02003185{
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003186 if (counter->attr.comm)
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02003187 return 1;
3188
3189 return 0;
3190}
3191
3192static void perf_counter_comm_ctx(struct perf_counter_context *ctx,
3193 struct perf_comm_event *comm_event)
3194{
3195 struct perf_counter *counter;
3196
3197 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
3198 return;
3199
3200 rcu_read_lock();
3201 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003202 if (perf_counter_comm_match(counter))
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02003203 perf_counter_comm_output(counter, comm_event);
3204 }
3205 rcu_read_unlock();
3206}
3207
3208static void perf_counter_comm_event(struct perf_comm_event *comm_event)
3209{
3210 struct perf_cpu_context *cpuctx;
Peter Zijlstra665c2142009-05-29 14:51:57 +02003211 struct perf_counter_context *ctx;
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02003212 unsigned int size;
Anton Blanchard413ee3b2009-07-16 15:15:52 +02003213 char comm[TASK_COMM_LEN];
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02003214
Anton Blanchard413ee3b2009-07-16 15:15:52 +02003215 memset(comm, 0, sizeof(comm));
3216 strncpy(comm, comm_event->task->comm, sizeof(comm));
Ingo Molnar888fcee2009-04-09 09:48:22 +02003217 size = ALIGN(strlen(comm)+1, sizeof(u64));
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02003218
3219 comm_event->comm = comm;
3220 comm_event->comm_size = size;
3221
3222 comm_event->event.header.size = sizeof(comm_event->event) + size;
3223
3224 cpuctx = &get_cpu_var(perf_cpu_context);
3225 perf_counter_comm_ctx(&cpuctx->ctx, comm_event);
3226 put_cpu_var(perf_cpu_context);
Peter Zijlstra665c2142009-05-29 14:51:57 +02003227
3228 rcu_read_lock();
3229 /*
3230 * doesn't really matter which of the child contexts the
3231 * events ends up in.
3232 */
3233 ctx = rcu_dereference(current->perf_counter_ctxp);
3234 if (ctx)
3235 perf_counter_comm_ctx(ctx, comm_event);
3236 rcu_read_unlock();
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02003237}
3238
3239void perf_counter_comm(struct task_struct *task)
3240{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02003241 struct perf_comm_event comm_event;
3242
Paul Mackerras57e79862009-06-30 16:07:19 +10003243 if (task->perf_counter_ctxp)
3244 perf_counter_enable_on_exec(task);
3245
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003246 if (!atomic_read(&nr_comm_counters))
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02003247 return;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003248
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02003249 comm_event = (struct perf_comm_event){
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02003250 .task = task,
Peter Zijlstra573402d2009-07-22 11:13:50 +02003251 /* .comm */
3252 /* .comm_size */
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02003253 .event = {
Peter Zijlstra573402d2009-07-22 11:13:50 +02003254 .header = {
3255 .type = PERF_EVENT_COMM,
3256 .misc = 0,
3257 /* .size */
3258 },
3259 /* .pid */
3260 /* .tid */
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02003261 },
3262 };
3263
3264 perf_counter_comm_event(&comm_event);
3265}
3266
3267/*
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003268 * mmap tracking
3269 */
3270
3271struct perf_mmap_event {
Peter Zijlstra089dd792009-06-05 14:04:55 +02003272 struct vm_area_struct *vma;
3273
3274 const char *file_name;
3275 int file_size;
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003276
3277 struct {
3278 struct perf_event_header header;
3279
3280 u32 pid;
3281 u32 tid;
3282 u64 start;
3283 u64 len;
3284 u64 pgoff;
3285 } event;
3286};
3287
3288static void perf_counter_mmap_output(struct perf_counter *counter,
3289 struct perf_mmap_event *mmap_event)
3290{
3291 struct perf_output_handle handle;
3292 int size = mmap_event->event.header.size;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02003293 int ret = perf_output_begin(&handle, counter, size, 0, 0);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003294
3295 if (ret)
3296 return;
3297
Peter Zijlstra709e50c2009-06-02 14:13:15 +02003298 mmap_event->event.pid = perf_counter_pid(counter, current);
3299 mmap_event->event.tid = perf_counter_tid(counter, current);
3300
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003301 perf_output_put(&handle, mmap_event->event);
3302 perf_output_copy(&handle, mmap_event->file_name,
3303 mmap_event->file_size);
Peter Zijlstra78d613e2009-03-30 19:07:11 +02003304 perf_output_end(&handle);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003305}
3306
3307static int perf_counter_mmap_match(struct perf_counter *counter,
3308 struct perf_mmap_event *mmap_event)
3309{
Peter Zijlstrad99e9442009-06-04 17:08:58 +02003310 if (counter->attr.mmap)
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003311 return 1;
3312
3313 return 0;
3314}
3315
3316static void perf_counter_mmap_ctx(struct perf_counter_context *ctx,
3317 struct perf_mmap_event *mmap_event)
3318{
3319 struct perf_counter *counter;
3320
3321 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
3322 return;
3323
3324 rcu_read_lock();
3325 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
3326 if (perf_counter_mmap_match(counter, mmap_event))
3327 perf_counter_mmap_output(counter, mmap_event);
3328 }
3329 rcu_read_unlock();
3330}
3331
3332static void perf_counter_mmap_event(struct perf_mmap_event *mmap_event)
3333{
3334 struct perf_cpu_context *cpuctx;
Peter Zijlstra665c2142009-05-29 14:51:57 +02003335 struct perf_counter_context *ctx;
Peter Zijlstra089dd792009-06-05 14:04:55 +02003336 struct vm_area_struct *vma = mmap_event->vma;
3337 struct file *file = vma->vm_file;
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003338 unsigned int size;
3339 char tmp[16];
3340 char *buf = NULL;
Peter Zijlstra089dd792009-06-05 14:04:55 +02003341 const char *name;
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003342
Anton Blanchard413ee3b2009-07-16 15:15:52 +02003343 memset(tmp, 0, sizeof(tmp));
3344
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003345 if (file) {
Anton Blanchard413ee3b2009-07-16 15:15:52 +02003346 /*
3347 * d_path works from the end of the buffer backwards, so we
3348 * need to add enough zero bytes after the string to handle
3349 * the 64bit alignment we do later.
3350 */
3351 buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003352 if (!buf) {
3353 name = strncpy(tmp, "//enomem", sizeof(tmp));
3354 goto got_name;
3355 }
Peter Zijlstrad3d21c42009-04-09 10:53:46 +02003356 name = d_path(&file->f_path, buf, PATH_MAX);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003357 if (IS_ERR(name)) {
3358 name = strncpy(tmp, "//toolong", sizeof(tmp));
3359 goto got_name;
3360 }
3361 } else {
Anton Blanchard413ee3b2009-07-16 15:15:52 +02003362 if (arch_vma_name(mmap_event->vma)) {
3363 name = strncpy(tmp, arch_vma_name(mmap_event->vma),
3364 sizeof(tmp));
Peter Zijlstra089dd792009-06-05 14:04:55 +02003365 goto got_name;
Anton Blanchard413ee3b2009-07-16 15:15:52 +02003366 }
Peter Zijlstra089dd792009-06-05 14:04:55 +02003367
3368 if (!vma->vm_mm) {
3369 name = strncpy(tmp, "[vdso]", sizeof(tmp));
3370 goto got_name;
3371 }
3372
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003373 name = strncpy(tmp, "//anon", sizeof(tmp));
3374 goto got_name;
3375 }
3376
3377got_name:
Ingo Molnar888fcee2009-04-09 09:48:22 +02003378 size = ALIGN(strlen(name)+1, sizeof(u64));
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003379
3380 mmap_event->file_name = name;
3381 mmap_event->file_size = size;
3382
3383 mmap_event->event.header.size = sizeof(mmap_event->event) + size;
3384
3385 cpuctx = &get_cpu_var(perf_cpu_context);
3386 perf_counter_mmap_ctx(&cpuctx->ctx, mmap_event);
3387 put_cpu_var(perf_cpu_context);
3388
Peter Zijlstra665c2142009-05-29 14:51:57 +02003389 rcu_read_lock();
3390 /*
3391 * doesn't really matter which of the child contexts the
3392 * events ends up in.
3393 */
3394 ctx = rcu_dereference(current->perf_counter_ctxp);
3395 if (ctx)
3396 perf_counter_mmap_ctx(ctx, mmap_event);
3397 rcu_read_unlock();
3398
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003399 kfree(buf);
3400}
3401
Peter Zijlstra089dd792009-06-05 14:04:55 +02003402void __perf_counter_mmap(struct vm_area_struct *vma)
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003403{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02003404 struct perf_mmap_event mmap_event;
3405
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003406 if (!atomic_read(&nr_mmap_counters))
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02003407 return;
3408
3409 mmap_event = (struct perf_mmap_event){
Peter Zijlstra089dd792009-06-05 14:04:55 +02003410 .vma = vma,
Peter Zijlstra573402d2009-07-22 11:13:50 +02003411 /* .file_name */
3412 /* .file_size */
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003413 .event = {
Peter Zijlstra573402d2009-07-22 11:13:50 +02003414 .header = {
3415 .type = PERF_EVENT_MMAP,
3416 .misc = 0,
3417 /* .size */
3418 },
3419 /* .pid */
3420 /* .tid */
Peter Zijlstra089dd792009-06-05 14:04:55 +02003421 .start = vma->vm_start,
3422 .len = vma->vm_end - vma->vm_start,
3423 .pgoff = vma->vm_pgoff,
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003424 },
3425 };
3426
3427 perf_counter_mmap_event(&mmap_event);
3428}
3429
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003430/*
Peter Zijlstraa78ac322009-05-25 17:39:05 +02003431 * IRQ throttle logging
3432 */
3433
3434static void perf_log_throttle(struct perf_counter *counter, int enable)
3435{
3436 struct perf_output_handle handle;
3437 int ret;
3438
3439 struct {
3440 struct perf_event_header header;
3441 u64 time;
Peter Zijlstracca3f452009-06-11 14:57:55 +02003442 u64 id;
Peter Zijlstra7f453c22009-07-21 13:19:40 +02003443 u64 stream_id;
Peter Zijlstraa78ac322009-05-25 17:39:05 +02003444 } throttle_event = {
3445 .header = {
Anton Blanchard966ee4d2009-07-22 23:05:46 +10003446 .type = PERF_EVENT_THROTTLE,
Peter Zijlstraa78ac322009-05-25 17:39:05 +02003447 .misc = 0,
3448 .size = sizeof(throttle_event),
3449 },
Peter Zijlstra7f453c22009-07-21 13:19:40 +02003450 .time = sched_clock(),
3451 .id = primary_counter_id(counter),
3452 .stream_id = counter->id,
Peter Zijlstraa78ac322009-05-25 17:39:05 +02003453 };
3454
Anton Blanchard966ee4d2009-07-22 23:05:46 +10003455 if (enable)
3456 throttle_event.header.type = PERF_EVENT_UNTHROTTLE;
3457
Ingo Molnar0127c3e2009-05-25 22:03:26 +02003458 ret = perf_output_begin(&handle, counter, sizeof(throttle_event), 1, 0);
Peter Zijlstraa78ac322009-05-25 17:39:05 +02003459 if (ret)
3460 return;
3461
3462 perf_output_put(&handle, throttle_event);
3463 perf_output_end(&handle);
3464}
3465
3466/*
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01003467 * Generic counter overflow handling, sampling.
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02003468 */
3469
Peter Zijlstradf1a1322009-06-10 21:02:22 +02003470int perf_counter_overflow(struct perf_counter *counter, int nmi,
3471 struct perf_sample_data *data)
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02003472{
Peter Zijlstra79f14642009-04-06 11:45:07 +02003473 int events = atomic_read(&counter->event_limit);
Peter Zijlstraa78ac322009-05-25 17:39:05 +02003474 int throttle = counter->pmu->unthrottle != NULL;
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02003475 struct hw_perf_counter *hwc = &counter->hw;
Peter Zijlstra79f14642009-04-06 11:45:07 +02003476 int ret = 0;
3477
Peter Zijlstraa78ac322009-05-25 17:39:05 +02003478 if (!throttle) {
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02003479 hwc->interrupts++;
Ingo Molnar128f0482009-06-03 22:19:36 +02003480 } else {
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02003481 if (hwc->interrupts != MAX_INTERRUPTS) {
3482 hwc->interrupts++;
Peter Zijlstradf58ab22009-06-11 11:25:05 +02003483 if (HZ * hwc->interrupts >
3484 (u64)sysctl_perf_counter_sample_rate) {
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02003485 hwc->interrupts = MAX_INTERRUPTS;
Ingo Molnar128f0482009-06-03 22:19:36 +02003486 perf_log_throttle(counter, 0);
3487 ret = 1;
3488 }
3489 } else {
3490 /*
3491 * Keep re-disabling counters even though on the previous
3492 * pass we disabled it - just in case we raced with a
3493 * sched-in and the counter got enabled again:
3494 */
Peter Zijlstraa78ac322009-05-25 17:39:05 +02003495 ret = 1;
3496 }
3497 }
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003498
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02003499 if (counter->attr.freq) {
3500 u64 now = sched_clock();
3501 s64 delta = now - hwc->freq_stamp;
3502
3503 hwc->freq_stamp = now;
3504
3505 if (delta > 0 && delta < TICK_NSEC)
3506 perf_adjust_period(counter, NSEC_PER_SEC / (int)delta);
3507 }
3508
Peter Zijlstra2023b352009-05-05 17:50:26 +02003509 /*
3510 * XXX event_limit might not quite work as expected on inherited
3511 * counters
3512 */
3513
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02003514 counter->pending_kill = POLL_IN;
Peter Zijlstra79f14642009-04-06 11:45:07 +02003515 if (events && atomic_dec_and_test(&counter->event_limit)) {
3516 ret = 1;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02003517 counter->pending_kill = POLL_HUP;
Peter Zijlstra79f14642009-04-06 11:45:07 +02003518 if (nmi) {
3519 counter->pending_disable = 1;
3520 perf_pending_queue(&counter->pending,
3521 perf_pending_counter);
3522 } else
3523 perf_counter_disable(counter);
3524 }
3525
Peter Zijlstradf1a1322009-06-10 21:02:22 +02003526 perf_counter_output(counter, nmi, data);
Peter Zijlstra79f14642009-04-06 11:45:07 +02003527 return ret;
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02003528}
3529
3530/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003531 * Generic software counter infrastructure
3532 */
3533
Peter Zijlstra7b4b6652009-07-22 09:29:32 +02003534/*
3535 * We directly increment counter->count and keep a second value in
3536 * counter->hw.period_left to count intervals. This period counter
3537 * is kept in the range [-sample_period, 0] so that we can use the
3538 * sign as trigger.
3539 */
3540
3541static u64 perf_swcounter_set_period(struct perf_counter *counter)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003542{
3543 struct hw_perf_counter *hwc = &counter->hw;
Peter Zijlstra7b4b6652009-07-22 09:29:32 +02003544 u64 period = hwc->last_period;
3545 u64 nr, offset;
3546 s64 old, val;
3547
3548 hwc->last_period = hwc->sample_period;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003549
3550again:
Peter Zijlstra7b4b6652009-07-22 09:29:32 +02003551 old = val = atomic64_read(&hwc->period_left);
3552 if (val < 0)
3553 return 0;
3554
3555 nr = div64_u64(period + val, period);
3556 offset = nr * period;
3557 val -= offset;
3558 if (atomic64_cmpxchg(&hwc->period_left, old, val) != old)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003559 goto again;
3560
Peter Zijlstra7b4b6652009-07-22 09:29:32 +02003561 return nr;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003562}
3563
3564static void perf_swcounter_overflow(struct perf_counter *counter,
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003565 int nmi, struct perf_sample_data *data)
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003566{
Peter Zijlstra7b4b6652009-07-22 09:29:32 +02003567 struct hw_perf_counter *hwc = &counter->hw;
3568 u64 overflow;
Peter Zijlstradf1a1322009-06-10 21:02:22 +02003569
Peter Zijlstra7b4b6652009-07-22 09:29:32 +02003570 data->period = counter->hw.last_period;
3571 overflow = perf_swcounter_set_period(counter);
3572
3573 if (hwc->interrupts == MAX_INTERRUPTS)
3574 return;
3575
3576 for (; overflow; overflow--) {
3577 if (perf_counter_overflow(counter, nmi, data)) {
3578 /*
3579 * We inhibit the overflow from happening when
3580 * hwc->interrupts == MAX_INTERRUPTS.
3581 */
3582 break;
3583 }
3584 }
3585}
3586
3587static void perf_swcounter_unthrottle(struct perf_counter *counter)
3588{
3589 /*
3590 * Nothing to do, we already reset hwc->interrupts.
3591 */
3592}
3593
3594static void perf_swcounter_add(struct perf_counter *counter, u64 nr,
3595 int nmi, struct perf_sample_data *data)
3596{
3597 struct hw_perf_counter *hwc = &counter->hw;
3598
3599 atomic64_add(nr, &counter->count);
3600
3601 if (!hwc->sample_period)
3602 return;
3603
3604 if (!data->regs)
3605 return;
3606
3607 if (!atomic64_add_negative(nr, &hwc->period_left))
3608 perf_swcounter_overflow(counter, nmi, data);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003609}
3610
Paul Mackerras880ca152009-06-01 17:49:14 +10003611static int perf_swcounter_is_counting(struct perf_counter *counter)
3612{
Peter Zijlstrabcfc2602009-08-13 09:51:55 +02003613 /*
3614 * The counter is active, we're good!
3615 */
Paul Mackerras880ca152009-06-01 17:49:14 +10003616 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
3617 return 1;
3618
Peter Zijlstrabcfc2602009-08-13 09:51:55 +02003619 /*
3620 * The counter is off/error, not counting.
3621 */
Paul Mackerras880ca152009-06-01 17:49:14 +10003622 if (counter->state != PERF_COUNTER_STATE_INACTIVE)
3623 return 0;
3624
3625 /*
Peter Zijlstrabcfc2602009-08-13 09:51:55 +02003626 * The counter is inactive, if the context is active
3627 * we're part of a group that didn't make it on the 'pmu',
3628 * not counting.
Paul Mackerras880ca152009-06-01 17:49:14 +10003629 */
Peter Zijlstrabcfc2602009-08-13 09:51:55 +02003630 if (counter->ctx->is_active)
3631 return 0;
3632
3633 /*
3634 * We're inactive and the context is too, this means the
3635 * task is scheduled out, we're counting events that happen
3636 * to us, like migration events.
3637 */
3638 return 1;
Paul Mackerras880ca152009-06-01 17:49:14 +10003639}
3640
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003641static int perf_swcounter_match(struct perf_counter *counter,
Peter Zijlstra1c432d82009-06-11 13:19:29 +02003642 enum perf_type_id type,
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003643 u32 event, struct pt_regs *regs)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003644{
Paul Mackerras880ca152009-06-01 17:49:14 +10003645 if (!perf_swcounter_is_counting(counter))
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003646 return 0;
3647
Ingo Molnara21ca2c2009-06-06 09:58:57 +02003648 if (counter->attr.type != type)
3649 return 0;
3650 if (counter->attr.config != event)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003651 return 0;
3652
Paul Mackerras3f731ca2009-06-01 17:52:30 +10003653 if (regs) {
Peter Zijlstra0d486962009-06-02 19:22:16 +02003654 if (counter->attr.exclude_user && user_mode(regs))
Paul Mackerras3f731ca2009-06-01 17:52:30 +10003655 return 0;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003656
Peter Zijlstra0d486962009-06-02 19:22:16 +02003657 if (counter->attr.exclude_kernel && !user_mode(regs))
Paul Mackerras3f731ca2009-06-01 17:52:30 +10003658 return 0;
3659 }
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003660
3661 return 1;
3662}
3663
3664static void perf_swcounter_ctx_event(struct perf_counter_context *ctx,
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003665 enum perf_type_id type,
3666 u32 event, u64 nr, int nmi,
3667 struct perf_sample_data *data)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003668{
3669 struct perf_counter *counter;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003670
Peter Zijlstra01ef09d2009-03-19 20:26:11 +01003671 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003672 return;
3673
Peter Zijlstra592903c2009-03-13 12:21:36 +01003674 rcu_read_lock();
3675 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003676 if (perf_swcounter_match(counter, type, event, data->regs))
3677 perf_swcounter_add(counter, nr, nmi, data);
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003678 }
Peter Zijlstra592903c2009-03-13 12:21:36 +01003679 rcu_read_unlock();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003680}
3681
Peter Zijlstra96f6d442009-03-23 18:22:07 +01003682static int *perf_swcounter_recursion_context(struct perf_cpu_context *cpuctx)
3683{
3684 if (in_nmi())
3685 return &cpuctx->recursion[3];
3686
3687 if (in_irq())
3688 return &cpuctx->recursion[2];
3689
3690 if (in_softirq())
3691 return &cpuctx->recursion[1];
3692
3693 return &cpuctx->recursion[0];
3694}
3695
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003696static void do_perf_swcounter_event(enum perf_type_id type, u32 event,
3697 u64 nr, int nmi,
3698 struct perf_sample_data *data)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003699{
3700 struct perf_cpu_context *cpuctx = &get_cpu_var(perf_cpu_context);
Peter Zijlstra96f6d442009-03-23 18:22:07 +01003701 int *recursion = perf_swcounter_recursion_context(cpuctx);
Peter Zijlstra665c2142009-05-29 14:51:57 +02003702 struct perf_counter_context *ctx;
Peter Zijlstra96f6d442009-03-23 18:22:07 +01003703
3704 if (*recursion)
3705 goto out;
3706
3707 (*recursion)++;
3708 barrier();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003709
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003710 perf_swcounter_ctx_event(&cpuctx->ctx, type, event,
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003711 nr, nmi, data);
Peter Zijlstra665c2142009-05-29 14:51:57 +02003712 rcu_read_lock();
3713 /*
3714 * doesn't really matter which of the child contexts the
3715 * events ends up in.
3716 */
3717 ctx = rcu_dereference(current->perf_counter_ctxp);
3718 if (ctx)
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003719 perf_swcounter_ctx_event(ctx, type, event, nr, nmi, data);
Peter Zijlstra665c2142009-05-29 14:51:57 +02003720 rcu_read_unlock();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003721
Peter Zijlstra96f6d442009-03-23 18:22:07 +01003722 barrier();
3723 (*recursion)--;
3724
3725out:
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003726 put_cpu_var(perf_cpu_context);
3727}
3728
Peter Zijlstraf29ac752009-06-19 18:27:26 +02003729void __perf_swcounter_event(u32 event, u64 nr, int nmi,
3730 struct pt_regs *regs, u64 addr)
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003731{
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003732 struct perf_sample_data data = {
3733 .regs = regs,
3734 .addr = addr,
3735 };
3736
3737 do_perf_swcounter_event(PERF_TYPE_SOFTWARE, event, nr, nmi, &data);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003738}
3739
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003740static void perf_swcounter_read(struct perf_counter *counter)
3741{
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003742}
3743
3744static int perf_swcounter_enable(struct perf_counter *counter)
3745{
Peter Zijlstra7b4b6652009-07-22 09:29:32 +02003746 struct hw_perf_counter *hwc = &counter->hw;
3747
3748 if (hwc->sample_period) {
3749 hwc->last_period = hwc->sample_period;
3750 perf_swcounter_set_period(counter);
3751 }
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003752 return 0;
3753}
3754
3755static void perf_swcounter_disable(struct perf_counter *counter)
3756{
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003757}
3758
Robert Richter4aeb0b42009-04-29 12:47:03 +02003759static const struct pmu perf_ops_generic = {
Peter Zijlstraac17dc82009-03-13 12:21:34 +01003760 .enable = perf_swcounter_enable,
3761 .disable = perf_swcounter_disable,
3762 .read = perf_swcounter_read,
Peter Zijlstra7b4b6652009-07-22 09:29:32 +02003763 .unthrottle = perf_swcounter_unthrottle,
Peter Zijlstraac17dc82009-03-13 12:21:34 +01003764};
3765
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003766/*
Peter Zijlstra7b4b6652009-07-22 09:29:32 +02003767 * hrtimer based swcounter callback
3768 */
3769
3770static enum hrtimer_restart perf_swcounter_hrtimer(struct hrtimer *hrtimer)
3771{
3772 enum hrtimer_restart ret = HRTIMER_RESTART;
3773 struct perf_sample_data data;
3774 struct perf_counter *counter;
3775 u64 period;
3776
3777 counter = container_of(hrtimer, struct perf_counter, hw.hrtimer);
3778 counter->pmu->read(counter);
3779
3780 data.addr = 0;
3781 data.regs = get_irq_regs();
3782 /*
3783 * In case we exclude kernel IPs or are somehow not in interrupt
3784 * context, provide the next best thing, the user IP.
3785 */
3786 if ((counter->attr.exclude_kernel || !data.regs) &&
3787 !counter->attr.exclude_user)
3788 data.regs = task_pt_regs(current);
3789
3790 if (data.regs) {
3791 if (perf_counter_overflow(counter, 0, &data))
3792 ret = HRTIMER_NORESTART;
3793 }
3794
3795 period = max_t(u64, 10000, counter->hw.sample_period);
3796 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
3797
3798 return ret;
3799}
3800
3801/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003802 * Software counter: cpu wall time clock
3803 */
3804
Paul Mackerras9abf8a02009-01-09 16:26:43 +11003805static void cpu_clock_perf_counter_update(struct perf_counter *counter)
3806{
3807 int cpu = raw_smp_processor_id();
3808 s64 prev;
3809 u64 now;
3810
3811 now = cpu_clock(cpu);
3812 prev = atomic64_read(&counter->hw.prev_count);
3813 atomic64_set(&counter->hw.prev_count, now);
3814 atomic64_add(now - prev, &counter->count);
3815}
3816
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003817static int cpu_clock_perf_counter_enable(struct perf_counter *counter)
3818{
3819 struct hw_perf_counter *hwc = &counter->hw;
3820 int cpu = raw_smp_processor_id();
3821
3822 atomic64_set(&hwc->prev_count, cpu_clock(cpu));
Peter Zijlstra039fc912009-03-13 16:43:47 +01003823 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
3824 hwc->hrtimer.function = perf_swcounter_hrtimer;
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003825 if (hwc->sample_period) {
3826 u64 period = max_t(u64, 10000, hwc->sample_period);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003827 __hrtimer_start_range_ns(&hwc->hrtimer,
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003828 ns_to_ktime(period), 0,
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003829 HRTIMER_MODE_REL, 0);
3830 }
3831
3832 return 0;
3833}
3834
Ingo Molnar5c92d122008-12-11 13:21:10 +01003835static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
3836{
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003837 if (counter->hw.sample_period)
Peter Zijlstrab986d7e2009-05-20 12:21:21 +02003838 hrtimer_cancel(&counter->hw.hrtimer);
Paul Mackerras9abf8a02009-01-09 16:26:43 +11003839 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01003840}
3841
3842static void cpu_clock_perf_counter_read(struct perf_counter *counter)
3843{
Paul Mackerras9abf8a02009-01-09 16:26:43 +11003844 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01003845}
3846
Robert Richter4aeb0b42009-04-29 12:47:03 +02003847static const struct pmu perf_ops_cpu_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +01003848 .enable = cpu_clock_perf_counter_enable,
3849 .disable = cpu_clock_perf_counter_disable,
3850 .read = cpu_clock_perf_counter_read,
Ingo Molnar5c92d122008-12-11 13:21:10 +01003851};
3852
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01003853/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003854 * Software counter: task time clock
3855 */
3856
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02003857static void task_clock_perf_counter_update(struct perf_counter *counter, u64 now)
Ingo Molnarbae43c92008-12-11 14:03:20 +01003858{
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02003859 u64 prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003860 s64 delta;
Ingo Molnarbae43c92008-12-11 14:03:20 +01003861
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02003862 prev = atomic64_xchg(&counter->hw.prev_count, now);
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003863 delta = now - prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003864 atomic64_add(delta, &counter->count);
Ingo Molnarbae43c92008-12-11 14:03:20 +01003865}
3866
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01003867static int task_clock_perf_counter_enable(struct perf_counter *counter)
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003868{
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003869 struct hw_perf_counter *hwc = &counter->hw;
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02003870 u64 now;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003871
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02003872 now = counter->ctx->time;
3873
3874 atomic64_set(&hwc->prev_count, now);
Peter Zijlstra039fc912009-03-13 16:43:47 +01003875 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
3876 hwc->hrtimer.function = perf_swcounter_hrtimer;
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003877 if (hwc->sample_period) {
3878 u64 period = max_t(u64, 10000, hwc->sample_period);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003879 __hrtimer_start_range_ns(&hwc->hrtimer,
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003880 ns_to_ktime(period), 0,
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003881 HRTIMER_MODE_REL, 0);
3882 }
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01003883
3884 return 0;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003885}
3886
3887static void task_clock_perf_counter_disable(struct perf_counter *counter)
3888{
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003889 if (counter->hw.sample_period)
Peter Zijlstrab986d7e2009-05-20 12:21:21 +02003890 hrtimer_cancel(&counter->hw.hrtimer);
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02003891 task_clock_perf_counter_update(counter, counter->ctx->time);
3892
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003893}
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01003894
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003895static void task_clock_perf_counter_read(struct perf_counter *counter)
3896{
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02003897 u64 time;
3898
3899 if (!in_nmi()) {
3900 update_context_time(counter->ctx);
3901 time = counter->ctx->time;
3902 } else {
3903 u64 now = perf_clock();
3904 u64 delta = now - counter->ctx->timestamp;
3905 time = counter->ctx->time + delta;
3906 }
3907
3908 task_clock_perf_counter_update(counter, time);
Ingo Molnarbae43c92008-12-11 14:03:20 +01003909}
3910
Robert Richter4aeb0b42009-04-29 12:47:03 +02003911static const struct pmu perf_ops_task_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +01003912 .enable = task_clock_perf_counter_enable,
3913 .disable = task_clock_perf_counter_disable,
3914 .read = task_clock_perf_counter_read,
Ingo Molnarbae43c92008-12-11 14:03:20 +01003915};
3916
Peter Zijlstrae077df42009-03-19 20:26:17 +01003917#ifdef CONFIG_EVENT_PROFILE
Frederic Weisbeckerf413cdb2009-08-07 01:25:54 +02003918void perf_tpcounter_event(int event_id, u64 addr, u64 count, void *record,
3919 int entry_size)
Peter Zijlstrae077df42009-03-19 20:26:17 +01003920{
Frederic Weisbecker3a43ce62009-08-08 04:26:37 +02003921 struct perf_raw_record raw = {
Frederic Weisbeckerf413cdb2009-08-07 01:25:54 +02003922 .size = entry_size,
Frederic Weisbecker3a43ce62009-08-08 04:26:37 +02003923 .data = record,
Frederic Weisbeckerf413cdb2009-08-07 01:25:54 +02003924 };
3925
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003926 struct perf_sample_data data = {
Chris Wilsond4d7d0b2009-07-06 09:31:33 +01003927 .regs = get_irq_regs(),
Peter Zijlstra3a659302009-07-21 17:34:57 +02003928 .addr = addr,
Frederic Weisbecker3a43ce62009-08-08 04:26:37 +02003929 .raw = &raw,
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003930 };
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003931
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003932 if (!data.regs)
3933 data.regs = task_pt_regs(current);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003934
Peter Zijlstra3a659302009-07-21 17:34:57 +02003935 do_perf_swcounter_event(PERF_TYPE_TRACEPOINT, event_id, count, 1, &data);
Peter Zijlstrae077df42009-03-19 20:26:17 +01003936}
Steven Whitehouseff7b1b42009-04-15 16:55:05 +01003937EXPORT_SYMBOL_GPL(perf_tpcounter_event);
Peter Zijlstrae077df42009-03-19 20:26:17 +01003938
3939extern int ftrace_profile_enable(int);
3940extern void ftrace_profile_disable(int);
3941
3942static void tp_perf_counter_destroy(struct perf_counter *counter)
3943{
Chris Wilsond4d7d0b2009-07-06 09:31:33 +01003944 ftrace_profile_disable(counter->attr.config);
Peter Zijlstrae077df42009-03-19 20:26:17 +01003945}
3946
Robert Richter4aeb0b42009-04-29 12:47:03 +02003947static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
Peter Zijlstrae077df42009-03-19 20:26:17 +01003948{
Peter Zijlstraa4e95fc2009-08-10 11:20:12 +02003949 /*
3950 * Raw tracepoint data is a severe data leak, only allow root to
3951 * have these.
3952 */
3953 if ((counter->attr.sample_type & PERF_SAMPLE_RAW) &&
3954 !capable(CAP_SYS_ADMIN))
3955 return ERR_PTR(-EPERM);
3956
Chris Wilsond4d7d0b2009-07-06 09:31:33 +01003957 if (ftrace_profile_enable(counter->attr.config))
Peter Zijlstrae077df42009-03-19 20:26:17 +01003958 return NULL;
3959
3960 counter->destroy = tp_perf_counter_destroy;
3961
3962 return &perf_ops_generic;
3963}
3964#else
Robert Richter4aeb0b42009-04-29 12:47:03 +02003965static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
Peter Zijlstrae077df42009-03-19 20:26:17 +01003966{
3967 return NULL;
3968}
3969#endif
3970
Peter Zijlstraf29ac752009-06-19 18:27:26 +02003971atomic_t perf_swcounter_enabled[PERF_COUNT_SW_MAX];
3972
3973static void sw_perf_counter_destroy(struct perf_counter *counter)
3974{
3975 u64 event = counter->attr.config;
3976
Peter Zijlstraf3440112009-06-22 13:58:35 +02003977 WARN_ON(counter->parent);
3978
Peter Zijlstraf29ac752009-06-19 18:27:26 +02003979 atomic_dec(&perf_swcounter_enabled[event]);
3980}
3981
Robert Richter4aeb0b42009-04-29 12:47:03 +02003982static const struct pmu *sw_perf_counter_init(struct perf_counter *counter)
Ingo Molnar5c92d122008-12-11 13:21:10 +01003983{
Robert Richter4aeb0b42009-04-29 12:47:03 +02003984 const struct pmu *pmu = NULL;
Peter Zijlstraf29ac752009-06-19 18:27:26 +02003985 u64 event = counter->attr.config;
Ingo Molnar5c92d122008-12-11 13:21:10 +01003986
Paul Mackerras0475f9e2009-02-11 14:35:35 +11003987 /*
3988 * Software counters (currently) can't in general distinguish
3989 * between user, kernel and hypervisor events.
3990 * However, context switches and cpu migrations are considered
3991 * to be kernel events, and page faults are never hypervisor
3992 * events.
3993 */
Peter Zijlstraf29ac752009-06-19 18:27:26 +02003994 switch (event) {
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +02003995 case PERF_COUNT_SW_CPU_CLOCK:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003996 pmu = &perf_ops_cpu_clock;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003997
Ingo Molnar5c92d122008-12-11 13:21:10 +01003998 break;
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +02003999 case PERF_COUNT_SW_TASK_CLOCK:
Paul Mackerras23a185c2009-02-09 22:42:47 +11004000 /*
4001 * If the user instantiates this as a per-cpu counter,
4002 * use the cpu_clock counter instead.
4003 */
4004 if (counter->ctx->task)
Robert Richter4aeb0b42009-04-29 12:47:03 +02004005 pmu = &perf_ops_task_clock;
Paul Mackerras23a185c2009-02-09 22:42:47 +11004006 else
Robert Richter4aeb0b42009-04-29 12:47:03 +02004007 pmu = &perf_ops_cpu_clock;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01004008
Ingo Molnarbae43c92008-12-11 14:03:20 +01004009 break;
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +02004010 case PERF_COUNT_SW_PAGE_FAULTS:
4011 case PERF_COUNT_SW_PAGE_FAULTS_MIN:
4012 case PERF_COUNT_SW_PAGE_FAULTS_MAJ:
4013 case PERF_COUNT_SW_CONTEXT_SWITCHES:
4014 case PERF_COUNT_SW_CPU_MIGRATIONS:
Peter Zijlstraf3440112009-06-22 13:58:35 +02004015 if (!counter->parent) {
4016 atomic_inc(&perf_swcounter_enabled[event]);
4017 counter->destroy = sw_perf_counter_destroy;
4018 }
Paul Mackerras3f731ca2009-06-01 17:52:30 +10004019 pmu = &perf_ops_generic;
Ingo Molnar6c594c22008-12-14 12:34:15 +01004020 break;
Ingo Molnar5c92d122008-12-11 13:21:10 +01004021 }
Peter Zijlstra15dbf272009-03-13 12:21:32 +01004022
Robert Richter4aeb0b42009-04-29 12:47:03 +02004023 return pmu;
Ingo Molnar5c92d122008-12-11 13:21:10 +01004024}
4025
Thomas Gleixner0793a612008-12-04 20:12:29 +01004026/*
4027 * Allocate and initialize a counter structure
4028 */
4029static struct perf_counter *
Peter Zijlstra0d486962009-06-02 19:22:16 +02004030perf_counter_alloc(struct perf_counter_attr *attr,
Ingo Molnar04289bb2008-12-11 08:38:42 +01004031 int cpu,
Paul Mackerras23a185c2009-02-09 22:42:47 +11004032 struct perf_counter_context *ctx,
Ingo Molnar9b51f662008-12-12 13:49:45 +01004033 struct perf_counter *group_leader,
Peter Zijlstrab84fbc92009-06-22 13:57:40 +02004034 struct perf_counter *parent_counter,
Ingo Molnar9b51f662008-12-12 13:49:45 +01004035 gfp_t gfpflags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01004036{
Robert Richter4aeb0b42009-04-29 12:47:03 +02004037 const struct pmu *pmu;
Ingo Molnar621a01e2008-12-11 12:46:46 +01004038 struct perf_counter *counter;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02004039 struct hw_perf_counter *hwc;
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02004040 long err;
Thomas Gleixner0793a612008-12-04 20:12:29 +01004041
Ingo Molnar9b51f662008-12-12 13:49:45 +01004042 counter = kzalloc(sizeof(*counter), gfpflags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004043 if (!counter)
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02004044 return ERR_PTR(-ENOMEM);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004045
Ingo Molnar04289bb2008-12-11 08:38:42 +01004046 /*
4047 * Single counters are their own group leaders, with an
4048 * empty sibling list:
4049 */
4050 if (!group_leader)
4051 group_leader = counter;
4052
Peter Zijlstrafccc7142009-05-23 18:28:56 +02004053 mutex_init(&counter->child_mutex);
4054 INIT_LIST_HEAD(&counter->child_list);
4055
Ingo Molnar04289bb2008-12-11 08:38:42 +01004056 INIT_LIST_HEAD(&counter->list_entry);
Peter Zijlstra592903c2009-03-13 12:21:36 +01004057 INIT_LIST_HEAD(&counter->event_entry);
Ingo Molnar04289bb2008-12-11 08:38:42 +01004058 INIT_LIST_HEAD(&counter->sibling_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004059 init_waitqueue_head(&counter->waitq);
4060
Peter Zijlstra7b732a72009-03-23 18:22:10 +01004061 mutex_init(&counter->mmap_mutex);
4062
Peter Zijlstraa96bbc12009-06-03 14:01:36 +02004063 counter->cpu = cpu;
Peter Zijlstra0d486962009-06-02 19:22:16 +02004064 counter->attr = *attr;
Peter Zijlstraa96bbc12009-06-03 14:01:36 +02004065 counter->group_leader = group_leader;
4066 counter->pmu = NULL;
4067 counter->ctx = ctx;
4068 counter->oncpu = -1;
Ingo Molnar329d8762009-05-26 08:10:00 +02004069
Peter Zijlstrab84fbc92009-06-22 13:57:40 +02004070 counter->parent = parent_counter;
4071
Peter Zijlstraa96bbc12009-06-03 14:01:36 +02004072 counter->ns = get_pid_ns(current->nsproxy->pid_ns);
4073 counter->id = atomic64_inc_return(&perf_counter_id);
4074
4075 counter->state = PERF_COUNTER_STATE_INACTIVE;
4076
Peter Zijlstra0d486962009-06-02 19:22:16 +02004077 if (attr->disabled)
Ingo Molnara86ed502008-12-17 00:43:10 +01004078 counter->state = PERF_COUNTER_STATE_OFF;
4079
Robert Richter4aeb0b42009-04-29 12:47:03 +02004080 pmu = NULL;
Peter Zijlstrab8e83512009-03-19 20:26:18 +01004081
Peter Zijlstra60db5e02009-05-15 15:19:28 +02004082 hwc = &counter->hw;
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02004083 hwc->sample_period = attr->sample_period;
Peter Zijlstra0d486962009-06-02 19:22:16 +02004084 if (attr->freq && attr->sample_freq)
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02004085 hwc->sample_period = 1;
4086
4087 atomic64_set(&hwc->period_left, hwc->sample_period);
Peter Zijlstra60db5e02009-05-15 15:19:28 +02004088
Peter Zijlstra2023b352009-05-05 17:50:26 +02004089 /*
Peter Zijlstra3dab77f2009-08-13 11:47:53 +02004090 * we currently do not support PERF_FORMAT_GROUP on inherited counters
Peter Zijlstra2023b352009-05-05 17:50:26 +02004091 */
Peter Zijlstra3dab77f2009-08-13 11:47:53 +02004092 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
Peter Zijlstra2023b352009-05-05 17:50:26 +02004093 goto done;
4094
Ingo Molnara21ca2c2009-06-06 09:58:57 +02004095 switch (attr->type) {
Peter Zijlstra081fad82009-06-11 17:57:21 +02004096 case PERF_TYPE_RAW:
Peter Zijlstrab8e83512009-03-19 20:26:18 +01004097 case PERF_TYPE_HARDWARE:
Ingo Molnar8326f442009-06-05 20:22:46 +02004098 case PERF_TYPE_HW_CACHE:
Robert Richter4aeb0b42009-04-29 12:47:03 +02004099 pmu = hw_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01004100 break;
4101
4102 case PERF_TYPE_SOFTWARE:
Robert Richter4aeb0b42009-04-29 12:47:03 +02004103 pmu = sw_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01004104 break;
4105
4106 case PERF_TYPE_TRACEPOINT:
Robert Richter4aeb0b42009-04-29 12:47:03 +02004107 pmu = tp_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01004108 break;
Peter Zijlstra974802e2009-06-12 12:46:55 +02004109
4110 default:
4111 break;
Peter Zijlstrab8e83512009-03-19 20:26:18 +01004112 }
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01004113done:
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02004114 err = 0;
Robert Richter4aeb0b42009-04-29 12:47:03 +02004115 if (!pmu)
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02004116 err = -EINVAL;
Robert Richter4aeb0b42009-04-29 12:47:03 +02004117 else if (IS_ERR(pmu))
4118 err = PTR_ERR(pmu);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02004119
4120 if (err) {
Peter Zijlstraa96bbc12009-06-03 14:01:36 +02004121 if (counter->ns)
4122 put_pid_ns(counter->ns);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02004123 kfree(counter);
4124 return ERR_PTR(err);
4125 }
4126
Robert Richter4aeb0b42009-04-29 12:47:03 +02004127 counter->pmu = pmu;
Thomas Gleixner0793a612008-12-04 20:12:29 +01004128
Peter Zijlstraf3440112009-06-22 13:58:35 +02004129 if (!counter->parent) {
4130 atomic_inc(&nr_counters);
4131 if (counter->attr.mmap)
4132 atomic_inc(&nr_mmap_counters);
4133 if (counter->attr.comm)
4134 atomic_inc(&nr_comm_counters);
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02004135 if (counter->attr.task)
4136 atomic_inc(&nr_task_counters);
Peter Zijlstraf3440112009-06-22 13:58:35 +02004137 }
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02004138
Thomas Gleixner0793a612008-12-04 20:12:29 +01004139 return counter;
4140}
4141
Peter Zijlstra974802e2009-06-12 12:46:55 +02004142static int perf_copy_attr(struct perf_counter_attr __user *uattr,
4143 struct perf_counter_attr *attr)
4144{
4145 int ret;
4146 u32 size;
4147
4148 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
4149 return -EFAULT;
4150
4151 /*
4152 * zero the full structure, so that a short copy will be nice.
4153 */
4154 memset(attr, 0, sizeof(*attr));
4155
4156 ret = get_user(size, &uattr->size);
4157 if (ret)
4158 return ret;
4159
4160 if (size > PAGE_SIZE) /* silly large */
4161 goto err_size;
4162
4163 if (!size) /* abi compat */
4164 size = PERF_ATTR_SIZE_VER0;
4165
4166 if (size < PERF_ATTR_SIZE_VER0)
4167 goto err_size;
4168
4169 /*
4170 * If we're handed a bigger struct than we know of,
4171 * ensure all the unknown bits are 0.
4172 */
4173 if (size > sizeof(*attr)) {
4174 unsigned long val;
4175 unsigned long __user *addr;
4176 unsigned long __user *end;
4177
4178 addr = PTR_ALIGN((void __user *)uattr + sizeof(*attr),
4179 sizeof(unsigned long));
4180 end = PTR_ALIGN((void __user *)uattr + size,
4181 sizeof(unsigned long));
4182
4183 for (; addr < end; addr += sizeof(unsigned long)) {
4184 ret = get_user(val, addr);
4185 if (ret)
4186 return ret;
4187 if (val)
4188 goto err_size;
4189 }
4190 }
4191
4192 ret = copy_from_user(attr, uattr, size);
4193 if (ret)
4194 return -EFAULT;
4195
4196 /*
4197 * If the type exists, the corresponding creation will verify
4198 * the attr->config.
4199 */
4200 if (attr->type >= PERF_TYPE_MAX)
4201 return -EINVAL;
4202
4203 if (attr->__reserved_1 || attr->__reserved_2 || attr->__reserved_3)
4204 return -EINVAL;
4205
4206 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
4207 return -EINVAL;
4208
4209 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
4210 return -EINVAL;
4211
4212out:
4213 return ret;
4214
4215err_size:
4216 put_user(sizeof(*attr), &uattr->size);
4217 ret = -E2BIG;
4218 goto out;
4219}
4220
Thomas Gleixner0793a612008-12-04 20:12:29 +01004221/**
Paul Mackerras2743a5b2009-03-04 20:36:51 +11004222 * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
Ingo Molnar9f66a382008-12-10 12:33:23 +01004223 *
Peter Zijlstra0d486962009-06-02 19:22:16 +02004224 * @attr_uptr: event type attributes for monitoring/sampling
Thomas Gleixner0793a612008-12-04 20:12:29 +01004225 * @pid: target pid
Ingo Molnar9f66a382008-12-10 12:33:23 +01004226 * @cpu: target cpu
4227 * @group_fd: group leader counter fd
Thomas Gleixner0793a612008-12-04 20:12:29 +01004228 */
Paul Mackerras2743a5b2009-03-04 20:36:51 +11004229SYSCALL_DEFINE5(perf_counter_open,
Peter Zijlstra974802e2009-06-12 12:46:55 +02004230 struct perf_counter_attr __user *, attr_uptr,
Paul Mackerras2743a5b2009-03-04 20:36:51 +11004231 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01004232{
Ingo Molnar04289bb2008-12-11 08:38:42 +01004233 struct perf_counter *counter, *group_leader;
Peter Zijlstra0d486962009-06-02 19:22:16 +02004234 struct perf_counter_attr attr;
Ingo Molnar04289bb2008-12-11 08:38:42 +01004235 struct perf_counter_context *ctx;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004236 struct file *counter_file = NULL;
Ingo Molnar04289bb2008-12-11 08:38:42 +01004237 struct file *group_file = NULL;
4238 int fput_needed = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004239 int fput_needed2 = 0;
Thomas Gleixner0793a612008-12-04 20:12:29 +01004240 int ret;
4241
Paul Mackerras2743a5b2009-03-04 20:36:51 +11004242 /* for future expandability... */
4243 if (flags)
4244 return -EINVAL;
4245
Peter Zijlstra974802e2009-06-12 12:46:55 +02004246 ret = perf_copy_attr(attr_uptr, &attr);
4247 if (ret)
4248 return ret;
Thomas Gleixnereab656a2008-12-08 19:26:59 +01004249
Peter Zijlstra07647712009-06-11 11:18:36 +02004250 if (!attr.exclude_kernel) {
4251 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
4252 return -EACCES;
4253 }
4254
Peter Zijlstradf58ab22009-06-11 11:25:05 +02004255 if (attr.freq) {
4256 if (attr.sample_freq > sysctl_perf_counter_sample_rate)
4257 return -EINVAL;
4258 }
4259
Ingo Molnar04289bb2008-12-11 08:38:42 +01004260 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01004261 * Get the target context (task or percpu):
4262 */
4263 ctx = find_get_context(pid, cpu);
4264 if (IS_ERR(ctx))
4265 return PTR_ERR(ctx);
4266
4267 /*
4268 * Look up the group leader (we will attach this counter to it):
Ingo Molnar04289bb2008-12-11 08:38:42 +01004269 */
4270 group_leader = NULL;
4271 if (group_fd != -1) {
4272 ret = -EINVAL;
4273 group_file = fget_light(group_fd, &fput_needed);
4274 if (!group_file)
Ingo Molnarccff2862008-12-11 11:26:29 +01004275 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01004276 if (group_file->f_op != &perf_fops)
Ingo Molnarccff2862008-12-11 11:26:29 +01004277 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01004278
4279 group_leader = group_file->private_data;
4280 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01004281 * Do not allow a recursive hierarchy (this new sibling
4282 * becoming part of another group-sibling):
Ingo Molnar04289bb2008-12-11 08:38:42 +01004283 */
Ingo Molnarccff2862008-12-11 11:26:29 +01004284 if (group_leader->group_leader != group_leader)
4285 goto err_put_context;
4286 /*
4287 * Do not allow to attach to a group in a different
4288 * task or CPU context:
4289 */
4290 if (group_leader->ctx != ctx)
4291 goto err_put_context;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11004292 /*
4293 * Only a group leader can be exclusive or pinned
4294 */
Peter Zijlstra0d486962009-06-02 19:22:16 +02004295 if (attr.exclusive || attr.pinned)
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11004296 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01004297 }
4298
Peter Zijlstra0d486962009-06-02 19:22:16 +02004299 counter = perf_counter_alloc(&attr, cpu, ctx, group_leader,
Peter Zijlstrab84fbc92009-06-22 13:57:40 +02004300 NULL, GFP_KERNEL);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02004301 ret = PTR_ERR(counter);
4302 if (IS_ERR(counter))
Thomas Gleixner0793a612008-12-04 20:12:29 +01004303 goto err_put_context;
4304
Thomas Gleixner0793a612008-12-04 20:12:29 +01004305 ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
4306 if (ret < 0)
Ingo Molnar9b51f662008-12-12 13:49:45 +01004307 goto err_free_put_context;
4308
4309 counter_file = fget_light(ret, &fput_needed2);
4310 if (!counter_file)
4311 goto err_free_put_context;
4312
4313 counter->filp = counter_file;
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004314 WARN_ON_ONCE(ctx->parent_ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +11004315 mutex_lock(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01004316 perf_install_in_context(ctx, counter, cpu);
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004317 ++ctx->generation;
Paul Mackerrasd859e292009-01-17 18:10:22 +11004318 mutex_unlock(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01004319
Peter Zijlstra082ff5a2009-05-23 18:29:00 +02004320 counter->owner = current;
4321 get_task_struct(current);
4322 mutex_lock(&current->perf_counter_mutex);
4323 list_add_tail(&counter->owner_entry, &current->perf_counter_list);
4324 mutex_unlock(&current->perf_counter_mutex);
4325
Ingo Molnar9b51f662008-12-12 13:49:45 +01004326 fput_light(counter_file, fput_needed2);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004327
Ingo Molnar04289bb2008-12-11 08:38:42 +01004328out_fput:
4329 fput_light(group_file, fput_needed);
4330
Thomas Gleixner0793a612008-12-04 20:12:29 +01004331 return ret;
4332
Ingo Molnar9b51f662008-12-12 13:49:45 +01004333err_free_put_context:
Thomas Gleixner0793a612008-12-04 20:12:29 +01004334 kfree(counter);
4335
4336err_put_context:
Paul Mackerrasc93f7662009-05-28 22:18:17 +10004337 put_ctx(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004338
Ingo Molnar04289bb2008-12-11 08:38:42 +01004339 goto out_fput;
Thomas Gleixner0793a612008-12-04 20:12:29 +01004340}
4341
Ingo Molnar9b51f662008-12-12 13:49:45 +01004342/*
Ingo Molnar9b51f662008-12-12 13:49:45 +01004343 * inherit a counter from parent task to child task:
4344 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11004345static struct perf_counter *
Ingo Molnar9b51f662008-12-12 13:49:45 +01004346inherit_counter(struct perf_counter *parent_counter,
4347 struct task_struct *parent,
4348 struct perf_counter_context *parent_ctx,
4349 struct task_struct *child,
Paul Mackerrasd859e292009-01-17 18:10:22 +11004350 struct perf_counter *group_leader,
Ingo Molnar9b51f662008-12-12 13:49:45 +01004351 struct perf_counter_context *child_ctx)
4352{
4353 struct perf_counter *child_counter;
4354
Paul Mackerrasd859e292009-01-17 18:10:22 +11004355 /*
4356 * Instead of creating recursive hierarchies of counters,
4357 * we link inherited counters back to the original parent,
4358 * which has a filp for sure, which we use as the reference
4359 * count:
4360 */
4361 if (parent_counter->parent)
4362 parent_counter = parent_counter->parent;
4363
Peter Zijlstra0d486962009-06-02 19:22:16 +02004364 child_counter = perf_counter_alloc(&parent_counter->attr,
Paul Mackerras23a185c2009-02-09 22:42:47 +11004365 parent_counter->cpu, child_ctx,
Peter Zijlstrab84fbc92009-06-22 13:57:40 +02004366 group_leader, parent_counter,
4367 GFP_KERNEL);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02004368 if (IS_ERR(child_counter))
4369 return child_counter;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10004370 get_ctx(child_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01004371
4372 /*
Paul Mackerras564c2b22009-05-22 14:27:22 +10004373 * Make the child state follow the state of the parent counter,
Peter Zijlstra0d486962009-06-02 19:22:16 +02004374 * not its attr.disabled bit. We hold the parent's mutex,
Ingo Molnar22a4f652009-06-01 10:13:37 +02004375 * so we won't race with perf_counter_{en, dis}able_family.
Paul Mackerras564c2b22009-05-22 14:27:22 +10004376 */
4377 if (parent_counter->state >= PERF_COUNTER_STATE_INACTIVE)
4378 child_counter->state = PERF_COUNTER_STATE_INACTIVE;
4379 else
4380 child_counter->state = PERF_COUNTER_STATE_OFF;
4381
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02004382 if (parent_counter->attr.freq)
4383 child_counter->hw.sample_period = parent_counter->hw.sample_period;
4384
Paul Mackerras564c2b22009-05-22 14:27:22 +10004385 /*
Ingo Molnar9b51f662008-12-12 13:49:45 +01004386 * Link it up in the child's context:
4387 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +11004388 add_counter_to_ctx(child_counter, child_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01004389
Ingo Molnar9b51f662008-12-12 13:49:45 +01004390 /*
4391 * Get a reference to the parent filp - we will fput it
4392 * when the child counter exits. This is safe to do because
4393 * we are in the parent and we know that the filp still
4394 * exists and has a nonzero count:
4395 */
4396 atomic_long_inc(&parent_counter->filp->f_count);
4397
Paul Mackerrasd859e292009-01-17 18:10:22 +11004398 /*
4399 * Link this into the parent counter's child list
4400 */
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004401 WARN_ON_ONCE(parent_counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02004402 mutex_lock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11004403 list_add_tail(&child_counter->child_list, &parent_counter->child_list);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02004404 mutex_unlock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11004405
4406 return child_counter;
4407}
4408
4409static int inherit_group(struct perf_counter *parent_counter,
4410 struct task_struct *parent,
4411 struct perf_counter_context *parent_ctx,
4412 struct task_struct *child,
4413 struct perf_counter_context *child_ctx)
4414{
4415 struct perf_counter *leader;
4416 struct perf_counter *sub;
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02004417 struct perf_counter *child_ctr;
Paul Mackerrasd859e292009-01-17 18:10:22 +11004418
4419 leader = inherit_counter(parent_counter, parent, parent_ctx,
4420 child, NULL, child_ctx);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02004421 if (IS_ERR(leader))
4422 return PTR_ERR(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +11004423 list_for_each_entry(sub, &parent_counter->sibling_list, list_entry) {
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02004424 child_ctr = inherit_counter(sub, parent, parent_ctx,
4425 child, leader, child_ctx);
4426 if (IS_ERR(child_ctr))
4427 return PTR_ERR(child_ctr);
Paul Mackerrasd859e292009-01-17 18:10:22 +11004428 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01004429 return 0;
4430}
4431
Paul Mackerrasd859e292009-01-17 18:10:22 +11004432static void sync_child_counter(struct perf_counter *child_counter,
Peter Zijlstra38b200d2009-06-23 20:13:11 +02004433 struct task_struct *child)
Paul Mackerrasd859e292009-01-17 18:10:22 +11004434{
Peter Zijlstra38b200d2009-06-23 20:13:11 +02004435 struct perf_counter *parent_counter = child_counter->parent;
Peter Zijlstra8bc20952009-05-15 20:45:59 +02004436 u64 child_val;
Paul Mackerrasd859e292009-01-17 18:10:22 +11004437
Peter Zijlstrabfbd3382009-06-24 21:11:59 +02004438 if (child_counter->attr.inherit_stat)
4439 perf_counter_read_event(child_counter, child);
Peter Zijlstra38b200d2009-06-23 20:13:11 +02004440
Paul Mackerrasd859e292009-01-17 18:10:22 +11004441 child_val = atomic64_read(&child_counter->count);
4442
4443 /*
4444 * Add back the child's count to the parent's count:
4445 */
4446 atomic64_add(child_val, &parent_counter->count);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11004447 atomic64_add(child_counter->total_time_enabled,
4448 &parent_counter->child_total_time_enabled);
4449 atomic64_add(child_counter->total_time_running,
4450 &parent_counter->child_total_time_running);
Paul Mackerrasd859e292009-01-17 18:10:22 +11004451
4452 /*
4453 * Remove this counter from the parent's list
4454 */
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004455 WARN_ON_ONCE(parent_counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02004456 mutex_lock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11004457 list_del_init(&child_counter->child_list);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02004458 mutex_unlock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11004459
4460 /*
4461 * Release the parent counter, if this was the last
4462 * reference to it.
4463 */
4464 fput(parent_counter->filp);
4465}
4466
Ingo Molnar9b51f662008-12-12 13:49:45 +01004467static void
Peter Zijlstrabbbee902009-05-29 14:25:58 +02004468__perf_counter_exit_task(struct perf_counter *child_counter,
Peter Zijlstra38b200d2009-06-23 20:13:11 +02004469 struct perf_counter_context *child_ctx,
4470 struct task_struct *child)
Ingo Molnar9b51f662008-12-12 13:49:45 +01004471{
4472 struct perf_counter *parent_counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004473
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004474 update_counter_times(child_counter);
Peter Zijlstraaa9c67f2009-05-23 18:28:59 +02004475 perf_counter_remove_from_context(child_counter);
Ingo Molnar0cc0c022008-12-14 23:20:36 +01004476
Ingo Molnar9b51f662008-12-12 13:49:45 +01004477 parent_counter = child_counter->parent;
4478 /*
4479 * It can happen that parent exits first, and has counters
4480 * that are still around due to the child reference. These
4481 * counters need to be zapped - but otherwise linger.
4482 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11004483 if (parent_counter) {
Peter Zijlstra38b200d2009-06-23 20:13:11 +02004484 sync_child_counter(child_counter, child);
Peter Zijlstraf1600952009-03-19 20:26:16 +01004485 free_counter(child_counter);
Paul Mackerras4bcf3492009-02-11 13:53:19 +01004486 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01004487}
4488
4489/*
Paul Mackerrasd859e292009-01-17 18:10:22 +11004490 * When a child task exits, feed back counter values to parent counters.
Ingo Molnar9b51f662008-12-12 13:49:45 +01004491 */
4492void perf_counter_exit_task(struct task_struct *child)
4493{
4494 struct perf_counter *child_counter, *tmp;
4495 struct perf_counter_context *child_ctx;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004496 unsigned long flags;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004497
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02004498 if (likely(!child->perf_counter_ctxp)) {
Peter Zijlstra3a80b4a2009-08-07 19:49:01 +02004499 perf_counter_task(child, NULL, 0);
Ingo Molnar9b51f662008-12-12 13:49:45 +01004500 return;
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02004501 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01004502
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004503 local_irq_save(flags);
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004504 /*
4505 * We can't reschedule here because interrupts are disabled,
4506 * and either child is current or it is a task that can't be
4507 * scheduled, so we are now safe from rescheduling changing
4508 * our context.
4509 */
4510 child_ctx = child->perf_counter_ctxp;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004511 __perf_counter_task_sched_out(child_ctx);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10004512
4513 /*
4514 * Take the context lock here so that if find_get_context is
4515 * reading child->perf_counter_ctxp, we wait until it has
4516 * incremented the context's refcount before we do put_ctx below.
4517 */
4518 spin_lock(&child_ctx->lock);
Peter Zijlstra3a80b4a2009-08-07 19:49:01 +02004519 child->perf_counter_ctxp = NULL;
Peter Zijlstra71a851b2009-07-10 09:06:56 +02004520 /*
4521 * If this context is a clone; unclone it so it can't get
4522 * swapped to another process while we're removing all
4523 * the counters from it.
4524 */
4525 unclone_ctx(child_ctx);
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02004526 spin_unlock_irqrestore(&child_ctx->lock, flags);
4527
4528 /*
4529 * Report the task dead after unscheduling the counters so that we
4530 * won't get any samples after PERF_EVENT_EXIT. We can however still
4531 * get a few PERF_EVENT_READ events.
4532 */
Peter Zijlstra3a80b4a2009-08-07 19:49:01 +02004533 perf_counter_task(child, child_ctx, 0);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004534
Peter Zijlstra66fff222009-06-10 22:53:37 +02004535 /*
4536 * We can recurse on the same lock type through:
4537 *
4538 * __perf_counter_exit_task()
4539 * sync_child_counter()
4540 * fput(parent_counter->filp)
4541 * perf_release()
4542 * mutex_lock(&ctx->mutex)
4543 *
4544 * But since its the parent context it won't be the same instance.
4545 */
4546 mutex_lock_nested(&child_ctx->mutex, SINGLE_DEPTH_NESTING);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004547
Peter Zijlstra8bc20952009-05-15 20:45:59 +02004548again:
Ingo Molnar9b51f662008-12-12 13:49:45 +01004549 list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list,
4550 list_entry)
Peter Zijlstra38b200d2009-06-23 20:13:11 +02004551 __perf_counter_exit_task(child_counter, child_ctx, child);
Peter Zijlstra8bc20952009-05-15 20:45:59 +02004552
4553 /*
4554 * If the last counter was a group counter, it will have appended all
4555 * its siblings to the list, but we obtained 'tmp' before that which
4556 * will still point to the list head terminating the iteration.
4557 */
4558 if (!list_empty(&child_ctx->counter_list))
4559 goto again;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004560
4561 mutex_unlock(&child_ctx->mutex);
4562
4563 put_ctx(child_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01004564}
4565
4566/*
Peter Zijlstrabbbee902009-05-29 14:25:58 +02004567 * free an unexposed, unused context as created by inheritance by
4568 * init_task below, used by fork() in case of fail.
4569 */
4570void perf_counter_free_task(struct task_struct *task)
4571{
4572 struct perf_counter_context *ctx = task->perf_counter_ctxp;
4573 struct perf_counter *counter, *tmp;
4574
4575 if (!ctx)
4576 return;
4577
4578 mutex_lock(&ctx->mutex);
4579again:
4580 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry) {
4581 struct perf_counter *parent = counter->parent;
4582
4583 if (WARN_ON_ONCE(!parent))
4584 continue;
4585
4586 mutex_lock(&parent->child_mutex);
4587 list_del_init(&counter->child_list);
4588 mutex_unlock(&parent->child_mutex);
4589
4590 fput(parent->filp);
4591
4592 list_del_counter(counter, ctx);
4593 free_counter(counter);
4594 }
4595
4596 if (!list_empty(&ctx->counter_list))
4597 goto again;
4598
4599 mutex_unlock(&ctx->mutex);
4600
4601 put_ctx(ctx);
4602}
4603
4604/*
Ingo Molnar9b51f662008-12-12 13:49:45 +01004605 * Initialize the perf_counter context in task_struct
4606 */
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02004607int perf_counter_init_task(struct task_struct *child)
Ingo Molnar9b51f662008-12-12 13:49:45 +01004608{
4609 struct perf_counter_context *child_ctx, *parent_ctx;
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004610 struct perf_counter_context *cloned_ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +11004611 struct perf_counter *counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004612 struct task_struct *parent = current;
Paul Mackerras564c2b22009-05-22 14:27:22 +10004613 int inherited_all = 1;
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02004614 int ret = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004615
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004616 child->perf_counter_ctxp = NULL;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004617
Peter Zijlstra082ff5a2009-05-23 18:29:00 +02004618 mutex_init(&child->perf_counter_mutex);
4619 INIT_LIST_HEAD(&child->perf_counter_list);
4620
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004621 if (likely(!parent->perf_counter_ctxp))
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02004622 return 0;
4623
Ingo Molnar9b51f662008-12-12 13:49:45 +01004624 /*
4625 * This is executed from the parent task context, so inherit
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004626 * counters that have been marked for cloning.
4627 * First allocate and initialize a context for the child.
Ingo Molnar9b51f662008-12-12 13:49:45 +01004628 */
4629
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004630 child_ctx = kmalloc(sizeof(struct perf_counter_context), GFP_KERNEL);
4631 if (!child_ctx)
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02004632 return -ENOMEM;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004633
4634 __perf_counter_init_context(child_ctx, child);
4635 child->perf_counter_ctxp = child_ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10004636 get_task_struct(child);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004637
Ingo Molnar9b51f662008-12-12 13:49:45 +01004638 /*
Paul Mackerras25346b92009-06-01 17:48:12 +10004639 * If the parent's context is a clone, pin it so it won't get
4640 * swapped under us.
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004641 */
Paul Mackerras25346b92009-06-01 17:48:12 +10004642 parent_ctx = perf_pin_task_context(parent);
4643
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004644 /*
4645 * No need to check if parent_ctx != NULL here; since we saw
4646 * it non-NULL earlier, the only reason for it to become NULL
4647 * is if we exit, and since we're currently in the middle of
4648 * a fork we can't be exiting at the same time.
4649 */
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004650
4651 /*
Ingo Molnar9b51f662008-12-12 13:49:45 +01004652 * Lock the parent list. No need to lock the child - not PID
4653 * hashed yet and not running, so nobody can access it.
4654 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11004655 mutex_lock(&parent_ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01004656
4657 /*
4658 * We dont have to disable NMIs - we are only looking at
4659 * the list, not manipulating it:
4660 */
Peter Zijlstrad7b629a2009-05-20 12:21:19 +02004661 list_for_each_entry_rcu(counter, &parent_ctx->event_list, event_entry) {
4662 if (counter != counter->group_leader)
4663 continue;
4664
Peter Zijlstra0d486962009-06-02 19:22:16 +02004665 if (!counter->attr.inherit) {
Paul Mackerras564c2b22009-05-22 14:27:22 +10004666 inherited_all = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004667 continue;
Paul Mackerras564c2b22009-05-22 14:27:22 +10004668 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01004669
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02004670 ret = inherit_group(counter, parent, parent_ctx,
4671 child, child_ctx);
4672 if (ret) {
Paul Mackerras564c2b22009-05-22 14:27:22 +10004673 inherited_all = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004674 break;
Paul Mackerras564c2b22009-05-22 14:27:22 +10004675 }
4676 }
4677
4678 if (inherited_all) {
4679 /*
4680 * Mark the child context as a clone of the parent
4681 * context, or of whatever the parent is a clone of.
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004682 * Note that if the parent is a clone, it could get
4683 * uncloned at any point, but that doesn't matter
4684 * because the list of counters and the generation
4685 * count can't have changed since we took the mutex.
Paul Mackerras564c2b22009-05-22 14:27:22 +10004686 */
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004687 cloned_ctx = rcu_dereference(parent_ctx->parent_ctx);
4688 if (cloned_ctx) {
4689 child_ctx->parent_ctx = cloned_ctx;
Paul Mackerras25346b92009-06-01 17:48:12 +10004690 child_ctx->parent_gen = parent_ctx->parent_gen;
Paul Mackerras564c2b22009-05-22 14:27:22 +10004691 } else {
4692 child_ctx->parent_ctx = parent_ctx;
4693 child_ctx->parent_gen = parent_ctx->generation;
4694 }
4695 get_ctx(child_ctx->parent_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01004696 }
4697
Paul Mackerrasd859e292009-01-17 18:10:22 +11004698 mutex_unlock(&parent_ctx->mutex);
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02004699
Paul Mackerras25346b92009-06-01 17:48:12 +10004700 perf_unpin_context(parent_ctx);
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004701
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02004702 return ret;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004703}
4704
Ingo Molnar04289bb2008-12-11 08:38:42 +01004705static void __cpuinit perf_counter_init_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01004706{
Ingo Molnar04289bb2008-12-11 08:38:42 +01004707 struct perf_cpu_context *cpuctx;
Thomas Gleixner0793a612008-12-04 20:12:29 +01004708
Ingo Molnar04289bb2008-12-11 08:38:42 +01004709 cpuctx = &per_cpu(perf_cpu_context, cpu);
4710 __perf_counter_init_context(&cpuctx->ctx, NULL);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004711
Ingo Molnar1dce8d92009-05-04 19:23:18 +02004712 spin_lock(&perf_resource_lock);
Ingo Molnar04289bb2008-12-11 08:38:42 +01004713 cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu;
Ingo Molnar1dce8d92009-05-04 19:23:18 +02004714 spin_unlock(&perf_resource_lock);
Ingo Molnar04289bb2008-12-11 08:38:42 +01004715
Paul Mackerras01d02872009-01-14 13:44:19 +11004716 hw_perf_counter_setup(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004717}
4718
4719#ifdef CONFIG_HOTPLUG_CPU
Ingo Molnar04289bb2008-12-11 08:38:42 +01004720static void __perf_counter_exit_cpu(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01004721{
4722 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
4723 struct perf_counter_context *ctx = &cpuctx->ctx;
4724 struct perf_counter *counter, *tmp;
4725
Ingo Molnar04289bb2008-12-11 08:38:42 +01004726 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry)
4727 __perf_counter_remove_from_context(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004728}
Ingo Molnar04289bb2008-12-11 08:38:42 +01004729static void perf_counter_exit_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01004730{
Paul Mackerrasd859e292009-01-17 18:10:22 +11004731 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
4732 struct perf_counter_context *ctx = &cpuctx->ctx;
4733
4734 mutex_lock(&ctx->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01004735 smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1);
Paul Mackerrasd859e292009-01-17 18:10:22 +11004736 mutex_unlock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004737}
4738#else
Ingo Molnar04289bb2008-12-11 08:38:42 +01004739static inline void perf_counter_exit_cpu(int cpu) { }
Thomas Gleixner0793a612008-12-04 20:12:29 +01004740#endif
4741
4742static int __cpuinit
4743perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
4744{
4745 unsigned int cpu = (long)hcpu;
4746
4747 switch (action) {
4748
4749 case CPU_UP_PREPARE:
4750 case CPU_UP_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01004751 perf_counter_init_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004752 break;
4753
Ingo Molnar28402972009-08-13 10:13:22 +02004754 case CPU_ONLINE:
4755 case CPU_ONLINE_FROZEN:
4756 hw_perf_counter_setup_online(cpu);
4757 break;
4758
Thomas Gleixner0793a612008-12-04 20:12:29 +01004759 case CPU_DOWN_PREPARE:
4760 case CPU_DOWN_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01004761 perf_counter_exit_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004762 break;
4763
4764 default:
4765 break;
4766 }
4767
4768 return NOTIFY_OK;
4769}
4770
Paul Mackerrasf38b0822009-06-02 21:05:16 +10004771/*
4772 * This has to have a higher priority than migration_notifier in sched.c.
4773 */
Thomas Gleixner0793a612008-12-04 20:12:29 +01004774static struct notifier_block __cpuinitdata perf_cpu_nb = {
4775 .notifier_call = perf_cpu_notify,
Paul Mackerrasf38b0822009-06-02 21:05:16 +10004776 .priority = 20,
Thomas Gleixner0793a612008-12-04 20:12:29 +01004777};
4778
Ingo Molnar0d905bc2009-05-04 19:13:30 +02004779void __init perf_counter_init(void)
Thomas Gleixner0793a612008-12-04 20:12:29 +01004780{
4781 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
4782 (void *)(long)smp_processor_id());
Ingo Molnar28402972009-08-13 10:13:22 +02004783 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_ONLINE,
4784 (void *)(long)smp_processor_id());
Thomas Gleixner0793a612008-12-04 20:12:29 +01004785 register_cpu_notifier(&perf_cpu_nb);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004786}
Thomas Gleixner0793a612008-12-04 20:12:29 +01004787
4788static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
4789{
4790 return sprintf(buf, "%d\n", perf_reserved_percpu);
4791}
4792
4793static ssize_t
4794perf_set_reserve_percpu(struct sysdev_class *class,
4795 const char *buf,
4796 size_t count)
4797{
4798 struct perf_cpu_context *cpuctx;
4799 unsigned long val;
4800 int err, cpu, mpt;
4801
4802 err = strict_strtoul(buf, 10, &val);
4803 if (err)
4804 return err;
4805 if (val > perf_max_counters)
4806 return -EINVAL;
4807
Ingo Molnar1dce8d92009-05-04 19:23:18 +02004808 spin_lock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004809 perf_reserved_percpu = val;
4810 for_each_online_cpu(cpu) {
4811 cpuctx = &per_cpu(perf_cpu_context, cpu);
4812 spin_lock_irq(&cpuctx->ctx.lock);
4813 mpt = min(perf_max_counters - cpuctx->ctx.nr_counters,
4814 perf_max_counters - perf_reserved_percpu);
4815 cpuctx->max_pertask = mpt;
4816 spin_unlock_irq(&cpuctx->ctx.lock);
4817 }
Ingo Molnar1dce8d92009-05-04 19:23:18 +02004818 spin_unlock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004819
4820 return count;
4821}
4822
4823static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
4824{
4825 return sprintf(buf, "%d\n", perf_overcommit);
4826}
4827
4828static ssize_t
4829perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
4830{
4831 unsigned long val;
4832 int err;
4833
4834 err = strict_strtoul(buf, 10, &val);
4835 if (err)
4836 return err;
4837 if (val > 1)
4838 return -EINVAL;
4839
Ingo Molnar1dce8d92009-05-04 19:23:18 +02004840 spin_lock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004841 perf_overcommit = val;
Ingo Molnar1dce8d92009-05-04 19:23:18 +02004842 spin_unlock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004843
4844 return count;
4845}
4846
4847static SYSDEV_CLASS_ATTR(
4848 reserve_percpu,
4849 0644,
4850 perf_show_reserve_percpu,
4851 perf_set_reserve_percpu
4852 );
4853
4854static SYSDEV_CLASS_ATTR(
4855 overcommit,
4856 0644,
4857 perf_show_overcommit,
4858 perf_set_overcommit
4859 );
4860
4861static struct attribute *perfclass_attrs[] = {
4862 &attr_reserve_percpu.attr,
4863 &attr_overcommit.attr,
4864 NULL
4865};
4866
4867static struct attribute_group perfclass_attr_group = {
4868 .attrs = perfclass_attrs,
4869 .name = "perf_counters",
4870};
4871
4872static int __init perf_counter_sysfs_init(void)
4873{
4874 return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
4875 &perfclass_attr_group);
4876}
4877device_initcall(perf_counter_sysfs_init);