blob: 534e20d14d631b44cefc136f99200b2257af378f [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 Mackerras25346b932009-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 Mackerras25346b932009-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 Mackerras25346b932009-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 Mackerras25346b932009-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
Peter Zijlstra4af49982009-04-06 11:45:10 +0200472 if (counter->state < PERF_COUNTER_STATE_INACTIVE)
473 return;
474
475 counter->total_time_enabled = ctx->time - counter->tstamp_enabled;
476
477 if (counter->state == PERF_COUNTER_STATE_INACTIVE)
478 run_end = counter->tstamp_stopped;
479 else
480 run_end = ctx->time;
481
482 counter->total_time_running = run_end - counter->tstamp_running;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100483}
484
485/*
486 * Update total_time_enabled and total_time_running for all counters in a group.
487 */
488static void update_group_times(struct perf_counter *leader)
489{
490 struct perf_counter *counter;
491
492 update_counter_times(leader);
493 list_for_each_entry(counter, &leader->sibling_list, list_entry)
494 update_counter_times(counter);
495}
496
497/*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100498 * Cross CPU call to disable a performance counter
499 */
500static void __perf_counter_disable(void *info)
501{
502 struct perf_counter *counter = info;
503 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
504 struct perf_counter_context *ctx = counter->ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100505
506 /*
507 * If this is a per-task counter, need to check whether this
508 * counter's task is the current task on this cpu.
509 */
Peter Zijlstra665c2142009-05-29 14:51:57 +0200510 if (ctx->task && cpuctx->task_ctx != ctx)
Paul Mackerrasd859e292009-01-17 18:10:22 +1100511 return;
512
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200513 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100514
515 /*
516 * If the counter is on, turn it off.
517 * If it is in error state, leave it in error state.
518 */
519 if (counter->state >= PERF_COUNTER_STATE_INACTIVE) {
Peter Zijlstra4af49982009-04-06 11:45:10 +0200520 update_context_time(ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100521 update_counter_times(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100522 if (counter == counter->group_leader)
523 group_sched_out(counter, cpuctx, ctx);
524 else
525 counter_sched_out(counter, cpuctx, ctx);
526 counter->state = PERF_COUNTER_STATE_OFF;
527 }
528
Peter Zijlstra665c2142009-05-29 14:51:57 +0200529 spin_unlock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100530}
531
532/*
533 * Disable a counter.
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000534 *
535 * If counter->ctx is a cloned context, callers must make sure that
536 * every task struct that counter->ctx->task could possibly point to
537 * remains valid. This condition is satisifed when called through
538 * perf_counter_for_each_child or perf_counter_for_each because they
539 * hold the top-level counter's child_mutex, so any descendant that
540 * goes to exit will block in sync_child_counter.
541 * When called from perf_pending_counter it's OK because counter->ctx
542 * is the current context on this CPU and preemption is disabled,
543 * hence we can't get into perf_counter_task_sched_out for this context.
Paul Mackerrasd859e292009-01-17 18:10:22 +1100544 */
545static void perf_counter_disable(struct perf_counter *counter)
546{
547 struct perf_counter_context *ctx = counter->ctx;
548 struct task_struct *task = ctx->task;
549
550 if (!task) {
551 /*
552 * Disable the counter on the cpu that it's on
553 */
554 smp_call_function_single(counter->cpu, __perf_counter_disable,
555 counter, 1);
556 return;
557 }
558
559 retry:
560 task_oncpu_function_call(task, __perf_counter_disable, counter);
561
562 spin_lock_irq(&ctx->lock);
563 /*
564 * If the counter is still active, we need to retry the cross-call.
565 */
566 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
567 spin_unlock_irq(&ctx->lock);
568 goto retry;
569 }
570
571 /*
572 * Since we have the lock this context can't be scheduled
573 * in, so we can change the state safely.
574 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100575 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
576 update_counter_times(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100577 counter->state = PERF_COUNTER_STATE_OFF;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100578 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100579
580 spin_unlock_irq(&ctx->lock);
581}
582
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100583static int
584counter_sched_in(struct perf_counter *counter,
585 struct perf_cpu_context *cpuctx,
586 struct perf_counter_context *ctx,
587 int cpu)
588{
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100589 if (counter->state <= PERF_COUNTER_STATE_OFF)
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100590 return 0;
591
592 counter->state = PERF_COUNTER_STATE_ACTIVE;
593 counter->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */
594 /*
595 * The new state must be visible before we turn it on in the hardware:
596 */
597 smp_wmb();
598
Robert Richter4aeb0b42009-04-29 12:47:03 +0200599 if (counter->pmu->enable(counter)) {
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100600 counter->state = PERF_COUNTER_STATE_INACTIVE;
601 counter->oncpu = -1;
602 return -EAGAIN;
603 }
604
Peter Zijlstra4af49982009-04-06 11:45:10 +0200605 counter->tstamp_running += ctx->time - counter->tstamp_stopped;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100606
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100607 if (!is_software_counter(counter))
608 cpuctx->active_oncpu++;
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100609 ctx->nr_active++;
610
Peter Zijlstra0d486962009-06-02 19:22:16 +0200611 if (counter->attr.exclusive)
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100612 cpuctx->exclusive = 1;
613
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100614 return 0;
615}
616
Paul Mackerras6751b712009-05-11 12:08:02 +1000617static int
618group_sched_in(struct perf_counter *group_counter,
619 struct perf_cpu_context *cpuctx,
620 struct perf_counter_context *ctx,
621 int cpu)
622{
623 struct perf_counter *counter, *partial_group;
624 int ret;
625
626 if (group_counter->state == PERF_COUNTER_STATE_OFF)
627 return 0;
628
629 ret = hw_perf_group_sched_in(group_counter, cpuctx, ctx, cpu);
630 if (ret)
631 return ret < 0 ? ret : 0;
632
Paul Mackerras6751b712009-05-11 12:08:02 +1000633 if (counter_sched_in(group_counter, cpuctx, ctx, cpu))
634 return -EAGAIN;
635
636 /*
637 * Schedule in siblings as one group (if any):
638 */
639 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
Paul Mackerras6751b712009-05-11 12:08:02 +1000640 if (counter_sched_in(counter, cpuctx, ctx, cpu)) {
641 partial_group = counter;
642 goto group_error;
643 }
644 }
645
646 return 0;
647
648group_error:
649 /*
650 * Groups can be scheduled in as one unit only, so undo any
651 * partial group before returning:
652 */
653 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
654 if (counter == partial_group)
655 break;
656 counter_sched_out(counter, cpuctx, ctx);
657 }
658 counter_sched_out(group_counter, cpuctx, ctx);
659
660 return -EAGAIN;
661}
662
Thomas Gleixner0793a612008-12-04 20:12:29 +0100663/*
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100664 * Return 1 for a group consisting entirely of software counters,
665 * 0 if the group contains any hardware counters.
666 */
667static int is_software_only_group(struct perf_counter *leader)
668{
669 struct perf_counter *counter;
670
671 if (!is_software_counter(leader))
672 return 0;
Peter Zijlstra5c148192009-03-25 12:30:23 +0100673
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100674 list_for_each_entry(counter, &leader->sibling_list, list_entry)
675 if (!is_software_counter(counter))
676 return 0;
Peter Zijlstra5c148192009-03-25 12:30:23 +0100677
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100678 return 1;
679}
680
681/*
682 * Work out whether we can put this counter group on the CPU now.
683 */
684static int group_can_go_on(struct perf_counter *counter,
685 struct perf_cpu_context *cpuctx,
686 int can_add_hw)
687{
688 /*
689 * Groups consisting entirely of software counters can always go on.
690 */
691 if (is_software_only_group(counter))
692 return 1;
693 /*
694 * If an exclusive group is already on, no other hardware
695 * counters can go on.
696 */
697 if (cpuctx->exclusive)
698 return 0;
699 /*
700 * If this group is exclusive and there are already
701 * counters on the CPU, it can't go on.
702 */
Peter Zijlstra0d486962009-06-02 19:22:16 +0200703 if (counter->attr.exclusive && cpuctx->active_oncpu)
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100704 return 0;
705 /*
706 * Otherwise, try to add it if all previous groups were able
707 * to go on.
708 */
709 return can_add_hw;
710}
711
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100712static void add_counter_to_ctx(struct perf_counter *counter,
713 struct perf_counter_context *ctx)
714{
715 list_add_counter(counter, ctx);
Peter Zijlstra4af49982009-04-06 11:45:10 +0200716 counter->tstamp_enabled = ctx->time;
717 counter->tstamp_running = ctx->time;
718 counter->tstamp_stopped = ctx->time;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100719}
720
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100721/*
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100722 * Cross CPU call to install and enable a performance counter
Peter Zijlstra682076a2009-05-23 18:28:57 +0200723 *
724 * Must be called with ctx->mutex held
Thomas Gleixner0793a612008-12-04 20:12:29 +0100725 */
726static void __perf_install_in_context(void *info)
727{
728 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
729 struct perf_counter *counter = info;
730 struct perf_counter_context *ctx = counter->ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100731 struct perf_counter *leader = counter->group_leader;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100732 int cpu = smp_processor_id();
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100733 int err;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100734
735 /*
736 * If this is a task context, we need to check whether it is
737 * the current task context of this cpu. If not it has been
738 * scheduled out before the smp call arrived.
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000739 * Or possibly this is the right context but it isn't
740 * on this cpu because it had no counters.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100741 */
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000742 if (ctx->task && cpuctx->task_ctx != ctx) {
Peter Zijlstra665c2142009-05-29 14:51:57 +0200743 if (cpuctx->task_ctx || ctx->task != current)
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000744 return;
745 cpuctx->task_ctx = ctx;
746 }
Thomas Gleixner0793a612008-12-04 20:12:29 +0100747
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200748 spin_lock(&ctx->lock);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000749 ctx->is_active = 1;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200750 update_context_time(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100751
752 /*
753 * Protect the list operation against NMI by disabling the
754 * counters on a global level. NOP for non NMI based counters.
755 */
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200756 perf_disable();
Thomas Gleixner0793a612008-12-04 20:12:29 +0100757
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100758 add_counter_to_ctx(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100759
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100760 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100761 * Don't put the counter on if it is disabled or if
762 * it is in a group and the group isn't on.
763 */
764 if (counter->state != PERF_COUNTER_STATE_INACTIVE ||
765 (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE))
766 goto unlock;
767
768 /*
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100769 * An exclusive counter can't go on if there are already active
770 * hardware counters, and no hardware counter can go on if there
771 * is already an exclusive counter on.
772 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100773 if (!group_can_go_on(counter, cpuctx, 1))
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100774 err = -EEXIST;
775 else
776 err = counter_sched_in(counter, cpuctx, ctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100777
Paul Mackerrasd859e292009-01-17 18:10:22 +1100778 if (err) {
779 /*
780 * This counter couldn't go on. If it is in a group
781 * then we have to pull the whole group off.
782 * If the counter group is pinned then put it in error state.
783 */
784 if (leader != counter)
785 group_sched_out(leader, cpuctx, ctx);
Peter Zijlstra0d486962009-06-02 19:22:16 +0200786 if (leader->attr.pinned) {
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100787 update_group_times(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100788 leader->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100789 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100790 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100791
792 if (!err && !ctx->task && cpuctx->max_pertask)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100793 cpuctx->max_pertask--;
794
Paul Mackerrasd859e292009-01-17 18:10:22 +1100795 unlock:
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200796 perf_enable();
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100797
Peter Zijlstra665c2142009-05-29 14:51:57 +0200798 spin_unlock(&ctx->lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100799}
800
801/*
802 * Attach a performance counter to a context
803 *
804 * First we add the counter to the list with the hardware enable bit
805 * in counter->hw_config cleared.
806 *
807 * If the counter is attached to a task which is on a CPU we use a smp
808 * call to enable it in the task context. The task might have been
809 * scheduled away, but we check this in the smp call again.
Paul Mackerrasd859e292009-01-17 18:10:22 +1100810 *
811 * Must be called with ctx->mutex held.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100812 */
813static void
814perf_install_in_context(struct perf_counter_context *ctx,
815 struct perf_counter *counter,
816 int cpu)
817{
818 struct task_struct *task = ctx->task;
819
Thomas Gleixner0793a612008-12-04 20:12:29 +0100820 if (!task) {
821 /*
822 * Per cpu counters are installed via an smp call and
823 * the install is always sucessful.
824 */
825 smp_call_function_single(cpu, __perf_install_in_context,
826 counter, 1);
827 return;
828 }
829
Thomas Gleixner0793a612008-12-04 20:12:29 +0100830retry:
831 task_oncpu_function_call(task, __perf_install_in_context,
832 counter);
833
834 spin_lock_irq(&ctx->lock);
835 /*
Thomas Gleixner0793a612008-12-04 20:12:29 +0100836 * we need to retry the smp call.
837 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100838 if (ctx->is_active && list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100839 spin_unlock_irq(&ctx->lock);
840 goto retry;
841 }
842
843 /*
844 * The lock prevents that this context is scheduled in so we
845 * can add the counter safely, if it the call above did not
846 * succeed.
847 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100848 if (list_empty(&counter->list_entry))
849 add_counter_to_ctx(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100850 spin_unlock_irq(&ctx->lock);
851}
852
Paul Mackerrasd859e292009-01-17 18:10:22 +1100853/*
854 * Cross CPU call to enable a performance counter
855 */
856static void __perf_counter_enable(void *info)
Ingo Molnar04289bb2008-12-11 08:38:42 +0100857{
Paul Mackerrasd859e292009-01-17 18:10:22 +1100858 struct perf_counter *counter = info;
859 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
860 struct perf_counter_context *ctx = counter->ctx;
861 struct perf_counter *leader = counter->group_leader;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100862 int err;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100863
864 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100865 * If this is a per-task counter, need to check whether this
866 * counter's task is the current task on this cpu.
Ingo Molnar04289bb2008-12-11 08:38:42 +0100867 */
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000868 if (ctx->task && cpuctx->task_ctx != ctx) {
Peter Zijlstra665c2142009-05-29 14:51:57 +0200869 if (cpuctx->task_ctx || ctx->task != current)
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000870 return;
871 cpuctx->task_ctx = ctx;
872 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100873
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200874 spin_lock(&ctx->lock);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000875 ctx->is_active = 1;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200876 update_context_time(ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100877
878 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
879 goto unlock;
880 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200881 counter->tstamp_enabled = ctx->time - counter->total_time_enabled;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100882
883 /*
884 * If the counter is in a group and isn't the group leader,
885 * then don't put it on unless the group is on.
886 */
887 if (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE)
888 goto unlock;
889
Paul Mackerrase758a332009-05-12 21:59:01 +1000890 if (!group_can_go_on(counter, cpuctx, 1)) {
Paul Mackerrasd859e292009-01-17 18:10:22 +1100891 err = -EEXIST;
Paul Mackerrase758a332009-05-12 21:59:01 +1000892 } else {
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200893 perf_disable();
Paul Mackerrase758a332009-05-12 21:59:01 +1000894 if (counter == leader)
895 err = group_sched_in(counter, cpuctx, ctx,
896 smp_processor_id());
897 else
898 err = counter_sched_in(counter, cpuctx, ctx,
899 smp_processor_id());
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200900 perf_enable();
Paul Mackerrase758a332009-05-12 21:59:01 +1000901 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100902
903 if (err) {
904 /*
905 * If this counter can't go on and it's part of a
906 * group, then the whole group has to come off.
907 */
908 if (leader != counter)
909 group_sched_out(leader, cpuctx, ctx);
Peter Zijlstra0d486962009-06-02 19:22:16 +0200910 if (leader->attr.pinned) {
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100911 update_group_times(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100912 leader->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100913 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100914 }
915
916 unlock:
Peter Zijlstra665c2142009-05-29 14:51:57 +0200917 spin_unlock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100918}
919
920/*
921 * Enable a counter.
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000922 *
923 * If counter->ctx is a cloned context, callers must make sure that
924 * every task struct that counter->ctx->task could possibly point to
925 * remains valid. This condition is satisfied when called through
926 * perf_counter_for_each_child or perf_counter_for_each as described
927 * for perf_counter_disable.
Paul Mackerrasd859e292009-01-17 18:10:22 +1100928 */
929static void perf_counter_enable(struct perf_counter *counter)
930{
931 struct perf_counter_context *ctx = counter->ctx;
932 struct task_struct *task = ctx->task;
933
934 if (!task) {
935 /*
936 * Enable the counter on the cpu that it's on
937 */
938 smp_call_function_single(counter->cpu, __perf_counter_enable,
939 counter, 1);
940 return;
941 }
942
943 spin_lock_irq(&ctx->lock);
944 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
945 goto out;
946
947 /*
948 * If the counter is in error state, clear that first.
949 * That way, if we see the counter in error state below, we
950 * know that it has gone back into error state, as distinct
951 * from the task having been scheduled away before the
952 * cross-call arrived.
953 */
954 if (counter->state == PERF_COUNTER_STATE_ERROR)
955 counter->state = PERF_COUNTER_STATE_OFF;
956
957 retry:
958 spin_unlock_irq(&ctx->lock);
959 task_oncpu_function_call(task, __perf_counter_enable, counter);
960
961 spin_lock_irq(&ctx->lock);
962
963 /*
964 * If the context is active and the counter is still off,
965 * we need to retry the cross-call.
966 */
967 if (ctx->is_active && counter->state == PERF_COUNTER_STATE_OFF)
968 goto retry;
969
970 /*
971 * Since we have the lock this context can't be scheduled
972 * in, so we can change the state safely.
973 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100974 if (counter->state == PERF_COUNTER_STATE_OFF) {
Paul Mackerrasd859e292009-01-17 18:10:22 +1100975 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200976 counter->tstamp_enabled =
977 ctx->time - counter->total_time_enabled;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100978 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100979 out:
980 spin_unlock_irq(&ctx->lock);
981}
982
Peter Zijlstra2023b352009-05-05 17:50:26 +0200983static int perf_counter_refresh(struct perf_counter *counter, int refresh)
Peter Zijlstra79f14642009-04-06 11:45:07 +0200984{
Peter Zijlstra2023b352009-05-05 17:50:26 +0200985 /*
986 * not supported on inherited counters
987 */
Peter Zijlstra0d486962009-06-02 19:22:16 +0200988 if (counter->attr.inherit)
Peter Zijlstra2023b352009-05-05 17:50:26 +0200989 return -EINVAL;
990
Peter Zijlstra79f14642009-04-06 11:45:07 +0200991 atomic_add(refresh, &counter->event_limit);
992 perf_counter_enable(counter);
Peter Zijlstra2023b352009-05-05 17:50:26 +0200993
994 return 0;
Peter Zijlstra79f14642009-04-06 11:45:07 +0200995}
996
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100997void __perf_counter_sched_out(struct perf_counter_context *ctx,
998 struct perf_cpu_context *cpuctx)
999{
1000 struct perf_counter *counter;
1001
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001002 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001003 ctx->is_active = 0;
1004 if (likely(!ctx->nr_counters))
1005 goto out;
Peter Zijlstra4af49982009-04-06 11:45:10 +02001006 update_context_time(ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001007
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001008 perf_disable();
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001009 if (ctx->nr_active) {
Peter Zijlstraafedadf2009-05-20 12:21:22 +02001010 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1011 if (counter != counter->group_leader)
1012 counter_sched_out(counter, cpuctx, ctx);
1013 else
1014 group_sched_out(counter, cpuctx, ctx);
1015 }
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001016 }
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001017 perf_enable();
Paul Mackerrasd859e292009-01-17 18:10:22 +11001018 out:
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001019 spin_unlock(&ctx->lock);
1020}
1021
Thomas Gleixner0793a612008-12-04 20:12:29 +01001022/*
Paul Mackerras564c2b22009-05-22 14:27:22 +10001023 * Test whether two contexts are equivalent, i.e. whether they
1024 * have both been cloned from the same version of the same context
1025 * and they both have the same number of enabled counters.
1026 * If the number of enabled counters is the same, then the set
1027 * of enabled counters should be the same, because these are both
1028 * inherited contexts, therefore we can't access individual counters
1029 * in them directly with an fd; we can only enable/disable all
1030 * counters via prctl, or enable/disable all counters in a family
1031 * via ioctl, which will have the same effect on both contexts.
1032 */
1033static int context_equiv(struct perf_counter_context *ctx1,
1034 struct perf_counter_context *ctx2)
1035{
1036 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001037 && ctx1->parent_gen == ctx2->parent_gen
Paul Mackerras25346b932009-06-01 17:48:12 +10001038 && !ctx1->pin_count && !ctx2->pin_count;
Paul Mackerras564c2b22009-05-22 14:27:22 +10001039}
1040
Peter Zijlstrabfbd3382009-06-24 21:11:59 +02001041static void __perf_counter_read(void *counter);
1042
1043static void __perf_counter_sync_stat(struct perf_counter *counter,
1044 struct perf_counter *next_counter)
1045{
1046 u64 value;
1047
1048 if (!counter->attr.inherit_stat)
1049 return;
1050
1051 /*
1052 * Update the counter value, we cannot use perf_counter_read()
1053 * because we're in the middle of a context switch and have IRQs
1054 * disabled, which upsets smp_call_function_single(), however
1055 * we know the counter must be on the current CPU, therefore we
1056 * don't need to use it.
1057 */
1058 switch (counter->state) {
1059 case PERF_COUNTER_STATE_ACTIVE:
1060 __perf_counter_read(counter);
1061 break;
1062
1063 case PERF_COUNTER_STATE_INACTIVE:
1064 update_counter_times(counter);
1065 break;
1066
1067 default:
1068 break;
1069 }
1070
1071 /*
1072 * In order to keep per-task stats reliable we need to flip the counter
1073 * values when we flip the contexts.
1074 */
1075 value = atomic64_read(&next_counter->count);
1076 value = atomic64_xchg(&counter->count, value);
1077 atomic64_set(&next_counter->count, value);
1078
Peter Zijlstra19d2e752009-06-26 13:10:23 +02001079 swap(counter->total_time_enabled, next_counter->total_time_enabled);
1080 swap(counter->total_time_running, next_counter->total_time_running);
1081
Peter Zijlstrabfbd3382009-06-24 21:11:59 +02001082 /*
Peter Zijlstra19d2e752009-06-26 13:10:23 +02001083 * Since we swizzled the values, update the user visible data too.
Peter Zijlstrabfbd3382009-06-24 21:11:59 +02001084 */
Peter Zijlstra19d2e752009-06-26 13:10:23 +02001085 perf_counter_update_userpage(counter);
1086 perf_counter_update_userpage(next_counter);
Peter Zijlstrabfbd3382009-06-24 21:11:59 +02001087}
1088
1089#define list_next_entry(pos, member) \
1090 list_entry(pos->member.next, typeof(*pos), member)
1091
1092static void perf_counter_sync_stat(struct perf_counter_context *ctx,
1093 struct perf_counter_context *next_ctx)
1094{
1095 struct perf_counter *counter, *next_counter;
1096
1097 if (!ctx->nr_stat)
1098 return;
1099
1100 counter = list_first_entry(&ctx->event_list,
1101 struct perf_counter, event_entry);
1102
1103 next_counter = list_first_entry(&next_ctx->event_list,
1104 struct perf_counter, event_entry);
1105
1106 while (&counter->event_entry != &ctx->event_list &&
1107 &next_counter->event_entry != &next_ctx->event_list) {
1108
1109 __perf_counter_sync_stat(counter, next_counter);
1110
1111 counter = list_next_entry(counter, event_entry);
Peter Zijlstra10545982009-08-06 18:06:26 +02001112 next_counter = list_next_entry(next_counter, event_entry);
Peter Zijlstrabfbd3382009-06-24 21:11:59 +02001113 }
1114}
1115
Paul Mackerras564c2b22009-05-22 14:27:22 +10001116/*
Thomas Gleixner0793a612008-12-04 20:12:29 +01001117 * Called from scheduler to remove the counters of the current task,
1118 * with interrupts disabled.
1119 *
1120 * We stop each counter and update the counter value in counter->count.
1121 *
Ingo Molnar76715812008-12-17 14:20:28 +01001122 * This does not protect us against NMI, but disable()
Thomas Gleixner0793a612008-12-04 20:12:29 +01001123 * sets the disabled bit in the control field of counter _before_
1124 * accessing the counter control register. If a NMI hits, then it will
1125 * not restart the counter.
1126 */
Paul Mackerras564c2b22009-05-22 14:27:22 +10001127void perf_counter_task_sched_out(struct task_struct *task,
1128 struct task_struct *next, int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001129{
1130 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001131 struct perf_counter_context *ctx = task->perf_counter_ctxp;
Paul Mackerras564c2b22009-05-22 14:27:22 +10001132 struct perf_counter_context *next_ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001133 struct perf_counter_context *parent;
Peter Zijlstra4a0deca2009-03-19 20:26:12 +01001134 struct pt_regs *regs;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001135 int do_switch = 1;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001136
Peter Zijlstra10989fb2009-05-25 14:45:28 +02001137 regs = task_pt_regs(task);
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +02001138 perf_swcounter_event(PERF_COUNT_SW_CONTEXT_SWITCHES, 1, 1, regs, 0);
Peter Zijlstra10989fb2009-05-25 14:45:28 +02001139
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001140 if (likely(!ctx || !cpuctx->task_ctx))
Thomas Gleixner0793a612008-12-04 20:12:29 +01001141 return;
1142
Peter Zijlstrabce379b2009-04-06 11:45:13 +02001143 update_context_time(ctx);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001144
1145 rcu_read_lock();
1146 parent = rcu_dereference(ctx->parent_ctx);
Paul Mackerras564c2b22009-05-22 14:27:22 +10001147 next_ctx = next->perf_counter_ctxp;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001148 if (parent && next_ctx &&
1149 rcu_dereference(next_ctx->parent_ctx) == parent) {
1150 /*
1151 * Looks like the two contexts are clones, so we might be
1152 * able to optimize the context switch. We lock both
1153 * contexts and check that they are clones under the
1154 * lock (including re-checking that neither has been
1155 * uncloned in the meantime). It doesn't matter which
1156 * order we take the locks because no other cpu could
1157 * be trying to lock both of these tasks.
1158 */
1159 spin_lock(&ctx->lock);
1160 spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
1161 if (context_equiv(ctx, next_ctx)) {
Peter Zijlstra665c2142009-05-29 14:51:57 +02001162 /*
1163 * XXX do we need a memory barrier of sorts
1164 * wrt to rcu_dereference() of perf_counter_ctxp
1165 */
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001166 task->perf_counter_ctxp = next_ctx;
1167 next->perf_counter_ctxp = ctx;
1168 ctx->task = next;
1169 next_ctx->task = task;
1170 do_switch = 0;
Peter Zijlstrabfbd3382009-06-24 21:11:59 +02001171
1172 perf_counter_sync_stat(ctx, next_ctx);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001173 }
1174 spin_unlock(&next_ctx->lock);
1175 spin_unlock(&ctx->lock);
Paul Mackerras564c2b22009-05-22 14:27:22 +10001176 }
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001177 rcu_read_unlock();
Paul Mackerras564c2b22009-05-22 14:27:22 +10001178
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001179 if (do_switch) {
1180 __perf_counter_sched_out(ctx, cpuctx);
1181 cpuctx->task_ctx = NULL;
1182 }
Thomas Gleixner0793a612008-12-04 20:12:29 +01001183}
1184
Peter Zijlstra665c2142009-05-29 14:51:57 +02001185/*
1186 * Called with IRQs disabled
1187 */
Paul Mackerrasa08b1592009-05-11 15:46:10 +10001188static void __perf_counter_task_sched_out(struct perf_counter_context *ctx)
1189{
1190 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1191
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001192 if (!cpuctx->task_ctx)
1193 return;
Ingo Molnar012b84d2009-05-17 11:08:41 +02001194
1195 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
1196 return;
1197
Paul Mackerrasa08b1592009-05-11 15:46:10 +10001198 __perf_counter_sched_out(ctx, cpuctx);
1199 cpuctx->task_ctx = NULL;
1200}
1201
Peter Zijlstra665c2142009-05-29 14:51:57 +02001202/*
1203 * Called with IRQs disabled
1204 */
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001205static void perf_counter_cpu_sched_out(struct perf_cpu_context *cpuctx)
Ingo Molnar04289bb2008-12-11 08:38:42 +01001206{
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001207 __perf_counter_sched_out(&cpuctx->ctx, cpuctx);
Ingo Molnar04289bb2008-12-11 08:38:42 +01001208}
1209
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001210static void
1211__perf_counter_sched_in(struct perf_counter_context *ctx,
1212 struct perf_cpu_context *cpuctx, int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001213{
Thomas Gleixner0793a612008-12-04 20:12:29 +01001214 struct perf_counter *counter;
Paul Mackerrasdd0e6ba2009-01-12 15:11:00 +11001215 int can_add_hw = 1;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001216
Thomas Gleixner0793a612008-12-04 20:12:29 +01001217 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001218 ctx->is_active = 1;
1219 if (likely(!ctx->nr_counters))
1220 goto out;
1221
Peter Zijlstra4af49982009-04-06 11:45:10 +02001222 ctx->timestamp = perf_clock();
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001223
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001224 perf_disable();
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001225
1226 /*
1227 * First go through the list and put on any pinned groups
1228 * in order to give them the best chance of going on.
1229 */
Ingo Molnar04289bb2008-12-11 08:38:42 +01001230 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001231 if (counter->state <= PERF_COUNTER_STATE_OFF ||
Peter Zijlstra0d486962009-06-02 19:22:16 +02001232 !counter->attr.pinned)
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001233 continue;
1234 if (counter->cpu != -1 && counter->cpu != cpu)
1235 continue;
1236
Peter Zijlstraafedadf2009-05-20 12:21:22 +02001237 if (counter != counter->group_leader)
1238 counter_sched_in(counter, cpuctx, ctx, cpu);
1239 else {
1240 if (group_can_go_on(counter, cpuctx, 1))
1241 group_sched_in(counter, cpuctx, ctx, cpu);
1242 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001243
1244 /*
1245 * If this pinned group hasn't been scheduled,
1246 * put it in error state.
1247 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001248 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
1249 update_group_times(counter);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001250 counter->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001251 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001252 }
1253
1254 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1255 /*
1256 * Ignore counters in OFF or ERROR state, and
1257 * ignore pinned counters since we did them already.
1258 */
1259 if (counter->state <= PERF_COUNTER_STATE_OFF ||
Peter Zijlstra0d486962009-06-02 19:22:16 +02001260 counter->attr.pinned)
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001261 continue;
1262
Ingo Molnar04289bb2008-12-11 08:38:42 +01001263 /*
1264 * Listen to the 'cpu' scheduling filter constraint
1265 * of counters:
1266 */
Thomas Gleixner0793a612008-12-04 20:12:29 +01001267 if (counter->cpu != -1 && counter->cpu != cpu)
1268 continue;
1269
Peter Zijlstraafedadf2009-05-20 12:21:22 +02001270 if (counter != counter->group_leader) {
1271 if (counter_sched_in(counter, cpuctx, ctx, cpu))
Paul Mackerrasdd0e6ba2009-01-12 15:11:00 +11001272 can_add_hw = 0;
Peter Zijlstraafedadf2009-05-20 12:21:22 +02001273 } else {
1274 if (group_can_go_on(counter, cpuctx, can_add_hw)) {
1275 if (group_sched_in(counter, cpuctx, ctx, cpu))
1276 can_add_hw = 0;
1277 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001278 }
Thomas Gleixner0793a612008-12-04 20:12:29 +01001279 }
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001280 perf_enable();
Paul Mackerrasd859e292009-01-17 18:10:22 +11001281 out:
Thomas Gleixner0793a612008-12-04 20:12:29 +01001282 spin_unlock(&ctx->lock);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001283}
Ingo Molnar04289bb2008-12-11 08:38:42 +01001284
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001285/*
1286 * Called from scheduler to add the counters of the current task
1287 * with interrupts disabled.
1288 *
1289 * We restore the counter value and then enable it.
1290 *
1291 * This does not protect us against NMI, but enable()
1292 * sets the enabled bit in the control field of counter _before_
1293 * accessing the counter control register. If a NMI hits, then it will
1294 * keep the counter running.
1295 */
1296void perf_counter_task_sched_in(struct task_struct *task, int cpu)
1297{
1298 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001299 struct perf_counter_context *ctx = task->perf_counter_ctxp;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001300
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001301 if (likely(!ctx))
1302 return;
Paul Mackerras564c2b22009-05-22 14:27:22 +10001303 if (cpuctx->task_ctx == ctx)
1304 return;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001305 __perf_counter_sched_in(ctx, cpuctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001306 cpuctx->task_ctx = ctx;
1307}
1308
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001309static void perf_counter_cpu_sched_in(struct perf_cpu_context *cpuctx, int cpu)
1310{
1311 struct perf_counter_context *ctx = &cpuctx->ctx;
1312
1313 __perf_counter_sched_in(ctx, cpuctx, cpu);
1314}
1315
Peter Zijlstraa78ac322009-05-25 17:39:05 +02001316#define MAX_INTERRUPTS (~0ULL)
1317
1318static void perf_log_throttle(struct perf_counter *counter, int enable);
Peter Zijlstra26b119b2009-05-20 12:21:20 +02001319
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001320static void perf_adjust_period(struct perf_counter *counter, u64 events)
1321{
1322 struct hw_perf_counter *hwc = &counter->hw;
1323 u64 period, sample_period;
1324 s64 delta;
1325
1326 events *= hwc->sample_period;
1327 period = div64_u64(events, counter->attr.sample_freq);
1328
1329 delta = (s64)(period - hwc->sample_period);
1330 delta = (delta + 7) / 8; /* low pass filter */
1331
1332 sample_period = hwc->sample_period + delta;
1333
1334 if (!sample_period)
1335 sample_period = 1;
1336
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001337 hwc->sample_period = sample_period;
1338}
1339
1340static void perf_ctx_adjust_freq(struct perf_counter_context *ctx)
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001341{
1342 struct perf_counter *counter;
Peter Zijlstra6a24ed6c2009-06-05 18:01:29 +02001343 struct hw_perf_counter *hwc;
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001344 u64 interrupts, freq;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001345
1346 spin_lock(&ctx->lock);
1347 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1348 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
1349 continue;
1350
Peter Zijlstra6a24ed6c2009-06-05 18:01:29 +02001351 hwc = &counter->hw;
1352
1353 interrupts = hwc->interrupts;
1354 hwc->interrupts = 0;
Peter Zijlstraa78ac322009-05-25 17:39:05 +02001355
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001356 /*
1357 * unthrottle counters on the tick
1358 */
Peter Zijlstraa78ac322009-05-25 17:39:05 +02001359 if (interrupts == MAX_INTERRUPTS) {
1360 perf_log_throttle(counter, 1);
1361 counter->pmu->unthrottle(counter);
Peter Zijlstradf58ab22009-06-11 11:25:05 +02001362 interrupts = 2*sysctl_perf_counter_sample_rate/HZ;
Peter Zijlstraa78ac322009-05-25 17:39:05 +02001363 }
1364
Peter Zijlstra0d486962009-06-02 19:22:16 +02001365 if (!counter->attr.freq || !counter->attr.sample_freq)
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001366 continue;
1367
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001368 /*
1369 * if the specified freq < HZ then we need to skip ticks
1370 */
Peter Zijlstra6a24ed6c2009-06-05 18:01:29 +02001371 if (counter->attr.sample_freq < HZ) {
1372 freq = counter->attr.sample_freq;
1373
1374 hwc->freq_count += freq;
1375 hwc->freq_interrupts += interrupts;
1376
1377 if (hwc->freq_count < HZ)
1378 continue;
1379
1380 interrupts = hwc->freq_interrupts;
1381 hwc->freq_interrupts = 0;
1382 hwc->freq_count -= HZ;
1383 } else
1384 freq = HZ;
1385
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001386 perf_adjust_period(counter, freq * interrupts);
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001387
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001388 /*
1389 * In order to avoid being stalled by an (accidental) huge
1390 * sample period, force reset the sample period if we didn't
1391 * get any events in this freq period.
1392 */
1393 if (!interrupts) {
1394 perf_disable();
1395 counter->pmu->disable(counter);
Paul Mackerras87847b82009-06-13 17:06:50 +10001396 atomic64_set(&hwc->period_left, 0);
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001397 counter->pmu->enable(counter);
1398 perf_enable();
1399 }
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001400 }
1401 spin_unlock(&ctx->lock);
1402}
1403
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001404/*
1405 * Round-robin a context's counters:
1406 */
1407static void rotate_ctx(struct perf_counter_context *ctx)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001408{
Thomas Gleixner0793a612008-12-04 20:12:29 +01001409 struct perf_counter *counter;
1410
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001411 if (!ctx->nr_counters)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001412 return;
1413
Thomas Gleixner0793a612008-12-04 20:12:29 +01001414 spin_lock(&ctx->lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001415 /*
Ingo Molnar04289bb2008-12-11 08:38:42 +01001416 * Rotate the first entry last (works just fine for group counters too):
Thomas Gleixner0793a612008-12-04 20:12:29 +01001417 */
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001418 perf_disable();
Ingo Molnar04289bb2008-12-11 08:38:42 +01001419 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Peter Zijlstra75564232009-03-13 12:21:29 +01001420 list_move_tail(&counter->list_entry, &ctx->counter_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001421 break;
1422 }
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001423 perf_enable();
Thomas Gleixner0793a612008-12-04 20:12:29 +01001424
1425 spin_unlock(&ctx->lock);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001426}
Thomas Gleixner0793a612008-12-04 20:12:29 +01001427
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001428void perf_counter_task_tick(struct task_struct *curr, int cpu)
1429{
Peter Zijlstra7fc23a52009-05-08 18:52:21 +02001430 struct perf_cpu_context *cpuctx;
1431 struct perf_counter_context *ctx;
1432
1433 if (!atomic_read(&nr_counters))
1434 return;
1435
1436 cpuctx = &per_cpu(perf_cpu_context, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001437 ctx = curr->perf_counter_ctxp;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001438
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001439 perf_ctx_adjust_freq(&cpuctx->ctx);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001440 if (ctx)
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001441 perf_ctx_adjust_freq(ctx);
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001442
Ingo Molnarb82914c2009-05-04 18:54:32 +02001443 perf_counter_cpu_sched_out(cpuctx);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001444 if (ctx)
1445 __perf_counter_task_sched_out(ctx);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001446
Ingo Molnarb82914c2009-05-04 18:54:32 +02001447 rotate_ctx(&cpuctx->ctx);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001448 if (ctx)
1449 rotate_ctx(ctx);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001450
Ingo Molnarb82914c2009-05-04 18:54:32 +02001451 perf_counter_cpu_sched_in(cpuctx, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001452 if (ctx)
1453 perf_counter_task_sched_in(curr, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001454}
1455
1456/*
Paul Mackerras57e79862009-06-30 16:07:19 +10001457 * Enable all of a task's counters that have been marked enable-on-exec.
1458 * This expects task == current.
1459 */
1460static void perf_counter_enable_on_exec(struct task_struct *task)
1461{
1462 struct perf_counter_context *ctx;
1463 struct perf_counter *counter;
1464 unsigned long flags;
1465 int enabled = 0;
1466
1467 local_irq_save(flags);
1468 ctx = task->perf_counter_ctxp;
1469 if (!ctx || !ctx->nr_counters)
1470 goto out;
1471
1472 __perf_counter_task_sched_out(ctx);
1473
1474 spin_lock(&ctx->lock);
1475
1476 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1477 if (!counter->attr.enable_on_exec)
1478 continue;
1479 counter->attr.enable_on_exec = 0;
1480 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
1481 continue;
1482 counter->state = PERF_COUNTER_STATE_INACTIVE;
1483 counter->tstamp_enabled =
1484 ctx->time - counter->total_time_enabled;
1485 enabled = 1;
1486 }
1487
1488 /*
1489 * Unclone this context if we enabled any counter.
1490 */
Peter Zijlstra71a851b2009-07-10 09:06:56 +02001491 if (enabled)
1492 unclone_ctx(ctx);
Paul Mackerras57e79862009-06-30 16:07:19 +10001493
1494 spin_unlock(&ctx->lock);
1495
1496 perf_counter_task_sched_in(task, smp_processor_id());
1497 out:
1498 local_irq_restore(flags);
1499}
1500
1501/*
Thomas Gleixner0793a612008-12-04 20:12:29 +01001502 * Cross CPU call to read the hardware counter
1503 */
Peter Zijlstrabfbd3382009-06-24 21:11:59 +02001504static void __perf_counter_read(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001505{
Ingo Molnar621a01e2008-12-11 12:46:46 +01001506 struct perf_counter *counter = info;
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001507 struct perf_counter_context *ctx = counter->ctx;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001508 unsigned long flags;
Ingo Molnar621a01e2008-12-11 12:46:46 +01001509
Peter Zijlstra849691a2009-04-06 11:45:12 +02001510 local_irq_save(flags);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001511 if (ctx->is_active)
Peter Zijlstra4af49982009-04-06 11:45:10 +02001512 update_context_time(ctx);
Robert Richter4aeb0b42009-04-29 12:47:03 +02001513 counter->pmu->read(counter);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001514 update_counter_times(counter);
Peter Zijlstra849691a2009-04-06 11:45:12 +02001515 local_irq_restore(flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001516}
1517
Ingo Molnar04289bb2008-12-11 08:38:42 +01001518static u64 perf_counter_read(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001519{
1520 /*
1521 * If counter is enabled and currently active on a CPU, update the
1522 * value in the counter structure:
1523 */
Ingo Molnar6a930702008-12-11 15:17:03 +01001524 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
Thomas Gleixner0793a612008-12-04 20:12:29 +01001525 smp_call_function_single(counter->oncpu,
Peter Zijlstrabfbd3382009-06-24 21:11:59 +02001526 __perf_counter_read, counter, 1);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001527 } else if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
1528 update_counter_times(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001529 }
1530
Ingo Molnaree060942008-12-13 09:00:03 +01001531 return atomic64_read(&counter->count);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001532}
1533
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001534/*
1535 * Initialize the perf_counter context in a task_struct:
1536 */
1537static void
1538__perf_counter_init_context(struct perf_counter_context *ctx,
1539 struct task_struct *task)
1540{
1541 memset(ctx, 0, sizeof(*ctx));
1542 spin_lock_init(&ctx->lock);
1543 mutex_init(&ctx->mutex);
1544 INIT_LIST_HEAD(&ctx->counter_list);
1545 INIT_LIST_HEAD(&ctx->event_list);
1546 atomic_set(&ctx->refcount, 1);
1547 ctx->task = task;
1548}
1549
Thomas Gleixner0793a612008-12-04 20:12:29 +01001550static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
1551{
Ingo Molnar22a4f652009-06-01 10:13:37 +02001552 struct perf_counter_context *ctx;
1553 struct perf_cpu_context *cpuctx;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001554 struct task_struct *task;
Paul Mackerras25346b932009-06-01 17:48:12 +10001555 unsigned long flags;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001556 int err;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001557
1558 /*
1559 * If cpu is not a wildcard then this is a percpu counter:
1560 */
1561 if (cpu != -1) {
1562 /* Must be root to operate on a CPU counter: */
Peter Zijlstra07647712009-06-11 11:18:36 +02001563 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
Thomas Gleixner0793a612008-12-04 20:12:29 +01001564 return ERR_PTR(-EACCES);
1565
1566 if (cpu < 0 || cpu > num_possible_cpus())
1567 return ERR_PTR(-EINVAL);
1568
1569 /*
1570 * We could be clever and allow to attach a counter to an
1571 * offline CPU and activate it when the CPU comes up, but
1572 * that's for later.
1573 */
1574 if (!cpu_isset(cpu, cpu_online_map))
1575 return ERR_PTR(-ENODEV);
1576
1577 cpuctx = &per_cpu(perf_cpu_context, cpu);
1578 ctx = &cpuctx->ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001579 get_ctx(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001580
Thomas Gleixner0793a612008-12-04 20:12:29 +01001581 return ctx;
1582 }
1583
1584 rcu_read_lock();
1585 if (!pid)
1586 task = current;
1587 else
1588 task = find_task_by_vpid(pid);
1589 if (task)
1590 get_task_struct(task);
1591 rcu_read_unlock();
1592
1593 if (!task)
1594 return ERR_PTR(-ESRCH);
1595
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001596 /*
1597 * Can't attach counters to a dying task.
1598 */
1599 err = -ESRCH;
1600 if (task->flags & PF_EXITING)
1601 goto errout;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001602
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001603 /* Reuse ptrace permission checks for now. */
1604 err = -EACCES;
1605 if (!ptrace_may_access(task, PTRACE_MODE_READ))
1606 goto errout;
1607
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001608 retry:
Paul Mackerras25346b932009-06-01 17:48:12 +10001609 ctx = perf_lock_task_context(task, &flags);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001610 if (ctx) {
Peter Zijlstra71a851b2009-07-10 09:06:56 +02001611 unclone_ctx(ctx);
Paul Mackerras25346b932009-06-01 17:48:12 +10001612 spin_unlock_irqrestore(&ctx->lock, flags);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001613 }
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001614
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001615 if (!ctx) {
1616 ctx = kmalloc(sizeof(struct perf_counter_context), GFP_KERNEL);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001617 err = -ENOMEM;
1618 if (!ctx)
1619 goto errout;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001620 __perf_counter_init_context(ctx, task);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001621 get_ctx(ctx);
1622 if (cmpxchg(&task->perf_counter_ctxp, NULL, ctx)) {
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001623 /*
1624 * We raced with some other task; use
1625 * the context they set.
1626 */
1627 kfree(ctx);
Paul Mackerras25346b932009-06-01 17:48:12 +10001628 goto retry;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001629 }
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001630 get_task_struct(task);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001631 }
1632
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001633 put_task_struct(task);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001634 return ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001635
1636 errout:
1637 put_task_struct(task);
1638 return ERR_PTR(err);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001639}
1640
Peter Zijlstra592903c2009-03-13 12:21:36 +01001641static void free_counter_rcu(struct rcu_head *head)
1642{
1643 struct perf_counter *counter;
1644
1645 counter = container_of(head, struct perf_counter, rcu_head);
Peter Zijlstra709e50c2009-06-02 14:13:15 +02001646 if (counter->ns)
1647 put_pid_ns(counter->ns);
Peter Zijlstra592903c2009-03-13 12:21:36 +01001648 kfree(counter);
1649}
1650
Peter Zijlstra925d5192009-03-30 19:07:02 +02001651static void perf_pending_sync(struct perf_counter *counter);
1652
Peter Zijlstraf1600952009-03-19 20:26:16 +01001653static void free_counter(struct perf_counter *counter)
1654{
Peter Zijlstra925d5192009-03-30 19:07:02 +02001655 perf_pending_sync(counter);
1656
Peter Zijlstraf3440112009-06-22 13:58:35 +02001657 if (!counter->parent) {
1658 atomic_dec(&nr_counters);
1659 if (counter->attr.mmap)
1660 atomic_dec(&nr_mmap_counters);
1661 if (counter->attr.comm)
1662 atomic_dec(&nr_comm_counters);
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02001663 if (counter->attr.task)
1664 atomic_dec(&nr_task_counters);
Peter Zijlstraf3440112009-06-22 13:58:35 +02001665 }
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02001666
Peter Zijlstrae077df42009-03-19 20:26:17 +01001667 if (counter->destroy)
1668 counter->destroy(counter);
1669
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001670 put_ctx(counter->ctx);
Peter Zijlstraf1600952009-03-19 20:26:16 +01001671 call_rcu(&counter->rcu_head, free_counter_rcu);
1672}
1673
Thomas Gleixner0793a612008-12-04 20:12:29 +01001674/*
1675 * Called when the last reference to the file is gone.
1676 */
1677static int perf_release(struct inode *inode, struct file *file)
1678{
1679 struct perf_counter *counter = file->private_data;
1680 struct perf_counter_context *ctx = counter->ctx;
1681
1682 file->private_data = NULL;
1683
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001684 WARN_ON_ONCE(ctx->parent_ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001685 mutex_lock(&ctx->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01001686 perf_counter_remove_from_context(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001687 mutex_unlock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001688
Peter Zijlstra082ff5a2009-05-23 18:29:00 +02001689 mutex_lock(&counter->owner->perf_counter_mutex);
1690 list_del_init(&counter->owner_entry);
1691 mutex_unlock(&counter->owner->perf_counter_mutex);
1692 put_task_struct(counter->owner);
1693
Peter Zijlstraf1600952009-03-19 20:26:16 +01001694 free_counter(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001695
1696 return 0;
1697}
1698
Peter Zijlstra3dab77f2009-08-13 11:47:53 +02001699static int perf_counter_read_size(struct perf_counter *counter)
1700{
1701 int entry = sizeof(u64); /* value */
1702 int size = 0;
1703 int nr = 1;
1704
1705 if (counter->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1706 size += sizeof(u64);
1707
1708 if (counter->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1709 size += sizeof(u64);
1710
1711 if (counter->attr.read_format & PERF_FORMAT_ID)
1712 entry += sizeof(u64);
1713
1714 if (counter->attr.read_format & PERF_FORMAT_GROUP) {
1715 nr += counter->group_leader->nr_siblings;
1716 size += sizeof(u64);
1717 }
1718
1719 size += entry * nr;
1720
1721 return size;
1722}
1723
1724static u64 perf_counter_read_value(struct perf_counter *counter)
Peter Zijlstrae53c0992009-07-24 14:42:10 +02001725{
1726 struct perf_counter *child;
1727 u64 total = 0;
1728
1729 total += perf_counter_read(counter);
1730 list_for_each_entry(child, &counter->child_list, child_list)
1731 total += perf_counter_read(child);
1732
1733 return total;
1734}
1735
Peter Zijlstra3dab77f2009-08-13 11:47:53 +02001736static int perf_counter_read_entry(struct perf_counter *counter,
1737 u64 read_format, char __user *buf)
1738{
1739 int n = 0, count = 0;
1740 u64 values[2];
1741
1742 values[n++] = perf_counter_read_value(counter);
1743 if (read_format & PERF_FORMAT_ID)
1744 values[n++] = primary_counter_id(counter);
1745
1746 count = n * sizeof(u64);
1747
1748 if (copy_to_user(buf, values, count))
1749 return -EFAULT;
1750
1751 return count;
1752}
1753
1754static int perf_counter_read_group(struct perf_counter *counter,
1755 u64 read_format, char __user *buf)
1756{
1757 struct perf_counter *leader = counter->group_leader, *sub;
1758 int n = 0, size = 0, err = -EFAULT;
1759 u64 values[3];
1760
1761 values[n++] = 1 + leader->nr_siblings;
1762 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
1763 values[n++] = leader->total_time_enabled +
1764 atomic64_read(&leader->child_total_time_enabled);
1765 }
1766 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
1767 values[n++] = leader->total_time_running +
1768 atomic64_read(&leader->child_total_time_running);
1769 }
1770
1771 size = n * sizeof(u64);
1772
1773 if (copy_to_user(buf, values, size))
1774 return -EFAULT;
1775
1776 err = perf_counter_read_entry(leader, read_format, buf + size);
1777 if (err < 0)
1778 return err;
1779
1780 size += err;
1781
1782 list_for_each_entry(sub, &leader->sibling_list, list_entry) {
1783 err = perf_counter_read_entry(counter, read_format,
1784 buf + size);
1785 if (err < 0)
1786 return err;
1787
1788 size += err;
1789 }
1790
1791 return size;
1792}
1793
1794static int perf_counter_read_one(struct perf_counter *counter,
1795 u64 read_format, char __user *buf)
1796{
1797 u64 values[4];
1798 int n = 0;
1799
1800 values[n++] = perf_counter_read_value(counter);
1801 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
1802 values[n++] = counter->total_time_enabled +
1803 atomic64_read(&counter->child_total_time_enabled);
1804 }
1805 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
1806 values[n++] = counter->total_time_running +
1807 atomic64_read(&counter->child_total_time_running);
1808 }
1809 if (read_format & PERF_FORMAT_ID)
1810 values[n++] = primary_counter_id(counter);
1811
1812 if (copy_to_user(buf, values, n * sizeof(u64)))
1813 return -EFAULT;
1814
1815 return n * sizeof(u64);
1816}
1817
Thomas Gleixner0793a612008-12-04 20:12:29 +01001818/*
1819 * Read the performance counter - simple non blocking version for now
1820 */
1821static ssize_t
1822perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
1823{
Peter Zijlstra3dab77f2009-08-13 11:47:53 +02001824 u64 read_format = counter->attr.read_format;
1825 int ret;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001826
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001827 /*
1828 * Return end-of-file for a read on a counter that is in
1829 * error state (i.e. because it was pinned but it couldn't be
1830 * scheduled on to the CPU at some point).
1831 */
1832 if (counter->state == PERF_COUNTER_STATE_ERROR)
1833 return 0;
1834
Peter Zijlstra3dab77f2009-08-13 11:47:53 +02001835 if (count < perf_counter_read_size(counter))
1836 return -ENOSPC;
1837
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001838 WARN_ON_ONCE(counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001839 mutex_lock(&counter->child_mutex);
Peter Zijlstra3dab77f2009-08-13 11:47:53 +02001840 if (read_format & PERF_FORMAT_GROUP)
1841 ret = perf_counter_read_group(counter, read_format, buf);
1842 else
1843 ret = perf_counter_read_one(counter, read_format, buf);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001844 mutex_unlock(&counter->child_mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001845
Peter Zijlstra3dab77f2009-08-13 11:47:53 +02001846 return ret;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001847}
1848
1849static ssize_t
Thomas Gleixner0793a612008-12-04 20:12:29 +01001850perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
1851{
1852 struct perf_counter *counter = file->private_data;
1853
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001854 return perf_read_hw(counter, buf, count);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001855}
1856
1857static unsigned int perf_poll(struct file *file, poll_table *wait)
1858{
1859 struct perf_counter *counter = file->private_data;
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001860 struct perf_mmap_data *data;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001861 unsigned int events = POLL_HUP;
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001862
1863 rcu_read_lock();
1864 data = rcu_dereference(counter->data);
1865 if (data)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001866 events = atomic_xchg(&data->poll, 0);
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001867 rcu_read_unlock();
Thomas Gleixner0793a612008-12-04 20:12:29 +01001868
1869 poll_wait(file, &counter->waitq, wait);
1870
Thomas Gleixner0793a612008-12-04 20:12:29 +01001871 return events;
1872}
1873
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001874static void perf_counter_reset(struct perf_counter *counter)
1875{
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001876 (void)perf_counter_read(counter);
Paul Mackerras615a3f12009-05-11 15:50:21 +10001877 atomic64_set(&counter->count, 0);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001878 perf_counter_update_userpage(counter);
1879}
1880
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001881/*
1882 * Holding the top-level counter's child_mutex means that any
1883 * descendant process that has inherited this counter will block
1884 * in sync_child_counter if it goes to exit, thus satisfying the
1885 * task existence requirements of perf_counter_enable/disable.
1886 */
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001887static void perf_counter_for_each_child(struct perf_counter *counter,
1888 void (*func)(struct perf_counter *))
1889{
1890 struct perf_counter *child;
1891
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001892 WARN_ON_ONCE(counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001893 mutex_lock(&counter->child_mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001894 func(counter);
1895 list_for_each_entry(child, &counter->child_list, child_list)
1896 func(child);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001897 mutex_unlock(&counter->child_mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001898}
1899
1900static void perf_counter_for_each(struct perf_counter *counter,
1901 void (*func)(struct perf_counter *))
1902{
Peter Zijlstra75f937f2009-06-15 15:05:12 +02001903 struct perf_counter_context *ctx = counter->ctx;
1904 struct perf_counter *sibling;
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001905
Peter Zijlstra75f937f2009-06-15 15:05:12 +02001906 WARN_ON_ONCE(ctx->parent_ctx);
1907 mutex_lock(&ctx->mutex);
1908 counter = counter->group_leader;
1909
1910 perf_counter_for_each_child(counter, func);
1911 func(counter);
1912 list_for_each_entry(sibling, &counter->sibling_list, list_entry)
1913 perf_counter_for_each_child(counter, func);
1914 mutex_unlock(&ctx->mutex);
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001915}
1916
Peter Zijlstra08247e32009-06-02 16:46:57 +02001917static int perf_counter_period(struct perf_counter *counter, u64 __user *arg)
1918{
1919 struct perf_counter_context *ctx = counter->ctx;
1920 unsigned long size;
1921 int ret = 0;
1922 u64 value;
1923
Peter Zijlstra0d486962009-06-02 19:22:16 +02001924 if (!counter->attr.sample_period)
Peter Zijlstra08247e32009-06-02 16:46:57 +02001925 return -EINVAL;
1926
1927 size = copy_from_user(&value, arg, sizeof(value));
1928 if (size != sizeof(value))
1929 return -EFAULT;
1930
1931 if (!value)
1932 return -EINVAL;
1933
1934 spin_lock_irq(&ctx->lock);
Peter Zijlstra0d486962009-06-02 19:22:16 +02001935 if (counter->attr.freq) {
Peter Zijlstradf58ab22009-06-11 11:25:05 +02001936 if (value > sysctl_perf_counter_sample_rate) {
Peter Zijlstra08247e32009-06-02 16:46:57 +02001937 ret = -EINVAL;
1938 goto unlock;
1939 }
1940
Peter Zijlstra0d486962009-06-02 19:22:16 +02001941 counter->attr.sample_freq = value;
Peter Zijlstra08247e32009-06-02 16:46:57 +02001942 } else {
Peter Zijlstra0d486962009-06-02 19:22:16 +02001943 counter->attr.sample_period = value;
Peter Zijlstra08247e32009-06-02 16:46:57 +02001944 counter->hw.sample_period = value;
Peter Zijlstra08247e32009-06-02 16:46:57 +02001945 }
1946unlock:
1947 spin_unlock_irq(&ctx->lock);
1948
1949 return ret;
1950}
1951
Paul Mackerrasd859e292009-01-17 18:10:22 +11001952static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1953{
1954 struct perf_counter *counter = file->private_data;
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001955 void (*func)(struct perf_counter *);
1956 u32 flags = arg;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001957
1958 switch (cmd) {
1959 case PERF_COUNTER_IOC_ENABLE:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001960 func = perf_counter_enable;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001961 break;
1962 case PERF_COUNTER_IOC_DISABLE:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001963 func = perf_counter_disable;
Peter Zijlstra79f14642009-04-06 11:45:07 +02001964 break;
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001965 case PERF_COUNTER_IOC_RESET:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001966 func = perf_counter_reset;
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001967 break;
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001968
1969 case PERF_COUNTER_IOC_REFRESH:
1970 return perf_counter_refresh(counter, arg);
Peter Zijlstra08247e32009-06-02 16:46:57 +02001971
1972 case PERF_COUNTER_IOC_PERIOD:
1973 return perf_counter_period(counter, (u64 __user *)arg);
1974
Paul Mackerrasd859e292009-01-17 18:10:22 +11001975 default:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001976 return -ENOTTY;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001977 }
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001978
1979 if (flags & PERF_IOC_FLAG_GROUP)
1980 perf_counter_for_each(counter, func);
1981 else
1982 perf_counter_for_each_child(counter, func);
1983
1984 return 0;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001985}
1986
Peter Zijlstra771d7cd2009-05-25 14:45:26 +02001987int perf_counter_task_enable(void)
1988{
1989 struct perf_counter *counter;
1990
1991 mutex_lock(&current->perf_counter_mutex);
1992 list_for_each_entry(counter, &current->perf_counter_list, owner_entry)
1993 perf_counter_for_each_child(counter, perf_counter_enable);
1994 mutex_unlock(&current->perf_counter_mutex);
1995
1996 return 0;
1997}
1998
1999int perf_counter_task_disable(void)
2000{
2001 struct perf_counter *counter;
2002
2003 mutex_lock(&current->perf_counter_mutex);
2004 list_for_each_entry(counter, &current->perf_counter_list, owner_entry)
2005 perf_counter_for_each_child(counter, perf_counter_disable);
2006 mutex_unlock(&current->perf_counter_mutex);
2007
2008 return 0;
2009}
2010
Peter Zijlstra194002b2009-06-22 16:35:24 +02002011static int perf_counter_index(struct perf_counter *counter)
2012{
2013 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
2014 return 0;
2015
2016 return counter->hw.idx + 1 - PERF_COUNTER_INDEX_OFFSET;
2017}
2018
Peter Zijlstra38ff6672009-03-30 19:07:03 +02002019/*
2020 * Callers need to ensure there can be no nesting of this function, otherwise
2021 * the seqlock logic goes bad. We can not serialize this because the arch
2022 * code calls this from NMI context.
2023 */
2024void perf_counter_update_userpage(struct perf_counter *counter)
Paul Mackerras37d81822009-03-23 18:22:08 +01002025{
Peter Zijlstra38ff6672009-03-30 19:07:03 +02002026 struct perf_counter_mmap_page *userpg;
Ingo Molnar22a4f652009-06-01 10:13:37 +02002027 struct perf_mmap_data *data;
Peter Zijlstra38ff6672009-03-30 19:07:03 +02002028
2029 rcu_read_lock();
2030 data = rcu_dereference(counter->data);
2031 if (!data)
2032 goto unlock;
2033
2034 userpg = data->user_page;
Paul Mackerras37d81822009-03-23 18:22:08 +01002035
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002036 /*
2037 * Disable preemption so as to not let the corresponding user-space
2038 * spin too long if we get preempted.
2039 */
2040 preempt_disable();
Paul Mackerras37d81822009-03-23 18:22:08 +01002041 ++userpg->lock;
Peter Zijlstra92f22a32009-04-02 11:12:04 +02002042 barrier();
Peter Zijlstra194002b2009-06-22 16:35:24 +02002043 userpg->index = perf_counter_index(counter);
Paul Mackerras37d81822009-03-23 18:22:08 +01002044 userpg->offset = atomic64_read(&counter->count);
2045 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
2046 userpg->offset -= atomic64_read(&counter->hw.prev_count);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002047
Peter Zijlstra7f8b4e42009-06-22 14:34:35 +02002048 userpg->time_enabled = counter->total_time_enabled +
2049 atomic64_read(&counter->child_total_time_enabled);
2050
2051 userpg->time_running = counter->total_time_running +
2052 atomic64_read(&counter->child_total_time_running);
2053
Peter Zijlstra92f22a32009-04-02 11:12:04 +02002054 barrier();
Paul Mackerras37d81822009-03-23 18:22:08 +01002055 ++userpg->lock;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002056 preempt_enable();
Peter Zijlstra38ff6672009-03-30 19:07:03 +02002057unlock:
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002058 rcu_read_unlock();
Paul Mackerras37d81822009-03-23 18:22:08 +01002059}
2060
2061static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
2062{
2063 struct perf_counter *counter = vma->vm_file->private_data;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002064 struct perf_mmap_data *data;
2065 int ret = VM_FAULT_SIGBUS;
Paul Mackerras37d81822009-03-23 18:22:08 +01002066
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002067 if (vmf->flags & FAULT_FLAG_MKWRITE) {
2068 if (vmf->pgoff == 0)
2069 ret = 0;
2070 return ret;
2071 }
2072
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002073 rcu_read_lock();
2074 data = rcu_dereference(counter->data);
2075 if (!data)
2076 goto unlock;
Paul Mackerras37d81822009-03-23 18:22:08 +01002077
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002078 if (vmf->pgoff == 0) {
2079 vmf->page = virt_to_page(data->user_page);
2080 } else {
2081 int nr = vmf->pgoff - 1;
2082
2083 if ((unsigned)nr > data->nr_pages)
2084 goto unlock;
2085
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002086 if (vmf->flags & FAULT_FLAG_WRITE)
2087 goto unlock;
2088
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002089 vmf->page = virt_to_page(data->data_pages[nr]);
2090 }
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002091
Paul Mackerras37d81822009-03-23 18:22:08 +01002092 get_page(vmf->page);
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002093 vmf->page->mapping = vma->vm_file->f_mapping;
2094 vmf->page->index = vmf->pgoff;
2095
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002096 ret = 0;
2097unlock:
2098 rcu_read_unlock();
2099
2100 return ret;
2101}
2102
2103static int perf_mmap_data_alloc(struct perf_counter *counter, int nr_pages)
2104{
2105 struct perf_mmap_data *data;
2106 unsigned long size;
2107 int i;
2108
2109 WARN_ON(atomic_read(&counter->mmap_count));
2110
2111 size = sizeof(struct perf_mmap_data);
2112 size += nr_pages * sizeof(void *);
2113
2114 data = kzalloc(size, GFP_KERNEL);
2115 if (!data)
2116 goto fail;
2117
2118 data->user_page = (void *)get_zeroed_page(GFP_KERNEL);
2119 if (!data->user_page)
2120 goto fail_user_page;
2121
2122 for (i = 0; i < nr_pages; i++) {
2123 data->data_pages[i] = (void *)get_zeroed_page(GFP_KERNEL);
2124 if (!data->data_pages[i])
2125 goto fail_data_pages;
2126 }
2127
2128 data->nr_pages = nr_pages;
Peter Zijlstra22c15582009-05-05 17:50:25 +02002129 atomic_set(&data->lock, -1);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002130
2131 rcu_assign_pointer(counter->data, data);
2132
Paul Mackerras37d81822009-03-23 18:22:08 +01002133 return 0;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002134
2135fail_data_pages:
2136 for (i--; i >= 0; i--)
2137 free_page((unsigned long)data->data_pages[i]);
2138
2139 free_page((unsigned long)data->user_page);
2140
2141fail_user_page:
2142 kfree(data);
2143
2144fail:
2145 return -ENOMEM;
2146}
2147
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002148static void perf_mmap_free_page(unsigned long addr)
2149{
Kevin Cernekee5bfd7562009-07-05 12:08:19 -07002150 struct page *page = virt_to_page((void *)addr);
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002151
2152 page->mapping = NULL;
2153 __free_page(page);
2154}
2155
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002156static void __perf_mmap_data_free(struct rcu_head *rcu_head)
2157{
Ingo Molnar22a4f652009-06-01 10:13:37 +02002158 struct perf_mmap_data *data;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002159 int i;
2160
Ingo Molnar22a4f652009-06-01 10:13:37 +02002161 data = container_of(rcu_head, struct perf_mmap_data, rcu_head);
2162
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002163 perf_mmap_free_page((unsigned long)data->user_page);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002164 for (i = 0; i < data->nr_pages; i++)
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002165 perf_mmap_free_page((unsigned long)data->data_pages[i]);
2166
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002167 kfree(data);
2168}
2169
2170static void perf_mmap_data_free(struct perf_counter *counter)
2171{
2172 struct perf_mmap_data *data = counter->data;
2173
2174 WARN_ON(atomic_read(&counter->mmap_count));
2175
2176 rcu_assign_pointer(counter->data, NULL);
2177 call_rcu(&data->rcu_head, __perf_mmap_data_free);
2178}
2179
2180static void perf_mmap_open(struct vm_area_struct *vma)
2181{
2182 struct perf_counter *counter = vma->vm_file->private_data;
2183
2184 atomic_inc(&counter->mmap_count);
2185}
2186
2187static void perf_mmap_close(struct vm_area_struct *vma)
2188{
2189 struct perf_counter *counter = vma->vm_file->private_data;
2190
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10002191 WARN_ON_ONCE(counter->ctx->parent_ctx);
Ingo Molnar22a4f652009-06-01 10:13:37 +02002192 if (atomic_dec_and_mutex_lock(&counter->mmap_count, &counter->mmap_mutex)) {
Peter Zijlstra789f90f2009-05-15 15:19:27 +02002193 struct user_struct *user = current_user();
2194
2195 atomic_long_sub(counter->data->nr_pages + 1, &user->locked_vm);
Peter Zijlstrac5078f72009-05-05 17:50:24 +02002196 vma->vm_mm->locked_vm -= counter->data->nr_locked;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002197 perf_mmap_data_free(counter);
2198 mutex_unlock(&counter->mmap_mutex);
2199 }
Paul Mackerras37d81822009-03-23 18:22:08 +01002200}
2201
2202static struct vm_operations_struct perf_mmap_vmops = {
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002203 .open = perf_mmap_open,
2204 .close = perf_mmap_close,
2205 .fault = perf_mmap_fault,
2206 .page_mkwrite = perf_mmap_fault,
Paul Mackerras37d81822009-03-23 18:22:08 +01002207};
2208
2209static int perf_mmap(struct file *file, struct vm_area_struct *vma)
2210{
2211 struct perf_counter *counter = file->private_data;
Ingo Molnar22a4f652009-06-01 10:13:37 +02002212 unsigned long user_locked, user_lock_limit;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02002213 struct user_struct *user = current_user();
Ingo Molnar22a4f652009-06-01 10:13:37 +02002214 unsigned long locked, lock_limit;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002215 unsigned long vma_size;
2216 unsigned long nr_pages;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02002217 long user_extra, extra;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002218 int ret = 0;
Paul Mackerras37d81822009-03-23 18:22:08 +01002219
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002220 if (!(vma->vm_flags & VM_SHARED))
Paul Mackerras37d81822009-03-23 18:22:08 +01002221 return -EINVAL;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002222
2223 vma_size = vma->vm_end - vma->vm_start;
2224 nr_pages = (vma_size / PAGE_SIZE) - 1;
2225
Peter Zijlstra7730d862009-03-25 12:48:31 +01002226 /*
2227 * If we have data pages ensure they're a power-of-two number, so we
2228 * can do bitmasks instead of modulo.
2229 */
2230 if (nr_pages != 0 && !is_power_of_2(nr_pages))
Paul Mackerras37d81822009-03-23 18:22:08 +01002231 return -EINVAL;
2232
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002233 if (vma_size != PAGE_SIZE * (1 + nr_pages))
Paul Mackerras37d81822009-03-23 18:22:08 +01002234 return -EINVAL;
2235
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002236 if (vma->vm_pgoff != 0)
2237 return -EINVAL;
Paul Mackerras37d81822009-03-23 18:22:08 +01002238
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10002239 WARN_ON_ONCE(counter->ctx->parent_ctx);
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02002240 mutex_lock(&counter->mmap_mutex);
2241 if (atomic_inc_not_zero(&counter->mmap_count)) {
2242 if (nr_pages != counter->data->nr_pages)
2243 ret = -EINVAL;
2244 goto unlock;
2245 }
2246
Peter Zijlstra789f90f2009-05-15 15:19:27 +02002247 user_extra = nr_pages + 1;
2248 user_lock_limit = sysctl_perf_counter_mlock >> (PAGE_SHIFT - 10);
Ingo Molnara3862d32009-05-24 09:02:37 +02002249
2250 /*
2251 * Increase the limit linearly with more CPUs:
2252 */
2253 user_lock_limit *= num_online_cpus();
2254
Peter Zijlstra789f90f2009-05-15 15:19:27 +02002255 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
Peter Zijlstrac5078f72009-05-05 17:50:24 +02002256
Peter Zijlstra789f90f2009-05-15 15:19:27 +02002257 extra = 0;
2258 if (user_locked > user_lock_limit)
2259 extra = user_locked - user_lock_limit;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002260
2261 lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
2262 lock_limit >>= PAGE_SHIFT;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02002263 locked = vma->vm_mm->locked_vm + extra;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002264
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02002265 if ((locked > lock_limit) && !capable(CAP_IPC_LOCK)) {
2266 ret = -EPERM;
2267 goto unlock;
2268 }
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002269
2270 WARN_ON(counter->data);
2271 ret = perf_mmap_data_alloc(counter, nr_pages);
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02002272 if (ret)
2273 goto unlock;
2274
2275 atomic_set(&counter->mmap_count, 1);
Peter Zijlstra789f90f2009-05-15 15:19:27 +02002276 atomic_long_add(user_extra, &user->locked_vm);
Peter Zijlstrac5078f72009-05-05 17:50:24 +02002277 vma->vm_mm->locked_vm += extra;
2278 counter->data->nr_locked = extra;
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002279 if (vma->vm_flags & VM_WRITE)
2280 counter->data->writable = 1;
2281
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02002282unlock:
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002283 mutex_unlock(&counter->mmap_mutex);
Paul Mackerras37d81822009-03-23 18:22:08 +01002284
Paul Mackerras37d81822009-03-23 18:22:08 +01002285 vma->vm_flags |= VM_RESERVED;
2286 vma->vm_ops = &perf_mmap_vmops;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002287
2288 return ret;
Paul Mackerras37d81822009-03-23 18:22:08 +01002289}
2290
Peter Zijlstra3c446b3d2009-04-06 11:45:01 +02002291static int perf_fasync(int fd, struct file *filp, int on)
2292{
Peter Zijlstra3c446b3d2009-04-06 11:45:01 +02002293 struct inode *inode = filp->f_path.dentry->d_inode;
Ingo Molnar22a4f652009-06-01 10:13:37 +02002294 struct perf_counter *counter = filp->private_data;
Peter Zijlstra3c446b3d2009-04-06 11:45:01 +02002295 int retval;
2296
2297 mutex_lock(&inode->i_mutex);
2298 retval = fasync_helper(fd, filp, on, &counter->fasync);
2299 mutex_unlock(&inode->i_mutex);
2300
2301 if (retval < 0)
2302 return retval;
2303
2304 return 0;
2305}
2306
Thomas Gleixner0793a612008-12-04 20:12:29 +01002307static const struct file_operations perf_fops = {
2308 .release = perf_release,
2309 .read = perf_read,
2310 .poll = perf_poll,
Paul Mackerrasd859e292009-01-17 18:10:22 +11002311 .unlocked_ioctl = perf_ioctl,
2312 .compat_ioctl = perf_ioctl,
Paul Mackerras37d81822009-03-23 18:22:08 +01002313 .mmap = perf_mmap,
Peter Zijlstra3c446b3d2009-04-06 11:45:01 +02002314 .fasync = perf_fasync,
Thomas Gleixner0793a612008-12-04 20:12:29 +01002315};
2316
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002317/*
Peter Zijlstra925d5192009-03-30 19:07:02 +02002318 * Perf counter wakeup
2319 *
2320 * If there's data, ensure we set the poll() state and publish everything
2321 * to user-space before waking everybody up.
2322 */
2323
2324void perf_counter_wakeup(struct perf_counter *counter)
2325{
Peter Zijlstra925d5192009-03-30 19:07:02 +02002326 wake_up_all(&counter->waitq);
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002327
2328 if (counter->pending_kill) {
2329 kill_fasync(&counter->fasync, SIGIO, counter->pending_kill);
2330 counter->pending_kill = 0;
2331 }
Peter Zijlstra925d5192009-03-30 19:07:02 +02002332}
2333
2334/*
2335 * Pending wakeups
2336 *
2337 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
2338 *
2339 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
2340 * single linked list and use cmpxchg() to add entries lockless.
2341 */
2342
Peter Zijlstra79f14642009-04-06 11:45:07 +02002343static void perf_pending_counter(struct perf_pending_entry *entry)
2344{
2345 struct perf_counter *counter = container_of(entry,
2346 struct perf_counter, pending);
2347
2348 if (counter->pending_disable) {
2349 counter->pending_disable = 0;
Peter Zijlstra970892a2009-08-13 11:47:54 +02002350 __perf_counter_disable(counter);
Peter Zijlstra79f14642009-04-06 11:45:07 +02002351 }
2352
2353 if (counter->pending_wakeup) {
2354 counter->pending_wakeup = 0;
2355 perf_counter_wakeup(counter);
2356 }
2357}
2358
Peter Zijlstra671dec52009-04-06 11:45:02 +02002359#define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
Peter Zijlstra925d5192009-03-30 19:07:02 +02002360
Peter Zijlstra671dec52009-04-06 11:45:02 +02002361static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = {
Peter Zijlstra925d5192009-03-30 19:07:02 +02002362 PENDING_TAIL,
2363};
2364
Peter Zijlstra671dec52009-04-06 11:45:02 +02002365static void perf_pending_queue(struct perf_pending_entry *entry,
2366 void (*func)(struct perf_pending_entry *))
Peter Zijlstra925d5192009-03-30 19:07:02 +02002367{
Peter Zijlstra671dec52009-04-06 11:45:02 +02002368 struct perf_pending_entry **head;
Peter Zijlstra925d5192009-03-30 19:07:02 +02002369
Peter Zijlstra671dec52009-04-06 11:45:02 +02002370 if (cmpxchg(&entry->next, NULL, PENDING_TAIL) != NULL)
Peter Zijlstra925d5192009-03-30 19:07:02 +02002371 return;
2372
Peter Zijlstra671dec52009-04-06 11:45:02 +02002373 entry->func = func;
2374
2375 head = &get_cpu_var(perf_pending_head);
Peter Zijlstra925d5192009-03-30 19:07:02 +02002376
2377 do {
Peter Zijlstra671dec52009-04-06 11:45:02 +02002378 entry->next = *head;
2379 } while (cmpxchg(head, entry->next, entry) != entry->next);
Peter Zijlstra925d5192009-03-30 19:07:02 +02002380
2381 set_perf_counter_pending();
2382
Peter Zijlstra671dec52009-04-06 11:45:02 +02002383 put_cpu_var(perf_pending_head);
Peter Zijlstra925d5192009-03-30 19:07:02 +02002384}
2385
2386static int __perf_pending_run(void)
2387{
Peter Zijlstra671dec52009-04-06 11:45:02 +02002388 struct perf_pending_entry *list;
Peter Zijlstra925d5192009-03-30 19:07:02 +02002389 int nr = 0;
2390
Peter Zijlstra671dec52009-04-06 11:45:02 +02002391 list = xchg(&__get_cpu_var(perf_pending_head), PENDING_TAIL);
Peter Zijlstra925d5192009-03-30 19:07:02 +02002392 while (list != PENDING_TAIL) {
Peter Zijlstra671dec52009-04-06 11:45:02 +02002393 void (*func)(struct perf_pending_entry *);
2394 struct perf_pending_entry *entry = list;
Peter Zijlstra925d5192009-03-30 19:07:02 +02002395
2396 list = list->next;
2397
Peter Zijlstra671dec52009-04-06 11:45:02 +02002398 func = entry->func;
2399 entry->next = NULL;
Peter Zijlstra925d5192009-03-30 19:07:02 +02002400 /*
2401 * Ensure we observe the unqueue before we issue the wakeup,
2402 * so that we won't be waiting forever.
2403 * -- see perf_not_pending().
2404 */
2405 smp_wmb();
2406
Peter Zijlstra671dec52009-04-06 11:45:02 +02002407 func(entry);
Peter Zijlstra925d5192009-03-30 19:07:02 +02002408 nr++;
2409 }
2410
2411 return nr;
2412}
2413
2414static inline int perf_not_pending(struct perf_counter *counter)
2415{
2416 /*
2417 * If we flush on whatever cpu we run, there is a chance we don't
2418 * need to wait.
2419 */
2420 get_cpu();
2421 __perf_pending_run();
2422 put_cpu();
2423
2424 /*
2425 * Ensure we see the proper queue state before going to sleep
2426 * so that we do not miss the wakeup. -- see perf_pending_handle()
2427 */
2428 smp_rmb();
Peter Zijlstra671dec52009-04-06 11:45:02 +02002429 return counter->pending.next == NULL;
Peter Zijlstra925d5192009-03-30 19:07:02 +02002430}
2431
2432static void perf_pending_sync(struct perf_counter *counter)
2433{
2434 wait_event(counter->waitq, perf_not_pending(counter));
2435}
2436
2437void perf_counter_do_pending(void)
2438{
2439 __perf_pending_run();
2440}
2441
2442/*
Peter Zijlstra394ee072009-03-30 19:07:14 +02002443 * Callchain support -- arch specific
2444 */
2445
Peter Zijlstra9c03d882009-04-06 11:45:00 +02002446__weak struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
Peter Zijlstra394ee072009-03-30 19:07:14 +02002447{
2448 return NULL;
2449}
2450
2451/*
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002452 * Output
2453 */
2454
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002455struct perf_output_handle {
2456 struct perf_counter *counter;
2457 struct perf_mmap_data *data;
Peter Zijlstra8e3747c2009-06-02 16:16:02 +02002458 unsigned long head;
2459 unsigned long offset;
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002460 int nmi;
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002461 int sample;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002462 int locked;
2463 unsigned long flags;
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002464};
2465
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002466static bool perf_output_space(struct perf_mmap_data *data,
2467 unsigned int offset, unsigned int head)
2468{
2469 unsigned long tail;
2470 unsigned long mask;
2471
2472 if (!data->writable)
2473 return true;
2474
2475 mask = (data->nr_pages << PAGE_SHIFT) - 1;
2476 /*
2477 * Userspace could choose to issue a mb() before updating the tail
2478 * pointer. So that all reads will be completed before the write is
2479 * issued.
2480 */
2481 tail = ACCESS_ONCE(data->user_page->data_tail);
2482 smp_rmb();
2483
2484 offset = (offset - tail) & mask;
2485 head = (head - tail) & mask;
2486
2487 if ((int)(head - offset) < 0)
2488 return false;
2489
2490 return true;
2491}
2492
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002493static void perf_output_wakeup(struct perf_output_handle *handle)
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002494{
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002495 atomic_set(&handle->data->poll, POLL_IN);
2496
Peter Zijlstra671dec52009-04-06 11:45:02 +02002497 if (handle->nmi) {
Peter Zijlstra79f14642009-04-06 11:45:07 +02002498 handle->counter->pending_wakeup = 1;
Peter Zijlstra671dec52009-04-06 11:45:02 +02002499 perf_pending_queue(&handle->counter->pending,
Peter Zijlstra79f14642009-04-06 11:45:07 +02002500 perf_pending_counter);
Peter Zijlstra671dec52009-04-06 11:45:02 +02002501 } else
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002502 perf_counter_wakeup(handle->counter);
2503}
2504
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002505/*
2506 * Curious locking construct.
2507 *
2508 * We need to ensure a later event doesn't publish a head when a former
2509 * event isn't done writing. However since we need to deal with NMIs we
2510 * cannot fully serialize things.
2511 *
2512 * What we do is serialize between CPUs so we only have to deal with NMI
2513 * nesting on a single CPU.
2514 *
2515 * We only publish the head (and generate a wakeup) when the outer-most
2516 * event completes.
2517 */
2518static void perf_output_lock(struct perf_output_handle *handle)
2519{
2520 struct perf_mmap_data *data = handle->data;
2521 int cpu;
2522
2523 handle->locked = 0;
2524
2525 local_irq_save(handle->flags);
2526 cpu = smp_processor_id();
2527
2528 if (in_nmi() && atomic_read(&data->lock) == cpu)
2529 return;
2530
Peter Zijlstra22c15582009-05-05 17:50:25 +02002531 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002532 cpu_relax();
2533
2534 handle->locked = 1;
2535}
2536
2537static void perf_output_unlock(struct perf_output_handle *handle)
2538{
2539 struct perf_mmap_data *data = handle->data;
Peter Zijlstra8e3747c2009-06-02 16:16:02 +02002540 unsigned long head;
2541 int cpu;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002542
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002543 data->done_head = data->head;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002544
2545 if (!handle->locked)
2546 goto out;
2547
2548again:
2549 /*
2550 * The xchg implies a full barrier that ensures all writes are done
2551 * before we publish the new head, matched by a rmb() in userspace when
2552 * reading this position.
2553 */
Peter Zijlstra8e3747c2009-06-02 16:16:02 +02002554 while ((head = atomic_long_xchg(&data->done_head, 0)))
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002555 data->user_page->data_head = head;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002556
2557 /*
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002558 * NMI can happen here, which means we can miss a done_head update.
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002559 */
2560
Peter Zijlstra22c15582009-05-05 17:50:25 +02002561 cpu = atomic_xchg(&data->lock, -1);
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002562 WARN_ON_ONCE(cpu != smp_processor_id());
2563
2564 /*
2565 * Therefore we have to validate we did not indeed do so.
2566 */
Peter Zijlstra8e3747c2009-06-02 16:16:02 +02002567 if (unlikely(atomic_long_read(&data->done_head))) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002568 /*
2569 * Since we had it locked, we can lock it again.
2570 */
Peter Zijlstra22c15582009-05-05 17:50:25 +02002571 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002572 cpu_relax();
2573
2574 goto again;
2575 }
2576
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002577 if (atomic_xchg(&data->wakeup, 0))
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002578 perf_output_wakeup(handle);
2579out:
2580 local_irq_restore(handle->flags);
2581}
2582
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002583static void perf_output_copy(struct perf_output_handle *handle,
Peter Zijlstra089dd792009-06-05 14:04:55 +02002584 const void *buf, unsigned int len)
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002585{
2586 unsigned int pages_mask;
2587 unsigned int offset;
2588 unsigned int size;
2589 void **pages;
2590
2591 offset = handle->offset;
2592 pages_mask = handle->data->nr_pages - 1;
2593 pages = handle->data->data_pages;
2594
2595 do {
2596 unsigned int page_offset;
2597 int nr;
2598
2599 nr = (offset >> PAGE_SHIFT) & pages_mask;
2600 page_offset = offset & (PAGE_SIZE - 1);
2601 size = min_t(unsigned int, PAGE_SIZE - page_offset, len);
2602
2603 memcpy(pages[nr] + page_offset, buf, size);
2604
2605 len -= size;
2606 buf += size;
2607 offset += size;
2608 } while (len);
2609
2610 handle->offset = offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01002611
Peter Zijlstra53020fe2009-05-13 21:26:19 +02002612 /*
2613 * Check we didn't copy past our reservation window, taking the
2614 * possible unsigned int wrap into account.
2615 */
Peter Zijlstra8e3747c2009-06-02 16:16:02 +02002616 WARN_ON_ONCE(((long)(handle->head - handle->offset)) < 0);
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002617}
2618
Peter Zijlstra5c148192009-03-25 12:30:23 +01002619#define perf_output_put(handle, x) \
2620 perf_output_copy((handle), &(x), sizeof(x))
2621
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002622static int perf_output_begin(struct perf_output_handle *handle,
2623 struct perf_counter *counter, unsigned int size,
2624 int nmi, int sample)
2625{
2626 struct perf_mmap_data *data;
2627 unsigned int offset, head;
2628 int have_lost;
2629 struct {
2630 struct perf_event_header header;
2631 u64 id;
2632 u64 lost;
2633 } lost_event;
2634
2635 /*
2636 * For inherited counters we send all the output towards the parent.
2637 */
2638 if (counter->parent)
2639 counter = counter->parent;
2640
2641 rcu_read_lock();
2642 data = rcu_dereference(counter->data);
2643 if (!data)
2644 goto out;
2645
2646 handle->data = data;
2647 handle->counter = counter;
2648 handle->nmi = nmi;
2649 handle->sample = sample;
2650
2651 if (!data->nr_pages)
2652 goto fail;
2653
2654 have_lost = atomic_read(&data->lost);
2655 if (have_lost)
2656 size += sizeof(lost_event);
2657
2658 perf_output_lock(handle);
2659
2660 do {
2661 offset = head = atomic_long_read(&data->head);
2662 head += size;
2663 if (unlikely(!perf_output_space(data, offset, head)))
2664 goto fail;
2665 } while (atomic_long_cmpxchg(&data->head, offset, head) != offset);
2666
2667 handle->offset = offset;
2668 handle->head = head;
2669
2670 if ((offset >> PAGE_SHIFT) != (head >> PAGE_SHIFT))
2671 atomic_set(&data->wakeup, 1);
2672
2673 if (have_lost) {
2674 lost_event.header.type = PERF_EVENT_LOST;
2675 lost_event.header.misc = 0;
2676 lost_event.header.size = sizeof(lost_event);
2677 lost_event.id = counter->id;
2678 lost_event.lost = atomic_xchg(&data->lost, 0);
2679
2680 perf_output_put(handle, lost_event);
2681 }
2682
2683 return 0;
2684
2685fail:
2686 atomic_inc(&data->lost);
2687 perf_output_unlock(handle);
2688out:
2689 rcu_read_unlock();
2690
2691 return -ENOSPC;
2692}
2693
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002694static void perf_output_end(struct perf_output_handle *handle)
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002695{
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002696 struct perf_counter *counter = handle->counter;
2697 struct perf_mmap_data *data = handle->data;
2698
Peter Zijlstra0d486962009-06-02 19:22:16 +02002699 int wakeup_events = counter->attr.wakeup_events;
Peter Zijlstrac4578102009-04-02 11:12:01 +02002700
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002701 if (handle->sample && wakeup_events) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002702 int events = atomic_inc_return(&data->events);
Peter Zijlstrac4578102009-04-02 11:12:01 +02002703 if (events >= wakeup_events) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002704 atomic_sub(wakeup_events, &data->events);
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002705 atomic_set(&data->wakeup, 1);
Peter Zijlstrac4578102009-04-02 11:12:01 +02002706 }
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002707 }
2708
2709 perf_output_unlock(handle);
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002710 rcu_read_unlock();
2711}
2712
Peter Zijlstra709e50c2009-06-02 14:13:15 +02002713static u32 perf_counter_pid(struct perf_counter *counter, struct task_struct *p)
2714{
2715 /*
2716 * only top level counters have the pid namespace they were created in
2717 */
2718 if (counter->parent)
2719 counter = counter->parent;
2720
2721 return task_tgid_nr_ns(p, counter->ns);
2722}
2723
2724static u32 perf_counter_tid(struct perf_counter *counter, struct task_struct *p)
2725{
2726 /*
2727 * only top level counters have the pid namespace they were created in
2728 */
2729 if (counter->parent)
2730 counter = counter->parent;
2731
2732 return task_pid_nr_ns(p, counter->ns);
2733}
2734
Peter Zijlstra3dab77f2009-08-13 11:47:53 +02002735static void perf_output_read_one(struct perf_output_handle *handle,
2736 struct perf_counter *counter)
2737{
2738 u64 read_format = counter->attr.read_format;
2739 u64 values[4];
2740 int n = 0;
2741
2742 values[n++] = atomic64_read(&counter->count);
2743 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
2744 values[n++] = counter->total_time_enabled +
2745 atomic64_read(&counter->child_total_time_enabled);
2746 }
2747 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
2748 values[n++] = counter->total_time_running +
2749 atomic64_read(&counter->child_total_time_running);
2750 }
2751 if (read_format & PERF_FORMAT_ID)
2752 values[n++] = primary_counter_id(counter);
2753
2754 perf_output_copy(handle, values, n * sizeof(u64));
2755}
2756
2757/*
2758 * XXX PERF_FORMAT_GROUP vs inherited counters seems difficult.
2759 */
2760static void perf_output_read_group(struct perf_output_handle *handle,
2761 struct perf_counter *counter)
2762{
2763 struct perf_counter *leader = counter->group_leader, *sub;
2764 u64 read_format = counter->attr.read_format;
2765 u64 values[5];
2766 int n = 0;
2767
2768 values[n++] = 1 + leader->nr_siblings;
2769
2770 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2771 values[n++] = leader->total_time_enabled;
2772
2773 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2774 values[n++] = leader->total_time_running;
2775
2776 if (leader != counter)
2777 leader->pmu->read(leader);
2778
2779 values[n++] = atomic64_read(&leader->count);
2780 if (read_format & PERF_FORMAT_ID)
2781 values[n++] = primary_counter_id(leader);
2782
2783 perf_output_copy(handle, values, n * sizeof(u64));
2784
2785 list_for_each_entry(sub, &leader->sibling_list, list_entry) {
2786 n = 0;
2787
2788 if (sub != counter)
2789 sub->pmu->read(sub);
2790
2791 values[n++] = atomic64_read(&sub->count);
2792 if (read_format & PERF_FORMAT_ID)
2793 values[n++] = primary_counter_id(sub);
2794
2795 perf_output_copy(handle, values, n * sizeof(u64));
2796 }
2797}
2798
2799static void perf_output_read(struct perf_output_handle *handle,
2800 struct perf_counter *counter)
2801{
2802 if (counter->attr.read_format & PERF_FORMAT_GROUP)
2803 perf_output_read_group(handle, counter);
2804 else
2805 perf_output_read_one(handle, counter);
2806}
2807
Ingo Molnar28402972009-08-13 10:13:22 +02002808void perf_counter_output(struct perf_counter *counter, int nmi,
Peter Zijlstradf1a1322009-06-10 21:02:22 +02002809 struct perf_sample_data *data)
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002810{
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002811 int ret;
Peter Zijlstra0d486962009-06-02 19:22:16 +02002812 u64 sample_type = counter->attr.sample_type;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002813 struct perf_output_handle handle;
2814 struct perf_event_header header;
2815 u64 ip;
Peter Zijlstra5c148192009-03-25 12:30:23 +01002816 struct {
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002817 u32 pid, tid;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002818 } tid_entry;
Peter Zijlstra394ee072009-03-30 19:07:14 +02002819 struct perf_callchain_entry *callchain = NULL;
2820 int callchain_size = 0;
Peter Zijlstra339f7c92009-04-06 11:45:06 +02002821 u64 time;
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002822 struct {
2823 u32 cpu, reserved;
2824 } cpu_entry;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002825
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02002826 header.type = PERF_EVENT_SAMPLE;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002827 header.size = sizeof(header);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002828
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02002829 header.misc = 0;
Peter Zijlstradf1a1322009-06-10 21:02:22 +02002830 header.misc |= perf_misc_flags(data->regs);
Peter Zijlstra6fab0192009-04-08 15:01:26 +02002831
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002832 if (sample_type & PERF_SAMPLE_IP) {
Peter Zijlstradf1a1322009-06-10 21:02:22 +02002833 ip = perf_instruction_pointer(data->regs);
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002834 header.size += sizeof(ip);
2835 }
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002836
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002837 if (sample_type & PERF_SAMPLE_TID) {
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002838 /* namespace issues */
Peter Zijlstra709e50c2009-06-02 14:13:15 +02002839 tid_entry.pid = perf_counter_pid(counter, current);
2840 tid_entry.tid = perf_counter_tid(counter, current);
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002841
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002842 header.size += sizeof(tid_entry);
2843 }
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002844
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002845 if (sample_type & PERF_SAMPLE_TIME) {
Peter Zijlstra4d855452009-04-08 15:01:32 +02002846 /*
2847 * Maybe do better on x86 and provide cpu_clock_nmi()
2848 */
2849 time = sched_clock();
2850
Peter Zijlstra4d855452009-04-08 15:01:32 +02002851 header.size += sizeof(u64);
2852 }
2853
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02002854 if (sample_type & PERF_SAMPLE_ADDR)
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002855 header.size += sizeof(u64);
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002856
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02002857 if (sample_type & PERF_SAMPLE_ID)
Peter Zijlstraa85f61a2009-05-08 18:52:23 +02002858 header.size += sizeof(u64);
Peter Zijlstraa85f61a2009-05-08 18:52:23 +02002859
Peter Zijlstra7f453c22009-07-21 13:19:40 +02002860 if (sample_type & PERF_SAMPLE_STREAM_ID)
2861 header.size += sizeof(u64);
2862
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002863 if (sample_type & PERF_SAMPLE_CPU) {
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002864 header.size += sizeof(cpu_entry);
2865
2866 cpu_entry.cpu = raw_smp_processor_id();
Arjan van de Ven0dc3d522009-07-21 00:55:05 -07002867 cpu_entry.reserved = 0;
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002868 }
2869
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02002870 if (sample_type & PERF_SAMPLE_PERIOD)
Peter Zijlstra689802b2009-06-05 15:05:43 +02002871 header.size += sizeof(u64);
Peter Zijlstra689802b2009-06-05 15:05:43 +02002872
Peter Zijlstra3dab77f2009-08-13 11:47:53 +02002873 if (sample_type & PERF_SAMPLE_READ)
2874 header.size += perf_counter_read_size(counter);
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002875
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002876 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
Peter Zijlstradf1a1322009-06-10 21:02:22 +02002877 callchain = perf_callchain(data->regs);
Peter Zijlstra394ee072009-03-30 19:07:14 +02002878
2879 if (callchain) {
Peter Zijlstra9c03d882009-04-06 11:45:00 +02002880 callchain_size = (1 + callchain->nr) * sizeof(u64);
Peter Zijlstra394ee072009-03-30 19:07:14 +02002881 header.size += callchain_size;
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02002882 } else
2883 header.size += sizeof(u64);
Peter Zijlstra394ee072009-03-30 19:07:14 +02002884 }
2885
Frederic Weisbecker3a43ce62009-08-08 04:26:37 +02002886 if (sample_type & PERF_SAMPLE_RAW) {
Peter Zijlstraa0445602009-08-10 11:16:52 +02002887 int size = sizeof(u32);
2888
2889 if (data->raw)
2890 size += data->raw->size;
2891 else
2892 size += sizeof(u32);
2893
2894 WARN_ON_ONCE(size & (sizeof(u64)-1));
2895 header.size += size;
Frederic Weisbeckerf413cdb2009-08-07 01:25:54 +02002896 }
2897
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002898 ret = perf_output_begin(&handle, counter, header.size, nmi, 1);
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002899 if (ret)
2900 return;
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002901
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002902 perf_output_put(&handle, header);
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002903
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002904 if (sample_type & PERF_SAMPLE_IP)
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002905 perf_output_put(&handle, ip);
2906
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002907 if (sample_type & PERF_SAMPLE_TID)
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002908 perf_output_put(&handle, tid_entry);
2909
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002910 if (sample_type & PERF_SAMPLE_TIME)
Peter Zijlstra4d855452009-04-08 15:01:32 +02002911 perf_output_put(&handle, time);
2912
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002913 if (sample_type & PERF_SAMPLE_ADDR)
Peter Zijlstradf1a1322009-06-10 21:02:22 +02002914 perf_output_put(&handle, data->addr);
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002915
Peter Zijlstra7f453c22009-07-21 13:19:40 +02002916 if (sample_type & PERF_SAMPLE_ID) {
2917 u64 id = primary_counter_id(counter);
2918
2919 perf_output_put(&handle, id);
2920 }
2921
2922 if (sample_type & PERF_SAMPLE_STREAM_ID)
Peter Zijlstraac4bcf82009-06-05 14:44:52 +02002923 perf_output_put(&handle, counter->id);
Peter Zijlstraa85f61a2009-05-08 18:52:23 +02002924
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002925 if (sample_type & PERF_SAMPLE_CPU)
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002926 perf_output_put(&handle, cpu_entry);
2927
Peter Zijlstra689802b2009-06-05 15:05:43 +02002928 if (sample_type & PERF_SAMPLE_PERIOD)
Peter Zijlstra9e350de2009-06-10 21:34:59 +02002929 perf_output_put(&handle, data->period);
Peter Zijlstra689802b2009-06-05 15:05:43 +02002930
Peter Zijlstra3dab77f2009-08-13 11:47:53 +02002931 if (sample_type & PERF_SAMPLE_READ)
2932 perf_output_read(&handle, counter);
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002933
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02002934 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
2935 if (callchain)
2936 perf_output_copy(&handle, callchain, callchain_size);
2937 else {
2938 u64 nr = 0;
2939 perf_output_put(&handle, nr);
2940 }
2941 }
Peter Zijlstra394ee072009-03-30 19:07:14 +02002942
Peter Zijlstraa0445602009-08-10 11:16:52 +02002943 if (sample_type & PERF_SAMPLE_RAW) {
2944 if (data->raw) {
2945 perf_output_put(&handle, data->raw->size);
2946 perf_output_copy(&handle, data->raw->data, data->raw->size);
2947 } else {
2948 struct {
2949 u32 size;
2950 u32 data;
2951 } raw = {
2952 .size = sizeof(u32),
2953 .data = 0,
2954 };
2955 perf_output_put(&handle, raw);
2956 }
2957 }
Frederic Weisbeckerf413cdb2009-08-07 01:25:54 +02002958
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002959 perf_output_end(&handle);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002960}
2961
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002962/*
Peter Zijlstra38b200d2009-06-23 20:13:11 +02002963 * read event
2964 */
2965
2966struct perf_read_event {
2967 struct perf_event_header header;
2968
2969 u32 pid;
2970 u32 tid;
Peter Zijlstra38b200d2009-06-23 20:13:11 +02002971};
2972
2973static void
2974perf_counter_read_event(struct perf_counter *counter,
2975 struct task_struct *task)
2976{
2977 struct perf_output_handle handle;
2978 struct perf_read_event event = {
2979 .header = {
2980 .type = PERF_EVENT_READ,
2981 .misc = 0,
Peter Zijlstra3dab77f2009-08-13 11:47:53 +02002982 .size = sizeof(event) + perf_counter_read_size(counter),
Peter Zijlstra38b200d2009-06-23 20:13:11 +02002983 },
2984 .pid = perf_counter_pid(counter, task),
2985 .tid = perf_counter_tid(counter, task),
Peter Zijlstra38b200d2009-06-23 20:13:11 +02002986 };
Peter Zijlstra3dab77f2009-08-13 11:47:53 +02002987 int ret;
Peter Zijlstra38b200d2009-06-23 20:13:11 +02002988
2989 ret = perf_output_begin(&handle, counter, event.header.size, 0, 0);
2990 if (ret)
2991 return;
2992
Peter Zijlstra3dab77f2009-08-13 11:47:53 +02002993 perf_output_put(&handle, event);
2994 perf_output_read(&handle, counter);
2995
Peter Zijlstra38b200d2009-06-23 20:13:11 +02002996 perf_output_end(&handle);
2997}
2998
2999/*
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02003000 * task tracking -- fork/exit
3001 *
3002 * enabled by: attr.comm | attr.mmap | attr.task
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003003 */
3004
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02003005struct perf_task_event {
Peter Zijlstra3a80b4a2009-08-07 19:49:01 +02003006 struct task_struct *task;
3007 struct perf_counter_context *task_ctx;
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003008
3009 struct {
3010 struct perf_event_header header;
3011
3012 u32 pid;
3013 u32 ppid;
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02003014 u32 tid;
3015 u32 ptid;
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003016 } event;
3017};
3018
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02003019static void perf_counter_task_output(struct perf_counter *counter,
3020 struct perf_task_event *task_event)
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003021{
3022 struct perf_output_handle handle;
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02003023 int size = task_event->event.header.size;
3024 struct task_struct *task = task_event->task;
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003025 int ret = perf_output_begin(&handle, counter, size, 0, 0);
3026
3027 if (ret)
3028 return;
3029
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02003030 task_event->event.pid = perf_counter_pid(counter, task);
Peter Zijlstra94d5d1b2009-08-13 16:14:42 +02003031 task_event->event.ppid = perf_counter_pid(counter, current);
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003032
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02003033 task_event->event.tid = perf_counter_tid(counter, task);
Peter Zijlstra94d5d1b2009-08-13 16:14:42 +02003034 task_event->event.ptid = perf_counter_tid(counter, current);
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02003035
3036 perf_output_put(&handle, task_event->event);
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003037 perf_output_end(&handle);
3038}
3039
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02003040static int perf_counter_task_match(struct perf_counter *counter)
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003041{
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02003042 if (counter->attr.comm || counter->attr.mmap || counter->attr.task)
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003043 return 1;
3044
3045 return 0;
3046}
3047
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02003048static void perf_counter_task_ctx(struct perf_counter_context *ctx,
3049 struct perf_task_event *task_event)
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003050{
3051 struct perf_counter *counter;
3052
3053 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
3054 return;
3055
3056 rcu_read_lock();
3057 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02003058 if (perf_counter_task_match(counter))
3059 perf_counter_task_output(counter, task_event);
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003060 }
3061 rcu_read_unlock();
3062}
3063
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02003064static void perf_counter_task_event(struct perf_task_event *task_event)
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003065{
3066 struct perf_cpu_context *cpuctx;
Peter Zijlstra3a80b4a2009-08-07 19:49:01 +02003067 struct perf_counter_context *ctx = task_event->task_ctx;
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003068
3069 cpuctx = &get_cpu_var(perf_cpu_context);
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02003070 perf_counter_task_ctx(&cpuctx->ctx, task_event);
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003071 put_cpu_var(perf_cpu_context);
3072
3073 rcu_read_lock();
Peter Zijlstra3a80b4a2009-08-07 19:49:01 +02003074 if (!ctx)
3075 ctx = rcu_dereference(task_event->task->perf_counter_ctxp);
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003076 if (ctx)
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02003077 perf_counter_task_ctx(ctx, task_event);
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003078 rcu_read_unlock();
3079}
3080
Peter Zijlstra3a80b4a2009-08-07 19:49:01 +02003081static void perf_counter_task(struct task_struct *task,
3082 struct perf_counter_context *task_ctx,
3083 int new)
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02003084{
3085 struct perf_task_event task_event;
3086
3087 if (!atomic_read(&nr_comm_counters) &&
3088 !atomic_read(&nr_mmap_counters) &&
3089 !atomic_read(&nr_task_counters))
3090 return;
3091
3092 task_event = (struct perf_task_event){
Peter Zijlstra3a80b4a2009-08-07 19:49:01 +02003093 .task = task,
3094 .task_ctx = task_ctx,
3095 .event = {
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02003096 .header = {
3097 .type = new ? PERF_EVENT_FORK : PERF_EVENT_EXIT,
3098 .misc = 0,
3099 .size = sizeof(task_event.event),
3100 },
3101 /* .pid */
3102 /* .ppid */
3103 /* .tid */
3104 /* .ptid */
3105 },
3106 };
3107
3108 perf_counter_task_event(&task_event);
3109}
3110
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003111void perf_counter_fork(struct task_struct *task)
3112{
Peter Zijlstra3a80b4a2009-08-07 19:49:01 +02003113 perf_counter_task(task, NULL, 1);
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003114}
3115
3116/*
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02003117 * comm tracking
3118 */
3119
3120struct perf_comm_event {
Ingo Molnar22a4f652009-06-01 10:13:37 +02003121 struct task_struct *task;
3122 char *comm;
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02003123 int comm_size;
3124
3125 struct {
3126 struct perf_event_header header;
3127
3128 u32 pid;
3129 u32 tid;
3130 } event;
3131};
3132
3133static void perf_counter_comm_output(struct perf_counter *counter,
3134 struct perf_comm_event *comm_event)
3135{
3136 struct perf_output_handle handle;
3137 int size = comm_event->event.header.size;
3138 int ret = perf_output_begin(&handle, counter, size, 0, 0);
3139
3140 if (ret)
3141 return;
3142
Peter Zijlstra709e50c2009-06-02 14:13:15 +02003143 comm_event->event.pid = perf_counter_pid(counter, comm_event->task);
3144 comm_event->event.tid = perf_counter_tid(counter, comm_event->task);
3145
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02003146 perf_output_put(&handle, comm_event->event);
3147 perf_output_copy(&handle, comm_event->comm,
3148 comm_event->comm_size);
3149 perf_output_end(&handle);
3150}
3151
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003152static int perf_counter_comm_match(struct perf_counter *counter)
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02003153{
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003154 if (counter->attr.comm)
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02003155 return 1;
3156
3157 return 0;
3158}
3159
3160static void perf_counter_comm_ctx(struct perf_counter_context *ctx,
3161 struct perf_comm_event *comm_event)
3162{
3163 struct perf_counter *counter;
3164
3165 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
3166 return;
3167
3168 rcu_read_lock();
3169 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003170 if (perf_counter_comm_match(counter))
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02003171 perf_counter_comm_output(counter, comm_event);
3172 }
3173 rcu_read_unlock();
3174}
3175
3176static void perf_counter_comm_event(struct perf_comm_event *comm_event)
3177{
3178 struct perf_cpu_context *cpuctx;
Peter Zijlstra665c2142009-05-29 14:51:57 +02003179 struct perf_counter_context *ctx;
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02003180 unsigned int size;
Anton Blanchard413ee3b2009-07-16 15:15:52 +02003181 char comm[TASK_COMM_LEN];
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02003182
Anton Blanchard413ee3b2009-07-16 15:15:52 +02003183 memset(comm, 0, sizeof(comm));
3184 strncpy(comm, comm_event->task->comm, sizeof(comm));
Ingo Molnar888fcee2009-04-09 09:48:22 +02003185 size = ALIGN(strlen(comm)+1, sizeof(u64));
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02003186
3187 comm_event->comm = comm;
3188 comm_event->comm_size = size;
3189
3190 comm_event->event.header.size = sizeof(comm_event->event) + size;
3191
3192 cpuctx = &get_cpu_var(perf_cpu_context);
3193 perf_counter_comm_ctx(&cpuctx->ctx, comm_event);
3194 put_cpu_var(perf_cpu_context);
Peter Zijlstra665c2142009-05-29 14:51:57 +02003195
3196 rcu_read_lock();
3197 /*
3198 * doesn't really matter which of the child contexts the
3199 * events ends up in.
3200 */
3201 ctx = rcu_dereference(current->perf_counter_ctxp);
3202 if (ctx)
3203 perf_counter_comm_ctx(ctx, comm_event);
3204 rcu_read_unlock();
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02003205}
3206
3207void perf_counter_comm(struct task_struct *task)
3208{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02003209 struct perf_comm_event comm_event;
3210
Paul Mackerras57e79862009-06-30 16:07:19 +10003211 if (task->perf_counter_ctxp)
3212 perf_counter_enable_on_exec(task);
3213
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003214 if (!atomic_read(&nr_comm_counters))
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02003215 return;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003216
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02003217 comm_event = (struct perf_comm_event){
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02003218 .task = task,
Peter Zijlstra573402d2009-07-22 11:13:50 +02003219 /* .comm */
3220 /* .comm_size */
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02003221 .event = {
Peter Zijlstra573402d2009-07-22 11:13:50 +02003222 .header = {
3223 .type = PERF_EVENT_COMM,
3224 .misc = 0,
3225 /* .size */
3226 },
3227 /* .pid */
3228 /* .tid */
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02003229 },
3230 };
3231
3232 perf_counter_comm_event(&comm_event);
3233}
3234
3235/*
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003236 * mmap tracking
3237 */
3238
3239struct perf_mmap_event {
Peter Zijlstra089dd792009-06-05 14:04:55 +02003240 struct vm_area_struct *vma;
3241
3242 const char *file_name;
3243 int file_size;
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003244
3245 struct {
3246 struct perf_event_header header;
3247
3248 u32 pid;
3249 u32 tid;
3250 u64 start;
3251 u64 len;
3252 u64 pgoff;
3253 } event;
3254};
3255
3256static void perf_counter_mmap_output(struct perf_counter *counter,
3257 struct perf_mmap_event *mmap_event)
3258{
3259 struct perf_output_handle handle;
3260 int size = mmap_event->event.header.size;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02003261 int ret = perf_output_begin(&handle, counter, size, 0, 0);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003262
3263 if (ret)
3264 return;
3265
Peter Zijlstra709e50c2009-06-02 14:13:15 +02003266 mmap_event->event.pid = perf_counter_pid(counter, current);
3267 mmap_event->event.tid = perf_counter_tid(counter, current);
3268
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003269 perf_output_put(&handle, mmap_event->event);
3270 perf_output_copy(&handle, mmap_event->file_name,
3271 mmap_event->file_size);
Peter Zijlstra78d613e2009-03-30 19:07:11 +02003272 perf_output_end(&handle);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003273}
3274
3275static int perf_counter_mmap_match(struct perf_counter *counter,
3276 struct perf_mmap_event *mmap_event)
3277{
Peter Zijlstrad99e9442009-06-04 17:08:58 +02003278 if (counter->attr.mmap)
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003279 return 1;
3280
3281 return 0;
3282}
3283
3284static void perf_counter_mmap_ctx(struct perf_counter_context *ctx,
3285 struct perf_mmap_event *mmap_event)
3286{
3287 struct perf_counter *counter;
3288
3289 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
3290 return;
3291
3292 rcu_read_lock();
3293 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
3294 if (perf_counter_mmap_match(counter, mmap_event))
3295 perf_counter_mmap_output(counter, mmap_event);
3296 }
3297 rcu_read_unlock();
3298}
3299
3300static void perf_counter_mmap_event(struct perf_mmap_event *mmap_event)
3301{
3302 struct perf_cpu_context *cpuctx;
Peter Zijlstra665c2142009-05-29 14:51:57 +02003303 struct perf_counter_context *ctx;
Peter Zijlstra089dd792009-06-05 14:04:55 +02003304 struct vm_area_struct *vma = mmap_event->vma;
3305 struct file *file = vma->vm_file;
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003306 unsigned int size;
3307 char tmp[16];
3308 char *buf = NULL;
Peter Zijlstra089dd792009-06-05 14:04:55 +02003309 const char *name;
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003310
Anton Blanchard413ee3b2009-07-16 15:15:52 +02003311 memset(tmp, 0, sizeof(tmp));
3312
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003313 if (file) {
Anton Blanchard413ee3b2009-07-16 15:15:52 +02003314 /*
3315 * d_path works from the end of the buffer backwards, so we
3316 * need to add enough zero bytes after the string to handle
3317 * the 64bit alignment we do later.
3318 */
3319 buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003320 if (!buf) {
3321 name = strncpy(tmp, "//enomem", sizeof(tmp));
3322 goto got_name;
3323 }
Peter Zijlstrad3d21c42009-04-09 10:53:46 +02003324 name = d_path(&file->f_path, buf, PATH_MAX);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003325 if (IS_ERR(name)) {
3326 name = strncpy(tmp, "//toolong", sizeof(tmp));
3327 goto got_name;
3328 }
3329 } else {
Anton Blanchard413ee3b2009-07-16 15:15:52 +02003330 if (arch_vma_name(mmap_event->vma)) {
3331 name = strncpy(tmp, arch_vma_name(mmap_event->vma),
3332 sizeof(tmp));
Peter Zijlstra089dd792009-06-05 14:04:55 +02003333 goto got_name;
Anton Blanchard413ee3b2009-07-16 15:15:52 +02003334 }
Peter Zijlstra089dd792009-06-05 14:04:55 +02003335
3336 if (!vma->vm_mm) {
3337 name = strncpy(tmp, "[vdso]", sizeof(tmp));
3338 goto got_name;
3339 }
3340
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003341 name = strncpy(tmp, "//anon", sizeof(tmp));
3342 goto got_name;
3343 }
3344
3345got_name:
Ingo Molnar888fcee2009-04-09 09:48:22 +02003346 size = ALIGN(strlen(name)+1, sizeof(u64));
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003347
3348 mmap_event->file_name = name;
3349 mmap_event->file_size = size;
3350
3351 mmap_event->event.header.size = sizeof(mmap_event->event) + size;
3352
3353 cpuctx = &get_cpu_var(perf_cpu_context);
3354 perf_counter_mmap_ctx(&cpuctx->ctx, mmap_event);
3355 put_cpu_var(perf_cpu_context);
3356
Peter Zijlstra665c2142009-05-29 14:51:57 +02003357 rcu_read_lock();
3358 /*
3359 * doesn't really matter which of the child contexts the
3360 * events ends up in.
3361 */
3362 ctx = rcu_dereference(current->perf_counter_ctxp);
3363 if (ctx)
3364 perf_counter_mmap_ctx(ctx, mmap_event);
3365 rcu_read_unlock();
3366
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003367 kfree(buf);
3368}
3369
Peter Zijlstra089dd792009-06-05 14:04:55 +02003370void __perf_counter_mmap(struct vm_area_struct *vma)
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003371{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02003372 struct perf_mmap_event mmap_event;
3373
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003374 if (!atomic_read(&nr_mmap_counters))
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02003375 return;
3376
3377 mmap_event = (struct perf_mmap_event){
Peter Zijlstra089dd792009-06-05 14:04:55 +02003378 .vma = vma,
Peter Zijlstra573402d2009-07-22 11:13:50 +02003379 /* .file_name */
3380 /* .file_size */
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003381 .event = {
Peter Zijlstra573402d2009-07-22 11:13:50 +02003382 .header = {
3383 .type = PERF_EVENT_MMAP,
3384 .misc = 0,
3385 /* .size */
3386 },
3387 /* .pid */
3388 /* .tid */
Peter Zijlstra089dd792009-06-05 14:04:55 +02003389 .start = vma->vm_start,
3390 .len = vma->vm_end - vma->vm_start,
3391 .pgoff = vma->vm_pgoff,
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003392 },
3393 };
3394
3395 perf_counter_mmap_event(&mmap_event);
3396}
3397
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003398/*
Peter Zijlstraa78ac322009-05-25 17:39:05 +02003399 * IRQ throttle logging
3400 */
3401
3402static void perf_log_throttle(struct perf_counter *counter, int enable)
3403{
3404 struct perf_output_handle handle;
3405 int ret;
3406
3407 struct {
3408 struct perf_event_header header;
3409 u64 time;
Peter Zijlstracca3f452009-06-11 14:57:55 +02003410 u64 id;
Peter Zijlstra7f453c22009-07-21 13:19:40 +02003411 u64 stream_id;
Peter Zijlstraa78ac322009-05-25 17:39:05 +02003412 } throttle_event = {
3413 .header = {
Anton Blanchard966ee4d2009-07-22 23:05:46 +10003414 .type = PERF_EVENT_THROTTLE,
Peter Zijlstraa78ac322009-05-25 17:39:05 +02003415 .misc = 0,
3416 .size = sizeof(throttle_event),
3417 },
Peter Zijlstra7f453c22009-07-21 13:19:40 +02003418 .time = sched_clock(),
3419 .id = primary_counter_id(counter),
3420 .stream_id = counter->id,
Peter Zijlstraa78ac322009-05-25 17:39:05 +02003421 };
3422
Anton Blanchard966ee4d2009-07-22 23:05:46 +10003423 if (enable)
3424 throttle_event.header.type = PERF_EVENT_UNTHROTTLE;
3425
Ingo Molnar0127c3e2009-05-25 22:03:26 +02003426 ret = perf_output_begin(&handle, counter, sizeof(throttle_event), 1, 0);
Peter Zijlstraa78ac322009-05-25 17:39:05 +02003427 if (ret)
3428 return;
3429
3430 perf_output_put(&handle, throttle_event);
3431 perf_output_end(&handle);
3432}
3433
3434/*
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01003435 * Generic counter overflow handling, sampling.
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02003436 */
3437
Peter Zijlstradf1a1322009-06-10 21:02:22 +02003438int perf_counter_overflow(struct perf_counter *counter, int nmi,
3439 struct perf_sample_data *data)
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02003440{
Peter Zijlstra79f14642009-04-06 11:45:07 +02003441 int events = atomic_read(&counter->event_limit);
Peter Zijlstraa78ac322009-05-25 17:39:05 +02003442 int throttle = counter->pmu->unthrottle != NULL;
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02003443 struct hw_perf_counter *hwc = &counter->hw;
Peter Zijlstra79f14642009-04-06 11:45:07 +02003444 int ret = 0;
3445
Peter Zijlstraa78ac322009-05-25 17:39:05 +02003446 if (!throttle) {
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02003447 hwc->interrupts++;
Ingo Molnar128f0482009-06-03 22:19:36 +02003448 } else {
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02003449 if (hwc->interrupts != MAX_INTERRUPTS) {
3450 hwc->interrupts++;
Peter Zijlstradf58ab22009-06-11 11:25:05 +02003451 if (HZ * hwc->interrupts >
3452 (u64)sysctl_perf_counter_sample_rate) {
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02003453 hwc->interrupts = MAX_INTERRUPTS;
Ingo Molnar128f0482009-06-03 22:19:36 +02003454 perf_log_throttle(counter, 0);
3455 ret = 1;
3456 }
3457 } else {
3458 /*
3459 * Keep re-disabling counters even though on the previous
3460 * pass we disabled it - just in case we raced with a
3461 * sched-in and the counter got enabled again:
3462 */
Peter Zijlstraa78ac322009-05-25 17:39:05 +02003463 ret = 1;
3464 }
3465 }
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003466
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02003467 if (counter->attr.freq) {
3468 u64 now = sched_clock();
3469 s64 delta = now - hwc->freq_stamp;
3470
3471 hwc->freq_stamp = now;
3472
3473 if (delta > 0 && delta < TICK_NSEC)
3474 perf_adjust_period(counter, NSEC_PER_SEC / (int)delta);
3475 }
3476
Peter Zijlstra2023b352009-05-05 17:50:26 +02003477 /*
3478 * XXX event_limit might not quite work as expected on inherited
3479 * counters
3480 */
3481
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02003482 counter->pending_kill = POLL_IN;
Peter Zijlstra79f14642009-04-06 11:45:07 +02003483 if (events && atomic_dec_and_test(&counter->event_limit)) {
3484 ret = 1;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02003485 counter->pending_kill = POLL_HUP;
Peter Zijlstra79f14642009-04-06 11:45:07 +02003486 if (nmi) {
3487 counter->pending_disable = 1;
3488 perf_pending_queue(&counter->pending,
3489 perf_pending_counter);
3490 } else
3491 perf_counter_disable(counter);
3492 }
3493
Peter Zijlstradf1a1322009-06-10 21:02:22 +02003494 perf_counter_output(counter, nmi, data);
Peter Zijlstra79f14642009-04-06 11:45:07 +02003495 return ret;
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02003496}
3497
3498/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003499 * Generic software counter infrastructure
3500 */
3501
Peter Zijlstra7b4b6652009-07-22 09:29:32 +02003502/*
3503 * We directly increment counter->count and keep a second value in
3504 * counter->hw.period_left to count intervals. This period counter
3505 * is kept in the range [-sample_period, 0] so that we can use the
3506 * sign as trigger.
3507 */
3508
3509static u64 perf_swcounter_set_period(struct perf_counter *counter)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003510{
3511 struct hw_perf_counter *hwc = &counter->hw;
Peter Zijlstra7b4b6652009-07-22 09:29:32 +02003512 u64 period = hwc->last_period;
3513 u64 nr, offset;
3514 s64 old, val;
3515
3516 hwc->last_period = hwc->sample_period;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003517
3518again:
Peter Zijlstra7b4b6652009-07-22 09:29:32 +02003519 old = val = atomic64_read(&hwc->period_left);
3520 if (val < 0)
3521 return 0;
3522
3523 nr = div64_u64(period + val, period);
3524 offset = nr * period;
3525 val -= offset;
3526 if (atomic64_cmpxchg(&hwc->period_left, old, val) != old)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003527 goto again;
3528
Peter Zijlstra7b4b6652009-07-22 09:29:32 +02003529 return nr;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003530}
3531
3532static void perf_swcounter_overflow(struct perf_counter *counter,
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003533 int nmi, struct perf_sample_data *data)
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003534{
Peter Zijlstra7b4b6652009-07-22 09:29:32 +02003535 struct hw_perf_counter *hwc = &counter->hw;
3536 u64 overflow;
Peter Zijlstradf1a1322009-06-10 21:02:22 +02003537
Peter Zijlstra7b4b6652009-07-22 09:29:32 +02003538 data->period = counter->hw.last_period;
3539 overflow = perf_swcounter_set_period(counter);
3540
3541 if (hwc->interrupts == MAX_INTERRUPTS)
3542 return;
3543
3544 for (; overflow; overflow--) {
3545 if (perf_counter_overflow(counter, nmi, data)) {
3546 /*
3547 * We inhibit the overflow from happening when
3548 * hwc->interrupts == MAX_INTERRUPTS.
3549 */
3550 break;
3551 }
3552 }
3553}
3554
3555static void perf_swcounter_unthrottle(struct perf_counter *counter)
3556{
3557 /*
3558 * Nothing to do, we already reset hwc->interrupts.
3559 */
3560}
3561
3562static void perf_swcounter_add(struct perf_counter *counter, u64 nr,
3563 int nmi, struct perf_sample_data *data)
3564{
3565 struct hw_perf_counter *hwc = &counter->hw;
3566
3567 atomic64_add(nr, &counter->count);
3568
3569 if (!hwc->sample_period)
3570 return;
3571
3572 if (!data->regs)
3573 return;
3574
3575 if (!atomic64_add_negative(nr, &hwc->period_left))
3576 perf_swcounter_overflow(counter, nmi, data);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003577}
3578
Paul Mackerras880ca152009-06-01 17:49:14 +10003579static int perf_swcounter_is_counting(struct perf_counter *counter)
3580{
Peter Zijlstrabcfc2602009-08-13 09:51:55 +02003581 /*
3582 * The counter is active, we're good!
3583 */
Paul Mackerras880ca152009-06-01 17:49:14 +10003584 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
3585 return 1;
3586
Peter Zijlstrabcfc2602009-08-13 09:51:55 +02003587 /*
3588 * The counter is off/error, not counting.
3589 */
Paul Mackerras880ca152009-06-01 17:49:14 +10003590 if (counter->state != PERF_COUNTER_STATE_INACTIVE)
3591 return 0;
3592
3593 /*
Peter Zijlstrabcfc2602009-08-13 09:51:55 +02003594 * The counter is inactive, if the context is active
3595 * we're part of a group that didn't make it on the 'pmu',
3596 * not counting.
Paul Mackerras880ca152009-06-01 17:49:14 +10003597 */
Peter Zijlstrabcfc2602009-08-13 09:51:55 +02003598 if (counter->ctx->is_active)
3599 return 0;
3600
3601 /*
3602 * We're inactive and the context is too, this means the
3603 * task is scheduled out, we're counting events that happen
3604 * to us, like migration events.
3605 */
3606 return 1;
Paul Mackerras880ca152009-06-01 17:49:14 +10003607}
3608
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003609static int perf_swcounter_match(struct perf_counter *counter,
Peter Zijlstra1c432d82009-06-11 13:19:29 +02003610 enum perf_type_id type,
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003611 u32 event, struct pt_regs *regs)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003612{
Paul Mackerras880ca152009-06-01 17:49:14 +10003613 if (!perf_swcounter_is_counting(counter))
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003614 return 0;
3615
Ingo Molnara21ca2c2009-06-06 09:58:57 +02003616 if (counter->attr.type != type)
3617 return 0;
3618 if (counter->attr.config != event)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003619 return 0;
3620
Paul Mackerras3f731ca2009-06-01 17:52:30 +10003621 if (regs) {
Peter Zijlstra0d486962009-06-02 19:22:16 +02003622 if (counter->attr.exclude_user && user_mode(regs))
Paul Mackerras3f731ca2009-06-01 17:52:30 +10003623 return 0;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003624
Peter Zijlstra0d486962009-06-02 19:22:16 +02003625 if (counter->attr.exclude_kernel && !user_mode(regs))
Paul Mackerras3f731ca2009-06-01 17:52:30 +10003626 return 0;
3627 }
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003628
3629 return 1;
3630}
3631
3632static void perf_swcounter_ctx_event(struct perf_counter_context *ctx,
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003633 enum perf_type_id type,
3634 u32 event, u64 nr, int nmi,
3635 struct perf_sample_data *data)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003636{
3637 struct perf_counter *counter;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003638
Peter Zijlstra01ef09d2009-03-19 20:26:11 +01003639 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003640 return;
3641
Peter Zijlstra592903c2009-03-13 12:21:36 +01003642 rcu_read_lock();
3643 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003644 if (perf_swcounter_match(counter, type, event, data->regs))
3645 perf_swcounter_add(counter, nr, nmi, data);
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003646 }
Peter Zijlstra592903c2009-03-13 12:21:36 +01003647 rcu_read_unlock();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003648}
3649
Peter Zijlstra96f6d442009-03-23 18:22:07 +01003650static int *perf_swcounter_recursion_context(struct perf_cpu_context *cpuctx)
3651{
3652 if (in_nmi())
3653 return &cpuctx->recursion[3];
3654
3655 if (in_irq())
3656 return &cpuctx->recursion[2];
3657
3658 if (in_softirq())
3659 return &cpuctx->recursion[1];
3660
3661 return &cpuctx->recursion[0];
3662}
3663
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003664static void do_perf_swcounter_event(enum perf_type_id type, u32 event,
3665 u64 nr, int nmi,
3666 struct perf_sample_data *data)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003667{
3668 struct perf_cpu_context *cpuctx = &get_cpu_var(perf_cpu_context);
Peter Zijlstra96f6d442009-03-23 18:22:07 +01003669 int *recursion = perf_swcounter_recursion_context(cpuctx);
Peter Zijlstra665c2142009-05-29 14:51:57 +02003670 struct perf_counter_context *ctx;
Peter Zijlstra96f6d442009-03-23 18:22:07 +01003671
3672 if (*recursion)
3673 goto out;
3674
3675 (*recursion)++;
3676 barrier();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003677
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003678 perf_swcounter_ctx_event(&cpuctx->ctx, type, event,
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003679 nr, nmi, data);
Peter Zijlstra665c2142009-05-29 14:51:57 +02003680 rcu_read_lock();
3681 /*
3682 * doesn't really matter which of the child contexts the
3683 * events ends up in.
3684 */
3685 ctx = rcu_dereference(current->perf_counter_ctxp);
3686 if (ctx)
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003687 perf_swcounter_ctx_event(ctx, type, event, nr, nmi, data);
Peter Zijlstra665c2142009-05-29 14:51:57 +02003688 rcu_read_unlock();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003689
Peter Zijlstra96f6d442009-03-23 18:22:07 +01003690 barrier();
3691 (*recursion)--;
3692
3693out:
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003694 put_cpu_var(perf_cpu_context);
3695}
3696
Peter Zijlstraf29ac752009-06-19 18:27:26 +02003697void __perf_swcounter_event(u32 event, u64 nr, int nmi,
3698 struct pt_regs *regs, u64 addr)
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003699{
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003700 struct perf_sample_data data = {
3701 .regs = regs,
3702 .addr = addr,
3703 };
3704
3705 do_perf_swcounter_event(PERF_TYPE_SOFTWARE, event, nr, nmi, &data);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003706}
3707
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003708static void perf_swcounter_read(struct perf_counter *counter)
3709{
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003710}
3711
3712static int perf_swcounter_enable(struct perf_counter *counter)
3713{
Peter Zijlstra7b4b6652009-07-22 09:29:32 +02003714 struct hw_perf_counter *hwc = &counter->hw;
3715
3716 if (hwc->sample_period) {
3717 hwc->last_period = hwc->sample_period;
3718 perf_swcounter_set_period(counter);
3719 }
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003720 return 0;
3721}
3722
3723static void perf_swcounter_disable(struct perf_counter *counter)
3724{
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003725}
3726
Robert Richter4aeb0b42009-04-29 12:47:03 +02003727static const struct pmu perf_ops_generic = {
Peter Zijlstraac17dc82009-03-13 12:21:34 +01003728 .enable = perf_swcounter_enable,
3729 .disable = perf_swcounter_disable,
3730 .read = perf_swcounter_read,
Peter Zijlstra7b4b6652009-07-22 09:29:32 +02003731 .unthrottle = perf_swcounter_unthrottle,
Peter Zijlstraac17dc82009-03-13 12:21:34 +01003732};
3733
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003734/*
Peter Zijlstra7b4b6652009-07-22 09:29:32 +02003735 * hrtimer based swcounter callback
3736 */
3737
3738static enum hrtimer_restart perf_swcounter_hrtimer(struct hrtimer *hrtimer)
3739{
3740 enum hrtimer_restart ret = HRTIMER_RESTART;
3741 struct perf_sample_data data;
3742 struct perf_counter *counter;
3743 u64 period;
3744
3745 counter = container_of(hrtimer, struct perf_counter, hw.hrtimer);
3746 counter->pmu->read(counter);
3747
3748 data.addr = 0;
3749 data.regs = get_irq_regs();
3750 /*
3751 * In case we exclude kernel IPs or are somehow not in interrupt
3752 * context, provide the next best thing, the user IP.
3753 */
3754 if ((counter->attr.exclude_kernel || !data.regs) &&
3755 !counter->attr.exclude_user)
3756 data.regs = task_pt_regs(current);
3757
3758 if (data.regs) {
3759 if (perf_counter_overflow(counter, 0, &data))
3760 ret = HRTIMER_NORESTART;
3761 }
3762
3763 period = max_t(u64, 10000, counter->hw.sample_period);
3764 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
3765
3766 return ret;
3767}
3768
3769/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003770 * Software counter: cpu wall time clock
3771 */
3772
Paul Mackerras9abf8a02009-01-09 16:26:43 +11003773static void cpu_clock_perf_counter_update(struct perf_counter *counter)
3774{
3775 int cpu = raw_smp_processor_id();
3776 s64 prev;
3777 u64 now;
3778
3779 now = cpu_clock(cpu);
3780 prev = atomic64_read(&counter->hw.prev_count);
3781 atomic64_set(&counter->hw.prev_count, now);
3782 atomic64_add(now - prev, &counter->count);
3783}
3784
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003785static int cpu_clock_perf_counter_enable(struct perf_counter *counter)
3786{
3787 struct hw_perf_counter *hwc = &counter->hw;
3788 int cpu = raw_smp_processor_id();
3789
3790 atomic64_set(&hwc->prev_count, cpu_clock(cpu));
Peter Zijlstra039fc912009-03-13 16:43:47 +01003791 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
3792 hwc->hrtimer.function = perf_swcounter_hrtimer;
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003793 if (hwc->sample_period) {
3794 u64 period = max_t(u64, 10000, hwc->sample_period);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003795 __hrtimer_start_range_ns(&hwc->hrtimer,
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003796 ns_to_ktime(period), 0,
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003797 HRTIMER_MODE_REL, 0);
3798 }
3799
3800 return 0;
3801}
3802
Ingo Molnar5c92d122008-12-11 13:21:10 +01003803static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
3804{
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003805 if (counter->hw.sample_period)
Peter Zijlstrab986d7e2009-05-20 12:21:21 +02003806 hrtimer_cancel(&counter->hw.hrtimer);
Paul Mackerras9abf8a02009-01-09 16:26:43 +11003807 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01003808}
3809
3810static void cpu_clock_perf_counter_read(struct perf_counter *counter)
3811{
Paul Mackerras9abf8a02009-01-09 16:26:43 +11003812 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01003813}
3814
Robert Richter4aeb0b42009-04-29 12:47:03 +02003815static const struct pmu perf_ops_cpu_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +01003816 .enable = cpu_clock_perf_counter_enable,
3817 .disable = cpu_clock_perf_counter_disable,
3818 .read = cpu_clock_perf_counter_read,
Ingo Molnar5c92d122008-12-11 13:21:10 +01003819};
3820
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01003821/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003822 * Software counter: task time clock
3823 */
3824
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02003825static void task_clock_perf_counter_update(struct perf_counter *counter, u64 now)
Ingo Molnarbae43c92008-12-11 14:03:20 +01003826{
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02003827 u64 prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003828 s64 delta;
Ingo Molnarbae43c92008-12-11 14:03:20 +01003829
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02003830 prev = atomic64_xchg(&counter->hw.prev_count, now);
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003831 delta = now - prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003832 atomic64_add(delta, &counter->count);
Ingo Molnarbae43c92008-12-11 14:03:20 +01003833}
3834
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01003835static int task_clock_perf_counter_enable(struct perf_counter *counter)
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003836{
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003837 struct hw_perf_counter *hwc = &counter->hw;
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02003838 u64 now;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003839
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02003840 now = counter->ctx->time;
3841
3842 atomic64_set(&hwc->prev_count, now);
Peter Zijlstra039fc912009-03-13 16:43:47 +01003843 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
3844 hwc->hrtimer.function = perf_swcounter_hrtimer;
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003845 if (hwc->sample_period) {
3846 u64 period = max_t(u64, 10000, hwc->sample_period);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003847 __hrtimer_start_range_ns(&hwc->hrtimer,
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003848 ns_to_ktime(period), 0,
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003849 HRTIMER_MODE_REL, 0);
3850 }
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01003851
3852 return 0;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003853}
3854
3855static void task_clock_perf_counter_disable(struct perf_counter *counter)
3856{
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003857 if (counter->hw.sample_period)
Peter Zijlstrab986d7e2009-05-20 12:21:21 +02003858 hrtimer_cancel(&counter->hw.hrtimer);
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02003859 task_clock_perf_counter_update(counter, counter->ctx->time);
3860
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003861}
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01003862
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003863static void task_clock_perf_counter_read(struct perf_counter *counter)
3864{
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02003865 u64 time;
3866
3867 if (!in_nmi()) {
3868 update_context_time(counter->ctx);
3869 time = counter->ctx->time;
3870 } else {
3871 u64 now = perf_clock();
3872 u64 delta = now - counter->ctx->timestamp;
3873 time = counter->ctx->time + delta;
3874 }
3875
3876 task_clock_perf_counter_update(counter, time);
Ingo Molnarbae43c92008-12-11 14:03:20 +01003877}
3878
Robert Richter4aeb0b42009-04-29 12:47:03 +02003879static const struct pmu perf_ops_task_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +01003880 .enable = task_clock_perf_counter_enable,
3881 .disable = task_clock_perf_counter_disable,
3882 .read = task_clock_perf_counter_read,
Ingo Molnarbae43c92008-12-11 14:03:20 +01003883};
3884
Peter Zijlstrae077df42009-03-19 20:26:17 +01003885#ifdef CONFIG_EVENT_PROFILE
Frederic Weisbeckerf413cdb2009-08-07 01:25:54 +02003886void perf_tpcounter_event(int event_id, u64 addr, u64 count, void *record,
3887 int entry_size)
Peter Zijlstrae077df42009-03-19 20:26:17 +01003888{
Frederic Weisbecker3a43ce62009-08-08 04:26:37 +02003889 struct perf_raw_record raw = {
Frederic Weisbeckerf413cdb2009-08-07 01:25:54 +02003890 .size = entry_size,
Frederic Weisbecker3a43ce62009-08-08 04:26:37 +02003891 .data = record,
Frederic Weisbeckerf413cdb2009-08-07 01:25:54 +02003892 };
3893
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003894 struct perf_sample_data data = {
Chris Wilsond4d7d0b2009-07-06 09:31:33 +01003895 .regs = get_irq_regs(),
Peter Zijlstra3a659302009-07-21 17:34:57 +02003896 .addr = addr,
Frederic Weisbecker3a43ce62009-08-08 04:26:37 +02003897 .raw = &raw,
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003898 };
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003899
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003900 if (!data.regs)
3901 data.regs = task_pt_regs(current);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003902
Peter Zijlstra3a659302009-07-21 17:34:57 +02003903 do_perf_swcounter_event(PERF_TYPE_TRACEPOINT, event_id, count, 1, &data);
Peter Zijlstrae077df42009-03-19 20:26:17 +01003904}
Steven Whitehouseff7b1b42009-04-15 16:55:05 +01003905EXPORT_SYMBOL_GPL(perf_tpcounter_event);
Peter Zijlstrae077df42009-03-19 20:26:17 +01003906
3907extern int ftrace_profile_enable(int);
3908extern void ftrace_profile_disable(int);
3909
3910static void tp_perf_counter_destroy(struct perf_counter *counter)
3911{
Chris Wilsond4d7d0b2009-07-06 09:31:33 +01003912 ftrace_profile_disable(counter->attr.config);
Peter Zijlstrae077df42009-03-19 20:26:17 +01003913}
3914
Robert Richter4aeb0b42009-04-29 12:47:03 +02003915static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
Peter Zijlstrae077df42009-03-19 20:26:17 +01003916{
Peter Zijlstraa4e95fc2009-08-10 11:20:12 +02003917 /*
3918 * Raw tracepoint data is a severe data leak, only allow root to
3919 * have these.
3920 */
3921 if ((counter->attr.sample_type & PERF_SAMPLE_RAW) &&
3922 !capable(CAP_SYS_ADMIN))
3923 return ERR_PTR(-EPERM);
3924
Chris Wilsond4d7d0b2009-07-06 09:31:33 +01003925 if (ftrace_profile_enable(counter->attr.config))
Peter Zijlstrae077df42009-03-19 20:26:17 +01003926 return NULL;
3927
3928 counter->destroy = tp_perf_counter_destroy;
3929
3930 return &perf_ops_generic;
3931}
3932#else
Robert Richter4aeb0b42009-04-29 12:47:03 +02003933static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
Peter Zijlstrae077df42009-03-19 20:26:17 +01003934{
3935 return NULL;
3936}
3937#endif
3938
Peter Zijlstraf29ac752009-06-19 18:27:26 +02003939atomic_t perf_swcounter_enabled[PERF_COUNT_SW_MAX];
3940
3941static void sw_perf_counter_destroy(struct perf_counter *counter)
3942{
3943 u64 event = counter->attr.config;
3944
Peter Zijlstraf3440112009-06-22 13:58:35 +02003945 WARN_ON(counter->parent);
3946
Peter Zijlstraf29ac752009-06-19 18:27:26 +02003947 atomic_dec(&perf_swcounter_enabled[event]);
3948}
3949
Robert Richter4aeb0b42009-04-29 12:47:03 +02003950static const struct pmu *sw_perf_counter_init(struct perf_counter *counter)
Ingo Molnar5c92d122008-12-11 13:21:10 +01003951{
Robert Richter4aeb0b42009-04-29 12:47:03 +02003952 const struct pmu *pmu = NULL;
Peter Zijlstraf29ac752009-06-19 18:27:26 +02003953 u64 event = counter->attr.config;
Ingo Molnar5c92d122008-12-11 13:21:10 +01003954
Paul Mackerras0475f9e2009-02-11 14:35:35 +11003955 /*
3956 * Software counters (currently) can't in general distinguish
3957 * between user, kernel and hypervisor events.
3958 * However, context switches and cpu migrations are considered
3959 * to be kernel events, and page faults are never hypervisor
3960 * events.
3961 */
Peter Zijlstraf29ac752009-06-19 18:27:26 +02003962 switch (event) {
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +02003963 case PERF_COUNT_SW_CPU_CLOCK:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003964 pmu = &perf_ops_cpu_clock;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003965
Ingo Molnar5c92d122008-12-11 13:21:10 +01003966 break;
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +02003967 case PERF_COUNT_SW_TASK_CLOCK:
Paul Mackerras23a185c2009-02-09 22:42:47 +11003968 /*
3969 * If the user instantiates this as a per-cpu counter,
3970 * use the cpu_clock counter instead.
3971 */
3972 if (counter->ctx->task)
Robert Richter4aeb0b42009-04-29 12:47:03 +02003973 pmu = &perf_ops_task_clock;
Paul Mackerras23a185c2009-02-09 22:42:47 +11003974 else
Robert Richter4aeb0b42009-04-29 12:47:03 +02003975 pmu = &perf_ops_cpu_clock;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003976
Ingo Molnarbae43c92008-12-11 14:03:20 +01003977 break;
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +02003978 case PERF_COUNT_SW_PAGE_FAULTS:
3979 case PERF_COUNT_SW_PAGE_FAULTS_MIN:
3980 case PERF_COUNT_SW_PAGE_FAULTS_MAJ:
3981 case PERF_COUNT_SW_CONTEXT_SWITCHES:
3982 case PERF_COUNT_SW_CPU_MIGRATIONS:
Peter Zijlstraf3440112009-06-22 13:58:35 +02003983 if (!counter->parent) {
3984 atomic_inc(&perf_swcounter_enabled[event]);
3985 counter->destroy = sw_perf_counter_destroy;
3986 }
Paul Mackerras3f731ca2009-06-01 17:52:30 +10003987 pmu = &perf_ops_generic;
Ingo Molnar6c594c22008-12-14 12:34:15 +01003988 break;
Ingo Molnar5c92d122008-12-11 13:21:10 +01003989 }
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003990
Robert Richter4aeb0b42009-04-29 12:47:03 +02003991 return pmu;
Ingo Molnar5c92d122008-12-11 13:21:10 +01003992}
3993
Thomas Gleixner0793a612008-12-04 20:12:29 +01003994/*
3995 * Allocate and initialize a counter structure
3996 */
3997static struct perf_counter *
Peter Zijlstra0d486962009-06-02 19:22:16 +02003998perf_counter_alloc(struct perf_counter_attr *attr,
Ingo Molnar04289bb2008-12-11 08:38:42 +01003999 int cpu,
Paul Mackerras23a185c2009-02-09 22:42:47 +11004000 struct perf_counter_context *ctx,
Ingo Molnar9b51f662008-12-12 13:49:45 +01004001 struct perf_counter *group_leader,
Peter Zijlstrab84fbc92009-06-22 13:57:40 +02004002 struct perf_counter *parent_counter,
Ingo Molnar9b51f662008-12-12 13:49:45 +01004003 gfp_t gfpflags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01004004{
Robert Richter4aeb0b42009-04-29 12:47:03 +02004005 const struct pmu *pmu;
Ingo Molnar621a01e2008-12-11 12:46:46 +01004006 struct perf_counter *counter;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02004007 struct hw_perf_counter *hwc;
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02004008 long err;
Thomas Gleixner0793a612008-12-04 20:12:29 +01004009
Ingo Molnar9b51f662008-12-12 13:49:45 +01004010 counter = kzalloc(sizeof(*counter), gfpflags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004011 if (!counter)
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02004012 return ERR_PTR(-ENOMEM);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004013
Ingo Molnar04289bb2008-12-11 08:38:42 +01004014 /*
4015 * Single counters are their own group leaders, with an
4016 * empty sibling list:
4017 */
4018 if (!group_leader)
4019 group_leader = counter;
4020
Peter Zijlstrafccc7142009-05-23 18:28:56 +02004021 mutex_init(&counter->child_mutex);
4022 INIT_LIST_HEAD(&counter->child_list);
4023
Ingo Molnar04289bb2008-12-11 08:38:42 +01004024 INIT_LIST_HEAD(&counter->list_entry);
Peter Zijlstra592903c2009-03-13 12:21:36 +01004025 INIT_LIST_HEAD(&counter->event_entry);
Ingo Molnar04289bb2008-12-11 08:38:42 +01004026 INIT_LIST_HEAD(&counter->sibling_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004027 init_waitqueue_head(&counter->waitq);
4028
Peter Zijlstra7b732a72009-03-23 18:22:10 +01004029 mutex_init(&counter->mmap_mutex);
4030
Peter Zijlstraa96bbc12009-06-03 14:01:36 +02004031 counter->cpu = cpu;
Peter Zijlstra0d486962009-06-02 19:22:16 +02004032 counter->attr = *attr;
Peter Zijlstraa96bbc12009-06-03 14:01:36 +02004033 counter->group_leader = group_leader;
4034 counter->pmu = NULL;
4035 counter->ctx = ctx;
4036 counter->oncpu = -1;
Ingo Molnar329d8762009-05-26 08:10:00 +02004037
Peter Zijlstrab84fbc92009-06-22 13:57:40 +02004038 counter->parent = parent_counter;
4039
Peter Zijlstraa96bbc12009-06-03 14:01:36 +02004040 counter->ns = get_pid_ns(current->nsproxy->pid_ns);
4041 counter->id = atomic64_inc_return(&perf_counter_id);
4042
4043 counter->state = PERF_COUNTER_STATE_INACTIVE;
4044
Peter Zijlstra0d486962009-06-02 19:22:16 +02004045 if (attr->disabled)
Ingo Molnara86ed502008-12-17 00:43:10 +01004046 counter->state = PERF_COUNTER_STATE_OFF;
4047
Robert Richter4aeb0b42009-04-29 12:47:03 +02004048 pmu = NULL;
Peter Zijlstrab8e83512009-03-19 20:26:18 +01004049
Peter Zijlstra60db5e02009-05-15 15:19:28 +02004050 hwc = &counter->hw;
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02004051 hwc->sample_period = attr->sample_period;
Peter Zijlstra0d486962009-06-02 19:22:16 +02004052 if (attr->freq && attr->sample_freq)
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02004053 hwc->sample_period = 1;
4054
4055 atomic64_set(&hwc->period_left, hwc->sample_period);
Peter Zijlstra60db5e02009-05-15 15:19:28 +02004056
Peter Zijlstra2023b352009-05-05 17:50:26 +02004057 /*
Peter Zijlstra3dab77f2009-08-13 11:47:53 +02004058 * we currently do not support PERF_FORMAT_GROUP on inherited counters
Peter Zijlstra2023b352009-05-05 17:50:26 +02004059 */
Peter Zijlstra3dab77f2009-08-13 11:47:53 +02004060 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
Peter Zijlstra2023b352009-05-05 17:50:26 +02004061 goto done;
4062
Ingo Molnara21ca2c2009-06-06 09:58:57 +02004063 switch (attr->type) {
Peter Zijlstra081fad82009-06-11 17:57:21 +02004064 case PERF_TYPE_RAW:
Peter Zijlstrab8e83512009-03-19 20:26:18 +01004065 case PERF_TYPE_HARDWARE:
Ingo Molnar8326f442009-06-05 20:22:46 +02004066 case PERF_TYPE_HW_CACHE:
Robert Richter4aeb0b42009-04-29 12:47:03 +02004067 pmu = hw_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01004068 break;
4069
4070 case PERF_TYPE_SOFTWARE:
Robert Richter4aeb0b42009-04-29 12:47:03 +02004071 pmu = sw_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01004072 break;
4073
4074 case PERF_TYPE_TRACEPOINT:
Robert Richter4aeb0b42009-04-29 12:47:03 +02004075 pmu = tp_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01004076 break;
Peter Zijlstra974802e2009-06-12 12:46:55 +02004077
4078 default:
4079 break;
Peter Zijlstrab8e83512009-03-19 20:26:18 +01004080 }
Peter Zijlstraf4a2deb42009-03-23 18:22:06 +01004081done:
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02004082 err = 0;
Robert Richter4aeb0b42009-04-29 12:47:03 +02004083 if (!pmu)
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02004084 err = -EINVAL;
Robert Richter4aeb0b42009-04-29 12:47:03 +02004085 else if (IS_ERR(pmu))
4086 err = PTR_ERR(pmu);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02004087
4088 if (err) {
Peter Zijlstraa96bbc12009-06-03 14:01:36 +02004089 if (counter->ns)
4090 put_pid_ns(counter->ns);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02004091 kfree(counter);
4092 return ERR_PTR(err);
4093 }
4094
Robert Richter4aeb0b42009-04-29 12:47:03 +02004095 counter->pmu = pmu;
Thomas Gleixner0793a612008-12-04 20:12:29 +01004096
Peter Zijlstraf3440112009-06-22 13:58:35 +02004097 if (!counter->parent) {
4098 atomic_inc(&nr_counters);
4099 if (counter->attr.mmap)
4100 atomic_inc(&nr_mmap_counters);
4101 if (counter->attr.comm)
4102 atomic_inc(&nr_comm_counters);
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02004103 if (counter->attr.task)
4104 atomic_inc(&nr_task_counters);
Peter Zijlstraf3440112009-06-22 13:58:35 +02004105 }
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02004106
Thomas Gleixner0793a612008-12-04 20:12:29 +01004107 return counter;
4108}
4109
Peter Zijlstra974802e2009-06-12 12:46:55 +02004110static int perf_copy_attr(struct perf_counter_attr __user *uattr,
4111 struct perf_counter_attr *attr)
4112{
4113 int ret;
4114 u32 size;
4115
4116 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
4117 return -EFAULT;
4118
4119 /*
4120 * zero the full structure, so that a short copy will be nice.
4121 */
4122 memset(attr, 0, sizeof(*attr));
4123
4124 ret = get_user(size, &uattr->size);
4125 if (ret)
4126 return ret;
4127
4128 if (size > PAGE_SIZE) /* silly large */
4129 goto err_size;
4130
4131 if (!size) /* abi compat */
4132 size = PERF_ATTR_SIZE_VER0;
4133
4134 if (size < PERF_ATTR_SIZE_VER0)
4135 goto err_size;
4136
4137 /*
4138 * If we're handed a bigger struct than we know of,
4139 * ensure all the unknown bits are 0.
4140 */
4141 if (size > sizeof(*attr)) {
4142 unsigned long val;
4143 unsigned long __user *addr;
4144 unsigned long __user *end;
4145
4146 addr = PTR_ALIGN((void __user *)uattr + sizeof(*attr),
4147 sizeof(unsigned long));
4148 end = PTR_ALIGN((void __user *)uattr + size,
4149 sizeof(unsigned long));
4150
4151 for (; addr < end; addr += sizeof(unsigned long)) {
4152 ret = get_user(val, addr);
4153 if (ret)
4154 return ret;
4155 if (val)
4156 goto err_size;
4157 }
4158 }
4159
4160 ret = copy_from_user(attr, uattr, size);
4161 if (ret)
4162 return -EFAULT;
4163
4164 /*
4165 * If the type exists, the corresponding creation will verify
4166 * the attr->config.
4167 */
4168 if (attr->type >= PERF_TYPE_MAX)
4169 return -EINVAL;
4170
4171 if (attr->__reserved_1 || attr->__reserved_2 || attr->__reserved_3)
4172 return -EINVAL;
4173
4174 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
4175 return -EINVAL;
4176
4177 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
4178 return -EINVAL;
4179
4180out:
4181 return ret;
4182
4183err_size:
4184 put_user(sizeof(*attr), &uattr->size);
4185 ret = -E2BIG;
4186 goto out;
4187}
4188
Thomas Gleixner0793a612008-12-04 20:12:29 +01004189/**
Paul Mackerras2743a5b2009-03-04 20:36:51 +11004190 * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
Ingo Molnar9f66a382008-12-10 12:33:23 +01004191 *
Peter Zijlstra0d486962009-06-02 19:22:16 +02004192 * @attr_uptr: event type attributes for monitoring/sampling
Thomas Gleixner0793a612008-12-04 20:12:29 +01004193 * @pid: target pid
Ingo Molnar9f66a382008-12-10 12:33:23 +01004194 * @cpu: target cpu
4195 * @group_fd: group leader counter fd
Thomas Gleixner0793a612008-12-04 20:12:29 +01004196 */
Paul Mackerras2743a5b2009-03-04 20:36:51 +11004197SYSCALL_DEFINE5(perf_counter_open,
Peter Zijlstra974802e2009-06-12 12:46:55 +02004198 struct perf_counter_attr __user *, attr_uptr,
Paul Mackerras2743a5b2009-03-04 20:36:51 +11004199 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01004200{
Ingo Molnar04289bb2008-12-11 08:38:42 +01004201 struct perf_counter *counter, *group_leader;
Peter Zijlstra0d486962009-06-02 19:22:16 +02004202 struct perf_counter_attr attr;
Ingo Molnar04289bb2008-12-11 08:38:42 +01004203 struct perf_counter_context *ctx;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004204 struct file *counter_file = NULL;
Ingo Molnar04289bb2008-12-11 08:38:42 +01004205 struct file *group_file = NULL;
4206 int fput_needed = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004207 int fput_needed2 = 0;
Thomas Gleixner0793a612008-12-04 20:12:29 +01004208 int ret;
4209
Paul Mackerras2743a5b2009-03-04 20:36:51 +11004210 /* for future expandability... */
4211 if (flags)
4212 return -EINVAL;
4213
Peter Zijlstra974802e2009-06-12 12:46:55 +02004214 ret = perf_copy_attr(attr_uptr, &attr);
4215 if (ret)
4216 return ret;
Thomas Gleixnereab656a2008-12-08 19:26:59 +01004217
Peter Zijlstra07647712009-06-11 11:18:36 +02004218 if (!attr.exclude_kernel) {
4219 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
4220 return -EACCES;
4221 }
4222
Peter Zijlstradf58ab22009-06-11 11:25:05 +02004223 if (attr.freq) {
4224 if (attr.sample_freq > sysctl_perf_counter_sample_rate)
4225 return -EINVAL;
4226 }
4227
Ingo Molnar04289bb2008-12-11 08:38:42 +01004228 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01004229 * Get the target context (task or percpu):
4230 */
4231 ctx = find_get_context(pid, cpu);
4232 if (IS_ERR(ctx))
4233 return PTR_ERR(ctx);
4234
4235 /*
4236 * Look up the group leader (we will attach this counter to it):
Ingo Molnar04289bb2008-12-11 08:38:42 +01004237 */
4238 group_leader = NULL;
4239 if (group_fd != -1) {
4240 ret = -EINVAL;
4241 group_file = fget_light(group_fd, &fput_needed);
4242 if (!group_file)
Ingo Molnarccff2862008-12-11 11:26:29 +01004243 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01004244 if (group_file->f_op != &perf_fops)
Ingo Molnarccff2862008-12-11 11:26:29 +01004245 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01004246
4247 group_leader = group_file->private_data;
4248 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01004249 * Do not allow a recursive hierarchy (this new sibling
4250 * becoming part of another group-sibling):
Ingo Molnar04289bb2008-12-11 08:38:42 +01004251 */
Ingo Molnarccff2862008-12-11 11:26:29 +01004252 if (group_leader->group_leader != group_leader)
4253 goto err_put_context;
4254 /*
4255 * Do not allow to attach to a group in a different
4256 * task or CPU context:
4257 */
4258 if (group_leader->ctx != ctx)
4259 goto err_put_context;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11004260 /*
4261 * Only a group leader can be exclusive or pinned
4262 */
Peter Zijlstra0d486962009-06-02 19:22:16 +02004263 if (attr.exclusive || attr.pinned)
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11004264 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01004265 }
4266
Peter Zijlstra0d486962009-06-02 19:22:16 +02004267 counter = perf_counter_alloc(&attr, cpu, ctx, group_leader,
Peter Zijlstrab84fbc92009-06-22 13:57:40 +02004268 NULL, GFP_KERNEL);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02004269 ret = PTR_ERR(counter);
4270 if (IS_ERR(counter))
Thomas Gleixner0793a612008-12-04 20:12:29 +01004271 goto err_put_context;
4272
Thomas Gleixner0793a612008-12-04 20:12:29 +01004273 ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
4274 if (ret < 0)
Ingo Molnar9b51f662008-12-12 13:49:45 +01004275 goto err_free_put_context;
4276
4277 counter_file = fget_light(ret, &fput_needed2);
4278 if (!counter_file)
4279 goto err_free_put_context;
4280
4281 counter->filp = counter_file;
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004282 WARN_ON_ONCE(ctx->parent_ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +11004283 mutex_lock(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01004284 perf_install_in_context(ctx, counter, cpu);
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004285 ++ctx->generation;
Paul Mackerrasd859e292009-01-17 18:10:22 +11004286 mutex_unlock(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01004287
Peter Zijlstra082ff5a2009-05-23 18:29:00 +02004288 counter->owner = current;
4289 get_task_struct(current);
4290 mutex_lock(&current->perf_counter_mutex);
4291 list_add_tail(&counter->owner_entry, &current->perf_counter_list);
4292 mutex_unlock(&current->perf_counter_mutex);
4293
Ingo Molnar9b51f662008-12-12 13:49:45 +01004294 fput_light(counter_file, fput_needed2);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004295
Ingo Molnar04289bb2008-12-11 08:38:42 +01004296out_fput:
4297 fput_light(group_file, fput_needed);
4298
Thomas Gleixner0793a612008-12-04 20:12:29 +01004299 return ret;
4300
Ingo Molnar9b51f662008-12-12 13:49:45 +01004301err_free_put_context:
Thomas Gleixner0793a612008-12-04 20:12:29 +01004302 kfree(counter);
4303
4304err_put_context:
Paul Mackerrasc93f7662009-05-28 22:18:17 +10004305 put_ctx(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004306
Ingo Molnar04289bb2008-12-11 08:38:42 +01004307 goto out_fput;
Thomas Gleixner0793a612008-12-04 20:12:29 +01004308}
4309
Ingo Molnar9b51f662008-12-12 13:49:45 +01004310/*
Ingo Molnar9b51f662008-12-12 13:49:45 +01004311 * inherit a counter from parent task to child task:
4312 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11004313static struct perf_counter *
Ingo Molnar9b51f662008-12-12 13:49:45 +01004314inherit_counter(struct perf_counter *parent_counter,
4315 struct task_struct *parent,
4316 struct perf_counter_context *parent_ctx,
4317 struct task_struct *child,
Paul Mackerrasd859e292009-01-17 18:10:22 +11004318 struct perf_counter *group_leader,
Ingo Molnar9b51f662008-12-12 13:49:45 +01004319 struct perf_counter_context *child_ctx)
4320{
4321 struct perf_counter *child_counter;
4322
Paul Mackerrasd859e292009-01-17 18:10:22 +11004323 /*
4324 * Instead of creating recursive hierarchies of counters,
4325 * we link inherited counters back to the original parent,
4326 * which has a filp for sure, which we use as the reference
4327 * count:
4328 */
4329 if (parent_counter->parent)
4330 parent_counter = parent_counter->parent;
4331
Peter Zijlstra0d486962009-06-02 19:22:16 +02004332 child_counter = perf_counter_alloc(&parent_counter->attr,
Paul Mackerras23a185c2009-02-09 22:42:47 +11004333 parent_counter->cpu, child_ctx,
Peter Zijlstrab84fbc92009-06-22 13:57:40 +02004334 group_leader, parent_counter,
4335 GFP_KERNEL);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02004336 if (IS_ERR(child_counter))
4337 return child_counter;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10004338 get_ctx(child_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01004339
4340 /*
Paul Mackerras564c2b22009-05-22 14:27:22 +10004341 * Make the child state follow the state of the parent counter,
Peter Zijlstra0d486962009-06-02 19:22:16 +02004342 * not its attr.disabled bit. We hold the parent's mutex,
Ingo Molnar22a4f652009-06-01 10:13:37 +02004343 * so we won't race with perf_counter_{en, dis}able_family.
Paul Mackerras564c2b22009-05-22 14:27:22 +10004344 */
4345 if (parent_counter->state >= PERF_COUNTER_STATE_INACTIVE)
4346 child_counter->state = PERF_COUNTER_STATE_INACTIVE;
4347 else
4348 child_counter->state = PERF_COUNTER_STATE_OFF;
4349
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02004350 if (parent_counter->attr.freq)
4351 child_counter->hw.sample_period = parent_counter->hw.sample_period;
4352
Paul Mackerras564c2b22009-05-22 14:27:22 +10004353 /*
Ingo Molnar9b51f662008-12-12 13:49:45 +01004354 * Link it up in the child's context:
4355 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +11004356 add_counter_to_ctx(child_counter, child_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01004357
Ingo Molnar9b51f662008-12-12 13:49:45 +01004358 /*
4359 * Get a reference to the parent filp - we will fput it
4360 * when the child counter exits. This is safe to do because
4361 * we are in the parent and we know that the filp still
4362 * exists and has a nonzero count:
4363 */
4364 atomic_long_inc(&parent_counter->filp->f_count);
4365
Paul Mackerrasd859e292009-01-17 18:10:22 +11004366 /*
4367 * Link this into the parent counter's child list
4368 */
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004369 WARN_ON_ONCE(parent_counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02004370 mutex_lock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11004371 list_add_tail(&child_counter->child_list, &parent_counter->child_list);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02004372 mutex_unlock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11004373
4374 return child_counter;
4375}
4376
4377static int inherit_group(struct perf_counter *parent_counter,
4378 struct task_struct *parent,
4379 struct perf_counter_context *parent_ctx,
4380 struct task_struct *child,
4381 struct perf_counter_context *child_ctx)
4382{
4383 struct perf_counter *leader;
4384 struct perf_counter *sub;
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02004385 struct perf_counter *child_ctr;
Paul Mackerrasd859e292009-01-17 18:10:22 +11004386
4387 leader = inherit_counter(parent_counter, parent, parent_ctx,
4388 child, NULL, child_ctx);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02004389 if (IS_ERR(leader))
4390 return PTR_ERR(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +11004391 list_for_each_entry(sub, &parent_counter->sibling_list, list_entry) {
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02004392 child_ctr = inherit_counter(sub, parent, parent_ctx,
4393 child, leader, child_ctx);
4394 if (IS_ERR(child_ctr))
4395 return PTR_ERR(child_ctr);
Paul Mackerrasd859e292009-01-17 18:10:22 +11004396 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01004397 return 0;
4398}
4399
Paul Mackerrasd859e292009-01-17 18:10:22 +11004400static void sync_child_counter(struct perf_counter *child_counter,
Peter Zijlstra38b200d2009-06-23 20:13:11 +02004401 struct task_struct *child)
Paul Mackerrasd859e292009-01-17 18:10:22 +11004402{
Peter Zijlstra38b200d2009-06-23 20:13:11 +02004403 struct perf_counter *parent_counter = child_counter->parent;
Peter Zijlstra8bc20952009-05-15 20:45:59 +02004404 u64 child_val;
Paul Mackerrasd859e292009-01-17 18:10:22 +11004405
Peter Zijlstrabfbd3382009-06-24 21:11:59 +02004406 if (child_counter->attr.inherit_stat)
4407 perf_counter_read_event(child_counter, child);
Peter Zijlstra38b200d2009-06-23 20:13:11 +02004408
Paul Mackerrasd859e292009-01-17 18:10:22 +11004409 child_val = atomic64_read(&child_counter->count);
4410
4411 /*
4412 * Add back the child's count to the parent's count:
4413 */
4414 atomic64_add(child_val, &parent_counter->count);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11004415 atomic64_add(child_counter->total_time_enabled,
4416 &parent_counter->child_total_time_enabled);
4417 atomic64_add(child_counter->total_time_running,
4418 &parent_counter->child_total_time_running);
Paul Mackerrasd859e292009-01-17 18:10:22 +11004419
4420 /*
4421 * Remove this counter from the parent's list
4422 */
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004423 WARN_ON_ONCE(parent_counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02004424 mutex_lock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11004425 list_del_init(&child_counter->child_list);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02004426 mutex_unlock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11004427
4428 /*
4429 * Release the parent counter, if this was the last
4430 * reference to it.
4431 */
4432 fput(parent_counter->filp);
4433}
4434
Ingo Molnar9b51f662008-12-12 13:49:45 +01004435static void
Peter Zijlstrabbbee902009-05-29 14:25:58 +02004436__perf_counter_exit_task(struct perf_counter *child_counter,
Peter Zijlstra38b200d2009-06-23 20:13:11 +02004437 struct perf_counter_context *child_ctx,
4438 struct task_struct *child)
Ingo Molnar9b51f662008-12-12 13:49:45 +01004439{
4440 struct perf_counter *parent_counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004441
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004442 update_counter_times(child_counter);
Peter Zijlstraaa9c67f2009-05-23 18:28:59 +02004443 perf_counter_remove_from_context(child_counter);
Ingo Molnar0cc0c022008-12-14 23:20:36 +01004444
Ingo Molnar9b51f662008-12-12 13:49:45 +01004445 parent_counter = child_counter->parent;
4446 /*
4447 * It can happen that parent exits first, and has counters
4448 * that are still around due to the child reference. These
4449 * counters need to be zapped - but otherwise linger.
4450 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11004451 if (parent_counter) {
Peter Zijlstra38b200d2009-06-23 20:13:11 +02004452 sync_child_counter(child_counter, child);
Peter Zijlstraf1600952009-03-19 20:26:16 +01004453 free_counter(child_counter);
Paul Mackerras4bcf3492009-02-11 13:53:19 +01004454 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01004455}
4456
4457/*
Paul Mackerrasd859e292009-01-17 18:10:22 +11004458 * When a child task exits, feed back counter values to parent counters.
Ingo Molnar9b51f662008-12-12 13:49:45 +01004459 */
4460void perf_counter_exit_task(struct task_struct *child)
4461{
4462 struct perf_counter *child_counter, *tmp;
4463 struct perf_counter_context *child_ctx;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004464 unsigned long flags;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004465
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02004466 if (likely(!child->perf_counter_ctxp)) {
Peter Zijlstra3a80b4a2009-08-07 19:49:01 +02004467 perf_counter_task(child, NULL, 0);
Ingo Molnar9b51f662008-12-12 13:49:45 +01004468 return;
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02004469 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01004470
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004471 local_irq_save(flags);
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004472 /*
4473 * We can't reschedule here because interrupts are disabled,
4474 * and either child is current or it is a task that can't be
4475 * scheduled, so we are now safe from rescheduling changing
4476 * our context.
4477 */
4478 child_ctx = child->perf_counter_ctxp;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004479 __perf_counter_task_sched_out(child_ctx);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10004480
4481 /*
4482 * Take the context lock here so that if find_get_context is
4483 * reading child->perf_counter_ctxp, we wait until it has
4484 * incremented the context's refcount before we do put_ctx below.
4485 */
4486 spin_lock(&child_ctx->lock);
Peter Zijlstra3a80b4a2009-08-07 19:49:01 +02004487 child->perf_counter_ctxp = NULL;
Peter Zijlstra71a851b2009-07-10 09:06:56 +02004488 /*
4489 * If this context is a clone; unclone it so it can't get
4490 * swapped to another process while we're removing all
4491 * the counters from it.
4492 */
4493 unclone_ctx(child_ctx);
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02004494 spin_unlock_irqrestore(&child_ctx->lock, flags);
4495
4496 /*
4497 * Report the task dead after unscheduling the counters so that we
4498 * won't get any samples after PERF_EVENT_EXIT. We can however still
4499 * get a few PERF_EVENT_READ events.
4500 */
Peter Zijlstra3a80b4a2009-08-07 19:49:01 +02004501 perf_counter_task(child, child_ctx, 0);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004502
Peter Zijlstra66fff222009-06-10 22:53:37 +02004503 /*
4504 * We can recurse on the same lock type through:
4505 *
4506 * __perf_counter_exit_task()
4507 * sync_child_counter()
4508 * fput(parent_counter->filp)
4509 * perf_release()
4510 * mutex_lock(&ctx->mutex)
4511 *
4512 * But since its the parent context it won't be the same instance.
4513 */
4514 mutex_lock_nested(&child_ctx->mutex, SINGLE_DEPTH_NESTING);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004515
Peter Zijlstra8bc20952009-05-15 20:45:59 +02004516again:
Ingo Molnar9b51f662008-12-12 13:49:45 +01004517 list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list,
4518 list_entry)
Peter Zijlstra38b200d2009-06-23 20:13:11 +02004519 __perf_counter_exit_task(child_counter, child_ctx, child);
Peter Zijlstra8bc20952009-05-15 20:45:59 +02004520
4521 /*
4522 * If the last counter was a group counter, it will have appended all
4523 * its siblings to the list, but we obtained 'tmp' before that which
4524 * will still point to the list head terminating the iteration.
4525 */
4526 if (!list_empty(&child_ctx->counter_list))
4527 goto again;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004528
4529 mutex_unlock(&child_ctx->mutex);
4530
4531 put_ctx(child_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01004532}
4533
4534/*
Peter Zijlstrabbbee902009-05-29 14:25:58 +02004535 * free an unexposed, unused context as created by inheritance by
4536 * init_task below, used by fork() in case of fail.
4537 */
4538void perf_counter_free_task(struct task_struct *task)
4539{
4540 struct perf_counter_context *ctx = task->perf_counter_ctxp;
4541 struct perf_counter *counter, *tmp;
4542
4543 if (!ctx)
4544 return;
4545
4546 mutex_lock(&ctx->mutex);
4547again:
4548 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry) {
4549 struct perf_counter *parent = counter->parent;
4550
4551 if (WARN_ON_ONCE(!parent))
4552 continue;
4553
4554 mutex_lock(&parent->child_mutex);
4555 list_del_init(&counter->child_list);
4556 mutex_unlock(&parent->child_mutex);
4557
4558 fput(parent->filp);
4559
4560 list_del_counter(counter, ctx);
4561 free_counter(counter);
4562 }
4563
4564 if (!list_empty(&ctx->counter_list))
4565 goto again;
4566
4567 mutex_unlock(&ctx->mutex);
4568
4569 put_ctx(ctx);
4570}
4571
4572/*
Ingo Molnar9b51f662008-12-12 13:49:45 +01004573 * Initialize the perf_counter context in task_struct
4574 */
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02004575int perf_counter_init_task(struct task_struct *child)
Ingo Molnar9b51f662008-12-12 13:49:45 +01004576{
4577 struct perf_counter_context *child_ctx, *parent_ctx;
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004578 struct perf_counter_context *cloned_ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +11004579 struct perf_counter *counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004580 struct task_struct *parent = current;
Paul Mackerras564c2b22009-05-22 14:27:22 +10004581 int inherited_all = 1;
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02004582 int ret = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004583
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004584 child->perf_counter_ctxp = NULL;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004585
Peter Zijlstra082ff5a2009-05-23 18:29:00 +02004586 mutex_init(&child->perf_counter_mutex);
4587 INIT_LIST_HEAD(&child->perf_counter_list);
4588
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004589 if (likely(!parent->perf_counter_ctxp))
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02004590 return 0;
4591
Ingo Molnar9b51f662008-12-12 13:49:45 +01004592 /*
4593 * This is executed from the parent task context, so inherit
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004594 * counters that have been marked for cloning.
4595 * First allocate and initialize a context for the child.
Ingo Molnar9b51f662008-12-12 13:49:45 +01004596 */
4597
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004598 child_ctx = kmalloc(sizeof(struct perf_counter_context), GFP_KERNEL);
4599 if (!child_ctx)
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02004600 return -ENOMEM;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004601
4602 __perf_counter_init_context(child_ctx, child);
4603 child->perf_counter_ctxp = child_ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10004604 get_task_struct(child);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004605
Ingo Molnar9b51f662008-12-12 13:49:45 +01004606 /*
Paul Mackerras25346b932009-06-01 17:48:12 +10004607 * If the parent's context is a clone, pin it so it won't get
4608 * swapped under us.
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004609 */
Paul Mackerras25346b932009-06-01 17:48:12 +10004610 parent_ctx = perf_pin_task_context(parent);
4611
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004612 /*
4613 * No need to check if parent_ctx != NULL here; since we saw
4614 * it non-NULL earlier, the only reason for it to become NULL
4615 * is if we exit, and since we're currently in the middle of
4616 * a fork we can't be exiting at the same time.
4617 */
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004618
4619 /*
Ingo Molnar9b51f662008-12-12 13:49:45 +01004620 * Lock the parent list. No need to lock the child - not PID
4621 * hashed yet and not running, so nobody can access it.
4622 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11004623 mutex_lock(&parent_ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01004624
4625 /*
4626 * We dont have to disable NMIs - we are only looking at
4627 * the list, not manipulating it:
4628 */
Peter Zijlstrad7b629a2009-05-20 12:21:19 +02004629 list_for_each_entry_rcu(counter, &parent_ctx->event_list, event_entry) {
4630 if (counter != counter->group_leader)
4631 continue;
4632
Peter Zijlstra0d486962009-06-02 19:22:16 +02004633 if (!counter->attr.inherit) {
Paul Mackerras564c2b22009-05-22 14:27:22 +10004634 inherited_all = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004635 continue;
Paul Mackerras564c2b22009-05-22 14:27:22 +10004636 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01004637
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02004638 ret = inherit_group(counter, parent, parent_ctx,
4639 child, child_ctx);
4640 if (ret) {
Paul Mackerras564c2b22009-05-22 14:27:22 +10004641 inherited_all = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004642 break;
Paul Mackerras564c2b22009-05-22 14:27:22 +10004643 }
4644 }
4645
4646 if (inherited_all) {
4647 /*
4648 * Mark the child context as a clone of the parent
4649 * context, or of whatever the parent is a clone of.
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004650 * Note that if the parent is a clone, it could get
4651 * uncloned at any point, but that doesn't matter
4652 * because the list of counters and the generation
4653 * count can't have changed since we took the mutex.
Paul Mackerras564c2b22009-05-22 14:27:22 +10004654 */
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004655 cloned_ctx = rcu_dereference(parent_ctx->parent_ctx);
4656 if (cloned_ctx) {
4657 child_ctx->parent_ctx = cloned_ctx;
Paul Mackerras25346b932009-06-01 17:48:12 +10004658 child_ctx->parent_gen = parent_ctx->parent_gen;
Paul Mackerras564c2b22009-05-22 14:27:22 +10004659 } else {
4660 child_ctx->parent_ctx = parent_ctx;
4661 child_ctx->parent_gen = parent_ctx->generation;
4662 }
4663 get_ctx(child_ctx->parent_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01004664 }
4665
Paul Mackerrasd859e292009-01-17 18:10:22 +11004666 mutex_unlock(&parent_ctx->mutex);
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02004667
Paul Mackerras25346b932009-06-01 17:48:12 +10004668 perf_unpin_context(parent_ctx);
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004669
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02004670 return ret;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004671}
4672
Ingo Molnar04289bb2008-12-11 08:38:42 +01004673static void __cpuinit perf_counter_init_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01004674{
Ingo Molnar04289bb2008-12-11 08:38:42 +01004675 struct perf_cpu_context *cpuctx;
Thomas Gleixner0793a612008-12-04 20:12:29 +01004676
Ingo Molnar04289bb2008-12-11 08:38:42 +01004677 cpuctx = &per_cpu(perf_cpu_context, cpu);
4678 __perf_counter_init_context(&cpuctx->ctx, NULL);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004679
Ingo Molnar1dce8d92009-05-04 19:23:18 +02004680 spin_lock(&perf_resource_lock);
Ingo Molnar04289bb2008-12-11 08:38:42 +01004681 cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu;
Ingo Molnar1dce8d92009-05-04 19:23:18 +02004682 spin_unlock(&perf_resource_lock);
Ingo Molnar04289bb2008-12-11 08:38:42 +01004683
Paul Mackerras01d02872009-01-14 13:44:19 +11004684 hw_perf_counter_setup(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004685}
4686
4687#ifdef CONFIG_HOTPLUG_CPU
Ingo Molnar04289bb2008-12-11 08:38:42 +01004688static void __perf_counter_exit_cpu(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01004689{
4690 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
4691 struct perf_counter_context *ctx = &cpuctx->ctx;
4692 struct perf_counter *counter, *tmp;
4693
Ingo Molnar04289bb2008-12-11 08:38:42 +01004694 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry)
4695 __perf_counter_remove_from_context(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004696}
Ingo Molnar04289bb2008-12-11 08:38:42 +01004697static void perf_counter_exit_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01004698{
Paul Mackerrasd859e292009-01-17 18:10:22 +11004699 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
4700 struct perf_counter_context *ctx = &cpuctx->ctx;
4701
4702 mutex_lock(&ctx->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01004703 smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1);
Paul Mackerrasd859e292009-01-17 18:10:22 +11004704 mutex_unlock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004705}
4706#else
Ingo Molnar04289bb2008-12-11 08:38:42 +01004707static inline void perf_counter_exit_cpu(int cpu) { }
Thomas Gleixner0793a612008-12-04 20:12:29 +01004708#endif
4709
4710static int __cpuinit
4711perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
4712{
4713 unsigned int cpu = (long)hcpu;
4714
4715 switch (action) {
4716
4717 case CPU_UP_PREPARE:
4718 case CPU_UP_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01004719 perf_counter_init_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004720 break;
4721
Ingo Molnar28402972009-08-13 10:13:22 +02004722 case CPU_ONLINE:
4723 case CPU_ONLINE_FROZEN:
4724 hw_perf_counter_setup_online(cpu);
4725 break;
4726
Thomas Gleixner0793a612008-12-04 20:12:29 +01004727 case CPU_DOWN_PREPARE:
4728 case CPU_DOWN_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01004729 perf_counter_exit_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004730 break;
4731
4732 default:
4733 break;
4734 }
4735
4736 return NOTIFY_OK;
4737}
4738
Paul Mackerrasf38b0822009-06-02 21:05:16 +10004739/*
4740 * This has to have a higher priority than migration_notifier in sched.c.
4741 */
Thomas Gleixner0793a612008-12-04 20:12:29 +01004742static struct notifier_block __cpuinitdata perf_cpu_nb = {
4743 .notifier_call = perf_cpu_notify,
Paul Mackerrasf38b0822009-06-02 21:05:16 +10004744 .priority = 20,
Thomas Gleixner0793a612008-12-04 20:12:29 +01004745};
4746
Ingo Molnar0d905bc2009-05-04 19:13:30 +02004747void __init perf_counter_init(void)
Thomas Gleixner0793a612008-12-04 20:12:29 +01004748{
4749 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
4750 (void *)(long)smp_processor_id());
Ingo Molnar28402972009-08-13 10:13:22 +02004751 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_ONLINE,
4752 (void *)(long)smp_processor_id());
Thomas Gleixner0793a612008-12-04 20:12:29 +01004753 register_cpu_notifier(&perf_cpu_nb);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004754}
Thomas Gleixner0793a612008-12-04 20:12:29 +01004755
4756static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
4757{
4758 return sprintf(buf, "%d\n", perf_reserved_percpu);
4759}
4760
4761static ssize_t
4762perf_set_reserve_percpu(struct sysdev_class *class,
4763 const char *buf,
4764 size_t count)
4765{
4766 struct perf_cpu_context *cpuctx;
4767 unsigned long val;
4768 int err, cpu, mpt;
4769
4770 err = strict_strtoul(buf, 10, &val);
4771 if (err)
4772 return err;
4773 if (val > perf_max_counters)
4774 return -EINVAL;
4775
Ingo Molnar1dce8d92009-05-04 19:23:18 +02004776 spin_lock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004777 perf_reserved_percpu = val;
4778 for_each_online_cpu(cpu) {
4779 cpuctx = &per_cpu(perf_cpu_context, cpu);
4780 spin_lock_irq(&cpuctx->ctx.lock);
4781 mpt = min(perf_max_counters - cpuctx->ctx.nr_counters,
4782 perf_max_counters - perf_reserved_percpu);
4783 cpuctx->max_pertask = mpt;
4784 spin_unlock_irq(&cpuctx->ctx.lock);
4785 }
Ingo Molnar1dce8d92009-05-04 19:23:18 +02004786 spin_unlock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004787
4788 return count;
4789}
4790
4791static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
4792{
4793 return sprintf(buf, "%d\n", perf_overcommit);
4794}
4795
4796static ssize_t
4797perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
4798{
4799 unsigned long val;
4800 int err;
4801
4802 err = strict_strtoul(buf, 10, &val);
4803 if (err)
4804 return err;
4805 if (val > 1)
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_overcommit = val;
Ingo Molnar1dce8d92009-05-04 19:23:18 +02004810 spin_unlock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004811
4812 return count;
4813}
4814
4815static SYSDEV_CLASS_ATTR(
4816 reserve_percpu,
4817 0644,
4818 perf_show_reserve_percpu,
4819 perf_set_reserve_percpu
4820 );
4821
4822static SYSDEV_CLASS_ATTR(
4823 overcommit,
4824 0644,
4825 perf_show_overcommit,
4826 perf_set_overcommit
4827 );
4828
4829static struct attribute *perfclass_attrs[] = {
4830 &attr_reserve_percpu.attr,
4831 &attr_overcommit.attr,
4832 NULL
4833};
4834
4835static struct attribute_group perfclass_attr_group = {
4836 .attrs = perfclass_attrs,
4837 .name = "perf_counters",
4838};
4839
4840static int __init perf_counter_sysfs_init(void)
4841{
4842 return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
4843 &perfclass_attr_group);
4844}
4845device_initcall(perf_counter_sysfs_init);