blob: 3dd4339589a073dec64998153fadc1cfcdb2fdf7 [file] [log] [blame]
Thomas Gleixner0793a612008-12-04 20:12:29 +01001/*
2 * Performance counter core code
3 *
Ingo Molnar98144512009-04-29 14:52:50 +02004 * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
5 * Copyright (C) 2008-2009 Red Hat, Inc., Ingo Molnar
6 * Copyright (C) 2008-2009 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
Paul Mackerrasc5dd0162009-04-30 09:48:16 +10007 * Copyright © 2009 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
Peter Zijlstra7b732a72009-03-23 18:22:10 +01008 *
9 * For licensing details see kernel-base/COPYING
Thomas Gleixner0793a612008-12-04 20:12:29 +010010 */
11
12#include <linux/fs.h>
Peter Zijlstrab9cacc72009-03-25 12:30:22 +010013#include <linux/mm.h>
Thomas Gleixner0793a612008-12-04 20:12:29 +010014#include <linux/cpu.h>
15#include <linux/smp.h>
Ingo Molnar04289bb2008-12-11 08:38:42 +010016#include <linux/file.h>
Thomas Gleixner0793a612008-12-04 20:12:29 +010017#include <linux/poll.h>
18#include <linux/sysfs.h>
Ingo Molnar22a4f652009-06-01 10:13:37 +020019#include <linux/dcache.h>
Thomas Gleixner0793a612008-12-04 20:12:29 +010020#include <linux/percpu.h>
Ingo Molnar22a4f652009-06-01 10:13:37 +020021#include <linux/ptrace.h>
Peter Zijlstrab9cacc72009-03-25 12:30:22 +010022#include <linux/vmstat.h>
23#include <linux/hardirq.h>
24#include <linux/rculist.h>
Thomas Gleixner0793a612008-12-04 20:12:29 +010025#include <linux/uaccess.h>
26#include <linux/syscalls.h>
27#include <linux/anon_inodes.h>
Ingo Molnaraa9c4c02008-12-17 14:10:57 +010028#include <linux/kernel_stat.h>
Thomas Gleixner0793a612008-12-04 20:12:29 +010029#include <linux/perf_counter.h>
30
Tim Blechmann4e193bd2009-03-14 14:29:25 +010031#include <asm/irq_regs.h>
32
Thomas Gleixner0793a612008-12-04 20:12:29 +010033/*
34 * Each CPU has a list of per CPU counters:
35 */
36DEFINE_PER_CPU(struct perf_cpu_context, perf_cpu_context);
37
Ingo Molnar088e2852008-12-14 20:21:00 +010038int perf_max_counters __read_mostly = 1;
Thomas Gleixner0793a612008-12-04 20:12:29 +010039static int perf_reserved_percpu __read_mostly;
40static int perf_overcommit __read_mostly = 1;
41
Peter Zijlstra7fc23a52009-05-08 18:52:21 +020042static atomic_t nr_counters __read_mostly;
Peter Zijlstra60313eb2009-06-04 16:53:44 +020043static atomic_t nr_mmap_counters __read_mostly;
Peter Zijlstra60313eb2009-06-04 16:53:44 +020044static atomic_t nr_comm_counters __read_mostly;
Peter Zijlstra9f498cc2009-07-23 14:46:33 +020045static atomic_t nr_task_counters __read_mostly;
Peter Zijlstra9ee318a2009-04-09 10:53:44 +020046
Peter Zijlstra07647712009-06-11 11:18:36 +020047/*
Peter Zijlstradf58ab22009-06-11 11:25:05 +020048 * perf counter paranoia level:
49 * 0 - not paranoid
50 * 1 - disallow cpu counters to unpriv
51 * 2 - disallow kernel profiling to unpriv
Peter Zijlstra07647712009-06-11 11:18:36 +020052 */
Peter Zijlstradf58ab22009-06-11 11:25:05 +020053int sysctl_perf_counter_paranoid __read_mostly;
Peter Zijlstra07647712009-06-11 11:18:36 +020054
55static inline bool perf_paranoid_cpu(void)
56{
57 return sysctl_perf_counter_paranoid > 0;
58}
59
60static inline bool perf_paranoid_kernel(void)
61{
62 return sysctl_perf_counter_paranoid > 1;
63}
64
Peter Zijlstra789f90f2009-05-15 15:19:27 +020065int sysctl_perf_counter_mlock __read_mostly = 512; /* 'free' kb per user */
Peter Zijlstradf58ab22009-06-11 11:25:05 +020066
67/*
68 * max perf counter sample rate
69 */
70int sysctl_perf_counter_sample_rate __read_mostly = 100000;
Peter Zijlstra1ccd1542009-04-09 10:53:45 +020071
Peter Zijlstraa96bbc12009-06-03 14:01:36 +020072static atomic64_t perf_counter_id;
73
Thomas Gleixner0793a612008-12-04 20:12:29 +010074/*
Ingo Molnar1dce8d92009-05-04 19:23:18 +020075 * Lock for (sysadmin-configurable) counter reservations:
Thomas Gleixner0793a612008-12-04 20:12:29 +010076 */
Ingo Molnar1dce8d92009-05-04 19:23:18 +020077static DEFINE_SPINLOCK(perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +010078
79/*
80 * Architecture provided APIs - weak aliases:
81 */
Robert Richter4aeb0b42009-04-29 12:47:03 +020082extern __weak const struct pmu *hw_perf_counter_init(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +010083{
Paul Mackerrasff6f0542009-01-09 16:19:25 +110084 return NULL;
Thomas Gleixner0793a612008-12-04 20:12:29 +010085}
86
Peter Zijlstra9e35ad32009-05-13 16:21:38 +020087void __weak hw_perf_disable(void) { barrier(); }
88void __weak hw_perf_enable(void) { barrier(); }
89
Paul Mackerras01d02872009-01-14 13:44:19 +110090void __weak hw_perf_counter_setup(int cpu) { barrier(); }
Ingo Molnar28402972009-08-13 10:13:22 +020091void __weak hw_perf_counter_setup_online(int cpu) { barrier(); }
Ingo Molnar22a4f652009-06-01 10:13:37 +020092
93int __weak
94hw_perf_group_sched_in(struct perf_counter *group_leader,
Paul Mackerras3cbed422009-01-09 16:43:42 +110095 struct perf_cpu_context *cpuctx,
96 struct perf_counter_context *ctx, int cpu)
97{
98 return 0;
99}
Thomas Gleixner0793a612008-12-04 20:12:29 +0100100
Paul Mackerras4eb96fc2009-01-09 17:24:34 +1100101void __weak perf_counter_print_debug(void) { }
102
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200103static DEFINE_PER_CPU(int, disable_count);
104
105void __perf_disable(void)
106{
107 __get_cpu_var(disable_count)++;
108}
109
110bool __perf_enable(void)
111{
112 return !--__get_cpu_var(disable_count);
113}
114
115void perf_disable(void)
116{
117 __perf_disable();
118 hw_perf_disable();
119}
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200120
121void perf_enable(void)
122{
123 if (__perf_enable())
124 hw_perf_enable();
125}
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200126
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000127static void get_ctx(struct perf_counter_context *ctx)
128{
Peter Zijlstrae5289d42009-06-19 13:22:51 +0200129 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000130}
131
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000132static void free_ctx(struct rcu_head *head)
133{
134 struct perf_counter_context *ctx;
135
136 ctx = container_of(head, struct perf_counter_context, rcu_head);
137 kfree(ctx);
138}
139
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000140static void put_ctx(struct perf_counter_context *ctx)
141{
Paul Mackerras564c2b22009-05-22 14:27:22 +1000142 if (atomic_dec_and_test(&ctx->refcount)) {
143 if (ctx->parent_ctx)
144 put_ctx(ctx->parent_ctx);
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000145 if (ctx->task)
146 put_task_struct(ctx->task);
147 call_rcu(&ctx->rcu_head, free_ctx);
Paul Mackerras564c2b22009-05-22 14:27:22 +1000148 }
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000149}
150
Peter Zijlstra71a851b2009-07-10 09:06:56 +0200151static void unclone_ctx(struct perf_counter_context *ctx)
152{
153 if (ctx->parent_ctx) {
154 put_ctx(ctx->parent_ctx);
155 ctx->parent_ctx = NULL;
156 }
157}
158
Peter Zijlstrafccc7142009-05-23 18:28:56 +0200159/*
Peter Zijlstra7f453c22009-07-21 13:19:40 +0200160 * If we inherit counters we want to return the parent counter id
161 * to userspace.
162 */
163static u64 primary_counter_id(struct perf_counter *counter)
164{
165 u64 id = counter->id;
166
167 if (counter->parent)
168 id = counter->parent->id;
169
170 return id;
171}
172
Peter Zijlstrafccc7142009-05-23 18:28:56 +0200173/*
Paul Mackerras25346b92009-06-01 17:48:12 +1000174 * Get the perf_counter_context for a task and lock it.
175 * This has to cope with with the fact that until it is locked,
176 * the context could get moved to another task.
177 */
Ingo Molnar22a4f652009-06-01 10:13:37 +0200178static struct perf_counter_context *
179perf_lock_task_context(struct task_struct *task, unsigned long *flags)
Paul Mackerras25346b92009-06-01 17:48:12 +1000180{
181 struct perf_counter_context *ctx;
182
183 rcu_read_lock();
184 retry:
185 ctx = rcu_dereference(task->perf_counter_ctxp);
186 if (ctx) {
187 /*
188 * If this context is a clone of another, it might
189 * get swapped for another underneath us by
190 * perf_counter_task_sched_out, though the
191 * rcu_read_lock() protects us from any context
192 * getting freed. Lock the context and check if it
193 * got swapped before we could get the lock, and retry
194 * if so. If we locked the right context, then it
195 * can't get swapped on us any more.
196 */
197 spin_lock_irqsave(&ctx->lock, *flags);
198 if (ctx != rcu_dereference(task->perf_counter_ctxp)) {
199 spin_unlock_irqrestore(&ctx->lock, *flags);
200 goto retry;
201 }
Peter Zijlstrab49a9e72009-06-19 17:39:33 +0200202
203 if (!atomic_inc_not_zero(&ctx->refcount)) {
204 spin_unlock_irqrestore(&ctx->lock, *flags);
205 ctx = NULL;
206 }
Paul Mackerras25346b92009-06-01 17:48:12 +1000207 }
208 rcu_read_unlock();
209 return ctx;
210}
211
212/*
213 * Get the context for a task and increment its pin_count so it
214 * can't get swapped to another task. This also increments its
215 * reference count so that the context can't get freed.
216 */
217static struct perf_counter_context *perf_pin_task_context(struct task_struct *task)
218{
219 struct perf_counter_context *ctx;
220 unsigned long flags;
221
222 ctx = perf_lock_task_context(task, &flags);
223 if (ctx) {
224 ++ctx->pin_count;
Paul Mackerras25346b92009-06-01 17:48:12 +1000225 spin_unlock_irqrestore(&ctx->lock, flags);
226 }
227 return ctx;
228}
229
230static void perf_unpin_context(struct perf_counter_context *ctx)
231{
232 unsigned long flags;
233
234 spin_lock_irqsave(&ctx->lock, flags);
235 --ctx->pin_count;
236 spin_unlock_irqrestore(&ctx->lock, flags);
237 put_ctx(ctx);
238}
239
240/*
Peter Zijlstrafccc7142009-05-23 18:28:56 +0200241 * Add a counter from the lists for its context.
242 * Must be called with ctx->mutex and ctx->lock held.
243 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100244static void
245list_add_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
246{
247 struct perf_counter *group_leader = counter->group_leader;
248
249 /*
250 * Depending on whether it is a standalone or sibling counter,
251 * add it straight to the context's counter list, or to the group
252 * leader's sibling list:
253 */
Peter Zijlstra3df5eda2009-05-08 18:52:22 +0200254 if (group_leader == counter)
Ingo Molnar04289bb2008-12-11 08:38:42 +0100255 list_add_tail(&counter->list_entry, &ctx->counter_list);
Peter Zijlstra5c148192009-03-25 12:30:23 +0100256 else {
Ingo Molnar04289bb2008-12-11 08:38:42 +0100257 list_add_tail(&counter->list_entry, &group_leader->sibling_list);
Peter Zijlstra5c148192009-03-25 12:30:23 +0100258 group_leader->nr_siblings++;
259 }
Peter Zijlstra592903c2009-03-13 12:21:36 +0100260
261 list_add_rcu(&counter->event_entry, &ctx->event_list);
Peter Zijlstra8bc20952009-05-15 20:45:59 +0200262 ctx->nr_counters++;
Peter Zijlstrabfbd3382009-06-24 21:11:59 +0200263 if (counter->attr.inherit_stat)
264 ctx->nr_stat++;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100265}
266
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000267/*
268 * Remove a counter from the lists for its context.
Peter Zijlstrafccc7142009-05-23 18:28:56 +0200269 * Must be called with ctx->mutex and ctx->lock held.
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000270 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100271static void
272list_del_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
273{
274 struct perf_counter *sibling, *tmp;
275
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000276 if (list_empty(&counter->list_entry))
277 return;
Peter Zijlstra8bc20952009-05-15 20:45:59 +0200278 ctx->nr_counters--;
Peter Zijlstrabfbd3382009-06-24 21:11:59 +0200279 if (counter->attr.inherit_stat)
280 ctx->nr_stat--;
Peter Zijlstra8bc20952009-05-15 20:45:59 +0200281
Ingo Molnar04289bb2008-12-11 08:38:42 +0100282 list_del_init(&counter->list_entry);
Peter Zijlstra592903c2009-03-13 12:21:36 +0100283 list_del_rcu(&counter->event_entry);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100284
Peter Zijlstra5c148192009-03-25 12:30:23 +0100285 if (counter->group_leader != counter)
286 counter->group_leader->nr_siblings--;
287
Ingo Molnar04289bb2008-12-11 08:38:42 +0100288 /*
289 * If this was a group counter with sibling counters then
290 * upgrade the siblings to singleton counters by adding them
291 * to the context list directly:
292 */
293 list_for_each_entry_safe(sibling, tmp,
294 &counter->sibling_list, list_entry) {
295
Peter Zijlstra75564232009-03-13 12:21:29 +0100296 list_move_tail(&sibling->list_entry, &ctx->counter_list);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100297 sibling->group_leader = sibling;
298 }
299}
300
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100301static void
302counter_sched_out(struct perf_counter *counter,
303 struct perf_cpu_context *cpuctx,
304 struct perf_counter_context *ctx)
305{
306 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
307 return;
308
309 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200310 counter->tstamp_stopped = ctx->time;
Robert Richter4aeb0b42009-04-29 12:47:03 +0200311 counter->pmu->disable(counter);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100312 counter->oncpu = -1;
313
314 if (!is_software_counter(counter))
315 cpuctx->active_oncpu--;
316 ctx->nr_active--;
Peter Zijlstra0d486962009-06-02 19:22:16 +0200317 if (counter->attr.exclusive || !cpuctx->active_oncpu)
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100318 cpuctx->exclusive = 0;
319}
320
Paul Mackerrasd859e292009-01-17 18:10:22 +1100321static void
322group_sched_out(struct perf_counter *group_counter,
323 struct perf_cpu_context *cpuctx,
324 struct perf_counter_context *ctx)
325{
326 struct perf_counter *counter;
327
328 if (group_counter->state != PERF_COUNTER_STATE_ACTIVE)
329 return;
330
331 counter_sched_out(group_counter, cpuctx, ctx);
332
333 /*
334 * Schedule out siblings (if any):
335 */
336 list_for_each_entry(counter, &group_counter->sibling_list, list_entry)
337 counter_sched_out(counter, cpuctx, ctx);
338
Peter Zijlstra0d486962009-06-02 19:22:16 +0200339 if (group_counter->attr.exclusive)
Paul Mackerrasd859e292009-01-17 18:10:22 +1100340 cpuctx->exclusive = 0;
341}
342
Thomas Gleixner0793a612008-12-04 20:12:29 +0100343/*
344 * Cross CPU call to remove a performance counter
345 *
346 * We disable the counter on the hardware level first. After that we
347 * remove it from the context list.
348 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100349static void __perf_counter_remove_from_context(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100350{
351 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
352 struct perf_counter *counter = info;
353 struct perf_counter_context *ctx = counter->ctx;
354
355 /*
356 * If this is a task context, we need to check whether it is
357 * the current task context of this cpu. If not it has been
358 * scheduled out before the smp call arrived.
359 */
Peter Zijlstra665c2142009-05-29 14:51:57 +0200360 if (ctx->task && cpuctx->task_ctx != ctx)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100361 return;
362
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200363 spin_lock(&ctx->lock);
Ingo Molnar34adc802009-05-20 20:13:28 +0200364 /*
365 * Protect the list operation against NMI by disabling the
366 * counters on a global level.
367 */
368 perf_disable();
Thomas Gleixner0793a612008-12-04 20:12:29 +0100369
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100370 counter_sched_out(counter, cpuctx, ctx);
371
Ingo Molnar04289bb2008-12-11 08:38:42 +0100372 list_del_counter(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100373
374 if (!ctx->task) {
375 /*
376 * Allow more per task counters with respect to the
377 * reservation:
378 */
379 cpuctx->max_pertask =
380 min(perf_max_counters - ctx->nr_counters,
381 perf_max_counters - perf_reserved_percpu);
382 }
383
Ingo Molnar34adc802009-05-20 20:13:28 +0200384 perf_enable();
Peter Zijlstra665c2142009-05-29 14:51:57 +0200385 spin_unlock(&ctx->lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100386}
387
388
389/*
390 * Remove the counter from a task's (or a CPU's) list of counters.
391 *
Peter Zijlstrafccc7142009-05-23 18:28:56 +0200392 * Must be called with ctx->mutex held.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100393 *
394 * CPU counters are removed with a smp call. For task counters we only
395 * call when the task is on a CPU.
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000396 *
397 * If counter->ctx is a cloned context, callers must make sure that
398 * every task struct that counter->ctx->task could possibly point to
399 * remains valid. This is OK when called from perf_release since
400 * that only calls us on the top-level context, which can't be a clone.
401 * When called from perf_counter_exit_task, it's OK because the
402 * context has been detached from its task.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100403 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100404static void perf_counter_remove_from_context(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100405{
406 struct perf_counter_context *ctx = counter->ctx;
407 struct task_struct *task = ctx->task;
408
409 if (!task) {
410 /*
411 * Per cpu counters are removed via an smp call and
412 * the removal is always sucessful.
413 */
414 smp_call_function_single(counter->cpu,
Ingo Molnar04289bb2008-12-11 08:38:42 +0100415 __perf_counter_remove_from_context,
Thomas Gleixner0793a612008-12-04 20:12:29 +0100416 counter, 1);
417 return;
418 }
419
420retry:
Ingo Molnar04289bb2008-12-11 08:38:42 +0100421 task_oncpu_function_call(task, __perf_counter_remove_from_context,
Thomas Gleixner0793a612008-12-04 20:12:29 +0100422 counter);
423
424 spin_lock_irq(&ctx->lock);
425 /*
426 * If the context is active we need to retry the smp call.
427 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100428 if (ctx->nr_active && !list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100429 spin_unlock_irq(&ctx->lock);
430 goto retry;
431 }
432
433 /*
434 * The lock prevents that this context is scheduled in so we
Ingo Molnar04289bb2008-12-11 08:38:42 +0100435 * can remove the counter safely, if the call above did not
Thomas Gleixner0793a612008-12-04 20:12:29 +0100436 * succeed.
437 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100438 if (!list_empty(&counter->list_entry)) {
Ingo Molnar04289bb2008-12-11 08:38:42 +0100439 list_del_counter(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100440 }
441 spin_unlock_irq(&ctx->lock);
442}
443
Peter Zijlstra4af49982009-04-06 11:45:10 +0200444static inline u64 perf_clock(void)
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100445{
Peter Zijlstra4af49982009-04-06 11:45:10 +0200446 return cpu_clock(smp_processor_id());
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100447}
448
449/*
450 * Update the record of the current time in a context.
451 */
Peter Zijlstra4af49982009-04-06 11:45:10 +0200452static void update_context_time(struct perf_counter_context *ctx)
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100453{
Peter Zijlstra4af49982009-04-06 11:45:10 +0200454 u64 now = perf_clock();
455
456 ctx->time += now - ctx->timestamp;
457 ctx->timestamp = now;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100458}
459
460/*
461 * Update the total_time_enabled and total_time_running fields for a counter.
462 */
463static void update_counter_times(struct perf_counter *counter)
464{
465 struct perf_counter_context *ctx = counter->ctx;
466 u64 run_end;
467
Peter Zijlstra4af49982009-04-06 11:45:10 +0200468 if (counter->state < PERF_COUNTER_STATE_INACTIVE)
469 return;
470
471 counter->total_time_enabled = ctx->time - counter->tstamp_enabled;
472
473 if (counter->state == PERF_COUNTER_STATE_INACTIVE)
474 run_end = counter->tstamp_stopped;
475 else
476 run_end = ctx->time;
477
478 counter->total_time_running = run_end - counter->tstamp_running;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100479}
480
481/*
482 * Update total_time_enabled and total_time_running for all counters in a group.
483 */
484static void update_group_times(struct perf_counter *leader)
485{
486 struct perf_counter *counter;
487
488 update_counter_times(leader);
489 list_for_each_entry(counter, &leader->sibling_list, list_entry)
490 update_counter_times(counter);
491}
492
493/*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100494 * Cross CPU call to disable a performance counter
495 */
496static void __perf_counter_disable(void *info)
497{
498 struct perf_counter *counter = info;
499 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
500 struct perf_counter_context *ctx = counter->ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100501
502 /*
503 * If this is a per-task counter, need to check whether this
504 * counter's task is the current task on this cpu.
505 */
Peter Zijlstra665c2142009-05-29 14:51:57 +0200506 if (ctx->task && cpuctx->task_ctx != ctx)
Paul Mackerrasd859e292009-01-17 18:10:22 +1100507 return;
508
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200509 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100510
511 /*
512 * If the counter is on, turn it off.
513 * If it is in error state, leave it in error state.
514 */
515 if (counter->state >= PERF_COUNTER_STATE_INACTIVE) {
Peter Zijlstra4af49982009-04-06 11:45:10 +0200516 update_context_time(ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100517 update_counter_times(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100518 if (counter == counter->group_leader)
519 group_sched_out(counter, cpuctx, ctx);
520 else
521 counter_sched_out(counter, cpuctx, ctx);
522 counter->state = PERF_COUNTER_STATE_OFF;
523 }
524
Peter Zijlstra665c2142009-05-29 14:51:57 +0200525 spin_unlock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100526}
527
528/*
529 * Disable a counter.
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000530 *
531 * If counter->ctx is a cloned context, callers must make sure that
532 * every task struct that counter->ctx->task could possibly point to
533 * remains valid. This condition is satisifed when called through
534 * perf_counter_for_each_child or perf_counter_for_each because they
535 * hold the top-level counter's child_mutex, so any descendant that
536 * goes to exit will block in sync_child_counter.
537 * When called from perf_pending_counter it's OK because counter->ctx
538 * is the current context on this CPU and preemption is disabled,
539 * hence we can't get into perf_counter_task_sched_out for this context.
Paul Mackerrasd859e292009-01-17 18:10:22 +1100540 */
541static void perf_counter_disable(struct perf_counter *counter)
542{
543 struct perf_counter_context *ctx = counter->ctx;
544 struct task_struct *task = ctx->task;
545
546 if (!task) {
547 /*
548 * Disable the counter on the cpu that it's on
549 */
550 smp_call_function_single(counter->cpu, __perf_counter_disable,
551 counter, 1);
552 return;
553 }
554
555 retry:
556 task_oncpu_function_call(task, __perf_counter_disable, counter);
557
558 spin_lock_irq(&ctx->lock);
559 /*
560 * If the counter is still active, we need to retry the cross-call.
561 */
562 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
563 spin_unlock_irq(&ctx->lock);
564 goto retry;
565 }
566
567 /*
568 * Since we have the lock this context can't be scheduled
569 * in, so we can change the state safely.
570 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100571 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
572 update_counter_times(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100573 counter->state = PERF_COUNTER_STATE_OFF;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100574 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100575
576 spin_unlock_irq(&ctx->lock);
577}
578
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100579static int
580counter_sched_in(struct perf_counter *counter,
581 struct perf_cpu_context *cpuctx,
582 struct perf_counter_context *ctx,
583 int cpu)
584{
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100585 if (counter->state <= PERF_COUNTER_STATE_OFF)
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100586 return 0;
587
588 counter->state = PERF_COUNTER_STATE_ACTIVE;
589 counter->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */
590 /*
591 * The new state must be visible before we turn it on in the hardware:
592 */
593 smp_wmb();
594
Robert Richter4aeb0b42009-04-29 12:47:03 +0200595 if (counter->pmu->enable(counter)) {
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100596 counter->state = PERF_COUNTER_STATE_INACTIVE;
597 counter->oncpu = -1;
598 return -EAGAIN;
599 }
600
Peter Zijlstra4af49982009-04-06 11:45:10 +0200601 counter->tstamp_running += ctx->time - counter->tstamp_stopped;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100602
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100603 if (!is_software_counter(counter))
604 cpuctx->active_oncpu++;
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100605 ctx->nr_active++;
606
Peter Zijlstra0d486962009-06-02 19:22:16 +0200607 if (counter->attr.exclusive)
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100608 cpuctx->exclusive = 1;
609
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100610 return 0;
611}
612
Paul Mackerras6751b712009-05-11 12:08:02 +1000613static int
614group_sched_in(struct perf_counter *group_counter,
615 struct perf_cpu_context *cpuctx,
616 struct perf_counter_context *ctx,
617 int cpu)
618{
619 struct perf_counter *counter, *partial_group;
620 int ret;
621
622 if (group_counter->state == PERF_COUNTER_STATE_OFF)
623 return 0;
624
625 ret = hw_perf_group_sched_in(group_counter, cpuctx, ctx, cpu);
626 if (ret)
627 return ret < 0 ? ret : 0;
628
Paul Mackerras6751b712009-05-11 12:08:02 +1000629 if (counter_sched_in(group_counter, cpuctx, ctx, cpu))
630 return -EAGAIN;
631
632 /*
633 * Schedule in siblings as one group (if any):
634 */
635 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
Paul Mackerras6751b712009-05-11 12:08:02 +1000636 if (counter_sched_in(counter, cpuctx, ctx, cpu)) {
637 partial_group = counter;
638 goto group_error;
639 }
640 }
641
642 return 0;
643
644group_error:
645 /*
646 * Groups can be scheduled in as one unit only, so undo any
647 * partial group before returning:
648 */
649 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
650 if (counter == partial_group)
651 break;
652 counter_sched_out(counter, cpuctx, ctx);
653 }
654 counter_sched_out(group_counter, cpuctx, ctx);
655
656 return -EAGAIN;
657}
658
Thomas Gleixner0793a612008-12-04 20:12:29 +0100659/*
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100660 * Return 1 for a group consisting entirely of software counters,
661 * 0 if the group contains any hardware counters.
662 */
663static int is_software_only_group(struct perf_counter *leader)
664{
665 struct perf_counter *counter;
666
667 if (!is_software_counter(leader))
668 return 0;
Peter Zijlstra5c148192009-03-25 12:30:23 +0100669
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100670 list_for_each_entry(counter, &leader->sibling_list, list_entry)
671 if (!is_software_counter(counter))
672 return 0;
Peter Zijlstra5c148192009-03-25 12:30:23 +0100673
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100674 return 1;
675}
676
677/*
678 * Work out whether we can put this counter group on the CPU now.
679 */
680static int group_can_go_on(struct perf_counter *counter,
681 struct perf_cpu_context *cpuctx,
682 int can_add_hw)
683{
684 /*
685 * Groups consisting entirely of software counters can always go on.
686 */
687 if (is_software_only_group(counter))
688 return 1;
689 /*
690 * If an exclusive group is already on, no other hardware
691 * counters can go on.
692 */
693 if (cpuctx->exclusive)
694 return 0;
695 /*
696 * If this group is exclusive and there are already
697 * counters on the CPU, it can't go on.
698 */
Peter Zijlstra0d486962009-06-02 19:22:16 +0200699 if (counter->attr.exclusive && cpuctx->active_oncpu)
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100700 return 0;
701 /*
702 * Otherwise, try to add it if all previous groups were able
703 * to go on.
704 */
705 return can_add_hw;
706}
707
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100708static void add_counter_to_ctx(struct perf_counter *counter,
709 struct perf_counter_context *ctx)
710{
711 list_add_counter(counter, ctx);
Peter Zijlstra4af49982009-04-06 11:45:10 +0200712 counter->tstamp_enabled = ctx->time;
713 counter->tstamp_running = ctx->time;
714 counter->tstamp_stopped = ctx->time;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100715}
716
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100717/*
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100718 * Cross CPU call to install and enable a performance counter
Peter Zijlstra682076a2009-05-23 18:28:57 +0200719 *
720 * Must be called with ctx->mutex held
Thomas Gleixner0793a612008-12-04 20:12:29 +0100721 */
722static void __perf_install_in_context(void *info)
723{
724 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
725 struct perf_counter *counter = info;
726 struct perf_counter_context *ctx = counter->ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100727 struct perf_counter *leader = counter->group_leader;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100728 int cpu = smp_processor_id();
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100729 int err;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100730
731 /*
732 * If this is a task context, we need to check whether it is
733 * the current task context of this cpu. If not it has been
734 * scheduled out before the smp call arrived.
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000735 * Or possibly this is the right context but it isn't
736 * on this cpu because it had no counters.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100737 */
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000738 if (ctx->task && cpuctx->task_ctx != ctx) {
Peter Zijlstra665c2142009-05-29 14:51:57 +0200739 if (cpuctx->task_ctx || ctx->task != current)
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000740 return;
741 cpuctx->task_ctx = ctx;
742 }
Thomas Gleixner0793a612008-12-04 20:12:29 +0100743
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200744 spin_lock(&ctx->lock);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000745 ctx->is_active = 1;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200746 update_context_time(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100747
748 /*
749 * Protect the list operation against NMI by disabling the
750 * counters on a global level. NOP for non NMI based counters.
751 */
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200752 perf_disable();
Thomas Gleixner0793a612008-12-04 20:12:29 +0100753
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100754 add_counter_to_ctx(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100755
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100756 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100757 * Don't put the counter on if it is disabled or if
758 * it is in a group and the group isn't on.
759 */
760 if (counter->state != PERF_COUNTER_STATE_INACTIVE ||
761 (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE))
762 goto unlock;
763
764 /*
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100765 * An exclusive counter can't go on if there are already active
766 * hardware counters, and no hardware counter can go on if there
767 * is already an exclusive counter on.
768 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100769 if (!group_can_go_on(counter, cpuctx, 1))
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100770 err = -EEXIST;
771 else
772 err = counter_sched_in(counter, cpuctx, ctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100773
Paul Mackerrasd859e292009-01-17 18:10:22 +1100774 if (err) {
775 /*
776 * This counter couldn't go on. If it is in a group
777 * then we have to pull the whole group off.
778 * If the counter group is pinned then put it in error state.
779 */
780 if (leader != counter)
781 group_sched_out(leader, cpuctx, ctx);
Peter Zijlstra0d486962009-06-02 19:22:16 +0200782 if (leader->attr.pinned) {
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100783 update_group_times(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100784 leader->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100785 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100786 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100787
788 if (!err && !ctx->task && cpuctx->max_pertask)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100789 cpuctx->max_pertask--;
790
Paul Mackerrasd859e292009-01-17 18:10:22 +1100791 unlock:
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200792 perf_enable();
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100793
Peter Zijlstra665c2142009-05-29 14:51:57 +0200794 spin_unlock(&ctx->lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100795}
796
797/*
798 * Attach a performance counter to a context
799 *
800 * First we add the counter to the list with the hardware enable bit
801 * in counter->hw_config cleared.
802 *
803 * If the counter is attached to a task which is on a CPU we use a smp
804 * call to enable it in the task context. The task might have been
805 * scheduled away, but we check this in the smp call again.
Paul Mackerrasd859e292009-01-17 18:10:22 +1100806 *
807 * Must be called with ctx->mutex held.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100808 */
809static void
810perf_install_in_context(struct perf_counter_context *ctx,
811 struct perf_counter *counter,
812 int cpu)
813{
814 struct task_struct *task = ctx->task;
815
Thomas Gleixner0793a612008-12-04 20:12:29 +0100816 if (!task) {
817 /*
818 * Per cpu counters are installed via an smp call and
819 * the install is always sucessful.
820 */
821 smp_call_function_single(cpu, __perf_install_in_context,
822 counter, 1);
823 return;
824 }
825
Thomas Gleixner0793a612008-12-04 20:12:29 +0100826retry:
827 task_oncpu_function_call(task, __perf_install_in_context,
828 counter);
829
830 spin_lock_irq(&ctx->lock);
831 /*
Thomas Gleixner0793a612008-12-04 20:12:29 +0100832 * we need to retry the smp call.
833 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100834 if (ctx->is_active && list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100835 spin_unlock_irq(&ctx->lock);
836 goto retry;
837 }
838
839 /*
840 * The lock prevents that this context is scheduled in so we
841 * can add the counter safely, if it the call above did not
842 * succeed.
843 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100844 if (list_empty(&counter->list_entry))
845 add_counter_to_ctx(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100846 spin_unlock_irq(&ctx->lock);
847}
848
Paul Mackerrasd859e292009-01-17 18:10:22 +1100849/*
850 * Cross CPU call to enable a performance counter
851 */
852static void __perf_counter_enable(void *info)
Ingo Molnar04289bb2008-12-11 08:38:42 +0100853{
Paul Mackerrasd859e292009-01-17 18:10:22 +1100854 struct perf_counter *counter = info;
855 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
856 struct perf_counter_context *ctx = counter->ctx;
857 struct perf_counter *leader = counter->group_leader;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100858 int err;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100859
860 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100861 * If this is a per-task counter, need to check whether this
862 * counter's task is the current task on this cpu.
Ingo Molnar04289bb2008-12-11 08:38:42 +0100863 */
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000864 if (ctx->task && cpuctx->task_ctx != ctx) {
Peter Zijlstra665c2142009-05-29 14:51:57 +0200865 if (cpuctx->task_ctx || ctx->task != current)
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000866 return;
867 cpuctx->task_ctx = ctx;
868 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100869
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200870 spin_lock(&ctx->lock);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000871 ctx->is_active = 1;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200872 update_context_time(ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100873
874 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
875 goto unlock;
876 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200877 counter->tstamp_enabled = ctx->time - counter->total_time_enabled;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100878
879 /*
880 * If the counter is in a group and isn't the group leader,
881 * then don't put it on unless the group is on.
882 */
883 if (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE)
884 goto unlock;
885
Paul Mackerrase758a332009-05-12 21:59:01 +1000886 if (!group_can_go_on(counter, cpuctx, 1)) {
Paul Mackerrasd859e292009-01-17 18:10:22 +1100887 err = -EEXIST;
Paul Mackerrase758a332009-05-12 21:59:01 +1000888 } else {
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200889 perf_disable();
Paul Mackerrase758a332009-05-12 21:59:01 +1000890 if (counter == leader)
891 err = group_sched_in(counter, cpuctx, ctx,
892 smp_processor_id());
893 else
894 err = counter_sched_in(counter, cpuctx, ctx,
895 smp_processor_id());
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200896 perf_enable();
Paul Mackerrase758a332009-05-12 21:59:01 +1000897 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100898
899 if (err) {
900 /*
901 * If this counter can't go on and it's part of a
902 * group, then the whole group has to come off.
903 */
904 if (leader != counter)
905 group_sched_out(leader, cpuctx, ctx);
Peter Zijlstra0d486962009-06-02 19:22:16 +0200906 if (leader->attr.pinned) {
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100907 update_group_times(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100908 leader->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100909 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100910 }
911
912 unlock:
Peter Zijlstra665c2142009-05-29 14:51:57 +0200913 spin_unlock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100914}
915
916/*
917 * Enable a counter.
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000918 *
919 * If counter->ctx is a cloned context, callers must make sure that
920 * every task struct that counter->ctx->task could possibly point to
921 * remains valid. This condition is satisfied when called through
922 * perf_counter_for_each_child or perf_counter_for_each as described
923 * for perf_counter_disable.
Paul Mackerrasd859e292009-01-17 18:10:22 +1100924 */
925static void perf_counter_enable(struct perf_counter *counter)
926{
927 struct perf_counter_context *ctx = counter->ctx;
928 struct task_struct *task = ctx->task;
929
930 if (!task) {
931 /*
932 * Enable the counter on the cpu that it's on
933 */
934 smp_call_function_single(counter->cpu, __perf_counter_enable,
935 counter, 1);
936 return;
937 }
938
939 spin_lock_irq(&ctx->lock);
940 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
941 goto out;
942
943 /*
944 * If the counter is in error state, clear that first.
945 * That way, if we see the counter in error state below, we
946 * know that it has gone back into error state, as distinct
947 * from the task having been scheduled away before the
948 * cross-call arrived.
949 */
950 if (counter->state == PERF_COUNTER_STATE_ERROR)
951 counter->state = PERF_COUNTER_STATE_OFF;
952
953 retry:
954 spin_unlock_irq(&ctx->lock);
955 task_oncpu_function_call(task, __perf_counter_enable, counter);
956
957 spin_lock_irq(&ctx->lock);
958
959 /*
960 * If the context is active and the counter is still off,
961 * we need to retry the cross-call.
962 */
963 if (ctx->is_active && counter->state == PERF_COUNTER_STATE_OFF)
964 goto retry;
965
966 /*
967 * Since we have the lock this context can't be scheduled
968 * in, so we can change the state safely.
969 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100970 if (counter->state == PERF_COUNTER_STATE_OFF) {
Paul Mackerrasd859e292009-01-17 18:10:22 +1100971 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200972 counter->tstamp_enabled =
973 ctx->time - counter->total_time_enabled;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100974 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100975 out:
976 spin_unlock_irq(&ctx->lock);
977}
978
Peter Zijlstra2023b352009-05-05 17:50:26 +0200979static int perf_counter_refresh(struct perf_counter *counter, int refresh)
Peter Zijlstra79f14642009-04-06 11:45:07 +0200980{
Peter Zijlstra2023b352009-05-05 17:50:26 +0200981 /*
982 * not supported on inherited counters
983 */
Peter Zijlstra0d486962009-06-02 19:22:16 +0200984 if (counter->attr.inherit)
Peter Zijlstra2023b352009-05-05 17:50:26 +0200985 return -EINVAL;
986
Peter Zijlstra79f14642009-04-06 11:45:07 +0200987 atomic_add(refresh, &counter->event_limit);
988 perf_counter_enable(counter);
Peter Zijlstra2023b352009-05-05 17:50:26 +0200989
990 return 0;
Peter Zijlstra79f14642009-04-06 11:45:07 +0200991}
992
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100993void __perf_counter_sched_out(struct perf_counter_context *ctx,
994 struct perf_cpu_context *cpuctx)
995{
996 struct perf_counter *counter;
997
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100998 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100999 ctx->is_active = 0;
1000 if (likely(!ctx->nr_counters))
1001 goto out;
Peter Zijlstra4af49982009-04-06 11:45:10 +02001002 update_context_time(ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001003
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001004 perf_disable();
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001005 if (ctx->nr_active) {
Peter Zijlstraafedadf2009-05-20 12:21:22 +02001006 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1007 if (counter != counter->group_leader)
1008 counter_sched_out(counter, cpuctx, ctx);
1009 else
1010 group_sched_out(counter, cpuctx, ctx);
1011 }
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001012 }
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001013 perf_enable();
Paul Mackerrasd859e292009-01-17 18:10:22 +11001014 out:
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001015 spin_unlock(&ctx->lock);
1016}
1017
Thomas Gleixner0793a612008-12-04 20:12:29 +01001018/*
Paul Mackerras564c2b22009-05-22 14:27:22 +10001019 * Test whether two contexts are equivalent, i.e. whether they
1020 * have both been cloned from the same version of the same context
1021 * and they both have the same number of enabled counters.
1022 * If the number of enabled counters is the same, then the set
1023 * of enabled counters should be the same, because these are both
1024 * inherited contexts, therefore we can't access individual counters
1025 * in them directly with an fd; we can only enable/disable all
1026 * counters via prctl, or enable/disable all counters in a family
1027 * via ioctl, which will have the same effect on both contexts.
1028 */
1029static int context_equiv(struct perf_counter_context *ctx1,
1030 struct perf_counter_context *ctx2)
1031{
1032 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001033 && ctx1->parent_gen == ctx2->parent_gen
Paul Mackerras25346b92009-06-01 17:48:12 +10001034 && !ctx1->pin_count && !ctx2->pin_count;
Paul Mackerras564c2b22009-05-22 14:27:22 +10001035}
1036
Peter Zijlstrabfbd3382009-06-24 21:11:59 +02001037static void __perf_counter_read(void *counter);
1038
1039static void __perf_counter_sync_stat(struct perf_counter *counter,
1040 struct perf_counter *next_counter)
1041{
1042 u64 value;
1043
1044 if (!counter->attr.inherit_stat)
1045 return;
1046
1047 /*
1048 * Update the counter value, we cannot use perf_counter_read()
1049 * because we're in the middle of a context switch and have IRQs
1050 * disabled, which upsets smp_call_function_single(), however
1051 * we know the counter must be on the current CPU, therefore we
1052 * don't need to use it.
1053 */
1054 switch (counter->state) {
1055 case PERF_COUNTER_STATE_ACTIVE:
1056 __perf_counter_read(counter);
1057 break;
1058
1059 case PERF_COUNTER_STATE_INACTIVE:
1060 update_counter_times(counter);
1061 break;
1062
1063 default:
1064 break;
1065 }
1066
1067 /*
1068 * In order to keep per-task stats reliable we need to flip the counter
1069 * values when we flip the contexts.
1070 */
1071 value = atomic64_read(&next_counter->count);
1072 value = atomic64_xchg(&counter->count, value);
1073 atomic64_set(&next_counter->count, value);
1074
Peter Zijlstra19d2e752009-06-26 13:10:23 +02001075 swap(counter->total_time_enabled, next_counter->total_time_enabled);
1076 swap(counter->total_time_running, next_counter->total_time_running);
1077
Peter Zijlstrabfbd3382009-06-24 21:11:59 +02001078 /*
Peter Zijlstra19d2e752009-06-26 13:10:23 +02001079 * Since we swizzled the values, update the user visible data too.
Peter Zijlstrabfbd3382009-06-24 21:11:59 +02001080 */
Peter Zijlstra19d2e752009-06-26 13:10:23 +02001081 perf_counter_update_userpage(counter);
1082 perf_counter_update_userpage(next_counter);
Peter Zijlstrabfbd3382009-06-24 21:11:59 +02001083}
1084
1085#define list_next_entry(pos, member) \
1086 list_entry(pos->member.next, typeof(*pos), member)
1087
1088static void perf_counter_sync_stat(struct perf_counter_context *ctx,
1089 struct perf_counter_context *next_ctx)
1090{
1091 struct perf_counter *counter, *next_counter;
1092
1093 if (!ctx->nr_stat)
1094 return;
1095
1096 counter = list_first_entry(&ctx->event_list,
1097 struct perf_counter, event_entry);
1098
1099 next_counter = list_first_entry(&next_ctx->event_list,
1100 struct perf_counter, event_entry);
1101
1102 while (&counter->event_entry != &ctx->event_list &&
1103 &next_counter->event_entry != &next_ctx->event_list) {
1104
1105 __perf_counter_sync_stat(counter, next_counter);
1106
1107 counter = list_next_entry(counter, event_entry);
Peter Zijlstra10545982009-08-06 18:06:26 +02001108 next_counter = list_next_entry(next_counter, event_entry);
Peter Zijlstrabfbd3382009-06-24 21:11:59 +02001109 }
1110}
1111
Paul Mackerras564c2b22009-05-22 14:27:22 +10001112/*
Thomas Gleixner0793a612008-12-04 20:12:29 +01001113 * Called from scheduler to remove the counters of the current task,
1114 * with interrupts disabled.
1115 *
1116 * We stop each counter and update the counter value in counter->count.
1117 *
Ingo Molnar76715812008-12-17 14:20:28 +01001118 * This does not protect us against NMI, but disable()
Thomas Gleixner0793a612008-12-04 20:12:29 +01001119 * sets the disabled bit in the control field of counter _before_
1120 * accessing the counter control register. If a NMI hits, then it will
1121 * not restart the counter.
1122 */
Paul Mackerras564c2b22009-05-22 14:27:22 +10001123void perf_counter_task_sched_out(struct task_struct *task,
1124 struct task_struct *next, int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001125{
1126 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001127 struct perf_counter_context *ctx = task->perf_counter_ctxp;
Paul Mackerras564c2b22009-05-22 14:27:22 +10001128 struct perf_counter_context *next_ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001129 struct perf_counter_context *parent;
Peter Zijlstra4a0deca2009-03-19 20:26:12 +01001130 struct pt_regs *regs;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001131 int do_switch = 1;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001132
Peter Zijlstra10989fb2009-05-25 14:45:28 +02001133 regs = task_pt_regs(task);
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +02001134 perf_swcounter_event(PERF_COUNT_SW_CONTEXT_SWITCHES, 1, 1, regs, 0);
Peter Zijlstra10989fb2009-05-25 14:45:28 +02001135
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001136 if (likely(!ctx || !cpuctx->task_ctx))
Thomas Gleixner0793a612008-12-04 20:12:29 +01001137 return;
1138
Peter Zijlstrabce379b2009-04-06 11:45:13 +02001139 update_context_time(ctx);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001140
1141 rcu_read_lock();
1142 parent = rcu_dereference(ctx->parent_ctx);
Paul Mackerras564c2b22009-05-22 14:27:22 +10001143 next_ctx = next->perf_counter_ctxp;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001144 if (parent && next_ctx &&
1145 rcu_dereference(next_ctx->parent_ctx) == parent) {
1146 /*
1147 * Looks like the two contexts are clones, so we might be
1148 * able to optimize the context switch. We lock both
1149 * contexts and check that they are clones under the
1150 * lock (including re-checking that neither has been
1151 * uncloned in the meantime). It doesn't matter which
1152 * order we take the locks because no other cpu could
1153 * be trying to lock both of these tasks.
1154 */
1155 spin_lock(&ctx->lock);
1156 spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
1157 if (context_equiv(ctx, next_ctx)) {
Peter Zijlstra665c2142009-05-29 14:51:57 +02001158 /*
1159 * XXX do we need a memory barrier of sorts
1160 * wrt to rcu_dereference() of perf_counter_ctxp
1161 */
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001162 task->perf_counter_ctxp = next_ctx;
1163 next->perf_counter_ctxp = ctx;
1164 ctx->task = next;
1165 next_ctx->task = task;
1166 do_switch = 0;
Peter Zijlstrabfbd3382009-06-24 21:11:59 +02001167
1168 perf_counter_sync_stat(ctx, next_ctx);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001169 }
1170 spin_unlock(&next_ctx->lock);
1171 spin_unlock(&ctx->lock);
Paul Mackerras564c2b22009-05-22 14:27:22 +10001172 }
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001173 rcu_read_unlock();
Paul Mackerras564c2b22009-05-22 14:27:22 +10001174
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001175 if (do_switch) {
1176 __perf_counter_sched_out(ctx, cpuctx);
1177 cpuctx->task_ctx = NULL;
1178 }
Thomas Gleixner0793a612008-12-04 20:12:29 +01001179}
1180
Peter Zijlstra665c2142009-05-29 14:51:57 +02001181/*
1182 * Called with IRQs disabled
1183 */
Paul Mackerrasa08b1592009-05-11 15:46:10 +10001184static void __perf_counter_task_sched_out(struct perf_counter_context *ctx)
1185{
1186 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1187
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001188 if (!cpuctx->task_ctx)
1189 return;
Ingo Molnar012b84d2009-05-17 11:08:41 +02001190
1191 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
1192 return;
1193
Paul Mackerrasa08b1592009-05-11 15:46:10 +10001194 __perf_counter_sched_out(ctx, cpuctx);
1195 cpuctx->task_ctx = NULL;
1196}
1197
Peter Zijlstra665c2142009-05-29 14:51:57 +02001198/*
1199 * Called with IRQs disabled
1200 */
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001201static void perf_counter_cpu_sched_out(struct perf_cpu_context *cpuctx)
Ingo Molnar04289bb2008-12-11 08:38:42 +01001202{
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001203 __perf_counter_sched_out(&cpuctx->ctx, cpuctx);
Ingo Molnar04289bb2008-12-11 08:38:42 +01001204}
1205
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001206static void
1207__perf_counter_sched_in(struct perf_counter_context *ctx,
1208 struct perf_cpu_context *cpuctx, int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001209{
Thomas Gleixner0793a612008-12-04 20:12:29 +01001210 struct perf_counter *counter;
Paul Mackerrasdd0e6ba2009-01-12 15:11:00 +11001211 int can_add_hw = 1;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001212
Thomas Gleixner0793a612008-12-04 20:12:29 +01001213 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001214 ctx->is_active = 1;
1215 if (likely(!ctx->nr_counters))
1216 goto out;
1217
Peter Zijlstra4af49982009-04-06 11:45:10 +02001218 ctx->timestamp = perf_clock();
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001219
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001220 perf_disable();
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001221
1222 /*
1223 * First go through the list and put on any pinned groups
1224 * in order to give them the best chance of going on.
1225 */
Ingo Molnar04289bb2008-12-11 08:38:42 +01001226 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001227 if (counter->state <= PERF_COUNTER_STATE_OFF ||
Peter Zijlstra0d486962009-06-02 19:22:16 +02001228 !counter->attr.pinned)
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001229 continue;
1230 if (counter->cpu != -1 && counter->cpu != cpu)
1231 continue;
1232
Peter Zijlstraafedadf2009-05-20 12:21:22 +02001233 if (counter != counter->group_leader)
1234 counter_sched_in(counter, cpuctx, ctx, cpu);
1235 else {
1236 if (group_can_go_on(counter, cpuctx, 1))
1237 group_sched_in(counter, cpuctx, ctx, cpu);
1238 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001239
1240 /*
1241 * If this pinned group hasn't been scheduled,
1242 * put it in error state.
1243 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001244 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
1245 update_group_times(counter);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001246 counter->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001247 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001248 }
1249
1250 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1251 /*
1252 * Ignore counters in OFF or ERROR state, and
1253 * ignore pinned counters since we did them already.
1254 */
1255 if (counter->state <= PERF_COUNTER_STATE_OFF ||
Peter Zijlstra0d486962009-06-02 19:22:16 +02001256 counter->attr.pinned)
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001257 continue;
1258
Ingo Molnar04289bb2008-12-11 08:38:42 +01001259 /*
1260 * Listen to the 'cpu' scheduling filter constraint
1261 * of counters:
1262 */
Thomas Gleixner0793a612008-12-04 20:12:29 +01001263 if (counter->cpu != -1 && counter->cpu != cpu)
1264 continue;
1265
Peter Zijlstraafedadf2009-05-20 12:21:22 +02001266 if (counter != counter->group_leader) {
1267 if (counter_sched_in(counter, cpuctx, ctx, cpu))
Paul Mackerrasdd0e6ba2009-01-12 15:11:00 +11001268 can_add_hw = 0;
Peter Zijlstraafedadf2009-05-20 12:21:22 +02001269 } else {
1270 if (group_can_go_on(counter, cpuctx, can_add_hw)) {
1271 if (group_sched_in(counter, cpuctx, ctx, cpu))
1272 can_add_hw = 0;
1273 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001274 }
Thomas Gleixner0793a612008-12-04 20:12:29 +01001275 }
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001276 perf_enable();
Paul Mackerrasd859e292009-01-17 18:10:22 +11001277 out:
Thomas Gleixner0793a612008-12-04 20:12:29 +01001278 spin_unlock(&ctx->lock);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001279}
Ingo Molnar04289bb2008-12-11 08:38:42 +01001280
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001281/*
1282 * Called from scheduler to add the counters of the current task
1283 * with interrupts disabled.
1284 *
1285 * We restore the counter value and then enable it.
1286 *
1287 * This does not protect us against NMI, but enable()
1288 * sets the enabled bit in the control field of counter _before_
1289 * accessing the counter control register. If a NMI hits, then it will
1290 * keep the counter running.
1291 */
1292void perf_counter_task_sched_in(struct task_struct *task, int cpu)
1293{
1294 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001295 struct perf_counter_context *ctx = task->perf_counter_ctxp;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001296
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001297 if (likely(!ctx))
1298 return;
Paul Mackerras564c2b22009-05-22 14:27:22 +10001299 if (cpuctx->task_ctx == ctx)
1300 return;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001301 __perf_counter_sched_in(ctx, cpuctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001302 cpuctx->task_ctx = ctx;
1303}
1304
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001305static void perf_counter_cpu_sched_in(struct perf_cpu_context *cpuctx, int cpu)
1306{
1307 struct perf_counter_context *ctx = &cpuctx->ctx;
1308
1309 __perf_counter_sched_in(ctx, cpuctx, cpu);
1310}
1311
Peter Zijlstraa78ac322009-05-25 17:39:05 +02001312#define MAX_INTERRUPTS (~0ULL)
1313
1314static void perf_log_throttle(struct perf_counter *counter, int enable);
Peter Zijlstra26b119b2009-05-20 12:21:20 +02001315
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001316static void perf_adjust_period(struct perf_counter *counter, u64 events)
1317{
1318 struct hw_perf_counter *hwc = &counter->hw;
1319 u64 period, sample_period;
1320 s64 delta;
1321
1322 events *= hwc->sample_period;
1323 period = div64_u64(events, counter->attr.sample_freq);
1324
1325 delta = (s64)(period - hwc->sample_period);
1326 delta = (delta + 7) / 8; /* low pass filter */
1327
1328 sample_period = hwc->sample_period + delta;
1329
1330 if (!sample_period)
1331 sample_period = 1;
1332
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001333 hwc->sample_period = sample_period;
1334}
1335
1336static void perf_ctx_adjust_freq(struct perf_counter_context *ctx)
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001337{
1338 struct perf_counter *counter;
Peter Zijlstra6a24ed6c2009-06-05 18:01:29 +02001339 struct hw_perf_counter *hwc;
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001340 u64 interrupts, freq;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001341
1342 spin_lock(&ctx->lock);
1343 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1344 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
1345 continue;
1346
Peter Zijlstra6a24ed6c2009-06-05 18:01:29 +02001347 hwc = &counter->hw;
1348
1349 interrupts = hwc->interrupts;
1350 hwc->interrupts = 0;
Peter Zijlstraa78ac322009-05-25 17:39:05 +02001351
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001352 /*
1353 * unthrottle counters on the tick
1354 */
Peter Zijlstraa78ac322009-05-25 17:39:05 +02001355 if (interrupts == MAX_INTERRUPTS) {
1356 perf_log_throttle(counter, 1);
1357 counter->pmu->unthrottle(counter);
Peter Zijlstradf58ab22009-06-11 11:25:05 +02001358 interrupts = 2*sysctl_perf_counter_sample_rate/HZ;
Peter Zijlstraa78ac322009-05-25 17:39:05 +02001359 }
1360
Peter Zijlstra0d486962009-06-02 19:22:16 +02001361 if (!counter->attr.freq || !counter->attr.sample_freq)
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001362 continue;
1363
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001364 /*
1365 * if the specified freq < HZ then we need to skip ticks
1366 */
Peter Zijlstra6a24ed6c2009-06-05 18:01:29 +02001367 if (counter->attr.sample_freq < HZ) {
1368 freq = counter->attr.sample_freq;
1369
1370 hwc->freq_count += freq;
1371 hwc->freq_interrupts += interrupts;
1372
1373 if (hwc->freq_count < HZ)
1374 continue;
1375
1376 interrupts = hwc->freq_interrupts;
1377 hwc->freq_interrupts = 0;
1378 hwc->freq_count -= HZ;
1379 } else
1380 freq = HZ;
1381
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001382 perf_adjust_period(counter, freq * interrupts);
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001383
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001384 /*
1385 * In order to avoid being stalled by an (accidental) huge
1386 * sample period, force reset the sample period if we didn't
1387 * get any events in this freq period.
1388 */
1389 if (!interrupts) {
1390 perf_disable();
1391 counter->pmu->disable(counter);
Paul Mackerras87847b82009-06-13 17:06:50 +10001392 atomic64_set(&hwc->period_left, 0);
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001393 counter->pmu->enable(counter);
1394 perf_enable();
1395 }
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001396 }
1397 spin_unlock(&ctx->lock);
1398}
1399
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001400/*
1401 * Round-robin a context's counters:
1402 */
1403static void rotate_ctx(struct perf_counter_context *ctx)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001404{
Thomas Gleixner0793a612008-12-04 20:12:29 +01001405 struct perf_counter *counter;
1406
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001407 if (!ctx->nr_counters)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001408 return;
1409
Thomas Gleixner0793a612008-12-04 20:12:29 +01001410 spin_lock(&ctx->lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001411 /*
Ingo Molnar04289bb2008-12-11 08:38:42 +01001412 * Rotate the first entry last (works just fine for group counters too):
Thomas Gleixner0793a612008-12-04 20:12:29 +01001413 */
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001414 perf_disable();
Ingo Molnar04289bb2008-12-11 08:38:42 +01001415 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Peter Zijlstra75564232009-03-13 12:21:29 +01001416 list_move_tail(&counter->list_entry, &ctx->counter_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001417 break;
1418 }
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001419 perf_enable();
Thomas Gleixner0793a612008-12-04 20:12:29 +01001420
1421 spin_unlock(&ctx->lock);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001422}
Thomas Gleixner0793a612008-12-04 20:12:29 +01001423
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001424void perf_counter_task_tick(struct task_struct *curr, int cpu)
1425{
Peter Zijlstra7fc23a52009-05-08 18:52:21 +02001426 struct perf_cpu_context *cpuctx;
1427 struct perf_counter_context *ctx;
1428
1429 if (!atomic_read(&nr_counters))
1430 return;
1431
1432 cpuctx = &per_cpu(perf_cpu_context, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001433 ctx = curr->perf_counter_ctxp;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001434
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001435 perf_ctx_adjust_freq(&cpuctx->ctx);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001436 if (ctx)
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001437 perf_ctx_adjust_freq(ctx);
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001438
Ingo Molnarb82914c2009-05-04 18:54:32 +02001439 perf_counter_cpu_sched_out(cpuctx);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001440 if (ctx)
1441 __perf_counter_task_sched_out(ctx);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001442
Ingo Molnarb82914c2009-05-04 18:54:32 +02001443 rotate_ctx(&cpuctx->ctx);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001444 if (ctx)
1445 rotate_ctx(ctx);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001446
Ingo Molnarb82914c2009-05-04 18:54:32 +02001447 perf_counter_cpu_sched_in(cpuctx, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001448 if (ctx)
1449 perf_counter_task_sched_in(curr, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001450}
1451
1452/*
Paul Mackerras57e79862009-06-30 16:07:19 +10001453 * Enable all of a task's counters that have been marked enable-on-exec.
1454 * This expects task == current.
1455 */
1456static void perf_counter_enable_on_exec(struct task_struct *task)
1457{
1458 struct perf_counter_context *ctx;
1459 struct perf_counter *counter;
1460 unsigned long flags;
1461 int enabled = 0;
1462
1463 local_irq_save(flags);
1464 ctx = task->perf_counter_ctxp;
1465 if (!ctx || !ctx->nr_counters)
1466 goto out;
1467
1468 __perf_counter_task_sched_out(ctx);
1469
1470 spin_lock(&ctx->lock);
1471
1472 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1473 if (!counter->attr.enable_on_exec)
1474 continue;
1475 counter->attr.enable_on_exec = 0;
1476 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
1477 continue;
1478 counter->state = PERF_COUNTER_STATE_INACTIVE;
1479 counter->tstamp_enabled =
1480 ctx->time - counter->total_time_enabled;
1481 enabled = 1;
1482 }
1483
1484 /*
1485 * Unclone this context if we enabled any counter.
1486 */
Peter Zijlstra71a851b2009-07-10 09:06:56 +02001487 if (enabled)
1488 unclone_ctx(ctx);
Paul Mackerras57e79862009-06-30 16:07:19 +10001489
1490 spin_unlock(&ctx->lock);
1491
1492 perf_counter_task_sched_in(task, smp_processor_id());
1493 out:
1494 local_irq_restore(flags);
1495}
1496
1497/*
Thomas Gleixner0793a612008-12-04 20:12:29 +01001498 * Cross CPU call to read the hardware counter
1499 */
Peter Zijlstrabfbd3382009-06-24 21:11:59 +02001500static void __perf_counter_read(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001501{
Ingo Molnar621a01e2008-12-11 12:46:46 +01001502 struct perf_counter *counter = info;
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001503 struct perf_counter_context *ctx = counter->ctx;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001504 unsigned long flags;
Ingo Molnar621a01e2008-12-11 12:46:46 +01001505
Peter Zijlstra849691a2009-04-06 11:45:12 +02001506 local_irq_save(flags);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001507 if (ctx->is_active)
Peter Zijlstra4af49982009-04-06 11:45:10 +02001508 update_context_time(ctx);
Robert Richter4aeb0b42009-04-29 12:47:03 +02001509 counter->pmu->read(counter);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001510 update_counter_times(counter);
Peter Zijlstra849691a2009-04-06 11:45:12 +02001511 local_irq_restore(flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001512}
1513
Ingo Molnar04289bb2008-12-11 08:38:42 +01001514static u64 perf_counter_read(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001515{
1516 /*
1517 * If counter is enabled and currently active on a CPU, update the
1518 * value in the counter structure:
1519 */
Ingo Molnar6a930702008-12-11 15:17:03 +01001520 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
Thomas Gleixner0793a612008-12-04 20:12:29 +01001521 smp_call_function_single(counter->oncpu,
Peter Zijlstrabfbd3382009-06-24 21:11:59 +02001522 __perf_counter_read, counter, 1);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001523 } else if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
1524 update_counter_times(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001525 }
1526
Ingo Molnaree060942008-12-13 09:00:03 +01001527 return atomic64_read(&counter->count);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001528}
1529
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001530/*
1531 * Initialize the perf_counter context in a task_struct:
1532 */
1533static void
1534__perf_counter_init_context(struct perf_counter_context *ctx,
1535 struct task_struct *task)
1536{
1537 memset(ctx, 0, sizeof(*ctx));
1538 spin_lock_init(&ctx->lock);
1539 mutex_init(&ctx->mutex);
1540 INIT_LIST_HEAD(&ctx->counter_list);
1541 INIT_LIST_HEAD(&ctx->event_list);
1542 atomic_set(&ctx->refcount, 1);
1543 ctx->task = task;
1544}
1545
Thomas Gleixner0793a612008-12-04 20:12:29 +01001546static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
1547{
Ingo Molnar22a4f652009-06-01 10:13:37 +02001548 struct perf_counter_context *ctx;
1549 struct perf_cpu_context *cpuctx;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001550 struct task_struct *task;
Paul Mackerras25346b92009-06-01 17:48:12 +10001551 unsigned long flags;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001552 int err;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001553
1554 /*
1555 * If cpu is not a wildcard then this is a percpu counter:
1556 */
1557 if (cpu != -1) {
1558 /* Must be root to operate on a CPU counter: */
Peter Zijlstra07647712009-06-11 11:18:36 +02001559 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
Thomas Gleixner0793a612008-12-04 20:12:29 +01001560 return ERR_PTR(-EACCES);
1561
1562 if (cpu < 0 || cpu > num_possible_cpus())
1563 return ERR_PTR(-EINVAL);
1564
1565 /*
1566 * We could be clever and allow to attach a counter to an
1567 * offline CPU and activate it when the CPU comes up, but
1568 * that's for later.
1569 */
1570 if (!cpu_isset(cpu, cpu_online_map))
1571 return ERR_PTR(-ENODEV);
1572
1573 cpuctx = &per_cpu(perf_cpu_context, cpu);
1574 ctx = &cpuctx->ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001575 get_ctx(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001576
Thomas Gleixner0793a612008-12-04 20:12:29 +01001577 return ctx;
1578 }
1579
1580 rcu_read_lock();
1581 if (!pid)
1582 task = current;
1583 else
1584 task = find_task_by_vpid(pid);
1585 if (task)
1586 get_task_struct(task);
1587 rcu_read_unlock();
1588
1589 if (!task)
1590 return ERR_PTR(-ESRCH);
1591
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001592 /*
1593 * Can't attach counters to a dying task.
1594 */
1595 err = -ESRCH;
1596 if (task->flags & PF_EXITING)
1597 goto errout;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001598
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001599 /* Reuse ptrace permission checks for now. */
1600 err = -EACCES;
1601 if (!ptrace_may_access(task, PTRACE_MODE_READ))
1602 goto errout;
1603
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001604 retry:
Paul Mackerras25346b92009-06-01 17:48:12 +10001605 ctx = perf_lock_task_context(task, &flags);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001606 if (ctx) {
Peter Zijlstra71a851b2009-07-10 09:06:56 +02001607 unclone_ctx(ctx);
Paul Mackerras25346b92009-06-01 17:48:12 +10001608 spin_unlock_irqrestore(&ctx->lock, flags);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001609 }
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001610
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001611 if (!ctx) {
1612 ctx = kmalloc(sizeof(struct perf_counter_context), GFP_KERNEL);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001613 err = -ENOMEM;
1614 if (!ctx)
1615 goto errout;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001616 __perf_counter_init_context(ctx, task);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001617 get_ctx(ctx);
1618 if (cmpxchg(&task->perf_counter_ctxp, NULL, ctx)) {
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001619 /*
1620 * We raced with some other task; use
1621 * the context they set.
1622 */
1623 kfree(ctx);
Paul Mackerras25346b92009-06-01 17:48:12 +10001624 goto retry;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001625 }
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001626 get_task_struct(task);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001627 }
1628
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001629 put_task_struct(task);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001630 return ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001631
1632 errout:
1633 put_task_struct(task);
1634 return ERR_PTR(err);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001635}
1636
Peter Zijlstra592903c2009-03-13 12:21:36 +01001637static void free_counter_rcu(struct rcu_head *head)
1638{
1639 struct perf_counter *counter;
1640
1641 counter = container_of(head, struct perf_counter, rcu_head);
Peter Zijlstra709e50c2009-06-02 14:13:15 +02001642 if (counter->ns)
1643 put_pid_ns(counter->ns);
Peter Zijlstra592903c2009-03-13 12:21:36 +01001644 kfree(counter);
1645}
1646
Peter Zijlstra925d5192009-03-30 19:07:02 +02001647static void perf_pending_sync(struct perf_counter *counter);
1648
Peter Zijlstraf1600952009-03-19 20:26:16 +01001649static void free_counter(struct perf_counter *counter)
1650{
Peter Zijlstra925d5192009-03-30 19:07:02 +02001651 perf_pending_sync(counter);
1652
Peter Zijlstraf3440112009-06-22 13:58:35 +02001653 if (!counter->parent) {
1654 atomic_dec(&nr_counters);
1655 if (counter->attr.mmap)
1656 atomic_dec(&nr_mmap_counters);
1657 if (counter->attr.comm)
1658 atomic_dec(&nr_comm_counters);
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02001659 if (counter->attr.task)
1660 atomic_dec(&nr_task_counters);
Peter Zijlstraf3440112009-06-22 13:58:35 +02001661 }
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02001662
Peter Zijlstrae077df42009-03-19 20:26:17 +01001663 if (counter->destroy)
1664 counter->destroy(counter);
1665
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001666 put_ctx(counter->ctx);
Peter Zijlstraf1600952009-03-19 20:26:16 +01001667 call_rcu(&counter->rcu_head, free_counter_rcu);
1668}
1669
Thomas Gleixner0793a612008-12-04 20:12:29 +01001670/*
1671 * Called when the last reference to the file is gone.
1672 */
1673static int perf_release(struct inode *inode, struct file *file)
1674{
1675 struct perf_counter *counter = file->private_data;
1676 struct perf_counter_context *ctx = counter->ctx;
1677
1678 file->private_data = NULL;
1679
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001680 WARN_ON_ONCE(ctx->parent_ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001681 mutex_lock(&ctx->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01001682 perf_counter_remove_from_context(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001683 mutex_unlock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001684
Peter Zijlstra082ff5a2009-05-23 18:29:00 +02001685 mutex_lock(&counter->owner->perf_counter_mutex);
1686 list_del_init(&counter->owner_entry);
1687 mutex_unlock(&counter->owner->perf_counter_mutex);
1688 put_task_struct(counter->owner);
1689
Peter Zijlstraf1600952009-03-19 20:26:16 +01001690 free_counter(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001691
1692 return 0;
1693}
1694
Peter Zijlstrae53c0992009-07-24 14:42:10 +02001695static u64 perf_counter_read_tree(struct perf_counter *counter)
1696{
1697 struct perf_counter *child;
1698 u64 total = 0;
1699
1700 total += perf_counter_read(counter);
1701 list_for_each_entry(child, &counter->child_list, child_list)
1702 total += perf_counter_read(child);
1703
1704 return total;
1705}
1706
Thomas Gleixner0793a612008-12-04 20:12:29 +01001707/*
1708 * Read the performance counter - simple non blocking version for now
1709 */
1710static ssize_t
1711perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
1712{
Marti Raudseppd5e8da62009-06-13 02:35:01 +03001713 u64 values[4];
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001714 int n;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001715
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001716 /*
1717 * Return end-of-file for a read on a counter that is in
1718 * error state (i.e. because it was pinned but it couldn't be
1719 * scheduled on to the CPU at some point).
1720 */
1721 if (counter->state == PERF_COUNTER_STATE_ERROR)
1722 return 0;
1723
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001724 WARN_ON_ONCE(counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001725 mutex_lock(&counter->child_mutex);
Peter Zijlstrae53c0992009-07-24 14:42:10 +02001726 values[0] = perf_counter_read_tree(counter);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001727 n = 1;
Peter Zijlstra0d486962009-06-02 19:22:16 +02001728 if (counter->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001729 values[n++] = counter->total_time_enabled +
1730 atomic64_read(&counter->child_total_time_enabled);
Peter Zijlstra0d486962009-06-02 19:22:16 +02001731 if (counter->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001732 values[n++] = counter->total_time_running +
1733 atomic64_read(&counter->child_total_time_running);
Peter Zijlstra0d486962009-06-02 19:22:16 +02001734 if (counter->attr.read_format & PERF_FORMAT_ID)
Peter Zijlstra7f453c22009-07-21 13:19:40 +02001735 values[n++] = primary_counter_id(counter);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001736 mutex_unlock(&counter->child_mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001737
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001738 if (count < n * sizeof(u64))
1739 return -EINVAL;
1740 count = n * sizeof(u64);
1741
1742 if (copy_to_user(buf, values, count))
1743 return -EFAULT;
1744
1745 return count;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001746}
1747
1748static ssize_t
Thomas Gleixner0793a612008-12-04 20:12:29 +01001749perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
1750{
1751 struct perf_counter *counter = file->private_data;
1752
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001753 return perf_read_hw(counter, buf, count);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001754}
1755
1756static unsigned int perf_poll(struct file *file, poll_table *wait)
1757{
1758 struct perf_counter *counter = file->private_data;
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001759 struct perf_mmap_data *data;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001760 unsigned int events = POLL_HUP;
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001761
1762 rcu_read_lock();
1763 data = rcu_dereference(counter->data);
1764 if (data)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001765 events = atomic_xchg(&data->poll, 0);
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001766 rcu_read_unlock();
Thomas Gleixner0793a612008-12-04 20:12:29 +01001767
1768 poll_wait(file, &counter->waitq, wait);
1769
Thomas Gleixner0793a612008-12-04 20:12:29 +01001770 return events;
1771}
1772
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001773static void perf_counter_reset(struct perf_counter *counter)
1774{
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001775 (void)perf_counter_read(counter);
Paul Mackerras615a3f12009-05-11 15:50:21 +10001776 atomic64_set(&counter->count, 0);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001777 perf_counter_update_userpage(counter);
1778}
1779
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001780/*
1781 * Holding the top-level counter's child_mutex means that any
1782 * descendant process that has inherited this counter will block
1783 * in sync_child_counter if it goes to exit, thus satisfying the
1784 * task existence requirements of perf_counter_enable/disable.
1785 */
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001786static void perf_counter_for_each_child(struct perf_counter *counter,
1787 void (*func)(struct perf_counter *))
1788{
1789 struct perf_counter *child;
1790
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001791 WARN_ON_ONCE(counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001792 mutex_lock(&counter->child_mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001793 func(counter);
1794 list_for_each_entry(child, &counter->child_list, child_list)
1795 func(child);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001796 mutex_unlock(&counter->child_mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001797}
1798
1799static void perf_counter_for_each(struct perf_counter *counter,
1800 void (*func)(struct perf_counter *))
1801{
Peter Zijlstra75f937f2009-06-15 15:05:12 +02001802 struct perf_counter_context *ctx = counter->ctx;
1803 struct perf_counter *sibling;
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001804
Peter Zijlstra75f937f2009-06-15 15:05:12 +02001805 WARN_ON_ONCE(ctx->parent_ctx);
1806 mutex_lock(&ctx->mutex);
1807 counter = counter->group_leader;
1808
1809 perf_counter_for_each_child(counter, func);
1810 func(counter);
1811 list_for_each_entry(sibling, &counter->sibling_list, list_entry)
1812 perf_counter_for_each_child(counter, func);
1813 mutex_unlock(&ctx->mutex);
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001814}
1815
Peter Zijlstra08247e32009-06-02 16:46:57 +02001816static int perf_counter_period(struct perf_counter *counter, u64 __user *arg)
1817{
1818 struct perf_counter_context *ctx = counter->ctx;
1819 unsigned long size;
1820 int ret = 0;
1821 u64 value;
1822
Peter Zijlstra0d486962009-06-02 19:22:16 +02001823 if (!counter->attr.sample_period)
Peter Zijlstra08247e32009-06-02 16:46:57 +02001824 return -EINVAL;
1825
1826 size = copy_from_user(&value, arg, sizeof(value));
1827 if (size != sizeof(value))
1828 return -EFAULT;
1829
1830 if (!value)
1831 return -EINVAL;
1832
1833 spin_lock_irq(&ctx->lock);
Peter Zijlstra0d486962009-06-02 19:22:16 +02001834 if (counter->attr.freq) {
Peter Zijlstradf58ab22009-06-11 11:25:05 +02001835 if (value > sysctl_perf_counter_sample_rate) {
Peter Zijlstra08247e32009-06-02 16:46:57 +02001836 ret = -EINVAL;
1837 goto unlock;
1838 }
1839
Peter Zijlstra0d486962009-06-02 19:22:16 +02001840 counter->attr.sample_freq = value;
Peter Zijlstra08247e32009-06-02 16:46:57 +02001841 } else {
Peter Zijlstra0d486962009-06-02 19:22:16 +02001842 counter->attr.sample_period = value;
Peter Zijlstra08247e32009-06-02 16:46:57 +02001843 counter->hw.sample_period = value;
Peter Zijlstra08247e32009-06-02 16:46:57 +02001844 }
1845unlock:
1846 spin_unlock_irq(&ctx->lock);
1847
1848 return ret;
1849}
1850
Paul Mackerrasd859e292009-01-17 18:10:22 +11001851static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1852{
1853 struct perf_counter *counter = file->private_data;
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001854 void (*func)(struct perf_counter *);
1855 u32 flags = arg;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001856
1857 switch (cmd) {
1858 case PERF_COUNTER_IOC_ENABLE:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001859 func = perf_counter_enable;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001860 break;
1861 case PERF_COUNTER_IOC_DISABLE:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001862 func = perf_counter_disable;
Peter Zijlstra79f14642009-04-06 11:45:07 +02001863 break;
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001864 case PERF_COUNTER_IOC_RESET:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001865 func = perf_counter_reset;
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001866 break;
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001867
1868 case PERF_COUNTER_IOC_REFRESH:
1869 return perf_counter_refresh(counter, arg);
Peter Zijlstra08247e32009-06-02 16:46:57 +02001870
1871 case PERF_COUNTER_IOC_PERIOD:
1872 return perf_counter_period(counter, (u64 __user *)arg);
1873
Paul Mackerrasd859e292009-01-17 18:10:22 +11001874 default:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001875 return -ENOTTY;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001876 }
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001877
1878 if (flags & PERF_IOC_FLAG_GROUP)
1879 perf_counter_for_each(counter, func);
1880 else
1881 perf_counter_for_each_child(counter, func);
1882
1883 return 0;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001884}
1885
Peter Zijlstra771d7cd2009-05-25 14:45:26 +02001886int perf_counter_task_enable(void)
1887{
1888 struct perf_counter *counter;
1889
1890 mutex_lock(&current->perf_counter_mutex);
1891 list_for_each_entry(counter, &current->perf_counter_list, owner_entry)
1892 perf_counter_for_each_child(counter, perf_counter_enable);
1893 mutex_unlock(&current->perf_counter_mutex);
1894
1895 return 0;
1896}
1897
1898int perf_counter_task_disable(void)
1899{
1900 struct perf_counter *counter;
1901
1902 mutex_lock(&current->perf_counter_mutex);
1903 list_for_each_entry(counter, &current->perf_counter_list, owner_entry)
1904 perf_counter_for_each_child(counter, perf_counter_disable);
1905 mutex_unlock(&current->perf_counter_mutex);
1906
1907 return 0;
1908}
1909
Peter Zijlstra194002b2009-06-22 16:35:24 +02001910static int perf_counter_index(struct perf_counter *counter)
1911{
1912 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
1913 return 0;
1914
1915 return counter->hw.idx + 1 - PERF_COUNTER_INDEX_OFFSET;
1916}
1917
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001918/*
1919 * Callers need to ensure there can be no nesting of this function, otherwise
1920 * the seqlock logic goes bad. We can not serialize this because the arch
1921 * code calls this from NMI context.
1922 */
1923void perf_counter_update_userpage(struct perf_counter *counter)
Paul Mackerras37d81822009-03-23 18:22:08 +01001924{
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001925 struct perf_counter_mmap_page *userpg;
Ingo Molnar22a4f652009-06-01 10:13:37 +02001926 struct perf_mmap_data *data;
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001927
1928 rcu_read_lock();
1929 data = rcu_dereference(counter->data);
1930 if (!data)
1931 goto unlock;
1932
1933 userpg = data->user_page;
Paul Mackerras37d81822009-03-23 18:22:08 +01001934
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001935 /*
1936 * Disable preemption so as to not let the corresponding user-space
1937 * spin too long if we get preempted.
1938 */
1939 preempt_disable();
Paul Mackerras37d81822009-03-23 18:22:08 +01001940 ++userpg->lock;
Peter Zijlstra92f22a32009-04-02 11:12:04 +02001941 barrier();
Peter Zijlstra194002b2009-06-22 16:35:24 +02001942 userpg->index = perf_counter_index(counter);
Paul Mackerras37d81822009-03-23 18:22:08 +01001943 userpg->offset = atomic64_read(&counter->count);
1944 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
1945 userpg->offset -= atomic64_read(&counter->hw.prev_count);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001946
Peter Zijlstra7f8b4e42009-06-22 14:34:35 +02001947 userpg->time_enabled = counter->total_time_enabled +
1948 atomic64_read(&counter->child_total_time_enabled);
1949
1950 userpg->time_running = counter->total_time_running +
1951 atomic64_read(&counter->child_total_time_running);
1952
Peter Zijlstra92f22a32009-04-02 11:12:04 +02001953 barrier();
Paul Mackerras37d81822009-03-23 18:22:08 +01001954 ++userpg->lock;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001955 preempt_enable();
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001956unlock:
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001957 rcu_read_unlock();
Paul Mackerras37d81822009-03-23 18:22:08 +01001958}
1959
1960static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1961{
1962 struct perf_counter *counter = vma->vm_file->private_data;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001963 struct perf_mmap_data *data;
1964 int ret = VM_FAULT_SIGBUS;
Paul Mackerras37d81822009-03-23 18:22:08 +01001965
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01001966 if (vmf->flags & FAULT_FLAG_MKWRITE) {
1967 if (vmf->pgoff == 0)
1968 ret = 0;
1969 return ret;
1970 }
1971
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001972 rcu_read_lock();
1973 data = rcu_dereference(counter->data);
1974 if (!data)
1975 goto unlock;
Paul Mackerras37d81822009-03-23 18:22:08 +01001976
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001977 if (vmf->pgoff == 0) {
1978 vmf->page = virt_to_page(data->user_page);
1979 } else {
1980 int nr = vmf->pgoff - 1;
1981
1982 if ((unsigned)nr > data->nr_pages)
1983 goto unlock;
1984
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01001985 if (vmf->flags & FAULT_FLAG_WRITE)
1986 goto unlock;
1987
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001988 vmf->page = virt_to_page(data->data_pages[nr]);
1989 }
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01001990
Paul Mackerras37d81822009-03-23 18:22:08 +01001991 get_page(vmf->page);
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01001992 vmf->page->mapping = vma->vm_file->f_mapping;
1993 vmf->page->index = vmf->pgoff;
1994
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001995 ret = 0;
1996unlock:
1997 rcu_read_unlock();
1998
1999 return ret;
2000}
2001
2002static int perf_mmap_data_alloc(struct perf_counter *counter, int nr_pages)
2003{
2004 struct perf_mmap_data *data;
2005 unsigned long size;
2006 int i;
2007
2008 WARN_ON(atomic_read(&counter->mmap_count));
2009
2010 size = sizeof(struct perf_mmap_data);
2011 size += nr_pages * sizeof(void *);
2012
2013 data = kzalloc(size, GFP_KERNEL);
2014 if (!data)
2015 goto fail;
2016
2017 data->user_page = (void *)get_zeroed_page(GFP_KERNEL);
2018 if (!data->user_page)
2019 goto fail_user_page;
2020
2021 for (i = 0; i < nr_pages; i++) {
2022 data->data_pages[i] = (void *)get_zeroed_page(GFP_KERNEL);
2023 if (!data->data_pages[i])
2024 goto fail_data_pages;
2025 }
2026
2027 data->nr_pages = nr_pages;
Peter Zijlstra22c15582009-05-05 17:50:25 +02002028 atomic_set(&data->lock, -1);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002029
2030 rcu_assign_pointer(counter->data, data);
2031
Paul Mackerras37d81822009-03-23 18:22:08 +01002032 return 0;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002033
2034fail_data_pages:
2035 for (i--; i >= 0; i--)
2036 free_page((unsigned long)data->data_pages[i]);
2037
2038 free_page((unsigned long)data->user_page);
2039
2040fail_user_page:
2041 kfree(data);
2042
2043fail:
2044 return -ENOMEM;
2045}
2046
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002047static void perf_mmap_free_page(unsigned long addr)
2048{
Kevin Cernekee5bfd7562009-07-05 12:08:19 -07002049 struct page *page = virt_to_page((void *)addr);
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002050
2051 page->mapping = NULL;
2052 __free_page(page);
2053}
2054
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002055static void __perf_mmap_data_free(struct rcu_head *rcu_head)
2056{
Ingo Molnar22a4f652009-06-01 10:13:37 +02002057 struct perf_mmap_data *data;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002058 int i;
2059
Ingo Molnar22a4f652009-06-01 10:13:37 +02002060 data = container_of(rcu_head, struct perf_mmap_data, rcu_head);
2061
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002062 perf_mmap_free_page((unsigned long)data->user_page);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002063 for (i = 0; i < data->nr_pages; i++)
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002064 perf_mmap_free_page((unsigned long)data->data_pages[i]);
2065
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002066 kfree(data);
2067}
2068
2069static void perf_mmap_data_free(struct perf_counter *counter)
2070{
2071 struct perf_mmap_data *data = counter->data;
2072
2073 WARN_ON(atomic_read(&counter->mmap_count));
2074
2075 rcu_assign_pointer(counter->data, NULL);
2076 call_rcu(&data->rcu_head, __perf_mmap_data_free);
2077}
2078
2079static void perf_mmap_open(struct vm_area_struct *vma)
2080{
2081 struct perf_counter *counter = vma->vm_file->private_data;
2082
2083 atomic_inc(&counter->mmap_count);
2084}
2085
2086static void perf_mmap_close(struct vm_area_struct *vma)
2087{
2088 struct perf_counter *counter = vma->vm_file->private_data;
2089
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10002090 WARN_ON_ONCE(counter->ctx->parent_ctx);
Ingo Molnar22a4f652009-06-01 10:13:37 +02002091 if (atomic_dec_and_mutex_lock(&counter->mmap_count, &counter->mmap_mutex)) {
Peter Zijlstra789f90f2009-05-15 15:19:27 +02002092 struct user_struct *user = current_user();
2093
2094 atomic_long_sub(counter->data->nr_pages + 1, &user->locked_vm);
Peter Zijlstrac5078f72009-05-05 17:50:24 +02002095 vma->vm_mm->locked_vm -= counter->data->nr_locked;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002096 perf_mmap_data_free(counter);
2097 mutex_unlock(&counter->mmap_mutex);
2098 }
Paul Mackerras37d81822009-03-23 18:22:08 +01002099}
2100
2101static struct vm_operations_struct perf_mmap_vmops = {
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002102 .open = perf_mmap_open,
2103 .close = perf_mmap_close,
2104 .fault = perf_mmap_fault,
2105 .page_mkwrite = perf_mmap_fault,
Paul Mackerras37d81822009-03-23 18:22:08 +01002106};
2107
2108static int perf_mmap(struct file *file, struct vm_area_struct *vma)
2109{
2110 struct perf_counter *counter = file->private_data;
Ingo Molnar22a4f652009-06-01 10:13:37 +02002111 unsigned long user_locked, user_lock_limit;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02002112 struct user_struct *user = current_user();
Ingo Molnar22a4f652009-06-01 10:13:37 +02002113 unsigned long locked, lock_limit;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002114 unsigned long vma_size;
2115 unsigned long nr_pages;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02002116 long user_extra, extra;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002117 int ret = 0;
Paul Mackerras37d81822009-03-23 18:22:08 +01002118
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002119 if (!(vma->vm_flags & VM_SHARED))
Paul Mackerras37d81822009-03-23 18:22:08 +01002120 return -EINVAL;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002121
2122 vma_size = vma->vm_end - vma->vm_start;
2123 nr_pages = (vma_size / PAGE_SIZE) - 1;
2124
Peter Zijlstra7730d862009-03-25 12:48:31 +01002125 /*
2126 * If we have data pages ensure they're a power-of-two number, so we
2127 * can do bitmasks instead of modulo.
2128 */
2129 if (nr_pages != 0 && !is_power_of_2(nr_pages))
Paul Mackerras37d81822009-03-23 18:22:08 +01002130 return -EINVAL;
2131
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002132 if (vma_size != PAGE_SIZE * (1 + nr_pages))
Paul Mackerras37d81822009-03-23 18:22:08 +01002133 return -EINVAL;
2134
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002135 if (vma->vm_pgoff != 0)
2136 return -EINVAL;
Paul Mackerras37d81822009-03-23 18:22:08 +01002137
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10002138 WARN_ON_ONCE(counter->ctx->parent_ctx);
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02002139 mutex_lock(&counter->mmap_mutex);
2140 if (atomic_inc_not_zero(&counter->mmap_count)) {
2141 if (nr_pages != counter->data->nr_pages)
2142 ret = -EINVAL;
2143 goto unlock;
2144 }
2145
Peter Zijlstra789f90f2009-05-15 15:19:27 +02002146 user_extra = nr_pages + 1;
2147 user_lock_limit = sysctl_perf_counter_mlock >> (PAGE_SHIFT - 10);
Ingo Molnara3862d32009-05-24 09:02:37 +02002148
2149 /*
2150 * Increase the limit linearly with more CPUs:
2151 */
2152 user_lock_limit *= num_online_cpus();
2153
Peter Zijlstra789f90f2009-05-15 15:19:27 +02002154 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
Peter Zijlstrac5078f72009-05-05 17:50:24 +02002155
Peter Zijlstra789f90f2009-05-15 15:19:27 +02002156 extra = 0;
2157 if (user_locked > user_lock_limit)
2158 extra = user_locked - user_lock_limit;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002159
2160 lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
2161 lock_limit >>= PAGE_SHIFT;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02002162 locked = vma->vm_mm->locked_vm + extra;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002163
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02002164 if ((locked > lock_limit) && !capable(CAP_IPC_LOCK)) {
2165 ret = -EPERM;
2166 goto unlock;
2167 }
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002168
2169 WARN_ON(counter->data);
2170 ret = perf_mmap_data_alloc(counter, nr_pages);
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02002171 if (ret)
2172 goto unlock;
2173
2174 atomic_set(&counter->mmap_count, 1);
Peter Zijlstra789f90f2009-05-15 15:19:27 +02002175 atomic_long_add(user_extra, &user->locked_vm);
Peter Zijlstrac5078f72009-05-05 17:50:24 +02002176 vma->vm_mm->locked_vm += extra;
2177 counter->data->nr_locked = extra;
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002178 if (vma->vm_flags & VM_WRITE)
2179 counter->data->writable = 1;
2180
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02002181unlock:
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002182 mutex_unlock(&counter->mmap_mutex);
Paul Mackerras37d81822009-03-23 18:22:08 +01002183
Paul Mackerras37d81822009-03-23 18:22:08 +01002184 vma->vm_flags |= VM_RESERVED;
2185 vma->vm_ops = &perf_mmap_vmops;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002186
2187 return ret;
Paul Mackerras37d81822009-03-23 18:22:08 +01002188}
2189
Peter Zijlstra3c446b32009-04-06 11:45:01 +02002190static int perf_fasync(int fd, struct file *filp, int on)
2191{
Peter Zijlstra3c446b32009-04-06 11:45:01 +02002192 struct inode *inode = filp->f_path.dentry->d_inode;
Ingo Molnar22a4f652009-06-01 10:13:37 +02002193 struct perf_counter *counter = filp->private_data;
Peter Zijlstra3c446b32009-04-06 11:45:01 +02002194 int retval;
2195
2196 mutex_lock(&inode->i_mutex);
2197 retval = fasync_helper(fd, filp, on, &counter->fasync);
2198 mutex_unlock(&inode->i_mutex);
2199
2200 if (retval < 0)
2201 return retval;
2202
2203 return 0;
2204}
2205
Thomas Gleixner0793a612008-12-04 20:12:29 +01002206static const struct file_operations perf_fops = {
2207 .release = perf_release,
2208 .read = perf_read,
2209 .poll = perf_poll,
Paul Mackerrasd859e292009-01-17 18:10:22 +11002210 .unlocked_ioctl = perf_ioctl,
2211 .compat_ioctl = perf_ioctl,
Paul Mackerras37d81822009-03-23 18:22:08 +01002212 .mmap = perf_mmap,
Peter Zijlstra3c446b32009-04-06 11:45:01 +02002213 .fasync = perf_fasync,
Thomas Gleixner0793a612008-12-04 20:12:29 +01002214};
2215
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002216/*
Peter Zijlstra925d5192009-03-30 19:07:02 +02002217 * Perf counter wakeup
2218 *
2219 * If there's data, ensure we set the poll() state and publish everything
2220 * to user-space before waking everybody up.
2221 */
2222
2223void perf_counter_wakeup(struct perf_counter *counter)
2224{
Peter Zijlstra925d5192009-03-30 19:07:02 +02002225 wake_up_all(&counter->waitq);
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002226
2227 if (counter->pending_kill) {
2228 kill_fasync(&counter->fasync, SIGIO, counter->pending_kill);
2229 counter->pending_kill = 0;
2230 }
Peter Zijlstra925d5192009-03-30 19:07:02 +02002231}
2232
2233/*
2234 * Pending wakeups
2235 *
2236 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
2237 *
2238 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
2239 * single linked list and use cmpxchg() to add entries lockless.
2240 */
2241
Peter Zijlstra79f14642009-04-06 11:45:07 +02002242static void perf_pending_counter(struct perf_pending_entry *entry)
2243{
2244 struct perf_counter *counter = container_of(entry,
2245 struct perf_counter, pending);
2246
2247 if (counter->pending_disable) {
2248 counter->pending_disable = 0;
2249 perf_counter_disable(counter);
2250 }
2251
2252 if (counter->pending_wakeup) {
2253 counter->pending_wakeup = 0;
2254 perf_counter_wakeup(counter);
2255 }
2256}
2257
Peter Zijlstra671dec52009-04-06 11:45:02 +02002258#define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
Peter Zijlstra925d5192009-03-30 19:07:02 +02002259
Peter Zijlstra671dec52009-04-06 11:45:02 +02002260static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = {
Peter Zijlstra925d5192009-03-30 19:07:02 +02002261 PENDING_TAIL,
2262};
2263
Peter Zijlstra671dec52009-04-06 11:45:02 +02002264static void perf_pending_queue(struct perf_pending_entry *entry,
2265 void (*func)(struct perf_pending_entry *))
Peter Zijlstra925d5192009-03-30 19:07:02 +02002266{
Peter Zijlstra671dec52009-04-06 11:45:02 +02002267 struct perf_pending_entry **head;
Peter Zijlstra925d5192009-03-30 19:07:02 +02002268
Peter Zijlstra671dec52009-04-06 11:45:02 +02002269 if (cmpxchg(&entry->next, NULL, PENDING_TAIL) != NULL)
Peter Zijlstra925d5192009-03-30 19:07:02 +02002270 return;
2271
Peter Zijlstra671dec52009-04-06 11:45:02 +02002272 entry->func = func;
2273
2274 head = &get_cpu_var(perf_pending_head);
Peter Zijlstra925d5192009-03-30 19:07:02 +02002275
2276 do {
Peter Zijlstra671dec52009-04-06 11:45:02 +02002277 entry->next = *head;
2278 } while (cmpxchg(head, entry->next, entry) != entry->next);
Peter Zijlstra925d5192009-03-30 19:07:02 +02002279
2280 set_perf_counter_pending();
2281
Peter Zijlstra671dec52009-04-06 11:45:02 +02002282 put_cpu_var(perf_pending_head);
Peter Zijlstra925d5192009-03-30 19:07:02 +02002283}
2284
2285static int __perf_pending_run(void)
2286{
Peter Zijlstra671dec52009-04-06 11:45:02 +02002287 struct perf_pending_entry *list;
Peter Zijlstra925d5192009-03-30 19:07:02 +02002288 int nr = 0;
2289
Peter Zijlstra671dec52009-04-06 11:45:02 +02002290 list = xchg(&__get_cpu_var(perf_pending_head), PENDING_TAIL);
Peter Zijlstra925d5192009-03-30 19:07:02 +02002291 while (list != PENDING_TAIL) {
Peter Zijlstra671dec52009-04-06 11:45:02 +02002292 void (*func)(struct perf_pending_entry *);
2293 struct perf_pending_entry *entry = list;
Peter Zijlstra925d5192009-03-30 19:07:02 +02002294
2295 list = list->next;
2296
Peter Zijlstra671dec52009-04-06 11:45:02 +02002297 func = entry->func;
2298 entry->next = NULL;
Peter Zijlstra925d5192009-03-30 19:07:02 +02002299 /*
2300 * Ensure we observe the unqueue before we issue the wakeup,
2301 * so that we won't be waiting forever.
2302 * -- see perf_not_pending().
2303 */
2304 smp_wmb();
2305
Peter Zijlstra671dec52009-04-06 11:45:02 +02002306 func(entry);
Peter Zijlstra925d5192009-03-30 19:07:02 +02002307 nr++;
2308 }
2309
2310 return nr;
2311}
2312
2313static inline int perf_not_pending(struct perf_counter *counter)
2314{
2315 /*
2316 * If we flush on whatever cpu we run, there is a chance we don't
2317 * need to wait.
2318 */
2319 get_cpu();
2320 __perf_pending_run();
2321 put_cpu();
2322
2323 /*
2324 * Ensure we see the proper queue state before going to sleep
2325 * so that we do not miss the wakeup. -- see perf_pending_handle()
2326 */
2327 smp_rmb();
Peter Zijlstra671dec52009-04-06 11:45:02 +02002328 return counter->pending.next == NULL;
Peter Zijlstra925d5192009-03-30 19:07:02 +02002329}
2330
2331static void perf_pending_sync(struct perf_counter *counter)
2332{
2333 wait_event(counter->waitq, perf_not_pending(counter));
2334}
2335
2336void perf_counter_do_pending(void)
2337{
2338 __perf_pending_run();
2339}
2340
2341/*
Peter Zijlstra394ee072009-03-30 19:07:14 +02002342 * Callchain support -- arch specific
2343 */
2344
Peter Zijlstra9c03d882009-04-06 11:45:00 +02002345__weak struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
Peter Zijlstra394ee072009-03-30 19:07:14 +02002346{
2347 return NULL;
2348}
2349
2350/*
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002351 * Output
2352 */
2353
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002354struct perf_output_handle {
2355 struct perf_counter *counter;
2356 struct perf_mmap_data *data;
Peter Zijlstra8e3747c2009-06-02 16:16:02 +02002357 unsigned long head;
2358 unsigned long offset;
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002359 int nmi;
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002360 int sample;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002361 int locked;
2362 unsigned long flags;
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002363};
2364
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002365static bool perf_output_space(struct perf_mmap_data *data,
2366 unsigned int offset, unsigned int head)
2367{
2368 unsigned long tail;
2369 unsigned long mask;
2370
2371 if (!data->writable)
2372 return true;
2373
2374 mask = (data->nr_pages << PAGE_SHIFT) - 1;
2375 /*
2376 * Userspace could choose to issue a mb() before updating the tail
2377 * pointer. So that all reads will be completed before the write is
2378 * issued.
2379 */
2380 tail = ACCESS_ONCE(data->user_page->data_tail);
2381 smp_rmb();
2382
2383 offset = (offset - tail) & mask;
2384 head = (head - tail) & mask;
2385
2386 if ((int)(head - offset) < 0)
2387 return false;
2388
2389 return true;
2390}
2391
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002392static void perf_output_wakeup(struct perf_output_handle *handle)
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002393{
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002394 atomic_set(&handle->data->poll, POLL_IN);
2395
Peter Zijlstra671dec52009-04-06 11:45:02 +02002396 if (handle->nmi) {
Peter Zijlstra79f14642009-04-06 11:45:07 +02002397 handle->counter->pending_wakeup = 1;
Peter Zijlstra671dec52009-04-06 11:45:02 +02002398 perf_pending_queue(&handle->counter->pending,
Peter Zijlstra79f14642009-04-06 11:45:07 +02002399 perf_pending_counter);
Peter Zijlstra671dec52009-04-06 11:45:02 +02002400 } else
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002401 perf_counter_wakeup(handle->counter);
2402}
2403
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002404/*
2405 * Curious locking construct.
2406 *
2407 * We need to ensure a later event doesn't publish a head when a former
2408 * event isn't done writing. However since we need to deal with NMIs we
2409 * cannot fully serialize things.
2410 *
2411 * What we do is serialize between CPUs so we only have to deal with NMI
2412 * nesting on a single CPU.
2413 *
2414 * We only publish the head (and generate a wakeup) when the outer-most
2415 * event completes.
2416 */
2417static void perf_output_lock(struct perf_output_handle *handle)
2418{
2419 struct perf_mmap_data *data = handle->data;
2420 int cpu;
2421
2422 handle->locked = 0;
2423
2424 local_irq_save(handle->flags);
2425 cpu = smp_processor_id();
2426
2427 if (in_nmi() && atomic_read(&data->lock) == cpu)
2428 return;
2429
Peter Zijlstra22c15582009-05-05 17:50:25 +02002430 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002431 cpu_relax();
2432
2433 handle->locked = 1;
2434}
2435
2436static void perf_output_unlock(struct perf_output_handle *handle)
2437{
2438 struct perf_mmap_data *data = handle->data;
Peter Zijlstra8e3747c2009-06-02 16:16:02 +02002439 unsigned long head;
2440 int cpu;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002441
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002442 data->done_head = data->head;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002443
2444 if (!handle->locked)
2445 goto out;
2446
2447again:
2448 /*
2449 * The xchg implies a full barrier that ensures all writes are done
2450 * before we publish the new head, matched by a rmb() in userspace when
2451 * reading this position.
2452 */
Peter Zijlstra8e3747c2009-06-02 16:16:02 +02002453 while ((head = atomic_long_xchg(&data->done_head, 0)))
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002454 data->user_page->data_head = head;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002455
2456 /*
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002457 * NMI can happen here, which means we can miss a done_head update.
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002458 */
2459
Peter Zijlstra22c15582009-05-05 17:50:25 +02002460 cpu = atomic_xchg(&data->lock, -1);
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002461 WARN_ON_ONCE(cpu != smp_processor_id());
2462
2463 /*
2464 * Therefore we have to validate we did not indeed do so.
2465 */
Peter Zijlstra8e3747c2009-06-02 16:16:02 +02002466 if (unlikely(atomic_long_read(&data->done_head))) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002467 /*
2468 * Since we had it locked, we can lock it again.
2469 */
Peter Zijlstra22c15582009-05-05 17:50:25 +02002470 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002471 cpu_relax();
2472
2473 goto again;
2474 }
2475
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002476 if (atomic_xchg(&data->wakeup, 0))
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002477 perf_output_wakeup(handle);
2478out:
2479 local_irq_restore(handle->flags);
2480}
2481
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002482static void perf_output_copy(struct perf_output_handle *handle,
Peter Zijlstra089dd792009-06-05 14:04:55 +02002483 const void *buf, unsigned int len)
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002484{
2485 unsigned int pages_mask;
2486 unsigned int offset;
2487 unsigned int size;
2488 void **pages;
2489
2490 offset = handle->offset;
2491 pages_mask = handle->data->nr_pages - 1;
2492 pages = handle->data->data_pages;
2493
2494 do {
2495 unsigned int page_offset;
2496 int nr;
2497
2498 nr = (offset >> PAGE_SHIFT) & pages_mask;
2499 page_offset = offset & (PAGE_SIZE - 1);
2500 size = min_t(unsigned int, PAGE_SIZE - page_offset, len);
2501
2502 memcpy(pages[nr] + page_offset, buf, size);
2503
2504 len -= size;
2505 buf += size;
2506 offset += size;
2507 } while (len);
2508
2509 handle->offset = offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01002510
Peter Zijlstra53020fe2009-05-13 21:26:19 +02002511 /*
2512 * Check we didn't copy past our reservation window, taking the
2513 * possible unsigned int wrap into account.
2514 */
Peter Zijlstra8e3747c2009-06-02 16:16:02 +02002515 WARN_ON_ONCE(((long)(handle->head - handle->offset)) < 0);
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002516}
2517
Peter Zijlstra5c148192009-03-25 12:30:23 +01002518#define perf_output_put(handle, x) \
2519 perf_output_copy((handle), &(x), sizeof(x))
2520
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002521static int perf_output_begin(struct perf_output_handle *handle,
2522 struct perf_counter *counter, unsigned int size,
2523 int nmi, int sample)
2524{
2525 struct perf_mmap_data *data;
2526 unsigned int offset, head;
2527 int have_lost;
2528 struct {
2529 struct perf_event_header header;
2530 u64 id;
2531 u64 lost;
2532 } lost_event;
2533
2534 /*
2535 * For inherited counters we send all the output towards the parent.
2536 */
2537 if (counter->parent)
2538 counter = counter->parent;
2539
2540 rcu_read_lock();
2541 data = rcu_dereference(counter->data);
2542 if (!data)
2543 goto out;
2544
2545 handle->data = data;
2546 handle->counter = counter;
2547 handle->nmi = nmi;
2548 handle->sample = sample;
2549
2550 if (!data->nr_pages)
2551 goto fail;
2552
2553 have_lost = atomic_read(&data->lost);
2554 if (have_lost)
2555 size += sizeof(lost_event);
2556
2557 perf_output_lock(handle);
2558
2559 do {
2560 offset = head = atomic_long_read(&data->head);
2561 head += size;
2562 if (unlikely(!perf_output_space(data, offset, head)))
2563 goto fail;
2564 } while (atomic_long_cmpxchg(&data->head, offset, head) != offset);
2565
2566 handle->offset = offset;
2567 handle->head = head;
2568
2569 if ((offset >> PAGE_SHIFT) != (head >> PAGE_SHIFT))
2570 atomic_set(&data->wakeup, 1);
2571
2572 if (have_lost) {
2573 lost_event.header.type = PERF_EVENT_LOST;
2574 lost_event.header.misc = 0;
2575 lost_event.header.size = sizeof(lost_event);
2576 lost_event.id = counter->id;
2577 lost_event.lost = atomic_xchg(&data->lost, 0);
2578
2579 perf_output_put(handle, lost_event);
2580 }
2581
2582 return 0;
2583
2584fail:
2585 atomic_inc(&data->lost);
2586 perf_output_unlock(handle);
2587out:
2588 rcu_read_unlock();
2589
2590 return -ENOSPC;
2591}
2592
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002593static void perf_output_end(struct perf_output_handle *handle)
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002594{
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002595 struct perf_counter *counter = handle->counter;
2596 struct perf_mmap_data *data = handle->data;
2597
Peter Zijlstra0d486962009-06-02 19:22:16 +02002598 int wakeup_events = counter->attr.wakeup_events;
Peter Zijlstrac4578102009-04-02 11:12:01 +02002599
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002600 if (handle->sample && wakeup_events) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002601 int events = atomic_inc_return(&data->events);
Peter Zijlstrac4578102009-04-02 11:12:01 +02002602 if (events >= wakeup_events) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002603 atomic_sub(wakeup_events, &data->events);
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002604 atomic_set(&data->wakeup, 1);
Peter Zijlstrac4578102009-04-02 11:12:01 +02002605 }
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002606 }
2607
2608 perf_output_unlock(handle);
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002609 rcu_read_unlock();
2610}
2611
Peter Zijlstra709e50c2009-06-02 14:13:15 +02002612static u32 perf_counter_pid(struct perf_counter *counter, struct task_struct *p)
2613{
2614 /*
2615 * only top level counters have the pid namespace they were created in
2616 */
2617 if (counter->parent)
2618 counter = counter->parent;
2619
2620 return task_tgid_nr_ns(p, counter->ns);
2621}
2622
2623static u32 perf_counter_tid(struct perf_counter *counter, struct task_struct *p)
2624{
2625 /*
2626 * only top level counters have the pid namespace they were created in
2627 */
2628 if (counter->parent)
2629 counter = counter->parent;
2630
2631 return task_pid_nr_ns(p, counter->ns);
2632}
2633
Ingo Molnar28402972009-08-13 10:13:22 +02002634void perf_counter_output(struct perf_counter *counter, int nmi,
Peter Zijlstradf1a1322009-06-10 21:02:22 +02002635 struct perf_sample_data *data)
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002636{
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002637 int ret;
Peter Zijlstra0d486962009-06-02 19:22:16 +02002638 u64 sample_type = counter->attr.sample_type;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002639 struct perf_output_handle handle;
2640 struct perf_event_header header;
2641 u64 ip;
Peter Zijlstra5c148192009-03-25 12:30:23 +01002642 struct {
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002643 u32 pid, tid;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002644 } tid_entry;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002645 struct {
Peter Zijlstra8e5799b2009-06-02 15:08:15 +02002646 u64 id;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002647 u64 counter;
2648 } group_entry;
Peter Zijlstra394ee072009-03-30 19:07:14 +02002649 struct perf_callchain_entry *callchain = NULL;
2650 int callchain_size = 0;
Peter Zijlstra339f7c92009-04-06 11:45:06 +02002651 u64 time;
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002652 struct {
2653 u32 cpu, reserved;
2654 } cpu_entry;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002655
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02002656 header.type = PERF_EVENT_SAMPLE;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002657 header.size = sizeof(header);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002658
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02002659 header.misc = 0;
Peter Zijlstradf1a1322009-06-10 21:02:22 +02002660 header.misc |= perf_misc_flags(data->regs);
Peter Zijlstra6fab0192009-04-08 15:01:26 +02002661
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002662 if (sample_type & PERF_SAMPLE_IP) {
Peter Zijlstradf1a1322009-06-10 21:02:22 +02002663 ip = perf_instruction_pointer(data->regs);
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002664 header.size += sizeof(ip);
2665 }
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002666
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002667 if (sample_type & PERF_SAMPLE_TID) {
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002668 /* namespace issues */
Peter Zijlstra709e50c2009-06-02 14:13:15 +02002669 tid_entry.pid = perf_counter_pid(counter, current);
2670 tid_entry.tid = perf_counter_tid(counter, current);
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002671
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002672 header.size += sizeof(tid_entry);
2673 }
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002674
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002675 if (sample_type & PERF_SAMPLE_TIME) {
Peter Zijlstra4d855452009-04-08 15:01:32 +02002676 /*
2677 * Maybe do better on x86 and provide cpu_clock_nmi()
2678 */
2679 time = sched_clock();
2680
Peter Zijlstra4d855452009-04-08 15:01:32 +02002681 header.size += sizeof(u64);
2682 }
2683
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02002684 if (sample_type & PERF_SAMPLE_ADDR)
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002685 header.size += sizeof(u64);
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002686
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02002687 if (sample_type & PERF_SAMPLE_ID)
Peter Zijlstraa85f61a2009-05-08 18:52:23 +02002688 header.size += sizeof(u64);
Peter Zijlstraa85f61a2009-05-08 18:52:23 +02002689
Peter Zijlstra7f453c22009-07-21 13:19:40 +02002690 if (sample_type & PERF_SAMPLE_STREAM_ID)
2691 header.size += sizeof(u64);
2692
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002693 if (sample_type & PERF_SAMPLE_CPU) {
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002694 header.size += sizeof(cpu_entry);
2695
2696 cpu_entry.cpu = raw_smp_processor_id();
Arjan van de Ven0dc3d522009-07-21 00:55:05 -07002697 cpu_entry.reserved = 0;
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002698 }
2699
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02002700 if (sample_type & PERF_SAMPLE_PERIOD)
Peter Zijlstra689802b2009-06-05 15:05:43 +02002701 header.size += sizeof(u64);
Peter Zijlstra689802b2009-06-05 15:05:43 +02002702
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002703 if (sample_type & PERF_SAMPLE_GROUP) {
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002704 header.size += sizeof(u64) +
2705 counter->nr_siblings * sizeof(group_entry);
2706 }
2707
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002708 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
Peter Zijlstradf1a1322009-06-10 21:02:22 +02002709 callchain = perf_callchain(data->regs);
Peter Zijlstra394ee072009-03-30 19:07:14 +02002710
2711 if (callchain) {
Peter Zijlstra9c03d882009-04-06 11:45:00 +02002712 callchain_size = (1 + callchain->nr) * sizeof(u64);
Peter Zijlstra394ee072009-03-30 19:07:14 +02002713 header.size += callchain_size;
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02002714 } else
2715 header.size += sizeof(u64);
Peter Zijlstra394ee072009-03-30 19:07:14 +02002716 }
2717
Frederic Weisbecker3a43ce62009-08-08 04:26:37 +02002718 if (sample_type & PERF_SAMPLE_RAW) {
Peter Zijlstraa0445602009-08-10 11:16:52 +02002719 int size = sizeof(u32);
2720
2721 if (data->raw)
2722 size += data->raw->size;
2723 else
2724 size += sizeof(u32);
2725
2726 WARN_ON_ONCE(size & (sizeof(u64)-1));
2727 header.size += size;
Frederic Weisbeckerf413cdb2009-08-07 01:25:54 +02002728 }
2729
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002730 ret = perf_output_begin(&handle, counter, header.size, nmi, 1);
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002731 if (ret)
2732 return;
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002733
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002734 perf_output_put(&handle, header);
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002735
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002736 if (sample_type & PERF_SAMPLE_IP)
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002737 perf_output_put(&handle, ip);
2738
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002739 if (sample_type & PERF_SAMPLE_TID)
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002740 perf_output_put(&handle, tid_entry);
2741
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002742 if (sample_type & PERF_SAMPLE_TIME)
Peter Zijlstra4d855452009-04-08 15:01:32 +02002743 perf_output_put(&handle, time);
2744
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002745 if (sample_type & PERF_SAMPLE_ADDR)
Peter Zijlstradf1a1322009-06-10 21:02:22 +02002746 perf_output_put(&handle, data->addr);
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002747
Peter Zijlstra7f453c22009-07-21 13:19:40 +02002748 if (sample_type & PERF_SAMPLE_ID) {
2749 u64 id = primary_counter_id(counter);
2750
2751 perf_output_put(&handle, id);
2752 }
2753
2754 if (sample_type & PERF_SAMPLE_STREAM_ID)
Peter Zijlstraac4bcf82009-06-05 14:44:52 +02002755 perf_output_put(&handle, counter->id);
Peter Zijlstraa85f61a2009-05-08 18:52:23 +02002756
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002757 if (sample_type & PERF_SAMPLE_CPU)
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002758 perf_output_put(&handle, cpu_entry);
2759
Peter Zijlstra689802b2009-06-05 15:05:43 +02002760 if (sample_type & PERF_SAMPLE_PERIOD)
Peter Zijlstra9e350de2009-06-10 21:34:59 +02002761 perf_output_put(&handle, data->period);
Peter Zijlstra689802b2009-06-05 15:05:43 +02002762
Peter Zijlstra2023b352009-05-05 17:50:26 +02002763 /*
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002764 * XXX PERF_SAMPLE_GROUP vs inherited counters seems difficult.
Peter Zijlstra2023b352009-05-05 17:50:26 +02002765 */
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002766 if (sample_type & PERF_SAMPLE_GROUP) {
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002767 struct perf_counter *leader, *sub;
2768 u64 nr = counter->nr_siblings;
2769
2770 perf_output_put(&handle, nr);
2771
2772 leader = counter->group_leader;
2773 list_for_each_entry(sub, &leader->sibling_list, list_entry) {
2774 if (sub != counter)
Robert Richter4aeb0b42009-04-29 12:47:03 +02002775 sub->pmu->read(sub);
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002776
Peter Zijlstra7f453c22009-07-21 13:19:40 +02002777 group_entry.id = primary_counter_id(sub);
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002778 group_entry.counter = atomic64_read(&sub->count);
2779
2780 perf_output_put(&handle, group_entry);
2781 }
2782 }
2783
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02002784 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
2785 if (callchain)
2786 perf_output_copy(&handle, callchain, callchain_size);
2787 else {
2788 u64 nr = 0;
2789 perf_output_put(&handle, nr);
2790 }
2791 }
Peter Zijlstra394ee072009-03-30 19:07:14 +02002792
Peter Zijlstraa0445602009-08-10 11:16:52 +02002793 if (sample_type & PERF_SAMPLE_RAW) {
2794 if (data->raw) {
2795 perf_output_put(&handle, data->raw->size);
2796 perf_output_copy(&handle, data->raw->data, data->raw->size);
2797 } else {
2798 struct {
2799 u32 size;
2800 u32 data;
2801 } raw = {
2802 .size = sizeof(u32),
2803 .data = 0,
2804 };
2805 perf_output_put(&handle, raw);
2806 }
2807 }
Frederic Weisbeckerf413cdb2009-08-07 01:25:54 +02002808
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002809 perf_output_end(&handle);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002810}
2811
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002812/*
Peter Zijlstra38b200d2009-06-23 20:13:11 +02002813 * read event
2814 */
2815
2816struct perf_read_event {
2817 struct perf_event_header header;
2818
2819 u32 pid;
2820 u32 tid;
2821 u64 value;
2822 u64 format[3];
2823};
2824
2825static void
2826perf_counter_read_event(struct perf_counter *counter,
2827 struct task_struct *task)
2828{
2829 struct perf_output_handle handle;
2830 struct perf_read_event event = {
2831 .header = {
2832 .type = PERF_EVENT_READ,
2833 .misc = 0,
2834 .size = sizeof(event) - sizeof(event.format),
2835 },
2836 .pid = perf_counter_pid(counter, task),
2837 .tid = perf_counter_tid(counter, task),
2838 .value = atomic64_read(&counter->count),
2839 };
2840 int ret, i = 0;
2841
2842 if (counter->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
2843 event.header.size += sizeof(u64);
2844 event.format[i++] = counter->total_time_enabled;
2845 }
2846
2847 if (counter->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
2848 event.header.size += sizeof(u64);
2849 event.format[i++] = counter->total_time_running;
2850 }
2851
2852 if (counter->attr.read_format & PERF_FORMAT_ID) {
Peter Zijlstra38b200d2009-06-23 20:13:11 +02002853 event.header.size += sizeof(u64);
Peter Zijlstra7f453c22009-07-21 13:19:40 +02002854 event.format[i++] = primary_counter_id(counter);
Peter Zijlstra38b200d2009-06-23 20:13:11 +02002855 }
2856
2857 ret = perf_output_begin(&handle, counter, event.header.size, 0, 0);
2858 if (ret)
2859 return;
2860
2861 perf_output_copy(&handle, &event, event.header.size);
2862 perf_output_end(&handle);
2863}
2864
2865/*
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02002866 * task tracking -- fork/exit
2867 *
2868 * enabled by: attr.comm | attr.mmap | attr.task
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002869 */
2870
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02002871struct perf_task_event {
Peter Zijlstra3a80b4a2009-08-07 19:49:01 +02002872 struct task_struct *task;
2873 struct perf_counter_context *task_ctx;
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002874
2875 struct {
2876 struct perf_event_header header;
2877
2878 u32 pid;
2879 u32 ppid;
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02002880 u32 tid;
2881 u32 ptid;
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002882 } event;
2883};
2884
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02002885static void perf_counter_task_output(struct perf_counter *counter,
2886 struct perf_task_event *task_event)
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002887{
2888 struct perf_output_handle handle;
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02002889 int size = task_event->event.header.size;
2890 struct task_struct *task = task_event->task;
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002891 int ret = perf_output_begin(&handle, counter, size, 0, 0);
2892
2893 if (ret)
2894 return;
2895
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02002896 task_event->event.pid = perf_counter_pid(counter, task);
2897 task_event->event.ppid = perf_counter_pid(counter, task->real_parent);
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002898
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02002899 task_event->event.tid = perf_counter_tid(counter, task);
2900 task_event->event.ptid = perf_counter_tid(counter, task->real_parent);
2901
2902 perf_output_put(&handle, task_event->event);
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002903 perf_output_end(&handle);
2904}
2905
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02002906static int perf_counter_task_match(struct perf_counter *counter)
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002907{
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02002908 if (counter->attr.comm || counter->attr.mmap || counter->attr.task)
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002909 return 1;
2910
2911 return 0;
2912}
2913
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02002914static void perf_counter_task_ctx(struct perf_counter_context *ctx,
2915 struct perf_task_event *task_event)
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002916{
2917 struct perf_counter *counter;
2918
2919 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2920 return;
2921
2922 rcu_read_lock();
2923 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02002924 if (perf_counter_task_match(counter))
2925 perf_counter_task_output(counter, task_event);
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002926 }
2927 rcu_read_unlock();
2928}
2929
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02002930static void perf_counter_task_event(struct perf_task_event *task_event)
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002931{
2932 struct perf_cpu_context *cpuctx;
Peter Zijlstra3a80b4a2009-08-07 19:49:01 +02002933 struct perf_counter_context *ctx = task_event->task_ctx;
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002934
2935 cpuctx = &get_cpu_var(perf_cpu_context);
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02002936 perf_counter_task_ctx(&cpuctx->ctx, task_event);
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002937 put_cpu_var(perf_cpu_context);
2938
2939 rcu_read_lock();
Peter Zijlstra3a80b4a2009-08-07 19:49:01 +02002940 if (!ctx)
2941 ctx = rcu_dereference(task_event->task->perf_counter_ctxp);
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002942 if (ctx)
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02002943 perf_counter_task_ctx(ctx, task_event);
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002944 rcu_read_unlock();
2945}
2946
Peter Zijlstra3a80b4a2009-08-07 19:49:01 +02002947static void perf_counter_task(struct task_struct *task,
2948 struct perf_counter_context *task_ctx,
2949 int new)
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02002950{
2951 struct perf_task_event task_event;
2952
2953 if (!atomic_read(&nr_comm_counters) &&
2954 !atomic_read(&nr_mmap_counters) &&
2955 !atomic_read(&nr_task_counters))
2956 return;
2957
2958 task_event = (struct perf_task_event){
Peter Zijlstra3a80b4a2009-08-07 19:49:01 +02002959 .task = task,
2960 .task_ctx = task_ctx,
2961 .event = {
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02002962 .header = {
2963 .type = new ? PERF_EVENT_FORK : PERF_EVENT_EXIT,
2964 .misc = 0,
2965 .size = sizeof(task_event.event),
2966 },
2967 /* .pid */
2968 /* .ppid */
2969 /* .tid */
2970 /* .ptid */
2971 },
2972 };
2973
2974 perf_counter_task_event(&task_event);
2975}
2976
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002977void perf_counter_fork(struct task_struct *task)
2978{
Peter Zijlstra3a80b4a2009-08-07 19:49:01 +02002979 perf_counter_task(task, NULL, 1);
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002980}
2981
2982/*
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002983 * comm tracking
2984 */
2985
2986struct perf_comm_event {
Ingo Molnar22a4f652009-06-01 10:13:37 +02002987 struct task_struct *task;
2988 char *comm;
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002989 int comm_size;
2990
2991 struct {
2992 struct perf_event_header header;
2993
2994 u32 pid;
2995 u32 tid;
2996 } event;
2997};
2998
2999static void perf_counter_comm_output(struct perf_counter *counter,
3000 struct perf_comm_event *comm_event)
3001{
3002 struct perf_output_handle handle;
3003 int size = comm_event->event.header.size;
3004 int ret = perf_output_begin(&handle, counter, size, 0, 0);
3005
3006 if (ret)
3007 return;
3008
Peter Zijlstra709e50c2009-06-02 14:13:15 +02003009 comm_event->event.pid = perf_counter_pid(counter, comm_event->task);
3010 comm_event->event.tid = perf_counter_tid(counter, comm_event->task);
3011
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02003012 perf_output_put(&handle, comm_event->event);
3013 perf_output_copy(&handle, comm_event->comm,
3014 comm_event->comm_size);
3015 perf_output_end(&handle);
3016}
3017
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003018static int perf_counter_comm_match(struct perf_counter *counter)
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02003019{
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003020 if (counter->attr.comm)
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02003021 return 1;
3022
3023 return 0;
3024}
3025
3026static void perf_counter_comm_ctx(struct perf_counter_context *ctx,
3027 struct perf_comm_event *comm_event)
3028{
3029 struct perf_counter *counter;
3030
3031 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
3032 return;
3033
3034 rcu_read_lock();
3035 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003036 if (perf_counter_comm_match(counter))
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02003037 perf_counter_comm_output(counter, comm_event);
3038 }
3039 rcu_read_unlock();
3040}
3041
3042static void perf_counter_comm_event(struct perf_comm_event *comm_event)
3043{
3044 struct perf_cpu_context *cpuctx;
Peter Zijlstra665c2142009-05-29 14:51:57 +02003045 struct perf_counter_context *ctx;
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02003046 unsigned int size;
Anton Blanchard413ee3b2009-07-16 15:15:52 +02003047 char comm[TASK_COMM_LEN];
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02003048
Anton Blanchard413ee3b2009-07-16 15:15:52 +02003049 memset(comm, 0, sizeof(comm));
3050 strncpy(comm, comm_event->task->comm, sizeof(comm));
Ingo Molnar888fcee2009-04-09 09:48:22 +02003051 size = ALIGN(strlen(comm)+1, sizeof(u64));
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02003052
3053 comm_event->comm = comm;
3054 comm_event->comm_size = size;
3055
3056 comm_event->event.header.size = sizeof(comm_event->event) + size;
3057
3058 cpuctx = &get_cpu_var(perf_cpu_context);
3059 perf_counter_comm_ctx(&cpuctx->ctx, comm_event);
3060 put_cpu_var(perf_cpu_context);
Peter Zijlstra665c2142009-05-29 14:51:57 +02003061
3062 rcu_read_lock();
3063 /*
3064 * doesn't really matter which of the child contexts the
3065 * events ends up in.
3066 */
3067 ctx = rcu_dereference(current->perf_counter_ctxp);
3068 if (ctx)
3069 perf_counter_comm_ctx(ctx, comm_event);
3070 rcu_read_unlock();
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02003071}
3072
3073void perf_counter_comm(struct task_struct *task)
3074{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02003075 struct perf_comm_event comm_event;
3076
Paul Mackerras57e79862009-06-30 16:07:19 +10003077 if (task->perf_counter_ctxp)
3078 perf_counter_enable_on_exec(task);
3079
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003080 if (!atomic_read(&nr_comm_counters))
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02003081 return;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003082
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02003083 comm_event = (struct perf_comm_event){
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02003084 .task = task,
Peter Zijlstra573402d2009-07-22 11:13:50 +02003085 /* .comm */
3086 /* .comm_size */
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02003087 .event = {
Peter Zijlstra573402d2009-07-22 11:13:50 +02003088 .header = {
3089 .type = PERF_EVENT_COMM,
3090 .misc = 0,
3091 /* .size */
3092 },
3093 /* .pid */
3094 /* .tid */
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02003095 },
3096 };
3097
3098 perf_counter_comm_event(&comm_event);
3099}
3100
3101/*
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003102 * mmap tracking
3103 */
3104
3105struct perf_mmap_event {
Peter Zijlstra089dd792009-06-05 14:04:55 +02003106 struct vm_area_struct *vma;
3107
3108 const char *file_name;
3109 int file_size;
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003110
3111 struct {
3112 struct perf_event_header header;
3113
3114 u32 pid;
3115 u32 tid;
3116 u64 start;
3117 u64 len;
3118 u64 pgoff;
3119 } event;
3120};
3121
3122static void perf_counter_mmap_output(struct perf_counter *counter,
3123 struct perf_mmap_event *mmap_event)
3124{
3125 struct perf_output_handle handle;
3126 int size = mmap_event->event.header.size;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02003127 int ret = perf_output_begin(&handle, counter, size, 0, 0);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003128
3129 if (ret)
3130 return;
3131
Peter Zijlstra709e50c2009-06-02 14:13:15 +02003132 mmap_event->event.pid = perf_counter_pid(counter, current);
3133 mmap_event->event.tid = perf_counter_tid(counter, current);
3134
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003135 perf_output_put(&handle, mmap_event->event);
3136 perf_output_copy(&handle, mmap_event->file_name,
3137 mmap_event->file_size);
Peter Zijlstra78d613e2009-03-30 19:07:11 +02003138 perf_output_end(&handle);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003139}
3140
3141static int perf_counter_mmap_match(struct perf_counter *counter,
3142 struct perf_mmap_event *mmap_event)
3143{
Peter Zijlstrad99e9442009-06-04 17:08:58 +02003144 if (counter->attr.mmap)
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003145 return 1;
3146
3147 return 0;
3148}
3149
3150static void perf_counter_mmap_ctx(struct perf_counter_context *ctx,
3151 struct perf_mmap_event *mmap_event)
3152{
3153 struct perf_counter *counter;
3154
3155 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
3156 return;
3157
3158 rcu_read_lock();
3159 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
3160 if (perf_counter_mmap_match(counter, mmap_event))
3161 perf_counter_mmap_output(counter, mmap_event);
3162 }
3163 rcu_read_unlock();
3164}
3165
3166static void perf_counter_mmap_event(struct perf_mmap_event *mmap_event)
3167{
3168 struct perf_cpu_context *cpuctx;
Peter Zijlstra665c2142009-05-29 14:51:57 +02003169 struct perf_counter_context *ctx;
Peter Zijlstra089dd792009-06-05 14:04:55 +02003170 struct vm_area_struct *vma = mmap_event->vma;
3171 struct file *file = vma->vm_file;
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003172 unsigned int size;
3173 char tmp[16];
3174 char *buf = NULL;
Peter Zijlstra089dd792009-06-05 14:04:55 +02003175 const char *name;
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003176
Anton Blanchard413ee3b2009-07-16 15:15:52 +02003177 memset(tmp, 0, sizeof(tmp));
3178
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003179 if (file) {
Anton Blanchard413ee3b2009-07-16 15:15:52 +02003180 /*
3181 * d_path works from the end of the buffer backwards, so we
3182 * need to add enough zero bytes after the string to handle
3183 * the 64bit alignment we do later.
3184 */
3185 buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003186 if (!buf) {
3187 name = strncpy(tmp, "//enomem", sizeof(tmp));
3188 goto got_name;
3189 }
Peter Zijlstrad3d21c42009-04-09 10:53:46 +02003190 name = d_path(&file->f_path, buf, PATH_MAX);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003191 if (IS_ERR(name)) {
3192 name = strncpy(tmp, "//toolong", sizeof(tmp));
3193 goto got_name;
3194 }
3195 } else {
Anton Blanchard413ee3b2009-07-16 15:15:52 +02003196 if (arch_vma_name(mmap_event->vma)) {
3197 name = strncpy(tmp, arch_vma_name(mmap_event->vma),
3198 sizeof(tmp));
Peter Zijlstra089dd792009-06-05 14:04:55 +02003199 goto got_name;
Anton Blanchard413ee3b2009-07-16 15:15:52 +02003200 }
Peter Zijlstra089dd792009-06-05 14:04:55 +02003201
3202 if (!vma->vm_mm) {
3203 name = strncpy(tmp, "[vdso]", sizeof(tmp));
3204 goto got_name;
3205 }
3206
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003207 name = strncpy(tmp, "//anon", sizeof(tmp));
3208 goto got_name;
3209 }
3210
3211got_name:
Ingo Molnar888fcee2009-04-09 09:48:22 +02003212 size = ALIGN(strlen(name)+1, sizeof(u64));
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003213
3214 mmap_event->file_name = name;
3215 mmap_event->file_size = size;
3216
3217 mmap_event->event.header.size = sizeof(mmap_event->event) + size;
3218
3219 cpuctx = &get_cpu_var(perf_cpu_context);
3220 perf_counter_mmap_ctx(&cpuctx->ctx, mmap_event);
3221 put_cpu_var(perf_cpu_context);
3222
Peter Zijlstra665c2142009-05-29 14:51:57 +02003223 rcu_read_lock();
3224 /*
3225 * doesn't really matter which of the child contexts the
3226 * events ends up in.
3227 */
3228 ctx = rcu_dereference(current->perf_counter_ctxp);
3229 if (ctx)
3230 perf_counter_mmap_ctx(ctx, mmap_event);
3231 rcu_read_unlock();
3232
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003233 kfree(buf);
3234}
3235
Peter Zijlstra089dd792009-06-05 14:04:55 +02003236void __perf_counter_mmap(struct vm_area_struct *vma)
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003237{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02003238 struct perf_mmap_event mmap_event;
3239
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003240 if (!atomic_read(&nr_mmap_counters))
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02003241 return;
3242
3243 mmap_event = (struct perf_mmap_event){
Peter Zijlstra089dd792009-06-05 14:04:55 +02003244 .vma = vma,
Peter Zijlstra573402d2009-07-22 11:13:50 +02003245 /* .file_name */
3246 /* .file_size */
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003247 .event = {
Peter Zijlstra573402d2009-07-22 11:13:50 +02003248 .header = {
3249 .type = PERF_EVENT_MMAP,
3250 .misc = 0,
3251 /* .size */
3252 },
3253 /* .pid */
3254 /* .tid */
Peter Zijlstra089dd792009-06-05 14:04:55 +02003255 .start = vma->vm_start,
3256 .len = vma->vm_end - vma->vm_start,
3257 .pgoff = vma->vm_pgoff,
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003258 },
3259 };
3260
3261 perf_counter_mmap_event(&mmap_event);
3262}
3263
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003264/*
Peter Zijlstraa78ac322009-05-25 17:39:05 +02003265 * IRQ throttle logging
3266 */
3267
3268static void perf_log_throttle(struct perf_counter *counter, int enable)
3269{
3270 struct perf_output_handle handle;
3271 int ret;
3272
3273 struct {
3274 struct perf_event_header header;
3275 u64 time;
Peter Zijlstracca3f452009-06-11 14:57:55 +02003276 u64 id;
Peter Zijlstra7f453c22009-07-21 13:19:40 +02003277 u64 stream_id;
Peter Zijlstraa78ac322009-05-25 17:39:05 +02003278 } throttle_event = {
3279 .header = {
Anton Blanchard966ee4d2009-07-22 23:05:46 +10003280 .type = PERF_EVENT_THROTTLE,
Peter Zijlstraa78ac322009-05-25 17:39:05 +02003281 .misc = 0,
3282 .size = sizeof(throttle_event),
3283 },
Peter Zijlstra7f453c22009-07-21 13:19:40 +02003284 .time = sched_clock(),
3285 .id = primary_counter_id(counter),
3286 .stream_id = counter->id,
Peter Zijlstraa78ac322009-05-25 17:39:05 +02003287 };
3288
Anton Blanchard966ee4d2009-07-22 23:05:46 +10003289 if (enable)
3290 throttle_event.header.type = PERF_EVENT_UNTHROTTLE;
3291
Ingo Molnar0127c3e2009-05-25 22:03:26 +02003292 ret = perf_output_begin(&handle, counter, sizeof(throttle_event), 1, 0);
Peter Zijlstraa78ac322009-05-25 17:39:05 +02003293 if (ret)
3294 return;
3295
3296 perf_output_put(&handle, throttle_event);
3297 perf_output_end(&handle);
3298}
3299
3300/*
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01003301 * Generic counter overflow handling, sampling.
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02003302 */
3303
Peter Zijlstradf1a1322009-06-10 21:02:22 +02003304int perf_counter_overflow(struct perf_counter *counter, int nmi,
3305 struct perf_sample_data *data)
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02003306{
Peter Zijlstra79f14642009-04-06 11:45:07 +02003307 int events = atomic_read(&counter->event_limit);
Peter Zijlstraa78ac322009-05-25 17:39:05 +02003308 int throttle = counter->pmu->unthrottle != NULL;
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02003309 struct hw_perf_counter *hwc = &counter->hw;
Peter Zijlstra79f14642009-04-06 11:45:07 +02003310 int ret = 0;
3311
Peter Zijlstraa78ac322009-05-25 17:39:05 +02003312 if (!throttle) {
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02003313 hwc->interrupts++;
Ingo Molnar128f0482009-06-03 22:19:36 +02003314 } else {
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02003315 if (hwc->interrupts != MAX_INTERRUPTS) {
3316 hwc->interrupts++;
Peter Zijlstradf58ab22009-06-11 11:25:05 +02003317 if (HZ * hwc->interrupts >
3318 (u64)sysctl_perf_counter_sample_rate) {
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02003319 hwc->interrupts = MAX_INTERRUPTS;
Ingo Molnar128f0482009-06-03 22:19:36 +02003320 perf_log_throttle(counter, 0);
3321 ret = 1;
3322 }
3323 } else {
3324 /*
3325 * Keep re-disabling counters even though on the previous
3326 * pass we disabled it - just in case we raced with a
3327 * sched-in and the counter got enabled again:
3328 */
Peter Zijlstraa78ac322009-05-25 17:39:05 +02003329 ret = 1;
3330 }
3331 }
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003332
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02003333 if (counter->attr.freq) {
3334 u64 now = sched_clock();
3335 s64 delta = now - hwc->freq_stamp;
3336
3337 hwc->freq_stamp = now;
3338
3339 if (delta > 0 && delta < TICK_NSEC)
3340 perf_adjust_period(counter, NSEC_PER_SEC / (int)delta);
3341 }
3342
Peter Zijlstra2023b352009-05-05 17:50:26 +02003343 /*
3344 * XXX event_limit might not quite work as expected on inherited
3345 * counters
3346 */
3347
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02003348 counter->pending_kill = POLL_IN;
Peter Zijlstra79f14642009-04-06 11:45:07 +02003349 if (events && atomic_dec_and_test(&counter->event_limit)) {
3350 ret = 1;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02003351 counter->pending_kill = POLL_HUP;
Peter Zijlstra79f14642009-04-06 11:45:07 +02003352 if (nmi) {
3353 counter->pending_disable = 1;
3354 perf_pending_queue(&counter->pending,
3355 perf_pending_counter);
3356 } else
3357 perf_counter_disable(counter);
3358 }
3359
Peter Zijlstradf1a1322009-06-10 21:02:22 +02003360 perf_counter_output(counter, nmi, data);
Peter Zijlstra79f14642009-04-06 11:45:07 +02003361 return ret;
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02003362}
3363
3364/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003365 * Generic software counter infrastructure
3366 */
3367
Peter Zijlstra7b4b6652009-07-22 09:29:32 +02003368/*
3369 * We directly increment counter->count and keep a second value in
3370 * counter->hw.period_left to count intervals. This period counter
3371 * is kept in the range [-sample_period, 0] so that we can use the
3372 * sign as trigger.
3373 */
3374
3375static u64 perf_swcounter_set_period(struct perf_counter *counter)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003376{
3377 struct hw_perf_counter *hwc = &counter->hw;
Peter Zijlstra7b4b6652009-07-22 09:29:32 +02003378 u64 period = hwc->last_period;
3379 u64 nr, offset;
3380 s64 old, val;
3381
3382 hwc->last_period = hwc->sample_period;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003383
3384again:
Peter Zijlstra7b4b6652009-07-22 09:29:32 +02003385 old = val = atomic64_read(&hwc->period_left);
3386 if (val < 0)
3387 return 0;
3388
3389 nr = div64_u64(period + val, period);
3390 offset = nr * period;
3391 val -= offset;
3392 if (atomic64_cmpxchg(&hwc->period_left, old, val) != old)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003393 goto again;
3394
Peter Zijlstra7b4b6652009-07-22 09:29:32 +02003395 return nr;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003396}
3397
3398static void perf_swcounter_overflow(struct perf_counter *counter,
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003399 int nmi, struct perf_sample_data *data)
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003400{
Peter Zijlstra7b4b6652009-07-22 09:29:32 +02003401 struct hw_perf_counter *hwc = &counter->hw;
3402 u64 overflow;
Peter Zijlstradf1a1322009-06-10 21:02:22 +02003403
Peter Zijlstra7b4b6652009-07-22 09:29:32 +02003404 data->period = counter->hw.last_period;
3405 overflow = perf_swcounter_set_period(counter);
3406
3407 if (hwc->interrupts == MAX_INTERRUPTS)
3408 return;
3409
3410 for (; overflow; overflow--) {
3411 if (perf_counter_overflow(counter, nmi, data)) {
3412 /*
3413 * We inhibit the overflow from happening when
3414 * hwc->interrupts == MAX_INTERRUPTS.
3415 */
3416 break;
3417 }
3418 }
3419}
3420
3421static void perf_swcounter_unthrottle(struct perf_counter *counter)
3422{
3423 /*
3424 * Nothing to do, we already reset hwc->interrupts.
3425 */
3426}
3427
3428static void perf_swcounter_add(struct perf_counter *counter, u64 nr,
3429 int nmi, struct perf_sample_data *data)
3430{
3431 struct hw_perf_counter *hwc = &counter->hw;
3432
3433 atomic64_add(nr, &counter->count);
3434
3435 if (!hwc->sample_period)
3436 return;
3437
3438 if (!data->regs)
3439 return;
3440
3441 if (!atomic64_add_negative(nr, &hwc->period_left))
3442 perf_swcounter_overflow(counter, nmi, data);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003443}
3444
Paul Mackerras880ca152009-06-01 17:49:14 +10003445static int perf_swcounter_is_counting(struct perf_counter *counter)
3446{
Peter Zijlstrabcfc2602009-08-13 09:51:55 +02003447 /*
3448 * The counter is active, we're good!
3449 */
Paul Mackerras880ca152009-06-01 17:49:14 +10003450 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
3451 return 1;
3452
Peter Zijlstrabcfc2602009-08-13 09:51:55 +02003453 /*
3454 * The counter is off/error, not counting.
3455 */
Paul Mackerras880ca152009-06-01 17:49:14 +10003456 if (counter->state != PERF_COUNTER_STATE_INACTIVE)
3457 return 0;
3458
3459 /*
Peter Zijlstrabcfc2602009-08-13 09:51:55 +02003460 * The counter is inactive, if the context is active
3461 * we're part of a group that didn't make it on the 'pmu',
3462 * not counting.
Paul Mackerras880ca152009-06-01 17:49:14 +10003463 */
Peter Zijlstrabcfc2602009-08-13 09:51:55 +02003464 if (counter->ctx->is_active)
3465 return 0;
3466
3467 /*
3468 * We're inactive and the context is too, this means the
3469 * task is scheduled out, we're counting events that happen
3470 * to us, like migration events.
3471 */
3472 return 1;
Paul Mackerras880ca152009-06-01 17:49:14 +10003473}
3474
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003475static int perf_swcounter_match(struct perf_counter *counter,
Peter Zijlstra1c432d82009-06-11 13:19:29 +02003476 enum perf_type_id type,
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003477 u32 event, struct pt_regs *regs)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003478{
Paul Mackerras880ca152009-06-01 17:49:14 +10003479 if (!perf_swcounter_is_counting(counter))
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003480 return 0;
3481
Ingo Molnara21ca2c2009-06-06 09:58:57 +02003482 if (counter->attr.type != type)
3483 return 0;
3484 if (counter->attr.config != event)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003485 return 0;
3486
Paul Mackerras3f731ca2009-06-01 17:52:30 +10003487 if (regs) {
Peter Zijlstra0d486962009-06-02 19:22:16 +02003488 if (counter->attr.exclude_user && user_mode(regs))
Paul Mackerras3f731ca2009-06-01 17:52:30 +10003489 return 0;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003490
Peter Zijlstra0d486962009-06-02 19:22:16 +02003491 if (counter->attr.exclude_kernel && !user_mode(regs))
Paul Mackerras3f731ca2009-06-01 17:52:30 +10003492 return 0;
3493 }
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003494
3495 return 1;
3496}
3497
3498static void perf_swcounter_ctx_event(struct perf_counter_context *ctx,
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003499 enum perf_type_id type,
3500 u32 event, u64 nr, int nmi,
3501 struct perf_sample_data *data)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003502{
3503 struct perf_counter *counter;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003504
Peter Zijlstra01ef09d2009-03-19 20:26:11 +01003505 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003506 return;
3507
Peter Zijlstra592903c2009-03-13 12:21:36 +01003508 rcu_read_lock();
3509 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003510 if (perf_swcounter_match(counter, type, event, data->regs))
3511 perf_swcounter_add(counter, nr, nmi, data);
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003512 }
Peter Zijlstra592903c2009-03-13 12:21:36 +01003513 rcu_read_unlock();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003514}
3515
Peter Zijlstra96f6d442009-03-23 18:22:07 +01003516static int *perf_swcounter_recursion_context(struct perf_cpu_context *cpuctx)
3517{
3518 if (in_nmi())
3519 return &cpuctx->recursion[3];
3520
3521 if (in_irq())
3522 return &cpuctx->recursion[2];
3523
3524 if (in_softirq())
3525 return &cpuctx->recursion[1];
3526
3527 return &cpuctx->recursion[0];
3528}
3529
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003530static void do_perf_swcounter_event(enum perf_type_id type, u32 event,
3531 u64 nr, int nmi,
3532 struct perf_sample_data *data)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003533{
3534 struct perf_cpu_context *cpuctx = &get_cpu_var(perf_cpu_context);
Peter Zijlstra96f6d442009-03-23 18:22:07 +01003535 int *recursion = perf_swcounter_recursion_context(cpuctx);
Peter Zijlstra665c2142009-05-29 14:51:57 +02003536 struct perf_counter_context *ctx;
Peter Zijlstra96f6d442009-03-23 18:22:07 +01003537
3538 if (*recursion)
3539 goto out;
3540
3541 (*recursion)++;
3542 barrier();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003543
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003544 perf_swcounter_ctx_event(&cpuctx->ctx, type, event,
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003545 nr, nmi, data);
Peter Zijlstra665c2142009-05-29 14:51:57 +02003546 rcu_read_lock();
3547 /*
3548 * doesn't really matter which of the child contexts the
3549 * events ends up in.
3550 */
3551 ctx = rcu_dereference(current->perf_counter_ctxp);
3552 if (ctx)
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003553 perf_swcounter_ctx_event(ctx, type, event, nr, nmi, data);
Peter Zijlstra665c2142009-05-29 14:51:57 +02003554 rcu_read_unlock();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003555
Peter Zijlstra96f6d442009-03-23 18:22:07 +01003556 barrier();
3557 (*recursion)--;
3558
3559out:
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003560 put_cpu_var(perf_cpu_context);
3561}
3562
Peter Zijlstraf29ac752009-06-19 18:27:26 +02003563void __perf_swcounter_event(u32 event, u64 nr, int nmi,
3564 struct pt_regs *regs, u64 addr)
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003565{
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003566 struct perf_sample_data data = {
3567 .regs = regs,
3568 .addr = addr,
3569 };
3570
3571 do_perf_swcounter_event(PERF_TYPE_SOFTWARE, event, nr, nmi, &data);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003572}
3573
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003574static void perf_swcounter_read(struct perf_counter *counter)
3575{
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003576}
3577
3578static int perf_swcounter_enable(struct perf_counter *counter)
3579{
Peter Zijlstra7b4b6652009-07-22 09:29:32 +02003580 struct hw_perf_counter *hwc = &counter->hw;
3581
3582 if (hwc->sample_period) {
3583 hwc->last_period = hwc->sample_period;
3584 perf_swcounter_set_period(counter);
3585 }
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003586 return 0;
3587}
3588
3589static void perf_swcounter_disable(struct perf_counter *counter)
3590{
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003591}
3592
Robert Richter4aeb0b42009-04-29 12:47:03 +02003593static const struct pmu perf_ops_generic = {
Peter Zijlstraac17dc82009-03-13 12:21:34 +01003594 .enable = perf_swcounter_enable,
3595 .disable = perf_swcounter_disable,
3596 .read = perf_swcounter_read,
Peter Zijlstra7b4b6652009-07-22 09:29:32 +02003597 .unthrottle = perf_swcounter_unthrottle,
Peter Zijlstraac17dc82009-03-13 12:21:34 +01003598};
3599
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003600/*
Peter Zijlstra7b4b6652009-07-22 09:29:32 +02003601 * hrtimer based swcounter callback
3602 */
3603
3604static enum hrtimer_restart perf_swcounter_hrtimer(struct hrtimer *hrtimer)
3605{
3606 enum hrtimer_restart ret = HRTIMER_RESTART;
3607 struct perf_sample_data data;
3608 struct perf_counter *counter;
3609 u64 period;
3610
3611 counter = container_of(hrtimer, struct perf_counter, hw.hrtimer);
3612 counter->pmu->read(counter);
3613
3614 data.addr = 0;
3615 data.regs = get_irq_regs();
3616 /*
3617 * In case we exclude kernel IPs or are somehow not in interrupt
3618 * context, provide the next best thing, the user IP.
3619 */
3620 if ((counter->attr.exclude_kernel || !data.regs) &&
3621 !counter->attr.exclude_user)
3622 data.regs = task_pt_regs(current);
3623
3624 if (data.regs) {
3625 if (perf_counter_overflow(counter, 0, &data))
3626 ret = HRTIMER_NORESTART;
3627 }
3628
3629 period = max_t(u64, 10000, counter->hw.sample_period);
3630 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
3631
3632 return ret;
3633}
3634
3635/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003636 * Software counter: cpu wall time clock
3637 */
3638
Paul Mackerras9abf8a02009-01-09 16:26:43 +11003639static void cpu_clock_perf_counter_update(struct perf_counter *counter)
3640{
3641 int cpu = raw_smp_processor_id();
3642 s64 prev;
3643 u64 now;
3644
3645 now = cpu_clock(cpu);
3646 prev = atomic64_read(&counter->hw.prev_count);
3647 atomic64_set(&counter->hw.prev_count, now);
3648 atomic64_add(now - prev, &counter->count);
3649}
3650
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003651static int cpu_clock_perf_counter_enable(struct perf_counter *counter)
3652{
3653 struct hw_perf_counter *hwc = &counter->hw;
3654 int cpu = raw_smp_processor_id();
3655
3656 atomic64_set(&hwc->prev_count, cpu_clock(cpu));
Peter Zijlstra039fc912009-03-13 16:43:47 +01003657 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
3658 hwc->hrtimer.function = perf_swcounter_hrtimer;
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003659 if (hwc->sample_period) {
3660 u64 period = max_t(u64, 10000, hwc->sample_period);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003661 __hrtimer_start_range_ns(&hwc->hrtimer,
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003662 ns_to_ktime(period), 0,
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003663 HRTIMER_MODE_REL, 0);
3664 }
3665
3666 return 0;
3667}
3668
Ingo Molnar5c92d122008-12-11 13:21:10 +01003669static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
3670{
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003671 if (counter->hw.sample_period)
Peter Zijlstrab986d7e2009-05-20 12:21:21 +02003672 hrtimer_cancel(&counter->hw.hrtimer);
Paul Mackerras9abf8a02009-01-09 16:26:43 +11003673 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01003674}
3675
3676static void cpu_clock_perf_counter_read(struct perf_counter *counter)
3677{
Paul Mackerras9abf8a02009-01-09 16:26:43 +11003678 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01003679}
3680
Robert Richter4aeb0b42009-04-29 12:47:03 +02003681static const struct pmu perf_ops_cpu_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +01003682 .enable = cpu_clock_perf_counter_enable,
3683 .disable = cpu_clock_perf_counter_disable,
3684 .read = cpu_clock_perf_counter_read,
Ingo Molnar5c92d122008-12-11 13:21:10 +01003685};
3686
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01003687/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003688 * Software counter: task time clock
3689 */
3690
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02003691static void task_clock_perf_counter_update(struct perf_counter *counter, u64 now)
Ingo Molnarbae43c92008-12-11 14:03:20 +01003692{
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02003693 u64 prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003694 s64 delta;
Ingo Molnarbae43c92008-12-11 14:03:20 +01003695
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02003696 prev = atomic64_xchg(&counter->hw.prev_count, now);
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003697 delta = now - prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003698 atomic64_add(delta, &counter->count);
Ingo Molnarbae43c92008-12-11 14:03:20 +01003699}
3700
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01003701static int task_clock_perf_counter_enable(struct perf_counter *counter)
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003702{
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003703 struct hw_perf_counter *hwc = &counter->hw;
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02003704 u64 now;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003705
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02003706 now = counter->ctx->time;
3707
3708 atomic64_set(&hwc->prev_count, now);
Peter Zijlstra039fc912009-03-13 16:43:47 +01003709 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
3710 hwc->hrtimer.function = perf_swcounter_hrtimer;
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003711 if (hwc->sample_period) {
3712 u64 period = max_t(u64, 10000, hwc->sample_period);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003713 __hrtimer_start_range_ns(&hwc->hrtimer,
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003714 ns_to_ktime(period), 0,
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003715 HRTIMER_MODE_REL, 0);
3716 }
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01003717
3718 return 0;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003719}
3720
3721static void task_clock_perf_counter_disable(struct perf_counter *counter)
3722{
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003723 if (counter->hw.sample_period)
Peter Zijlstrab986d7e2009-05-20 12:21:21 +02003724 hrtimer_cancel(&counter->hw.hrtimer);
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02003725 task_clock_perf_counter_update(counter, counter->ctx->time);
3726
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003727}
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01003728
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003729static void task_clock_perf_counter_read(struct perf_counter *counter)
3730{
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02003731 u64 time;
3732
3733 if (!in_nmi()) {
3734 update_context_time(counter->ctx);
3735 time = counter->ctx->time;
3736 } else {
3737 u64 now = perf_clock();
3738 u64 delta = now - counter->ctx->timestamp;
3739 time = counter->ctx->time + delta;
3740 }
3741
3742 task_clock_perf_counter_update(counter, time);
Ingo Molnarbae43c92008-12-11 14:03:20 +01003743}
3744
Robert Richter4aeb0b42009-04-29 12:47:03 +02003745static const struct pmu perf_ops_task_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +01003746 .enable = task_clock_perf_counter_enable,
3747 .disable = task_clock_perf_counter_disable,
3748 .read = task_clock_perf_counter_read,
Ingo Molnarbae43c92008-12-11 14:03:20 +01003749};
3750
Peter Zijlstrae077df42009-03-19 20:26:17 +01003751#ifdef CONFIG_EVENT_PROFILE
Frederic Weisbeckerf413cdb2009-08-07 01:25:54 +02003752void perf_tpcounter_event(int event_id, u64 addr, u64 count, void *record,
3753 int entry_size)
Peter Zijlstrae077df42009-03-19 20:26:17 +01003754{
Frederic Weisbecker3a43ce62009-08-08 04:26:37 +02003755 struct perf_raw_record raw = {
Frederic Weisbeckerf413cdb2009-08-07 01:25:54 +02003756 .size = entry_size,
Frederic Weisbecker3a43ce62009-08-08 04:26:37 +02003757 .data = record,
Frederic Weisbeckerf413cdb2009-08-07 01:25:54 +02003758 };
3759
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003760 struct perf_sample_data data = {
Chris Wilsond4d7d0b2009-07-06 09:31:33 +01003761 .regs = get_irq_regs(),
Peter Zijlstra3a659302009-07-21 17:34:57 +02003762 .addr = addr,
Frederic Weisbecker3a43ce62009-08-08 04:26:37 +02003763 .raw = &raw,
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003764 };
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003765
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003766 if (!data.regs)
3767 data.regs = task_pt_regs(current);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003768
Peter Zijlstra3a659302009-07-21 17:34:57 +02003769 do_perf_swcounter_event(PERF_TYPE_TRACEPOINT, event_id, count, 1, &data);
Peter Zijlstrae077df42009-03-19 20:26:17 +01003770}
Steven Whitehouseff7b1b42009-04-15 16:55:05 +01003771EXPORT_SYMBOL_GPL(perf_tpcounter_event);
Peter Zijlstrae077df42009-03-19 20:26:17 +01003772
3773extern int ftrace_profile_enable(int);
3774extern void ftrace_profile_disable(int);
3775
3776static void tp_perf_counter_destroy(struct perf_counter *counter)
3777{
Chris Wilsond4d7d0b2009-07-06 09:31:33 +01003778 ftrace_profile_disable(counter->attr.config);
Peter Zijlstrae077df42009-03-19 20:26:17 +01003779}
3780
Robert Richter4aeb0b42009-04-29 12:47:03 +02003781static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
Peter Zijlstrae077df42009-03-19 20:26:17 +01003782{
Peter Zijlstraa4e95fc2009-08-10 11:20:12 +02003783 /*
3784 * Raw tracepoint data is a severe data leak, only allow root to
3785 * have these.
3786 */
3787 if ((counter->attr.sample_type & PERF_SAMPLE_RAW) &&
3788 !capable(CAP_SYS_ADMIN))
3789 return ERR_PTR(-EPERM);
3790
Chris Wilsond4d7d0b2009-07-06 09:31:33 +01003791 if (ftrace_profile_enable(counter->attr.config))
Peter Zijlstrae077df42009-03-19 20:26:17 +01003792 return NULL;
3793
3794 counter->destroy = tp_perf_counter_destroy;
3795
3796 return &perf_ops_generic;
3797}
3798#else
Robert Richter4aeb0b42009-04-29 12:47:03 +02003799static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
Peter Zijlstrae077df42009-03-19 20:26:17 +01003800{
3801 return NULL;
3802}
3803#endif
3804
Peter Zijlstraf29ac752009-06-19 18:27:26 +02003805atomic_t perf_swcounter_enabled[PERF_COUNT_SW_MAX];
3806
3807static void sw_perf_counter_destroy(struct perf_counter *counter)
3808{
3809 u64 event = counter->attr.config;
3810
Peter Zijlstraf3440112009-06-22 13:58:35 +02003811 WARN_ON(counter->parent);
3812
Peter Zijlstraf29ac752009-06-19 18:27:26 +02003813 atomic_dec(&perf_swcounter_enabled[event]);
3814}
3815
Robert Richter4aeb0b42009-04-29 12:47:03 +02003816static const struct pmu *sw_perf_counter_init(struct perf_counter *counter)
Ingo Molnar5c92d122008-12-11 13:21:10 +01003817{
Robert Richter4aeb0b42009-04-29 12:47:03 +02003818 const struct pmu *pmu = NULL;
Peter Zijlstraf29ac752009-06-19 18:27:26 +02003819 u64 event = counter->attr.config;
Ingo Molnar5c92d122008-12-11 13:21:10 +01003820
Paul Mackerras0475f9e2009-02-11 14:35:35 +11003821 /*
3822 * Software counters (currently) can't in general distinguish
3823 * between user, kernel and hypervisor events.
3824 * However, context switches and cpu migrations are considered
3825 * to be kernel events, and page faults are never hypervisor
3826 * events.
3827 */
Peter Zijlstraf29ac752009-06-19 18:27:26 +02003828 switch (event) {
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +02003829 case PERF_COUNT_SW_CPU_CLOCK:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003830 pmu = &perf_ops_cpu_clock;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003831
Ingo Molnar5c92d122008-12-11 13:21:10 +01003832 break;
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +02003833 case PERF_COUNT_SW_TASK_CLOCK:
Paul Mackerras23a185c2009-02-09 22:42:47 +11003834 /*
3835 * If the user instantiates this as a per-cpu counter,
3836 * use the cpu_clock counter instead.
3837 */
3838 if (counter->ctx->task)
Robert Richter4aeb0b42009-04-29 12:47:03 +02003839 pmu = &perf_ops_task_clock;
Paul Mackerras23a185c2009-02-09 22:42:47 +11003840 else
Robert Richter4aeb0b42009-04-29 12:47:03 +02003841 pmu = &perf_ops_cpu_clock;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003842
Ingo Molnarbae43c92008-12-11 14:03:20 +01003843 break;
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +02003844 case PERF_COUNT_SW_PAGE_FAULTS:
3845 case PERF_COUNT_SW_PAGE_FAULTS_MIN:
3846 case PERF_COUNT_SW_PAGE_FAULTS_MAJ:
3847 case PERF_COUNT_SW_CONTEXT_SWITCHES:
3848 case PERF_COUNT_SW_CPU_MIGRATIONS:
Peter Zijlstraf3440112009-06-22 13:58:35 +02003849 if (!counter->parent) {
3850 atomic_inc(&perf_swcounter_enabled[event]);
3851 counter->destroy = sw_perf_counter_destroy;
3852 }
Paul Mackerras3f731ca2009-06-01 17:52:30 +10003853 pmu = &perf_ops_generic;
Ingo Molnar6c594c22008-12-14 12:34:15 +01003854 break;
Ingo Molnar5c92d122008-12-11 13:21:10 +01003855 }
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003856
Robert Richter4aeb0b42009-04-29 12:47:03 +02003857 return pmu;
Ingo Molnar5c92d122008-12-11 13:21:10 +01003858}
3859
Thomas Gleixner0793a612008-12-04 20:12:29 +01003860/*
3861 * Allocate and initialize a counter structure
3862 */
3863static struct perf_counter *
Peter Zijlstra0d486962009-06-02 19:22:16 +02003864perf_counter_alloc(struct perf_counter_attr *attr,
Ingo Molnar04289bb2008-12-11 08:38:42 +01003865 int cpu,
Paul Mackerras23a185c2009-02-09 22:42:47 +11003866 struct perf_counter_context *ctx,
Ingo Molnar9b51f662008-12-12 13:49:45 +01003867 struct perf_counter *group_leader,
Peter Zijlstrab84fbc92009-06-22 13:57:40 +02003868 struct perf_counter *parent_counter,
Ingo Molnar9b51f662008-12-12 13:49:45 +01003869 gfp_t gfpflags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003870{
Robert Richter4aeb0b42009-04-29 12:47:03 +02003871 const struct pmu *pmu;
Ingo Molnar621a01e2008-12-11 12:46:46 +01003872 struct perf_counter *counter;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003873 struct hw_perf_counter *hwc;
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003874 long err;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003875
Ingo Molnar9b51f662008-12-12 13:49:45 +01003876 counter = kzalloc(sizeof(*counter), gfpflags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003877 if (!counter)
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003878 return ERR_PTR(-ENOMEM);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003879
Ingo Molnar04289bb2008-12-11 08:38:42 +01003880 /*
3881 * Single counters are their own group leaders, with an
3882 * empty sibling list:
3883 */
3884 if (!group_leader)
3885 group_leader = counter;
3886
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003887 mutex_init(&counter->child_mutex);
3888 INIT_LIST_HEAD(&counter->child_list);
3889
Ingo Molnar04289bb2008-12-11 08:38:42 +01003890 INIT_LIST_HEAD(&counter->list_entry);
Peter Zijlstra592903c2009-03-13 12:21:36 +01003891 INIT_LIST_HEAD(&counter->event_entry);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003892 INIT_LIST_HEAD(&counter->sibling_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003893 init_waitqueue_head(&counter->waitq);
3894
Peter Zijlstra7b732a72009-03-23 18:22:10 +01003895 mutex_init(&counter->mmap_mutex);
3896
Peter Zijlstraa96bbc12009-06-03 14:01:36 +02003897 counter->cpu = cpu;
Peter Zijlstra0d486962009-06-02 19:22:16 +02003898 counter->attr = *attr;
Peter Zijlstraa96bbc12009-06-03 14:01:36 +02003899 counter->group_leader = group_leader;
3900 counter->pmu = NULL;
3901 counter->ctx = ctx;
3902 counter->oncpu = -1;
Ingo Molnar329d8762009-05-26 08:10:00 +02003903
Peter Zijlstrab84fbc92009-06-22 13:57:40 +02003904 counter->parent = parent_counter;
3905
Peter Zijlstraa96bbc12009-06-03 14:01:36 +02003906 counter->ns = get_pid_ns(current->nsproxy->pid_ns);
3907 counter->id = atomic64_inc_return(&perf_counter_id);
3908
3909 counter->state = PERF_COUNTER_STATE_INACTIVE;
3910
Peter Zijlstra0d486962009-06-02 19:22:16 +02003911 if (attr->disabled)
Ingo Molnara86ed502008-12-17 00:43:10 +01003912 counter->state = PERF_COUNTER_STATE_OFF;
3913
Robert Richter4aeb0b42009-04-29 12:47:03 +02003914 pmu = NULL;
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003915
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003916 hwc = &counter->hw;
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02003917 hwc->sample_period = attr->sample_period;
Peter Zijlstra0d486962009-06-02 19:22:16 +02003918 if (attr->freq && attr->sample_freq)
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02003919 hwc->sample_period = 1;
3920
3921 atomic64_set(&hwc->period_left, hwc->sample_period);
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003922
Peter Zijlstra2023b352009-05-05 17:50:26 +02003923 /*
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003924 * we currently do not support PERF_SAMPLE_GROUP on inherited counters
Peter Zijlstra2023b352009-05-05 17:50:26 +02003925 */
Peter Zijlstra0d486962009-06-02 19:22:16 +02003926 if (attr->inherit && (attr->sample_type & PERF_SAMPLE_GROUP))
Peter Zijlstra2023b352009-05-05 17:50:26 +02003927 goto done;
3928
Ingo Molnara21ca2c2009-06-06 09:58:57 +02003929 switch (attr->type) {
Peter Zijlstra081fad82009-06-11 17:57:21 +02003930 case PERF_TYPE_RAW:
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003931 case PERF_TYPE_HARDWARE:
Ingo Molnar8326f442009-06-05 20:22:46 +02003932 case PERF_TYPE_HW_CACHE:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003933 pmu = hw_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003934 break;
3935
3936 case PERF_TYPE_SOFTWARE:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003937 pmu = sw_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003938 break;
3939
3940 case PERF_TYPE_TRACEPOINT:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003941 pmu = tp_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003942 break;
Peter Zijlstra974802e2009-06-12 12:46:55 +02003943
3944 default:
3945 break;
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003946 }
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01003947done:
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003948 err = 0;
Robert Richter4aeb0b42009-04-29 12:47:03 +02003949 if (!pmu)
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003950 err = -EINVAL;
Robert Richter4aeb0b42009-04-29 12:47:03 +02003951 else if (IS_ERR(pmu))
3952 err = PTR_ERR(pmu);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003953
3954 if (err) {
Peter Zijlstraa96bbc12009-06-03 14:01:36 +02003955 if (counter->ns)
3956 put_pid_ns(counter->ns);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003957 kfree(counter);
3958 return ERR_PTR(err);
3959 }
3960
Robert Richter4aeb0b42009-04-29 12:47:03 +02003961 counter->pmu = pmu;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003962
Peter Zijlstraf3440112009-06-22 13:58:35 +02003963 if (!counter->parent) {
3964 atomic_inc(&nr_counters);
3965 if (counter->attr.mmap)
3966 atomic_inc(&nr_mmap_counters);
3967 if (counter->attr.comm)
3968 atomic_inc(&nr_comm_counters);
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02003969 if (counter->attr.task)
3970 atomic_inc(&nr_task_counters);
Peter Zijlstraf3440112009-06-22 13:58:35 +02003971 }
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02003972
Thomas Gleixner0793a612008-12-04 20:12:29 +01003973 return counter;
3974}
3975
Peter Zijlstra974802e2009-06-12 12:46:55 +02003976static int perf_copy_attr(struct perf_counter_attr __user *uattr,
3977 struct perf_counter_attr *attr)
3978{
3979 int ret;
3980 u32 size;
3981
3982 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
3983 return -EFAULT;
3984
3985 /*
3986 * zero the full structure, so that a short copy will be nice.
3987 */
3988 memset(attr, 0, sizeof(*attr));
3989
3990 ret = get_user(size, &uattr->size);
3991 if (ret)
3992 return ret;
3993
3994 if (size > PAGE_SIZE) /* silly large */
3995 goto err_size;
3996
3997 if (!size) /* abi compat */
3998 size = PERF_ATTR_SIZE_VER0;
3999
4000 if (size < PERF_ATTR_SIZE_VER0)
4001 goto err_size;
4002
4003 /*
4004 * If we're handed a bigger struct than we know of,
4005 * ensure all the unknown bits are 0.
4006 */
4007 if (size > sizeof(*attr)) {
4008 unsigned long val;
4009 unsigned long __user *addr;
4010 unsigned long __user *end;
4011
4012 addr = PTR_ALIGN((void __user *)uattr + sizeof(*attr),
4013 sizeof(unsigned long));
4014 end = PTR_ALIGN((void __user *)uattr + size,
4015 sizeof(unsigned long));
4016
4017 for (; addr < end; addr += sizeof(unsigned long)) {
4018 ret = get_user(val, addr);
4019 if (ret)
4020 return ret;
4021 if (val)
4022 goto err_size;
4023 }
4024 }
4025
4026 ret = copy_from_user(attr, uattr, size);
4027 if (ret)
4028 return -EFAULT;
4029
4030 /*
4031 * If the type exists, the corresponding creation will verify
4032 * the attr->config.
4033 */
4034 if (attr->type >= PERF_TYPE_MAX)
4035 return -EINVAL;
4036
4037 if (attr->__reserved_1 || attr->__reserved_2 || attr->__reserved_3)
4038 return -EINVAL;
4039
4040 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
4041 return -EINVAL;
4042
4043 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
4044 return -EINVAL;
4045
4046out:
4047 return ret;
4048
4049err_size:
4050 put_user(sizeof(*attr), &uattr->size);
4051 ret = -E2BIG;
4052 goto out;
4053}
4054
Thomas Gleixner0793a612008-12-04 20:12:29 +01004055/**
Paul Mackerras2743a5b2009-03-04 20:36:51 +11004056 * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
Ingo Molnar9f66a382008-12-10 12:33:23 +01004057 *
Peter Zijlstra0d486962009-06-02 19:22:16 +02004058 * @attr_uptr: event type attributes for monitoring/sampling
Thomas Gleixner0793a612008-12-04 20:12:29 +01004059 * @pid: target pid
Ingo Molnar9f66a382008-12-10 12:33:23 +01004060 * @cpu: target cpu
4061 * @group_fd: group leader counter fd
Thomas Gleixner0793a612008-12-04 20:12:29 +01004062 */
Paul Mackerras2743a5b2009-03-04 20:36:51 +11004063SYSCALL_DEFINE5(perf_counter_open,
Peter Zijlstra974802e2009-06-12 12:46:55 +02004064 struct perf_counter_attr __user *, attr_uptr,
Paul Mackerras2743a5b2009-03-04 20:36:51 +11004065 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01004066{
Ingo Molnar04289bb2008-12-11 08:38:42 +01004067 struct perf_counter *counter, *group_leader;
Peter Zijlstra0d486962009-06-02 19:22:16 +02004068 struct perf_counter_attr attr;
Ingo Molnar04289bb2008-12-11 08:38:42 +01004069 struct perf_counter_context *ctx;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004070 struct file *counter_file = NULL;
Ingo Molnar04289bb2008-12-11 08:38:42 +01004071 struct file *group_file = NULL;
4072 int fput_needed = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004073 int fput_needed2 = 0;
Thomas Gleixner0793a612008-12-04 20:12:29 +01004074 int ret;
4075
Paul Mackerras2743a5b2009-03-04 20:36:51 +11004076 /* for future expandability... */
4077 if (flags)
4078 return -EINVAL;
4079
Peter Zijlstra974802e2009-06-12 12:46:55 +02004080 ret = perf_copy_attr(attr_uptr, &attr);
4081 if (ret)
4082 return ret;
Thomas Gleixnereab656a2008-12-08 19:26:59 +01004083
Peter Zijlstra07647712009-06-11 11:18:36 +02004084 if (!attr.exclude_kernel) {
4085 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
4086 return -EACCES;
4087 }
4088
Peter Zijlstradf58ab22009-06-11 11:25:05 +02004089 if (attr.freq) {
4090 if (attr.sample_freq > sysctl_perf_counter_sample_rate)
4091 return -EINVAL;
4092 }
4093
Ingo Molnar04289bb2008-12-11 08:38:42 +01004094 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01004095 * Get the target context (task or percpu):
4096 */
4097 ctx = find_get_context(pid, cpu);
4098 if (IS_ERR(ctx))
4099 return PTR_ERR(ctx);
4100
4101 /*
4102 * Look up the group leader (we will attach this counter to it):
Ingo Molnar04289bb2008-12-11 08:38:42 +01004103 */
4104 group_leader = NULL;
4105 if (group_fd != -1) {
4106 ret = -EINVAL;
4107 group_file = fget_light(group_fd, &fput_needed);
4108 if (!group_file)
Ingo Molnarccff2862008-12-11 11:26:29 +01004109 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01004110 if (group_file->f_op != &perf_fops)
Ingo Molnarccff2862008-12-11 11:26:29 +01004111 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01004112
4113 group_leader = group_file->private_data;
4114 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01004115 * Do not allow a recursive hierarchy (this new sibling
4116 * becoming part of another group-sibling):
Ingo Molnar04289bb2008-12-11 08:38:42 +01004117 */
Ingo Molnarccff2862008-12-11 11:26:29 +01004118 if (group_leader->group_leader != group_leader)
4119 goto err_put_context;
4120 /*
4121 * Do not allow to attach to a group in a different
4122 * task or CPU context:
4123 */
4124 if (group_leader->ctx != ctx)
4125 goto err_put_context;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11004126 /*
4127 * Only a group leader can be exclusive or pinned
4128 */
Peter Zijlstra0d486962009-06-02 19:22:16 +02004129 if (attr.exclusive || attr.pinned)
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11004130 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01004131 }
4132
Peter Zijlstra0d486962009-06-02 19:22:16 +02004133 counter = perf_counter_alloc(&attr, cpu, ctx, group_leader,
Peter Zijlstrab84fbc92009-06-22 13:57:40 +02004134 NULL, GFP_KERNEL);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02004135 ret = PTR_ERR(counter);
4136 if (IS_ERR(counter))
Thomas Gleixner0793a612008-12-04 20:12:29 +01004137 goto err_put_context;
4138
Thomas Gleixner0793a612008-12-04 20:12:29 +01004139 ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
4140 if (ret < 0)
Ingo Molnar9b51f662008-12-12 13:49:45 +01004141 goto err_free_put_context;
4142
4143 counter_file = fget_light(ret, &fput_needed2);
4144 if (!counter_file)
4145 goto err_free_put_context;
4146
4147 counter->filp = counter_file;
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004148 WARN_ON_ONCE(ctx->parent_ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +11004149 mutex_lock(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01004150 perf_install_in_context(ctx, counter, cpu);
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004151 ++ctx->generation;
Paul Mackerrasd859e292009-01-17 18:10:22 +11004152 mutex_unlock(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01004153
Peter Zijlstra082ff5a2009-05-23 18:29:00 +02004154 counter->owner = current;
4155 get_task_struct(current);
4156 mutex_lock(&current->perf_counter_mutex);
4157 list_add_tail(&counter->owner_entry, &current->perf_counter_list);
4158 mutex_unlock(&current->perf_counter_mutex);
4159
Ingo Molnar9b51f662008-12-12 13:49:45 +01004160 fput_light(counter_file, fput_needed2);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004161
Ingo Molnar04289bb2008-12-11 08:38:42 +01004162out_fput:
4163 fput_light(group_file, fput_needed);
4164
Thomas Gleixner0793a612008-12-04 20:12:29 +01004165 return ret;
4166
Ingo Molnar9b51f662008-12-12 13:49:45 +01004167err_free_put_context:
Thomas Gleixner0793a612008-12-04 20:12:29 +01004168 kfree(counter);
4169
4170err_put_context:
Paul Mackerrasc93f7662009-05-28 22:18:17 +10004171 put_ctx(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004172
Ingo Molnar04289bb2008-12-11 08:38:42 +01004173 goto out_fput;
Thomas Gleixner0793a612008-12-04 20:12:29 +01004174}
4175
Ingo Molnar9b51f662008-12-12 13:49:45 +01004176/*
Ingo Molnar9b51f662008-12-12 13:49:45 +01004177 * inherit a counter from parent task to child task:
4178 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11004179static struct perf_counter *
Ingo Molnar9b51f662008-12-12 13:49:45 +01004180inherit_counter(struct perf_counter *parent_counter,
4181 struct task_struct *parent,
4182 struct perf_counter_context *parent_ctx,
4183 struct task_struct *child,
Paul Mackerrasd859e292009-01-17 18:10:22 +11004184 struct perf_counter *group_leader,
Ingo Molnar9b51f662008-12-12 13:49:45 +01004185 struct perf_counter_context *child_ctx)
4186{
4187 struct perf_counter *child_counter;
4188
Paul Mackerrasd859e292009-01-17 18:10:22 +11004189 /*
4190 * Instead of creating recursive hierarchies of counters,
4191 * we link inherited counters back to the original parent,
4192 * which has a filp for sure, which we use as the reference
4193 * count:
4194 */
4195 if (parent_counter->parent)
4196 parent_counter = parent_counter->parent;
4197
Peter Zijlstra0d486962009-06-02 19:22:16 +02004198 child_counter = perf_counter_alloc(&parent_counter->attr,
Paul Mackerras23a185c2009-02-09 22:42:47 +11004199 parent_counter->cpu, child_ctx,
Peter Zijlstrab84fbc92009-06-22 13:57:40 +02004200 group_leader, parent_counter,
4201 GFP_KERNEL);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02004202 if (IS_ERR(child_counter))
4203 return child_counter;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10004204 get_ctx(child_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01004205
4206 /*
Paul Mackerras564c2b22009-05-22 14:27:22 +10004207 * Make the child state follow the state of the parent counter,
Peter Zijlstra0d486962009-06-02 19:22:16 +02004208 * not its attr.disabled bit. We hold the parent's mutex,
Ingo Molnar22a4f652009-06-01 10:13:37 +02004209 * so we won't race with perf_counter_{en, dis}able_family.
Paul Mackerras564c2b22009-05-22 14:27:22 +10004210 */
4211 if (parent_counter->state >= PERF_COUNTER_STATE_INACTIVE)
4212 child_counter->state = PERF_COUNTER_STATE_INACTIVE;
4213 else
4214 child_counter->state = PERF_COUNTER_STATE_OFF;
4215
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02004216 if (parent_counter->attr.freq)
4217 child_counter->hw.sample_period = parent_counter->hw.sample_period;
4218
Paul Mackerras564c2b22009-05-22 14:27:22 +10004219 /*
Ingo Molnar9b51f662008-12-12 13:49:45 +01004220 * Link it up in the child's context:
4221 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +11004222 add_counter_to_ctx(child_counter, child_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01004223
Ingo Molnar9b51f662008-12-12 13:49:45 +01004224 /*
4225 * Get a reference to the parent filp - we will fput it
4226 * when the child counter exits. This is safe to do because
4227 * we are in the parent and we know that the filp still
4228 * exists and has a nonzero count:
4229 */
4230 atomic_long_inc(&parent_counter->filp->f_count);
4231
Paul Mackerrasd859e292009-01-17 18:10:22 +11004232 /*
4233 * Link this into the parent counter's child list
4234 */
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004235 WARN_ON_ONCE(parent_counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02004236 mutex_lock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11004237 list_add_tail(&child_counter->child_list, &parent_counter->child_list);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02004238 mutex_unlock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11004239
4240 return child_counter;
4241}
4242
4243static int inherit_group(struct perf_counter *parent_counter,
4244 struct task_struct *parent,
4245 struct perf_counter_context *parent_ctx,
4246 struct task_struct *child,
4247 struct perf_counter_context *child_ctx)
4248{
4249 struct perf_counter *leader;
4250 struct perf_counter *sub;
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02004251 struct perf_counter *child_ctr;
Paul Mackerrasd859e292009-01-17 18:10:22 +11004252
4253 leader = inherit_counter(parent_counter, parent, parent_ctx,
4254 child, NULL, child_ctx);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02004255 if (IS_ERR(leader))
4256 return PTR_ERR(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +11004257 list_for_each_entry(sub, &parent_counter->sibling_list, list_entry) {
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02004258 child_ctr = inherit_counter(sub, parent, parent_ctx,
4259 child, leader, child_ctx);
4260 if (IS_ERR(child_ctr))
4261 return PTR_ERR(child_ctr);
Paul Mackerrasd859e292009-01-17 18:10:22 +11004262 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01004263 return 0;
4264}
4265
Paul Mackerrasd859e292009-01-17 18:10:22 +11004266static void sync_child_counter(struct perf_counter *child_counter,
Peter Zijlstra38b200d2009-06-23 20:13:11 +02004267 struct task_struct *child)
Paul Mackerrasd859e292009-01-17 18:10:22 +11004268{
Peter Zijlstra38b200d2009-06-23 20:13:11 +02004269 struct perf_counter *parent_counter = child_counter->parent;
Peter Zijlstra8bc20952009-05-15 20:45:59 +02004270 u64 child_val;
Paul Mackerrasd859e292009-01-17 18:10:22 +11004271
Peter Zijlstrabfbd3382009-06-24 21:11:59 +02004272 if (child_counter->attr.inherit_stat)
4273 perf_counter_read_event(child_counter, child);
Peter Zijlstra38b200d2009-06-23 20:13:11 +02004274
Paul Mackerrasd859e292009-01-17 18:10:22 +11004275 child_val = atomic64_read(&child_counter->count);
4276
4277 /*
4278 * Add back the child's count to the parent's count:
4279 */
4280 atomic64_add(child_val, &parent_counter->count);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11004281 atomic64_add(child_counter->total_time_enabled,
4282 &parent_counter->child_total_time_enabled);
4283 atomic64_add(child_counter->total_time_running,
4284 &parent_counter->child_total_time_running);
Paul Mackerrasd859e292009-01-17 18:10:22 +11004285
4286 /*
4287 * Remove this counter from the parent's list
4288 */
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004289 WARN_ON_ONCE(parent_counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02004290 mutex_lock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11004291 list_del_init(&child_counter->child_list);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02004292 mutex_unlock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11004293
4294 /*
4295 * Release the parent counter, if this was the last
4296 * reference to it.
4297 */
4298 fput(parent_counter->filp);
4299}
4300
Ingo Molnar9b51f662008-12-12 13:49:45 +01004301static void
Peter Zijlstrabbbee902009-05-29 14:25:58 +02004302__perf_counter_exit_task(struct perf_counter *child_counter,
Peter Zijlstra38b200d2009-06-23 20:13:11 +02004303 struct perf_counter_context *child_ctx,
4304 struct task_struct *child)
Ingo Molnar9b51f662008-12-12 13:49:45 +01004305{
4306 struct perf_counter *parent_counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004307
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004308 update_counter_times(child_counter);
Peter Zijlstraaa9c67f2009-05-23 18:28:59 +02004309 perf_counter_remove_from_context(child_counter);
Ingo Molnar0cc0c022008-12-14 23:20:36 +01004310
Ingo Molnar9b51f662008-12-12 13:49:45 +01004311 parent_counter = child_counter->parent;
4312 /*
4313 * It can happen that parent exits first, and has counters
4314 * that are still around due to the child reference. These
4315 * counters need to be zapped - but otherwise linger.
4316 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11004317 if (parent_counter) {
Peter Zijlstra38b200d2009-06-23 20:13:11 +02004318 sync_child_counter(child_counter, child);
Peter Zijlstraf1600952009-03-19 20:26:16 +01004319 free_counter(child_counter);
Paul Mackerras4bcf3492009-02-11 13:53:19 +01004320 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01004321}
4322
4323/*
Paul Mackerrasd859e292009-01-17 18:10:22 +11004324 * When a child task exits, feed back counter values to parent counters.
Ingo Molnar9b51f662008-12-12 13:49:45 +01004325 */
4326void perf_counter_exit_task(struct task_struct *child)
4327{
4328 struct perf_counter *child_counter, *tmp;
4329 struct perf_counter_context *child_ctx;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004330 unsigned long flags;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004331
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02004332 if (likely(!child->perf_counter_ctxp)) {
Peter Zijlstra3a80b4a2009-08-07 19:49:01 +02004333 perf_counter_task(child, NULL, 0);
Ingo Molnar9b51f662008-12-12 13:49:45 +01004334 return;
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02004335 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01004336
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004337 local_irq_save(flags);
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004338 /*
4339 * We can't reschedule here because interrupts are disabled,
4340 * and either child is current or it is a task that can't be
4341 * scheduled, so we are now safe from rescheduling changing
4342 * our context.
4343 */
4344 child_ctx = child->perf_counter_ctxp;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004345 __perf_counter_task_sched_out(child_ctx);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10004346
4347 /*
4348 * Take the context lock here so that if find_get_context is
4349 * reading child->perf_counter_ctxp, we wait until it has
4350 * incremented the context's refcount before we do put_ctx below.
4351 */
4352 spin_lock(&child_ctx->lock);
Peter Zijlstra3a80b4a2009-08-07 19:49:01 +02004353 child->perf_counter_ctxp = NULL;
Peter Zijlstra71a851b2009-07-10 09:06:56 +02004354 /*
4355 * If this context is a clone; unclone it so it can't get
4356 * swapped to another process while we're removing all
4357 * the counters from it.
4358 */
4359 unclone_ctx(child_ctx);
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02004360 spin_unlock_irqrestore(&child_ctx->lock, flags);
4361
4362 /*
4363 * Report the task dead after unscheduling the counters so that we
4364 * won't get any samples after PERF_EVENT_EXIT. We can however still
4365 * get a few PERF_EVENT_READ events.
4366 */
Peter Zijlstra3a80b4a2009-08-07 19:49:01 +02004367 perf_counter_task(child, child_ctx, 0);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004368
Peter Zijlstra66fff222009-06-10 22:53:37 +02004369 /*
4370 * We can recurse on the same lock type through:
4371 *
4372 * __perf_counter_exit_task()
4373 * sync_child_counter()
4374 * fput(parent_counter->filp)
4375 * perf_release()
4376 * mutex_lock(&ctx->mutex)
4377 *
4378 * But since its the parent context it won't be the same instance.
4379 */
4380 mutex_lock_nested(&child_ctx->mutex, SINGLE_DEPTH_NESTING);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004381
Peter Zijlstra8bc20952009-05-15 20:45:59 +02004382again:
Ingo Molnar9b51f662008-12-12 13:49:45 +01004383 list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list,
4384 list_entry)
Peter Zijlstra38b200d2009-06-23 20:13:11 +02004385 __perf_counter_exit_task(child_counter, child_ctx, child);
Peter Zijlstra8bc20952009-05-15 20:45:59 +02004386
4387 /*
4388 * If the last counter was a group counter, it will have appended all
4389 * its siblings to the list, but we obtained 'tmp' before that which
4390 * will still point to the list head terminating the iteration.
4391 */
4392 if (!list_empty(&child_ctx->counter_list))
4393 goto again;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004394
4395 mutex_unlock(&child_ctx->mutex);
4396
4397 put_ctx(child_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01004398}
4399
4400/*
Peter Zijlstrabbbee902009-05-29 14:25:58 +02004401 * free an unexposed, unused context as created by inheritance by
4402 * init_task below, used by fork() in case of fail.
4403 */
4404void perf_counter_free_task(struct task_struct *task)
4405{
4406 struct perf_counter_context *ctx = task->perf_counter_ctxp;
4407 struct perf_counter *counter, *tmp;
4408
4409 if (!ctx)
4410 return;
4411
4412 mutex_lock(&ctx->mutex);
4413again:
4414 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry) {
4415 struct perf_counter *parent = counter->parent;
4416
4417 if (WARN_ON_ONCE(!parent))
4418 continue;
4419
4420 mutex_lock(&parent->child_mutex);
4421 list_del_init(&counter->child_list);
4422 mutex_unlock(&parent->child_mutex);
4423
4424 fput(parent->filp);
4425
4426 list_del_counter(counter, ctx);
4427 free_counter(counter);
4428 }
4429
4430 if (!list_empty(&ctx->counter_list))
4431 goto again;
4432
4433 mutex_unlock(&ctx->mutex);
4434
4435 put_ctx(ctx);
4436}
4437
4438/*
Ingo Molnar9b51f662008-12-12 13:49:45 +01004439 * Initialize the perf_counter context in task_struct
4440 */
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02004441int perf_counter_init_task(struct task_struct *child)
Ingo Molnar9b51f662008-12-12 13:49:45 +01004442{
4443 struct perf_counter_context *child_ctx, *parent_ctx;
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004444 struct perf_counter_context *cloned_ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +11004445 struct perf_counter *counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004446 struct task_struct *parent = current;
Paul Mackerras564c2b22009-05-22 14:27:22 +10004447 int inherited_all = 1;
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02004448 int ret = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004449
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004450 child->perf_counter_ctxp = NULL;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004451
Peter Zijlstra082ff5a2009-05-23 18:29:00 +02004452 mutex_init(&child->perf_counter_mutex);
4453 INIT_LIST_HEAD(&child->perf_counter_list);
4454
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004455 if (likely(!parent->perf_counter_ctxp))
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02004456 return 0;
4457
Ingo Molnar9b51f662008-12-12 13:49:45 +01004458 /*
4459 * This is executed from the parent task context, so inherit
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004460 * counters that have been marked for cloning.
4461 * First allocate and initialize a context for the child.
Ingo Molnar9b51f662008-12-12 13:49:45 +01004462 */
4463
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004464 child_ctx = kmalloc(sizeof(struct perf_counter_context), GFP_KERNEL);
4465 if (!child_ctx)
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02004466 return -ENOMEM;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004467
4468 __perf_counter_init_context(child_ctx, child);
4469 child->perf_counter_ctxp = child_ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10004470 get_task_struct(child);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004471
Ingo Molnar9b51f662008-12-12 13:49:45 +01004472 /*
Paul Mackerras25346b92009-06-01 17:48:12 +10004473 * If the parent's context is a clone, pin it so it won't get
4474 * swapped under us.
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004475 */
Paul Mackerras25346b92009-06-01 17:48:12 +10004476 parent_ctx = perf_pin_task_context(parent);
4477
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004478 /*
4479 * No need to check if parent_ctx != NULL here; since we saw
4480 * it non-NULL earlier, the only reason for it to become NULL
4481 * is if we exit, and since we're currently in the middle of
4482 * a fork we can't be exiting at the same time.
4483 */
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004484
4485 /*
Ingo Molnar9b51f662008-12-12 13:49:45 +01004486 * Lock the parent list. No need to lock the child - not PID
4487 * hashed yet and not running, so nobody can access it.
4488 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11004489 mutex_lock(&parent_ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01004490
4491 /*
4492 * We dont have to disable NMIs - we are only looking at
4493 * the list, not manipulating it:
4494 */
Peter Zijlstrad7b629a2009-05-20 12:21:19 +02004495 list_for_each_entry_rcu(counter, &parent_ctx->event_list, event_entry) {
4496 if (counter != counter->group_leader)
4497 continue;
4498
Peter Zijlstra0d486962009-06-02 19:22:16 +02004499 if (!counter->attr.inherit) {
Paul Mackerras564c2b22009-05-22 14:27:22 +10004500 inherited_all = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004501 continue;
Paul Mackerras564c2b22009-05-22 14:27:22 +10004502 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01004503
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02004504 ret = inherit_group(counter, parent, parent_ctx,
4505 child, child_ctx);
4506 if (ret) {
Paul Mackerras564c2b22009-05-22 14:27:22 +10004507 inherited_all = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004508 break;
Paul Mackerras564c2b22009-05-22 14:27:22 +10004509 }
4510 }
4511
4512 if (inherited_all) {
4513 /*
4514 * Mark the child context as a clone of the parent
4515 * context, or of whatever the parent is a clone of.
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004516 * Note that if the parent is a clone, it could get
4517 * uncloned at any point, but that doesn't matter
4518 * because the list of counters and the generation
4519 * count can't have changed since we took the mutex.
Paul Mackerras564c2b22009-05-22 14:27:22 +10004520 */
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004521 cloned_ctx = rcu_dereference(parent_ctx->parent_ctx);
4522 if (cloned_ctx) {
4523 child_ctx->parent_ctx = cloned_ctx;
Paul Mackerras25346b92009-06-01 17:48:12 +10004524 child_ctx->parent_gen = parent_ctx->parent_gen;
Paul Mackerras564c2b22009-05-22 14:27:22 +10004525 } else {
4526 child_ctx->parent_ctx = parent_ctx;
4527 child_ctx->parent_gen = parent_ctx->generation;
4528 }
4529 get_ctx(child_ctx->parent_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01004530 }
4531
Paul Mackerrasd859e292009-01-17 18:10:22 +11004532 mutex_unlock(&parent_ctx->mutex);
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02004533
Paul Mackerras25346b92009-06-01 17:48:12 +10004534 perf_unpin_context(parent_ctx);
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004535
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02004536 return ret;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004537}
4538
Ingo Molnar04289bb2008-12-11 08:38:42 +01004539static void __cpuinit perf_counter_init_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01004540{
Ingo Molnar04289bb2008-12-11 08:38:42 +01004541 struct perf_cpu_context *cpuctx;
Thomas Gleixner0793a612008-12-04 20:12:29 +01004542
Ingo Molnar04289bb2008-12-11 08:38:42 +01004543 cpuctx = &per_cpu(perf_cpu_context, cpu);
4544 __perf_counter_init_context(&cpuctx->ctx, NULL);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004545
Ingo Molnar1dce8d92009-05-04 19:23:18 +02004546 spin_lock(&perf_resource_lock);
Ingo Molnar04289bb2008-12-11 08:38:42 +01004547 cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu;
Ingo Molnar1dce8d92009-05-04 19:23:18 +02004548 spin_unlock(&perf_resource_lock);
Ingo Molnar04289bb2008-12-11 08:38:42 +01004549
Paul Mackerras01d02872009-01-14 13:44:19 +11004550 hw_perf_counter_setup(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004551}
4552
4553#ifdef CONFIG_HOTPLUG_CPU
Ingo Molnar04289bb2008-12-11 08:38:42 +01004554static void __perf_counter_exit_cpu(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01004555{
4556 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
4557 struct perf_counter_context *ctx = &cpuctx->ctx;
4558 struct perf_counter *counter, *tmp;
4559
Ingo Molnar04289bb2008-12-11 08:38:42 +01004560 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry)
4561 __perf_counter_remove_from_context(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004562}
Ingo Molnar04289bb2008-12-11 08:38:42 +01004563static void perf_counter_exit_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01004564{
Paul Mackerrasd859e292009-01-17 18:10:22 +11004565 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
4566 struct perf_counter_context *ctx = &cpuctx->ctx;
4567
4568 mutex_lock(&ctx->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01004569 smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1);
Paul Mackerrasd859e292009-01-17 18:10:22 +11004570 mutex_unlock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004571}
4572#else
Ingo Molnar04289bb2008-12-11 08:38:42 +01004573static inline void perf_counter_exit_cpu(int cpu) { }
Thomas Gleixner0793a612008-12-04 20:12:29 +01004574#endif
4575
4576static int __cpuinit
4577perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
4578{
4579 unsigned int cpu = (long)hcpu;
4580
4581 switch (action) {
4582
4583 case CPU_UP_PREPARE:
4584 case CPU_UP_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01004585 perf_counter_init_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004586 break;
4587
Ingo Molnar28402972009-08-13 10:13:22 +02004588 case CPU_ONLINE:
4589 case CPU_ONLINE_FROZEN:
4590 hw_perf_counter_setup_online(cpu);
4591 break;
4592
Thomas Gleixner0793a612008-12-04 20:12:29 +01004593 case CPU_DOWN_PREPARE:
4594 case CPU_DOWN_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01004595 perf_counter_exit_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004596 break;
4597
4598 default:
4599 break;
4600 }
4601
4602 return NOTIFY_OK;
4603}
4604
Paul Mackerrasf38b0822009-06-02 21:05:16 +10004605/*
4606 * This has to have a higher priority than migration_notifier in sched.c.
4607 */
Thomas Gleixner0793a612008-12-04 20:12:29 +01004608static struct notifier_block __cpuinitdata perf_cpu_nb = {
4609 .notifier_call = perf_cpu_notify,
Paul Mackerrasf38b0822009-06-02 21:05:16 +10004610 .priority = 20,
Thomas Gleixner0793a612008-12-04 20:12:29 +01004611};
4612
Ingo Molnar0d905bc2009-05-04 19:13:30 +02004613void __init perf_counter_init(void)
Thomas Gleixner0793a612008-12-04 20:12:29 +01004614{
4615 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
4616 (void *)(long)smp_processor_id());
Ingo Molnar28402972009-08-13 10:13:22 +02004617 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_ONLINE,
4618 (void *)(long)smp_processor_id());
Thomas Gleixner0793a612008-12-04 20:12:29 +01004619 register_cpu_notifier(&perf_cpu_nb);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004620}
Thomas Gleixner0793a612008-12-04 20:12:29 +01004621
4622static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
4623{
4624 return sprintf(buf, "%d\n", perf_reserved_percpu);
4625}
4626
4627static ssize_t
4628perf_set_reserve_percpu(struct sysdev_class *class,
4629 const char *buf,
4630 size_t count)
4631{
4632 struct perf_cpu_context *cpuctx;
4633 unsigned long val;
4634 int err, cpu, mpt;
4635
4636 err = strict_strtoul(buf, 10, &val);
4637 if (err)
4638 return err;
4639 if (val > perf_max_counters)
4640 return -EINVAL;
4641
Ingo Molnar1dce8d92009-05-04 19:23:18 +02004642 spin_lock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004643 perf_reserved_percpu = val;
4644 for_each_online_cpu(cpu) {
4645 cpuctx = &per_cpu(perf_cpu_context, cpu);
4646 spin_lock_irq(&cpuctx->ctx.lock);
4647 mpt = min(perf_max_counters - cpuctx->ctx.nr_counters,
4648 perf_max_counters - perf_reserved_percpu);
4649 cpuctx->max_pertask = mpt;
4650 spin_unlock_irq(&cpuctx->ctx.lock);
4651 }
Ingo Molnar1dce8d92009-05-04 19:23:18 +02004652 spin_unlock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004653
4654 return count;
4655}
4656
4657static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
4658{
4659 return sprintf(buf, "%d\n", perf_overcommit);
4660}
4661
4662static ssize_t
4663perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
4664{
4665 unsigned long val;
4666 int err;
4667
4668 err = strict_strtoul(buf, 10, &val);
4669 if (err)
4670 return err;
4671 if (val > 1)
4672 return -EINVAL;
4673
Ingo Molnar1dce8d92009-05-04 19:23:18 +02004674 spin_lock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004675 perf_overcommit = val;
Ingo Molnar1dce8d92009-05-04 19:23:18 +02004676 spin_unlock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004677
4678 return count;
4679}
4680
4681static SYSDEV_CLASS_ATTR(
4682 reserve_percpu,
4683 0644,
4684 perf_show_reserve_percpu,
4685 perf_set_reserve_percpu
4686 );
4687
4688static SYSDEV_CLASS_ATTR(
4689 overcommit,
4690 0644,
4691 perf_show_overcommit,
4692 perf_set_overcommit
4693 );
4694
4695static struct attribute *perfclass_attrs[] = {
4696 &attr_reserve_percpu.attr,
4697 &attr_overcommit.attr,
4698 NULL
4699};
4700
4701static struct attribute_group perfclass_attr_group = {
4702 .attrs = perfclass_attrs,
4703 .name = "perf_counters",
4704};
4705
4706static int __init perf_counter_sysfs_init(void)
4707{
4708 return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
4709 &perfclass_attr_group);
4710}
4711device_initcall(perf_counter_sysfs_init);