blob: ff166c11b69ae6a8f4937debe0f641c2d268ab49 [file] [log] [blame]
Thomas Gleixner0793a612008-12-04 20:12:29 +01001/*
2 * Performance counter core code
3 *
Ingo Molnar98144512009-04-29 14:52:50 +02004 * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
5 * Copyright (C) 2008-2009 Red Hat, Inc., Ingo Molnar
6 * Copyright (C) 2008-2009 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
Paul Mackerrasc5dd0162009-04-30 09:48:16 +10007 * Copyright © 2009 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
Peter Zijlstra7b732a72009-03-23 18:22:10 +01008 *
9 * For licensing details see kernel-base/COPYING
Thomas Gleixner0793a612008-12-04 20:12:29 +010010 */
11
12#include <linux/fs.h>
Peter Zijlstrab9cacc72009-03-25 12:30:22 +010013#include <linux/mm.h>
Thomas Gleixner0793a612008-12-04 20:12:29 +010014#include <linux/cpu.h>
15#include <linux/smp.h>
Ingo Molnar04289bb2008-12-11 08:38:42 +010016#include <linux/file.h>
Thomas Gleixner0793a612008-12-04 20:12:29 +010017#include <linux/poll.h>
18#include <linux/sysfs.h>
19#include <linux/ptrace.h>
20#include <linux/percpu.h>
Peter Zijlstrab9cacc72009-03-25 12:30:22 +010021#include <linux/vmstat.h>
22#include <linux/hardirq.h>
23#include <linux/rculist.h>
Thomas Gleixner0793a612008-12-04 20:12:29 +010024#include <linux/uaccess.h>
25#include <linux/syscalls.h>
26#include <linux/anon_inodes.h>
Ingo Molnaraa9c4c02008-12-17 14:10:57 +010027#include <linux/kernel_stat.h>
Thomas Gleixner0793a612008-12-04 20:12:29 +010028#include <linux/perf_counter.h>
Peter Zijlstra0a4a9392009-03-30 19:07:05 +020029#include <linux/dcache.h>
Thomas Gleixner0793a612008-12-04 20:12:29 +010030
Tim Blechmann4e193bd2009-03-14 14:29:25 +010031#include <asm/irq_regs.h>
32
Thomas Gleixner0793a612008-12-04 20:12:29 +010033/*
34 * Each CPU has a list of per CPU counters:
35 */
36DEFINE_PER_CPU(struct perf_cpu_context, perf_cpu_context);
37
Ingo Molnar088e2852008-12-14 20:21:00 +010038int perf_max_counters __read_mostly = 1;
Thomas Gleixner0793a612008-12-04 20:12:29 +010039static int perf_reserved_percpu __read_mostly;
40static int perf_overcommit __read_mostly = 1;
41
Peter Zijlstra7fc23a52009-05-08 18:52:21 +020042static atomic_t nr_counters __read_mostly;
Peter Zijlstra9ee318a2009-04-09 10:53:44 +020043static atomic_t nr_mmap_tracking __read_mostly;
44static atomic_t nr_munmap_tracking __read_mostly;
45static atomic_t nr_comm_tracking __read_mostly;
46
Peter Zijlstra1ccd1542009-04-09 10:53:45 +020047int sysctl_perf_counter_priv __read_mostly; /* do we need to be privileged */
Peter Zijlstrac5078f72009-05-05 17:50:24 +020048int sysctl_perf_counter_mlock __read_mostly = 128; /* 'free' kb per counter */
Peter Zijlstra1ccd1542009-04-09 10:53:45 +020049
Thomas Gleixner0793a612008-12-04 20:12:29 +010050/*
Ingo Molnar1dce8d92009-05-04 19:23:18 +020051 * Lock for (sysadmin-configurable) counter reservations:
Thomas Gleixner0793a612008-12-04 20:12:29 +010052 */
Ingo Molnar1dce8d92009-05-04 19:23:18 +020053static DEFINE_SPINLOCK(perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +010054
55/*
56 * Architecture provided APIs - weak aliases:
57 */
Robert Richter4aeb0b42009-04-29 12:47:03 +020058extern __weak const struct pmu *hw_perf_counter_init(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +010059{
Paul Mackerrasff6f0542009-01-09 16:19:25 +110060 return NULL;
Thomas Gleixner0793a612008-12-04 20:12:29 +010061}
62
Ingo Molnar01b28382008-12-11 13:45:51 +010063u64 __weak hw_perf_save_disable(void) { return 0; }
Yinghai Lu01ea1cc2008-12-26 21:05:06 -080064void __weak hw_perf_restore(u64 ctrl) { barrier(); }
Paul Mackerras01d02872009-01-14 13:44:19 +110065void __weak hw_perf_counter_setup(int cpu) { barrier(); }
Paul Mackerras3cbed422009-01-09 16:43:42 +110066int __weak hw_perf_group_sched_in(struct perf_counter *group_leader,
67 struct perf_cpu_context *cpuctx,
68 struct perf_counter_context *ctx, int cpu)
69{
70 return 0;
71}
Thomas Gleixner0793a612008-12-04 20:12:29 +010072
Paul Mackerras4eb96fc2009-01-09 17:24:34 +110073void __weak perf_counter_print_debug(void) { }
74
Ingo Molnar04289bb2008-12-11 08:38:42 +010075static void
76list_add_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
77{
78 struct perf_counter *group_leader = counter->group_leader;
79
80 /*
81 * Depending on whether it is a standalone or sibling counter,
82 * add it straight to the context's counter list, or to the group
83 * leader's sibling list:
84 */
Peter Zijlstra3df5eda2009-05-08 18:52:22 +020085 if (group_leader == counter)
Ingo Molnar04289bb2008-12-11 08:38:42 +010086 list_add_tail(&counter->list_entry, &ctx->counter_list);
Peter Zijlstra5c148192009-03-25 12:30:23 +010087 else {
Ingo Molnar04289bb2008-12-11 08:38:42 +010088 list_add_tail(&counter->list_entry, &group_leader->sibling_list);
Peter Zijlstra5c148192009-03-25 12:30:23 +010089 group_leader->nr_siblings++;
90 }
Peter Zijlstra592903c2009-03-13 12:21:36 +010091
92 list_add_rcu(&counter->event_entry, &ctx->event_list);
Ingo Molnar04289bb2008-12-11 08:38:42 +010093}
94
95static void
96list_del_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
97{
98 struct perf_counter *sibling, *tmp;
99
100 list_del_init(&counter->list_entry);
Peter Zijlstra592903c2009-03-13 12:21:36 +0100101 list_del_rcu(&counter->event_entry);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100102
Peter Zijlstra5c148192009-03-25 12:30:23 +0100103 if (counter->group_leader != counter)
104 counter->group_leader->nr_siblings--;
105
Ingo Molnar04289bb2008-12-11 08:38:42 +0100106 /*
107 * If this was a group counter with sibling counters then
108 * upgrade the siblings to singleton counters by adding them
109 * to the context list directly:
110 */
111 list_for_each_entry_safe(sibling, tmp,
112 &counter->sibling_list, list_entry) {
113
Peter Zijlstra75564232009-03-13 12:21:29 +0100114 list_move_tail(&sibling->list_entry, &ctx->counter_list);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100115 sibling->group_leader = sibling;
116 }
117}
118
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100119static void
120counter_sched_out(struct perf_counter *counter,
121 struct perf_cpu_context *cpuctx,
122 struct perf_counter_context *ctx)
123{
124 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
125 return;
126
127 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200128 counter->tstamp_stopped = ctx->time;
Robert Richter4aeb0b42009-04-29 12:47:03 +0200129 counter->pmu->disable(counter);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100130 counter->oncpu = -1;
131
132 if (!is_software_counter(counter))
133 cpuctx->active_oncpu--;
134 ctx->nr_active--;
135 if (counter->hw_event.exclusive || !cpuctx->active_oncpu)
136 cpuctx->exclusive = 0;
137}
138
Paul Mackerrasd859e292009-01-17 18:10:22 +1100139static void
140group_sched_out(struct perf_counter *group_counter,
141 struct perf_cpu_context *cpuctx,
142 struct perf_counter_context *ctx)
143{
144 struct perf_counter *counter;
145
146 if (group_counter->state != PERF_COUNTER_STATE_ACTIVE)
147 return;
148
149 counter_sched_out(group_counter, cpuctx, ctx);
150
151 /*
152 * Schedule out siblings (if any):
153 */
154 list_for_each_entry(counter, &group_counter->sibling_list, list_entry)
155 counter_sched_out(counter, cpuctx, ctx);
156
157 if (group_counter->hw_event.exclusive)
158 cpuctx->exclusive = 0;
159}
160
Thomas Gleixner0793a612008-12-04 20:12:29 +0100161/*
162 * Cross CPU call to remove a performance counter
163 *
164 * We disable the counter on the hardware level first. After that we
165 * remove it from the context list.
166 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100167static void __perf_counter_remove_from_context(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100168{
169 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
170 struct perf_counter *counter = info;
171 struct perf_counter_context *ctx = counter->ctx;
Ingo Molnar9b51f662008-12-12 13:49:45 +0100172 unsigned long flags;
Ingo Molnar5c92d122008-12-11 13:21:10 +0100173 u64 perf_flags;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100174
175 /*
176 * If this is a task context, we need to check whether it is
177 * the current task context of this cpu. If not it has been
178 * scheduled out before the smp call arrived.
179 */
180 if (ctx->task && cpuctx->task_ctx != ctx)
181 return;
182
Peter Zijlstra849691a2009-04-06 11:45:12 +0200183 spin_lock_irqsave(&ctx->lock, flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100184
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100185 counter_sched_out(counter, cpuctx, ctx);
186
187 counter->task = NULL;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100188 ctx->nr_counters--;
189
190 /*
191 * Protect the list operation against NMI by disabling the
192 * counters on a global level. NOP for non NMI based counters.
193 */
Ingo Molnar01b28382008-12-11 13:45:51 +0100194 perf_flags = hw_perf_save_disable();
Ingo Molnar04289bb2008-12-11 08:38:42 +0100195 list_del_counter(counter, ctx);
Ingo Molnar01b28382008-12-11 13:45:51 +0100196 hw_perf_restore(perf_flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100197
198 if (!ctx->task) {
199 /*
200 * Allow more per task counters with respect to the
201 * reservation:
202 */
203 cpuctx->max_pertask =
204 min(perf_max_counters - ctx->nr_counters,
205 perf_max_counters - perf_reserved_percpu);
206 }
207
Peter Zijlstra849691a2009-04-06 11:45:12 +0200208 spin_unlock_irqrestore(&ctx->lock, flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100209}
210
211
212/*
213 * Remove the counter from a task's (or a CPU's) list of counters.
214 *
Paul Mackerrasd859e292009-01-17 18:10:22 +1100215 * Must be called with counter->mutex and ctx->mutex held.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100216 *
217 * CPU counters are removed with a smp call. For task counters we only
218 * call when the task is on a CPU.
219 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100220static void perf_counter_remove_from_context(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100221{
222 struct perf_counter_context *ctx = counter->ctx;
223 struct task_struct *task = ctx->task;
224
225 if (!task) {
226 /*
227 * Per cpu counters are removed via an smp call and
228 * the removal is always sucessful.
229 */
230 smp_call_function_single(counter->cpu,
Ingo Molnar04289bb2008-12-11 08:38:42 +0100231 __perf_counter_remove_from_context,
Thomas Gleixner0793a612008-12-04 20:12:29 +0100232 counter, 1);
233 return;
234 }
235
236retry:
Ingo Molnar04289bb2008-12-11 08:38:42 +0100237 task_oncpu_function_call(task, __perf_counter_remove_from_context,
Thomas Gleixner0793a612008-12-04 20:12:29 +0100238 counter);
239
240 spin_lock_irq(&ctx->lock);
241 /*
242 * If the context is active we need to retry the smp call.
243 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100244 if (ctx->nr_active && !list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100245 spin_unlock_irq(&ctx->lock);
246 goto retry;
247 }
248
249 /*
250 * The lock prevents that this context is scheduled in so we
Ingo Molnar04289bb2008-12-11 08:38:42 +0100251 * can remove the counter safely, if the call above did not
Thomas Gleixner0793a612008-12-04 20:12:29 +0100252 * succeed.
253 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100254 if (!list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100255 ctx->nr_counters--;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100256 list_del_counter(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100257 counter->task = NULL;
258 }
259 spin_unlock_irq(&ctx->lock);
260}
261
Peter Zijlstra4af49982009-04-06 11:45:10 +0200262static inline u64 perf_clock(void)
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100263{
Peter Zijlstra4af49982009-04-06 11:45:10 +0200264 return cpu_clock(smp_processor_id());
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100265}
266
267/*
268 * Update the record of the current time in a context.
269 */
Peter Zijlstra4af49982009-04-06 11:45:10 +0200270static void update_context_time(struct perf_counter_context *ctx)
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100271{
Peter Zijlstra4af49982009-04-06 11:45:10 +0200272 u64 now = perf_clock();
273
274 ctx->time += now - ctx->timestamp;
275 ctx->timestamp = now;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100276}
277
278/*
279 * Update the total_time_enabled and total_time_running fields for a counter.
280 */
281static void update_counter_times(struct perf_counter *counter)
282{
283 struct perf_counter_context *ctx = counter->ctx;
284 u64 run_end;
285
Peter Zijlstra4af49982009-04-06 11:45:10 +0200286 if (counter->state < PERF_COUNTER_STATE_INACTIVE)
287 return;
288
289 counter->total_time_enabled = ctx->time - counter->tstamp_enabled;
290
291 if (counter->state == PERF_COUNTER_STATE_INACTIVE)
292 run_end = counter->tstamp_stopped;
293 else
294 run_end = ctx->time;
295
296 counter->total_time_running = run_end - counter->tstamp_running;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100297}
298
299/*
300 * Update total_time_enabled and total_time_running for all counters in a group.
301 */
302static void update_group_times(struct perf_counter *leader)
303{
304 struct perf_counter *counter;
305
306 update_counter_times(leader);
307 list_for_each_entry(counter, &leader->sibling_list, list_entry)
308 update_counter_times(counter);
309}
310
311/*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100312 * Cross CPU call to disable a performance counter
313 */
314static void __perf_counter_disable(void *info)
315{
316 struct perf_counter *counter = info;
317 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
318 struct perf_counter_context *ctx = counter->ctx;
319 unsigned long flags;
320
321 /*
322 * If this is a per-task counter, need to check whether this
323 * counter's task is the current task on this cpu.
324 */
325 if (ctx->task && cpuctx->task_ctx != ctx)
326 return;
327
Peter Zijlstra849691a2009-04-06 11:45:12 +0200328 spin_lock_irqsave(&ctx->lock, flags);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100329
330 /*
331 * If the counter is on, turn it off.
332 * If it is in error state, leave it in error state.
333 */
334 if (counter->state >= PERF_COUNTER_STATE_INACTIVE) {
Peter Zijlstra4af49982009-04-06 11:45:10 +0200335 update_context_time(ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100336 update_counter_times(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100337 if (counter == counter->group_leader)
338 group_sched_out(counter, cpuctx, ctx);
339 else
340 counter_sched_out(counter, cpuctx, ctx);
341 counter->state = PERF_COUNTER_STATE_OFF;
342 }
343
Peter Zijlstra849691a2009-04-06 11:45:12 +0200344 spin_unlock_irqrestore(&ctx->lock, flags);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100345}
346
347/*
348 * Disable a counter.
349 */
350static void perf_counter_disable(struct perf_counter *counter)
351{
352 struct perf_counter_context *ctx = counter->ctx;
353 struct task_struct *task = ctx->task;
354
355 if (!task) {
356 /*
357 * Disable the counter on the cpu that it's on
358 */
359 smp_call_function_single(counter->cpu, __perf_counter_disable,
360 counter, 1);
361 return;
362 }
363
364 retry:
365 task_oncpu_function_call(task, __perf_counter_disable, counter);
366
367 spin_lock_irq(&ctx->lock);
368 /*
369 * If the counter is still active, we need to retry the cross-call.
370 */
371 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
372 spin_unlock_irq(&ctx->lock);
373 goto retry;
374 }
375
376 /*
377 * Since we have the lock this context can't be scheduled
378 * in, so we can change the state safely.
379 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100380 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
381 update_counter_times(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100382 counter->state = PERF_COUNTER_STATE_OFF;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100383 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100384
385 spin_unlock_irq(&ctx->lock);
386}
387
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100388static int
389counter_sched_in(struct perf_counter *counter,
390 struct perf_cpu_context *cpuctx,
391 struct perf_counter_context *ctx,
392 int cpu)
393{
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100394 if (counter->state <= PERF_COUNTER_STATE_OFF)
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100395 return 0;
396
397 counter->state = PERF_COUNTER_STATE_ACTIVE;
398 counter->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */
399 /*
400 * The new state must be visible before we turn it on in the hardware:
401 */
402 smp_wmb();
403
Robert Richter4aeb0b42009-04-29 12:47:03 +0200404 if (counter->pmu->enable(counter)) {
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100405 counter->state = PERF_COUNTER_STATE_INACTIVE;
406 counter->oncpu = -1;
407 return -EAGAIN;
408 }
409
Peter Zijlstra4af49982009-04-06 11:45:10 +0200410 counter->tstamp_running += ctx->time - counter->tstamp_stopped;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100411
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100412 if (!is_software_counter(counter))
413 cpuctx->active_oncpu++;
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100414 ctx->nr_active++;
415
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100416 if (counter->hw_event.exclusive)
417 cpuctx->exclusive = 1;
418
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100419 return 0;
420}
421
Paul Mackerras6751b712009-05-11 12:08:02 +1000422static int
423group_sched_in(struct perf_counter *group_counter,
424 struct perf_cpu_context *cpuctx,
425 struct perf_counter_context *ctx,
426 int cpu)
427{
428 struct perf_counter *counter, *partial_group;
429 int ret;
430
431 if (group_counter->state == PERF_COUNTER_STATE_OFF)
432 return 0;
433
434 ret = hw_perf_group_sched_in(group_counter, cpuctx, ctx, cpu);
435 if (ret)
436 return ret < 0 ? ret : 0;
437
438 group_counter->prev_state = group_counter->state;
439 if (counter_sched_in(group_counter, cpuctx, ctx, cpu))
440 return -EAGAIN;
441
442 /*
443 * Schedule in siblings as one group (if any):
444 */
445 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
446 counter->prev_state = counter->state;
447 if (counter_sched_in(counter, cpuctx, ctx, cpu)) {
448 partial_group = counter;
449 goto group_error;
450 }
451 }
452
453 return 0;
454
455group_error:
456 /*
457 * Groups can be scheduled in as one unit only, so undo any
458 * partial group before returning:
459 */
460 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
461 if (counter == partial_group)
462 break;
463 counter_sched_out(counter, cpuctx, ctx);
464 }
465 counter_sched_out(group_counter, cpuctx, ctx);
466
467 return -EAGAIN;
468}
469
Thomas Gleixner0793a612008-12-04 20:12:29 +0100470/*
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100471 * Return 1 for a group consisting entirely of software counters,
472 * 0 if the group contains any hardware counters.
473 */
474static int is_software_only_group(struct perf_counter *leader)
475{
476 struct perf_counter *counter;
477
478 if (!is_software_counter(leader))
479 return 0;
Peter Zijlstra5c148192009-03-25 12:30:23 +0100480
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100481 list_for_each_entry(counter, &leader->sibling_list, list_entry)
482 if (!is_software_counter(counter))
483 return 0;
Peter Zijlstra5c148192009-03-25 12:30:23 +0100484
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100485 return 1;
486}
487
488/*
489 * Work out whether we can put this counter group on the CPU now.
490 */
491static int group_can_go_on(struct perf_counter *counter,
492 struct perf_cpu_context *cpuctx,
493 int can_add_hw)
494{
495 /*
496 * Groups consisting entirely of software counters can always go on.
497 */
498 if (is_software_only_group(counter))
499 return 1;
500 /*
501 * If an exclusive group is already on, no other hardware
502 * counters can go on.
503 */
504 if (cpuctx->exclusive)
505 return 0;
506 /*
507 * If this group is exclusive and there are already
508 * counters on the CPU, it can't go on.
509 */
510 if (counter->hw_event.exclusive && cpuctx->active_oncpu)
511 return 0;
512 /*
513 * Otherwise, try to add it if all previous groups were able
514 * to go on.
515 */
516 return can_add_hw;
517}
518
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100519static void add_counter_to_ctx(struct perf_counter *counter,
520 struct perf_counter_context *ctx)
521{
522 list_add_counter(counter, ctx);
523 ctx->nr_counters++;
524 counter->prev_state = PERF_COUNTER_STATE_OFF;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200525 counter->tstamp_enabled = ctx->time;
526 counter->tstamp_running = ctx->time;
527 counter->tstamp_stopped = ctx->time;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100528}
529
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100530/*
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100531 * Cross CPU call to install and enable a performance counter
Thomas Gleixner0793a612008-12-04 20:12:29 +0100532 */
533static void __perf_install_in_context(void *info)
534{
535 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
536 struct perf_counter *counter = info;
537 struct perf_counter_context *ctx = counter->ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100538 struct perf_counter *leader = counter->group_leader;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100539 int cpu = smp_processor_id();
Ingo Molnar9b51f662008-12-12 13:49:45 +0100540 unsigned long flags;
Ingo Molnar5c92d122008-12-11 13:21:10 +0100541 u64 perf_flags;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100542 int err;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100543
544 /*
545 * If this is a task context, we need to check whether it is
546 * the current task context of this cpu. If not it has been
547 * scheduled out before the smp call arrived.
548 */
549 if (ctx->task && cpuctx->task_ctx != ctx)
550 return;
551
Peter Zijlstra849691a2009-04-06 11:45:12 +0200552 spin_lock_irqsave(&ctx->lock, flags);
Peter Zijlstra4af49982009-04-06 11:45:10 +0200553 update_context_time(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100554
555 /*
556 * Protect the list operation against NMI by disabling the
557 * counters on a global level. NOP for non NMI based counters.
558 */
Ingo Molnar01b28382008-12-11 13:45:51 +0100559 perf_flags = hw_perf_save_disable();
Thomas Gleixner0793a612008-12-04 20:12:29 +0100560
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100561 add_counter_to_ctx(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100562
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100563 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100564 * Don't put the counter on if it is disabled or if
565 * it is in a group and the group isn't on.
566 */
567 if (counter->state != PERF_COUNTER_STATE_INACTIVE ||
568 (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE))
569 goto unlock;
570
571 /*
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100572 * An exclusive counter can't go on if there are already active
573 * hardware counters, and no hardware counter can go on if there
574 * is already an exclusive counter on.
575 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100576 if (!group_can_go_on(counter, cpuctx, 1))
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100577 err = -EEXIST;
578 else
579 err = counter_sched_in(counter, cpuctx, ctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100580
Paul Mackerrasd859e292009-01-17 18:10:22 +1100581 if (err) {
582 /*
583 * This counter couldn't go on. If it is in a group
584 * then we have to pull the whole group off.
585 * If the counter group is pinned then put it in error state.
586 */
587 if (leader != counter)
588 group_sched_out(leader, cpuctx, ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100589 if (leader->hw_event.pinned) {
590 update_group_times(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100591 leader->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100592 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100593 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100594
595 if (!err && !ctx->task && cpuctx->max_pertask)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100596 cpuctx->max_pertask--;
597
Paul Mackerrasd859e292009-01-17 18:10:22 +1100598 unlock:
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100599 hw_perf_restore(perf_flags);
600
Peter Zijlstra849691a2009-04-06 11:45:12 +0200601 spin_unlock_irqrestore(&ctx->lock, flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100602}
603
604/*
605 * Attach a performance counter to a context
606 *
607 * First we add the counter to the list with the hardware enable bit
608 * in counter->hw_config cleared.
609 *
610 * If the counter is attached to a task which is on a CPU we use a smp
611 * call to enable it in the task context. The task might have been
612 * scheduled away, but we check this in the smp call again.
Paul Mackerrasd859e292009-01-17 18:10:22 +1100613 *
614 * Must be called with ctx->mutex held.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100615 */
616static void
617perf_install_in_context(struct perf_counter_context *ctx,
618 struct perf_counter *counter,
619 int cpu)
620{
621 struct task_struct *task = ctx->task;
622
Thomas Gleixner0793a612008-12-04 20:12:29 +0100623 if (!task) {
624 /*
625 * Per cpu counters are installed via an smp call and
626 * the install is always sucessful.
627 */
628 smp_call_function_single(cpu, __perf_install_in_context,
629 counter, 1);
630 return;
631 }
632
633 counter->task = task;
634retry:
635 task_oncpu_function_call(task, __perf_install_in_context,
636 counter);
637
638 spin_lock_irq(&ctx->lock);
639 /*
Thomas Gleixner0793a612008-12-04 20:12:29 +0100640 * we need to retry the smp call.
641 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100642 if (ctx->is_active && list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100643 spin_unlock_irq(&ctx->lock);
644 goto retry;
645 }
646
647 /*
648 * The lock prevents that this context is scheduled in so we
649 * can add the counter safely, if it the call above did not
650 * succeed.
651 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100652 if (list_empty(&counter->list_entry))
653 add_counter_to_ctx(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100654 spin_unlock_irq(&ctx->lock);
655}
656
Paul Mackerrasd859e292009-01-17 18:10:22 +1100657/*
658 * Cross CPU call to enable a performance counter
659 */
660static void __perf_counter_enable(void *info)
Ingo Molnar04289bb2008-12-11 08:38:42 +0100661{
Paul Mackerrasd859e292009-01-17 18:10:22 +1100662 struct perf_counter *counter = info;
663 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
664 struct perf_counter_context *ctx = counter->ctx;
665 struct perf_counter *leader = counter->group_leader;
Paul Mackerrase758a332009-05-12 21:59:01 +1000666 unsigned long pmuflags;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100667 unsigned long flags;
668 int err;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100669
670 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100671 * If this is a per-task counter, need to check whether this
672 * counter's task is the current task on this cpu.
Ingo Molnar04289bb2008-12-11 08:38:42 +0100673 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100674 if (ctx->task && cpuctx->task_ctx != ctx)
675 return;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100676
Peter Zijlstra849691a2009-04-06 11:45:12 +0200677 spin_lock_irqsave(&ctx->lock, flags);
Peter Zijlstra4af49982009-04-06 11:45:10 +0200678 update_context_time(ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100679
Paul Mackerrasc07c99b2009-02-13 22:10:34 +1100680 counter->prev_state = counter->state;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100681 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
682 goto unlock;
683 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200684 counter->tstamp_enabled = ctx->time - counter->total_time_enabled;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100685
686 /*
687 * If the counter is in a group and isn't the group leader,
688 * then don't put it on unless the group is on.
689 */
690 if (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE)
691 goto unlock;
692
Paul Mackerrase758a332009-05-12 21:59:01 +1000693 if (!group_can_go_on(counter, cpuctx, 1)) {
Paul Mackerrasd859e292009-01-17 18:10:22 +1100694 err = -EEXIST;
Paul Mackerrase758a332009-05-12 21:59:01 +1000695 } else {
696 pmuflags = hw_perf_save_disable();
697 if (counter == leader)
698 err = group_sched_in(counter, cpuctx, ctx,
699 smp_processor_id());
700 else
701 err = counter_sched_in(counter, cpuctx, ctx,
702 smp_processor_id());
703 hw_perf_restore(pmuflags);
704 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100705
706 if (err) {
707 /*
708 * If this counter can't go on and it's part of a
709 * group, then the whole group has to come off.
710 */
711 if (leader != counter)
712 group_sched_out(leader, cpuctx, ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100713 if (leader->hw_event.pinned) {
714 update_group_times(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100715 leader->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100716 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100717 }
718
719 unlock:
Peter Zijlstra849691a2009-04-06 11:45:12 +0200720 spin_unlock_irqrestore(&ctx->lock, flags);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100721}
722
723/*
724 * Enable a counter.
725 */
726static void perf_counter_enable(struct perf_counter *counter)
727{
728 struct perf_counter_context *ctx = counter->ctx;
729 struct task_struct *task = ctx->task;
730
731 if (!task) {
732 /*
733 * Enable the counter on the cpu that it's on
734 */
735 smp_call_function_single(counter->cpu, __perf_counter_enable,
736 counter, 1);
737 return;
738 }
739
740 spin_lock_irq(&ctx->lock);
741 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
742 goto out;
743
744 /*
745 * If the counter is in error state, clear that first.
746 * That way, if we see the counter in error state below, we
747 * know that it has gone back into error state, as distinct
748 * from the task having been scheduled away before the
749 * cross-call arrived.
750 */
751 if (counter->state == PERF_COUNTER_STATE_ERROR)
752 counter->state = PERF_COUNTER_STATE_OFF;
753
754 retry:
755 spin_unlock_irq(&ctx->lock);
756 task_oncpu_function_call(task, __perf_counter_enable, counter);
757
758 spin_lock_irq(&ctx->lock);
759
760 /*
761 * If the context is active and the counter is still off,
762 * we need to retry the cross-call.
763 */
764 if (ctx->is_active && counter->state == PERF_COUNTER_STATE_OFF)
765 goto retry;
766
767 /*
768 * Since we have the lock this context can't be scheduled
769 * in, so we can change the state safely.
770 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100771 if (counter->state == PERF_COUNTER_STATE_OFF) {
Paul Mackerrasd859e292009-01-17 18:10:22 +1100772 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200773 counter->tstamp_enabled =
774 ctx->time - counter->total_time_enabled;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100775 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100776 out:
777 spin_unlock_irq(&ctx->lock);
778}
779
Peter Zijlstra2023b352009-05-05 17:50:26 +0200780static int perf_counter_refresh(struct perf_counter *counter, int refresh)
Peter Zijlstra79f14642009-04-06 11:45:07 +0200781{
Peter Zijlstra2023b352009-05-05 17:50:26 +0200782 /*
783 * not supported on inherited counters
784 */
785 if (counter->hw_event.inherit)
786 return -EINVAL;
787
Peter Zijlstra79f14642009-04-06 11:45:07 +0200788 atomic_add(refresh, &counter->event_limit);
789 perf_counter_enable(counter);
Peter Zijlstra2023b352009-05-05 17:50:26 +0200790
791 return 0;
Peter Zijlstra79f14642009-04-06 11:45:07 +0200792}
793
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100794void __perf_counter_sched_out(struct perf_counter_context *ctx,
795 struct perf_cpu_context *cpuctx)
796{
797 struct perf_counter *counter;
Paul Mackerras3cbed422009-01-09 16:43:42 +1100798 u64 flags;
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100799
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100800 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100801 ctx->is_active = 0;
802 if (likely(!ctx->nr_counters))
803 goto out;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200804 update_context_time(ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100805
Paul Mackerras3cbed422009-01-09 16:43:42 +1100806 flags = hw_perf_save_disable();
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100807 if (ctx->nr_active) {
808 list_for_each_entry(counter, &ctx->counter_list, list_entry)
809 group_sched_out(counter, cpuctx, ctx);
810 }
Paul Mackerras3cbed422009-01-09 16:43:42 +1100811 hw_perf_restore(flags);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100812 out:
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100813 spin_unlock(&ctx->lock);
814}
815
Thomas Gleixner0793a612008-12-04 20:12:29 +0100816/*
817 * Called from scheduler to remove the counters of the current task,
818 * with interrupts disabled.
819 *
820 * We stop each counter and update the counter value in counter->count.
821 *
Ingo Molnar76715812008-12-17 14:20:28 +0100822 * This does not protect us against NMI, but disable()
Thomas Gleixner0793a612008-12-04 20:12:29 +0100823 * sets the disabled bit in the control field of counter _before_
824 * accessing the counter control register. If a NMI hits, then it will
825 * not restart the counter.
826 */
827void perf_counter_task_sched_out(struct task_struct *task, int cpu)
828{
829 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
830 struct perf_counter_context *ctx = &task->perf_counter_ctx;
Peter Zijlstra4a0deca2009-03-19 20:26:12 +0100831 struct pt_regs *regs;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100832
833 if (likely(!cpuctx->task_ctx))
834 return;
835
Peter Zijlstrabce379b2009-04-06 11:45:13 +0200836 update_context_time(ctx);
837
Peter Zijlstra4a0deca2009-03-19 20:26:12 +0100838 regs = task_pt_regs(task);
Peter Zijlstra78f13e92009-04-08 15:01:33 +0200839 perf_swcounter_event(PERF_COUNT_CONTEXT_SWITCHES, 1, 1, regs, 0);
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100840 __perf_counter_sched_out(ctx, cpuctx);
841
Thomas Gleixner0793a612008-12-04 20:12:29 +0100842 cpuctx->task_ctx = NULL;
843}
844
Paul Mackerrasa08b1592009-05-11 15:46:10 +1000845static void __perf_counter_task_sched_out(struct perf_counter_context *ctx)
846{
847 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
848
849 __perf_counter_sched_out(ctx, cpuctx);
850 cpuctx->task_ctx = NULL;
851}
852
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100853static void perf_counter_cpu_sched_out(struct perf_cpu_context *cpuctx)
Ingo Molnar04289bb2008-12-11 08:38:42 +0100854{
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100855 __perf_counter_sched_out(&cpuctx->ctx, cpuctx);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100856}
857
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100858static void
859__perf_counter_sched_in(struct perf_counter_context *ctx,
860 struct perf_cpu_context *cpuctx, int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100861{
Thomas Gleixner0793a612008-12-04 20:12:29 +0100862 struct perf_counter *counter;
Paul Mackerras3cbed422009-01-09 16:43:42 +1100863 u64 flags;
Paul Mackerrasdd0e6ba2009-01-12 15:11:00 +1100864 int can_add_hw = 1;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100865
Thomas Gleixner0793a612008-12-04 20:12:29 +0100866 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100867 ctx->is_active = 1;
868 if (likely(!ctx->nr_counters))
869 goto out;
870
Peter Zijlstra4af49982009-04-06 11:45:10 +0200871 ctx->timestamp = perf_clock();
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100872
Paul Mackerras3cbed422009-01-09 16:43:42 +1100873 flags = hw_perf_save_disable();
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100874
875 /*
876 * First go through the list and put on any pinned groups
877 * in order to give them the best chance of going on.
878 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100879 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100880 if (counter->state <= PERF_COUNTER_STATE_OFF ||
881 !counter->hw_event.pinned)
882 continue;
883 if (counter->cpu != -1 && counter->cpu != cpu)
884 continue;
885
886 if (group_can_go_on(counter, cpuctx, 1))
887 group_sched_in(counter, cpuctx, ctx, cpu);
888
889 /*
890 * If this pinned group hasn't been scheduled,
891 * put it in error state.
892 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100893 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
894 update_group_times(counter);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100895 counter->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100896 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100897 }
898
899 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
900 /*
901 * Ignore counters in OFF or ERROR state, and
902 * ignore pinned counters since we did them already.
903 */
904 if (counter->state <= PERF_COUNTER_STATE_OFF ||
905 counter->hw_event.pinned)
906 continue;
907
Ingo Molnar04289bb2008-12-11 08:38:42 +0100908 /*
909 * Listen to the 'cpu' scheduling filter constraint
910 * of counters:
911 */
Thomas Gleixner0793a612008-12-04 20:12:29 +0100912 if (counter->cpu != -1 && counter->cpu != cpu)
913 continue;
914
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100915 if (group_can_go_on(counter, cpuctx, can_add_hw)) {
Paul Mackerrasdd0e6ba2009-01-12 15:11:00 +1100916 if (group_sched_in(counter, cpuctx, ctx, cpu))
917 can_add_hw = 0;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100918 }
Thomas Gleixner0793a612008-12-04 20:12:29 +0100919 }
Paul Mackerras3cbed422009-01-09 16:43:42 +1100920 hw_perf_restore(flags);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100921 out:
Thomas Gleixner0793a612008-12-04 20:12:29 +0100922 spin_unlock(&ctx->lock);
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100923}
Ingo Molnar04289bb2008-12-11 08:38:42 +0100924
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100925/*
926 * Called from scheduler to add the counters of the current task
927 * with interrupts disabled.
928 *
929 * We restore the counter value and then enable it.
930 *
931 * This does not protect us against NMI, but enable()
932 * sets the enabled bit in the control field of counter _before_
933 * accessing the counter control register. If a NMI hits, then it will
934 * keep the counter running.
935 */
936void perf_counter_task_sched_in(struct task_struct *task, int cpu)
937{
938 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
939 struct perf_counter_context *ctx = &task->perf_counter_ctx;
940
941 __perf_counter_sched_in(ctx, cpuctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100942 cpuctx->task_ctx = ctx;
943}
944
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100945static void perf_counter_cpu_sched_in(struct perf_cpu_context *cpuctx, int cpu)
946{
947 struct perf_counter_context *ctx = &cpuctx->ctx;
948
949 __perf_counter_sched_in(ctx, cpuctx, cpu);
950}
951
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100952int perf_counter_task_disable(void)
953{
954 struct task_struct *curr = current;
955 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
956 struct perf_counter *counter;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100957 unsigned long flags;
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100958 u64 perf_flags;
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100959
960 if (likely(!ctx->nr_counters))
961 return 0;
962
Peter Zijlstra849691a2009-04-06 11:45:12 +0200963 local_irq_save(flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100964
Paul Mackerrasa08b1592009-05-11 15:46:10 +1000965 __perf_counter_task_sched_out(ctx);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100966
967 spin_lock(&ctx->lock);
968
969 /*
970 * Disable all the counters:
971 */
972 perf_flags = hw_perf_save_disable();
973
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100974 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100975 if (counter->state != PERF_COUNTER_STATE_ERROR) {
976 update_group_times(counter);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100977 counter->state = PERF_COUNTER_STATE_OFF;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100978 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100979 }
Ingo Molnar9b51f662008-12-12 13:49:45 +0100980
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100981 hw_perf_restore(perf_flags);
982
Peter Zijlstra849691a2009-04-06 11:45:12 +0200983 spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100984
985 return 0;
986}
987
988int perf_counter_task_enable(void)
989{
990 struct task_struct *curr = current;
991 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
992 struct perf_counter *counter;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100993 unsigned long flags;
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100994 u64 perf_flags;
995 int cpu;
996
997 if (likely(!ctx->nr_counters))
998 return 0;
999
Peter Zijlstra849691a2009-04-06 11:45:12 +02001000 local_irq_save(flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001001 cpu = smp_processor_id();
1002
Paul Mackerrasa08b1592009-05-11 15:46:10 +10001003 __perf_counter_task_sched_out(ctx);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001004
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001005 spin_lock(&ctx->lock);
1006
1007 /*
1008 * Disable all the counters:
1009 */
1010 perf_flags = hw_perf_save_disable();
1011
1012 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001013 if (counter->state > PERF_COUNTER_STATE_OFF)
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001014 continue;
Ingo Molnar6a930702008-12-11 15:17:03 +01001015 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +02001016 counter->tstamp_enabled =
1017 ctx->time - counter->total_time_enabled;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001018 counter->hw_event.disabled = 0;
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001019 }
1020 hw_perf_restore(perf_flags);
1021
1022 spin_unlock(&ctx->lock);
1023
1024 perf_counter_task_sched_in(curr, cpu);
1025
Peter Zijlstra849691a2009-04-06 11:45:12 +02001026 local_irq_restore(flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001027
1028 return 0;
1029}
1030
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001031/*
1032 * Round-robin a context's counters:
1033 */
1034static void rotate_ctx(struct perf_counter_context *ctx)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001035{
Thomas Gleixner0793a612008-12-04 20:12:29 +01001036 struct perf_counter *counter;
Ingo Molnar5c92d122008-12-11 13:21:10 +01001037 u64 perf_flags;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001038
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001039 if (!ctx->nr_counters)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001040 return;
1041
Thomas Gleixner0793a612008-12-04 20:12:29 +01001042 spin_lock(&ctx->lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001043 /*
Ingo Molnar04289bb2008-12-11 08:38:42 +01001044 * Rotate the first entry last (works just fine for group counters too):
Thomas Gleixner0793a612008-12-04 20:12:29 +01001045 */
Ingo Molnar01b28382008-12-11 13:45:51 +01001046 perf_flags = hw_perf_save_disable();
Ingo Molnar04289bb2008-12-11 08:38:42 +01001047 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Peter Zijlstra75564232009-03-13 12:21:29 +01001048 list_move_tail(&counter->list_entry, &ctx->counter_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001049 break;
1050 }
Ingo Molnar01b28382008-12-11 13:45:51 +01001051 hw_perf_restore(perf_flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001052
1053 spin_unlock(&ctx->lock);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001054}
Thomas Gleixner0793a612008-12-04 20:12:29 +01001055
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001056void perf_counter_task_tick(struct task_struct *curr, int cpu)
1057{
Peter Zijlstra7fc23a52009-05-08 18:52:21 +02001058 struct perf_cpu_context *cpuctx;
1059 struct perf_counter_context *ctx;
1060
1061 if (!atomic_read(&nr_counters))
1062 return;
1063
1064 cpuctx = &per_cpu(perf_cpu_context, cpu);
1065 ctx = &curr->perf_counter_ctx;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001066
Ingo Molnarb82914c2009-05-04 18:54:32 +02001067 perf_counter_cpu_sched_out(cpuctx);
Paul Mackerrasa08b1592009-05-11 15:46:10 +10001068 __perf_counter_task_sched_out(ctx);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001069
Ingo Molnarb82914c2009-05-04 18:54:32 +02001070 rotate_ctx(&cpuctx->ctx);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001071 rotate_ctx(ctx);
1072
Ingo Molnarb82914c2009-05-04 18:54:32 +02001073 perf_counter_cpu_sched_in(cpuctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001074 perf_counter_task_sched_in(curr, cpu);
1075}
1076
1077/*
Thomas Gleixner0793a612008-12-04 20:12:29 +01001078 * Cross CPU call to read the hardware counter
1079 */
Ingo Molnar76715812008-12-17 14:20:28 +01001080static void __read(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001081{
Ingo Molnar621a01e2008-12-11 12:46:46 +01001082 struct perf_counter *counter = info;
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001083 struct perf_counter_context *ctx = counter->ctx;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001084 unsigned long flags;
Ingo Molnar621a01e2008-12-11 12:46:46 +01001085
Peter Zijlstra849691a2009-04-06 11:45:12 +02001086 local_irq_save(flags);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001087 if (ctx->is_active)
Peter Zijlstra4af49982009-04-06 11:45:10 +02001088 update_context_time(ctx);
Robert Richter4aeb0b42009-04-29 12:47:03 +02001089 counter->pmu->read(counter);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001090 update_counter_times(counter);
Peter Zijlstra849691a2009-04-06 11:45:12 +02001091 local_irq_restore(flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001092}
1093
Ingo Molnar04289bb2008-12-11 08:38:42 +01001094static u64 perf_counter_read(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001095{
1096 /*
1097 * If counter is enabled and currently active on a CPU, update the
1098 * value in the counter structure:
1099 */
Ingo Molnar6a930702008-12-11 15:17:03 +01001100 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
Thomas Gleixner0793a612008-12-04 20:12:29 +01001101 smp_call_function_single(counter->oncpu,
Ingo Molnar76715812008-12-17 14:20:28 +01001102 __read, counter, 1);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001103 } else if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
1104 update_counter_times(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001105 }
1106
Ingo Molnaree060942008-12-13 09:00:03 +01001107 return atomic64_read(&counter->count);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001108}
1109
Thomas Gleixner0793a612008-12-04 20:12:29 +01001110static void put_context(struct perf_counter_context *ctx)
1111{
1112 if (ctx->task)
1113 put_task_struct(ctx->task);
1114}
1115
1116static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
1117{
1118 struct perf_cpu_context *cpuctx;
1119 struct perf_counter_context *ctx;
1120 struct task_struct *task;
1121
1122 /*
1123 * If cpu is not a wildcard then this is a percpu counter:
1124 */
1125 if (cpu != -1) {
1126 /* Must be root to operate on a CPU counter: */
Peter Zijlstra1ccd1542009-04-09 10:53:45 +02001127 if (sysctl_perf_counter_priv && !capable(CAP_SYS_ADMIN))
Thomas Gleixner0793a612008-12-04 20:12:29 +01001128 return ERR_PTR(-EACCES);
1129
1130 if (cpu < 0 || cpu > num_possible_cpus())
1131 return ERR_PTR(-EINVAL);
1132
1133 /*
1134 * We could be clever and allow to attach a counter to an
1135 * offline CPU and activate it when the CPU comes up, but
1136 * that's for later.
1137 */
1138 if (!cpu_isset(cpu, cpu_online_map))
1139 return ERR_PTR(-ENODEV);
1140
1141 cpuctx = &per_cpu(perf_cpu_context, cpu);
1142 ctx = &cpuctx->ctx;
1143
Thomas Gleixner0793a612008-12-04 20:12:29 +01001144 return ctx;
1145 }
1146
1147 rcu_read_lock();
1148 if (!pid)
1149 task = current;
1150 else
1151 task = find_task_by_vpid(pid);
1152 if (task)
1153 get_task_struct(task);
1154 rcu_read_unlock();
1155
1156 if (!task)
1157 return ERR_PTR(-ESRCH);
1158
1159 ctx = &task->perf_counter_ctx;
1160 ctx->task = task;
1161
1162 /* Reuse ptrace permission checks for now. */
1163 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
1164 put_context(ctx);
1165 return ERR_PTR(-EACCES);
1166 }
1167
1168 return ctx;
1169}
1170
Peter Zijlstra592903c2009-03-13 12:21:36 +01001171static void free_counter_rcu(struct rcu_head *head)
1172{
1173 struct perf_counter *counter;
1174
1175 counter = container_of(head, struct perf_counter, rcu_head);
1176 kfree(counter);
1177}
1178
Peter Zijlstra925d5192009-03-30 19:07:02 +02001179static void perf_pending_sync(struct perf_counter *counter);
1180
Peter Zijlstraf1600952009-03-19 20:26:16 +01001181static void free_counter(struct perf_counter *counter)
1182{
Peter Zijlstra925d5192009-03-30 19:07:02 +02001183 perf_pending_sync(counter);
1184
Peter Zijlstra7fc23a52009-05-08 18:52:21 +02001185 atomic_dec(&nr_counters);
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02001186 if (counter->hw_event.mmap)
1187 atomic_dec(&nr_mmap_tracking);
1188 if (counter->hw_event.munmap)
1189 atomic_dec(&nr_munmap_tracking);
1190 if (counter->hw_event.comm)
1191 atomic_dec(&nr_comm_tracking);
1192
Peter Zijlstrae077df42009-03-19 20:26:17 +01001193 if (counter->destroy)
1194 counter->destroy(counter);
1195
Peter Zijlstraf1600952009-03-19 20:26:16 +01001196 call_rcu(&counter->rcu_head, free_counter_rcu);
1197}
1198
Thomas Gleixner0793a612008-12-04 20:12:29 +01001199/*
1200 * Called when the last reference to the file is gone.
1201 */
1202static int perf_release(struct inode *inode, struct file *file)
1203{
1204 struct perf_counter *counter = file->private_data;
1205 struct perf_counter_context *ctx = counter->ctx;
1206
1207 file->private_data = NULL;
1208
Paul Mackerrasd859e292009-01-17 18:10:22 +11001209 mutex_lock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001210 mutex_lock(&counter->mutex);
1211
Ingo Molnar04289bb2008-12-11 08:38:42 +01001212 perf_counter_remove_from_context(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001213
1214 mutex_unlock(&counter->mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001215 mutex_unlock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001216
Peter Zijlstraf1600952009-03-19 20:26:16 +01001217 free_counter(counter);
Mike Galbraith5af75912009-02-11 10:53:37 +01001218 put_context(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001219
1220 return 0;
1221}
1222
1223/*
1224 * Read the performance counter - simple non blocking version for now
1225 */
1226static ssize_t
1227perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
1228{
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001229 u64 values[3];
1230 int n;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001231
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001232 /*
1233 * Return end-of-file for a read on a counter that is in
1234 * error state (i.e. because it was pinned but it couldn't be
1235 * scheduled on to the CPU at some point).
1236 */
1237 if (counter->state == PERF_COUNTER_STATE_ERROR)
1238 return 0;
1239
Thomas Gleixner0793a612008-12-04 20:12:29 +01001240 mutex_lock(&counter->mutex);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001241 values[0] = perf_counter_read(counter);
1242 n = 1;
1243 if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1244 values[n++] = counter->total_time_enabled +
1245 atomic64_read(&counter->child_total_time_enabled);
1246 if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1247 values[n++] = counter->total_time_running +
1248 atomic64_read(&counter->child_total_time_running);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001249 mutex_unlock(&counter->mutex);
1250
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001251 if (count < n * sizeof(u64))
1252 return -EINVAL;
1253 count = n * sizeof(u64);
1254
1255 if (copy_to_user(buf, values, count))
1256 return -EFAULT;
1257
1258 return count;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001259}
1260
1261static ssize_t
Thomas Gleixner0793a612008-12-04 20:12:29 +01001262perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
1263{
1264 struct perf_counter *counter = file->private_data;
1265
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001266 return perf_read_hw(counter, buf, count);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001267}
1268
1269static unsigned int perf_poll(struct file *file, poll_table *wait)
1270{
1271 struct perf_counter *counter = file->private_data;
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001272 struct perf_mmap_data *data;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001273 unsigned int events = POLL_HUP;
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001274
1275 rcu_read_lock();
1276 data = rcu_dereference(counter->data);
1277 if (data)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001278 events = atomic_xchg(&data->poll, 0);
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001279 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
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001286static void perf_counter_reset(struct perf_counter *counter)
1287{
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001288 (void)perf_counter_read(counter);
Paul Mackerras615a3f12009-05-11 15:50:21 +10001289 atomic64_set(&counter->count, 0);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001290 perf_counter_update_userpage(counter);
1291}
1292
1293static void perf_counter_for_each_sibling(struct perf_counter *counter,
1294 void (*func)(struct perf_counter *))
1295{
1296 struct perf_counter_context *ctx = counter->ctx;
1297 struct perf_counter *sibling;
1298
1299 spin_lock_irq(&ctx->lock);
1300 counter = counter->group_leader;
1301
1302 func(counter);
1303 list_for_each_entry(sibling, &counter->sibling_list, list_entry)
1304 func(sibling);
1305 spin_unlock_irq(&ctx->lock);
1306}
1307
1308static void perf_counter_for_each_child(struct perf_counter *counter,
1309 void (*func)(struct perf_counter *))
1310{
1311 struct perf_counter *child;
1312
1313 mutex_lock(&counter->mutex);
1314 func(counter);
1315 list_for_each_entry(child, &counter->child_list, child_list)
1316 func(child);
1317 mutex_unlock(&counter->mutex);
1318}
1319
1320static void perf_counter_for_each(struct perf_counter *counter,
1321 void (*func)(struct perf_counter *))
1322{
1323 struct perf_counter *child;
1324
1325 mutex_lock(&counter->mutex);
1326 perf_counter_for_each_sibling(counter, func);
1327 list_for_each_entry(child, &counter->child_list, child_list)
1328 perf_counter_for_each_sibling(child, func);
1329 mutex_unlock(&counter->mutex);
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001330}
1331
Paul Mackerrasd859e292009-01-17 18:10:22 +11001332static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1333{
1334 struct perf_counter *counter = file->private_data;
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001335 void (*func)(struct perf_counter *);
1336 u32 flags = arg;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001337
1338 switch (cmd) {
1339 case PERF_COUNTER_IOC_ENABLE:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001340 func = perf_counter_enable;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001341 break;
1342 case PERF_COUNTER_IOC_DISABLE:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001343 func = perf_counter_disable;
Peter Zijlstra79f14642009-04-06 11:45:07 +02001344 break;
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001345 case PERF_COUNTER_IOC_RESET:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001346 func = perf_counter_reset;
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001347 break;
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001348
1349 case PERF_COUNTER_IOC_REFRESH:
1350 return perf_counter_refresh(counter, arg);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001351 default:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001352 return -ENOTTY;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001353 }
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001354
1355 if (flags & PERF_IOC_FLAG_GROUP)
1356 perf_counter_for_each(counter, func);
1357 else
1358 perf_counter_for_each_child(counter, func);
1359
1360 return 0;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001361}
1362
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001363/*
1364 * Callers need to ensure there can be no nesting of this function, otherwise
1365 * the seqlock logic goes bad. We can not serialize this because the arch
1366 * code calls this from NMI context.
1367 */
1368void perf_counter_update_userpage(struct perf_counter *counter)
Paul Mackerras37d81822009-03-23 18:22:08 +01001369{
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001370 struct perf_mmap_data *data;
1371 struct perf_counter_mmap_page *userpg;
1372
1373 rcu_read_lock();
1374 data = rcu_dereference(counter->data);
1375 if (!data)
1376 goto unlock;
1377
1378 userpg = data->user_page;
Paul Mackerras37d81822009-03-23 18:22:08 +01001379
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001380 /*
1381 * Disable preemption so as to not let the corresponding user-space
1382 * spin too long if we get preempted.
1383 */
1384 preempt_disable();
Paul Mackerras37d81822009-03-23 18:22:08 +01001385 ++userpg->lock;
Peter Zijlstra92f22a32009-04-02 11:12:04 +02001386 barrier();
Paul Mackerras37d81822009-03-23 18:22:08 +01001387 userpg->index = counter->hw.idx;
1388 userpg->offset = atomic64_read(&counter->count);
1389 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
1390 userpg->offset -= atomic64_read(&counter->hw.prev_count);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001391
Peter Zijlstra92f22a32009-04-02 11:12:04 +02001392 barrier();
Paul Mackerras37d81822009-03-23 18:22:08 +01001393 ++userpg->lock;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001394 preempt_enable();
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001395unlock:
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001396 rcu_read_unlock();
Paul Mackerras37d81822009-03-23 18:22:08 +01001397}
1398
1399static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1400{
1401 struct perf_counter *counter = vma->vm_file->private_data;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001402 struct perf_mmap_data *data;
1403 int ret = VM_FAULT_SIGBUS;
Paul Mackerras37d81822009-03-23 18:22:08 +01001404
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001405 rcu_read_lock();
1406 data = rcu_dereference(counter->data);
1407 if (!data)
1408 goto unlock;
Paul Mackerras37d81822009-03-23 18:22:08 +01001409
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001410 if (vmf->pgoff == 0) {
1411 vmf->page = virt_to_page(data->user_page);
1412 } else {
1413 int nr = vmf->pgoff - 1;
1414
1415 if ((unsigned)nr > data->nr_pages)
1416 goto unlock;
1417
1418 vmf->page = virt_to_page(data->data_pages[nr]);
1419 }
Paul Mackerras37d81822009-03-23 18:22:08 +01001420 get_page(vmf->page);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001421 ret = 0;
1422unlock:
1423 rcu_read_unlock();
1424
1425 return ret;
1426}
1427
1428static int perf_mmap_data_alloc(struct perf_counter *counter, int nr_pages)
1429{
1430 struct perf_mmap_data *data;
1431 unsigned long size;
1432 int i;
1433
1434 WARN_ON(atomic_read(&counter->mmap_count));
1435
1436 size = sizeof(struct perf_mmap_data);
1437 size += nr_pages * sizeof(void *);
1438
1439 data = kzalloc(size, GFP_KERNEL);
1440 if (!data)
1441 goto fail;
1442
1443 data->user_page = (void *)get_zeroed_page(GFP_KERNEL);
1444 if (!data->user_page)
1445 goto fail_user_page;
1446
1447 for (i = 0; i < nr_pages; i++) {
1448 data->data_pages[i] = (void *)get_zeroed_page(GFP_KERNEL);
1449 if (!data->data_pages[i])
1450 goto fail_data_pages;
1451 }
1452
1453 data->nr_pages = nr_pages;
Peter Zijlstra22c15582009-05-05 17:50:25 +02001454 atomic_set(&data->lock, -1);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001455
1456 rcu_assign_pointer(counter->data, data);
1457
Paul Mackerras37d81822009-03-23 18:22:08 +01001458 return 0;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001459
1460fail_data_pages:
1461 for (i--; i >= 0; i--)
1462 free_page((unsigned long)data->data_pages[i]);
1463
1464 free_page((unsigned long)data->user_page);
1465
1466fail_user_page:
1467 kfree(data);
1468
1469fail:
1470 return -ENOMEM;
1471}
1472
1473static void __perf_mmap_data_free(struct rcu_head *rcu_head)
1474{
1475 struct perf_mmap_data *data = container_of(rcu_head,
1476 struct perf_mmap_data, rcu_head);
1477 int i;
1478
1479 free_page((unsigned long)data->user_page);
1480 for (i = 0; i < data->nr_pages; i++)
1481 free_page((unsigned long)data->data_pages[i]);
1482 kfree(data);
1483}
1484
1485static void perf_mmap_data_free(struct perf_counter *counter)
1486{
1487 struct perf_mmap_data *data = counter->data;
1488
1489 WARN_ON(atomic_read(&counter->mmap_count));
1490
1491 rcu_assign_pointer(counter->data, NULL);
1492 call_rcu(&data->rcu_head, __perf_mmap_data_free);
1493}
1494
1495static void perf_mmap_open(struct vm_area_struct *vma)
1496{
1497 struct perf_counter *counter = vma->vm_file->private_data;
1498
1499 atomic_inc(&counter->mmap_count);
1500}
1501
1502static void perf_mmap_close(struct vm_area_struct *vma)
1503{
1504 struct perf_counter *counter = vma->vm_file->private_data;
1505
1506 if (atomic_dec_and_mutex_lock(&counter->mmap_count,
1507 &counter->mmap_mutex)) {
Peter Zijlstrac5078f72009-05-05 17:50:24 +02001508 vma->vm_mm->locked_vm -= counter->data->nr_locked;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001509 perf_mmap_data_free(counter);
1510 mutex_unlock(&counter->mmap_mutex);
1511 }
Paul Mackerras37d81822009-03-23 18:22:08 +01001512}
1513
1514static struct vm_operations_struct perf_mmap_vmops = {
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001515 .open = perf_mmap_open,
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001516 .close = perf_mmap_close,
Paul Mackerras37d81822009-03-23 18:22:08 +01001517 .fault = perf_mmap_fault,
1518};
1519
1520static int perf_mmap(struct file *file, struct vm_area_struct *vma)
1521{
1522 struct perf_counter *counter = file->private_data;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001523 unsigned long vma_size;
1524 unsigned long nr_pages;
1525 unsigned long locked, lock_limit;
1526 int ret = 0;
Peter Zijlstrac5078f72009-05-05 17:50:24 +02001527 long extra;
Paul Mackerras37d81822009-03-23 18:22:08 +01001528
1529 if (!(vma->vm_flags & VM_SHARED) || (vma->vm_flags & VM_WRITE))
1530 return -EINVAL;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001531
1532 vma_size = vma->vm_end - vma->vm_start;
1533 nr_pages = (vma_size / PAGE_SIZE) - 1;
1534
Peter Zijlstra7730d862009-03-25 12:48:31 +01001535 /*
1536 * If we have data pages ensure they're a power-of-two number, so we
1537 * can do bitmasks instead of modulo.
1538 */
1539 if (nr_pages != 0 && !is_power_of_2(nr_pages))
Paul Mackerras37d81822009-03-23 18:22:08 +01001540 return -EINVAL;
1541
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001542 if (vma_size != PAGE_SIZE * (1 + nr_pages))
Paul Mackerras37d81822009-03-23 18:22:08 +01001543 return -EINVAL;
1544
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001545 if (vma->vm_pgoff != 0)
1546 return -EINVAL;
Paul Mackerras37d81822009-03-23 18:22:08 +01001547
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001548 mutex_lock(&counter->mmap_mutex);
1549 if (atomic_inc_not_zero(&counter->mmap_count)) {
1550 if (nr_pages != counter->data->nr_pages)
1551 ret = -EINVAL;
1552 goto unlock;
1553 }
1554
Peter Zijlstrac5078f72009-05-05 17:50:24 +02001555 extra = nr_pages /* + 1 only account the data pages */;
1556 extra -= sysctl_perf_counter_mlock >> (PAGE_SHIFT - 10);
1557 if (extra < 0)
1558 extra = 0;
1559
1560 locked = vma->vm_mm->locked_vm + extra;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001561
1562 lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
1563 lock_limit >>= PAGE_SHIFT;
1564
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001565 if ((locked > lock_limit) && !capable(CAP_IPC_LOCK)) {
1566 ret = -EPERM;
1567 goto unlock;
1568 }
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001569
1570 WARN_ON(counter->data);
1571 ret = perf_mmap_data_alloc(counter, nr_pages);
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001572 if (ret)
1573 goto unlock;
1574
1575 atomic_set(&counter->mmap_count, 1);
Peter Zijlstrac5078f72009-05-05 17:50:24 +02001576 vma->vm_mm->locked_vm += extra;
1577 counter->data->nr_locked = extra;
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001578unlock:
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001579 mutex_unlock(&counter->mmap_mutex);
Paul Mackerras37d81822009-03-23 18:22:08 +01001580
1581 vma->vm_flags &= ~VM_MAYWRITE;
1582 vma->vm_flags |= VM_RESERVED;
1583 vma->vm_ops = &perf_mmap_vmops;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001584
1585 return ret;
Paul Mackerras37d81822009-03-23 18:22:08 +01001586}
1587
Peter Zijlstra3c446b32009-04-06 11:45:01 +02001588static int perf_fasync(int fd, struct file *filp, int on)
1589{
1590 struct perf_counter *counter = filp->private_data;
1591 struct inode *inode = filp->f_path.dentry->d_inode;
1592 int retval;
1593
1594 mutex_lock(&inode->i_mutex);
1595 retval = fasync_helper(fd, filp, on, &counter->fasync);
1596 mutex_unlock(&inode->i_mutex);
1597
1598 if (retval < 0)
1599 return retval;
1600
1601 return 0;
1602}
1603
Thomas Gleixner0793a612008-12-04 20:12:29 +01001604static const struct file_operations perf_fops = {
1605 .release = perf_release,
1606 .read = perf_read,
1607 .poll = perf_poll,
Paul Mackerrasd859e292009-01-17 18:10:22 +11001608 .unlocked_ioctl = perf_ioctl,
1609 .compat_ioctl = perf_ioctl,
Paul Mackerras37d81822009-03-23 18:22:08 +01001610 .mmap = perf_mmap,
Peter Zijlstra3c446b32009-04-06 11:45:01 +02001611 .fasync = perf_fasync,
Thomas Gleixner0793a612008-12-04 20:12:29 +01001612};
1613
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001614/*
Peter Zijlstra925d5192009-03-30 19:07:02 +02001615 * Perf counter wakeup
1616 *
1617 * If there's data, ensure we set the poll() state and publish everything
1618 * to user-space before waking everybody up.
1619 */
1620
1621void perf_counter_wakeup(struct perf_counter *counter)
1622{
Peter Zijlstra925d5192009-03-30 19:07:02 +02001623 wake_up_all(&counter->waitq);
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02001624
1625 if (counter->pending_kill) {
1626 kill_fasync(&counter->fasync, SIGIO, counter->pending_kill);
1627 counter->pending_kill = 0;
1628 }
Peter Zijlstra925d5192009-03-30 19:07:02 +02001629}
1630
1631/*
1632 * Pending wakeups
1633 *
1634 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
1635 *
1636 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
1637 * single linked list and use cmpxchg() to add entries lockless.
1638 */
1639
Peter Zijlstra79f14642009-04-06 11:45:07 +02001640static void perf_pending_counter(struct perf_pending_entry *entry)
1641{
1642 struct perf_counter *counter = container_of(entry,
1643 struct perf_counter, pending);
1644
1645 if (counter->pending_disable) {
1646 counter->pending_disable = 0;
1647 perf_counter_disable(counter);
1648 }
1649
1650 if (counter->pending_wakeup) {
1651 counter->pending_wakeup = 0;
1652 perf_counter_wakeup(counter);
1653 }
1654}
1655
Peter Zijlstra671dec52009-04-06 11:45:02 +02001656#define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
Peter Zijlstra925d5192009-03-30 19:07:02 +02001657
Peter Zijlstra671dec52009-04-06 11:45:02 +02001658static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = {
Peter Zijlstra925d5192009-03-30 19:07:02 +02001659 PENDING_TAIL,
1660};
1661
Peter Zijlstra671dec52009-04-06 11:45:02 +02001662static void perf_pending_queue(struct perf_pending_entry *entry,
1663 void (*func)(struct perf_pending_entry *))
Peter Zijlstra925d5192009-03-30 19:07:02 +02001664{
Peter Zijlstra671dec52009-04-06 11:45:02 +02001665 struct perf_pending_entry **head;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001666
Peter Zijlstra671dec52009-04-06 11:45:02 +02001667 if (cmpxchg(&entry->next, NULL, PENDING_TAIL) != NULL)
Peter Zijlstra925d5192009-03-30 19:07:02 +02001668 return;
1669
Peter Zijlstra671dec52009-04-06 11:45:02 +02001670 entry->func = func;
1671
1672 head = &get_cpu_var(perf_pending_head);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001673
1674 do {
Peter Zijlstra671dec52009-04-06 11:45:02 +02001675 entry->next = *head;
1676 } while (cmpxchg(head, entry->next, entry) != entry->next);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001677
1678 set_perf_counter_pending();
1679
Peter Zijlstra671dec52009-04-06 11:45:02 +02001680 put_cpu_var(perf_pending_head);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001681}
1682
1683static int __perf_pending_run(void)
1684{
Peter Zijlstra671dec52009-04-06 11:45:02 +02001685 struct perf_pending_entry *list;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001686 int nr = 0;
1687
Peter Zijlstra671dec52009-04-06 11:45:02 +02001688 list = xchg(&__get_cpu_var(perf_pending_head), PENDING_TAIL);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001689 while (list != PENDING_TAIL) {
Peter Zijlstra671dec52009-04-06 11:45:02 +02001690 void (*func)(struct perf_pending_entry *);
1691 struct perf_pending_entry *entry = list;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001692
1693 list = list->next;
1694
Peter Zijlstra671dec52009-04-06 11:45:02 +02001695 func = entry->func;
1696 entry->next = NULL;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001697 /*
1698 * Ensure we observe the unqueue before we issue the wakeup,
1699 * so that we won't be waiting forever.
1700 * -- see perf_not_pending().
1701 */
1702 smp_wmb();
1703
Peter Zijlstra671dec52009-04-06 11:45:02 +02001704 func(entry);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001705 nr++;
1706 }
1707
1708 return nr;
1709}
1710
1711static inline int perf_not_pending(struct perf_counter *counter)
1712{
1713 /*
1714 * If we flush on whatever cpu we run, there is a chance we don't
1715 * need to wait.
1716 */
1717 get_cpu();
1718 __perf_pending_run();
1719 put_cpu();
1720
1721 /*
1722 * Ensure we see the proper queue state before going to sleep
1723 * so that we do not miss the wakeup. -- see perf_pending_handle()
1724 */
1725 smp_rmb();
Peter Zijlstra671dec52009-04-06 11:45:02 +02001726 return counter->pending.next == NULL;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001727}
1728
1729static void perf_pending_sync(struct perf_counter *counter)
1730{
1731 wait_event(counter->waitq, perf_not_pending(counter));
1732}
1733
1734void perf_counter_do_pending(void)
1735{
1736 __perf_pending_run();
1737}
1738
1739/*
Peter Zijlstra394ee072009-03-30 19:07:14 +02001740 * Callchain support -- arch specific
1741 */
1742
Peter Zijlstra9c03d882009-04-06 11:45:00 +02001743__weak struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
Peter Zijlstra394ee072009-03-30 19:07:14 +02001744{
1745 return NULL;
1746}
1747
1748/*
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001749 * Output
1750 */
1751
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001752struct perf_output_handle {
1753 struct perf_counter *counter;
1754 struct perf_mmap_data *data;
1755 unsigned int offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01001756 unsigned int head;
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001757 int nmi;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02001758 int overflow;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001759 int locked;
1760 unsigned long flags;
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001761};
1762
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001763static void perf_output_wakeup(struct perf_output_handle *handle)
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001764{
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001765 atomic_set(&handle->data->poll, POLL_IN);
1766
Peter Zijlstra671dec52009-04-06 11:45:02 +02001767 if (handle->nmi) {
Peter Zijlstra79f14642009-04-06 11:45:07 +02001768 handle->counter->pending_wakeup = 1;
Peter Zijlstra671dec52009-04-06 11:45:02 +02001769 perf_pending_queue(&handle->counter->pending,
Peter Zijlstra79f14642009-04-06 11:45:07 +02001770 perf_pending_counter);
Peter Zijlstra671dec52009-04-06 11:45:02 +02001771 } else
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001772 perf_counter_wakeup(handle->counter);
1773}
1774
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001775/*
1776 * Curious locking construct.
1777 *
1778 * We need to ensure a later event doesn't publish a head when a former
1779 * event isn't done writing. However since we need to deal with NMIs we
1780 * cannot fully serialize things.
1781 *
1782 * What we do is serialize between CPUs so we only have to deal with NMI
1783 * nesting on a single CPU.
1784 *
1785 * We only publish the head (and generate a wakeup) when the outer-most
1786 * event completes.
1787 */
1788static void perf_output_lock(struct perf_output_handle *handle)
1789{
1790 struct perf_mmap_data *data = handle->data;
1791 int cpu;
1792
1793 handle->locked = 0;
1794
1795 local_irq_save(handle->flags);
1796 cpu = smp_processor_id();
1797
1798 if (in_nmi() && atomic_read(&data->lock) == cpu)
1799 return;
1800
Peter Zijlstra22c15582009-05-05 17:50:25 +02001801 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001802 cpu_relax();
1803
1804 handle->locked = 1;
1805}
1806
1807static void perf_output_unlock(struct perf_output_handle *handle)
1808{
1809 struct perf_mmap_data *data = handle->data;
1810 int head, cpu;
1811
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02001812 data->done_head = data->head;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001813
1814 if (!handle->locked)
1815 goto out;
1816
1817again:
1818 /*
1819 * The xchg implies a full barrier that ensures all writes are done
1820 * before we publish the new head, matched by a rmb() in userspace when
1821 * reading this position.
1822 */
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02001823 while ((head = atomic_xchg(&data->done_head, 0)))
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001824 data->user_page->data_head = head;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001825
1826 /*
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02001827 * NMI can happen here, which means we can miss a done_head update.
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001828 */
1829
Peter Zijlstra22c15582009-05-05 17:50:25 +02001830 cpu = atomic_xchg(&data->lock, -1);
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001831 WARN_ON_ONCE(cpu != smp_processor_id());
1832
1833 /*
1834 * Therefore we have to validate we did not indeed do so.
1835 */
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02001836 if (unlikely(atomic_read(&data->done_head))) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001837 /*
1838 * Since we had it locked, we can lock it again.
1839 */
Peter Zijlstra22c15582009-05-05 17:50:25 +02001840 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001841 cpu_relax();
1842
1843 goto again;
1844 }
1845
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02001846 if (atomic_xchg(&data->wakeup, 0))
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001847 perf_output_wakeup(handle);
1848out:
1849 local_irq_restore(handle->flags);
1850}
1851
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001852static int perf_output_begin(struct perf_output_handle *handle,
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001853 struct perf_counter *counter, unsigned int size,
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02001854 int nmi, int overflow)
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001855{
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001856 struct perf_mmap_data *data;
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001857 unsigned int offset, head;
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001858
Peter Zijlstra2023b352009-05-05 17:50:26 +02001859 /*
1860 * For inherited counters we send all the output towards the parent.
1861 */
1862 if (counter->parent)
1863 counter = counter->parent;
1864
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001865 rcu_read_lock();
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001866 data = rcu_dereference(counter->data);
1867 if (!data)
1868 goto out;
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001869
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001870 handle->data = data;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02001871 handle->counter = counter;
1872 handle->nmi = nmi;
1873 handle->overflow = overflow;
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001874
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001875 if (!data->nr_pages)
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001876 goto fail;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001877
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001878 perf_output_lock(handle);
1879
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001880 do {
1881 offset = head = atomic_read(&data->head);
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001882 head += size;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001883 } while (atomic_cmpxchg(&data->head, offset, head) != offset);
1884
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001885 handle->offset = offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01001886 handle->head = head;
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02001887
1888 if ((offset >> PAGE_SHIFT) != (head >> PAGE_SHIFT))
1889 atomic_set(&data->wakeup, 1);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001890
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001891 return 0;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001892
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001893fail:
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001894 perf_output_wakeup(handle);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001895out:
1896 rcu_read_unlock();
1897
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001898 return -ENOSPC;
1899}
1900
1901static void perf_output_copy(struct perf_output_handle *handle,
1902 void *buf, unsigned int len)
1903{
1904 unsigned int pages_mask;
1905 unsigned int offset;
1906 unsigned int size;
1907 void **pages;
1908
1909 offset = handle->offset;
1910 pages_mask = handle->data->nr_pages - 1;
1911 pages = handle->data->data_pages;
1912
1913 do {
1914 unsigned int page_offset;
1915 int nr;
1916
1917 nr = (offset >> PAGE_SHIFT) & pages_mask;
1918 page_offset = offset & (PAGE_SIZE - 1);
1919 size = min_t(unsigned int, PAGE_SIZE - page_offset, len);
1920
1921 memcpy(pages[nr] + page_offset, buf, size);
1922
1923 len -= size;
1924 buf += size;
1925 offset += size;
1926 } while (len);
1927
1928 handle->offset = offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01001929
1930 WARN_ON_ONCE(handle->offset > handle->head);
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001931}
1932
Peter Zijlstra5c148192009-03-25 12:30:23 +01001933#define perf_output_put(handle, x) \
1934 perf_output_copy((handle), &(x), sizeof(x))
1935
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001936static void perf_output_end(struct perf_output_handle *handle)
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001937{
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001938 struct perf_counter *counter = handle->counter;
1939 struct perf_mmap_data *data = handle->data;
1940
1941 int wakeup_events = counter->hw_event.wakeup_events;
Peter Zijlstrac4578102009-04-02 11:12:01 +02001942
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02001943 if (handle->overflow && wakeup_events) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001944 int events = atomic_inc_return(&data->events);
Peter Zijlstrac4578102009-04-02 11:12:01 +02001945 if (events >= wakeup_events) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001946 atomic_sub(wakeup_events, &data->events);
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02001947 atomic_set(&data->wakeup, 1);
Peter Zijlstrac4578102009-04-02 11:12:01 +02001948 }
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001949 }
1950
1951 perf_output_unlock(handle);
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001952 rcu_read_unlock();
1953}
1954
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02001955static void perf_counter_output(struct perf_counter *counter,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02001956 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001957{
Peter Zijlstra5ed00412009-03-30 19:07:12 +02001958 int ret;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02001959 u64 record_type = counter->hw_event.record_type;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02001960 struct perf_output_handle handle;
1961 struct perf_event_header header;
1962 u64 ip;
Peter Zijlstra5c148192009-03-25 12:30:23 +01001963 struct {
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01001964 u32 pid, tid;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02001965 } tid_entry;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02001966 struct {
1967 u64 event;
1968 u64 counter;
1969 } group_entry;
Peter Zijlstra394ee072009-03-30 19:07:14 +02001970 struct perf_callchain_entry *callchain = NULL;
1971 int callchain_size = 0;
Peter Zijlstra339f7c92009-04-06 11:45:06 +02001972 u64 time;
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02001973 struct {
1974 u32 cpu, reserved;
1975 } cpu_entry;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001976
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02001977 header.type = 0;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02001978 header.size = sizeof(header);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001979
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02001980 header.misc = PERF_EVENT_MISC_OVERFLOW;
1981 header.misc |= user_mode(regs) ?
Peter Zijlstra6fab0192009-04-08 15:01:26 +02001982 PERF_EVENT_MISC_USER : PERF_EVENT_MISC_KERNEL;
1983
Peter Zijlstra8a057d82009-04-02 11:11:59 +02001984 if (record_type & PERF_RECORD_IP) {
1985 ip = instruction_pointer(regs);
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02001986 header.type |= PERF_RECORD_IP;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02001987 header.size += sizeof(ip);
1988 }
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01001989
Peter Zijlstra8a057d82009-04-02 11:11:59 +02001990 if (record_type & PERF_RECORD_TID) {
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01001991 /* namespace issues */
Peter Zijlstra5ed00412009-03-30 19:07:12 +02001992 tid_entry.pid = current->group_leader->pid;
1993 tid_entry.tid = current->pid;
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01001994
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02001995 header.type |= PERF_RECORD_TID;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02001996 header.size += sizeof(tid_entry);
1997 }
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01001998
Peter Zijlstra4d855452009-04-08 15:01:32 +02001999 if (record_type & PERF_RECORD_TIME) {
2000 /*
2001 * Maybe do better on x86 and provide cpu_clock_nmi()
2002 */
2003 time = sched_clock();
2004
2005 header.type |= PERF_RECORD_TIME;
2006 header.size += sizeof(u64);
2007 }
2008
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002009 if (record_type & PERF_RECORD_ADDR) {
2010 header.type |= PERF_RECORD_ADDR;
2011 header.size += sizeof(u64);
2012 }
2013
Peter Zijlstraa85f61a2009-05-08 18:52:23 +02002014 if (record_type & PERF_RECORD_CONFIG) {
2015 header.type |= PERF_RECORD_CONFIG;
2016 header.size += sizeof(u64);
2017 }
2018
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002019 if (record_type & PERF_RECORD_CPU) {
2020 header.type |= PERF_RECORD_CPU;
2021 header.size += sizeof(cpu_entry);
2022
2023 cpu_entry.cpu = raw_smp_processor_id();
2024 }
2025
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002026 if (record_type & PERF_RECORD_GROUP) {
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02002027 header.type |= PERF_RECORD_GROUP;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002028 header.size += sizeof(u64) +
2029 counter->nr_siblings * sizeof(group_entry);
2030 }
2031
2032 if (record_type & PERF_RECORD_CALLCHAIN) {
Peter Zijlstra394ee072009-03-30 19:07:14 +02002033 callchain = perf_callchain(regs);
2034
2035 if (callchain) {
Peter Zijlstra9c03d882009-04-06 11:45:00 +02002036 callchain_size = (1 + callchain->nr) * sizeof(u64);
Peter Zijlstra394ee072009-03-30 19:07:14 +02002037
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02002038 header.type |= PERF_RECORD_CALLCHAIN;
Peter Zijlstra394ee072009-03-30 19:07:14 +02002039 header.size += callchain_size;
2040 }
2041 }
2042
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002043 ret = perf_output_begin(&handle, counter, header.size, nmi, 1);
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002044 if (ret)
2045 return;
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002046
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002047 perf_output_put(&handle, header);
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002048
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002049 if (record_type & PERF_RECORD_IP)
2050 perf_output_put(&handle, ip);
2051
2052 if (record_type & PERF_RECORD_TID)
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002053 perf_output_put(&handle, tid_entry);
2054
Peter Zijlstra4d855452009-04-08 15:01:32 +02002055 if (record_type & PERF_RECORD_TIME)
2056 perf_output_put(&handle, time);
2057
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002058 if (record_type & PERF_RECORD_ADDR)
2059 perf_output_put(&handle, addr);
2060
Peter Zijlstraa85f61a2009-05-08 18:52:23 +02002061 if (record_type & PERF_RECORD_CONFIG)
2062 perf_output_put(&handle, counter->hw_event.config);
2063
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002064 if (record_type & PERF_RECORD_CPU)
2065 perf_output_put(&handle, cpu_entry);
2066
Peter Zijlstra2023b352009-05-05 17:50:26 +02002067 /*
2068 * XXX PERF_RECORD_GROUP vs inherited counters seems difficult.
2069 */
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002070 if (record_type & PERF_RECORD_GROUP) {
2071 struct perf_counter *leader, *sub;
2072 u64 nr = counter->nr_siblings;
2073
2074 perf_output_put(&handle, nr);
2075
2076 leader = counter->group_leader;
2077 list_for_each_entry(sub, &leader->sibling_list, list_entry) {
2078 if (sub != counter)
Robert Richter4aeb0b42009-04-29 12:47:03 +02002079 sub->pmu->read(sub);
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002080
2081 group_entry.event = sub->hw_event.config;
2082 group_entry.counter = atomic64_read(&sub->count);
2083
2084 perf_output_put(&handle, group_entry);
2085 }
2086 }
2087
Peter Zijlstra394ee072009-03-30 19:07:14 +02002088 if (callchain)
2089 perf_output_copy(&handle, callchain, callchain_size);
2090
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002091 perf_output_end(&handle);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002092}
2093
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002094/*
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002095 * comm tracking
2096 */
2097
2098struct perf_comm_event {
2099 struct task_struct *task;
2100 char *comm;
2101 int comm_size;
2102
2103 struct {
2104 struct perf_event_header header;
2105
2106 u32 pid;
2107 u32 tid;
2108 } event;
2109};
2110
2111static void perf_counter_comm_output(struct perf_counter *counter,
2112 struct perf_comm_event *comm_event)
2113{
2114 struct perf_output_handle handle;
2115 int size = comm_event->event.header.size;
2116 int ret = perf_output_begin(&handle, counter, size, 0, 0);
2117
2118 if (ret)
2119 return;
2120
2121 perf_output_put(&handle, comm_event->event);
2122 perf_output_copy(&handle, comm_event->comm,
2123 comm_event->comm_size);
2124 perf_output_end(&handle);
2125}
2126
2127static int perf_counter_comm_match(struct perf_counter *counter,
2128 struct perf_comm_event *comm_event)
2129{
2130 if (counter->hw_event.comm &&
2131 comm_event->event.header.type == PERF_EVENT_COMM)
2132 return 1;
2133
2134 return 0;
2135}
2136
2137static void perf_counter_comm_ctx(struct perf_counter_context *ctx,
2138 struct perf_comm_event *comm_event)
2139{
2140 struct perf_counter *counter;
2141
2142 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2143 return;
2144
2145 rcu_read_lock();
2146 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2147 if (perf_counter_comm_match(counter, comm_event))
2148 perf_counter_comm_output(counter, comm_event);
2149 }
2150 rcu_read_unlock();
2151}
2152
2153static void perf_counter_comm_event(struct perf_comm_event *comm_event)
2154{
2155 struct perf_cpu_context *cpuctx;
2156 unsigned int size;
2157 char *comm = comm_event->task->comm;
2158
Ingo Molnar888fcee2009-04-09 09:48:22 +02002159 size = ALIGN(strlen(comm)+1, sizeof(u64));
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002160
2161 comm_event->comm = comm;
2162 comm_event->comm_size = size;
2163
2164 comm_event->event.header.size = sizeof(comm_event->event) + size;
2165
2166 cpuctx = &get_cpu_var(perf_cpu_context);
2167 perf_counter_comm_ctx(&cpuctx->ctx, comm_event);
2168 put_cpu_var(perf_cpu_context);
2169
2170 perf_counter_comm_ctx(&current->perf_counter_ctx, comm_event);
2171}
2172
2173void perf_counter_comm(struct task_struct *task)
2174{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002175 struct perf_comm_event comm_event;
2176
2177 if (!atomic_read(&nr_comm_tracking))
2178 return;
2179
2180 comm_event = (struct perf_comm_event){
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002181 .task = task,
2182 .event = {
2183 .header = { .type = PERF_EVENT_COMM, },
2184 .pid = task->group_leader->pid,
2185 .tid = task->pid,
2186 },
2187 };
2188
2189 perf_counter_comm_event(&comm_event);
2190}
2191
2192/*
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002193 * mmap tracking
2194 */
2195
2196struct perf_mmap_event {
2197 struct file *file;
2198 char *file_name;
2199 int file_size;
2200
2201 struct {
2202 struct perf_event_header header;
2203
2204 u32 pid;
2205 u32 tid;
2206 u64 start;
2207 u64 len;
2208 u64 pgoff;
2209 } event;
2210};
2211
2212static void perf_counter_mmap_output(struct perf_counter *counter,
2213 struct perf_mmap_event *mmap_event)
2214{
2215 struct perf_output_handle handle;
2216 int size = mmap_event->event.header.size;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002217 int ret = perf_output_begin(&handle, counter, size, 0, 0);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002218
2219 if (ret)
2220 return;
2221
2222 perf_output_put(&handle, mmap_event->event);
2223 perf_output_copy(&handle, mmap_event->file_name,
2224 mmap_event->file_size);
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002225 perf_output_end(&handle);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002226}
2227
2228static int perf_counter_mmap_match(struct perf_counter *counter,
2229 struct perf_mmap_event *mmap_event)
2230{
2231 if (counter->hw_event.mmap &&
2232 mmap_event->event.header.type == PERF_EVENT_MMAP)
2233 return 1;
2234
2235 if (counter->hw_event.munmap &&
2236 mmap_event->event.header.type == PERF_EVENT_MUNMAP)
2237 return 1;
2238
2239 return 0;
2240}
2241
2242static void perf_counter_mmap_ctx(struct perf_counter_context *ctx,
2243 struct perf_mmap_event *mmap_event)
2244{
2245 struct perf_counter *counter;
2246
2247 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2248 return;
2249
2250 rcu_read_lock();
2251 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2252 if (perf_counter_mmap_match(counter, mmap_event))
2253 perf_counter_mmap_output(counter, mmap_event);
2254 }
2255 rcu_read_unlock();
2256}
2257
2258static void perf_counter_mmap_event(struct perf_mmap_event *mmap_event)
2259{
2260 struct perf_cpu_context *cpuctx;
2261 struct file *file = mmap_event->file;
2262 unsigned int size;
2263 char tmp[16];
2264 char *buf = NULL;
2265 char *name;
2266
2267 if (file) {
2268 buf = kzalloc(PATH_MAX, GFP_KERNEL);
2269 if (!buf) {
2270 name = strncpy(tmp, "//enomem", sizeof(tmp));
2271 goto got_name;
2272 }
Peter Zijlstrad3d21c42009-04-09 10:53:46 +02002273 name = d_path(&file->f_path, buf, PATH_MAX);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002274 if (IS_ERR(name)) {
2275 name = strncpy(tmp, "//toolong", sizeof(tmp));
2276 goto got_name;
2277 }
2278 } else {
2279 name = strncpy(tmp, "//anon", sizeof(tmp));
2280 goto got_name;
2281 }
2282
2283got_name:
Ingo Molnar888fcee2009-04-09 09:48:22 +02002284 size = ALIGN(strlen(name)+1, sizeof(u64));
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002285
2286 mmap_event->file_name = name;
2287 mmap_event->file_size = size;
2288
2289 mmap_event->event.header.size = sizeof(mmap_event->event) + size;
2290
2291 cpuctx = &get_cpu_var(perf_cpu_context);
2292 perf_counter_mmap_ctx(&cpuctx->ctx, mmap_event);
2293 put_cpu_var(perf_cpu_context);
2294
2295 perf_counter_mmap_ctx(&current->perf_counter_ctx, mmap_event);
2296
2297 kfree(buf);
2298}
2299
2300void perf_counter_mmap(unsigned long addr, unsigned long len,
2301 unsigned long pgoff, struct file *file)
2302{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002303 struct perf_mmap_event mmap_event;
2304
2305 if (!atomic_read(&nr_mmap_tracking))
2306 return;
2307
2308 mmap_event = (struct perf_mmap_event){
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002309 .file = file,
2310 .event = {
2311 .header = { .type = PERF_EVENT_MMAP, },
2312 .pid = current->group_leader->pid,
2313 .tid = current->pid,
2314 .start = addr,
2315 .len = len,
2316 .pgoff = pgoff,
2317 },
2318 };
2319
2320 perf_counter_mmap_event(&mmap_event);
2321}
2322
2323void perf_counter_munmap(unsigned long addr, unsigned long len,
2324 unsigned long pgoff, struct file *file)
2325{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002326 struct perf_mmap_event mmap_event;
2327
2328 if (!atomic_read(&nr_munmap_tracking))
2329 return;
2330
2331 mmap_event = (struct perf_mmap_event){
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002332 .file = file,
2333 .event = {
2334 .header = { .type = PERF_EVENT_MUNMAP, },
2335 .pid = current->group_leader->pid,
2336 .tid = current->pid,
2337 .start = addr,
2338 .len = len,
2339 .pgoff = pgoff,
2340 },
2341 };
2342
2343 perf_counter_mmap_event(&mmap_event);
2344}
2345
2346/*
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002347 * Generic counter overflow handling.
2348 */
2349
2350int perf_counter_overflow(struct perf_counter *counter,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002351 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002352{
Peter Zijlstra79f14642009-04-06 11:45:07 +02002353 int events = atomic_read(&counter->event_limit);
2354 int ret = 0;
2355
Peter Zijlstra2023b352009-05-05 17:50:26 +02002356 /*
2357 * XXX event_limit might not quite work as expected on inherited
2358 * counters
2359 */
2360
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002361 counter->pending_kill = POLL_IN;
Peter Zijlstra79f14642009-04-06 11:45:07 +02002362 if (events && atomic_dec_and_test(&counter->event_limit)) {
2363 ret = 1;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002364 counter->pending_kill = POLL_HUP;
Peter Zijlstra79f14642009-04-06 11:45:07 +02002365 if (nmi) {
2366 counter->pending_disable = 1;
2367 perf_pending_queue(&counter->pending,
2368 perf_pending_counter);
2369 } else
2370 perf_counter_disable(counter);
2371 }
2372
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002373 perf_counter_output(counter, nmi, regs, addr);
Peter Zijlstra79f14642009-04-06 11:45:07 +02002374 return ret;
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002375}
2376
2377/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002378 * Generic software counter infrastructure
2379 */
2380
2381static void perf_swcounter_update(struct perf_counter *counter)
2382{
2383 struct hw_perf_counter *hwc = &counter->hw;
2384 u64 prev, now;
2385 s64 delta;
2386
2387again:
2388 prev = atomic64_read(&hwc->prev_count);
2389 now = atomic64_read(&hwc->count);
2390 if (atomic64_cmpxchg(&hwc->prev_count, prev, now) != prev)
2391 goto again;
2392
2393 delta = now - prev;
2394
2395 atomic64_add(delta, &counter->count);
2396 atomic64_sub(delta, &hwc->period_left);
2397}
2398
2399static void perf_swcounter_set_period(struct perf_counter *counter)
2400{
2401 struct hw_perf_counter *hwc = &counter->hw;
2402 s64 left = atomic64_read(&hwc->period_left);
2403 s64 period = hwc->irq_period;
2404
2405 if (unlikely(left <= -period)) {
2406 left = period;
2407 atomic64_set(&hwc->period_left, left);
2408 }
2409
2410 if (unlikely(left <= 0)) {
2411 left += period;
2412 atomic64_add(period, &hwc->period_left);
2413 }
2414
2415 atomic64_set(&hwc->prev_count, -left);
2416 atomic64_set(&hwc->count, -left);
2417}
2418
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002419static enum hrtimer_restart perf_swcounter_hrtimer(struct hrtimer *hrtimer)
2420{
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002421 enum hrtimer_restart ret = HRTIMER_RESTART;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002422 struct perf_counter *counter;
2423 struct pt_regs *regs;
2424
2425 counter = container_of(hrtimer, struct perf_counter, hw.hrtimer);
Robert Richter4aeb0b42009-04-29 12:47:03 +02002426 counter->pmu->read(counter);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002427
2428 regs = get_irq_regs();
2429 /*
2430 * In case we exclude kernel IPs or are somehow not in interrupt
2431 * context, provide the next best thing, the user IP.
2432 */
2433 if ((counter->hw_event.exclude_kernel || !regs) &&
2434 !counter->hw_event.exclude_user)
2435 regs = task_pt_regs(current);
2436
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002437 if (regs) {
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002438 if (perf_counter_overflow(counter, 0, regs, 0))
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002439 ret = HRTIMER_NORESTART;
2440 }
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002441
2442 hrtimer_forward_now(hrtimer, ns_to_ktime(counter->hw.irq_period));
2443
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002444 return ret;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002445}
2446
2447static void perf_swcounter_overflow(struct perf_counter *counter,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002448 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002449{
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002450 perf_swcounter_update(counter);
2451 perf_swcounter_set_period(counter);
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002452 if (perf_counter_overflow(counter, nmi, regs, addr))
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002453 /* soft-disable the counter */
2454 ;
2455
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002456}
2457
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002458static int perf_swcounter_match(struct perf_counter *counter,
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002459 enum perf_event_types type,
2460 u32 event, struct pt_regs *regs)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002461{
2462 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
2463 return 0;
2464
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002465 if (perf_event_raw(&counter->hw_event))
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002466 return 0;
2467
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002468 if (perf_event_type(&counter->hw_event) != type)
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002469 return 0;
2470
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002471 if (perf_event_id(&counter->hw_event) != event)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002472 return 0;
2473
2474 if (counter->hw_event.exclude_user && user_mode(regs))
2475 return 0;
2476
2477 if (counter->hw_event.exclude_kernel && !user_mode(regs))
2478 return 0;
2479
2480 return 1;
2481}
2482
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002483static void perf_swcounter_add(struct perf_counter *counter, u64 nr,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002484 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002485{
2486 int neg = atomic64_add_negative(nr, &counter->hw.count);
2487 if (counter->hw.irq_period && !neg)
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002488 perf_swcounter_overflow(counter, nmi, regs, addr);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002489}
2490
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002491static void perf_swcounter_ctx_event(struct perf_counter_context *ctx,
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002492 enum perf_event_types type, u32 event,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002493 u64 nr, int nmi, struct pt_regs *regs,
2494 u64 addr)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002495{
2496 struct perf_counter *counter;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002497
Peter Zijlstra01ef09d2009-03-19 20:26:11 +01002498 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002499 return;
2500
Peter Zijlstra592903c2009-03-13 12:21:36 +01002501 rcu_read_lock();
2502 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002503 if (perf_swcounter_match(counter, type, event, regs))
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002504 perf_swcounter_add(counter, nr, nmi, regs, addr);
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002505 }
Peter Zijlstra592903c2009-03-13 12:21:36 +01002506 rcu_read_unlock();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002507}
2508
Peter Zijlstra96f6d442009-03-23 18:22:07 +01002509static int *perf_swcounter_recursion_context(struct perf_cpu_context *cpuctx)
2510{
2511 if (in_nmi())
2512 return &cpuctx->recursion[3];
2513
2514 if (in_irq())
2515 return &cpuctx->recursion[2];
2516
2517 if (in_softirq())
2518 return &cpuctx->recursion[1];
2519
2520 return &cpuctx->recursion[0];
2521}
2522
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002523static void __perf_swcounter_event(enum perf_event_types type, u32 event,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002524 u64 nr, int nmi, struct pt_regs *regs,
2525 u64 addr)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002526{
2527 struct perf_cpu_context *cpuctx = &get_cpu_var(perf_cpu_context);
Peter Zijlstra96f6d442009-03-23 18:22:07 +01002528 int *recursion = perf_swcounter_recursion_context(cpuctx);
2529
2530 if (*recursion)
2531 goto out;
2532
2533 (*recursion)++;
2534 barrier();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002535
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002536 perf_swcounter_ctx_event(&cpuctx->ctx, type, event,
2537 nr, nmi, regs, addr);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002538 if (cpuctx->task_ctx) {
2539 perf_swcounter_ctx_event(cpuctx->task_ctx, type, event,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002540 nr, nmi, regs, addr);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002541 }
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002542
Peter Zijlstra96f6d442009-03-23 18:22:07 +01002543 barrier();
2544 (*recursion)--;
2545
2546out:
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002547 put_cpu_var(perf_cpu_context);
2548}
2549
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002550void
2551perf_swcounter_event(u32 event, u64 nr, int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002552{
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002553 __perf_swcounter_event(PERF_TYPE_SOFTWARE, event, nr, nmi, regs, addr);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002554}
2555
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002556static void perf_swcounter_read(struct perf_counter *counter)
2557{
2558 perf_swcounter_update(counter);
2559}
2560
2561static int perf_swcounter_enable(struct perf_counter *counter)
2562{
2563 perf_swcounter_set_period(counter);
2564 return 0;
2565}
2566
2567static void perf_swcounter_disable(struct perf_counter *counter)
2568{
2569 perf_swcounter_update(counter);
2570}
2571
Robert Richter4aeb0b42009-04-29 12:47:03 +02002572static const struct pmu perf_ops_generic = {
Peter Zijlstraac17dc82009-03-13 12:21:34 +01002573 .enable = perf_swcounter_enable,
2574 .disable = perf_swcounter_disable,
2575 .read = perf_swcounter_read,
2576};
2577
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002578/*
2579 * Software counter: cpu wall time clock
2580 */
2581
Paul Mackerras9abf8a02009-01-09 16:26:43 +11002582static void cpu_clock_perf_counter_update(struct perf_counter *counter)
2583{
2584 int cpu = raw_smp_processor_id();
2585 s64 prev;
2586 u64 now;
2587
2588 now = cpu_clock(cpu);
2589 prev = atomic64_read(&counter->hw.prev_count);
2590 atomic64_set(&counter->hw.prev_count, now);
2591 atomic64_add(now - prev, &counter->count);
2592}
2593
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002594static int cpu_clock_perf_counter_enable(struct perf_counter *counter)
2595{
2596 struct hw_perf_counter *hwc = &counter->hw;
2597 int cpu = raw_smp_processor_id();
2598
2599 atomic64_set(&hwc->prev_count, cpu_clock(cpu));
Peter Zijlstra039fc912009-03-13 16:43:47 +01002600 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2601 hwc->hrtimer.function = perf_swcounter_hrtimer;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002602 if (hwc->irq_period) {
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002603 __hrtimer_start_range_ns(&hwc->hrtimer,
2604 ns_to_ktime(hwc->irq_period), 0,
2605 HRTIMER_MODE_REL, 0);
2606 }
2607
2608 return 0;
2609}
2610
Ingo Molnar5c92d122008-12-11 13:21:10 +01002611static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
2612{
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002613 hrtimer_cancel(&counter->hw.hrtimer);
Paul Mackerras9abf8a02009-01-09 16:26:43 +11002614 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01002615}
2616
2617static void cpu_clock_perf_counter_read(struct perf_counter *counter)
2618{
Paul Mackerras9abf8a02009-01-09 16:26:43 +11002619 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01002620}
2621
Robert Richter4aeb0b42009-04-29 12:47:03 +02002622static const struct pmu perf_ops_cpu_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +01002623 .enable = cpu_clock_perf_counter_enable,
2624 .disable = cpu_clock_perf_counter_disable,
2625 .read = cpu_clock_perf_counter_read,
Ingo Molnar5c92d122008-12-11 13:21:10 +01002626};
2627
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01002628/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002629 * Software counter: task time clock
2630 */
2631
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02002632static void task_clock_perf_counter_update(struct perf_counter *counter, u64 now)
Ingo Molnarbae43c92008-12-11 14:03:20 +01002633{
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02002634 u64 prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002635 s64 delta;
Ingo Molnarbae43c92008-12-11 14:03:20 +01002636
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02002637 prev = atomic64_xchg(&counter->hw.prev_count, now);
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002638 delta = now - prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002639 atomic64_add(delta, &counter->count);
Ingo Molnarbae43c92008-12-11 14:03:20 +01002640}
2641
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01002642static int task_clock_perf_counter_enable(struct perf_counter *counter)
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002643{
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002644 struct hw_perf_counter *hwc = &counter->hw;
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02002645 u64 now;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002646
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02002647 now = counter->ctx->time;
2648
2649 atomic64_set(&hwc->prev_count, now);
Peter Zijlstra039fc912009-03-13 16:43:47 +01002650 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2651 hwc->hrtimer.function = perf_swcounter_hrtimer;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002652 if (hwc->irq_period) {
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002653 __hrtimer_start_range_ns(&hwc->hrtimer,
2654 ns_to_ktime(hwc->irq_period), 0,
2655 HRTIMER_MODE_REL, 0);
2656 }
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01002657
2658 return 0;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002659}
2660
2661static void task_clock_perf_counter_disable(struct perf_counter *counter)
2662{
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002663 hrtimer_cancel(&counter->hw.hrtimer);
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02002664 task_clock_perf_counter_update(counter, counter->ctx->time);
2665
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002666}
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01002667
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002668static void task_clock_perf_counter_read(struct perf_counter *counter)
2669{
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02002670 u64 time;
2671
2672 if (!in_nmi()) {
2673 update_context_time(counter->ctx);
2674 time = counter->ctx->time;
2675 } else {
2676 u64 now = perf_clock();
2677 u64 delta = now - counter->ctx->timestamp;
2678 time = counter->ctx->time + delta;
2679 }
2680
2681 task_clock_perf_counter_update(counter, time);
Ingo Molnarbae43c92008-12-11 14:03:20 +01002682}
2683
Robert Richter4aeb0b42009-04-29 12:47:03 +02002684static const struct pmu perf_ops_task_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +01002685 .enable = task_clock_perf_counter_enable,
2686 .disable = task_clock_perf_counter_disable,
2687 .read = task_clock_perf_counter_read,
Ingo Molnarbae43c92008-12-11 14:03:20 +01002688};
2689
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002690/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002691 * Software counter: cpu migrations
2692 */
2693
Paul Mackerras23a185c2009-02-09 22:42:47 +11002694static inline u64 get_cpu_migrations(struct perf_counter *counter)
Ingo Molnar6c594c22008-12-14 12:34:15 +01002695{
Paul Mackerras23a185c2009-02-09 22:42:47 +11002696 struct task_struct *curr = counter->ctx->task;
2697
2698 if (curr)
2699 return curr->se.nr_migrations;
2700 return cpu_nr_migrations(smp_processor_id());
Ingo Molnar6c594c22008-12-14 12:34:15 +01002701}
2702
2703static void cpu_migrations_perf_counter_update(struct perf_counter *counter)
2704{
2705 u64 prev, now;
2706 s64 delta;
2707
2708 prev = atomic64_read(&counter->hw.prev_count);
Paul Mackerras23a185c2009-02-09 22:42:47 +11002709 now = get_cpu_migrations(counter);
Ingo Molnar6c594c22008-12-14 12:34:15 +01002710
2711 atomic64_set(&counter->hw.prev_count, now);
2712
2713 delta = now - prev;
Ingo Molnar6c594c22008-12-14 12:34:15 +01002714
2715 atomic64_add(delta, &counter->count);
2716}
2717
2718static void cpu_migrations_perf_counter_read(struct perf_counter *counter)
2719{
2720 cpu_migrations_perf_counter_update(counter);
2721}
2722
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01002723static int cpu_migrations_perf_counter_enable(struct perf_counter *counter)
Ingo Molnar6c594c22008-12-14 12:34:15 +01002724{
Paul Mackerrasc07c99b2009-02-13 22:10:34 +11002725 if (counter->prev_state <= PERF_COUNTER_STATE_OFF)
2726 atomic64_set(&counter->hw.prev_count,
2727 get_cpu_migrations(counter));
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01002728 return 0;
Ingo Molnar6c594c22008-12-14 12:34:15 +01002729}
2730
2731static void cpu_migrations_perf_counter_disable(struct perf_counter *counter)
2732{
2733 cpu_migrations_perf_counter_update(counter);
2734}
2735
Robert Richter4aeb0b42009-04-29 12:47:03 +02002736static const struct pmu perf_ops_cpu_migrations = {
Ingo Molnar76715812008-12-17 14:20:28 +01002737 .enable = cpu_migrations_perf_counter_enable,
2738 .disable = cpu_migrations_perf_counter_disable,
2739 .read = cpu_migrations_perf_counter_read,
Ingo Molnar6c594c22008-12-14 12:34:15 +01002740};
2741
Peter Zijlstrae077df42009-03-19 20:26:17 +01002742#ifdef CONFIG_EVENT_PROFILE
2743void perf_tpcounter_event(int event_id)
2744{
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002745 struct pt_regs *regs = get_irq_regs();
2746
2747 if (!regs)
2748 regs = task_pt_regs(current);
2749
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002750 __perf_swcounter_event(PERF_TYPE_TRACEPOINT, event_id, 1, 1, regs, 0);
Peter Zijlstrae077df42009-03-19 20:26:17 +01002751}
Steven Whitehouseff7b1b42009-04-15 16:55:05 +01002752EXPORT_SYMBOL_GPL(perf_tpcounter_event);
Peter Zijlstrae077df42009-03-19 20:26:17 +01002753
2754extern int ftrace_profile_enable(int);
2755extern void ftrace_profile_disable(int);
2756
2757static void tp_perf_counter_destroy(struct perf_counter *counter)
2758{
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002759 ftrace_profile_disable(perf_event_id(&counter->hw_event));
Peter Zijlstrae077df42009-03-19 20:26:17 +01002760}
2761
Robert Richter4aeb0b42009-04-29 12:47:03 +02002762static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
Peter Zijlstrae077df42009-03-19 20:26:17 +01002763{
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002764 int event_id = perf_event_id(&counter->hw_event);
Peter Zijlstrae077df42009-03-19 20:26:17 +01002765 int ret;
2766
2767 ret = ftrace_profile_enable(event_id);
2768 if (ret)
2769 return NULL;
2770
2771 counter->destroy = tp_perf_counter_destroy;
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002772 counter->hw.irq_period = counter->hw_event.irq_period;
Peter Zijlstrae077df42009-03-19 20:26:17 +01002773
2774 return &perf_ops_generic;
2775}
2776#else
Robert Richter4aeb0b42009-04-29 12:47:03 +02002777static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
Peter Zijlstrae077df42009-03-19 20:26:17 +01002778{
2779 return NULL;
2780}
2781#endif
2782
Robert Richter4aeb0b42009-04-29 12:47:03 +02002783static const struct pmu *sw_perf_counter_init(struct perf_counter *counter)
Ingo Molnar5c92d122008-12-11 13:21:10 +01002784{
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002785 struct perf_counter_hw_event *hw_event = &counter->hw_event;
Robert Richter4aeb0b42009-04-29 12:47:03 +02002786 const struct pmu *pmu = NULL;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002787 struct hw_perf_counter *hwc = &counter->hw;
Ingo Molnar5c92d122008-12-11 13:21:10 +01002788
Paul Mackerras0475f9e2009-02-11 14:35:35 +11002789 /*
2790 * Software counters (currently) can't in general distinguish
2791 * between user, kernel and hypervisor events.
2792 * However, context switches and cpu migrations are considered
2793 * to be kernel events, and page faults are never hypervisor
2794 * events.
2795 */
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002796 switch (perf_event_id(&counter->hw_event)) {
Ingo Molnar5c92d122008-12-11 13:21:10 +01002797 case PERF_COUNT_CPU_CLOCK:
Robert Richter4aeb0b42009-04-29 12:47:03 +02002798 pmu = &perf_ops_cpu_clock;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002799
2800 if (hw_event->irq_period && hw_event->irq_period < 10000)
2801 hw_event->irq_period = 10000;
Ingo Molnar5c92d122008-12-11 13:21:10 +01002802 break;
Ingo Molnarbae43c92008-12-11 14:03:20 +01002803 case PERF_COUNT_TASK_CLOCK:
Paul Mackerras23a185c2009-02-09 22:42:47 +11002804 /*
2805 * If the user instantiates this as a per-cpu counter,
2806 * use the cpu_clock counter instead.
2807 */
2808 if (counter->ctx->task)
Robert Richter4aeb0b42009-04-29 12:47:03 +02002809 pmu = &perf_ops_task_clock;
Paul Mackerras23a185c2009-02-09 22:42:47 +11002810 else
Robert Richter4aeb0b42009-04-29 12:47:03 +02002811 pmu = &perf_ops_cpu_clock;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002812
2813 if (hw_event->irq_period && hw_event->irq_period < 10000)
2814 hw_event->irq_period = 10000;
Ingo Molnarbae43c92008-12-11 14:03:20 +01002815 break;
Ingo Molnare06c61a2008-12-14 14:44:31 +01002816 case PERF_COUNT_PAGE_FAULTS:
Peter Zijlstraac17dc82009-03-13 12:21:34 +01002817 case PERF_COUNT_PAGE_FAULTS_MIN:
2818 case PERF_COUNT_PAGE_FAULTS_MAJ:
Ingo Molnar5d6a27d2008-12-14 12:28:33 +01002819 case PERF_COUNT_CONTEXT_SWITCHES:
Robert Richter4aeb0b42009-04-29 12:47:03 +02002820 pmu = &perf_ops_generic;
Ingo Molnar5d6a27d2008-12-14 12:28:33 +01002821 break;
Ingo Molnar6c594c22008-12-14 12:34:15 +01002822 case PERF_COUNT_CPU_MIGRATIONS:
Paul Mackerras0475f9e2009-02-11 14:35:35 +11002823 if (!counter->hw_event.exclude_kernel)
Robert Richter4aeb0b42009-04-29 12:47:03 +02002824 pmu = &perf_ops_cpu_migrations;
Ingo Molnar6c594c22008-12-14 12:34:15 +01002825 break;
Ingo Molnar5c92d122008-12-11 13:21:10 +01002826 }
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002827
Robert Richter4aeb0b42009-04-29 12:47:03 +02002828 if (pmu)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002829 hwc->irq_period = hw_event->irq_period;
2830
Robert Richter4aeb0b42009-04-29 12:47:03 +02002831 return pmu;
Ingo Molnar5c92d122008-12-11 13:21:10 +01002832}
2833
Thomas Gleixner0793a612008-12-04 20:12:29 +01002834/*
2835 * Allocate and initialize a counter structure
2836 */
2837static struct perf_counter *
Ingo Molnar04289bb2008-12-11 08:38:42 +01002838perf_counter_alloc(struct perf_counter_hw_event *hw_event,
2839 int cpu,
Paul Mackerras23a185c2009-02-09 22:42:47 +11002840 struct perf_counter_context *ctx,
Ingo Molnar9b51f662008-12-12 13:49:45 +01002841 struct perf_counter *group_leader,
2842 gfp_t gfpflags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01002843{
Robert Richter4aeb0b42009-04-29 12:47:03 +02002844 const struct pmu *pmu;
Ingo Molnar621a01e2008-12-11 12:46:46 +01002845 struct perf_counter *counter;
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02002846 long err;
Thomas Gleixner0793a612008-12-04 20:12:29 +01002847
Ingo Molnar9b51f662008-12-12 13:49:45 +01002848 counter = kzalloc(sizeof(*counter), gfpflags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002849 if (!counter)
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02002850 return ERR_PTR(-ENOMEM);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002851
Ingo Molnar04289bb2008-12-11 08:38:42 +01002852 /*
2853 * Single counters are their own group leaders, with an
2854 * empty sibling list:
2855 */
2856 if (!group_leader)
2857 group_leader = counter;
2858
Thomas Gleixner0793a612008-12-04 20:12:29 +01002859 mutex_init(&counter->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01002860 INIT_LIST_HEAD(&counter->list_entry);
Peter Zijlstra592903c2009-03-13 12:21:36 +01002861 INIT_LIST_HEAD(&counter->event_entry);
Ingo Molnar04289bb2008-12-11 08:38:42 +01002862 INIT_LIST_HEAD(&counter->sibling_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002863 init_waitqueue_head(&counter->waitq);
2864
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002865 mutex_init(&counter->mmap_mutex);
2866
Paul Mackerrasd859e292009-01-17 18:10:22 +11002867 INIT_LIST_HEAD(&counter->child_list);
2868
Ingo Molnar9f66a382008-12-10 12:33:23 +01002869 counter->cpu = cpu;
2870 counter->hw_event = *hw_event;
Ingo Molnar04289bb2008-12-11 08:38:42 +01002871 counter->group_leader = group_leader;
Robert Richter4aeb0b42009-04-29 12:47:03 +02002872 counter->pmu = NULL;
Paul Mackerras23a185c2009-02-09 22:42:47 +11002873 counter->ctx = ctx;
Ingo Molnar621a01e2008-12-11 12:46:46 +01002874
Ingo Molnar235c7fc2008-12-21 14:43:25 +01002875 counter->state = PERF_COUNTER_STATE_INACTIVE;
Ingo Molnara86ed502008-12-17 00:43:10 +01002876 if (hw_event->disabled)
2877 counter->state = PERF_COUNTER_STATE_OFF;
2878
Robert Richter4aeb0b42009-04-29 12:47:03 +02002879 pmu = NULL;
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002880
Peter Zijlstra2023b352009-05-05 17:50:26 +02002881 /*
2882 * we currently do not support PERF_RECORD_GROUP on inherited counters
2883 */
2884 if (hw_event->inherit && (hw_event->record_type & PERF_RECORD_GROUP))
2885 goto done;
2886
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002887 if (perf_event_raw(hw_event)) {
Robert Richter4aeb0b42009-04-29 12:47:03 +02002888 pmu = hw_perf_counter_init(counter);
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002889 goto done;
2890 }
2891
2892 switch (perf_event_type(hw_event)) {
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002893 case PERF_TYPE_HARDWARE:
Robert Richter4aeb0b42009-04-29 12:47:03 +02002894 pmu = hw_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002895 break;
2896
2897 case PERF_TYPE_SOFTWARE:
Robert Richter4aeb0b42009-04-29 12:47:03 +02002898 pmu = sw_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002899 break;
2900
2901 case PERF_TYPE_TRACEPOINT:
Robert Richter4aeb0b42009-04-29 12:47:03 +02002902 pmu = tp_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002903 break;
2904 }
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002905done:
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02002906 err = 0;
Robert Richter4aeb0b42009-04-29 12:47:03 +02002907 if (!pmu)
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02002908 err = -EINVAL;
Robert Richter4aeb0b42009-04-29 12:47:03 +02002909 else if (IS_ERR(pmu))
2910 err = PTR_ERR(pmu);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02002911
2912 if (err) {
2913 kfree(counter);
2914 return ERR_PTR(err);
2915 }
2916
Robert Richter4aeb0b42009-04-29 12:47:03 +02002917 counter->pmu = pmu;
Thomas Gleixner0793a612008-12-04 20:12:29 +01002918
Peter Zijlstra7fc23a52009-05-08 18:52:21 +02002919 atomic_inc(&nr_counters);
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002920 if (counter->hw_event.mmap)
2921 atomic_inc(&nr_mmap_tracking);
2922 if (counter->hw_event.munmap)
2923 atomic_inc(&nr_munmap_tracking);
2924 if (counter->hw_event.comm)
2925 atomic_inc(&nr_comm_tracking);
2926
Thomas Gleixner0793a612008-12-04 20:12:29 +01002927 return counter;
2928}
2929
2930/**
Paul Mackerras2743a5b2009-03-04 20:36:51 +11002931 * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
Ingo Molnar9f66a382008-12-10 12:33:23 +01002932 *
2933 * @hw_event_uptr: event type attributes for monitoring/sampling
Thomas Gleixner0793a612008-12-04 20:12:29 +01002934 * @pid: target pid
Ingo Molnar9f66a382008-12-10 12:33:23 +01002935 * @cpu: target cpu
2936 * @group_fd: group leader counter fd
Thomas Gleixner0793a612008-12-04 20:12:29 +01002937 */
Paul Mackerras2743a5b2009-03-04 20:36:51 +11002938SYSCALL_DEFINE5(perf_counter_open,
Paul Mackerrasf3dfd262009-02-26 22:43:46 +11002939 const struct perf_counter_hw_event __user *, hw_event_uptr,
Paul Mackerras2743a5b2009-03-04 20:36:51 +11002940 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01002941{
Ingo Molnar04289bb2008-12-11 08:38:42 +01002942 struct perf_counter *counter, *group_leader;
Ingo Molnar9f66a382008-12-10 12:33:23 +01002943 struct perf_counter_hw_event hw_event;
Ingo Molnar04289bb2008-12-11 08:38:42 +01002944 struct perf_counter_context *ctx;
Ingo Molnar9b51f662008-12-12 13:49:45 +01002945 struct file *counter_file = NULL;
Ingo Molnar04289bb2008-12-11 08:38:42 +01002946 struct file *group_file = NULL;
2947 int fput_needed = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01002948 int fput_needed2 = 0;
Thomas Gleixner0793a612008-12-04 20:12:29 +01002949 int ret;
2950
Paul Mackerras2743a5b2009-03-04 20:36:51 +11002951 /* for future expandability... */
2952 if (flags)
2953 return -EINVAL;
2954
Ingo Molnar9f66a382008-12-10 12:33:23 +01002955 if (copy_from_user(&hw_event, hw_event_uptr, sizeof(hw_event)) != 0)
Thomas Gleixnereab656a2008-12-08 19:26:59 +01002956 return -EFAULT;
2957
Ingo Molnar04289bb2008-12-11 08:38:42 +01002958 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01002959 * Get the target context (task or percpu):
2960 */
2961 ctx = find_get_context(pid, cpu);
2962 if (IS_ERR(ctx))
2963 return PTR_ERR(ctx);
2964
2965 /*
2966 * Look up the group leader (we will attach this counter to it):
Ingo Molnar04289bb2008-12-11 08:38:42 +01002967 */
2968 group_leader = NULL;
2969 if (group_fd != -1) {
2970 ret = -EINVAL;
2971 group_file = fget_light(group_fd, &fput_needed);
2972 if (!group_file)
Ingo Molnarccff2862008-12-11 11:26:29 +01002973 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01002974 if (group_file->f_op != &perf_fops)
Ingo Molnarccff2862008-12-11 11:26:29 +01002975 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01002976
2977 group_leader = group_file->private_data;
2978 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01002979 * Do not allow a recursive hierarchy (this new sibling
2980 * becoming part of another group-sibling):
Ingo Molnar04289bb2008-12-11 08:38:42 +01002981 */
Ingo Molnarccff2862008-12-11 11:26:29 +01002982 if (group_leader->group_leader != group_leader)
2983 goto err_put_context;
2984 /*
2985 * Do not allow to attach to a group in a different
2986 * task or CPU context:
2987 */
2988 if (group_leader->ctx != ctx)
2989 goto err_put_context;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11002990 /*
2991 * Only a group leader can be exclusive or pinned
2992 */
2993 if (hw_event.exclusive || hw_event.pinned)
2994 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01002995 }
2996
Paul Mackerras23a185c2009-02-09 22:42:47 +11002997 counter = perf_counter_alloc(&hw_event, cpu, ctx, group_leader,
2998 GFP_KERNEL);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02002999 ret = PTR_ERR(counter);
3000 if (IS_ERR(counter))
Thomas Gleixner0793a612008-12-04 20:12:29 +01003001 goto err_put_context;
3002
Thomas Gleixner0793a612008-12-04 20:12:29 +01003003 ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
3004 if (ret < 0)
Ingo Molnar9b51f662008-12-12 13:49:45 +01003005 goto err_free_put_context;
3006
3007 counter_file = fget_light(ret, &fput_needed2);
3008 if (!counter_file)
3009 goto err_free_put_context;
3010
3011 counter->filp = counter_file;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003012 mutex_lock(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003013 perf_install_in_context(ctx, counter, cpu);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003014 mutex_unlock(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003015
3016 fput_light(counter_file, fput_needed2);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003017
Ingo Molnar04289bb2008-12-11 08:38:42 +01003018out_fput:
3019 fput_light(group_file, fput_needed);
3020
Thomas Gleixner0793a612008-12-04 20:12:29 +01003021 return ret;
3022
Ingo Molnar9b51f662008-12-12 13:49:45 +01003023err_free_put_context:
Thomas Gleixner0793a612008-12-04 20:12:29 +01003024 kfree(counter);
3025
3026err_put_context:
3027 put_context(ctx);
3028
Ingo Molnar04289bb2008-12-11 08:38:42 +01003029 goto out_fput;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003030}
3031
Ingo Molnar9b51f662008-12-12 13:49:45 +01003032/*
3033 * Initialize the perf_counter context in a task_struct:
3034 */
3035static void
3036__perf_counter_init_context(struct perf_counter_context *ctx,
3037 struct task_struct *task)
3038{
3039 memset(ctx, 0, sizeof(*ctx));
3040 spin_lock_init(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003041 mutex_init(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003042 INIT_LIST_HEAD(&ctx->counter_list);
Peter Zijlstra592903c2009-03-13 12:21:36 +01003043 INIT_LIST_HEAD(&ctx->event_list);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003044 ctx->task = task;
3045}
3046
3047/*
3048 * inherit a counter from parent task to child task:
3049 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11003050static struct perf_counter *
Ingo Molnar9b51f662008-12-12 13:49:45 +01003051inherit_counter(struct perf_counter *parent_counter,
3052 struct task_struct *parent,
3053 struct perf_counter_context *parent_ctx,
3054 struct task_struct *child,
Paul Mackerrasd859e292009-01-17 18:10:22 +11003055 struct perf_counter *group_leader,
Ingo Molnar9b51f662008-12-12 13:49:45 +01003056 struct perf_counter_context *child_ctx)
3057{
3058 struct perf_counter *child_counter;
3059
Paul Mackerrasd859e292009-01-17 18:10:22 +11003060 /*
3061 * Instead of creating recursive hierarchies of counters,
3062 * we link inherited counters back to the original parent,
3063 * which has a filp for sure, which we use as the reference
3064 * count:
3065 */
3066 if (parent_counter->parent)
3067 parent_counter = parent_counter->parent;
3068
Ingo Molnar9b51f662008-12-12 13:49:45 +01003069 child_counter = perf_counter_alloc(&parent_counter->hw_event,
Paul Mackerras23a185c2009-02-09 22:42:47 +11003070 parent_counter->cpu, child_ctx,
3071 group_leader, GFP_KERNEL);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003072 if (IS_ERR(child_counter))
3073 return child_counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003074
3075 /*
3076 * Link it up in the child's context:
3077 */
Ingo Molnar9b51f662008-12-12 13:49:45 +01003078 child_counter->task = child;
Paul Mackerras53cfbf52009-03-25 22:46:58 +11003079 add_counter_to_ctx(child_counter, child_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003080
3081 child_counter->parent = parent_counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003082 /*
3083 * inherit into child's child as well:
3084 */
3085 child_counter->hw_event.inherit = 1;
3086
3087 /*
3088 * Get a reference to the parent filp - we will fput it
3089 * when the child counter exits. This is safe to do because
3090 * we are in the parent and we know that the filp still
3091 * exists and has a nonzero count:
3092 */
3093 atomic_long_inc(&parent_counter->filp->f_count);
3094
Paul Mackerrasd859e292009-01-17 18:10:22 +11003095 /*
3096 * Link this into the parent counter's child list
3097 */
3098 mutex_lock(&parent_counter->mutex);
3099 list_add_tail(&child_counter->child_list, &parent_counter->child_list);
3100
3101 /*
3102 * Make the child state follow the state of the parent counter,
3103 * not its hw_event.disabled bit. We hold the parent's mutex,
3104 * so we won't race with perf_counter_{en,dis}able_family.
3105 */
3106 if (parent_counter->state >= PERF_COUNTER_STATE_INACTIVE)
3107 child_counter->state = PERF_COUNTER_STATE_INACTIVE;
3108 else
3109 child_counter->state = PERF_COUNTER_STATE_OFF;
3110
3111 mutex_unlock(&parent_counter->mutex);
3112
3113 return child_counter;
3114}
3115
3116static int inherit_group(struct perf_counter *parent_counter,
3117 struct task_struct *parent,
3118 struct perf_counter_context *parent_ctx,
3119 struct task_struct *child,
3120 struct perf_counter_context *child_ctx)
3121{
3122 struct perf_counter *leader;
3123 struct perf_counter *sub;
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003124 struct perf_counter *child_ctr;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003125
3126 leader = inherit_counter(parent_counter, parent, parent_ctx,
3127 child, NULL, child_ctx);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003128 if (IS_ERR(leader))
3129 return PTR_ERR(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003130 list_for_each_entry(sub, &parent_counter->sibling_list, list_entry) {
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003131 child_ctr = inherit_counter(sub, parent, parent_ctx,
3132 child, leader, child_ctx);
3133 if (IS_ERR(child_ctr))
3134 return PTR_ERR(child_ctr);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003135 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01003136 return 0;
3137}
3138
Paul Mackerrasd859e292009-01-17 18:10:22 +11003139static void sync_child_counter(struct perf_counter *child_counter,
3140 struct perf_counter *parent_counter)
3141{
3142 u64 parent_val, child_val;
3143
3144 parent_val = atomic64_read(&parent_counter->count);
3145 child_val = atomic64_read(&child_counter->count);
3146
3147 /*
3148 * Add back the child's count to the parent's count:
3149 */
3150 atomic64_add(child_val, &parent_counter->count);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11003151 atomic64_add(child_counter->total_time_enabled,
3152 &parent_counter->child_total_time_enabled);
3153 atomic64_add(child_counter->total_time_running,
3154 &parent_counter->child_total_time_running);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003155
3156 /*
3157 * Remove this counter from the parent's list
3158 */
3159 mutex_lock(&parent_counter->mutex);
3160 list_del_init(&child_counter->child_list);
3161 mutex_unlock(&parent_counter->mutex);
3162
3163 /*
3164 * Release the parent counter, if this was the last
3165 * reference to it.
3166 */
3167 fput(parent_counter->filp);
3168}
3169
Ingo Molnar9b51f662008-12-12 13:49:45 +01003170static void
3171__perf_counter_exit_task(struct task_struct *child,
3172 struct perf_counter *child_counter,
3173 struct perf_counter_context *child_ctx)
3174{
3175 struct perf_counter *parent_counter;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003176 struct perf_counter *sub, *tmp;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003177
3178 /*
Ingo Molnar235c7fc2008-12-21 14:43:25 +01003179 * If we do not self-reap then we have to wait for the
3180 * child task to unschedule (it will happen for sure),
3181 * so that its counter is at its final count. (This
3182 * condition triggers rarely - child tasks usually get
3183 * off their CPU before the parent has a chance to
3184 * get this far into the reaping action)
Ingo Molnar9b51f662008-12-12 13:49:45 +01003185 */
Ingo Molnar235c7fc2008-12-21 14:43:25 +01003186 if (child != current) {
3187 wait_task_inactive(child, 0);
3188 list_del_init(&child_counter->list_entry);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11003189 update_counter_times(child_counter);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01003190 } else {
Ingo Molnar0cc0c022008-12-14 23:20:36 +01003191 struct perf_cpu_context *cpuctx;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01003192 unsigned long flags;
3193 u64 perf_flags;
3194
3195 /*
3196 * Disable and unlink this counter.
3197 *
3198 * Be careful about zapping the list - IRQ/NMI context
3199 * could still be processing it:
3200 */
Peter Zijlstra849691a2009-04-06 11:45:12 +02003201 local_irq_save(flags);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01003202 perf_flags = hw_perf_save_disable();
Ingo Molnar0cc0c022008-12-14 23:20:36 +01003203
3204 cpuctx = &__get_cpu_var(perf_cpu_context);
3205
Paul Mackerrasd859e292009-01-17 18:10:22 +11003206 group_sched_out(child_counter, cpuctx, child_ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11003207 update_counter_times(child_counter);
Ingo Molnar0cc0c022008-12-14 23:20:36 +01003208
Ingo Molnar235c7fc2008-12-21 14:43:25 +01003209 list_del_init(&child_counter->list_entry);
3210
3211 child_ctx->nr_counters--;
3212
3213 hw_perf_restore(perf_flags);
Peter Zijlstra849691a2009-04-06 11:45:12 +02003214 local_irq_restore(flags);
Ingo Molnar0cc0c022008-12-14 23:20:36 +01003215 }
3216
Ingo Molnar9b51f662008-12-12 13:49:45 +01003217 parent_counter = child_counter->parent;
3218 /*
3219 * It can happen that parent exits first, and has counters
3220 * that are still around due to the child reference. These
3221 * counters need to be zapped - but otherwise linger.
3222 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11003223 if (parent_counter) {
3224 sync_child_counter(child_counter, parent_counter);
3225 list_for_each_entry_safe(sub, tmp, &child_counter->sibling_list,
3226 list_entry) {
Paul Mackerras4bcf3492009-02-11 13:53:19 +01003227 if (sub->parent) {
Paul Mackerrasd859e292009-01-17 18:10:22 +11003228 sync_child_counter(sub, sub->parent);
Peter Zijlstraf1600952009-03-19 20:26:16 +01003229 free_counter(sub);
Paul Mackerras4bcf3492009-02-11 13:53:19 +01003230 }
Paul Mackerrasd859e292009-01-17 18:10:22 +11003231 }
Peter Zijlstraf1600952009-03-19 20:26:16 +01003232 free_counter(child_counter);
Paul Mackerras4bcf3492009-02-11 13:53:19 +01003233 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01003234}
3235
3236/*
Paul Mackerrasd859e292009-01-17 18:10:22 +11003237 * When a child task exits, feed back counter values to parent counters.
Ingo Molnar9b51f662008-12-12 13:49:45 +01003238 *
Paul Mackerrasd859e292009-01-17 18:10:22 +11003239 * Note: we may be running in child context, but the PID is not hashed
Ingo Molnar9b51f662008-12-12 13:49:45 +01003240 * anymore so new counters will not be added.
3241 */
3242void perf_counter_exit_task(struct task_struct *child)
3243{
3244 struct perf_counter *child_counter, *tmp;
3245 struct perf_counter_context *child_ctx;
3246
3247 child_ctx = &child->perf_counter_ctx;
3248
3249 if (likely(!child_ctx->nr_counters))
3250 return;
3251
3252 list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list,
3253 list_entry)
3254 __perf_counter_exit_task(child, child_counter, child_ctx);
3255}
3256
3257/*
3258 * Initialize the perf_counter context in task_struct
3259 */
3260void perf_counter_init_task(struct task_struct *child)
3261{
3262 struct perf_counter_context *child_ctx, *parent_ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003263 struct perf_counter *counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003264 struct task_struct *parent = current;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003265
3266 child_ctx = &child->perf_counter_ctx;
3267 parent_ctx = &parent->perf_counter_ctx;
3268
3269 __perf_counter_init_context(child_ctx, child);
3270
3271 /*
3272 * This is executed from the parent task context, so inherit
3273 * counters that have been marked for cloning:
3274 */
3275
3276 if (likely(!parent_ctx->nr_counters))
3277 return;
3278
3279 /*
3280 * Lock the parent list. No need to lock the child - not PID
3281 * hashed yet and not running, so nobody can access it.
3282 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11003283 mutex_lock(&parent_ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003284
3285 /*
3286 * We dont have to disable NMIs - we are only looking at
3287 * the list, not manipulating it:
3288 */
3289 list_for_each_entry(counter, &parent_ctx->counter_list, list_entry) {
Paul Mackerrasd859e292009-01-17 18:10:22 +11003290 if (!counter->hw_event.inherit)
Ingo Molnar9b51f662008-12-12 13:49:45 +01003291 continue;
3292
Paul Mackerrasd859e292009-01-17 18:10:22 +11003293 if (inherit_group(counter, parent,
Ingo Molnar9b51f662008-12-12 13:49:45 +01003294 parent_ctx, child, child_ctx))
3295 break;
3296 }
3297
Paul Mackerrasd859e292009-01-17 18:10:22 +11003298 mutex_unlock(&parent_ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003299}
3300
Ingo Molnar04289bb2008-12-11 08:38:42 +01003301static void __cpuinit perf_counter_init_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003302{
Ingo Molnar04289bb2008-12-11 08:38:42 +01003303 struct perf_cpu_context *cpuctx;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003304
Ingo Molnar04289bb2008-12-11 08:38:42 +01003305 cpuctx = &per_cpu(perf_cpu_context, cpu);
3306 __perf_counter_init_context(&cpuctx->ctx, NULL);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003307
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003308 spin_lock(&perf_resource_lock);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003309 cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu;
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003310 spin_unlock(&perf_resource_lock);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003311
Paul Mackerras01d02872009-01-14 13:44:19 +11003312 hw_perf_counter_setup(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003313}
3314
3315#ifdef CONFIG_HOTPLUG_CPU
Ingo Molnar04289bb2008-12-11 08:38:42 +01003316static void __perf_counter_exit_cpu(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003317{
3318 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
3319 struct perf_counter_context *ctx = &cpuctx->ctx;
3320 struct perf_counter *counter, *tmp;
3321
Ingo Molnar04289bb2008-12-11 08:38:42 +01003322 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry)
3323 __perf_counter_remove_from_context(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003324}
Ingo Molnar04289bb2008-12-11 08:38:42 +01003325static void perf_counter_exit_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003326{
Paul Mackerrasd859e292009-01-17 18:10:22 +11003327 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
3328 struct perf_counter_context *ctx = &cpuctx->ctx;
3329
3330 mutex_lock(&ctx->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003331 smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003332 mutex_unlock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003333}
3334#else
Ingo Molnar04289bb2008-12-11 08:38:42 +01003335static inline void perf_counter_exit_cpu(int cpu) { }
Thomas Gleixner0793a612008-12-04 20:12:29 +01003336#endif
3337
3338static int __cpuinit
3339perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
3340{
3341 unsigned int cpu = (long)hcpu;
3342
3343 switch (action) {
3344
3345 case CPU_UP_PREPARE:
3346 case CPU_UP_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01003347 perf_counter_init_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003348 break;
3349
3350 case CPU_DOWN_PREPARE:
3351 case CPU_DOWN_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01003352 perf_counter_exit_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003353 break;
3354
3355 default:
3356 break;
3357 }
3358
3359 return NOTIFY_OK;
3360}
3361
3362static struct notifier_block __cpuinitdata perf_cpu_nb = {
3363 .notifier_call = perf_cpu_notify,
3364};
3365
Ingo Molnar0d905bc2009-05-04 19:13:30 +02003366void __init perf_counter_init(void)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003367{
3368 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
3369 (void *)(long)smp_processor_id());
3370 register_cpu_notifier(&perf_cpu_nb);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003371}
Thomas Gleixner0793a612008-12-04 20:12:29 +01003372
3373static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
3374{
3375 return sprintf(buf, "%d\n", perf_reserved_percpu);
3376}
3377
3378static ssize_t
3379perf_set_reserve_percpu(struct sysdev_class *class,
3380 const char *buf,
3381 size_t count)
3382{
3383 struct perf_cpu_context *cpuctx;
3384 unsigned long val;
3385 int err, cpu, mpt;
3386
3387 err = strict_strtoul(buf, 10, &val);
3388 if (err)
3389 return err;
3390 if (val > perf_max_counters)
3391 return -EINVAL;
3392
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003393 spin_lock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003394 perf_reserved_percpu = val;
3395 for_each_online_cpu(cpu) {
3396 cpuctx = &per_cpu(perf_cpu_context, cpu);
3397 spin_lock_irq(&cpuctx->ctx.lock);
3398 mpt = min(perf_max_counters - cpuctx->ctx.nr_counters,
3399 perf_max_counters - perf_reserved_percpu);
3400 cpuctx->max_pertask = mpt;
3401 spin_unlock_irq(&cpuctx->ctx.lock);
3402 }
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003403 spin_unlock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003404
3405 return count;
3406}
3407
3408static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
3409{
3410 return sprintf(buf, "%d\n", perf_overcommit);
3411}
3412
3413static ssize_t
3414perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
3415{
3416 unsigned long val;
3417 int err;
3418
3419 err = strict_strtoul(buf, 10, &val);
3420 if (err)
3421 return err;
3422 if (val > 1)
3423 return -EINVAL;
3424
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003425 spin_lock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003426 perf_overcommit = val;
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003427 spin_unlock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003428
3429 return count;
3430}
3431
3432static SYSDEV_CLASS_ATTR(
3433 reserve_percpu,
3434 0644,
3435 perf_show_reserve_percpu,
3436 perf_set_reserve_percpu
3437 );
3438
3439static SYSDEV_CLASS_ATTR(
3440 overcommit,
3441 0644,
3442 perf_show_overcommit,
3443 perf_set_overcommit
3444 );
3445
3446static struct attribute *perfclass_attrs[] = {
3447 &attr_reserve_percpu.attr,
3448 &attr_overcommit.attr,
3449 NULL
3450};
3451
3452static struct attribute_group perfclass_attr_group = {
3453 .attrs = perfclass_attrs,
3454 .name = "perf_counters",
3455};
3456
3457static int __init perf_counter_sysfs_init(void)
3458{
3459 return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
3460 &perfclass_attr_group);
3461}
3462device_initcall(perf_counter_sysfs_init);