blob: 863703b3158f8682bafdfe2d4618c4310cb63b4d [file] [log] [blame]
Thomas Gleixner0793a612008-12-04 20:12:29 +01001/*
2 * Performance counter core code
3 *
4 * Copyright(C) 2008 Thomas Gleixner <tglx@linutronix.de>
5 * Copyright(C) 2008 Red Hat, Inc., Ingo Molnar
6 *
Peter Zijlstra7b732a72009-03-23 18:22:10 +01007 *
8 * For licensing details see kernel-base/COPYING
Thomas Gleixner0793a612008-12-04 20:12:29 +01009 */
10
11#include <linux/fs.h>
Peter Zijlstrab9cacc72009-03-25 12:30:22 +010012#include <linux/mm.h>
Thomas Gleixner0793a612008-12-04 20:12:29 +010013#include <linux/cpu.h>
14#include <linux/smp.h>
Ingo Molnar04289bb2008-12-11 08:38:42 +010015#include <linux/file.h>
Thomas Gleixner0793a612008-12-04 20:12:29 +010016#include <linux/poll.h>
17#include <linux/sysfs.h>
18#include <linux/ptrace.h>
19#include <linux/percpu.h>
Peter Zijlstrab9cacc72009-03-25 12:30:22 +010020#include <linux/vmstat.h>
21#include <linux/hardirq.h>
22#include <linux/rculist.h>
Thomas Gleixner0793a612008-12-04 20:12:29 +010023#include <linux/uaccess.h>
24#include <linux/syscalls.h>
25#include <linux/anon_inodes.h>
Ingo Molnaraa9c4c02008-12-17 14:10:57 +010026#include <linux/kernel_stat.h>
Thomas Gleixner0793a612008-12-04 20:12:29 +010027#include <linux/perf_counter.h>
Peter Zijlstra0a4a9392009-03-30 19:07:05 +020028#include <linux/dcache.h>
Thomas Gleixner0793a612008-12-04 20:12:29 +010029
Tim Blechmann4e193bd2009-03-14 14:29:25 +010030#include <asm/irq_regs.h>
31
Thomas Gleixner0793a612008-12-04 20:12:29 +010032/*
33 * Each CPU has a list of per CPU counters:
34 */
35DEFINE_PER_CPU(struct perf_cpu_context, perf_cpu_context);
36
Ingo Molnar088e2852008-12-14 20:21:00 +010037int perf_max_counters __read_mostly = 1;
Thomas Gleixner0793a612008-12-04 20:12:29 +010038static int perf_reserved_percpu __read_mostly;
39static int perf_overcommit __read_mostly = 1;
40
41/*
42 * Mutex for (sysadmin-configurable) counter reservations:
43 */
44static DEFINE_MUTEX(perf_resource_mutex);
45
46/*
47 * Architecture provided APIs - weak aliases:
48 */
Ingo Molnar5c92d122008-12-11 13:21:10 +010049extern __weak const struct hw_perf_counter_ops *
Ingo Molnar621a01e2008-12-11 12:46:46 +010050hw_perf_counter_init(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +010051{
Paul Mackerrasff6f0542009-01-09 16:19:25 +110052 return NULL;
Thomas Gleixner0793a612008-12-04 20:12:29 +010053}
54
Ingo Molnar01b28382008-12-11 13:45:51 +010055u64 __weak hw_perf_save_disable(void) { return 0; }
Yinghai Lu01ea1cc2008-12-26 21:05:06 -080056void __weak hw_perf_restore(u64 ctrl) { barrier(); }
Paul Mackerras01d02872009-01-14 13:44:19 +110057void __weak hw_perf_counter_setup(int cpu) { barrier(); }
Paul Mackerras3cbed422009-01-09 16:43:42 +110058int __weak hw_perf_group_sched_in(struct perf_counter *group_leader,
59 struct perf_cpu_context *cpuctx,
60 struct perf_counter_context *ctx, int cpu)
61{
62 return 0;
63}
Thomas Gleixner0793a612008-12-04 20:12:29 +010064
Paul Mackerras4eb96fc2009-01-09 17:24:34 +110065void __weak perf_counter_print_debug(void) { }
66
Ingo Molnar04289bb2008-12-11 08:38:42 +010067static void
68list_add_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
69{
70 struct perf_counter *group_leader = counter->group_leader;
71
72 /*
73 * Depending on whether it is a standalone or sibling counter,
74 * add it straight to the context's counter list, or to the group
75 * leader's sibling list:
76 */
77 if (counter->group_leader == counter)
78 list_add_tail(&counter->list_entry, &ctx->counter_list);
Peter Zijlstra5c148192009-03-25 12:30:23 +010079 else {
Ingo Molnar04289bb2008-12-11 08:38:42 +010080 list_add_tail(&counter->list_entry, &group_leader->sibling_list);
Peter Zijlstra5c148192009-03-25 12:30:23 +010081 group_leader->nr_siblings++;
82 }
Peter Zijlstra592903c2009-03-13 12:21:36 +010083
84 list_add_rcu(&counter->event_entry, &ctx->event_list);
Ingo Molnar04289bb2008-12-11 08:38:42 +010085}
86
87static void
88list_del_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
89{
90 struct perf_counter *sibling, *tmp;
91
92 list_del_init(&counter->list_entry);
Peter Zijlstra592903c2009-03-13 12:21:36 +010093 list_del_rcu(&counter->event_entry);
Ingo Molnar04289bb2008-12-11 08:38:42 +010094
Peter Zijlstra5c148192009-03-25 12:30:23 +010095 if (counter->group_leader != counter)
96 counter->group_leader->nr_siblings--;
97
Ingo Molnar04289bb2008-12-11 08:38:42 +010098 /*
99 * If this was a group counter with sibling counters then
100 * upgrade the siblings to singleton counters by adding them
101 * to the context list directly:
102 */
103 list_for_each_entry_safe(sibling, tmp,
104 &counter->sibling_list, list_entry) {
105
Peter Zijlstra75564232009-03-13 12:21:29 +0100106 list_move_tail(&sibling->list_entry, &ctx->counter_list);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100107 sibling->group_leader = sibling;
108 }
109}
110
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100111static void
112counter_sched_out(struct perf_counter *counter,
113 struct perf_cpu_context *cpuctx,
114 struct perf_counter_context *ctx)
115{
116 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
117 return;
118
119 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200120 counter->tstamp_stopped = ctx->time;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100121 counter->hw_ops->disable(counter);
122 counter->oncpu = -1;
123
124 if (!is_software_counter(counter))
125 cpuctx->active_oncpu--;
126 ctx->nr_active--;
127 if (counter->hw_event.exclusive || !cpuctx->active_oncpu)
128 cpuctx->exclusive = 0;
129}
130
Paul Mackerrasd859e292009-01-17 18:10:22 +1100131static void
132group_sched_out(struct perf_counter *group_counter,
133 struct perf_cpu_context *cpuctx,
134 struct perf_counter_context *ctx)
135{
136 struct perf_counter *counter;
137
138 if (group_counter->state != PERF_COUNTER_STATE_ACTIVE)
139 return;
140
141 counter_sched_out(group_counter, cpuctx, ctx);
142
143 /*
144 * Schedule out siblings (if any):
145 */
146 list_for_each_entry(counter, &group_counter->sibling_list, list_entry)
147 counter_sched_out(counter, cpuctx, ctx);
148
149 if (group_counter->hw_event.exclusive)
150 cpuctx->exclusive = 0;
151}
152
Thomas Gleixner0793a612008-12-04 20:12:29 +0100153/*
154 * Cross CPU call to remove a performance counter
155 *
156 * We disable the counter on the hardware level first. After that we
157 * remove it from the context list.
158 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100159static void __perf_counter_remove_from_context(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100160{
161 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
162 struct perf_counter *counter = info;
163 struct perf_counter_context *ctx = counter->ctx;
Ingo Molnar9b51f662008-12-12 13:49:45 +0100164 unsigned long flags;
Ingo Molnar5c92d122008-12-11 13:21:10 +0100165 u64 perf_flags;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100166
167 /*
168 * If this is a task context, we need to check whether it is
169 * the current task context of this cpu. If not it has been
170 * scheduled out before the smp call arrived.
171 */
172 if (ctx->task && cpuctx->task_ctx != ctx)
173 return;
174
Peter Zijlstra849691a2009-04-06 11:45:12 +0200175 spin_lock_irqsave(&ctx->lock, flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100176
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100177 counter_sched_out(counter, cpuctx, ctx);
178
179 counter->task = NULL;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100180 ctx->nr_counters--;
181
182 /*
183 * Protect the list operation against NMI by disabling the
184 * counters on a global level. NOP for non NMI based counters.
185 */
Ingo Molnar01b28382008-12-11 13:45:51 +0100186 perf_flags = hw_perf_save_disable();
Ingo Molnar04289bb2008-12-11 08:38:42 +0100187 list_del_counter(counter, ctx);
Ingo Molnar01b28382008-12-11 13:45:51 +0100188 hw_perf_restore(perf_flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100189
190 if (!ctx->task) {
191 /*
192 * Allow more per task counters with respect to the
193 * reservation:
194 */
195 cpuctx->max_pertask =
196 min(perf_max_counters - ctx->nr_counters,
197 perf_max_counters - perf_reserved_percpu);
198 }
199
Peter Zijlstra849691a2009-04-06 11:45:12 +0200200 spin_unlock_irqrestore(&ctx->lock, flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100201}
202
203
204/*
205 * Remove the counter from a task's (or a CPU's) list of counters.
206 *
Paul Mackerrasd859e292009-01-17 18:10:22 +1100207 * Must be called with counter->mutex and ctx->mutex held.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100208 *
209 * CPU counters are removed with a smp call. For task counters we only
210 * call when the task is on a CPU.
211 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100212static void perf_counter_remove_from_context(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100213{
214 struct perf_counter_context *ctx = counter->ctx;
215 struct task_struct *task = ctx->task;
216
217 if (!task) {
218 /*
219 * Per cpu counters are removed via an smp call and
220 * the removal is always sucessful.
221 */
222 smp_call_function_single(counter->cpu,
Ingo Molnar04289bb2008-12-11 08:38:42 +0100223 __perf_counter_remove_from_context,
Thomas Gleixner0793a612008-12-04 20:12:29 +0100224 counter, 1);
225 return;
226 }
227
228retry:
Ingo Molnar04289bb2008-12-11 08:38:42 +0100229 task_oncpu_function_call(task, __perf_counter_remove_from_context,
Thomas Gleixner0793a612008-12-04 20:12:29 +0100230 counter);
231
232 spin_lock_irq(&ctx->lock);
233 /*
234 * If the context is active we need to retry the smp call.
235 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100236 if (ctx->nr_active && !list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100237 spin_unlock_irq(&ctx->lock);
238 goto retry;
239 }
240
241 /*
242 * The lock prevents that this context is scheduled in so we
Ingo Molnar04289bb2008-12-11 08:38:42 +0100243 * can remove the counter safely, if the call above did not
Thomas Gleixner0793a612008-12-04 20:12:29 +0100244 * succeed.
245 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100246 if (!list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100247 ctx->nr_counters--;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100248 list_del_counter(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100249 counter->task = NULL;
250 }
251 spin_unlock_irq(&ctx->lock);
252}
253
Peter Zijlstra4af49982009-04-06 11:45:10 +0200254static inline u64 perf_clock(void)
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100255{
Peter Zijlstra4af49982009-04-06 11:45:10 +0200256 return cpu_clock(smp_processor_id());
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100257}
258
259/*
260 * Update the record of the current time in a context.
261 */
Peter Zijlstra4af49982009-04-06 11:45:10 +0200262static void update_context_time(struct perf_counter_context *ctx)
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100263{
Peter Zijlstra4af49982009-04-06 11:45:10 +0200264 u64 now = perf_clock();
265
266 ctx->time += now - ctx->timestamp;
267 ctx->timestamp = now;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100268}
269
270/*
271 * Update the total_time_enabled and total_time_running fields for a counter.
272 */
273static void update_counter_times(struct perf_counter *counter)
274{
275 struct perf_counter_context *ctx = counter->ctx;
276 u64 run_end;
277
Peter Zijlstra4af49982009-04-06 11:45:10 +0200278 if (counter->state < PERF_COUNTER_STATE_INACTIVE)
279 return;
280
281 counter->total_time_enabled = ctx->time - counter->tstamp_enabled;
282
283 if (counter->state == PERF_COUNTER_STATE_INACTIVE)
284 run_end = counter->tstamp_stopped;
285 else
286 run_end = ctx->time;
287
288 counter->total_time_running = run_end - counter->tstamp_running;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100289}
290
291/*
292 * Update total_time_enabled and total_time_running for all counters in a group.
293 */
294static void update_group_times(struct perf_counter *leader)
295{
296 struct perf_counter *counter;
297
298 update_counter_times(leader);
299 list_for_each_entry(counter, &leader->sibling_list, list_entry)
300 update_counter_times(counter);
301}
302
303/*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100304 * Cross CPU call to disable a performance counter
305 */
306static void __perf_counter_disable(void *info)
307{
308 struct perf_counter *counter = info;
309 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
310 struct perf_counter_context *ctx = counter->ctx;
311 unsigned long flags;
312
313 /*
314 * If this is a per-task counter, need to check whether this
315 * counter's task is the current task on this cpu.
316 */
317 if (ctx->task && cpuctx->task_ctx != ctx)
318 return;
319
Peter Zijlstra849691a2009-04-06 11:45:12 +0200320 spin_lock_irqsave(&ctx->lock, flags);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100321
Peter Zijlstrabce379b2009-04-06 11:45:13 +0200322 update_context_time(ctx);
323
Paul Mackerrasd859e292009-01-17 18:10:22 +1100324 /*
325 * If the counter is on, turn it off.
326 * If it is in error state, leave it in error state.
327 */
328 if (counter->state >= PERF_COUNTER_STATE_INACTIVE) {
Peter Zijlstra4af49982009-04-06 11:45:10 +0200329 update_context_time(ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100330 update_counter_times(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100331 if (counter == counter->group_leader)
332 group_sched_out(counter, cpuctx, ctx);
333 else
334 counter_sched_out(counter, cpuctx, ctx);
335 counter->state = PERF_COUNTER_STATE_OFF;
336 }
337
Peter Zijlstra849691a2009-04-06 11:45:12 +0200338 spin_unlock_irqrestore(&ctx->lock, flags);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100339}
340
341/*
342 * Disable a counter.
343 */
344static void perf_counter_disable(struct perf_counter *counter)
345{
346 struct perf_counter_context *ctx = counter->ctx;
347 struct task_struct *task = ctx->task;
348
349 if (!task) {
350 /*
351 * Disable the counter on the cpu that it's on
352 */
353 smp_call_function_single(counter->cpu, __perf_counter_disable,
354 counter, 1);
355 return;
356 }
357
358 retry:
359 task_oncpu_function_call(task, __perf_counter_disable, counter);
360
361 spin_lock_irq(&ctx->lock);
362 /*
363 * If the counter is still active, we need to retry the cross-call.
364 */
365 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
366 spin_unlock_irq(&ctx->lock);
367 goto retry;
368 }
369
370 /*
371 * Since we have the lock this context can't be scheduled
372 * in, so we can change the state safely.
373 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100374 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
375 update_counter_times(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100376 counter->state = PERF_COUNTER_STATE_OFF;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100377 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100378
379 spin_unlock_irq(&ctx->lock);
380}
381
382/*
383 * Disable a counter and all its children.
384 */
385static void perf_counter_disable_family(struct perf_counter *counter)
386{
387 struct perf_counter *child;
388
389 perf_counter_disable(counter);
390
391 /*
392 * Lock the mutex to protect the list of children
393 */
394 mutex_lock(&counter->mutex);
395 list_for_each_entry(child, &counter->child_list, child_list)
396 perf_counter_disable(child);
397 mutex_unlock(&counter->mutex);
398}
399
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100400static int
401counter_sched_in(struct perf_counter *counter,
402 struct perf_cpu_context *cpuctx,
403 struct perf_counter_context *ctx,
404 int cpu)
405{
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100406 if (counter->state <= PERF_COUNTER_STATE_OFF)
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100407 return 0;
408
409 counter->state = PERF_COUNTER_STATE_ACTIVE;
410 counter->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */
411 /*
412 * The new state must be visible before we turn it on in the hardware:
413 */
414 smp_wmb();
415
416 if (counter->hw_ops->enable(counter)) {
417 counter->state = PERF_COUNTER_STATE_INACTIVE;
418 counter->oncpu = -1;
419 return -EAGAIN;
420 }
421
Peter Zijlstra4af49982009-04-06 11:45:10 +0200422 counter->tstamp_running += ctx->time - counter->tstamp_stopped;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100423
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100424 if (!is_software_counter(counter))
425 cpuctx->active_oncpu++;
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100426 ctx->nr_active++;
427
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100428 if (counter->hw_event.exclusive)
429 cpuctx->exclusive = 1;
430
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100431 return 0;
432}
433
Thomas Gleixner0793a612008-12-04 20:12:29 +0100434/*
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100435 * Return 1 for a group consisting entirely of software counters,
436 * 0 if the group contains any hardware counters.
437 */
438static int is_software_only_group(struct perf_counter *leader)
439{
440 struct perf_counter *counter;
441
442 if (!is_software_counter(leader))
443 return 0;
Peter Zijlstra5c148192009-03-25 12:30:23 +0100444
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100445 list_for_each_entry(counter, &leader->sibling_list, list_entry)
446 if (!is_software_counter(counter))
447 return 0;
Peter Zijlstra5c148192009-03-25 12:30:23 +0100448
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100449 return 1;
450}
451
452/*
453 * Work out whether we can put this counter group on the CPU now.
454 */
455static int group_can_go_on(struct perf_counter *counter,
456 struct perf_cpu_context *cpuctx,
457 int can_add_hw)
458{
459 /*
460 * Groups consisting entirely of software counters can always go on.
461 */
462 if (is_software_only_group(counter))
463 return 1;
464 /*
465 * If an exclusive group is already on, no other hardware
466 * counters can go on.
467 */
468 if (cpuctx->exclusive)
469 return 0;
470 /*
471 * If this group is exclusive and there are already
472 * counters on the CPU, it can't go on.
473 */
474 if (counter->hw_event.exclusive && cpuctx->active_oncpu)
475 return 0;
476 /*
477 * Otherwise, try to add it if all previous groups were able
478 * to go on.
479 */
480 return can_add_hw;
481}
482
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100483static void add_counter_to_ctx(struct perf_counter *counter,
484 struct perf_counter_context *ctx)
485{
486 list_add_counter(counter, ctx);
487 ctx->nr_counters++;
488 counter->prev_state = PERF_COUNTER_STATE_OFF;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200489 counter->tstamp_enabled = ctx->time;
490 counter->tstamp_running = ctx->time;
491 counter->tstamp_stopped = ctx->time;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100492}
493
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100494/*
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100495 * Cross CPU call to install and enable a performance counter
Thomas Gleixner0793a612008-12-04 20:12:29 +0100496 */
497static void __perf_install_in_context(void *info)
498{
499 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
500 struct perf_counter *counter = info;
501 struct perf_counter_context *ctx = counter->ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100502 struct perf_counter *leader = counter->group_leader;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100503 int cpu = smp_processor_id();
Ingo Molnar9b51f662008-12-12 13:49:45 +0100504 unsigned long flags;
Ingo Molnar5c92d122008-12-11 13:21:10 +0100505 u64 perf_flags;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100506 int err;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100507
508 /*
509 * If this is a task context, we need to check whether it is
510 * the current task context of this cpu. If not it has been
511 * scheduled out before the smp call arrived.
512 */
513 if (ctx->task && cpuctx->task_ctx != ctx)
514 return;
515
Peter Zijlstra849691a2009-04-06 11:45:12 +0200516 spin_lock_irqsave(&ctx->lock, flags);
Peter Zijlstra4af49982009-04-06 11:45:10 +0200517 update_context_time(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100518
519 /*
520 * Protect the list operation against NMI by disabling the
521 * counters on a global level. NOP for non NMI based counters.
522 */
Ingo Molnar01b28382008-12-11 13:45:51 +0100523 perf_flags = hw_perf_save_disable();
Thomas Gleixner0793a612008-12-04 20:12:29 +0100524
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100525 add_counter_to_ctx(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100526
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100527 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100528 * Don't put the counter on if it is disabled or if
529 * it is in a group and the group isn't on.
530 */
531 if (counter->state != PERF_COUNTER_STATE_INACTIVE ||
532 (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE))
533 goto unlock;
534
535 /*
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100536 * An exclusive counter can't go on if there are already active
537 * hardware counters, and no hardware counter can go on if there
538 * is already an exclusive counter on.
539 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100540 if (!group_can_go_on(counter, cpuctx, 1))
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100541 err = -EEXIST;
542 else
543 err = counter_sched_in(counter, cpuctx, ctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100544
Paul Mackerrasd859e292009-01-17 18:10:22 +1100545 if (err) {
546 /*
547 * This counter couldn't go on. If it is in a group
548 * then we have to pull the whole group off.
549 * If the counter group is pinned then put it in error state.
550 */
551 if (leader != counter)
552 group_sched_out(leader, cpuctx, ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100553 if (leader->hw_event.pinned) {
554 update_group_times(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100555 leader->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100556 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100557 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100558
559 if (!err && !ctx->task && cpuctx->max_pertask)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100560 cpuctx->max_pertask--;
561
Paul Mackerrasd859e292009-01-17 18:10:22 +1100562 unlock:
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100563 hw_perf_restore(perf_flags);
564
Peter Zijlstra849691a2009-04-06 11:45:12 +0200565 spin_unlock_irqrestore(&ctx->lock, flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100566}
567
568/*
569 * Attach a performance counter to a context
570 *
571 * First we add the counter to the list with the hardware enable bit
572 * in counter->hw_config cleared.
573 *
574 * If the counter is attached to a task which is on a CPU we use a smp
575 * call to enable it in the task context. The task might have been
576 * scheduled away, but we check this in the smp call again.
Paul Mackerrasd859e292009-01-17 18:10:22 +1100577 *
578 * Must be called with ctx->mutex held.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100579 */
580static void
581perf_install_in_context(struct perf_counter_context *ctx,
582 struct perf_counter *counter,
583 int cpu)
584{
585 struct task_struct *task = ctx->task;
586
Thomas Gleixner0793a612008-12-04 20:12:29 +0100587 if (!task) {
588 /*
589 * Per cpu counters are installed via an smp call and
590 * the install is always sucessful.
591 */
592 smp_call_function_single(cpu, __perf_install_in_context,
593 counter, 1);
594 return;
595 }
596
597 counter->task = task;
598retry:
599 task_oncpu_function_call(task, __perf_install_in_context,
600 counter);
601
602 spin_lock_irq(&ctx->lock);
603 /*
Thomas Gleixner0793a612008-12-04 20:12:29 +0100604 * we need to retry the smp call.
605 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100606 if (ctx->is_active && list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100607 spin_unlock_irq(&ctx->lock);
608 goto retry;
609 }
610
611 /*
612 * The lock prevents that this context is scheduled in so we
613 * can add the counter safely, if it the call above did not
614 * succeed.
615 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100616 if (list_empty(&counter->list_entry))
617 add_counter_to_ctx(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100618 spin_unlock_irq(&ctx->lock);
619}
620
Paul Mackerrasd859e292009-01-17 18:10:22 +1100621/*
622 * Cross CPU call to enable a performance counter
623 */
624static void __perf_counter_enable(void *info)
Ingo Molnar04289bb2008-12-11 08:38:42 +0100625{
Paul Mackerrasd859e292009-01-17 18:10:22 +1100626 struct perf_counter *counter = info;
627 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
628 struct perf_counter_context *ctx = counter->ctx;
629 struct perf_counter *leader = counter->group_leader;
630 unsigned long flags;
631 int err;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100632
633 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100634 * If this is a per-task counter, need to check whether this
635 * counter's task is the current task on this cpu.
Ingo Molnar04289bb2008-12-11 08:38:42 +0100636 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100637 if (ctx->task && cpuctx->task_ctx != ctx)
638 return;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100639
Peter Zijlstra849691a2009-04-06 11:45:12 +0200640 spin_lock_irqsave(&ctx->lock, flags);
Peter Zijlstra4af49982009-04-06 11:45:10 +0200641 update_context_time(ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100642
Paul Mackerrasc07c99b2009-02-13 22:10:34 +1100643 counter->prev_state = counter->state;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100644 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
645 goto unlock;
646 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200647 counter->tstamp_enabled = ctx->time - counter->total_time_enabled;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100648
649 /*
650 * If the counter is in a group and isn't the group leader,
651 * then don't put it on unless the group is on.
652 */
653 if (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE)
654 goto unlock;
655
656 if (!group_can_go_on(counter, cpuctx, 1))
657 err = -EEXIST;
658 else
659 err = counter_sched_in(counter, cpuctx, ctx,
660 smp_processor_id());
661
662 if (err) {
663 /*
664 * If this counter can't go on and it's part of a
665 * group, then the whole group has to come off.
666 */
667 if (leader != counter)
668 group_sched_out(leader, cpuctx, ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100669 if (leader->hw_event.pinned) {
670 update_group_times(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100671 leader->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100672 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100673 }
674
675 unlock:
Peter Zijlstra849691a2009-04-06 11:45:12 +0200676 spin_unlock_irqrestore(&ctx->lock, flags);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100677}
678
679/*
680 * Enable a counter.
681 */
682static void perf_counter_enable(struct perf_counter *counter)
683{
684 struct perf_counter_context *ctx = counter->ctx;
685 struct task_struct *task = ctx->task;
686
687 if (!task) {
688 /*
689 * Enable the counter on the cpu that it's on
690 */
691 smp_call_function_single(counter->cpu, __perf_counter_enable,
692 counter, 1);
693 return;
694 }
695
696 spin_lock_irq(&ctx->lock);
697 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
698 goto out;
699
700 /*
701 * If the counter is in error state, clear that first.
702 * That way, if we see the counter in error state below, we
703 * know that it has gone back into error state, as distinct
704 * from the task having been scheduled away before the
705 * cross-call arrived.
706 */
707 if (counter->state == PERF_COUNTER_STATE_ERROR)
708 counter->state = PERF_COUNTER_STATE_OFF;
709
710 retry:
711 spin_unlock_irq(&ctx->lock);
712 task_oncpu_function_call(task, __perf_counter_enable, counter);
713
714 spin_lock_irq(&ctx->lock);
715
716 /*
717 * If the context is active and the counter is still off,
718 * we need to retry the cross-call.
719 */
720 if (ctx->is_active && counter->state == PERF_COUNTER_STATE_OFF)
721 goto retry;
722
723 /*
724 * Since we have the lock this context can't be scheduled
725 * in, so we can change the state safely.
726 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100727 if (counter->state == PERF_COUNTER_STATE_OFF) {
Paul Mackerrasd859e292009-01-17 18:10:22 +1100728 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200729 counter->tstamp_enabled =
730 ctx->time - counter->total_time_enabled;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100731 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100732 out:
733 spin_unlock_irq(&ctx->lock);
734}
735
Peter Zijlstra79f14642009-04-06 11:45:07 +0200736static void perf_counter_refresh(struct perf_counter *counter, int refresh)
737{
738 atomic_add(refresh, &counter->event_limit);
739 perf_counter_enable(counter);
740}
741
Paul Mackerrasd859e292009-01-17 18:10:22 +1100742/*
743 * Enable a counter and all its children.
744 */
745static void perf_counter_enable_family(struct perf_counter *counter)
746{
747 struct perf_counter *child;
748
749 perf_counter_enable(counter);
750
751 /*
752 * Lock the mutex to protect the list of children
753 */
754 mutex_lock(&counter->mutex);
755 list_for_each_entry(child, &counter->child_list, child_list)
756 perf_counter_enable(child);
757 mutex_unlock(&counter->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100758}
759
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100760void __perf_counter_sched_out(struct perf_counter_context *ctx,
761 struct perf_cpu_context *cpuctx)
762{
763 struct perf_counter *counter;
Paul Mackerras3cbed422009-01-09 16:43:42 +1100764 u64 flags;
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100765
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100766 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100767 ctx->is_active = 0;
768 if (likely(!ctx->nr_counters))
769 goto out;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200770 update_context_time(ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100771
Paul Mackerras3cbed422009-01-09 16:43:42 +1100772 flags = hw_perf_save_disable();
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100773 if (ctx->nr_active) {
774 list_for_each_entry(counter, &ctx->counter_list, list_entry)
775 group_sched_out(counter, cpuctx, ctx);
776 }
Paul Mackerras3cbed422009-01-09 16:43:42 +1100777 hw_perf_restore(flags);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100778 out:
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100779 spin_unlock(&ctx->lock);
780}
781
Thomas Gleixner0793a612008-12-04 20:12:29 +0100782/*
783 * Called from scheduler to remove the counters of the current task,
784 * with interrupts disabled.
785 *
786 * We stop each counter and update the counter value in counter->count.
787 *
Ingo Molnar76715812008-12-17 14:20:28 +0100788 * This does not protect us against NMI, but disable()
Thomas Gleixner0793a612008-12-04 20:12:29 +0100789 * sets the disabled bit in the control field of counter _before_
790 * accessing the counter control register. If a NMI hits, then it will
791 * not restart the counter.
792 */
793void perf_counter_task_sched_out(struct task_struct *task, int cpu)
794{
795 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
796 struct perf_counter_context *ctx = &task->perf_counter_ctx;
Peter Zijlstra4a0deca2009-03-19 20:26:12 +0100797 struct pt_regs *regs;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100798
799 if (likely(!cpuctx->task_ctx))
800 return;
801
Peter Zijlstrabce379b2009-04-06 11:45:13 +0200802 update_context_time(ctx);
803
Peter Zijlstra4a0deca2009-03-19 20:26:12 +0100804 regs = task_pt_regs(task);
805 perf_swcounter_event(PERF_COUNT_CONTEXT_SWITCHES, 1, 1, regs);
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100806 __perf_counter_sched_out(ctx, cpuctx);
807
Thomas Gleixner0793a612008-12-04 20:12:29 +0100808 cpuctx->task_ctx = NULL;
809}
810
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100811static void perf_counter_cpu_sched_out(struct perf_cpu_context *cpuctx)
Ingo Molnar04289bb2008-12-11 08:38:42 +0100812{
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100813 __perf_counter_sched_out(&cpuctx->ctx, cpuctx);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100814}
815
Ingo Molnar79958882008-12-17 08:54:56 +0100816static int
Ingo Molnar04289bb2008-12-11 08:38:42 +0100817group_sched_in(struct perf_counter *group_counter,
818 struct perf_cpu_context *cpuctx,
819 struct perf_counter_context *ctx,
820 int cpu)
821{
Ingo Molnar95cdd2e2008-12-21 13:50:42 +0100822 struct perf_counter *counter, *partial_group;
Paul Mackerras3cbed422009-01-09 16:43:42 +1100823 int ret;
824
825 if (group_counter->state == PERF_COUNTER_STATE_OFF)
826 return 0;
827
828 ret = hw_perf_group_sched_in(group_counter, cpuctx, ctx, cpu);
829 if (ret)
830 return ret < 0 ? ret : 0;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100831
Paul Mackerrasc07c99b2009-02-13 22:10:34 +1100832 group_counter->prev_state = group_counter->state;
Ingo Molnar95cdd2e2008-12-21 13:50:42 +0100833 if (counter_sched_in(group_counter, cpuctx, ctx, cpu))
834 return -EAGAIN;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100835
836 /*
837 * Schedule in siblings as one group (if any):
838 */
Ingo Molnar79958882008-12-17 08:54:56 +0100839 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
Paul Mackerrasc07c99b2009-02-13 22:10:34 +1100840 counter->prev_state = counter->state;
Ingo Molnar95cdd2e2008-12-21 13:50:42 +0100841 if (counter_sched_in(counter, cpuctx, ctx, cpu)) {
842 partial_group = counter;
843 goto group_error;
844 }
Ingo Molnar79958882008-12-17 08:54:56 +0100845 }
846
Paul Mackerras3cbed422009-01-09 16:43:42 +1100847 return 0;
Ingo Molnar95cdd2e2008-12-21 13:50:42 +0100848
849group_error:
850 /*
851 * Groups can be scheduled in as one unit only, so undo any
852 * partial group before returning:
853 */
854 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
855 if (counter == partial_group)
856 break;
857 counter_sched_out(counter, cpuctx, ctx);
858 }
859 counter_sched_out(group_counter, cpuctx, ctx);
860
861 return -EAGAIN;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100862}
863
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100864static void
865__perf_counter_sched_in(struct perf_counter_context *ctx,
866 struct perf_cpu_context *cpuctx, int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100867{
Thomas Gleixner0793a612008-12-04 20:12:29 +0100868 struct perf_counter *counter;
Paul Mackerras3cbed422009-01-09 16:43:42 +1100869 u64 flags;
Paul Mackerrasdd0e6ba2009-01-12 15:11:00 +1100870 int can_add_hw = 1;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100871
Thomas Gleixner0793a612008-12-04 20:12:29 +0100872 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100873 ctx->is_active = 1;
874 if (likely(!ctx->nr_counters))
875 goto out;
876
Peter Zijlstra4af49982009-04-06 11:45:10 +0200877 ctx->timestamp = perf_clock();
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100878
Paul Mackerras3cbed422009-01-09 16:43:42 +1100879 flags = hw_perf_save_disable();
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100880
881 /*
882 * First go through the list and put on any pinned groups
883 * in order to give them the best chance of going on.
884 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100885 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100886 if (counter->state <= PERF_COUNTER_STATE_OFF ||
887 !counter->hw_event.pinned)
888 continue;
889 if (counter->cpu != -1 && counter->cpu != cpu)
890 continue;
891
892 if (group_can_go_on(counter, cpuctx, 1))
893 group_sched_in(counter, cpuctx, ctx, cpu);
894
895 /*
896 * If this pinned group hasn't been scheduled,
897 * put it in error state.
898 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100899 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
900 update_group_times(counter);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100901 counter->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100902 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100903 }
904
905 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
906 /*
907 * Ignore counters in OFF or ERROR state, and
908 * ignore pinned counters since we did them already.
909 */
910 if (counter->state <= PERF_COUNTER_STATE_OFF ||
911 counter->hw_event.pinned)
912 continue;
913
Ingo Molnar04289bb2008-12-11 08:38:42 +0100914 /*
915 * Listen to the 'cpu' scheduling filter constraint
916 * of counters:
917 */
Thomas Gleixner0793a612008-12-04 20:12:29 +0100918 if (counter->cpu != -1 && counter->cpu != cpu)
919 continue;
920
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100921 if (group_can_go_on(counter, cpuctx, can_add_hw)) {
Paul Mackerrasdd0e6ba2009-01-12 15:11:00 +1100922 if (group_sched_in(counter, cpuctx, ctx, cpu))
923 can_add_hw = 0;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100924 }
Thomas Gleixner0793a612008-12-04 20:12:29 +0100925 }
Paul Mackerras3cbed422009-01-09 16:43:42 +1100926 hw_perf_restore(flags);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100927 out:
Thomas Gleixner0793a612008-12-04 20:12:29 +0100928 spin_unlock(&ctx->lock);
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100929}
Ingo Molnar04289bb2008-12-11 08:38:42 +0100930
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100931/*
932 * Called from scheduler to add the counters of the current task
933 * with interrupts disabled.
934 *
935 * We restore the counter value and then enable it.
936 *
937 * This does not protect us against NMI, but enable()
938 * sets the enabled bit in the control field of counter _before_
939 * accessing the counter control register. If a NMI hits, then it will
940 * keep the counter running.
941 */
942void perf_counter_task_sched_in(struct task_struct *task, int cpu)
943{
944 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
945 struct perf_counter_context *ctx = &task->perf_counter_ctx;
946
947 __perf_counter_sched_in(ctx, cpuctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100948 cpuctx->task_ctx = ctx;
949}
950
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100951static void perf_counter_cpu_sched_in(struct perf_cpu_context *cpuctx, int cpu)
952{
953 struct perf_counter_context *ctx = &cpuctx->ctx;
954
955 __perf_counter_sched_in(ctx, cpuctx, cpu);
956}
957
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100958int perf_counter_task_disable(void)
959{
960 struct task_struct *curr = current;
961 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
962 struct perf_counter *counter;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100963 unsigned long flags;
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100964 u64 perf_flags;
965 int cpu;
966
967 if (likely(!ctx->nr_counters))
968 return 0;
969
Peter Zijlstra849691a2009-04-06 11:45:12 +0200970 local_irq_save(flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100971 cpu = smp_processor_id();
972
973 perf_counter_task_sched_out(curr, cpu);
974
975 spin_lock(&ctx->lock);
976
977 /*
978 * Disable all the counters:
979 */
980 perf_flags = hw_perf_save_disable();
981
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100982 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100983 if (counter->state != PERF_COUNTER_STATE_ERROR) {
984 update_group_times(counter);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100985 counter->state = PERF_COUNTER_STATE_OFF;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100986 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100987 }
Ingo Molnar9b51f662008-12-12 13:49:45 +0100988
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100989 hw_perf_restore(perf_flags);
990
Peter Zijlstra849691a2009-04-06 11:45:12 +0200991 spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100992
993 return 0;
994}
995
996int perf_counter_task_enable(void)
997{
998 struct task_struct *curr = current;
999 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
1000 struct perf_counter *counter;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001001 unsigned long flags;
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001002 u64 perf_flags;
1003 int cpu;
1004
1005 if (likely(!ctx->nr_counters))
1006 return 0;
1007
Peter Zijlstra849691a2009-04-06 11:45:12 +02001008 local_irq_save(flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001009 cpu = smp_processor_id();
1010
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001011 perf_counter_task_sched_out(curr, cpu);
1012
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001013 spin_lock(&ctx->lock);
1014
1015 /*
1016 * Disable all the counters:
1017 */
1018 perf_flags = hw_perf_save_disable();
1019
1020 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001021 if (counter->state > PERF_COUNTER_STATE_OFF)
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001022 continue;
Ingo Molnar6a930702008-12-11 15:17:03 +01001023 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +02001024 counter->tstamp_enabled =
1025 ctx->time - counter->total_time_enabled;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001026 counter->hw_event.disabled = 0;
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001027 }
1028 hw_perf_restore(perf_flags);
1029
1030 spin_unlock(&ctx->lock);
1031
1032 perf_counter_task_sched_in(curr, cpu);
1033
Peter Zijlstra849691a2009-04-06 11:45:12 +02001034 local_irq_restore(flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001035
1036 return 0;
1037}
1038
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001039/*
1040 * Round-robin a context's counters:
1041 */
1042static void rotate_ctx(struct perf_counter_context *ctx)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001043{
Thomas Gleixner0793a612008-12-04 20:12:29 +01001044 struct perf_counter *counter;
Ingo Molnar5c92d122008-12-11 13:21:10 +01001045 u64 perf_flags;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001046
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001047 if (!ctx->nr_counters)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001048 return;
1049
Thomas Gleixner0793a612008-12-04 20:12:29 +01001050 spin_lock(&ctx->lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001051 /*
Ingo Molnar04289bb2008-12-11 08:38:42 +01001052 * Rotate the first entry last (works just fine for group counters too):
Thomas Gleixner0793a612008-12-04 20:12:29 +01001053 */
Ingo Molnar01b28382008-12-11 13:45:51 +01001054 perf_flags = hw_perf_save_disable();
Ingo Molnar04289bb2008-12-11 08:38:42 +01001055 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Peter Zijlstra75564232009-03-13 12:21:29 +01001056 list_move_tail(&counter->list_entry, &ctx->counter_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001057 break;
1058 }
Ingo Molnar01b28382008-12-11 13:45:51 +01001059 hw_perf_restore(perf_flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001060
1061 spin_unlock(&ctx->lock);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001062}
Thomas Gleixner0793a612008-12-04 20:12:29 +01001063
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001064void perf_counter_task_tick(struct task_struct *curr, int cpu)
1065{
1066 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
1067 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
1068 const int rotate_percpu = 0;
1069
1070 if (rotate_percpu)
1071 perf_counter_cpu_sched_out(cpuctx);
1072 perf_counter_task_sched_out(curr, cpu);
1073
1074 if (rotate_percpu)
1075 rotate_ctx(&cpuctx->ctx);
1076 rotate_ctx(ctx);
1077
1078 if (rotate_percpu)
1079 perf_counter_cpu_sched_in(cpuctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001080 perf_counter_task_sched_in(curr, cpu);
1081}
1082
1083/*
Thomas Gleixner0793a612008-12-04 20:12:29 +01001084 * Cross CPU call to read the hardware counter
1085 */
Ingo Molnar76715812008-12-17 14:20:28 +01001086static void __read(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001087{
Ingo Molnar621a01e2008-12-11 12:46:46 +01001088 struct perf_counter *counter = info;
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001089 struct perf_counter_context *ctx = counter->ctx;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001090 unsigned long flags;
Ingo Molnar621a01e2008-12-11 12:46:46 +01001091
Peter Zijlstra849691a2009-04-06 11:45:12 +02001092 local_irq_save(flags);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001093 if (ctx->is_active)
Peter Zijlstra4af49982009-04-06 11:45:10 +02001094 update_context_time(ctx);
Ingo Molnar76715812008-12-17 14:20:28 +01001095 counter->hw_ops->read(counter);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001096 update_counter_times(counter);
Peter Zijlstra849691a2009-04-06 11:45:12 +02001097 local_irq_restore(flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001098}
1099
Ingo Molnar04289bb2008-12-11 08:38:42 +01001100static u64 perf_counter_read(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001101{
1102 /*
1103 * If counter is enabled and currently active on a CPU, update the
1104 * value in the counter structure:
1105 */
Ingo Molnar6a930702008-12-11 15:17:03 +01001106 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
Thomas Gleixner0793a612008-12-04 20:12:29 +01001107 smp_call_function_single(counter->oncpu,
Ingo Molnar76715812008-12-17 14:20:28 +01001108 __read, counter, 1);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001109 } else if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
1110 update_counter_times(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001111 }
1112
Ingo Molnaree060942008-12-13 09:00:03 +01001113 return atomic64_read(&counter->count);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001114}
1115
Thomas Gleixner0793a612008-12-04 20:12:29 +01001116static void put_context(struct perf_counter_context *ctx)
1117{
1118 if (ctx->task)
1119 put_task_struct(ctx->task);
1120}
1121
1122static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
1123{
1124 struct perf_cpu_context *cpuctx;
1125 struct perf_counter_context *ctx;
1126 struct task_struct *task;
1127
1128 /*
1129 * If cpu is not a wildcard then this is a percpu counter:
1130 */
1131 if (cpu != -1) {
1132 /* Must be root to operate on a CPU counter: */
1133 if (!capable(CAP_SYS_ADMIN))
1134 return ERR_PTR(-EACCES);
1135
1136 if (cpu < 0 || cpu > num_possible_cpus())
1137 return ERR_PTR(-EINVAL);
1138
1139 /*
1140 * We could be clever and allow to attach a counter to an
1141 * offline CPU and activate it when the CPU comes up, but
1142 * that's for later.
1143 */
1144 if (!cpu_isset(cpu, cpu_online_map))
1145 return ERR_PTR(-ENODEV);
1146
1147 cpuctx = &per_cpu(perf_cpu_context, cpu);
1148 ctx = &cpuctx->ctx;
1149
Thomas Gleixner0793a612008-12-04 20:12:29 +01001150 return ctx;
1151 }
1152
1153 rcu_read_lock();
1154 if (!pid)
1155 task = current;
1156 else
1157 task = find_task_by_vpid(pid);
1158 if (task)
1159 get_task_struct(task);
1160 rcu_read_unlock();
1161
1162 if (!task)
1163 return ERR_PTR(-ESRCH);
1164
1165 ctx = &task->perf_counter_ctx;
1166 ctx->task = task;
1167
1168 /* Reuse ptrace permission checks for now. */
1169 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
1170 put_context(ctx);
1171 return ERR_PTR(-EACCES);
1172 }
1173
1174 return ctx;
1175}
1176
Peter Zijlstra592903c2009-03-13 12:21:36 +01001177static void free_counter_rcu(struct rcu_head *head)
1178{
1179 struct perf_counter *counter;
1180
1181 counter = container_of(head, struct perf_counter, rcu_head);
1182 kfree(counter);
1183}
1184
Peter Zijlstra925d5192009-03-30 19:07:02 +02001185static void perf_pending_sync(struct perf_counter *counter);
1186
Peter Zijlstraf1600952009-03-19 20:26:16 +01001187static void free_counter(struct perf_counter *counter)
1188{
Peter Zijlstra925d5192009-03-30 19:07:02 +02001189 perf_pending_sync(counter);
1190
Peter Zijlstrae077df42009-03-19 20:26:17 +01001191 if (counter->destroy)
1192 counter->destroy(counter);
1193
Peter Zijlstraf1600952009-03-19 20:26:16 +01001194 call_rcu(&counter->rcu_head, free_counter_rcu);
1195}
1196
Thomas Gleixner0793a612008-12-04 20:12:29 +01001197/*
1198 * Called when the last reference to the file is gone.
1199 */
1200static int perf_release(struct inode *inode, struct file *file)
1201{
1202 struct perf_counter *counter = file->private_data;
1203 struct perf_counter_context *ctx = counter->ctx;
1204
1205 file->private_data = NULL;
1206
Paul Mackerrasd859e292009-01-17 18:10:22 +11001207 mutex_lock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001208 mutex_lock(&counter->mutex);
1209
Ingo Molnar04289bb2008-12-11 08:38:42 +01001210 perf_counter_remove_from_context(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001211
1212 mutex_unlock(&counter->mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001213 mutex_unlock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001214
Peter Zijlstraf1600952009-03-19 20:26:16 +01001215 free_counter(counter);
Mike Galbraith5af75912009-02-11 10:53:37 +01001216 put_context(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001217
1218 return 0;
1219}
1220
1221/*
1222 * Read the performance counter - simple non blocking version for now
1223 */
1224static ssize_t
1225perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
1226{
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001227 u64 values[3];
1228 int n;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001229
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001230 /*
1231 * Return end-of-file for a read on a counter that is in
1232 * error state (i.e. because it was pinned but it couldn't be
1233 * scheduled on to the CPU at some point).
1234 */
1235 if (counter->state == PERF_COUNTER_STATE_ERROR)
1236 return 0;
1237
Thomas Gleixner0793a612008-12-04 20:12:29 +01001238 mutex_lock(&counter->mutex);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001239 values[0] = perf_counter_read(counter);
1240 n = 1;
1241 if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1242 values[n++] = counter->total_time_enabled +
1243 atomic64_read(&counter->child_total_time_enabled);
1244 if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1245 values[n++] = counter->total_time_running +
1246 atomic64_read(&counter->child_total_time_running);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001247 mutex_unlock(&counter->mutex);
1248
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001249 if (count < n * sizeof(u64))
1250 return -EINVAL;
1251 count = n * sizeof(u64);
1252
1253 if (copy_to_user(buf, values, count))
1254 return -EFAULT;
1255
1256 return count;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001257}
1258
1259static ssize_t
Thomas Gleixner0793a612008-12-04 20:12:29 +01001260perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
1261{
1262 struct perf_counter *counter = file->private_data;
1263
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001264 return perf_read_hw(counter, buf, count);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001265}
1266
1267static unsigned int perf_poll(struct file *file, poll_table *wait)
1268{
1269 struct perf_counter *counter = file->private_data;
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001270 struct perf_mmap_data *data;
1271 unsigned int events;
1272
1273 rcu_read_lock();
1274 data = rcu_dereference(counter->data);
1275 if (data)
1276 events = atomic_xchg(&data->wakeup, 0);
1277 else
1278 events = POLL_HUP;
1279 rcu_read_unlock();
Thomas Gleixner0793a612008-12-04 20:12:29 +01001280
1281 poll_wait(file, &counter->waitq, wait);
1282
Thomas Gleixner0793a612008-12-04 20:12:29 +01001283 return events;
1284}
1285
Paul Mackerrasd859e292009-01-17 18:10:22 +11001286static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1287{
1288 struct perf_counter *counter = file->private_data;
1289 int err = 0;
1290
1291 switch (cmd) {
1292 case PERF_COUNTER_IOC_ENABLE:
1293 perf_counter_enable_family(counter);
1294 break;
1295 case PERF_COUNTER_IOC_DISABLE:
1296 perf_counter_disable_family(counter);
1297 break;
Peter Zijlstra79f14642009-04-06 11:45:07 +02001298 case PERF_COUNTER_IOC_REFRESH:
1299 perf_counter_refresh(counter, arg);
1300 break;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001301 default:
1302 err = -ENOTTY;
1303 }
1304 return err;
1305}
1306
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001307/*
1308 * Callers need to ensure there can be no nesting of this function, otherwise
1309 * the seqlock logic goes bad. We can not serialize this because the arch
1310 * code calls this from NMI context.
1311 */
1312void perf_counter_update_userpage(struct perf_counter *counter)
Paul Mackerras37d81822009-03-23 18:22:08 +01001313{
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001314 struct perf_mmap_data *data;
1315 struct perf_counter_mmap_page *userpg;
1316
1317 rcu_read_lock();
1318 data = rcu_dereference(counter->data);
1319 if (!data)
1320 goto unlock;
1321
1322 userpg = data->user_page;
Paul Mackerras37d81822009-03-23 18:22:08 +01001323
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001324 /*
1325 * Disable preemption so as to not let the corresponding user-space
1326 * spin too long if we get preempted.
1327 */
1328 preempt_disable();
Paul Mackerras37d81822009-03-23 18:22:08 +01001329 ++userpg->lock;
Peter Zijlstra92f22a32009-04-02 11:12:04 +02001330 barrier();
Paul Mackerras37d81822009-03-23 18:22:08 +01001331 userpg->index = counter->hw.idx;
1332 userpg->offset = atomic64_read(&counter->count);
1333 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
1334 userpg->offset -= atomic64_read(&counter->hw.prev_count);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001335
Peter Zijlstra92f22a32009-04-02 11:12:04 +02001336 barrier();
Paul Mackerras37d81822009-03-23 18:22:08 +01001337 ++userpg->lock;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001338 preempt_enable();
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001339unlock:
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001340 rcu_read_unlock();
Paul Mackerras37d81822009-03-23 18:22:08 +01001341}
1342
1343static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1344{
1345 struct perf_counter *counter = vma->vm_file->private_data;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001346 struct perf_mmap_data *data;
1347 int ret = VM_FAULT_SIGBUS;
Paul Mackerras37d81822009-03-23 18:22:08 +01001348
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001349 rcu_read_lock();
1350 data = rcu_dereference(counter->data);
1351 if (!data)
1352 goto unlock;
Paul Mackerras37d81822009-03-23 18:22:08 +01001353
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001354 if (vmf->pgoff == 0) {
1355 vmf->page = virt_to_page(data->user_page);
1356 } else {
1357 int nr = vmf->pgoff - 1;
1358
1359 if ((unsigned)nr > data->nr_pages)
1360 goto unlock;
1361
1362 vmf->page = virt_to_page(data->data_pages[nr]);
1363 }
Paul Mackerras37d81822009-03-23 18:22:08 +01001364 get_page(vmf->page);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001365 ret = 0;
1366unlock:
1367 rcu_read_unlock();
1368
1369 return ret;
1370}
1371
1372static int perf_mmap_data_alloc(struct perf_counter *counter, int nr_pages)
1373{
1374 struct perf_mmap_data *data;
1375 unsigned long size;
1376 int i;
1377
1378 WARN_ON(atomic_read(&counter->mmap_count));
1379
1380 size = sizeof(struct perf_mmap_data);
1381 size += nr_pages * sizeof(void *);
1382
1383 data = kzalloc(size, GFP_KERNEL);
1384 if (!data)
1385 goto fail;
1386
1387 data->user_page = (void *)get_zeroed_page(GFP_KERNEL);
1388 if (!data->user_page)
1389 goto fail_user_page;
1390
1391 for (i = 0; i < nr_pages; i++) {
1392 data->data_pages[i] = (void *)get_zeroed_page(GFP_KERNEL);
1393 if (!data->data_pages[i])
1394 goto fail_data_pages;
1395 }
1396
1397 data->nr_pages = nr_pages;
1398
1399 rcu_assign_pointer(counter->data, data);
1400
Paul Mackerras37d81822009-03-23 18:22:08 +01001401 return 0;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001402
1403fail_data_pages:
1404 for (i--; i >= 0; i--)
1405 free_page((unsigned long)data->data_pages[i]);
1406
1407 free_page((unsigned long)data->user_page);
1408
1409fail_user_page:
1410 kfree(data);
1411
1412fail:
1413 return -ENOMEM;
1414}
1415
1416static void __perf_mmap_data_free(struct rcu_head *rcu_head)
1417{
1418 struct perf_mmap_data *data = container_of(rcu_head,
1419 struct perf_mmap_data, rcu_head);
1420 int i;
1421
1422 free_page((unsigned long)data->user_page);
1423 for (i = 0; i < data->nr_pages; i++)
1424 free_page((unsigned long)data->data_pages[i]);
1425 kfree(data);
1426}
1427
1428static void perf_mmap_data_free(struct perf_counter *counter)
1429{
1430 struct perf_mmap_data *data = counter->data;
1431
1432 WARN_ON(atomic_read(&counter->mmap_count));
1433
1434 rcu_assign_pointer(counter->data, NULL);
1435 call_rcu(&data->rcu_head, __perf_mmap_data_free);
1436}
1437
1438static void perf_mmap_open(struct vm_area_struct *vma)
1439{
1440 struct perf_counter *counter = vma->vm_file->private_data;
1441
1442 atomic_inc(&counter->mmap_count);
1443}
1444
1445static void perf_mmap_close(struct vm_area_struct *vma)
1446{
1447 struct perf_counter *counter = vma->vm_file->private_data;
1448
1449 if (atomic_dec_and_mutex_lock(&counter->mmap_count,
1450 &counter->mmap_mutex)) {
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001451 vma->vm_mm->locked_vm -= counter->data->nr_pages + 1;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001452 perf_mmap_data_free(counter);
1453 mutex_unlock(&counter->mmap_mutex);
1454 }
Paul Mackerras37d81822009-03-23 18:22:08 +01001455}
1456
1457static struct vm_operations_struct perf_mmap_vmops = {
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001458 .open = perf_mmap_open,
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001459 .close = perf_mmap_close,
Paul Mackerras37d81822009-03-23 18:22:08 +01001460 .fault = perf_mmap_fault,
1461};
1462
1463static int perf_mmap(struct file *file, struct vm_area_struct *vma)
1464{
1465 struct perf_counter *counter = file->private_data;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001466 unsigned long vma_size;
1467 unsigned long nr_pages;
1468 unsigned long locked, lock_limit;
1469 int ret = 0;
Paul Mackerras37d81822009-03-23 18:22:08 +01001470
1471 if (!(vma->vm_flags & VM_SHARED) || (vma->vm_flags & VM_WRITE))
1472 return -EINVAL;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001473
1474 vma_size = vma->vm_end - vma->vm_start;
1475 nr_pages = (vma_size / PAGE_SIZE) - 1;
1476
Peter Zijlstra7730d862009-03-25 12:48:31 +01001477 /*
1478 * If we have data pages ensure they're a power-of-two number, so we
1479 * can do bitmasks instead of modulo.
1480 */
1481 if (nr_pages != 0 && !is_power_of_2(nr_pages))
Paul Mackerras37d81822009-03-23 18:22:08 +01001482 return -EINVAL;
1483
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001484 if (vma_size != PAGE_SIZE * (1 + nr_pages))
Paul Mackerras37d81822009-03-23 18:22:08 +01001485 return -EINVAL;
1486
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001487 if (vma->vm_pgoff != 0)
1488 return -EINVAL;
Paul Mackerras37d81822009-03-23 18:22:08 +01001489
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001490 mutex_lock(&counter->mmap_mutex);
1491 if (atomic_inc_not_zero(&counter->mmap_count)) {
1492 if (nr_pages != counter->data->nr_pages)
1493 ret = -EINVAL;
1494 goto unlock;
1495 }
1496
1497 locked = vma->vm_mm->locked_vm;
1498 locked += nr_pages + 1;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001499
1500 lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
1501 lock_limit >>= PAGE_SHIFT;
1502
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001503 if ((locked > lock_limit) && !capable(CAP_IPC_LOCK)) {
1504 ret = -EPERM;
1505 goto unlock;
1506 }
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001507
1508 WARN_ON(counter->data);
1509 ret = perf_mmap_data_alloc(counter, nr_pages);
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001510 if (ret)
1511 goto unlock;
1512
1513 atomic_set(&counter->mmap_count, 1);
1514 vma->vm_mm->locked_vm += nr_pages + 1;
1515unlock:
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001516 mutex_unlock(&counter->mmap_mutex);
Paul Mackerras37d81822009-03-23 18:22:08 +01001517
1518 vma->vm_flags &= ~VM_MAYWRITE;
1519 vma->vm_flags |= VM_RESERVED;
1520 vma->vm_ops = &perf_mmap_vmops;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001521
1522 return ret;
Paul Mackerras37d81822009-03-23 18:22:08 +01001523}
1524
Peter Zijlstra3c446b3d2009-04-06 11:45:01 +02001525static int perf_fasync(int fd, struct file *filp, int on)
1526{
1527 struct perf_counter *counter = filp->private_data;
1528 struct inode *inode = filp->f_path.dentry->d_inode;
1529 int retval;
1530
1531 mutex_lock(&inode->i_mutex);
1532 retval = fasync_helper(fd, filp, on, &counter->fasync);
1533 mutex_unlock(&inode->i_mutex);
1534
1535 if (retval < 0)
1536 return retval;
1537
1538 return 0;
1539}
1540
Thomas Gleixner0793a612008-12-04 20:12:29 +01001541static const struct file_operations perf_fops = {
1542 .release = perf_release,
1543 .read = perf_read,
1544 .poll = perf_poll,
Paul Mackerrasd859e292009-01-17 18:10:22 +11001545 .unlocked_ioctl = perf_ioctl,
1546 .compat_ioctl = perf_ioctl,
Paul Mackerras37d81822009-03-23 18:22:08 +01001547 .mmap = perf_mmap,
Peter Zijlstra3c446b3d2009-04-06 11:45:01 +02001548 .fasync = perf_fasync,
Thomas Gleixner0793a612008-12-04 20:12:29 +01001549};
1550
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001551/*
Peter Zijlstra925d5192009-03-30 19:07:02 +02001552 * Perf counter wakeup
1553 *
1554 * If there's data, ensure we set the poll() state and publish everything
1555 * to user-space before waking everybody up.
1556 */
1557
1558void perf_counter_wakeup(struct perf_counter *counter)
1559{
1560 struct perf_mmap_data *data;
1561
1562 rcu_read_lock();
1563 data = rcu_dereference(counter->data);
1564 if (data) {
Peter Zijlstra3c446b3d2009-04-06 11:45:01 +02001565 atomic_set(&data->wakeup, POLL_IN);
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001566 /*
1567 * Ensure all data writes are issued before updating the
1568 * user-space data head information. The matching rmb()
1569 * will be in userspace after reading this value.
1570 */
1571 smp_wmb();
1572 data->user_page->data_head = atomic_read(&data->head);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001573 }
1574 rcu_read_unlock();
1575
1576 wake_up_all(&counter->waitq);
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02001577
1578 if (counter->pending_kill) {
1579 kill_fasync(&counter->fasync, SIGIO, counter->pending_kill);
1580 counter->pending_kill = 0;
1581 }
Peter Zijlstra925d5192009-03-30 19:07:02 +02001582}
1583
1584/*
1585 * Pending wakeups
1586 *
1587 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
1588 *
1589 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
1590 * single linked list and use cmpxchg() to add entries lockless.
1591 */
1592
Peter Zijlstra79f14642009-04-06 11:45:07 +02001593static void perf_pending_counter(struct perf_pending_entry *entry)
1594{
1595 struct perf_counter *counter = container_of(entry,
1596 struct perf_counter, pending);
1597
1598 if (counter->pending_disable) {
1599 counter->pending_disable = 0;
1600 perf_counter_disable(counter);
1601 }
1602
1603 if (counter->pending_wakeup) {
1604 counter->pending_wakeup = 0;
1605 perf_counter_wakeup(counter);
1606 }
1607}
1608
Peter Zijlstra671dec52009-04-06 11:45:02 +02001609#define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
Peter Zijlstra925d5192009-03-30 19:07:02 +02001610
Peter Zijlstra671dec52009-04-06 11:45:02 +02001611static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = {
Peter Zijlstra925d5192009-03-30 19:07:02 +02001612 PENDING_TAIL,
1613};
1614
Peter Zijlstra671dec52009-04-06 11:45:02 +02001615static void perf_pending_queue(struct perf_pending_entry *entry,
1616 void (*func)(struct perf_pending_entry *))
Peter Zijlstra925d5192009-03-30 19:07:02 +02001617{
Peter Zijlstra671dec52009-04-06 11:45:02 +02001618 struct perf_pending_entry **head;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001619
Peter Zijlstra671dec52009-04-06 11:45:02 +02001620 if (cmpxchg(&entry->next, NULL, PENDING_TAIL) != NULL)
Peter Zijlstra925d5192009-03-30 19:07:02 +02001621 return;
1622
Peter Zijlstra671dec52009-04-06 11:45:02 +02001623 entry->func = func;
1624
1625 head = &get_cpu_var(perf_pending_head);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001626
1627 do {
Peter Zijlstra671dec52009-04-06 11:45:02 +02001628 entry->next = *head;
1629 } while (cmpxchg(head, entry->next, entry) != entry->next);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001630
1631 set_perf_counter_pending();
1632
Peter Zijlstra671dec52009-04-06 11:45:02 +02001633 put_cpu_var(perf_pending_head);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001634}
1635
1636static int __perf_pending_run(void)
1637{
Peter Zijlstra671dec52009-04-06 11:45:02 +02001638 struct perf_pending_entry *list;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001639 int nr = 0;
1640
Peter Zijlstra671dec52009-04-06 11:45:02 +02001641 list = xchg(&__get_cpu_var(perf_pending_head), PENDING_TAIL);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001642 while (list != PENDING_TAIL) {
Peter Zijlstra671dec52009-04-06 11:45:02 +02001643 void (*func)(struct perf_pending_entry *);
1644 struct perf_pending_entry *entry = list;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001645
1646 list = list->next;
1647
Peter Zijlstra671dec52009-04-06 11:45:02 +02001648 func = entry->func;
1649 entry->next = NULL;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001650 /*
1651 * Ensure we observe the unqueue before we issue the wakeup,
1652 * so that we won't be waiting forever.
1653 * -- see perf_not_pending().
1654 */
1655 smp_wmb();
1656
Peter Zijlstra671dec52009-04-06 11:45:02 +02001657 func(entry);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001658 nr++;
1659 }
1660
1661 return nr;
1662}
1663
1664static inline int perf_not_pending(struct perf_counter *counter)
1665{
1666 /*
1667 * If we flush on whatever cpu we run, there is a chance we don't
1668 * need to wait.
1669 */
1670 get_cpu();
1671 __perf_pending_run();
1672 put_cpu();
1673
1674 /*
1675 * Ensure we see the proper queue state before going to sleep
1676 * so that we do not miss the wakeup. -- see perf_pending_handle()
1677 */
1678 smp_rmb();
Peter Zijlstra671dec52009-04-06 11:45:02 +02001679 return counter->pending.next == NULL;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001680}
1681
1682static void perf_pending_sync(struct perf_counter *counter)
1683{
1684 wait_event(counter->waitq, perf_not_pending(counter));
1685}
1686
1687void perf_counter_do_pending(void)
1688{
1689 __perf_pending_run();
1690}
1691
1692/*
Peter Zijlstra394ee072009-03-30 19:07:14 +02001693 * Callchain support -- arch specific
1694 */
1695
Peter Zijlstra9c03d882009-04-06 11:45:00 +02001696__weak struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
Peter Zijlstra394ee072009-03-30 19:07:14 +02001697{
1698 return NULL;
1699}
1700
1701/*
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001702 * Output
1703 */
1704
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001705struct perf_output_handle {
1706 struct perf_counter *counter;
1707 struct perf_mmap_data *data;
1708 unsigned int offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01001709 unsigned int head;
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001710 int wakeup;
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001711 int nmi;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02001712 int overflow;
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001713};
1714
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001715static inline void __perf_output_wakeup(struct perf_output_handle *handle)
1716{
Peter Zijlstra671dec52009-04-06 11:45:02 +02001717 if (handle->nmi) {
Peter Zijlstra79f14642009-04-06 11:45:07 +02001718 handle->counter->pending_wakeup = 1;
Peter Zijlstra671dec52009-04-06 11:45:02 +02001719 perf_pending_queue(&handle->counter->pending,
Peter Zijlstra79f14642009-04-06 11:45:07 +02001720 perf_pending_counter);
Peter Zijlstra671dec52009-04-06 11:45:02 +02001721 } else
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001722 perf_counter_wakeup(handle->counter);
1723}
1724
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001725static int perf_output_begin(struct perf_output_handle *handle,
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001726 struct perf_counter *counter, unsigned int size,
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02001727 int nmi, int overflow)
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001728{
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001729 struct perf_mmap_data *data;
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001730 unsigned int offset, head;
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001731
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001732 rcu_read_lock();
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001733 data = rcu_dereference(counter->data);
1734 if (!data)
1735 goto out;
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001736
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02001737 handle->counter = counter;
1738 handle->nmi = nmi;
1739 handle->overflow = overflow;
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001740
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001741 if (!data->nr_pages)
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001742 goto fail;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001743
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001744 do {
1745 offset = head = atomic_read(&data->head);
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001746 head += size;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001747 } while (atomic_cmpxchg(&data->head, offset, head) != offset);
1748
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001749 handle->data = data;
1750 handle->offset = offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01001751 handle->head = head;
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001752 handle->wakeup = (offset >> PAGE_SHIFT) != (head >> PAGE_SHIFT);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001753
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001754 return 0;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001755
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001756fail:
1757 __perf_output_wakeup(handle);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001758out:
1759 rcu_read_unlock();
1760
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001761 return -ENOSPC;
1762}
1763
1764static void perf_output_copy(struct perf_output_handle *handle,
1765 void *buf, unsigned int len)
1766{
1767 unsigned int pages_mask;
1768 unsigned int offset;
1769 unsigned int size;
1770 void **pages;
1771
1772 offset = handle->offset;
1773 pages_mask = handle->data->nr_pages - 1;
1774 pages = handle->data->data_pages;
1775
1776 do {
1777 unsigned int page_offset;
1778 int nr;
1779
1780 nr = (offset >> PAGE_SHIFT) & pages_mask;
1781 page_offset = offset & (PAGE_SIZE - 1);
1782 size = min_t(unsigned int, PAGE_SIZE - page_offset, len);
1783
1784 memcpy(pages[nr] + page_offset, buf, size);
1785
1786 len -= size;
1787 buf += size;
1788 offset += size;
1789 } while (len);
1790
1791 handle->offset = offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01001792
1793 WARN_ON_ONCE(handle->offset > handle->head);
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001794}
1795
Peter Zijlstra5c148192009-03-25 12:30:23 +01001796#define perf_output_put(handle, x) \
1797 perf_output_copy((handle), &(x), sizeof(x))
1798
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001799static void perf_output_end(struct perf_output_handle *handle)
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001800{
Peter Zijlstrac4578102009-04-02 11:12:01 +02001801 int wakeup_events = handle->counter->hw_event.wakeup_events;
1802
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02001803 if (handle->overflow && wakeup_events) {
Peter Zijlstrac4578102009-04-02 11:12:01 +02001804 int events = atomic_inc_return(&handle->data->events);
1805 if (events >= wakeup_events) {
1806 atomic_sub(wakeup_events, &handle->data->events);
1807 __perf_output_wakeup(handle);
1808 }
1809 } else if (handle->wakeup)
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001810 __perf_output_wakeup(handle);
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001811 rcu_read_unlock();
1812}
1813
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02001814static void perf_counter_output(struct perf_counter *counter,
1815 int nmi, struct pt_regs *regs)
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001816{
Peter Zijlstra5ed00412009-03-30 19:07:12 +02001817 int ret;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02001818 u64 record_type = counter->hw_event.record_type;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02001819 struct perf_output_handle handle;
1820 struct perf_event_header header;
1821 u64 ip;
Peter Zijlstra5c148192009-03-25 12:30:23 +01001822 struct {
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01001823 u32 pid, tid;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02001824 } tid_entry;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02001825 struct {
1826 u64 event;
1827 u64 counter;
1828 } group_entry;
Peter Zijlstra394ee072009-03-30 19:07:14 +02001829 struct perf_callchain_entry *callchain = NULL;
1830 int callchain_size = 0;
Peter Zijlstra339f7c92009-04-06 11:45:06 +02001831 u64 time;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001832
Peter Zijlstra8a057d82009-04-02 11:11:59 +02001833 header.type = PERF_EVENT_COUNTER_OVERFLOW;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02001834 header.size = sizeof(header);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001835
Peter Zijlstra8a057d82009-04-02 11:11:59 +02001836 if (record_type & PERF_RECORD_IP) {
1837 ip = instruction_pointer(regs);
1838 header.type |= __PERF_EVENT_IP;
1839 header.size += sizeof(ip);
1840 }
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01001841
Peter Zijlstra8a057d82009-04-02 11:11:59 +02001842 if (record_type & PERF_RECORD_TID) {
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01001843 /* namespace issues */
Peter Zijlstra5ed00412009-03-30 19:07:12 +02001844 tid_entry.pid = current->group_leader->pid;
1845 tid_entry.tid = current->pid;
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01001846
Peter Zijlstra5ed00412009-03-30 19:07:12 +02001847 header.type |= __PERF_EVENT_TID;
1848 header.size += sizeof(tid_entry);
1849 }
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01001850
Peter Zijlstra8a057d82009-04-02 11:11:59 +02001851 if (record_type & PERF_RECORD_GROUP) {
1852 header.type |= __PERF_EVENT_GROUP;
1853 header.size += sizeof(u64) +
1854 counter->nr_siblings * sizeof(group_entry);
1855 }
1856
1857 if (record_type & PERF_RECORD_CALLCHAIN) {
Peter Zijlstra394ee072009-03-30 19:07:14 +02001858 callchain = perf_callchain(regs);
1859
1860 if (callchain) {
Peter Zijlstra9c03d882009-04-06 11:45:00 +02001861 callchain_size = (1 + callchain->nr) * sizeof(u64);
Peter Zijlstra394ee072009-03-30 19:07:14 +02001862
1863 header.type |= __PERF_EVENT_CALLCHAIN;
1864 header.size += callchain_size;
1865 }
1866 }
1867
Peter Zijlstra339f7c92009-04-06 11:45:06 +02001868 if (record_type & PERF_RECORD_TIME) {
1869 /*
1870 * Maybe do better on x86 and provide cpu_clock_nmi()
1871 */
1872 time = sched_clock();
1873
1874 header.type |= __PERF_EVENT_TIME;
1875 header.size += sizeof(u64);
1876 }
1877
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02001878 ret = perf_output_begin(&handle, counter, header.size, nmi, 1);
Peter Zijlstra5ed00412009-03-30 19:07:12 +02001879 if (ret)
1880 return;
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01001881
Peter Zijlstra5ed00412009-03-30 19:07:12 +02001882 perf_output_put(&handle, header);
Peter Zijlstra5ed00412009-03-30 19:07:12 +02001883
Peter Zijlstra8a057d82009-04-02 11:11:59 +02001884 if (record_type & PERF_RECORD_IP)
1885 perf_output_put(&handle, ip);
1886
1887 if (record_type & PERF_RECORD_TID)
Peter Zijlstra5ed00412009-03-30 19:07:12 +02001888 perf_output_put(&handle, tid_entry);
1889
Peter Zijlstra8a057d82009-04-02 11:11:59 +02001890 if (record_type & PERF_RECORD_GROUP) {
1891 struct perf_counter *leader, *sub;
1892 u64 nr = counter->nr_siblings;
1893
1894 perf_output_put(&handle, nr);
1895
1896 leader = counter->group_leader;
1897 list_for_each_entry(sub, &leader->sibling_list, list_entry) {
1898 if (sub != counter)
1899 sub->hw_ops->read(sub);
1900
1901 group_entry.event = sub->hw_event.config;
1902 group_entry.counter = atomic64_read(&sub->count);
1903
1904 perf_output_put(&handle, group_entry);
1905 }
1906 }
1907
Peter Zijlstra394ee072009-03-30 19:07:14 +02001908 if (callchain)
1909 perf_output_copy(&handle, callchain, callchain_size);
1910
Peter Zijlstra339f7c92009-04-06 11:45:06 +02001911 if (record_type & PERF_RECORD_TIME)
1912 perf_output_put(&handle, time);
1913
Peter Zijlstra5ed00412009-03-30 19:07:12 +02001914 perf_output_end(&handle);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001915}
1916
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001917/*
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02001918 * mmap tracking
1919 */
1920
1921struct perf_mmap_event {
1922 struct file *file;
1923 char *file_name;
1924 int file_size;
1925
1926 struct {
1927 struct perf_event_header header;
1928
1929 u32 pid;
1930 u32 tid;
1931 u64 start;
1932 u64 len;
1933 u64 pgoff;
1934 } event;
1935};
1936
1937static void perf_counter_mmap_output(struct perf_counter *counter,
1938 struct perf_mmap_event *mmap_event)
1939{
1940 struct perf_output_handle handle;
1941 int size = mmap_event->event.header.size;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02001942 int ret = perf_output_begin(&handle, counter, size, 0, 0);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02001943
1944 if (ret)
1945 return;
1946
1947 perf_output_put(&handle, mmap_event->event);
1948 perf_output_copy(&handle, mmap_event->file_name,
1949 mmap_event->file_size);
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001950 perf_output_end(&handle);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02001951}
1952
1953static int perf_counter_mmap_match(struct perf_counter *counter,
1954 struct perf_mmap_event *mmap_event)
1955{
1956 if (counter->hw_event.mmap &&
1957 mmap_event->event.header.type == PERF_EVENT_MMAP)
1958 return 1;
1959
1960 if (counter->hw_event.munmap &&
1961 mmap_event->event.header.type == PERF_EVENT_MUNMAP)
1962 return 1;
1963
1964 return 0;
1965}
1966
1967static void perf_counter_mmap_ctx(struct perf_counter_context *ctx,
1968 struct perf_mmap_event *mmap_event)
1969{
1970 struct perf_counter *counter;
1971
1972 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
1973 return;
1974
1975 rcu_read_lock();
1976 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
1977 if (perf_counter_mmap_match(counter, mmap_event))
1978 perf_counter_mmap_output(counter, mmap_event);
1979 }
1980 rcu_read_unlock();
1981}
1982
1983static void perf_counter_mmap_event(struct perf_mmap_event *mmap_event)
1984{
1985 struct perf_cpu_context *cpuctx;
1986 struct file *file = mmap_event->file;
1987 unsigned int size;
1988 char tmp[16];
1989 char *buf = NULL;
1990 char *name;
1991
1992 if (file) {
1993 buf = kzalloc(PATH_MAX, GFP_KERNEL);
1994 if (!buf) {
1995 name = strncpy(tmp, "//enomem", sizeof(tmp));
1996 goto got_name;
1997 }
1998 name = dentry_path(file->f_dentry, buf, PATH_MAX);
1999 if (IS_ERR(name)) {
2000 name = strncpy(tmp, "//toolong", sizeof(tmp));
2001 goto got_name;
2002 }
2003 } else {
2004 name = strncpy(tmp, "//anon", sizeof(tmp));
2005 goto got_name;
2006 }
2007
2008got_name:
2009 size = ALIGN(strlen(name), sizeof(u64));
2010
2011 mmap_event->file_name = name;
2012 mmap_event->file_size = size;
2013
2014 mmap_event->event.header.size = sizeof(mmap_event->event) + size;
2015
2016 cpuctx = &get_cpu_var(perf_cpu_context);
2017 perf_counter_mmap_ctx(&cpuctx->ctx, mmap_event);
2018 put_cpu_var(perf_cpu_context);
2019
2020 perf_counter_mmap_ctx(&current->perf_counter_ctx, mmap_event);
2021
2022 kfree(buf);
2023}
2024
2025void perf_counter_mmap(unsigned long addr, unsigned long len,
2026 unsigned long pgoff, struct file *file)
2027{
2028 struct perf_mmap_event mmap_event = {
2029 .file = file,
2030 .event = {
2031 .header = { .type = PERF_EVENT_MMAP, },
2032 .pid = current->group_leader->pid,
2033 .tid = current->pid,
2034 .start = addr,
2035 .len = len,
2036 .pgoff = pgoff,
2037 },
2038 };
2039
2040 perf_counter_mmap_event(&mmap_event);
2041}
2042
2043void perf_counter_munmap(unsigned long addr, unsigned long len,
2044 unsigned long pgoff, struct file *file)
2045{
2046 struct perf_mmap_event mmap_event = {
2047 .file = file,
2048 .event = {
2049 .header = { .type = PERF_EVENT_MUNMAP, },
2050 .pid = current->group_leader->pid,
2051 .tid = current->pid,
2052 .start = addr,
2053 .len = len,
2054 .pgoff = pgoff,
2055 },
2056 };
2057
2058 perf_counter_mmap_event(&mmap_event);
2059}
2060
2061/*
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002062 * Generic counter overflow handling.
2063 */
2064
2065int perf_counter_overflow(struct perf_counter *counter,
2066 int nmi, struct pt_regs *regs)
2067{
Peter Zijlstra79f14642009-04-06 11:45:07 +02002068 int events = atomic_read(&counter->event_limit);
2069 int ret = 0;
2070
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002071 counter->pending_kill = POLL_IN;
Peter Zijlstra79f14642009-04-06 11:45:07 +02002072 if (events && atomic_dec_and_test(&counter->event_limit)) {
2073 ret = 1;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002074 counter->pending_kill = POLL_HUP;
Peter Zijlstra79f14642009-04-06 11:45:07 +02002075 if (nmi) {
2076 counter->pending_disable = 1;
2077 perf_pending_queue(&counter->pending,
2078 perf_pending_counter);
2079 } else
2080 perf_counter_disable(counter);
2081 }
2082
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002083 perf_counter_output(counter, nmi, regs);
Peter Zijlstra79f14642009-04-06 11:45:07 +02002084 return ret;
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002085}
2086
2087/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002088 * Generic software counter infrastructure
2089 */
2090
2091static void perf_swcounter_update(struct perf_counter *counter)
2092{
2093 struct hw_perf_counter *hwc = &counter->hw;
2094 u64 prev, now;
2095 s64 delta;
2096
2097again:
2098 prev = atomic64_read(&hwc->prev_count);
2099 now = atomic64_read(&hwc->count);
2100 if (atomic64_cmpxchg(&hwc->prev_count, prev, now) != prev)
2101 goto again;
2102
2103 delta = now - prev;
2104
2105 atomic64_add(delta, &counter->count);
2106 atomic64_sub(delta, &hwc->period_left);
2107}
2108
2109static void perf_swcounter_set_period(struct perf_counter *counter)
2110{
2111 struct hw_perf_counter *hwc = &counter->hw;
2112 s64 left = atomic64_read(&hwc->period_left);
2113 s64 period = hwc->irq_period;
2114
2115 if (unlikely(left <= -period)) {
2116 left = period;
2117 atomic64_set(&hwc->period_left, left);
2118 }
2119
2120 if (unlikely(left <= 0)) {
2121 left += period;
2122 atomic64_add(period, &hwc->period_left);
2123 }
2124
2125 atomic64_set(&hwc->prev_count, -left);
2126 atomic64_set(&hwc->count, -left);
2127}
2128
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002129static enum hrtimer_restart perf_swcounter_hrtimer(struct hrtimer *hrtimer)
2130{
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002131 enum hrtimer_restart ret = HRTIMER_RESTART;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002132 struct perf_counter *counter;
2133 struct pt_regs *regs;
2134
2135 counter = container_of(hrtimer, struct perf_counter, hw.hrtimer);
2136 counter->hw_ops->read(counter);
2137
2138 regs = get_irq_regs();
2139 /*
2140 * In case we exclude kernel IPs or are somehow not in interrupt
2141 * context, provide the next best thing, the user IP.
2142 */
2143 if ((counter->hw_event.exclude_kernel || !regs) &&
2144 !counter->hw_event.exclude_user)
2145 regs = task_pt_regs(current);
2146
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002147 if (regs) {
2148 if (perf_counter_overflow(counter, 0, regs))
2149 ret = HRTIMER_NORESTART;
2150 }
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002151
2152 hrtimer_forward_now(hrtimer, ns_to_ktime(counter->hw.irq_period));
2153
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002154 return ret;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002155}
2156
2157static void perf_swcounter_overflow(struct perf_counter *counter,
2158 int nmi, struct pt_regs *regs)
2159{
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002160 perf_swcounter_update(counter);
2161 perf_swcounter_set_period(counter);
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002162 if (perf_counter_overflow(counter, nmi, regs))
2163 /* soft-disable the counter */
2164 ;
2165
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002166}
2167
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002168static int perf_swcounter_match(struct perf_counter *counter,
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002169 enum perf_event_types type,
2170 u32 event, struct pt_regs *regs)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002171{
2172 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
2173 return 0;
2174
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002175 if (perf_event_raw(&counter->hw_event))
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002176 return 0;
2177
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002178 if (perf_event_type(&counter->hw_event) != type)
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002179 return 0;
2180
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002181 if (perf_event_id(&counter->hw_event) != event)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002182 return 0;
2183
2184 if (counter->hw_event.exclude_user && user_mode(regs))
2185 return 0;
2186
2187 if (counter->hw_event.exclude_kernel && !user_mode(regs))
2188 return 0;
2189
2190 return 1;
2191}
2192
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002193static void perf_swcounter_add(struct perf_counter *counter, u64 nr,
2194 int nmi, struct pt_regs *regs)
2195{
2196 int neg = atomic64_add_negative(nr, &counter->hw.count);
2197 if (counter->hw.irq_period && !neg)
2198 perf_swcounter_overflow(counter, nmi, regs);
2199}
2200
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002201static void perf_swcounter_ctx_event(struct perf_counter_context *ctx,
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002202 enum perf_event_types type, u32 event,
2203 u64 nr, int nmi, struct pt_regs *regs)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002204{
2205 struct perf_counter *counter;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002206
Peter Zijlstra01ef09d2009-03-19 20:26:11 +01002207 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002208 return;
2209
Peter Zijlstra592903c2009-03-13 12:21:36 +01002210 rcu_read_lock();
2211 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002212 if (perf_swcounter_match(counter, type, event, regs))
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002213 perf_swcounter_add(counter, nr, nmi, regs);
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002214 }
Peter Zijlstra592903c2009-03-13 12:21:36 +01002215 rcu_read_unlock();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002216}
2217
Peter Zijlstra96f6d442009-03-23 18:22:07 +01002218static int *perf_swcounter_recursion_context(struct perf_cpu_context *cpuctx)
2219{
2220 if (in_nmi())
2221 return &cpuctx->recursion[3];
2222
2223 if (in_irq())
2224 return &cpuctx->recursion[2];
2225
2226 if (in_softirq())
2227 return &cpuctx->recursion[1];
2228
2229 return &cpuctx->recursion[0];
2230}
2231
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002232static void __perf_swcounter_event(enum perf_event_types type, u32 event,
2233 u64 nr, int nmi, struct pt_regs *regs)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002234{
2235 struct perf_cpu_context *cpuctx = &get_cpu_var(perf_cpu_context);
Peter Zijlstra96f6d442009-03-23 18:22:07 +01002236 int *recursion = perf_swcounter_recursion_context(cpuctx);
2237
2238 if (*recursion)
2239 goto out;
2240
2241 (*recursion)++;
2242 barrier();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002243
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002244 perf_swcounter_ctx_event(&cpuctx->ctx, type, event, nr, nmi, regs);
2245 if (cpuctx->task_ctx) {
2246 perf_swcounter_ctx_event(cpuctx->task_ctx, type, event,
2247 nr, nmi, regs);
2248 }
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002249
Peter Zijlstra96f6d442009-03-23 18:22:07 +01002250 barrier();
2251 (*recursion)--;
2252
2253out:
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002254 put_cpu_var(perf_cpu_context);
2255}
2256
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002257void perf_swcounter_event(u32 event, u64 nr, int nmi, struct pt_regs *regs)
2258{
2259 __perf_swcounter_event(PERF_TYPE_SOFTWARE, event, nr, nmi, regs);
2260}
2261
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002262static void perf_swcounter_read(struct perf_counter *counter)
2263{
2264 perf_swcounter_update(counter);
2265}
2266
2267static int perf_swcounter_enable(struct perf_counter *counter)
2268{
2269 perf_swcounter_set_period(counter);
2270 return 0;
2271}
2272
2273static void perf_swcounter_disable(struct perf_counter *counter)
2274{
2275 perf_swcounter_update(counter);
2276}
2277
Peter Zijlstraac17dc82009-03-13 12:21:34 +01002278static const struct hw_perf_counter_ops perf_ops_generic = {
2279 .enable = perf_swcounter_enable,
2280 .disable = perf_swcounter_disable,
2281 .read = perf_swcounter_read,
2282};
2283
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002284/*
2285 * Software counter: cpu wall time clock
2286 */
2287
Paul Mackerras9abf8a02009-01-09 16:26:43 +11002288static void cpu_clock_perf_counter_update(struct perf_counter *counter)
2289{
2290 int cpu = raw_smp_processor_id();
2291 s64 prev;
2292 u64 now;
2293
2294 now = cpu_clock(cpu);
2295 prev = atomic64_read(&counter->hw.prev_count);
2296 atomic64_set(&counter->hw.prev_count, now);
2297 atomic64_add(now - prev, &counter->count);
2298}
2299
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002300static int cpu_clock_perf_counter_enable(struct perf_counter *counter)
2301{
2302 struct hw_perf_counter *hwc = &counter->hw;
2303 int cpu = raw_smp_processor_id();
2304
2305 atomic64_set(&hwc->prev_count, cpu_clock(cpu));
Peter Zijlstra039fc912009-03-13 16:43:47 +01002306 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2307 hwc->hrtimer.function = perf_swcounter_hrtimer;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002308 if (hwc->irq_period) {
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002309 __hrtimer_start_range_ns(&hwc->hrtimer,
2310 ns_to_ktime(hwc->irq_period), 0,
2311 HRTIMER_MODE_REL, 0);
2312 }
2313
2314 return 0;
2315}
2316
Ingo Molnar5c92d122008-12-11 13:21:10 +01002317static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
2318{
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002319 hrtimer_cancel(&counter->hw.hrtimer);
Paul Mackerras9abf8a02009-01-09 16:26:43 +11002320 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01002321}
2322
2323static void cpu_clock_perf_counter_read(struct perf_counter *counter)
2324{
Paul Mackerras9abf8a02009-01-09 16:26:43 +11002325 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01002326}
2327
2328static const struct hw_perf_counter_ops perf_ops_cpu_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +01002329 .enable = cpu_clock_perf_counter_enable,
2330 .disable = cpu_clock_perf_counter_disable,
2331 .read = cpu_clock_perf_counter_read,
Ingo Molnar5c92d122008-12-11 13:21:10 +01002332};
2333
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01002334/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002335 * Software counter: task time clock
2336 */
2337
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02002338static void task_clock_perf_counter_update(struct perf_counter *counter)
Ingo Molnarbae43c92008-12-11 14:03:20 +01002339{
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02002340 u64 prev, now;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002341 s64 delta;
Ingo Molnarbae43c92008-12-11 14:03:20 +01002342
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02002343 now = counter->ctx->time;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002344
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02002345 prev = atomic64_xchg(&counter->hw.prev_count, now);
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002346 delta = now - prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002347 atomic64_add(delta, &counter->count);
Ingo Molnarbae43c92008-12-11 14:03:20 +01002348}
2349
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01002350static int task_clock_perf_counter_enable(struct perf_counter *counter)
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002351{
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002352 struct hw_perf_counter *hwc = &counter->hw;
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02002353 u64 now;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002354
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02002355 now = counter->ctx->time;
2356
2357 atomic64_set(&hwc->prev_count, now);
Peter Zijlstra039fc912009-03-13 16:43:47 +01002358 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2359 hwc->hrtimer.function = perf_swcounter_hrtimer;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002360 if (hwc->irq_period) {
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002361 __hrtimer_start_range_ns(&hwc->hrtimer,
2362 ns_to_ktime(hwc->irq_period), 0,
2363 HRTIMER_MODE_REL, 0);
2364 }
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01002365
2366 return 0;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002367}
2368
2369static void task_clock_perf_counter_disable(struct perf_counter *counter)
2370{
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002371 hrtimer_cancel(&counter->hw.hrtimer);
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02002372 task_clock_perf_counter_update(counter);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002373}
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01002374
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002375static void task_clock_perf_counter_read(struct perf_counter *counter)
2376{
Peter Zijlstrabce379b2009-04-06 11:45:13 +02002377 update_context_time(counter->ctx);
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02002378 task_clock_perf_counter_update(counter);
Ingo Molnarbae43c92008-12-11 14:03:20 +01002379}
2380
2381static const struct hw_perf_counter_ops perf_ops_task_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +01002382 .enable = task_clock_perf_counter_enable,
2383 .disable = task_clock_perf_counter_disable,
2384 .read = task_clock_perf_counter_read,
Ingo Molnarbae43c92008-12-11 14:03:20 +01002385};
2386
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002387/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002388 * Software counter: cpu migrations
2389 */
2390
Paul Mackerras23a185c2009-02-09 22:42:47 +11002391static inline u64 get_cpu_migrations(struct perf_counter *counter)
Ingo Molnar6c594c22008-12-14 12:34:15 +01002392{
Paul Mackerras23a185c2009-02-09 22:42:47 +11002393 struct task_struct *curr = counter->ctx->task;
2394
2395 if (curr)
2396 return curr->se.nr_migrations;
2397 return cpu_nr_migrations(smp_processor_id());
Ingo Molnar6c594c22008-12-14 12:34:15 +01002398}
2399
2400static void cpu_migrations_perf_counter_update(struct perf_counter *counter)
2401{
2402 u64 prev, now;
2403 s64 delta;
2404
2405 prev = atomic64_read(&counter->hw.prev_count);
Paul Mackerras23a185c2009-02-09 22:42:47 +11002406 now = get_cpu_migrations(counter);
Ingo Molnar6c594c22008-12-14 12:34:15 +01002407
2408 atomic64_set(&counter->hw.prev_count, now);
2409
2410 delta = now - prev;
Ingo Molnar6c594c22008-12-14 12:34:15 +01002411
2412 atomic64_add(delta, &counter->count);
2413}
2414
2415static void cpu_migrations_perf_counter_read(struct perf_counter *counter)
2416{
2417 cpu_migrations_perf_counter_update(counter);
2418}
2419
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01002420static int cpu_migrations_perf_counter_enable(struct perf_counter *counter)
Ingo Molnar6c594c22008-12-14 12:34:15 +01002421{
Paul Mackerrasc07c99b2009-02-13 22:10:34 +11002422 if (counter->prev_state <= PERF_COUNTER_STATE_OFF)
2423 atomic64_set(&counter->hw.prev_count,
2424 get_cpu_migrations(counter));
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01002425 return 0;
Ingo Molnar6c594c22008-12-14 12:34:15 +01002426}
2427
2428static void cpu_migrations_perf_counter_disable(struct perf_counter *counter)
2429{
2430 cpu_migrations_perf_counter_update(counter);
2431}
2432
2433static const struct hw_perf_counter_ops perf_ops_cpu_migrations = {
Ingo Molnar76715812008-12-17 14:20:28 +01002434 .enable = cpu_migrations_perf_counter_enable,
2435 .disable = cpu_migrations_perf_counter_disable,
2436 .read = cpu_migrations_perf_counter_read,
Ingo Molnar6c594c22008-12-14 12:34:15 +01002437};
2438
Peter Zijlstrae077df42009-03-19 20:26:17 +01002439#ifdef CONFIG_EVENT_PROFILE
2440void perf_tpcounter_event(int event_id)
2441{
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002442 struct pt_regs *regs = get_irq_regs();
2443
2444 if (!regs)
2445 regs = task_pt_regs(current);
2446
2447 __perf_swcounter_event(PERF_TYPE_TRACEPOINT, event_id, 1, 1, regs);
Peter Zijlstrae077df42009-03-19 20:26:17 +01002448}
2449
2450extern int ftrace_profile_enable(int);
2451extern void ftrace_profile_disable(int);
2452
2453static void tp_perf_counter_destroy(struct perf_counter *counter)
2454{
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002455 ftrace_profile_disable(perf_event_id(&counter->hw_event));
Peter Zijlstrae077df42009-03-19 20:26:17 +01002456}
2457
2458static const struct hw_perf_counter_ops *
2459tp_perf_counter_init(struct perf_counter *counter)
2460{
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002461 int event_id = perf_event_id(&counter->hw_event);
Peter Zijlstrae077df42009-03-19 20:26:17 +01002462 int ret;
2463
2464 ret = ftrace_profile_enable(event_id);
2465 if (ret)
2466 return NULL;
2467
2468 counter->destroy = tp_perf_counter_destroy;
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002469 counter->hw.irq_period = counter->hw_event.irq_period;
Peter Zijlstrae077df42009-03-19 20:26:17 +01002470
2471 return &perf_ops_generic;
2472}
2473#else
2474static const struct hw_perf_counter_ops *
2475tp_perf_counter_init(struct perf_counter *counter)
2476{
2477 return NULL;
2478}
2479#endif
2480
Ingo Molnar5c92d122008-12-11 13:21:10 +01002481static const struct hw_perf_counter_ops *
2482sw_perf_counter_init(struct perf_counter *counter)
2483{
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002484 struct perf_counter_hw_event *hw_event = &counter->hw_event;
Ingo Molnar5c92d122008-12-11 13:21:10 +01002485 const struct hw_perf_counter_ops *hw_ops = NULL;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002486 struct hw_perf_counter *hwc = &counter->hw;
Ingo Molnar5c92d122008-12-11 13:21:10 +01002487
Paul Mackerras0475f9e2009-02-11 14:35:35 +11002488 /*
2489 * Software counters (currently) can't in general distinguish
2490 * between user, kernel and hypervisor events.
2491 * However, context switches and cpu migrations are considered
2492 * to be kernel events, and page faults are never hypervisor
2493 * events.
2494 */
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002495 switch (perf_event_id(&counter->hw_event)) {
Ingo Molnar5c92d122008-12-11 13:21:10 +01002496 case PERF_COUNT_CPU_CLOCK:
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002497 hw_ops = &perf_ops_cpu_clock;
2498
2499 if (hw_event->irq_period && hw_event->irq_period < 10000)
2500 hw_event->irq_period = 10000;
Ingo Molnar5c92d122008-12-11 13:21:10 +01002501 break;
Ingo Molnarbae43c92008-12-11 14:03:20 +01002502 case PERF_COUNT_TASK_CLOCK:
Paul Mackerras23a185c2009-02-09 22:42:47 +11002503 /*
2504 * If the user instantiates this as a per-cpu counter,
2505 * use the cpu_clock counter instead.
2506 */
2507 if (counter->ctx->task)
2508 hw_ops = &perf_ops_task_clock;
2509 else
2510 hw_ops = &perf_ops_cpu_clock;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002511
2512 if (hw_event->irq_period && hw_event->irq_period < 10000)
2513 hw_event->irq_period = 10000;
Ingo Molnarbae43c92008-12-11 14:03:20 +01002514 break;
Ingo Molnare06c61a2008-12-14 14:44:31 +01002515 case PERF_COUNT_PAGE_FAULTS:
Peter Zijlstraac17dc82009-03-13 12:21:34 +01002516 case PERF_COUNT_PAGE_FAULTS_MIN:
2517 case PERF_COUNT_PAGE_FAULTS_MAJ:
Ingo Molnar5d6a27d2008-12-14 12:28:33 +01002518 case PERF_COUNT_CONTEXT_SWITCHES:
Peter Zijlstra4a0deca2009-03-19 20:26:12 +01002519 hw_ops = &perf_ops_generic;
Ingo Molnar5d6a27d2008-12-14 12:28:33 +01002520 break;
Ingo Molnar6c594c22008-12-14 12:34:15 +01002521 case PERF_COUNT_CPU_MIGRATIONS:
Paul Mackerras0475f9e2009-02-11 14:35:35 +11002522 if (!counter->hw_event.exclude_kernel)
2523 hw_ops = &perf_ops_cpu_migrations;
Ingo Molnar6c594c22008-12-14 12:34:15 +01002524 break;
Ingo Molnar5c92d122008-12-11 13:21:10 +01002525 }
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002526
2527 if (hw_ops)
2528 hwc->irq_period = hw_event->irq_period;
2529
Ingo Molnar5c92d122008-12-11 13:21:10 +01002530 return hw_ops;
2531}
2532
Thomas Gleixner0793a612008-12-04 20:12:29 +01002533/*
2534 * Allocate and initialize a counter structure
2535 */
2536static struct perf_counter *
Ingo Molnar04289bb2008-12-11 08:38:42 +01002537perf_counter_alloc(struct perf_counter_hw_event *hw_event,
2538 int cpu,
Paul Mackerras23a185c2009-02-09 22:42:47 +11002539 struct perf_counter_context *ctx,
Ingo Molnar9b51f662008-12-12 13:49:45 +01002540 struct perf_counter *group_leader,
2541 gfp_t gfpflags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01002542{
Ingo Molnar5c92d122008-12-11 13:21:10 +01002543 const struct hw_perf_counter_ops *hw_ops;
Ingo Molnar621a01e2008-12-11 12:46:46 +01002544 struct perf_counter *counter;
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02002545 long err;
Thomas Gleixner0793a612008-12-04 20:12:29 +01002546
Ingo Molnar9b51f662008-12-12 13:49:45 +01002547 counter = kzalloc(sizeof(*counter), gfpflags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002548 if (!counter)
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02002549 return ERR_PTR(-ENOMEM);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002550
Ingo Molnar04289bb2008-12-11 08:38:42 +01002551 /*
2552 * Single counters are their own group leaders, with an
2553 * empty sibling list:
2554 */
2555 if (!group_leader)
2556 group_leader = counter;
2557
Thomas Gleixner0793a612008-12-04 20:12:29 +01002558 mutex_init(&counter->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01002559 INIT_LIST_HEAD(&counter->list_entry);
Peter Zijlstra592903c2009-03-13 12:21:36 +01002560 INIT_LIST_HEAD(&counter->event_entry);
Ingo Molnar04289bb2008-12-11 08:38:42 +01002561 INIT_LIST_HEAD(&counter->sibling_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002562 init_waitqueue_head(&counter->waitq);
2563
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002564 mutex_init(&counter->mmap_mutex);
2565
Paul Mackerrasd859e292009-01-17 18:10:22 +11002566 INIT_LIST_HEAD(&counter->child_list);
2567
Ingo Molnar9f66a382008-12-10 12:33:23 +01002568 counter->cpu = cpu;
2569 counter->hw_event = *hw_event;
Ingo Molnar04289bb2008-12-11 08:38:42 +01002570 counter->group_leader = group_leader;
Ingo Molnar621a01e2008-12-11 12:46:46 +01002571 counter->hw_ops = NULL;
Paul Mackerras23a185c2009-02-09 22:42:47 +11002572 counter->ctx = ctx;
Ingo Molnar621a01e2008-12-11 12:46:46 +01002573
Ingo Molnar235c7fc2008-12-21 14:43:25 +01002574 counter->state = PERF_COUNTER_STATE_INACTIVE;
Ingo Molnara86ed502008-12-17 00:43:10 +01002575 if (hw_event->disabled)
2576 counter->state = PERF_COUNTER_STATE_OFF;
2577
Ingo Molnar5c92d122008-12-11 13:21:10 +01002578 hw_ops = NULL;
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002579
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002580 if (perf_event_raw(hw_event)) {
Ingo Molnar5c92d122008-12-11 13:21:10 +01002581 hw_ops = hw_perf_counter_init(counter);
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002582 goto done;
2583 }
2584
2585 switch (perf_event_type(hw_event)) {
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002586 case PERF_TYPE_HARDWARE:
2587 hw_ops = hw_perf_counter_init(counter);
2588 break;
2589
2590 case PERF_TYPE_SOFTWARE:
2591 hw_ops = sw_perf_counter_init(counter);
2592 break;
2593
2594 case PERF_TYPE_TRACEPOINT:
2595 hw_ops = tp_perf_counter_init(counter);
2596 break;
2597 }
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002598done:
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02002599 err = 0;
2600 if (!hw_ops)
2601 err = -EINVAL;
2602 else if (IS_ERR(hw_ops))
2603 err = PTR_ERR(hw_ops);
2604
2605 if (err) {
2606 kfree(counter);
2607 return ERR_PTR(err);
2608 }
2609
Ingo Molnar621a01e2008-12-11 12:46:46 +01002610 counter->hw_ops = hw_ops;
Thomas Gleixner0793a612008-12-04 20:12:29 +01002611
2612 return counter;
2613}
2614
2615/**
Paul Mackerras2743a5b2009-03-04 20:36:51 +11002616 * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
Ingo Molnar9f66a382008-12-10 12:33:23 +01002617 *
2618 * @hw_event_uptr: event type attributes for monitoring/sampling
Thomas Gleixner0793a612008-12-04 20:12:29 +01002619 * @pid: target pid
Ingo Molnar9f66a382008-12-10 12:33:23 +01002620 * @cpu: target cpu
2621 * @group_fd: group leader counter fd
Thomas Gleixner0793a612008-12-04 20:12:29 +01002622 */
Paul Mackerras2743a5b2009-03-04 20:36:51 +11002623SYSCALL_DEFINE5(perf_counter_open,
Paul Mackerrasf3dfd262009-02-26 22:43:46 +11002624 const struct perf_counter_hw_event __user *, hw_event_uptr,
Paul Mackerras2743a5b2009-03-04 20:36:51 +11002625 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01002626{
Ingo Molnar04289bb2008-12-11 08:38:42 +01002627 struct perf_counter *counter, *group_leader;
Ingo Molnar9f66a382008-12-10 12:33:23 +01002628 struct perf_counter_hw_event hw_event;
Ingo Molnar04289bb2008-12-11 08:38:42 +01002629 struct perf_counter_context *ctx;
Ingo Molnar9b51f662008-12-12 13:49:45 +01002630 struct file *counter_file = NULL;
Ingo Molnar04289bb2008-12-11 08:38:42 +01002631 struct file *group_file = NULL;
2632 int fput_needed = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01002633 int fput_needed2 = 0;
Thomas Gleixner0793a612008-12-04 20:12:29 +01002634 int ret;
2635
Paul Mackerras2743a5b2009-03-04 20:36:51 +11002636 /* for future expandability... */
2637 if (flags)
2638 return -EINVAL;
2639
Ingo Molnar9f66a382008-12-10 12:33:23 +01002640 if (copy_from_user(&hw_event, hw_event_uptr, sizeof(hw_event)) != 0)
Thomas Gleixnereab656a2008-12-08 19:26:59 +01002641 return -EFAULT;
2642
Ingo Molnar04289bb2008-12-11 08:38:42 +01002643 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01002644 * Get the target context (task or percpu):
2645 */
2646 ctx = find_get_context(pid, cpu);
2647 if (IS_ERR(ctx))
2648 return PTR_ERR(ctx);
2649
2650 /*
2651 * Look up the group leader (we will attach this counter to it):
Ingo Molnar04289bb2008-12-11 08:38:42 +01002652 */
2653 group_leader = NULL;
2654 if (group_fd != -1) {
2655 ret = -EINVAL;
2656 group_file = fget_light(group_fd, &fput_needed);
2657 if (!group_file)
Ingo Molnarccff2862008-12-11 11:26:29 +01002658 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01002659 if (group_file->f_op != &perf_fops)
Ingo Molnarccff2862008-12-11 11:26:29 +01002660 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01002661
2662 group_leader = group_file->private_data;
2663 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01002664 * Do not allow a recursive hierarchy (this new sibling
2665 * becoming part of another group-sibling):
Ingo Molnar04289bb2008-12-11 08:38:42 +01002666 */
Ingo Molnarccff2862008-12-11 11:26:29 +01002667 if (group_leader->group_leader != group_leader)
2668 goto err_put_context;
2669 /*
2670 * Do not allow to attach to a group in a different
2671 * task or CPU context:
2672 */
2673 if (group_leader->ctx != ctx)
2674 goto err_put_context;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11002675 /*
2676 * Only a group leader can be exclusive or pinned
2677 */
2678 if (hw_event.exclusive || hw_event.pinned)
2679 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01002680 }
2681
Paul Mackerras23a185c2009-02-09 22:42:47 +11002682 counter = perf_counter_alloc(&hw_event, cpu, ctx, group_leader,
2683 GFP_KERNEL);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02002684 ret = PTR_ERR(counter);
2685 if (IS_ERR(counter))
Thomas Gleixner0793a612008-12-04 20:12:29 +01002686 goto err_put_context;
2687
Thomas Gleixner0793a612008-12-04 20:12:29 +01002688 ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
2689 if (ret < 0)
Ingo Molnar9b51f662008-12-12 13:49:45 +01002690 goto err_free_put_context;
2691
2692 counter_file = fget_light(ret, &fput_needed2);
2693 if (!counter_file)
2694 goto err_free_put_context;
2695
2696 counter->filp = counter_file;
Paul Mackerrasd859e292009-01-17 18:10:22 +11002697 mutex_lock(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01002698 perf_install_in_context(ctx, counter, cpu);
Paul Mackerrasd859e292009-01-17 18:10:22 +11002699 mutex_unlock(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01002700
2701 fput_light(counter_file, fput_needed2);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002702
Ingo Molnar04289bb2008-12-11 08:38:42 +01002703out_fput:
2704 fput_light(group_file, fput_needed);
2705
Thomas Gleixner0793a612008-12-04 20:12:29 +01002706 return ret;
2707
Ingo Molnar9b51f662008-12-12 13:49:45 +01002708err_free_put_context:
Thomas Gleixner0793a612008-12-04 20:12:29 +01002709 kfree(counter);
2710
2711err_put_context:
2712 put_context(ctx);
2713
Ingo Molnar04289bb2008-12-11 08:38:42 +01002714 goto out_fput;
Thomas Gleixner0793a612008-12-04 20:12:29 +01002715}
2716
Ingo Molnar9b51f662008-12-12 13:49:45 +01002717/*
2718 * Initialize the perf_counter context in a task_struct:
2719 */
2720static void
2721__perf_counter_init_context(struct perf_counter_context *ctx,
2722 struct task_struct *task)
2723{
2724 memset(ctx, 0, sizeof(*ctx));
2725 spin_lock_init(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +11002726 mutex_init(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01002727 INIT_LIST_HEAD(&ctx->counter_list);
Peter Zijlstra592903c2009-03-13 12:21:36 +01002728 INIT_LIST_HEAD(&ctx->event_list);
Ingo Molnar9b51f662008-12-12 13:49:45 +01002729 ctx->task = task;
2730}
2731
2732/*
2733 * inherit a counter from parent task to child task:
2734 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11002735static struct perf_counter *
Ingo Molnar9b51f662008-12-12 13:49:45 +01002736inherit_counter(struct perf_counter *parent_counter,
2737 struct task_struct *parent,
2738 struct perf_counter_context *parent_ctx,
2739 struct task_struct *child,
Paul Mackerrasd859e292009-01-17 18:10:22 +11002740 struct perf_counter *group_leader,
Ingo Molnar9b51f662008-12-12 13:49:45 +01002741 struct perf_counter_context *child_ctx)
2742{
2743 struct perf_counter *child_counter;
2744
Paul Mackerrasd859e292009-01-17 18:10:22 +11002745 /*
2746 * Instead of creating recursive hierarchies of counters,
2747 * we link inherited counters back to the original parent,
2748 * which has a filp for sure, which we use as the reference
2749 * count:
2750 */
2751 if (parent_counter->parent)
2752 parent_counter = parent_counter->parent;
2753
Ingo Molnar9b51f662008-12-12 13:49:45 +01002754 child_counter = perf_counter_alloc(&parent_counter->hw_event,
Paul Mackerras23a185c2009-02-09 22:42:47 +11002755 parent_counter->cpu, child_ctx,
2756 group_leader, GFP_KERNEL);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02002757 if (IS_ERR(child_counter))
2758 return child_counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01002759
2760 /*
2761 * Link it up in the child's context:
2762 */
Ingo Molnar9b51f662008-12-12 13:49:45 +01002763 child_counter->task = child;
Paul Mackerras53cfbf52009-03-25 22:46:58 +11002764 add_counter_to_ctx(child_counter, child_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01002765
2766 child_counter->parent = parent_counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01002767 /*
2768 * inherit into child's child as well:
2769 */
2770 child_counter->hw_event.inherit = 1;
2771
2772 /*
2773 * Get a reference to the parent filp - we will fput it
2774 * when the child counter exits. This is safe to do because
2775 * we are in the parent and we know that the filp still
2776 * exists and has a nonzero count:
2777 */
2778 atomic_long_inc(&parent_counter->filp->f_count);
2779
Paul Mackerrasd859e292009-01-17 18:10:22 +11002780 /*
2781 * Link this into the parent counter's child list
2782 */
2783 mutex_lock(&parent_counter->mutex);
2784 list_add_tail(&child_counter->child_list, &parent_counter->child_list);
2785
2786 /*
2787 * Make the child state follow the state of the parent counter,
2788 * not its hw_event.disabled bit. We hold the parent's mutex,
2789 * so we won't race with perf_counter_{en,dis}able_family.
2790 */
2791 if (parent_counter->state >= PERF_COUNTER_STATE_INACTIVE)
2792 child_counter->state = PERF_COUNTER_STATE_INACTIVE;
2793 else
2794 child_counter->state = PERF_COUNTER_STATE_OFF;
2795
2796 mutex_unlock(&parent_counter->mutex);
2797
2798 return child_counter;
2799}
2800
2801static int inherit_group(struct perf_counter *parent_counter,
2802 struct task_struct *parent,
2803 struct perf_counter_context *parent_ctx,
2804 struct task_struct *child,
2805 struct perf_counter_context *child_ctx)
2806{
2807 struct perf_counter *leader;
2808 struct perf_counter *sub;
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02002809 struct perf_counter *child_ctr;
Paul Mackerrasd859e292009-01-17 18:10:22 +11002810
2811 leader = inherit_counter(parent_counter, parent, parent_ctx,
2812 child, NULL, child_ctx);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02002813 if (IS_ERR(leader))
2814 return PTR_ERR(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +11002815 list_for_each_entry(sub, &parent_counter->sibling_list, list_entry) {
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02002816 child_ctr = inherit_counter(sub, parent, parent_ctx,
2817 child, leader, child_ctx);
2818 if (IS_ERR(child_ctr))
2819 return PTR_ERR(child_ctr);
Paul Mackerrasd859e292009-01-17 18:10:22 +11002820 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01002821 return 0;
2822}
2823
Paul Mackerrasd859e292009-01-17 18:10:22 +11002824static void sync_child_counter(struct perf_counter *child_counter,
2825 struct perf_counter *parent_counter)
2826{
2827 u64 parent_val, child_val;
2828
2829 parent_val = atomic64_read(&parent_counter->count);
2830 child_val = atomic64_read(&child_counter->count);
2831
2832 /*
2833 * Add back the child's count to the parent's count:
2834 */
2835 atomic64_add(child_val, &parent_counter->count);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11002836 atomic64_add(child_counter->total_time_enabled,
2837 &parent_counter->child_total_time_enabled);
2838 atomic64_add(child_counter->total_time_running,
2839 &parent_counter->child_total_time_running);
Paul Mackerrasd859e292009-01-17 18:10:22 +11002840
2841 /*
2842 * Remove this counter from the parent's list
2843 */
2844 mutex_lock(&parent_counter->mutex);
2845 list_del_init(&child_counter->child_list);
2846 mutex_unlock(&parent_counter->mutex);
2847
2848 /*
2849 * Release the parent counter, if this was the last
2850 * reference to it.
2851 */
2852 fput(parent_counter->filp);
2853}
2854
Ingo Molnar9b51f662008-12-12 13:49:45 +01002855static void
2856__perf_counter_exit_task(struct task_struct *child,
2857 struct perf_counter *child_counter,
2858 struct perf_counter_context *child_ctx)
2859{
2860 struct perf_counter *parent_counter;
Paul Mackerrasd859e292009-01-17 18:10:22 +11002861 struct perf_counter *sub, *tmp;
Ingo Molnar9b51f662008-12-12 13:49:45 +01002862
2863 /*
Ingo Molnar235c7fc2008-12-21 14:43:25 +01002864 * If we do not self-reap then we have to wait for the
2865 * child task to unschedule (it will happen for sure),
2866 * so that its counter is at its final count. (This
2867 * condition triggers rarely - child tasks usually get
2868 * off their CPU before the parent has a chance to
2869 * get this far into the reaping action)
Ingo Molnar9b51f662008-12-12 13:49:45 +01002870 */
Ingo Molnar235c7fc2008-12-21 14:43:25 +01002871 if (child != current) {
2872 wait_task_inactive(child, 0);
2873 list_del_init(&child_counter->list_entry);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11002874 update_counter_times(child_counter);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01002875 } else {
Ingo Molnar0cc0c022008-12-14 23:20:36 +01002876 struct perf_cpu_context *cpuctx;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01002877 unsigned long flags;
2878 u64 perf_flags;
2879
2880 /*
2881 * Disable and unlink this counter.
2882 *
2883 * Be careful about zapping the list - IRQ/NMI context
2884 * could still be processing it:
2885 */
Peter Zijlstra849691a2009-04-06 11:45:12 +02002886 local_irq_save(flags);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01002887 perf_flags = hw_perf_save_disable();
Ingo Molnar0cc0c022008-12-14 23:20:36 +01002888
2889 cpuctx = &__get_cpu_var(perf_cpu_context);
2890
Paul Mackerrasd859e292009-01-17 18:10:22 +11002891 group_sched_out(child_counter, cpuctx, child_ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11002892 update_counter_times(child_counter);
Ingo Molnar0cc0c022008-12-14 23:20:36 +01002893
Ingo Molnar235c7fc2008-12-21 14:43:25 +01002894 list_del_init(&child_counter->list_entry);
2895
2896 child_ctx->nr_counters--;
2897
2898 hw_perf_restore(perf_flags);
Peter Zijlstra849691a2009-04-06 11:45:12 +02002899 local_irq_restore(flags);
Ingo Molnar0cc0c022008-12-14 23:20:36 +01002900 }
2901
Ingo Molnar9b51f662008-12-12 13:49:45 +01002902 parent_counter = child_counter->parent;
2903 /*
2904 * It can happen that parent exits first, and has counters
2905 * that are still around due to the child reference. These
2906 * counters need to be zapped - but otherwise linger.
2907 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11002908 if (parent_counter) {
2909 sync_child_counter(child_counter, parent_counter);
2910 list_for_each_entry_safe(sub, tmp, &child_counter->sibling_list,
2911 list_entry) {
Paul Mackerras4bcf3492009-02-11 13:53:19 +01002912 if (sub->parent) {
Paul Mackerrasd859e292009-01-17 18:10:22 +11002913 sync_child_counter(sub, sub->parent);
Peter Zijlstraf1600952009-03-19 20:26:16 +01002914 free_counter(sub);
Paul Mackerras4bcf3492009-02-11 13:53:19 +01002915 }
Paul Mackerrasd859e292009-01-17 18:10:22 +11002916 }
Peter Zijlstraf1600952009-03-19 20:26:16 +01002917 free_counter(child_counter);
Paul Mackerras4bcf3492009-02-11 13:53:19 +01002918 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01002919}
2920
2921/*
Paul Mackerrasd859e292009-01-17 18:10:22 +11002922 * When a child task exits, feed back counter values to parent counters.
Ingo Molnar9b51f662008-12-12 13:49:45 +01002923 *
Paul Mackerrasd859e292009-01-17 18:10:22 +11002924 * Note: we may be running in child context, but the PID is not hashed
Ingo Molnar9b51f662008-12-12 13:49:45 +01002925 * anymore so new counters will not be added.
2926 */
2927void perf_counter_exit_task(struct task_struct *child)
2928{
2929 struct perf_counter *child_counter, *tmp;
2930 struct perf_counter_context *child_ctx;
2931
2932 child_ctx = &child->perf_counter_ctx;
2933
2934 if (likely(!child_ctx->nr_counters))
2935 return;
2936
2937 list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list,
2938 list_entry)
2939 __perf_counter_exit_task(child, child_counter, child_ctx);
2940}
2941
2942/*
2943 * Initialize the perf_counter context in task_struct
2944 */
2945void perf_counter_init_task(struct task_struct *child)
2946{
2947 struct perf_counter_context *child_ctx, *parent_ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +11002948 struct perf_counter *counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01002949 struct task_struct *parent = current;
Ingo Molnar9b51f662008-12-12 13:49:45 +01002950
2951 child_ctx = &child->perf_counter_ctx;
2952 parent_ctx = &parent->perf_counter_ctx;
2953
2954 __perf_counter_init_context(child_ctx, child);
2955
2956 /*
2957 * This is executed from the parent task context, so inherit
2958 * counters that have been marked for cloning:
2959 */
2960
2961 if (likely(!parent_ctx->nr_counters))
2962 return;
2963
2964 /*
2965 * Lock the parent list. No need to lock the child - not PID
2966 * hashed yet and not running, so nobody can access it.
2967 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11002968 mutex_lock(&parent_ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01002969
2970 /*
2971 * We dont have to disable NMIs - we are only looking at
2972 * the list, not manipulating it:
2973 */
2974 list_for_each_entry(counter, &parent_ctx->counter_list, list_entry) {
Paul Mackerrasd859e292009-01-17 18:10:22 +11002975 if (!counter->hw_event.inherit)
Ingo Molnar9b51f662008-12-12 13:49:45 +01002976 continue;
2977
Paul Mackerrasd859e292009-01-17 18:10:22 +11002978 if (inherit_group(counter, parent,
Ingo Molnar9b51f662008-12-12 13:49:45 +01002979 parent_ctx, child, child_ctx))
2980 break;
2981 }
2982
Paul Mackerrasd859e292009-01-17 18:10:22 +11002983 mutex_unlock(&parent_ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01002984}
2985
Ingo Molnar04289bb2008-12-11 08:38:42 +01002986static void __cpuinit perf_counter_init_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01002987{
Ingo Molnar04289bb2008-12-11 08:38:42 +01002988 struct perf_cpu_context *cpuctx;
Thomas Gleixner0793a612008-12-04 20:12:29 +01002989
Ingo Molnar04289bb2008-12-11 08:38:42 +01002990 cpuctx = &per_cpu(perf_cpu_context, cpu);
2991 __perf_counter_init_context(&cpuctx->ctx, NULL);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002992
2993 mutex_lock(&perf_resource_mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01002994 cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu;
Thomas Gleixner0793a612008-12-04 20:12:29 +01002995 mutex_unlock(&perf_resource_mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01002996
Paul Mackerras01d02872009-01-14 13:44:19 +11002997 hw_perf_counter_setup(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002998}
2999
3000#ifdef CONFIG_HOTPLUG_CPU
Ingo Molnar04289bb2008-12-11 08:38:42 +01003001static void __perf_counter_exit_cpu(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003002{
3003 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
3004 struct perf_counter_context *ctx = &cpuctx->ctx;
3005 struct perf_counter *counter, *tmp;
3006
Ingo Molnar04289bb2008-12-11 08:38:42 +01003007 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry)
3008 __perf_counter_remove_from_context(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003009}
Ingo Molnar04289bb2008-12-11 08:38:42 +01003010static void perf_counter_exit_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003011{
Paul Mackerrasd859e292009-01-17 18:10:22 +11003012 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
3013 struct perf_counter_context *ctx = &cpuctx->ctx;
3014
3015 mutex_lock(&ctx->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003016 smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003017 mutex_unlock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003018}
3019#else
Ingo Molnar04289bb2008-12-11 08:38:42 +01003020static inline void perf_counter_exit_cpu(int cpu) { }
Thomas Gleixner0793a612008-12-04 20:12:29 +01003021#endif
3022
3023static int __cpuinit
3024perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
3025{
3026 unsigned int cpu = (long)hcpu;
3027
3028 switch (action) {
3029
3030 case CPU_UP_PREPARE:
3031 case CPU_UP_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01003032 perf_counter_init_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003033 break;
3034
3035 case CPU_DOWN_PREPARE:
3036 case CPU_DOWN_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01003037 perf_counter_exit_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003038 break;
3039
3040 default:
3041 break;
3042 }
3043
3044 return NOTIFY_OK;
3045}
3046
3047static struct notifier_block __cpuinitdata perf_cpu_nb = {
3048 .notifier_call = perf_cpu_notify,
3049};
3050
3051static int __init perf_counter_init(void)
3052{
3053 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
3054 (void *)(long)smp_processor_id());
3055 register_cpu_notifier(&perf_cpu_nb);
3056
3057 return 0;
3058}
3059early_initcall(perf_counter_init);
3060
3061static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
3062{
3063 return sprintf(buf, "%d\n", perf_reserved_percpu);
3064}
3065
3066static ssize_t
3067perf_set_reserve_percpu(struct sysdev_class *class,
3068 const char *buf,
3069 size_t count)
3070{
3071 struct perf_cpu_context *cpuctx;
3072 unsigned long val;
3073 int err, cpu, mpt;
3074
3075 err = strict_strtoul(buf, 10, &val);
3076 if (err)
3077 return err;
3078 if (val > perf_max_counters)
3079 return -EINVAL;
3080
3081 mutex_lock(&perf_resource_mutex);
3082 perf_reserved_percpu = val;
3083 for_each_online_cpu(cpu) {
3084 cpuctx = &per_cpu(perf_cpu_context, cpu);
3085 spin_lock_irq(&cpuctx->ctx.lock);
3086 mpt = min(perf_max_counters - cpuctx->ctx.nr_counters,
3087 perf_max_counters - perf_reserved_percpu);
3088 cpuctx->max_pertask = mpt;
3089 spin_unlock_irq(&cpuctx->ctx.lock);
3090 }
3091 mutex_unlock(&perf_resource_mutex);
3092
3093 return count;
3094}
3095
3096static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
3097{
3098 return sprintf(buf, "%d\n", perf_overcommit);
3099}
3100
3101static ssize_t
3102perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
3103{
3104 unsigned long val;
3105 int err;
3106
3107 err = strict_strtoul(buf, 10, &val);
3108 if (err)
3109 return err;
3110 if (val > 1)
3111 return -EINVAL;
3112
3113 mutex_lock(&perf_resource_mutex);
3114 perf_overcommit = val;
3115 mutex_unlock(&perf_resource_mutex);
3116
3117 return count;
3118}
3119
3120static SYSDEV_CLASS_ATTR(
3121 reserve_percpu,
3122 0644,
3123 perf_show_reserve_percpu,
3124 perf_set_reserve_percpu
3125 );
3126
3127static SYSDEV_CLASS_ATTR(
3128 overcommit,
3129 0644,
3130 perf_show_overcommit,
3131 perf_set_overcommit
3132 );
3133
3134static struct attribute *perfclass_attrs[] = {
3135 &attr_reserve_percpu.attr,
3136 &attr_overcommit.attr,
3137 NULL
3138};
3139
3140static struct attribute_group perfclass_attr_group = {
3141 .attrs = perfclass_attrs,
3142 .name = "perf_counters",
3143};
3144
3145static int __init perf_counter_sysfs_init(void)
3146{
3147 return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
3148 &perfclass_attr_group);
3149}
3150device_initcall(perf_counter_sysfs_init);