blob: abe2f3b6c4242aaad8d45577376b4e92545779c1 [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 Zijlstra9ee318a2009-04-09 10:53:44 +020043static atomic_t nr_mmap_tracking __read_mostly;
44static atomic_t nr_munmap_tracking __read_mostly;
45static atomic_t nr_comm_tracking __read_mostly;
46
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
Thomas Gleixner0793a612008-12-04 20:12:29 +010051/*
Ingo Molnar1dce8d92009-05-04 19:23:18 +020052 * Lock for (sysadmin-configurable) counter reservations:
Thomas Gleixner0793a612008-12-04 20:12:29 +010053 */
Ingo Molnar1dce8d92009-05-04 19:23:18 +020054static DEFINE_SPINLOCK(perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +010055
56/*
57 * Architecture provided APIs - weak aliases:
58 */
Robert Richter4aeb0b42009-04-29 12:47:03 +020059extern __weak const struct pmu *hw_perf_counter_init(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +010060{
Paul Mackerrasff6f0542009-01-09 16:19:25 +110061 return NULL;
Thomas Gleixner0793a612008-12-04 20:12:29 +010062}
63
Peter Zijlstra9e35ad32009-05-13 16:21:38 +020064void __weak hw_perf_disable(void) { barrier(); }
65void __weak hw_perf_enable(void) { barrier(); }
66
Paul Mackerras01d02872009-01-14 13:44:19 +110067void __weak hw_perf_counter_setup(int cpu) { barrier(); }
Ingo Molnar22a4f652009-06-01 10:13:37 +020068
69int __weak
70hw_perf_group_sched_in(struct perf_counter *group_leader,
Paul Mackerras3cbed422009-01-09 16:43:42 +110071 struct perf_cpu_context *cpuctx,
72 struct perf_counter_context *ctx, int cpu)
73{
74 return 0;
75}
Thomas Gleixner0793a612008-12-04 20:12:29 +010076
Paul Mackerras4eb96fc2009-01-09 17:24:34 +110077void __weak perf_counter_print_debug(void) { }
78
Peter Zijlstra9e35ad32009-05-13 16:21:38 +020079static DEFINE_PER_CPU(int, disable_count);
80
81void __perf_disable(void)
82{
83 __get_cpu_var(disable_count)++;
84}
85
86bool __perf_enable(void)
87{
88 return !--__get_cpu_var(disable_count);
89}
90
91void perf_disable(void)
92{
93 __perf_disable();
94 hw_perf_disable();
95}
Peter Zijlstra9e35ad32009-05-13 16:21:38 +020096
97void perf_enable(void)
98{
99 if (__perf_enable())
100 hw_perf_enable();
101}
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200102
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000103static void get_ctx(struct perf_counter_context *ctx)
104{
105 atomic_inc(&ctx->refcount);
106}
107
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000108static void free_ctx(struct rcu_head *head)
109{
110 struct perf_counter_context *ctx;
111
112 ctx = container_of(head, struct perf_counter_context, rcu_head);
113 kfree(ctx);
114}
115
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000116static void put_ctx(struct perf_counter_context *ctx)
117{
Paul Mackerras564c2b22009-05-22 14:27:22 +1000118 if (atomic_dec_and_test(&ctx->refcount)) {
119 if (ctx->parent_ctx)
120 put_ctx(ctx->parent_ctx);
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000121 if (ctx->task)
122 put_task_struct(ctx->task);
123 call_rcu(&ctx->rcu_head, free_ctx);
Paul Mackerras564c2b22009-05-22 14:27:22 +1000124 }
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000125}
126
Peter Zijlstrafccc7142009-05-23 18:28:56 +0200127/*
Paul Mackerras25346b932009-06-01 17:48:12 +1000128 * Get the perf_counter_context for a task and lock it.
129 * This has to cope with with the fact that until it is locked,
130 * the context could get moved to another task.
131 */
Ingo Molnar22a4f652009-06-01 10:13:37 +0200132static struct perf_counter_context *
133perf_lock_task_context(struct task_struct *task, unsigned long *flags)
Paul Mackerras25346b932009-06-01 17:48:12 +1000134{
135 struct perf_counter_context *ctx;
136
137 rcu_read_lock();
138 retry:
139 ctx = rcu_dereference(task->perf_counter_ctxp);
140 if (ctx) {
141 /*
142 * If this context is a clone of another, it might
143 * get swapped for another underneath us by
144 * perf_counter_task_sched_out, though the
145 * rcu_read_lock() protects us from any context
146 * getting freed. Lock the context and check if it
147 * got swapped before we could get the lock, and retry
148 * if so. If we locked the right context, then it
149 * can't get swapped on us any more.
150 */
151 spin_lock_irqsave(&ctx->lock, *flags);
152 if (ctx != rcu_dereference(task->perf_counter_ctxp)) {
153 spin_unlock_irqrestore(&ctx->lock, *flags);
154 goto retry;
155 }
156 }
157 rcu_read_unlock();
158 return ctx;
159}
160
161/*
162 * Get the context for a task and increment its pin_count so it
163 * can't get swapped to another task. This also increments its
164 * reference count so that the context can't get freed.
165 */
166static struct perf_counter_context *perf_pin_task_context(struct task_struct *task)
167{
168 struct perf_counter_context *ctx;
169 unsigned long flags;
170
171 ctx = perf_lock_task_context(task, &flags);
172 if (ctx) {
173 ++ctx->pin_count;
174 get_ctx(ctx);
175 spin_unlock_irqrestore(&ctx->lock, flags);
176 }
177 return ctx;
178}
179
180static void perf_unpin_context(struct perf_counter_context *ctx)
181{
182 unsigned long flags;
183
184 spin_lock_irqsave(&ctx->lock, flags);
185 --ctx->pin_count;
186 spin_unlock_irqrestore(&ctx->lock, flags);
187 put_ctx(ctx);
188}
189
190/*
Peter Zijlstrafccc7142009-05-23 18:28:56 +0200191 * Add a counter from the lists for its context.
192 * Must be called with ctx->mutex and ctx->lock held.
193 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100194static void
195list_add_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
196{
197 struct perf_counter *group_leader = counter->group_leader;
198
199 /*
200 * Depending on whether it is a standalone or sibling counter,
201 * add it straight to the context's counter list, or to the group
202 * leader's sibling list:
203 */
Peter Zijlstra3df5eda2009-05-08 18:52:22 +0200204 if (group_leader == counter)
Ingo Molnar04289bb2008-12-11 08:38:42 +0100205 list_add_tail(&counter->list_entry, &ctx->counter_list);
Peter Zijlstra5c148192009-03-25 12:30:23 +0100206 else {
Ingo Molnar04289bb2008-12-11 08:38:42 +0100207 list_add_tail(&counter->list_entry, &group_leader->sibling_list);
Peter Zijlstra5c148192009-03-25 12:30:23 +0100208 group_leader->nr_siblings++;
209 }
Peter Zijlstra592903c2009-03-13 12:21:36 +0100210
211 list_add_rcu(&counter->event_entry, &ctx->event_list);
Peter Zijlstra8bc20952009-05-15 20:45:59 +0200212 ctx->nr_counters++;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100213}
214
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000215/*
216 * Remove a counter from the lists for its context.
Peter Zijlstrafccc7142009-05-23 18:28:56 +0200217 * Must be called with ctx->mutex and ctx->lock held.
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000218 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100219static void
220list_del_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
221{
222 struct perf_counter *sibling, *tmp;
223
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000224 if (list_empty(&counter->list_entry))
225 return;
Peter Zijlstra8bc20952009-05-15 20:45:59 +0200226 ctx->nr_counters--;
227
Ingo Molnar04289bb2008-12-11 08:38:42 +0100228 list_del_init(&counter->list_entry);
Peter Zijlstra592903c2009-03-13 12:21:36 +0100229 list_del_rcu(&counter->event_entry);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100230
Peter Zijlstra5c148192009-03-25 12:30:23 +0100231 if (counter->group_leader != counter)
232 counter->group_leader->nr_siblings--;
233
Ingo Molnar04289bb2008-12-11 08:38:42 +0100234 /*
235 * If this was a group counter with sibling counters then
236 * upgrade the siblings to singleton counters by adding them
237 * to the context list directly:
238 */
239 list_for_each_entry_safe(sibling, tmp,
240 &counter->sibling_list, list_entry) {
241
Peter Zijlstra75564232009-03-13 12:21:29 +0100242 list_move_tail(&sibling->list_entry, &ctx->counter_list);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100243 sibling->group_leader = sibling;
244 }
245}
246
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100247static void
248counter_sched_out(struct perf_counter *counter,
249 struct perf_cpu_context *cpuctx,
250 struct perf_counter_context *ctx)
251{
252 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
253 return;
254
255 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200256 counter->tstamp_stopped = ctx->time;
Robert Richter4aeb0b42009-04-29 12:47:03 +0200257 counter->pmu->disable(counter);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100258 counter->oncpu = -1;
259
260 if (!is_software_counter(counter))
261 cpuctx->active_oncpu--;
262 ctx->nr_active--;
263 if (counter->hw_event.exclusive || !cpuctx->active_oncpu)
264 cpuctx->exclusive = 0;
265}
266
Paul Mackerrasd859e292009-01-17 18:10:22 +1100267static void
268group_sched_out(struct perf_counter *group_counter,
269 struct perf_cpu_context *cpuctx,
270 struct perf_counter_context *ctx)
271{
272 struct perf_counter *counter;
273
274 if (group_counter->state != PERF_COUNTER_STATE_ACTIVE)
275 return;
276
277 counter_sched_out(group_counter, cpuctx, ctx);
278
279 /*
280 * Schedule out siblings (if any):
281 */
282 list_for_each_entry(counter, &group_counter->sibling_list, list_entry)
283 counter_sched_out(counter, cpuctx, ctx);
284
285 if (group_counter->hw_event.exclusive)
286 cpuctx->exclusive = 0;
287}
288
Thomas Gleixner0793a612008-12-04 20:12:29 +0100289/*
290 * Cross CPU call to remove a performance counter
291 *
292 * We disable the counter on the hardware level first. After that we
293 * remove it from the context list.
294 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100295static void __perf_counter_remove_from_context(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100296{
297 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
298 struct perf_counter *counter = info;
299 struct perf_counter_context *ctx = counter->ctx;
300
301 /*
302 * If this is a task context, we need to check whether it is
303 * the current task context of this cpu. If not it has been
304 * scheduled out before the smp call arrived.
305 */
Peter Zijlstra665c2142009-05-29 14:51:57 +0200306 if (ctx->task && cpuctx->task_ctx != ctx)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100307 return;
308
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200309 spin_lock(&ctx->lock);
Ingo Molnar34adc802009-05-20 20:13:28 +0200310 /*
311 * Protect the list operation against NMI by disabling the
312 * counters on a global level.
313 */
314 perf_disable();
Thomas Gleixner0793a612008-12-04 20:12:29 +0100315
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100316 counter_sched_out(counter, cpuctx, ctx);
317
Ingo Molnar04289bb2008-12-11 08:38:42 +0100318 list_del_counter(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100319
320 if (!ctx->task) {
321 /*
322 * Allow more per task counters with respect to the
323 * reservation:
324 */
325 cpuctx->max_pertask =
326 min(perf_max_counters - ctx->nr_counters,
327 perf_max_counters - perf_reserved_percpu);
328 }
329
Ingo Molnar34adc802009-05-20 20:13:28 +0200330 perf_enable();
Peter Zijlstra665c2142009-05-29 14:51:57 +0200331 spin_unlock(&ctx->lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100332}
333
334
335/*
336 * Remove the counter from a task's (or a CPU's) list of counters.
337 *
Peter Zijlstrafccc7142009-05-23 18:28:56 +0200338 * Must be called with ctx->mutex held.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100339 *
340 * CPU counters are removed with a smp call. For task counters we only
341 * call when the task is on a CPU.
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000342 *
343 * If counter->ctx is a cloned context, callers must make sure that
344 * every task struct that counter->ctx->task could possibly point to
345 * remains valid. This is OK when called from perf_release since
346 * that only calls us on the top-level context, which can't be a clone.
347 * When called from perf_counter_exit_task, it's OK because the
348 * context has been detached from its task.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100349 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100350static void perf_counter_remove_from_context(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100351{
352 struct perf_counter_context *ctx = counter->ctx;
353 struct task_struct *task = ctx->task;
354
355 if (!task) {
356 /*
357 * Per cpu counters are removed via an smp call and
358 * the removal is always sucessful.
359 */
360 smp_call_function_single(counter->cpu,
Ingo Molnar04289bb2008-12-11 08:38:42 +0100361 __perf_counter_remove_from_context,
Thomas Gleixner0793a612008-12-04 20:12:29 +0100362 counter, 1);
363 return;
364 }
365
366retry:
Ingo Molnar04289bb2008-12-11 08:38:42 +0100367 task_oncpu_function_call(task, __perf_counter_remove_from_context,
Thomas Gleixner0793a612008-12-04 20:12:29 +0100368 counter);
369
370 spin_lock_irq(&ctx->lock);
371 /*
372 * If the context is active we need to retry the smp call.
373 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100374 if (ctx->nr_active && !list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100375 spin_unlock_irq(&ctx->lock);
376 goto retry;
377 }
378
379 /*
380 * The lock prevents that this context is scheduled in so we
Ingo Molnar04289bb2008-12-11 08:38:42 +0100381 * can remove the counter safely, if the call above did not
Thomas Gleixner0793a612008-12-04 20:12:29 +0100382 * succeed.
383 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100384 if (!list_empty(&counter->list_entry)) {
Ingo Molnar04289bb2008-12-11 08:38:42 +0100385 list_del_counter(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100386 }
387 spin_unlock_irq(&ctx->lock);
388}
389
Peter Zijlstra4af49982009-04-06 11:45:10 +0200390static inline u64 perf_clock(void)
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100391{
Peter Zijlstra4af49982009-04-06 11:45:10 +0200392 return cpu_clock(smp_processor_id());
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100393}
394
395/*
396 * Update the record of the current time in a context.
397 */
Peter Zijlstra4af49982009-04-06 11:45:10 +0200398static void update_context_time(struct perf_counter_context *ctx)
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100399{
Peter Zijlstra4af49982009-04-06 11:45:10 +0200400 u64 now = perf_clock();
401
402 ctx->time += now - ctx->timestamp;
403 ctx->timestamp = now;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100404}
405
406/*
407 * Update the total_time_enabled and total_time_running fields for a counter.
408 */
409static void update_counter_times(struct perf_counter *counter)
410{
411 struct perf_counter_context *ctx = counter->ctx;
412 u64 run_end;
413
Peter Zijlstra4af49982009-04-06 11:45:10 +0200414 if (counter->state < PERF_COUNTER_STATE_INACTIVE)
415 return;
416
417 counter->total_time_enabled = ctx->time - counter->tstamp_enabled;
418
419 if (counter->state == PERF_COUNTER_STATE_INACTIVE)
420 run_end = counter->tstamp_stopped;
421 else
422 run_end = ctx->time;
423
424 counter->total_time_running = run_end - counter->tstamp_running;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100425}
426
427/*
428 * Update total_time_enabled and total_time_running for all counters in a group.
429 */
430static void update_group_times(struct perf_counter *leader)
431{
432 struct perf_counter *counter;
433
434 update_counter_times(leader);
435 list_for_each_entry(counter, &leader->sibling_list, list_entry)
436 update_counter_times(counter);
437}
438
439/*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100440 * Cross CPU call to disable a performance counter
441 */
442static void __perf_counter_disable(void *info)
443{
444 struct perf_counter *counter = info;
445 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
446 struct perf_counter_context *ctx = counter->ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100447
448 /*
449 * If this is a per-task counter, need to check whether this
450 * counter's task is the current task on this cpu.
451 */
Peter Zijlstra665c2142009-05-29 14:51:57 +0200452 if (ctx->task && cpuctx->task_ctx != ctx)
Paul Mackerrasd859e292009-01-17 18:10:22 +1100453 return;
454
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200455 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100456
457 /*
458 * If the counter is on, turn it off.
459 * If it is in error state, leave it in error state.
460 */
461 if (counter->state >= PERF_COUNTER_STATE_INACTIVE) {
Peter Zijlstra4af49982009-04-06 11:45:10 +0200462 update_context_time(ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100463 update_counter_times(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100464 if (counter == counter->group_leader)
465 group_sched_out(counter, cpuctx, ctx);
466 else
467 counter_sched_out(counter, cpuctx, ctx);
468 counter->state = PERF_COUNTER_STATE_OFF;
469 }
470
Peter Zijlstra665c2142009-05-29 14:51:57 +0200471 spin_unlock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100472}
473
474/*
475 * Disable a counter.
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000476 *
477 * If counter->ctx is a cloned context, callers must make sure that
478 * every task struct that counter->ctx->task could possibly point to
479 * remains valid. This condition is satisifed when called through
480 * perf_counter_for_each_child or perf_counter_for_each because they
481 * hold the top-level counter's child_mutex, so any descendant that
482 * goes to exit will block in sync_child_counter.
483 * When called from perf_pending_counter it's OK because counter->ctx
484 * is the current context on this CPU and preemption is disabled,
485 * hence we can't get into perf_counter_task_sched_out for this context.
Paul Mackerrasd859e292009-01-17 18:10:22 +1100486 */
487static void perf_counter_disable(struct perf_counter *counter)
488{
489 struct perf_counter_context *ctx = counter->ctx;
490 struct task_struct *task = ctx->task;
491
492 if (!task) {
493 /*
494 * Disable the counter on the cpu that it's on
495 */
496 smp_call_function_single(counter->cpu, __perf_counter_disable,
497 counter, 1);
498 return;
499 }
500
501 retry:
502 task_oncpu_function_call(task, __perf_counter_disable, counter);
503
504 spin_lock_irq(&ctx->lock);
505 /*
506 * If the counter is still active, we need to retry the cross-call.
507 */
508 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
509 spin_unlock_irq(&ctx->lock);
510 goto retry;
511 }
512
513 /*
514 * Since we have the lock this context can't be scheduled
515 * in, so we can change the state safely.
516 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100517 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
518 update_counter_times(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100519 counter->state = PERF_COUNTER_STATE_OFF;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100520 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100521
522 spin_unlock_irq(&ctx->lock);
523}
524
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100525static int
526counter_sched_in(struct perf_counter *counter,
527 struct perf_cpu_context *cpuctx,
528 struct perf_counter_context *ctx,
529 int cpu)
530{
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100531 if (counter->state <= PERF_COUNTER_STATE_OFF)
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100532 return 0;
533
534 counter->state = PERF_COUNTER_STATE_ACTIVE;
535 counter->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */
536 /*
537 * The new state must be visible before we turn it on in the hardware:
538 */
539 smp_wmb();
540
Robert Richter4aeb0b42009-04-29 12:47:03 +0200541 if (counter->pmu->enable(counter)) {
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100542 counter->state = PERF_COUNTER_STATE_INACTIVE;
543 counter->oncpu = -1;
544 return -EAGAIN;
545 }
546
Peter Zijlstra4af49982009-04-06 11:45:10 +0200547 counter->tstamp_running += ctx->time - counter->tstamp_stopped;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100548
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100549 if (!is_software_counter(counter))
550 cpuctx->active_oncpu++;
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100551 ctx->nr_active++;
552
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100553 if (counter->hw_event.exclusive)
554 cpuctx->exclusive = 1;
555
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100556 return 0;
557}
558
Paul Mackerras6751b712009-05-11 12:08:02 +1000559static int
560group_sched_in(struct perf_counter *group_counter,
561 struct perf_cpu_context *cpuctx,
562 struct perf_counter_context *ctx,
563 int cpu)
564{
565 struct perf_counter *counter, *partial_group;
566 int ret;
567
568 if (group_counter->state == PERF_COUNTER_STATE_OFF)
569 return 0;
570
571 ret = hw_perf_group_sched_in(group_counter, cpuctx, ctx, cpu);
572 if (ret)
573 return ret < 0 ? ret : 0;
574
Paul Mackerras6751b712009-05-11 12:08:02 +1000575 if (counter_sched_in(group_counter, cpuctx, ctx, cpu))
576 return -EAGAIN;
577
578 /*
579 * Schedule in siblings as one group (if any):
580 */
581 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
Paul Mackerras6751b712009-05-11 12:08:02 +1000582 if (counter_sched_in(counter, cpuctx, ctx, cpu)) {
583 partial_group = counter;
584 goto group_error;
585 }
586 }
587
588 return 0;
589
590group_error:
591 /*
592 * Groups can be scheduled in as one unit only, so undo any
593 * partial group before returning:
594 */
595 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
596 if (counter == partial_group)
597 break;
598 counter_sched_out(counter, cpuctx, ctx);
599 }
600 counter_sched_out(group_counter, cpuctx, ctx);
601
602 return -EAGAIN;
603}
604
Thomas Gleixner0793a612008-12-04 20:12:29 +0100605/*
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100606 * Return 1 for a group consisting entirely of software counters,
607 * 0 if the group contains any hardware counters.
608 */
609static int is_software_only_group(struct perf_counter *leader)
610{
611 struct perf_counter *counter;
612
613 if (!is_software_counter(leader))
614 return 0;
Peter Zijlstra5c148192009-03-25 12:30:23 +0100615
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100616 list_for_each_entry(counter, &leader->sibling_list, list_entry)
617 if (!is_software_counter(counter))
618 return 0;
Peter Zijlstra5c148192009-03-25 12:30:23 +0100619
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100620 return 1;
621}
622
623/*
624 * Work out whether we can put this counter group on the CPU now.
625 */
626static int group_can_go_on(struct perf_counter *counter,
627 struct perf_cpu_context *cpuctx,
628 int can_add_hw)
629{
630 /*
631 * Groups consisting entirely of software counters can always go on.
632 */
633 if (is_software_only_group(counter))
634 return 1;
635 /*
636 * If an exclusive group is already on, no other hardware
637 * counters can go on.
638 */
639 if (cpuctx->exclusive)
640 return 0;
641 /*
642 * If this group is exclusive and there are already
643 * counters on the CPU, it can't go on.
644 */
645 if (counter->hw_event.exclusive && cpuctx->active_oncpu)
646 return 0;
647 /*
648 * Otherwise, try to add it if all previous groups were able
649 * to go on.
650 */
651 return can_add_hw;
652}
653
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100654static void add_counter_to_ctx(struct perf_counter *counter,
655 struct perf_counter_context *ctx)
656{
657 list_add_counter(counter, ctx);
Peter Zijlstra4af49982009-04-06 11:45:10 +0200658 counter->tstamp_enabled = ctx->time;
659 counter->tstamp_running = ctx->time;
660 counter->tstamp_stopped = ctx->time;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100661}
662
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100663/*
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100664 * Cross CPU call to install and enable a performance counter
Peter Zijlstra682076a2009-05-23 18:28:57 +0200665 *
666 * Must be called with ctx->mutex held
Thomas Gleixner0793a612008-12-04 20:12:29 +0100667 */
668static void __perf_install_in_context(void *info)
669{
670 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
671 struct perf_counter *counter = info;
672 struct perf_counter_context *ctx = counter->ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100673 struct perf_counter *leader = counter->group_leader;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100674 int cpu = smp_processor_id();
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100675 int err;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100676
677 /*
678 * If this is a task context, we need to check whether it is
679 * the current task context of this cpu. If not it has been
680 * scheduled out before the smp call arrived.
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000681 * Or possibly this is the right context but it isn't
682 * on this cpu because it had no counters.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100683 */
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000684 if (ctx->task && cpuctx->task_ctx != ctx) {
Peter Zijlstra665c2142009-05-29 14:51:57 +0200685 if (cpuctx->task_ctx || ctx->task != current)
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000686 return;
687 cpuctx->task_ctx = ctx;
688 }
Thomas Gleixner0793a612008-12-04 20:12:29 +0100689
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200690 spin_lock(&ctx->lock);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000691 ctx->is_active = 1;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200692 update_context_time(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100693
694 /*
695 * Protect the list operation against NMI by disabling the
696 * counters on a global level. NOP for non NMI based counters.
697 */
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200698 perf_disable();
Thomas Gleixner0793a612008-12-04 20:12:29 +0100699
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100700 add_counter_to_ctx(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100701
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100702 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100703 * Don't put the counter on if it is disabled or if
704 * it is in a group and the group isn't on.
705 */
706 if (counter->state != PERF_COUNTER_STATE_INACTIVE ||
707 (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE))
708 goto unlock;
709
710 /*
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100711 * An exclusive counter can't go on if there are already active
712 * hardware counters, and no hardware counter can go on if there
713 * is already an exclusive counter on.
714 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100715 if (!group_can_go_on(counter, cpuctx, 1))
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100716 err = -EEXIST;
717 else
718 err = counter_sched_in(counter, cpuctx, ctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100719
Paul Mackerrasd859e292009-01-17 18:10:22 +1100720 if (err) {
721 /*
722 * This counter couldn't go on. If it is in a group
723 * then we have to pull the whole group off.
724 * If the counter group is pinned then put it in error state.
725 */
726 if (leader != counter)
727 group_sched_out(leader, cpuctx, ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100728 if (leader->hw_event.pinned) {
729 update_group_times(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100730 leader->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100731 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100732 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100733
734 if (!err && !ctx->task && cpuctx->max_pertask)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100735 cpuctx->max_pertask--;
736
Paul Mackerrasd859e292009-01-17 18:10:22 +1100737 unlock:
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200738 perf_enable();
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100739
Peter Zijlstra665c2142009-05-29 14:51:57 +0200740 spin_unlock(&ctx->lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100741}
742
743/*
744 * Attach a performance counter to a context
745 *
746 * First we add the counter to the list with the hardware enable bit
747 * in counter->hw_config cleared.
748 *
749 * If the counter is attached to a task which is on a CPU we use a smp
750 * call to enable it in the task context. The task might have been
751 * scheduled away, but we check this in the smp call again.
Paul Mackerrasd859e292009-01-17 18:10:22 +1100752 *
753 * Must be called with ctx->mutex held.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100754 */
755static void
756perf_install_in_context(struct perf_counter_context *ctx,
757 struct perf_counter *counter,
758 int cpu)
759{
760 struct task_struct *task = ctx->task;
761
Thomas Gleixner0793a612008-12-04 20:12:29 +0100762 if (!task) {
763 /*
764 * Per cpu counters are installed via an smp call and
765 * the install is always sucessful.
766 */
767 smp_call_function_single(cpu, __perf_install_in_context,
768 counter, 1);
769 return;
770 }
771
Thomas Gleixner0793a612008-12-04 20:12:29 +0100772retry:
773 task_oncpu_function_call(task, __perf_install_in_context,
774 counter);
775
776 spin_lock_irq(&ctx->lock);
777 /*
Thomas Gleixner0793a612008-12-04 20:12:29 +0100778 * we need to retry the smp call.
779 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100780 if (ctx->is_active && list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100781 spin_unlock_irq(&ctx->lock);
782 goto retry;
783 }
784
785 /*
786 * The lock prevents that this context is scheduled in so we
787 * can add the counter safely, if it the call above did not
788 * succeed.
789 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100790 if (list_empty(&counter->list_entry))
791 add_counter_to_ctx(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100792 spin_unlock_irq(&ctx->lock);
793}
794
Paul Mackerrasd859e292009-01-17 18:10:22 +1100795/*
796 * Cross CPU call to enable a performance counter
797 */
798static void __perf_counter_enable(void *info)
Ingo Molnar04289bb2008-12-11 08:38:42 +0100799{
Paul Mackerrasd859e292009-01-17 18:10:22 +1100800 struct perf_counter *counter = info;
801 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
802 struct perf_counter_context *ctx = counter->ctx;
803 struct perf_counter *leader = counter->group_leader;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100804 int err;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100805
806 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100807 * If this is a per-task counter, need to check whether this
808 * counter's task is the current task on this cpu.
Ingo Molnar04289bb2008-12-11 08:38:42 +0100809 */
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000810 if (ctx->task && cpuctx->task_ctx != ctx) {
Peter Zijlstra665c2142009-05-29 14:51:57 +0200811 if (cpuctx->task_ctx || ctx->task != current)
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000812 return;
813 cpuctx->task_ctx = ctx;
814 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100815
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200816 spin_lock(&ctx->lock);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000817 ctx->is_active = 1;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200818 update_context_time(ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100819
820 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
821 goto unlock;
822 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200823 counter->tstamp_enabled = ctx->time - counter->total_time_enabled;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100824
825 /*
826 * If the counter is in a group and isn't the group leader,
827 * then don't put it on unless the group is on.
828 */
829 if (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE)
830 goto unlock;
831
Paul Mackerrase758a332009-05-12 21:59:01 +1000832 if (!group_can_go_on(counter, cpuctx, 1)) {
Paul Mackerrasd859e292009-01-17 18:10:22 +1100833 err = -EEXIST;
Paul Mackerrase758a332009-05-12 21:59:01 +1000834 } else {
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200835 perf_disable();
Paul Mackerrase758a332009-05-12 21:59:01 +1000836 if (counter == leader)
837 err = group_sched_in(counter, cpuctx, ctx,
838 smp_processor_id());
839 else
840 err = counter_sched_in(counter, cpuctx, ctx,
841 smp_processor_id());
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200842 perf_enable();
Paul Mackerrase758a332009-05-12 21:59:01 +1000843 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100844
845 if (err) {
846 /*
847 * If this counter can't go on and it's part of a
848 * group, then the whole group has to come off.
849 */
850 if (leader != counter)
851 group_sched_out(leader, cpuctx, ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100852 if (leader->hw_event.pinned) {
853 update_group_times(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100854 leader->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100855 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100856 }
857
858 unlock:
Peter Zijlstra665c2142009-05-29 14:51:57 +0200859 spin_unlock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100860}
861
862/*
863 * Enable a counter.
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000864 *
865 * If counter->ctx is a cloned context, callers must make sure that
866 * every task struct that counter->ctx->task could possibly point to
867 * remains valid. This condition is satisfied when called through
868 * perf_counter_for_each_child or perf_counter_for_each as described
869 * for perf_counter_disable.
Paul Mackerrasd859e292009-01-17 18:10:22 +1100870 */
871static void perf_counter_enable(struct perf_counter *counter)
872{
873 struct perf_counter_context *ctx = counter->ctx;
874 struct task_struct *task = ctx->task;
875
876 if (!task) {
877 /*
878 * Enable the counter on the cpu that it's on
879 */
880 smp_call_function_single(counter->cpu, __perf_counter_enable,
881 counter, 1);
882 return;
883 }
884
885 spin_lock_irq(&ctx->lock);
886 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
887 goto out;
888
889 /*
890 * If the counter is in error state, clear that first.
891 * That way, if we see the counter in error state below, we
892 * know that it has gone back into error state, as distinct
893 * from the task having been scheduled away before the
894 * cross-call arrived.
895 */
896 if (counter->state == PERF_COUNTER_STATE_ERROR)
897 counter->state = PERF_COUNTER_STATE_OFF;
898
899 retry:
900 spin_unlock_irq(&ctx->lock);
901 task_oncpu_function_call(task, __perf_counter_enable, counter);
902
903 spin_lock_irq(&ctx->lock);
904
905 /*
906 * If the context is active and the counter is still off,
907 * we need to retry the cross-call.
908 */
909 if (ctx->is_active && counter->state == PERF_COUNTER_STATE_OFF)
910 goto retry;
911
912 /*
913 * Since we have the lock this context can't be scheduled
914 * in, so we can change the state safely.
915 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100916 if (counter->state == PERF_COUNTER_STATE_OFF) {
Paul Mackerrasd859e292009-01-17 18:10:22 +1100917 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200918 counter->tstamp_enabled =
919 ctx->time - counter->total_time_enabled;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100920 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100921 out:
922 spin_unlock_irq(&ctx->lock);
923}
924
Peter Zijlstra2023b352009-05-05 17:50:26 +0200925static int perf_counter_refresh(struct perf_counter *counter, int refresh)
Peter Zijlstra79f14642009-04-06 11:45:07 +0200926{
Peter Zijlstra2023b352009-05-05 17:50:26 +0200927 /*
928 * not supported on inherited counters
929 */
930 if (counter->hw_event.inherit)
931 return -EINVAL;
932
Peter Zijlstra79f14642009-04-06 11:45:07 +0200933 atomic_add(refresh, &counter->event_limit);
934 perf_counter_enable(counter);
Peter Zijlstra2023b352009-05-05 17:50:26 +0200935
936 return 0;
Peter Zijlstra79f14642009-04-06 11:45:07 +0200937}
938
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100939void __perf_counter_sched_out(struct perf_counter_context *ctx,
940 struct perf_cpu_context *cpuctx)
941{
942 struct perf_counter *counter;
943
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100944 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100945 ctx->is_active = 0;
946 if (likely(!ctx->nr_counters))
947 goto out;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200948 update_context_time(ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100949
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200950 perf_disable();
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100951 if (ctx->nr_active) {
Peter Zijlstraafedadf2009-05-20 12:21:22 +0200952 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
953 if (counter != counter->group_leader)
954 counter_sched_out(counter, cpuctx, ctx);
955 else
956 group_sched_out(counter, cpuctx, ctx);
957 }
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100958 }
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200959 perf_enable();
Paul Mackerrasd859e292009-01-17 18:10:22 +1100960 out:
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100961 spin_unlock(&ctx->lock);
962}
963
Thomas Gleixner0793a612008-12-04 20:12:29 +0100964/*
Paul Mackerras564c2b22009-05-22 14:27:22 +1000965 * Test whether two contexts are equivalent, i.e. whether they
966 * have both been cloned from the same version of the same context
967 * and they both have the same number of enabled counters.
968 * If the number of enabled counters is the same, then the set
969 * of enabled counters should be the same, because these are both
970 * inherited contexts, therefore we can't access individual counters
971 * in them directly with an fd; we can only enable/disable all
972 * counters via prctl, or enable/disable all counters in a family
973 * via ioctl, which will have the same effect on both contexts.
974 */
975static int context_equiv(struct perf_counter_context *ctx1,
976 struct perf_counter_context *ctx2)
977{
978 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
Paul Mackerrasad3a37d2009-05-29 16:06:20 +1000979 && ctx1->parent_gen == ctx2->parent_gen
Paul Mackerras25346b932009-06-01 17:48:12 +1000980 && !ctx1->pin_count && !ctx2->pin_count;
Paul Mackerras564c2b22009-05-22 14:27:22 +1000981}
982
983/*
Thomas Gleixner0793a612008-12-04 20:12:29 +0100984 * Called from scheduler to remove the counters of the current task,
985 * with interrupts disabled.
986 *
987 * We stop each counter and update the counter value in counter->count.
988 *
Ingo Molnar76715812008-12-17 14:20:28 +0100989 * This does not protect us against NMI, but disable()
Thomas Gleixner0793a612008-12-04 20:12:29 +0100990 * sets the disabled bit in the control field of counter _before_
991 * accessing the counter control register. If a NMI hits, then it will
992 * not restart the counter.
993 */
Paul Mackerras564c2b22009-05-22 14:27:22 +1000994void perf_counter_task_sched_out(struct task_struct *task,
995 struct task_struct *next, int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100996{
997 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000998 struct perf_counter_context *ctx = task->perf_counter_ctxp;
Paul Mackerras564c2b22009-05-22 14:27:22 +1000999 struct perf_counter_context *next_ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001000 struct perf_counter_context *parent;
Peter Zijlstra4a0deca2009-03-19 20:26:12 +01001001 struct pt_regs *regs;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001002 int do_switch = 1;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001003
Peter Zijlstra10989fb2009-05-25 14:45:28 +02001004 regs = task_pt_regs(task);
1005 perf_swcounter_event(PERF_COUNT_CONTEXT_SWITCHES, 1, 1, regs, 0);
1006
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001007 if (likely(!ctx || !cpuctx->task_ctx))
Thomas Gleixner0793a612008-12-04 20:12:29 +01001008 return;
1009
Peter Zijlstrabce379b2009-04-06 11:45:13 +02001010 update_context_time(ctx);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001011
1012 rcu_read_lock();
1013 parent = rcu_dereference(ctx->parent_ctx);
Paul Mackerras564c2b22009-05-22 14:27:22 +10001014 next_ctx = next->perf_counter_ctxp;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001015 if (parent && next_ctx &&
1016 rcu_dereference(next_ctx->parent_ctx) == parent) {
1017 /*
1018 * Looks like the two contexts are clones, so we might be
1019 * able to optimize the context switch. We lock both
1020 * contexts and check that they are clones under the
1021 * lock (including re-checking that neither has been
1022 * uncloned in the meantime). It doesn't matter which
1023 * order we take the locks because no other cpu could
1024 * be trying to lock both of these tasks.
1025 */
1026 spin_lock(&ctx->lock);
1027 spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
1028 if (context_equiv(ctx, next_ctx)) {
Peter Zijlstra665c2142009-05-29 14:51:57 +02001029 /*
1030 * XXX do we need a memory barrier of sorts
1031 * wrt to rcu_dereference() of perf_counter_ctxp
1032 */
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001033 task->perf_counter_ctxp = next_ctx;
1034 next->perf_counter_ctxp = ctx;
1035 ctx->task = next;
1036 next_ctx->task = task;
1037 do_switch = 0;
1038 }
1039 spin_unlock(&next_ctx->lock);
1040 spin_unlock(&ctx->lock);
Paul Mackerras564c2b22009-05-22 14:27:22 +10001041 }
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001042 rcu_read_unlock();
Paul Mackerras564c2b22009-05-22 14:27:22 +10001043
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001044 if (do_switch) {
1045 __perf_counter_sched_out(ctx, cpuctx);
1046 cpuctx->task_ctx = NULL;
1047 }
Thomas Gleixner0793a612008-12-04 20:12:29 +01001048}
1049
Peter Zijlstra665c2142009-05-29 14:51:57 +02001050/*
1051 * Called with IRQs disabled
1052 */
Paul Mackerrasa08b1592009-05-11 15:46:10 +10001053static void __perf_counter_task_sched_out(struct perf_counter_context *ctx)
1054{
1055 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1056
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001057 if (!cpuctx->task_ctx)
1058 return;
Ingo Molnar012b84d2009-05-17 11:08:41 +02001059
1060 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
1061 return;
1062
Paul Mackerrasa08b1592009-05-11 15:46:10 +10001063 __perf_counter_sched_out(ctx, cpuctx);
1064 cpuctx->task_ctx = NULL;
1065}
1066
Peter Zijlstra665c2142009-05-29 14:51:57 +02001067/*
1068 * Called with IRQs disabled
1069 */
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001070static void perf_counter_cpu_sched_out(struct perf_cpu_context *cpuctx)
Ingo Molnar04289bb2008-12-11 08:38:42 +01001071{
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001072 __perf_counter_sched_out(&cpuctx->ctx, cpuctx);
Ingo Molnar04289bb2008-12-11 08:38:42 +01001073}
1074
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001075static void
1076__perf_counter_sched_in(struct perf_counter_context *ctx,
1077 struct perf_cpu_context *cpuctx, int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001078{
Thomas Gleixner0793a612008-12-04 20:12:29 +01001079 struct perf_counter *counter;
Paul Mackerrasdd0e6ba2009-01-12 15:11:00 +11001080 int can_add_hw = 1;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001081
Thomas Gleixner0793a612008-12-04 20:12:29 +01001082 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001083 ctx->is_active = 1;
1084 if (likely(!ctx->nr_counters))
1085 goto out;
1086
Peter Zijlstra4af49982009-04-06 11:45:10 +02001087 ctx->timestamp = perf_clock();
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001088
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001089 perf_disable();
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001090
1091 /*
1092 * First go through the list and put on any pinned groups
1093 * in order to give them the best chance of going on.
1094 */
Ingo Molnar04289bb2008-12-11 08:38:42 +01001095 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001096 if (counter->state <= PERF_COUNTER_STATE_OFF ||
1097 !counter->hw_event.pinned)
1098 continue;
1099 if (counter->cpu != -1 && counter->cpu != cpu)
1100 continue;
1101
Peter Zijlstraafedadf2009-05-20 12:21:22 +02001102 if (counter != counter->group_leader)
1103 counter_sched_in(counter, cpuctx, ctx, cpu);
1104 else {
1105 if (group_can_go_on(counter, cpuctx, 1))
1106 group_sched_in(counter, cpuctx, ctx, cpu);
1107 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001108
1109 /*
1110 * If this pinned group hasn't been scheduled,
1111 * put it in error state.
1112 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001113 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
1114 update_group_times(counter);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001115 counter->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001116 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001117 }
1118
1119 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1120 /*
1121 * Ignore counters in OFF or ERROR state, and
1122 * ignore pinned counters since we did them already.
1123 */
1124 if (counter->state <= PERF_COUNTER_STATE_OFF ||
1125 counter->hw_event.pinned)
1126 continue;
1127
Ingo Molnar04289bb2008-12-11 08:38:42 +01001128 /*
1129 * Listen to the 'cpu' scheduling filter constraint
1130 * of counters:
1131 */
Thomas Gleixner0793a612008-12-04 20:12:29 +01001132 if (counter->cpu != -1 && counter->cpu != cpu)
1133 continue;
1134
Peter Zijlstraafedadf2009-05-20 12:21:22 +02001135 if (counter != counter->group_leader) {
1136 if (counter_sched_in(counter, cpuctx, ctx, cpu))
Paul Mackerrasdd0e6ba2009-01-12 15:11:00 +11001137 can_add_hw = 0;
Peter Zijlstraafedadf2009-05-20 12:21:22 +02001138 } else {
1139 if (group_can_go_on(counter, cpuctx, can_add_hw)) {
1140 if (group_sched_in(counter, cpuctx, ctx, cpu))
1141 can_add_hw = 0;
1142 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001143 }
Thomas Gleixner0793a612008-12-04 20:12:29 +01001144 }
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001145 perf_enable();
Paul Mackerrasd859e292009-01-17 18:10:22 +11001146 out:
Thomas Gleixner0793a612008-12-04 20:12:29 +01001147 spin_unlock(&ctx->lock);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001148}
Ingo Molnar04289bb2008-12-11 08:38:42 +01001149
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001150/*
1151 * Called from scheduler to add the counters of the current task
1152 * with interrupts disabled.
1153 *
1154 * We restore the counter value and then enable it.
1155 *
1156 * This does not protect us against NMI, but enable()
1157 * sets the enabled bit in the control field of counter _before_
1158 * accessing the counter control register. If a NMI hits, then it will
1159 * keep the counter running.
1160 */
1161void perf_counter_task_sched_in(struct task_struct *task, int cpu)
1162{
1163 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001164 struct perf_counter_context *ctx = task->perf_counter_ctxp;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001165
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001166 if (likely(!ctx))
1167 return;
Paul Mackerras564c2b22009-05-22 14:27:22 +10001168 if (cpuctx->task_ctx == ctx)
1169 return;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001170 __perf_counter_sched_in(ctx, cpuctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001171 cpuctx->task_ctx = ctx;
1172}
1173
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001174static void perf_counter_cpu_sched_in(struct perf_cpu_context *cpuctx, int cpu)
1175{
1176 struct perf_counter_context *ctx = &cpuctx->ctx;
1177
1178 __perf_counter_sched_in(ctx, cpuctx, cpu);
1179}
1180
Peter Zijlstraa78ac322009-05-25 17:39:05 +02001181#define MAX_INTERRUPTS (~0ULL)
1182
1183static void perf_log_throttle(struct perf_counter *counter, int enable);
Peter Zijlstra26b119b2009-05-20 12:21:20 +02001184static void perf_log_period(struct perf_counter *counter, u64 period);
1185
1186static void perf_adjust_freq(struct perf_counter_context *ctx)
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001187{
1188 struct perf_counter *counter;
Peter Zijlstrab23f3322009-06-02 15:13:03 +02001189 u64 interrupts, sample_period;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001190 u64 events, period;
1191 s64 delta;
1192
1193 spin_lock(&ctx->lock);
1194 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1195 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
1196 continue;
1197
Peter Zijlstraa78ac322009-05-25 17:39:05 +02001198 interrupts = counter->hw.interrupts;
1199 counter->hw.interrupts = 0;
1200
1201 if (interrupts == MAX_INTERRUPTS) {
1202 perf_log_throttle(counter, 1);
1203 counter->pmu->unthrottle(counter);
1204 interrupts = 2*sysctl_perf_counter_limit/HZ;
1205 }
1206
Peter Zijlstrab23f3322009-06-02 15:13:03 +02001207 if (!counter->hw_event.freq || !counter->hw_event.sample_freq)
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001208 continue;
1209
Peter Zijlstrab23f3322009-06-02 15:13:03 +02001210 events = HZ * interrupts * counter->hw.sample_period;
1211 period = div64_u64(events, counter->hw_event.sample_freq);
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001212
Peter Zijlstrab23f3322009-06-02 15:13:03 +02001213 delta = (s64)(1 + period - counter->hw.sample_period);
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001214 delta >>= 1;
1215
Peter Zijlstrab23f3322009-06-02 15:13:03 +02001216 sample_period = counter->hw.sample_period + delta;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001217
Peter Zijlstrab23f3322009-06-02 15:13:03 +02001218 if (!sample_period)
1219 sample_period = 1;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001220
Peter Zijlstrab23f3322009-06-02 15:13:03 +02001221 perf_log_period(counter, sample_period);
Peter Zijlstra26b119b2009-05-20 12:21:20 +02001222
Peter Zijlstrab23f3322009-06-02 15:13:03 +02001223 counter->hw.sample_period = sample_period;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001224 }
1225 spin_unlock(&ctx->lock);
1226}
1227
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001228/*
1229 * Round-robin a context's counters:
1230 */
1231static void rotate_ctx(struct perf_counter_context *ctx)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001232{
Thomas Gleixner0793a612008-12-04 20:12:29 +01001233 struct perf_counter *counter;
1234
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001235 if (!ctx->nr_counters)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001236 return;
1237
Thomas Gleixner0793a612008-12-04 20:12:29 +01001238 spin_lock(&ctx->lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001239 /*
Ingo Molnar04289bb2008-12-11 08:38:42 +01001240 * Rotate the first entry last (works just fine for group counters too):
Thomas Gleixner0793a612008-12-04 20:12:29 +01001241 */
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001242 perf_disable();
Ingo Molnar04289bb2008-12-11 08:38:42 +01001243 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Peter Zijlstra75564232009-03-13 12:21:29 +01001244 list_move_tail(&counter->list_entry, &ctx->counter_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001245 break;
1246 }
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001247 perf_enable();
Thomas Gleixner0793a612008-12-04 20:12:29 +01001248
1249 spin_unlock(&ctx->lock);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001250}
Thomas Gleixner0793a612008-12-04 20:12:29 +01001251
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001252void perf_counter_task_tick(struct task_struct *curr, int cpu)
1253{
Peter Zijlstra7fc23a52009-05-08 18:52:21 +02001254 struct perf_cpu_context *cpuctx;
1255 struct perf_counter_context *ctx;
1256
1257 if (!atomic_read(&nr_counters))
1258 return;
1259
1260 cpuctx = &per_cpu(perf_cpu_context, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001261 ctx = curr->perf_counter_ctxp;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001262
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001263 perf_adjust_freq(&cpuctx->ctx);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001264 if (ctx)
1265 perf_adjust_freq(ctx);
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001266
Ingo Molnarb82914c2009-05-04 18:54:32 +02001267 perf_counter_cpu_sched_out(cpuctx);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001268 if (ctx)
1269 __perf_counter_task_sched_out(ctx);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001270
Ingo Molnarb82914c2009-05-04 18:54:32 +02001271 rotate_ctx(&cpuctx->ctx);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001272 if (ctx)
1273 rotate_ctx(ctx);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001274
Ingo Molnarb82914c2009-05-04 18:54:32 +02001275 perf_counter_cpu_sched_in(cpuctx, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001276 if (ctx)
1277 perf_counter_task_sched_in(curr, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001278}
1279
1280/*
Thomas Gleixner0793a612008-12-04 20:12:29 +01001281 * Cross CPU call to read the hardware counter
1282 */
Ingo Molnar76715812008-12-17 14:20:28 +01001283static void __read(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001284{
Ingo Molnar621a01e2008-12-11 12:46:46 +01001285 struct perf_counter *counter = info;
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001286 struct perf_counter_context *ctx = counter->ctx;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001287 unsigned long flags;
Ingo Molnar621a01e2008-12-11 12:46:46 +01001288
Peter Zijlstra849691a2009-04-06 11:45:12 +02001289 local_irq_save(flags);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001290 if (ctx->is_active)
Peter Zijlstra4af49982009-04-06 11:45:10 +02001291 update_context_time(ctx);
Robert Richter4aeb0b42009-04-29 12:47:03 +02001292 counter->pmu->read(counter);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001293 update_counter_times(counter);
Peter Zijlstra849691a2009-04-06 11:45:12 +02001294 local_irq_restore(flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001295}
1296
Ingo Molnar04289bb2008-12-11 08:38:42 +01001297static u64 perf_counter_read(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001298{
1299 /*
1300 * If counter is enabled and currently active on a CPU, update the
1301 * value in the counter structure:
1302 */
Ingo Molnar6a930702008-12-11 15:17:03 +01001303 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
Thomas Gleixner0793a612008-12-04 20:12:29 +01001304 smp_call_function_single(counter->oncpu,
Ingo Molnar76715812008-12-17 14:20:28 +01001305 __read, counter, 1);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001306 } else if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
1307 update_counter_times(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001308 }
1309
Ingo Molnaree060942008-12-13 09:00:03 +01001310 return atomic64_read(&counter->count);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001311}
1312
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001313/*
1314 * Initialize the perf_counter context in a task_struct:
1315 */
1316static void
1317__perf_counter_init_context(struct perf_counter_context *ctx,
1318 struct task_struct *task)
1319{
1320 memset(ctx, 0, sizeof(*ctx));
1321 spin_lock_init(&ctx->lock);
1322 mutex_init(&ctx->mutex);
1323 INIT_LIST_HEAD(&ctx->counter_list);
1324 INIT_LIST_HEAD(&ctx->event_list);
1325 atomic_set(&ctx->refcount, 1);
1326 ctx->task = task;
1327}
1328
Thomas Gleixner0793a612008-12-04 20:12:29 +01001329static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
1330{
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001331 struct perf_counter_context *parent_ctx;
Ingo Molnar22a4f652009-06-01 10:13:37 +02001332 struct perf_counter_context *ctx;
1333 struct perf_cpu_context *cpuctx;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001334 struct task_struct *task;
Paul Mackerras25346b932009-06-01 17:48:12 +10001335 unsigned long flags;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001336 int err;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001337
1338 /*
1339 * If cpu is not a wildcard then this is a percpu counter:
1340 */
1341 if (cpu != -1) {
1342 /* Must be root to operate on a CPU counter: */
Peter Zijlstra1ccd1542009-04-09 10:53:45 +02001343 if (sysctl_perf_counter_priv && !capable(CAP_SYS_ADMIN))
Thomas Gleixner0793a612008-12-04 20:12:29 +01001344 return ERR_PTR(-EACCES);
1345
1346 if (cpu < 0 || cpu > num_possible_cpus())
1347 return ERR_PTR(-EINVAL);
1348
1349 /*
1350 * We could be clever and allow to attach a counter to an
1351 * offline CPU and activate it when the CPU comes up, but
1352 * that's for later.
1353 */
1354 if (!cpu_isset(cpu, cpu_online_map))
1355 return ERR_PTR(-ENODEV);
1356
1357 cpuctx = &per_cpu(perf_cpu_context, cpu);
1358 ctx = &cpuctx->ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001359 get_ctx(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001360
Thomas Gleixner0793a612008-12-04 20:12:29 +01001361 return ctx;
1362 }
1363
1364 rcu_read_lock();
1365 if (!pid)
1366 task = current;
1367 else
1368 task = find_task_by_vpid(pid);
1369 if (task)
1370 get_task_struct(task);
1371 rcu_read_unlock();
1372
1373 if (!task)
1374 return ERR_PTR(-ESRCH);
1375
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001376 /*
1377 * Can't attach counters to a dying task.
1378 */
1379 err = -ESRCH;
1380 if (task->flags & PF_EXITING)
1381 goto errout;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001382
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001383 /* Reuse ptrace permission checks for now. */
1384 err = -EACCES;
1385 if (!ptrace_may_access(task, PTRACE_MODE_READ))
1386 goto errout;
1387
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001388 retry:
Paul Mackerras25346b932009-06-01 17:48:12 +10001389 ctx = perf_lock_task_context(task, &flags);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001390 if (ctx) {
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001391 parent_ctx = ctx->parent_ctx;
1392 if (parent_ctx) {
1393 put_ctx(parent_ctx);
1394 ctx->parent_ctx = NULL; /* no longer a clone */
1395 }
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001396 /*
1397 * Get an extra reference before dropping the lock so that
1398 * this context won't get freed if the task exits.
1399 */
1400 get_ctx(ctx);
Paul Mackerras25346b932009-06-01 17:48:12 +10001401 spin_unlock_irqrestore(&ctx->lock, flags);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001402 }
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001403
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001404 if (!ctx) {
1405 ctx = kmalloc(sizeof(struct perf_counter_context), GFP_KERNEL);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001406 err = -ENOMEM;
1407 if (!ctx)
1408 goto errout;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001409 __perf_counter_init_context(ctx, task);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001410 get_ctx(ctx);
1411 if (cmpxchg(&task->perf_counter_ctxp, NULL, ctx)) {
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001412 /*
1413 * We raced with some other task; use
1414 * the context they set.
1415 */
1416 kfree(ctx);
Paul Mackerras25346b932009-06-01 17:48:12 +10001417 goto retry;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001418 }
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001419 get_task_struct(task);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001420 }
1421
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001422 put_task_struct(task);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001423 return ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001424
1425 errout:
1426 put_task_struct(task);
1427 return ERR_PTR(err);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001428}
1429
Peter Zijlstra592903c2009-03-13 12:21:36 +01001430static void free_counter_rcu(struct rcu_head *head)
1431{
1432 struct perf_counter *counter;
1433
1434 counter = container_of(head, struct perf_counter, rcu_head);
Peter Zijlstra709e50c2009-06-02 14:13:15 +02001435 if (counter->ns)
1436 put_pid_ns(counter->ns);
Peter Zijlstra592903c2009-03-13 12:21:36 +01001437 kfree(counter);
1438}
1439
Peter Zijlstra925d5192009-03-30 19:07:02 +02001440static void perf_pending_sync(struct perf_counter *counter);
1441
Peter Zijlstraf1600952009-03-19 20:26:16 +01001442static void free_counter(struct perf_counter *counter)
1443{
Peter Zijlstra925d5192009-03-30 19:07:02 +02001444 perf_pending_sync(counter);
1445
Peter Zijlstra7fc23a52009-05-08 18:52:21 +02001446 atomic_dec(&nr_counters);
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02001447 if (counter->hw_event.mmap)
1448 atomic_dec(&nr_mmap_tracking);
1449 if (counter->hw_event.munmap)
1450 atomic_dec(&nr_munmap_tracking);
1451 if (counter->hw_event.comm)
1452 atomic_dec(&nr_comm_tracking);
1453
Peter Zijlstrae077df42009-03-19 20:26:17 +01001454 if (counter->destroy)
1455 counter->destroy(counter);
1456
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001457 put_ctx(counter->ctx);
Peter Zijlstraf1600952009-03-19 20:26:16 +01001458 call_rcu(&counter->rcu_head, free_counter_rcu);
1459}
1460
Thomas Gleixner0793a612008-12-04 20:12:29 +01001461/*
1462 * Called when the last reference to the file is gone.
1463 */
1464static int perf_release(struct inode *inode, struct file *file)
1465{
1466 struct perf_counter *counter = file->private_data;
1467 struct perf_counter_context *ctx = counter->ctx;
1468
1469 file->private_data = NULL;
1470
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001471 WARN_ON_ONCE(ctx->parent_ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001472 mutex_lock(&ctx->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01001473 perf_counter_remove_from_context(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001474 mutex_unlock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001475
Peter Zijlstra082ff5a2009-05-23 18:29:00 +02001476 mutex_lock(&counter->owner->perf_counter_mutex);
1477 list_del_init(&counter->owner_entry);
1478 mutex_unlock(&counter->owner->perf_counter_mutex);
1479 put_task_struct(counter->owner);
1480
Peter Zijlstraf1600952009-03-19 20:26:16 +01001481 free_counter(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001482
1483 return 0;
1484}
1485
1486/*
1487 * Read the performance counter - simple non blocking version for now
1488 */
1489static ssize_t
1490perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
1491{
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001492 u64 values[3];
1493 int n;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001494
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001495 /*
1496 * Return end-of-file for a read on a counter that is in
1497 * error state (i.e. because it was pinned but it couldn't be
1498 * scheduled on to the CPU at some point).
1499 */
1500 if (counter->state == PERF_COUNTER_STATE_ERROR)
1501 return 0;
1502
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001503 WARN_ON_ONCE(counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001504 mutex_lock(&counter->child_mutex);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001505 values[0] = perf_counter_read(counter);
1506 n = 1;
1507 if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1508 values[n++] = counter->total_time_enabled +
1509 atomic64_read(&counter->child_total_time_enabled);
1510 if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1511 values[n++] = counter->total_time_running +
1512 atomic64_read(&counter->child_total_time_running);
Peter Zijlstra8e5799b2009-06-02 15:08:15 +02001513 if (counter->hw_event.read_format & PERF_FORMAT_ID)
1514 values[n++] = counter->id;
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001515 mutex_unlock(&counter->child_mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001516
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001517 if (count < n * sizeof(u64))
1518 return -EINVAL;
1519 count = n * sizeof(u64);
1520
1521 if (copy_to_user(buf, values, count))
1522 return -EFAULT;
1523
1524 return count;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001525}
1526
1527static ssize_t
Thomas Gleixner0793a612008-12-04 20:12:29 +01001528perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
1529{
1530 struct perf_counter *counter = file->private_data;
1531
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001532 return perf_read_hw(counter, buf, count);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001533}
1534
1535static unsigned int perf_poll(struct file *file, poll_table *wait)
1536{
1537 struct perf_counter *counter = file->private_data;
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001538 struct perf_mmap_data *data;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001539 unsigned int events = POLL_HUP;
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001540
1541 rcu_read_lock();
1542 data = rcu_dereference(counter->data);
1543 if (data)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001544 events = atomic_xchg(&data->poll, 0);
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001545 rcu_read_unlock();
Thomas Gleixner0793a612008-12-04 20:12:29 +01001546
1547 poll_wait(file, &counter->waitq, wait);
1548
Thomas Gleixner0793a612008-12-04 20:12:29 +01001549 return events;
1550}
1551
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001552static void perf_counter_reset(struct perf_counter *counter)
1553{
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001554 (void)perf_counter_read(counter);
Paul Mackerras615a3f12009-05-11 15:50:21 +10001555 atomic64_set(&counter->count, 0);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001556 perf_counter_update_userpage(counter);
1557}
1558
1559static void perf_counter_for_each_sibling(struct perf_counter *counter,
1560 void (*func)(struct perf_counter *))
1561{
1562 struct perf_counter_context *ctx = counter->ctx;
1563 struct perf_counter *sibling;
1564
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001565 WARN_ON_ONCE(ctx->parent_ctx);
Peter Zijlstra682076a2009-05-23 18:28:57 +02001566 mutex_lock(&ctx->mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001567 counter = counter->group_leader;
1568
1569 func(counter);
1570 list_for_each_entry(sibling, &counter->sibling_list, list_entry)
1571 func(sibling);
Peter Zijlstra682076a2009-05-23 18:28:57 +02001572 mutex_unlock(&ctx->mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001573}
1574
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001575/*
1576 * Holding the top-level counter's child_mutex means that any
1577 * descendant process that has inherited this counter will block
1578 * in sync_child_counter if it goes to exit, thus satisfying the
1579 * task existence requirements of perf_counter_enable/disable.
1580 */
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001581static void perf_counter_for_each_child(struct perf_counter *counter,
1582 void (*func)(struct perf_counter *))
1583{
1584 struct perf_counter *child;
1585
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001586 WARN_ON_ONCE(counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001587 mutex_lock(&counter->child_mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001588 func(counter);
1589 list_for_each_entry(child, &counter->child_list, child_list)
1590 func(child);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001591 mutex_unlock(&counter->child_mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001592}
1593
1594static void perf_counter_for_each(struct perf_counter *counter,
1595 void (*func)(struct perf_counter *))
1596{
1597 struct perf_counter *child;
1598
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001599 WARN_ON_ONCE(counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001600 mutex_lock(&counter->child_mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001601 perf_counter_for_each_sibling(counter, func);
1602 list_for_each_entry(child, &counter->child_list, child_list)
1603 perf_counter_for_each_sibling(child, func);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001604 mutex_unlock(&counter->child_mutex);
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001605}
1606
Peter Zijlstra08247e32009-06-02 16:46:57 +02001607static int perf_counter_period(struct perf_counter *counter, u64 __user *arg)
1608{
1609 struct perf_counter_context *ctx = counter->ctx;
1610 unsigned long size;
1611 int ret = 0;
1612 u64 value;
1613
1614 if (!counter->hw_event.sample_period)
1615 return -EINVAL;
1616
1617 size = copy_from_user(&value, arg, sizeof(value));
1618 if (size != sizeof(value))
1619 return -EFAULT;
1620
1621 if (!value)
1622 return -EINVAL;
1623
1624 spin_lock_irq(&ctx->lock);
1625 if (counter->hw_event.freq) {
1626 if (value > sysctl_perf_counter_limit) {
1627 ret = -EINVAL;
1628 goto unlock;
1629 }
1630
1631 counter->hw_event.sample_freq = value;
1632 } else {
1633 counter->hw_event.sample_period = value;
1634 counter->hw.sample_period = value;
1635
1636 perf_log_period(counter, value);
1637 }
1638unlock:
1639 spin_unlock_irq(&ctx->lock);
1640
1641 return ret;
1642}
1643
Paul Mackerrasd859e292009-01-17 18:10:22 +11001644static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1645{
1646 struct perf_counter *counter = file->private_data;
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001647 void (*func)(struct perf_counter *);
1648 u32 flags = arg;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001649
1650 switch (cmd) {
1651 case PERF_COUNTER_IOC_ENABLE:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001652 func = perf_counter_enable;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001653 break;
1654 case PERF_COUNTER_IOC_DISABLE:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001655 func = perf_counter_disable;
Peter Zijlstra79f14642009-04-06 11:45:07 +02001656 break;
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001657 case PERF_COUNTER_IOC_RESET:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001658 func = perf_counter_reset;
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001659 break;
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001660
1661 case PERF_COUNTER_IOC_REFRESH:
1662 return perf_counter_refresh(counter, arg);
Peter Zijlstra08247e32009-06-02 16:46:57 +02001663
1664 case PERF_COUNTER_IOC_PERIOD:
1665 return perf_counter_period(counter, (u64 __user *)arg);
1666
Paul Mackerrasd859e292009-01-17 18:10:22 +11001667 default:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001668 return -ENOTTY;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001669 }
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001670
1671 if (flags & PERF_IOC_FLAG_GROUP)
1672 perf_counter_for_each(counter, func);
1673 else
1674 perf_counter_for_each_child(counter, func);
1675
1676 return 0;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001677}
1678
Peter Zijlstra771d7cd2009-05-25 14:45:26 +02001679int perf_counter_task_enable(void)
1680{
1681 struct perf_counter *counter;
1682
1683 mutex_lock(&current->perf_counter_mutex);
1684 list_for_each_entry(counter, &current->perf_counter_list, owner_entry)
1685 perf_counter_for_each_child(counter, perf_counter_enable);
1686 mutex_unlock(&current->perf_counter_mutex);
1687
1688 return 0;
1689}
1690
1691int perf_counter_task_disable(void)
1692{
1693 struct perf_counter *counter;
1694
1695 mutex_lock(&current->perf_counter_mutex);
1696 list_for_each_entry(counter, &current->perf_counter_list, owner_entry)
1697 perf_counter_for_each_child(counter, perf_counter_disable);
1698 mutex_unlock(&current->perf_counter_mutex);
1699
1700 return 0;
1701}
1702
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001703/*
1704 * Callers need to ensure there can be no nesting of this function, otherwise
1705 * the seqlock logic goes bad. We can not serialize this because the arch
1706 * code calls this from NMI context.
1707 */
1708void perf_counter_update_userpage(struct perf_counter *counter)
Paul Mackerras37d81822009-03-23 18:22:08 +01001709{
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001710 struct perf_counter_mmap_page *userpg;
Ingo Molnar22a4f652009-06-01 10:13:37 +02001711 struct perf_mmap_data *data;
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001712
1713 rcu_read_lock();
1714 data = rcu_dereference(counter->data);
1715 if (!data)
1716 goto unlock;
1717
1718 userpg = data->user_page;
Paul Mackerras37d81822009-03-23 18:22:08 +01001719
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001720 /*
1721 * Disable preemption so as to not let the corresponding user-space
1722 * spin too long if we get preempted.
1723 */
1724 preempt_disable();
Paul Mackerras37d81822009-03-23 18:22:08 +01001725 ++userpg->lock;
Peter Zijlstra92f22a32009-04-02 11:12:04 +02001726 barrier();
Paul Mackerras37d81822009-03-23 18:22:08 +01001727 userpg->index = counter->hw.idx;
1728 userpg->offset = atomic64_read(&counter->count);
1729 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
1730 userpg->offset -= atomic64_read(&counter->hw.prev_count);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001731
Peter Zijlstra92f22a32009-04-02 11:12:04 +02001732 barrier();
Paul Mackerras37d81822009-03-23 18:22:08 +01001733 ++userpg->lock;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001734 preempt_enable();
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001735unlock:
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001736 rcu_read_unlock();
Paul Mackerras37d81822009-03-23 18:22:08 +01001737}
1738
1739static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1740{
1741 struct perf_counter *counter = vma->vm_file->private_data;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001742 struct perf_mmap_data *data;
1743 int ret = VM_FAULT_SIGBUS;
Paul Mackerras37d81822009-03-23 18:22:08 +01001744
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001745 rcu_read_lock();
1746 data = rcu_dereference(counter->data);
1747 if (!data)
1748 goto unlock;
Paul Mackerras37d81822009-03-23 18:22:08 +01001749
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001750 if (vmf->pgoff == 0) {
1751 vmf->page = virt_to_page(data->user_page);
1752 } else {
1753 int nr = vmf->pgoff - 1;
1754
1755 if ((unsigned)nr > data->nr_pages)
1756 goto unlock;
1757
1758 vmf->page = virt_to_page(data->data_pages[nr]);
1759 }
Paul Mackerras37d81822009-03-23 18:22:08 +01001760 get_page(vmf->page);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001761 ret = 0;
1762unlock:
1763 rcu_read_unlock();
1764
1765 return ret;
1766}
1767
1768static int perf_mmap_data_alloc(struct perf_counter *counter, int nr_pages)
1769{
1770 struct perf_mmap_data *data;
1771 unsigned long size;
1772 int i;
1773
1774 WARN_ON(atomic_read(&counter->mmap_count));
1775
1776 size = sizeof(struct perf_mmap_data);
1777 size += nr_pages * sizeof(void *);
1778
1779 data = kzalloc(size, GFP_KERNEL);
1780 if (!data)
1781 goto fail;
1782
1783 data->user_page = (void *)get_zeroed_page(GFP_KERNEL);
1784 if (!data->user_page)
1785 goto fail_user_page;
1786
1787 for (i = 0; i < nr_pages; i++) {
1788 data->data_pages[i] = (void *)get_zeroed_page(GFP_KERNEL);
1789 if (!data->data_pages[i])
1790 goto fail_data_pages;
1791 }
1792
1793 data->nr_pages = nr_pages;
Peter Zijlstra22c15582009-05-05 17:50:25 +02001794 atomic_set(&data->lock, -1);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001795
1796 rcu_assign_pointer(counter->data, data);
1797
Paul Mackerras37d81822009-03-23 18:22:08 +01001798 return 0;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001799
1800fail_data_pages:
1801 for (i--; i >= 0; i--)
1802 free_page((unsigned long)data->data_pages[i]);
1803
1804 free_page((unsigned long)data->user_page);
1805
1806fail_user_page:
1807 kfree(data);
1808
1809fail:
1810 return -ENOMEM;
1811}
1812
1813static void __perf_mmap_data_free(struct rcu_head *rcu_head)
1814{
Ingo Molnar22a4f652009-06-01 10:13:37 +02001815 struct perf_mmap_data *data;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001816 int i;
1817
Ingo Molnar22a4f652009-06-01 10:13:37 +02001818 data = container_of(rcu_head, struct perf_mmap_data, rcu_head);
1819
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001820 free_page((unsigned long)data->user_page);
1821 for (i = 0; i < data->nr_pages; i++)
1822 free_page((unsigned long)data->data_pages[i]);
1823 kfree(data);
1824}
1825
1826static void perf_mmap_data_free(struct perf_counter *counter)
1827{
1828 struct perf_mmap_data *data = counter->data;
1829
1830 WARN_ON(atomic_read(&counter->mmap_count));
1831
1832 rcu_assign_pointer(counter->data, NULL);
1833 call_rcu(&data->rcu_head, __perf_mmap_data_free);
1834}
1835
1836static void perf_mmap_open(struct vm_area_struct *vma)
1837{
1838 struct perf_counter *counter = vma->vm_file->private_data;
1839
1840 atomic_inc(&counter->mmap_count);
1841}
1842
1843static void perf_mmap_close(struct vm_area_struct *vma)
1844{
1845 struct perf_counter *counter = vma->vm_file->private_data;
1846
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001847 WARN_ON_ONCE(counter->ctx->parent_ctx);
Ingo Molnar22a4f652009-06-01 10:13:37 +02001848 if (atomic_dec_and_mutex_lock(&counter->mmap_count, &counter->mmap_mutex)) {
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001849 struct user_struct *user = current_user();
1850
1851 atomic_long_sub(counter->data->nr_pages + 1, &user->locked_vm);
Peter Zijlstrac5078f72009-05-05 17:50:24 +02001852 vma->vm_mm->locked_vm -= counter->data->nr_locked;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001853 perf_mmap_data_free(counter);
1854 mutex_unlock(&counter->mmap_mutex);
1855 }
Paul Mackerras37d81822009-03-23 18:22:08 +01001856}
1857
1858static struct vm_operations_struct perf_mmap_vmops = {
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001859 .open = perf_mmap_open,
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001860 .close = perf_mmap_close,
Paul Mackerras37d81822009-03-23 18:22:08 +01001861 .fault = perf_mmap_fault,
1862};
1863
1864static int perf_mmap(struct file *file, struct vm_area_struct *vma)
1865{
1866 struct perf_counter *counter = file->private_data;
Ingo Molnar22a4f652009-06-01 10:13:37 +02001867 unsigned long user_locked, user_lock_limit;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001868 struct user_struct *user = current_user();
Ingo Molnar22a4f652009-06-01 10:13:37 +02001869 unsigned long locked, lock_limit;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001870 unsigned long vma_size;
1871 unsigned long nr_pages;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001872 long user_extra, extra;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001873 int ret = 0;
Paul Mackerras37d81822009-03-23 18:22:08 +01001874
1875 if (!(vma->vm_flags & VM_SHARED) || (vma->vm_flags & VM_WRITE))
1876 return -EINVAL;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001877
1878 vma_size = vma->vm_end - vma->vm_start;
1879 nr_pages = (vma_size / PAGE_SIZE) - 1;
1880
Peter Zijlstra7730d862009-03-25 12:48:31 +01001881 /*
1882 * If we have data pages ensure they're a power-of-two number, so we
1883 * can do bitmasks instead of modulo.
1884 */
1885 if (nr_pages != 0 && !is_power_of_2(nr_pages))
Paul Mackerras37d81822009-03-23 18:22:08 +01001886 return -EINVAL;
1887
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001888 if (vma_size != PAGE_SIZE * (1 + nr_pages))
Paul Mackerras37d81822009-03-23 18:22:08 +01001889 return -EINVAL;
1890
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001891 if (vma->vm_pgoff != 0)
1892 return -EINVAL;
Paul Mackerras37d81822009-03-23 18:22:08 +01001893
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001894 WARN_ON_ONCE(counter->ctx->parent_ctx);
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001895 mutex_lock(&counter->mmap_mutex);
1896 if (atomic_inc_not_zero(&counter->mmap_count)) {
1897 if (nr_pages != counter->data->nr_pages)
1898 ret = -EINVAL;
1899 goto unlock;
1900 }
1901
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001902 user_extra = nr_pages + 1;
1903 user_lock_limit = sysctl_perf_counter_mlock >> (PAGE_SHIFT - 10);
Ingo Molnara3862d32009-05-24 09:02:37 +02001904
1905 /*
1906 * Increase the limit linearly with more CPUs:
1907 */
1908 user_lock_limit *= num_online_cpus();
1909
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001910 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
Peter Zijlstrac5078f72009-05-05 17:50:24 +02001911
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001912 extra = 0;
1913 if (user_locked > user_lock_limit)
1914 extra = user_locked - user_lock_limit;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001915
1916 lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
1917 lock_limit >>= PAGE_SHIFT;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001918 locked = vma->vm_mm->locked_vm + extra;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001919
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001920 if ((locked > lock_limit) && !capable(CAP_IPC_LOCK)) {
1921 ret = -EPERM;
1922 goto unlock;
1923 }
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001924
1925 WARN_ON(counter->data);
1926 ret = perf_mmap_data_alloc(counter, nr_pages);
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001927 if (ret)
1928 goto unlock;
1929
1930 atomic_set(&counter->mmap_count, 1);
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001931 atomic_long_add(user_extra, &user->locked_vm);
Peter Zijlstrac5078f72009-05-05 17:50:24 +02001932 vma->vm_mm->locked_vm += extra;
1933 counter->data->nr_locked = extra;
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001934unlock:
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001935 mutex_unlock(&counter->mmap_mutex);
Paul Mackerras37d81822009-03-23 18:22:08 +01001936
1937 vma->vm_flags &= ~VM_MAYWRITE;
1938 vma->vm_flags |= VM_RESERVED;
1939 vma->vm_ops = &perf_mmap_vmops;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001940
1941 return ret;
Paul Mackerras37d81822009-03-23 18:22:08 +01001942}
1943
Peter Zijlstra3c446b3d2009-04-06 11:45:01 +02001944static int perf_fasync(int fd, struct file *filp, int on)
1945{
Peter Zijlstra3c446b3d2009-04-06 11:45:01 +02001946 struct inode *inode = filp->f_path.dentry->d_inode;
Ingo Molnar22a4f652009-06-01 10:13:37 +02001947 struct perf_counter *counter = filp->private_data;
Peter Zijlstra3c446b3d2009-04-06 11:45:01 +02001948 int retval;
1949
1950 mutex_lock(&inode->i_mutex);
1951 retval = fasync_helper(fd, filp, on, &counter->fasync);
1952 mutex_unlock(&inode->i_mutex);
1953
1954 if (retval < 0)
1955 return retval;
1956
1957 return 0;
1958}
1959
Thomas Gleixner0793a612008-12-04 20:12:29 +01001960static const struct file_operations perf_fops = {
1961 .release = perf_release,
1962 .read = perf_read,
1963 .poll = perf_poll,
Paul Mackerrasd859e292009-01-17 18:10:22 +11001964 .unlocked_ioctl = perf_ioctl,
1965 .compat_ioctl = perf_ioctl,
Paul Mackerras37d81822009-03-23 18:22:08 +01001966 .mmap = perf_mmap,
Peter Zijlstra3c446b3d2009-04-06 11:45:01 +02001967 .fasync = perf_fasync,
Thomas Gleixner0793a612008-12-04 20:12:29 +01001968};
1969
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001970/*
Peter Zijlstra925d5192009-03-30 19:07:02 +02001971 * Perf counter wakeup
1972 *
1973 * If there's data, ensure we set the poll() state and publish everything
1974 * to user-space before waking everybody up.
1975 */
1976
1977void perf_counter_wakeup(struct perf_counter *counter)
1978{
Peter Zijlstra925d5192009-03-30 19:07:02 +02001979 wake_up_all(&counter->waitq);
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02001980
1981 if (counter->pending_kill) {
1982 kill_fasync(&counter->fasync, SIGIO, counter->pending_kill);
1983 counter->pending_kill = 0;
1984 }
Peter Zijlstra925d5192009-03-30 19:07:02 +02001985}
1986
1987/*
1988 * Pending wakeups
1989 *
1990 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
1991 *
1992 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
1993 * single linked list and use cmpxchg() to add entries lockless.
1994 */
1995
Peter Zijlstra79f14642009-04-06 11:45:07 +02001996static void perf_pending_counter(struct perf_pending_entry *entry)
1997{
1998 struct perf_counter *counter = container_of(entry,
1999 struct perf_counter, pending);
2000
2001 if (counter->pending_disable) {
2002 counter->pending_disable = 0;
2003 perf_counter_disable(counter);
2004 }
2005
2006 if (counter->pending_wakeup) {
2007 counter->pending_wakeup = 0;
2008 perf_counter_wakeup(counter);
2009 }
2010}
2011
Peter Zijlstra671dec52009-04-06 11:45:02 +02002012#define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
Peter Zijlstra925d5192009-03-30 19:07:02 +02002013
Peter Zijlstra671dec52009-04-06 11:45:02 +02002014static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = {
Peter Zijlstra925d5192009-03-30 19:07:02 +02002015 PENDING_TAIL,
2016};
2017
Peter Zijlstra671dec52009-04-06 11:45:02 +02002018static void perf_pending_queue(struct perf_pending_entry *entry,
2019 void (*func)(struct perf_pending_entry *))
Peter Zijlstra925d5192009-03-30 19:07:02 +02002020{
Peter Zijlstra671dec52009-04-06 11:45:02 +02002021 struct perf_pending_entry **head;
Peter Zijlstra925d5192009-03-30 19:07:02 +02002022
Peter Zijlstra671dec52009-04-06 11:45:02 +02002023 if (cmpxchg(&entry->next, NULL, PENDING_TAIL) != NULL)
Peter Zijlstra925d5192009-03-30 19:07:02 +02002024 return;
2025
Peter Zijlstra671dec52009-04-06 11:45:02 +02002026 entry->func = func;
2027
2028 head = &get_cpu_var(perf_pending_head);
Peter Zijlstra925d5192009-03-30 19:07:02 +02002029
2030 do {
Peter Zijlstra671dec52009-04-06 11:45:02 +02002031 entry->next = *head;
2032 } while (cmpxchg(head, entry->next, entry) != entry->next);
Peter Zijlstra925d5192009-03-30 19:07:02 +02002033
2034 set_perf_counter_pending();
2035
Peter Zijlstra671dec52009-04-06 11:45:02 +02002036 put_cpu_var(perf_pending_head);
Peter Zijlstra925d5192009-03-30 19:07:02 +02002037}
2038
2039static int __perf_pending_run(void)
2040{
Peter Zijlstra671dec52009-04-06 11:45:02 +02002041 struct perf_pending_entry *list;
Peter Zijlstra925d5192009-03-30 19:07:02 +02002042 int nr = 0;
2043
Peter Zijlstra671dec52009-04-06 11:45:02 +02002044 list = xchg(&__get_cpu_var(perf_pending_head), PENDING_TAIL);
Peter Zijlstra925d5192009-03-30 19:07:02 +02002045 while (list != PENDING_TAIL) {
Peter Zijlstra671dec52009-04-06 11:45:02 +02002046 void (*func)(struct perf_pending_entry *);
2047 struct perf_pending_entry *entry = list;
Peter Zijlstra925d5192009-03-30 19:07:02 +02002048
2049 list = list->next;
2050
Peter Zijlstra671dec52009-04-06 11:45:02 +02002051 func = entry->func;
2052 entry->next = NULL;
Peter Zijlstra925d5192009-03-30 19:07:02 +02002053 /*
2054 * Ensure we observe the unqueue before we issue the wakeup,
2055 * so that we won't be waiting forever.
2056 * -- see perf_not_pending().
2057 */
2058 smp_wmb();
2059
Peter Zijlstra671dec52009-04-06 11:45:02 +02002060 func(entry);
Peter Zijlstra925d5192009-03-30 19:07:02 +02002061 nr++;
2062 }
2063
2064 return nr;
2065}
2066
2067static inline int perf_not_pending(struct perf_counter *counter)
2068{
2069 /*
2070 * If we flush on whatever cpu we run, there is a chance we don't
2071 * need to wait.
2072 */
2073 get_cpu();
2074 __perf_pending_run();
2075 put_cpu();
2076
2077 /*
2078 * Ensure we see the proper queue state before going to sleep
2079 * so that we do not miss the wakeup. -- see perf_pending_handle()
2080 */
2081 smp_rmb();
Peter Zijlstra671dec52009-04-06 11:45:02 +02002082 return counter->pending.next == NULL;
Peter Zijlstra925d5192009-03-30 19:07:02 +02002083}
2084
2085static void perf_pending_sync(struct perf_counter *counter)
2086{
2087 wait_event(counter->waitq, perf_not_pending(counter));
2088}
2089
2090void perf_counter_do_pending(void)
2091{
2092 __perf_pending_run();
2093}
2094
2095/*
Peter Zijlstra394ee072009-03-30 19:07:14 +02002096 * Callchain support -- arch specific
2097 */
2098
Peter Zijlstra9c03d882009-04-06 11:45:00 +02002099__weak struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
Peter Zijlstra394ee072009-03-30 19:07:14 +02002100{
2101 return NULL;
2102}
2103
2104/*
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002105 * Output
2106 */
2107
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002108struct perf_output_handle {
2109 struct perf_counter *counter;
2110 struct perf_mmap_data *data;
Peter Zijlstra8e3747c2009-06-02 16:16:02 +02002111 unsigned long head;
2112 unsigned long offset;
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002113 int nmi;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002114 int overflow;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002115 int locked;
2116 unsigned long flags;
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002117};
2118
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002119static void perf_output_wakeup(struct perf_output_handle *handle)
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002120{
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002121 atomic_set(&handle->data->poll, POLL_IN);
2122
Peter Zijlstra671dec52009-04-06 11:45:02 +02002123 if (handle->nmi) {
Peter Zijlstra79f14642009-04-06 11:45:07 +02002124 handle->counter->pending_wakeup = 1;
Peter Zijlstra671dec52009-04-06 11:45:02 +02002125 perf_pending_queue(&handle->counter->pending,
Peter Zijlstra79f14642009-04-06 11:45:07 +02002126 perf_pending_counter);
Peter Zijlstra671dec52009-04-06 11:45:02 +02002127 } else
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002128 perf_counter_wakeup(handle->counter);
2129}
2130
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002131/*
2132 * Curious locking construct.
2133 *
2134 * We need to ensure a later event doesn't publish a head when a former
2135 * event isn't done writing. However since we need to deal with NMIs we
2136 * cannot fully serialize things.
2137 *
2138 * What we do is serialize between CPUs so we only have to deal with NMI
2139 * nesting on a single CPU.
2140 *
2141 * We only publish the head (and generate a wakeup) when the outer-most
2142 * event completes.
2143 */
2144static void perf_output_lock(struct perf_output_handle *handle)
2145{
2146 struct perf_mmap_data *data = handle->data;
2147 int cpu;
2148
2149 handle->locked = 0;
2150
2151 local_irq_save(handle->flags);
2152 cpu = smp_processor_id();
2153
2154 if (in_nmi() && atomic_read(&data->lock) == cpu)
2155 return;
2156
Peter Zijlstra22c15582009-05-05 17:50:25 +02002157 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002158 cpu_relax();
2159
2160 handle->locked = 1;
2161}
2162
2163static void perf_output_unlock(struct perf_output_handle *handle)
2164{
2165 struct perf_mmap_data *data = handle->data;
Peter Zijlstra8e3747c2009-06-02 16:16:02 +02002166 unsigned long head;
2167 int cpu;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002168
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002169 data->done_head = data->head;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002170
2171 if (!handle->locked)
2172 goto out;
2173
2174again:
2175 /*
2176 * The xchg implies a full barrier that ensures all writes are done
2177 * before we publish the new head, matched by a rmb() in userspace when
2178 * reading this position.
2179 */
Peter Zijlstra8e3747c2009-06-02 16:16:02 +02002180 while ((head = atomic_long_xchg(&data->done_head, 0)))
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002181 data->user_page->data_head = head;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002182
2183 /*
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002184 * NMI can happen here, which means we can miss a done_head update.
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002185 */
2186
Peter Zijlstra22c15582009-05-05 17:50:25 +02002187 cpu = atomic_xchg(&data->lock, -1);
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002188 WARN_ON_ONCE(cpu != smp_processor_id());
2189
2190 /*
2191 * Therefore we have to validate we did not indeed do so.
2192 */
Peter Zijlstra8e3747c2009-06-02 16:16:02 +02002193 if (unlikely(atomic_long_read(&data->done_head))) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002194 /*
2195 * Since we had it locked, we can lock it again.
2196 */
Peter Zijlstra22c15582009-05-05 17:50:25 +02002197 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002198 cpu_relax();
2199
2200 goto again;
2201 }
2202
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002203 if (atomic_xchg(&data->wakeup, 0))
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002204 perf_output_wakeup(handle);
2205out:
2206 local_irq_restore(handle->flags);
2207}
2208
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002209static int perf_output_begin(struct perf_output_handle *handle,
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002210 struct perf_counter *counter, unsigned int size,
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002211 int nmi, int overflow)
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002212{
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002213 struct perf_mmap_data *data;
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002214 unsigned int offset, head;
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002215
Peter Zijlstra2023b352009-05-05 17:50:26 +02002216 /*
2217 * For inherited counters we send all the output towards the parent.
2218 */
2219 if (counter->parent)
2220 counter = counter->parent;
2221
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002222 rcu_read_lock();
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002223 data = rcu_dereference(counter->data);
2224 if (!data)
2225 goto out;
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002226
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002227 handle->data = data;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002228 handle->counter = counter;
2229 handle->nmi = nmi;
2230 handle->overflow = overflow;
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002231
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002232 if (!data->nr_pages)
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002233 goto fail;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002234
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002235 perf_output_lock(handle);
2236
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002237 do {
2238 offset = head = atomic_read(&data->head);
Peter Zijlstrac7138f32009-03-24 13:18:16 +01002239 head += size;
Peter Zijlstra8e3747c2009-06-02 16:16:02 +02002240 } while (atomic_long_cmpxchg(&data->head, offset, head) != offset);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002241
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002242 handle->offset = offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01002243 handle->head = head;
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002244
2245 if ((offset >> PAGE_SHIFT) != (head >> PAGE_SHIFT))
2246 atomic_set(&data->wakeup, 1);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002247
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002248 return 0;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002249
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002250fail:
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002251 perf_output_wakeup(handle);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002252out:
2253 rcu_read_unlock();
2254
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002255 return -ENOSPC;
2256}
2257
2258static void perf_output_copy(struct perf_output_handle *handle,
2259 void *buf, unsigned int len)
2260{
2261 unsigned int pages_mask;
2262 unsigned int offset;
2263 unsigned int size;
2264 void **pages;
2265
2266 offset = handle->offset;
2267 pages_mask = handle->data->nr_pages - 1;
2268 pages = handle->data->data_pages;
2269
2270 do {
2271 unsigned int page_offset;
2272 int nr;
2273
2274 nr = (offset >> PAGE_SHIFT) & pages_mask;
2275 page_offset = offset & (PAGE_SIZE - 1);
2276 size = min_t(unsigned int, PAGE_SIZE - page_offset, len);
2277
2278 memcpy(pages[nr] + page_offset, buf, size);
2279
2280 len -= size;
2281 buf += size;
2282 offset += size;
2283 } while (len);
2284
2285 handle->offset = offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01002286
Peter Zijlstra53020fe2009-05-13 21:26:19 +02002287 /*
2288 * Check we didn't copy past our reservation window, taking the
2289 * possible unsigned int wrap into account.
2290 */
Peter Zijlstra8e3747c2009-06-02 16:16:02 +02002291 WARN_ON_ONCE(((long)(handle->head - handle->offset)) < 0);
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002292}
2293
Peter Zijlstra5c148192009-03-25 12:30:23 +01002294#define perf_output_put(handle, x) \
2295 perf_output_copy((handle), &(x), sizeof(x))
2296
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002297static void perf_output_end(struct perf_output_handle *handle)
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002298{
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002299 struct perf_counter *counter = handle->counter;
2300 struct perf_mmap_data *data = handle->data;
2301
2302 int wakeup_events = counter->hw_event.wakeup_events;
Peter Zijlstrac4578102009-04-02 11:12:01 +02002303
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002304 if (handle->overflow && wakeup_events) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002305 int events = atomic_inc_return(&data->events);
Peter Zijlstrac4578102009-04-02 11:12:01 +02002306 if (events >= wakeup_events) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002307 atomic_sub(wakeup_events, &data->events);
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002308 atomic_set(&data->wakeup, 1);
Peter Zijlstrac4578102009-04-02 11:12:01 +02002309 }
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002310 }
2311
2312 perf_output_unlock(handle);
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002313 rcu_read_unlock();
2314}
2315
Peter Zijlstra709e50c2009-06-02 14:13:15 +02002316static u32 perf_counter_pid(struct perf_counter *counter, struct task_struct *p)
2317{
2318 /*
2319 * only top level counters have the pid namespace they were created in
2320 */
2321 if (counter->parent)
2322 counter = counter->parent;
2323
2324 return task_tgid_nr_ns(p, counter->ns);
2325}
2326
2327static u32 perf_counter_tid(struct perf_counter *counter, struct task_struct *p)
2328{
2329 /*
2330 * only top level counters have the pid namespace they were created in
2331 */
2332 if (counter->parent)
2333 counter = counter->parent;
2334
2335 return task_pid_nr_ns(p, counter->ns);
2336}
2337
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002338static void perf_counter_output(struct perf_counter *counter,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002339 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002340{
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002341 int ret;
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002342 u64 sample_type = counter->hw_event.sample_type;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002343 struct perf_output_handle handle;
2344 struct perf_event_header header;
2345 u64 ip;
Peter Zijlstra5c148192009-03-25 12:30:23 +01002346 struct {
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002347 u32 pid, tid;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002348 } tid_entry;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002349 struct {
Peter Zijlstra8e5799b2009-06-02 15:08:15 +02002350 u64 id;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002351 u64 counter;
2352 } group_entry;
Peter Zijlstra394ee072009-03-30 19:07:14 +02002353 struct perf_callchain_entry *callchain = NULL;
2354 int callchain_size = 0;
Peter Zijlstra339f7c92009-04-06 11:45:06 +02002355 u64 time;
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002356 struct {
2357 u32 cpu, reserved;
2358 } cpu_entry;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002359
Peter Zijlstra6b6e54862009-04-08 15:01:27 +02002360 header.type = 0;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002361 header.size = sizeof(header);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002362
Peter Zijlstra6b6e54862009-04-08 15:01:27 +02002363 header.misc = PERF_EVENT_MISC_OVERFLOW;
Paul Mackerras9d23a902009-05-14 21:48:08 +10002364 header.misc |= perf_misc_flags(regs);
Peter Zijlstra6fab0192009-04-08 15:01:26 +02002365
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002366 if (sample_type & PERF_SAMPLE_IP) {
Paul Mackerras9d23a902009-05-14 21:48:08 +10002367 ip = perf_instruction_pointer(regs);
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002368 header.type |= PERF_SAMPLE_IP;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002369 header.size += sizeof(ip);
2370 }
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002371
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002372 if (sample_type & PERF_SAMPLE_TID) {
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002373 /* namespace issues */
Peter Zijlstra709e50c2009-06-02 14:13:15 +02002374 tid_entry.pid = perf_counter_pid(counter, current);
2375 tid_entry.tid = perf_counter_tid(counter, current);
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002376
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002377 header.type |= PERF_SAMPLE_TID;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002378 header.size += sizeof(tid_entry);
2379 }
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002380
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002381 if (sample_type & PERF_SAMPLE_TIME) {
Peter Zijlstra4d855452009-04-08 15:01:32 +02002382 /*
2383 * Maybe do better on x86 and provide cpu_clock_nmi()
2384 */
2385 time = sched_clock();
2386
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002387 header.type |= PERF_SAMPLE_TIME;
Peter Zijlstra4d855452009-04-08 15:01:32 +02002388 header.size += sizeof(u64);
2389 }
2390
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002391 if (sample_type & PERF_SAMPLE_ADDR) {
2392 header.type |= PERF_SAMPLE_ADDR;
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002393 header.size += sizeof(u64);
2394 }
2395
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002396 if (sample_type & PERF_SAMPLE_CONFIG) {
2397 header.type |= PERF_SAMPLE_CONFIG;
Peter Zijlstraa85f61a2009-05-08 18:52:23 +02002398 header.size += sizeof(u64);
2399 }
2400
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002401 if (sample_type & PERF_SAMPLE_CPU) {
2402 header.type |= PERF_SAMPLE_CPU;
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002403 header.size += sizeof(cpu_entry);
2404
2405 cpu_entry.cpu = raw_smp_processor_id();
2406 }
2407
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002408 if (sample_type & PERF_SAMPLE_GROUP) {
2409 header.type |= PERF_SAMPLE_GROUP;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002410 header.size += sizeof(u64) +
2411 counter->nr_siblings * sizeof(group_entry);
2412 }
2413
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002414 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
Peter Zijlstra394ee072009-03-30 19:07:14 +02002415 callchain = perf_callchain(regs);
2416
2417 if (callchain) {
Peter Zijlstra9c03d882009-04-06 11:45:00 +02002418 callchain_size = (1 + callchain->nr) * sizeof(u64);
Peter Zijlstra394ee072009-03-30 19:07:14 +02002419
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002420 header.type |= PERF_SAMPLE_CALLCHAIN;
Peter Zijlstra394ee072009-03-30 19:07:14 +02002421 header.size += callchain_size;
2422 }
2423 }
2424
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002425 ret = perf_output_begin(&handle, counter, header.size, nmi, 1);
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002426 if (ret)
2427 return;
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002428
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002429 perf_output_put(&handle, header);
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002430
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002431 if (sample_type & PERF_SAMPLE_IP)
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002432 perf_output_put(&handle, ip);
2433
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002434 if (sample_type & PERF_SAMPLE_TID)
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002435 perf_output_put(&handle, tid_entry);
2436
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002437 if (sample_type & PERF_SAMPLE_TIME)
Peter Zijlstra4d855452009-04-08 15:01:32 +02002438 perf_output_put(&handle, time);
2439
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002440 if (sample_type & PERF_SAMPLE_ADDR)
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002441 perf_output_put(&handle, addr);
2442
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002443 if (sample_type & PERF_SAMPLE_CONFIG)
Peter Zijlstraa85f61a2009-05-08 18:52:23 +02002444 perf_output_put(&handle, counter->hw_event.config);
2445
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002446 if (sample_type & PERF_SAMPLE_CPU)
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002447 perf_output_put(&handle, cpu_entry);
2448
Peter Zijlstra2023b352009-05-05 17:50:26 +02002449 /*
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002450 * XXX PERF_SAMPLE_GROUP vs inherited counters seems difficult.
Peter Zijlstra2023b352009-05-05 17:50:26 +02002451 */
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002452 if (sample_type & PERF_SAMPLE_GROUP) {
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002453 struct perf_counter *leader, *sub;
2454 u64 nr = counter->nr_siblings;
2455
2456 perf_output_put(&handle, nr);
2457
2458 leader = counter->group_leader;
2459 list_for_each_entry(sub, &leader->sibling_list, list_entry) {
2460 if (sub != counter)
Robert Richter4aeb0b42009-04-29 12:47:03 +02002461 sub->pmu->read(sub);
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002462
Peter Zijlstra8e5799b2009-06-02 15:08:15 +02002463 group_entry.id = sub->id;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002464 group_entry.counter = atomic64_read(&sub->count);
2465
2466 perf_output_put(&handle, group_entry);
2467 }
2468 }
2469
Peter Zijlstra394ee072009-03-30 19:07:14 +02002470 if (callchain)
2471 perf_output_copy(&handle, callchain, callchain_size);
2472
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002473 perf_output_end(&handle);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002474}
2475
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002476/*
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002477 * comm tracking
2478 */
2479
2480struct perf_comm_event {
Ingo Molnar22a4f652009-06-01 10:13:37 +02002481 struct task_struct *task;
2482 char *comm;
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002483 int comm_size;
2484
2485 struct {
2486 struct perf_event_header header;
2487
2488 u32 pid;
2489 u32 tid;
2490 } event;
2491};
2492
2493static void perf_counter_comm_output(struct perf_counter *counter,
2494 struct perf_comm_event *comm_event)
2495{
2496 struct perf_output_handle handle;
2497 int size = comm_event->event.header.size;
2498 int ret = perf_output_begin(&handle, counter, size, 0, 0);
2499
2500 if (ret)
2501 return;
2502
Peter Zijlstra709e50c2009-06-02 14:13:15 +02002503 comm_event->event.pid = perf_counter_pid(counter, comm_event->task);
2504 comm_event->event.tid = perf_counter_tid(counter, comm_event->task);
2505
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002506 perf_output_put(&handle, comm_event->event);
2507 perf_output_copy(&handle, comm_event->comm,
2508 comm_event->comm_size);
2509 perf_output_end(&handle);
2510}
2511
2512static int perf_counter_comm_match(struct perf_counter *counter,
2513 struct perf_comm_event *comm_event)
2514{
2515 if (counter->hw_event.comm &&
2516 comm_event->event.header.type == PERF_EVENT_COMM)
2517 return 1;
2518
2519 return 0;
2520}
2521
2522static void perf_counter_comm_ctx(struct perf_counter_context *ctx,
2523 struct perf_comm_event *comm_event)
2524{
2525 struct perf_counter *counter;
2526
2527 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2528 return;
2529
2530 rcu_read_lock();
2531 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2532 if (perf_counter_comm_match(counter, comm_event))
2533 perf_counter_comm_output(counter, comm_event);
2534 }
2535 rcu_read_unlock();
2536}
2537
2538static void perf_counter_comm_event(struct perf_comm_event *comm_event)
2539{
2540 struct perf_cpu_context *cpuctx;
Peter Zijlstra665c2142009-05-29 14:51:57 +02002541 struct perf_counter_context *ctx;
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002542 unsigned int size;
2543 char *comm = comm_event->task->comm;
2544
Ingo Molnar888fcee2009-04-09 09:48:22 +02002545 size = ALIGN(strlen(comm)+1, sizeof(u64));
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002546
2547 comm_event->comm = comm;
2548 comm_event->comm_size = size;
2549
2550 comm_event->event.header.size = sizeof(comm_event->event) + size;
2551
2552 cpuctx = &get_cpu_var(perf_cpu_context);
2553 perf_counter_comm_ctx(&cpuctx->ctx, comm_event);
2554 put_cpu_var(perf_cpu_context);
Peter Zijlstra665c2142009-05-29 14:51:57 +02002555
2556 rcu_read_lock();
2557 /*
2558 * doesn't really matter which of the child contexts the
2559 * events ends up in.
2560 */
2561 ctx = rcu_dereference(current->perf_counter_ctxp);
2562 if (ctx)
2563 perf_counter_comm_ctx(ctx, comm_event);
2564 rcu_read_unlock();
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002565}
2566
2567void perf_counter_comm(struct task_struct *task)
2568{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002569 struct perf_comm_event comm_event;
2570
2571 if (!atomic_read(&nr_comm_tracking))
2572 return;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10002573
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002574 comm_event = (struct perf_comm_event){
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002575 .task = task,
2576 .event = {
2577 .header = { .type = PERF_EVENT_COMM, },
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002578 },
2579 };
2580
2581 perf_counter_comm_event(&comm_event);
2582}
2583
2584/*
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002585 * mmap tracking
2586 */
2587
2588struct perf_mmap_event {
2589 struct file *file;
2590 char *file_name;
2591 int file_size;
2592
2593 struct {
2594 struct perf_event_header header;
2595
2596 u32 pid;
2597 u32 tid;
2598 u64 start;
2599 u64 len;
2600 u64 pgoff;
2601 } event;
2602};
2603
2604static void perf_counter_mmap_output(struct perf_counter *counter,
2605 struct perf_mmap_event *mmap_event)
2606{
2607 struct perf_output_handle handle;
2608 int size = mmap_event->event.header.size;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002609 int ret = perf_output_begin(&handle, counter, size, 0, 0);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002610
2611 if (ret)
2612 return;
2613
Peter Zijlstra709e50c2009-06-02 14:13:15 +02002614 mmap_event->event.pid = perf_counter_pid(counter, current);
2615 mmap_event->event.tid = perf_counter_tid(counter, current);
2616
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002617 perf_output_put(&handle, mmap_event->event);
2618 perf_output_copy(&handle, mmap_event->file_name,
2619 mmap_event->file_size);
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002620 perf_output_end(&handle);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002621}
2622
2623static int perf_counter_mmap_match(struct perf_counter *counter,
2624 struct perf_mmap_event *mmap_event)
2625{
2626 if (counter->hw_event.mmap &&
2627 mmap_event->event.header.type == PERF_EVENT_MMAP)
2628 return 1;
2629
2630 if (counter->hw_event.munmap &&
2631 mmap_event->event.header.type == PERF_EVENT_MUNMAP)
2632 return 1;
2633
2634 return 0;
2635}
2636
2637static void perf_counter_mmap_ctx(struct perf_counter_context *ctx,
2638 struct perf_mmap_event *mmap_event)
2639{
2640 struct perf_counter *counter;
2641
2642 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2643 return;
2644
2645 rcu_read_lock();
2646 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2647 if (perf_counter_mmap_match(counter, mmap_event))
2648 perf_counter_mmap_output(counter, mmap_event);
2649 }
2650 rcu_read_unlock();
2651}
2652
2653static void perf_counter_mmap_event(struct perf_mmap_event *mmap_event)
2654{
2655 struct perf_cpu_context *cpuctx;
Peter Zijlstra665c2142009-05-29 14:51:57 +02002656 struct perf_counter_context *ctx;
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002657 struct file *file = mmap_event->file;
2658 unsigned int size;
2659 char tmp[16];
2660 char *buf = NULL;
2661 char *name;
2662
2663 if (file) {
2664 buf = kzalloc(PATH_MAX, GFP_KERNEL);
2665 if (!buf) {
2666 name = strncpy(tmp, "//enomem", sizeof(tmp));
2667 goto got_name;
2668 }
Peter Zijlstrad3d21c42009-04-09 10:53:46 +02002669 name = d_path(&file->f_path, buf, PATH_MAX);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002670 if (IS_ERR(name)) {
2671 name = strncpy(tmp, "//toolong", sizeof(tmp));
2672 goto got_name;
2673 }
2674 } else {
2675 name = strncpy(tmp, "//anon", sizeof(tmp));
2676 goto got_name;
2677 }
2678
2679got_name:
Ingo Molnar888fcee2009-04-09 09:48:22 +02002680 size = ALIGN(strlen(name)+1, sizeof(u64));
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002681
2682 mmap_event->file_name = name;
2683 mmap_event->file_size = size;
2684
2685 mmap_event->event.header.size = sizeof(mmap_event->event) + size;
2686
2687 cpuctx = &get_cpu_var(perf_cpu_context);
2688 perf_counter_mmap_ctx(&cpuctx->ctx, mmap_event);
2689 put_cpu_var(perf_cpu_context);
2690
Peter Zijlstra665c2142009-05-29 14:51:57 +02002691 rcu_read_lock();
2692 /*
2693 * doesn't really matter which of the child contexts the
2694 * events ends up in.
2695 */
2696 ctx = rcu_dereference(current->perf_counter_ctxp);
2697 if (ctx)
2698 perf_counter_mmap_ctx(ctx, mmap_event);
2699 rcu_read_unlock();
2700
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002701 kfree(buf);
2702}
2703
2704void perf_counter_mmap(unsigned long addr, unsigned long len,
2705 unsigned long pgoff, struct file *file)
2706{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002707 struct perf_mmap_event mmap_event;
2708
2709 if (!atomic_read(&nr_mmap_tracking))
2710 return;
2711
2712 mmap_event = (struct perf_mmap_event){
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002713 .file = file,
2714 .event = {
2715 .header = { .type = PERF_EVENT_MMAP, },
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002716 .start = addr,
2717 .len = len,
2718 .pgoff = pgoff,
2719 },
2720 };
2721
2722 perf_counter_mmap_event(&mmap_event);
2723}
2724
2725void perf_counter_munmap(unsigned long addr, unsigned long len,
2726 unsigned long pgoff, struct file *file)
2727{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002728 struct perf_mmap_event mmap_event;
2729
2730 if (!atomic_read(&nr_munmap_tracking))
2731 return;
2732
2733 mmap_event = (struct perf_mmap_event){
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002734 .file = file,
2735 .event = {
2736 .header = { .type = PERF_EVENT_MUNMAP, },
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002737 .start = addr,
2738 .len = len,
2739 .pgoff = pgoff,
2740 },
2741 };
2742
2743 perf_counter_mmap_event(&mmap_event);
2744}
2745
2746/*
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002747 * Log sample_period changes so that analyzing tools can re-normalize the
Peter Zijlstrae220d2d2009-05-23 18:28:55 +02002748 * event flow.
Peter Zijlstra26b119b2009-05-20 12:21:20 +02002749 */
2750
2751static void perf_log_period(struct perf_counter *counter, u64 period)
2752{
2753 struct perf_output_handle handle;
2754 int ret;
2755
2756 struct {
2757 struct perf_event_header header;
2758 u64 time;
2759 u64 period;
2760 } freq_event = {
2761 .header = {
2762 .type = PERF_EVENT_PERIOD,
2763 .misc = 0,
2764 .size = sizeof(freq_event),
2765 },
2766 .time = sched_clock(),
2767 .period = period,
2768 };
2769
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002770 if (counter->hw.sample_period == period)
Peter Zijlstra26b119b2009-05-20 12:21:20 +02002771 return;
2772
2773 ret = perf_output_begin(&handle, counter, sizeof(freq_event), 0, 0);
2774 if (ret)
2775 return;
2776
2777 perf_output_put(&handle, freq_event);
2778 perf_output_end(&handle);
2779}
2780
2781/*
Peter Zijlstraa78ac322009-05-25 17:39:05 +02002782 * IRQ throttle logging
2783 */
2784
2785static void perf_log_throttle(struct perf_counter *counter, int enable)
2786{
2787 struct perf_output_handle handle;
2788 int ret;
2789
2790 struct {
2791 struct perf_event_header header;
2792 u64 time;
2793 } throttle_event = {
2794 .header = {
2795 .type = PERF_EVENT_THROTTLE + 1,
2796 .misc = 0,
2797 .size = sizeof(throttle_event),
2798 },
2799 .time = sched_clock(),
2800 };
2801
Ingo Molnar0127c3e2009-05-25 22:03:26 +02002802 ret = perf_output_begin(&handle, counter, sizeof(throttle_event), 1, 0);
Peter Zijlstraa78ac322009-05-25 17:39:05 +02002803 if (ret)
2804 return;
2805
2806 perf_output_put(&handle, throttle_event);
2807 perf_output_end(&handle);
2808}
2809
2810/*
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002811 * Generic counter overflow handling.
2812 */
2813
2814int perf_counter_overflow(struct perf_counter *counter,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002815 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002816{
Peter Zijlstra79f14642009-04-06 11:45:07 +02002817 int events = atomic_read(&counter->event_limit);
Peter Zijlstraa78ac322009-05-25 17:39:05 +02002818 int throttle = counter->pmu->unthrottle != NULL;
Peter Zijlstra79f14642009-04-06 11:45:07 +02002819 int ret = 0;
2820
Peter Zijlstraa78ac322009-05-25 17:39:05 +02002821 if (!throttle) {
2822 counter->hw.interrupts++;
2823 } else if (counter->hw.interrupts != MAX_INTERRUPTS) {
2824 counter->hw.interrupts++;
2825 if (HZ*counter->hw.interrupts > (u64)sysctl_perf_counter_limit) {
2826 counter->hw.interrupts = MAX_INTERRUPTS;
2827 perf_log_throttle(counter, 0);
2828 ret = 1;
2829 }
2830 }
Peter Zijlstra60db5e02009-05-15 15:19:28 +02002831
Peter Zijlstra2023b352009-05-05 17:50:26 +02002832 /*
2833 * XXX event_limit might not quite work as expected on inherited
2834 * counters
2835 */
2836
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002837 counter->pending_kill = POLL_IN;
Peter Zijlstra79f14642009-04-06 11:45:07 +02002838 if (events && atomic_dec_and_test(&counter->event_limit)) {
2839 ret = 1;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002840 counter->pending_kill = POLL_HUP;
Peter Zijlstra79f14642009-04-06 11:45:07 +02002841 if (nmi) {
2842 counter->pending_disable = 1;
2843 perf_pending_queue(&counter->pending,
2844 perf_pending_counter);
2845 } else
2846 perf_counter_disable(counter);
2847 }
2848
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002849 perf_counter_output(counter, nmi, regs, addr);
Peter Zijlstra79f14642009-04-06 11:45:07 +02002850 return ret;
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002851}
2852
2853/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002854 * Generic software counter infrastructure
2855 */
2856
2857static void perf_swcounter_update(struct perf_counter *counter)
2858{
2859 struct hw_perf_counter *hwc = &counter->hw;
2860 u64 prev, now;
2861 s64 delta;
2862
2863again:
2864 prev = atomic64_read(&hwc->prev_count);
2865 now = atomic64_read(&hwc->count);
2866 if (atomic64_cmpxchg(&hwc->prev_count, prev, now) != prev)
2867 goto again;
2868
2869 delta = now - prev;
2870
2871 atomic64_add(delta, &counter->count);
2872 atomic64_sub(delta, &hwc->period_left);
2873}
2874
2875static void perf_swcounter_set_period(struct perf_counter *counter)
2876{
2877 struct hw_perf_counter *hwc = &counter->hw;
2878 s64 left = atomic64_read(&hwc->period_left);
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002879 s64 period = hwc->sample_period;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002880
2881 if (unlikely(left <= -period)) {
2882 left = period;
2883 atomic64_set(&hwc->period_left, left);
2884 }
2885
2886 if (unlikely(left <= 0)) {
2887 left += period;
2888 atomic64_add(period, &hwc->period_left);
2889 }
2890
2891 atomic64_set(&hwc->prev_count, -left);
2892 atomic64_set(&hwc->count, -left);
2893}
2894
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002895static enum hrtimer_restart perf_swcounter_hrtimer(struct hrtimer *hrtimer)
2896{
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002897 enum hrtimer_restart ret = HRTIMER_RESTART;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002898 struct perf_counter *counter;
2899 struct pt_regs *regs;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02002900 u64 period;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002901
2902 counter = container_of(hrtimer, struct perf_counter, hw.hrtimer);
Robert Richter4aeb0b42009-04-29 12:47:03 +02002903 counter->pmu->read(counter);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002904
2905 regs = get_irq_regs();
2906 /*
2907 * In case we exclude kernel IPs or are somehow not in interrupt
2908 * context, provide the next best thing, the user IP.
2909 */
2910 if ((counter->hw_event.exclude_kernel || !regs) &&
2911 !counter->hw_event.exclude_user)
2912 regs = task_pt_regs(current);
2913
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002914 if (regs) {
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002915 if (perf_counter_overflow(counter, 0, regs, 0))
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002916 ret = HRTIMER_NORESTART;
2917 }
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002918
Peter Zijlstrab23f3322009-06-02 15:13:03 +02002919 period = max_t(u64, 10000, counter->hw.sample_period);
Peter Zijlstra60db5e02009-05-15 15:19:28 +02002920 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002921
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002922 return ret;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002923}
2924
2925static void perf_swcounter_overflow(struct perf_counter *counter,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002926 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002927{
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002928 perf_swcounter_update(counter);
2929 perf_swcounter_set_period(counter);
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002930 if (perf_counter_overflow(counter, nmi, regs, addr))
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002931 /* soft-disable the counter */
2932 ;
2933
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002934}
2935
Paul Mackerras880ca152009-06-01 17:49:14 +10002936static int perf_swcounter_is_counting(struct perf_counter *counter)
2937{
2938 struct perf_counter_context *ctx;
2939 unsigned long flags;
2940 int count;
2941
2942 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
2943 return 1;
2944
2945 if (counter->state != PERF_COUNTER_STATE_INACTIVE)
2946 return 0;
2947
2948 /*
2949 * If the counter is inactive, it could be just because
2950 * its task is scheduled out, or because it's in a group
2951 * which could not go on the PMU. We want to count in
2952 * the first case but not the second. If the context is
2953 * currently active then an inactive software counter must
2954 * be the second case. If it's not currently active then
2955 * we need to know whether the counter was active when the
2956 * context was last active, which we can determine by
2957 * comparing counter->tstamp_stopped with ctx->time.
2958 *
2959 * We are within an RCU read-side critical section,
2960 * which protects the existence of *ctx.
2961 */
2962 ctx = counter->ctx;
2963 spin_lock_irqsave(&ctx->lock, flags);
2964 count = 1;
2965 /* Re-check state now we have the lock */
2966 if (counter->state < PERF_COUNTER_STATE_INACTIVE ||
2967 counter->ctx->is_active ||
2968 counter->tstamp_stopped < ctx->time)
2969 count = 0;
2970 spin_unlock_irqrestore(&ctx->lock, flags);
2971 return count;
2972}
2973
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002974static int perf_swcounter_match(struct perf_counter *counter,
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002975 enum perf_event_types type,
2976 u32 event, struct pt_regs *regs)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002977{
Paul Mackerras880ca152009-06-01 17:49:14 +10002978 u64 event_config;
2979
2980 event_config = ((u64) type << PERF_COUNTER_TYPE_SHIFT) | event;
2981
2982 if (!perf_swcounter_is_counting(counter))
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002983 return 0;
2984
Paul Mackerras880ca152009-06-01 17:49:14 +10002985 if (counter->hw_event.config != event_config)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002986 return 0;
2987
Paul Mackerras3f731ca2009-06-01 17:52:30 +10002988 if (regs) {
2989 if (counter->hw_event.exclude_user && user_mode(regs))
2990 return 0;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002991
Paul Mackerras3f731ca2009-06-01 17:52:30 +10002992 if (counter->hw_event.exclude_kernel && !user_mode(regs))
2993 return 0;
2994 }
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002995
2996 return 1;
2997}
2998
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002999static void perf_swcounter_add(struct perf_counter *counter, u64 nr,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003000 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003001{
3002 int neg = atomic64_add_negative(nr, &counter->hw.count);
Ingo Molnar22a4f652009-06-01 10:13:37 +02003003
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003004 if (counter->hw.sample_period && !neg && regs)
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003005 perf_swcounter_overflow(counter, nmi, regs, addr);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003006}
3007
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003008static void perf_swcounter_ctx_event(struct perf_counter_context *ctx,
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003009 enum perf_event_types type, u32 event,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003010 u64 nr, int nmi, struct pt_regs *regs,
3011 u64 addr)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003012{
3013 struct perf_counter *counter;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003014
Peter Zijlstra01ef09d2009-03-19 20:26:11 +01003015 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003016 return;
3017
Peter Zijlstra592903c2009-03-13 12:21:36 +01003018 rcu_read_lock();
3019 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003020 if (perf_swcounter_match(counter, type, event, regs))
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003021 perf_swcounter_add(counter, nr, nmi, regs, addr);
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003022 }
Peter Zijlstra592903c2009-03-13 12:21:36 +01003023 rcu_read_unlock();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003024}
3025
Peter Zijlstra96f6d442009-03-23 18:22:07 +01003026static int *perf_swcounter_recursion_context(struct perf_cpu_context *cpuctx)
3027{
3028 if (in_nmi())
3029 return &cpuctx->recursion[3];
3030
3031 if (in_irq())
3032 return &cpuctx->recursion[2];
3033
3034 if (in_softirq())
3035 return &cpuctx->recursion[1];
3036
3037 return &cpuctx->recursion[0];
3038}
3039
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003040static void __perf_swcounter_event(enum perf_event_types type, u32 event,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003041 u64 nr, int nmi, struct pt_regs *regs,
3042 u64 addr)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003043{
3044 struct perf_cpu_context *cpuctx = &get_cpu_var(perf_cpu_context);
Peter Zijlstra96f6d442009-03-23 18:22:07 +01003045 int *recursion = perf_swcounter_recursion_context(cpuctx);
Peter Zijlstra665c2142009-05-29 14:51:57 +02003046 struct perf_counter_context *ctx;
Peter Zijlstra96f6d442009-03-23 18:22:07 +01003047
3048 if (*recursion)
3049 goto out;
3050
3051 (*recursion)++;
3052 barrier();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003053
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003054 perf_swcounter_ctx_event(&cpuctx->ctx, type, event,
3055 nr, nmi, regs, addr);
Peter Zijlstra665c2142009-05-29 14:51:57 +02003056 rcu_read_lock();
3057 /*
3058 * doesn't really matter which of the child contexts the
3059 * events ends up in.
3060 */
3061 ctx = rcu_dereference(current->perf_counter_ctxp);
3062 if (ctx)
3063 perf_swcounter_ctx_event(ctx, type, event, nr, nmi, regs, addr);
3064 rcu_read_unlock();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003065
Peter Zijlstra96f6d442009-03-23 18:22:07 +01003066 barrier();
3067 (*recursion)--;
3068
3069out:
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003070 put_cpu_var(perf_cpu_context);
3071}
3072
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003073void
3074perf_swcounter_event(u32 event, u64 nr, int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003075{
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003076 __perf_swcounter_event(PERF_TYPE_SOFTWARE, event, nr, nmi, regs, addr);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003077}
3078
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003079static void perf_swcounter_read(struct perf_counter *counter)
3080{
3081 perf_swcounter_update(counter);
3082}
3083
3084static int perf_swcounter_enable(struct perf_counter *counter)
3085{
3086 perf_swcounter_set_period(counter);
3087 return 0;
3088}
3089
3090static void perf_swcounter_disable(struct perf_counter *counter)
3091{
3092 perf_swcounter_update(counter);
3093}
3094
Robert Richter4aeb0b42009-04-29 12:47:03 +02003095static const struct pmu perf_ops_generic = {
Peter Zijlstraac17dc82009-03-13 12:21:34 +01003096 .enable = perf_swcounter_enable,
3097 .disable = perf_swcounter_disable,
3098 .read = perf_swcounter_read,
3099};
3100
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003101/*
3102 * Software counter: cpu wall time clock
3103 */
3104
Paul Mackerras9abf8a02009-01-09 16:26:43 +11003105static void cpu_clock_perf_counter_update(struct perf_counter *counter)
3106{
3107 int cpu = raw_smp_processor_id();
3108 s64 prev;
3109 u64 now;
3110
3111 now = cpu_clock(cpu);
3112 prev = atomic64_read(&counter->hw.prev_count);
3113 atomic64_set(&counter->hw.prev_count, now);
3114 atomic64_add(now - prev, &counter->count);
3115}
3116
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003117static int cpu_clock_perf_counter_enable(struct perf_counter *counter)
3118{
3119 struct hw_perf_counter *hwc = &counter->hw;
3120 int cpu = raw_smp_processor_id();
3121
3122 atomic64_set(&hwc->prev_count, cpu_clock(cpu));
Peter Zijlstra039fc912009-03-13 16:43:47 +01003123 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
3124 hwc->hrtimer.function = perf_swcounter_hrtimer;
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003125 if (hwc->sample_period) {
3126 u64 period = max_t(u64, 10000, hwc->sample_period);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003127 __hrtimer_start_range_ns(&hwc->hrtimer,
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003128 ns_to_ktime(period), 0,
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003129 HRTIMER_MODE_REL, 0);
3130 }
3131
3132 return 0;
3133}
3134
Ingo Molnar5c92d122008-12-11 13:21:10 +01003135static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
3136{
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003137 if (counter->hw.sample_period)
Peter Zijlstrab986d7e2009-05-20 12:21:21 +02003138 hrtimer_cancel(&counter->hw.hrtimer);
Paul Mackerras9abf8a02009-01-09 16:26:43 +11003139 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01003140}
3141
3142static void cpu_clock_perf_counter_read(struct perf_counter *counter)
3143{
Paul Mackerras9abf8a02009-01-09 16:26:43 +11003144 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01003145}
3146
Robert Richter4aeb0b42009-04-29 12:47:03 +02003147static const struct pmu perf_ops_cpu_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +01003148 .enable = cpu_clock_perf_counter_enable,
3149 .disable = cpu_clock_perf_counter_disable,
3150 .read = cpu_clock_perf_counter_read,
Ingo Molnar5c92d122008-12-11 13:21:10 +01003151};
3152
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01003153/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003154 * Software counter: task time clock
3155 */
3156
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02003157static void task_clock_perf_counter_update(struct perf_counter *counter, u64 now)
Ingo Molnarbae43c92008-12-11 14:03:20 +01003158{
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02003159 u64 prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003160 s64 delta;
Ingo Molnarbae43c92008-12-11 14:03:20 +01003161
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02003162 prev = atomic64_xchg(&counter->hw.prev_count, now);
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003163 delta = now - prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003164 atomic64_add(delta, &counter->count);
Ingo Molnarbae43c92008-12-11 14:03:20 +01003165}
3166
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01003167static int task_clock_perf_counter_enable(struct perf_counter *counter)
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003168{
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003169 struct hw_perf_counter *hwc = &counter->hw;
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02003170 u64 now;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003171
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02003172 now = counter->ctx->time;
3173
3174 atomic64_set(&hwc->prev_count, now);
Peter Zijlstra039fc912009-03-13 16:43:47 +01003175 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
3176 hwc->hrtimer.function = perf_swcounter_hrtimer;
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003177 if (hwc->sample_period) {
3178 u64 period = max_t(u64, 10000, hwc->sample_period);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003179 __hrtimer_start_range_ns(&hwc->hrtimer,
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003180 ns_to_ktime(period), 0,
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003181 HRTIMER_MODE_REL, 0);
3182 }
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01003183
3184 return 0;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003185}
3186
3187static void task_clock_perf_counter_disable(struct perf_counter *counter)
3188{
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003189 if (counter->hw.sample_period)
Peter Zijlstrab986d7e2009-05-20 12:21:21 +02003190 hrtimer_cancel(&counter->hw.hrtimer);
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02003191 task_clock_perf_counter_update(counter, counter->ctx->time);
3192
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003193}
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01003194
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003195static void task_clock_perf_counter_read(struct perf_counter *counter)
3196{
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02003197 u64 time;
3198
3199 if (!in_nmi()) {
3200 update_context_time(counter->ctx);
3201 time = counter->ctx->time;
3202 } else {
3203 u64 now = perf_clock();
3204 u64 delta = now - counter->ctx->timestamp;
3205 time = counter->ctx->time + delta;
3206 }
3207
3208 task_clock_perf_counter_update(counter, time);
Ingo Molnarbae43c92008-12-11 14:03:20 +01003209}
3210
Robert Richter4aeb0b42009-04-29 12:47:03 +02003211static const struct pmu perf_ops_task_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +01003212 .enable = task_clock_perf_counter_enable,
3213 .disable = task_clock_perf_counter_disable,
3214 .read = task_clock_perf_counter_read,
Ingo Molnarbae43c92008-12-11 14:03:20 +01003215};
3216
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003217/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003218 * Software counter: cpu migrations
3219 */
Paul Mackerras3f731ca2009-06-01 17:52:30 +10003220void perf_counter_task_migration(struct task_struct *task, int cpu)
Ingo Molnar6c594c22008-12-14 12:34:15 +01003221{
Paul Mackerras3f731ca2009-06-01 17:52:30 +10003222 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
3223 struct perf_counter_context *ctx;
Paul Mackerras23a185c2009-02-09 22:42:47 +11003224
Paul Mackerras3f731ca2009-06-01 17:52:30 +10003225 perf_swcounter_ctx_event(&cpuctx->ctx, PERF_TYPE_SOFTWARE,
3226 PERF_COUNT_CPU_MIGRATIONS,
3227 1, 1, NULL, 0);
3228
3229 ctx = perf_pin_task_context(task);
3230 if (ctx) {
3231 perf_swcounter_ctx_event(ctx, PERF_TYPE_SOFTWARE,
3232 PERF_COUNT_CPU_MIGRATIONS,
3233 1, 1, NULL, 0);
3234 perf_unpin_context(ctx);
3235 }
Ingo Molnar6c594c22008-12-14 12:34:15 +01003236}
3237
Peter Zijlstrae077df42009-03-19 20:26:17 +01003238#ifdef CONFIG_EVENT_PROFILE
3239void perf_tpcounter_event(int event_id)
3240{
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003241 struct pt_regs *regs = get_irq_regs();
3242
3243 if (!regs)
3244 regs = task_pt_regs(current);
3245
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003246 __perf_swcounter_event(PERF_TYPE_TRACEPOINT, event_id, 1, 1, regs, 0);
Peter Zijlstrae077df42009-03-19 20:26:17 +01003247}
Steven Whitehouseff7b1b42009-04-15 16:55:05 +01003248EXPORT_SYMBOL_GPL(perf_tpcounter_event);
Peter Zijlstrae077df42009-03-19 20:26:17 +01003249
3250extern int ftrace_profile_enable(int);
3251extern void ftrace_profile_disable(int);
3252
3253static void tp_perf_counter_destroy(struct perf_counter *counter)
3254{
Peter Zijlstraf4a2deb42009-03-23 18:22:06 +01003255 ftrace_profile_disable(perf_event_id(&counter->hw_event));
Peter Zijlstrae077df42009-03-19 20:26:17 +01003256}
3257
Robert Richter4aeb0b42009-04-29 12:47:03 +02003258static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
Peter Zijlstrae077df42009-03-19 20:26:17 +01003259{
Peter Zijlstraf4a2deb42009-03-23 18:22:06 +01003260 int event_id = perf_event_id(&counter->hw_event);
Peter Zijlstrae077df42009-03-19 20:26:17 +01003261 int ret;
3262
3263 ret = ftrace_profile_enable(event_id);
3264 if (ret)
3265 return NULL;
3266
3267 counter->destroy = tp_perf_counter_destroy;
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003268 counter->hw.sample_period = counter->hw_event.sample_period;
Peter Zijlstrae077df42009-03-19 20:26:17 +01003269
3270 return &perf_ops_generic;
3271}
3272#else
Robert Richter4aeb0b42009-04-29 12:47:03 +02003273static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
Peter Zijlstrae077df42009-03-19 20:26:17 +01003274{
3275 return NULL;
3276}
3277#endif
3278
Robert Richter4aeb0b42009-04-29 12:47:03 +02003279static const struct pmu *sw_perf_counter_init(struct perf_counter *counter)
Ingo Molnar5c92d122008-12-11 13:21:10 +01003280{
Robert Richter4aeb0b42009-04-29 12:47:03 +02003281 const struct pmu *pmu = NULL;
Ingo Molnar5c92d122008-12-11 13:21:10 +01003282
Paul Mackerras0475f9e2009-02-11 14:35:35 +11003283 /*
3284 * Software counters (currently) can't in general distinguish
3285 * between user, kernel and hypervisor events.
3286 * However, context switches and cpu migrations are considered
3287 * to be kernel events, and page faults are never hypervisor
3288 * events.
3289 */
Peter Zijlstraf4a2deb42009-03-23 18:22:06 +01003290 switch (perf_event_id(&counter->hw_event)) {
Ingo Molnar5c92d122008-12-11 13:21:10 +01003291 case PERF_COUNT_CPU_CLOCK:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003292 pmu = &perf_ops_cpu_clock;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003293
Ingo Molnar5c92d122008-12-11 13:21:10 +01003294 break;
Ingo Molnarbae43c92008-12-11 14:03:20 +01003295 case PERF_COUNT_TASK_CLOCK:
Paul Mackerras23a185c2009-02-09 22:42:47 +11003296 /*
3297 * If the user instantiates this as a per-cpu counter,
3298 * use the cpu_clock counter instead.
3299 */
3300 if (counter->ctx->task)
Robert Richter4aeb0b42009-04-29 12:47:03 +02003301 pmu = &perf_ops_task_clock;
Paul Mackerras23a185c2009-02-09 22:42:47 +11003302 else
Robert Richter4aeb0b42009-04-29 12:47:03 +02003303 pmu = &perf_ops_cpu_clock;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003304
Ingo Molnarbae43c92008-12-11 14:03:20 +01003305 break;
Ingo Molnare06c61a2008-12-14 14:44:31 +01003306 case PERF_COUNT_PAGE_FAULTS:
Peter Zijlstraac17dc82009-03-13 12:21:34 +01003307 case PERF_COUNT_PAGE_FAULTS_MIN:
3308 case PERF_COUNT_PAGE_FAULTS_MAJ:
Ingo Molnar5d6a27d2008-12-14 12:28:33 +01003309 case PERF_COUNT_CONTEXT_SWITCHES:
Ingo Molnar6c594c22008-12-14 12:34:15 +01003310 case PERF_COUNT_CPU_MIGRATIONS:
Paul Mackerras3f731ca2009-06-01 17:52:30 +10003311 pmu = &perf_ops_generic;
Ingo Molnar6c594c22008-12-14 12:34:15 +01003312 break;
Ingo Molnar5c92d122008-12-11 13:21:10 +01003313 }
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003314
Robert Richter4aeb0b42009-04-29 12:47:03 +02003315 return pmu;
Ingo Molnar5c92d122008-12-11 13:21:10 +01003316}
3317
Thomas Gleixner0793a612008-12-04 20:12:29 +01003318/*
3319 * Allocate and initialize a counter structure
3320 */
3321static struct perf_counter *
Ingo Molnar04289bb2008-12-11 08:38:42 +01003322perf_counter_alloc(struct perf_counter_hw_event *hw_event,
3323 int cpu,
Paul Mackerras23a185c2009-02-09 22:42:47 +11003324 struct perf_counter_context *ctx,
Ingo Molnar9b51f662008-12-12 13:49:45 +01003325 struct perf_counter *group_leader,
3326 gfp_t gfpflags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003327{
Robert Richter4aeb0b42009-04-29 12:47:03 +02003328 const struct pmu *pmu;
Ingo Molnar621a01e2008-12-11 12:46:46 +01003329 struct perf_counter *counter;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003330 struct hw_perf_counter *hwc;
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003331 long err;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003332
Ingo Molnar9b51f662008-12-12 13:49:45 +01003333 counter = kzalloc(sizeof(*counter), gfpflags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003334 if (!counter)
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003335 return ERR_PTR(-ENOMEM);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003336
Ingo Molnar04289bb2008-12-11 08:38:42 +01003337 /*
3338 * Single counters are their own group leaders, with an
3339 * empty sibling list:
3340 */
3341 if (!group_leader)
3342 group_leader = counter;
3343
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003344 mutex_init(&counter->child_mutex);
3345 INIT_LIST_HEAD(&counter->child_list);
3346
Ingo Molnar04289bb2008-12-11 08:38:42 +01003347 INIT_LIST_HEAD(&counter->list_entry);
Peter Zijlstra592903c2009-03-13 12:21:36 +01003348 INIT_LIST_HEAD(&counter->event_entry);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003349 INIT_LIST_HEAD(&counter->sibling_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003350 init_waitqueue_head(&counter->waitq);
3351
Peter Zijlstra7b732a72009-03-23 18:22:10 +01003352 mutex_init(&counter->mmap_mutex);
3353
Ingo Molnar9f66a382008-12-10 12:33:23 +01003354 counter->cpu = cpu;
3355 counter->hw_event = *hw_event;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003356 counter->group_leader = group_leader;
Robert Richter4aeb0b42009-04-29 12:47:03 +02003357 counter->pmu = NULL;
Paul Mackerras23a185c2009-02-09 22:42:47 +11003358 counter->ctx = ctx;
Ingo Molnar329d8762009-05-26 08:10:00 +02003359 counter->oncpu = -1;
3360
Ingo Molnar235c7fc2008-12-21 14:43:25 +01003361 counter->state = PERF_COUNTER_STATE_INACTIVE;
Ingo Molnara86ed502008-12-17 00:43:10 +01003362 if (hw_event->disabled)
3363 counter->state = PERF_COUNTER_STATE_OFF;
3364
Robert Richter4aeb0b42009-04-29 12:47:03 +02003365 pmu = NULL;
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003366
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003367 hwc = &counter->hw;
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003368 if (hw_event->freq && hw_event->sample_freq)
3369 hwc->sample_period = div64_u64(TICK_NSEC, hw_event->sample_freq);
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003370 else
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003371 hwc->sample_period = hw_event->sample_period;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003372
Peter Zijlstra2023b352009-05-05 17:50:26 +02003373 /*
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003374 * we currently do not support PERF_SAMPLE_GROUP on inherited counters
Peter Zijlstra2023b352009-05-05 17:50:26 +02003375 */
Peter Zijlstrab23f3322009-06-02 15:13:03 +02003376 if (hw_event->inherit && (hw_event->sample_type & PERF_SAMPLE_GROUP))
Peter Zijlstra2023b352009-05-05 17:50:26 +02003377 goto done;
3378
Peter Zijlstraf4a2deb42009-03-23 18:22:06 +01003379 if (perf_event_raw(hw_event)) {
Robert Richter4aeb0b42009-04-29 12:47:03 +02003380 pmu = hw_perf_counter_init(counter);
Peter Zijlstraf4a2deb42009-03-23 18:22:06 +01003381 goto done;
3382 }
3383
3384 switch (perf_event_type(hw_event)) {
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003385 case PERF_TYPE_HARDWARE:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003386 pmu = hw_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003387 break;
3388
3389 case PERF_TYPE_SOFTWARE:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003390 pmu = sw_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003391 break;
3392
3393 case PERF_TYPE_TRACEPOINT:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003394 pmu = tp_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003395 break;
3396 }
Peter Zijlstraf4a2deb42009-03-23 18:22:06 +01003397done:
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003398 err = 0;
Robert Richter4aeb0b42009-04-29 12:47:03 +02003399 if (!pmu)
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003400 err = -EINVAL;
Robert Richter4aeb0b42009-04-29 12:47:03 +02003401 else if (IS_ERR(pmu))
3402 err = PTR_ERR(pmu);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003403
3404 if (err) {
3405 kfree(counter);
3406 return ERR_PTR(err);
3407 }
3408
Robert Richter4aeb0b42009-04-29 12:47:03 +02003409 counter->pmu = pmu;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003410
Peter Zijlstra7fc23a52009-05-08 18:52:21 +02003411 atomic_inc(&nr_counters);
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02003412 if (counter->hw_event.mmap)
3413 atomic_inc(&nr_mmap_tracking);
3414 if (counter->hw_event.munmap)
3415 atomic_inc(&nr_munmap_tracking);
3416 if (counter->hw_event.comm)
3417 atomic_inc(&nr_comm_tracking);
3418
Thomas Gleixner0793a612008-12-04 20:12:29 +01003419 return counter;
3420}
3421
Peter Zijlstra8e5799b2009-06-02 15:08:15 +02003422static atomic64_t perf_counter_id;
3423
Thomas Gleixner0793a612008-12-04 20:12:29 +01003424/**
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003425 * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
Ingo Molnar9f66a382008-12-10 12:33:23 +01003426 *
3427 * @hw_event_uptr: event type attributes for monitoring/sampling
Thomas Gleixner0793a612008-12-04 20:12:29 +01003428 * @pid: target pid
Ingo Molnar9f66a382008-12-10 12:33:23 +01003429 * @cpu: target cpu
3430 * @group_fd: group leader counter fd
Thomas Gleixner0793a612008-12-04 20:12:29 +01003431 */
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003432SYSCALL_DEFINE5(perf_counter_open,
Paul Mackerrasf3dfd262009-02-26 22:43:46 +11003433 const struct perf_counter_hw_event __user *, hw_event_uptr,
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003434 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003435{
Ingo Molnar04289bb2008-12-11 08:38:42 +01003436 struct perf_counter *counter, *group_leader;
Ingo Molnar9f66a382008-12-10 12:33:23 +01003437 struct perf_counter_hw_event hw_event;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003438 struct perf_counter_context *ctx;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003439 struct file *counter_file = NULL;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003440 struct file *group_file = NULL;
3441 int fput_needed = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003442 int fput_needed2 = 0;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003443 int ret;
3444
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003445 /* for future expandability... */
3446 if (flags)
3447 return -EINVAL;
3448
Ingo Molnar9f66a382008-12-10 12:33:23 +01003449 if (copy_from_user(&hw_event, hw_event_uptr, sizeof(hw_event)) != 0)
Thomas Gleixnereab656a2008-12-08 19:26:59 +01003450 return -EFAULT;
3451
Ingo Molnar04289bb2008-12-11 08:38:42 +01003452 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01003453 * Get the target context (task or percpu):
3454 */
3455 ctx = find_get_context(pid, cpu);
3456 if (IS_ERR(ctx))
3457 return PTR_ERR(ctx);
3458
3459 /*
3460 * Look up the group leader (we will attach this counter to it):
Ingo Molnar04289bb2008-12-11 08:38:42 +01003461 */
3462 group_leader = NULL;
3463 if (group_fd != -1) {
3464 ret = -EINVAL;
3465 group_file = fget_light(group_fd, &fput_needed);
3466 if (!group_file)
Ingo Molnarccff2862008-12-11 11:26:29 +01003467 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003468 if (group_file->f_op != &perf_fops)
Ingo Molnarccff2862008-12-11 11:26:29 +01003469 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003470
3471 group_leader = group_file->private_data;
3472 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01003473 * Do not allow a recursive hierarchy (this new sibling
3474 * becoming part of another group-sibling):
Ingo Molnar04289bb2008-12-11 08:38:42 +01003475 */
Ingo Molnarccff2862008-12-11 11:26:29 +01003476 if (group_leader->group_leader != group_leader)
3477 goto err_put_context;
3478 /*
3479 * Do not allow to attach to a group in a different
3480 * task or CPU context:
3481 */
3482 if (group_leader->ctx != ctx)
3483 goto err_put_context;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11003484 /*
3485 * Only a group leader can be exclusive or pinned
3486 */
3487 if (hw_event.exclusive || hw_event.pinned)
3488 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003489 }
3490
Paul Mackerras23a185c2009-02-09 22:42:47 +11003491 counter = perf_counter_alloc(&hw_event, cpu, ctx, group_leader,
3492 GFP_KERNEL);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003493 ret = PTR_ERR(counter);
3494 if (IS_ERR(counter))
Thomas Gleixner0793a612008-12-04 20:12:29 +01003495 goto err_put_context;
3496
Thomas Gleixner0793a612008-12-04 20:12:29 +01003497 ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
3498 if (ret < 0)
Ingo Molnar9b51f662008-12-12 13:49:45 +01003499 goto err_free_put_context;
3500
3501 counter_file = fget_light(ret, &fput_needed2);
3502 if (!counter_file)
3503 goto err_free_put_context;
3504
3505 counter->filp = counter_file;
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003506 WARN_ON_ONCE(ctx->parent_ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003507 mutex_lock(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003508 perf_install_in_context(ctx, counter, cpu);
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003509 ++ctx->generation;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003510 mutex_unlock(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003511
Peter Zijlstra082ff5a2009-05-23 18:29:00 +02003512 counter->owner = current;
3513 get_task_struct(current);
3514 mutex_lock(&current->perf_counter_mutex);
3515 list_add_tail(&counter->owner_entry, &current->perf_counter_list);
3516 mutex_unlock(&current->perf_counter_mutex);
3517
Peter Zijlstra709e50c2009-06-02 14:13:15 +02003518 counter->ns = get_pid_ns(current->nsproxy->pid_ns);
Peter Zijlstra8e5799b2009-06-02 15:08:15 +02003519 counter->id = atomic64_inc_return(&perf_counter_id);
Peter Zijlstra709e50c2009-06-02 14:13:15 +02003520
Ingo Molnar9b51f662008-12-12 13:49:45 +01003521 fput_light(counter_file, fput_needed2);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003522
Ingo Molnar04289bb2008-12-11 08:38:42 +01003523out_fput:
3524 fput_light(group_file, fput_needed);
3525
Thomas Gleixner0793a612008-12-04 20:12:29 +01003526 return ret;
3527
Ingo Molnar9b51f662008-12-12 13:49:45 +01003528err_free_put_context:
Thomas Gleixner0793a612008-12-04 20:12:29 +01003529 kfree(counter);
3530
3531err_put_context:
Paul Mackerrasc93f7662009-05-28 22:18:17 +10003532 put_ctx(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003533
Ingo Molnar04289bb2008-12-11 08:38:42 +01003534 goto out_fput;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003535}
3536
Ingo Molnar9b51f662008-12-12 13:49:45 +01003537/*
Ingo Molnar9b51f662008-12-12 13:49:45 +01003538 * inherit a counter from parent task to child task:
3539 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11003540static struct perf_counter *
Ingo Molnar9b51f662008-12-12 13:49:45 +01003541inherit_counter(struct perf_counter *parent_counter,
3542 struct task_struct *parent,
3543 struct perf_counter_context *parent_ctx,
3544 struct task_struct *child,
Paul Mackerrasd859e292009-01-17 18:10:22 +11003545 struct perf_counter *group_leader,
Ingo Molnar9b51f662008-12-12 13:49:45 +01003546 struct perf_counter_context *child_ctx)
3547{
3548 struct perf_counter *child_counter;
3549
Paul Mackerrasd859e292009-01-17 18:10:22 +11003550 /*
3551 * Instead of creating recursive hierarchies of counters,
3552 * we link inherited counters back to the original parent,
3553 * which has a filp for sure, which we use as the reference
3554 * count:
3555 */
3556 if (parent_counter->parent)
3557 parent_counter = parent_counter->parent;
3558
Ingo Molnar9b51f662008-12-12 13:49:45 +01003559 child_counter = perf_counter_alloc(&parent_counter->hw_event,
Paul Mackerras23a185c2009-02-09 22:42:47 +11003560 parent_counter->cpu, child_ctx,
3561 group_leader, GFP_KERNEL);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003562 if (IS_ERR(child_counter))
3563 return child_counter;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10003564 get_ctx(child_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003565
3566 /*
Paul Mackerras564c2b22009-05-22 14:27:22 +10003567 * Make the child state follow the state of the parent counter,
3568 * not its hw_event.disabled bit. We hold the parent's mutex,
Ingo Molnar22a4f652009-06-01 10:13:37 +02003569 * so we won't race with perf_counter_{en, dis}able_family.
Paul Mackerras564c2b22009-05-22 14:27:22 +10003570 */
3571 if (parent_counter->state >= PERF_COUNTER_STATE_INACTIVE)
3572 child_counter->state = PERF_COUNTER_STATE_INACTIVE;
3573 else
3574 child_counter->state = PERF_COUNTER_STATE_OFF;
3575
3576 /*
Ingo Molnar9b51f662008-12-12 13:49:45 +01003577 * Link it up in the child's context:
3578 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +11003579 add_counter_to_ctx(child_counter, child_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003580
3581 child_counter->parent = parent_counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003582 /*
3583 * inherit into child's child as well:
3584 */
3585 child_counter->hw_event.inherit = 1;
3586
3587 /*
3588 * Get a reference to the parent filp - we will fput it
3589 * when the child counter exits. This is safe to do because
3590 * we are in the parent and we know that the filp still
3591 * exists and has a nonzero count:
3592 */
3593 atomic_long_inc(&parent_counter->filp->f_count);
3594
Paul Mackerrasd859e292009-01-17 18:10:22 +11003595 /*
3596 * Link this into the parent counter's child list
3597 */
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003598 WARN_ON_ONCE(parent_counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003599 mutex_lock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003600 list_add_tail(&child_counter->child_list, &parent_counter->child_list);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003601 mutex_unlock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003602
3603 return child_counter;
3604}
3605
3606static int inherit_group(struct perf_counter *parent_counter,
3607 struct task_struct *parent,
3608 struct perf_counter_context *parent_ctx,
3609 struct task_struct *child,
3610 struct perf_counter_context *child_ctx)
3611{
3612 struct perf_counter *leader;
3613 struct perf_counter *sub;
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003614 struct perf_counter *child_ctr;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003615
3616 leader = inherit_counter(parent_counter, parent, parent_ctx,
3617 child, NULL, child_ctx);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003618 if (IS_ERR(leader))
3619 return PTR_ERR(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003620 list_for_each_entry(sub, &parent_counter->sibling_list, list_entry) {
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003621 child_ctr = inherit_counter(sub, parent, parent_ctx,
3622 child, leader, child_ctx);
3623 if (IS_ERR(child_ctr))
3624 return PTR_ERR(child_ctr);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003625 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01003626 return 0;
3627}
3628
Paul Mackerrasd859e292009-01-17 18:10:22 +11003629static void sync_child_counter(struct perf_counter *child_counter,
3630 struct perf_counter *parent_counter)
3631{
Peter Zijlstra8bc20952009-05-15 20:45:59 +02003632 u64 child_val;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003633
Paul Mackerrasd859e292009-01-17 18:10:22 +11003634 child_val = atomic64_read(&child_counter->count);
3635
3636 /*
3637 * Add back the child's count to the parent's count:
3638 */
3639 atomic64_add(child_val, &parent_counter->count);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11003640 atomic64_add(child_counter->total_time_enabled,
3641 &parent_counter->child_total_time_enabled);
3642 atomic64_add(child_counter->total_time_running,
3643 &parent_counter->child_total_time_running);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003644
3645 /*
3646 * Remove this counter from the parent's list
3647 */
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003648 WARN_ON_ONCE(parent_counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003649 mutex_lock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003650 list_del_init(&child_counter->child_list);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003651 mutex_unlock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003652
3653 /*
3654 * Release the parent counter, if this was the last
3655 * reference to it.
3656 */
3657 fput(parent_counter->filp);
3658}
3659
Ingo Molnar9b51f662008-12-12 13:49:45 +01003660static void
Peter Zijlstrabbbee902009-05-29 14:25:58 +02003661__perf_counter_exit_task(struct perf_counter *child_counter,
Ingo Molnar9b51f662008-12-12 13:49:45 +01003662 struct perf_counter_context *child_ctx)
3663{
3664 struct perf_counter *parent_counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003665
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003666 update_counter_times(child_counter);
Peter Zijlstraaa9c67f2009-05-23 18:28:59 +02003667 perf_counter_remove_from_context(child_counter);
Ingo Molnar0cc0c022008-12-14 23:20:36 +01003668
Ingo Molnar9b51f662008-12-12 13:49:45 +01003669 parent_counter = child_counter->parent;
3670 /*
3671 * It can happen that parent exits first, and has counters
3672 * that are still around due to the child reference. These
3673 * counters need to be zapped - but otherwise linger.
3674 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11003675 if (parent_counter) {
3676 sync_child_counter(child_counter, parent_counter);
Peter Zijlstraf1600952009-03-19 20:26:16 +01003677 free_counter(child_counter);
Paul Mackerras4bcf3492009-02-11 13:53:19 +01003678 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01003679}
3680
3681/*
Paul Mackerrasd859e292009-01-17 18:10:22 +11003682 * When a child task exits, feed back counter values to parent counters.
Ingo Molnar9b51f662008-12-12 13:49:45 +01003683 */
3684void perf_counter_exit_task(struct task_struct *child)
3685{
3686 struct perf_counter *child_counter, *tmp;
3687 struct perf_counter_context *child_ctx;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003688 unsigned long flags;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003689
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003690 if (likely(!child->perf_counter_ctxp))
Ingo Molnar9b51f662008-12-12 13:49:45 +01003691 return;
3692
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003693 local_irq_save(flags);
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003694 /*
3695 * We can't reschedule here because interrupts are disabled,
3696 * and either child is current or it is a task that can't be
3697 * scheduled, so we are now safe from rescheduling changing
3698 * our context.
3699 */
3700 child_ctx = child->perf_counter_ctxp;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003701 __perf_counter_task_sched_out(child_ctx);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10003702
3703 /*
3704 * Take the context lock here so that if find_get_context is
3705 * reading child->perf_counter_ctxp, we wait until it has
3706 * incremented the context's refcount before we do put_ctx below.
3707 */
3708 spin_lock(&child_ctx->lock);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003709 child->perf_counter_ctxp = NULL;
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003710 if (child_ctx->parent_ctx) {
3711 /*
3712 * This context is a clone; unclone it so it can't get
3713 * swapped to another process while we're removing all
3714 * the counters from it.
3715 */
3716 put_ctx(child_ctx->parent_ctx);
3717 child_ctx->parent_ctx = NULL;
3718 }
Paul Mackerrasc93f7662009-05-28 22:18:17 +10003719 spin_unlock(&child_ctx->lock);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003720 local_irq_restore(flags);
3721
3722 mutex_lock(&child_ctx->mutex);
3723
Peter Zijlstra8bc20952009-05-15 20:45:59 +02003724again:
Ingo Molnar9b51f662008-12-12 13:49:45 +01003725 list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list,
3726 list_entry)
Peter Zijlstrabbbee902009-05-29 14:25:58 +02003727 __perf_counter_exit_task(child_counter, child_ctx);
Peter Zijlstra8bc20952009-05-15 20:45:59 +02003728
3729 /*
3730 * If the last counter was a group counter, it will have appended all
3731 * its siblings to the list, but we obtained 'tmp' before that which
3732 * will still point to the list head terminating the iteration.
3733 */
3734 if (!list_empty(&child_ctx->counter_list))
3735 goto again;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003736
3737 mutex_unlock(&child_ctx->mutex);
3738
3739 put_ctx(child_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003740}
3741
3742/*
Peter Zijlstrabbbee902009-05-29 14:25:58 +02003743 * free an unexposed, unused context as created by inheritance by
3744 * init_task below, used by fork() in case of fail.
3745 */
3746void perf_counter_free_task(struct task_struct *task)
3747{
3748 struct perf_counter_context *ctx = task->perf_counter_ctxp;
3749 struct perf_counter *counter, *tmp;
3750
3751 if (!ctx)
3752 return;
3753
3754 mutex_lock(&ctx->mutex);
3755again:
3756 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry) {
3757 struct perf_counter *parent = counter->parent;
3758
3759 if (WARN_ON_ONCE(!parent))
3760 continue;
3761
3762 mutex_lock(&parent->child_mutex);
3763 list_del_init(&counter->child_list);
3764 mutex_unlock(&parent->child_mutex);
3765
3766 fput(parent->filp);
3767
3768 list_del_counter(counter, ctx);
3769 free_counter(counter);
3770 }
3771
3772 if (!list_empty(&ctx->counter_list))
3773 goto again;
3774
3775 mutex_unlock(&ctx->mutex);
3776
3777 put_ctx(ctx);
3778}
3779
3780/*
Ingo Molnar9b51f662008-12-12 13:49:45 +01003781 * Initialize the perf_counter context in task_struct
3782 */
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02003783int perf_counter_init_task(struct task_struct *child)
Ingo Molnar9b51f662008-12-12 13:49:45 +01003784{
3785 struct perf_counter_context *child_ctx, *parent_ctx;
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003786 struct perf_counter_context *cloned_ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003787 struct perf_counter *counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003788 struct task_struct *parent = current;
Paul Mackerras564c2b22009-05-22 14:27:22 +10003789 int inherited_all = 1;
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02003790 int ret = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003791
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003792 child->perf_counter_ctxp = NULL;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003793
Peter Zijlstra082ff5a2009-05-23 18:29:00 +02003794 mutex_init(&child->perf_counter_mutex);
3795 INIT_LIST_HEAD(&child->perf_counter_list);
3796
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003797 if (likely(!parent->perf_counter_ctxp))
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02003798 return 0;
3799
Ingo Molnar9b51f662008-12-12 13:49:45 +01003800 /*
3801 * This is executed from the parent task context, so inherit
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003802 * counters that have been marked for cloning.
3803 * First allocate and initialize a context for the child.
Ingo Molnar9b51f662008-12-12 13:49:45 +01003804 */
3805
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003806 child_ctx = kmalloc(sizeof(struct perf_counter_context), GFP_KERNEL);
3807 if (!child_ctx)
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02003808 return -ENOMEM;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003809
3810 __perf_counter_init_context(child_ctx, child);
3811 child->perf_counter_ctxp = child_ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10003812 get_task_struct(child);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003813
Ingo Molnar9b51f662008-12-12 13:49:45 +01003814 /*
Paul Mackerras25346b932009-06-01 17:48:12 +10003815 * If the parent's context is a clone, pin it so it won't get
3816 * swapped under us.
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003817 */
Paul Mackerras25346b932009-06-01 17:48:12 +10003818 parent_ctx = perf_pin_task_context(parent);
3819
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003820 /*
3821 * No need to check if parent_ctx != NULL here; since we saw
3822 * it non-NULL earlier, the only reason for it to become NULL
3823 * is if we exit, and since we're currently in the middle of
3824 * a fork we can't be exiting at the same time.
3825 */
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003826
3827 /*
Ingo Molnar9b51f662008-12-12 13:49:45 +01003828 * Lock the parent list. No need to lock the child - not PID
3829 * hashed yet and not running, so nobody can access it.
3830 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11003831 mutex_lock(&parent_ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003832
3833 /*
3834 * We dont have to disable NMIs - we are only looking at
3835 * the list, not manipulating it:
3836 */
Peter Zijlstrad7b629a2009-05-20 12:21:19 +02003837 list_for_each_entry_rcu(counter, &parent_ctx->event_list, event_entry) {
3838 if (counter != counter->group_leader)
3839 continue;
3840
Paul Mackerras564c2b22009-05-22 14:27:22 +10003841 if (!counter->hw_event.inherit) {
3842 inherited_all = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003843 continue;
Paul Mackerras564c2b22009-05-22 14:27:22 +10003844 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01003845
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02003846 ret = inherit_group(counter, parent, parent_ctx,
3847 child, child_ctx);
3848 if (ret) {
Paul Mackerras564c2b22009-05-22 14:27:22 +10003849 inherited_all = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003850 break;
Paul Mackerras564c2b22009-05-22 14:27:22 +10003851 }
3852 }
3853
3854 if (inherited_all) {
3855 /*
3856 * Mark the child context as a clone of the parent
3857 * context, or of whatever the parent is a clone of.
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003858 * Note that if the parent is a clone, it could get
3859 * uncloned at any point, but that doesn't matter
3860 * because the list of counters and the generation
3861 * count can't have changed since we took the mutex.
Paul Mackerras564c2b22009-05-22 14:27:22 +10003862 */
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003863 cloned_ctx = rcu_dereference(parent_ctx->parent_ctx);
3864 if (cloned_ctx) {
3865 child_ctx->parent_ctx = cloned_ctx;
Paul Mackerras25346b932009-06-01 17:48:12 +10003866 child_ctx->parent_gen = parent_ctx->parent_gen;
Paul Mackerras564c2b22009-05-22 14:27:22 +10003867 } else {
3868 child_ctx->parent_ctx = parent_ctx;
3869 child_ctx->parent_gen = parent_ctx->generation;
3870 }
3871 get_ctx(child_ctx->parent_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003872 }
3873
Paul Mackerrasd859e292009-01-17 18:10:22 +11003874 mutex_unlock(&parent_ctx->mutex);
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02003875
Paul Mackerras25346b932009-06-01 17:48:12 +10003876 perf_unpin_context(parent_ctx);
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003877
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02003878 return ret;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003879}
3880
Ingo Molnar04289bb2008-12-11 08:38:42 +01003881static void __cpuinit perf_counter_init_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003882{
Ingo Molnar04289bb2008-12-11 08:38:42 +01003883 struct perf_cpu_context *cpuctx;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003884
Ingo Molnar04289bb2008-12-11 08:38:42 +01003885 cpuctx = &per_cpu(perf_cpu_context, cpu);
3886 __perf_counter_init_context(&cpuctx->ctx, NULL);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003887
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003888 spin_lock(&perf_resource_lock);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003889 cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu;
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003890 spin_unlock(&perf_resource_lock);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003891
Paul Mackerras01d02872009-01-14 13:44:19 +11003892 hw_perf_counter_setup(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003893}
3894
3895#ifdef CONFIG_HOTPLUG_CPU
Ingo Molnar04289bb2008-12-11 08:38:42 +01003896static void __perf_counter_exit_cpu(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003897{
3898 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
3899 struct perf_counter_context *ctx = &cpuctx->ctx;
3900 struct perf_counter *counter, *tmp;
3901
Ingo Molnar04289bb2008-12-11 08:38:42 +01003902 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry)
3903 __perf_counter_remove_from_context(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003904}
Ingo Molnar04289bb2008-12-11 08:38:42 +01003905static void perf_counter_exit_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003906{
Paul Mackerrasd859e292009-01-17 18:10:22 +11003907 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
3908 struct perf_counter_context *ctx = &cpuctx->ctx;
3909
3910 mutex_lock(&ctx->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003911 smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003912 mutex_unlock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003913}
3914#else
Ingo Molnar04289bb2008-12-11 08:38:42 +01003915static inline void perf_counter_exit_cpu(int cpu) { }
Thomas Gleixner0793a612008-12-04 20:12:29 +01003916#endif
3917
3918static int __cpuinit
3919perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
3920{
3921 unsigned int cpu = (long)hcpu;
3922
3923 switch (action) {
3924
3925 case CPU_UP_PREPARE:
3926 case CPU_UP_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01003927 perf_counter_init_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003928 break;
3929
3930 case CPU_DOWN_PREPARE:
3931 case CPU_DOWN_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01003932 perf_counter_exit_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003933 break;
3934
3935 default:
3936 break;
3937 }
3938
3939 return NOTIFY_OK;
3940}
3941
Paul Mackerrasf38b0822009-06-02 21:05:16 +10003942/*
3943 * This has to have a higher priority than migration_notifier in sched.c.
3944 */
Thomas Gleixner0793a612008-12-04 20:12:29 +01003945static struct notifier_block __cpuinitdata perf_cpu_nb = {
3946 .notifier_call = perf_cpu_notify,
Paul Mackerrasf38b0822009-06-02 21:05:16 +10003947 .priority = 20,
Thomas Gleixner0793a612008-12-04 20:12:29 +01003948};
3949
Ingo Molnar0d905bc2009-05-04 19:13:30 +02003950void __init perf_counter_init(void)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003951{
3952 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
3953 (void *)(long)smp_processor_id());
3954 register_cpu_notifier(&perf_cpu_nb);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003955}
Thomas Gleixner0793a612008-12-04 20:12:29 +01003956
3957static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
3958{
3959 return sprintf(buf, "%d\n", perf_reserved_percpu);
3960}
3961
3962static ssize_t
3963perf_set_reserve_percpu(struct sysdev_class *class,
3964 const char *buf,
3965 size_t count)
3966{
3967 struct perf_cpu_context *cpuctx;
3968 unsigned long val;
3969 int err, cpu, mpt;
3970
3971 err = strict_strtoul(buf, 10, &val);
3972 if (err)
3973 return err;
3974 if (val > perf_max_counters)
3975 return -EINVAL;
3976
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003977 spin_lock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003978 perf_reserved_percpu = val;
3979 for_each_online_cpu(cpu) {
3980 cpuctx = &per_cpu(perf_cpu_context, cpu);
3981 spin_lock_irq(&cpuctx->ctx.lock);
3982 mpt = min(perf_max_counters - cpuctx->ctx.nr_counters,
3983 perf_max_counters - perf_reserved_percpu);
3984 cpuctx->max_pertask = mpt;
3985 spin_unlock_irq(&cpuctx->ctx.lock);
3986 }
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003987 spin_unlock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003988
3989 return count;
3990}
3991
3992static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
3993{
3994 return sprintf(buf, "%d\n", perf_overcommit);
3995}
3996
3997static ssize_t
3998perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
3999{
4000 unsigned long val;
4001 int err;
4002
4003 err = strict_strtoul(buf, 10, &val);
4004 if (err)
4005 return err;
4006 if (val > 1)
4007 return -EINVAL;
4008
Ingo Molnar1dce8d92009-05-04 19:23:18 +02004009 spin_lock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004010 perf_overcommit = val;
Ingo Molnar1dce8d92009-05-04 19:23:18 +02004011 spin_unlock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01004012
4013 return count;
4014}
4015
4016static SYSDEV_CLASS_ATTR(
4017 reserve_percpu,
4018 0644,
4019 perf_show_reserve_percpu,
4020 perf_set_reserve_percpu
4021 );
4022
4023static SYSDEV_CLASS_ATTR(
4024 overcommit,
4025 0644,
4026 perf_show_overcommit,
4027 perf_set_overcommit
4028 );
4029
4030static struct attribute *perfclass_attrs[] = {
4031 &attr_reserve_percpu.attr,
4032 &attr_overcommit.attr,
4033 NULL
4034};
4035
4036static struct attribute_group perfclass_attr_group = {
4037 .attrs = perfclass_attrs,
4038 .name = "perf_counters",
4039};
4040
4041static int __init perf_counter_sysfs_init(void)
4042{
4043 return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
4044 &perfclass_attr_group);
4045}
4046device_initcall(perf_counter_sysfs_init);