blob: 2e73929a69595c5ca17416aa93583240f422f06c [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 Molnar95cdd2e2008-12-21 13:50:42 +0100358static int
Ingo Molnar04289bb2008-12-11 08:38:42 +0100359counter_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 Molnar95cdd2e2008-12-21 13:50:42 +0100365 return 0;
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100366
Ingo Molnar95cdd2e2008-12-21 13:50:42 +0100367 if (counter->hw_ops->enable(counter))
368 return -EAGAIN;
369
Ingo Molnar6a930702008-12-11 15:17:03 +0100370 counter->state = PERF_COUNTER_STATE_ACTIVE;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100371 counter->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */
372
373 cpuctx->active_oncpu++;
374 ctx->nr_active++;
Ingo Molnar95cdd2e2008-12-21 13:50:42 +0100375
376 return 0;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100377}
378
Ingo Molnar79958882008-12-17 08:54:56 +0100379static int
Ingo Molnar04289bb2008-12-11 08:38:42 +0100380group_sched_in(struct perf_counter *group_counter,
381 struct perf_cpu_context *cpuctx,
382 struct perf_counter_context *ctx,
383 int cpu)
384{
Ingo Molnar95cdd2e2008-12-21 13:50:42 +0100385 struct perf_counter *counter, *partial_group;
386 int ret = 0;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100387
Ingo Molnar95cdd2e2008-12-21 13:50:42 +0100388 if (counter_sched_in(group_counter, cpuctx, ctx, cpu))
389 return -EAGAIN;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100390
391 /*
392 * Schedule in siblings as one group (if any):
393 */
Ingo Molnar79958882008-12-17 08:54:56 +0100394 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
Ingo Molnar95cdd2e2008-12-21 13:50:42 +0100395 if (counter_sched_in(counter, cpuctx, ctx, cpu)) {
396 partial_group = counter;
397 goto group_error;
398 }
399 ret = -EAGAIN;
Ingo Molnar79958882008-12-17 08:54:56 +0100400 }
401
Ingo Molnar95cdd2e2008-12-21 13:50:42 +0100402 return ret;
403
404group_error:
405 /*
406 * Groups can be scheduled in as one unit only, so undo any
407 * partial group before returning:
408 */
409 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
410 if (counter == partial_group)
411 break;
412 counter_sched_out(counter, cpuctx, ctx);
413 }
414 counter_sched_out(group_counter, cpuctx, ctx);
415
416 return -EAGAIN;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100417}
418
Thomas Gleixner0793a612008-12-04 20:12:29 +0100419/*
420 * Called from scheduler to add the counters of the current task
421 * with interrupts disabled.
422 *
423 * We restore the counter value and then enable it.
424 *
Ingo Molnar76715812008-12-17 14:20:28 +0100425 * This does not protect us against NMI, but enable()
Thomas Gleixner0793a612008-12-04 20:12:29 +0100426 * sets the enabled bit in the control field of counter _before_
427 * accessing the counter control register. If a NMI hits, then it will
428 * keep the counter running.
429 */
430void perf_counter_task_sched_in(struct task_struct *task, int cpu)
431{
432 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
433 struct perf_counter_context *ctx = &task->perf_counter_ctx;
434 struct perf_counter *counter;
435
436 if (likely(!ctx->nr_counters))
437 return;
438
439 spin_lock(&ctx->lock);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100440 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Ingo Molnar04289bb2008-12-11 08:38:42 +0100441 /*
442 * Listen to the 'cpu' scheduling filter constraint
443 * of counters:
444 */
Thomas Gleixner0793a612008-12-04 20:12:29 +0100445 if (counter->cpu != -1 && counter->cpu != cpu)
446 continue;
447
Ingo Molnar79958882008-12-17 08:54:56 +0100448 /*
449 * If we scheduled in a group atomically and
450 * exclusively, break out:
451 */
452 if (group_sched_in(counter, cpuctx, ctx, cpu))
453 break;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100454 }
455 spin_unlock(&ctx->lock);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100456
Thomas Gleixner0793a612008-12-04 20:12:29 +0100457 cpuctx->task_ctx = ctx;
458}
459
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100460int perf_counter_task_disable(void)
461{
462 struct task_struct *curr = current;
463 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
464 struct perf_counter *counter;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100465 unsigned long flags;
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100466 u64 perf_flags;
467 int cpu;
468
469 if (likely(!ctx->nr_counters))
470 return 0;
471
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100472 curr_rq_lock_irq_save(&flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100473 cpu = smp_processor_id();
474
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100475 /* force the update of the task clock: */
476 __task_delta_exec(curr, 1);
477
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100478 perf_counter_task_sched_out(curr, cpu);
479
480 spin_lock(&ctx->lock);
481
482 /*
483 * Disable all the counters:
484 */
485 perf_flags = hw_perf_save_disable();
486
Ingo Molnar9b51f662008-12-12 13:49:45 +0100487 list_for_each_entry(counter, &ctx->counter_list, list_entry)
Ingo Molnar6a930702008-12-11 15:17:03 +0100488 counter->state = PERF_COUNTER_STATE_OFF;
Ingo Molnar9b51f662008-12-12 13:49:45 +0100489
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100490 hw_perf_restore(perf_flags);
491
492 spin_unlock(&ctx->lock);
493
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100494 curr_rq_unlock_irq_restore(&flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100495
496 return 0;
497}
498
499int perf_counter_task_enable(void)
500{
501 struct task_struct *curr = current;
502 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
503 struct perf_counter *counter;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100504 unsigned long flags;
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100505 u64 perf_flags;
506 int cpu;
507
508 if (likely(!ctx->nr_counters))
509 return 0;
510
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100511 curr_rq_lock_irq_save(&flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100512 cpu = smp_processor_id();
513
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100514 /* force the update of the task clock: */
515 __task_delta_exec(curr, 1);
516
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100517 spin_lock(&ctx->lock);
518
519 /*
520 * Disable all the counters:
521 */
522 perf_flags = hw_perf_save_disable();
523
524 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Ingo Molnar6a930702008-12-11 15:17:03 +0100525 if (counter->state != PERF_COUNTER_STATE_OFF)
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100526 continue;
Ingo Molnar6a930702008-12-11 15:17:03 +0100527 counter->state = PERF_COUNTER_STATE_INACTIVE;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100528 counter->hw_event.disabled = 0;
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100529 }
530 hw_perf_restore(perf_flags);
531
532 spin_unlock(&ctx->lock);
533
534 perf_counter_task_sched_in(curr, cpu);
535
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100536 curr_rq_unlock_irq_restore(&flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100537
538 return 0;
539}
540
Thomas Gleixner0793a612008-12-04 20:12:29 +0100541void perf_counter_task_tick(struct task_struct *curr, int cpu)
542{
543 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
544 struct perf_counter *counter;
Ingo Molnar5c92d122008-12-11 13:21:10 +0100545 u64 perf_flags;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100546
547 if (likely(!ctx->nr_counters))
548 return;
549
550 perf_counter_task_sched_out(curr, cpu);
551
552 spin_lock(&ctx->lock);
553
554 /*
Ingo Molnar04289bb2008-12-11 08:38:42 +0100555 * Rotate the first entry last (works just fine for group counters too):
Thomas Gleixner0793a612008-12-04 20:12:29 +0100556 */
Ingo Molnar01b28382008-12-11 13:45:51 +0100557 perf_flags = hw_perf_save_disable();
Ingo Molnar04289bb2008-12-11 08:38:42 +0100558 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
559 list_del(&counter->list_entry);
560 list_add_tail(&counter->list_entry, &ctx->counter_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100561 break;
562 }
Ingo Molnar01b28382008-12-11 13:45:51 +0100563 hw_perf_restore(perf_flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100564
565 spin_unlock(&ctx->lock);
566
567 perf_counter_task_sched_in(curr, cpu);
568}
569
570/*
Thomas Gleixner0793a612008-12-04 20:12:29 +0100571 * Cross CPU call to read the hardware counter
572 */
Ingo Molnar76715812008-12-17 14:20:28 +0100573static void __read(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100574{
Ingo Molnar621a01e2008-12-11 12:46:46 +0100575 struct perf_counter *counter = info;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100576 unsigned long flags;
Ingo Molnar621a01e2008-12-11 12:46:46 +0100577
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100578 curr_rq_lock_irq_save(&flags);
Ingo Molnar76715812008-12-17 14:20:28 +0100579 counter->hw_ops->read(counter);
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100580 curr_rq_unlock_irq_restore(&flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100581}
582
Ingo Molnar04289bb2008-12-11 08:38:42 +0100583static u64 perf_counter_read(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100584{
585 /*
586 * If counter is enabled and currently active on a CPU, update the
587 * value in the counter structure:
588 */
Ingo Molnar6a930702008-12-11 15:17:03 +0100589 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100590 smp_call_function_single(counter->oncpu,
Ingo Molnar76715812008-12-17 14:20:28 +0100591 __read, counter, 1);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100592 }
593
Ingo Molnaree060942008-12-13 09:00:03 +0100594 return atomic64_read(&counter->count);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100595}
596
597/*
598 * Cross CPU call to switch performance data pointers
599 */
600static void __perf_switch_irq_data(void *info)
601{
602 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
603 struct perf_counter *counter = info;
604 struct perf_counter_context *ctx = counter->ctx;
605 struct perf_data *oldirqdata = counter->irqdata;
606
607 /*
608 * If this is a task context, we need to check whether it is
609 * the current task context of this cpu. If not it has been
610 * scheduled out before the smp call arrived.
611 */
612 if (ctx->task) {
613 if (cpuctx->task_ctx != ctx)
614 return;
615 spin_lock(&ctx->lock);
616 }
617
618 /* Change the pointer NMI safe */
619 atomic_long_set((atomic_long_t *)&counter->irqdata,
620 (unsigned long) counter->usrdata);
621 counter->usrdata = oldirqdata;
622
623 if (ctx->task)
624 spin_unlock(&ctx->lock);
625}
626
627static struct perf_data *perf_switch_irq_data(struct perf_counter *counter)
628{
629 struct perf_counter_context *ctx = counter->ctx;
630 struct perf_data *oldirqdata = counter->irqdata;
631 struct task_struct *task = ctx->task;
632
633 if (!task) {
634 smp_call_function_single(counter->cpu,
635 __perf_switch_irq_data,
636 counter, 1);
637 return counter->usrdata;
638 }
639
640retry:
641 spin_lock_irq(&ctx->lock);
Ingo Molnar6a930702008-12-11 15:17:03 +0100642 if (counter->state != PERF_COUNTER_STATE_ACTIVE) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100643 counter->irqdata = counter->usrdata;
644 counter->usrdata = oldirqdata;
645 spin_unlock_irq(&ctx->lock);
646 return oldirqdata;
647 }
648 spin_unlock_irq(&ctx->lock);
649 task_oncpu_function_call(task, __perf_switch_irq_data, counter);
650 /* Might have failed, because task was scheduled out */
651 if (counter->irqdata == oldirqdata)
652 goto retry;
653
654 return counter->usrdata;
655}
656
657static void put_context(struct perf_counter_context *ctx)
658{
659 if (ctx->task)
660 put_task_struct(ctx->task);
661}
662
663static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
664{
665 struct perf_cpu_context *cpuctx;
666 struct perf_counter_context *ctx;
667 struct task_struct *task;
668
669 /*
670 * If cpu is not a wildcard then this is a percpu counter:
671 */
672 if (cpu != -1) {
673 /* Must be root to operate on a CPU counter: */
674 if (!capable(CAP_SYS_ADMIN))
675 return ERR_PTR(-EACCES);
676
677 if (cpu < 0 || cpu > num_possible_cpus())
678 return ERR_PTR(-EINVAL);
679
680 /*
681 * We could be clever and allow to attach a counter to an
682 * offline CPU and activate it when the CPU comes up, but
683 * that's for later.
684 */
685 if (!cpu_isset(cpu, cpu_online_map))
686 return ERR_PTR(-ENODEV);
687
688 cpuctx = &per_cpu(perf_cpu_context, cpu);
689 ctx = &cpuctx->ctx;
690
Thomas Gleixner0793a612008-12-04 20:12:29 +0100691 return ctx;
692 }
693
694 rcu_read_lock();
695 if (!pid)
696 task = current;
697 else
698 task = find_task_by_vpid(pid);
699 if (task)
700 get_task_struct(task);
701 rcu_read_unlock();
702
703 if (!task)
704 return ERR_PTR(-ESRCH);
705
706 ctx = &task->perf_counter_ctx;
707 ctx->task = task;
708
709 /* Reuse ptrace permission checks for now. */
710 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
711 put_context(ctx);
712 return ERR_PTR(-EACCES);
713 }
714
715 return ctx;
716}
717
718/*
719 * Called when the last reference to the file is gone.
720 */
721static int perf_release(struct inode *inode, struct file *file)
722{
723 struct perf_counter *counter = file->private_data;
724 struct perf_counter_context *ctx = counter->ctx;
725
726 file->private_data = NULL;
727
728 mutex_lock(&counter->mutex);
729
Ingo Molnar04289bb2008-12-11 08:38:42 +0100730 perf_counter_remove_from_context(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100731 put_context(ctx);
732
733 mutex_unlock(&counter->mutex);
734
735 kfree(counter);
736
737 return 0;
738}
739
740/*
741 * Read the performance counter - simple non blocking version for now
742 */
743static ssize_t
744perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
745{
746 u64 cntval;
747
748 if (count != sizeof(cntval))
749 return -EINVAL;
750
751 mutex_lock(&counter->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100752 cntval = perf_counter_read(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100753 mutex_unlock(&counter->mutex);
754
755 return put_user(cntval, (u64 __user *) buf) ? -EFAULT : sizeof(cntval);
756}
757
758static ssize_t
759perf_copy_usrdata(struct perf_data *usrdata, char __user *buf, size_t count)
760{
761 if (!usrdata->len)
762 return 0;
763
764 count = min(count, (size_t)usrdata->len);
765 if (copy_to_user(buf, usrdata->data + usrdata->rd_idx, count))
766 return -EFAULT;
767
768 /* Adjust the counters */
769 usrdata->len -= count;
770 if (!usrdata->len)
771 usrdata->rd_idx = 0;
772 else
773 usrdata->rd_idx += count;
774
775 return count;
776}
777
778static ssize_t
779perf_read_irq_data(struct perf_counter *counter,
780 char __user *buf,
781 size_t count,
782 int nonblocking)
783{
784 struct perf_data *irqdata, *usrdata;
785 DECLARE_WAITQUEUE(wait, current);
786 ssize_t res;
787
788 irqdata = counter->irqdata;
789 usrdata = counter->usrdata;
790
791 if (usrdata->len + irqdata->len >= count)
792 goto read_pending;
793
794 if (nonblocking)
795 return -EAGAIN;
796
797 spin_lock_irq(&counter->waitq.lock);
798 __add_wait_queue(&counter->waitq, &wait);
799 for (;;) {
800 set_current_state(TASK_INTERRUPTIBLE);
801 if (usrdata->len + irqdata->len >= count)
802 break;
803
804 if (signal_pending(current))
805 break;
806
807 spin_unlock_irq(&counter->waitq.lock);
808 schedule();
809 spin_lock_irq(&counter->waitq.lock);
810 }
811 __remove_wait_queue(&counter->waitq, &wait);
812 __set_current_state(TASK_RUNNING);
813 spin_unlock_irq(&counter->waitq.lock);
814
815 if (usrdata->len + irqdata->len < count)
816 return -ERESTARTSYS;
817read_pending:
818 mutex_lock(&counter->mutex);
819
820 /* Drain pending data first: */
821 res = perf_copy_usrdata(usrdata, buf, count);
822 if (res < 0 || res == count)
823 goto out;
824
825 /* Switch irq buffer: */
826 usrdata = perf_switch_irq_data(counter);
827 if (perf_copy_usrdata(usrdata, buf + res, count - res) < 0) {
828 if (!res)
829 res = -EFAULT;
830 } else {
831 res = count;
832 }
833out:
834 mutex_unlock(&counter->mutex);
835
836 return res;
837}
838
839static ssize_t
840perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
841{
842 struct perf_counter *counter = file->private_data;
843
Ingo Molnar9f66a382008-12-10 12:33:23 +0100844 switch (counter->hw_event.record_type) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100845 case PERF_RECORD_SIMPLE:
846 return perf_read_hw(counter, buf, count);
847
848 case PERF_RECORD_IRQ:
849 case PERF_RECORD_GROUP:
850 return perf_read_irq_data(counter, buf, count,
851 file->f_flags & O_NONBLOCK);
852 }
853 return -EINVAL;
854}
855
856static unsigned int perf_poll(struct file *file, poll_table *wait)
857{
858 struct perf_counter *counter = file->private_data;
859 unsigned int events = 0;
860 unsigned long flags;
861
862 poll_wait(file, &counter->waitq, wait);
863
864 spin_lock_irqsave(&counter->waitq.lock, flags);
865 if (counter->usrdata->len || counter->irqdata->len)
866 events |= POLLIN;
867 spin_unlock_irqrestore(&counter->waitq.lock, flags);
868
869 return events;
870}
871
872static const struct file_operations perf_fops = {
873 .release = perf_release,
874 .read = perf_read,
875 .poll = perf_poll,
876};
877
Ingo Molnar95cdd2e2008-12-21 13:50:42 +0100878static int cpu_clock_perf_counter_enable(struct perf_counter *counter)
Ingo Molnar5c92d122008-12-11 13:21:10 +0100879{
Ingo Molnar95cdd2e2008-12-21 13:50:42 +0100880 return 0;
Ingo Molnar5c92d122008-12-11 13:21:10 +0100881}
882
883static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
884{
885}
886
887static void cpu_clock_perf_counter_read(struct perf_counter *counter)
888{
889 int cpu = raw_smp_processor_id();
890
Ingo Molnaree060942008-12-13 09:00:03 +0100891 atomic64_set(&counter->count, cpu_clock(cpu));
Ingo Molnar5c92d122008-12-11 13:21:10 +0100892}
893
894static const struct hw_perf_counter_ops perf_ops_cpu_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +0100895 .enable = cpu_clock_perf_counter_enable,
896 .disable = cpu_clock_perf_counter_disable,
897 .read = cpu_clock_perf_counter_read,
Ingo Molnar5c92d122008-12-11 13:21:10 +0100898};
899
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100900/*
901 * Called from within the scheduler:
902 */
903static u64 task_clock_perf_counter_val(struct perf_counter *counter, int update)
Ingo Molnarbae43c92008-12-11 14:03:20 +0100904{
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100905 struct task_struct *curr = counter->task;
906 u64 delta;
907
908 WARN_ON_ONCE(counter->task != current);
909
910 delta = __task_delta_exec(curr, update);
911
912 return curr->se.sum_exec_runtime + delta;
913}
914
915static void task_clock_perf_counter_update(struct perf_counter *counter, u64 now)
916{
917 u64 prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +0100918 s64 delta;
Ingo Molnarbae43c92008-12-11 14:03:20 +0100919
Ingo Molnar8cb391e2008-12-14 12:22:31 +0100920 prev = atomic64_read(&counter->hw.prev_count);
Ingo Molnar8cb391e2008-12-14 12:22:31 +0100921
922 atomic64_set(&counter->hw.prev_count, now);
923
924 delta = now - prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +0100925
926 atomic64_add(delta, &counter->count);
Ingo Molnarbae43c92008-12-11 14:03:20 +0100927}
928
929static void task_clock_perf_counter_read(struct perf_counter *counter)
930{
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100931 u64 now = task_clock_perf_counter_val(counter, 1);
932
933 task_clock_perf_counter_update(counter, now);
Ingo Molnar8cb391e2008-12-14 12:22:31 +0100934}
935
Ingo Molnar95cdd2e2008-12-21 13:50:42 +0100936static int task_clock_perf_counter_enable(struct perf_counter *counter)
Ingo Molnar8cb391e2008-12-14 12:22:31 +0100937{
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100938 u64 now = task_clock_perf_counter_val(counter, 0);
939
940 atomic64_set(&counter->hw.prev_count, now);
Ingo Molnar95cdd2e2008-12-21 13:50:42 +0100941
942 return 0;
Ingo Molnar8cb391e2008-12-14 12:22:31 +0100943}
944
945static void task_clock_perf_counter_disable(struct perf_counter *counter)
946{
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100947 u64 now = task_clock_perf_counter_val(counter, 0);
948
949 task_clock_perf_counter_update(counter, now);
Ingo Molnarbae43c92008-12-11 14:03:20 +0100950}
951
952static const struct hw_perf_counter_ops perf_ops_task_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +0100953 .enable = task_clock_perf_counter_enable,
954 .disable = task_clock_perf_counter_disable,
955 .read = task_clock_perf_counter_read,
Ingo Molnarbae43c92008-12-11 14:03:20 +0100956};
957
Ingo Molnare06c61a2008-12-14 14:44:31 +0100958static u64 get_page_faults(void)
959{
960 struct task_struct *curr = current;
961
962 return curr->maj_flt + curr->min_flt;
963}
964
965static void page_faults_perf_counter_update(struct perf_counter *counter)
966{
967 u64 prev, now;
968 s64 delta;
969
970 prev = atomic64_read(&counter->hw.prev_count);
971 now = get_page_faults();
972
973 atomic64_set(&counter->hw.prev_count, now);
974
975 delta = now - prev;
Ingo Molnare06c61a2008-12-14 14:44:31 +0100976
977 atomic64_add(delta, &counter->count);
978}
979
980static void page_faults_perf_counter_read(struct perf_counter *counter)
981{
982 page_faults_perf_counter_update(counter);
983}
984
Ingo Molnar95cdd2e2008-12-21 13:50:42 +0100985static int page_faults_perf_counter_enable(struct perf_counter *counter)
Ingo Molnare06c61a2008-12-14 14:44:31 +0100986{
987 /*
988 * page-faults is a per-task value already,
989 * so we dont have to clear it on switch-in.
990 */
Ingo Molnar95cdd2e2008-12-21 13:50:42 +0100991
992 return 0;
Ingo Molnare06c61a2008-12-14 14:44:31 +0100993}
994
995static void page_faults_perf_counter_disable(struct perf_counter *counter)
996{
997 page_faults_perf_counter_update(counter);
998}
999
1000static const struct hw_perf_counter_ops perf_ops_page_faults = {
Ingo Molnar76715812008-12-17 14:20:28 +01001001 .enable = page_faults_perf_counter_enable,
1002 .disable = page_faults_perf_counter_disable,
1003 .read = page_faults_perf_counter_read,
Ingo Molnare06c61a2008-12-14 14:44:31 +01001004};
1005
Ingo Molnar5d6a27d2008-12-14 12:28:33 +01001006static u64 get_context_switches(void)
1007{
1008 struct task_struct *curr = current;
1009
1010 return curr->nvcsw + curr->nivcsw;
1011}
1012
1013static void context_switches_perf_counter_update(struct perf_counter *counter)
1014{
1015 u64 prev, now;
1016 s64 delta;
1017
1018 prev = atomic64_read(&counter->hw.prev_count);
1019 now = get_context_switches();
1020
1021 atomic64_set(&counter->hw.prev_count, now);
1022
1023 delta = now - prev;
Ingo Molnar5d6a27d2008-12-14 12:28:33 +01001024
1025 atomic64_add(delta, &counter->count);
1026}
1027
1028static void context_switches_perf_counter_read(struct perf_counter *counter)
1029{
1030 context_switches_perf_counter_update(counter);
1031}
1032
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01001033static int context_switches_perf_counter_enable(struct perf_counter *counter)
Ingo Molnar5d6a27d2008-12-14 12:28:33 +01001034{
1035 /*
1036 * ->nvcsw + curr->nivcsw is a per-task value already,
1037 * so we dont have to clear it on switch-in.
1038 */
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01001039
1040 return 0;
Ingo Molnar5d6a27d2008-12-14 12:28:33 +01001041}
1042
1043static void context_switches_perf_counter_disable(struct perf_counter *counter)
1044{
1045 context_switches_perf_counter_update(counter);
1046}
1047
1048static const struct hw_perf_counter_ops perf_ops_context_switches = {
Ingo Molnar76715812008-12-17 14:20:28 +01001049 .enable = context_switches_perf_counter_enable,
1050 .disable = context_switches_perf_counter_disable,
1051 .read = context_switches_perf_counter_read,
Ingo Molnar5d6a27d2008-12-14 12:28:33 +01001052};
1053
Ingo Molnar6c594c22008-12-14 12:34:15 +01001054static inline u64 get_cpu_migrations(void)
1055{
1056 return current->se.nr_migrations;
1057}
1058
1059static void cpu_migrations_perf_counter_update(struct perf_counter *counter)
1060{
1061 u64 prev, now;
1062 s64 delta;
1063
1064 prev = atomic64_read(&counter->hw.prev_count);
1065 now = get_cpu_migrations();
1066
1067 atomic64_set(&counter->hw.prev_count, now);
1068
1069 delta = now - prev;
Ingo Molnar6c594c22008-12-14 12:34:15 +01001070
1071 atomic64_add(delta, &counter->count);
1072}
1073
1074static void cpu_migrations_perf_counter_read(struct perf_counter *counter)
1075{
1076 cpu_migrations_perf_counter_update(counter);
1077}
1078
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01001079static int cpu_migrations_perf_counter_enable(struct perf_counter *counter)
Ingo Molnar6c594c22008-12-14 12:34:15 +01001080{
1081 /*
1082 * se.nr_migrations is a per-task value already,
1083 * so we dont have to clear it on switch-in.
1084 */
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01001085
1086 return 0;
Ingo Molnar6c594c22008-12-14 12:34:15 +01001087}
1088
1089static void cpu_migrations_perf_counter_disable(struct perf_counter *counter)
1090{
1091 cpu_migrations_perf_counter_update(counter);
1092}
1093
1094static const struct hw_perf_counter_ops perf_ops_cpu_migrations = {
Ingo Molnar76715812008-12-17 14:20:28 +01001095 .enable = cpu_migrations_perf_counter_enable,
1096 .disable = cpu_migrations_perf_counter_disable,
1097 .read = cpu_migrations_perf_counter_read,
Ingo Molnar6c594c22008-12-14 12:34:15 +01001098};
1099
Ingo Molnar5c92d122008-12-11 13:21:10 +01001100static const struct hw_perf_counter_ops *
1101sw_perf_counter_init(struct perf_counter *counter)
1102{
1103 const struct hw_perf_counter_ops *hw_ops = NULL;
1104
1105 switch (counter->hw_event.type) {
1106 case PERF_COUNT_CPU_CLOCK:
1107 hw_ops = &perf_ops_cpu_clock;
1108 break;
Ingo Molnarbae43c92008-12-11 14:03:20 +01001109 case PERF_COUNT_TASK_CLOCK:
1110 hw_ops = &perf_ops_task_clock;
1111 break;
Ingo Molnare06c61a2008-12-14 14:44:31 +01001112 case PERF_COUNT_PAGE_FAULTS:
1113 hw_ops = &perf_ops_page_faults;
1114 break;
Ingo Molnar5d6a27d2008-12-14 12:28:33 +01001115 case PERF_COUNT_CONTEXT_SWITCHES:
1116 hw_ops = &perf_ops_context_switches;
1117 break;
Ingo Molnar6c594c22008-12-14 12:34:15 +01001118 case PERF_COUNT_CPU_MIGRATIONS:
1119 hw_ops = &perf_ops_cpu_migrations;
1120 break;
Ingo Molnar5c92d122008-12-11 13:21:10 +01001121 default:
1122 break;
1123 }
1124 return hw_ops;
1125}
1126
Thomas Gleixner0793a612008-12-04 20:12:29 +01001127/*
1128 * Allocate and initialize a counter structure
1129 */
1130static struct perf_counter *
Ingo Molnar04289bb2008-12-11 08:38:42 +01001131perf_counter_alloc(struct perf_counter_hw_event *hw_event,
1132 int cpu,
Ingo Molnar9b51f662008-12-12 13:49:45 +01001133 struct perf_counter *group_leader,
1134 gfp_t gfpflags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001135{
Ingo Molnar5c92d122008-12-11 13:21:10 +01001136 const struct hw_perf_counter_ops *hw_ops;
Ingo Molnar621a01e2008-12-11 12:46:46 +01001137 struct perf_counter *counter;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001138
Ingo Molnar9b51f662008-12-12 13:49:45 +01001139 counter = kzalloc(sizeof(*counter), gfpflags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001140 if (!counter)
1141 return NULL;
1142
Ingo Molnar04289bb2008-12-11 08:38:42 +01001143 /*
1144 * Single counters are their own group leaders, with an
1145 * empty sibling list:
1146 */
1147 if (!group_leader)
1148 group_leader = counter;
1149
Thomas Gleixner0793a612008-12-04 20:12:29 +01001150 mutex_init(&counter->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01001151 INIT_LIST_HEAD(&counter->list_entry);
1152 INIT_LIST_HEAD(&counter->sibling_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001153 init_waitqueue_head(&counter->waitq);
1154
Ingo Molnar9f66a382008-12-10 12:33:23 +01001155 counter->irqdata = &counter->data[0];
1156 counter->usrdata = &counter->data[1];
1157 counter->cpu = cpu;
1158 counter->hw_event = *hw_event;
1159 counter->wakeup_pending = 0;
Ingo Molnar04289bb2008-12-11 08:38:42 +01001160 counter->group_leader = group_leader;
Ingo Molnar621a01e2008-12-11 12:46:46 +01001161 counter->hw_ops = NULL;
1162
Ingo Molnara86ed502008-12-17 00:43:10 +01001163 if (hw_event->disabled)
1164 counter->state = PERF_COUNTER_STATE_OFF;
1165
Ingo Molnar5c92d122008-12-11 13:21:10 +01001166 hw_ops = NULL;
1167 if (!hw_event->raw && hw_event->type < 0)
1168 hw_ops = sw_perf_counter_init(counter);
Ingo Molnar9b51f662008-12-12 13:49:45 +01001169 if (!hw_ops)
Ingo Molnar5c92d122008-12-11 13:21:10 +01001170 hw_ops = hw_perf_counter_init(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01001171
Ingo Molnar621a01e2008-12-11 12:46:46 +01001172 if (!hw_ops) {
1173 kfree(counter);
1174 return NULL;
1175 }
1176 counter->hw_ops = hw_ops;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001177
1178 return counter;
1179}
1180
1181/**
Ingo Molnar9f66a382008-12-10 12:33:23 +01001182 * sys_perf_task_open - open a performance counter, associate it to a task/cpu
1183 *
1184 * @hw_event_uptr: event type attributes for monitoring/sampling
Thomas Gleixner0793a612008-12-04 20:12:29 +01001185 * @pid: target pid
Ingo Molnar9f66a382008-12-10 12:33:23 +01001186 * @cpu: target cpu
1187 * @group_fd: group leader counter fd
Thomas Gleixner0793a612008-12-04 20:12:29 +01001188 */
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001189asmlinkage int
1190sys_perf_counter_open(struct perf_counter_hw_event *hw_event_uptr __user,
1191 pid_t pid, int cpu, int group_fd)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001192{
Ingo Molnar04289bb2008-12-11 08:38:42 +01001193 struct perf_counter *counter, *group_leader;
Ingo Molnar9f66a382008-12-10 12:33:23 +01001194 struct perf_counter_hw_event hw_event;
Ingo Molnar04289bb2008-12-11 08:38:42 +01001195 struct perf_counter_context *ctx;
Ingo Molnar9b51f662008-12-12 13:49:45 +01001196 struct file *counter_file = NULL;
Ingo Molnar04289bb2008-12-11 08:38:42 +01001197 struct file *group_file = NULL;
1198 int fput_needed = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01001199 int fput_needed2 = 0;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001200 int ret;
1201
Ingo Molnar9f66a382008-12-10 12:33:23 +01001202 if (copy_from_user(&hw_event, hw_event_uptr, sizeof(hw_event)) != 0)
Thomas Gleixnereab656a2008-12-08 19:26:59 +01001203 return -EFAULT;
1204
Ingo Molnar04289bb2008-12-11 08:38:42 +01001205 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01001206 * Get the target context (task or percpu):
1207 */
1208 ctx = find_get_context(pid, cpu);
1209 if (IS_ERR(ctx))
1210 return PTR_ERR(ctx);
1211
1212 /*
1213 * Look up the group leader (we will attach this counter to it):
Ingo Molnar04289bb2008-12-11 08:38:42 +01001214 */
1215 group_leader = NULL;
1216 if (group_fd != -1) {
1217 ret = -EINVAL;
1218 group_file = fget_light(group_fd, &fput_needed);
1219 if (!group_file)
Ingo Molnarccff2862008-12-11 11:26:29 +01001220 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01001221 if (group_file->f_op != &perf_fops)
Ingo Molnarccff2862008-12-11 11:26:29 +01001222 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01001223
1224 group_leader = group_file->private_data;
1225 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01001226 * Do not allow a recursive hierarchy (this new sibling
1227 * becoming part of another group-sibling):
Ingo Molnar04289bb2008-12-11 08:38:42 +01001228 */
Ingo Molnarccff2862008-12-11 11:26:29 +01001229 if (group_leader->group_leader != group_leader)
1230 goto err_put_context;
1231 /*
1232 * Do not allow to attach to a group in a different
1233 * task or CPU context:
1234 */
1235 if (group_leader->ctx != ctx)
1236 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01001237 }
1238
Ingo Molnar5c92d122008-12-11 13:21:10 +01001239 ret = -EINVAL;
Ingo Molnar9b51f662008-12-12 13:49:45 +01001240 counter = perf_counter_alloc(&hw_event, cpu, group_leader, GFP_KERNEL);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001241 if (!counter)
1242 goto err_put_context;
1243
Thomas Gleixner0793a612008-12-04 20:12:29 +01001244 ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
1245 if (ret < 0)
Ingo Molnar9b51f662008-12-12 13:49:45 +01001246 goto err_free_put_context;
1247
1248 counter_file = fget_light(ret, &fput_needed2);
1249 if (!counter_file)
1250 goto err_free_put_context;
1251
1252 counter->filp = counter_file;
1253 perf_install_in_context(ctx, counter, cpu);
1254
1255 fput_light(counter_file, fput_needed2);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001256
Ingo Molnar04289bb2008-12-11 08:38:42 +01001257out_fput:
1258 fput_light(group_file, fput_needed);
1259
Thomas Gleixner0793a612008-12-04 20:12:29 +01001260 return ret;
1261
Ingo Molnar9b51f662008-12-12 13:49:45 +01001262err_free_put_context:
Thomas Gleixner0793a612008-12-04 20:12:29 +01001263 kfree(counter);
1264
1265err_put_context:
1266 put_context(ctx);
1267
Ingo Molnar04289bb2008-12-11 08:38:42 +01001268 goto out_fput;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001269}
1270
Ingo Molnar9b51f662008-12-12 13:49:45 +01001271/*
1272 * Initialize the perf_counter context in a task_struct:
1273 */
1274static void
1275__perf_counter_init_context(struct perf_counter_context *ctx,
1276 struct task_struct *task)
1277{
1278 memset(ctx, 0, sizeof(*ctx));
1279 spin_lock_init(&ctx->lock);
1280 INIT_LIST_HEAD(&ctx->counter_list);
1281 ctx->task = task;
1282}
1283
1284/*
1285 * inherit a counter from parent task to child task:
1286 */
1287static int
1288inherit_counter(struct perf_counter *parent_counter,
1289 struct task_struct *parent,
1290 struct perf_counter_context *parent_ctx,
1291 struct task_struct *child,
1292 struct perf_counter_context *child_ctx)
1293{
1294 struct perf_counter *child_counter;
1295
1296 child_counter = perf_counter_alloc(&parent_counter->hw_event,
1297 parent_counter->cpu, NULL,
1298 GFP_ATOMIC);
1299 if (!child_counter)
1300 return -ENOMEM;
1301
1302 /*
1303 * Link it up in the child's context:
1304 */
1305 child_counter->ctx = child_ctx;
1306 child_counter->task = child;
1307 list_add_counter(child_counter, child_ctx);
1308 child_ctx->nr_counters++;
1309
1310 child_counter->parent = parent_counter;
1311 parent_counter->nr_inherited++;
1312 /*
1313 * inherit into child's child as well:
1314 */
1315 child_counter->hw_event.inherit = 1;
1316
1317 /*
1318 * Get a reference to the parent filp - we will fput it
1319 * when the child counter exits. This is safe to do because
1320 * we are in the parent and we know that the filp still
1321 * exists and has a nonzero count:
1322 */
1323 atomic_long_inc(&parent_counter->filp->f_count);
1324
1325 return 0;
1326}
1327
1328static void
1329__perf_counter_exit_task(struct task_struct *child,
1330 struct perf_counter *child_counter,
1331 struct perf_counter_context *child_ctx)
1332{
1333 struct perf_counter *parent_counter;
1334 u64 parent_val, child_val;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001335 unsigned long flags;
Ingo Molnar9b51f662008-12-12 13:49:45 +01001336 u64 perf_flags;
1337
1338 /*
1339 * Disable and unlink this counter.
1340 *
1341 * Be careful about zapping the list - IRQ/NMI context
1342 * could still be processing it:
1343 */
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001344 curr_rq_lock_irq_save(&flags);
Ingo Molnar9b51f662008-12-12 13:49:45 +01001345 perf_flags = hw_perf_save_disable();
1346
Ingo Molnar0cc0c022008-12-14 23:20:36 +01001347 if (child_counter->state == PERF_COUNTER_STATE_ACTIVE) {
1348 struct perf_cpu_context *cpuctx;
1349
1350 cpuctx = &__get_cpu_var(perf_cpu_context);
1351
Ingo Molnar76715812008-12-17 14:20:28 +01001352 child_counter->hw_ops->disable(child_counter);
Ingo Molnar0cc0c022008-12-14 23:20:36 +01001353 child_counter->state = PERF_COUNTER_STATE_INACTIVE;
1354 child_counter->oncpu = -1;
1355
1356 cpuctx->active_oncpu--;
1357 child_ctx->nr_active--;
1358 }
1359
Ingo Molnar9b51f662008-12-12 13:49:45 +01001360 list_del_init(&child_counter->list_entry);
1361
1362 hw_perf_restore(perf_flags);
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001363 curr_rq_unlock_irq_restore(&flags);
Ingo Molnar9b51f662008-12-12 13:49:45 +01001364
1365 parent_counter = child_counter->parent;
1366 /*
1367 * It can happen that parent exits first, and has counters
1368 * that are still around due to the child reference. These
1369 * counters need to be zapped - but otherwise linger.
1370 */
1371 if (!parent_counter)
1372 return;
1373
1374 parent_val = atomic64_read(&parent_counter->count);
1375 child_val = atomic64_read(&child_counter->count);
1376
1377 /*
1378 * Add back the child's count to the parent's count:
1379 */
1380 atomic64_add(child_val, &parent_counter->count);
1381
1382 fput(parent_counter->filp);
1383
1384 kfree(child_counter);
1385}
1386
1387/*
1388 * When a child task exist, feed back counter values to parent counters.
1389 *
1390 * Note: we are running in child context, but the PID is not hashed
1391 * anymore so new counters will not be added.
1392 */
1393void perf_counter_exit_task(struct task_struct *child)
1394{
1395 struct perf_counter *child_counter, *tmp;
1396 struct perf_counter_context *child_ctx;
1397
1398 child_ctx = &child->perf_counter_ctx;
1399
1400 if (likely(!child_ctx->nr_counters))
1401 return;
1402
1403 list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list,
1404 list_entry)
1405 __perf_counter_exit_task(child, child_counter, child_ctx);
1406}
1407
1408/*
1409 * Initialize the perf_counter context in task_struct
1410 */
1411void perf_counter_init_task(struct task_struct *child)
1412{
1413 struct perf_counter_context *child_ctx, *parent_ctx;
1414 struct perf_counter *counter, *parent_counter;
1415 struct task_struct *parent = current;
1416 unsigned long flags;
1417
1418 child_ctx = &child->perf_counter_ctx;
1419 parent_ctx = &parent->perf_counter_ctx;
1420
1421 __perf_counter_init_context(child_ctx, child);
1422
1423 /*
1424 * This is executed from the parent task context, so inherit
1425 * counters that have been marked for cloning:
1426 */
1427
1428 if (likely(!parent_ctx->nr_counters))
1429 return;
1430
1431 /*
1432 * Lock the parent list. No need to lock the child - not PID
1433 * hashed yet and not running, so nobody can access it.
1434 */
1435 spin_lock_irqsave(&parent_ctx->lock, flags);
1436
1437 /*
1438 * We dont have to disable NMIs - we are only looking at
1439 * the list, not manipulating it:
1440 */
1441 list_for_each_entry(counter, &parent_ctx->counter_list, list_entry) {
1442 if (!counter->hw_event.inherit || counter->group_leader != counter)
1443 continue;
1444
1445 /*
1446 * Instead of creating recursive hierarchies of counters,
1447 * we link inheritd counters back to the original parent,
1448 * which has a filp for sure, which we use as the reference
1449 * count:
1450 */
1451 parent_counter = counter;
1452 if (counter->parent)
1453 parent_counter = counter->parent;
1454
1455 if (inherit_counter(parent_counter, parent,
1456 parent_ctx, child, child_ctx))
1457 break;
1458 }
1459
1460 spin_unlock_irqrestore(&parent_ctx->lock, flags);
1461}
1462
Ingo Molnar04289bb2008-12-11 08:38:42 +01001463static void __cpuinit perf_counter_init_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001464{
Ingo Molnar04289bb2008-12-11 08:38:42 +01001465 struct perf_cpu_context *cpuctx;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001466
Ingo Molnar04289bb2008-12-11 08:38:42 +01001467 cpuctx = &per_cpu(perf_cpu_context, cpu);
1468 __perf_counter_init_context(&cpuctx->ctx, NULL);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001469
1470 mutex_lock(&perf_resource_mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01001471 cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001472 mutex_unlock(&perf_resource_mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01001473
Thomas Gleixner0793a612008-12-04 20:12:29 +01001474 hw_perf_counter_setup();
1475}
1476
1477#ifdef CONFIG_HOTPLUG_CPU
Ingo Molnar04289bb2008-12-11 08:38:42 +01001478static void __perf_counter_exit_cpu(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001479{
1480 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1481 struct perf_counter_context *ctx = &cpuctx->ctx;
1482 struct perf_counter *counter, *tmp;
1483
Ingo Molnar04289bb2008-12-11 08:38:42 +01001484 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry)
1485 __perf_counter_remove_from_context(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001486
1487}
Ingo Molnar04289bb2008-12-11 08:38:42 +01001488static void perf_counter_exit_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001489{
Ingo Molnar04289bb2008-12-11 08:38:42 +01001490 smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001491}
1492#else
Ingo Molnar04289bb2008-12-11 08:38:42 +01001493static inline void perf_counter_exit_cpu(int cpu) { }
Thomas Gleixner0793a612008-12-04 20:12:29 +01001494#endif
1495
1496static int __cpuinit
1497perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
1498{
1499 unsigned int cpu = (long)hcpu;
1500
1501 switch (action) {
1502
1503 case CPU_UP_PREPARE:
1504 case CPU_UP_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01001505 perf_counter_init_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001506 break;
1507
1508 case CPU_DOWN_PREPARE:
1509 case CPU_DOWN_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01001510 perf_counter_exit_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001511 break;
1512
1513 default:
1514 break;
1515 }
1516
1517 return NOTIFY_OK;
1518}
1519
1520static struct notifier_block __cpuinitdata perf_cpu_nb = {
1521 .notifier_call = perf_cpu_notify,
1522};
1523
1524static int __init perf_counter_init(void)
1525{
1526 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
1527 (void *)(long)smp_processor_id());
1528 register_cpu_notifier(&perf_cpu_nb);
1529
1530 return 0;
1531}
1532early_initcall(perf_counter_init);
1533
1534static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
1535{
1536 return sprintf(buf, "%d\n", perf_reserved_percpu);
1537}
1538
1539static ssize_t
1540perf_set_reserve_percpu(struct sysdev_class *class,
1541 const char *buf,
1542 size_t count)
1543{
1544 struct perf_cpu_context *cpuctx;
1545 unsigned long val;
1546 int err, cpu, mpt;
1547
1548 err = strict_strtoul(buf, 10, &val);
1549 if (err)
1550 return err;
1551 if (val > perf_max_counters)
1552 return -EINVAL;
1553
1554 mutex_lock(&perf_resource_mutex);
1555 perf_reserved_percpu = val;
1556 for_each_online_cpu(cpu) {
1557 cpuctx = &per_cpu(perf_cpu_context, cpu);
1558 spin_lock_irq(&cpuctx->ctx.lock);
1559 mpt = min(perf_max_counters - cpuctx->ctx.nr_counters,
1560 perf_max_counters - perf_reserved_percpu);
1561 cpuctx->max_pertask = mpt;
1562 spin_unlock_irq(&cpuctx->ctx.lock);
1563 }
1564 mutex_unlock(&perf_resource_mutex);
1565
1566 return count;
1567}
1568
1569static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
1570{
1571 return sprintf(buf, "%d\n", perf_overcommit);
1572}
1573
1574static ssize_t
1575perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
1576{
1577 unsigned long val;
1578 int err;
1579
1580 err = strict_strtoul(buf, 10, &val);
1581 if (err)
1582 return err;
1583 if (val > 1)
1584 return -EINVAL;
1585
1586 mutex_lock(&perf_resource_mutex);
1587 perf_overcommit = val;
1588 mutex_unlock(&perf_resource_mutex);
1589
1590 return count;
1591}
1592
1593static SYSDEV_CLASS_ATTR(
1594 reserve_percpu,
1595 0644,
1596 perf_show_reserve_percpu,
1597 perf_set_reserve_percpu
1598 );
1599
1600static SYSDEV_CLASS_ATTR(
1601 overcommit,
1602 0644,
1603 perf_show_overcommit,
1604 perf_set_overcommit
1605 );
1606
1607static struct attribute *perfclass_attrs[] = {
1608 &attr_reserve_percpu.attr,
1609 &attr_overcommit.attr,
1610 NULL
1611};
1612
1613static struct attribute_group perfclass_attr_group = {
1614 .attrs = perfclass_attrs,
1615 .name = "perf_counters",
1616};
1617
1618static int __init perf_counter_sysfs_init(void)
1619{
1620 return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
1621 &perfclass_attr_group);
1622}
1623device_initcall(perf_counter_sysfs_init);