blob: b0b20a07f3942422f614a4d0f429a23d19db396e [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 Molnar22a4f652009-06-01 10:13:37 +020091
92int __weak
93hw_perf_group_sched_in(struct perf_counter *group_leader,
Paul Mackerras3cbed422009-01-09 16:43:42 +110094 struct perf_cpu_context *cpuctx,
95 struct perf_counter_context *ctx, int cpu)
96{
97 return 0;
98}
Thomas Gleixner0793a612008-12-04 20:12:29 +010099
Paul Mackerras4eb96fc2009-01-09 17:24:34 +1100100void __weak perf_counter_print_debug(void) { }
101
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200102static DEFINE_PER_CPU(int, disable_count);
103
104void __perf_disable(void)
105{
106 __get_cpu_var(disable_count)++;
107}
108
109bool __perf_enable(void)
110{
111 return !--__get_cpu_var(disable_count);
112}
113
114void perf_disable(void)
115{
116 __perf_disable();
117 hw_perf_disable();
118}
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200119
120void perf_enable(void)
121{
122 if (__perf_enable())
123 hw_perf_enable();
124}
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200125
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000126static void get_ctx(struct perf_counter_context *ctx)
127{
Peter Zijlstrae5289d42009-06-19 13:22:51 +0200128 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000129}
130
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000131static void free_ctx(struct rcu_head *head)
132{
133 struct perf_counter_context *ctx;
134
135 ctx = container_of(head, struct perf_counter_context, rcu_head);
136 kfree(ctx);
137}
138
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000139static void put_ctx(struct perf_counter_context *ctx)
140{
Paul Mackerras564c2b22009-05-22 14:27:22 +1000141 if (atomic_dec_and_test(&ctx->refcount)) {
142 if (ctx->parent_ctx)
143 put_ctx(ctx->parent_ctx);
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000144 if (ctx->task)
145 put_task_struct(ctx->task);
146 call_rcu(&ctx->rcu_head, free_ctx);
Paul Mackerras564c2b22009-05-22 14:27:22 +1000147 }
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000148}
149
Peter Zijlstra71a851b2009-07-10 09:06:56 +0200150static void unclone_ctx(struct perf_counter_context *ctx)
151{
152 if (ctx->parent_ctx) {
153 put_ctx(ctx->parent_ctx);
154 ctx->parent_ctx = NULL;
155 }
156}
157
Peter Zijlstrafccc7142009-05-23 18:28:56 +0200158/*
Peter Zijlstra7f453c22009-07-21 13:19:40 +0200159 * If we inherit counters we want to return the parent counter id
160 * to userspace.
161 */
162static u64 primary_counter_id(struct perf_counter *counter)
163{
164 u64 id = counter->id;
165
166 if (counter->parent)
167 id = counter->parent->id;
168
169 return id;
170}
171
Peter Zijlstrafccc7142009-05-23 18:28:56 +0200172/*
Paul Mackerras25346b92009-06-01 17:48:12 +1000173 * Get the perf_counter_context for a task and lock it.
174 * This has to cope with with the fact that until it is locked,
175 * the context could get moved to another task.
176 */
Ingo Molnar22a4f652009-06-01 10:13:37 +0200177static struct perf_counter_context *
178perf_lock_task_context(struct task_struct *task, unsigned long *flags)
Paul Mackerras25346b92009-06-01 17:48:12 +1000179{
180 struct perf_counter_context *ctx;
181
182 rcu_read_lock();
183 retry:
184 ctx = rcu_dereference(task->perf_counter_ctxp);
185 if (ctx) {
186 /*
187 * If this context is a clone of another, it might
188 * get swapped for another underneath us by
189 * perf_counter_task_sched_out, though the
190 * rcu_read_lock() protects us from any context
191 * getting freed. Lock the context and check if it
192 * got swapped before we could get the lock, and retry
193 * if so. If we locked the right context, then it
194 * can't get swapped on us any more.
195 */
196 spin_lock_irqsave(&ctx->lock, *flags);
197 if (ctx != rcu_dereference(task->perf_counter_ctxp)) {
198 spin_unlock_irqrestore(&ctx->lock, *flags);
199 goto retry;
200 }
Peter Zijlstrab49a9e72009-06-19 17:39:33 +0200201
202 if (!atomic_inc_not_zero(&ctx->refcount)) {
203 spin_unlock_irqrestore(&ctx->lock, *flags);
204 ctx = NULL;
205 }
Paul Mackerras25346b92009-06-01 17:48:12 +1000206 }
207 rcu_read_unlock();
208 return ctx;
209}
210
211/*
212 * Get the context for a task and increment its pin_count so it
213 * can't get swapped to another task. This also increments its
214 * reference count so that the context can't get freed.
215 */
216static struct perf_counter_context *perf_pin_task_context(struct task_struct *task)
217{
218 struct perf_counter_context *ctx;
219 unsigned long flags;
220
221 ctx = perf_lock_task_context(task, &flags);
222 if (ctx) {
223 ++ctx->pin_count;
Paul Mackerras25346b92009-06-01 17:48:12 +1000224 spin_unlock_irqrestore(&ctx->lock, flags);
225 }
226 return ctx;
227}
228
229static void perf_unpin_context(struct perf_counter_context *ctx)
230{
231 unsigned long flags;
232
233 spin_lock_irqsave(&ctx->lock, flags);
234 --ctx->pin_count;
235 spin_unlock_irqrestore(&ctx->lock, flags);
236 put_ctx(ctx);
237}
238
239/*
Peter Zijlstrafccc7142009-05-23 18:28:56 +0200240 * Add a counter from the lists for its context.
241 * Must be called with ctx->mutex and ctx->lock held.
242 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100243static void
244list_add_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
245{
246 struct perf_counter *group_leader = counter->group_leader;
247
248 /*
249 * Depending on whether it is a standalone or sibling counter,
250 * add it straight to the context's counter list, or to the group
251 * leader's sibling list:
252 */
Peter Zijlstra3df5eda2009-05-08 18:52:22 +0200253 if (group_leader == counter)
Ingo Molnar04289bb2008-12-11 08:38:42 +0100254 list_add_tail(&counter->list_entry, &ctx->counter_list);
Peter Zijlstra5c148192009-03-25 12:30:23 +0100255 else {
Ingo Molnar04289bb2008-12-11 08:38:42 +0100256 list_add_tail(&counter->list_entry, &group_leader->sibling_list);
Peter Zijlstra5c148192009-03-25 12:30:23 +0100257 group_leader->nr_siblings++;
258 }
Peter Zijlstra592903c2009-03-13 12:21:36 +0100259
260 list_add_rcu(&counter->event_entry, &ctx->event_list);
Peter Zijlstra8bc20952009-05-15 20:45:59 +0200261 ctx->nr_counters++;
Peter Zijlstrabfbd3382009-06-24 21:11:59 +0200262 if (counter->attr.inherit_stat)
263 ctx->nr_stat++;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100264}
265
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000266/*
267 * Remove a counter from the lists for its context.
Peter Zijlstrafccc7142009-05-23 18:28:56 +0200268 * Must be called with ctx->mutex and ctx->lock held.
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000269 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100270static void
271list_del_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
272{
273 struct perf_counter *sibling, *tmp;
274
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000275 if (list_empty(&counter->list_entry))
276 return;
Peter Zijlstra8bc20952009-05-15 20:45:59 +0200277 ctx->nr_counters--;
Peter Zijlstrabfbd3382009-06-24 21:11:59 +0200278 if (counter->attr.inherit_stat)
279 ctx->nr_stat--;
Peter Zijlstra8bc20952009-05-15 20:45:59 +0200280
Ingo Molnar04289bb2008-12-11 08:38:42 +0100281 list_del_init(&counter->list_entry);
Peter Zijlstra592903c2009-03-13 12:21:36 +0100282 list_del_rcu(&counter->event_entry);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100283
Peter Zijlstra5c148192009-03-25 12:30:23 +0100284 if (counter->group_leader != counter)
285 counter->group_leader->nr_siblings--;
286
Ingo Molnar04289bb2008-12-11 08:38:42 +0100287 /*
288 * If this was a group counter with sibling counters then
289 * upgrade the siblings to singleton counters by adding them
290 * to the context list directly:
291 */
292 list_for_each_entry_safe(sibling, tmp,
293 &counter->sibling_list, list_entry) {
294
Peter Zijlstra75564232009-03-13 12:21:29 +0100295 list_move_tail(&sibling->list_entry, &ctx->counter_list);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100296 sibling->group_leader = sibling;
297 }
298}
299
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100300static void
301counter_sched_out(struct perf_counter *counter,
302 struct perf_cpu_context *cpuctx,
303 struct perf_counter_context *ctx)
304{
305 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
306 return;
307
308 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200309 counter->tstamp_stopped = ctx->time;
Robert Richter4aeb0b42009-04-29 12:47:03 +0200310 counter->pmu->disable(counter);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100311 counter->oncpu = -1;
312
313 if (!is_software_counter(counter))
314 cpuctx->active_oncpu--;
315 ctx->nr_active--;
Peter Zijlstra0d486962009-06-02 19:22:16 +0200316 if (counter->attr.exclusive || !cpuctx->active_oncpu)
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100317 cpuctx->exclusive = 0;
318}
319
Paul Mackerrasd859e292009-01-17 18:10:22 +1100320static void
321group_sched_out(struct perf_counter *group_counter,
322 struct perf_cpu_context *cpuctx,
323 struct perf_counter_context *ctx)
324{
325 struct perf_counter *counter;
326
327 if (group_counter->state != PERF_COUNTER_STATE_ACTIVE)
328 return;
329
330 counter_sched_out(group_counter, cpuctx, ctx);
331
332 /*
333 * Schedule out siblings (if any):
334 */
335 list_for_each_entry(counter, &group_counter->sibling_list, list_entry)
336 counter_sched_out(counter, cpuctx, ctx);
337
Peter Zijlstra0d486962009-06-02 19:22:16 +0200338 if (group_counter->attr.exclusive)
Paul Mackerrasd859e292009-01-17 18:10:22 +1100339 cpuctx->exclusive = 0;
340}
341
Thomas Gleixner0793a612008-12-04 20:12:29 +0100342/*
343 * Cross CPU call to remove a performance counter
344 *
345 * We disable the counter on the hardware level first. After that we
346 * remove it from the context list.
347 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100348static void __perf_counter_remove_from_context(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100349{
350 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
351 struct perf_counter *counter = info;
352 struct perf_counter_context *ctx = counter->ctx;
353
354 /*
355 * If this is a task context, we need to check whether it is
356 * the current task context of this cpu. If not it has been
357 * scheduled out before the smp call arrived.
358 */
Peter Zijlstra665c2142009-05-29 14:51:57 +0200359 if (ctx->task && cpuctx->task_ctx != ctx)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100360 return;
361
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200362 spin_lock(&ctx->lock);
Ingo Molnar34adc802009-05-20 20:13:28 +0200363 /*
364 * Protect the list operation against NMI by disabling the
365 * counters on a global level.
366 */
367 perf_disable();
Thomas Gleixner0793a612008-12-04 20:12:29 +0100368
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100369 counter_sched_out(counter, cpuctx, ctx);
370
Ingo Molnar04289bb2008-12-11 08:38:42 +0100371 list_del_counter(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100372
373 if (!ctx->task) {
374 /*
375 * Allow more per task counters with respect to the
376 * reservation:
377 */
378 cpuctx->max_pertask =
379 min(perf_max_counters - ctx->nr_counters,
380 perf_max_counters - perf_reserved_percpu);
381 }
382
Ingo Molnar34adc802009-05-20 20:13:28 +0200383 perf_enable();
Peter Zijlstra665c2142009-05-29 14:51:57 +0200384 spin_unlock(&ctx->lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100385}
386
387
388/*
389 * Remove the counter from a task's (or a CPU's) list of counters.
390 *
Peter Zijlstrafccc7142009-05-23 18:28:56 +0200391 * Must be called with ctx->mutex held.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100392 *
393 * CPU counters are removed with a smp call. For task counters we only
394 * call when the task is on a CPU.
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000395 *
396 * If counter->ctx is a cloned context, callers must make sure that
397 * every task struct that counter->ctx->task could possibly point to
398 * remains valid. This is OK when called from perf_release since
399 * that only calls us on the top-level context, which can't be a clone.
400 * When called from perf_counter_exit_task, it's OK because the
401 * context has been detached from its task.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100402 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100403static void perf_counter_remove_from_context(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100404{
405 struct perf_counter_context *ctx = counter->ctx;
406 struct task_struct *task = ctx->task;
407
408 if (!task) {
409 /*
410 * Per cpu counters are removed via an smp call and
411 * the removal is always sucessful.
412 */
413 smp_call_function_single(counter->cpu,
Ingo Molnar04289bb2008-12-11 08:38:42 +0100414 __perf_counter_remove_from_context,
Thomas Gleixner0793a612008-12-04 20:12:29 +0100415 counter, 1);
416 return;
417 }
418
419retry:
Ingo Molnar04289bb2008-12-11 08:38:42 +0100420 task_oncpu_function_call(task, __perf_counter_remove_from_context,
Thomas Gleixner0793a612008-12-04 20:12:29 +0100421 counter);
422
423 spin_lock_irq(&ctx->lock);
424 /*
425 * If the context is active we need to retry the smp call.
426 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100427 if (ctx->nr_active && !list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100428 spin_unlock_irq(&ctx->lock);
429 goto retry;
430 }
431
432 /*
433 * The lock prevents that this context is scheduled in so we
Ingo Molnar04289bb2008-12-11 08:38:42 +0100434 * can remove the counter safely, if the call above did not
Thomas Gleixner0793a612008-12-04 20:12:29 +0100435 * succeed.
436 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100437 if (!list_empty(&counter->list_entry)) {
Ingo Molnar04289bb2008-12-11 08:38:42 +0100438 list_del_counter(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100439 }
440 spin_unlock_irq(&ctx->lock);
441}
442
Peter Zijlstra4af49982009-04-06 11:45:10 +0200443static inline u64 perf_clock(void)
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100444{
Peter Zijlstra4af49982009-04-06 11:45:10 +0200445 return cpu_clock(smp_processor_id());
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100446}
447
448/*
449 * Update the record of the current time in a context.
450 */
Peter Zijlstra4af49982009-04-06 11:45:10 +0200451static void update_context_time(struct perf_counter_context *ctx)
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100452{
Peter Zijlstra4af49982009-04-06 11:45:10 +0200453 u64 now = perf_clock();
454
455 ctx->time += now - ctx->timestamp;
456 ctx->timestamp = now;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100457}
458
459/*
460 * Update the total_time_enabled and total_time_running fields for a counter.
461 */
462static void update_counter_times(struct perf_counter *counter)
463{
464 struct perf_counter_context *ctx = counter->ctx;
465 u64 run_end;
466
Peter Zijlstra4af49982009-04-06 11:45:10 +0200467 if (counter->state < PERF_COUNTER_STATE_INACTIVE)
468 return;
469
470 counter->total_time_enabled = ctx->time - counter->tstamp_enabled;
471
472 if (counter->state == PERF_COUNTER_STATE_INACTIVE)
473 run_end = counter->tstamp_stopped;
474 else
475 run_end = ctx->time;
476
477 counter->total_time_running = run_end - counter->tstamp_running;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100478}
479
480/*
481 * Update total_time_enabled and total_time_running for all counters in a group.
482 */
483static void update_group_times(struct perf_counter *leader)
484{
485 struct perf_counter *counter;
486
487 update_counter_times(leader);
488 list_for_each_entry(counter, &leader->sibling_list, list_entry)
489 update_counter_times(counter);
490}
491
492/*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100493 * Cross CPU call to disable a performance counter
494 */
495static void __perf_counter_disable(void *info)
496{
497 struct perf_counter *counter = info;
498 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
499 struct perf_counter_context *ctx = counter->ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100500
501 /*
502 * If this is a per-task counter, need to check whether this
503 * counter's task is the current task on this cpu.
504 */
Peter Zijlstra665c2142009-05-29 14:51:57 +0200505 if (ctx->task && cpuctx->task_ctx != ctx)
Paul Mackerrasd859e292009-01-17 18:10:22 +1100506 return;
507
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200508 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100509
510 /*
511 * If the counter is on, turn it off.
512 * If it is in error state, leave it in error state.
513 */
514 if (counter->state >= PERF_COUNTER_STATE_INACTIVE) {
Peter Zijlstra4af49982009-04-06 11:45:10 +0200515 update_context_time(ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100516 update_counter_times(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100517 if (counter == counter->group_leader)
518 group_sched_out(counter, cpuctx, ctx);
519 else
520 counter_sched_out(counter, cpuctx, ctx);
521 counter->state = PERF_COUNTER_STATE_OFF;
522 }
523
Peter Zijlstra665c2142009-05-29 14:51:57 +0200524 spin_unlock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100525}
526
527/*
528 * Disable a counter.
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000529 *
530 * If counter->ctx is a cloned context, callers must make sure that
531 * every task struct that counter->ctx->task could possibly point to
532 * remains valid. This condition is satisifed when called through
533 * perf_counter_for_each_child or perf_counter_for_each because they
534 * hold the top-level counter's child_mutex, so any descendant that
535 * goes to exit will block in sync_child_counter.
536 * When called from perf_pending_counter it's OK because counter->ctx
537 * is the current context on this CPU and preemption is disabled,
538 * hence we can't get into perf_counter_task_sched_out for this context.
Paul Mackerrasd859e292009-01-17 18:10:22 +1100539 */
540static void perf_counter_disable(struct perf_counter *counter)
541{
542 struct perf_counter_context *ctx = counter->ctx;
543 struct task_struct *task = ctx->task;
544
545 if (!task) {
546 /*
547 * Disable the counter on the cpu that it's on
548 */
549 smp_call_function_single(counter->cpu, __perf_counter_disable,
550 counter, 1);
551 return;
552 }
553
554 retry:
555 task_oncpu_function_call(task, __perf_counter_disable, counter);
556
557 spin_lock_irq(&ctx->lock);
558 /*
559 * If the counter is still active, we need to retry the cross-call.
560 */
561 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
562 spin_unlock_irq(&ctx->lock);
563 goto retry;
564 }
565
566 /*
567 * Since we have the lock this context can't be scheduled
568 * in, so we can change the state safely.
569 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100570 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
571 update_counter_times(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100572 counter->state = PERF_COUNTER_STATE_OFF;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100573 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100574
575 spin_unlock_irq(&ctx->lock);
576}
577
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100578static int
579counter_sched_in(struct perf_counter *counter,
580 struct perf_cpu_context *cpuctx,
581 struct perf_counter_context *ctx,
582 int cpu)
583{
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100584 if (counter->state <= PERF_COUNTER_STATE_OFF)
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100585 return 0;
586
587 counter->state = PERF_COUNTER_STATE_ACTIVE;
588 counter->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */
589 /*
590 * The new state must be visible before we turn it on in the hardware:
591 */
592 smp_wmb();
593
Robert Richter4aeb0b42009-04-29 12:47:03 +0200594 if (counter->pmu->enable(counter)) {
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100595 counter->state = PERF_COUNTER_STATE_INACTIVE;
596 counter->oncpu = -1;
597 return -EAGAIN;
598 }
599
Peter Zijlstra4af49982009-04-06 11:45:10 +0200600 counter->tstamp_running += ctx->time - counter->tstamp_stopped;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100601
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100602 if (!is_software_counter(counter))
603 cpuctx->active_oncpu++;
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100604 ctx->nr_active++;
605
Peter Zijlstra0d486962009-06-02 19:22:16 +0200606 if (counter->attr.exclusive)
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100607 cpuctx->exclusive = 1;
608
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100609 return 0;
610}
611
Paul Mackerras6751b712009-05-11 12:08:02 +1000612static int
613group_sched_in(struct perf_counter *group_counter,
614 struct perf_cpu_context *cpuctx,
615 struct perf_counter_context *ctx,
616 int cpu)
617{
618 struct perf_counter *counter, *partial_group;
619 int ret;
620
621 if (group_counter->state == PERF_COUNTER_STATE_OFF)
622 return 0;
623
624 ret = hw_perf_group_sched_in(group_counter, cpuctx, ctx, cpu);
625 if (ret)
626 return ret < 0 ? ret : 0;
627
Paul Mackerras6751b712009-05-11 12:08:02 +1000628 if (counter_sched_in(group_counter, cpuctx, ctx, cpu))
629 return -EAGAIN;
630
631 /*
632 * Schedule in siblings as one group (if any):
633 */
634 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
Paul Mackerras6751b712009-05-11 12:08:02 +1000635 if (counter_sched_in(counter, cpuctx, ctx, cpu)) {
636 partial_group = counter;
637 goto group_error;
638 }
639 }
640
641 return 0;
642
643group_error:
644 /*
645 * Groups can be scheduled in as one unit only, so undo any
646 * partial group before returning:
647 */
648 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
649 if (counter == partial_group)
650 break;
651 counter_sched_out(counter, cpuctx, ctx);
652 }
653 counter_sched_out(group_counter, cpuctx, ctx);
654
655 return -EAGAIN;
656}
657
Thomas Gleixner0793a612008-12-04 20:12:29 +0100658/*
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100659 * Return 1 for a group consisting entirely of software counters,
660 * 0 if the group contains any hardware counters.
661 */
662static int is_software_only_group(struct perf_counter *leader)
663{
664 struct perf_counter *counter;
665
666 if (!is_software_counter(leader))
667 return 0;
Peter Zijlstra5c148192009-03-25 12:30:23 +0100668
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100669 list_for_each_entry(counter, &leader->sibling_list, list_entry)
670 if (!is_software_counter(counter))
671 return 0;
Peter Zijlstra5c148192009-03-25 12:30:23 +0100672
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100673 return 1;
674}
675
676/*
677 * Work out whether we can put this counter group on the CPU now.
678 */
679static int group_can_go_on(struct perf_counter *counter,
680 struct perf_cpu_context *cpuctx,
681 int can_add_hw)
682{
683 /*
684 * Groups consisting entirely of software counters can always go on.
685 */
686 if (is_software_only_group(counter))
687 return 1;
688 /*
689 * If an exclusive group is already on, no other hardware
690 * counters can go on.
691 */
692 if (cpuctx->exclusive)
693 return 0;
694 /*
695 * If this group is exclusive and there are already
696 * counters on the CPU, it can't go on.
697 */
Peter Zijlstra0d486962009-06-02 19:22:16 +0200698 if (counter->attr.exclusive && cpuctx->active_oncpu)
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100699 return 0;
700 /*
701 * Otherwise, try to add it if all previous groups were able
702 * to go on.
703 */
704 return can_add_hw;
705}
706
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100707static void add_counter_to_ctx(struct perf_counter *counter,
708 struct perf_counter_context *ctx)
709{
710 list_add_counter(counter, ctx);
Peter Zijlstra4af49982009-04-06 11:45:10 +0200711 counter->tstamp_enabled = ctx->time;
712 counter->tstamp_running = ctx->time;
713 counter->tstamp_stopped = ctx->time;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100714}
715
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100716/*
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100717 * Cross CPU call to install and enable a performance counter
Peter Zijlstra682076a2009-05-23 18:28:57 +0200718 *
719 * Must be called with ctx->mutex held
Thomas Gleixner0793a612008-12-04 20:12:29 +0100720 */
721static void __perf_install_in_context(void *info)
722{
723 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
724 struct perf_counter *counter = info;
725 struct perf_counter_context *ctx = counter->ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100726 struct perf_counter *leader = counter->group_leader;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100727 int cpu = smp_processor_id();
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100728 int err;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100729
730 /*
731 * If this is a task context, we need to check whether it is
732 * the current task context of this cpu. If not it has been
733 * scheduled out before the smp call arrived.
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000734 * Or possibly this is the right context but it isn't
735 * on this cpu because it had no counters.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100736 */
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000737 if (ctx->task && cpuctx->task_ctx != ctx) {
Peter Zijlstra665c2142009-05-29 14:51:57 +0200738 if (cpuctx->task_ctx || ctx->task != current)
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000739 return;
740 cpuctx->task_ctx = ctx;
741 }
Thomas Gleixner0793a612008-12-04 20:12:29 +0100742
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200743 spin_lock(&ctx->lock);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000744 ctx->is_active = 1;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200745 update_context_time(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100746
747 /*
748 * Protect the list operation against NMI by disabling the
749 * counters on a global level. NOP for non NMI based counters.
750 */
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200751 perf_disable();
Thomas Gleixner0793a612008-12-04 20:12:29 +0100752
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100753 add_counter_to_ctx(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100754
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100755 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100756 * Don't put the counter on if it is disabled or if
757 * it is in a group and the group isn't on.
758 */
759 if (counter->state != PERF_COUNTER_STATE_INACTIVE ||
760 (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE))
761 goto unlock;
762
763 /*
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100764 * An exclusive counter can't go on if there are already active
765 * hardware counters, and no hardware counter can go on if there
766 * is already an exclusive counter on.
767 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100768 if (!group_can_go_on(counter, cpuctx, 1))
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100769 err = -EEXIST;
770 else
771 err = counter_sched_in(counter, cpuctx, ctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100772
Paul Mackerrasd859e292009-01-17 18:10:22 +1100773 if (err) {
774 /*
775 * This counter couldn't go on. If it is in a group
776 * then we have to pull the whole group off.
777 * If the counter group is pinned then put it in error state.
778 */
779 if (leader != counter)
780 group_sched_out(leader, cpuctx, ctx);
Peter Zijlstra0d486962009-06-02 19:22:16 +0200781 if (leader->attr.pinned) {
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100782 update_group_times(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100783 leader->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100784 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100785 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100786
787 if (!err && !ctx->task && cpuctx->max_pertask)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100788 cpuctx->max_pertask--;
789
Paul Mackerrasd859e292009-01-17 18:10:22 +1100790 unlock:
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200791 perf_enable();
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100792
Peter Zijlstra665c2142009-05-29 14:51:57 +0200793 spin_unlock(&ctx->lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100794}
795
796/*
797 * Attach a performance counter to a context
798 *
799 * First we add the counter to the list with the hardware enable bit
800 * in counter->hw_config cleared.
801 *
802 * If the counter is attached to a task which is on a CPU we use a smp
803 * call to enable it in the task context. The task might have been
804 * scheduled away, but we check this in the smp call again.
Paul Mackerrasd859e292009-01-17 18:10:22 +1100805 *
806 * Must be called with ctx->mutex held.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100807 */
808static void
809perf_install_in_context(struct perf_counter_context *ctx,
810 struct perf_counter *counter,
811 int cpu)
812{
813 struct task_struct *task = ctx->task;
814
Thomas Gleixner0793a612008-12-04 20:12:29 +0100815 if (!task) {
816 /*
817 * Per cpu counters are installed via an smp call and
818 * the install is always sucessful.
819 */
820 smp_call_function_single(cpu, __perf_install_in_context,
821 counter, 1);
822 return;
823 }
824
Thomas Gleixner0793a612008-12-04 20:12:29 +0100825retry:
826 task_oncpu_function_call(task, __perf_install_in_context,
827 counter);
828
829 spin_lock_irq(&ctx->lock);
830 /*
Thomas Gleixner0793a612008-12-04 20:12:29 +0100831 * we need to retry the smp call.
832 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100833 if (ctx->is_active && list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100834 spin_unlock_irq(&ctx->lock);
835 goto retry;
836 }
837
838 /*
839 * The lock prevents that this context is scheduled in so we
840 * can add the counter safely, if it the call above did not
841 * succeed.
842 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100843 if (list_empty(&counter->list_entry))
844 add_counter_to_ctx(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100845 spin_unlock_irq(&ctx->lock);
846}
847
Paul Mackerrasd859e292009-01-17 18:10:22 +1100848/*
849 * Cross CPU call to enable a performance counter
850 */
851static void __perf_counter_enable(void *info)
Ingo Molnar04289bb2008-12-11 08:38:42 +0100852{
Paul Mackerrasd859e292009-01-17 18:10:22 +1100853 struct perf_counter *counter = info;
854 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
855 struct perf_counter_context *ctx = counter->ctx;
856 struct perf_counter *leader = counter->group_leader;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100857 int err;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100858
859 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100860 * If this is a per-task counter, need to check whether this
861 * counter's task is the current task on this cpu.
Ingo Molnar04289bb2008-12-11 08:38:42 +0100862 */
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000863 if (ctx->task && cpuctx->task_ctx != ctx) {
Peter Zijlstra665c2142009-05-29 14:51:57 +0200864 if (cpuctx->task_ctx || ctx->task != current)
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000865 return;
866 cpuctx->task_ctx = ctx;
867 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100868
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200869 spin_lock(&ctx->lock);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000870 ctx->is_active = 1;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200871 update_context_time(ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100872
873 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
874 goto unlock;
875 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200876 counter->tstamp_enabled = ctx->time - counter->total_time_enabled;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100877
878 /*
879 * If the counter is in a group and isn't the group leader,
880 * then don't put it on unless the group is on.
881 */
882 if (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE)
883 goto unlock;
884
Paul Mackerrase758a332009-05-12 21:59:01 +1000885 if (!group_can_go_on(counter, cpuctx, 1)) {
Paul Mackerrasd859e292009-01-17 18:10:22 +1100886 err = -EEXIST;
Paul Mackerrase758a332009-05-12 21:59:01 +1000887 } else {
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200888 perf_disable();
Paul Mackerrase758a332009-05-12 21:59:01 +1000889 if (counter == leader)
890 err = group_sched_in(counter, cpuctx, ctx,
891 smp_processor_id());
892 else
893 err = counter_sched_in(counter, cpuctx, ctx,
894 smp_processor_id());
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200895 perf_enable();
Paul Mackerrase758a332009-05-12 21:59:01 +1000896 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100897
898 if (err) {
899 /*
900 * If this counter can't go on and it's part of a
901 * group, then the whole group has to come off.
902 */
903 if (leader != counter)
904 group_sched_out(leader, cpuctx, ctx);
Peter Zijlstra0d486962009-06-02 19:22:16 +0200905 if (leader->attr.pinned) {
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100906 update_group_times(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100907 leader->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100908 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100909 }
910
911 unlock:
Peter Zijlstra665c2142009-05-29 14:51:57 +0200912 spin_unlock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100913}
914
915/*
916 * Enable a counter.
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000917 *
918 * If counter->ctx is a cloned context, callers must make sure that
919 * every task struct that counter->ctx->task could possibly point to
920 * remains valid. This condition is satisfied when called through
921 * perf_counter_for_each_child or perf_counter_for_each as described
922 * for perf_counter_disable.
Paul Mackerrasd859e292009-01-17 18:10:22 +1100923 */
924static void perf_counter_enable(struct perf_counter *counter)
925{
926 struct perf_counter_context *ctx = counter->ctx;
927 struct task_struct *task = ctx->task;
928
929 if (!task) {
930 /*
931 * Enable the counter on the cpu that it's on
932 */
933 smp_call_function_single(counter->cpu, __perf_counter_enable,
934 counter, 1);
935 return;
936 }
937
938 spin_lock_irq(&ctx->lock);
939 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
940 goto out;
941
942 /*
943 * If the counter is in error state, clear that first.
944 * That way, if we see the counter in error state below, we
945 * know that it has gone back into error state, as distinct
946 * from the task having been scheduled away before the
947 * cross-call arrived.
948 */
949 if (counter->state == PERF_COUNTER_STATE_ERROR)
950 counter->state = PERF_COUNTER_STATE_OFF;
951
952 retry:
953 spin_unlock_irq(&ctx->lock);
954 task_oncpu_function_call(task, __perf_counter_enable, counter);
955
956 spin_lock_irq(&ctx->lock);
957
958 /*
959 * If the context is active and the counter is still off,
960 * we need to retry the cross-call.
961 */
962 if (ctx->is_active && counter->state == PERF_COUNTER_STATE_OFF)
963 goto retry;
964
965 /*
966 * Since we have the lock this context can't be scheduled
967 * in, so we can change the state safely.
968 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100969 if (counter->state == PERF_COUNTER_STATE_OFF) {
Paul Mackerrasd859e292009-01-17 18:10:22 +1100970 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200971 counter->tstamp_enabled =
972 ctx->time - counter->total_time_enabled;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100973 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100974 out:
975 spin_unlock_irq(&ctx->lock);
976}
977
Peter Zijlstra2023b352009-05-05 17:50:26 +0200978static int perf_counter_refresh(struct perf_counter *counter, int refresh)
Peter Zijlstra79f14642009-04-06 11:45:07 +0200979{
Peter Zijlstra2023b352009-05-05 17:50:26 +0200980 /*
981 * not supported on inherited counters
982 */
Peter Zijlstra0d486962009-06-02 19:22:16 +0200983 if (counter->attr.inherit)
Peter Zijlstra2023b352009-05-05 17:50:26 +0200984 return -EINVAL;
985
Peter Zijlstra79f14642009-04-06 11:45:07 +0200986 atomic_add(refresh, &counter->event_limit);
987 perf_counter_enable(counter);
Peter Zijlstra2023b352009-05-05 17:50:26 +0200988
989 return 0;
Peter Zijlstra79f14642009-04-06 11:45:07 +0200990}
991
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100992void __perf_counter_sched_out(struct perf_counter_context *ctx,
993 struct perf_cpu_context *cpuctx)
994{
995 struct perf_counter *counter;
996
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100997 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100998 ctx->is_active = 0;
999 if (likely(!ctx->nr_counters))
1000 goto out;
Peter Zijlstra4af49982009-04-06 11:45:10 +02001001 update_context_time(ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001002
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001003 perf_disable();
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001004 if (ctx->nr_active) {
Peter Zijlstraafedadf2009-05-20 12:21:22 +02001005 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1006 if (counter != counter->group_leader)
1007 counter_sched_out(counter, cpuctx, ctx);
1008 else
1009 group_sched_out(counter, cpuctx, ctx);
1010 }
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001011 }
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001012 perf_enable();
Paul Mackerrasd859e292009-01-17 18:10:22 +11001013 out:
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001014 spin_unlock(&ctx->lock);
1015}
1016
Thomas Gleixner0793a612008-12-04 20:12:29 +01001017/*
Paul Mackerras564c2b22009-05-22 14:27:22 +10001018 * Test whether two contexts are equivalent, i.e. whether they
1019 * have both been cloned from the same version of the same context
1020 * and they both have the same number of enabled counters.
1021 * If the number of enabled counters is the same, then the set
1022 * of enabled counters should be the same, because these are both
1023 * inherited contexts, therefore we can't access individual counters
1024 * in them directly with an fd; we can only enable/disable all
1025 * counters via prctl, or enable/disable all counters in a family
1026 * via ioctl, which will have the same effect on both contexts.
1027 */
1028static int context_equiv(struct perf_counter_context *ctx1,
1029 struct perf_counter_context *ctx2)
1030{
1031 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001032 && ctx1->parent_gen == ctx2->parent_gen
Paul Mackerras25346b92009-06-01 17:48:12 +10001033 && !ctx1->pin_count && !ctx2->pin_count;
Paul Mackerras564c2b22009-05-22 14:27:22 +10001034}
1035
Peter Zijlstrabfbd3382009-06-24 21:11:59 +02001036static void __perf_counter_read(void *counter);
1037
1038static void __perf_counter_sync_stat(struct perf_counter *counter,
1039 struct perf_counter *next_counter)
1040{
1041 u64 value;
1042
1043 if (!counter->attr.inherit_stat)
1044 return;
1045
1046 /*
1047 * Update the counter value, we cannot use perf_counter_read()
1048 * because we're in the middle of a context switch and have IRQs
1049 * disabled, which upsets smp_call_function_single(), however
1050 * we know the counter must be on the current CPU, therefore we
1051 * don't need to use it.
1052 */
1053 switch (counter->state) {
1054 case PERF_COUNTER_STATE_ACTIVE:
1055 __perf_counter_read(counter);
1056 break;
1057
1058 case PERF_COUNTER_STATE_INACTIVE:
1059 update_counter_times(counter);
1060 break;
1061
1062 default:
1063 break;
1064 }
1065
1066 /*
1067 * In order to keep per-task stats reliable we need to flip the counter
1068 * values when we flip the contexts.
1069 */
1070 value = atomic64_read(&next_counter->count);
1071 value = atomic64_xchg(&counter->count, value);
1072 atomic64_set(&next_counter->count, value);
1073
Peter Zijlstra19d2e752009-06-26 13:10:23 +02001074 swap(counter->total_time_enabled, next_counter->total_time_enabled);
1075 swap(counter->total_time_running, next_counter->total_time_running);
1076
Peter Zijlstrabfbd3382009-06-24 21:11:59 +02001077 /*
Peter Zijlstra19d2e752009-06-26 13:10:23 +02001078 * Since we swizzled the values, update the user visible data too.
Peter Zijlstrabfbd3382009-06-24 21:11:59 +02001079 */
Peter Zijlstra19d2e752009-06-26 13:10:23 +02001080 perf_counter_update_userpage(counter);
1081 perf_counter_update_userpage(next_counter);
Peter Zijlstrabfbd3382009-06-24 21:11:59 +02001082}
1083
1084#define list_next_entry(pos, member) \
1085 list_entry(pos->member.next, typeof(*pos), member)
1086
1087static void perf_counter_sync_stat(struct perf_counter_context *ctx,
1088 struct perf_counter_context *next_ctx)
1089{
1090 struct perf_counter *counter, *next_counter;
1091
1092 if (!ctx->nr_stat)
1093 return;
1094
1095 counter = list_first_entry(&ctx->event_list,
1096 struct perf_counter, event_entry);
1097
1098 next_counter = list_first_entry(&next_ctx->event_list,
1099 struct perf_counter, event_entry);
1100
1101 while (&counter->event_entry != &ctx->event_list &&
1102 &next_counter->event_entry != &next_ctx->event_list) {
1103
1104 __perf_counter_sync_stat(counter, next_counter);
1105
1106 counter = list_next_entry(counter, event_entry);
Peter Zijlstra10545982009-08-06 18:06:26 +02001107 next_counter = list_next_entry(next_counter, event_entry);
Peter Zijlstrabfbd3382009-06-24 21:11:59 +02001108 }
1109}
1110
Paul Mackerras564c2b22009-05-22 14:27:22 +10001111/*
Thomas Gleixner0793a612008-12-04 20:12:29 +01001112 * Called from scheduler to remove the counters of the current task,
1113 * with interrupts disabled.
1114 *
1115 * We stop each counter and update the counter value in counter->count.
1116 *
Ingo Molnar76715812008-12-17 14:20:28 +01001117 * This does not protect us against NMI, but disable()
Thomas Gleixner0793a612008-12-04 20:12:29 +01001118 * sets the disabled bit in the control field of counter _before_
1119 * accessing the counter control register. If a NMI hits, then it will
1120 * not restart the counter.
1121 */
Paul Mackerras564c2b22009-05-22 14:27:22 +10001122void perf_counter_task_sched_out(struct task_struct *task,
1123 struct task_struct *next, int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001124{
1125 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001126 struct perf_counter_context *ctx = task->perf_counter_ctxp;
Paul Mackerras564c2b22009-05-22 14:27:22 +10001127 struct perf_counter_context *next_ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001128 struct perf_counter_context *parent;
Peter Zijlstra4a0deca2009-03-19 20:26:12 +01001129 struct pt_regs *regs;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001130 int do_switch = 1;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001131
Peter Zijlstra10989fb2009-05-25 14:45:28 +02001132 regs = task_pt_regs(task);
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +02001133 perf_swcounter_event(PERF_COUNT_SW_CONTEXT_SWITCHES, 1, 1, regs, 0);
Peter Zijlstra10989fb2009-05-25 14:45:28 +02001134
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001135 if (likely(!ctx || !cpuctx->task_ctx))
Thomas Gleixner0793a612008-12-04 20:12:29 +01001136 return;
1137
Peter Zijlstrabce379b2009-04-06 11:45:13 +02001138 update_context_time(ctx);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001139
1140 rcu_read_lock();
1141 parent = rcu_dereference(ctx->parent_ctx);
Paul Mackerras564c2b22009-05-22 14:27:22 +10001142 next_ctx = next->perf_counter_ctxp;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001143 if (parent && next_ctx &&
1144 rcu_dereference(next_ctx->parent_ctx) == parent) {
1145 /*
1146 * Looks like the two contexts are clones, so we might be
1147 * able to optimize the context switch. We lock both
1148 * contexts and check that they are clones under the
1149 * lock (including re-checking that neither has been
1150 * uncloned in the meantime). It doesn't matter which
1151 * order we take the locks because no other cpu could
1152 * be trying to lock both of these tasks.
1153 */
1154 spin_lock(&ctx->lock);
1155 spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
1156 if (context_equiv(ctx, next_ctx)) {
Peter Zijlstra665c2142009-05-29 14:51:57 +02001157 /*
1158 * XXX do we need a memory barrier of sorts
1159 * wrt to rcu_dereference() of perf_counter_ctxp
1160 */
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001161 task->perf_counter_ctxp = next_ctx;
1162 next->perf_counter_ctxp = ctx;
1163 ctx->task = next;
1164 next_ctx->task = task;
1165 do_switch = 0;
Peter Zijlstrabfbd3382009-06-24 21:11:59 +02001166
1167 perf_counter_sync_stat(ctx, next_ctx);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001168 }
1169 spin_unlock(&next_ctx->lock);
1170 spin_unlock(&ctx->lock);
Paul Mackerras564c2b22009-05-22 14:27:22 +10001171 }
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001172 rcu_read_unlock();
Paul Mackerras564c2b22009-05-22 14:27:22 +10001173
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001174 if (do_switch) {
1175 __perf_counter_sched_out(ctx, cpuctx);
1176 cpuctx->task_ctx = NULL;
1177 }
Thomas Gleixner0793a612008-12-04 20:12:29 +01001178}
1179
Peter Zijlstra665c2142009-05-29 14:51:57 +02001180/*
1181 * Called with IRQs disabled
1182 */
Paul Mackerrasa08b1592009-05-11 15:46:10 +10001183static void __perf_counter_task_sched_out(struct perf_counter_context *ctx)
1184{
1185 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1186
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001187 if (!cpuctx->task_ctx)
1188 return;
Ingo Molnar012b84d2009-05-17 11:08:41 +02001189
1190 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
1191 return;
1192
Paul Mackerrasa08b1592009-05-11 15:46:10 +10001193 __perf_counter_sched_out(ctx, cpuctx);
1194 cpuctx->task_ctx = NULL;
1195}
1196
Peter Zijlstra665c2142009-05-29 14:51:57 +02001197/*
1198 * Called with IRQs disabled
1199 */
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001200static void perf_counter_cpu_sched_out(struct perf_cpu_context *cpuctx)
Ingo Molnar04289bb2008-12-11 08:38:42 +01001201{
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001202 __perf_counter_sched_out(&cpuctx->ctx, cpuctx);
Ingo Molnar04289bb2008-12-11 08:38:42 +01001203}
1204
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001205static void
1206__perf_counter_sched_in(struct perf_counter_context *ctx,
1207 struct perf_cpu_context *cpuctx, int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001208{
Thomas Gleixner0793a612008-12-04 20:12:29 +01001209 struct perf_counter *counter;
Paul Mackerrasdd0e6ba2009-01-12 15:11:00 +11001210 int can_add_hw = 1;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001211
Thomas Gleixner0793a612008-12-04 20:12:29 +01001212 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001213 ctx->is_active = 1;
1214 if (likely(!ctx->nr_counters))
1215 goto out;
1216
Peter Zijlstra4af49982009-04-06 11:45:10 +02001217 ctx->timestamp = perf_clock();
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001218
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001219 perf_disable();
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001220
1221 /*
1222 * First go through the list and put on any pinned groups
1223 * in order to give them the best chance of going on.
1224 */
Ingo Molnar04289bb2008-12-11 08:38:42 +01001225 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001226 if (counter->state <= PERF_COUNTER_STATE_OFF ||
Peter Zijlstra0d486962009-06-02 19:22:16 +02001227 !counter->attr.pinned)
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001228 continue;
1229 if (counter->cpu != -1 && counter->cpu != cpu)
1230 continue;
1231
Peter Zijlstraafedadf2009-05-20 12:21:22 +02001232 if (counter != counter->group_leader)
1233 counter_sched_in(counter, cpuctx, ctx, cpu);
1234 else {
1235 if (group_can_go_on(counter, cpuctx, 1))
1236 group_sched_in(counter, cpuctx, ctx, cpu);
1237 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001238
1239 /*
1240 * If this pinned group hasn't been scheduled,
1241 * put it in error state.
1242 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001243 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
1244 update_group_times(counter);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001245 counter->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001246 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001247 }
1248
1249 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1250 /*
1251 * Ignore counters in OFF or ERROR state, and
1252 * ignore pinned counters since we did them already.
1253 */
1254 if (counter->state <= PERF_COUNTER_STATE_OFF ||
Peter Zijlstra0d486962009-06-02 19:22:16 +02001255 counter->attr.pinned)
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001256 continue;
1257
Ingo Molnar04289bb2008-12-11 08:38:42 +01001258 /*
1259 * Listen to the 'cpu' scheduling filter constraint
1260 * of counters:
1261 */
Thomas Gleixner0793a612008-12-04 20:12:29 +01001262 if (counter->cpu != -1 && counter->cpu != cpu)
1263 continue;
1264
Peter Zijlstraafedadf2009-05-20 12:21:22 +02001265 if (counter != counter->group_leader) {
1266 if (counter_sched_in(counter, cpuctx, ctx, cpu))
Paul Mackerrasdd0e6ba2009-01-12 15:11:00 +11001267 can_add_hw = 0;
Peter Zijlstraafedadf2009-05-20 12:21:22 +02001268 } else {
1269 if (group_can_go_on(counter, cpuctx, can_add_hw)) {
1270 if (group_sched_in(counter, cpuctx, ctx, cpu))
1271 can_add_hw = 0;
1272 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001273 }
Thomas Gleixner0793a612008-12-04 20:12:29 +01001274 }
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001275 perf_enable();
Paul Mackerrasd859e292009-01-17 18:10:22 +11001276 out:
Thomas Gleixner0793a612008-12-04 20:12:29 +01001277 spin_unlock(&ctx->lock);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001278}
Ingo Molnar04289bb2008-12-11 08:38:42 +01001279
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001280/*
1281 * Called from scheduler to add the counters of the current task
1282 * with interrupts disabled.
1283 *
1284 * We restore the counter value and then enable it.
1285 *
1286 * This does not protect us against NMI, but enable()
1287 * sets the enabled bit in the control field of counter _before_
1288 * accessing the counter control register. If a NMI hits, then it will
1289 * keep the counter running.
1290 */
1291void perf_counter_task_sched_in(struct task_struct *task, int cpu)
1292{
1293 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001294 struct perf_counter_context *ctx = task->perf_counter_ctxp;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001295
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001296 if (likely(!ctx))
1297 return;
Paul Mackerras564c2b22009-05-22 14:27:22 +10001298 if (cpuctx->task_ctx == ctx)
1299 return;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001300 __perf_counter_sched_in(ctx, cpuctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001301 cpuctx->task_ctx = ctx;
1302}
1303
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001304static void perf_counter_cpu_sched_in(struct perf_cpu_context *cpuctx, int cpu)
1305{
1306 struct perf_counter_context *ctx = &cpuctx->ctx;
1307
1308 __perf_counter_sched_in(ctx, cpuctx, cpu);
1309}
1310
Peter Zijlstraa78ac322009-05-25 17:39:05 +02001311#define MAX_INTERRUPTS (~0ULL)
1312
1313static void perf_log_throttle(struct perf_counter *counter, int enable);
Peter Zijlstra26b119b2009-05-20 12:21:20 +02001314
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001315static void perf_adjust_period(struct perf_counter *counter, u64 events)
1316{
1317 struct hw_perf_counter *hwc = &counter->hw;
1318 u64 period, sample_period;
1319 s64 delta;
1320
1321 events *= hwc->sample_period;
1322 period = div64_u64(events, counter->attr.sample_freq);
1323
1324 delta = (s64)(period - hwc->sample_period);
1325 delta = (delta + 7) / 8; /* low pass filter */
1326
1327 sample_period = hwc->sample_period + delta;
1328
1329 if (!sample_period)
1330 sample_period = 1;
1331
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001332 hwc->sample_period = sample_period;
1333}
1334
1335static void perf_ctx_adjust_freq(struct perf_counter_context *ctx)
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001336{
1337 struct perf_counter *counter;
Peter Zijlstra6a24ed6c2009-06-05 18:01:29 +02001338 struct hw_perf_counter *hwc;
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001339 u64 interrupts, freq;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001340
1341 spin_lock(&ctx->lock);
1342 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1343 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
1344 continue;
1345
Peter Zijlstra6a24ed6c2009-06-05 18:01:29 +02001346 hwc = &counter->hw;
1347
1348 interrupts = hwc->interrupts;
1349 hwc->interrupts = 0;
Peter Zijlstraa78ac322009-05-25 17:39:05 +02001350
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001351 /*
1352 * unthrottle counters on the tick
1353 */
Peter Zijlstraa78ac322009-05-25 17:39:05 +02001354 if (interrupts == MAX_INTERRUPTS) {
1355 perf_log_throttle(counter, 1);
1356 counter->pmu->unthrottle(counter);
Peter Zijlstradf58ab22009-06-11 11:25:05 +02001357 interrupts = 2*sysctl_perf_counter_sample_rate/HZ;
Peter Zijlstraa78ac322009-05-25 17:39:05 +02001358 }
1359
Peter Zijlstra0d486962009-06-02 19:22:16 +02001360 if (!counter->attr.freq || !counter->attr.sample_freq)
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001361 continue;
1362
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001363 /*
1364 * if the specified freq < HZ then we need to skip ticks
1365 */
Peter Zijlstra6a24ed6c2009-06-05 18:01:29 +02001366 if (counter->attr.sample_freq < HZ) {
1367 freq = counter->attr.sample_freq;
1368
1369 hwc->freq_count += freq;
1370 hwc->freq_interrupts += interrupts;
1371
1372 if (hwc->freq_count < HZ)
1373 continue;
1374
1375 interrupts = hwc->freq_interrupts;
1376 hwc->freq_interrupts = 0;
1377 hwc->freq_count -= HZ;
1378 } else
1379 freq = HZ;
1380
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001381 perf_adjust_period(counter, freq * interrupts);
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001382
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001383 /*
1384 * In order to avoid being stalled by an (accidental) huge
1385 * sample period, force reset the sample period if we didn't
1386 * get any events in this freq period.
1387 */
1388 if (!interrupts) {
1389 perf_disable();
1390 counter->pmu->disable(counter);
Paul Mackerras87847b82009-06-13 17:06:50 +10001391 atomic64_set(&hwc->period_left, 0);
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001392 counter->pmu->enable(counter);
1393 perf_enable();
1394 }
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001395 }
1396 spin_unlock(&ctx->lock);
1397}
1398
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001399/*
1400 * Round-robin a context's counters:
1401 */
1402static void rotate_ctx(struct perf_counter_context *ctx)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001403{
Thomas Gleixner0793a612008-12-04 20:12:29 +01001404 struct perf_counter *counter;
1405
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001406 if (!ctx->nr_counters)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001407 return;
1408
Thomas Gleixner0793a612008-12-04 20:12:29 +01001409 spin_lock(&ctx->lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001410 /*
Ingo Molnar04289bb2008-12-11 08:38:42 +01001411 * Rotate the first entry last (works just fine for group counters too):
Thomas Gleixner0793a612008-12-04 20:12:29 +01001412 */
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001413 perf_disable();
Ingo Molnar04289bb2008-12-11 08:38:42 +01001414 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Peter Zijlstra75564232009-03-13 12:21:29 +01001415 list_move_tail(&counter->list_entry, &ctx->counter_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001416 break;
1417 }
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001418 perf_enable();
Thomas Gleixner0793a612008-12-04 20:12:29 +01001419
1420 spin_unlock(&ctx->lock);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001421}
Thomas Gleixner0793a612008-12-04 20:12:29 +01001422
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001423void perf_counter_task_tick(struct task_struct *curr, int cpu)
1424{
Peter Zijlstra7fc23a52009-05-08 18:52:21 +02001425 struct perf_cpu_context *cpuctx;
1426 struct perf_counter_context *ctx;
1427
1428 if (!atomic_read(&nr_counters))
1429 return;
1430
1431 cpuctx = &per_cpu(perf_cpu_context, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001432 ctx = curr->perf_counter_ctxp;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001433
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001434 perf_ctx_adjust_freq(&cpuctx->ctx);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001435 if (ctx)
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02001436 perf_ctx_adjust_freq(ctx);
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001437
Ingo Molnarb82914c2009-05-04 18:54:32 +02001438 perf_counter_cpu_sched_out(cpuctx);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001439 if (ctx)
1440 __perf_counter_task_sched_out(ctx);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001441
Ingo Molnarb82914c2009-05-04 18:54:32 +02001442 rotate_ctx(&cpuctx->ctx);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001443 if (ctx)
1444 rotate_ctx(ctx);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001445
Ingo Molnarb82914c2009-05-04 18:54:32 +02001446 perf_counter_cpu_sched_in(cpuctx, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001447 if (ctx)
1448 perf_counter_task_sched_in(curr, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001449}
1450
1451/*
Paul Mackerras57e79862009-06-30 16:07:19 +10001452 * Enable all of a task's counters that have been marked enable-on-exec.
1453 * This expects task == current.
1454 */
1455static void perf_counter_enable_on_exec(struct task_struct *task)
1456{
1457 struct perf_counter_context *ctx;
1458 struct perf_counter *counter;
1459 unsigned long flags;
1460 int enabled = 0;
1461
1462 local_irq_save(flags);
1463 ctx = task->perf_counter_ctxp;
1464 if (!ctx || !ctx->nr_counters)
1465 goto out;
1466
1467 __perf_counter_task_sched_out(ctx);
1468
1469 spin_lock(&ctx->lock);
1470
1471 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1472 if (!counter->attr.enable_on_exec)
1473 continue;
1474 counter->attr.enable_on_exec = 0;
1475 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
1476 continue;
1477 counter->state = PERF_COUNTER_STATE_INACTIVE;
1478 counter->tstamp_enabled =
1479 ctx->time - counter->total_time_enabled;
1480 enabled = 1;
1481 }
1482
1483 /*
1484 * Unclone this context if we enabled any counter.
1485 */
Peter Zijlstra71a851b2009-07-10 09:06:56 +02001486 if (enabled)
1487 unclone_ctx(ctx);
Paul Mackerras57e79862009-06-30 16:07:19 +10001488
1489 spin_unlock(&ctx->lock);
1490
1491 perf_counter_task_sched_in(task, smp_processor_id());
1492 out:
1493 local_irq_restore(flags);
1494}
1495
1496/*
Thomas Gleixner0793a612008-12-04 20:12:29 +01001497 * Cross CPU call to read the hardware counter
1498 */
Peter Zijlstrabfbd3382009-06-24 21:11:59 +02001499static void __perf_counter_read(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001500{
Ingo Molnar621a01e2008-12-11 12:46:46 +01001501 struct perf_counter *counter = info;
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001502 struct perf_counter_context *ctx = counter->ctx;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001503 unsigned long flags;
Ingo Molnar621a01e2008-12-11 12:46:46 +01001504
Peter Zijlstra849691a2009-04-06 11:45:12 +02001505 local_irq_save(flags);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001506 if (ctx->is_active)
Peter Zijlstra4af49982009-04-06 11:45:10 +02001507 update_context_time(ctx);
Robert Richter4aeb0b42009-04-29 12:47:03 +02001508 counter->pmu->read(counter);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001509 update_counter_times(counter);
Peter Zijlstra849691a2009-04-06 11:45:12 +02001510 local_irq_restore(flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001511}
1512
Ingo Molnar04289bb2008-12-11 08:38:42 +01001513static u64 perf_counter_read(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001514{
1515 /*
1516 * If counter is enabled and currently active on a CPU, update the
1517 * value in the counter structure:
1518 */
Ingo Molnar6a930702008-12-11 15:17:03 +01001519 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
Thomas Gleixner0793a612008-12-04 20:12:29 +01001520 smp_call_function_single(counter->oncpu,
Peter Zijlstrabfbd3382009-06-24 21:11:59 +02001521 __perf_counter_read, counter, 1);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001522 } else if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
1523 update_counter_times(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001524 }
1525
Ingo Molnaree060942008-12-13 09:00:03 +01001526 return atomic64_read(&counter->count);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001527}
1528
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001529/*
1530 * Initialize the perf_counter context in a task_struct:
1531 */
1532static void
1533__perf_counter_init_context(struct perf_counter_context *ctx,
1534 struct task_struct *task)
1535{
1536 memset(ctx, 0, sizeof(*ctx));
1537 spin_lock_init(&ctx->lock);
1538 mutex_init(&ctx->mutex);
1539 INIT_LIST_HEAD(&ctx->counter_list);
1540 INIT_LIST_HEAD(&ctx->event_list);
1541 atomic_set(&ctx->refcount, 1);
1542 ctx->task = task;
1543}
1544
Thomas Gleixner0793a612008-12-04 20:12:29 +01001545static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
1546{
Ingo Molnar22a4f652009-06-01 10:13:37 +02001547 struct perf_counter_context *ctx;
1548 struct perf_cpu_context *cpuctx;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001549 struct task_struct *task;
Paul Mackerras25346b92009-06-01 17:48:12 +10001550 unsigned long flags;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001551 int err;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001552
1553 /*
1554 * If cpu is not a wildcard then this is a percpu counter:
1555 */
1556 if (cpu != -1) {
1557 /* Must be root to operate on a CPU counter: */
Peter Zijlstra07647712009-06-11 11:18:36 +02001558 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
Thomas Gleixner0793a612008-12-04 20:12:29 +01001559 return ERR_PTR(-EACCES);
1560
1561 if (cpu < 0 || cpu > num_possible_cpus())
1562 return ERR_PTR(-EINVAL);
1563
1564 /*
1565 * We could be clever and allow to attach a counter to an
1566 * offline CPU and activate it when the CPU comes up, but
1567 * that's for later.
1568 */
1569 if (!cpu_isset(cpu, cpu_online_map))
1570 return ERR_PTR(-ENODEV);
1571
1572 cpuctx = &per_cpu(perf_cpu_context, cpu);
1573 ctx = &cpuctx->ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001574 get_ctx(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001575
Thomas Gleixner0793a612008-12-04 20:12:29 +01001576 return ctx;
1577 }
1578
1579 rcu_read_lock();
1580 if (!pid)
1581 task = current;
1582 else
1583 task = find_task_by_vpid(pid);
1584 if (task)
1585 get_task_struct(task);
1586 rcu_read_unlock();
1587
1588 if (!task)
1589 return ERR_PTR(-ESRCH);
1590
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001591 /*
1592 * Can't attach counters to a dying task.
1593 */
1594 err = -ESRCH;
1595 if (task->flags & PF_EXITING)
1596 goto errout;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001597
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001598 /* Reuse ptrace permission checks for now. */
1599 err = -EACCES;
1600 if (!ptrace_may_access(task, PTRACE_MODE_READ))
1601 goto errout;
1602
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001603 retry:
Paul Mackerras25346b92009-06-01 17:48:12 +10001604 ctx = perf_lock_task_context(task, &flags);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001605 if (ctx) {
Peter Zijlstra71a851b2009-07-10 09:06:56 +02001606 unclone_ctx(ctx);
Paul Mackerras25346b92009-06-01 17:48:12 +10001607 spin_unlock_irqrestore(&ctx->lock, flags);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001608 }
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001609
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001610 if (!ctx) {
1611 ctx = kmalloc(sizeof(struct perf_counter_context), GFP_KERNEL);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001612 err = -ENOMEM;
1613 if (!ctx)
1614 goto errout;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001615 __perf_counter_init_context(ctx, task);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001616 get_ctx(ctx);
1617 if (cmpxchg(&task->perf_counter_ctxp, NULL, ctx)) {
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001618 /*
1619 * We raced with some other task; use
1620 * the context they set.
1621 */
1622 kfree(ctx);
Paul Mackerras25346b92009-06-01 17:48:12 +10001623 goto retry;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001624 }
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001625 get_task_struct(task);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001626 }
1627
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001628 put_task_struct(task);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001629 return ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001630
1631 errout:
1632 put_task_struct(task);
1633 return ERR_PTR(err);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001634}
1635
Peter Zijlstra592903c2009-03-13 12:21:36 +01001636static void free_counter_rcu(struct rcu_head *head)
1637{
1638 struct perf_counter *counter;
1639
1640 counter = container_of(head, struct perf_counter, rcu_head);
Peter Zijlstra709e50c2009-06-02 14:13:15 +02001641 if (counter->ns)
1642 put_pid_ns(counter->ns);
Peter Zijlstra592903c2009-03-13 12:21:36 +01001643 kfree(counter);
1644}
1645
Peter Zijlstra925d5192009-03-30 19:07:02 +02001646static void perf_pending_sync(struct perf_counter *counter);
1647
Peter Zijlstraf1600952009-03-19 20:26:16 +01001648static void free_counter(struct perf_counter *counter)
1649{
Peter Zijlstra925d5192009-03-30 19:07:02 +02001650 perf_pending_sync(counter);
1651
Peter Zijlstraf3440112009-06-22 13:58:35 +02001652 if (!counter->parent) {
1653 atomic_dec(&nr_counters);
1654 if (counter->attr.mmap)
1655 atomic_dec(&nr_mmap_counters);
1656 if (counter->attr.comm)
1657 atomic_dec(&nr_comm_counters);
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02001658 if (counter->attr.task)
1659 atomic_dec(&nr_task_counters);
Peter Zijlstraf3440112009-06-22 13:58:35 +02001660 }
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02001661
Peter Zijlstrae077df42009-03-19 20:26:17 +01001662 if (counter->destroy)
1663 counter->destroy(counter);
1664
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001665 put_ctx(counter->ctx);
Peter Zijlstraf1600952009-03-19 20:26:16 +01001666 call_rcu(&counter->rcu_head, free_counter_rcu);
1667}
1668
Thomas Gleixner0793a612008-12-04 20:12:29 +01001669/*
1670 * Called when the last reference to the file is gone.
1671 */
1672static int perf_release(struct inode *inode, struct file *file)
1673{
1674 struct perf_counter *counter = file->private_data;
1675 struct perf_counter_context *ctx = counter->ctx;
1676
1677 file->private_data = NULL;
1678
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001679 WARN_ON_ONCE(ctx->parent_ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001680 mutex_lock(&ctx->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01001681 perf_counter_remove_from_context(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001682 mutex_unlock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001683
Peter Zijlstra082ff5a2009-05-23 18:29:00 +02001684 mutex_lock(&counter->owner->perf_counter_mutex);
1685 list_del_init(&counter->owner_entry);
1686 mutex_unlock(&counter->owner->perf_counter_mutex);
1687 put_task_struct(counter->owner);
1688
Peter Zijlstraf1600952009-03-19 20:26:16 +01001689 free_counter(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001690
1691 return 0;
1692}
1693
Peter Zijlstrae53c0992009-07-24 14:42:10 +02001694static u64 perf_counter_read_tree(struct perf_counter *counter)
1695{
1696 struct perf_counter *child;
1697 u64 total = 0;
1698
1699 total += perf_counter_read(counter);
1700 list_for_each_entry(child, &counter->child_list, child_list)
1701 total += perf_counter_read(child);
1702
1703 return total;
1704}
1705
Thomas Gleixner0793a612008-12-04 20:12:29 +01001706/*
1707 * Read the performance counter - simple non blocking version for now
1708 */
1709static ssize_t
1710perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
1711{
Marti Raudseppd5e8da62009-06-13 02:35:01 +03001712 u64 values[4];
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001713 int n;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001714
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001715 /*
1716 * Return end-of-file for a read on a counter that is in
1717 * error state (i.e. because it was pinned but it couldn't be
1718 * scheduled on to the CPU at some point).
1719 */
1720 if (counter->state == PERF_COUNTER_STATE_ERROR)
1721 return 0;
1722
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001723 WARN_ON_ONCE(counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001724 mutex_lock(&counter->child_mutex);
Peter Zijlstrae53c0992009-07-24 14:42:10 +02001725 values[0] = perf_counter_read_tree(counter);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001726 n = 1;
Peter Zijlstra0d486962009-06-02 19:22:16 +02001727 if (counter->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001728 values[n++] = counter->total_time_enabled +
1729 atomic64_read(&counter->child_total_time_enabled);
Peter Zijlstra0d486962009-06-02 19:22:16 +02001730 if (counter->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001731 values[n++] = counter->total_time_running +
1732 atomic64_read(&counter->child_total_time_running);
Peter Zijlstra0d486962009-06-02 19:22:16 +02001733 if (counter->attr.read_format & PERF_FORMAT_ID)
Peter Zijlstra7f453c22009-07-21 13:19:40 +02001734 values[n++] = primary_counter_id(counter);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001735 mutex_unlock(&counter->child_mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001736
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001737 if (count < n * sizeof(u64))
1738 return -EINVAL;
1739 count = n * sizeof(u64);
1740
1741 if (copy_to_user(buf, values, count))
1742 return -EFAULT;
1743
1744 return count;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001745}
1746
1747static ssize_t
Thomas Gleixner0793a612008-12-04 20:12:29 +01001748perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
1749{
1750 struct perf_counter *counter = file->private_data;
1751
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001752 return perf_read_hw(counter, buf, count);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001753}
1754
1755static unsigned int perf_poll(struct file *file, poll_table *wait)
1756{
1757 struct perf_counter *counter = file->private_data;
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001758 struct perf_mmap_data *data;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001759 unsigned int events = POLL_HUP;
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001760
1761 rcu_read_lock();
1762 data = rcu_dereference(counter->data);
1763 if (data)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001764 events = atomic_xchg(&data->poll, 0);
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001765 rcu_read_unlock();
Thomas Gleixner0793a612008-12-04 20:12:29 +01001766
1767 poll_wait(file, &counter->waitq, wait);
1768
Thomas Gleixner0793a612008-12-04 20:12:29 +01001769 return events;
1770}
1771
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001772static void perf_counter_reset(struct perf_counter *counter)
1773{
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001774 (void)perf_counter_read(counter);
Paul Mackerras615a3f12009-05-11 15:50:21 +10001775 atomic64_set(&counter->count, 0);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001776 perf_counter_update_userpage(counter);
1777}
1778
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001779/*
1780 * Holding the top-level counter's child_mutex means that any
1781 * descendant process that has inherited this counter will block
1782 * in sync_child_counter if it goes to exit, thus satisfying the
1783 * task existence requirements of perf_counter_enable/disable.
1784 */
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001785static void perf_counter_for_each_child(struct perf_counter *counter,
1786 void (*func)(struct perf_counter *))
1787{
1788 struct perf_counter *child;
1789
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001790 WARN_ON_ONCE(counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001791 mutex_lock(&counter->child_mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001792 func(counter);
1793 list_for_each_entry(child, &counter->child_list, child_list)
1794 func(child);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001795 mutex_unlock(&counter->child_mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001796}
1797
1798static void perf_counter_for_each(struct perf_counter *counter,
1799 void (*func)(struct perf_counter *))
1800{
Peter Zijlstra75f937f2009-06-15 15:05:12 +02001801 struct perf_counter_context *ctx = counter->ctx;
1802 struct perf_counter *sibling;
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001803
Peter Zijlstra75f937f2009-06-15 15:05:12 +02001804 WARN_ON_ONCE(ctx->parent_ctx);
1805 mutex_lock(&ctx->mutex);
1806 counter = counter->group_leader;
1807
1808 perf_counter_for_each_child(counter, func);
1809 func(counter);
1810 list_for_each_entry(sibling, &counter->sibling_list, list_entry)
1811 perf_counter_for_each_child(counter, func);
1812 mutex_unlock(&ctx->mutex);
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001813}
1814
Peter Zijlstra08247e32009-06-02 16:46:57 +02001815static int perf_counter_period(struct perf_counter *counter, u64 __user *arg)
1816{
1817 struct perf_counter_context *ctx = counter->ctx;
1818 unsigned long size;
1819 int ret = 0;
1820 u64 value;
1821
Peter Zijlstra0d486962009-06-02 19:22:16 +02001822 if (!counter->attr.sample_period)
Peter Zijlstra08247e32009-06-02 16:46:57 +02001823 return -EINVAL;
1824
1825 size = copy_from_user(&value, arg, sizeof(value));
1826 if (size != sizeof(value))
1827 return -EFAULT;
1828
1829 if (!value)
1830 return -EINVAL;
1831
1832 spin_lock_irq(&ctx->lock);
Peter Zijlstra0d486962009-06-02 19:22:16 +02001833 if (counter->attr.freq) {
Peter Zijlstradf58ab22009-06-11 11:25:05 +02001834 if (value > sysctl_perf_counter_sample_rate) {
Peter Zijlstra08247e32009-06-02 16:46:57 +02001835 ret = -EINVAL;
1836 goto unlock;
1837 }
1838
Peter Zijlstra0d486962009-06-02 19:22:16 +02001839 counter->attr.sample_freq = value;
Peter Zijlstra08247e32009-06-02 16:46:57 +02001840 } else {
Peter Zijlstra0d486962009-06-02 19:22:16 +02001841 counter->attr.sample_period = value;
Peter Zijlstra08247e32009-06-02 16:46:57 +02001842 counter->hw.sample_period = value;
Peter Zijlstra08247e32009-06-02 16:46:57 +02001843 }
1844unlock:
1845 spin_unlock_irq(&ctx->lock);
1846
1847 return ret;
1848}
1849
Paul Mackerrasd859e292009-01-17 18:10:22 +11001850static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1851{
1852 struct perf_counter *counter = file->private_data;
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001853 void (*func)(struct perf_counter *);
1854 u32 flags = arg;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001855
1856 switch (cmd) {
1857 case PERF_COUNTER_IOC_ENABLE:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001858 func = perf_counter_enable;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001859 break;
1860 case PERF_COUNTER_IOC_DISABLE:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001861 func = perf_counter_disable;
Peter Zijlstra79f14642009-04-06 11:45:07 +02001862 break;
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001863 case PERF_COUNTER_IOC_RESET:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001864 func = perf_counter_reset;
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001865 break;
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001866
1867 case PERF_COUNTER_IOC_REFRESH:
1868 return perf_counter_refresh(counter, arg);
Peter Zijlstra08247e32009-06-02 16:46:57 +02001869
1870 case PERF_COUNTER_IOC_PERIOD:
1871 return perf_counter_period(counter, (u64 __user *)arg);
1872
Paul Mackerrasd859e292009-01-17 18:10:22 +11001873 default:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001874 return -ENOTTY;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001875 }
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001876
1877 if (flags & PERF_IOC_FLAG_GROUP)
1878 perf_counter_for_each(counter, func);
1879 else
1880 perf_counter_for_each_child(counter, func);
1881
1882 return 0;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001883}
1884
Peter Zijlstra771d7cd2009-05-25 14:45:26 +02001885int perf_counter_task_enable(void)
1886{
1887 struct perf_counter *counter;
1888
1889 mutex_lock(&current->perf_counter_mutex);
1890 list_for_each_entry(counter, &current->perf_counter_list, owner_entry)
1891 perf_counter_for_each_child(counter, perf_counter_enable);
1892 mutex_unlock(&current->perf_counter_mutex);
1893
1894 return 0;
1895}
1896
1897int perf_counter_task_disable(void)
1898{
1899 struct perf_counter *counter;
1900
1901 mutex_lock(&current->perf_counter_mutex);
1902 list_for_each_entry(counter, &current->perf_counter_list, owner_entry)
1903 perf_counter_for_each_child(counter, perf_counter_disable);
1904 mutex_unlock(&current->perf_counter_mutex);
1905
1906 return 0;
1907}
1908
Peter Zijlstra194002b2009-06-22 16:35:24 +02001909static int perf_counter_index(struct perf_counter *counter)
1910{
1911 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
1912 return 0;
1913
1914 return counter->hw.idx + 1 - PERF_COUNTER_INDEX_OFFSET;
1915}
1916
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001917/*
1918 * Callers need to ensure there can be no nesting of this function, otherwise
1919 * the seqlock logic goes bad. We can not serialize this because the arch
1920 * code calls this from NMI context.
1921 */
1922void perf_counter_update_userpage(struct perf_counter *counter)
Paul Mackerras37d81822009-03-23 18:22:08 +01001923{
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001924 struct perf_counter_mmap_page *userpg;
Ingo Molnar22a4f652009-06-01 10:13:37 +02001925 struct perf_mmap_data *data;
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001926
1927 rcu_read_lock();
1928 data = rcu_dereference(counter->data);
1929 if (!data)
1930 goto unlock;
1931
1932 userpg = data->user_page;
Paul Mackerras37d81822009-03-23 18:22:08 +01001933
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001934 /*
1935 * Disable preemption so as to not let the corresponding user-space
1936 * spin too long if we get preempted.
1937 */
1938 preempt_disable();
Paul Mackerras37d81822009-03-23 18:22:08 +01001939 ++userpg->lock;
Peter Zijlstra92f22a32009-04-02 11:12:04 +02001940 barrier();
Peter Zijlstra194002b2009-06-22 16:35:24 +02001941 userpg->index = perf_counter_index(counter);
Paul Mackerras37d81822009-03-23 18:22:08 +01001942 userpg->offset = atomic64_read(&counter->count);
1943 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
1944 userpg->offset -= atomic64_read(&counter->hw.prev_count);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001945
Peter Zijlstra7f8b4e42009-06-22 14:34:35 +02001946 userpg->time_enabled = counter->total_time_enabled +
1947 atomic64_read(&counter->child_total_time_enabled);
1948
1949 userpg->time_running = counter->total_time_running +
1950 atomic64_read(&counter->child_total_time_running);
1951
Peter Zijlstra92f22a32009-04-02 11:12:04 +02001952 barrier();
Paul Mackerras37d81822009-03-23 18:22:08 +01001953 ++userpg->lock;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001954 preempt_enable();
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001955unlock:
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001956 rcu_read_unlock();
Paul Mackerras37d81822009-03-23 18:22:08 +01001957}
1958
1959static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1960{
1961 struct perf_counter *counter = vma->vm_file->private_data;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001962 struct perf_mmap_data *data;
1963 int ret = VM_FAULT_SIGBUS;
Paul Mackerras37d81822009-03-23 18:22:08 +01001964
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01001965 if (vmf->flags & FAULT_FLAG_MKWRITE) {
1966 if (vmf->pgoff == 0)
1967 ret = 0;
1968 return ret;
1969 }
1970
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001971 rcu_read_lock();
1972 data = rcu_dereference(counter->data);
1973 if (!data)
1974 goto unlock;
Paul Mackerras37d81822009-03-23 18:22:08 +01001975
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001976 if (vmf->pgoff == 0) {
1977 vmf->page = virt_to_page(data->user_page);
1978 } else {
1979 int nr = vmf->pgoff - 1;
1980
1981 if ((unsigned)nr > data->nr_pages)
1982 goto unlock;
1983
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01001984 if (vmf->flags & FAULT_FLAG_WRITE)
1985 goto unlock;
1986
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001987 vmf->page = virt_to_page(data->data_pages[nr]);
1988 }
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01001989
Paul Mackerras37d81822009-03-23 18:22:08 +01001990 get_page(vmf->page);
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01001991 vmf->page->mapping = vma->vm_file->f_mapping;
1992 vmf->page->index = vmf->pgoff;
1993
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001994 ret = 0;
1995unlock:
1996 rcu_read_unlock();
1997
1998 return ret;
1999}
2000
2001static int perf_mmap_data_alloc(struct perf_counter *counter, int nr_pages)
2002{
2003 struct perf_mmap_data *data;
2004 unsigned long size;
2005 int i;
2006
2007 WARN_ON(atomic_read(&counter->mmap_count));
2008
2009 size = sizeof(struct perf_mmap_data);
2010 size += nr_pages * sizeof(void *);
2011
2012 data = kzalloc(size, GFP_KERNEL);
2013 if (!data)
2014 goto fail;
2015
2016 data->user_page = (void *)get_zeroed_page(GFP_KERNEL);
2017 if (!data->user_page)
2018 goto fail_user_page;
2019
2020 for (i = 0; i < nr_pages; i++) {
2021 data->data_pages[i] = (void *)get_zeroed_page(GFP_KERNEL);
2022 if (!data->data_pages[i])
2023 goto fail_data_pages;
2024 }
2025
2026 data->nr_pages = nr_pages;
Peter Zijlstra22c15582009-05-05 17:50:25 +02002027 atomic_set(&data->lock, -1);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002028
2029 rcu_assign_pointer(counter->data, data);
2030
Paul Mackerras37d81822009-03-23 18:22:08 +01002031 return 0;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002032
2033fail_data_pages:
2034 for (i--; i >= 0; i--)
2035 free_page((unsigned long)data->data_pages[i]);
2036
2037 free_page((unsigned long)data->user_page);
2038
2039fail_user_page:
2040 kfree(data);
2041
2042fail:
2043 return -ENOMEM;
2044}
2045
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002046static void perf_mmap_free_page(unsigned long addr)
2047{
Kevin Cernekee5bfd7562009-07-05 12:08:19 -07002048 struct page *page = virt_to_page((void *)addr);
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002049
2050 page->mapping = NULL;
2051 __free_page(page);
2052}
2053
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002054static void __perf_mmap_data_free(struct rcu_head *rcu_head)
2055{
Ingo Molnar22a4f652009-06-01 10:13:37 +02002056 struct perf_mmap_data *data;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002057 int i;
2058
Ingo Molnar22a4f652009-06-01 10:13:37 +02002059 data = container_of(rcu_head, struct perf_mmap_data, rcu_head);
2060
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002061 perf_mmap_free_page((unsigned long)data->user_page);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002062 for (i = 0; i < data->nr_pages; i++)
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002063 perf_mmap_free_page((unsigned long)data->data_pages[i]);
2064
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002065 kfree(data);
2066}
2067
2068static void perf_mmap_data_free(struct perf_counter *counter)
2069{
2070 struct perf_mmap_data *data = counter->data;
2071
2072 WARN_ON(atomic_read(&counter->mmap_count));
2073
2074 rcu_assign_pointer(counter->data, NULL);
2075 call_rcu(&data->rcu_head, __perf_mmap_data_free);
2076}
2077
2078static void perf_mmap_open(struct vm_area_struct *vma)
2079{
2080 struct perf_counter *counter = vma->vm_file->private_data;
2081
2082 atomic_inc(&counter->mmap_count);
2083}
2084
2085static void perf_mmap_close(struct vm_area_struct *vma)
2086{
2087 struct perf_counter *counter = vma->vm_file->private_data;
2088
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10002089 WARN_ON_ONCE(counter->ctx->parent_ctx);
Ingo Molnar22a4f652009-06-01 10:13:37 +02002090 if (atomic_dec_and_mutex_lock(&counter->mmap_count, &counter->mmap_mutex)) {
Peter Zijlstra789f90f2009-05-15 15:19:27 +02002091 struct user_struct *user = current_user();
2092
2093 atomic_long_sub(counter->data->nr_pages + 1, &user->locked_vm);
Peter Zijlstrac5078f72009-05-05 17:50:24 +02002094 vma->vm_mm->locked_vm -= counter->data->nr_locked;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002095 perf_mmap_data_free(counter);
2096 mutex_unlock(&counter->mmap_mutex);
2097 }
Paul Mackerras37d81822009-03-23 18:22:08 +01002098}
2099
2100static struct vm_operations_struct perf_mmap_vmops = {
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002101 .open = perf_mmap_open,
2102 .close = perf_mmap_close,
2103 .fault = perf_mmap_fault,
2104 .page_mkwrite = perf_mmap_fault,
Paul Mackerras37d81822009-03-23 18:22:08 +01002105};
2106
2107static int perf_mmap(struct file *file, struct vm_area_struct *vma)
2108{
2109 struct perf_counter *counter = file->private_data;
Ingo Molnar22a4f652009-06-01 10:13:37 +02002110 unsigned long user_locked, user_lock_limit;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02002111 struct user_struct *user = current_user();
Ingo Molnar22a4f652009-06-01 10:13:37 +02002112 unsigned long locked, lock_limit;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002113 unsigned long vma_size;
2114 unsigned long nr_pages;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02002115 long user_extra, extra;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002116 int ret = 0;
Paul Mackerras37d81822009-03-23 18:22:08 +01002117
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002118 if (!(vma->vm_flags & VM_SHARED))
Paul Mackerras37d81822009-03-23 18:22:08 +01002119 return -EINVAL;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002120
2121 vma_size = vma->vm_end - vma->vm_start;
2122 nr_pages = (vma_size / PAGE_SIZE) - 1;
2123
Peter Zijlstra7730d862009-03-25 12:48:31 +01002124 /*
2125 * If we have data pages ensure they're a power-of-two number, so we
2126 * can do bitmasks instead of modulo.
2127 */
2128 if (nr_pages != 0 && !is_power_of_2(nr_pages))
Paul Mackerras37d81822009-03-23 18:22:08 +01002129 return -EINVAL;
2130
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002131 if (vma_size != PAGE_SIZE * (1 + nr_pages))
Paul Mackerras37d81822009-03-23 18:22:08 +01002132 return -EINVAL;
2133
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002134 if (vma->vm_pgoff != 0)
2135 return -EINVAL;
Paul Mackerras37d81822009-03-23 18:22:08 +01002136
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10002137 WARN_ON_ONCE(counter->ctx->parent_ctx);
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02002138 mutex_lock(&counter->mmap_mutex);
2139 if (atomic_inc_not_zero(&counter->mmap_count)) {
2140 if (nr_pages != counter->data->nr_pages)
2141 ret = -EINVAL;
2142 goto unlock;
2143 }
2144
Peter Zijlstra789f90f2009-05-15 15:19:27 +02002145 user_extra = nr_pages + 1;
2146 user_lock_limit = sysctl_perf_counter_mlock >> (PAGE_SHIFT - 10);
Ingo Molnara3862d32009-05-24 09:02:37 +02002147
2148 /*
2149 * Increase the limit linearly with more CPUs:
2150 */
2151 user_lock_limit *= num_online_cpus();
2152
Peter Zijlstra789f90f2009-05-15 15:19:27 +02002153 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
Peter Zijlstrac5078f72009-05-05 17:50:24 +02002154
Peter Zijlstra789f90f2009-05-15 15:19:27 +02002155 extra = 0;
2156 if (user_locked > user_lock_limit)
2157 extra = user_locked - user_lock_limit;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002158
2159 lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
2160 lock_limit >>= PAGE_SHIFT;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02002161 locked = vma->vm_mm->locked_vm + extra;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002162
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02002163 if ((locked > lock_limit) && !capable(CAP_IPC_LOCK)) {
2164 ret = -EPERM;
2165 goto unlock;
2166 }
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002167
2168 WARN_ON(counter->data);
2169 ret = perf_mmap_data_alloc(counter, nr_pages);
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02002170 if (ret)
2171 goto unlock;
2172
2173 atomic_set(&counter->mmap_count, 1);
Peter Zijlstra789f90f2009-05-15 15:19:27 +02002174 atomic_long_add(user_extra, &user->locked_vm);
Peter Zijlstrac5078f72009-05-05 17:50:24 +02002175 vma->vm_mm->locked_vm += extra;
2176 counter->data->nr_locked = extra;
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002177 if (vma->vm_flags & VM_WRITE)
2178 counter->data->writable = 1;
2179
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02002180unlock:
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002181 mutex_unlock(&counter->mmap_mutex);
Paul Mackerras37d81822009-03-23 18:22:08 +01002182
Paul Mackerras37d81822009-03-23 18:22:08 +01002183 vma->vm_flags |= VM_RESERVED;
2184 vma->vm_ops = &perf_mmap_vmops;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002185
2186 return ret;
Paul Mackerras37d81822009-03-23 18:22:08 +01002187}
2188
Peter Zijlstra3c446b32009-04-06 11:45:01 +02002189static int perf_fasync(int fd, struct file *filp, int on)
2190{
Peter Zijlstra3c446b32009-04-06 11:45:01 +02002191 struct inode *inode = filp->f_path.dentry->d_inode;
Ingo Molnar22a4f652009-06-01 10:13:37 +02002192 struct perf_counter *counter = filp->private_data;
Peter Zijlstra3c446b32009-04-06 11:45:01 +02002193 int retval;
2194
2195 mutex_lock(&inode->i_mutex);
2196 retval = fasync_helper(fd, filp, on, &counter->fasync);
2197 mutex_unlock(&inode->i_mutex);
2198
2199 if (retval < 0)
2200 return retval;
2201
2202 return 0;
2203}
2204
Thomas Gleixner0793a612008-12-04 20:12:29 +01002205static const struct file_operations perf_fops = {
2206 .release = perf_release,
2207 .read = perf_read,
2208 .poll = perf_poll,
Paul Mackerrasd859e292009-01-17 18:10:22 +11002209 .unlocked_ioctl = perf_ioctl,
2210 .compat_ioctl = perf_ioctl,
Paul Mackerras37d81822009-03-23 18:22:08 +01002211 .mmap = perf_mmap,
Peter Zijlstra3c446b32009-04-06 11:45:01 +02002212 .fasync = perf_fasync,
Thomas Gleixner0793a612008-12-04 20:12:29 +01002213};
2214
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002215/*
Peter Zijlstra925d5192009-03-30 19:07:02 +02002216 * Perf counter wakeup
2217 *
2218 * If there's data, ensure we set the poll() state and publish everything
2219 * to user-space before waking everybody up.
2220 */
2221
2222void perf_counter_wakeup(struct perf_counter *counter)
2223{
Peter Zijlstra925d5192009-03-30 19:07:02 +02002224 wake_up_all(&counter->waitq);
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002225
2226 if (counter->pending_kill) {
2227 kill_fasync(&counter->fasync, SIGIO, counter->pending_kill);
2228 counter->pending_kill = 0;
2229 }
Peter Zijlstra925d5192009-03-30 19:07:02 +02002230}
2231
2232/*
2233 * Pending wakeups
2234 *
2235 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
2236 *
2237 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
2238 * single linked list and use cmpxchg() to add entries lockless.
2239 */
2240
Peter Zijlstra79f14642009-04-06 11:45:07 +02002241static void perf_pending_counter(struct perf_pending_entry *entry)
2242{
2243 struct perf_counter *counter = container_of(entry,
2244 struct perf_counter, pending);
2245
2246 if (counter->pending_disable) {
2247 counter->pending_disable = 0;
2248 perf_counter_disable(counter);
2249 }
2250
2251 if (counter->pending_wakeup) {
2252 counter->pending_wakeup = 0;
2253 perf_counter_wakeup(counter);
2254 }
2255}
2256
Peter Zijlstra671dec52009-04-06 11:45:02 +02002257#define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
Peter Zijlstra925d5192009-03-30 19:07:02 +02002258
Peter Zijlstra671dec52009-04-06 11:45:02 +02002259static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = {
Peter Zijlstra925d5192009-03-30 19:07:02 +02002260 PENDING_TAIL,
2261};
2262
Peter Zijlstra671dec52009-04-06 11:45:02 +02002263static void perf_pending_queue(struct perf_pending_entry *entry,
2264 void (*func)(struct perf_pending_entry *))
Peter Zijlstra925d5192009-03-30 19:07:02 +02002265{
Peter Zijlstra671dec52009-04-06 11:45:02 +02002266 struct perf_pending_entry **head;
Peter Zijlstra925d5192009-03-30 19:07:02 +02002267
Peter Zijlstra671dec52009-04-06 11:45:02 +02002268 if (cmpxchg(&entry->next, NULL, PENDING_TAIL) != NULL)
Peter Zijlstra925d5192009-03-30 19:07:02 +02002269 return;
2270
Peter Zijlstra671dec52009-04-06 11:45:02 +02002271 entry->func = func;
2272
2273 head = &get_cpu_var(perf_pending_head);
Peter Zijlstra925d5192009-03-30 19:07:02 +02002274
2275 do {
Peter Zijlstra671dec52009-04-06 11:45:02 +02002276 entry->next = *head;
2277 } while (cmpxchg(head, entry->next, entry) != entry->next);
Peter Zijlstra925d5192009-03-30 19:07:02 +02002278
2279 set_perf_counter_pending();
2280
Peter Zijlstra671dec52009-04-06 11:45:02 +02002281 put_cpu_var(perf_pending_head);
Peter Zijlstra925d5192009-03-30 19:07:02 +02002282}
2283
2284static int __perf_pending_run(void)
2285{
Peter Zijlstra671dec52009-04-06 11:45:02 +02002286 struct perf_pending_entry *list;
Peter Zijlstra925d5192009-03-30 19:07:02 +02002287 int nr = 0;
2288
Peter Zijlstra671dec52009-04-06 11:45:02 +02002289 list = xchg(&__get_cpu_var(perf_pending_head), PENDING_TAIL);
Peter Zijlstra925d5192009-03-30 19:07:02 +02002290 while (list != PENDING_TAIL) {
Peter Zijlstra671dec52009-04-06 11:45:02 +02002291 void (*func)(struct perf_pending_entry *);
2292 struct perf_pending_entry *entry = list;
Peter Zijlstra925d5192009-03-30 19:07:02 +02002293
2294 list = list->next;
2295
Peter Zijlstra671dec52009-04-06 11:45:02 +02002296 func = entry->func;
2297 entry->next = NULL;
Peter Zijlstra925d5192009-03-30 19:07:02 +02002298 /*
2299 * Ensure we observe the unqueue before we issue the wakeup,
2300 * so that we won't be waiting forever.
2301 * -- see perf_not_pending().
2302 */
2303 smp_wmb();
2304
Peter Zijlstra671dec52009-04-06 11:45:02 +02002305 func(entry);
Peter Zijlstra925d5192009-03-30 19:07:02 +02002306 nr++;
2307 }
2308
2309 return nr;
2310}
2311
2312static inline int perf_not_pending(struct perf_counter *counter)
2313{
2314 /*
2315 * If we flush on whatever cpu we run, there is a chance we don't
2316 * need to wait.
2317 */
2318 get_cpu();
2319 __perf_pending_run();
2320 put_cpu();
2321
2322 /*
2323 * Ensure we see the proper queue state before going to sleep
2324 * so that we do not miss the wakeup. -- see perf_pending_handle()
2325 */
2326 smp_rmb();
Peter Zijlstra671dec52009-04-06 11:45:02 +02002327 return counter->pending.next == NULL;
Peter Zijlstra925d5192009-03-30 19:07:02 +02002328}
2329
2330static void perf_pending_sync(struct perf_counter *counter)
2331{
2332 wait_event(counter->waitq, perf_not_pending(counter));
2333}
2334
2335void perf_counter_do_pending(void)
2336{
2337 __perf_pending_run();
2338}
2339
2340/*
Peter Zijlstra394ee072009-03-30 19:07:14 +02002341 * Callchain support -- arch specific
2342 */
2343
Peter Zijlstra9c03d882009-04-06 11:45:00 +02002344__weak struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
Peter Zijlstra394ee072009-03-30 19:07:14 +02002345{
2346 return NULL;
2347}
2348
2349/*
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002350 * Output
2351 */
2352
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002353struct perf_output_handle {
2354 struct perf_counter *counter;
2355 struct perf_mmap_data *data;
Peter Zijlstra8e3747c2009-06-02 16:16:02 +02002356 unsigned long head;
2357 unsigned long offset;
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002358 int nmi;
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002359 int sample;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002360 int locked;
2361 unsigned long flags;
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002362};
2363
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002364static bool perf_output_space(struct perf_mmap_data *data,
2365 unsigned int offset, unsigned int head)
2366{
2367 unsigned long tail;
2368 unsigned long mask;
2369
2370 if (!data->writable)
2371 return true;
2372
2373 mask = (data->nr_pages << PAGE_SHIFT) - 1;
2374 /*
2375 * Userspace could choose to issue a mb() before updating the tail
2376 * pointer. So that all reads will be completed before the write is
2377 * issued.
2378 */
2379 tail = ACCESS_ONCE(data->user_page->data_tail);
2380 smp_rmb();
2381
2382 offset = (offset - tail) & mask;
2383 head = (head - tail) & mask;
2384
2385 if ((int)(head - offset) < 0)
2386 return false;
2387
2388 return true;
2389}
2390
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002391static void perf_output_wakeup(struct perf_output_handle *handle)
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002392{
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002393 atomic_set(&handle->data->poll, POLL_IN);
2394
Peter Zijlstra671dec52009-04-06 11:45:02 +02002395 if (handle->nmi) {
Peter Zijlstra79f14642009-04-06 11:45:07 +02002396 handle->counter->pending_wakeup = 1;
Peter Zijlstra671dec52009-04-06 11:45:02 +02002397 perf_pending_queue(&handle->counter->pending,
Peter Zijlstra79f14642009-04-06 11:45:07 +02002398 perf_pending_counter);
Peter Zijlstra671dec52009-04-06 11:45:02 +02002399 } else
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002400 perf_counter_wakeup(handle->counter);
2401}
2402
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002403/*
2404 * Curious locking construct.
2405 *
2406 * We need to ensure a later event doesn't publish a head when a former
2407 * event isn't done writing. However since we need to deal with NMIs we
2408 * cannot fully serialize things.
2409 *
2410 * What we do is serialize between CPUs so we only have to deal with NMI
2411 * nesting on a single CPU.
2412 *
2413 * We only publish the head (and generate a wakeup) when the outer-most
2414 * event completes.
2415 */
2416static void perf_output_lock(struct perf_output_handle *handle)
2417{
2418 struct perf_mmap_data *data = handle->data;
2419 int cpu;
2420
2421 handle->locked = 0;
2422
2423 local_irq_save(handle->flags);
2424 cpu = smp_processor_id();
2425
2426 if (in_nmi() && atomic_read(&data->lock) == cpu)
2427 return;
2428
Peter Zijlstra22c15582009-05-05 17:50:25 +02002429 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002430 cpu_relax();
2431
2432 handle->locked = 1;
2433}
2434
2435static void perf_output_unlock(struct perf_output_handle *handle)
2436{
2437 struct perf_mmap_data *data = handle->data;
Peter Zijlstra8e3747c2009-06-02 16:16:02 +02002438 unsigned long head;
2439 int cpu;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002440
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002441 data->done_head = data->head;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002442
2443 if (!handle->locked)
2444 goto out;
2445
2446again:
2447 /*
2448 * The xchg implies a full barrier that ensures all writes are done
2449 * before we publish the new head, matched by a rmb() in userspace when
2450 * reading this position.
2451 */
Peter Zijlstra8e3747c2009-06-02 16:16:02 +02002452 while ((head = atomic_long_xchg(&data->done_head, 0)))
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002453 data->user_page->data_head = head;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002454
2455 /*
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002456 * NMI can happen here, which means we can miss a done_head update.
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002457 */
2458
Peter Zijlstra22c15582009-05-05 17:50:25 +02002459 cpu = atomic_xchg(&data->lock, -1);
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002460 WARN_ON_ONCE(cpu != smp_processor_id());
2461
2462 /*
2463 * Therefore we have to validate we did not indeed do so.
2464 */
Peter Zijlstra8e3747c2009-06-02 16:16:02 +02002465 if (unlikely(atomic_long_read(&data->done_head))) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002466 /*
2467 * Since we had it locked, we can lock it again.
2468 */
Peter Zijlstra22c15582009-05-05 17:50:25 +02002469 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002470 cpu_relax();
2471
2472 goto again;
2473 }
2474
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002475 if (atomic_xchg(&data->wakeup, 0))
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002476 perf_output_wakeup(handle);
2477out:
2478 local_irq_restore(handle->flags);
2479}
2480
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002481static void perf_output_copy(struct perf_output_handle *handle,
Peter Zijlstra089dd792009-06-05 14:04:55 +02002482 const void *buf, unsigned int len)
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002483{
2484 unsigned int pages_mask;
2485 unsigned int offset;
2486 unsigned int size;
2487 void **pages;
2488
2489 offset = handle->offset;
2490 pages_mask = handle->data->nr_pages - 1;
2491 pages = handle->data->data_pages;
2492
2493 do {
2494 unsigned int page_offset;
2495 int nr;
2496
2497 nr = (offset >> PAGE_SHIFT) & pages_mask;
2498 page_offset = offset & (PAGE_SIZE - 1);
2499 size = min_t(unsigned int, PAGE_SIZE - page_offset, len);
2500
2501 memcpy(pages[nr] + page_offset, buf, size);
2502
2503 len -= size;
2504 buf += size;
2505 offset += size;
2506 } while (len);
2507
2508 handle->offset = offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01002509
Peter Zijlstra53020fe2009-05-13 21:26:19 +02002510 /*
2511 * Check we didn't copy past our reservation window, taking the
2512 * possible unsigned int wrap into account.
2513 */
Peter Zijlstra8e3747c2009-06-02 16:16:02 +02002514 WARN_ON_ONCE(((long)(handle->head - handle->offset)) < 0);
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002515}
2516
Peter Zijlstra5c148192009-03-25 12:30:23 +01002517#define perf_output_put(handle, x) \
2518 perf_output_copy((handle), &(x), sizeof(x))
2519
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002520static int perf_output_begin(struct perf_output_handle *handle,
2521 struct perf_counter *counter, unsigned int size,
2522 int nmi, int sample)
2523{
2524 struct perf_mmap_data *data;
2525 unsigned int offset, head;
2526 int have_lost;
2527 struct {
2528 struct perf_event_header header;
2529 u64 id;
2530 u64 lost;
2531 } lost_event;
2532
2533 /*
2534 * For inherited counters we send all the output towards the parent.
2535 */
2536 if (counter->parent)
2537 counter = counter->parent;
2538
2539 rcu_read_lock();
2540 data = rcu_dereference(counter->data);
2541 if (!data)
2542 goto out;
2543
2544 handle->data = data;
2545 handle->counter = counter;
2546 handle->nmi = nmi;
2547 handle->sample = sample;
2548
2549 if (!data->nr_pages)
2550 goto fail;
2551
2552 have_lost = atomic_read(&data->lost);
2553 if (have_lost)
2554 size += sizeof(lost_event);
2555
2556 perf_output_lock(handle);
2557
2558 do {
2559 offset = head = atomic_long_read(&data->head);
2560 head += size;
2561 if (unlikely(!perf_output_space(data, offset, head)))
2562 goto fail;
2563 } while (atomic_long_cmpxchg(&data->head, offset, head) != offset);
2564
2565 handle->offset = offset;
2566 handle->head = head;
2567
2568 if ((offset >> PAGE_SHIFT) != (head >> PAGE_SHIFT))
2569 atomic_set(&data->wakeup, 1);
2570
2571 if (have_lost) {
2572 lost_event.header.type = PERF_EVENT_LOST;
2573 lost_event.header.misc = 0;
2574 lost_event.header.size = sizeof(lost_event);
2575 lost_event.id = counter->id;
2576 lost_event.lost = atomic_xchg(&data->lost, 0);
2577
2578 perf_output_put(handle, lost_event);
2579 }
2580
2581 return 0;
2582
2583fail:
2584 atomic_inc(&data->lost);
2585 perf_output_unlock(handle);
2586out:
2587 rcu_read_unlock();
2588
2589 return -ENOSPC;
2590}
2591
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002592static void perf_output_end(struct perf_output_handle *handle)
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002593{
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002594 struct perf_counter *counter = handle->counter;
2595 struct perf_mmap_data *data = handle->data;
2596
Peter Zijlstra0d486962009-06-02 19:22:16 +02002597 int wakeup_events = counter->attr.wakeup_events;
Peter Zijlstrac4578102009-04-02 11:12:01 +02002598
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01002599 if (handle->sample && wakeup_events) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002600 int events = atomic_inc_return(&data->events);
Peter Zijlstrac4578102009-04-02 11:12:01 +02002601 if (events >= wakeup_events) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002602 atomic_sub(wakeup_events, &data->events);
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002603 atomic_set(&data->wakeup, 1);
Peter Zijlstrac4578102009-04-02 11:12:01 +02002604 }
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002605 }
2606
2607 perf_output_unlock(handle);
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002608 rcu_read_unlock();
2609}
2610
Peter Zijlstra709e50c2009-06-02 14:13:15 +02002611static u32 perf_counter_pid(struct perf_counter *counter, struct task_struct *p)
2612{
2613 /*
2614 * only top level counters have the pid namespace they were created in
2615 */
2616 if (counter->parent)
2617 counter = counter->parent;
2618
2619 return task_tgid_nr_ns(p, counter->ns);
2620}
2621
2622static u32 perf_counter_tid(struct perf_counter *counter, struct task_struct *p)
2623{
2624 /*
2625 * only top level counters have the pid namespace they were created in
2626 */
2627 if (counter->parent)
2628 counter = counter->parent;
2629
2630 return task_pid_nr_ns(p, counter->ns);
2631}
2632
Peter Zijlstradf1a1322009-06-10 21:02:22 +02002633static void perf_counter_output(struct perf_counter *counter, int nmi,
2634 struct perf_sample_data *data)
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002635{
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002636 int ret;
Peter Zijlstra0d486962009-06-02 19:22:16 +02002637 u64 sample_type = counter->attr.sample_type;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002638 struct perf_output_handle handle;
2639 struct perf_event_header header;
2640 u64 ip;
Peter Zijlstra5c148192009-03-25 12:30:23 +01002641 struct {
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002642 u32 pid, tid;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002643 } tid_entry;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002644 struct {
Peter Zijlstra8e5799b2009-06-02 15:08:15 +02002645 u64 id;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002646 u64 counter;
2647 } group_entry;
Peter Zijlstra394ee072009-03-30 19:07:14 +02002648 struct perf_callchain_entry *callchain = NULL;
2649 int callchain_size = 0;
Peter Zijlstra339f7c92009-04-06 11:45:06 +02002650 u64 time;
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002651 struct {
2652 u32 cpu, reserved;
2653 } cpu_entry;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002654
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02002655 header.type = PERF_EVENT_SAMPLE;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002656 header.size = sizeof(header);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002657
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02002658 header.misc = 0;
Peter Zijlstradf1a1322009-06-10 21:02:22 +02002659 header.misc |= perf_misc_flags(data->regs);
Peter Zijlstra6fab0192009-04-08 15:01:26 +02002660
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002661 if (sample_type & PERF_SAMPLE_IP) {
Peter Zijlstradf1a1322009-06-10 21:02:22 +02002662 ip = perf_instruction_pointer(data->regs);
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002663 header.size += sizeof(ip);
2664 }
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002665
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002666 if (sample_type & PERF_SAMPLE_TID) {
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002667 /* namespace issues */
Peter Zijlstra709e50c2009-06-02 14:13:15 +02002668 tid_entry.pid = perf_counter_pid(counter, current);
2669 tid_entry.tid = perf_counter_tid(counter, current);
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002670
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002671 header.size += sizeof(tid_entry);
2672 }
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002673
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002674 if (sample_type & PERF_SAMPLE_TIME) {
Peter Zijlstra4d855452009-04-08 15:01:32 +02002675 /*
2676 * Maybe do better on x86 and provide cpu_clock_nmi()
2677 */
2678 time = sched_clock();
2679
Peter Zijlstra4d855452009-04-08 15:01:32 +02002680 header.size += sizeof(u64);
2681 }
2682
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02002683 if (sample_type & PERF_SAMPLE_ADDR)
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002684 header.size += sizeof(u64);
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002685
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02002686 if (sample_type & PERF_SAMPLE_ID)
Peter Zijlstraa85f61a2009-05-08 18:52:23 +02002687 header.size += sizeof(u64);
Peter Zijlstraa85f61a2009-05-08 18:52:23 +02002688
Peter Zijlstra7f453c22009-07-21 13:19:40 +02002689 if (sample_type & PERF_SAMPLE_STREAM_ID)
2690 header.size += sizeof(u64);
2691
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002692 if (sample_type & PERF_SAMPLE_CPU) {
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002693 header.size += sizeof(cpu_entry);
2694
2695 cpu_entry.cpu = raw_smp_processor_id();
Arjan van de Ven0dc3d522009-07-21 00:55:05 -07002696 cpu_entry.reserved = 0;
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002697 }
2698
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02002699 if (sample_type & PERF_SAMPLE_PERIOD)
Peter Zijlstra689802b2009-06-05 15:05:43 +02002700 header.size += sizeof(u64);
Peter Zijlstra689802b2009-06-05 15:05:43 +02002701
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002702 if (sample_type & PERF_SAMPLE_GROUP) {
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002703 header.size += sizeof(u64) +
2704 counter->nr_siblings * sizeof(group_entry);
2705 }
2706
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002707 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
Peter Zijlstradf1a1322009-06-10 21:02:22 +02002708 callchain = perf_callchain(data->regs);
Peter Zijlstra394ee072009-03-30 19:07:14 +02002709
2710 if (callchain) {
Peter Zijlstra9c03d882009-04-06 11:45:00 +02002711 callchain_size = (1 + callchain->nr) * sizeof(u64);
Peter Zijlstra394ee072009-03-30 19:07:14 +02002712 header.size += callchain_size;
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02002713 } else
2714 header.size += sizeof(u64);
Peter Zijlstra394ee072009-03-30 19:07:14 +02002715 }
2716
Frederic Weisbecker3a43ce62009-08-08 04:26:37 +02002717 if (sample_type & PERF_SAMPLE_RAW) {
Peter Zijlstraa0445602009-08-10 11:16:52 +02002718 int size = sizeof(u32);
2719
2720 if (data->raw)
2721 size += data->raw->size;
2722 else
2723 size += sizeof(u32);
2724
2725 WARN_ON_ONCE(size & (sizeof(u64)-1));
2726 header.size += size;
Frederic Weisbeckerf413cdb2009-08-07 01:25:54 +02002727 }
2728
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002729 ret = perf_output_begin(&handle, counter, header.size, nmi, 1);
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002730 if (ret)
2731 return;
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002732
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002733 perf_output_put(&handle, header);
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002734
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002735 if (sample_type & PERF_SAMPLE_IP)
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002736 perf_output_put(&handle, ip);
2737
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002738 if (sample_type & PERF_SAMPLE_TID)
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002739 perf_output_put(&handle, tid_entry);
2740
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002741 if (sample_type & PERF_SAMPLE_TIME)
Peter Zijlstra4d855452009-04-08 15:01:32 +02002742 perf_output_put(&handle, time);
2743
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002744 if (sample_type & PERF_SAMPLE_ADDR)
Peter Zijlstradf1a1322009-06-10 21:02:22 +02002745 perf_output_put(&handle, data->addr);
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002746
Peter Zijlstra7f453c22009-07-21 13:19:40 +02002747 if (sample_type & PERF_SAMPLE_ID) {
2748 u64 id = primary_counter_id(counter);
2749
2750 perf_output_put(&handle, id);
2751 }
2752
2753 if (sample_type & PERF_SAMPLE_STREAM_ID)
Peter Zijlstraac4bcf82009-06-05 14:44:52 +02002754 perf_output_put(&handle, counter->id);
Peter Zijlstraa85f61a2009-05-08 18:52:23 +02002755
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002756 if (sample_type & PERF_SAMPLE_CPU)
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002757 perf_output_put(&handle, cpu_entry);
2758
Peter Zijlstra689802b2009-06-05 15:05:43 +02002759 if (sample_type & PERF_SAMPLE_PERIOD)
Peter Zijlstra9e350de2009-06-10 21:34:59 +02002760 perf_output_put(&handle, data->period);
Peter Zijlstra689802b2009-06-05 15:05:43 +02002761
Peter Zijlstra2023b352009-05-05 17:50:26 +02002762 /*
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002763 * XXX PERF_SAMPLE_GROUP vs inherited counters seems difficult.
Peter Zijlstra2023b352009-05-05 17:50:26 +02002764 */
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002765 if (sample_type & PERF_SAMPLE_GROUP) {
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002766 struct perf_counter *leader, *sub;
2767 u64 nr = counter->nr_siblings;
2768
2769 perf_output_put(&handle, nr);
2770
2771 leader = counter->group_leader;
2772 list_for_each_entry(sub, &leader->sibling_list, list_entry) {
2773 if (sub != counter)
Robert Richter4aeb0b42009-04-29 12:47:03 +02002774 sub->pmu->read(sub);
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002775
Peter Zijlstra7f453c22009-07-21 13:19:40 +02002776 group_entry.id = primary_counter_id(sub);
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002777 group_entry.counter = atomic64_read(&sub->count);
2778
2779 perf_output_put(&handle, group_entry);
2780 }
2781 }
2782
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +02002783 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
2784 if (callchain)
2785 perf_output_copy(&handle, callchain, callchain_size);
2786 else {
2787 u64 nr = 0;
2788 perf_output_put(&handle, nr);
2789 }
2790 }
Peter Zijlstra394ee072009-03-30 19:07:14 +02002791
Peter Zijlstraa0445602009-08-10 11:16:52 +02002792 if (sample_type & PERF_SAMPLE_RAW) {
2793 if (data->raw) {
2794 perf_output_put(&handle, data->raw->size);
2795 perf_output_copy(&handle, data->raw->data, data->raw->size);
2796 } else {
2797 struct {
2798 u32 size;
2799 u32 data;
2800 } raw = {
2801 .size = sizeof(u32),
2802 .data = 0,
2803 };
2804 perf_output_put(&handle, raw);
2805 }
2806 }
Frederic Weisbeckerf413cdb2009-08-07 01:25:54 +02002807
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002808 perf_output_end(&handle);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002809}
2810
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002811/*
Peter Zijlstra38b200d2009-06-23 20:13:11 +02002812 * read event
2813 */
2814
2815struct perf_read_event {
2816 struct perf_event_header header;
2817
2818 u32 pid;
2819 u32 tid;
2820 u64 value;
2821 u64 format[3];
2822};
2823
2824static void
2825perf_counter_read_event(struct perf_counter *counter,
2826 struct task_struct *task)
2827{
2828 struct perf_output_handle handle;
2829 struct perf_read_event event = {
2830 .header = {
2831 .type = PERF_EVENT_READ,
2832 .misc = 0,
2833 .size = sizeof(event) - sizeof(event.format),
2834 },
2835 .pid = perf_counter_pid(counter, task),
2836 .tid = perf_counter_tid(counter, task),
2837 .value = atomic64_read(&counter->count),
2838 };
2839 int ret, i = 0;
2840
2841 if (counter->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
2842 event.header.size += sizeof(u64);
2843 event.format[i++] = counter->total_time_enabled;
2844 }
2845
2846 if (counter->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
2847 event.header.size += sizeof(u64);
2848 event.format[i++] = counter->total_time_running;
2849 }
2850
2851 if (counter->attr.read_format & PERF_FORMAT_ID) {
Peter Zijlstra38b200d2009-06-23 20:13:11 +02002852 event.header.size += sizeof(u64);
Peter Zijlstra7f453c22009-07-21 13:19:40 +02002853 event.format[i++] = primary_counter_id(counter);
Peter Zijlstra38b200d2009-06-23 20:13:11 +02002854 }
2855
2856 ret = perf_output_begin(&handle, counter, event.header.size, 0, 0);
2857 if (ret)
2858 return;
2859
2860 perf_output_copy(&handle, &event, event.header.size);
2861 perf_output_end(&handle);
2862}
2863
2864/*
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02002865 * task tracking -- fork/exit
2866 *
2867 * enabled by: attr.comm | attr.mmap | attr.task
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002868 */
2869
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02002870struct perf_task_event {
Peter Zijlstra3a80b4a2009-08-07 19:49:01 +02002871 struct task_struct *task;
2872 struct perf_counter_context *task_ctx;
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002873
2874 struct {
2875 struct perf_event_header header;
2876
2877 u32 pid;
2878 u32 ppid;
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02002879 u32 tid;
2880 u32 ptid;
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002881 } event;
2882};
2883
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02002884static void perf_counter_task_output(struct perf_counter *counter,
2885 struct perf_task_event *task_event)
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002886{
2887 struct perf_output_handle handle;
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02002888 int size = task_event->event.header.size;
2889 struct task_struct *task = task_event->task;
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002890 int ret = perf_output_begin(&handle, counter, size, 0, 0);
2891
2892 if (ret)
2893 return;
2894
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02002895 task_event->event.pid = perf_counter_pid(counter, task);
2896 task_event->event.ppid = perf_counter_pid(counter, task->real_parent);
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002897
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02002898 task_event->event.tid = perf_counter_tid(counter, task);
2899 task_event->event.ptid = perf_counter_tid(counter, task->real_parent);
2900
2901 perf_output_put(&handle, task_event->event);
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002902 perf_output_end(&handle);
2903}
2904
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02002905static int perf_counter_task_match(struct perf_counter *counter)
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002906{
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02002907 if (counter->attr.comm || counter->attr.mmap || counter->attr.task)
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002908 return 1;
2909
2910 return 0;
2911}
2912
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02002913static void perf_counter_task_ctx(struct perf_counter_context *ctx,
2914 struct perf_task_event *task_event)
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002915{
2916 struct perf_counter *counter;
2917
2918 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2919 return;
2920
2921 rcu_read_lock();
2922 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02002923 if (perf_counter_task_match(counter))
2924 perf_counter_task_output(counter, task_event);
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002925 }
2926 rcu_read_unlock();
2927}
2928
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02002929static void perf_counter_task_event(struct perf_task_event *task_event)
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002930{
2931 struct perf_cpu_context *cpuctx;
Peter Zijlstra3a80b4a2009-08-07 19:49:01 +02002932 struct perf_counter_context *ctx = task_event->task_ctx;
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002933
2934 cpuctx = &get_cpu_var(perf_cpu_context);
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02002935 perf_counter_task_ctx(&cpuctx->ctx, task_event);
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002936 put_cpu_var(perf_cpu_context);
2937
2938 rcu_read_lock();
Peter Zijlstra3a80b4a2009-08-07 19:49:01 +02002939 if (!ctx)
2940 ctx = rcu_dereference(task_event->task->perf_counter_ctxp);
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002941 if (ctx)
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02002942 perf_counter_task_ctx(ctx, task_event);
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002943 rcu_read_unlock();
2944}
2945
Peter Zijlstra3a80b4a2009-08-07 19:49:01 +02002946static void perf_counter_task(struct task_struct *task,
2947 struct perf_counter_context *task_ctx,
2948 int new)
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02002949{
2950 struct perf_task_event task_event;
2951
2952 if (!atomic_read(&nr_comm_counters) &&
2953 !atomic_read(&nr_mmap_counters) &&
2954 !atomic_read(&nr_task_counters))
2955 return;
2956
2957 task_event = (struct perf_task_event){
Peter Zijlstra3a80b4a2009-08-07 19:49:01 +02002958 .task = task,
2959 .task_ctx = task_ctx,
2960 .event = {
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02002961 .header = {
2962 .type = new ? PERF_EVENT_FORK : PERF_EVENT_EXIT,
2963 .misc = 0,
2964 .size = sizeof(task_event.event),
2965 },
2966 /* .pid */
2967 /* .ppid */
2968 /* .tid */
2969 /* .ptid */
2970 },
2971 };
2972
2973 perf_counter_task_event(&task_event);
2974}
2975
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002976void perf_counter_fork(struct task_struct *task)
2977{
Peter Zijlstra3a80b4a2009-08-07 19:49:01 +02002978 perf_counter_task(task, NULL, 1);
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002979}
2980
2981/*
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002982 * comm tracking
2983 */
2984
2985struct perf_comm_event {
Ingo Molnar22a4f652009-06-01 10:13:37 +02002986 struct task_struct *task;
2987 char *comm;
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002988 int comm_size;
2989
2990 struct {
2991 struct perf_event_header header;
2992
2993 u32 pid;
2994 u32 tid;
2995 } event;
2996};
2997
2998static void perf_counter_comm_output(struct perf_counter *counter,
2999 struct perf_comm_event *comm_event)
3000{
3001 struct perf_output_handle handle;
3002 int size = comm_event->event.header.size;
3003 int ret = perf_output_begin(&handle, counter, size, 0, 0);
3004
3005 if (ret)
3006 return;
3007
Peter Zijlstra709e50c2009-06-02 14:13:15 +02003008 comm_event->event.pid = perf_counter_pid(counter, comm_event->task);
3009 comm_event->event.tid = perf_counter_tid(counter, comm_event->task);
3010
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02003011 perf_output_put(&handle, comm_event->event);
3012 perf_output_copy(&handle, comm_event->comm,
3013 comm_event->comm_size);
3014 perf_output_end(&handle);
3015}
3016
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003017static int perf_counter_comm_match(struct perf_counter *counter)
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02003018{
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003019 if (counter->attr.comm)
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02003020 return 1;
3021
3022 return 0;
3023}
3024
3025static void perf_counter_comm_ctx(struct perf_counter_context *ctx,
3026 struct perf_comm_event *comm_event)
3027{
3028 struct perf_counter *counter;
3029
3030 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
3031 return;
3032
3033 rcu_read_lock();
3034 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003035 if (perf_counter_comm_match(counter))
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02003036 perf_counter_comm_output(counter, comm_event);
3037 }
3038 rcu_read_unlock();
3039}
3040
3041static void perf_counter_comm_event(struct perf_comm_event *comm_event)
3042{
3043 struct perf_cpu_context *cpuctx;
Peter Zijlstra665c2142009-05-29 14:51:57 +02003044 struct perf_counter_context *ctx;
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02003045 unsigned int size;
Anton Blanchard413ee3b2009-07-16 15:15:52 +02003046 char comm[TASK_COMM_LEN];
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02003047
Anton Blanchard413ee3b2009-07-16 15:15:52 +02003048 memset(comm, 0, sizeof(comm));
3049 strncpy(comm, comm_event->task->comm, sizeof(comm));
Ingo Molnar888fcee2009-04-09 09:48:22 +02003050 size = ALIGN(strlen(comm)+1, sizeof(u64));
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02003051
3052 comm_event->comm = comm;
3053 comm_event->comm_size = size;
3054
3055 comm_event->event.header.size = sizeof(comm_event->event) + size;
3056
3057 cpuctx = &get_cpu_var(perf_cpu_context);
3058 perf_counter_comm_ctx(&cpuctx->ctx, comm_event);
3059 put_cpu_var(perf_cpu_context);
Peter Zijlstra665c2142009-05-29 14:51:57 +02003060
3061 rcu_read_lock();
3062 /*
3063 * doesn't really matter which of the child contexts the
3064 * events ends up in.
3065 */
3066 ctx = rcu_dereference(current->perf_counter_ctxp);
3067 if (ctx)
3068 perf_counter_comm_ctx(ctx, comm_event);
3069 rcu_read_unlock();
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02003070}
3071
3072void perf_counter_comm(struct task_struct *task)
3073{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02003074 struct perf_comm_event comm_event;
3075
Paul Mackerras57e79862009-06-30 16:07:19 +10003076 if (task->perf_counter_ctxp)
3077 perf_counter_enable_on_exec(task);
3078
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003079 if (!atomic_read(&nr_comm_counters))
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02003080 return;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003081
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02003082 comm_event = (struct perf_comm_event){
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02003083 .task = task,
Peter Zijlstra573402d2009-07-22 11:13:50 +02003084 /* .comm */
3085 /* .comm_size */
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02003086 .event = {
Peter Zijlstra573402d2009-07-22 11:13:50 +02003087 .header = {
3088 .type = PERF_EVENT_COMM,
3089 .misc = 0,
3090 /* .size */
3091 },
3092 /* .pid */
3093 /* .tid */
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02003094 },
3095 };
3096
3097 perf_counter_comm_event(&comm_event);
3098}
3099
3100/*
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003101 * mmap tracking
3102 */
3103
3104struct perf_mmap_event {
Peter Zijlstra089dd792009-06-05 14:04:55 +02003105 struct vm_area_struct *vma;
3106
3107 const char *file_name;
3108 int file_size;
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003109
3110 struct {
3111 struct perf_event_header header;
3112
3113 u32 pid;
3114 u32 tid;
3115 u64 start;
3116 u64 len;
3117 u64 pgoff;
3118 } event;
3119};
3120
3121static void perf_counter_mmap_output(struct perf_counter *counter,
3122 struct perf_mmap_event *mmap_event)
3123{
3124 struct perf_output_handle handle;
3125 int size = mmap_event->event.header.size;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02003126 int ret = perf_output_begin(&handle, counter, size, 0, 0);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003127
3128 if (ret)
3129 return;
3130
Peter Zijlstra709e50c2009-06-02 14:13:15 +02003131 mmap_event->event.pid = perf_counter_pid(counter, current);
3132 mmap_event->event.tid = perf_counter_tid(counter, current);
3133
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003134 perf_output_put(&handle, mmap_event->event);
3135 perf_output_copy(&handle, mmap_event->file_name,
3136 mmap_event->file_size);
Peter Zijlstra78d613e2009-03-30 19:07:11 +02003137 perf_output_end(&handle);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003138}
3139
3140static int perf_counter_mmap_match(struct perf_counter *counter,
3141 struct perf_mmap_event *mmap_event)
3142{
Peter Zijlstrad99e9442009-06-04 17:08:58 +02003143 if (counter->attr.mmap)
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003144 return 1;
3145
3146 return 0;
3147}
3148
3149static void perf_counter_mmap_ctx(struct perf_counter_context *ctx,
3150 struct perf_mmap_event *mmap_event)
3151{
3152 struct perf_counter *counter;
3153
3154 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
3155 return;
3156
3157 rcu_read_lock();
3158 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
3159 if (perf_counter_mmap_match(counter, mmap_event))
3160 perf_counter_mmap_output(counter, mmap_event);
3161 }
3162 rcu_read_unlock();
3163}
3164
3165static void perf_counter_mmap_event(struct perf_mmap_event *mmap_event)
3166{
3167 struct perf_cpu_context *cpuctx;
Peter Zijlstra665c2142009-05-29 14:51:57 +02003168 struct perf_counter_context *ctx;
Peter Zijlstra089dd792009-06-05 14:04:55 +02003169 struct vm_area_struct *vma = mmap_event->vma;
3170 struct file *file = vma->vm_file;
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003171 unsigned int size;
3172 char tmp[16];
3173 char *buf = NULL;
Peter Zijlstra089dd792009-06-05 14:04:55 +02003174 const char *name;
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003175
Anton Blanchard413ee3b2009-07-16 15:15:52 +02003176 memset(tmp, 0, sizeof(tmp));
3177
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003178 if (file) {
Anton Blanchard413ee3b2009-07-16 15:15:52 +02003179 /*
3180 * d_path works from the end of the buffer backwards, so we
3181 * need to add enough zero bytes after the string to handle
3182 * the 64bit alignment we do later.
3183 */
3184 buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003185 if (!buf) {
3186 name = strncpy(tmp, "//enomem", sizeof(tmp));
3187 goto got_name;
3188 }
Peter Zijlstrad3d21c42009-04-09 10:53:46 +02003189 name = d_path(&file->f_path, buf, PATH_MAX);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003190 if (IS_ERR(name)) {
3191 name = strncpy(tmp, "//toolong", sizeof(tmp));
3192 goto got_name;
3193 }
3194 } else {
Anton Blanchard413ee3b2009-07-16 15:15:52 +02003195 if (arch_vma_name(mmap_event->vma)) {
3196 name = strncpy(tmp, arch_vma_name(mmap_event->vma),
3197 sizeof(tmp));
Peter Zijlstra089dd792009-06-05 14:04:55 +02003198 goto got_name;
Anton Blanchard413ee3b2009-07-16 15:15:52 +02003199 }
Peter Zijlstra089dd792009-06-05 14:04:55 +02003200
3201 if (!vma->vm_mm) {
3202 name = strncpy(tmp, "[vdso]", sizeof(tmp));
3203 goto got_name;
3204 }
3205
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003206 name = strncpy(tmp, "//anon", sizeof(tmp));
3207 goto got_name;
3208 }
3209
3210got_name:
Ingo Molnar888fcee2009-04-09 09:48:22 +02003211 size = ALIGN(strlen(name)+1, sizeof(u64));
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003212
3213 mmap_event->file_name = name;
3214 mmap_event->file_size = size;
3215
3216 mmap_event->event.header.size = sizeof(mmap_event->event) + size;
3217
3218 cpuctx = &get_cpu_var(perf_cpu_context);
3219 perf_counter_mmap_ctx(&cpuctx->ctx, mmap_event);
3220 put_cpu_var(perf_cpu_context);
3221
Peter Zijlstra665c2142009-05-29 14:51:57 +02003222 rcu_read_lock();
3223 /*
3224 * doesn't really matter which of the child contexts the
3225 * events ends up in.
3226 */
3227 ctx = rcu_dereference(current->perf_counter_ctxp);
3228 if (ctx)
3229 perf_counter_mmap_ctx(ctx, mmap_event);
3230 rcu_read_unlock();
3231
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003232 kfree(buf);
3233}
3234
Peter Zijlstra089dd792009-06-05 14:04:55 +02003235void __perf_counter_mmap(struct vm_area_struct *vma)
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003236{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02003237 struct perf_mmap_event mmap_event;
3238
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003239 if (!atomic_read(&nr_mmap_counters))
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02003240 return;
3241
3242 mmap_event = (struct perf_mmap_event){
Peter Zijlstra089dd792009-06-05 14:04:55 +02003243 .vma = vma,
Peter Zijlstra573402d2009-07-22 11:13:50 +02003244 /* .file_name */
3245 /* .file_size */
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003246 .event = {
Peter Zijlstra573402d2009-07-22 11:13:50 +02003247 .header = {
3248 .type = PERF_EVENT_MMAP,
3249 .misc = 0,
3250 /* .size */
3251 },
3252 /* .pid */
3253 /* .tid */
Peter Zijlstra089dd792009-06-05 14:04:55 +02003254 .start = vma->vm_start,
3255 .len = vma->vm_end - vma->vm_start,
3256 .pgoff = vma->vm_pgoff,
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003257 },
3258 };
3259
3260 perf_counter_mmap_event(&mmap_event);
3261}
3262
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02003263/*
Peter Zijlstraa78ac322009-05-25 17:39:05 +02003264 * IRQ throttle logging
3265 */
3266
3267static void perf_log_throttle(struct perf_counter *counter, int enable)
3268{
3269 struct perf_output_handle handle;
3270 int ret;
3271
3272 struct {
3273 struct perf_event_header header;
3274 u64 time;
Peter Zijlstracca3f452009-06-11 14:57:55 +02003275 u64 id;
Peter Zijlstra7f453c22009-07-21 13:19:40 +02003276 u64 stream_id;
Peter Zijlstraa78ac322009-05-25 17:39:05 +02003277 } throttle_event = {
3278 .header = {
Anton Blanchard966ee4d2009-07-22 23:05:46 +10003279 .type = PERF_EVENT_THROTTLE,
Peter Zijlstraa78ac322009-05-25 17:39:05 +02003280 .misc = 0,
3281 .size = sizeof(throttle_event),
3282 },
Peter Zijlstra7f453c22009-07-21 13:19:40 +02003283 .time = sched_clock(),
3284 .id = primary_counter_id(counter),
3285 .stream_id = counter->id,
Peter Zijlstraa78ac322009-05-25 17:39:05 +02003286 };
3287
Anton Blanchard966ee4d2009-07-22 23:05:46 +10003288 if (enable)
3289 throttle_event.header.type = PERF_EVENT_UNTHROTTLE;
3290
Ingo Molnar0127c3e2009-05-25 22:03:26 +02003291 ret = perf_output_begin(&handle, counter, sizeof(throttle_event), 1, 0);
Peter Zijlstraa78ac322009-05-25 17:39:05 +02003292 if (ret)
3293 return;
3294
3295 perf_output_put(&handle, throttle_event);
3296 perf_output_end(&handle);
3297}
3298
3299/*
Peter Zijlstra43a21ea2009-03-25 19:39:37 +01003300 * Generic counter overflow handling, sampling.
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02003301 */
3302
Peter Zijlstradf1a1322009-06-10 21:02:22 +02003303int perf_counter_overflow(struct perf_counter *counter, int nmi,
3304 struct perf_sample_data *data)
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02003305{
Peter Zijlstra79f14642009-04-06 11:45:07 +02003306 int events = atomic_read(&counter->event_limit);
Peter Zijlstraa78ac322009-05-25 17:39:05 +02003307 int throttle = counter->pmu->unthrottle != NULL;
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02003308 struct hw_perf_counter *hwc = &counter->hw;
Peter Zijlstra79f14642009-04-06 11:45:07 +02003309 int ret = 0;
3310
Peter Zijlstraa78ac322009-05-25 17:39:05 +02003311 if (!throttle) {
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02003312 hwc->interrupts++;
Ingo Molnar128f0482009-06-03 22:19:36 +02003313 } else {
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02003314 if (hwc->interrupts != MAX_INTERRUPTS) {
3315 hwc->interrupts++;
Peter Zijlstradf58ab22009-06-11 11:25:05 +02003316 if (HZ * hwc->interrupts >
3317 (u64)sysctl_perf_counter_sample_rate) {
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02003318 hwc->interrupts = MAX_INTERRUPTS;
Ingo Molnar128f0482009-06-03 22:19:36 +02003319 perf_log_throttle(counter, 0);
3320 ret = 1;
3321 }
3322 } else {
3323 /*
3324 * Keep re-disabling counters even though on the previous
3325 * pass we disabled it - just in case we raced with a
3326 * sched-in and the counter got enabled again:
3327 */
Peter Zijlstraa78ac322009-05-25 17:39:05 +02003328 ret = 1;
3329 }
3330 }
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003331
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02003332 if (counter->attr.freq) {
3333 u64 now = sched_clock();
3334 s64 delta = now - hwc->freq_stamp;
3335
3336 hwc->freq_stamp = now;
3337
3338 if (delta > 0 && delta < TICK_NSEC)
3339 perf_adjust_period(counter, NSEC_PER_SEC / (int)delta);
3340 }
3341
Peter Zijlstra2023b352009-05-05 17:50:26 +02003342 /*
3343 * XXX event_limit might not quite work as expected on inherited
3344 * counters
3345 */
3346
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02003347 counter->pending_kill = POLL_IN;
Peter Zijlstra79f14642009-04-06 11:45:07 +02003348 if (events && atomic_dec_and_test(&counter->event_limit)) {
3349 ret = 1;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02003350 counter->pending_kill = POLL_HUP;
Peter Zijlstra79f14642009-04-06 11:45:07 +02003351 if (nmi) {
3352 counter->pending_disable = 1;
3353 perf_pending_queue(&counter->pending,
3354 perf_pending_counter);
3355 } else
3356 perf_counter_disable(counter);
3357 }
3358
Peter Zijlstradf1a1322009-06-10 21:02:22 +02003359 perf_counter_output(counter, nmi, data);
Peter Zijlstra79f14642009-04-06 11:45:07 +02003360 return ret;
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02003361}
3362
3363/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003364 * Generic software counter infrastructure
3365 */
3366
Peter Zijlstra7b4b6652009-07-22 09:29:32 +02003367/*
3368 * We directly increment counter->count and keep a second value in
3369 * counter->hw.period_left to count intervals. This period counter
3370 * is kept in the range [-sample_period, 0] so that we can use the
3371 * sign as trigger.
3372 */
3373
3374static u64 perf_swcounter_set_period(struct perf_counter *counter)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003375{
3376 struct hw_perf_counter *hwc = &counter->hw;
Peter Zijlstra7b4b6652009-07-22 09:29:32 +02003377 u64 period = hwc->last_period;
3378 u64 nr, offset;
3379 s64 old, val;
3380
3381 hwc->last_period = hwc->sample_period;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003382
3383again:
Peter Zijlstra7b4b6652009-07-22 09:29:32 +02003384 old = val = atomic64_read(&hwc->period_left);
3385 if (val < 0)
3386 return 0;
3387
3388 nr = div64_u64(period + val, period);
3389 offset = nr * period;
3390 val -= offset;
3391 if (atomic64_cmpxchg(&hwc->period_left, old, val) != old)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003392 goto again;
3393
Peter Zijlstra7b4b6652009-07-22 09:29:32 +02003394 return nr;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003395}
3396
3397static void perf_swcounter_overflow(struct perf_counter *counter,
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003398 int nmi, struct perf_sample_data *data)
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003399{
Peter Zijlstra7b4b6652009-07-22 09:29:32 +02003400 struct hw_perf_counter *hwc = &counter->hw;
3401 u64 overflow;
Peter Zijlstradf1a1322009-06-10 21:02:22 +02003402
Peter Zijlstra7b4b6652009-07-22 09:29:32 +02003403 data->period = counter->hw.last_period;
3404 overflow = perf_swcounter_set_period(counter);
3405
3406 if (hwc->interrupts == MAX_INTERRUPTS)
3407 return;
3408
3409 for (; overflow; overflow--) {
3410 if (perf_counter_overflow(counter, nmi, data)) {
3411 /*
3412 * We inhibit the overflow from happening when
3413 * hwc->interrupts == MAX_INTERRUPTS.
3414 */
3415 break;
3416 }
3417 }
3418}
3419
3420static void perf_swcounter_unthrottle(struct perf_counter *counter)
3421{
3422 /*
3423 * Nothing to do, we already reset hwc->interrupts.
3424 */
3425}
3426
3427static void perf_swcounter_add(struct perf_counter *counter, u64 nr,
3428 int nmi, struct perf_sample_data *data)
3429{
3430 struct hw_perf_counter *hwc = &counter->hw;
3431
3432 atomic64_add(nr, &counter->count);
3433
3434 if (!hwc->sample_period)
3435 return;
3436
3437 if (!data->regs)
3438 return;
3439
3440 if (!atomic64_add_negative(nr, &hwc->period_left))
3441 perf_swcounter_overflow(counter, nmi, data);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003442}
3443
Paul Mackerras880ca152009-06-01 17:49:14 +10003444static int perf_swcounter_is_counting(struct perf_counter *counter)
3445{
3446 struct perf_counter_context *ctx;
3447 unsigned long flags;
3448 int count;
3449
3450 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
3451 return 1;
3452
3453 if (counter->state != PERF_COUNTER_STATE_INACTIVE)
3454 return 0;
3455
3456 /*
3457 * If the counter is inactive, it could be just because
3458 * its task is scheduled out, or because it's in a group
3459 * which could not go on the PMU. We want to count in
3460 * the first case but not the second. If the context is
3461 * currently active then an inactive software counter must
3462 * be the second case. If it's not currently active then
3463 * we need to know whether the counter was active when the
3464 * context was last active, which we can determine by
3465 * comparing counter->tstamp_stopped with ctx->time.
3466 *
3467 * We are within an RCU read-side critical section,
3468 * which protects the existence of *ctx.
3469 */
3470 ctx = counter->ctx;
3471 spin_lock_irqsave(&ctx->lock, flags);
3472 count = 1;
3473 /* Re-check state now we have the lock */
3474 if (counter->state < PERF_COUNTER_STATE_INACTIVE ||
3475 counter->ctx->is_active ||
3476 counter->tstamp_stopped < ctx->time)
3477 count = 0;
3478 spin_unlock_irqrestore(&ctx->lock, flags);
3479 return count;
3480}
3481
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003482static int perf_swcounter_match(struct perf_counter *counter,
Peter Zijlstra1c432d82009-06-11 13:19:29 +02003483 enum perf_type_id type,
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003484 u32 event, struct pt_regs *regs)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003485{
Paul Mackerras880ca152009-06-01 17:49:14 +10003486 if (!perf_swcounter_is_counting(counter))
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003487 return 0;
3488
Ingo Molnara21ca2c2009-06-06 09:58:57 +02003489 if (counter->attr.type != type)
3490 return 0;
3491 if (counter->attr.config != event)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003492 return 0;
3493
Paul Mackerras3f731ca2009-06-01 17:52:30 +10003494 if (regs) {
Peter Zijlstra0d486962009-06-02 19:22:16 +02003495 if (counter->attr.exclude_user && user_mode(regs))
Paul Mackerras3f731ca2009-06-01 17:52:30 +10003496 return 0;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003497
Peter Zijlstra0d486962009-06-02 19:22:16 +02003498 if (counter->attr.exclude_kernel && !user_mode(regs))
Paul Mackerras3f731ca2009-06-01 17:52:30 +10003499 return 0;
3500 }
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003501
3502 return 1;
3503}
3504
3505static void perf_swcounter_ctx_event(struct perf_counter_context *ctx,
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003506 enum perf_type_id type,
3507 u32 event, u64 nr, int nmi,
3508 struct perf_sample_data *data)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003509{
3510 struct perf_counter *counter;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003511
Peter Zijlstra01ef09d2009-03-19 20:26:11 +01003512 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003513 return;
3514
Peter Zijlstra592903c2009-03-13 12:21:36 +01003515 rcu_read_lock();
3516 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003517 if (perf_swcounter_match(counter, type, event, data->regs))
3518 perf_swcounter_add(counter, nr, nmi, data);
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003519 }
Peter Zijlstra592903c2009-03-13 12:21:36 +01003520 rcu_read_unlock();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003521}
3522
Peter Zijlstra96f6d442009-03-23 18:22:07 +01003523static int *perf_swcounter_recursion_context(struct perf_cpu_context *cpuctx)
3524{
3525 if (in_nmi())
3526 return &cpuctx->recursion[3];
3527
3528 if (in_irq())
3529 return &cpuctx->recursion[2];
3530
3531 if (in_softirq())
3532 return &cpuctx->recursion[1];
3533
3534 return &cpuctx->recursion[0];
3535}
3536
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003537static void do_perf_swcounter_event(enum perf_type_id type, u32 event,
3538 u64 nr, int nmi,
3539 struct perf_sample_data *data)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003540{
3541 struct perf_cpu_context *cpuctx = &get_cpu_var(perf_cpu_context);
Peter Zijlstra96f6d442009-03-23 18:22:07 +01003542 int *recursion = perf_swcounter_recursion_context(cpuctx);
Peter Zijlstra665c2142009-05-29 14:51:57 +02003543 struct perf_counter_context *ctx;
Peter Zijlstra96f6d442009-03-23 18:22:07 +01003544
3545 if (*recursion)
3546 goto out;
3547
3548 (*recursion)++;
3549 barrier();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003550
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003551 perf_swcounter_ctx_event(&cpuctx->ctx, type, event,
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003552 nr, nmi, data);
Peter Zijlstra665c2142009-05-29 14:51:57 +02003553 rcu_read_lock();
3554 /*
3555 * doesn't really matter which of the child contexts the
3556 * events ends up in.
3557 */
3558 ctx = rcu_dereference(current->perf_counter_ctxp);
3559 if (ctx)
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003560 perf_swcounter_ctx_event(ctx, type, event, nr, nmi, data);
Peter Zijlstra665c2142009-05-29 14:51:57 +02003561 rcu_read_unlock();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003562
Peter Zijlstra96f6d442009-03-23 18:22:07 +01003563 barrier();
3564 (*recursion)--;
3565
3566out:
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003567 put_cpu_var(perf_cpu_context);
3568}
3569
Peter Zijlstraf29ac752009-06-19 18:27:26 +02003570void __perf_swcounter_event(u32 event, u64 nr, int nmi,
3571 struct pt_regs *regs, u64 addr)
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003572{
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003573 struct perf_sample_data data = {
3574 .regs = regs,
3575 .addr = addr,
3576 };
3577
3578 do_perf_swcounter_event(PERF_TYPE_SOFTWARE, event, nr, nmi, &data);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003579}
3580
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003581static void perf_swcounter_read(struct perf_counter *counter)
3582{
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003583}
3584
3585static int perf_swcounter_enable(struct perf_counter *counter)
3586{
Peter Zijlstra7b4b6652009-07-22 09:29:32 +02003587 struct hw_perf_counter *hwc = &counter->hw;
3588
3589 if (hwc->sample_period) {
3590 hwc->last_period = hwc->sample_period;
3591 perf_swcounter_set_period(counter);
3592 }
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003593 return 0;
3594}
3595
3596static void perf_swcounter_disable(struct perf_counter *counter)
3597{
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003598}
3599
Robert Richter4aeb0b42009-04-29 12:47:03 +02003600static const struct pmu perf_ops_generic = {
Peter Zijlstraac17dc82009-03-13 12:21:34 +01003601 .enable = perf_swcounter_enable,
3602 .disable = perf_swcounter_disable,
3603 .read = perf_swcounter_read,
Peter Zijlstra7b4b6652009-07-22 09:29:32 +02003604 .unthrottle = perf_swcounter_unthrottle,
Peter Zijlstraac17dc82009-03-13 12:21:34 +01003605};
3606
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003607/*
Peter Zijlstra7b4b6652009-07-22 09:29:32 +02003608 * hrtimer based swcounter callback
3609 */
3610
3611static enum hrtimer_restart perf_swcounter_hrtimer(struct hrtimer *hrtimer)
3612{
3613 enum hrtimer_restart ret = HRTIMER_RESTART;
3614 struct perf_sample_data data;
3615 struct perf_counter *counter;
3616 u64 period;
3617
3618 counter = container_of(hrtimer, struct perf_counter, hw.hrtimer);
3619 counter->pmu->read(counter);
3620
3621 data.addr = 0;
3622 data.regs = get_irq_regs();
3623 /*
3624 * In case we exclude kernel IPs or are somehow not in interrupt
3625 * context, provide the next best thing, the user IP.
3626 */
3627 if ((counter->attr.exclude_kernel || !data.regs) &&
3628 !counter->attr.exclude_user)
3629 data.regs = task_pt_regs(current);
3630
3631 if (data.regs) {
3632 if (perf_counter_overflow(counter, 0, &data))
3633 ret = HRTIMER_NORESTART;
3634 }
3635
3636 period = max_t(u64, 10000, counter->hw.sample_period);
3637 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
3638
3639 return ret;
3640}
3641
3642/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003643 * Software counter: cpu wall time clock
3644 */
3645
Paul Mackerras9abf8a02009-01-09 16:26:43 +11003646static void cpu_clock_perf_counter_update(struct perf_counter *counter)
3647{
3648 int cpu = raw_smp_processor_id();
3649 s64 prev;
3650 u64 now;
3651
3652 now = cpu_clock(cpu);
3653 prev = atomic64_read(&counter->hw.prev_count);
3654 atomic64_set(&counter->hw.prev_count, now);
3655 atomic64_add(now - prev, &counter->count);
3656}
3657
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003658static int cpu_clock_perf_counter_enable(struct perf_counter *counter)
3659{
3660 struct hw_perf_counter *hwc = &counter->hw;
3661 int cpu = raw_smp_processor_id();
3662
3663 atomic64_set(&hwc->prev_count, cpu_clock(cpu));
Peter Zijlstra039fc912009-03-13 16:43:47 +01003664 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
3665 hwc->hrtimer.function = perf_swcounter_hrtimer;
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003666 if (hwc->sample_period) {
3667 u64 period = max_t(u64, 10000, hwc->sample_period);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003668 __hrtimer_start_range_ns(&hwc->hrtimer,
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003669 ns_to_ktime(period), 0,
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003670 HRTIMER_MODE_REL, 0);
3671 }
3672
3673 return 0;
3674}
3675
Ingo Molnar5c92d122008-12-11 13:21:10 +01003676static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
3677{
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003678 if (counter->hw.sample_period)
Peter Zijlstrab986d7e2009-05-20 12:21:21 +02003679 hrtimer_cancel(&counter->hw.hrtimer);
Paul Mackerras9abf8a02009-01-09 16:26:43 +11003680 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01003681}
3682
3683static void cpu_clock_perf_counter_read(struct perf_counter *counter)
3684{
Paul Mackerras9abf8a02009-01-09 16:26:43 +11003685 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01003686}
3687
Robert Richter4aeb0b42009-04-29 12:47:03 +02003688static const struct pmu perf_ops_cpu_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +01003689 .enable = cpu_clock_perf_counter_enable,
3690 .disable = cpu_clock_perf_counter_disable,
3691 .read = cpu_clock_perf_counter_read,
Ingo Molnar5c92d122008-12-11 13:21:10 +01003692};
3693
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01003694/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003695 * Software counter: task time clock
3696 */
3697
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02003698static void task_clock_perf_counter_update(struct perf_counter *counter, u64 now)
Ingo Molnarbae43c92008-12-11 14:03:20 +01003699{
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02003700 u64 prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003701 s64 delta;
Ingo Molnarbae43c92008-12-11 14:03:20 +01003702
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02003703 prev = atomic64_xchg(&counter->hw.prev_count, now);
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003704 delta = now - prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003705 atomic64_add(delta, &counter->count);
Ingo Molnarbae43c92008-12-11 14:03:20 +01003706}
3707
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01003708static int task_clock_perf_counter_enable(struct perf_counter *counter)
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003709{
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003710 struct hw_perf_counter *hwc = &counter->hw;
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02003711 u64 now;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003712
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02003713 now = counter->ctx->time;
3714
3715 atomic64_set(&hwc->prev_count, now);
Peter Zijlstra039fc912009-03-13 16:43:47 +01003716 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
3717 hwc->hrtimer.function = perf_swcounter_hrtimer;
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003718 if (hwc->sample_period) {
3719 u64 period = max_t(u64, 10000, hwc->sample_period);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003720 __hrtimer_start_range_ns(&hwc->hrtimer,
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003721 ns_to_ktime(period), 0,
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003722 HRTIMER_MODE_REL, 0);
3723 }
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01003724
3725 return 0;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003726}
3727
3728static void task_clock_perf_counter_disable(struct perf_counter *counter)
3729{
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003730 if (counter->hw.sample_period)
Peter Zijlstrab986d7e2009-05-20 12:21:21 +02003731 hrtimer_cancel(&counter->hw.hrtimer);
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02003732 task_clock_perf_counter_update(counter, counter->ctx->time);
3733
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003734}
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01003735
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003736static void task_clock_perf_counter_read(struct perf_counter *counter)
3737{
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02003738 u64 time;
3739
3740 if (!in_nmi()) {
3741 update_context_time(counter->ctx);
3742 time = counter->ctx->time;
3743 } else {
3744 u64 now = perf_clock();
3745 u64 delta = now - counter->ctx->timestamp;
3746 time = counter->ctx->time + delta;
3747 }
3748
3749 task_clock_perf_counter_update(counter, time);
Ingo Molnarbae43c92008-12-11 14:03:20 +01003750}
3751
Robert Richter4aeb0b42009-04-29 12:47:03 +02003752static const struct pmu perf_ops_task_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +01003753 .enable = task_clock_perf_counter_enable,
3754 .disable = task_clock_perf_counter_disable,
3755 .read = task_clock_perf_counter_read,
Ingo Molnarbae43c92008-12-11 14:03:20 +01003756};
3757
Peter Zijlstrae077df42009-03-19 20:26:17 +01003758#ifdef CONFIG_EVENT_PROFILE
Frederic Weisbeckerf413cdb2009-08-07 01:25:54 +02003759void perf_tpcounter_event(int event_id, u64 addr, u64 count, void *record,
3760 int entry_size)
Peter Zijlstrae077df42009-03-19 20:26:17 +01003761{
Frederic Weisbecker3a43ce62009-08-08 04:26:37 +02003762 struct perf_raw_record raw = {
Frederic Weisbeckerf413cdb2009-08-07 01:25:54 +02003763 .size = entry_size,
Frederic Weisbecker3a43ce62009-08-08 04:26:37 +02003764 .data = record,
Frederic Weisbeckerf413cdb2009-08-07 01:25:54 +02003765 };
3766
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003767 struct perf_sample_data data = {
Chris Wilsond4d7d0b2009-07-06 09:31:33 +01003768 .regs = get_irq_regs(),
Peter Zijlstra3a659302009-07-21 17:34:57 +02003769 .addr = addr,
Frederic Weisbecker3a43ce62009-08-08 04:26:37 +02003770 .raw = &raw,
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003771 };
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003772
Peter Zijlstra92bf3092009-06-19 18:11:53 +02003773 if (!data.regs)
3774 data.regs = task_pt_regs(current);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003775
Peter Zijlstra3a659302009-07-21 17:34:57 +02003776 do_perf_swcounter_event(PERF_TYPE_TRACEPOINT, event_id, count, 1, &data);
Peter Zijlstrae077df42009-03-19 20:26:17 +01003777}
Steven Whitehouseff7b1b42009-04-15 16:55:05 +01003778EXPORT_SYMBOL_GPL(perf_tpcounter_event);
Peter Zijlstrae077df42009-03-19 20:26:17 +01003779
3780extern int ftrace_profile_enable(int);
3781extern void ftrace_profile_disable(int);
3782
3783static void tp_perf_counter_destroy(struct perf_counter *counter)
3784{
Chris Wilsond4d7d0b2009-07-06 09:31:33 +01003785 ftrace_profile_disable(counter->attr.config);
Peter Zijlstrae077df42009-03-19 20:26:17 +01003786}
3787
Robert Richter4aeb0b42009-04-29 12:47:03 +02003788static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
Peter Zijlstrae077df42009-03-19 20:26:17 +01003789{
Peter Zijlstraa4e95fc2009-08-10 11:20:12 +02003790 /*
3791 * Raw tracepoint data is a severe data leak, only allow root to
3792 * have these.
3793 */
3794 if ((counter->attr.sample_type & PERF_SAMPLE_RAW) &&
3795 !capable(CAP_SYS_ADMIN))
3796 return ERR_PTR(-EPERM);
3797
Chris Wilsond4d7d0b2009-07-06 09:31:33 +01003798 if (ftrace_profile_enable(counter->attr.config))
Peter Zijlstrae077df42009-03-19 20:26:17 +01003799 return NULL;
3800
3801 counter->destroy = tp_perf_counter_destroy;
3802
3803 return &perf_ops_generic;
3804}
3805#else
Robert Richter4aeb0b42009-04-29 12:47:03 +02003806static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
Peter Zijlstrae077df42009-03-19 20:26:17 +01003807{
3808 return NULL;
3809}
3810#endif
3811
Peter Zijlstraf29ac752009-06-19 18:27:26 +02003812atomic_t perf_swcounter_enabled[PERF_COUNT_SW_MAX];
3813
3814static void sw_perf_counter_destroy(struct perf_counter *counter)
3815{
3816 u64 event = counter->attr.config;
3817
Peter Zijlstraf3440112009-06-22 13:58:35 +02003818 WARN_ON(counter->parent);
3819
Peter Zijlstraf29ac752009-06-19 18:27:26 +02003820 atomic_dec(&perf_swcounter_enabled[event]);
3821}
3822
Robert Richter4aeb0b42009-04-29 12:47:03 +02003823static const struct pmu *sw_perf_counter_init(struct perf_counter *counter)
Ingo Molnar5c92d122008-12-11 13:21:10 +01003824{
Robert Richter4aeb0b42009-04-29 12:47:03 +02003825 const struct pmu *pmu = NULL;
Peter Zijlstraf29ac752009-06-19 18:27:26 +02003826 u64 event = counter->attr.config;
Ingo Molnar5c92d122008-12-11 13:21:10 +01003827
Paul Mackerras0475f9e2009-02-11 14:35:35 +11003828 /*
3829 * Software counters (currently) can't in general distinguish
3830 * between user, kernel and hypervisor events.
3831 * However, context switches and cpu migrations are considered
3832 * to be kernel events, and page faults are never hypervisor
3833 * events.
3834 */
Peter Zijlstraf29ac752009-06-19 18:27:26 +02003835 switch (event) {
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +02003836 case PERF_COUNT_SW_CPU_CLOCK:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003837 pmu = &perf_ops_cpu_clock;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003838
Ingo Molnar5c92d122008-12-11 13:21:10 +01003839 break;
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +02003840 case PERF_COUNT_SW_TASK_CLOCK:
Paul Mackerras23a185c2009-02-09 22:42:47 +11003841 /*
3842 * If the user instantiates this as a per-cpu counter,
3843 * use the cpu_clock counter instead.
3844 */
3845 if (counter->ctx->task)
Robert Richter4aeb0b42009-04-29 12:47:03 +02003846 pmu = &perf_ops_task_clock;
Paul Mackerras23a185c2009-02-09 22:42:47 +11003847 else
Robert Richter4aeb0b42009-04-29 12:47:03 +02003848 pmu = &perf_ops_cpu_clock;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003849
Ingo Molnarbae43c92008-12-11 14:03:20 +01003850 break;
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +02003851 case PERF_COUNT_SW_PAGE_FAULTS:
3852 case PERF_COUNT_SW_PAGE_FAULTS_MIN:
3853 case PERF_COUNT_SW_PAGE_FAULTS_MAJ:
3854 case PERF_COUNT_SW_CONTEXT_SWITCHES:
3855 case PERF_COUNT_SW_CPU_MIGRATIONS:
Peter Zijlstraf3440112009-06-22 13:58:35 +02003856 if (!counter->parent) {
3857 atomic_inc(&perf_swcounter_enabled[event]);
3858 counter->destroy = sw_perf_counter_destroy;
3859 }
Paul Mackerras3f731ca2009-06-01 17:52:30 +10003860 pmu = &perf_ops_generic;
Ingo Molnar6c594c22008-12-14 12:34:15 +01003861 break;
Ingo Molnar5c92d122008-12-11 13:21:10 +01003862 }
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003863
Robert Richter4aeb0b42009-04-29 12:47:03 +02003864 return pmu;
Ingo Molnar5c92d122008-12-11 13:21:10 +01003865}
3866
Thomas Gleixner0793a612008-12-04 20:12:29 +01003867/*
3868 * Allocate and initialize a counter structure
3869 */
3870static struct perf_counter *
Peter Zijlstra0d486962009-06-02 19:22:16 +02003871perf_counter_alloc(struct perf_counter_attr *attr,
Ingo Molnar04289bb2008-12-11 08:38:42 +01003872 int cpu,
Paul Mackerras23a185c2009-02-09 22:42:47 +11003873 struct perf_counter_context *ctx,
Ingo Molnar9b51f662008-12-12 13:49:45 +01003874 struct perf_counter *group_leader,
Peter Zijlstrab84fbc92009-06-22 13:57:40 +02003875 struct perf_counter *parent_counter,
Ingo Molnar9b51f662008-12-12 13:49:45 +01003876 gfp_t gfpflags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003877{
Robert Richter4aeb0b42009-04-29 12:47:03 +02003878 const struct pmu *pmu;
Ingo Molnar621a01e2008-12-11 12:46:46 +01003879 struct perf_counter *counter;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003880 struct hw_perf_counter *hwc;
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003881 long err;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003882
Ingo Molnar9b51f662008-12-12 13:49:45 +01003883 counter = kzalloc(sizeof(*counter), gfpflags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003884 if (!counter)
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003885 return ERR_PTR(-ENOMEM);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003886
Ingo Molnar04289bb2008-12-11 08:38:42 +01003887 /*
3888 * Single counters are their own group leaders, with an
3889 * empty sibling list:
3890 */
3891 if (!group_leader)
3892 group_leader = counter;
3893
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003894 mutex_init(&counter->child_mutex);
3895 INIT_LIST_HEAD(&counter->child_list);
3896
Ingo Molnar04289bb2008-12-11 08:38:42 +01003897 INIT_LIST_HEAD(&counter->list_entry);
Peter Zijlstra592903c2009-03-13 12:21:36 +01003898 INIT_LIST_HEAD(&counter->event_entry);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003899 INIT_LIST_HEAD(&counter->sibling_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003900 init_waitqueue_head(&counter->waitq);
3901
Peter Zijlstra7b732a72009-03-23 18:22:10 +01003902 mutex_init(&counter->mmap_mutex);
3903
Peter Zijlstraa96bbc12009-06-03 14:01:36 +02003904 counter->cpu = cpu;
Peter Zijlstra0d486962009-06-02 19:22:16 +02003905 counter->attr = *attr;
Peter Zijlstraa96bbc12009-06-03 14:01:36 +02003906 counter->group_leader = group_leader;
3907 counter->pmu = NULL;
3908 counter->ctx = ctx;
3909 counter->oncpu = -1;
Ingo Molnar329d8762009-05-26 08:10:00 +02003910
Peter Zijlstrab84fbc92009-06-22 13:57:40 +02003911 counter->parent = parent_counter;
3912
Peter Zijlstraa96bbc12009-06-03 14:01:36 +02003913 counter->ns = get_pid_ns(current->nsproxy->pid_ns);
3914 counter->id = atomic64_inc_return(&perf_counter_id);
3915
3916 counter->state = PERF_COUNTER_STATE_INACTIVE;
3917
Peter Zijlstra0d486962009-06-02 19:22:16 +02003918 if (attr->disabled)
Ingo Molnara86ed502008-12-17 00:43:10 +01003919 counter->state = PERF_COUNTER_STATE_OFF;
3920
Robert Richter4aeb0b42009-04-29 12:47:03 +02003921 pmu = NULL;
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003922
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003923 hwc = &counter->hw;
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02003924 hwc->sample_period = attr->sample_period;
Peter Zijlstra0d486962009-06-02 19:22:16 +02003925 if (attr->freq && attr->sample_freq)
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02003926 hwc->sample_period = 1;
3927
3928 atomic64_set(&hwc->period_left, hwc->sample_period);
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003929
Peter Zijlstra2023b352009-05-05 17:50:26 +02003930 /*
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003931 * we currently do not support PERF_SAMPLE_GROUP on inherited counters
Peter Zijlstra2023b352009-05-05 17:50:26 +02003932 */
Peter Zijlstra0d486962009-06-02 19:22:16 +02003933 if (attr->inherit && (attr->sample_type & PERF_SAMPLE_GROUP))
Peter Zijlstra2023b352009-05-05 17:50:26 +02003934 goto done;
3935
Ingo Molnara21ca2c2009-06-06 09:58:57 +02003936 switch (attr->type) {
Peter Zijlstra081fad82009-06-11 17:57:21 +02003937 case PERF_TYPE_RAW:
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003938 case PERF_TYPE_HARDWARE:
Ingo Molnar8326f442009-06-05 20:22:46 +02003939 case PERF_TYPE_HW_CACHE:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003940 pmu = hw_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003941 break;
3942
3943 case PERF_TYPE_SOFTWARE:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003944 pmu = sw_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003945 break;
3946
3947 case PERF_TYPE_TRACEPOINT:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003948 pmu = tp_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003949 break;
Peter Zijlstra974802e2009-06-12 12:46:55 +02003950
3951 default:
3952 break;
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003953 }
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01003954done:
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003955 err = 0;
Robert Richter4aeb0b42009-04-29 12:47:03 +02003956 if (!pmu)
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003957 err = -EINVAL;
Robert Richter4aeb0b42009-04-29 12:47:03 +02003958 else if (IS_ERR(pmu))
3959 err = PTR_ERR(pmu);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003960
3961 if (err) {
Peter Zijlstraa96bbc12009-06-03 14:01:36 +02003962 if (counter->ns)
3963 put_pid_ns(counter->ns);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003964 kfree(counter);
3965 return ERR_PTR(err);
3966 }
3967
Robert Richter4aeb0b42009-04-29 12:47:03 +02003968 counter->pmu = pmu;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003969
Peter Zijlstraf3440112009-06-22 13:58:35 +02003970 if (!counter->parent) {
3971 atomic_inc(&nr_counters);
3972 if (counter->attr.mmap)
3973 atomic_inc(&nr_mmap_counters);
3974 if (counter->attr.comm)
3975 atomic_inc(&nr_comm_counters);
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02003976 if (counter->attr.task)
3977 atomic_inc(&nr_task_counters);
Peter Zijlstraf3440112009-06-22 13:58:35 +02003978 }
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02003979
Thomas Gleixner0793a612008-12-04 20:12:29 +01003980 return counter;
3981}
3982
Peter Zijlstra974802e2009-06-12 12:46:55 +02003983static int perf_copy_attr(struct perf_counter_attr __user *uattr,
3984 struct perf_counter_attr *attr)
3985{
3986 int ret;
3987 u32 size;
3988
3989 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
3990 return -EFAULT;
3991
3992 /*
3993 * zero the full structure, so that a short copy will be nice.
3994 */
3995 memset(attr, 0, sizeof(*attr));
3996
3997 ret = get_user(size, &uattr->size);
3998 if (ret)
3999 return ret;
4000
4001 if (size > PAGE_SIZE) /* silly large */
4002 goto err_size;
4003
4004 if (!size) /* abi compat */
4005 size = PERF_ATTR_SIZE_VER0;
4006
4007 if (size < PERF_ATTR_SIZE_VER0)
4008 goto err_size;
4009
4010 /*
4011 * If we're handed a bigger struct than we know of,
4012 * ensure all the unknown bits are 0.
4013 */
4014 if (size > sizeof(*attr)) {
4015 unsigned long val;
4016 unsigned long __user *addr;
4017 unsigned long __user *end;
4018
4019 addr = PTR_ALIGN((void __user *)uattr + sizeof(*attr),
4020 sizeof(unsigned long));
4021 end = PTR_ALIGN((void __user *)uattr + size,
4022 sizeof(unsigned long));
4023
4024 for (; addr < end; addr += sizeof(unsigned long)) {
4025 ret = get_user(val, addr);
4026 if (ret)
4027 return ret;
4028 if (val)
4029 goto err_size;
4030 }
4031 }
4032
4033 ret = copy_from_user(attr, uattr, size);
4034 if (ret)
4035 return -EFAULT;
4036
4037 /*
4038 * If the type exists, the corresponding creation will verify
4039 * the attr->config.
4040 */
4041 if (attr->type >= PERF_TYPE_MAX)
4042 return -EINVAL;
4043
4044 if (attr->__reserved_1 || attr->__reserved_2 || attr->__reserved_3)
4045 return -EINVAL;
4046
4047 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
4048 return -EINVAL;
4049
4050 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
4051 return -EINVAL;
4052
4053out:
4054 return ret;
4055
4056err_size:
4057 put_user(sizeof(*attr), &uattr->size);
4058 ret = -E2BIG;
4059 goto out;
4060}
4061
Thomas Gleixner0793a612008-12-04 20:12:29 +01004062/**
Paul Mackerras2743a5b2009-03-04 20:36:51 +11004063 * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
Ingo Molnar9f66a382008-12-10 12:33:23 +01004064 *
Peter Zijlstra0d486962009-06-02 19:22:16 +02004065 * @attr_uptr: event type attributes for monitoring/sampling
Thomas Gleixner0793a612008-12-04 20:12:29 +01004066 * @pid: target pid
Ingo Molnar9f66a382008-12-10 12:33:23 +01004067 * @cpu: target cpu
4068 * @group_fd: group leader counter fd
Thomas Gleixner0793a612008-12-04 20:12:29 +01004069 */
Paul Mackerras2743a5b2009-03-04 20:36:51 +11004070SYSCALL_DEFINE5(perf_counter_open,
Peter Zijlstra974802e2009-06-12 12:46:55 +02004071 struct perf_counter_attr __user *, attr_uptr,
Paul Mackerras2743a5b2009-03-04 20:36:51 +11004072 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01004073{
Ingo Molnar04289bb2008-12-11 08:38:42 +01004074 struct perf_counter *counter, *group_leader;
Peter Zijlstra0d486962009-06-02 19:22:16 +02004075 struct perf_counter_attr attr;
Ingo Molnar04289bb2008-12-11 08:38:42 +01004076 struct perf_counter_context *ctx;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004077 struct file *counter_file = NULL;
Ingo Molnar04289bb2008-12-11 08:38:42 +01004078 struct file *group_file = NULL;
4079 int fput_needed = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004080 int fput_needed2 = 0;
Thomas Gleixner0793a612008-12-04 20:12:29 +01004081 int ret;
4082
Paul Mackerras2743a5b2009-03-04 20:36:51 +11004083 /* for future expandability... */
4084 if (flags)
4085 return -EINVAL;
4086
Peter Zijlstra974802e2009-06-12 12:46:55 +02004087 ret = perf_copy_attr(attr_uptr, &attr);
4088 if (ret)
4089 return ret;
Thomas Gleixnereab656a2008-12-08 19:26:59 +01004090
Peter Zijlstra07647712009-06-11 11:18:36 +02004091 if (!attr.exclude_kernel) {
4092 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
4093 return -EACCES;
4094 }
4095
Peter Zijlstradf58ab22009-06-11 11:25:05 +02004096 if (attr.freq) {
4097 if (attr.sample_freq > sysctl_perf_counter_sample_rate)
4098 return -EINVAL;
4099 }
4100
Ingo Molnar04289bb2008-12-11 08:38:42 +01004101 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01004102 * Get the target context (task or percpu):
4103 */
4104 ctx = find_get_context(pid, cpu);
4105 if (IS_ERR(ctx))
4106 return PTR_ERR(ctx);
4107
4108 /*
4109 * Look up the group leader (we will attach this counter to it):
Ingo Molnar04289bb2008-12-11 08:38:42 +01004110 */
4111 group_leader = NULL;
4112 if (group_fd != -1) {
4113 ret = -EINVAL;
4114 group_file = fget_light(group_fd, &fput_needed);
4115 if (!group_file)
Ingo Molnarccff2862008-12-11 11:26:29 +01004116 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01004117 if (group_file->f_op != &perf_fops)
Ingo Molnarccff2862008-12-11 11:26:29 +01004118 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01004119
4120 group_leader = group_file->private_data;
4121 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01004122 * Do not allow a recursive hierarchy (this new sibling
4123 * becoming part of another group-sibling):
Ingo Molnar04289bb2008-12-11 08:38:42 +01004124 */
Ingo Molnarccff2862008-12-11 11:26:29 +01004125 if (group_leader->group_leader != group_leader)
4126 goto err_put_context;
4127 /*
4128 * Do not allow to attach to a group in a different
4129 * task or CPU context:
4130 */
4131 if (group_leader->ctx != ctx)
4132 goto err_put_context;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11004133 /*
4134 * Only a group leader can be exclusive or pinned
4135 */
Peter Zijlstra0d486962009-06-02 19:22:16 +02004136 if (attr.exclusive || attr.pinned)
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11004137 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01004138 }
4139
Peter Zijlstra0d486962009-06-02 19:22:16 +02004140 counter = perf_counter_alloc(&attr, cpu, ctx, group_leader,
Peter Zijlstrab84fbc92009-06-22 13:57:40 +02004141 NULL, GFP_KERNEL);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02004142 ret = PTR_ERR(counter);
4143 if (IS_ERR(counter))
Thomas Gleixner0793a612008-12-04 20:12:29 +01004144 goto err_put_context;
4145
Thomas Gleixner0793a612008-12-04 20:12:29 +01004146 ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
4147 if (ret < 0)
Ingo Molnar9b51f662008-12-12 13:49:45 +01004148 goto err_free_put_context;
4149
4150 counter_file = fget_light(ret, &fput_needed2);
4151 if (!counter_file)
4152 goto err_free_put_context;
4153
4154 counter->filp = counter_file;
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004155 WARN_ON_ONCE(ctx->parent_ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +11004156 mutex_lock(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01004157 perf_install_in_context(ctx, counter, cpu);
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004158 ++ctx->generation;
Paul Mackerrasd859e292009-01-17 18:10:22 +11004159 mutex_unlock(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01004160
Peter Zijlstra082ff5a2009-05-23 18:29:00 +02004161 counter->owner = current;
4162 get_task_struct(current);
4163 mutex_lock(&current->perf_counter_mutex);
4164 list_add_tail(&counter->owner_entry, &current->perf_counter_list);
4165 mutex_unlock(&current->perf_counter_mutex);
4166
Ingo Molnar9b51f662008-12-12 13:49:45 +01004167 fput_light(counter_file, fput_needed2);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004168
Ingo Molnar04289bb2008-12-11 08:38:42 +01004169out_fput:
4170 fput_light(group_file, fput_needed);
4171
Thomas Gleixner0793a612008-12-04 20:12:29 +01004172 return ret;
4173
Ingo Molnar9b51f662008-12-12 13:49:45 +01004174err_free_put_context:
Thomas Gleixner0793a612008-12-04 20:12:29 +01004175 kfree(counter);
4176
4177err_put_context:
Paul Mackerrasc93f7662009-05-28 22:18:17 +10004178 put_ctx(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004179
Ingo Molnar04289bb2008-12-11 08:38:42 +01004180 goto out_fput;
Thomas Gleixner0793a612008-12-04 20:12:29 +01004181}
4182
Ingo Molnar9b51f662008-12-12 13:49:45 +01004183/*
Ingo Molnar9b51f662008-12-12 13:49:45 +01004184 * inherit a counter from parent task to child task:
4185 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11004186static struct perf_counter *
Ingo Molnar9b51f662008-12-12 13:49:45 +01004187inherit_counter(struct perf_counter *parent_counter,
4188 struct task_struct *parent,
4189 struct perf_counter_context *parent_ctx,
4190 struct task_struct *child,
Paul Mackerrasd859e292009-01-17 18:10:22 +11004191 struct perf_counter *group_leader,
Ingo Molnar9b51f662008-12-12 13:49:45 +01004192 struct perf_counter_context *child_ctx)
4193{
4194 struct perf_counter *child_counter;
4195
Paul Mackerrasd859e292009-01-17 18:10:22 +11004196 /*
4197 * Instead of creating recursive hierarchies of counters,
4198 * we link inherited counters back to the original parent,
4199 * which has a filp for sure, which we use as the reference
4200 * count:
4201 */
4202 if (parent_counter->parent)
4203 parent_counter = parent_counter->parent;
4204
Peter Zijlstra0d486962009-06-02 19:22:16 +02004205 child_counter = perf_counter_alloc(&parent_counter->attr,
Paul Mackerras23a185c2009-02-09 22:42:47 +11004206 parent_counter->cpu, child_ctx,
Peter Zijlstrab84fbc92009-06-22 13:57:40 +02004207 group_leader, parent_counter,
4208 GFP_KERNEL);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02004209 if (IS_ERR(child_counter))
4210 return child_counter;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10004211 get_ctx(child_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01004212
4213 /*
Paul Mackerras564c2b22009-05-22 14:27:22 +10004214 * Make the child state follow the state of the parent counter,
Peter Zijlstra0d486962009-06-02 19:22:16 +02004215 * not its attr.disabled bit. We hold the parent's mutex,
Ingo Molnar22a4f652009-06-01 10:13:37 +02004216 * so we won't race with perf_counter_{en, dis}able_family.
Paul Mackerras564c2b22009-05-22 14:27:22 +10004217 */
4218 if (parent_counter->state >= PERF_COUNTER_STATE_INACTIVE)
4219 child_counter->state = PERF_COUNTER_STATE_INACTIVE;
4220 else
4221 child_counter->state = PERF_COUNTER_STATE_OFF;
4222
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +02004223 if (parent_counter->attr.freq)
4224 child_counter->hw.sample_period = parent_counter->hw.sample_period;
4225
Paul Mackerras564c2b22009-05-22 14:27:22 +10004226 /*
Ingo Molnar9b51f662008-12-12 13:49:45 +01004227 * Link it up in the child's context:
4228 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +11004229 add_counter_to_ctx(child_counter, child_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01004230
Ingo Molnar9b51f662008-12-12 13:49:45 +01004231 /*
4232 * Get a reference to the parent filp - we will fput it
4233 * when the child counter exits. This is safe to do because
4234 * we are in the parent and we know that the filp still
4235 * exists and has a nonzero count:
4236 */
4237 atomic_long_inc(&parent_counter->filp->f_count);
4238
Paul Mackerrasd859e292009-01-17 18:10:22 +11004239 /*
4240 * Link this into the parent counter's child list
4241 */
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004242 WARN_ON_ONCE(parent_counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02004243 mutex_lock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11004244 list_add_tail(&child_counter->child_list, &parent_counter->child_list);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02004245 mutex_unlock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11004246
4247 return child_counter;
4248}
4249
4250static int inherit_group(struct perf_counter *parent_counter,
4251 struct task_struct *parent,
4252 struct perf_counter_context *parent_ctx,
4253 struct task_struct *child,
4254 struct perf_counter_context *child_ctx)
4255{
4256 struct perf_counter *leader;
4257 struct perf_counter *sub;
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02004258 struct perf_counter *child_ctr;
Paul Mackerrasd859e292009-01-17 18:10:22 +11004259
4260 leader = inherit_counter(parent_counter, parent, parent_ctx,
4261 child, NULL, child_ctx);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02004262 if (IS_ERR(leader))
4263 return PTR_ERR(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +11004264 list_for_each_entry(sub, &parent_counter->sibling_list, list_entry) {
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02004265 child_ctr = inherit_counter(sub, parent, parent_ctx,
4266 child, leader, child_ctx);
4267 if (IS_ERR(child_ctr))
4268 return PTR_ERR(child_ctr);
Paul Mackerrasd859e292009-01-17 18:10:22 +11004269 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01004270 return 0;
4271}
4272
Paul Mackerrasd859e292009-01-17 18:10:22 +11004273static void sync_child_counter(struct perf_counter *child_counter,
Peter Zijlstra38b200d2009-06-23 20:13:11 +02004274 struct task_struct *child)
Paul Mackerrasd859e292009-01-17 18:10:22 +11004275{
Peter Zijlstra38b200d2009-06-23 20:13:11 +02004276 struct perf_counter *parent_counter = child_counter->parent;
Peter Zijlstra8bc20952009-05-15 20:45:59 +02004277 u64 child_val;
Paul Mackerrasd859e292009-01-17 18:10:22 +11004278
Peter Zijlstrabfbd3382009-06-24 21:11:59 +02004279 if (child_counter->attr.inherit_stat)
4280 perf_counter_read_event(child_counter, child);
Peter Zijlstra38b200d2009-06-23 20:13:11 +02004281
Paul Mackerrasd859e292009-01-17 18:10:22 +11004282 child_val = atomic64_read(&child_counter->count);
4283
4284 /*
4285 * Add back the child's count to the parent's count:
4286 */
4287 atomic64_add(child_val, &parent_counter->count);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11004288 atomic64_add(child_counter->total_time_enabled,
4289 &parent_counter->child_total_time_enabled);
4290 atomic64_add(child_counter->total_time_running,
4291 &parent_counter->child_total_time_running);
Paul Mackerrasd859e292009-01-17 18:10:22 +11004292
4293 /*
4294 * Remove this counter from the parent's list
4295 */
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004296 WARN_ON_ONCE(parent_counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02004297 mutex_lock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11004298 list_del_init(&child_counter->child_list);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02004299 mutex_unlock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11004300
4301 /*
4302 * Release the parent counter, if this was the last
4303 * reference to it.
4304 */
4305 fput(parent_counter->filp);
4306}
4307
Ingo Molnar9b51f662008-12-12 13:49:45 +01004308static void
Peter Zijlstrabbbee902009-05-29 14:25:58 +02004309__perf_counter_exit_task(struct perf_counter *child_counter,
Peter Zijlstra38b200d2009-06-23 20:13:11 +02004310 struct perf_counter_context *child_ctx,
4311 struct task_struct *child)
Ingo Molnar9b51f662008-12-12 13:49:45 +01004312{
4313 struct perf_counter *parent_counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004314
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004315 update_counter_times(child_counter);
Peter Zijlstraaa9c67f2009-05-23 18:28:59 +02004316 perf_counter_remove_from_context(child_counter);
Ingo Molnar0cc0c022008-12-14 23:20:36 +01004317
Ingo Molnar9b51f662008-12-12 13:49:45 +01004318 parent_counter = child_counter->parent;
4319 /*
4320 * It can happen that parent exits first, and has counters
4321 * that are still around due to the child reference. These
4322 * counters need to be zapped - but otherwise linger.
4323 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11004324 if (parent_counter) {
Peter Zijlstra38b200d2009-06-23 20:13:11 +02004325 sync_child_counter(child_counter, child);
Peter Zijlstraf1600952009-03-19 20:26:16 +01004326 free_counter(child_counter);
Paul Mackerras4bcf3492009-02-11 13:53:19 +01004327 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01004328}
4329
4330/*
Paul Mackerrasd859e292009-01-17 18:10:22 +11004331 * When a child task exits, feed back counter values to parent counters.
Ingo Molnar9b51f662008-12-12 13:49:45 +01004332 */
4333void perf_counter_exit_task(struct task_struct *child)
4334{
4335 struct perf_counter *child_counter, *tmp;
4336 struct perf_counter_context *child_ctx;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004337 unsigned long flags;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004338
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02004339 if (likely(!child->perf_counter_ctxp)) {
Peter Zijlstra3a80b4a2009-08-07 19:49:01 +02004340 perf_counter_task(child, NULL, 0);
Ingo Molnar9b51f662008-12-12 13:49:45 +01004341 return;
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02004342 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01004343
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004344 local_irq_save(flags);
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004345 /*
4346 * We can't reschedule here because interrupts are disabled,
4347 * and either child is current or it is a task that can't be
4348 * scheduled, so we are now safe from rescheduling changing
4349 * our context.
4350 */
4351 child_ctx = child->perf_counter_ctxp;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004352 __perf_counter_task_sched_out(child_ctx);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10004353
4354 /*
4355 * Take the context lock here so that if find_get_context is
4356 * reading child->perf_counter_ctxp, we wait until it has
4357 * incremented the context's refcount before we do put_ctx below.
4358 */
4359 spin_lock(&child_ctx->lock);
Peter Zijlstra3a80b4a2009-08-07 19:49:01 +02004360 child->perf_counter_ctxp = NULL;
Peter Zijlstra71a851b2009-07-10 09:06:56 +02004361 /*
4362 * If this context is a clone; unclone it so it can't get
4363 * swapped to another process while we're removing all
4364 * the counters from it.
4365 */
4366 unclone_ctx(child_ctx);
Peter Zijlstra9f498cc2009-07-23 14:46:33 +02004367 spin_unlock_irqrestore(&child_ctx->lock, flags);
4368
4369 /*
4370 * Report the task dead after unscheduling the counters so that we
4371 * won't get any samples after PERF_EVENT_EXIT. We can however still
4372 * get a few PERF_EVENT_READ events.
4373 */
Peter Zijlstra3a80b4a2009-08-07 19:49:01 +02004374 perf_counter_task(child, child_ctx, 0);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004375
Peter Zijlstra66fff222009-06-10 22:53:37 +02004376 /*
4377 * We can recurse on the same lock type through:
4378 *
4379 * __perf_counter_exit_task()
4380 * sync_child_counter()
4381 * fput(parent_counter->filp)
4382 * perf_release()
4383 * mutex_lock(&ctx->mutex)
4384 *
4385 * But since its the parent context it won't be the same instance.
4386 */
4387 mutex_lock_nested(&child_ctx->mutex, SINGLE_DEPTH_NESTING);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004388
Peter Zijlstra8bc20952009-05-15 20:45:59 +02004389again:
Ingo Molnar9b51f662008-12-12 13:49:45 +01004390 list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list,
4391 list_entry)
Peter Zijlstra38b200d2009-06-23 20:13:11 +02004392 __perf_counter_exit_task(child_counter, child_ctx, child);
Peter Zijlstra8bc20952009-05-15 20:45:59 +02004393
4394 /*
4395 * If the last counter was a group counter, it will have appended all
4396 * its siblings to the list, but we obtained 'tmp' before that which
4397 * will still point to the list head terminating the iteration.
4398 */
4399 if (!list_empty(&child_ctx->counter_list))
4400 goto again;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004401
4402 mutex_unlock(&child_ctx->mutex);
4403
4404 put_ctx(child_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01004405}
4406
4407/*
Peter Zijlstrabbbee902009-05-29 14:25:58 +02004408 * free an unexposed, unused context as created by inheritance by
4409 * init_task below, used by fork() in case of fail.
4410 */
4411void perf_counter_free_task(struct task_struct *task)
4412{
4413 struct perf_counter_context *ctx = task->perf_counter_ctxp;
4414 struct perf_counter *counter, *tmp;
4415
4416 if (!ctx)
4417 return;
4418
4419 mutex_lock(&ctx->mutex);
4420again:
4421 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry) {
4422 struct perf_counter *parent = counter->parent;
4423
4424 if (WARN_ON_ONCE(!parent))
4425 continue;
4426
4427 mutex_lock(&parent->child_mutex);
4428 list_del_init(&counter->child_list);
4429 mutex_unlock(&parent->child_mutex);
4430
4431 fput(parent->filp);
4432
4433 list_del_counter(counter, ctx);
4434 free_counter(counter);
4435 }
4436
4437 if (!list_empty(&ctx->counter_list))
4438 goto again;
4439
4440 mutex_unlock(&ctx->mutex);
4441
4442 put_ctx(ctx);
4443}
4444
4445/*
Ingo Molnar9b51f662008-12-12 13:49:45 +01004446 * Initialize the perf_counter context in task_struct
4447 */
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02004448int perf_counter_init_task(struct task_struct *child)
Ingo Molnar9b51f662008-12-12 13:49:45 +01004449{
4450 struct perf_counter_context *child_ctx, *parent_ctx;
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004451 struct perf_counter_context *cloned_ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +11004452 struct perf_counter *counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004453 struct task_struct *parent = current;
Paul Mackerras564c2b22009-05-22 14:27:22 +10004454 int inherited_all = 1;
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02004455 int ret = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004456
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004457 child->perf_counter_ctxp = NULL;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004458
Peter Zijlstra082ff5a2009-05-23 18:29:00 +02004459 mutex_init(&child->perf_counter_mutex);
4460 INIT_LIST_HEAD(&child->perf_counter_list);
4461
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004462 if (likely(!parent->perf_counter_ctxp))
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02004463 return 0;
4464
Ingo Molnar9b51f662008-12-12 13:49:45 +01004465 /*
4466 * This is executed from the parent task context, so inherit
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004467 * counters that have been marked for cloning.
4468 * First allocate and initialize a context for the child.
Ingo Molnar9b51f662008-12-12 13:49:45 +01004469 */
4470
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004471 child_ctx = kmalloc(sizeof(struct perf_counter_context), GFP_KERNEL);
4472 if (!child_ctx)
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02004473 return -ENOMEM;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004474
4475 __perf_counter_init_context(child_ctx, child);
4476 child->perf_counter_ctxp = child_ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10004477 get_task_struct(child);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10004478
Ingo Molnar9b51f662008-12-12 13:49:45 +01004479 /*
Paul Mackerras25346b92009-06-01 17:48:12 +10004480 * If the parent's context is a clone, pin it so it won't get
4481 * swapped under us.
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004482 */
Paul Mackerras25346b92009-06-01 17:48:12 +10004483 parent_ctx = perf_pin_task_context(parent);
4484
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004485 /*
4486 * No need to check if parent_ctx != NULL here; since we saw
4487 * it non-NULL earlier, the only reason for it to become NULL
4488 * is if we exit, and since we're currently in the middle of
4489 * a fork we can't be exiting at the same time.
4490 */
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004491
4492 /*
Ingo Molnar9b51f662008-12-12 13:49:45 +01004493 * Lock the parent list. No need to lock the child - not PID
4494 * hashed yet and not running, so nobody can access it.
4495 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11004496 mutex_lock(&parent_ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01004497
4498 /*
4499 * We dont have to disable NMIs - we are only looking at
4500 * the list, not manipulating it:
4501 */
Peter Zijlstrad7b629a2009-05-20 12:21:19 +02004502 list_for_each_entry_rcu(counter, &parent_ctx->event_list, event_entry) {
4503 if (counter != counter->group_leader)
4504 continue;
4505
Peter Zijlstra0d486962009-06-02 19:22:16 +02004506 if (!counter->attr.inherit) {
Paul Mackerras564c2b22009-05-22 14:27:22 +10004507 inherited_all = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004508 continue;
Paul Mackerras564c2b22009-05-22 14:27:22 +10004509 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01004510
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02004511 ret = inherit_group(counter, parent, parent_ctx,
4512 child, child_ctx);
4513 if (ret) {
Paul Mackerras564c2b22009-05-22 14:27:22 +10004514 inherited_all = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004515 break;
Paul Mackerras564c2b22009-05-22 14:27:22 +10004516 }
4517 }
4518
4519 if (inherited_all) {
4520 /*
4521 * Mark the child context as a clone of the parent
4522 * context, or of whatever the parent is a clone of.
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004523 * Note that if the parent is a clone, it could get
4524 * uncloned at any point, but that doesn't matter
4525 * because the list of counters and the generation
4526 * count can't have changed since we took the mutex.
Paul Mackerras564c2b22009-05-22 14:27:22 +10004527 */
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004528 cloned_ctx = rcu_dereference(parent_ctx->parent_ctx);
4529 if (cloned_ctx) {
4530 child_ctx->parent_ctx = cloned_ctx;
Paul Mackerras25346b92009-06-01 17:48:12 +10004531 child_ctx->parent_gen = parent_ctx->parent_gen;
Paul Mackerras564c2b22009-05-22 14:27:22 +10004532 } else {
4533 child_ctx->parent_ctx = parent_ctx;
4534 child_ctx->parent_gen = parent_ctx->generation;
4535 }
4536 get_ctx(child_ctx->parent_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01004537 }
4538
Paul Mackerrasd859e292009-01-17 18:10:22 +11004539 mutex_unlock(&parent_ctx->mutex);
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02004540
Paul Mackerras25346b92009-06-01 17:48:12 +10004541 perf_unpin_context(parent_ctx);
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10004542
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02004543 return ret;
Ingo Molnar9b51f662008-12-12 13:49:45 +01004544}
4545
Ingo Molnar04289bb2008-12-11 08:38:42 +01004546static void __cpuinit perf_counter_init_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01004547{
Ingo Molnar04289bb2008-12-11 08:38:42 +01004548 struct perf_cpu_context *cpuctx;
Thomas Gleixner0793a612008-12-04 20:12:29 +01004549
Ingo Molnar04289bb2008-12-11 08:38:42 +01004550 cpuctx = &per_cpu(perf_cpu_context, cpu);
4551 __perf_counter_init_context(&cpuctx->ctx, NULL);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004552
Ingo Molnar1dce8d92009-05-04 19:23:18 +02004553 spin_lock(&perf_resource_lock);
Ingo Molnar04289bb2008-12-11 08:38:42 +01004554 cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu;
Ingo Molnar1dce8d92009-05-04 19:23:18 +02004555 spin_unlock(&perf_resource_lock);
Ingo Molnar04289bb2008-12-11 08:38:42 +01004556
Paul Mackerras01d02872009-01-14 13:44:19 +11004557 hw_perf_counter_setup(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004558}
4559
4560#ifdef CONFIG_HOTPLUG_CPU
Ingo Molnar04289bb2008-12-11 08:38:42 +01004561static void __perf_counter_exit_cpu(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01004562{
4563 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
4564 struct perf_counter_context *ctx = &cpuctx->ctx;
4565 struct perf_counter *counter, *tmp;
4566
Ingo Molnar04289bb2008-12-11 08:38:42 +01004567 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry)
4568 __perf_counter_remove_from_context(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004569}
Ingo Molnar04289bb2008-12-11 08:38:42 +01004570static void perf_counter_exit_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01004571{
Paul Mackerrasd859e292009-01-17 18:10:22 +11004572 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
4573 struct perf_counter_context *ctx = &cpuctx->ctx;
4574
4575 mutex_lock(&ctx->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01004576 smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1);
Paul Mackerrasd859e292009-01-17 18:10:22 +11004577 mutex_unlock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004578}
4579#else
Ingo Molnar04289bb2008-12-11 08:38:42 +01004580static inline void perf_counter_exit_cpu(int cpu) { }
Thomas Gleixner0793a612008-12-04 20:12:29 +01004581#endif
4582
4583static int __cpuinit
4584perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
4585{
4586 unsigned int cpu = (long)hcpu;
4587
4588 switch (action) {
4589
4590 case CPU_UP_PREPARE:
4591 case CPU_UP_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01004592 perf_counter_init_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004593 break;
4594
4595 case CPU_DOWN_PREPARE:
4596 case CPU_DOWN_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01004597 perf_counter_exit_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004598 break;
4599
4600 default:
4601 break;
4602 }
4603
4604 return NOTIFY_OK;
4605}
4606
Paul Mackerrasf38b0822009-06-02 21:05:16 +10004607/*
4608 * This has to have a higher priority than migration_notifier in sched.c.
4609 */
Thomas Gleixner0793a612008-12-04 20:12:29 +01004610static struct notifier_block __cpuinitdata perf_cpu_nb = {
4611 .notifier_call = perf_cpu_notify,
Paul Mackerrasf38b0822009-06-02 21:05:16 +10004612 .priority = 20,
Thomas Gleixner0793a612008-12-04 20:12:29 +01004613};
4614
Ingo Molnar0d905bc2009-05-04 19:13:30 +02004615void __init perf_counter_init(void)
Thomas Gleixner0793a612008-12-04 20:12:29 +01004616{
4617 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
4618 (void *)(long)smp_processor_id());
4619 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);