blob: 78c58623a0dd207ee6856c2378c73913b446efc1 [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;
44static atomic_t nr_munmap_counters __read_mostly;
45static atomic_t nr_comm_counters __read_mostly;
Peter Zijlstra9ee318a2009-04-09 10:53:44 +020046
Peter Zijlstra1ccd1542009-04-09 10:53:45 +020047int sysctl_perf_counter_priv __read_mostly; /* do we need to be privileged */
Peter Zijlstra789f90f2009-05-15 15:19:27 +020048int sysctl_perf_counter_mlock __read_mostly = 512; /* 'free' kb per user */
Peter Zijlstraa78ac322009-05-25 17:39:05 +020049int sysctl_perf_counter_limit __read_mostly = 100000; /* max NMIs per second */
Peter Zijlstra1ccd1542009-04-09 10:53:45 +020050
Peter Zijlstraa96bbc12009-06-03 14:01:36 +020051static atomic64_t perf_counter_id;
52
Thomas Gleixner0793a612008-12-04 20:12:29 +010053/*
Ingo Molnar1dce8d92009-05-04 19:23:18 +020054 * Lock for (sysadmin-configurable) counter reservations:
Thomas Gleixner0793a612008-12-04 20:12:29 +010055 */
Ingo Molnar1dce8d92009-05-04 19:23:18 +020056static DEFINE_SPINLOCK(perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +010057
58/*
59 * Architecture provided APIs - weak aliases:
60 */
Robert Richter4aeb0b42009-04-29 12:47:03 +020061extern __weak const struct pmu *hw_perf_counter_init(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +010062{
Paul Mackerrasff6f0542009-01-09 16:19:25 +110063 return NULL;
Thomas Gleixner0793a612008-12-04 20:12:29 +010064}
65
Peter Zijlstra9e35ad32009-05-13 16:21:38 +020066void __weak hw_perf_disable(void) { barrier(); }
67void __weak hw_perf_enable(void) { barrier(); }
68
Paul Mackerras01d02872009-01-14 13:44:19 +110069void __weak hw_perf_counter_setup(int cpu) { barrier(); }
Ingo Molnar22a4f652009-06-01 10:13:37 +020070
71int __weak
72hw_perf_group_sched_in(struct perf_counter *group_leader,
Paul Mackerras3cbed422009-01-09 16:43:42 +110073 struct perf_cpu_context *cpuctx,
74 struct perf_counter_context *ctx, int cpu)
75{
76 return 0;
77}
Thomas Gleixner0793a612008-12-04 20:12:29 +010078
Paul Mackerras4eb96fc2009-01-09 17:24:34 +110079void __weak perf_counter_print_debug(void) { }
80
Peter Zijlstra9e35ad32009-05-13 16:21:38 +020081static DEFINE_PER_CPU(int, disable_count);
82
83void __perf_disable(void)
84{
85 __get_cpu_var(disable_count)++;
86}
87
88bool __perf_enable(void)
89{
90 return !--__get_cpu_var(disable_count);
91}
92
93void perf_disable(void)
94{
95 __perf_disable();
96 hw_perf_disable();
97}
Peter Zijlstra9e35ad32009-05-13 16:21:38 +020098
99void perf_enable(void)
100{
101 if (__perf_enable())
102 hw_perf_enable();
103}
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200104
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000105static void get_ctx(struct perf_counter_context *ctx)
106{
107 atomic_inc(&ctx->refcount);
108}
109
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000110static void free_ctx(struct rcu_head *head)
111{
112 struct perf_counter_context *ctx;
113
114 ctx = container_of(head, struct perf_counter_context, rcu_head);
115 kfree(ctx);
116}
117
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000118static void put_ctx(struct perf_counter_context *ctx)
119{
Paul Mackerras564c2b22009-05-22 14:27:22 +1000120 if (atomic_dec_and_test(&ctx->refcount)) {
121 if (ctx->parent_ctx)
122 put_ctx(ctx->parent_ctx);
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000123 if (ctx->task)
124 put_task_struct(ctx->task);
125 call_rcu(&ctx->rcu_head, free_ctx);
Paul Mackerras564c2b22009-05-22 14:27:22 +1000126 }
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000127}
128
Peter Zijlstrafccc7142009-05-23 18:28:56 +0200129/*
Paul Mackerras25346b932009-06-01 17:48:12 +1000130 * Get the perf_counter_context for a task and lock it.
131 * This has to cope with with the fact that until it is locked,
132 * the context could get moved to another task.
133 */
Ingo Molnar22a4f652009-06-01 10:13:37 +0200134static struct perf_counter_context *
135perf_lock_task_context(struct task_struct *task, unsigned long *flags)
Paul Mackerras25346b932009-06-01 17:48:12 +1000136{
137 struct perf_counter_context *ctx;
138
139 rcu_read_lock();
140 retry:
141 ctx = rcu_dereference(task->perf_counter_ctxp);
142 if (ctx) {
143 /*
144 * If this context is a clone of another, it might
145 * get swapped for another underneath us by
146 * perf_counter_task_sched_out, though the
147 * rcu_read_lock() protects us from any context
148 * getting freed. Lock the context and check if it
149 * got swapped before we could get the lock, and retry
150 * if so. If we locked the right context, then it
151 * can't get swapped on us any more.
152 */
153 spin_lock_irqsave(&ctx->lock, *flags);
154 if (ctx != rcu_dereference(task->perf_counter_ctxp)) {
155 spin_unlock_irqrestore(&ctx->lock, *flags);
156 goto retry;
157 }
158 }
159 rcu_read_unlock();
160 return ctx;
161}
162
163/*
164 * Get the context for a task and increment its pin_count so it
165 * can't get swapped to another task. This also increments its
166 * reference count so that the context can't get freed.
167 */
168static struct perf_counter_context *perf_pin_task_context(struct task_struct *task)
169{
170 struct perf_counter_context *ctx;
171 unsigned long flags;
172
173 ctx = perf_lock_task_context(task, &flags);
174 if (ctx) {
175 ++ctx->pin_count;
176 get_ctx(ctx);
177 spin_unlock_irqrestore(&ctx->lock, flags);
178 }
179 return ctx;
180}
181
182static void perf_unpin_context(struct perf_counter_context *ctx)
183{
184 unsigned long flags;
185
186 spin_lock_irqsave(&ctx->lock, flags);
187 --ctx->pin_count;
188 spin_unlock_irqrestore(&ctx->lock, flags);
189 put_ctx(ctx);
190}
191
192/*
Peter Zijlstrafccc7142009-05-23 18:28:56 +0200193 * Add a counter from the lists for its context.
194 * Must be called with ctx->mutex and ctx->lock held.
195 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100196static void
197list_add_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
198{
199 struct perf_counter *group_leader = counter->group_leader;
200
201 /*
202 * Depending on whether it is a standalone or sibling counter,
203 * add it straight to the context's counter list, or to the group
204 * leader's sibling list:
205 */
Peter Zijlstra3df5eda2009-05-08 18:52:22 +0200206 if (group_leader == counter)
Ingo Molnar04289bb2008-12-11 08:38:42 +0100207 list_add_tail(&counter->list_entry, &ctx->counter_list);
Peter Zijlstra5c148192009-03-25 12:30:23 +0100208 else {
Ingo Molnar04289bb2008-12-11 08:38:42 +0100209 list_add_tail(&counter->list_entry, &group_leader->sibling_list);
Peter Zijlstra5c148192009-03-25 12:30:23 +0100210 group_leader->nr_siblings++;
211 }
Peter Zijlstra592903c2009-03-13 12:21:36 +0100212
213 list_add_rcu(&counter->event_entry, &ctx->event_list);
Peter Zijlstra8bc20952009-05-15 20:45:59 +0200214 ctx->nr_counters++;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100215}
216
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000217/*
218 * Remove a counter from the lists for its context.
Peter Zijlstrafccc7142009-05-23 18:28:56 +0200219 * Must be called with ctx->mutex and ctx->lock held.
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000220 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100221static void
222list_del_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
223{
224 struct perf_counter *sibling, *tmp;
225
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000226 if (list_empty(&counter->list_entry))
227 return;
Peter Zijlstra8bc20952009-05-15 20:45:59 +0200228 ctx->nr_counters--;
229
Ingo Molnar04289bb2008-12-11 08:38:42 +0100230 list_del_init(&counter->list_entry);
Peter Zijlstra592903c2009-03-13 12:21:36 +0100231 list_del_rcu(&counter->event_entry);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100232
Peter Zijlstra5c148192009-03-25 12:30:23 +0100233 if (counter->group_leader != counter)
234 counter->group_leader->nr_siblings--;
235
Ingo Molnar04289bb2008-12-11 08:38:42 +0100236 /*
237 * If this was a group counter with sibling counters then
238 * upgrade the siblings to singleton counters by adding them
239 * to the context list directly:
240 */
241 list_for_each_entry_safe(sibling, tmp,
242 &counter->sibling_list, list_entry) {
243
Peter Zijlstra75564232009-03-13 12:21:29 +0100244 list_move_tail(&sibling->list_entry, &ctx->counter_list);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100245 sibling->group_leader = sibling;
246 }
247}
248
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100249static void
250counter_sched_out(struct perf_counter *counter,
251 struct perf_cpu_context *cpuctx,
252 struct perf_counter_context *ctx)
253{
254 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
255 return;
256
257 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200258 counter->tstamp_stopped = ctx->time;
Robert Richter4aeb0b42009-04-29 12:47:03 +0200259 counter->pmu->disable(counter);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100260 counter->oncpu = -1;
261
262 if (!is_software_counter(counter))
263 cpuctx->active_oncpu--;
264 ctx->nr_active--;
Peter Zijlstra0d486962009-06-02 19:22:16 +0200265 if (counter->attr.exclusive || !cpuctx->active_oncpu)
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100266 cpuctx->exclusive = 0;
267}
268
Paul Mackerrasd859e292009-01-17 18:10:22 +1100269static void
270group_sched_out(struct perf_counter *group_counter,
271 struct perf_cpu_context *cpuctx,
272 struct perf_counter_context *ctx)
273{
274 struct perf_counter *counter;
275
276 if (group_counter->state != PERF_COUNTER_STATE_ACTIVE)
277 return;
278
279 counter_sched_out(group_counter, cpuctx, ctx);
280
281 /*
282 * Schedule out siblings (if any):
283 */
284 list_for_each_entry(counter, &group_counter->sibling_list, list_entry)
285 counter_sched_out(counter, cpuctx, ctx);
286
Peter Zijlstra0d486962009-06-02 19:22:16 +0200287 if (group_counter->attr.exclusive)
Paul Mackerrasd859e292009-01-17 18:10:22 +1100288 cpuctx->exclusive = 0;
289}
290
Thomas Gleixner0793a612008-12-04 20:12:29 +0100291/*
292 * Cross CPU call to remove a performance counter
293 *
294 * We disable the counter on the hardware level first. After that we
295 * remove it from the context list.
296 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100297static void __perf_counter_remove_from_context(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100298{
299 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
300 struct perf_counter *counter = info;
301 struct perf_counter_context *ctx = counter->ctx;
302
303 /*
304 * If this is a task context, we need to check whether it is
305 * the current task context of this cpu. If not it has been
306 * scheduled out before the smp call arrived.
307 */
Peter Zijlstra665c2142009-05-29 14:51:57 +0200308 if (ctx->task && cpuctx->task_ctx != ctx)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100309 return;
310
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200311 spin_lock(&ctx->lock);
Ingo Molnar34adc802009-05-20 20:13:28 +0200312 /*
313 * Protect the list operation against NMI by disabling the
314 * counters on a global level.
315 */
316 perf_disable();
Thomas Gleixner0793a612008-12-04 20:12:29 +0100317
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100318 counter_sched_out(counter, cpuctx, ctx);
319
Ingo Molnar04289bb2008-12-11 08:38:42 +0100320 list_del_counter(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100321
322 if (!ctx->task) {
323 /*
324 * Allow more per task counters with respect to the
325 * reservation:
326 */
327 cpuctx->max_pertask =
328 min(perf_max_counters - ctx->nr_counters,
329 perf_max_counters - perf_reserved_percpu);
330 }
331
Ingo Molnar34adc802009-05-20 20:13:28 +0200332 perf_enable();
Peter Zijlstra665c2142009-05-29 14:51:57 +0200333 spin_unlock(&ctx->lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100334}
335
336
337/*
338 * Remove the counter from a task's (or a CPU's) list of counters.
339 *
Peter Zijlstrafccc7142009-05-23 18:28:56 +0200340 * Must be called with ctx->mutex held.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100341 *
342 * CPU counters are removed with a smp call. For task counters we only
343 * call when the task is on a CPU.
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000344 *
345 * If counter->ctx is a cloned context, callers must make sure that
346 * every task struct that counter->ctx->task could possibly point to
347 * remains valid. This is OK when called from perf_release since
348 * that only calls us on the top-level context, which can't be a clone.
349 * When called from perf_counter_exit_task, it's OK because the
350 * context has been detached from its task.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100351 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100352static void perf_counter_remove_from_context(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100353{
354 struct perf_counter_context *ctx = counter->ctx;
355 struct task_struct *task = ctx->task;
356
357 if (!task) {
358 /*
359 * Per cpu counters are removed via an smp call and
360 * the removal is always sucessful.
361 */
362 smp_call_function_single(counter->cpu,
Ingo Molnar04289bb2008-12-11 08:38:42 +0100363 __perf_counter_remove_from_context,
Thomas Gleixner0793a612008-12-04 20:12:29 +0100364 counter, 1);
365 return;
366 }
367
368retry:
Ingo Molnar04289bb2008-12-11 08:38:42 +0100369 task_oncpu_function_call(task, __perf_counter_remove_from_context,
Thomas Gleixner0793a612008-12-04 20:12:29 +0100370 counter);
371
372 spin_lock_irq(&ctx->lock);
373 /*
374 * If the context is active we need to retry the smp call.
375 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100376 if (ctx->nr_active && !list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100377 spin_unlock_irq(&ctx->lock);
378 goto retry;
379 }
380
381 /*
382 * The lock prevents that this context is scheduled in so we
Ingo Molnar04289bb2008-12-11 08:38:42 +0100383 * can remove the counter safely, if the call above did not
Thomas Gleixner0793a612008-12-04 20:12:29 +0100384 * succeed.
385 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100386 if (!list_empty(&counter->list_entry)) {
Ingo Molnar04289bb2008-12-11 08:38:42 +0100387 list_del_counter(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100388 }
389 spin_unlock_irq(&ctx->lock);
390}
391
Peter Zijlstra4af49982009-04-06 11:45:10 +0200392static inline u64 perf_clock(void)
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100393{
Peter Zijlstra4af49982009-04-06 11:45:10 +0200394 return cpu_clock(smp_processor_id());
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100395}
396
397/*
398 * Update the record of the current time in a context.
399 */
Peter Zijlstra4af49982009-04-06 11:45:10 +0200400static void update_context_time(struct perf_counter_context *ctx)
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100401{
Peter Zijlstra4af49982009-04-06 11:45:10 +0200402 u64 now = perf_clock();
403
404 ctx->time += now - ctx->timestamp;
405 ctx->timestamp = now;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100406}
407
408/*
409 * Update the total_time_enabled and total_time_running fields for a counter.
410 */
411static void update_counter_times(struct perf_counter *counter)
412{
413 struct perf_counter_context *ctx = counter->ctx;
414 u64 run_end;
415
Peter Zijlstra4af49982009-04-06 11:45:10 +0200416 if (counter->state < PERF_COUNTER_STATE_INACTIVE)
417 return;
418
419 counter->total_time_enabled = ctx->time - counter->tstamp_enabled;
420
421 if (counter->state == PERF_COUNTER_STATE_INACTIVE)
422 run_end = counter->tstamp_stopped;
423 else
424 run_end = ctx->time;
425
426 counter->total_time_running = run_end - counter->tstamp_running;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100427}
428
429/*
430 * Update total_time_enabled and total_time_running for all counters in a group.
431 */
432static void update_group_times(struct perf_counter *leader)
433{
434 struct perf_counter *counter;
435
436 update_counter_times(leader);
437 list_for_each_entry(counter, &leader->sibling_list, list_entry)
438 update_counter_times(counter);
439}
440
441/*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100442 * Cross CPU call to disable a performance counter
443 */
444static void __perf_counter_disable(void *info)
445{
446 struct perf_counter *counter = info;
447 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
448 struct perf_counter_context *ctx = counter->ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100449
450 /*
451 * If this is a per-task counter, need to check whether this
452 * counter's task is the current task on this cpu.
453 */
Peter Zijlstra665c2142009-05-29 14:51:57 +0200454 if (ctx->task && cpuctx->task_ctx != ctx)
Paul Mackerrasd859e292009-01-17 18:10:22 +1100455 return;
456
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200457 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100458
459 /*
460 * If the counter is on, turn it off.
461 * If it is in error state, leave it in error state.
462 */
463 if (counter->state >= PERF_COUNTER_STATE_INACTIVE) {
Peter Zijlstra4af49982009-04-06 11:45:10 +0200464 update_context_time(ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100465 update_counter_times(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100466 if (counter == counter->group_leader)
467 group_sched_out(counter, cpuctx, ctx);
468 else
469 counter_sched_out(counter, cpuctx, ctx);
470 counter->state = PERF_COUNTER_STATE_OFF;
471 }
472
Peter Zijlstra665c2142009-05-29 14:51:57 +0200473 spin_unlock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100474}
475
476/*
477 * Disable a counter.
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000478 *
479 * If counter->ctx is a cloned context, callers must make sure that
480 * every task struct that counter->ctx->task could possibly point to
481 * remains valid. This condition is satisifed when called through
482 * perf_counter_for_each_child or perf_counter_for_each because they
483 * hold the top-level counter's child_mutex, so any descendant that
484 * goes to exit will block in sync_child_counter.
485 * When called from perf_pending_counter it's OK because counter->ctx
486 * is the current context on this CPU and preemption is disabled,
487 * hence we can't get into perf_counter_task_sched_out for this context.
Paul Mackerrasd859e292009-01-17 18:10:22 +1100488 */
489static void perf_counter_disable(struct perf_counter *counter)
490{
491 struct perf_counter_context *ctx = counter->ctx;
492 struct task_struct *task = ctx->task;
493
494 if (!task) {
495 /*
496 * Disable the counter on the cpu that it's on
497 */
498 smp_call_function_single(counter->cpu, __perf_counter_disable,
499 counter, 1);
500 return;
501 }
502
503 retry:
504 task_oncpu_function_call(task, __perf_counter_disable, counter);
505
506 spin_lock_irq(&ctx->lock);
507 /*
508 * If the counter is still active, we need to retry the cross-call.
509 */
510 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
511 spin_unlock_irq(&ctx->lock);
512 goto retry;
513 }
514
515 /*
516 * Since we have the lock this context can't be scheduled
517 * in, so we can change the state safely.
518 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100519 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
520 update_counter_times(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100521 counter->state = PERF_COUNTER_STATE_OFF;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100522 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100523
524 spin_unlock_irq(&ctx->lock);
525}
526
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100527static int
528counter_sched_in(struct perf_counter *counter,
529 struct perf_cpu_context *cpuctx,
530 struct perf_counter_context *ctx,
531 int cpu)
532{
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100533 if (counter->state <= PERF_COUNTER_STATE_OFF)
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100534 return 0;
535
536 counter->state = PERF_COUNTER_STATE_ACTIVE;
537 counter->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */
538 /*
539 * The new state must be visible before we turn it on in the hardware:
540 */
541 smp_wmb();
542
Robert Richter4aeb0b42009-04-29 12:47:03 +0200543 if (counter->pmu->enable(counter)) {
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100544 counter->state = PERF_COUNTER_STATE_INACTIVE;
545 counter->oncpu = -1;
546 return -EAGAIN;
547 }
548
Peter Zijlstra4af49982009-04-06 11:45:10 +0200549 counter->tstamp_running += ctx->time - counter->tstamp_stopped;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100550
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100551 if (!is_software_counter(counter))
552 cpuctx->active_oncpu++;
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100553 ctx->nr_active++;
554
Peter Zijlstra0d486962009-06-02 19:22:16 +0200555 if (counter->attr.exclusive)
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100556 cpuctx->exclusive = 1;
557
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100558 return 0;
559}
560
Paul Mackerras6751b712009-05-11 12:08:02 +1000561static int
562group_sched_in(struct perf_counter *group_counter,
563 struct perf_cpu_context *cpuctx,
564 struct perf_counter_context *ctx,
565 int cpu)
566{
567 struct perf_counter *counter, *partial_group;
568 int ret;
569
570 if (group_counter->state == PERF_COUNTER_STATE_OFF)
571 return 0;
572
573 ret = hw_perf_group_sched_in(group_counter, cpuctx, ctx, cpu);
574 if (ret)
575 return ret < 0 ? ret : 0;
576
Paul Mackerras6751b712009-05-11 12:08:02 +1000577 if (counter_sched_in(group_counter, cpuctx, ctx, cpu))
578 return -EAGAIN;
579
580 /*
581 * Schedule in siblings as one group (if any):
582 */
583 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
Paul Mackerras6751b712009-05-11 12:08:02 +1000584 if (counter_sched_in(counter, cpuctx, ctx, cpu)) {
585 partial_group = counter;
586 goto group_error;
587 }
588 }
589
590 return 0;
591
592group_error:
593 /*
594 * Groups can be scheduled in as one unit only, so undo any
595 * partial group before returning:
596 */
597 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
598 if (counter == partial_group)
599 break;
600 counter_sched_out(counter, cpuctx, ctx);
601 }
602 counter_sched_out(group_counter, cpuctx, ctx);
603
604 return -EAGAIN;
605}
606
Thomas Gleixner0793a612008-12-04 20:12:29 +0100607/*
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100608 * Return 1 for a group consisting entirely of software counters,
609 * 0 if the group contains any hardware counters.
610 */
611static int is_software_only_group(struct perf_counter *leader)
612{
613 struct perf_counter *counter;
614
615 if (!is_software_counter(leader))
616 return 0;
Peter Zijlstra5c148192009-03-25 12:30:23 +0100617
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100618 list_for_each_entry(counter, &leader->sibling_list, list_entry)
619 if (!is_software_counter(counter))
620 return 0;
Peter Zijlstra5c148192009-03-25 12:30:23 +0100621
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100622 return 1;
623}
624
625/*
626 * Work out whether we can put this counter group on the CPU now.
627 */
628static int group_can_go_on(struct perf_counter *counter,
629 struct perf_cpu_context *cpuctx,
630 int can_add_hw)
631{
632 /*
633 * Groups consisting entirely of software counters can always go on.
634 */
635 if (is_software_only_group(counter))
636 return 1;
637 /*
638 * If an exclusive group is already on, no other hardware
639 * counters can go on.
640 */
641 if (cpuctx->exclusive)
642 return 0;
643 /*
644 * If this group is exclusive and there are already
645 * counters on the CPU, it can't go on.
646 */
Peter Zijlstra0d486962009-06-02 19:22:16 +0200647 if (counter->attr.exclusive && cpuctx->active_oncpu)
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100648 return 0;
649 /*
650 * Otherwise, try to add it if all previous groups were able
651 * to go on.
652 */
653 return can_add_hw;
654}
655
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100656static void add_counter_to_ctx(struct perf_counter *counter,
657 struct perf_counter_context *ctx)
658{
659 list_add_counter(counter, ctx);
Peter Zijlstra4af49982009-04-06 11:45:10 +0200660 counter->tstamp_enabled = ctx->time;
661 counter->tstamp_running = ctx->time;
662 counter->tstamp_stopped = ctx->time;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100663}
664
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100665/*
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100666 * Cross CPU call to install and enable a performance counter
Peter Zijlstra682076a2009-05-23 18:28:57 +0200667 *
668 * Must be called with ctx->mutex held
Thomas Gleixner0793a612008-12-04 20:12:29 +0100669 */
670static void __perf_install_in_context(void *info)
671{
672 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
673 struct perf_counter *counter = info;
674 struct perf_counter_context *ctx = counter->ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100675 struct perf_counter *leader = counter->group_leader;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100676 int cpu = smp_processor_id();
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100677 int err;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100678
679 /*
680 * If this is a task context, we need to check whether it is
681 * the current task context of this cpu. If not it has been
682 * scheduled out before the smp call arrived.
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000683 * Or possibly this is the right context but it isn't
684 * on this cpu because it had no counters.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100685 */
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000686 if (ctx->task && cpuctx->task_ctx != ctx) {
Peter Zijlstra665c2142009-05-29 14:51:57 +0200687 if (cpuctx->task_ctx || ctx->task != current)
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000688 return;
689 cpuctx->task_ctx = ctx;
690 }
Thomas Gleixner0793a612008-12-04 20:12:29 +0100691
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200692 spin_lock(&ctx->lock);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000693 ctx->is_active = 1;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200694 update_context_time(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100695
696 /*
697 * Protect the list operation against NMI by disabling the
698 * counters on a global level. NOP for non NMI based counters.
699 */
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200700 perf_disable();
Thomas Gleixner0793a612008-12-04 20:12:29 +0100701
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100702 add_counter_to_ctx(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100703
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100704 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100705 * Don't put the counter on if it is disabled or if
706 * it is in a group and the group isn't on.
707 */
708 if (counter->state != PERF_COUNTER_STATE_INACTIVE ||
709 (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE))
710 goto unlock;
711
712 /*
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100713 * An exclusive counter can't go on if there are already active
714 * hardware counters, and no hardware counter can go on if there
715 * is already an exclusive counter on.
716 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100717 if (!group_can_go_on(counter, cpuctx, 1))
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100718 err = -EEXIST;
719 else
720 err = counter_sched_in(counter, cpuctx, ctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100721
Paul Mackerrasd859e292009-01-17 18:10:22 +1100722 if (err) {
723 /*
724 * This counter couldn't go on. If it is in a group
725 * then we have to pull the whole group off.
726 * If the counter group is pinned then put it in error state.
727 */
728 if (leader != counter)
729 group_sched_out(leader, cpuctx, ctx);
Peter Zijlstra0d486962009-06-02 19:22:16 +0200730 if (leader->attr.pinned) {
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100731 update_group_times(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100732 leader->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100733 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100734 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100735
736 if (!err && !ctx->task && cpuctx->max_pertask)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100737 cpuctx->max_pertask--;
738
Paul Mackerrasd859e292009-01-17 18:10:22 +1100739 unlock:
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200740 perf_enable();
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100741
Peter Zijlstra665c2142009-05-29 14:51:57 +0200742 spin_unlock(&ctx->lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100743}
744
745/*
746 * Attach a performance counter to a context
747 *
748 * First we add the counter to the list with the hardware enable bit
749 * in counter->hw_config cleared.
750 *
751 * If the counter is attached to a task which is on a CPU we use a smp
752 * call to enable it in the task context. The task might have been
753 * scheduled away, but we check this in the smp call again.
Paul Mackerrasd859e292009-01-17 18:10:22 +1100754 *
755 * Must be called with ctx->mutex held.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100756 */
757static void
758perf_install_in_context(struct perf_counter_context *ctx,
759 struct perf_counter *counter,
760 int cpu)
761{
762 struct task_struct *task = ctx->task;
763
Thomas Gleixner0793a612008-12-04 20:12:29 +0100764 if (!task) {
765 /*
766 * Per cpu counters are installed via an smp call and
767 * the install is always sucessful.
768 */
769 smp_call_function_single(cpu, __perf_install_in_context,
770 counter, 1);
771 return;
772 }
773
Thomas Gleixner0793a612008-12-04 20:12:29 +0100774retry:
775 task_oncpu_function_call(task, __perf_install_in_context,
776 counter);
777
778 spin_lock_irq(&ctx->lock);
779 /*
Thomas Gleixner0793a612008-12-04 20:12:29 +0100780 * we need to retry the smp call.
781 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100782 if (ctx->is_active && list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100783 spin_unlock_irq(&ctx->lock);
784 goto retry;
785 }
786
787 /*
788 * The lock prevents that this context is scheduled in so we
789 * can add the counter safely, if it the call above did not
790 * succeed.
791 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100792 if (list_empty(&counter->list_entry))
793 add_counter_to_ctx(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100794 spin_unlock_irq(&ctx->lock);
795}
796
Paul Mackerrasd859e292009-01-17 18:10:22 +1100797/*
798 * Cross CPU call to enable a performance counter
799 */
800static void __perf_counter_enable(void *info)
Ingo Molnar04289bb2008-12-11 08:38:42 +0100801{
Paul Mackerrasd859e292009-01-17 18:10:22 +1100802 struct perf_counter *counter = info;
803 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
804 struct perf_counter_context *ctx = counter->ctx;
805 struct perf_counter *leader = counter->group_leader;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100806 int err;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100807
808 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100809 * If this is a per-task counter, need to check whether this
810 * counter's task is the current task on this cpu.
Ingo Molnar04289bb2008-12-11 08:38:42 +0100811 */
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000812 if (ctx->task && cpuctx->task_ctx != ctx) {
Peter Zijlstra665c2142009-05-29 14:51:57 +0200813 if (cpuctx->task_ctx || ctx->task != current)
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000814 return;
815 cpuctx->task_ctx = ctx;
816 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100817
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200818 spin_lock(&ctx->lock);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000819 ctx->is_active = 1;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200820 update_context_time(ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100821
822 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
823 goto unlock;
824 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200825 counter->tstamp_enabled = ctx->time - counter->total_time_enabled;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100826
827 /*
828 * If the counter is in a group and isn't the group leader,
829 * then don't put it on unless the group is on.
830 */
831 if (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE)
832 goto unlock;
833
Paul Mackerrase758a332009-05-12 21:59:01 +1000834 if (!group_can_go_on(counter, cpuctx, 1)) {
Paul Mackerrasd859e292009-01-17 18:10:22 +1100835 err = -EEXIST;
Paul Mackerrase758a332009-05-12 21:59:01 +1000836 } else {
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200837 perf_disable();
Paul Mackerrase758a332009-05-12 21:59:01 +1000838 if (counter == leader)
839 err = group_sched_in(counter, cpuctx, ctx,
840 smp_processor_id());
841 else
842 err = counter_sched_in(counter, cpuctx, ctx,
843 smp_processor_id());
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200844 perf_enable();
Paul Mackerrase758a332009-05-12 21:59:01 +1000845 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100846
847 if (err) {
848 /*
849 * If this counter can't go on and it's part of a
850 * group, then the whole group has to come off.
851 */
852 if (leader != counter)
853 group_sched_out(leader, cpuctx, ctx);
Peter Zijlstra0d486962009-06-02 19:22:16 +0200854 if (leader->attr.pinned) {
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100855 update_group_times(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100856 leader->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100857 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100858 }
859
860 unlock:
Peter Zijlstra665c2142009-05-29 14:51:57 +0200861 spin_unlock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100862}
863
864/*
865 * Enable a counter.
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000866 *
867 * If counter->ctx is a cloned context, callers must make sure that
868 * every task struct that counter->ctx->task could possibly point to
869 * remains valid. This condition is satisfied when called through
870 * perf_counter_for_each_child or perf_counter_for_each as described
871 * for perf_counter_disable.
Paul Mackerrasd859e292009-01-17 18:10:22 +1100872 */
873static void perf_counter_enable(struct perf_counter *counter)
874{
875 struct perf_counter_context *ctx = counter->ctx;
876 struct task_struct *task = ctx->task;
877
878 if (!task) {
879 /*
880 * Enable the counter on the cpu that it's on
881 */
882 smp_call_function_single(counter->cpu, __perf_counter_enable,
883 counter, 1);
884 return;
885 }
886
887 spin_lock_irq(&ctx->lock);
888 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
889 goto out;
890
891 /*
892 * If the counter is in error state, clear that first.
893 * That way, if we see the counter in error state below, we
894 * know that it has gone back into error state, as distinct
895 * from the task having been scheduled away before the
896 * cross-call arrived.
897 */
898 if (counter->state == PERF_COUNTER_STATE_ERROR)
899 counter->state = PERF_COUNTER_STATE_OFF;
900
901 retry:
902 spin_unlock_irq(&ctx->lock);
903 task_oncpu_function_call(task, __perf_counter_enable, counter);
904
905 spin_lock_irq(&ctx->lock);
906
907 /*
908 * If the context is active and the counter is still off,
909 * we need to retry the cross-call.
910 */
911 if (ctx->is_active && counter->state == PERF_COUNTER_STATE_OFF)
912 goto retry;
913
914 /*
915 * Since we have the lock this context can't be scheduled
916 * in, so we can change the state safely.
917 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100918 if (counter->state == PERF_COUNTER_STATE_OFF) {
Paul Mackerrasd859e292009-01-17 18:10:22 +1100919 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200920 counter->tstamp_enabled =
921 ctx->time - counter->total_time_enabled;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100922 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100923 out:
924 spin_unlock_irq(&ctx->lock);
925}
926
Peter Zijlstra2023b352009-05-05 17:50:26 +0200927static int perf_counter_refresh(struct perf_counter *counter, int refresh)
Peter Zijlstra79f14642009-04-06 11:45:07 +0200928{
Peter Zijlstra2023b352009-05-05 17:50:26 +0200929 /*
930 * not supported on inherited counters
931 */
Peter Zijlstra0d486962009-06-02 19:22:16 +0200932 if (counter->attr.inherit)
Peter Zijlstra2023b352009-05-05 17:50:26 +0200933 return -EINVAL;
934
Peter Zijlstra79f14642009-04-06 11:45:07 +0200935 atomic_add(refresh, &counter->event_limit);
936 perf_counter_enable(counter);
Peter Zijlstra2023b352009-05-05 17:50:26 +0200937
938 return 0;
Peter Zijlstra79f14642009-04-06 11:45:07 +0200939}
940
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100941void __perf_counter_sched_out(struct perf_counter_context *ctx,
942 struct perf_cpu_context *cpuctx)
943{
944 struct perf_counter *counter;
945
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100946 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100947 ctx->is_active = 0;
948 if (likely(!ctx->nr_counters))
949 goto out;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200950 update_context_time(ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100951
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200952 perf_disable();
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100953 if (ctx->nr_active) {
Peter Zijlstraafedadf2009-05-20 12:21:22 +0200954 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
955 if (counter != counter->group_leader)
956 counter_sched_out(counter, cpuctx, ctx);
957 else
958 group_sched_out(counter, cpuctx, ctx);
959 }
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100960 }
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200961 perf_enable();
Paul Mackerrasd859e292009-01-17 18:10:22 +1100962 out:
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100963 spin_unlock(&ctx->lock);
964}
965
Thomas Gleixner0793a612008-12-04 20:12:29 +0100966/*
Paul Mackerras564c2b22009-05-22 14:27:22 +1000967 * Test whether two contexts are equivalent, i.e. whether they
968 * have both been cloned from the same version of the same context
969 * and they both have the same number of enabled counters.
970 * If the number of enabled counters is the same, then the set
971 * of enabled counters should be the same, because these are both
972 * inherited contexts, therefore we can't access individual counters
973 * in them directly with an fd; we can only enable/disable all
974 * counters via prctl, or enable/disable all counters in a family
975 * via ioctl, which will have the same effect on both contexts.
976 */
977static int context_equiv(struct perf_counter_context *ctx1,
978 struct perf_counter_context *ctx2)
979{
980 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
Paul Mackerrasad3a37d2009-05-29 16:06:20 +1000981 && ctx1->parent_gen == ctx2->parent_gen
Paul Mackerras25346b932009-06-01 17:48:12 +1000982 && !ctx1->pin_count && !ctx2->pin_count;
Paul Mackerras564c2b22009-05-22 14:27:22 +1000983}
984
985/*
Thomas Gleixner0793a612008-12-04 20:12:29 +0100986 * Called from scheduler to remove the counters of the current task,
987 * with interrupts disabled.
988 *
989 * We stop each counter and update the counter value in counter->count.
990 *
Ingo Molnar76715812008-12-17 14:20:28 +0100991 * This does not protect us against NMI, but disable()
Thomas Gleixner0793a612008-12-04 20:12:29 +0100992 * sets the disabled bit in the control field of counter _before_
993 * accessing the counter control register. If a NMI hits, then it will
994 * not restart the counter.
995 */
Paul Mackerras564c2b22009-05-22 14:27:22 +1000996void perf_counter_task_sched_out(struct task_struct *task,
997 struct task_struct *next, int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100998{
999 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001000 struct perf_counter_context *ctx = task->perf_counter_ctxp;
Paul Mackerras564c2b22009-05-22 14:27:22 +10001001 struct perf_counter_context *next_ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001002 struct perf_counter_context *parent;
Peter Zijlstra4a0deca2009-03-19 20:26:12 +01001003 struct pt_regs *regs;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001004 int do_switch = 1;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001005
Peter Zijlstra10989fb2009-05-25 14:45:28 +02001006 regs = task_pt_regs(task);
1007 perf_swcounter_event(PERF_COUNT_CONTEXT_SWITCHES, 1, 1, regs, 0);
1008
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001009 if (likely(!ctx || !cpuctx->task_ctx))
Thomas Gleixner0793a612008-12-04 20:12:29 +01001010 return;
1011
Peter Zijlstrabce379b2009-04-06 11:45:13 +02001012 update_context_time(ctx);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001013
1014 rcu_read_lock();
1015 parent = rcu_dereference(ctx->parent_ctx);
Paul Mackerras564c2b22009-05-22 14:27:22 +10001016 next_ctx = next->perf_counter_ctxp;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001017 if (parent && next_ctx &&
1018 rcu_dereference(next_ctx->parent_ctx) == parent) {
1019 /*
1020 * Looks like the two contexts are clones, so we might be
1021 * able to optimize the context switch. We lock both
1022 * contexts and check that they are clones under the
1023 * lock (including re-checking that neither has been
1024 * uncloned in the meantime). It doesn't matter which
1025 * order we take the locks because no other cpu could
1026 * be trying to lock both of these tasks.
1027 */
1028 spin_lock(&ctx->lock);
1029 spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
1030 if (context_equiv(ctx, next_ctx)) {
Peter Zijlstra665c2142009-05-29 14:51:57 +02001031 /*
1032 * XXX do we need a memory barrier of sorts
1033 * wrt to rcu_dereference() of perf_counter_ctxp
1034 */
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001035 task->perf_counter_ctxp = next_ctx;
1036 next->perf_counter_ctxp = ctx;
1037 ctx->task = next;
1038 next_ctx->task = task;
1039 do_switch = 0;
1040 }
1041 spin_unlock(&next_ctx->lock);
1042 spin_unlock(&ctx->lock);
Paul Mackerras564c2b22009-05-22 14:27:22 +10001043 }
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001044 rcu_read_unlock();
Paul Mackerras564c2b22009-05-22 14:27:22 +10001045
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001046 if (do_switch) {
1047 __perf_counter_sched_out(ctx, cpuctx);
1048 cpuctx->task_ctx = NULL;
1049 }
Thomas Gleixner0793a612008-12-04 20:12:29 +01001050}
1051
Peter Zijlstra665c2142009-05-29 14:51:57 +02001052/*
1053 * Called with IRQs disabled
1054 */
Paul Mackerrasa08b1592009-05-11 15:46:10 +10001055static void __perf_counter_task_sched_out(struct perf_counter_context *ctx)
1056{
1057 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1058
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001059 if (!cpuctx->task_ctx)
1060 return;
Ingo Molnar012b84d2009-05-17 11:08:41 +02001061
1062 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
1063 return;
1064
Paul Mackerrasa08b1592009-05-11 15:46:10 +10001065 __perf_counter_sched_out(ctx, cpuctx);
1066 cpuctx->task_ctx = NULL;
1067}
1068
Peter Zijlstra665c2142009-05-29 14:51:57 +02001069/*
1070 * Called with IRQs disabled
1071 */
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001072static void perf_counter_cpu_sched_out(struct perf_cpu_context *cpuctx)
Ingo Molnar04289bb2008-12-11 08:38:42 +01001073{
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001074 __perf_counter_sched_out(&cpuctx->ctx, cpuctx);
Ingo Molnar04289bb2008-12-11 08:38:42 +01001075}
1076
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001077static void
1078__perf_counter_sched_in(struct perf_counter_context *ctx,
1079 struct perf_cpu_context *cpuctx, int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001080{
Thomas Gleixner0793a612008-12-04 20:12:29 +01001081 struct perf_counter *counter;
Paul Mackerrasdd0e6ba2009-01-12 15:11:00 +11001082 int can_add_hw = 1;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001083
Thomas Gleixner0793a612008-12-04 20:12:29 +01001084 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001085 ctx->is_active = 1;
1086 if (likely(!ctx->nr_counters))
1087 goto out;
1088
Peter Zijlstra4af49982009-04-06 11:45:10 +02001089 ctx->timestamp = perf_clock();
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001090
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001091 perf_disable();
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001092
1093 /*
1094 * First go through the list and put on any pinned groups
1095 * in order to give them the best chance of going on.
1096 */
Ingo Molnar04289bb2008-12-11 08:38:42 +01001097 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001098 if (counter->state <= PERF_COUNTER_STATE_OFF ||
Peter Zijlstra0d486962009-06-02 19:22:16 +02001099 !counter->attr.pinned)
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001100 continue;
1101 if (counter->cpu != -1 && counter->cpu != cpu)
1102 continue;
1103
Peter Zijlstraafedadf2009-05-20 12:21:22 +02001104 if (counter != counter->group_leader)
1105 counter_sched_in(counter, cpuctx, ctx, cpu);
1106 else {
1107 if (group_can_go_on(counter, cpuctx, 1))
1108 group_sched_in(counter, cpuctx, ctx, cpu);
1109 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001110
1111 /*
1112 * If this pinned group hasn't been scheduled,
1113 * put it in error state.
1114 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001115 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
1116 update_group_times(counter);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001117 counter->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001118 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001119 }
1120
1121 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1122 /*
1123 * Ignore counters in OFF or ERROR state, and
1124 * ignore pinned counters since we did them already.
1125 */
1126 if (counter->state <= PERF_COUNTER_STATE_OFF ||
Peter Zijlstra0d486962009-06-02 19:22:16 +02001127 counter->attr.pinned)
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001128 continue;
1129
Ingo Molnar04289bb2008-12-11 08:38:42 +01001130 /*
1131 * Listen to the 'cpu' scheduling filter constraint
1132 * of counters:
1133 */
Thomas Gleixner0793a612008-12-04 20:12:29 +01001134 if (counter->cpu != -1 && counter->cpu != cpu)
1135 continue;
1136
Peter Zijlstraafedadf2009-05-20 12:21:22 +02001137 if (counter != counter->group_leader) {
1138 if (counter_sched_in(counter, cpuctx, ctx, cpu))
Paul Mackerrasdd0e6ba2009-01-12 15:11:00 +11001139 can_add_hw = 0;
Peter Zijlstraafedadf2009-05-20 12:21:22 +02001140 } else {
1141 if (group_can_go_on(counter, cpuctx, can_add_hw)) {
1142 if (group_sched_in(counter, cpuctx, ctx, cpu))
1143 can_add_hw = 0;
1144 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001145 }
Thomas Gleixner0793a612008-12-04 20:12:29 +01001146 }
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001147 perf_enable();
Paul Mackerrasd859e292009-01-17 18:10:22 +11001148 out:
Thomas Gleixner0793a612008-12-04 20:12:29 +01001149 spin_unlock(&ctx->lock);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001150}
Ingo Molnar04289bb2008-12-11 08:38:42 +01001151
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001152/*
1153 * Called from scheduler to add the counters of the current task
1154 * with interrupts disabled.
1155 *
1156 * We restore the counter value and then enable it.
1157 *
1158 * This does not protect us against NMI, but enable()
1159 * sets the enabled bit in the control field of counter _before_
1160 * accessing the counter control register. If a NMI hits, then it will
1161 * keep the counter running.
1162 */
1163void perf_counter_task_sched_in(struct task_struct *task, int cpu)
1164{
1165 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001166 struct perf_counter_context *ctx = task->perf_counter_ctxp;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001167
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001168 if (likely(!ctx))
1169 return;
Paul Mackerras564c2b22009-05-22 14:27:22 +10001170 if (cpuctx->task_ctx == ctx)
1171 return;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001172 __perf_counter_sched_in(ctx, cpuctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001173 cpuctx->task_ctx = ctx;
1174}
1175
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001176static void perf_counter_cpu_sched_in(struct perf_cpu_context *cpuctx, int cpu)
1177{
1178 struct perf_counter_context *ctx = &cpuctx->ctx;
1179
1180 __perf_counter_sched_in(ctx, cpuctx, cpu);
1181}
1182
Peter Zijlstraa78ac322009-05-25 17:39:05 +02001183#define MAX_INTERRUPTS (~0ULL)
1184
1185static void perf_log_throttle(struct perf_counter *counter, int enable);
Peter Zijlstra26b119b2009-05-20 12:21:20 +02001186static void perf_log_period(struct perf_counter *counter, u64 period);
1187
1188static void perf_adjust_freq(struct perf_counter_context *ctx)
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001189{
1190 struct perf_counter *counter;
Peter Zijlstrab23f3322009-06-02 15:13:03 +02001191 u64 interrupts, sample_period;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001192 u64 events, period;
1193 s64 delta;
1194
1195 spin_lock(&ctx->lock);
1196 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1197 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
1198 continue;
1199
Peter Zijlstraa78ac322009-05-25 17:39:05 +02001200 interrupts = counter->hw.interrupts;
1201 counter->hw.interrupts = 0;
1202
1203 if (interrupts == MAX_INTERRUPTS) {
1204 perf_log_throttle(counter, 1);
1205 counter->pmu->unthrottle(counter);
1206 interrupts = 2*sysctl_perf_counter_limit/HZ;
1207 }
1208
Peter Zijlstra0d486962009-06-02 19:22:16 +02001209 if (!counter->attr.freq || !counter->attr.sample_freq)
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001210 continue;
1211
Peter Zijlstrab23f3322009-06-02 15:13:03 +02001212 events = HZ * interrupts * counter->hw.sample_period;
Peter Zijlstra0d486962009-06-02 19:22:16 +02001213 period = div64_u64(events, counter->attr.sample_freq);
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001214
Peter Zijlstrab23f3322009-06-02 15:13:03 +02001215 delta = (s64)(1 + period - counter->hw.sample_period);
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001216 delta >>= 1;
1217
Peter Zijlstrab23f3322009-06-02 15:13:03 +02001218 sample_period = counter->hw.sample_period + delta;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001219
Peter Zijlstrab23f3322009-06-02 15:13:03 +02001220 if (!sample_period)
1221 sample_period = 1;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001222
Peter Zijlstrab23f3322009-06-02 15:13:03 +02001223 perf_log_period(counter, sample_period);
Peter Zijlstra26b119b2009-05-20 12:21:20 +02001224
Peter Zijlstrab23f3322009-06-02 15:13:03 +02001225 counter->hw.sample_period = sample_period;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001226 }
1227 spin_unlock(&ctx->lock);
1228}
1229
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001230/*
1231 * Round-robin a context's counters:
1232 */
1233static void rotate_ctx(struct perf_counter_context *ctx)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001234{
Thomas Gleixner0793a612008-12-04 20:12:29 +01001235 struct perf_counter *counter;
1236
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001237 if (!ctx->nr_counters)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001238 return;
1239
Thomas Gleixner0793a612008-12-04 20:12:29 +01001240 spin_lock(&ctx->lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001241 /*
Ingo Molnar04289bb2008-12-11 08:38:42 +01001242 * Rotate the first entry last (works just fine for group counters too):
Thomas Gleixner0793a612008-12-04 20:12:29 +01001243 */
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001244 perf_disable();
Ingo Molnar04289bb2008-12-11 08:38:42 +01001245 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Peter Zijlstra75564232009-03-13 12:21:29 +01001246 list_move_tail(&counter->list_entry, &ctx->counter_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001247 break;
1248 }
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001249 perf_enable();
Thomas Gleixner0793a612008-12-04 20:12:29 +01001250
1251 spin_unlock(&ctx->lock);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001252}
Thomas Gleixner0793a612008-12-04 20:12:29 +01001253
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001254void perf_counter_task_tick(struct task_struct *curr, int cpu)
1255{
Peter Zijlstra7fc23a52009-05-08 18:52:21 +02001256 struct perf_cpu_context *cpuctx;
1257 struct perf_counter_context *ctx;
1258
1259 if (!atomic_read(&nr_counters))
1260 return;
1261
1262 cpuctx = &per_cpu(perf_cpu_context, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001263 ctx = curr->perf_counter_ctxp;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001264
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001265 perf_adjust_freq(&cpuctx->ctx);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001266 if (ctx)
1267 perf_adjust_freq(ctx);
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001268
Ingo Molnarb82914c2009-05-04 18:54:32 +02001269 perf_counter_cpu_sched_out(cpuctx);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001270 if (ctx)
1271 __perf_counter_task_sched_out(ctx);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001272
Ingo Molnarb82914c2009-05-04 18:54:32 +02001273 rotate_ctx(&cpuctx->ctx);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001274 if (ctx)
1275 rotate_ctx(ctx);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001276
Ingo Molnarb82914c2009-05-04 18:54:32 +02001277 perf_counter_cpu_sched_in(cpuctx, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001278 if (ctx)
1279 perf_counter_task_sched_in(curr, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001280}
1281
1282/*
Thomas Gleixner0793a612008-12-04 20:12:29 +01001283 * Cross CPU call to read the hardware counter
1284 */
Ingo Molnar76715812008-12-17 14:20:28 +01001285static void __read(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001286{
Ingo Molnar621a01e2008-12-11 12:46:46 +01001287 struct perf_counter *counter = info;
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001288 struct perf_counter_context *ctx = counter->ctx;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001289 unsigned long flags;
Ingo Molnar621a01e2008-12-11 12:46:46 +01001290
Peter Zijlstra849691a2009-04-06 11:45:12 +02001291 local_irq_save(flags);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001292 if (ctx->is_active)
Peter Zijlstra4af49982009-04-06 11:45:10 +02001293 update_context_time(ctx);
Robert Richter4aeb0b42009-04-29 12:47:03 +02001294 counter->pmu->read(counter);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001295 update_counter_times(counter);
Peter Zijlstra849691a2009-04-06 11:45:12 +02001296 local_irq_restore(flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001297}
1298
Ingo Molnar04289bb2008-12-11 08:38:42 +01001299static u64 perf_counter_read(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001300{
1301 /*
1302 * If counter is enabled and currently active on a CPU, update the
1303 * value in the counter structure:
1304 */
Ingo Molnar6a930702008-12-11 15:17:03 +01001305 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
Thomas Gleixner0793a612008-12-04 20:12:29 +01001306 smp_call_function_single(counter->oncpu,
Ingo Molnar76715812008-12-17 14:20:28 +01001307 __read, counter, 1);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001308 } else if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
1309 update_counter_times(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001310 }
1311
Ingo Molnaree060942008-12-13 09:00:03 +01001312 return atomic64_read(&counter->count);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001313}
1314
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001315/*
1316 * Initialize the perf_counter context in a task_struct:
1317 */
1318static void
1319__perf_counter_init_context(struct perf_counter_context *ctx,
1320 struct task_struct *task)
1321{
1322 memset(ctx, 0, sizeof(*ctx));
1323 spin_lock_init(&ctx->lock);
1324 mutex_init(&ctx->mutex);
1325 INIT_LIST_HEAD(&ctx->counter_list);
1326 INIT_LIST_HEAD(&ctx->event_list);
1327 atomic_set(&ctx->refcount, 1);
1328 ctx->task = task;
1329}
1330
Thomas Gleixner0793a612008-12-04 20:12:29 +01001331static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
1332{
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001333 struct perf_counter_context *parent_ctx;
Ingo Molnar22a4f652009-06-01 10:13:37 +02001334 struct perf_counter_context *ctx;
1335 struct perf_cpu_context *cpuctx;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001336 struct task_struct *task;
Paul Mackerras25346b932009-06-01 17:48:12 +10001337 unsigned long flags;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001338 int err;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001339
1340 /*
1341 * If cpu is not a wildcard then this is a percpu counter:
1342 */
1343 if (cpu != -1) {
1344 /* Must be root to operate on a CPU counter: */
Peter Zijlstra1ccd1542009-04-09 10:53:45 +02001345 if (sysctl_perf_counter_priv && !capable(CAP_SYS_ADMIN))
Thomas Gleixner0793a612008-12-04 20:12:29 +01001346 return ERR_PTR(-EACCES);
1347
1348 if (cpu < 0 || cpu > num_possible_cpus())
1349 return ERR_PTR(-EINVAL);
1350
1351 /*
1352 * We could be clever and allow to attach a counter to an
1353 * offline CPU and activate it when the CPU comes up, but
1354 * that's for later.
1355 */
1356 if (!cpu_isset(cpu, cpu_online_map))
1357 return ERR_PTR(-ENODEV);
1358
1359 cpuctx = &per_cpu(perf_cpu_context, cpu);
1360 ctx = &cpuctx->ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001361 get_ctx(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001362
Thomas Gleixner0793a612008-12-04 20:12:29 +01001363 return ctx;
1364 }
1365
1366 rcu_read_lock();
1367 if (!pid)
1368 task = current;
1369 else
1370 task = find_task_by_vpid(pid);
1371 if (task)
1372 get_task_struct(task);
1373 rcu_read_unlock();
1374
1375 if (!task)
1376 return ERR_PTR(-ESRCH);
1377
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001378 /*
1379 * Can't attach counters to a dying task.
1380 */
1381 err = -ESRCH;
1382 if (task->flags & PF_EXITING)
1383 goto errout;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001384
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001385 /* Reuse ptrace permission checks for now. */
1386 err = -EACCES;
1387 if (!ptrace_may_access(task, PTRACE_MODE_READ))
1388 goto errout;
1389
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001390 retry:
Paul Mackerras25346b932009-06-01 17:48:12 +10001391 ctx = perf_lock_task_context(task, &flags);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001392 if (ctx) {
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001393 parent_ctx = ctx->parent_ctx;
1394 if (parent_ctx) {
1395 put_ctx(parent_ctx);
1396 ctx->parent_ctx = NULL; /* no longer a clone */
1397 }
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001398 /*
1399 * Get an extra reference before dropping the lock so that
1400 * this context won't get freed if the task exits.
1401 */
1402 get_ctx(ctx);
Paul Mackerras25346b932009-06-01 17:48:12 +10001403 spin_unlock_irqrestore(&ctx->lock, flags);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001404 }
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001405
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001406 if (!ctx) {
1407 ctx = kmalloc(sizeof(struct perf_counter_context), GFP_KERNEL);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001408 err = -ENOMEM;
1409 if (!ctx)
1410 goto errout;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001411 __perf_counter_init_context(ctx, task);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001412 get_ctx(ctx);
1413 if (cmpxchg(&task->perf_counter_ctxp, NULL, ctx)) {
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001414 /*
1415 * We raced with some other task; use
1416 * the context they set.
1417 */
1418 kfree(ctx);
Paul Mackerras25346b932009-06-01 17:48:12 +10001419 goto retry;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001420 }
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001421 get_task_struct(task);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001422 }
1423
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001424 put_task_struct(task);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001425 return ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001426
1427 errout:
1428 put_task_struct(task);
1429 return ERR_PTR(err);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001430}
1431
Peter Zijlstra592903c2009-03-13 12:21:36 +01001432static void free_counter_rcu(struct rcu_head *head)
1433{
1434 struct perf_counter *counter;
1435
1436 counter = container_of(head, struct perf_counter, rcu_head);
Peter Zijlstra709e50c2009-06-02 14:13:15 +02001437 if (counter->ns)
1438 put_pid_ns(counter->ns);
Peter Zijlstra592903c2009-03-13 12:21:36 +01001439 kfree(counter);
1440}
1441
Peter Zijlstra925d5192009-03-30 19:07:02 +02001442static void perf_pending_sync(struct perf_counter *counter);
1443
Peter Zijlstraf1600952009-03-19 20:26:16 +01001444static void free_counter(struct perf_counter *counter)
1445{
Peter Zijlstra925d5192009-03-30 19:07:02 +02001446 perf_pending_sync(counter);
1447
Peter Zijlstra7fc23a52009-05-08 18:52:21 +02001448 atomic_dec(&nr_counters);
Peter Zijlstra0d486962009-06-02 19:22:16 +02001449 if (counter->attr.mmap)
Peter Zijlstra60313eb2009-06-04 16:53:44 +02001450 atomic_dec(&nr_mmap_counters);
Peter Zijlstra0d486962009-06-02 19:22:16 +02001451 if (counter->attr.munmap)
Peter Zijlstra60313eb2009-06-04 16:53:44 +02001452 atomic_dec(&nr_munmap_counters);
Peter Zijlstra0d486962009-06-02 19:22:16 +02001453 if (counter->attr.comm)
Peter Zijlstra60313eb2009-06-04 16:53:44 +02001454 atomic_dec(&nr_comm_counters);
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02001455
Peter Zijlstrae077df42009-03-19 20:26:17 +01001456 if (counter->destroy)
1457 counter->destroy(counter);
1458
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001459 put_ctx(counter->ctx);
Peter Zijlstraf1600952009-03-19 20:26:16 +01001460 call_rcu(&counter->rcu_head, free_counter_rcu);
1461}
1462
Thomas Gleixner0793a612008-12-04 20:12:29 +01001463/*
1464 * Called when the last reference to the file is gone.
1465 */
1466static int perf_release(struct inode *inode, struct file *file)
1467{
1468 struct perf_counter *counter = file->private_data;
1469 struct perf_counter_context *ctx = counter->ctx;
1470
1471 file->private_data = NULL;
1472
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001473 WARN_ON_ONCE(ctx->parent_ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001474 mutex_lock(&ctx->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01001475 perf_counter_remove_from_context(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001476 mutex_unlock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001477
Peter Zijlstra082ff5a2009-05-23 18:29:00 +02001478 mutex_lock(&counter->owner->perf_counter_mutex);
1479 list_del_init(&counter->owner_entry);
1480 mutex_unlock(&counter->owner->perf_counter_mutex);
1481 put_task_struct(counter->owner);
1482
Peter Zijlstraf1600952009-03-19 20:26:16 +01001483 free_counter(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001484
1485 return 0;
1486}
1487
1488/*
1489 * Read the performance counter - simple non blocking version for now
1490 */
1491static ssize_t
1492perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
1493{
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001494 u64 values[3];
1495 int n;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001496
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001497 /*
1498 * Return end-of-file for a read on a counter that is in
1499 * error state (i.e. because it was pinned but it couldn't be
1500 * scheduled on to the CPU at some point).
1501 */
1502 if (counter->state == PERF_COUNTER_STATE_ERROR)
1503 return 0;
1504
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001505 WARN_ON_ONCE(counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001506 mutex_lock(&counter->child_mutex);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001507 values[0] = perf_counter_read(counter);
1508 n = 1;
Peter Zijlstra0d486962009-06-02 19:22:16 +02001509 if (counter->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001510 values[n++] = counter->total_time_enabled +
1511 atomic64_read(&counter->child_total_time_enabled);
Peter Zijlstra0d486962009-06-02 19:22:16 +02001512 if (counter->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001513 values[n++] = counter->total_time_running +
1514 atomic64_read(&counter->child_total_time_running);
Peter Zijlstra0d486962009-06-02 19:22:16 +02001515 if (counter->attr.read_format & PERF_FORMAT_ID)
Peter Zijlstra8e5799b2009-06-02 15:08:15 +02001516 values[n++] = counter->id;
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001517 mutex_unlock(&counter->child_mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001518
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001519 if (count < n * sizeof(u64))
1520 return -EINVAL;
1521 count = n * sizeof(u64);
1522
1523 if (copy_to_user(buf, values, count))
1524 return -EFAULT;
1525
1526 return count;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001527}
1528
1529static ssize_t
Thomas Gleixner0793a612008-12-04 20:12:29 +01001530perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
1531{
1532 struct perf_counter *counter = file->private_data;
1533
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001534 return perf_read_hw(counter, buf, count);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001535}
1536
1537static unsigned int perf_poll(struct file *file, poll_table *wait)
1538{
1539 struct perf_counter *counter = file->private_data;
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001540 struct perf_mmap_data *data;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001541 unsigned int events = POLL_HUP;
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001542
1543 rcu_read_lock();
1544 data = rcu_dereference(counter->data);
1545 if (data)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001546 events = atomic_xchg(&data->poll, 0);
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001547 rcu_read_unlock();
Thomas Gleixner0793a612008-12-04 20:12:29 +01001548
1549 poll_wait(file, &counter->waitq, wait);
1550
Thomas Gleixner0793a612008-12-04 20:12:29 +01001551 return events;
1552}
1553
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001554static void perf_counter_reset(struct perf_counter *counter)
1555{
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001556 (void)perf_counter_read(counter);
Paul Mackerras615a3f12009-05-11 15:50:21 +10001557 atomic64_set(&counter->count, 0);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001558 perf_counter_update_userpage(counter);
1559}
1560
1561static void perf_counter_for_each_sibling(struct perf_counter *counter,
1562 void (*func)(struct perf_counter *))
1563{
1564 struct perf_counter_context *ctx = counter->ctx;
1565 struct perf_counter *sibling;
1566
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001567 WARN_ON_ONCE(ctx->parent_ctx);
Peter Zijlstra682076a2009-05-23 18:28:57 +02001568 mutex_lock(&ctx->mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001569 counter = counter->group_leader;
1570
1571 func(counter);
1572 list_for_each_entry(sibling, &counter->sibling_list, list_entry)
1573 func(sibling);
Peter Zijlstra682076a2009-05-23 18:28:57 +02001574 mutex_unlock(&ctx->mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001575}
1576
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001577/*
1578 * Holding the top-level counter's child_mutex means that any
1579 * descendant process that has inherited this counter will block
1580 * in sync_child_counter if it goes to exit, thus satisfying the
1581 * task existence requirements of perf_counter_enable/disable.
1582 */
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001583static void perf_counter_for_each_child(struct perf_counter *counter,
1584 void (*func)(struct perf_counter *))
1585{
1586 struct perf_counter *child;
1587
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001588 WARN_ON_ONCE(counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001589 mutex_lock(&counter->child_mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001590 func(counter);
1591 list_for_each_entry(child, &counter->child_list, child_list)
1592 func(child);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001593 mutex_unlock(&counter->child_mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001594}
1595
1596static void perf_counter_for_each(struct perf_counter *counter,
1597 void (*func)(struct perf_counter *))
1598{
1599 struct perf_counter *child;
1600
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001601 WARN_ON_ONCE(counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001602 mutex_lock(&counter->child_mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001603 perf_counter_for_each_sibling(counter, func);
1604 list_for_each_entry(child, &counter->child_list, child_list)
1605 perf_counter_for_each_sibling(child, func);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001606 mutex_unlock(&counter->child_mutex);
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001607}
1608
Peter Zijlstra08247e32009-06-02 16:46:57 +02001609static int perf_counter_period(struct perf_counter *counter, u64 __user *arg)
1610{
1611 struct perf_counter_context *ctx = counter->ctx;
1612 unsigned long size;
1613 int ret = 0;
1614 u64 value;
1615
Peter Zijlstra0d486962009-06-02 19:22:16 +02001616 if (!counter->attr.sample_period)
Peter Zijlstra08247e32009-06-02 16:46:57 +02001617 return -EINVAL;
1618
1619 size = copy_from_user(&value, arg, sizeof(value));
1620 if (size != sizeof(value))
1621 return -EFAULT;
1622
1623 if (!value)
1624 return -EINVAL;
1625
1626 spin_lock_irq(&ctx->lock);
Peter Zijlstra0d486962009-06-02 19:22:16 +02001627 if (counter->attr.freq) {
Peter Zijlstra08247e32009-06-02 16:46:57 +02001628 if (value > sysctl_perf_counter_limit) {
1629 ret = -EINVAL;
1630 goto unlock;
1631 }
1632
Peter Zijlstra0d486962009-06-02 19:22:16 +02001633 counter->attr.sample_freq = value;
Peter Zijlstra08247e32009-06-02 16:46:57 +02001634 } else {
Peter Zijlstra0d486962009-06-02 19:22:16 +02001635 counter->attr.sample_period = value;
Peter Zijlstra08247e32009-06-02 16:46:57 +02001636 counter->hw.sample_period = value;
1637
1638 perf_log_period(counter, value);
1639 }
1640unlock:
1641 spin_unlock_irq(&ctx->lock);
1642
1643 return ret;
1644}
1645
Paul Mackerrasd859e292009-01-17 18:10:22 +11001646static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1647{
1648 struct perf_counter *counter = file->private_data;
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001649 void (*func)(struct perf_counter *);
1650 u32 flags = arg;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001651
1652 switch (cmd) {
1653 case PERF_COUNTER_IOC_ENABLE:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001654 func = perf_counter_enable;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001655 break;
1656 case PERF_COUNTER_IOC_DISABLE:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001657 func = perf_counter_disable;
Peter Zijlstra79f14642009-04-06 11:45:07 +02001658 break;
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001659 case PERF_COUNTER_IOC_RESET:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001660 func = perf_counter_reset;
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001661 break;
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001662
1663 case PERF_COUNTER_IOC_REFRESH:
1664 return perf_counter_refresh(counter, arg);
Peter Zijlstra08247e32009-06-02 16:46:57 +02001665
1666 case PERF_COUNTER_IOC_PERIOD:
1667 return perf_counter_period(counter, (u64 __user *)arg);
1668
Paul Mackerrasd859e292009-01-17 18:10:22 +11001669 default:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001670 return -ENOTTY;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001671 }
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001672
1673 if (flags & PERF_IOC_FLAG_GROUP)
1674 perf_counter_for_each(counter, func);
1675 else
1676 perf_counter_for_each_child(counter, func);
1677
1678 return 0;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001679}
1680
Peter Zijlstra771d7cd2009-05-25 14:45:26 +02001681int perf_counter_task_enable(void)
1682{
1683 struct perf_counter *counter;
1684
1685 mutex_lock(&current->perf_counter_mutex);
1686 list_for_each_entry(counter, &current->perf_counter_list, owner_entry)
1687 perf_counter_for_each_child(counter, perf_counter_enable);
1688 mutex_unlock(&current->perf_counter_mutex);
1689
1690 return 0;
1691}
1692
1693int perf_counter_task_disable(void)
1694{
1695 struct perf_counter *counter;
1696
1697 mutex_lock(&current->perf_counter_mutex);
1698 list_for_each_entry(counter, &current->perf_counter_list, owner_entry)
1699 perf_counter_for_each_child(counter, perf_counter_disable);
1700 mutex_unlock(&current->perf_counter_mutex);
1701
1702 return 0;
1703}
1704
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001705/*
1706 * Callers need to ensure there can be no nesting of this function, otherwise
1707 * the seqlock logic goes bad. We can not serialize this because the arch
1708 * code calls this from NMI context.
1709 */
1710void perf_counter_update_userpage(struct perf_counter *counter)
Paul Mackerras37d81822009-03-23 18:22:08 +01001711{
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001712 struct perf_counter_mmap_page *userpg;
Ingo Molnar22a4f652009-06-01 10:13:37 +02001713 struct perf_mmap_data *data;
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001714
1715 rcu_read_lock();
1716 data = rcu_dereference(counter->data);
1717 if (!data)
1718 goto unlock;
1719
1720 userpg = data->user_page;
Paul Mackerras37d81822009-03-23 18:22:08 +01001721
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001722 /*
1723 * Disable preemption so as to not let the corresponding user-space
1724 * spin too long if we get preempted.
1725 */
1726 preempt_disable();
Paul Mackerras37d81822009-03-23 18:22:08 +01001727 ++userpg->lock;
Peter Zijlstra92f22a32009-04-02 11:12:04 +02001728 barrier();
Paul Mackerras37d81822009-03-23 18:22:08 +01001729 userpg->index = counter->hw.idx;
1730 userpg->offset = atomic64_read(&counter->count);
1731 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
1732 userpg->offset -= atomic64_read(&counter->hw.prev_count);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001733
Peter Zijlstra92f22a32009-04-02 11:12:04 +02001734 barrier();
Paul Mackerras37d81822009-03-23 18:22:08 +01001735 ++userpg->lock;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001736 preempt_enable();
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001737unlock:
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001738 rcu_read_unlock();
Paul Mackerras37d81822009-03-23 18:22:08 +01001739}
1740
1741static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1742{
1743 struct perf_counter *counter = vma->vm_file->private_data;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001744 struct perf_mmap_data *data;
1745 int ret = VM_FAULT_SIGBUS;
Paul Mackerras37d81822009-03-23 18:22:08 +01001746
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001747 rcu_read_lock();
1748 data = rcu_dereference(counter->data);
1749 if (!data)
1750 goto unlock;
Paul Mackerras37d81822009-03-23 18:22:08 +01001751
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001752 if (vmf->pgoff == 0) {
1753 vmf->page = virt_to_page(data->user_page);
1754 } else {
1755 int nr = vmf->pgoff - 1;
1756
1757 if ((unsigned)nr > data->nr_pages)
1758 goto unlock;
1759
1760 vmf->page = virt_to_page(data->data_pages[nr]);
1761 }
Paul Mackerras37d81822009-03-23 18:22:08 +01001762 get_page(vmf->page);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001763 ret = 0;
1764unlock:
1765 rcu_read_unlock();
1766
1767 return ret;
1768}
1769
1770static int perf_mmap_data_alloc(struct perf_counter *counter, int nr_pages)
1771{
1772 struct perf_mmap_data *data;
1773 unsigned long size;
1774 int i;
1775
1776 WARN_ON(atomic_read(&counter->mmap_count));
1777
1778 size = sizeof(struct perf_mmap_data);
1779 size += nr_pages * sizeof(void *);
1780
1781 data = kzalloc(size, GFP_KERNEL);
1782 if (!data)
1783 goto fail;
1784
1785 data->user_page = (void *)get_zeroed_page(GFP_KERNEL);
1786 if (!data->user_page)
1787 goto fail_user_page;
1788
1789 for (i = 0; i < nr_pages; i++) {
1790 data->data_pages[i] = (void *)get_zeroed_page(GFP_KERNEL);
1791 if (!data->data_pages[i])
1792 goto fail_data_pages;
1793 }
1794
1795 data->nr_pages = nr_pages;
Peter Zijlstra22c15582009-05-05 17:50:25 +02001796 atomic_set(&data->lock, -1);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001797
1798 rcu_assign_pointer(counter->data, data);
1799
Paul Mackerras37d81822009-03-23 18:22:08 +01001800 return 0;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001801
1802fail_data_pages:
1803 for (i--; i >= 0; i--)
1804 free_page((unsigned long)data->data_pages[i]);
1805
1806 free_page((unsigned long)data->user_page);
1807
1808fail_user_page:
1809 kfree(data);
1810
1811fail:
1812 return -ENOMEM;
1813}
1814
1815static void __perf_mmap_data_free(struct rcu_head *rcu_head)
1816{
Ingo Molnar22a4f652009-06-01 10:13:37 +02001817 struct perf_mmap_data *data;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001818 int i;
1819
Ingo Molnar22a4f652009-06-01 10:13:37 +02001820 data = container_of(rcu_head, struct perf_mmap_data, rcu_head);
1821
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001822 free_page((unsigned long)data->user_page);
1823 for (i = 0; i < data->nr_pages; i++)
1824 free_page((unsigned long)data->data_pages[i]);
1825 kfree(data);
1826}
1827
1828static void perf_mmap_data_free(struct perf_counter *counter)
1829{
1830 struct perf_mmap_data *data = counter->data;
1831
1832 WARN_ON(atomic_read(&counter->mmap_count));
1833
1834 rcu_assign_pointer(counter->data, NULL);
1835 call_rcu(&data->rcu_head, __perf_mmap_data_free);
1836}
1837
1838static void perf_mmap_open(struct vm_area_struct *vma)
1839{
1840 struct perf_counter *counter = vma->vm_file->private_data;
1841
1842 atomic_inc(&counter->mmap_count);
1843}
1844
1845static void perf_mmap_close(struct vm_area_struct *vma)
1846{
1847 struct perf_counter *counter = vma->vm_file->private_data;
1848
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001849 WARN_ON_ONCE(counter->ctx->parent_ctx);
Ingo Molnar22a4f652009-06-01 10:13:37 +02001850 if (atomic_dec_and_mutex_lock(&counter->mmap_count, &counter->mmap_mutex)) {
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001851 struct user_struct *user = current_user();
1852
1853 atomic_long_sub(counter->data->nr_pages + 1, &user->locked_vm);
Peter Zijlstrac5078f72009-05-05 17:50:24 +02001854 vma->vm_mm->locked_vm -= counter->data->nr_locked;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001855 perf_mmap_data_free(counter);
1856 mutex_unlock(&counter->mmap_mutex);
1857 }
Paul Mackerras37d81822009-03-23 18:22:08 +01001858}
1859
1860static struct vm_operations_struct perf_mmap_vmops = {
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001861 .open = perf_mmap_open,
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001862 .close = perf_mmap_close,
Paul Mackerras37d81822009-03-23 18:22:08 +01001863 .fault = perf_mmap_fault,
1864};
1865
1866static int perf_mmap(struct file *file, struct vm_area_struct *vma)
1867{
1868 struct perf_counter *counter = file->private_data;
Ingo Molnar22a4f652009-06-01 10:13:37 +02001869 unsigned long user_locked, user_lock_limit;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001870 struct user_struct *user = current_user();
Ingo Molnar22a4f652009-06-01 10:13:37 +02001871 unsigned long locked, lock_limit;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001872 unsigned long vma_size;
1873 unsigned long nr_pages;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001874 long user_extra, extra;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001875 int ret = 0;
Paul Mackerras37d81822009-03-23 18:22:08 +01001876
1877 if (!(vma->vm_flags & VM_SHARED) || (vma->vm_flags & VM_WRITE))
1878 return -EINVAL;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001879
1880 vma_size = vma->vm_end - vma->vm_start;
1881 nr_pages = (vma_size / PAGE_SIZE) - 1;
1882
Peter Zijlstra7730d862009-03-25 12:48:31 +01001883 /*
1884 * If we have data pages ensure they're a power-of-two number, so we
1885 * can do bitmasks instead of modulo.
1886 */
1887 if (nr_pages != 0 && !is_power_of_2(nr_pages))
Paul Mackerras37d81822009-03-23 18:22:08 +01001888 return -EINVAL;
1889
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001890 if (vma_size != PAGE_SIZE * (1 + nr_pages))
Paul Mackerras37d81822009-03-23 18:22:08 +01001891 return -EINVAL;
1892
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001893 if (vma->vm_pgoff != 0)
1894 return -EINVAL;
Paul Mackerras37d81822009-03-23 18:22:08 +01001895
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001896 WARN_ON_ONCE(counter->ctx->parent_ctx);
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001897 mutex_lock(&counter->mmap_mutex);
1898 if (atomic_inc_not_zero(&counter->mmap_count)) {
1899 if (nr_pages != counter->data->nr_pages)
1900 ret = -EINVAL;
1901 goto unlock;
1902 }
1903
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001904 user_extra = nr_pages + 1;
1905 user_lock_limit = sysctl_perf_counter_mlock >> (PAGE_SHIFT - 10);
Ingo Molnara3862d32009-05-24 09:02:37 +02001906
1907 /*
1908 * Increase the limit linearly with more CPUs:
1909 */
1910 user_lock_limit *= num_online_cpus();
1911
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001912 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
Peter Zijlstrac5078f72009-05-05 17:50:24 +02001913
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001914 extra = 0;
1915 if (user_locked > user_lock_limit)
1916 extra = user_locked - user_lock_limit;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001917
1918 lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
1919 lock_limit >>= PAGE_SHIFT;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001920 locked = vma->vm_mm->locked_vm + extra;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001921
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001922 if ((locked > lock_limit) && !capable(CAP_IPC_LOCK)) {
1923 ret = -EPERM;
1924 goto unlock;
1925 }
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001926
1927 WARN_ON(counter->data);
1928 ret = perf_mmap_data_alloc(counter, nr_pages);
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001929 if (ret)
1930 goto unlock;
1931
1932 atomic_set(&counter->mmap_count, 1);
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001933 atomic_long_add(user_extra, &user->locked_vm);
Peter Zijlstrac5078f72009-05-05 17:50:24 +02001934 vma->vm_mm->locked_vm += extra;
1935 counter->data->nr_locked = extra;
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001936unlock:
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001937 mutex_unlock(&counter->mmap_mutex);
Paul Mackerras37d81822009-03-23 18:22:08 +01001938
1939 vma->vm_flags &= ~VM_MAYWRITE;
1940 vma->vm_flags |= VM_RESERVED;
1941 vma->vm_ops = &perf_mmap_vmops;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001942
1943 return ret;
Paul Mackerras37d81822009-03-23 18:22:08 +01001944}
1945
Peter Zijlstra3c446b3d2009-04-06 11:45:01 +02001946static int perf_fasync(int fd, struct file *filp, int on)
1947{
Peter Zijlstra3c446b3d2009-04-06 11:45:01 +02001948 struct inode *inode = filp->f_path.dentry->d_inode;
Ingo Molnar22a4f652009-06-01 10:13:37 +02001949 struct perf_counter *counter = filp->private_data;
Peter Zijlstra3c446b3d2009-04-06 11:45:01 +02001950 int retval;
1951
1952 mutex_lock(&inode->i_mutex);
1953 retval = fasync_helper(fd, filp, on, &counter->fasync);
1954 mutex_unlock(&inode->i_mutex);
1955
1956 if (retval < 0)
1957 return retval;
1958
1959 return 0;
1960}
1961
Thomas Gleixner0793a612008-12-04 20:12:29 +01001962static const struct file_operations perf_fops = {
1963 .release = perf_release,
1964 .read = perf_read,
1965 .poll = perf_poll,
Paul Mackerrasd859e292009-01-17 18:10:22 +11001966 .unlocked_ioctl = perf_ioctl,
1967 .compat_ioctl = perf_ioctl,
Paul Mackerras37d81822009-03-23 18:22:08 +01001968 .mmap = perf_mmap,
Peter Zijlstra3c446b3d2009-04-06 11:45:01 +02001969 .fasync = perf_fasync,
Thomas Gleixner0793a612008-12-04 20:12:29 +01001970};
1971
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001972/*
Peter Zijlstra925d5192009-03-30 19:07:02 +02001973 * Perf counter wakeup
1974 *
1975 * If there's data, ensure we set the poll() state and publish everything
1976 * to user-space before waking everybody up.
1977 */
1978
1979void perf_counter_wakeup(struct perf_counter *counter)
1980{
Peter Zijlstra925d5192009-03-30 19:07:02 +02001981 wake_up_all(&counter->waitq);
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02001982
1983 if (counter->pending_kill) {
1984 kill_fasync(&counter->fasync, SIGIO, counter->pending_kill);
1985 counter->pending_kill = 0;
1986 }
Peter Zijlstra925d5192009-03-30 19:07:02 +02001987}
1988
1989/*
1990 * Pending wakeups
1991 *
1992 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
1993 *
1994 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
1995 * single linked list and use cmpxchg() to add entries lockless.
1996 */
1997
Peter Zijlstra79f14642009-04-06 11:45:07 +02001998static void perf_pending_counter(struct perf_pending_entry *entry)
1999{
2000 struct perf_counter *counter = container_of(entry,
2001 struct perf_counter, pending);
2002
2003 if (counter->pending_disable) {
2004 counter->pending_disable = 0;
2005 perf_counter_disable(counter);
2006 }
2007
2008 if (counter->pending_wakeup) {
2009 counter->pending_wakeup = 0;
2010 perf_counter_wakeup(counter);
2011 }
2012}
2013
Peter Zijlstra671dec52009-04-06 11:45:02 +02002014#define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
Peter Zijlstra925d5192009-03-30 19:07:02 +02002015
Peter Zijlstra671dec52009-04-06 11:45:02 +02002016static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = {
Peter Zijlstra925d5192009-03-30 19:07:02 +02002017 PENDING_TAIL,
2018};
2019
Peter Zijlstra671dec52009-04-06 11:45:02 +02002020static void perf_pending_queue(struct perf_pending_entry *entry,
2021 void (*func)(struct perf_pending_entry *))
Peter Zijlstra925d5192009-03-30 19:07:02 +02002022{
Peter Zijlstra671dec52009-04-06 11:45:02 +02002023 struct perf_pending_entry **head;
Peter Zijlstra925d5192009-03-30 19:07:02 +02002024
Peter Zijlstra671dec52009-04-06 11:45:02 +02002025 if (cmpxchg(&entry->next, NULL, PENDING_TAIL) != NULL)
Peter Zijlstra925d5192009-03-30 19:07:02 +02002026 return;
2027
Peter Zijlstra671dec52009-04-06 11:45:02 +02002028 entry->func = func;
2029
2030 head = &get_cpu_var(perf_pending_head);
Peter Zijlstra925d5192009-03-30 19:07:02 +02002031
2032 do {
Peter Zijlstra671dec52009-04-06 11:45:02 +02002033 entry->next = *head;
2034 } while (cmpxchg(head, entry->next, entry) != entry->next);
Peter Zijlstra925d5192009-03-30 19:07:02 +02002035
2036 set_perf_counter_pending();
2037
Peter Zijlstra671dec52009-04-06 11:45:02 +02002038 put_cpu_var(perf_pending_head);
Peter Zijlstra925d5192009-03-30 19:07:02 +02002039}
2040
2041static int __perf_pending_run(void)
2042{
Peter Zijlstra671dec52009-04-06 11:45:02 +02002043 struct perf_pending_entry *list;
Peter Zijlstra925d5192009-03-30 19:07:02 +02002044 int nr = 0;
2045
Peter Zijlstra671dec52009-04-06 11:45:02 +02002046 list = xchg(&__get_cpu_var(perf_pending_head), PENDING_TAIL);
Peter Zijlstra925d5192009-03-30 19:07:02 +02002047 while (list != PENDING_TAIL) {
Peter Zijlstra671dec52009-04-06 11:45:02 +02002048 void (*func)(struct perf_pending_entry *);
2049 struct perf_pending_entry *entry = list;
Peter Zijlstra925d5192009-03-30 19:07:02 +02002050
2051 list = list->next;
2052
Peter Zijlstra671dec52009-04-06 11:45:02 +02002053 func = entry->func;
2054 entry->next = NULL;
Peter Zijlstra925d5192009-03-30 19:07:02 +02002055 /*
2056 * Ensure we observe the unqueue before we issue the wakeup,
2057 * so that we won't be waiting forever.
2058 * -- see perf_not_pending().
2059 */
2060 smp_wmb();
2061
Peter Zijlstra671dec52009-04-06 11:45:02 +02002062 func(entry);
Peter Zijlstra925d5192009-03-30 19:07:02 +02002063 nr++;
2064 }
2065
2066 return nr;
2067}
2068
2069static inline int perf_not_pending(struct perf_counter *counter)
2070{
2071 /*
2072 * If we flush on whatever cpu we run, there is a chance we don't
2073 * need to wait.
2074 */
2075 get_cpu();
2076 __perf_pending_run();
2077 put_cpu();
2078
2079 /*
2080 * Ensure we see the proper queue state before going to sleep
2081 * so that we do not miss the wakeup. -- see perf_pending_handle()
2082 */
2083 smp_rmb();
Peter Zijlstra671dec52009-04-06 11:45:02 +02002084 return counter->pending.next == NULL;
Peter Zijlstra925d5192009-03-30 19:07:02 +02002085}
2086
2087static void perf_pending_sync(struct perf_counter *counter)
2088{
2089 wait_event(counter->waitq, perf_not_pending(counter));
2090}
2091
2092void perf_counter_do_pending(void)
2093{
2094 __perf_pending_run();
2095}
2096
2097/*
Peter Zijlstra394ee072009-03-30 19:07:14 +02002098 * Callchain support -- arch specific
2099 */
2100
Peter Zijlstra9c03d882009-04-06 11:45:00 +02002101__weak struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
Peter Zijlstra394ee072009-03-30 19:07:14 +02002102{
2103 return NULL;
2104}
2105
2106/*
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002107 * Output
2108 */
2109
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002110struct perf_output_handle {
2111 struct perf_counter *counter;
2112 struct perf_mmap_data *data;
Peter Zijlstra8e3747c2009-06-02 16:16:02 +02002113 unsigned long head;
2114 unsigned long offset;
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002115 int nmi;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002116 int overflow;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002117 int locked;
2118 unsigned long flags;
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002119};
2120
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002121static void perf_output_wakeup(struct perf_output_handle *handle)
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002122{
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002123 atomic_set(&handle->data->poll, POLL_IN);
2124
Peter Zijlstra671dec52009-04-06 11:45:02 +02002125 if (handle->nmi) {
Peter Zijlstra79f14642009-04-06 11:45:07 +02002126 handle->counter->pending_wakeup = 1;
Peter Zijlstra671dec52009-04-06 11:45:02 +02002127 perf_pending_queue(&handle->counter->pending,
Peter Zijlstra79f14642009-04-06 11:45:07 +02002128 perf_pending_counter);
Peter Zijlstra671dec52009-04-06 11:45:02 +02002129 } else
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002130 perf_counter_wakeup(handle->counter);
2131}
2132
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002133/*
2134 * Curious locking construct.
2135 *
2136 * We need to ensure a later event doesn't publish a head when a former
2137 * event isn't done writing. However since we need to deal with NMIs we
2138 * cannot fully serialize things.
2139 *
2140 * What we do is serialize between CPUs so we only have to deal with NMI
2141 * nesting on a single CPU.
2142 *
2143 * We only publish the head (and generate a wakeup) when the outer-most
2144 * event completes.
2145 */
2146static void perf_output_lock(struct perf_output_handle *handle)
2147{
2148 struct perf_mmap_data *data = handle->data;
2149 int cpu;
2150
2151 handle->locked = 0;
2152
2153 local_irq_save(handle->flags);
2154 cpu = smp_processor_id();
2155
2156 if (in_nmi() && atomic_read(&data->lock) == cpu)
2157 return;
2158
Peter Zijlstra22c15582009-05-05 17:50:25 +02002159 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002160 cpu_relax();
2161
2162 handle->locked = 1;
2163}
2164
2165static void perf_output_unlock(struct perf_output_handle *handle)
2166{
2167 struct perf_mmap_data *data = handle->data;
Peter Zijlstra8e3747c2009-06-02 16:16:02 +02002168 unsigned long head;
2169 int cpu;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002170
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002171 data->done_head = data->head;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002172
2173 if (!handle->locked)
2174 goto out;
2175
2176again:
2177 /*
2178 * The xchg implies a full barrier that ensures all writes are done
2179 * before we publish the new head, matched by a rmb() in userspace when
2180 * reading this position.
2181 */
Peter Zijlstra8e3747c2009-06-02 16:16:02 +02002182 while ((head = atomic_long_xchg(&data->done_head, 0)))
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002183 data->user_page->data_head = head;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002184
2185 /*
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002186 * NMI can happen here, which means we can miss a done_head update.
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002187 */
2188
Peter Zijlstra22c15582009-05-05 17:50:25 +02002189 cpu = atomic_xchg(&data->lock, -1);
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002190 WARN_ON_ONCE(cpu != smp_processor_id());
2191
2192 /*
2193 * Therefore we have to validate we did not indeed do so.
2194 */
Peter Zijlstra8e3747c2009-06-02 16:16:02 +02002195 if (unlikely(atomic_long_read(&data->done_head))) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002196 /*
2197 * Since we had it locked, we can lock it again.
2198 */
Peter Zijlstra22c15582009-05-05 17:50:25 +02002199 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002200 cpu_relax();
2201
2202 goto again;
2203 }
2204
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002205 if (atomic_xchg(&data->wakeup, 0))
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002206 perf_output_wakeup(handle);
2207out:
2208 local_irq_restore(handle->flags);
2209}
2210
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002211static int perf_output_begin(struct perf_output_handle *handle,
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002212 struct perf_counter *counter, unsigned int size,
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002213 int nmi, int overflow)
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002214{
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002215 struct perf_mmap_data *data;
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002216 unsigned int offset, head;
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002217
Peter Zijlstra2023b352009-05-05 17:50:26 +02002218 /*
2219 * For inherited counters we send all the output towards the parent.
2220 */
2221 if (counter->parent)
2222 counter = counter->parent;
2223
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002224 rcu_read_lock();
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002225 data = rcu_dereference(counter->data);
2226 if (!data)
2227 goto out;
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002228
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002229 handle->data = data;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002230 handle->counter = counter;
2231 handle->nmi = nmi;
2232 handle->overflow = overflow;
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002233
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002234 if (!data->nr_pages)
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002235 goto fail;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002236
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002237 perf_output_lock(handle);
2238
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002239 do {
2240 offset = head = atomic_read(&data->head);
Peter Zijlstrac7138f32009-03-24 13:18:16 +01002241 head += size;
Peter Zijlstra8e3747c2009-06-02 16:16:02 +02002242 } while (atomic_long_cmpxchg(&data->head, offset, head) != offset);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002243
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002244 handle->offset = offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01002245 handle->head = head;
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002246
2247 if ((offset >> PAGE_SHIFT) != (head >> PAGE_SHIFT))
2248 atomic_set(&data->wakeup, 1);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002249
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002250 return 0;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002251
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002252fail:
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002253 perf_output_wakeup(handle);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002254out:
2255 rcu_read_unlock();
2256
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002257 return -ENOSPC;
2258}
2259
2260static void perf_output_copy(struct perf_output_handle *handle,
2261 void *buf, unsigned int len)
2262{
2263 unsigned int pages_mask;
2264 unsigned int offset;
2265 unsigned int size;
2266 void **pages;
2267
2268 offset = handle->offset;
2269 pages_mask = handle->data->nr_pages - 1;
2270 pages = handle->data->data_pages;
2271
2272 do {
2273 unsigned int page_offset;
2274 int nr;
2275
2276 nr = (offset >> PAGE_SHIFT) & pages_mask;
2277 page_offset = offset & (PAGE_SIZE - 1);
2278 size = min_t(unsigned int, PAGE_SIZE - page_offset, len);
2279
2280 memcpy(pages[nr] + page_offset, buf, size);
2281
2282 len -= size;
2283 buf += size;
2284 offset += size;
2285 } while (len);
2286
2287 handle->offset = offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01002288
Peter Zijlstra53020fe2009-05-13 21:26:19 +02002289 /*
2290 * Check we didn't copy past our reservation window, taking the
2291 * possible unsigned int wrap into account.
2292 */
Peter Zijlstra8e3747c2009-06-02 16:16:02 +02002293 WARN_ON_ONCE(((long)(handle->head - handle->offset)) < 0);
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002294}
2295
Peter Zijlstra5c148192009-03-25 12:30:23 +01002296#define perf_output_put(handle, x) \
2297 perf_output_copy((handle), &(x), sizeof(x))
2298
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002299static void perf_output_end(struct perf_output_handle *handle)
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002300{
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002301 struct perf_counter *counter = handle->counter;
2302 struct perf_mmap_data *data = handle->data;
2303
Peter Zijlstra0d486962009-06-02 19:22:16 +02002304 int wakeup_events = counter->attr.wakeup_events;
Peter Zijlstrac4578102009-04-02 11:12:01 +02002305
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002306 if (handle->overflow && wakeup_events) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002307 int events = atomic_inc_return(&data->events);
Peter Zijlstrac4578102009-04-02 11:12:01 +02002308 if (events >= wakeup_events) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002309 atomic_sub(wakeup_events, &data->events);
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002310 atomic_set(&data->wakeup, 1);
Peter Zijlstrac4578102009-04-02 11:12:01 +02002311 }
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002312 }
2313
2314 perf_output_unlock(handle);
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002315 rcu_read_unlock();
2316}
2317
Peter Zijlstra709e50c2009-06-02 14:13:15 +02002318static u32 perf_counter_pid(struct perf_counter *counter, struct task_struct *p)
2319{
2320 /*
2321 * only top level counters have the pid namespace they were created in
2322 */
2323 if (counter->parent)
2324 counter = counter->parent;
2325
2326 return task_tgid_nr_ns(p, counter->ns);
2327}
2328
2329static u32 perf_counter_tid(struct perf_counter *counter, struct task_struct *p)
2330{
2331 /*
2332 * only top level counters have the pid namespace they were created in
2333 */
2334 if (counter->parent)
2335 counter = counter->parent;
2336
2337 return task_pid_nr_ns(p, counter->ns);
2338}
2339
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002340static void perf_counter_output(struct perf_counter *counter,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002341 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002342{
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002343 int ret;
Peter Zijlstra0d486962009-06-02 19:22:16 +02002344 u64 sample_type = counter->attr.sample_type;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002345 struct perf_output_handle handle;
2346 struct perf_event_header header;
2347 u64 ip;
Peter Zijlstra5c148192009-03-25 12:30:23 +01002348 struct {
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002349 u32 pid, tid;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002350 } tid_entry;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002351 struct {
Peter Zijlstra8e5799b2009-06-02 15:08:15 +02002352 u64 id;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002353 u64 counter;
2354 } group_entry;
Peter Zijlstra394ee072009-03-30 19:07:14 +02002355 struct perf_callchain_entry *callchain = NULL;
2356 int callchain_size = 0;
Peter Zijlstra339f7c92009-04-06 11:45:06 +02002357 u64 time;
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002358 struct {
2359 u32 cpu, reserved;
2360 } cpu_entry;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002361
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02002362 header.type = 0;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002363 header.size = sizeof(header);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002364
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02002365 header.misc = PERF_EVENT_MISC_OVERFLOW;
Paul Mackerras9d23a902009-05-14 21:48:08 +10002366 header.misc |= perf_misc_flags(regs);
Peter Zijlstra6fab0192009-04-08 15:01:26 +02002367
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002368 if (sample_type & PERF_SAMPLE_IP) {
Paul Mackerras9d23a902009-05-14 21:48:08 +10002369 ip = perf_instruction_pointer(regs);
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002370 header.type |= PERF_SAMPLE_IP;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002371 header.size += sizeof(ip);
2372 }
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002373
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002374 if (sample_type & PERF_SAMPLE_TID) {
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002375 /* namespace issues */
Peter Zijlstra709e50c2009-06-02 14:13:15 +02002376 tid_entry.pid = perf_counter_pid(counter, current);
2377 tid_entry.tid = perf_counter_tid(counter, current);
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002378
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002379 header.type |= PERF_SAMPLE_TID;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002380 header.size += sizeof(tid_entry);
2381 }
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002382
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002383 if (sample_type & PERF_SAMPLE_TIME) {
Peter Zijlstra4d855452009-04-08 15:01:32 +02002384 /*
2385 * Maybe do better on x86 and provide cpu_clock_nmi()
2386 */
2387 time = sched_clock();
2388
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002389 header.type |= PERF_SAMPLE_TIME;
Peter Zijlstra4d855452009-04-08 15:01:32 +02002390 header.size += sizeof(u64);
2391 }
2392
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002393 if (sample_type & PERF_SAMPLE_ADDR) {
2394 header.type |= PERF_SAMPLE_ADDR;
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002395 header.size += sizeof(u64);
2396 }
2397
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002398 if (sample_type & PERF_SAMPLE_CONFIG) {
2399 header.type |= PERF_SAMPLE_CONFIG;
Peter Zijlstraa85f61a2009-05-08 18:52:23 +02002400 header.size += sizeof(u64);
2401 }
2402
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002403 if (sample_type & PERF_SAMPLE_CPU) {
2404 header.type |= PERF_SAMPLE_CPU;
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002405 header.size += sizeof(cpu_entry);
2406
2407 cpu_entry.cpu = raw_smp_processor_id();
2408 }
2409
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002410 if (sample_type & PERF_SAMPLE_GROUP) {
2411 header.type |= PERF_SAMPLE_GROUP;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002412 header.size += sizeof(u64) +
2413 counter->nr_siblings * sizeof(group_entry);
2414 }
2415
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002416 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
Peter Zijlstra394ee072009-03-30 19:07:14 +02002417 callchain = perf_callchain(regs);
2418
2419 if (callchain) {
Peter Zijlstra9c03d882009-04-06 11:45:00 +02002420 callchain_size = (1 + callchain->nr) * sizeof(u64);
Peter Zijlstra394ee072009-03-30 19:07:14 +02002421
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002422 header.type |= PERF_SAMPLE_CALLCHAIN;
Peter Zijlstra394ee072009-03-30 19:07:14 +02002423 header.size += callchain_size;
2424 }
2425 }
2426
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002427 ret = perf_output_begin(&handle, counter, header.size, nmi, 1);
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002428 if (ret)
2429 return;
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002430
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002431 perf_output_put(&handle, header);
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002432
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002433 if (sample_type & PERF_SAMPLE_IP)
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002434 perf_output_put(&handle, ip);
2435
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002436 if (sample_type & PERF_SAMPLE_TID)
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002437 perf_output_put(&handle, tid_entry);
2438
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002439 if (sample_type & PERF_SAMPLE_TIME)
Peter Zijlstra4d855452009-04-08 15:01:32 +02002440 perf_output_put(&handle, time);
2441
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002442 if (sample_type & PERF_SAMPLE_ADDR)
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002443 perf_output_put(&handle, addr);
2444
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002445 if (sample_type & PERF_SAMPLE_CONFIG)
Peter Zijlstra0d486962009-06-02 19:22:16 +02002446 perf_output_put(&handle, counter->attr.config);
Peter Zijlstraa85f61a2009-05-08 18:52:23 +02002447
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002448 if (sample_type & PERF_SAMPLE_CPU)
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002449 perf_output_put(&handle, cpu_entry);
2450
Peter Zijlstra2023b352009-05-05 17:50:26 +02002451 /*
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002452 * XXX PERF_SAMPLE_GROUP vs inherited counters seems difficult.
Peter Zijlstra2023b352009-05-05 17:50:26 +02002453 */
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002454 if (sample_type & PERF_SAMPLE_GROUP) {
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002455 struct perf_counter *leader, *sub;
2456 u64 nr = counter->nr_siblings;
2457
2458 perf_output_put(&handle, nr);
2459
2460 leader = counter->group_leader;
2461 list_for_each_entry(sub, &leader->sibling_list, list_entry) {
2462 if (sub != counter)
Robert Richter4aeb0b42009-04-29 12:47:03 +02002463 sub->pmu->read(sub);
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002464
Peter Zijlstra8e5799b2009-06-02 15:08:15 +02002465 group_entry.id = sub->id;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002466 group_entry.counter = atomic64_read(&sub->count);
2467
2468 perf_output_put(&handle, group_entry);
2469 }
2470 }
2471
Peter Zijlstra394ee072009-03-30 19:07:14 +02002472 if (callchain)
2473 perf_output_copy(&handle, callchain, callchain_size);
2474
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002475 perf_output_end(&handle);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002476}
2477
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002478/*
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002479 * fork tracking
2480 */
2481
2482struct perf_fork_event {
2483 struct task_struct *task;
2484
2485 struct {
2486 struct perf_event_header header;
2487
2488 u32 pid;
2489 u32 ppid;
2490 } event;
2491};
2492
2493static void perf_counter_fork_output(struct perf_counter *counter,
2494 struct perf_fork_event *fork_event)
2495{
2496 struct perf_output_handle handle;
2497 int size = fork_event->event.header.size;
2498 struct task_struct *task = fork_event->task;
2499 int ret = perf_output_begin(&handle, counter, size, 0, 0);
2500
2501 if (ret)
2502 return;
2503
2504 fork_event->event.pid = perf_counter_pid(counter, task);
2505 fork_event->event.ppid = perf_counter_pid(counter, task->real_parent);
2506
2507 perf_output_put(&handle, fork_event->event);
2508 perf_output_end(&handle);
2509}
2510
2511static int perf_counter_fork_match(struct perf_counter *counter)
2512{
2513 if (counter->attr.comm || counter->attr.mmap || counter->attr.munmap)
2514 return 1;
2515
2516 return 0;
2517}
2518
2519static void perf_counter_fork_ctx(struct perf_counter_context *ctx,
2520 struct perf_fork_event *fork_event)
2521{
2522 struct perf_counter *counter;
2523
2524 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2525 return;
2526
2527 rcu_read_lock();
2528 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2529 if (perf_counter_fork_match(counter))
2530 perf_counter_fork_output(counter, fork_event);
2531 }
2532 rcu_read_unlock();
2533}
2534
2535static void perf_counter_fork_event(struct perf_fork_event *fork_event)
2536{
2537 struct perf_cpu_context *cpuctx;
2538 struct perf_counter_context *ctx;
2539
2540 cpuctx = &get_cpu_var(perf_cpu_context);
2541 perf_counter_fork_ctx(&cpuctx->ctx, fork_event);
2542 put_cpu_var(perf_cpu_context);
2543
2544 rcu_read_lock();
2545 /*
2546 * doesn't really matter which of the child contexts the
2547 * events ends up in.
2548 */
2549 ctx = rcu_dereference(current->perf_counter_ctxp);
2550 if (ctx)
2551 perf_counter_fork_ctx(ctx, fork_event);
2552 rcu_read_unlock();
2553}
2554
2555void perf_counter_fork(struct task_struct *task)
2556{
2557 struct perf_fork_event fork_event;
2558
2559 if (!atomic_read(&nr_comm_counters) &&
2560 !atomic_read(&nr_mmap_counters) &&
2561 !atomic_read(&nr_munmap_counters))
2562 return;
2563
2564 fork_event = (struct perf_fork_event){
2565 .task = task,
2566 .event = {
2567 .header = {
2568 .type = PERF_EVENT_FORK,
2569 .size = sizeof(fork_event.event),
2570 },
2571 },
2572 };
2573
2574 perf_counter_fork_event(&fork_event);
2575}
2576
2577/*
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002578 * comm tracking
2579 */
2580
2581struct perf_comm_event {
Ingo Molnar22a4f652009-06-01 10:13:37 +02002582 struct task_struct *task;
2583 char *comm;
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002584 int comm_size;
2585
2586 struct {
2587 struct perf_event_header header;
2588
2589 u32 pid;
2590 u32 tid;
2591 } event;
2592};
2593
2594static void perf_counter_comm_output(struct perf_counter *counter,
2595 struct perf_comm_event *comm_event)
2596{
2597 struct perf_output_handle handle;
2598 int size = comm_event->event.header.size;
2599 int ret = perf_output_begin(&handle, counter, size, 0, 0);
2600
2601 if (ret)
2602 return;
2603
Peter Zijlstra709e50c2009-06-02 14:13:15 +02002604 comm_event->event.pid = perf_counter_pid(counter, comm_event->task);
2605 comm_event->event.tid = perf_counter_tid(counter, comm_event->task);
2606
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002607 perf_output_put(&handle, comm_event->event);
2608 perf_output_copy(&handle, comm_event->comm,
2609 comm_event->comm_size);
2610 perf_output_end(&handle);
2611}
2612
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002613static int perf_counter_comm_match(struct perf_counter *counter)
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002614{
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002615 if (counter->attr.comm)
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002616 return 1;
2617
2618 return 0;
2619}
2620
2621static void perf_counter_comm_ctx(struct perf_counter_context *ctx,
2622 struct perf_comm_event *comm_event)
2623{
2624 struct perf_counter *counter;
2625
2626 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2627 return;
2628
2629 rcu_read_lock();
2630 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002631 if (perf_counter_comm_match(counter))
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002632 perf_counter_comm_output(counter, comm_event);
2633 }
2634 rcu_read_unlock();
2635}
2636
2637static void perf_counter_comm_event(struct perf_comm_event *comm_event)
2638{
2639 struct perf_cpu_context *cpuctx;
Peter Zijlstra665c2142009-05-29 14:51:57 +02002640 struct perf_counter_context *ctx;
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002641 unsigned int size;
2642 char *comm = comm_event->task->comm;
2643
Ingo Molnar888fcee2009-04-09 09:48:22 +02002644 size = ALIGN(strlen(comm)+1, sizeof(u64));
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002645
2646 comm_event->comm = comm;
2647 comm_event->comm_size = size;
2648
2649 comm_event->event.header.size = sizeof(comm_event->event) + size;
2650
2651 cpuctx = &get_cpu_var(perf_cpu_context);
2652 perf_counter_comm_ctx(&cpuctx->ctx, comm_event);
2653 put_cpu_var(perf_cpu_context);
Peter Zijlstra665c2142009-05-29 14:51:57 +02002654
2655 rcu_read_lock();
2656 /*
2657 * doesn't really matter which of the child contexts the
2658 * events ends up in.
2659 */
2660 ctx = rcu_dereference(current->perf_counter_ctxp);
2661 if (ctx)
2662 perf_counter_comm_ctx(ctx, comm_event);
2663 rcu_read_unlock();
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002664}
2665
2666void perf_counter_comm(struct task_struct *task)
2667{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002668 struct perf_comm_event comm_event;
2669
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002670 if (!atomic_read(&nr_comm_counters))
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002671 return;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10002672
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002673 comm_event = (struct perf_comm_event){
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002674 .task = task,
2675 .event = {
2676 .header = { .type = PERF_EVENT_COMM, },
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002677 },
2678 };
2679
2680 perf_counter_comm_event(&comm_event);
2681}
2682
2683/*
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002684 * mmap tracking
2685 */
2686
2687struct perf_mmap_event {
2688 struct file *file;
2689 char *file_name;
2690 int file_size;
2691
2692 struct {
2693 struct perf_event_header header;
2694
2695 u32 pid;
2696 u32 tid;
2697 u64 start;
2698 u64 len;
2699 u64 pgoff;
2700 } event;
2701};
2702
2703static void perf_counter_mmap_output(struct perf_counter *counter,
2704 struct perf_mmap_event *mmap_event)
2705{
2706 struct perf_output_handle handle;
2707 int size = mmap_event->event.header.size;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002708 int ret = perf_output_begin(&handle, counter, size, 0, 0);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002709
2710 if (ret)
2711 return;
2712
Peter Zijlstra709e50c2009-06-02 14:13:15 +02002713 mmap_event->event.pid = perf_counter_pid(counter, current);
2714 mmap_event->event.tid = perf_counter_tid(counter, current);
2715
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002716 perf_output_put(&handle, mmap_event->event);
2717 perf_output_copy(&handle, mmap_event->file_name,
2718 mmap_event->file_size);
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002719 perf_output_end(&handle);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002720}
2721
2722static int perf_counter_mmap_match(struct perf_counter *counter,
2723 struct perf_mmap_event *mmap_event)
2724{
Peter Zijlstra0d486962009-06-02 19:22:16 +02002725 if (counter->attr.mmap &&
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002726 mmap_event->event.header.type == PERF_EVENT_MMAP)
2727 return 1;
2728
Peter Zijlstra0d486962009-06-02 19:22:16 +02002729 if (counter->attr.munmap &&
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002730 mmap_event->event.header.type == PERF_EVENT_MUNMAP)
2731 return 1;
2732
2733 return 0;
2734}
2735
2736static void perf_counter_mmap_ctx(struct perf_counter_context *ctx,
2737 struct perf_mmap_event *mmap_event)
2738{
2739 struct perf_counter *counter;
2740
2741 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2742 return;
2743
2744 rcu_read_lock();
2745 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2746 if (perf_counter_mmap_match(counter, mmap_event))
2747 perf_counter_mmap_output(counter, mmap_event);
2748 }
2749 rcu_read_unlock();
2750}
2751
2752static void perf_counter_mmap_event(struct perf_mmap_event *mmap_event)
2753{
2754 struct perf_cpu_context *cpuctx;
Peter Zijlstra665c2142009-05-29 14:51:57 +02002755 struct perf_counter_context *ctx;
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002756 struct file *file = mmap_event->file;
2757 unsigned int size;
2758 char tmp[16];
2759 char *buf = NULL;
2760 char *name;
2761
2762 if (file) {
2763 buf = kzalloc(PATH_MAX, GFP_KERNEL);
2764 if (!buf) {
2765 name = strncpy(tmp, "//enomem", sizeof(tmp));
2766 goto got_name;
2767 }
Peter Zijlstrad3d21c42009-04-09 10:53:46 +02002768 name = d_path(&file->f_path, buf, PATH_MAX);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002769 if (IS_ERR(name)) {
2770 name = strncpy(tmp, "//toolong", sizeof(tmp));
2771 goto got_name;
2772 }
2773 } else {
2774 name = strncpy(tmp, "//anon", sizeof(tmp));
2775 goto got_name;
2776 }
2777
2778got_name:
Ingo Molnar888fcee2009-04-09 09:48:22 +02002779 size = ALIGN(strlen(name)+1, sizeof(u64));
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002780
2781 mmap_event->file_name = name;
2782 mmap_event->file_size = size;
2783
2784 mmap_event->event.header.size = sizeof(mmap_event->event) + size;
2785
2786 cpuctx = &get_cpu_var(perf_cpu_context);
2787 perf_counter_mmap_ctx(&cpuctx->ctx, mmap_event);
2788 put_cpu_var(perf_cpu_context);
2789
Peter Zijlstra665c2142009-05-29 14:51:57 +02002790 rcu_read_lock();
2791 /*
2792 * doesn't really matter which of the child contexts the
2793 * events ends up in.
2794 */
2795 ctx = rcu_dereference(current->perf_counter_ctxp);
2796 if (ctx)
2797 perf_counter_mmap_ctx(ctx, mmap_event);
2798 rcu_read_unlock();
2799
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002800 kfree(buf);
2801}
2802
2803void perf_counter_mmap(unsigned long addr, unsigned long len,
2804 unsigned long pgoff, struct file *file)
2805{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002806 struct perf_mmap_event mmap_event;
2807
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002808 if (!atomic_read(&nr_mmap_counters))
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002809 return;
2810
2811 mmap_event = (struct perf_mmap_event){
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002812 .file = file,
2813 .event = {
2814 .header = { .type = PERF_EVENT_MMAP, },
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002815 .start = addr,
2816 .len = len,
2817 .pgoff = pgoff,
2818 },
2819 };
2820
2821 perf_counter_mmap_event(&mmap_event);
2822}
2823
2824void perf_counter_munmap(unsigned long addr, unsigned long len,
2825 unsigned long pgoff, struct file *file)
2826{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002827 struct perf_mmap_event mmap_event;
2828
Peter Zijlstra60313eb2009-06-04 16:53:44 +02002829 if (!atomic_read(&nr_munmap_counters))
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002830 return;
2831
2832 mmap_event = (struct perf_mmap_event){
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002833 .file = file,
2834 .event = {
2835 .header = { .type = PERF_EVENT_MUNMAP, },
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002836 .start = addr,
2837 .len = len,
2838 .pgoff = pgoff,
2839 },
2840 };
2841
2842 perf_counter_mmap_event(&mmap_event);
2843}
2844
2845/*
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002846 * Log sample_period changes so that analyzing tools can re-normalize the
Peter Zijlstrae220d2d2009-05-23 18:28:55 +02002847 * event flow.
Peter Zijlstra26b119b2009-05-20 12:21:20 +02002848 */
2849
2850static void perf_log_period(struct perf_counter *counter, u64 period)
2851{
2852 struct perf_output_handle handle;
2853 int ret;
2854
2855 struct {
2856 struct perf_event_header header;
2857 u64 time;
2858 u64 period;
2859 } freq_event = {
2860 .header = {
2861 .type = PERF_EVENT_PERIOD,
2862 .misc = 0,
2863 .size = sizeof(freq_event),
2864 },
2865 .time = sched_clock(),
2866 .period = period,
2867 };
2868
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002869 if (counter->hw.sample_period == period)
Peter Zijlstra26b119b2009-05-20 12:21:20 +02002870 return;
2871
2872 ret = perf_output_begin(&handle, counter, sizeof(freq_event), 0, 0);
2873 if (ret)
2874 return;
2875
2876 perf_output_put(&handle, freq_event);
2877 perf_output_end(&handle);
2878}
2879
2880/*
Peter Zijlstraa78ac322009-05-25 17:39:05 +02002881 * IRQ throttle logging
2882 */
2883
2884static void perf_log_throttle(struct perf_counter *counter, int enable)
2885{
2886 struct perf_output_handle handle;
2887 int ret;
2888
2889 struct {
2890 struct perf_event_header header;
2891 u64 time;
2892 } throttle_event = {
2893 .header = {
2894 .type = PERF_EVENT_THROTTLE + 1,
2895 .misc = 0,
2896 .size = sizeof(throttle_event),
2897 },
2898 .time = sched_clock(),
2899 };
2900
Ingo Molnar0127c3e2009-05-25 22:03:26 +02002901 ret = perf_output_begin(&handle, counter, sizeof(throttle_event), 1, 0);
Peter Zijlstraa78ac322009-05-25 17:39:05 +02002902 if (ret)
2903 return;
2904
2905 perf_output_put(&handle, throttle_event);
2906 perf_output_end(&handle);
2907}
2908
2909/*
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002910 * Generic counter overflow handling.
2911 */
2912
2913int perf_counter_overflow(struct perf_counter *counter,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002914 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002915{
Peter Zijlstra79f14642009-04-06 11:45:07 +02002916 int events = atomic_read(&counter->event_limit);
Peter Zijlstraa78ac322009-05-25 17:39:05 +02002917 int throttle = counter->pmu->unthrottle != NULL;
Peter Zijlstra79f14642009-04-06 11:45:07 +02002918 int ret = 0;
2919
Peter Zijlstraa78ac322009-05-25 17:39:05 +02002920 if (!throttle) {
2921 counter->hw.interrupts++;
Ingo Molnar128f0482009-06-03 22:19:36 +02002922 } else {
2923 if (counter->hw.interrupts != MAX_INTERRUPTS) {
2924 counter->hw.interrupts++;
2925 if (HZ*counter->hw.interrupts > (u64)sysctl_perf_counter_limit) {
2926 counter->hw.interrupts = MAX_INTERRUPTS;
2927 perf_log_throttle(counter, 0);
2928 ret = 1;
2929 }
2930 } else {
2931 /*
2932 * Keep re-disabling counters even though on the previous
2933 * pass we disabled it - just in case we raced with a
2934 * sched-in and the counter got enabled again:
2935 */
Peter Zijlstraa78ac322009-05-25 17:39:05 +02002936 ret = 1;
2937 }
2938 }
Peter Zijlstra60db5e02009-05-15 15:19:28 +02002939
Peter Zijlstra2023b352009-05-05 17:50:26 +02002940 /*
2941 * XXX event_limit might not quite work as expected on inherited
2942 * counters
2943 */
2944
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002945 counter->pending_kill = POLL_IN;
Peter Zijlstra79f14642009-04-06 11:45:07 +02002946 if (events && atomic_dec_and_test(&counter->event_limit)) {
2947 ret = 1;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002948 counter->pending_kill = POLL_HUP;
Peter Zijlstra79f14642009-04-06 11:45:07 +02002949 if (nmi) {
2950 counter->pending_disable = 1;
2951 perf_pending_queue(&counter->pending,
2952 perf_pending_counter);
2953 } else
2954 perf_counter_disable(counter);
2955 }
2956
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002957 perf_counter_output(counter, nmi, regs, addr);
Peter Zijlstra79f14642009-04-06 11:45:07 +02002958 return ret;
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002959}
2960
2961/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002962 * Generic software counter infrastructure
2963 */
2964
2965static void perf_swcounter_update(struct perf_counter *counter)
2966{
2967 struct hw_perf_counter *hwc = &counter->hw;
2968 u64 prev, now;
2969 s64 delta;
2970
2971again:
2972 prev = atomic64_read(&hwc->prev_count);
2973 now = atomic64_read(&hwc->count);
2974 if (atomic64_cmpxchg(&hwc->prev_count, prev, now) != prev)
2975 goto again;
2976
2977 delta = now - prev;
2978
2979 atomic64_add(delta, &counter->count);
2980 atomic64_sub(delta, &hwc->period_left);
2981}
2982
2983static void perf_swcounter_set_period(struct perf_counter *counter)
2984{
2985 struct hw_perf_counter *hwc = &counter->hw;
2986 s64 left = atomic64_read(&hwc->period_left);
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002987 s64 period = hwc->sample_period;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002988
2989 if (unlikely(left <= -period)) {
2990 left = period;
2991 atomic64_set(&hwc->period_left, left);
2992 }
2993
2994 if (unlikely(left <= 0)) {
2995 left += period;
2996 atomic64_add(period, &hwc->period_left);
2997 }
2998
2999 atomic64_set(&hwc->prev_count, -left);
3000 atomic64_set(&hwc->count, -left);
3001}
3002
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003003static enum hrtimer_restart perf_swcounter_hrtimer(struct hrtimer *hrtimer)
3004{
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02003005 enum hrtimer_restart ret = HRTIMER_RESTART;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003006 struct perf_counter *counter;
3007 struct pt_regs *regs;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003008 u64 period;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003009
3010 counter = container_of(hrtimer, struct perf_counter, hw.hrtimer);
Robert Richter4aeb0b42009-04-29 12:47:03 +02003011 counter->pmu->read(counter);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003012
3013 regs = get_irq_regs();
3014 /*
3015 * In case we exclude kernel IPs or are somehow not in interrupt
3016 * context, provide the next best thing, the user IP.
3017 */
Peter Zijlstra0d486962009-06-02 19:22:16 +02003018 if ((counter->attr.exclude_kernel || !regs) &&
3019 !counter->attr.exclude_user)
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003020 regs = task_pt_regs(current);
3021
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02003022 if (regs) {
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003023 if (perf_counter_overflow(counter, 0, regs, 0))
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02003024 ret = HRTIMER_NORESTART;
3025 }
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003026
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003027 period = max_t(u64, 10000, counter->hw.sample_period);
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003028 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003029
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02003030 return ret;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003031}
3032
3033static void perf_swcounter_overflow(struct perf_counter *counter,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003034 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003035{
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003036 perf_swcounter_update(counter);
3037 perf_swcounter_set_period(counter);
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003038 if (perf_counter_overflow(counter, nmi, regs, addr))
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02003039 /* soft-disable the counter */
3040 ;
3041
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003042}
3043
Paul Mackerras880ca152009-06-01 17:49:14 +10003044static int perf_swcounter_is_counting(struct perf_counter *counter)
3045{
3046 struct perf_counter_context *ctx;
3047 unsigned long flags;
3048 int count;
3049
3050 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
3051 return 1;
3052
3053 if (counter->state != PERF_COUNTER_STATE_INACTIVE)
3054 return 0;
3055
3056 /*
3057 * If the counter is inactive, it could be just because
3058 * its task is scheduled out, or because it's in a group
3059 * which could not go on the PMU. We want to count in
3060 * the first case but not the second. If the context is
3061 * currently active then an inactive software counter must
3062 * be the second case. If it's not currently active then
3063 * we need to know whether the counter was active when the
3064 * context was last active, which we can determine by
3065 * comparing counter->tstamp_stopped with ctx->time.
3066 *
3067 * We are within an RCU read-side critical section,
3068 * which protects the existence of *ctx.
3069 */
3070 ctx = counter->ctx;
3071 spin_lock_irqsave(&ctx->lock, flags);
3072 count = 1;
3073 /* Re-check state now we have the lock */
3074 if (counter->state < PERF_COUNTER_STATE_INACTIVE ||
3075 counter->ctx->is_active ||
3076 counter->tstamp_stopped < ctx->time)
3077 count = 0;
3078 spin_unlock_irqrestore(&ctx->lock, flags);
3079 return count;
3080}
3081
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003082static int perf_swcounter_match(struct perf_counter *counter,
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003083 enum perf_event_types type,
3084 u32 event, struct pt_regs *regs)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003085{
Paul Mackerras880ca152009-06-01 17:49:14 +10003086 u64 event_config;
3087
3088 event_config = ((u64) type << PERF_COUNTER_TYPE_SHIFT) | event;
3089
3090 if (!perf_swcounter_is_counting(counter))
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003091 return 0;
3092
Peter Zijlstra0d486962009-06-02 19:22:16 +02003093 if (counter->attr.config != event_config)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003094 return 0;
3095
Paul Mackerras3f731ca2009-06-01 17:52:30 +10003096 if (regs) {
Peter Zijlstra0d486962009-06-02 19:22:16 +02003097 if (counter->attr.exclude_user && user_mode(regs))
Paul Mackerras3f731ca2009-06-01 17:52:30 +10003098 return 0;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003099
Peter Zijlstra0d486962009-06-02 19:22:16 +02003100 if (counter->attr.exclude_kernel && !user_mode(regs))
Paul Mackerras3f731ca2009-06-01 17:52:30 +10003101 return 0;
3102 }
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003103
3104 return 1;
3105}
3106
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003107static void perf_swcounter_add(struct perf_counter *counter, u64 nr,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003108 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003109{
3110 int neg = atomic64_add_negative(nr, &counter->hw.count);
Ingo Molnar22a4f652009-06-01 10:13:37 +02003111
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003112 if (counter->hw.sample_period && !neg && regs)
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003113 perf_swcounter_overflow(counter, nmi, regs, addr);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003114}
3115
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003116static void perf_swcounter_ctx_event(struct perf_counter_context *ctx,
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003117 enum perf_event_types type, u32 event,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003118 u64 nr, int nmi, struct pt_regs *regs,
3119 u64 addr)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003120{
3121 struct perf_counter *counter;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003122
Peter Zijlstra01ef09d2009-03-19 20:26:11 +01003123 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003124 return;
3125
Peter Zijlstra592903c2009-03-13 12:21:36 +01003126 rcu_read_lock();
3127 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003128 if (perf_swcounter_match(counter, type, event, regs))
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003129 perf_swcounter_add(counter, nr, nmi, regs, addr);
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003130 }
Peter Zijlstra592903c2009-03-13 12:21:36 +01003131 rcu_read_unlock();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003132}
3133
Peter Zijlstra96f6d442009-03-23 18:22:07 +01003134static int *perf_swcounter_recursion_context(struct perf_cpu_context *cpuctx)
3135{
3136 if (in_nmi())
3137 return &cpuctx->recursion[3];
3138
3139 if (in_irq())
3140 return &cpuctx->recursion[2];
3141
3142 if (in_softirq())
3143 return &cpuctx->recursion[1];
3144
3145 return &cpuctx->recursion[0];
3146}
3147
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003148static void __perf_swcounter_event(enum perf_event_types type, u32 event,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003149 u64 nr, int nmi, struct pt_regs *regs,
3150 u64 addr)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003151{
3152 struct perf_cpu_context *cpuctx = &get_cpu_var(perf_cpu_context);
Peter Zijlstra96f6d442009-03-23 18:22:07 +01003153 int *recursion = perf_swcounter_recursion_context(cpuctx);
Peter Zijlstra665c2142009-05-29 14:51:57 +02003154 struct perf_counter_context *ctx;
Peter Zijlstra96f6d442009-03-23 18:22:07 +01003155
3156 if (*recursion)
3157 goto out;
3158
3159 (*recursion)++;
3160 barrier();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003161
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003162 perf_swcounter_ctx_event(&cpuctx->ctx, type, event,
3163 nr, nmi, regs, addr);
Peter Zijlstra665c2142009-05-29 14:51:57 +02003164 rcu_read_lock();
3165 /*
3166 * doesn't really matter which of the child contexts the
3167 * events ends up in.
3168 */
3169 ctx = rcu_dereference(current->perf_counter_ctxp);
3170 if (ctx)
3171 perf_swcounter_ctx_event(ctx, type, event, nr, nmi, regs, addr);
3172 rcu_read_unlock();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003173
Peter Zijlstra96f6d442009-03-23 18:22:07 +01003174 barrier();
3175 (*recursion)--;
3176
3177out:
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003178 put_cpu_var(perf_cpu_context);
3179}
3180
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003181void
3182perf_swcounter_event(u32 event, u64 nr, int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003183{
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003184 __perf_swcounter_event(PERF_TYPE_SOFTWARE, event, nr, nmi, regs, addr);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003185}
3186
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003187static void perf_swcounter_read(struct perf_counter *counter)
3188{
3189 perf_swcounter_update(counter);
3190}
3191
3192static int perf_swcounter_enable(struct perf_counter *counter)
3193{
3194 perf_swcounter_set_period(counter);
3195 return 0;
3196}
3197
3198static void perf_swcounter_disable(struct perf_counter *counter)
3199{
3200 perf_swcounter_update(counter);
3201}
3202
Robert Richter4aeb0b42009-04-29 12:47:03 +02003203static const struct pmu perf_ops_generic = {
Peter Zijlstraac17dc82009-03-13 12:21:34 +01003204 .enable = perf_swcounter_enable,
3205 .disable = perf_swcounter_disable,
3206 .read = perf_swcounter_read,
3207};
3208
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003209/*
3210 * Software counter: cpu wall time clock
3211 */
3212
Paul Mackerras9abf8a02009-01-09 16:26:43 +11003213static void cpu_clock_perf_counter_update(struct perf_counter *counter)
3214{
3215 int cpu = raw_smp_processor_id();
3216 s64 prev;
3217 u64 now;
3218
3219 now = cpu_clock(cpu);
3220 prev = atomic64_read(&counter->hw.prev_count);
3221 atomic64_set(&counter->hw.prev_count, now);
3222 atomic64_add(now - prev, &counter->count);
3223}
3224
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003225static int cpu_clock_perf_counter_enable(struct perf_counter *counter)
3226{
3227 struct hw_perf_counter *hwc = &counter->hw;
3228 int cpu = raw_smp_processor_id();
3229
3230 atomic64_set(&hwc->prev_count, cpu_clock(cpu));
Peter Zijlstra039fc912009-03-13 16:43:47 +01003231 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
3232 hwc->hrtimer.function = perf_swcounter_hrtimer;
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003233 if (hwc->sample_period) {
3234 u64 period = max_t(u64, 10000, hwc->sample_period);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003235 __hrtimer_start_range_ns(&hwc->hrtimer,
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003236 ns_to_ktime(period), 0,
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003237 HRTIMER_MODE_REL, 0);
3238 }
3239
3240 return 0;
3241}
3242
Ingo Molnar5c92d122008-12-11 13:21:10 +01003243static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
3244{
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003245 if (counter->hw.sample_period)
Peter Zijlstrab986d7e2009-05-20 12:21:21 +02003246 hrtimer_cancel(&counter->hw.hrtimer);
Paul Mackerras9abf8a02009-01-09 16:26:43 +11003247 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01003248}
3249
3250static void cpu_clock_perf_counter_read(struct perf_counter *counter)
3251{
Paul Mackerras9abf8a02009-01-09 16:26:43 +11003252 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01003253}
3254
Robert Richter4aeb0b42009-04-29 12:47:03 +02003255static const struct pmu perf_ops_cpu_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +01003256 .enable = cpu_clock_perf_counter_enable,
3257 .disable = cpu_clock_perf_counter_disable,
3258 .read = cpu_clock_perf_counter_read,
Ingo Molnar5c92d122008-12-11 13:21:10 +01003259};
3260
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01003261/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003262 * Software counter: task time clock
3263 */
3264
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02003265static void task_clock_perf_counter_update(struct perf_counter *counter, u64 now)
Ingo Molnarbae43c92008-12-11 14:03:20 +01003266{
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02003267 u64 prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003268 s64 delta;
Ingo Molnarbae43c92008-12-11 14:03:20 +01003269
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02003270 prev = atomic64_xchg(&counter->hw.prev_count, now);
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003271 delta = now - prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003272 atomic64_add(delta, &counter->count);
Ingo Molnarbae43c92008-12-11 14:03:20 +01003273}
3274
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01003275static int task_clock_perf_counter_enable(struct perf_counter *counter)
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003276{
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003277 struct hw_perf_counter *hwc = &counter->hw;
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02003278 u64 now;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003279
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02003280 now = counter->ctx->time;
3281
3282 atomic64_set(&hwc->prev_count, now);
Peter Zijlstra039fc912009-03-13 16:43:47 +01003283 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
3284 hwc->hrtimer.function = perf_swcounter_hrtimer;
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003285 if (hwc->sample_period) {
3286 u64 period = max_t(u64, 10000, hwc->sample_period);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003287 __hrtimer_start_range_ns(&hwc->hrtimer,
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003288 ns_to_ktime(period), 0,
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003289 HRTIMER_MODE_REL, 0);
3290 }
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01003291
3292 return 0;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003293}
3294
3295static void task_clock_perf_counter_disable(struct perf_counter *counter)
3296{
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003297 if (counter->hw.sample_period)
Peter Zijlstrab986d7e2009-05-20 12:21:21 +02003298 hrtimer_cancel(&counter->hw.hrtimer);
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02003299 task_clock_perf_counter_update(counter, counter->ctx->time);
3300
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003301}
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01003302
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003303static void task_clock_perf_counter_read(struct perf_counter *counter)
3304{
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02003305 u64 time;
3306
3307 if (!in_nmi()) {
3308 update_context_time(counter->ctx);
3309 time = counter->ctx->time;
3310 } else {
3311 u64 now = perf_clock();
3312 u64 delta = now - counter->ctx->timestamp;
3313 time = counter->ctx->time + delta;
3314 }
3315
3316 task_clock_perf_counter_update(counter, time);
Ingo Molnarbae43c92008-12-11 14:03:20 +01003317}
3318
Robert Richter4aeb0b42009-04-29 12:47:03 +02003319static const struct pmu perf_ops_task_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +01003320 .enable = task_clock_perf_counter_enable,
3321 .disable = task_clock_perf_counter_disable,
3322 .read = task_clock_perf_counter_read,
Ingo Molnarbae43c92008-12-11 14:03:20 +01003323};
3324
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003325/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003326 * Software counter: cpu migrations
3327 */
Paul Mackerras3f731ca2009-06-01 17:52:30 +10003328void perf_counter_task_migration(struct task_struct *task, int cpu)
Ingo Molnar6c594c22008-12-14 12:34:15 +01003329{
Paul Mackerras3f731ca2009-06-01 17:52:30 +10003330 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
3331 struct perf_counter_context *ctx;
Paul Mackerras23a185c2009-02-09 22:42:47 +11003332
Paul Mackerras3f731ca2009-06-01 17:52:30 +10003333 perf_swcounter_ctx_event(&cpuctx->ctx, PERF_TYPE_SOFTWARE,
3334 PERF_COUNT_CPU_MIGRATIONS,
3335 1, 1, NULL, 0);
3336
3337 ctx = perf_pin_task_context(task);
3338 if (ctx) {
3339 perf_swcounter_ctx_event(ctx, PERF_TYPE_SOFTWARE,
3340 PERF_COUNT_CPU_MIGRATIONS,
3341 1, 1, NULL, 0);
3342 perf_unpin_context(ctx);
3343 }
Ingo Molnar6c594c22008-12-14 12:34:15 +01003344}
3345
Peter Zijlstrae077df42009-03-19 20:26:17 +01003346#ifdef CONFIG_EVENT_PROFILE
3347void perf_tpcounter_event(int event_id)
3348{
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003349 struct pt_regs *regs = get_irq_regs();
3350
3351 if (!regs)
3352 regs = task_pt_regs(current);
3353
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003354 __perf_swcounter_event(PERF_TYPE_TRACEPOINT, event_id, 1, 1, regs, 0);
Peter Zijlstrae077df42009-03-19 20:26:17 +01003355}
Steven Whitehouseff7b1b42009-04-15 16:55:05 +01003356EXPORT_SYMBOL_GPL(perf_tpcounter_event);
Peter Zijlstrae077df42009-03-19 20:26:17 +01003357
3358extern int ftrace_profile_enable(int);
3359extern void ftrace_profile_disable(int);
3360
3361static void tp_perf_counter_destroy(struct perf_counter *counter)
3362{
Peter Zijlstra0d486962009-06-02 19:22:16 +02003363 ftrace_profile_disable(perf_event_id(&counter->attr));
Peter Zijlstrae077df42009-03-19 20:26:17 +01003364}
3365
Robert Richter4aeb0b42009-04-29 12:47:03 +02003366static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
Peter Zijlstrae077df42009-03-19 20:26:17 +01003367{
Peter Zijlstra0d486962009-06-02 19:22:16 +02003368 int event_id = perf_event_id(&counter->attr);
Peter Zijlstrae077df42009-03-19 20:26:17 +01003369 int ret;
3370
3371 ret = ftrace_profile_enable(event_id);
3372 if (ret)
3373 return NULL;
3374
3375 counter->destroy = tp_perf_counter_destroy;
Peter Zijlstra0d486962009-06-02 19:22:16 +02003376 counter->hw.sample_period = counter->attr.sample_period;
Peter Zijlstrae077df42009-03-19 20:26:17 +01003377
3378 return &perf_ops_generic;
3379}
3380#else
Robert Richter4aeb0b42009-04-29 12:47:03 +02003381static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
Peter Zijlstrae077df42009-03-19 20:26:17 +01003382{
3383 return NULL;
3384}
3385#endif
3386
Robert Richter4aeb0b42009-04-29 12:47:03 +02003387static const struct pmu *sw_perf_counter_init(struct perf_counter *counter)
Ingo Molnar5c92d122008-12-11 13:21:10 +01003388{
Robert Richter4aeb0b42009-04-29 12:47:03 +02003389 const struct pmu *pmu = NULL;
Ingo Molnar5c92d122008-12-11 13:21:10 +01003390
Paul Mackerras0475f9e2009-02-11 14:35:35 +11003391 /*
3392 * Software counters (currently) can't in general distinguish
3393 * between user, kernel and hypervisor events.
3394 * However, context switches and cpu migrations are considered
3395 * to be kernel events, and page faults are never hypervisor
3396 * events.
3397 */
Peter Zijlstra0d486962009-06-02 19:22:16 +02003398 switch (perf_event_id(&counter->attr)) {
Ingo Molnar5c92d122008-12-11 13:21:10 +01003399 case PERF_COUNT_CPU_CLOCK:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003400 pmu = &perf_ops_cpu_clock;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003401
Ingo Molnar5c92d122008-12-11 13:21:10 +01003402 break;
Ingo Molnarbae43c92008-12-11 14:03:20 +01003403 case PERF_COUNT_TASK_CLOCK:
Paul Mackerras23a185c2009-02-09 22:42:47 +11003404 /*
3405 * If the user instantiates this as a per-cpu counter,
3406 * use the cpu_clock counter instead.
3407 */
3408 if (counter->ctx->task)
Robert Richter4aeb0b42009-04-29 12:47:03 +02003409 pmu = &perf_ops_task_clock;
Paul Mackerras23a185c2009-02-09 22:42:47 +11003410 else
Robert Richter4aeb0b42009-04-29 12:47:03 +02003411 pmu = &perf_ops_cpu_clock;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003412
Ingo Molnarbae43c92008-12-11 14:03:20 +01003413 break;
Ingo Molnare06c61a2008-12-14 14:44:31 +01003414 case PERF_COUNT_PAGE_FAULTS:
Peter Zijlstraac17dc82009-03-13 12:21:34 +01003415 case PERF_COUNT_PAGE_FAULTS_MIN:
3416 case PERF_COUNT_PAGE_FAULTS_MAJ:
Ingo Molnar5d6a27d2008-12-14 12:28:33 +01003417 case PERF_COUNT_CONTEXT_SWITCHES:
Ingo Molnar6c594c22008-12-14 12:34:15 +01003418 case PERF_COUNT_CPU_MIGRATIONS:
Paul Mackerras3f731ca2009-06-01 17:52:30 +10003419 pmu = &perf_ops_generic;
Ingo Molnar6c594c22008-12-14 12:34:15 +01003420 break;
Ingo Molnar5c92d122008-12-11 13:21:10 +01003421 }
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003422
Robert Richter4aeb0b42009-04-29 12:47:03 +02003423 return pmu;
Ingo Molnar5c92d122008-12-11 13:21:10 +01003424}
3425
Thomas Gleixner0793a612008-12-04 20:12:29 +01003426/*
3427 * Allocate and initialize a counter structure
3428 */
3429static struct perf_counter *
Peter Zijlstra0d486962009-06-02 19:22:16 +02003430perf_counter_alloc(struct perf_counter_attr *attr,
Ingo Molnar04289bb2008-12-11 08:38:42 +01003431 int cpu,
Paul Mackerras23a185c2009-02-09 22:42:47 +11003432 struct perf_counter_context *ctx,
Ingo Molnar9b51f662008-12-12 13:49:45 +01003433 struct perf_counter *group_leader,
3434 gfp_t gfpflags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003435{
Robert Richter4aeb0b42009-04-29 12:47:03 +02003436 const struct pmu *pmu;
Ingo Molnar621a01e2008-12-11 12:46:46 +01003437 struct perf_counter *counter;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003438 struct hw_perf_counter *hwc;
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003439 long err;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003440
Ingo Molnar9b51f662008-12-12 13:49:45 +01003441 counter = kzalloc(sizeof(*counter), gfpflags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003442 if (!counter)
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003443 return ERR_PTR(-ENOMEM);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003444
Ingo Molnar04289bb2008-12-11 08:38:42 +01003445 /*
3446 * Single counters are their own group leaders, with an
3447 * empty sibling list:
3448 */
3449 if (!group_leader)
3450 group_leader = counter;
3451
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003452 mutex_init(&counter->child_mutex);
3453 INIT_LIST_HEAD(&counter->child_list);
3454
Ingo Molnar04289bb2008-12-11 08:38:42 +01003455 INIT_LIST_HEAD(&counter->list_entry);
Peter Zijlstra592903c2009-03-13 12:21:36 +01003456 INIT_LIST_HEAD(&counter->event_entry);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003457 INIT_LIST_HEAD(&counter->sibling_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003458 init_waitqueue_head(&counter->waitq);
3459
Peter Zijlstra7b732a72009-03-23 18:22:10 +01003460 mutex_init(&counter->mmap_mutex);
3461
Peter Zijlstraa96bbc12009-06-03 14:01:36 +02003462 counter->cpu = cpu;
Peter Zijlstra0d486962009-06-02 19:22:16 +02003463 counter->attr = *attr;
Peter Zijlstraa96bbc12009-06-03 14:01:36 +02003464 counter->group_leader = group_leader;
3465 counter->pmu = NULL;
3466 counter->ctx = ctx;
3467 counter->oncpu = -1;
Ingo Molnar329d8762009-05-26 08:10:00 +02003468
Peter Zijlstraa96bbc12009-06-03 14:01:36 +02003469 counter->ns = get_pid_ns(current->nsproxy->pid_ns);
3470 counter->id = atomic64_inc_return(&perf_counter_id);
3471
3472 counter->state = PERF_COUNTER_STATE_INACTIVE;
3473
Peter Zijlstra0d486962009-06-02 19:22:16 +02003474 if (attr->disabled)
Ingo Molnara86ed502008-12-17 00:43:10 +01003475 counter->state = PERF_COUNTER_STATE_OFF;
3476
Robert Richter4aeb0b42009-04-29 12:47:03 +02003477 pmu = NULL;
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003478
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003479 hwc = &counter->hw;
Peter Zijlstra0d486962009-06-02 19:22:16 +02003480 if (attr->freq && attr->sample_freq)
3481 hwc->sample_period = div64_u64(TICK_NSEC, attr->sample_freq);
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003482 else
Peter Zijlstra0d486962009-06-02 19:22:16 +02003483 hwc->sample_period = attr->sample_period;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003484
Peter Zijlstra2023b352009-05-05 17:50:26 +02003485 /*
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003486 * we currently do not support PERF_SAMPLE_GROUP on inherited counters
Peter Zijlstra2023b352009-05-05 17:50:26 +02003487 */
Peter Zijlstra0d486962009-06-02 19:22:16 +02003488 if (attr->inherit && (attr->sample_type & PERF_SAMPLE_GROUP))
Peter Zijlstra2023b352009-05-05 17:50:26 +02003489 goto done;
3490
Peter Zijlstra0d486962009-06-02 19:22:16 +02003491 if (perf_event_raw(attr)) {
Robert Richter4aeb0b42009-04-29 12:47:03 +02003492 pmu = hw_perf_counter_init(counter);
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01003493 goto done;
3494 }
3495
Peter Zijlstra0d486962009-06-02 19:22:16 +02003496 switch (perf_event_type(attr)) {
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003497 case PERF_TYPE_HARDWARE:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003498 pmu = hw_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003499 break;
3500
3501 case PERF_TYPE_SOFTWARE:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003502 pmu = sw_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003503 break;
3504
3505 case PERF_TYPE_TRACEPOINT:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003506 pmu = tp_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003507 break;
3508 }
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01003509done:
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003510 err = 0;
Robert Richter4aeb0b42009-04-29 12:47:03 +02003511 if (!pmu)
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003512 err = -EINVAL;
Robert Richter4aeb0b42009-04-29 12:47:03 +02003513 else if (IS_ERR(pmu))
3514 err = PTR_ERR(pmu);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003515
3516 if (err) {
Peter Zijlstraa96bbc12009-06-03 14:01:36 +02003517 if (counter->ns)
3518 put_pid_ns(counter->ns);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003519 kfree(counter);
3520 return ERR_PTR(err);
3521 }
3522
Robert Richter4aeb0b42009-04-29 12:47:03 +02003523 counter->pmu = pmu;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003524
Peter Zijlstra7fc23a52009-05-08 18:52:21 +02003525 atomic_inc(&nr_counters);
Peter Zijlstra0d486962009-06-02 19:22:16 +02003526 if (counter->attr.mmap)
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003527 atomic_inc(&nr_mmap_counters);
Peter Zijlstra0d486962009-06-02 19:22:16 +02003528 if (counter->attr.munmap)
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003529 atomic_inc(&nr_munmap_counters);
Peter Zijlstra0d486962009-06-02 19:22:16 +02003530 if (counter->attr.comm)
Peter Zijlstra60313eb2009-06-04 16:53:44 +02003531 atomic_inc(&nr_comm_counters);
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02003532
Thomas Gleixner0793a612008-12-04 20:12:29 +01003533 return counter;
3534}
3535
3536/**
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003537 * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
Ingo Molnar9f66a382008-12-10 12:33:23 +01003538 *
Peter Zijlstra0d486962009-06-02 19:22:16 +02003539 * @attr_uptr: event type attributes for monitoring/sampling
Thomas Gleixner0793a612008-12-04 20:12:29 +01003540 * @pid: target pid
Ingo Molnar9f66a382008-12-10 12:33:23 +01003541 * @cpu: target cpu
3542 * @group_fd: group leader counter fd
Thomas Gleixner0793a612008-12-04 20:12:29 +01003543 */
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003544SYSCALL_DEFINE5(perf_counter_open,
Peter Zijlstra0d486962009-06-02 19:22:16 +02003545 const struct perf_counter_attr __user *, attr_uptr,
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003546 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003547{
Ingo Molnar04289bb2008-12-11 08:38:42 +01003548 struct perf_counter *counter, *group_leader;
Peter Zijlstra0d486962009-06-02 19:22:16 +02003549 struct perf_counter_attr attr;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003550 struct perf_counter_context *ctx;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003551 struct file *counter_file = NULL;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003552 struct file *group_file = NULL;
3553 int fput_needed = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003554 int fput_needed2 = 0;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003555 int ret;
3556
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003557 /* for future expandability... */
3558 if (flags)
3559 return -EINVAL;
3560
Peter Zijlstra0d486962009-06-02 19:22:16 +02003561 if (copy_from_user(&attr, attr_uptr, sizeof(attr)) != 0)
Thomas Gleixnereab656a2008-12-08 19:26:59 +01003562 return -EFAULT;
3563
Ingo Molnar04289bb2008-12-11 08:38:42 +01003564 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01003565 * Get the target context (task or percpu):
3566 */
3567 ctx = find_get_context(pid, cpu);
3568 if (IS_ERR(ctx))
3569 return PTR_ERR(ctx);
3570
3571 /*
3572 * Look up the group leader (we will attach this counter to it):
Ingo Molnar04289bb2008-12-11 08:38:42 +01003573 */
3574 group_leader = NULL;
3575 if (group_fd != -1) {
3576 ret = -EINVAL;
3577 group_file = fget_light(group_fd, &fput_needed);
3578 if (!group_file)
Ingo Molnarccff2862008-12-11 11:26:29 +01003579 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003580 if (group_file->f_op != &perf_fops)
Ingo Molnarccff2862008-12-11 11:26:29 +01003581 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003582
3583 group_leader = group_file->private_data;
3584 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01003585 * Do not allow a recursive hierarchy (this new sibling
3586 * becoming part of another group-sibling):
Ingo Molnar04289bb2008-12-11 08:38:42 +01003587 */
Ingo Molnarccff2862008-12-11 11:26:29 +01003588 if (group_leader->group_leader != group_leader)
3589 goto err_put_context;
3590 /*
3591 * Do not allow to attach to a group in a different
3592 * task or CPU context:
3593 */
3594 if (group_leader->ctx != ctx)
3595 goto err_put_context;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11003596 /*
3597 * Only a group leader can be exclusive or pinned
3598 */
Peter Zijlstra0d486962009-06-02 19:22:16 +02003599 if (attr.exclusive || attr.pinned)
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11003600 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003601 }
3602
Peter Zijlstra0d486962009-06-02 19:22:16 +02003603 counter = perf_counter_alloc(&attr, cpu, ctx, group_leader,
Paul Mackerras23a185c2009-02-09 22:42:47 +11003604 GFP_KERNEL);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003605 ret = PTR_ERR(counter);
3606 if (IS_ERR(counter))
Thomas Gleixner0793a612008-12-04 20:12:29 +01003607 goto err_put_context;
3608
Thomas Gleixner0793a612008-12-04 20:12:29 +01003609 ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
3610 if (ret < 0)
Ingo Molnar9b51f662008-12-12 13:49:45 +01003611 goto err_free_put_context;
3612
3613 counter_file = fget_light(ret, &fput_needed2);
3614 if (!counter_file)
3615 goto err_free_put_context;
3616
3617 counter->filp = counter_file;
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003618 WARN_ON_ONCE(ctx->parent_ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003619 mutex_lock(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003620 perf_install_in_context(ctx, counter, cpu);
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003621 ++ctx->generation;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003622 mutex_unlock(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003623
Peter Zijlstra082ff5a2009-05-23 18:29:00 +02003624 counter->owner = current;
3625 get_task_struct(current);
3626 mutex_lock(&current->perf_counter_mutex);
3627 list_add_tail(&counter->owner_entry, &current->perf_counter_list);
3628 mutex_unlock(&current->perf_counter_mutex);
3629
Ingo Molnar9b51f662008-12-12 13:49:45 +01003630 fput_light(counter_file, fput_needed2);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003631
Ingo Molnar04289bb2008-12-11 08:38:42 +01003632out_fput:
3633 fput_light(group_file, fput_needed);
3634
Thomas Gleixner0793a612008-12-04 20:12:29 +01003635 return ret;
3636
Ingo Molnar9b51f662008-12-12 13:49:45 +01003637err_free_put_context:
Thomas Gleixner0793a612008-12-04 20:12:29 +01003638 kfree(counter);
3639
3640err_put_context:
Paul Mackerrasc93f7662009-05-28 22:18:17 +10003641 put_ctx(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003642
Ingo Molnar04289bb2008-12-11 08:38:42 +01003643 goto out_fput;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003644}
3645
Ingo Molnar9b51f662008-12-12 13:49:45 +01003646/*
Ingo Molnar9b51f662008-12-12 13:49:45 +01003647 * inherit a counter from parent task to child task:
3648 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11003649static struct perf_counter *
Ingo Molnar9b51f662008-12-12 13:49:45 +01003650inherit_counter(struct perf_counter *parent_counter,
3651 struct task_struct *parent,
3652 struct perf_counter_context *parent_ctx,
3653 struct task_struct *child,
Paul Mackerrasd859e292009-01-17 18:10:22 +11003654 struct perf_counter *group_leader,
Ingo Molnar9b51f662008-12-12 13:49:45 +01003655 struct perf_counter_context *child_ctx)
3656{
3657 struct perf_counter *child_counter;
3658
Paul Mackerrasd859e292009-01-17 18:10:22 +11003659 /*
3660 * Instead of creating recursive hierarchies of counters,
3661 * we link inherited counters back to the original parent,
3662 * which has a filp for sure, which we use as the reference
3663 * count:
3664 */
3665 if (parent_counter->parent)
3666 parent_counter = parent_counter->parent;
3667
Peter Zijlstra0d486962009-06-02 19:22:16 +02003668 child_counter = perf_counter_alloc(&parent_counter->attr,
Paul Mackerras23a185c2009-02-09 22:42:47 +11003669 parent_counter->cpu, child_ctx,
3670 group_leader, GFP_KERNEL);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003671 if (IS_ERR(child_counter))
3672 return child_counter;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10003673 get_ctx(child_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003674
3675 /*
Paul Mackerras564c2b22009-05-22 14:27:22 +10003676 * Make the child state follow the state of the parent counter,
Peter Zijlstra0d486962009-06-02 19:22:16 +02003677 * not its attr.disabled bit. We hold the parent's mutex,
Ingo Molnar22a4f652009-06-01 10:13:37 +02003678 * so we won't race with perf_counter_{en, dis}able_family.
Paul Mackerras564c2b22009-05-22 14:27:22 +10003679 */
3680 if (parent_counter->state >= PERF_COUNTER_STATE_INACTIVE)
3681 child_counter->state = PERF_COUNTER_STATE_INACTIVE;
3682 else
3683 child_counter->state = PERF_COUNTER_STATE_OFF;
3684
3685 /*
Ingo Molnar9b51f662008-12-12 13:49:45 +01003686 * Link it up in the child's context:
3687 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +11003688 add_counter_to_ctx(child_counter, child_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003689
3690 child_counter->parent = parent_counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003691 /*
3692 * inherit into child's child as well:
3693 */
Peter Zijlstra0d486962009-06-02 19:22:16 +02003694 child_counter->attr.inherit = 1;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003695
3696 /*
3697 * Get a reference to the parent filp - we will fput it
3698 * when the child counter exits. This is safe to do because
3699 * we are in the parent and we know that the filp still
3700 * exists and has a nonzero count:
3701 */
3702 atomic_long_inc(&parent_counter->filp->f_count);
3703
Paul Mackerrasd859e292009-01-17 18:10:22 +11003704 /*
3705 * Link this into the parent counter's child list
3706 */
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003707 WARN_ON_ONCE(parent_counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003708 mutex_lock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003709 list_add_tail(&child_counter->child_list, &parent_counter->child_list);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003710 mutex_unlock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003711
3712 return child_counter;
3713}
3714
3715static int inherit_group(struct perf_counter *parent_counter,
3716 struct task_struct *parent,
3717 struct perf_counter_context *parent_ctx,
3718 struct task_struct *child,
3719 struct perf_counter_context *child_ctx)
3720{
3721 struct perf_counter *leader;
3722 struct perf_counter *sub;
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003723 struct perf_counter *child_ctr;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003724
3725 leader = inherit_counter(parent_counter, parent, parent_ctx,
3726 child, NULL, child_ctx);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003727 if (IS_ERR(leader))
3728 return PTR_ERR(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003729 list_for_each_entry(sub, &parent_counter->sibling_list, list_entry) {
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003730 child_ctr = inherit_counter(sub, parent, parent_ctx,
3731 child, leader, child_ctx);
3732 if (IS_ERR(child_ctr))
3733 return PTR_ERR(child_ctr);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003734 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01003735 return 0;
3736}
3737
Paul Mackerrasd859e292009-01-17 18:10:22 +11003738static void sync_child_counter(struct perf_counter *child_counter,
3739 struct perf_counter *parent_counter)
3740{
Peter Zijlstra8bc20952009-05-15 20:45:59 +02003741 u64 child_val;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003742
Paul Mackerrasd859e292009-01-17 18:10:22 +11003743 child_val = atomic64_read(&child_counter->count);
3744
3745 /*
3746 * Add back the child's count to the parent's count:
3747 */
3748 atomic64_add(child_val, &parent_counter->count);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11003749 atomic64_add(child_counter->total_time_enabled,
3750 &parent_counter->child_total_time_enabled);
3751 atomic64_add(child_counter->total_time_running,
3752 &parent_counter->child_total_time_running);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003753
3754 /*
3755 * Remove this counter from the parent's list
3756 */
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003757 WARN_ON_ONCE(parent_counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003758 mutex_lock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003759 list_del_init(&child_counter->child_list);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003760 mutex_unlock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003761
3762 /*
3763 * Release the parent counter, if this was the last
3764 * reference to it.
3765 */
3766 fput(parent_counter->filp);
3767}
3768
Ingo Molnar9b51f662008-12-12 13:49:45 +01003769static void
Peter Zijlstrabbbee902009-05-29 14:25:58 +02003770__perf_counter_exit_task(struct perf_counter *child_counter,
Ingo Molnar9b51f662008-12-12 13:49:45 +01003771 struct perf_counter_context *child_ctx)
3772{
3773 struct perf_counter *parent_counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003774
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003775 update_counter_times(child_counter);
Peter Zijlstraaa9c67f2009-05-23 18:28:59 +02003776 perf_counter_remove_from_context(child_counter);
Ingo Molnar0cc0c022008-12-14 23:20:36 +01003777
Ingo Molnar9b51f662008-12-12 13:49:45 +01003778 parent_counter = child_counter->parent;
3779 /*
3780 * It can happen that parent exits first, and has counters
3781 * that are still around due to the child reference. These
3782 * counters need to be zapped - but otherwise linger.
3783 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11003784 if (parent_counter) {
3785 sync_child_counter(child_counter, parent_counter);
Peter Zijlstraf1600952009-03-19 20:26:16 +01003786 free_counter(child_counter);
Paul Mackerras4bcf3492009-02-11 13:53:19 +01003787 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01003788}
3789
3790/*
Paul Mackerrasd859e292009-01-17 18:10:22 +11003791 * When a child task exits, feed back counter values to parent counters.
Ingo Molnar9b51f662008-12-12 13:49:45 +01003792 */
3793void perf_counter_exit_task(struct task_struct *child)
3794{
3795 struct perf_counter *child_counter, *tmp;
3796 struct perf_counter_context *child_ctx;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003797 unsigned long flags;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003798
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003799 if (likely(!child->perf_counter_ctxp))
Ingo Molnar9b51f662008-12-12 13:49:45 +01003800 return;
3801
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003802 local_irq_save(flags);
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003803 /*
3804 * We can't reschedule here because interrupts are disabled,
3805 * and either child is current or it is a task that can't be
3806 * scheduled, so we are now safe from rescheduling changing
3807 * our context.
3808 */
3809 child_ctx = child->perf_counter_ctxp;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003810 __perf_counter_task_sched_out(child_ctx);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10003811
3812 /*
3813 * Take the context lock here so that if find_get_context is
3814 * reading child->perf_counter_ctxp, we wait until it has
3815 * incremented the context's refcount before we do put_ctx below.
3816 */
3817 spin_lock(&child_ctx->lock);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003818 child->perf_counter_ctxp = NULL;
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003819 if (child_ctx->parent_ctx) {
3820 /*
3821 * This context is a clone; unclone it so it can't get
3822 * swapped to another process while we're removing all
3823 * the counters from it.
3824 */
3825 put_ctx(child_ctx->parent_ctx);
3826 child_ctx->parent_ctx = NULL;
3827 }
Paul Mackerrasc93f7662009-05-28 22:18:17 +10003828 spin_unlock(&child_ctx->lock);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003829 local_irq_restore(flags);
3830
3831 mutex_lock(&child_ctx->mutex);
3832
Peter Zijlstra8bc20952009-05-15 20:45:59 +02003833again:
Ingo Molnar9b51f662008-12-12 13:49:45 +01003834 list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list,
3835 list_entry)
Peter Zijlstrabbbee902009-05-29 14:25:58 +02003836 __perf_counter_exit_task(child_counter, child_ctx);
Peter Zijlstra8bc20952009-05-15 20:45:59 +02003837
3838 /*
3839 * If the last counter was a group counter, it will have appended all
3840 * its siblings to the list, but we obtained 'tmp' before that which
3841 * will still point to the list head terminating the iteration.
3842 */
3843 if (!list_empty(&child_ctx->counter_list))
3844 goto again;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003845
3846 mutex_unlock(&child_ctx->mutex);
3847
3848 put_ctx(child_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003849}
3850
3851/*
Peter Zijlstrabbbee902009-05-29 14:25:58 +02003852 * free an unexposed, unused context as created by inheritance by
3853 * init_task below, used by fork() in case of fail.
3854 */
3855void perf_counter_free_task(struct task_struct *task)
3856{
3857 struct perf_counter_context *ctx = task->perf_counter_ctxp;
3858 struct perf_counter *counter, *tmp;
3859
3860 if (!ctx)
3861 return;
3862
3863 mutex_lock(&ctx->mutex);
3864again:
3865 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry) {
3866 struct perf_counter *parent = counter->parent;
3867
3868 if (WARN_ON_ONCE(!parent))
3869 continue;
3870
3871 mutex_lock(&parent->child_mutex);
3872 list_del_init(&counter->child_list);
3873 mutex_unlock(&parent->child_mutex);
3874
3875 fput(parent->filp);
3876
3877 list_del_counter(counter, ctx);
3878 free_counter(counter);
3879 }
3880
3881 if (!list_empty(&ctx->counter_list))
3882 goto again;
3883
3884 mutex_unlock(&ctx->mutex);
3885
3886 put_ctx(ctx);
3887}
3888
3889/*
Ingo Molnar9b51f662008-12-12 13:49:45 +01003890 * Initialize the perf_counter context in task_struct
3891 */
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02003892int perf_counter_init_task(struct task_struct *child)
Ingo Molnar9b51f662008-12-12 13:49:45 +01003893{
3894 struct perf_counter_context *child_ctx, *parent_ctx;
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003895 struct perf_counter_context *cloned_ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003896 struct perf_counter *counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003897 struct task_struct *parent = current;
Paul Mackerras564c2b22009-05-22 14:27:22 +10003898 int inherited_all = 1;
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02003899 int ret = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003900
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003901 child->perf_counter_ctxp = NULL;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003902
Peter Zijlstra082ff5a2009-05-23 18:29:00 +02003903 mutex_init(&child->perf_counter_mutex);
3904 INIT_LIST_HEAD(&child->perf_counter_list);
3905
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003906 if (likely(!parent->perf_counter_ctxp))
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02003907 return 0;
3908
Ingo Molnar9b51f662008-12-12 13:49:45 +01003909 /*
3910 * This is executed from the parent task context, so inherit
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003911 * counters that have been marked for cloning.
3912 * First allocate and initialize a context for the child.
Ingo Molnar9b51f662008-12-12 13:49:45 +01003913 */
3914
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003915 child_ctx = kmalloc(sizeof(struct perf_counter_context), GFP_KERNEL);
3916 if (!child_ctx)
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02003917 return -ENOMEM;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003918
3919 __perf_counter_init_context(child_ctx, child);
3920 child->perf_counter_ctxp = child_ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10003921 get_task_struct(child);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003922
Ingo Molnar9b51f662008-12-12 13:49:45 +01003923 /*
Paul Mackerras25346b932009-06-01 17:48:12 +10003924 * If the parent's context is a clone, pin it so it won't get
3925 * swapped under us.
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003926 */
Paul Mackerras25346b932009-06-01 17:48:12 +10003927 parent_ctx = perf_pin_task_context(parent);
3928
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003929 /*
3930 * No need to check if parent_ctx != NULL here; since we saw
3931 * it non-NULL earlier, the only reason for it to become NULL
3932 * is if we exit, and since we're currently in the middle of
3933 * a fork we can't be exiting at the same time.
3934 */
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003935
3936 /*
Ingo Molnar9b51f662008-12-12 13:49:45 +01003937 * Lock the parent list. No need to lock the child - not PID
3938 * hashed yet and not running, so nobody can access it.
3939 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11003940 mutex_lock(&parent_ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003941
3942 /*
3943 * We dont have to disable NMIs - we are only looking at
3944 * the list, not manipulating it:
3945 */
Peter Zijlstrad7b629a2009-05-20 12:21:19 +02003946 list_for_each_entry_rcu(counter, &parent_ctx->event_list, event_entry) {
3947 if (counter != counter->group_leader)
3948 continue;
3949
Peter Zijlstra0d486962009-06-02 19:22:16 +02003950 if (!counter->attr.inherit) {
Paul Mackerras564c2b22009-05-22 14:27:22 +10003951 inherited_all = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003952 continue;
Paul Mackerras564c2b22009-05-22 14:27:22 +10003953 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01003954
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02003955 ret = inherit_group(counter, parent, parent_ctx,
3956 child, child_ctx);
3957 if (ret) {
Paul Mackerras564c2b22009-05-22 14:27:22 +10003958 inherited_all = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003959 break;
Paul Mackerras564c2b22009-05-22 14:27:22 +10003960 }
3961 }
3962
3963 if (inherited_all) {
3964 /*
3965 * Mark the child context as a clone of the parent
3966 * context, or of whatever the parent is a clone of.
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003967 * Note that if the parent is a clone, it could get
3968 * uncloned at any point, but that doesn't matter
3969 * because the list of counters and the generation
3970 * count can't have changed since we took the mutex.
Paul Mackerras564c2b22009-05-22 14:27:22 +10003971 */
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003972 cloned_ctx = rcu_dereference(parent_ctx->parent_ctx);
3973 if (cloned_ctx) {
3974 child_ctx->parent_ctx = cloned_ctx;
Paul Mackerras25346b932009-06-01 17:48:12 +10003975 child_ctx->parent_gen = parent_ctx->parent_gen;
Paul Mackerras564c2b22009-05-22 14:27:22 +10003976 } else {
3977 child_ctx->parent_ctx = parent_ctx;
3978 child_ctx->parent_gen = parent_ctx->generation;
3979 }
3980 get_ctx(child_ctx->parent_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003981 }
3982
Paul Mackerrasd859e292009-01-17 18:10:22 +11003983 mutex_unlock(&parent_ctx->mutex);
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02003984
Paul Mackerras25346b932009-06-01 17:48:12 +10003985 perf_unpin_context(parent_ctx);
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003986
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02003987 return ret;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003988}
3989
Ingo Molnar04289bb2008-12-11 08:38:42 +01003990static void __cpuinit perf_counter_init_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003991{
Ingo Molnar04289bb2008-12-11 08:38:42 +01003992 struct perf_cpu_context *cpuctx;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003993
Ingo Molnar04289bb2008-12-11 08:38:42 +01003994 cpuctx = &per_cpu(perf_cpu_context, cpu);
3995 __perf_counter_init_context(&cpuctx->ctx, NULL);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003996
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003997 spin_lock(&perf_resource_lock);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003998 cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu;
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003999 spin_unlock(&perf_resource_lock);
Ingo Molnar04289bb2008-12-11 08:38:42 +01004000
Paul Mackerras01d02872009-01-14 13:44:19 +11004001 hw_perf_counter_setup(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004002}
4003
4004#ifdef CONFIG_HOTPLUG_CPU
Ingo Molnar04289bb2008-12-11 08:38:42 +01004005static void __perf_counter_exit_cpu(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01004006{
4007 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
4008 struct perf_counter_context *ctx = &cpuctx->ctx;
4009 struct perf_counter *counter, *tmp;
4010
Ingo Molnar04289bb2008-12-11 08:38:42 +01004011 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry)
4012 __perf_counter_remove_from_context(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004013}
Ingo Molnar04289bb2008-12-11 08:38:42 +01004014static void perf_counter_exit_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01004015{
Paul Mackerrasd859e292009-01-17 18:10:22 +11004016 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
4017 struct perf_counter_context *ctx = &cpuctx->ctx;
4018
4019 mutex_lock(&ctx->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01004020 smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1);
Paul Mackerrasd859e292009-01-17 18:10:22 +11004021 mutex_unlock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004022}
4023#else
Ingo Molnar04289bb2008-12-11 08:38:42 +01004024static inline void perf_counter_exit_cpu(int cpu) { }
Thomas Gleixner0793a612008-12-04 20:12:29 +01004025#endif
4026
4027static int __cpuinit
4028perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
4029{
4030 unsigned int cpu = (long)hcpu;
4031
4032 switch (action) {
4033
4034 case CPU_UP_PREPARE:
4035 case CPU_UP_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01004036 perf_counter_init_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004037 break;
4038
4039 case CPU_DOWN_PREPARE:
4040 case CPU_DOWN_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01004041 perf_counter_exit_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004042 break;
4043
4044 default:
4045 break;
4046 }
4047
4048 return NOTIFY_OK;
4049}
4050
Paul Mackerrasf38b0822009-06-02 21:05:16 +10004051/*
4052 * This has to have a higher priority than migration_notifier in sched.c.
4053 */
Thomas Gleixner0793a612008-12-04 20:12:29 +01004054static struct notifier_block __cpuinitdata perf_cpu_nb = {
4055 .notifier_call = perf_cpu_notify,
Paul Mackerrasf38b0822009-06-02 21:05:16 +10004056 .priority = 20,
Thomas Gleixner0793a612008-12-04 20:12:29 +01004057};
4058
Ingo Molnar0d905bc2009-05-04 19:13:30 +02004059void __init perf_counter_init(void)
Thomas Gleixner0793a612008-12-04 20:12:29 +01004060{
4061 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
4062 (void *)(long)smp_processor_id());
4063 register_cpu_notifier(&perf_cpu_nb);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004064}
Thomas Gleixner0793a612008-12-04 20:12:29 +01004065
4066static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
4067{
4068 return sprintf(buf, "%d\n", perf_reserved_percpu);
4069}
4070
4071static ssize_t
4072perf_set_reserve_percpu(struct sysdev_class *class,
4073 const char *buf,
4074 size_t count)
4075{
4076 struct perf_cpu_context *cpuctx;
4077 unsigned long val;
4078 int err, cpu, mpt;
4079
4080 err = strict_strtoul(buf, 10, &val);
4081 if (err)
4082 return err;
4083 if (val > perf_max_counters)
4084 return -EINVAL;
4085
Ingo Molnar1dce8d92009-05-04 19:23:18 +02004086 spin_lock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004087 perf_reserved_percpu = val;
4088 for_each_online_cpu(cpu) {
4089 cpuctx = &per_cpu(perf_cpu_context, cpu);
4090 spin_lock_irq(&cpuctx->ctx.lock);
4091 mpt = min(perf_max_counters - cpuctx->ctx.nr_counters,
4092 perf_max_counters - perf_reserved_percpu);
4093 cpuctx->max_pertask = mpt;
4094 spin_unlock_irq(&cpuctx->ctx.lock);
4095 }
Ingo Molnar1dce8d92009-05-04 19:23:18 +02004096 spin_unlock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004097
4098 return count;
4099}
4100
4101static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
4102{
4103 return sprintf(buf, "%d\n", perf_overcommit);
4104}
4105
4106static ssize_t
4107perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
4108{
4109 unsigned long val;
4110 int err;
4111
4112 err = strict_strtoul(buf, 10, &val);
4113 if (err)
4114 return err;
4115 if (val > 1)
4116 return -EINVAL;
4117
Ingo Molnar1dce8d92009-05-04 19:23:18 +02004118 spin_lock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004119 perf_overcommit = val;
Ingo Molnar1dce8d92009-05-04 19:23:18 +02004120 spin_unlock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004121
4122 return count;
4123}
4124
4125static SYSDEV_CLASS_ATTR(
4126 reserve_percpu,
4127 0644,
4128 perf_show_reserve_percpu,
4129 perf_set_reserve_percpu
4130 );
4131
4132static SYSDEV_CLASS_ATTR(
4133 overcommit,
4134 0644,
4135 perf_show_overcommit,
4136 perf_set_overcommit
4137 );
4138
4139static struct attribute *perfclass_attrs[] = {
4140 &attr_reserve_percpu.attr,
4141 &attr_overcommit.attr,
4142 NULL
4143};
4144
4145static struct attribute_group perfclass_attr_group = {
4146 .attrs = perfclass_attrs,
4147 .name = "perf_counters",
4148};
4149
4150static int __init perf_counter_sysfs_init(void)
4151{
4152 return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
4153 &perfclass_attr_group);
4154}
4155device_initcall(perf_counter_sysfs_init);