blob: a5bdc93ac4775d301aed2271d954207398f1983d [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 Zijlstra7b732a752009-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;
666 unsigned long flags;
667 int err;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100668
669 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100670 * If this is a per-task counter, need to check whether this
671 * counter's task is the current task on this cpu.
Ingo Molnar04289bb2008-12-11 08:38:42 +0100672 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100673 if (ctx->task && cpuctx->task_ctx != ctx)
674 return;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100675
Peter Zijlstra849691a2009-04-06 11:45:12 +0200676 spin_lock_irqsave(&ctx->lock, flags);
Peter Zijlstra4af49982009-04-06 11:45:10 +0200677 update_context_time(ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100678
Paul Mackerrasc07c99b2009-02-13 22:10:34 +1100679 counter->prev_state = counter->state;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100680 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
681 goto unlock;
682 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200683 counter->tstamp_enabled = ctx->time - counter->total_time_enabled;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100684
685 /*
686 * If the counter is in a group and isn't the group leader,
687 * then don't put it on unless the group is on.
688 */
689 if (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE)
690 goto unlock;
691
692 if (!group_can_go_on(counter, cpuctx, 1))
693 err = -EEXIST;
Paul Mackerras6751b712009-05-11 12:08:02 +1000694 else if (counter == leader)
695 err = group_sched_in(counter, cpuctx, ctx,
696 smp_processor_id());
Paul Mackerrasd859e292009-01-17 18:10:22 +1100697 else
698 err = counter_sched_in(counter, cpuctx, ctx,
699 smp_processor_id());
700
701 if (err) {
702 /*
703 * If this counter can't go on and it's part of a
704 * group, then the whole group has to come off.
705 */
706 if (leader != counter)
707 group_sched_out(leader, cpuctx, ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100708 if (leader->hw_event.pinned) {
709 update_group_times(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100710 leader->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100711 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100712 }
713
714 unlock:
Peter Zijlstra849691a2009-04-06 11:45:12 +0200715 spin_unlock_irqrestore(&ctx->lock, flags);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100716}
717
718/*
719 * Enable a counter.
720 */
721static void perf_counter_enable(struct perf_counter *counter)
722{
723 struct perf_counter_context *ctx = counter->ctx;
724 struct task_struct *task = ctx->task;
725
726 if (!task) {
727 /*
728 * Enable the counter on the cpu that it's on
729 */
730 smp_call_function_single(counter->cpu, __perf_counter_enable,
731 counter, 1);
732 return;
733 }
734
735 spin_lock_irq(&ctx->lock);
736 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
737 goto out;
738
739 /*
740 * If the counter is in error state, clear that first.
741 * That way, if we see the counter in error state below, we
742 * know that it has gone back into error state, as distinct
743 * from the task having been scheduled away before the
744 * cross-call arrived.
745 */
746 if (counter->state == PERF_COUNTER_STATE_ERROR)
747 counter->state = PERF_COUNTER_STATE_OFF;
748
749 retry:
750 spin_unlock_irq(&ctx->lock);
751 task_oncpu_function_call(task, __perf_counter_enable, counter);
752
753 spin_lock_irq(&ctx->lock);
754
755 /*
756 * If the context is active and the counter is still off,
757 * we need to retry the cross-call.
758 */
759 if (ctx->is_active && counter->state == PERF_COUNTER_STATE_OFF)
760 goto retry;
761
762 /*
763 * Since we have the lock this context can't be scheduled
764 * in, so we can change the state safely.
765 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100766 if (counter->state == PERF_COUNTER_STATE_OFF) {
Paul Mackerrasd859e292009-01-17 18:10:22 +1100767 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200768 counter->tstamp_enabled =
769 ctx->time - counter->total_time_enabled;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100770 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100771 out:
772 spin_unlock_irq(&ctx->lock);
773}
774
Peter Zijlstra2023b352009-05-05 17:50:26 +0200775static int perf_counter_refresh(struct perf_counter *counter, int refresh)
Peter Zijlstra79f14642009-04-06 11:45:07 +0200776{
Peter Zijlstra2023b352009-05-05 17:50:26 +0200777 /*
778 * not supported on inherited counters
779 */
780 if (counter->hw_event.inherit)
781 return -EINVAL;
782
Peter Zijlstra79f14642009-04-06 11:45:07 +0200783 atomic_add(refresh, &counter->event_limit);
784 perf_counter_enable(counter);
Peter Zijlstra2023b352009-05-05 17:50:26 +0200785
786 return 0;
Peter Zijlstra79f14642009-04-06 11:45:07 +0200787}
788
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100789void __perf_counter_sched_out(struct perf_counter_context *ctx,
790 struct perf_cpu_context *cpuctx)
791{
792 struct perf_counter *counter;
Paul Mackerras3cbed422009-01-09 16:43:42 +1100793 u64 flags;
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100794
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100795 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100796 ctx->is_active = 0;
797 if (likely(!ctx->nr_counters))
798 goto out;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200799 update_context_time(ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100800
Paul Mackerras3cbed422009-01-09 16:43:42 +1100801 flags = hw_perf_save_disable();
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100802 if (ctx->nr_active) {
803 list_for_each_entry(counter, &ctx->counter_list, list_entry)
804 group_sched_out(counter, cpuctx, ctx);
805 }
Paul Mackerras3cbed422009-01-09 16:43:42 +1100806 hw_perf_restore(flags);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100807 out:
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100808 spin_unlock(&ctx->lock);
809}
810
Thomas Gleixner0793a612008-12-04 20:12:29 +0100811/*
812 * Called from scheduler to remove the counters of the current task,
813 * with interrupts disabled.
814 *
815 * We stop each counter and update the counter value in counter->count.
816 *
Ingo Molnar76715812008-12-17 14:20:28 +0100817 * This does not protect us against NMI, but disable()
Thomas Gleixner0793a612008-12-04 20:12:29 +0100818 * sets the disabled bit in the control field of counter _before_
819 * accessing the counter control register. If a NMI hits, then it will
820 * not restart the counter.
821 */
822void perf_counter_task_sched_out(struct task_struct *task, int cpu)
823{
824 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
825 struct perf_counter_context *ctx = &task->perf_counter_ctx;
Peter Zijlstra4a0deca2009-03-19 20:26:12 +0100826 struct pt_regs *regs;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100827
828 if (likely(!cpuctx->task_ctx))
829 return;
830
Peter Zijlstrabce379b2009-04-06 11:45:13 +0200831 update_context_time(ctx);
832
Peter Zijlstra4a0deca2009-03-19 20:26:12 +0100833 regs = task_pt_regs(task);
Peter Zijlstra78f13e92009-04-08 15:01:33 +0200834 perf_swcounter_event(PERF_COUNT_CONTEXT_SWITCHES, 1, 1, regs, 0);
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100835 __perf_counter_sched_out(ctx, cpuctx);
836
Thomas Gleixner0793a612008-12-04 20:12:29 +0100837 cpuctx->task_ctx = NULL;
838}
839
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100840static void perf_counter_cpu_sched_out(struct perf_cpu_context *cpuctx)
Ingo Molnar04289bb2008-12-11 08:38:42 +0100841{
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100842 __perf_counter_sched_out(&cpuctx->ctx, cpuctx);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100843}
844
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100845static void
846__perf_counter_sched_in(struct perf_counter_context *ctx,
847 struct perf_cpu_context *cpuctx, int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100848{
Thomas Gleixner0793a612008-12-04 20:12:29 +0100849 struct perf_counter *counter;
Paul Mackerras3cbed422009-01-09 16:43:42 +1100850 u64 flags;
Paul Mackerrasdd0e6ba2009-01-12 15:11:00 +1100851 int can_add_hw = 1;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100852
Thomas Gleixner0793a612008-12-04 20:12:29 +0100853 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100854 ctx->is_active = 1;
855 if (likely(!ctx->nr_counters))
856 goto out;
857
Peter Zijlstra4af49982009-04-06 11:45:10 +0200858 ctx->timestamp = perf_clock();
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100859
Paul Mackerras3cbed422009-01-09 16:43:42 +1100860 flags = hw_perf_save_disable();
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100861
862 /*
863 * First go through the list and put on any pinned groups
864 * in order to give them the best chance of going on.
865 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100866 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100867 if (counter->state <= PERF_COUNTER_STATE_OFF ||
868 !counter->hw_event.pinned)
869 continue;
870 if (counter->cpu != -1 && counter->cpu != cpu)
871 continue;
872
873 if (group_can_go_on(counter, cpuctx, 1))
874 group_sched_in(counter, cpuctx, ctx, cpu);
875
876 /*
877 * If this pinned group hasn't been scheduled,
878 * put it in error state.
879 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100880 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
881 update_group_times(counter);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100882 counter->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100883 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100884 }
885
886 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
887 /*
888 * Ignore counters in OFF or ERROR state, and
889 * ignore pinned counters since we did them already.
890 */
891 if (counter->state <= PERF_COUNTER_STATE_OFF ||
892 counter->hw_event.pinned)
893 continue;
894
Ingo Molnar04289bb2008-12-11 08:38:42 +0100895 /*
896 * Listen to the 'cpu' scheduling filter constraint
897 * of counters:
898 */
Thomas Gleixner0793a612008-12-04 20:12:29 +0100899 if (counter->cpu != -1 && counter->cpu != cpu)
900 continue;
901
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100902 if (group_can_go_on(counter, cpuctx, can_add_hw)) {
Paul Mackerrasdd0e6ba2009-01-12 15:11:00 +1100903 if (group_sched_in(counter, cpuctx, ctx, cpu))
904 can_add_hw = 0;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100905 }
Thomas Gleixner0793a612008-12-04 20:12:29 +0100906 }
Paul Mackerras3cbed422009-01-09 16:43:42 +1100907 hw_perf_restore(flags);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100908 out:
Thomas Gleixner0793a612008-12-04 20:12:29 +0100909 spin_unlock(&ctx->lock);
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100910}
Ingo Molnar04289bb2008-12-11 08:38:42 +0100911
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100912/*
913 * Called from scheduler to add the counters of the current task
914 * with interrupts disabled.
915 *
916 * We restore the counter value and then enable it.
917 *
918 * This does not protect us against NMI, but enable()
919 * sets the enabled bit in the control field of counter _before_
920 * accessing the counter control register. If a NMI hits, then it will
921 * keep the counter running.
922 */
923void perf_counter_task_sched_in(struct task_struct *task, int cpu)
924{
925 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
926 struct perf_counter_context *ctx = &task->perf_counter_ctx;
927
928 __perf_counter_sched_in(ctx, cpuctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100929 cpuctx->task_ctx = ctx;
930}
931
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100932static void perf_counter_cpu_sched_in(struct perf_cpu_context *cpuctx, int cpu)
933{
934 struct perf_counter_context *ctx = &cpuctx->ctx;
935
936 __perf_counter_sched_in(ctx, cpuctx, cpu);
937}
938
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100939int perf_counter_task_disable(void)
940{
941 struct task_struct *curr = current;
942 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
943 struct perf_counter *counter;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100944 unsigned long flags;
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100945 u64 perf_flags;
946 int cpu;
947
948 if (likely(!ctx->nr_counters))
949 return 0;
950
Peter Zijlstra849691a2009-04-06 11:45:12 +0200951 local_irq_save(flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100952 cpu = smp_processor_id();
953
954 perf_counter_task_sched_out(curr, cpu);
955
956 spin_lock(&ctx->lock);
957
958 /*
959 * Disable all the counters:
960 */
961 perf_flags = hw_perf_save_disable();
962
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100963 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100964 if (counter->state != PERF_COUNTER_STATE_ERROR) {
965 update_group_times(counter);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100966 counter->state = PERF_COUNTER_STATE_OFF;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100967 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100968 }
Ingo Molnar9b51f662008-12-12 13:49:45 +0100969
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100970 hw_perf_restore(perf_flags);
971
Peter Zijlstra849691a2009-04-06 11:45:12 +0200972 spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100973
974 return 0;
975}
976
977int perf_counter_task_enable(void)
978{
979 struct task_struct *curr = current;
980 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
981 struct perf_counter *counter;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100982 unsigned long flags;
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100983 u64 perf_flags;
984 int cpu;
985
986 if (likely(!ctx->nr_counters))
987 return 0;
988
Peter Zijlstra849691a2009-04-06 11:45:12 +0200989 local_irq_save(flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100990 cpu = smp_processor_id();
991
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100992 perf_counter_task_sched_out(curr, cpu);
993
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100994 spin_lock(&ctx->lock);
995
996 /*
997 * Disable all the counters:
998 */
999 perf_flags = hw_perf_save_disable();
1000
1001 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001002 if (counter->state > PERF_COUNTER_STATE_OFF)
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001003 continue;
Ingo Molnar6a930702008-12-11 15:17:03 +01001004 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +02001005 counter->tstamp_enabled =
1006 ctx->time - counter->total_time_enabled;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001007 counter->hw_event.disabled = 0;
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001008 }
1009 hw_perf_restore(perf_flags);
1010
1011 spin_unlock(&ctx->lock);
1012
1013 perf_counter_task_sched_in(curr, cpu);
1014
Peter Zijlstra849691a2009-04-06 11:45:12 +02001015 local_irq_restore(flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001016
1017 return 0;
1018}
1019
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001020/*
1021 * Round-robin a context's counters:
1022 */
1023static void rotate_ctx(struct perf_counter_context *ctx)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001024{
Thomas Gleixner0793a612008-12-04 20:12:29 +01001025 struct perf_counter *counter;
Ingo Molnar5c92d122008-12-11 13:21:10 +01001026 u64 perf_flags;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001027
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001028 if (!ctx->nr_counters)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001029 return;
1030
Thomas Gleixner0793a612008-12-04 20:12:29 +01001031 spin_lock(&ctx->lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001032 /*
Ingo Molnar04289bb2008-12-11 08:38:42 +01001033 * Rotate the first entry last (works just fine for group counters too):
Thomas Gleixner0793a612008-12-04 20:12:29 +01001034 */
Ingo Molnar01b28382008-12-11 13:45:51 +01001035 perf_flags = hw_perf_save_disable();
Ingo Molnar04289bb2008-12-11 08:38:42 +01001036 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Peter Zijlstra75564232009-03-13 12:21:29 +01001037 list_move_tail(&counter->list_entry, &ctx->counter_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001038 break;
1039 }
Ingo Molnar01b28382008-12-11 13:45:51 +01001040 hw_perf_restore(perf_flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001041
1042 spin_unlock(&ctx->lock);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001043}
Thomas Gleixner0793a612008-12-04 20:12:29 +01001044
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001045void perf_counter_task_tick(struct task_struct *curr, int cpu)
1046{
Peter Zijlstra7fc23a52009-05-08 18:52:21 +02001047 struct perf_cpu_context *cpuctx;
1048 struct perf_counter_context *ctx;
1049
1050 if (!atomic_read(&nr_counters))
1051 return;
1052
1053 cpuctx = &per_cpu(perf_cpu_context, cpu);
1054 ctx = &curr->perf_counter_ctx;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001055
Ingo Molnarb82914c2009-05-04 18:54:32 +02001056 perf_counter_cpu_sched_out(cpuctx);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001057 perf_counter_task_sched_out(curr, cpu);
1058
Ingo Molnarb82914c2009-05-04 18:54:32 +02001059 rotate_ctx(&cpuctx->ctx);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001060 rotate_ctx(ctx);
1061
Ingo Molnarb82914c2009-05-04 18:54:32 +02001062 perf_counter_cpu_sched_in(cpuctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001063 perf_counter_task_sched_in(curr, cpu);
1064}
1065
1066/*
Thomas Gleixner0793a612008-12-04 20:12:29 +01001067 * Cross CPU call to read the hardware counter
1068 */
Ingo Molnar76715812008-12-17 14:20:28 +01001069static void __read(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001070{
Ingo Molnar621a01e2008-12-11 12:46:46 +01001071 struct perf_counter *counter = info;
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001072 struct perf_counter_context *ctx = counter->ctx;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001073 unsigned long flags;
Ingo Molnar621a01e2008-12-11 12:46:46 +01001074
Peter Zijlstra849691a2009-04-06 11:45:12 +02001075 local_irq_save(flags);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001076 if (ctx->is_active)
Peter Zijlstra4af49982009-04-06 11:45:10 +02001077 update_context_time(ctx);
Robert Richter4aeb0b42009-04-29 12:47:03 +02001078 counter->pmu->read(counter);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001079 update_counter_times(counter);
Peter Zijlstra849691a2009-04-06 11:45:12 +02001080 local_irq_restore(flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001081}
1082
Ingo Molnar04289bb2008-12-11 08:38:42 +01001083static u64 perf_counter_read(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001084{
1085 /*
1086 * If counter is enabled and currently active on a CPU, update the
1087 * value in the counter structure:
1088 */
Ingo Molnar6a930702008-12-11 15:17:03 +01001089 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
Thomas Gleixner0793a612008-12-04 20:12:29 +01001090 smp_call_function_single(counter->oncpu,
Ingo Molnar76715812008-12-17 14:20:28 +01001091 __read, counter, 1);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001092 } else if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
1093 update_counter_times(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001094 }
1095
Ingo Molnaree060942008-12-13 09:00:03 +01001096 return atomic64_read(&counter->count);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001097}
1098
Thomas Gleixner0793a612008-12-04 20:12:29 +01001099static void put_context(struct perf_counter_context *ctx)
1100{
1101 if (ctx->task)
1102 put_task_struct(ctx->task);
1103}
1104
1105static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
1106{
1107 struct perf_cpu_context *cpuctx;
1108 struct perf_counter_context *ctx;
1109 struct task_struct *task;
1110
1111 /*
1112 * If cpu is not a wildcard then this is a percpu counter:
1113 */
1114 if (cpu != -1) {
1115 /* Must be root to operate on a CPU counter: */
Peter Zijlstra1ccd1542009-04-09 10:53:45 +02001116 if (sysctl_perf_counter_priv && !capable(CAP_SYS_ADMIN))
Thomas Gleixner0793a612008-12-04 20:12:29 +01001117 return ERR_PTR(-EACCES);
1118
1119 if (cpu < 0 || cpu > num_possible_cpus())
1120 return ERR_PTR(-EINVAL);
1121
1122 /*
1123 * We could be clever and allow to attach a counter to an
1124 * offline CPU and activate it when the CPU comes up, but
1125 * that's for later.
1126 */
1127 if (!cpu_isset(cpu, cpu_online_map))
1128 return ERR_PTR(-ENODEV);
1129
1130 cpuctx = &per_cpu(perf_cpu_context, cpu);
1131 ctx = &cpuctx->ctx;
1132
Thomas Gleixner0793a612008-12-04 20:12:29 +01001133 return ctx;
1134 }
1135
1136 rcu_read_lock();
1137 if (!pid)
1138 task = current;
1139 else
1140 task = find_task_by_vpid(pid);
1141 if (task)
1142 get_task_struct(task);
1143 rcu_read_unlock();
1144
1145 if (!task)
1146 return ERR_PTR(-ESRCH);
1147
1148 ctx = &task->perf_counter_ctx;
1149 ctx->task = task;
1150
1151 /* Reuse ptrace permission checks for now. */
1152 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
1153 put_context(ctx);
1154 return ERR_PTR(-EACCES);
1155 }
1156
1157 return ctx;
1158}
1159
Peter Zijlstra592903c2009-03-13 12:21:36 +01001160static void free_counter_rcu(struct rcu_head *head)
1161{
1162 struct perf_counter *counter;
1163
1164 counter = container_of(head, struct perf_counter, rcu_head);
1165 kfree(counter);
1166}
1167
Peter Zijlstra925d5192009-03-30 19:07:02 +02001168static void perf_pending_sync(struct perf_counter *counter);
1169
Peter Zijlstraf1600952009-03-19 20:26:16 +01001170static void free_counter(struct perf_counter *counter)
1171{
Peter Zijlstra925d5192009-03-30 19:07:02 +02001172 perf_pending_sync(counter);
1173
Peter Zijlstra7fc23a52009-05-08 18:52:21 +02001174 atomic_dec(&nr_counters);
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02001175 if (counter->hw_event.mmap)
1176 atomic_dec(&nr_mmap_tracking);
1177 if (counter->hw_event.munmap)
1178 atomic_dec(&nr_munmap_tracking);
1179 if (counter->hw_event.comm)
1180 atomic_dec(&nr_comm_tracking);
1181
Peter Zijlstrae077df42009-03-19 20:26:17 +01001182 if (counter->destroy)
1183 counter->destroy(counter);
1184
Peter Zijlstraf1600952009-03-19 20:26:16 +01001185 call_rcu(&counter->rcu_head, free_counter_rcu);
1186}
1187
Thomas Gleixner0793a612008-12-04 20:12:29 +01001188/*
1189 * Called when the last reference to the file is gone.
1190 */
1191static int perf_release(struct inode *inode, struct file *file)
1192{
1193 struct perf_counter *counter = file->private_data;
1194 struct perf_counter_context *ctx = counter->ctx;
1195
1196 file->private_data = NULL;
1197
Paul Mackerrasd859e292009-01-17 18:10:22 +11001198 mutex_lock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001199 mutex_lock(&counter->mutex);
1200
Ingo Molnar04289bb2008-12-11 08:38:42 +01001201 perf_counter_remove_from_context(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001202
1203 mutex_unlock(&counter->mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001204 mutex_unlock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001205
Peter Zijlstraf1600952009-03-19 20:26:16 +01001206 free_counter(counter);
Mike Galbraith5af75912009-02-11 10:53:37 +01001207 put_context(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001208
1209 return 0;
1210}
1211
1212/*
1213 * Read the performance counter - simple non blocking version for now
1214 */
1215static ssize_t
1216perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
1217{
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001218 u64 values[3];
1219 int n;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001220
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001221 /*
1222 * Return end-of-file for a read on a counter that is in
1223 * error state (i.e. because it was pinned but it couldn't be
1224 * scheduled on to the CPU at some point).
1225 */
1226 if (counter->state == PERF_COUNTER_STATE_ERROR)
1227 return 0;
1228
Thomas Gleixner0793a612008-12-04 20:12:29 +01001229 mutex_lock(&counter->mutex);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001230 values[0] = perf_counter_read(counter);
1231 n = 1;
1232 if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1233 values[n++] = counter->total_time_enabled +
1234 atomic64_read(&counter->child_total_time_enabled);
1235 if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1236 values[n++] = counter->total_time_running +
1237 atomic64_read(&counter->child_total_time_running);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001238 mutex_unlock(&counter->mutex);
1239
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001240 if (count < n * sizeof(u64))
1241 return -EINVAL;
1242 count = n * sizeof(u64);
1243
1244 if (copy_to_user(buf, values, count))
1245 return -EFAULT;
1246
1247 return count;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001248}
1249
1250static ssize_t
Thomas Gleixner0793a612008-12-04 20:12:29 +01001251perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
1252{
1253 struct perf_counter *counter = file->private_data;
1254
Peter Zijlstra7b732a752009-03-23 18:22:10 +01001255 return perf_read_hw(counter, buf, count);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001256}
1257
1258static unsigned int perf_poll(struct file *file, poll_table *wait)
1259{
1260 struct perf_counter *counter = file->private_data;
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001261 struct perf_mmap_data *data;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001262 unsigned int events = POLL_HUP;
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001263
1264 rcu_read_lock();
1265 data = rcu_dereference(counter->data);
1266 if (data)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001267 events = atomic_xchg(&data->poll, 0);
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001268 rcu_read_unlock();
Thomas Gleixner0793a612008-12-04 20:12:29 +01001269
1270 poll_wait(file, &counter->waitq, wait);
1271
Thomas Gleixner0793a612008-12-04 20:12:29 +01001272 return events;
1273}
1274
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001275static void perf_counter_reset(struct perf_counter *counter)
1276{
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001277 (void)perf_counter_read(counter);
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001278 atomic_set(&counter->count, 0);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001279 perf_counter_update_userpage(counter);
1280}
1281
1282static void perf_counter_for_each_sibling(struct perf_counter *counter,
1283 void (*func)(struct perf_counter *))
1284{
1285 struct perf_counter_context *ctx = counter->ctx;
1286 struct perf_counter *sibling;
1287
1288 spin_lock_irq(&ctx->lock);
1289 counter = counter->group_leader;
1290
1291 func(counter);
1292 list_for_each_entry(sibling, &counter->sibling_list, list_entry)
1293 func(sibling);
1294 spin_unlock_irq(&ctx->lock);
1295}
1296
1297static void perf_counter_for_each_child(struct perf_counter *counter,
1298 void (*func)(struct perf_counter *))
1299{
1300 struct perf_counter *child;
1301
1302 mutex_lock(&counter->mutex);
1303 func(counter);
1304 list_for_each_entry(child, &counter->child_list, child_list)
1305 func(child);
1306 mutex_unlock(&counter->mutex);
1307}
1308
1309static void perf_counter_for_each(struct perf_counter *counter,
1310 void (*func)(struct perf_counter *))
1311{
1312 struct perf_counter *child;
1313
1314 mutex_lock(&counter->mutex);
1315 perf_counter_for_each_sibling(counter, func);
1316 list_for_each_entry(child, &counter->child_list, child_list)
1317 perf_counter_for_each_sibling(child, func);
1318 mutex_unlock(&counter->mutex);
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001319}
1320
Paul Mackerrasd859e292009-01-17 18:10:22 +11001321static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1322{
1323 struct perf_counter *counter = file->private_data;
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001324 void (*func)(struct perf_counter *);
1325 u32 flags = arg;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001326
1327 switch (cmd) {
1328 case PERF_COUNTER_IOC_ENABLE:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001329 func = perf_counter_enable;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001330 break;
1331 case PERF_COUNTER_IOC_DISABLE:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001332 func = perf_counter_disable;
Peter Zijlstra79f14642009-04-06 11:45:07 +02001333 break;
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001334 case PERF_COUNTER_IOC_RESET:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001335 func = perf_counter_reset;
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001336 break;
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001337
1338 case PERF_COUNTER_IOC_REFRESH:
1339 return perf_counter_refresh(counter, arg);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001340 default:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001341 return -ENOTTY;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001342 }
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001343
1344 if (flags & PERF_IOC_FLAG_GROUP)
1345 perf_counter_for_each(counter, func);
1346 else
1347 perf_counter_for_each_child(counter, func);
1348
1349 return 0;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001350}
1351
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001352/*
1353 * Callers need to ensure there can be no nesting of this function, otherwise
1354 * the seqlock logic goes bad. We can not serialize this because the arch
1355 * code calls this from NMI context.
1356 */
1357void perf_counter_update_userpage(struct perf_counter *counter)
Paul Mackerras37d81822009-03-23 18:22:08 +01001358{
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001359 struct perf_mmap_data *data;
1360 struct perf_counter_mmap_page *userpg;
1361
1362 rcu_read_lock();
1363 data = rcu_dereference(counter->data);
1364 if (!data)
1365 goto unlock;
1366
1367 userpg = data->user_page;
Paul Mackerras37d81822009-03-23 18:22:08 +01001368
Peter Zijlstra7b732a752009-03-23 18:22:10 +01001369 /*
1370 * Disable preemption so as to not let the corresponding user-space
1371 * spin too long if we get preempted.
1372 */
1373 preempt_disable();
Paul Mackerras37d81822009-03-23 18:22:08 +01001374 ++userpg->lock;
Peter Zijlstra92f22a32009-04-02 11:12:04 +02001375 barrier();
Paul Mackerras37d81822009-03-23 18:22:08 +01001376 userpg->index = counter->hw.idx;
1377 userpg->offset = atomic64_read(&counter->count);
1378 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
1379 userpg->offset -= atomic64_read(&counter->hw.prev_count);
Peter Zijlstra7b732a752009-03-23 18:22:10 +01001380
Peter Zijlstra92f22a32009-04-02 11:12:04 +02001381 barrier();
Paul Mackerras37d81822009-03-23 18:22:08 +01001382 ++userpg->lock;
Peter Zijlstra7b732a752009-03-23 18:22:10 +01001383 preempt_enable();
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001384unlock:
Peter Zijlstra7b732a752009-03-23 18:22:10 +01001385 rcu_read_unlock();
Paul Mackerras37d81822009-03-23 18:22:08 +01001386}
1387
1388static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1389{
1390 struct perf_counter *counter = vma->vm_file->private_data;
Peter Zijlstra7b732a752009-03-23 18:22:10 +01001391 struct perf_mmap_data *data;
1392 int ret = VM_FAULT_SIGBUS;
Paul Mackerras37d81822009-03-23 18:22:08 +01001393
Peter Zijlstra7b732a752009-03-23 18:22:10 +01001394 rcu_read_lock();
1395 data = rcu_dereference(counter->data);
1396 if (!data)
1397 goto unlock;
Paul Mackerras37d81822009-03-23 18:22:08 +01001398
Peter Zijlstra7b732a752009-03-23 18:22:10 +01001399 if (vmf->pgoff == 0) {
1400 vmf->page = virt_to_page(data->user_page);
1401 } else {
1402 int nr = vmf->pgoff - 1;
1403
1404 if ((unsigned)nr > data->nr_pages)
1405 goto unlock;
1406
1407 vmf->page = virt_to_page(data->data_pages[nr]);
1408 }
Paul Mackerras37d81822009-03-23 18:22:08 +01001409 get_page(vmf->page);
Peter Zijlstra7b732a752009-03-23 18:22:10 +01001410 ret = 0;
1411unlock:
1412 rcu_read_unlock();
1413
1414 return ret;
1415}
1416
1417static int perf_mmap_data_alloc(struct perf_counter *counter, int nr_pages)
1418{
1419 struct perf_mmap_data *data;
1420 unsigned long size;
1421 int i;
1422
1423 WARN_ON(atomic_read(&counter->mmap_count));
1424
1425 size = sizeof(struct perf_mmap_data);
1426 size += nr_pages * sizeof(void *);
1427
1428 data = kzalloc(size, GFP_KERNEL);
1429 if (!data)
1430 goto fail;
1431
1432 data->user_page = (void *)get_zeroed_page(GFP_KERNEL);
1433 if (!data->user_page)
1434 goto fail_user_page;
1435
1436 for (i = 0; i < nr_pages; i++) {
1437 data->data_pages[i] = (void *)get_zeroed_page(GFP_KERNEL);
1438 if (!data->data_pages[i])
1439 goto fail_data_pages;
1440 }
1441
1442 data->nr_pages = nr_pages;
Peter Zijlstra22c15582009-05-05 17:50:25 +02001443 atomic_set(&data->lock, -1);
Peter Zijlstra7b732a752009-03-23 18:22:10 +01001444
1445 rcu_assign_pointer(counter->data, data);
1446
Paul Mackerras37d81822009-03-23 18:22:08 +01001447 return 0;
Peter Zijlstra7b732a752009-03-23 18:22:10 +01001448
1449fail_data_pages:
1450 for (i--; i >= 0; i--)
1451 free_page((unsigned long)data->data_pages[i]);
1452
1453 free_page((unsigned long)data->user_page);
1454
1455fail_user_page:
1456 kfree(data);
1457
1458fail:
1459 return -ENOMEM;
1460}
1461
1462static void __perf_mmap_data_free(struct rcu_head *rcu_head)
1463{
1464 struct perf_mmap_data *data = container_of(rcu_head,
1465 struct perf_mmap_data, rcu_head);
1466 int i;
1467
1468 free_page((unsigned long)data->user_page);
1469 for (i = 0; i < data->nr_pages; i++)
1470 free_page((unsigned long)data->data_pages[i]);
1471 kfree(data);
1472}
1473
1474static void perf_mmap_data_free(struct perf_counter *counter)
1475{
1476 struct perf_mmap_data *data = counter->data;
1477
1478 WARN_ON(atomic_read(&counter->mmap_count));
1479
1480 rcu_assign_pointer(counter->data, NULL);
1481 call_rcu(&data->rcu_head, __perf_mmap_data_free);
1482}
1483
1484static void perf_mmap_open(struct vm_area_struct *vma)
1485{
1486 struct perf_counter *counter = vma->vm_file->private_data;
1487
1488 atomic_inc(&counter->mmap_count);
1489}
1490
1491static void perf_mmap_close(struct vm_area_struct *vma)
1492{
1493 struct perf_counter *counter = vma->vm_file->private_data;
1494
1495 if (atomic_dec_and_mutex_lock(&counter->mmap_count,
1496 &counter->mmap_mutex)) {
Peter Zijlstrac5078f72009-05-05 17:50:24 +02001497 vma->vm_mm->locked_vm -= counter->data->nr_locked;
Peter Zijlstra7b732a752009-03-23 18:22:10 +01001498 perf_mmap_data_free(counter);
1499 mutex_unlock(&counter->mmap_mutex);
1500 }
Paul Mackerras37d81822009-03-23 18:22:08 +01001501}
1502
1503static struct vm_operations_struct perf_mmap_vmops = {
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001504 .open = perf_mmap_open,
Peter Zijlstra7b732a752009-03-23 18:22:10 +01001505 .close = perf_mmap_close,
Paul Mackerras37d81822009-03-23 18:22:08 +01001506 .fault = perf_mmap_fault,
1507};
1508
1509static int perf_mmap(struct file *file, struct vm_area_struct *vma)
1510{
1511 struct perf_counter *counter = file->private_data;
Peter Zijlstra7b732a752009-03-23 18:22:10 +01001512 unsigned long vma_size;
1513 unsigned long nr_pages;
1514 unsigned long locked, lock_limit;
1515 int ret = 0;
Peter Zijlstrac5078f72009-05-05 17:50:24 +02001516 long extra;
Paul Mackerras37d81822009-03-23 18:22:08 +01001517
1518 if (!(vma->vm_flags & VM_SHARED) || (vma->vm_flags & VM_WRITE))
1519 return -EINVAL;
Peter Zijlstra7b732a752009-03-23 18:22:10 +01001520
1521 vma_size = vma->vm_end - vma->vm_start;
1522 nr_pages = (vma_size / PAGE_SIZE) - 1;
1523
Peter Zijlstra7730d862009-03-25 12:48:31 +01001524 /*
1525 * If we have data pages ensure they're a power-of-two number, so we
1526 * can do bitmasks instead of modulo.
1527 */
1528 if (nr_pages != 0 && !is_power_of_2(nr_pages))
Paul Mackerras37d81822009-03-23 18:22:08 +01001529 return -EINVAL;
1530
Peter Zijlstra7b732a752009-03-23 18:22:10 +01001531 if (vma_size != PAGE_SIZE * (1 + nr_pages))
Paul Mackerras37d81822009-03-23 18:22:08 +01001532 return -EINVAL;
1533
Peter Zijlstra7b732a752009-03-23 18:22:10 +01001534 if (vma->vm_pgoff != 0)
1535 return -EINVAL;
Paul Mackerras37d81822009-03-23 18:22:08 +01001536
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001537 mutex_lock(&counter->mmap_mutex);
1538 if (atomic_inc_not_zero(&counter->mmap_count)) {
1539 if (nr_pages != counter->data->nr_pages)
1540 ret = -EINVAL;
1541 goto unlock;
1542 }
1543
Peter Zijlstrac5078f72009-05-05 17:50:24 +02001544 extra = nr_pages /* + 1 only account the data pages */;
1545 extra -= sysctl_perf_counter_mlock >> (PAGE_SHIFT - 10);
1546 if (extra < 0)
1547 extra = 0;
1548
1549 locked = vma->vm_mm->locked_vm + extra;
Peter Zijlstra7b732a752009-03-23 18:22:10 +01001550
1551 lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
1552 lock_limit >>= PAGE_SHIFT;
1553
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001554 if ((locked > lock_limit) && !capable(CAP_IPC_LOCK)) {
1555 ret = -EPERM;
1556 goto unlock;
1557 }
Peter Zijlstra7b732a752009-03-23 18:22:10 +01001558
1559 WARN_ON(counter->data);
1560 ret = perf_mmap_data_alloc(counter, nr_pages);
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001561 if (ret)
1562 goto unlock;
1563
1564 atomic_set(&counter->mmap_count, 1);
Peter Zijlstrac5078f72009-05-05 17:50:24 +02001565 vma->vm_mm->locked_vm += extra;
1566 counter->data->nr_locked = extra;
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001567unlock:
Peter Zijlstra7b732a752009-03-23 18:22:10 +01001568 mutex_unlock(&counter->mmap_mutex);
Paul Mackerras37d81822009-03-23 18:22:08 +01001569
1570 vma->vm_flags &= ~VM_MAYWRITE;
1571 vma->vm_flags |= VM_RESERVED;
1572 vma->vm_ops = &perf_mmap_vmops;
Peter Zijlstra7b732a752009-03-23 18:22:10 +01001573
1574 return ret;
Paul Mackerras37d81822009-03-23 18:22:08 +01001575}
1576
Peter Zijlstra3c446b3d2009-04-06 11:45:01 +02001577static int perf_fasync(int fd, struct file *filp, int on)
1578{
1579 struct perf_counter *counter = filp->private_data;
1580 struct inode *inode = filp->f_path.dentry->d_inode;
1581 int retval;
1582
1583 mutex_lock(&inode->i_mutex);
1584 retval = fasync_helper(fd, filp, on, &counter->fasync);
1585 mutex_unlock(&inode->i_mutex);
1586
1587 if (retval < 0)
1588 return retval;
1589
1590 return 0;
1591}
1592
Thomas Gleixner0793a612008-12-04 20:12:29 +01001593static const struct file_operations perf_fops = {
1594 .release = perf_release,
1595 .read = perf_read,
1596 .poll = perf_poll,
Paul Mackerrasd859e292009-01-17 18:10:22 +11001597 .unlocked_ioctl = perf_ioctl,
1598 .compat_ioctl = perf_ioctl,
Paul Mackerras37d81822009-03-23 18:22:08 +01001599 .mmap = perf_mmap,
Peter Zijlstra3c446b3d2009-04-06 11:45:01 +02001600 .fasync = perf_fasync,
Thomas Gleixner0793a612008-12-04 20:12:29 +01001601};
1602
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001603/*
Peter Zijlstra925d5192009-03-30 19:07:02 +02001604 * Perf counter wakeup
1605 *
1606 * If there's data, ensure we set the poll() state and publish everything
1607 * to user-space before waking everybody up.
1608 */
1609
1610void perf_counter_wakeup(struct perf_counter *counter)
1611{
Peter Zijlstra925d5192009-03-30 19:07:02 +02001612 wake_up_all(&counter->waitq);
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02001613
1614 if (counter->pending_kill) {
1615 kill_fasync(&counter->fasync, SIGIO, counter->pending_kill);
1616 counter->pending_kill = 0;
1617 }
Peter Zijlstra925d5192009-03-30 19:07:02 +02001618}
1619
1620/*
1621 * Pending wakeups
1622 *
1623 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
1624 *
1625 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
1626 * single linked list and use cmpxchg() to add entries lockless.
1627 */
1628
Peter Zijlstra79f14642009-04-06 11:45:07 +02001629static void perf_pending_counter(struct perf_pending_entry *entry)
1630{
1631 struct perf_counter *counter = container_of(entry,
1632 struct perf_counter, pending);
1633
1634 if (counter->pending_disable) {
1635 counter->pending_disable = 0;
1636 perf_counter_disable(counter);
1637 }
1638
1639 if (counter->pending_wakeup) {
1640 counter->pending_wakeup = 0;
1641 perf_counter_wakeup(counter);
1642 }
1643}
1644
Peter Zijlstra671dec52009-04-06 11:45:02 +02001645#define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
Peter Zijlstra925d5192009-03-30 19:07:02 +02001646
Peter Zijlstra671dec52009-04-06 11:45:02 +02001647static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = {
Peter Zijlstra925d5192009-03-30 19:07:02 +02001648 PENDING_TAIL,
1649};
1650
Peter Zijlstra671dec52009-04-06 11:45:02 +02001651static void perf_pending_queue(struct perf_pending_entry *entry,
1652 void (*func)(struct perf_pending_entry *))
Peter Zijlstra925d5192009-03-30 19:07:02 +02001653{
Peter Zijlstra671dec52009-04-06 11:45:02 +02001654 struct perf_pending_entry **head;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001655
Peter Zijlstra671dec52009-04-06 11:45:02 +02001656 if (cmpxchg(&entry->next, NULL, PENDING_TAIL) != NULL)
Peter Zijlstra925d5192009-03-30 19:07:02 +02001657 return;
1658
Peter Zijlstra671dec52009-04-06 11:45:02 +02001659 entry->func = func;
1660
1661 head = &get_cpu_var(perf_pending_head);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001662
1663 do {
Peter Zijlstra671dec52009-04-06 11:45:02 +02001664 entry->next = *head;
1665 } while (cmpxchg(head, entry->next, entry) != entry->next);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001666
1667 set_perf_counter_pending();
1668
Peter Zijlstra671dec52009-04-06 11:45:02 +02001669 put_cpu_var(perf_pending_head);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001670}
1671
1672static int __perf_pending_run(void)
1673{
Peter Zijlstra671dec52009-04-06 11:45:02 +02001674 struct perf_pending_entry *list;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001675 int nr = 0;
1676
Peter Zijlstra671dec52009-04-06 11:45:02 +02001677 list = xchg(&__get_cpu_var(perf_pending_head), PENDING_TAIL);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001678 while (list != PENDING_TAIL) {
Peter Zijlstra671dec52009-04-06 11:45:02 +02001679 void (*func)(struct perf_pending_entry *);
1680 struct perf_pending_entry *entry = list;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001681
1682 list = list->next;
1683
Peter Zijlstra671dec52009-04-06 11:45:02 +02001684 func = entry->func;
1685 entry->next = NULL;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001686 /*
1687 * Ensure we observe the unqueue before we issue the wakeup,
1688 * so that we won't be waiting forever.
1689 * -- see perf_not_pending().
1690 */
1691 smp_wmb();
1692
Peter Zijlstra671dec52009-04-06 11:45:02 +02001693 func(entry);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001694 nr++;
1695 }
1696
1697 return nr;
1698}
1699
1700static inline int perf_not_pending(struct perf_counter *counter)
1701{
1702 /*
1703 * If we flush on whatever cpu we run, there is a chance we don't
1704 * need to wait.
1705 */
1706 get_cpu();
1707 __perf_pending_run();
1708 put_cpu();
1709
1710 /*
1711 * Ensure we see the proper queue state before going to sleep
1712 * so that we do not miss the wakeup. -- see perf_pending_handle()
1713 */
1714 smp_rmb();
Peter Zijlstra671dec52009-04-06 11:45:02 +02001715 return counter->pending.next == NULL;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001716}
1717
1718static void perf_pending_sync(struct perf_counter *counter)
1719{
1720 wait_event(counter->waitq, perf_not_pending(counter));
1721}
1722
1723void perf_counter_do_pending(void)
1724{
1725 __perf_pending_run();
1726}
1727
1728/*
Peter Zijlstra394ee072009-03-30 19:07:14 +02001729 * Callchain support -- arch specific
1730 */
1731
Peter Zijlstra9c03d882009-04-06 11:45:00 +02001732__weak struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
Peter Zijlstra394ee072009-03-30 19:07:14 +02001733{
1734 return NULL;
1735}
1736
1737/*
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001738 * Output
1739 */
1740
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001741struct perf_output_handle {
1742 struct perf_counter *counter;
1743 struct perf_mmap_data *data;
1744 unsigned int offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01001745 unsigned int head;
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001746 int nmi;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02001747 int overflow;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001748 int locked;
1749 unsigned long flags;
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001750};
1751
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001752static void perf_output_wakeup(struct perf_output_handle *handle)
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001753{
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001754 atomic_set(&handle->data->poll, POLL_IN);
1755
Peter Zijlstra671dec52009-04-06 11:45:02 +02001756 if (handle->nmi) {
Peter Zijlstra79f14642009-04-06 11:45:07 +02001757 handle->counter->pending_wakeup = 1;
Peter Zijlstra671dec52009-04-06 11:45:02 +02001758 perf_pending_queue(&handle->counter->pending,
Peter Zijlstra79f14642009-04-06 11:45:07 +02001759 perf_pending_counter);
Peter Zijlstra671dec52009-04-06 11:45:02 +02001760 } else
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001761 perf_counter_wakeup(handle->counter);
1762}
1763
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001764/*
1765 * Curious locking construct.
1766 *
1767 * We need to ensure a later event doesn't publish a head when a former
1768 * event isn't done writing. However since we need to deal with NMIs we
1769 * cannot fully serialize things.
1770 *
1771 * What we do is serialize between CPUs so we only have to deal with NMI
1772 * nesting on a single CPU.
1773 *
1774 * We only publish the head (and generate a wakeup) when the outer-most
1775 * event completes.
1776 */
1777static void perf_output_lock(struct perf_output_handle *handle)
1778{
1779 struct perf_mmap_data *data = handle->data;
1780 int cpu;
1781
1782 handle->locked = 0;
1783
1784 local_irq_save(handle->flags);
1785 cpu = smp_processor_id();
1786
1787 if (in_nmi() && atomic_read(&data->lock) == cpu)
1788 return;
1789
Peter Zijlstra22c15582009-05-05 17:50:25 +02001790 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001791 cpu_relax();
1792
1793 handle->locked = 1;
1794}
1795
1796static void perf_output_unlock(struct perf_output_handle *handle)
1797{
1798 struct perf_mmap_data *data = handle->data;
1799 int head, cpu;
1800
Peter Zijlstrac66de4a52009-05-05 17:50:22 +02001801 data->done_head = data->head;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001802
1803 if (!handle->locked)
1804 goto out;
1805
1806again:
1807 /*
1808 * The xchg implies a full barrier that ensures all writes are done
1809 * before we publish the new head, matched by a rmb() in userspace when
1810 * reading this position.
1811 */
Peter Zijlstrac66de4a52009-05-05 17:50:22 +02001812 while ((head = atomic_xchg(&data->done_head, 0)))
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001813 data->user_page->data_head = head;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001814
1815 /*
Peter Zijlstrac66de4a52009-05-05 17:50:22 +02001816 * NMI can happen here, which means we can miss a done_head update.
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001817 */
1818
Peter Zijlstra22c15582009-05-05 17:50:25 +02001819 cpu = atomic_xchg(&data->lock, -1);
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001820 WARN_ON_ONCE(cpu != smp_processor_id());
1821
1822 /*
1823 * Therefore we have to validate we did not indeed do so.
1824 */
Peter Zijlstrac66de4a52009-05-05 17:50:22 +02001825 if (unlikely(atomic_read(&data->done_head))) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001826 /*
1827 * Since we had it locked, we can lock it again.
1828 */
Peter Zijlstra22c15582009-05-05 17:50:25 +02001829 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001830 cpu_relax();
1831
1832 goto again;
1833 }
1834
Peter Zijlstrac66de4a52009-05-05 17:50:22 +02001835 if (atomic_xchg(&data->wakeup, 0))
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001836 perf_output_wakeup(handle);
1837out:
1838 local_irq_restore(handle->flags);
1839}
1840
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001841static int perf_output_begin(struct perf_output_handle *handle,
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001842 struct perf_counter *counter, unsigned int size,
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02001843 int nmi, int overflow)
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001844{
Peter Zijlstra7b732a752009-03-23 18:22:10 +01001845 struct perf_mmap_data *data;
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001846 unsigned int offset, head;
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001847
Peter Zijlstra2023b352009-05-05 17:50:26 +02001848 /*
1849 * For inherited counters we send all the output towards the parent.
1850 */
1851 if (counter->parent)
1852 counter = counter->parent;
1853
Peter Zijlstra7b732a752009-03-23 18:22:10 +01001854 rcu_read_lock();
Peter Zijlstra7b732a752009-03-23 18:22:10 +01001855 data = rcu_dereference(counter->data);
1856 if (!data)
1857 goto out;
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001858
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001859 handle->data = data;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02001860 handle->counter = counter;
1861 handle->nmi = nmi;
1862 handle->overflow = overflow;
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001863
Peter Zijlstra7b732a752009-03-23 18:22:10 +01001864 if (!data->nr_pages)
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001865 goto fail;
Peter Zijlstra7b732a752009-03-23 18:22:10 +01001866
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001867 perf_output_lock(handle);
1868
Peter Zijlstra7b732a752009-03-23 18:22:10 +01001869 do {
1870 offset = head = atomic_read(&data->head);
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001871 head += size;
Peter Zijlstra7b732a752009-03-23 18:22:10 +01001872 } while (atomic_cmpxchg(&data->head, offset, head) != offset);
1873
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001874 handle->offset = offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01001875 handle->head = head;
Peter Zijlstrac66de4a52009-05-05 17:50:22 +02001876
1877 if ((offset >> PAGE_SHIFT) != (head >> PAGE_SHIFT))
1878 atomic_set(&data->wakeup, 1);
Peter Zijlstra7b732a752009-03-23 18:22:10 +01001879
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001880 return 0;
Peter Zijlstra7b732a752009-03-23 18:22:10 +01001881
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001882fail:
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001883 perf_output_wakeup(handle);
Peter Zijlstra7b732a752009-03-23 18:22:10 +01001884out:
1885 rcu_read_unlock();
1886
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001887 return -ENOSPC;
1888}
1889
1890static void perf_output_copy(struct perf_output_handle *handle,
1891 void *buf, unsigned int len)
1892{
1893 unsigned int pages_mask;
1894 unsigned int offset;
1895 unsigned int size;
1896 void **pages;
1897
1898 offset = handle->offset;
1899 pages_mask = handle->data->nr_pages - 1;
1900 pages = handle->data->data_pages;
1901
1902 do {
1903 unsigned int page_offset;
1904 int nr;
1905
1906 nr = (offset >> PAGE_SHIFT) & pages_mask;
1907 page_offset = offset & (PAGE_SIZE - 1);
1908 size = min_t(unsigned int, PAGE_SIZE - page_offset, len);
1909
1910 memcpy(pages[nr] + page_offset, buf, size);
1911
1912 len -= size;
1913 buf += size;
1914 offset += size;
1915 } while (len);
1916
1917 handle->offset = offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01001918
1919 WARN_ON_ONCE(handle->offset > handle->head);
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001920}
1921
Peter Zijlstra5c148192009-03-25 12:30:23 +01001922#define perf_output_put(handle, x) \
1923 perf_output_copy((handle), &(x), sizeof(x))
1924
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001925static void perf_output_end(struct perf_output_handle *handle)
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001926{
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001927 struct perf_counter *counter = handle->counter;
1928 struct perf_mmap_data *data = handle->data;
1929
1930 int wakeup_events = counter->hw_event.wakeup_events;
Peter Zijlstrac4578102009-04-02 11:12:01 +02001931
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02001932 if (handle->overflow && wakeup_events) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001933 int events = atomic_inc_return(&data->events);
Peter Zijlstrac4578102009-04-02 11:12:01 +02001934 if (events >= wakeup_events) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001935 atomic_sub(wakeup_events, &data->events);
Peter Zijlstrac66de4a52009-05-05 17:50:22 +02001936 atomic_set(&data->wakeup, 1);
Peter Zijlstrac4578102009-04-02 11:12:01 +02001937 }
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001938 }
1939
1940 perf_output_unlock(handle);
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001941 rcu_read_unlock();
1942}
1943
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02001944static void perf_counter_output(struct perf_counter *counter,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02001945 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstra7b732a752009-03-23 18:22:10 +01001946{
Peter Zijlstra5ed00412009-03-30 19:07:12 +02001947 int ret;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02001948 u64 record_type = counter->hw_event.record_type;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02001949 struct perf_output_handle handle;
1950 struct perf_event_header header;
1951 u64 ip;
Peter Zijlstra5c148192009-03-25 12:30:23 +01001952 struct {
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01001953 u32 pid, tid;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02001954 } tid_entry;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02001955 struct {
1956 u64 event;
1957 u64 counter;
1958 } group_entry;
Peter Zijlstra394ee072009-03-30 19:07:14 +02001959 struct perf_callchain_entry *callchain = NULL;
1960 int callchain_size = 0;
Peter Zijlstra339f7c92009-04-06 11:45:06 +02001961 u64 time;
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02001962 struct {
1963 u32 cpu, reserved;
1964 } cpu_entry;
Peter Zijlstra7b732a752009-03-23 18:22:10 +01001965
Peter Zijlstra6b6e54862009-04-08 15:01:27 +02001966 header.type = 0;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02001967 header.size = sizeof(header);
Peter Zijlstra7b732a752009-03-23 18:22:10 +01001968
Peter Zijlstra6b6e54862009-04-08 15:01:27 +02001969 header.misc = PERF_EVENT_MISC_OVERFLOW;
1970 header.misc |= user_mode(regs) ?
Peter Zijlstra6fab0192009-04-08 15:01:26 +02001971 PERF_EVENT_MISC_USER : PERF_EVENT_MISC_KERNEL;
1972
Peter Zijlstra8a057d82009-04-02 11:11:59 +02001973 if (record_type & PERF_RECORD_IP) {
1974 ip = instruction_pointer(regs);
Peter Zijlstra6b6e54862009-04-08 15:01:27 +02001975 header.type |= PERF_RECORD_IP;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02001976 header.size += sizeof(ip);
1977 }
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01001978
Peter Zijlstra8a057d82009-04-02 11:11:59 +02001979 if (record_type & PERF_RECORD_TID) {
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01001980 /* namespace issues */
Peter Zijlstra5ed00412009-03-30 19:07:12 +02001981 tid_entry.pid = current->group_leader->pid;
1982 tid_entry.tid = current->pid;
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01001983
Peter Zijlstra6b6e54862009-04-08 15:01:27 +02001984 header.type |= PERF_RECORD_TID;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02001985 header.size += sizeof(tid_entry);
1986 }
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01001987
Peter Zijlstra4d855452009-04-08 15:01:32 +02001988 if (record_type & PERF_RECORD_TIME) {
1989 /*
1990 * Maybe do better on x86 and provide cpu_clock_nmi()
1991 */
1992 time = sched_clock();
1993
1994 header.type |= PERF_RECORD_TIME;
1995 header.size += sizeof(u64);
1996 }
1997
Peter Zijlstra78f13e92009-04-08 15:01:33 +02001998 if (record_type & PERF_RECORD_ADDR) {
1999 header.type |= PERF_RECORD_ADDR;
2000 header.size += sizeof(u64);
2001 }
2002
Peter Zijlstraa85f61a2009-05-08 18:52:23 +02002003 if (record_type & PERF_RECORD_CONFIG) {
2004 header.type |= PERF_RECORD_CONFIG;
2005 header.size += sizeof(u64);
2006 }
2007
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002008 if (record_type & PERF_RECORD_CPU) {
2009 header.type |= PERF_RECORD_CPU;
2010 header.size += sizeof(cpu_entry);
2011
2012 cpu_entry.cpu = raw_smp_processor_id();
2013 }
2014
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002015 if (record_type & PERF_RECORD_GROUP) {
Peter Zijlstra6b6e54862009-04-08 15:01:27 +02002016 header.type |= PERF_RECORD_GROUP;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002017 header.size += sizeof(u64) +
2018 counter->nr_siblings * sizeof(group_entry);
2019 }
2020
2021 if (record_type & PERF_RECORD_CALLCHAIN) {
Peter Zijlstra394ee072009-03-30 19:07:14 +02002022 callchain = perf_callchain(regs);
2023
2024 if (callchain) {
Peter Zijlstra9c03d882009-04-06 11:45:00 +02002025 callchain_size = (1 + callchain->nr) * sizeof(u64);
Peter Zijlstra394ee072009-03-30 19:07:14 +02002026
Peter Zijlstra6b6e54862009-04-08 15:01:27 +02002027 header.type |= PERF_RECORD_CALLCHAIN;
Peter Zijlstra394ee072009-03-30 19:07:14 +02002028 header.size += callchain_size;
2029 }
2030 }
2031
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002032 ret = perf_output_begin(&handle, counter, header.size, nmi, 1);
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002033 if (ret)
2034 return;
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002035
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002036 perf_output_put(&handle, header);
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002037
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002038 if (record_type & PERF_RECORD_IP)
2039 perf_output_put(&handle, ip);
2040
2041 if (record_type & PERF_RECORD_TID)
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002042 perf_output_put(&handle, tid_entry);
2043
Peter Zijlstra4d855452009-04-08 15:01:32 +02002044 if (record_type & PERF_RECORD_TIME)
2045 perf_output_put(&handle, time);
2046
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002047 if (record_type & PERF_RECORD_ADDR)
2048 perf_output_put(&handle, addr);
2049
Peter Zijlstraa85f61a2009-05-08 18:52:23 +02002050 if (record_type & PERF_RECORD_CONFIG)
2051 perf_output_put(&handle, counter->hw_event.config);
2052
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002053 if (record_type & PERF_RECORD_CPU)
2054 perf_output_put(&handle, cpu_entry);
2055
Peter Zijlstra2023b352009-05-05 17:50:26 +02002056 /*
2057 * XXX PERF_RECORD_GROUP vs inherited counters seems difficult.
2058 */
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002059 if (record_type & PERF_RECORD_GROUP) {
2060 struct perf_counter *leader, *sub;
2061 u64 nr = counter->nr_siblings;
2062
2063 perf_output_put(&handle, nr);
2064
2065 leader = counter->group_leader;
2066 list_for_each_entry(sub, &leader->sibling_list, list_entry) {
2067 if (sub != counter)
Robert Richter4aeb0b42009-04-29 12:47:03 +02002068 sub->pmu->read(sub);
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002069
2070 group_entry.event = sub->hw_event.config;
2071 group_entry.counter = atomic64_read(&sub->count);
2072
2073 perf_output_put(&handle, group_entry);
2074 }
2075 }
2076
Peter Zijlstra394ee072009-03-30 19:07:14 +02002077 if (callchain)
2078 perf_output_copy(&handle, callchain, callchain_size);
2079
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002080 perf_output_end(&handle);
Peter Zijlstra7b732a752009-03-23 18:22:10 +01002081}
2082
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002083/*
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002084 * comm tracking
2085 */
2086
2087struct perf_comm_event {
2088 struct task_struct *task;
2089 char *comm;
2090 int comm_size;
2091
2092 struct {
2093 struct perf_event_header header;
2094
2095 u32 pid;
2096 u32 tid;
2097 } event;
2098};
2099
2100static void perf_counter_comm_output(struct perf_counter *counter,
2101 struct perf_comm_event *comm_event)
2102{
2103 struct perf_output_handle handle;
2104 int size = comm_event->event.header.size;
2105 int ret = perf_output_begin(&handle, counter, size, 0, 0);
2106
2107 if (ret)
2108 return;
2109
2110 perf_output_put(&handle, comm_event->event);
2111 perf_output_copy(&handle, comm_event->comm,
2112 comm_event->comm_size);
2113 perf_output_end(&handle);
2114}
2115
2116static int perf_counter_comm_match(struct perf_counter *counter,
2117 struct perf_comm_event *comm_event)
2118{
2119 if (counter->hw_event.comm &&
2120 comm_event->event.header.type == PERF_EVENT_COMM)
2121 return 1;
2122
2123 return 0;
2124}
2125
2126static void perf_counter_comm_ctx(struct perf_counter_context *ctx,
2127 struct perf_comm_event *comm_event)
2128{
2129 struct perf_counter *counter;
2130
2131 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2132 return;
2133
2134 rcu_read_lock();
2135 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2136 if (perf_counter_comm_match(counter, comm_event))
2137 perf_counter_comm_output(counter, comm_event);
2138 }
2139 rcu_read_unlock();
2140}
2141
2142static void perf_counter_comm_event(struct perf_comm_event *comm_event)
2143{
2144 struct perf_cpu_context *cpuctx;
2145 unsigned int size;
2146 char *comm = comm_event->task->comm;
2147
Ingo Molnar888fcee2009-04-09 09:48:22 +02002148 size = ALIGN(strlen(comm)+1, sizeof(u64));
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002149
2150 comm_event->comm = comm;
2151 comm_event->comm_size = size;
2152
2153 comm_event->event.header.size = sizeof(comm_event->event) + size;
2154
2155 cpuctx = &get_cpu_var(perf_cpu_context);
2156 perf_counter_comm_ctx(&cpuctx->ctx, comm_event);
2157 put_cpu_var(perf_cpu_context);
2158
2159 perf_counter_comm_ctx(&current->perf_counter_ctx, comm_event);
2160}
2161
2162void perf_counter_comm(struct task_struct *task)
2163{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002164 struct perf_comm_event comm_event;
2165
2166 if (!atomic_read(&nr_comm_tracking))
2167 return;
2168
2169 comm_event = (struct perf_comm_event){
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002170 .task = task,
2171 .event = {
2172 .header = { .type = PERF_EVENT_COMM, },
2173 .pid = task->group_leader->pid,
2174 .tid = task->pid,
2175 },
2176 };
2177
2178 perf_counter_comm_event(&comm_event);
2179}
2180
2181/*
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002182 * mmap tracking
2183 */
2184
2185struct perf_mmap_event {
2186 struct file *file;
2187 char *file_name;
2188 int file_size;
2189
2190 struct {
2191 struct perf_event_header header;
2192
2193 u32 pid;
2194 u32 tid;
2195 u64 start;
2196 u64 len;
2197 u64 pgoff;
2198 } event;
2199};
2200
2201static void perf_counter_mmap_output(struct perf_counter *counter,
2202 struct perf_mmap_event *mmap_event)
2203{
2204 struct perf_output_handle handle;
2205 int size = mmap_event->event.header.size;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002206 int ret = perf_output_begin(&handle, counter, size, 0, 0);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002207
2208 if (ret)
2209 return;
2210
2211 perf_output_put(&handle, mmap_event->event);
2212 perf_output_copy(&handle, mmap_event->file_name,
2213 mmap_event->file_size);
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002214 perf_output_end(&handle);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002215}
2216
2217static int perf_counter_mmap_match(struct perf_counter *counter,
2218 struct perf_mmap_event *mmap_event)
2219{
2220 if (counter->hw_event.mmap &&
2221 mmap_event->event.header.type == PERF_EVENT_MMAP)
2222 return 1;
2223
2224 if (counter->hw_event.munmap &&
2225 mmap_event->event.header.type == PERF_EVENT_MUNMAP)
2226 return 1;
2227
2228 return 0;
2229}
2230
2231static void perf_counter_mmap_ctx(struct perf_counter_context *ctx,
2232 struct perf_mmap_event *mmap_event)
2233{
2234 struct perf_counter *counter;
2235
2236 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2237 return;
2238
2239 rcu_read_lock();
2240 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2241 if (perf_counter_mmap_match(counter, mmap_event))
2242 perf_counter_mmap_output(counter, mmap_event);
2243 }
2244 rcu_read_unlock();
2245}
2246
2247static void perf_counter_mmap_event(struct perf_mmap_event *mmap_event)
2248{
2249 struct perf_cpu_context *cpuctx;
2250 struct file *file = mmap_event->file;
2251 unsigned int size;
2252 char tmp[16];
2253 char *buf = NULL;
2254 char *name;
2255
2256 if (file) {
2257 buf = kzalloc(PATH_MAX, GFP_KERNEL);
2258 if (!buf) {
2259 name = strncpy(tmp, "//enomem", sizeof(tmp));
2260 goto got_name;
2261 }
Peter Zijlstrad3d21c42009-04-09 10:53:46 +02002262 name = d_path(&file->f_path, buf, PATH_MAX);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002263 if (IS_ERR(name)) {
2264 name = strncpy(tmp, "//toolong", sizeof(tmp));
2265 goto got_name;
2266 }
2267 } else {
2268 name = strncpy(tmp, "//anon", sizeof(tmp));
2269 goto got_name;
2270 }
2271
2272got_name:
Ingo Molnar888fcee2009-04-09 09:48:22 +02002273 size = ALIGN(strlen(name)+1, sizeof(u64));
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002274
2275 mmap_event->file_name = name;
2276 mmap_event->file_size = size;
2277
2278 mmap_event->event.header.size = sizeof(mmap_event->event) + size;
2279
2280 cpuctx = &get_cpu_var(perf_cpu_context);
2281 perf_counter_mmap_ctx(&cpuctx->ctx, mmap_event);
2282 put_cpu_var(perf_cpu_context);
2283
2284 perf_counter_mmap_ctx(&current->perf_counter_ctx, mmap_event);
2285
2286 kfree(buf);
2287}
2288
2289void perf_counter_mmap(unsigned long addr, unsigned long len,
2290 unsigned long pgoff, struct file *file)
2291{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002292 struct perf_mmap_event mmap_event;
2293
2294 if (!atomic_read(&nr_mmap_tracking))
2295 return;
2296
2297 mmap_event = (struct perf_mmap_event){
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002298 .file = file,
2299 .event = {
2300 .header = { .type = PERF_EVENT_MMAP, },
2301 .pid = current->group_leader->pid,
2302 .tid = current->pid,
2303 .start = addr,
2304 .len = len,
2305 .pgoff = pgoff,
2306 },
2307 };
2308
2309 perf_counter_mmap_event(&mmap_event);
2310}
2311
2312void perf_counter_munmap(unsigned long addr, unsigned long len,
2313 unsigned long pgoff, struct file *file)
2314{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002315 struct perf_mmap_event mmap_event;
2316
2317 if (!atomic_read(&nr_munmap_tracking))
2318 return;
2319
2320 mmap_event = (struct perf_mmap_event){
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002321 .file = file,
2322 .event = {
2323 .header = { .type = PERF_EVENT_MUNMAP, },
2324 .pid = current->group_leader->pid,
2325 .tid = current->pid,
2326 .start = addr,
2327 .len = len,
2328 .pgoff = pgoff,
2329 },
2330 };
2331
2332 perf_counter_mmap_event(&mmap_event);
2333}
2334
2335/*
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002336 * Generic counter overflow handling.
2337 */
2338
2339int perf_counter_overflow(struct perf_counter *counter,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002340 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002341{
Peter Zijlstra79f14642009-04-06 11:45:07 +02002342 int events = atomic_read(&counter->event_limit);
2343 int ret = 0;
2344
Peter Zijlstra2023b352009-05-05 17:50:26 +02002345 /*
2346 * XXX event_limit might not quite work as expected on inherited
2347 * counters
2348 */
2349
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002350 counter->pending_kill = POLL_IN;
Peter Zijlstra79f14642009-04-06 11:45:07 +02002351 if (events && atomic_dec_and_test(&counter->event_limit)) {
2352 ret = 1;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002353 counter->pending_kill = POLL_HUP;
Peter Zijlstra79f14642009-04-06 11:45:07 +02002354 if (nmi) {
2355 counter->pending_disable = 1;
2356 perf_pending_queue(&counter->pending,
2357 perf_pending_counter);
2358 } else
2359 perf_counter_disable(counter);
2360 }
2361
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002362 perf_counter_output(counter, nmi, regs, addr);
Peter Zijlstra79f14642009-04-06 11:45:07 +02002363 return ret;
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002364}
2365
2366/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002367 * Generic software counter infrastructure
2368 */
2369
2370static void perf_swcounter_update(struct perf_counter *counter)
2371{
2372 struct hw_perf_counter *hwc = &counter->hw;
2373 u64 prev, now;
2374 s64 delta;
2375
2376again:
2377 prev = atomic64_read(&hwc->prev_count);
2378 now = atomic64_read(&hwc->count);
2379 if (atomic64_cmpxchg(&hwc->prev_count, prev, now) != prev)
2380 goto again;
2381
2382 delta = now - prev;
2383
2384 atomic64_add(delta, &counter->count);
2385 atomic64_sub(delta, &hwc->period_left);
2386}
2387
2388static void perf_swcounter_set_period(struct perf_counter *counter)
2389{
2390 struct hw_perf_counter *hwc = &counter->hw;
2391 s64 left = atomic64_read(&hwc->period_left);
2392 s64 period = hwc->irq_period;
2393
2394 if (unlikely(left <= -period)) {
2395 left = period;
2396 atomic64_set(&hwc->period_left, left);
2397 }
2398
2399 if (unlikely(left <= 0)) {
2400 left += period;
2401 atomic64_add(period, &hwc->period_left);
2402 }
2403
2404 atomic64_set(&hwc->prev_count, -left);
2405 atomic64_set(&hwc->count, -left);
2406}
2407
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002408static enum hrtimer_restart perf_swcounter_hrtimer(struct hrtimer *hrtimer)
2409{
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002410 enum hrtimer_restart ret = HRTIMER_RESTART;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002411 struct perf_counter *counter;
2412 struct pt_regs *regs;
2413
2414 counter = container_of(hrtimer, struct perf_counter, hw.hrtimer);
Robert Richter4aeb0b42009-04-29 12:47:03 +02002415 counter->pmu->read(counter);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002416
2417 regs = get_irq_regs();
2418 /*
2419 * In case we exclude kernel IPs or are somehow not in interrupt
2420 * context, provide the next best thing, the user IP.
2421 */
2422 if ((counter->hw_event.exclude_kernel || !regs) &&
2423 !counter->hw_event.exclude_user)
2424 regs = task_pt_regs(current);
2425
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002426 if (regs) {
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002427 if (perf_counter_overflow(counter, 0, regs, 0))
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002428 ret = HRTIMER_NORESTART;
2429 }
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002430
2431 hrtimer_forward_now(hrtimer, ns_to_ktime(counter->hw.irq_period));
2432
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002433 return ret;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002434}
2435
2436static void perf_swcounter_overflow(struct perf_counter *counter,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002437 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002438{
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002439 perf_swcounter_update(counter);
2440 perf_swcounter_set_period(counter);
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002441 if (perf_counter_overflow(counter, nmi, regs, addr))
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002442 /* soft-disable the counter */
2443 ;
2444
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002445}
2446
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002447static int perf_swcounter_match(struct perf_counter *counter,
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002448 enum perf_event_types type,
2449 u32 event, struct pt_regs *regs)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002450{
2451 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
2452 return 0;
2453
Peter Zijlstraf4a2deb42009-03-23 18:22:06 +01002454 if (perf_event_raw(&counter->hw_event))
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002455 return 0;
2456
Peter Zijlstraf4a2deb42009-03-23 18:22:06 +01002457 if (perf_event_type(&counter->hw_event) != type)
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002458 return 0;
2459
Peter Zijlstraf4a2deb42009-03-23 18:22:06 +01002460 if (perf_event_id(&counter->hw_event) != event)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002461 return 0;
2462
2463 if (counter->hw_event.exclude_user && user_mode(regs))
2464 return 0;
2465
2466 if (counter->hw_event.exclude_kernel && !user_mode(regs))
2467 return 0;
2468
2469 return 1;
2470}
2471
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002472static void perf_swcounter_add(struct perf_counter *counter, u64 nr,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002473 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002474{
2475 int neg = atomic64_add_negative(nr, &counter->hw.count);
2476 if (counter->hw.irq_period && !neg)
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002477 perf_swcounter_overflow(counter, nmi, regs, addr);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002478}
2479
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002480static void perf_swcounter_ctx_event(struct perf_counter_context *ctx,
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002481 enum perf_event_types type, u32 event,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002482 u64 nr, int nmi, struct pt_regs *regs,
2483 u64 addr)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002484{
2485 struct perf_counter *counter;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002486
Peter Zijlstra01ef09d2009-03-19 20:26:11 +01002487 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002488 return;
2489
Peter Zijlstra592903c2009-03-13 12:21:36 +01002490 rcu_read_lock();
2491 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002492 if (perf_swcounter_match(counter, type, event, regs))
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002493 perf_swcounter_add(counter, nr, nmi, regs, addr);
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002494 }
Peter Zijlstra592903c2009-03-13 12:21:36 +01002495 rcu_read_unlock();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002496}
2497
Peter Zijlstra96f6d442009-03-23 18:22:07 +01002498static int *perf_swcounter_recursion_context(struct perf_cpu_context *cpuctx)
2499{
2500 if (in_nmi())
2501 return &cpuctx->recursion[3];
2502
2503 if (in_irq())
2504 return &cpuctx->recursion[2];
2505
2506 if (in_softirq())
2507 return &cpuctx->recursion[1];
2508
2509 return &cpuctx->recursion[0];
2510}
2511
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002512static void __perf_swcounter_event(enum perf_event_types type, u32 event,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002513 u64 nr, int nmi, struct pt_regs *regs,
2514 u64 addr)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002515{
2516 struct perf_cpu_context *cpuctx = &get_cpu_var(perf_cpu_context);
Peter Zijlstra96f6d442009-03-23 18:22:07 +01002517 int *recursion = perf_swcounter_recursion_context(cpuctx);
2518
2519 if (*recursion)
2520 goto out;
2521
2522 (*recursion)++;
2523 barrier();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002524
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002525 perf_swcounter_ctx_event(&cpuctx->ctx, type, event,
2526 nr, nmi, regs, addr);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002527 if (cpuctx->task_ctx) {
2528 perf_swcounter_ctx_event(cpuctx->task_ctx, type, event,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002529 nr, nmi, regs, addr);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002530 }
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002531
Peter Zijlstra96f6d442009-03-23 18:22:07 +01002532 barrier();
2533 (*recursion)--;
2534
2535out:
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002536 put_cpu_var(perf_cpu_context);
2537}
2538
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002539void
2540perf_swcounter_event(u32 event, u64 nr, int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002541{
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002542 __perf_swcounter_event(PERF_TYPE_SOFTWARE, event, nr, nmi, regs, addr);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002543}
2544
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002545static void perf_swcounter_read(struct perf_counter *counter)
2546{
2547 perf_swcounter_update(counter);
2548}
2549
2550static int perf_swcounter_enable(struct perf_counter *counter)
2551{
2552 perf_swcounter_set_period(counter);
2553 return 0;
2554}
2555
2556static void perf_swcounter_disable(struct perf_counter *counter)
2557{
2558 perf_swcounter_update(counter);
2559}
2560
Robert Richter4aeb0b42009-04-29 12:47:03 +02002561static const struct pmu perf_ops_generic = {
Peter Zijlstraac17dc82009-03-13 12:21:34 +01002562 .enable = perf_swcounter_enable,
2563 .disable = perf_swcounter_disable,
2564 .read = perf_swcounter_read,
2565};
2566
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002567/*
2568 * Software counter: cpu wall time clock
2569 */
2570
Paul Mackerras9abf8a02009-01-09 16:26:43 +11002571static void cpu_clock_perf_counter_update(struct perf_counter *counter)
2572{
2573 int cpu = raw_smp_processor_id();
2574 s64 prev;
2575 u64 now;
2576
2577 now = cpu_clock(cpu);
2578 prev = atomic64_read(&counter->hw.prev_count);
2579 atomic64_set(&counter->hw.prev_count, now);
2580 atomic64_add(now - prev, &counter->count);
2581}
2582
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002583static int cpu_clock_perf_counter_enable(struct perf_counter *counter)
2584{
2585 struct hw_perf_counter *hwc = &counter->hw;
2586 int cpu = raw_smp_processor_id();
2587
2588 atomic64_set(&hwc->prev_count, cpu_clock(cpu));
Peter Zijlstra039fc912009-03-13 16:43:47 +01002589 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2590 hwc->hrtimer.function = perf_swcounter_hrtimer;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002591 if (hwc->irq_period) {
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002592 __hrtimer_start_range_ns(&hwc->hrtimer,
2593 ns_to_ktime(hwc->irq_period), 0,
2594 HRTIMER_MODE_REL, 0);
2595 }
2596
2597 return 0;
2598}
2599
Ingo Molnar5c92d122008-12-11 13:21:10 +01002600static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
2601{
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002602 hrtimer_cancel(&counter->hw.hrtimer);
Paul Mackerras9abf8a02009-01-09 16:26:43 +11002603 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01002604}
2605
2606static void cpu_clock_perf_counter_read(struct perf_counter *counter)
2607{
Paul Mackerras9abf8a02009-01-09 16:26:43 +11002608 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01002609}
2610
Robert Richter4aeb0b42009-04-29 12:47:03 +02002611static const struct pmu perf_ops_cpu_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +01002612 .enable = cpu_clock_perf_counter_enable,
2613 .disable = cpu_clock_perf_counter_disable,
2614 .read = cpu_clock_perf_counter_read,
Ingo Molnar5c92d122008-12-11 13:21:10 +01002615};
2616
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01002617/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002618 * Software counter: task time clock
2619 */
2620
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02002621static void task_clock_perf_counter_update(struct perf_counter *counter, u64 now)
Ingo Molnarbae43c92008-12-11 14:03:20 +01002622{
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02002623 u64 prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002624 s64 delta;
Ingo Molnarbae43c92008-12-11 14:03:20 +01002625
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02002626 prev = atomic64_xchg(&counter->hw.prev_count, now);
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002627 delta = now - prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002628 atomic64_add(delta, &counter->count);
Ingo Molnarbae43c92008-12-11 14:03:20 +01002629}
2630
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01002631static int task_clock_perf_counter_enable(struct perf_counter *counter)
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002632{
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002633 struct hw_perf_counter *hwc = &counter->hw;
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02002634 u64 now;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002635
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02002636 now = counter->ctx->time;
2637
2638 atomic64_set(&hwc->prev_count, now);
Peter Zijlstra039fc912009-03-13 16:43:47 +01002639 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2640 hwc->hrtimer.function = perf_swcounter_hrtimer;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002641 if (hwc->irq_period) {
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002642 __hrtimer_start_range_ns(&hwc->hrtimer,
2643 ns_to_ktime(hwc->irq_period), 0,
2644 HRTIMER_MODE_REL, 0);
2645 }
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01002646
2647 return 0;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002648}
2649
2650static void task_clock_perf_counter_disable(struct perf_counter *counter)
2651{
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002652 hrtimer_cancel(&counter->hw.hrtimer);
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02002653 task_clock_perf_counter_update(counter, counter->ctx->time);
2654
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002655}
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01002656
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002657static void task_clock_perf_counter_read(struct perf_counter *counter)
2658{
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02002659 u64 time;
2660
2661 if (!in_nmi()) {
2662 update_context_time(counter->ctx);
2663 time = counter->ctx->time;
2664 } else {
2665 u64 now = perf_clock();
2666 u64 delta = now - counter->ctx->timestamp;
2667 time = counter->ctx->time + delta;
2668 }
2669
2670 task_clock_perf_counter_update(counter, time);
Ingo Molnarbae43c92008-12-11 14:03:20 +01002671}
2672
Robert Richter4aeb0b42009-04-29 12:47:03 +02002673static const struct pmu perf_ops_task_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +01002674 .enable = task_clock_perf_counter_enable,
2675 .disable = task_clock_perf_counter_disable,
2676 .read = task_clock_perf_counter_read,
Ingo Molnarbae43c92008-12-11 14:03:20 +01002677};
2678
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002679/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002680 * Software counter: cpu migrations
2681 */
2682
Paul Mackerras23a185c2009-02-09 22:42:47 +11002683static inline u64 get_cpu_migrations(struct perf_counter *counter)
Ingo Molnar6c594c22008-12-14 12:34:15 +01002684{
Paul Mackerras23a185c2009-02-09 22:42:47 +11002685 struct task_struct *curr = counter->ctx->task;
2686
2687 if (curr)
2688 return curr->se.nr_migrations;
2689 return cpu_nr_migrations(smp_processor_id());
Ingo Molnar6c594c22008-12-14 12:34:15 +01002690}
2691
2692static void cpu_migrations_perf_counter_update(struct perf_counter *counter)
2693{
2694 u64 prev, now;
2695 s64 delta;
2696
2697 prev = atomic64_read(&counter->hw.prev_count);
Paul Mackerras23a185c2009-02-09 22:42:47 +11002698 now = get_cpu_migrations(counter);
Ingo Molnar6c594c22008-12-14 12:34:15 +01002699
2700 atomic64_set(&counter->hw.prev_count, now);
2701
2702 delta = now - prev;
Ingo Molnar6c594c22008-12-14 12:34:15 +01002703
2704 atomic64_add(delta, &counter->count);
2705}
2706
2707static void cpu_migrations_perf_counter_read(struct perf_counter *counter)
2708{
2709 cpu_migrations_perf_counter_update(counter);
2710}
2711
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01002712static int cpu_migrations_perf_counter_enable(struct perf_counter *counter)
Ingo Molnar6c594c22008-12-14 12:34:15 +01002713{
Paul Mackerrasc07c99b2009-02-13 22:10:34 +11002714 if (counter->prev_state <= PERF_COUNTER_STATE_OFF)
2715 atomic64_set(&counter->hw.prev_count,
2716 get_cpu_migrations(counter));
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01002717 return 0;
Ingo Molnar6c594c22008-12-14 12:34:15 +01002718}
2719
2720static void cpu_migrations_perf_counter_disable(struct perf_counter *counter)
2721{
2722 cpu_migrations_perf_counter_update(counter);
2723}
2724
Robert Richter4aeb0b42009-04-29 12:47:03 +02002725static const struct pmu perf_ops_cpu_migrations = {
Ingo Molnar76715812008-12-17 14:20:28 +01002726 .enable = cpu_migrations_perf_counter_enable,
2727 .disable = cpu_migrations_perf_counter_disable,
2728 .read = cpu_migrations_perf_counter_read,
Ingo Molnar6c594c22008-12-14 12:34:15 +01002729};
2730
Peter Zijlstrae077df42009-03-19 20:26:17 +01002731#ifdef CONFIG_EVENT_PROFILE
2732void perf_tpcounter_event(int event_id)
2733{
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002734 struct pt_regs *regs = get_irq_regs();
2735
2736 if (!regs)
2737 regs = task_pt_regs(current);
2738
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002739 __perf_swcounter_event(PERF_TYPE_TRACEPOINT, event_id, 1, 1, regs, 0);
Peter Zijlstrae077df42009-03-19 20:26:17 +01002740}
Steven Whitehouseff7b1b42009-04-15 16:55:05 +01002741EXPORT_SYMBOL_GPL(perf_tpcounter_event);
Peter Zijlstrae077df42009-03-19 20:26:17 +01002742
2743extern int ftrace_profile_enable(int);
2744extern void ftrace_profile_disable(int);
2745
2746static void tp_perf_counter_destroy(struct perf_counter *counter)
2747{
Peter Zijlstraf4a2deb42009-03-23 18:22:06 +01002748 ftrace_profile_disable(perf_event_id(&counter->hw_event));
Peter Zijlstrae077df42009-03-19 20:26:17 +01002749}
2750
Robert Richter4aeb0b42009-04-29 12:47:03 +02002751static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
Peter Zijlstrae077df42009-03-19 20:26:17 +01002752{
Peter Zijlstraf4a2deb42009-03-23 18:22:06 +01002753 int event_id = perf_event_id(&counter->hw_event);
Peter Zijlstrae077df42009-03-19 20:26:17 +01002754 int ret;
2755
2756 ret = ftrace_profile_enable(event_id);
2757 if (ret)
2758 return NULL;
2759
2760 counter->destroy = tp_perf_counter_destroy;
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002761 counter->hw.irq_period = counter->hw_event.irq_period;
Peter Zijlstrae077df42009-03-19 20:26:17 +01002762
2763 return &perf_ops_generic;
2764}
2765#else
Robert Richter4aeb0b42009-04-29 12:47:03 +02002766static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
Peter Zijlstrae077df42009-03-19 20:26:17 +01002767{
2768 return NULL;
2769}
2770#endif
2771
Robert Richter4aeb0b42009-04-29 12:47:03 +02002772static const struct pmu *sw_perf_counter_init(struct perf_counter *counter)
Ingo Molnar5c92d122008-12-11 13:21:10 +01002773{
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002774 struct perf_counter_hw_event *hw_event = &counter->hw_event;
Robert Richter4aeb0b42009-04-29 12:47:03 +02002775 const struct pmu *pmu = NULL;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002776 struct hw_perf_counter *hwc = &counter->hw;
Ingo Molnar5c92d122008-12-11 13:21:10 +01002777
Paul Mackerras0475f9e2009-02-11 14:35:35 +11002778 /*
2779 * Software counters (currently) can't in general distinguish
2780 * between user, kernel and hypervisor events.
2781 * However, context switches and cpu migrations are considered
2782 * to be kernel events, and page faults are never hypervisor
2783 * events.
2784 */
Peter Zijlstraf4a2deb42009-03-23 18:22:06 +01002785 switch (perf_event_id(&counter->hw_event)) {
Ingo Molnar5c92d122008-12-11 13:21:10 +01002786 case PERF_COUNT_CPU_CLOCK:
Robert Richter4aeb0b42009-04-29 12:47:03 +02002787 pmu = &perf_ops_cpu_clock;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002788
2789 if (hw_event->irq_period && hw_event->irq_period < 10000)
2790 hw_event->irq_period = 10000;
Ingo Molnar5c92d122008-12-11 13:21:10 +01002791 break;
Ingo Molnarbae43c92008-12-11 14:03:20 +01002792 case PERF_COUNT_TASK_CLOCK:
Paul Mackerras23a185c2009-02-09 22:42:47 +11002793 /*
2794 * If the user instantiates this as a per-cpu counter,
2795 * use the cpu_clock counter instead.
2796 */
2797 if (counter->ctx->task)
Robert Richter4aeb0b42009-04-29 12:47:03 +02002798 pmu = &perf_ops_task_clock;
Paul Mackerras23a185c2009-02-09 22:42:47 +11002799 else
Robert Richter4aeb0b42009-04-29 12:47:03 +02002800 pmu = &perf_ops_cpu_clock;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002801
2802 if (hw_event->irq_period && hw_event->irq_period < 10000)
2803 hw_event->irq_period = 10000;
Ingo Molnarbae43c92008-12-11 14:03:20 +01002804 break;
Ingo Molnare06c61a2008-12-14 14:44:31 +01002805 case PERF_COUNT_PAGE_FAULTS:
Peter Zijlstraac17dc82009-03-13 12:21:34 +01002806 case PERF_COUNT_PAGE_FAULTS_MIN:
2807 case PERF_COUNT_PAGE_FAULTS_MAJ:
Ingo Molnar5d6a27d2008-12-14 12:28:33 +01002808 case PERF_COUNT_CONTEXT_SWITCHES:
Robert Richter4aeb0b42009-04-29 12:47:03 +02002809 pmu = &perf_ops_generic;
Ingo Molnar5d6a27d2008-12-14 12:28:33 +01002810 break;
Ingo Molnar6c594c22008-12-14 12:34:15 +01002811 case PERF_COUNT_CPU_MIGRATIONS:
Paul Mackerras0475f9e2009-02-11 14:35:35 +11002812 if (!counter->hw_event.exclude_kernel)
Robert Richter4aeb0b42009-04-29 12:47:03 +02002813 pmu = &perf_ops_cpu_migrations;
Ingo Molnar6c594c22008-12-14 12:34:15 +01002814 break;
Ingo Molnar5c92d122008-12-11 13:21:10 +01002815 }
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002816
Robert Richter4aeb0b42009-04-29 12:47:03 +02002817 if (pmu)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002818 hwc->irq_period = hw_event->irq_period;
2819
Robert Richter4aeb0b42009-04-29 12:47:03 +02002820 return pmu;
Ingo Molnar5c92d122008-12-11 13:21:10 +01002821}
2822
Thomas Gleixner0793a612008-12-04 20:12:29 +01002823/*
2824 * Allocate and initialize a counter structure
2825 */
2826static struct perf_counter *
Ingo Molnar04289bb2008-12-11 08:38:42 +01002827perf_counter_alloc(struct perf_counter_hw_event *hw_event,
2828 int cpu,
Paul Mackerras23a185c2009-02-09 22:42:47 +11002829 struct perf_counter_context *ctx,
Ingo Molnar9b51f662008-12-12 13:49:45 +01002830 struct perf_counter *group_leader,
2831 gfp_t gfpflags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01002832{
Robert Richter4aeb0b42009-04-29 12:47:03 +02002833 const struct pmu *pmu;
Ingo Molnar621a01e2008-12-11 12:46:46 +01002834 struct perf_counter *counter;
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02002835 long err;
Thomas Gleixner0793a612008-12-04 20:12:29 +01002836
Ingo Molnar9b51f662008-12-12 13:49:45 +01002837 counter = kzalloc(sizeof(*counter), gfpflags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002838 if (!counter)
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02002839 return ERR_PTR(-ENOMEM);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002840
Ingo Molnar04289bb2008-12-11 08:38:42 +01002841 /*
2842 * Single counters are their own group leaders, with an
2843 * empty sibling list:
2844 */
2845 if (!group_leader)
2846 group_leader = counter;
2847
Thomas Gleixner0793a612008-12-04 20:12:29 +01002848 mutex_init(&counter->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01002849 INIT_LIST_HEAD(&counter->list_entry);
Peter Zijlstra592903c2009-03-13 12:21:36 +01002850 INIT_LIST_HEAD(&counter->event_entry);
Ingo Molnar04289bb2008-12-11 08:38:42 +01002851 INIT_LIST_HEAD(&counter->sibling_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002852 init_waitqueue_head(&counter->waitq);
2853
Peter Zijlstra7b732a752009-03-23 18:22:10 +01002854 mutex_init(&counter->mmap_mutex);
2855
Paul Mackerrasd859e292009-01-17 18:10:22 +11002856 INIT_LIST_HEAD(&counter->child_list);
2857
Ingo Molnar9f66a382008-12-10 12:33:23 +01002858 counter->cpu = cpu;
2859 counter->hw_event = *hw_event;
Ingo Molnar04289bb2008-12-11 08:38:42 +01002860 counter->group_leader = group_leader;
Robert Richter4aeb0b42009-04-29 12:47:03 +02002861 counter->pmu = NULL;
Paul Mackerras23a185c2009-02-09 22:42:47 +11002862 counter->ctx = ctx;
Ingo Molnar621a01e2008-12-11 12:46:46 +01002863
Ingo Molnar235c7fc2008-12-21 14:43:25 +01002864 counter->state = PERF_COUNTER_STATE_INACTIVE;
Ingo Molnara86ed502008-12-17 00:43:10 +01002865 if (hw_event->disabled)
2866 counter->state = PERF_COUNTER_STATE_OFF;
2867
Robert Richter4aeb0b42009-04-29 12:47:03 +02002868 pmu = NULL;
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002869
Peter Zijlstra2023b352009-05-05 17:50:26 +02002870 /*
2871 * we currently do not support PERF_RECORD_GROUP on inherited counters
2872 */
2873 if (hw_event->inherit && (hw_event->record_type & PERF_RECORD_GROUP))
2874 goto done;
2875
Peter Zijlstraf4a2deb42009-03-23 18:22:06 +01002876 if (perf_event_raw(hw_event)) {
Robert Richter4aeb0b42009-04-29 12:47:03 +02002877 pmu = hw_perf_counter_init(counter);
Peter Zijlstraf4a2deb42009-03-23 18:22:06 +01002878 goto done;
2879 }
2880
2881 switch (perf_event_type(hw_event)) {
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002882 case PERF_TYPE_HARDWARE:
Robert Richter4aeb0b42009-04-29 12:47:03 +02002883 pmu = hw_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002884 break;
2885
2886 case PERF_TYPE_SOFTWARE:
Robert Richter4aeb0b42009-04-29 12:47:03 +02002887 pmu = sw_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002888 break;
2889
2890 case PERF_TYPE_TRACEPOINT:
Robert Richter4aeb0b42009-04-29 12:47:03 +02002891 pmu = tp_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002892 break;
2893 }
Peter Zijlstraf4a2deb42009-03-23 18:22:06 +01002894done:
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02002895 err = 0;
Robert Richter4aeb0b42009-04-29 12:47:03 +02002896 if (!pmu)
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02002897 err = -EINVAL;
Robert Richter4aeb0b42009-04-29 12:47:03 +02002898 else if (IS_ERR(pmu))
2899 err = PTR_ERR(pmu);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02002900
2901 if (err) {
2902 kfree(counter);
2903 return ERR_PTR(err);
2904 }
2905
Robert Richter4aeb0b42009-04-29 12:47:03 +02002906 counter->pmu = pmu;
Thomas Gleixner0793a612008-12-04 20:12:29 +01002907
Peter Zijlstra7fc23a52009-05-08 18:52:21 +02002908 atomic_inc(&nr_counters);
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002909 if (counter->hw_event.mmap)
2910 atomic_inc(&nr_mmap_tracking);
2911 if (counter->hw_event.munmap)
2912 atomic_inc(&nr_munmap_tracking);
2913 if (counter->hw_event.comm)
2914 atomic_inc(&nr_comm_tracking);
2915
Thomas Gleixner0793a612008-12-04 20:12:29 +01002916 return counter;
2917}
2918
2919/**
Paul Mackerras2743a5b2009-03-04 20:36:51 +11002920 * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
Ingo Molnar9f66a382008-12-10 12:33:23 +01002921 *
2922 * @hw_event_uptr: event type attributes for monitoring/sampling
Thomas Gleixner0793a612008-12-04 20:12:29 +01002923 * @pid: target pid
Ingo Molnar9f66a382008-12-10 12:33:23 +01002924 * @cpu: target cpu
2925 * @group_fd: group leader counter fd
Thomas Gleixner0793a612008-12-04 20:12:29 +01002926 */
Paul Mackerras2743a5b2009-03-04 20:36:51 +11002927SYSCALL_DEFINE5(perf_counter_open,
Paul Mackerrasf3dfd262009-02-26 22:43:46 +11002928 const struct perf_counter_hw_event __user *, hw_event_uptr,
Paul Mackerras2743a5b2009-03-04 20:36:51 +11002929 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01002930{
Ingo Molnar04289bb2008-12-11 08:38:42 +01002931 struct perf_counter *counter, *group_leader;
Ingo Molnar9f66a382008-12-10 12:33:23 +01002932 struct perf_counter_hw_event hw_event;
Ingo Molnar04289bb2008-12-11 08:38:42 +01002933 struct perf_counter_context *ctx;
Ingo Molnar9b51f662008-12-12 13:49:45 +01002934 struct file *counter_file = NULL;
Ingo Molnar04289bb2008-12-11 08:38:42 +01002935 struct file *group_file = NULL;
2936 int fput_needed = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01002937 int fput_needed2 = 0;
Thomas Gleixner0793a612008-12-04 20:12:29 +01002938 int ret;
2939
Paul Mackerras2743a5b2009-03-04 20:36:51 +11002940 /* for future expandability... */
2941 if (flags)
2942 return -EINVAL;
2943
Ingo Molnar9f66a382008-12-10 12:33:23 +01002944 if (copy_from_user(&hw_event, hw_event_uptr, sizeof(hw_event)) != 0)
Thomas Gleixnereab656a2008-12-08 19:26:59 +01002945 return -EFAULT;
2946
Ingo Molnar04289bb2008-12-11 08:38:42 +01002947 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01002948 * Get the target context (task or percpu):
2949 */
2950 ctx = find_get_context(pid, cpu);
2951 if (IS_ERR(ctx))
2952 return PTR_ERR(ctx);
2953
2954 /*
2955 * Look up the group leader (we will attach this counter to it):
Ingo Molnar04289bb2008-12-11 08:38:42 +01002956 */
2957 group_leader = NULL;
2958 if (group_fd != -1) {
2959 ret = -EINVAL;
2960 group_file = fget_light(group_fd, &fput_needed);
2961 if (!group_file)
Ingo Molnarccff2862008-12-11 11:26:29 +01002962 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01002963 if (group_file->f_op != &perf_fops)
Ingo Molnarccff2862008-12-11 11:26:29 +01002964 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01002965
2966 group_leader = group_file->private_data;
2967 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01002968 * Do not allow a recursive hierarchy (this new sibling
2969 * becoming part of another group-sibling):
Ingo Molnar04289bb2008-12-11 08:38:42 +01002970 */
Ingo Molnarccff2862008-12-11 11:26:29 +01002971 if (group_leader->group_leader != group_leader)
2972 goto err_put_context;
2973 /*
2974 * Do not allow to attach to a group in a different
2975 * task or CPU context:
2976 */
2977 if (group_leader->ctx != ctx)
2978 goto err_put_context;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11002979 /*
2980 * Only a group leader can be exclusive or pinned
2981 */
2982 if (hw_event.exclusive || hw_event.pinned)
2983 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01002984 }
2985
Paul Mackerras23a185c2009-02-09 22:42:47 +11002986 counter = perf_counter_alloc(&hw_event, cpu, ctx, group_leader,
2987 GFP_KERNEL);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02002988 ret = PTR_ERR(counter);
2989 if (IS_ERR(counter))
Thomas Gleixner0793a612008-12-04 20:12:29 +01002990 goto err_put_context;
2991
Thomas Gleixner0793a612008-12-04 20:12:29 +01002992 ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
2993 if (ret < 0)
Ingo Molnar9b51f662008-12-12 13:49:45 +01002994 goto err_free_put_context;
2995
2996 counter_file = fget_light(ret, &fput_needed2);
2997 if (!counter_file)
2998 goto err_free_put_context;
2999
3000 counter->filp = counter_file;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003001 mutex_lock(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003002 perf_install_in_context(ctx, counter, cpu);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003003 mutex_unlock(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003004
3005 fput_light(counter_file, fput_needed2);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003006
Ingo Molnar04289bb2008-12-11 08:38:42 +01003007out_fput:
3008 fput_light(group_file, fput_needed);
3009
Thomas Gleixner0793a612008-12-04 20:12:29 +01003010 return ret;
3011
Ingo Molnar9b51f662008-12-12 13:49:45 +01003012err_free_put_context:
Thomas Gleixner0793a612008-12-04 20:12:29 +01003013 kfree(counter);
3014
3015err_put_context:
3016 put_context(ctx);
3017
Ingo Molnar04289bb2008-12-11 08:38:42 +01003018 goto out_fput;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003019}
3020
Ingo Molnar9b51f662008-12-12 13:49:45 +01003021/*
3022 * Initialize the perf_counter context in a task_struct:
3023 */
3024static void
3025__perf_counter_init_context(struct perf_counter_context *ctx,
3026 struct task_struct *task)
3027{
3028 memset(ctx, 0, sizeof(*ctx));
3029 spin_lock_init(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003030 mutex_init(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003031 INIT_LIST_HEAD(&ctx->counter_list);
Peter Zijlstra592903c2009-03-13 12:21:36 +01003032 INIT_LIST_HEAD(&ctx->event_list);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003033 ctx->task = task;
3034}
3035
3036/*
3037 * inherit a counter from parent task to child task:
3038 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11003039static struct perf_counter *
Ingo Molnar9b51f662008-12-12 13:49:45 +01003040inherit_counter(struct perf_counter *parent_counter,
3041 struct task_struct *parent,
3042 struct perf_counter_context *parent_ctx,
3043 struct task_struct *child,
Paul Mackerrasd859e292009-01-17 18:10:22 +11003044 struct perf_counter *group_leader,
Ingo Molnar9b51f662008-12-12 13:49:45 +01003045 struct perf_counter_context *child_ctx)
3046{
3047 struct perf_counter *child_counter;
3048
Paul Mackerrasd859e292009-01-17 18:10:22 +11003049 /*
3050 * Instead of creating recursive hierarchies of counters,
3051 * we link inherited counters back to the original parent,
3052 * which has a filp for sure, which we use as the reference
3053 * count:
3054 */
3055 if (parent_counter->parent)
3056 parent_counter = parent_counter->parent;
3057
Ingo Molnar9b51f662008-12-12 13:49:45 +01003058 child_counter = perf_counter_alloc(&parent_counter->hw_event,
Paul Mackerras23a185c2009-02-09 22:42:47 +11003059 parent_counter->cpu, child_ctx,
3060 group_leader, GFP_KERNEL);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003061 if (IS_ERR(child_counter))
3062 return child_counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003063
3064 /*
3065 * Link it up in the child's context:
3066 */
Ingo Molnar9b51f662008-12-12 13:49:45 +01003067 child_counter->task = child;
Paul Mackerras53cfbf52009-03-25 22:46:58 +11003068 add_counter_to_ctx(child_counter, child_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003069
3070 child_counter->parent = parent_counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003071 /*
3072 * inherit into child's child as well:
3073 */
3074 child_counter->hw_event.inherit = 1;
3075
3076 /*
3077 * Get a reference to the parent filp - we will fput it
3078 * when the child counter exits. This is safe to do because
3079 * we are in the parent and we know that the filp still
3080 * exists and has a nonzero count:
3081 */
3082 atomic_long_inc(&parent_counter->filp->f_count);
3083
Paul Mackerrasd859e292009-01-17 18:10:22 +11003084 /*
3085 * Link this into the parent counter's child list
3086 */
3087 mutex_lock(&parent_counter->mutex);
3088 list_add_tail(&child_counter->child_list, &parent_counter->child_list);
3089
3090 /*
3091 * Make the child state follow the state of the parent counter,
3092 * not its hw_event.disabled bit. We hold the parent's mutex,
3093 * so we won't race with perf_counter_{en,dis}able_family.
3094 */
3095 if (parent_counter->state >= PERF_COUNTER_STATE_INACTIVE)
3096 child_counter->state = PERF_COUNTER_STATE_INACTIVE;
3097 else
3098 child_counter->state = PERF_COUNTER_STATE_OFF;
3099
3100 mutex_unlock(&parent_counter->mutex);
3101
3102 return child_counter;
3103}
3104
3105static int inherit_group(struct perf_counter *parent_counter,
3106 struct task_struct *parent,
3107 struct perf_counter_context *parent_ctx,
3108 struct task_struct *child,
3109 struct perf_counter_context *child_ctx)
3110{
3111 struct perf_counter *leader;
3112 struct perf_counter *sub;
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003113 struct perf_counter *child_ctr;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003114
3115 leader = inherit_counter(parent_counter, parent, parent_ctx,
3116 child, NULL, child_ctx);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003117 if (IS_ERR(leader))
3118 return PTR_ERR(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003119 list_for_each_entry(sub, &parent_counter->sibling_list, list_entry) {
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003120 child_ctr = inherit_counter(sub, parent, parent_ctx,
3121 child, leader, child_ctx);
3122 if (IS_ERR(child_ctr))
3123 return PTR_ERR(child_ctr);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003124 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01003125 return 0;
3126}
3127
Paul Mackerrasd859e292009-01-17 18:10:22 +11003128static void sync_child_counter(struct perf_counter *child_counter,
3129 struct perf_counter *parent_counter)
3130{
3131 u64 parent_val, child_val;
3132
3133 parent_val = atomic64_read(&parent_counter->count);
3134 child_val = atomic64_read(&child_counter->count);
3135
3136 /*
3137 * Add back the child's count to the parent's count:
3138 */
3139 atomic64_add(child_val, &parent_counter->count);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11003140 atomic64_add(child_counter->total_time_enabled,
3141 &parent_counter->child_total_time_enabled);
3142 atomic64_add(child_counter->total_time_running,
3143 &parent_counter->child_total_time_running);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003144
3145 /*
3146 * Remove this counter from the parent's list
3147 */
3148 mutex_lock(&parent_counter->mutex);
3149 list_del_init(&child_counter->child_list);
3150 mutex_unlock(&parent_counter->mutex);
3151
3152 /*
3153 * Release the parent counter, if this was the last
3154 * reference to it.
3155 */
3156 fput(parent_counter->filp);
3157}
3158
Ingo Molnar9b51f662008-12-12 13:49:45 +01003159static void
3160__perf_counter_exit_task(struct task_struct *child,
3161 struct perf_counter *child_counter,
3162 struct perf_counter_context *child_ctx)
3163{
3164 struct perf_counter *parent_counter;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003165 struct perf_counter *sub, *tmp;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003166
3167 /*
Ingo Molnar235c7fc2008-12-21 14:43:25 +01003168 * If we do not self-reap then we have to wait for the
3169 * child task to unschedule (it will happen for sure),
3170 * so that its counter is at its final count. (This
3171 * condition triggers rarely - child tasks usually get
3172 * off their CPU before the parent has a chance to
3173 * get this far into the reaping action)
Ingo Molnar9b51f662008-12-12 13:49:45 +01003174 */
Ingo Molnar235c7fc2008-12-21 14:43:25 +01003175 if (child != current) {
3176 wait_task_inactive(child, 0);
3177 list_del_init(&child_counter->list_entry);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11003178 update_counter_times(child_counter);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01003179 } else {
Ingo Molnar0cc0c022008-12-14 23:20:36 +01003180 struct perf_cpu_context *cpuctx;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01003181 unsigned long flags;
3182 u64 perf_flags;
3183
3184 /*
3185 * Disable and unlink this counter.
3186 *
3187 * Be careful about zapping the list - IRQ/NMI context
3188 * could still be processing it:
3189 */
Peter Zijlstra849691a2009-04-06 11:45:12 +02003190 local_irq_save(flags);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01003191 perf_flags = hw_perf_save_disable();
Ingo Molnar0cc0c022008-12-14 23:20:36 +01003192
3193 cpuctx = &__get_cpu_var(perf_cpu_context);
3194
Paul Mackerrasd859e292009-01-17 18:10:22 +11003195 group_sched_out(child_counter, cpuctx, child_ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11003196 update_counter_times(child_counter);
Ingo Molnar0cc0c022008-12-14 23:20:36 +01003197
Ingo Molnar235c7fc2008-12-21 14:43:25 +01003198 list_del_init(&child_counter->list_entry);
3199
3200 child_ctx->nr_counters--;
3201
3202 hw_perf_restore(perf_flags);
Peter Zijlstra849691a2009-04-06 11:45:12 +02003203 local_irq_restore(flags);
Ingo Molnar0cc0c022008-12-14 23:20:36 +01003204 }
3205
Ingo Molnar9b51f662008-12-12 13:49:45 +01003206 parent_counter = child_counter->parent;
3207 /*
3208 * It can happen that parent exits first, and has counters
3209 * that are still around due to the child reference. These
3210 * counters need to be zapped - but otherwise linger.
3211 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11003212 if (parent_counter) {
3213 sync_child_counter(child_counter, parent_counter);
3214 list_for_each_entry_safe(sub, tmp, &child_counter->sibling_list,
3215 list_entry) {
Paul Mackerras4bcf3492009-02-11 13:53:19 +01003216 if (sub->parent) {
Paul Mackerrasd859e292009-01-17 18:10:22 +11003217 sync_child_counter(sub, sub->parent);
Peter Zijlstraf1600952009-03-19 20:26:16 +01003218 free_counter(sub);
Paul Mackerras4bcf3492009-02-11 13:53:19 +01003219 }
Paul Mackerrasd859e292009-01-17 18:10:22 +11003220 }
Peter Zijlstraf1600952009-03-19 20:26:16 +01003221 free_counter(child_counter);
Paul Mackerras4bcf3492009-02-11 13:53:19 +01003222 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01003223}
3224
3225/*
Paul Mackerrasd859e292009-01-17 18:10:22 +11003226 * When a child task exits, feed back counter values to parent counters.
Ingo Molnar9b51f662008-12-12 13:49:45 +01003227 *
Paul Mackerrasd859e292009-01-17 18:10:22 +11003228 * Note: we may be running in child context, but the PID is not hashed
Ingo Molnar9b51f662008-12-12 13:49:45 +01003229 * anymore so new counters will not be added.
3230 */
3231void perf_counter_exit_task(struct task_struct *child)
3232{
3233 struct perf_counter *child_counter, *tmp;
3234 struct perf_counter_context *child_ctx;
3235
3236 child_ctx = &child->perf_counter_ctx;
3237
3238 if (likely(!child_ctx->nr_counters))
3239 return;
3240
3241 list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list,
3242 list_entry)
3243 __perf_counter_exit_task(child, child_counter, child_ctx);
3244}
3245
3246/*
3247 * Initialize the perf_counter context in task_struct
3248 */
3249void perf_counter_init_task(struct task_struct *child)
3250{
3251 struct perf_counter_context *child_ctx, *parent_ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003252 struct perf_counter *counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003253 struct task_struct *parent = current;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003254
3255 child_ctx = &child->perf_counter_ctx;
3256 parent_ctx = &parent->perf_counter_ctx;
3257
3258 __perf_counter_init_context(child_ctx, child);
3259
3260 /*
3261 * This is executed from the parent task context, so inherit
3262 * counters that have been marked for cloning:
3263 */
3264
3265 if (likely(!parent_ctx->nr_counters))
3266 return;
3267
3268 /*
3269 * Lock the parent list. No need to lock the child - not PID
3270 * hashed yet and not running, so nobody can access it.
3271 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11003272 mutex_lock(&parent_ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003273
3274 /*
3275 * We dont have to disable NMIs - we are only looking at
3276 * the list, not manipulating it:
3277 */
3278 list_for_each_entry(counter, &parent_ctx->counter_list, list_entry) {
Paul Mackerrasd859e292009-01-17 18:10:22 +11003279 if (!counter->hw_event.inherit)
Ingo Molnar9b51f662008-12-12 13:49:45 +01003280 continue;
3281
Paul Mackerrasd859e292009-01-17 18:10:22 +11003282 if (inherit_group(counter, parent,
Ingo Molnar9b51f662008-12-12 13:49:45 +01003283 parent_ctx, child, child_ctx))
3284 break;
3285 }
3286
Paul Mackerrasd859e292009-01-17 18:10:22 +11003287 mutex_unlock(&parent_ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003288}
3289
Ingo Molnar04289bb2008-12-11 08:38:42 +01003290static void __cpuinit perf_counter_init_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003291{
Ingo Molnar04289bb2008-12-11 08:38:42 +01003292 struct perf_cpu_context *cpuctx;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003293
Ingo Molnar04289bb2008-12-11 08:38:42 +01003294 cpuctx = &per_cpu(perf_cpu_context, cpu);
3295 __perf_counter_init_context(&cpuctx->ctx, NULL);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003296
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003297 spin_lock(&perf_resource_lock);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003298 cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu;
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003299 spin_unlock(&perf_resource_lock);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003300
Paul Mackerras01d02872009-01-14 13:44:19 +11003301 hw_perf_counter_setup(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003302}
3303
3304#ifdef CONFIG_HOTPLUG_CPU
Ingo Molnar04289bb2008-12-11 08:38:42 +01003305static void __perf_counter_exit_cpu(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003306{
3307 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
3308 struct perf_counter_context *ctx = &cpuctx->ctx;
3309 struct perf_counter *counter, *tmp;
3310
Ingo Molnar04289bb2008-12-11 08:38:42 +01003311 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry)
3312 __perf_counter_remove_from_context(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003313}
Ingo Molnar04289bb2008-12-11 08:38:42 +01003314static void perf_counter_exit_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003315{
Paul Mackerrasd859e292009-01-17 18:10:22 +11003316 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
3317 struct perf_counter_context *ctx = &cpuctx->ctx;
3318
3319 mutex_lock(&ctx->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003320 smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003321 mutex_unlock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003322}
3323#else
Ingo Molnar04289bb2008-12-11 08:38:42 +01003324static inline void perf_counter_exit_cpu(int cpu) { }
Thomas Gleixner0793a612008-12-04 20:12:29 +01003325#endif
3326
3327static int __cpuinit
3328perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
3329{
3330 unsigned int cpu = (long)hcpu;
3331
3332 switch (action) {
3333
3334 case CPU_UP_PREPARE:
3335 case CPU_UP_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01003336 perf_counter_init_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003337 break;
3338
3339 case CPU_DOWN_PREPARE:
3340 case CPU_DOWN_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01003341 perf_counter_exit_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003342 break;
3343
3344 default:
3345 break;
3346 }
3347
3348 return NOTIFY_OK;
3349}
3350
3351static struct notifier_block __cpuinitdata perf_cpu_nb = {
3352 .notifier_call = perf_cpu_notify,
3353};
3354
Ingo Molnar0d905bc2009-05-04 19:13:30 +02003355void __init perf_counter_init(void)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003356{
3357 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
3358 (void *)(long)smp_processor_id());
3359 register_cpu_notifier(&perf_cpu_nb);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003360}
Thomas Gleixner0793a612008-12-04 20:12:29 +01003361
3362static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
3363{
3364 return sprintf(buf, "%d\n", perf_reserved_percpu);
3365}
3366
3367static ssize_t
3368perf_set_reserve_percpu(struct sysdev_class *class,
3369 const char *buf,
3370 size_t count)
3371{
3372 struct perf_cpu_context *cpuctx;
3373 unsigned long val;
3374 int err, cpu, mpt;
3375
3376 err = strict_strtoul(buf, 10, &val);
3377 if (err)
3378 return err;
3379 if (val > perf_max_counters)
3380 return -EINVAL;
3381
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003382 spin_lock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003383 perf_reserved_percpu = val;
3384 for_each_online_cpu(cpu) {
3385 cpuctx = &per_cpu(perf_cpu_context, cpu);
3386 spin_lock_irq(&cpuctx->ctx.lock);
3387 mpt = min(perf_max_counters - cpuctx->ctx.nr_counters,
3388 perf_max_counters - perf_reserved_percpu);
3389 cpuctx->max_pertask = mpt;
3390 spin_unlock_irq(&cpuctx->ctx.lock);
3391 }
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003392 spin_unlock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003393
3394 return count;
3395}
3396
3397static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
3398{
3399 return sprintf(buf, "%d\n", perf_overcommit);
3400}
3401
3402static ssize_t
3403perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
3404{
3405 unsigned long val;
3406 int err;
3407
3408 err = strict_strtoul(buf, 10, &val);
3409 if (err)
3410 return err;
3411 if (val > 1)
3412 return -EINVAL;
3413
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003414 spin_lock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003415 perf_overcommit = val;
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003416 spin_unlock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003417
3418 return count;
3419}
3420
3421static SYSDEV_CLASS_ATTR(
3422 reserve_percpu,
3423 0644,
3424 perf_show_reserve_percpu,
3425 perf_set_reserve_percpu
3426 );
3427
3428static SYSDEV_CLASS_ATTR(
3429 overcommit,
3430 0644,
3431 perf_show_overcommit,
3432 perf_set_overcommit
3433 );
3434
3435static struct attribute *perfclass_attrs[] = {
3436 &attr_reserve_percpu.attr,
3437 &attr_overcommit.attr,
3438 NULL
3439};
3440
3441static struct attribute_group perfclass_attr_group = {
3442 .attrs = perfclass_attrs,
3443 .name = "perf_counters",
3444};
3445
3446static int __init perf_counter_sysfs_init(void)
3447{
3448 return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
3449 &perfclass_attr_group);
3450}
3451device_initcall(perf_counter_sysfs_init);