blob: f1110ac1267bcf06e4dddb49c893690afd346d26 [file] [log] [blame]
Thomas Gleixner0793a612008-12-04 20:12:29 +01001/*
2 * Performance counter core code
3 *
4 * Copyright(C) 2008 Thomas Gleixner <tglx@linutronix.de>
5 * Copyright(C) 2008 Red Hat, Inc., Ingo Molnar
6 *
7 * For licencing details see kernel-base/COPYING
8 */
9
10#include <linux/fs.h>
11#include <linux/cpu.h>
12#include <linux/smp.h>
Ingo Molnar04289bb2008-12-11 08:38:42 +010013#include <linux/file.h>
Thomas Gleixner0793a612008-12-04 20:12:29 +010014#include <linux/poll.h>
15#include <linux/sysfs.h>
16#include <linux/ptrace.h>
17#include <linux/percpu.h>
18#include <linux/uaccess.h>
19#include <linux/syscalls.h>
20#include <linux/anon_inodes.h>
Ingo Molnaraa9c4c02008-12-17 14:10:57 +010021#include <linux/kernel_stat.h>
Thomas Gleixner0793a612008-12-04 20:12:29 +010022#include <linux/perf_counter.h>
23
24/*
25 * Each CPU has a list of per CPU counters:
26 */
27DEFINE_PER_CPU(struct perf_cpu_context, perf_cpu_context);
28
Ingo Molnar088e2852008-12-14 20:21:00 +010029int perf_max_counters __read_mostly = 1;
Thomas Gleixner0793a612008-12-04 20:12:29 +010030static int perf_reserved_percpu __read_mostly;
31static int perf_overcommit __read_mostly = 1;
32
33/*
34 * Mutex for (sysadmin-configurable) counter reservations:
35 */
36static DEFINE_MUTEX(perf_resource_mutex);
37
38/*
39 * Architecture provided APIs - weak aliases:
40 */
Ingo Molnar5c92d122008-12-11 13:21:10 +010041extern __weak const struct hw_perf_counter_ops *
Ingo Molnar621a01e2008-12-11 12:46:46 +010042hw_perf_counter_init(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +010043{
Ingo Molnar621a01e2008-12-11 12:46:46 +010044 return ERR_PTR(-EINVAL);
Thomas Gleixner0793a612008-12-04 20:12:29 +010045}
46
Ingo Molnar01b28382008-12-11 13:45:51 +010047u64 __weak hw_perf_save_disable(void) { return 0; }
Ingo Molnaree060942008-12-13 09:00:03 +010048void __weak hw_perf_restore(u64 ctrl) { }
Ingo Molnar5c92d122008-12-11 13:21:10 +010049void __weak hw_perf_counter_setup(void) { }
Thomas Gleixner0793a612008-12-04 20:12:29 +010050
Ingo Molnar04289bb2008-12-11 08:38:42 +010051static void
52list_add_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
53{
54 struct perf_counter *group_leader = counter->group_leader;
55
56 /*
57 * Depending on whether it is a standalone or sibling counter,
58 * add it straight to the context's counter list, or to the group
59 * leader's sibling list:
60 */
61 if (counter->group_leader == counter)
62 list_add_tail(&counter->list_entry, &ctx->counter_list);
63 else
64 list_add_tail(&counter->list_entry, &group_leader->sibling_list);
65}
66
67static void
68list_del_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
69{
70 struct perf_counter *sibling, *tmp;
71
72 list_del_init(&counter->list_entry);
73
Ingo Molnar04289bb2008-12-11 08:38:42 +010074 /*
75 * If this was a group counter with sibling counters then
76 * upgrade the siblings to singleton counters by adding them
77 * to the context list directly:
78 */
79 list_for_each_entry_safe(sibling, tmp,
80 &counter->sibling_list, list_entry) {
81
82 list_del_init(&sibling->list_entry);
83 list_add_tail(&sibling->list_entry, &ctx->counter_list);
Ingo Molnar04289bb2008-12-11 08:38:42 +010084 sibling->group_leader = sibling;
85 }
86}
87
Thomas Gleixner0793a612008-12-04 20:12:29 +010088/*
89 * Cross CPU call to remove a performance counter
90 *
91 * We disable the counter on the hardware level first. After that we
92 * remove it from the context list.
93 */
Ingo Molnar04289bb2008-12-11 08:38:42 +010094static void __perf_counter_remove_from_context(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +010095{
96 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
97 struct perf_counter *counter = info;
98 struct perf_counter_context *ctx = counter->ctx;
Ingo Molnar9b51f662008-12-12 13:49:45 +010099 unsigned long flags;
Ingo Molnar5c92d122008-12-11 13:21:10 +0100100 u64 perf_flags;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100101
102 /*
103 * If this is a task context, we need to check whether it is
104 * the current task context of this cpu. If not it has been
105 * scheduled out before the smp call arrived.
106 */
107 if (ctx->task && cpuctx->task_ctx != ctx)
108 return;
109
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100110 curr_rq_lock_irq_save(&flags);
111 spin_lock(&ctx->lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100112
Ingo Molnar6a930702008-12-11 15:17:03 +0100113 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
Ingo Molnar76715812008-12-17 14:20:28 +0100114 counter->hw_ops->disable(counter);
Ingo Molnar6a930702008-12-11 15:17:03 +0100115 counter->state = PERF_COUNTER_STATE_INACTIVE;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100116 ctx->nr_active--;
117 cpuctx->active_oncpu--;
118 counter->task = NULL;
119 }
120 ctx->nr_counters--;
121
122 /*
123 * Protect the list operation against NMI by disabling the
124 * counters on a global level. NOP for non NMI based counters.
125 */
Ingo Molnar01b28382008-12-11 13:45:51 +0100126 perf_flags = hw_perf_save_disable();
Ingo Molnar04289bb2008-12-11 08:38:42 +0100127 list_del_counter(counter, ctx);
Ingo Molnar01b28382008-12-11 13:45:51 +0100128 hw_perf_restore(perf_flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100129
130 if (!ctx->task) {
131 /*
132 * Allow more per task counters with respect to the
133 * reservation:
134 */
135 cpuctx->max_pertask =
136 min(perf_max_counters - ctx->nr_counters,
137 perf_max_counters - perf_reserved_percpu);
138 }
139
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100140 spin_unlock(&ctx->lock);
141 curr_rq_unlock_irq_restore(&flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100142}
143
144
145/*
146 * Remove the counter from a task's (or a CPU's) list of counters.
147 *
148 * Must be called with counter->mutex held.
149 *
150 * CPU counters are removed with a smp call. For task counters we only
151 * call when the task is on a CPU.
152 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100153static void perf_counter_remove_from_context(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100154{
155 struct perf_counter_context *ctx = counter->ctx;
156 struct task_struct *task = ctx->task;
157
158 if (!task) {
159 /*
160 * Per cpu counters are removed via an smp call and
161 * the removal is always sucessful.
162 */
163 smp_call_function_single(counter->cpu,
Ingo Molnar04289bb2008-12-11 08:38:42 +0100164 __perf_counter_remove_from_context,
Thomas Gleixner0793a612008-12-04 20:12:29 +0100165 counter, 1);
166 return;
167 }
168
169retry:
Ingo Molnar04289bb2008-12-11 08:38:42 +0100170 task_oncpu_function_call(task, __perf_counter_remove_from_context,
Thomas Gleixner0793a612008-12-04 20:12:29 +0100171 counter);
172
173 spin_lock_irq(&ctx->lock);
174 /*
175 * If the context is active we need to retry the smp call.
176 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100177 if (ctx->nr_active && !list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100178 spin_unlock_irq(&ctx->lock);
179 goto retry;
180 }
181
182 /*
183 * The lock prevents that this context is scheduled in so we
Ingo Molnar04289bb2008-12-11 08:38:42 +0100184 * can remove the counter safely, if the call above did not
Thomas Gleixner0793a612008-12-04 20:12:29 +0100185 * succeed.
186 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100187 if (!list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100188 ctx->nr_counters--;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100189 list_del_counter(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100190 counter->task = NULL;
191 }
192 spin_unlock_irq(&ctx->lock);
193}
194
195/*
196 * Cross CPU call to install and enable a preformance counter
197 */
198static void __perf_install_in_context(void *info)
199{
200 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
201 struct perf_counter *counter = info;
202 struct perf_counter_context *ctx = counter->ctx;
203 int cpu = smp_processor_id();
Ingo Molnar9b51f662008-12-12 13:49:45 +0100204 unsigned long flags;
Ingo Molnar5c92d122008-12-11 13:21:10 +0100205 u64 perf_flags;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100206
207 /*
208 * If this is a task context, we need to check whether it is
209 * the current task context of this cpu. If not it has been
210 * scheduled out before the smp call arrived.
211 */
212 if (ctx->task && cpuctx->task_ctx != ctx)
213 return;
214
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100215 curr_rq_lock_irq_save(&flags);
216 spin_lock(&ctx->lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100217
218 /*
219 * Protect the list operation against NMI by disabling the
220 * counters on a global level. NOP for non NMI based counters.
221 */
Ingo Molnar01b28382008-12-11 13:45:51 +0100222 perf_flags = hw_perf_save_disable();
Ingo Molnar04289bb2008-12-11 08:38:42 +0100223 list_add_counter(counter, ctx);
Ingo Molnar01b28382008-12-11 13:45:51 +0100224 hw_perf_restore(perf_flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100225
226 ctx->nr_counters++;
227
228 if (cpuctx->active_oncpu < perf_max_counters) {
Ingo Molnar6a930702008-12-11 15:17:03 +0100229 counter->state = PERF_COUNTER_STATE_ACTIVE;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100230 counter->oncpu = cpu;
231 ctx->nr_active++;
232 cpuctx->active_oncpu++;
Ingo Molnar76715812008-12-17 14:20:28 +0100233 counter->hw_ops->enable(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100234 }
235
236 if (!ctx->task && cpuctx->max_pertask)
237 cpuctx->max_pertask--;
238
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100239 spin_unlock(&ctx->lock);
240 curr_rq_unlock_irq_restore(&flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100241}
242
243/*
244 * Attach a performance counter to a context
245 *
246 * First we add the counter to the list with the hardware enable bit
247 * in counter->hw_config cleared.
248 *
249 * If the counter is attached to a task which is on a CPU we use a smp
250 * call to enable it in the task context. The task might have been
251 * scheduled away, but we check this in the smp call again.
252 */
253static void
254perf_install_in_context(struct perf_counter_context *ctx,
255 struct perf_counter *counter,
256 int cpu)
257{
258 struct task_struct *task = ctx->task;
259
260 counter->ctx = ctx;
261 if (!task) {
262 /*
263 * Per cpu counters are installed via an smp call and
264 * the install is always sucessful.
265 */
266 smp_call_function_single(cpu, __perf_install_in_context,
267 counter, 1);
268 return;
269 }
270
271 counter->task = task;
272retry:
273 task_oncpu_function_call(task, __perf_install_in_context,
274 counter);
275
276 spin_lock_irq(&ctx->lock);
277 /*
Thomas Gleixner0793a612008-12-04 20:12:29 +0100278 * we need to retry the smp call.
279 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100280 if (ctx->nr_active && list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100281 spin_unlock_irq(&ctx->lock);
282 goto retry;
283 }
284
285 /*
286 * The lock prevents that this context is scheduled in so we
287 * can add the counter safely, if it the call above did not
288 * succeed.
289 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100290 if (list_empty(&counter->list_entry)) {
291 list_add_counter(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100292 ctx->nr_counters++;
293 }
294 spin_unlock_irq(&ctx->lock);
295}
296
Ingo Molnar04289bb2008-12-11 08:38:42 +0100297static void
298counter_sched_out(struct perf_counter *counter,
299 struct perf_cpu_context *cpuctx,
300 struct perf_counter_context *ctx)
301{
Ingo Molnar6a930702008-12-11 15:17:03 +0100302 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
Ingo Molnar04289bb2008-12-11 08:38:42 +0100303 return;
304
Ingo Molnar76715812008-12-17 14:20:28 +0100305 counter->hw_ops->disable(counter);
Ingo Molnar6a930702008-12-11 15:17:03 +0100306 counter->state = PERF_COUNTER_STATE_INACTIVE;
307 counter->oncpu = -1;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100308
309 cpuctx->active_oncpu--;
310 ctx->nr_active--;
311}
312
313static void
314group_sched_out(struct perf_counter *group_counter,
315 struct perf_cpu_context *cpuctx,
316 struct perf_counter_context *ctx)
317{
318 struct perf_counter *counter;
319
320 counter_sched_out(group_counter, cpuctx, ctx);
321
322 /*
323 * Schedule out siblings (if any):
324 */
325 list_for_each_entry(counter, &group_counter->sibling_list, list_entry)
326 counter_sched_out(counter, cpuctx, ctx);
327}
328
Thomas Gleixner0793a612008-12-04 20:12:29 +0100329/*
330 * Called from scheduler to remove the counters of the current task,
331 * with interrupts disabled.
332 *
333 * We stop each counter and update the counter value in counter->count.
334 *
Ingo Molnar76715812008-12-17 14:20:28 +0100335 * This does not protect us against NMI, but disable()
Thomas Gleixner0793a612008-12-04 20:12:29 +0100336 * sets the disabled bit in the control field of counter _before_
337 * accessing the counter control register. If a NMI hits, then it will
338 * not restart the counter.
339 */
340void perf_counter_task_sched_out(struct task_struct *task, int cpu)
341{
342 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
343 struct perf_counter_context *ctx = &task->perf_counter_ctx;
344 struct perf_counter *counter;
345
346 if (likely(!cpuctx->task_ctx))
347 return;
348
349 spin_lock(&ctx->lock);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100350 if (ctx->nr_active) {
351 list_for_each_entry(counter, &ctx->counter_list, list_entry)
352 group_sched_out(counter, cpuctx, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100353 }
354 spin_unlock(&ctx->lock);
355 cpuctx->task_ctx = NULL;
356}
357
Ingo Molnar04289bb2008-12-11 08:38:42 +0100358static void
359counter_sched_in(struct perf_counter *counter,
360 struct perf_cpu_context *cpuctx,
361 struct perf_counter_context *ctx,
362 int cpu)
363{
Ingo Molnar6a930702008-12-11 15:17:03 +0100364 if (counter->state == PERF_COUNTER_STATE_OFF)
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100365 return;
366
Ingo Molnar76715812008-12-17 14:20:28 +0100367 counter->hw_ops->enable(counter);
Ingo Molnar6a930702008-12-11 15:17:03 +0100368 counter->state = PERF_COUNTER_STATE_ACTIVE;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100369 counter->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */
370
371 cpuctx->active_oncpu++;
372 ctx->nr_active++;
373}
374
Ingo Molnar79958882008-12-17 08:54:56 +0100375static int
Ingo Molnar04289bb2008-12-11 08:38:42 +0100376group_sched_in(struct perf_counter *group_counter,
377 struct perf_cpu_context *cpuctx,
378 struct perf_counter_context *ctx,
379 int cpu)
380{
381 struct perf_counter *counter;
Ingo Molnar79958882008-12-17 08:54:56 +0100382 int was_group = 0;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100383
384 counter_sched_in(group_counter, cpuctx, ctx, cpu);
385
386 /*
387 * Schedule in siblings as one group (if any):
388 */
Ingo Molnar79958882008-12-17 08:54:56 +0100389 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
Ingo Molnar04289bb2008-12-11 08:38:42 +0100390 counter_sched_in(counter, cpuctx, ctx, cpu);
Ingo Molnar79958882008-12-17 08:54:56 +0100391 was_group = 1;
392 }
393
394 return was_group;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100395}
396
Thomas Gleixner0793a612008-12-04 20:12:29 +0100397/*
398 * Called from scheduler to add the counters of the current task
399 * with interrupts disabled.
400 *
401 * We restore the counter value and then enable it.
402 *
Ingo Molnar76715812008-12-17 14:20:28 +0100403 * This does not protect us against NMI, but enable()
Thomas Gleixner0793a612008-12-04 20:12:29 +0100404 * sets the enabled bit in the control field of counter _before_
405 * accessing the counter control register. If a NMI hits, then it will
406 * keep the counter running.
407 */
408void perf_counter_task_sched_in(struct task_struct *task, int cpu)
409{
410 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
411 struct perf_counter_context *ctx = &task->perf_counter_ctx;
412 struct perf_counter *counter;
413
414 if (likely(!ctx->nr_counters))
415 return;
416
417 spin_lock(&ctx->lock);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100418 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100419 if (ctx->nr_active == cpuctx->max_pertask)
420 break;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100421
422 /*
423 * Listen to the 'cpu' scheduling filter constraint
424 * of counters:
425 */
Thomas Gleixner0793a612008-12-04 20:12:29 +0100426 if (counter->cpu != -1 && counter->cpu != cpu)
427 continue;
428
Ingo Molnar79958882008-12-17 08:54:56 +0100429 /*
430 * If we scheduled in a group atomically and
431 * exclusively, break out:
432 */
433 if (group_sched_in(counter, cpuctx, ctx, cpu))
434 break;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100435 }
436 spin_unlock(&ctx->lock);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100437
Thomas Gleixner0793a612008-12-04 20:12:29 +0100438 cpuctx->task_ctx = ctx;
439}
440
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100441int perf_counter_task_disable(void)
442{
443 struct task_struct *curr = current;
444 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
445 struct perf_counter *counter;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100446 unsigned long flags;
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100447 u64 perf_flags;
448 int cpu;
449
450 if (likely(!ctx->nr_counters))
451 return 0;
452
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100453 curr_rq_lock_irq_save(&flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100454 cpu = smp_processor_id();
455
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100456 /* force the update of the task clock: */
457 __task_delta_exec(curr, 1);
458
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100459 perf_counter_task_sched_out(curr, cpu);
460
461 spin_lock(&ctx->lock);
462
463 /*
464 * Disable all the counters:
465 */
466 perf_flags = hw_perf_save_disable();
467
Ingo Molnar9b51f662008-12-12 13:49:45 +0100468 list_for_each_entry(counter, &ctx->counter_list, list_entry)
Ingo Molnar6a930702008-12-11 15:17:03 +0100469 counter->state = PERF_COUNTER_STATE_OFF;
Ingo Molnar9b51f662008-12-12 13:49:45 +0100470
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100471 hw_perf_restore(perf_flags);
472
473 spin_unlock(&ctx->lock);
474
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100475 curr_rq_unlock_irq_restore(&flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100476
477 return 0;
478}
479
480int perf_counter_task_enable(void)
481{
482 struct task_struct *curr = current;
483 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
484 struct perf_counter *counter;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100485 unsigned long flags;
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100486 u64 perf_flags;
487 int cpu;
488
489 if (likely(!ctx->nr_counters))
490 return 0;
491
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100492 curr_rq_lock_irq_save(&flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100493 cpu = smp_processor_id();
494
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100495 /* force the update of the task clock: */
496 __task_delta_exec(curr, 1);
497
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100498 spin_lock(&ctx->lock);
499
500 /*
501 * Disable all the counters:
502 */
503 perf_flags = hw_perf_save_disable();
504
505 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Ingo Molnar6a930702008-12-11 15:17:03 +0100506 if (counter->state != PERF_COUNTER_STATE_OFF)
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100507 continue;
Ingo Molnar6a930702008-12-11 15:17:03 +0100508 counter->state = PERF_COUNTER_STATE_INACTIVE;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100509 counter->hw_event.disabled = 0;
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100510 }
511 hw_perf_restore(perf_flags);
512
513 spin_unlock(&ctx->lock);
514
515 perf_counter_task_sched_in(curr, cpu);
516
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100517 curr_rq_unlock_irq_restore(&flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100518
519 return 0;
520}
521
Thomas Gleixner0793a612008-12-04 20:12:29 +0100522void perf_counter_task_tick(struct task_struct *curr, int cpu)
523{
524 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
525 struct perf_counter *counter;
Ingo Molnar5c92d122008-12-11 13:21:10 +0100526 u64 perf_flags;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100527
528 if (likely(!ctx->nr_counters))
529 return;
530
531 perf_counter_task_sched_out(curr, cpu);
532
533 spin_lock(&ctx->lock);
534
535 /*
Ingo Molnar04289bb2008-12-11 08:38:42 +0100536 * Rotate the first entry last (works just fine for group counters too):
Thomas Gleixner0793a612008-12-04 20:12:29 +0100537 */
Ingo Molnar01b28382008-12-11 13:45:51 +0100538 perf_flags = hw_perf_save_disable();
Ingo Molnar04289bb2008-12-11 08:38:42 +0100539 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
540 list_del(&counter->list_entry);
541 list_add_tail(&counter->list_entry, &ctx->counter_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100542 break;
543 }
Ingo Molnar01b28382008-12-11 13:45:51 +0100544 hw_perf_restore(perf_flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100545
546 spin_unlock(&ctx->lock);
547
548 perf_counter_task_sched_in(curr, cpu);
549}
550
551/*
Thomas Gleixner0793a612008-12-04 20:12:29 +0100552 * Cross CPU call to read the hardware counter
553 */
Ingo Molnar76715812008-12-17 14:20:28 +0100554static void __read(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100555{
Ingo Molnar621a01e2008-12-11 12:46:46 +0100556 struct perf_counter *counter = info;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100557 unsigned long flags;
Ingo Molnar621a01e2008-12-11 12:46:46 +0100558
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100559 curr_rq_lock_irq_save(&flags);
Ingo Molnar76715812008-12-17 14:20:28 +0100560 counter->hw_ops->read(counter);
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100561 curr_rq_unlock_irq_restore(&flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100562}
563
Ingo Molnar04289bb2008-12-11 08:38:42 +0100564static u64 perf_counter_read(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100565{
566 /*
567 * If counter is enabled and currently active on a CPU, update the
568 * value in the counter structure:
569 */
Ingo Molnar6a930702008-12-11 15:17:03 +0100570 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100571 smp_call_function_single(counter->oncpu,
Ingo Molnar76715812008-12-17 14:20:28 +0100572 __read, counter, 1);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100573 }
574
Ingo Molnaree060942008-12-13 09:00:03 +0100575 return atomic64_read(&counter->count);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100576}
577
578/*
579 * Cross CPU call to switch performance data pointers
580 */
581static void __perf_switch_irq_data(void *info)
582{
583 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
584 struct perf_counter *counter = info;
585 struct perf_counter_context *ctx = counter->ctx;
586 struct perf_data *oldirqdata = counter->irqdata;
587
588 /*
589 * If this is a task context, we need to check whether it is
590 * the current task context of this cpu. If not it has been
591 * scheduled out before the smp call arrived.
592 */
593 if (ctx->task) {
594 if (cpuctx->task_ctx != ctx)
595 return;
596 spin_lock(&ctx->lock);
597 }
598
599 /* Change the pointer NMI safe */
600 atomic_long_set((atomic_long_t *)&counter->irqdata,
601 (unsigned long) counter->usrdata);
602 counter->usrdata = oldirqdata;
603
604 if (ctx->task)
605 spin_unlock(&ctx->lock);
606}
607
608static struct perf_data *perf_switch_irq_data(struct perf_counter *counter)
609{
610 struct perf_counter_context *ctx = counter->ctx;
611 struct perf_data *oldirqdata = counter->irqdata;
612 struct task_struct *task = ctx->task;
613
614 if (!task) {
615 smp_call_function_single(counter->cpu,
616 __perf_switch_irq_data,
617 counter, 1);
618 return counter->usrdata;
619 }
620
621retry:
622 spin_lock_irq(&ctx->lock);
Ingo Molnar6a930702008-12-11 15:17:03 +0100623 if (counter->state != PERF_COUNTER_STATE_ACTIVE) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100624 counter->irqdata = counter->usrdata;
625 counter->usrdata = oldirqdata;
626 spin_unlock_irq(&ctx->lock);
627 return oldirqdata;
628 }
629 spin_unlock_irq(&ctx->lock);
630 task_oncpu_function_call(task, __perf_switch_irq_data, counter);
631 /* Might have failed, because task was scheduled out */
632 if (counter->irqdata == oldirqdata)
633 goto retry;
634
635 return counter->usrdata;
636}
637
638static void put_context(struct perf_counter_context *ctx)
639{
640 if (ctx->task)
641 put_task_struct(ctx->task);
642}
643
644static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
645{
646 struct perf_cpu_context *cpuctx;
647 struct perf_counter_context *ctx;
648 struct task_struct *task;
649
650 /*
651 * If cpu is not a wildcard then this is a percpu counter:
652 */
653 if (cpu != -1) {
654 /* Must be root to operate on a CPU counter: */
655 if (!capable(CAP_SYS_ADMIN))
656 return ERR_PTR(-EACCES);
657
658 if (cpu < 0 || cpu > num_possible_cpus())
659 return ERR_PTR(-EINVAL);
660
661 /*
662 * We could be clever and allow to attach a counter to an
663 * offline CPU and activate it when the CPU comes up, but
664 * that's for later.
665 */
666 if (!cpu_isset(cpu, cpu_online_map))
667 return ERR_PTR(-ENODEV);
668
669 cpuctx = &per_cpu(perf_cpu_context, cpu);
670 ctx = &cpuctx->ctx;
671
Thomas Gleixner0793a612008-12-04 20:12:29 +0100672 return ctx;
673 }
674
675 rcu_read_lock();
676 if (!pid)
677 task = current;
678 else
679 task = find_task_by_vpid(pid);
680 if (task)
681 get_task_struct(task);
682 rcu_read_unlock();
683
684 if (!task)
685 return ERR_PTR(-ESRCH);
686
687 ctx = &task->perf_counter_ctx;
688 ctx->task = task;
689
690 /* Reuse ptrace permission checks for now. */
691 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
692 put_context(ctx);
693 return ERR_PTR(-EACCES);
694 }
695
696 return ctx;
697}
698
699/*
700 * Called when the last reference to the file is gone.
701 */
702static int perf_release(struct inode *inode, struct file *file)
703{
704 struct perf_counter *counter = file->private_data;
705 struct perf_counter_context *ctx = counter->ctx;
706
707 file->private_data = NULL;
708
709 mutex_lock(&counter->mutex);
710
Ingo Molnar04289bb2008-12-11 08:38:42 +0100711 perf_counter_remove_from_context(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100712 put_context(ctx);
713
714 mutex_unlock(&counter->mutex);
715
716 kfree(counter);
717
718 return 0;
719}
720
721/*
722 * Read the performance counter - simple non blocking version for now
723 */
724static ssize_t
725perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
726{
727 u64 cntval;
728
729 if (count != sizeof(cntval))
730 return -EINVAL;
731
732 mutex_lock(&counter->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100733 cntval = perf_counter_read(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100734 mutex_unlock(&counter->mutex);
735
736 return put_user(cntval, (u64 __user *) buf) ? -EFAULT : sizeof(cntval);
737}
738
739static ssize_t
740perf_copy_usrdata(struct perf_data *usrdata, char __user *buf, size_t count)
741{
742 if (!usrdata->len)
743 return 0;
744
745 count = min(count, (size_t)usrdata->len);
746 if (copy_to_user(buf, usrdata->data + usrdata->rd_idx, count))
747 return -EFAULT;
748
749 /* Adjust the counters */
750 usrdata->len -= count;
751 if (!usrdata->len)
752 usrdata->rd_idx = 0;
753 else
754 usrdata->rd_idx += count;
755
756 return count;
757}
758
759static ssize_t
760perf_read_irq_data(struct perf_counter *counter,
761 char __user *buf,
762 size_t count,
763 int nonblocking)
764{
765 struct perf_data *irqdata, *usrdata;
766 DECLARE_WAITQUEUE(wait, current);
767 ssize_t res;
768
769 irqdata = counter->irqdata;
770 usrdata = counter->usrdata;
771
772 if (usrdata->len + irqdata->len >= count)
773 goto read_pending;
774
775 if (nonblocking)
776 return -EAGAIN;
777
778 spin_lock_irq(&counter->waitq.lock);
779 __add_wait_queue(&counter->waitq, &wait);
780 for (;;) {
781 set_current_state(TASK_INTERRUPTIBLE);
782 if (usrdata->len + irqdata->len >= count)
783 break;
784
785 if (signal_pending(current))
786 break;
787
788 spin_unlock_irq(&counter->waitq.lock);
789 schedule();
790 spin_lock_irq(&counter->waitq.lock);
791 }
792 __remove_wait_queue(&counter->waitq, &wait);
793 __set_current_state(TASK_RUNNING);
794 spin_unlock_irq(&counter->waitq.lock);
795
796 if (usrdata->len + irqdata->len < count)
797 return -ERESTARTSYS;
798read_pending:
799 mutex_lock(&counter->mutex);
800
801 /* Drain pending data first: */
802 res = perf_copy_usrdata(usrdata, buf, count);
803 if (res < 0 || res == count)
804 goto out;
805
806 /* Switch irq buffer: */
807 usrdata = perf_switch_irq_data(counter);
808 if (perf_copy_usrdata(usrdata, buf + res, count - res) < 0) {
809 if (!res)
810 res = -EFAULT;
811 } else {
812 res = count;
813 }
814out:
815 mutex_unlock(&counter->mutex);
816
817 return res;
818}
819
820static ssize_t
821perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
822{
823 struct perf_counter *counter = file->private_data;
824
Ingo Molnar9f66a382008-12-10 12:33:23 +0100825 switch (counter->hw_event.record_type) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100826 case PERF_RECORD_SIMPLE:
827 return perf_read_hw(counter, buf, count);
828
829 case PERF_RECORD_IRQ:
830 case PERF_RECORD_GROUP:
831 return perf_read_irq_data(counter, buf, count,
832 file->f_flags & O_NONBLOCK);
833 }
834 return -EINVAL;
835}
836
837static unsigned int perf_poll(struct file *file, poll_table *wait)
838{
839 struct perf_counter *counter = file->private_data;
840 unsigned int events = 0;
841 unsigned long flags;
842
843 poll_wait(file, &counter->waitq, wait);
844
845 spin_lock_irqsave(&counter->waitq.lock, flags);
846 if (counter->usrdata->len || counter->irqdata->len)
847 events |= POLLIN;
848 spin_unlock_irqrestore(&counter->waitq.lock, flags);
849
850 return events;
851}
852
853static const struct file_operations perf_fops = {
854 .release = perf_release,
855 .read = perf_read,
856 .poll = perf_poll,
857};
858
Ingo Molnar5c92d122008-12-11 13:21:10 +0100859static void cpu_clock_perf_counter_enable(struct perf_counter *counter)
860{
861}
862
863static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
864{
865}
866
867static void cpu_clock_perf_counter_read(struct perf_counter *counter)
868{
869 int cpu = raw_smp_processor_id();
870
Ingo Molnaree060942008-12-13 09:00:03 +0100871 atomic64_set(&counter->count, cpu_clock(cpu));
Ingo Molnar5c92d122008-12-11 13:21:10 +0100872}
873
874static const struct hw_perf_counter_ops perf_ops_cpu_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +0100875 .enable = cpu_clock_perf_counter_enable,
876 .disable = cpu_clock_perf_counter_disable,
877 .read = cpu_clock_perf_counter_read,
Ingo Molnar5c92d122008-12-11 13:21:10 +0100878};
879
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100880/*
881 * Called from within the scheduler:
882 */
883static u64 task_clock_perf_counter_val(struct perf_counter *counter, int update)
Ingo Molnarbae43c92008-12-11 14:03:20 +0100884{
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100885 struct task_struct *curr = counter->task;
886 u64 delta;
887
888 WARN_ON_ONCE(counter->task != current);
889
890 delta = __task_delta_exec(curr, update);
891
892 return curr->se.sum_exec_runtime + delta;
893}
894
895static void task_clock_perf_counter_update(struct perf_counter *counter, u64 now)
896{
897 u64 prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +0100898 s64 delta;
Ingo Molnarbae43c92008-12-11 14:03:20 +0100899
Ingo Molnar8cb391e2008-12-14 12:22:31 +0100900 prev = atomic64_read(&counter->hw.prev_count);
Ingo Molnar8cb391e2008-12-14 12:22:31 +0100901
902 atomic64_set(&counter->hw.prev_count, now);
903
904 delta = now - prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +0100905
906 atomic64_add(delta, &counter->count);
Ingo Molnarbae43c92008-12-11 14:03:20 +0100907}
908
909static void task_clock_perf_counter_read(struct perf_counter *counter)
910{
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100911 u64 now = task_clock_perf_counter_val(counter, 1);
912
913 task_clock_perf_counter_update(counter, now);
Ingo Molnar8cb391e2008-12-14 12:22:31 +0100914}
915
916static void task_clock_perf_counter_enable(struct perf_counter *counter)
917{
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100918 u64 now = task_clock_perf_counter_val(counter, 0);
919
920 atomic64_set(&counter->hw.prev_count, now);
Ingo Molnar8cb391e2008-12-14 12:22:31 +0100921}
922
923static void task_clock_perf_counter_disable(struct perf_counter *counter)
924{
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100925 u64 now = task_clock_perf_counter_val(counter, 0);
926
927 task_clock_perf_counter_update(counter, now);
Ingo Molnarbae43c92008-12-11 14:03:20 +0100928}
929
930static const struct hw_perf_counter_ops perf_ops_task_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +0100931 .enable = task_clock_perf_counter_enable,
932 .disable = task_clock_perf_counter_disable,
933 .read = task_clock_perf_counter_read,
Ingo Molnarbae43c92008-12-11 14:03:20 +0100934};
935
Ingo Molnare06c61a2008-12-14 14:44:31 +0100936static u64 get_page_faults(void)
937{
938 struct task_struct *curr = current;
939
940 return curr->maj_flt + curr->min_flt;
941}
942
943static void page_faults_perf_counter_update(struct perf_counter *counter)
944{
945 u64 prev, now;
946 s64 delta;
947
948 prev = atomic64_read(&counter->hw.prev_count);
949 now = get_page_faults();
950
951 atomic64_set(&counter->hw.prev_count, now);
952
953 delta = now - prev;
Ingo Molnare06c61a2008-12-14 14:44:31 +0100954
955 atomic64_add(delta, &counter->count);
956}
957
958static void page_faults_perf_counter_read(struct perf_counter *counter)
959{
960 page_faults_perf_counter_update(counter);
961}
962
963static void page_faults_perf_counter_enable(struct perf_counter *counter)
964{
965 /*
966 * page-faults is a per-task value already,
967 * so we dont have to clear it on switch-in.
968 */
969}
970
971static void page_faults_perf_counter_disable(struct perf_counter *counter)
972{
973 page_faults_perf_counter_update(counter);
974}
975
976static const struct hw_perf_counter_ops perf_ops_page_faults = {
Ingo Molnar76715812008-12-17 14:20:28 +0100977 .enable = page_faults_perf_counter_enable,
978 .disable = page_faults_perf_counter_disable,
979 .read = page_faults_perf_counter_read,
Ingo Molnare06c61a2008-12-14 14:44:31 +0100980};
981
Ingo Molnar5d6a27d2008-12-14 12:28:33 +0100982static u64 get_context_switches(void)
983{
984 struct task_struct *curr = current;
985
986 return curr->nvcsw + curr->nivcsw;
987}
988
989static void context_switches_perf_counter_update(struct perf_counter *counter)
990{
991 u64 prev, now;
992 s64 delta;
993
994 prev = atomic64_read(&counter->hw.prev_count);
995 now = get_context_switches();
996
997 atomic64_set(&counter->hw.prev_count, now);
998
999 delta = now - prev;
Ingo Molnar5d6a27d2008-12-14 12:28:33 +01001000
1001 atomic64_add(delta, &counter->count);
1002}
1003
1004static void context_switches_perf_counter_read(struct perf_counter *counter)
1005{
1006 context_switches_perf_counter_update(counter);
1007}
1008
1009static void context_switches_perf_counter_enable(struct perf_counter *counter)
1010{
1011 /*
1012 * ->nvcsw + curr->nivcsw is a per-task value already,
1013 * so we dont have to clear it on switch-in.
1014 */
1015}
1016
1017static void context_switches_perf_counter_disable(struct perf_counter *counter)
1018{
1019 context_switches_perf_counter_update(counter);
1020}
1021
1022static const struct hw_perf_counter_ops perf_ops_context_switches = {
Ingo Molnar76715812008-12-17 14:20:28 +01001023 .enable = context_switches_perf_counter_enable,
1024 .disable = context_switches_perf_counter_disable,
1025 .read = context_switches_perf_counter_read,
Ingo Molnar5d6a27d2008-12-14 12:28:33 +01001026};
1027
Ingo Molnar6c594c22008-12-14 12:34:15 +01001028static inline u64 get_cpu_migrations(void)
1029{
1030 return current->se.nr_migrations;
1031}
1032
1033static void cpu_migrations_perf_counter_update(struct perf_counter *counter)
1034{
1035 u64 prev, now;
1036 s64 delta;
1037
1038 prev = atomic64_read(&counter->hw.prev_count);
1039 now = get_cpu_migrations();
1040
1041 atomic64_set(&counter->hw.prev_count, now);
1042
1043 delta = now - prev;
Ingo Molnar6c594c22008-12-14 12:34:15 +01001044
1045 atomic64_add(delta, &counter->count);
1046}
1047
1048static void cpu_migrations_perf_counter_read(struct perf_counter *counter)
1049{
1050 cpu_migrations_perf_counter_update(counter);
1051}
1052
1053static void cpu_migrations_perf_counter_enable(struct perf_counter *counter)
1054{
1055 /*
1056 * se.nr_migrations is a per-task value already,
1057 * so we dont have to clear it on switch-in.
1058 */
1059}
1060
1061static void cpu_migrations_perf_counter_disable(struct perf_counter *counter)
1062{
1063 cpu_migrations_perf_counter_update(counter);
1064}
1065
1066static const struct hw_perf_counter_ops perf_ops_cpu_migrations = {
Ingo Molnar76715812008-12-17 14:20:28 +01001067 .enable = cpu_migrations_perf_counter_enable,
1068 .disable = cpu_migrations_perf_counter_disable,
1069 .read = cpu_migrations_perf_counter_read,
Ingo Molnar6c594c22008-12-14 12:34:15 +01001070};
1071
Ingo Molnar5c92d122008-12-11 13:21:10 +01001072static const struct hw_perf_counter_ops *
1073sw_perf_counter_init(struct perf_counter *counter)
1074{
1075 const struct hw_perf_counter_ops *hw_ops = NULL;
1076
1077 switch (counter->hw_event.type) {
1078 case PERF_COUNT_CPU_CLOCK:
1079 hw_ops = &perf_ops_cpu_clock;
1080 break;
Ingo Molnarbae43c92008-12-11 14:03:20 +01001081 case PERF_COUNT_TASK_CLOCK:
1082 hw_ops = &perf_ops_task_clock;
1083 break;
Ingo Molnare06c61a2008-12-14 14:44:31 +01001084 case PERF_COUNT_PAGE_FAULTS:
1085 hw_ops = &perf_ops_page_faults;
1086 break;
Ingo Molnar5d6a27d2008-12-14 12:28:33 +01001087 case PERF_COUNT_CONTEXT_SWITCHES:
1088 hw_ops = &perf_ops_context_switches;
1089 break;
Ingo Molnar6c594c22008-12-14 12:34:15 +01001090 case PERF_COUNT_CPU_MIGRATIONS:
1091 hw_ops = &perf_ops_cpu_migrations;
1092 break;
Ingo Molnar5c92d122008-12-11 13:21:10 +01001093 default:
1094 break;
1095 }
1096 return hw_ops;
1097}
1098
Thomas Gleixner0793a612008-12-04 20:12:29 +01001099/*
1100 * Allocate and initialize a counter structure
1101 */
1102static struct perf_counter *
Ingo Molnar04289bb2008-12-11 08:38:42 +01001103perf_counter_alloc(struct perf_counter_hw_event *hw_event,
1104 int cpu,
Ingo Molnar9b51f662008-12-12 13:49:45 +01001105 struct perf_counter *group_leader,
1106 gfp_t gfpflags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001107{
Ingo Molnar5c92d122008-12-11 13:21:10 +01001108 const struct hw_perf_counter_ops *hw_ops;
Ingo Molnar621a01e2008-12-11 12:46:46 +01001109 struct perf_counter *counter;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001110
Ingo Molnar9b51f662008-12-12 13:49:45 +01001111 counter = kzalloc(sizeof(*counter), gfpflags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001112 if (!counter)
1113 return NULL;
1114
Ingo Molnar04289bb2008-12-11 08:38:42 +01001115 /*
1116 * Single counters are their own group leaders, with an
1117 * empty sibling list:
1118 */
1119 if (!group_leader)
1120 group_leader = counter;
1121
Thomas Gleixner0793a612008-12-04 20:12:29 +01001122 mutex_init(&counter->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01001123 INIT_LIST_HEAD(&counter->list_entry);
1124 INIT_LIST_HEAD(&counter->sibling_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001125 init_waitqueue_head(&counter->waitq);
1126
Ingo Molnar9f66a382008-12-10 12:33:23 +01001127 counter->irqdata = &counter->data[0];
1128 counter->usrdata = &counter->data[1];
1129 counter->cpu = cpu;
1130 counter->hw_event = *hw_event;
1131 counter->wakeup_pending = 0;
Ingo Molnar04289bb2008-12-11 08:38:42 +01001132 counter->group_leader = group_leader;
Ingo Molnar621a01e2008-12-11 12:46:46 +01001133 counter->hw_ops = NULL;
1134
Ingo Molnara86ed502008-12-17 00:43:10 +01001135 if (hw_event->disabled)
1136 counter->state = PERF_COUNTER_STATE_OFF;
1137
Ingo Molnar5c92d122008-12-11 13:21:10 +01001138 hw_ops = NULL;
1139 if (!hw_event->raw && hw_event->type < 0)
1140 hw_ops = sw_perf_counter_init(counter);
Ingo Molnar9b51f662008-12-12 13:49:45 +01001141 if (!hw_ops)
Ingo Molnar5c92d122008-12-11 13:21:10 +01001142 hw_ops = hw_perf_counter_init(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01001143
Ingo Molnar621a01e2008-12-11 12:46:46 +01001144 if (!hw_ops) {
1145 kfree(counter);
1146 return NULL;
1147 }
1148 counter->hw_ops = hw_ops;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001149
1150 return counter;
1151}
1152
1153/**
Ingo Molnar9f66a382008-12-10 12:33:23 +01001154 * sys_perf_task_open - open a performance counter, associate it to a task/cpu
1155 *
1156 * @hw_event_uptr: event type attributes for monitoring/sampling
Thomas Gleixner0793a612008-12-04 20:12:29 +01001157 * @pid: target pid
Ingo Molnar9f66a382008-12-10 12:33:23 +01001158 * @cpu: target cpu
1159 * @group_fd: group leader counter fd
Thomas Gleixner0793a612008-12-04 20:12:29 +01001160 */
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001161asmlinkage int
1162sys_perf_counter_open(struct perf_counter_hw_event *hw_event_uptr __user,
1163 pid_t pid, int cpu, int group_fd)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001164{
Ingo Molnar04289bb2008-12-11 08:38:42 +01001165 struct perf_counter *counter, *group_leader;
Ingo Molnar9f66a382008-12-10 12:33:23 +01001166 struct perf_counter_hw_event hw_event;
Ingo Molnar04289bb2008-12-11 08:38:42 +01001167 struct perf_counter_context *ctx;
Ingo Molnar9b51f662008-12-12 13:49:45 +01001168 struct file *counter_file = NULL;
Ingo Molnar04289bb2008-12-11 08:38:42 +01001169 struct file *group_file = NULL;
1170 int fput_needed = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01001171 int fput_needed2 = 0;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001172 int ret;
1173
Ingo Molnar9f66a382008-12-10 12:33:23 +01001174 if (copy_from_user(&hw_event, hw_event_uptr, sizeof(hw_event)) != 0)
Thomas Gleixnereab656a2008-12-08 19:26:59 +01001175 return -EFAULT;
1176
Ingo Molnar04289bb2008-12-11 08:38:42 +01001177 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01001178 * Get the target context (task or percpu):
1179 */
1180 ctx = find_get_context(pid, cpu);
1181 if (IS_ERR(ctx))
1182 return PTR_ERR(ctx);
1183
1184 /*
1185 * Look up the group leader (we will attach this counter to it):
Ingo Molnar04289bb2008-12-11 08:38:42 +01001186 */
1187 group_leader = NULL;
1188 if (group_fd != -1) {
1189 ret = -EINVAL;
1190 group_file = fget_light(group_fd, &fput_needed);
1191 if (!group_file)
Ingo Molnarccff2862008-12-11 11:26:29 +01001192 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01001193 if (group_file->f_op != &perf_fops)
Ingo Molnarccff2862008-12-11 11:26:29 +01001194 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01001195
1196 group_leader = group_file->private_data;
1197 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01001198 * Do not allow a recursive hierarchy (this new sibling
1199 * becoming part of another group-sibling):
Ingo Molnar04289bb2008-12-11 08:38:42 +01001200 */
Ingo Molnarccff2862008-12-11 11:26:29 +01001201 if (group_leader->group_leader != group_leader)
1202 goto err_put_context;
1203 /*
1204 * Do not allow to attach to a group in a different
1205 * task or CPU context:
1206 */
1207 if (group_leader->ctx != ctx)
1208 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01001209 }
1210
Ingo Molnar5c92d122008-12-11 13:21:10 +01001211 ret = -EINVAL;
Ingo Molnar9b51f662008-12-12 13:49:45 +01001212 counter = perf_counter_alloc(&hw_event, cpu, group_leader, GFP_KERNEL);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001213 if (!counter)
1214 goto err_put_context;
1215
Thomas Gleixner0793a612008-12-04 20:12:29 +01001216 ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
1217 if (ret < 0)
Ingo Molnar9b51f662008-12-12 13:49:45 +01001218 goto err_free_put_context;
1219
1220 counter_file = fget_light(ret, &fput_needed2);
1221 if (!counter_file)
1222 goto err_free_put_context;
1223
1224 counter->filp = counter_file;
1225 perf_install_in_context(ctx, counter, cpu);
1226
1227 fput_light(counter_file, fput_needed2);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001228
Ingo Molnar04289bb2008-12-11 08:38:42 +01001229out_fput:
1230 fput_light(group_file, fput_needed);
1231
Thomas Gleixner0793a612008-12-04 20:12:29 +01001232 return ret;
1233
Ingo Molnar9b51f662008-12-12 13:49:45 +01001234err_free_put_context:
Thomas Gleixner0793a612008-12-04 20:12:29 +01001235 kfree(counter);
1236
1237err_put_context:
1238 put_context(ctx);
1239
Ingo Molnar04289bb2008-12-11 08:38:42 +01001240 goto out_fput;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001241}
1242
Ingo Molnar9b51f662008-12-12 13:49:45 +01001243/*
1244 * Initialize the perf_counter context in a task_struct:
1245 */
1246static void
1247__perf_counter_init_context(struct perf_counter_context *ctx,
1248 struct task_struct *task)
1249{
1250 memset(ctx, 0, sizeof(*ctx));
1251 spin_lock_init(&ctx->lock);
1252 INIT_LIST_HEAD(&ctx->counter_list);
1253 ctx->task = task;
1254}
1255
1256/*
1257 * inherit a counter from parent task to child task:
1258 */
1259static int
1260inherit_counter(struct perf_counter *parent_counter,
1261 struct task_struct *parent,
1262 struct perf_counter_context *parent_ctx,
1263 struct task_struct *child,
1264 struct perf_counter_context *child_ctx)
1265{
1266 struct perf_counter *child_counter;
1267
1268 child_counter = perf_counter_alloc(&parent_counter->hw_event,
1269 parent_counter->cpu, NULL,
1270 GFP_ATOMIC);
1271 if (!child_counter)
1272 return -ENOMEM;
1273
1274 /*
1275 * Link it up in the child's context:
1276 */
1277 child_counter->ctx = child_ctx;
1278 child_counter->task = child;
1279 list_add_counter(child_counter, child_ctx);
1280 child_ctx->nr_counters++;
1281
1282 child_counter->parent = parent_counter;
1283 parent_counter->nr_inherited++;
1284 /*
1285 * inherit into child's child as well:
1286 */
1287 child_counter->hw_event.inherit = 1;
1288
1289 /*
1290 * Get a reference to the parent filp - we will fput it
1291 * when the child counter exits. This is safe to do because
1292 * we are in the parent and we know that the filp still
1293 * exists and has a nonzero count:
1294 */
1295 atomic_long_inc(&parent_counter->filp->f_count);
1296
1297 return 0;
1298}
1299
1300static void
1301__perf_counter_exit_task(struct task_struct *child,
1302 struct perf_counter *child_counter,
1303 struct perf_counter_context *child_ctx)
1304{
1305 struct perf_counter *parent_counter;
1306 u64 parent_val, child_val;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001307 unsigned long flags;
Ingo Molnar9b51f662008-12-12 13:49:45 +01001308 u64 perf_flags;
1309
1310 /*
1311 * Disable and unlink this counter.
1312 *
1313 * Be careful about zapping the list - IRQ/NMI context
1314 * could still be processing it:
1315 */
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001316 curr_rq_lock_irq_save(&flags);
Ingo Molnar9b51f662008-12-12 13:49:45 +01001317 perf_flags = hw_perf_save_disable();
1318
Ingo Molnar0cc0c022008-12-14 23:20:36 +01001319 if (child_counter->state == PERF_COUNTER_STATE_ACTIVE) {
1320 struct perf_cpu_context *cpuctx;
1321
1322 cpuctx = &__get_cpu_var(perf_cpu_context);
1323
Ingo Molnar76715812008-12-17 14:20:28 +01001324 child_counter->hw_ops->disable(child_counter);
Ingo Molnar0cc0c022008-12-14 23:20:36 +01001325 child_counter->state = PERF_COUNTER_STATE_INACTIVE;
1326 child_counter->oncpu = -1;
1327
1328 cpuctx->active_oncpu--;
1329 child_ctx->nr_active--;
1330 }
1331
Ingo Molnar9b51f662008-12-12 13:49:45 +01001332 list_del_init(&child_counter->list_entry);
1333
1334 hw_perf_restore(perf_flags);
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001335 curr_rq_unlock_irq_restore(&flags);
Ingo Molnar9b51f662008-12-12 13:49:45 +01001336
1337 parent_counter = child_counter->parent;
1338 /*
1339 * It can happen that parent exits first, and has counters
1340 * that are still around due to the child reference. These
1341 * counters need to be zapped - but otherwise linger.
1342 */
1343 if (!parent_counter)
1344 return;
1345
1346 parent_val = atomic64_read(&parent_counter->count);
1347 child_val = atomic64_read(&child_counter->count);
1348
1349 /*
1350 * Add back the child's count to the parent's count:
1351 */
1352 atomic64_add(child_val, &parent_counter->count);
1353
1354 fput(parent_counter->filp);
1355
1356 kfree(child_counter);
1357}
1358
1359/*
1360 * When a child task exist, feed back counter values to parent counters.
1361 *
1362 * Note: we are running in child context, but the PID is not hashed
1363 * anymore so new counters will not be added.
1364 */
1365void perf_counter_exit_task(struct task_struct *child)
1366{
1367 struct perf_counter *child_counter, *tmp;
1368 struct perf_counter_context *child_ctx;
1369
1370 child_ctx = &child->perf_counter_ctx;
1371
1372 if (likely(!child_ctx->nr_counters))
1373 return;
1374
1375 list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list,
1376 list_entry)
1377 __perf_counter_exit_task(child, child_counter, child_ctx);
1378}
1379
1380/*
1381 * Initialize the perf_counter context in task_struct
1382 */
1383void perf_counter_init_task(struct task_struct *child)
1384{
1385 struct perf_counter_context *child_ctx, *parent_ctx;
1386 struct perf_counter *counter, *parent_counter;
1387 struct task_struct *parent = current;
1388 unsigned long flags;
1389
1390 child_ctx = &child->perf_counter_ctx;
1391 parent_ctx = &parent->perf_counter_ctx;
1392
1393 __perf_counter_init_context(child_ctx, child);
1394
1395 /*
1396 * This is executed from the parent task context, so inherit
1397 * counters that have been marked for cloning:
1398 */
1399
1400 if (likely(!parent_ctx->nr_counters))
1401 return;
1402
1403 /*
1404 * Lock the parent list. No need to lock the child - not PID
1405 * hashed yet and not running, so nobody can access it.
1406 */
1407 spin_lock_irqsave(&parent_ctx->lock, flags);
1408
1409 /*
1410 * We dont have to disable NMIs - we are only looking at
1411 * the list, not manipulating it:
1412 */
1413 list_for_each_entry(counter, &parent_ctx->counter_list, list_entry) {
1414 if (!counter->hw_event.inherit || counter->group_leader != counter)
1415 continue;
1416
1417 /*
1418 * Instead of creating recursive hierarchies of counters,
1419 * we link inheritd counters back to the original parent,
1420 * which has a filp for sure, which we use as the reference
1421 * count:
1422 */
1423 parent_counter = counter;
1424 if (counter->parent)
1425 parent_counter = counter->parent;
1426
1427 if (inherit_counter(parent_counter, parent,
1428 parent_ctx, child, child_ctx))
1429 break;
1430 }
1431
1432 spin_unlock_irqrestore(&parent_ctx->lock, flags);
1433}
1434
Ingo Molnar04289bb2008-12-11 08:38:42 +01001435static void __cpuinit perf_counter_init_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001436{
Ingo Molnar04289bb2008-12-11 08:38:42 +01001437 struct perf_cpu_context *cpuctx;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001438
Ingo Molnar04289bb2008-12-11 08:38:42 +01001439 cpuctx = &per_cpu(perf_cpu_context, cpu);
1440 __perf_counter_init_context(&cpuctx->ctx, NULL);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001441
1442 mutex_lock(&perf_resource_mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01001443 cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001444 mutex_unlock(&perf_resource_mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01001445
Thomas Gleixner0793a612008-12-04 20:12:29 +01001446 hw_perf_counter_setup();
1447}
1448
1449#ifdef CONFIG_HOTPLUG_CPU
Ingo Molnar04289bb2008-12-11 08:38:42 +01001450static void __perf_counter_exit_cpu(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001451{
1452 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1453 struct perf_counter_context *ctx = &cpuctx->ctx;
1454 struct perf_counter *counter, *tmp;
1455
Ingo Molnar04289bb2008-12-11 08:38:42 +01001456 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry)
1457 __perf_counter_remove_from_context(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001458
1459}
Ingo Molnar04289bb2008-12-11 08:38:42 +01001460static void perf_counter_exit_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001461{
Ingo Molnar04289bb2008-12-11 08:38:42 +01001462 smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001463}
1464#else
Ingo Molnar04289bb2008-12-11 08:38:42 +01001465static inline void perf_counter_exit_cpu(int cpu) { }
Thomas Gleixner0793a612008-12-04 20:12:29 +01001466#endif
1467
1468static int __cpuinit
1469perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
1470{
1471 unsigned int cpu = (long)hcpu;
1472
1473 switch (action) {
1474
1475 case CPU_UP_PREPARE:
1476 case CPU_UP_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01001477 perf_counter_init_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001478 break;
1479
1480 case CPU_DOWN_PREPARE:
1481 case CPU_DOWN_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01001482 perf_counter_exit_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001483 break;
1484
1485 default:
1486 break;
1487 }
1488
1489 return NOTIFY_OK;
1490}
1491
1492static struct notifier_block __cpuinitdata perf_cpu_nb = {
1493 .notifier_call = perf_cpu_notify,
1494};
1495
1496static int __init perf_counter_init(void)
1497{
1498 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
1499 (void *)(long)smp_processor_id());
1500 register_cpu_notifier(&perf_cpu_nb);
1501
1502 return 0;
1503}
1504early_initcall(perf_counter_init);
1505
1506static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
1507{
1508 return sprintf(buf, "%d\n", perf_reserved_percpu);
1509}
1510
1511static ssize_t
1512perf_set_reserve_percpu(struct sysdev_class *class,
1513 const char *buf,
1514 size_t count)
1515{
1516 struct perf_cpu_context *cpuctx;
1517 unsigned long val;
1518 int err, cpu, mpt;
1519
1520 err = strict_strtoul(buf, 10, &val);
1521 if (err)
1522 return err;
1523 if (val > perf_max_counters)
1524 return -EINVAL;
1525
1526 mutex_lock(&perf_resource_mutex);
1527 perf_reserved_percpu = val;
1528 for_each_online_cpu(cpu) {
1529 cpuctx = &per_cpu(perf_cpu_context, cpu);
1530 spin_lock_irq(&cpuctx->ctx.lock);
1531 mpt = min(perf_max_counters - cpuctx->ctx.nr_counters,
1532 perf_max_counters - perf_reserved_percpu);
1533 cpuctx->max_pertask = mpt;
1534 spin_unlock_irq(&cpuctx->ctx.lock);
1535 }
1536 mutex_unlock(&perf_resource_mutex);
1537
1538 return count;
1539}
1540
1541static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
1542{
1543 return sprintf(buf, "%d\n", perf_overcommit);
1544}
1545
1546static ssize_t
1547perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
1548{
1549 unsigned long val;
1550 int err;
1551
1552 err = strict_strtoul(buf, 10, &val);
1553 if (err)
1554 return err;
1555 if (val > 1)
1556 return -EINVAL;
1557
1558 mutex_lock(&perf_resource_mutex);
1559 perf_overcommit = val;
1560 mutex_unlock(&perf_resource_mutex);
1561
1562 return count;
1563}
1564
1565static SYSDEV_CLASS_ATTR(
1566 reserve_percpu,
1567 0644,
1568 perf_show_reserve_percpu,
1569 perf_set_reserve_percpu
1570 );
1571
1572static SYSDEV_CLASS_ATTR(
1573 overcommit,
1574 0644,
1575 perf_show_overcommit,
1576 perf_set_overcommit
1577 );
1578
1579static struct attribute *perfclass_attrs[] = {
1580 &attr_reserve_percpu.attr,
1581 &attr_overcommit.attr,
1582 NULL
1583};
1584
1585static struct attribute_group perfclass_attr_group = {
1586 .attrs = perfclass_attrs,
1587 .name = "perf_counters",
1588};
1589
1590static int __init perf_counter_sysfs_init(void)
1591{
1592 return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
1593 &perfclass_attr_group);
1594}
1595device_initcall(perf_counter_sysfs_init);