blob: 1f81cde0dc43032fcab08a087be844733afca48f [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>
21#include <linux/perf_counter.h>
22
23/*
24 * Each CPU has a list of per CPU counters:
25 */
26DEFINE_PER_CPU(struct perf_cpu_context, perf_cpu_context);
27
28int perf_max_counters __read_mostly;
29static int perf_reserved_percpu __read_mostly;
30static int perf_overcommit __read_mostly = 1;
31
32/*
33 * Mutex for (sysadmin-configurable) counter reservations:
34 */
35static DEFINE_MUTEX(perf_resource_mutex);
36
37/*
38 * Architecture provided APIs - weak aliases:
39 */
Ingo Molnar5c92d122008-12-11 13:21:10 +010040extern __weak const struct hw_perf_counter_ops *
Ingo Molnar621a01e2008-12-11 12:46:46 +010041hw_perf_counter_init(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +010042{
Ingo Molnar621a01e2008-12-11 12:46:46 +010043 return ERR_PTR(-EINVAL);
Thomas Gleixner0793a612008-12-04 20:12:29 +010044}
45
Ingo Molnar01b28382008-12-11 13:45:51 +010046u64 __weak hw_perf_save_disable(void) { return 0; }
Ingo Molnaree060942008-12-13 09:00:03 +010047void __weak hw_perf_restore(u64 ctrl) { }
Ingo Molnar5c92d122008-12-11 13:21:10 +010048void __weak hw_perf_counter_setup(void) { }
Thomas Gleixner0793a612008-12-04 20:12:29 +010049
Ingo Molnar04289bb2008-12-11 08:38:42 +010050static void
51list_add_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
52{
53 struct perf_counter *group_leader = counter->group_leader;
54
55 /*
56 * Depending on whether it is a standalone or sibling counter,
57 * add it straight to the context's counter list, or to the group
58 * leader's sibling list:
59 */
60 if (counter->group_leader == counter)
61 list_add_tail(&counter->list_entry, &ctx->counter_list);
62 else
63 list_add_tail(&counter->list_entry, &group_leader->sibling_list);
64}
65
66static void
67list_del_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
68{
69 struct perf_counter *sibling, *tmp;
70
71 list_del_init(&counter->list_entry);
72
Ingo Molnar04289bb2008-12-11 08:38:42 +010073 /*
74 * If this was a group counter with sibling counters then
75 * upgrade the siblings to singleton counters by adding them
76 * to the context list directly:
77 */
78 list_for_each_entry_safe(sibling, tmp,
79 &counter->sibling_list, list_entry) {
80
81 list_del_init(&sibling->list_entry);
82 list_add_tail(&sibling->list_entry, &ctx->counter_list);
Ingo Molnar04289bb2008-12-11 08:38:42 +010083 sibling->group_leader = sibling;
84 }
85}
86
Thomas Gleixner0793a612008-12-04 20:12:29 +010087/*
88 * Cross CPU call to remove a performance counter
89 *
90 * We disable the counter on the hardware level first. After that we
91 * remove it from the context list.
92 */
Ingo Molnar04289bb2008-12-11 08:38:42 +010093static void __perf_counter_remove_from_context(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +010094{
95 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
96 struct perf_counter *counter = info;
97 struct perf_counter_context *ctx = counter->ctx;
Ingo Molnar9b51f662008-12-12 13:49:45 +010098 unsigned long flags;
Ingo Molnar5c92d122008-12-11 13:21:10 +010099 u64 perf_flags;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100100
101 /*
102 * If this is a task context, we need to check whether it is
103 * the current task context of this cpu. If not it has been
104 * scheduled out before the smp call arrived.
105 */
106 if (ctx->task && cpuctx->task_ctx != ctx)
107 return;
108
Ingo Molnar9b51f662008-12-12 13:49:45 +0100109 spin_lock_irqsave(&ctx->lock, flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100110
Ingo Molnar6a930702008-12-11 15:17:03 +0100111 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
Ingo Molnar621a01e2008-12-11 12:46:46 +0100112 counter->hw_ops->hw_perf_counter_disable(counter);
Ingo Molnar6a930702008-12-11 15:17:03 +0100113 counter->state = PERF_COUNTER_STATE_INACTIVE;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100114 ctx->nr_active--;
115 cpuctx->active_oncpu--;
116 counter->task = NULL;
117 }
118 ctx->nr_counters--;
119
120 /*
121 * Protect the list operation against NMI by disabling the
122 * counters on a global level. NOP for non NMI based counters.
123 */
Ingo Molnar01b28382008-12-11 13:45:51 +0100124 perf_flags = hw_perf_save_disable();
Ingo Molnar04289bb2008-12-11 08:38:42 +0100125 list_del_counter(counter, ctx);
Ingo Molnar01b28382008-12-11 13:45:51 +0100126 hw_perf_restore(perf_flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100127
128 if (!ctx->task) {
129 /*
130 * Allow more per task counters with respect to the
131 * reservation:
132 */
133 cpuctx->max_pertask =
134 min(perf_max_counters - ctx->nr_counters,
135 perf_max_counters - perf_reserved_percpu);
136 }
137
Ingo Molnar9b51f662008-12-12 13:49:45 +0100138 spin_unlock_irqrestore(&ctx->lock, flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100139}
140
141
142/*
143 * Remove the counter from a task's (or a CPU's) list of counters.
144 *
145 * Must be called with counter->mutex held.
146 *
147 * CPU counters are removed with a smp call. For task counters we only
148 * call when the task is on a CPU.
149 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100150static void perf_counter_remove_from_context(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100151{
152 struct perf_counter_context *ctx = counter->ctx;
153 struct task_struct *task = ctx->task;
154
155 if (!task) {
156 /*
157 * Per cpu counters are removed via an smp call and
158 * the removal is always sucessful.
159 */
160 smp_call_function_single(counter->cpu,
Ingo Molnar04289bb2008-12-11 08:38:42 +0100161 __perf_counter_remove_from_context,
Thomas Gleixner0793a612008-12-04 20:12:29 +0100162 counter, 1);
163 return;
164 }
165
166retry:
Ingo Molnar04289bb2008-12-11 08:38:42 +0100167 task_oncpu_function_call(task, __perf_counter_remove_from_context,
Thomas Gleixner0793a612008-12-04 20:12:29 +0100168 counter);
169
170 spin_lock_irq(&ctx->lock);
171 /*
172 * If the context is active we need to retry the smp call.
173 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100174 if (ctx->nr_active && !list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100175 spin_unlock_irq(&ctx->lock);
176 goto retry;
177 }
178
179 /*
180 * The lock prevents that this context is scheduled in so we
Ingo Molnar04289bb2008-12-11 08:38:42 +0100181 * can remove the counter safely, if the call above did not
Thomas Gleixner0793a612008-12-04 20:12:29 +0100182 * succeed.
183 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100184 if (!list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100185 ctx->nr_counters--;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100186 list_del_counter(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100187 counter->task = NULL;
188 }
189 spin_unlock_irq(&ctx->lock);
190}
191
192/*
193 * Cross CPU call to install and enable a preformance counter
194 */
195static void __perf_install_in_context(void *info)
196{
197 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
198 struct perf_counter *counter = info;
199 struct perf_counter_context *ctx = counter->ctx;
200 int cpu = smp_processor_id();
Ingo Molnar9b51f662008-12-12 13:49:45 +0100201 unsigned long flags;
Ingo Molnar5c92d122008-12-11 13:21:10 +0100202 u64 perf_flags;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100203
204 /*
205 * If this is a task context, we need to check whether it is
206 * the current task context of this cpu. If not it has been
207 * scheduled out before the smp call arrived.
208 */
209 if (ctx->task && cpuctx->task_ctx != ctx)
210 return;
211
Ingo Molnar9b51f662008-12-12 13:49:45 +0100212 spin_lock_irqsave(&ctx->lock, flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100213
214 /*
215 * Protect the list operation against NMI by disabling the
216 * counters on a global level. NOP for non NMI based counters.
217 */
Ingo Molnar01b28382008-12-11 13:45:51 +0100218 perf_flags = hw_perf_save_disable();
Ingo Molnar04289bb2008-12-11 08:38:42 +0100219 list_add_counter(counter, ctx);
Ingo Molnar01b28382008-12-11 13:45:51 +0100220 hw_perf_restore(perf_flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100221
222 ctx->nr_counters++;
223
224 if (cpuctx->active_oncpu < perf_max_counters) {
Ingo Molnar6a930702008-12-11 15:17:03 +0100225 counter->state = PERF_COUNTER_STATE_ACTIVE;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100226 counter->oncpu = cpu;
227 ctx->nr_active++;
228 cpuctx->active_oncpu++;
Ingo Molnaree060942008-12-13 09:00:03 +0100229 counter->hw_ops->hw_perf_counter_enable(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100230 }
231
232 if (!ctx->task && cpuctx->max_pertask)
233 cpuctx->max_pertask--;
234
Ingo Molnar9b51f662008-12-12 13:49:45 +0100235 spin_unlock_irqrestore(&ctx->lock, flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100236}
237
238/*
239 * Attach a performance counter to a context
240 *
241 * First we add the counter to the list with the hardware enable bit
242 * in counter->hw_config cleared.
243 *
244 * If the counter is attached to a task which is on a CPU we use a smp
245 * call to enable it in the task context. The task might have been
246 * scheduled away, but we check this in the smp call again.
247 */
248static void
249perf_install_in_context(struct perf_counter_context *ctx,
250 struct perf_counter *counter,
251 int cpu)
252{
253 struct task_struct *task = ctx->task;
254
255 counter->ctx = ctx;
256 if (!task) {
257 /*
258 * Per cpu counters are installed via an smp call and
259 * the install is always sucessful.
260 */
261 smp_call_function_single(cpu, __perf_install_in_context,
262 counter, 1);
263 return;
264 }
265
266 counter->task = task;
267retry:
268 task_oncpu_function_call(task, __perf_install_in_context,
269 counter);
270
271 spin_lock_irq(&ctx->lock);
272 /*
Thomas Gleixner0793a612008-12-04 20:12:29 +0100273 * we need to retry the smp call.
274 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100275 if (ctx->nr_active && list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100276 spin_unlock_irq(&ctx->lock);
277 goto retry;
278 }
279
280 /*
281 * The lock prevents that this context is scheduled in so we
282 * can add the counter safely, if it the call above did not
283 * succeed.
284 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100285 if (list_empty(&counter->list_entry)) {
286 list_add_counter(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100287 ctx->nr_counters++;
288 }
289 spin_unlock_irq(&ctx->lock);
290}
291
Ingo Molnar04289bb2008-12-11 08:38:42 +0100292static void
293counter_sched_out(struct perf_counter *counter,
294 struct perf_cpu_context *cpuctx,
295 struct perf_counter_context *ctx)
296{
Ingo Molnar6a930702008-12-11 15:17:03 +0100297 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
Ingo Molnar04289bb2008-12-11 08:38:42 +0100298 return;
299
Ingo Molnar621a01e2008-12-11 12:46:46 +0100300 counter->hw_ops->hw_perf_counter_disable(counter);
Ingo Molnar6a930702008-12-11 15:17:03 +0100301 counter->state = PERF_COUNTER_STATE_INACTIVE;
302 counter->oncpu = -1;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100303
304 cpuctx->active_oncpu--;
305 ctx->nr_active--;
306}
307
308static void
309group_sched_out(struct perf_counter *group_counter,
310 struct perf_cpu_context *cpuctx,
311 struct perf_counter_context *ctx)
312{
313 struct perf_counter *counter;
314
315 counter_sched_out(group_counter, cpuctx, ctx);
316
317 /*
318 * Schedule out siblings (if any):
319 */
320 list_for_each_entry(counter, &group_counter->sibling_list, list_entry)
321 counter_sched_out(counter, cpuctx, ctx);
322}
323
Thomas Gleixner0793a612008-12-04 20:12:29 +0100324/*
325 * Called from scheduler to remove the counters of the current task,
326 * with interrupts disabled.
327 *
328 * We stop each counter and update the counter value in counter->count.
329 *
330 * This does not protect us against NMI, but hw_perf_counter_disable()
331 * sets the disabled bit in the control field of counter _before_
332 * accessing the counter control register. If a NMI hits, then it will
333 * not restart the counter.
334 */
335void perf_counter_task_sched_out(struct task_struct *task, int cpu)
336{
337 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
338 struct perf_counter_context *ctx = &task->perf_counter_ctx;
339 struct perf_counter *counter;
340
341 if (likely(!cpuctx->task_ctx))
342 return;
343
344 spin_lock(&ctx->lock);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100345 if (ctx->nr_active) {
346 list_for_each_entry(counter, &ctx->counter_list, list_entry)
347 group_sched_out(counter, cpuctx, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100348 }
349 spin_unlock(&ctx->lock);
350 cpuctx->task_ctx = NULL;
351}
352
Ingo Molnar04289bb2008-12-11 08:38:42 +0100353static void
354counter_sched_in(struct perf_counter *counter,
355 struct perf_cpu_context *cpuctx,
356 struct perf_counter_context *ctx,
357 int cpu)
358{
Ingo Molnar6a930702008-12-11 15:17:03 +0100359 if (counter->state == PERF_COUNTER_STATE_OFF)
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100360 return;
361
Ingo Molnar621a01e2008-12-11 12:46:46 +0100362 counter->hw_ops->hw_perf_counter_enable(counter);
Ingo Molnar6a930702008-12-11 15:17:03 +0100363 counter->state = PERF_COUNTER_STATE_ACTIVE;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100364 counter->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */
365
366 cpuctx->active_oncpu++;
367 ctx->nr_active++;
368}
369
370static void
371group_sched_in(struct perf_counter *group_counter,
372 struct perf_cpu_context *cpuctx,
373 struct perf_counter_context *ctx,
374 int cpu)
375{
376 struct perf_counter *counter;
377
378 counter_sched_in(group_counter, cpuctx, ctx, cpu);
379
380 /*
381 * Schedule in siblings as one group (if any):
382 */
383 list_for_each_entry(counter, &group_counter->sibling_list, list_entry)
384 counter_sched_in(counter, cpuctx, ctx, cpu);
385}
386
Thomas Gleixner0793a612008-12-04 20:12:29 +0100387/*
388 * Called from scheduler to add the counters of the current task
389 * with interrupts disabled.
390 *
391 * We restore the counter value and then enable it.
392 *
393 * This does not protect us against NMI, but hw_perf_counter_enable()
394 * sets the enabled bit in the control field of counter _before_
395 * accessing the counter control register. If a NMI hits, then it will
396 * keep the counter running.
397 */
398void perf_counter_task_sched_in(struct task_struct *task, int cpu)
399{
400 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
401 struct perf_counter_context *ctx = &task->perf_counter_ctx;
402 struct perf_counter *counter;
403
404 if (likely(!ctx->nr_counters))
405 return;
406
407 spin_lock(&ctx->lock);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100408 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100409 if (ctx->nr_active == cpuctx->max_pertask)
410 break;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100411
412 /*
413 * Listen to the 'cpu' scheduling filter constraint
414 * of counters:
415 */
Thomas Gleixner0793a612008-12-04 20:12:29 +0100416 if (counter->cpu != -1 && counter->cpu != cpu)
417 continue;
418
Ingo Molnar04289bb2008-12-11 08:38:42 +0100419 group_sched_in(counter, cpuctx, ctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100420 }
421 spin_unlock(&ctx->lock);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100422
Thomas Gleixner0793a612008-12-04 20:12:29 +0100423 cpuctx->task_ctx = ctx;
424}
425
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100426int perf_counter_task_disable(void)
427{
428 struct task_struct *curr = current;
429 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
430 struct perf_counter *counter;
431 u64 perf_flags;
432 int cpu;
433
434 if (likely(!ctx->nr_counters))
435 return 0;
436
437 local_irq_disable();
438 cpu = smp_processor_id();
439
440 perf_counter_task_sched_out(curr, cpu);
441
442 spin_lock(&ctx->lock);
443
444 /*
445 * Disable all the counters:
446 */
447 perf_flags = hw_perf_save_disable();
448
Ingo Molnar9b51f662008-12-12 13:49:45 +0100449 list_for_each_entry(counter, &ctx->counter_list, list_entry)
Ingo Molnar6a930702008-12-11 15:17:03 +0100450 counter->state = PERF_COUNTER_STATE_OFF;
Ingo Molnar9b51f662008-12-12 13:49:45 +0100451
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100452 hw_perf_restore(perf_flags);
453
454 spin_unlock(&ctx->lock);
455
456 local_irq_enable();
457
458 return 0;
459}
460
461int perf_counter_task_enable(void)
462{
463 struct task_struct *curr = current;
464 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
465 struct perf_counter *counter;
466 u64 perf_flags;
467 int cpu;
468
469 if (likely(!ctx->nr_counters))
470 return 0;
471
472 local_irq_disable();
473 cpu = smp_processor_id();
474
475 spin_lock(&ctx->lock);
476
477 /*
478 * Disable all the counters:
479 */
480 perf_flags = hw_perf_save_disable();
481
482 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Ingo Molnar6a930702008-12-11 15:17:03 +0100483 if (counter->state != PERF_COUNTER_STATE_OFF)
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100484 continue;
Ingo Molnar6a930702008-12-11 15:17:03 +0100485 counter->state = PERF_COUNTER_STATE_INACTIVE;
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100486 }
487 hw_perf_restore(perf_flags);
488
489 spin_unlock(&ctx->lock);
490
491 perf_counter_task_sched_in(curr, cpu);
492
493 local_irq_enable();
494
495 return 0;
496}
497
Thomas Gleixner0793a612008-12-04 20:12:29 +0100498void perf_counter_task_tick(struct task_struct *curr, int cpu)
499{
500 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
501 struct perf_counter *counter;
Ingo Molnar5c92d122008-12-11 13:21:10 +0100502 u64 perf_flags;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100503
504 if (likely(!ctx->nr_counters))
505 return;
506
507 perf_counter_task_sched_out(curr, cpu);
508
509 spin_lock(&ctx->lock);
510
511 /*
Ingo Molnar04289bb2008-12-11 08:38:42 +0100512 * Rotate the first entry last (works just fine for group counters too):
Thomas Gleixner0793a612008-12-04 20:12:29 +0100513 */
Ingo Molnar01b28382008-12-11 13:45:51 +0100514 perf_flags = hw_perf_save_disable();
Ingo Molnar04289bb2008-12-11 08:38:42 +0100515 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
516 list_del(&counter->list_entry);
517 list_add_tail(&counter->list_entry, &ctx->counter_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100518 break;
519 }
Ingo Molnar01b28382008-12-11 13:45:51 +0100520 hw_perf_restore(perf_flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100521
522 spin_unlock(&ctx->lock);
523
524 perf_counter_task_sched_in(curr, cpu);
525}
526
527/*
Thomas Gleixner0793a612008-12-04 20:12:29 +0100528 * Cross CPU call to read the hardware counter
529 */
530static void __hw_perf_counter_read(void *info)
531{
Ingo Molnar621a01e2008-12-11 12:46:46 +0100532 struct perf_counter *counter = info;
533
534 counter->hw_ops->hw_perf_counter_read(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100535}
536
Ingo Molnar04289bb2008-12-11 08:38:42 +0100537static u64 perf_counter_read(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100538{
539 /*
540 * If counter is enabled and currently active on a CPU, update the
541 * value in the counter structure:
542 */
Ingo Molnar6a930702008-12-11 15:17:03 +0100543 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100544 smp_call_function_single(counter->oncpu,
545 __hw_perf_counter_read, counter, 1);
546 }
547
Ingo Molnaree060942008-12-13 09:00:03 +0100548 return atomic64_read(&counter->count);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100549}
550
551/*
552 * Cross CPU call to switch performance data pointers
553 */
554static void __perf_switch_irq_data(void *info)
555{
556 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
557 struct perf_counter *counter = info;
558 struct perf_counter_context *ctx = counter->ctx;
559 struct perf_data *oldirqdata = counter->irqdata;
560
561 /*
562 * If this is a task context, we need to check whether it is
563 * the current task context of this cpu. If not it has been
564 * scheduled out before the smp call arrived.
565 */
566 if (ctx->task) {
567 if (cpuctx->task_ctx != ctx)
568 return;
569 spin_lock(&ctx->lock);
570 }
571
572 /* Change the pointer NMI safe */
573 atomic_long_set((atomic_long_t *)&counter->irqdata,
574 (unsigned long) counter->usrdata);
575 counter->usrdata = oldirqdata;
576
577 if (ctx->task)
578 spin_unlock(&ctx->lock);
579}
580
581static struct perf_data *perf_switch_irq_data(struct perf_counter *counter)
582{
583 struct perf_counter_context *ctx = counter->ctx;
584 struct perf_data *oldirqdata = counter->irqdata;
585 struct task_struct *task = ctx->task;
586
587 if (!task) {
588 smp_call_function_single(counter->cpu,
589 __perf_switch_irq_data,
590 counter, 1);
591 return counter->usrdata;
592 }
593
594retry:
595 spin_lock_irq(&ctx->lock);
Ingo Molnar6a930702008-12-11 15:17:03 +0100596 if (counter->state != PERF_COUNTER_STATE_ACTIVE) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100597 counter->irqdata = counter->usrdata;
598 counter->usrdata = oldirqdata;
599 spin_unlock_irq(&ctx->lock);
600 return oldirqdata;
601 }
602 spin_unlock_irq(&ctx->lock);
603 task_oncpu_function_call(task, __perf_switch_irq_data, counter);
604 /* Might have failed, because task was scheduled out */
605 if (counter->irqdata == oldirqdata)
606 goto retry;
607
608 return counter->usrdata;
609}
610
611static void put_context(struct perf_counter_context *ctx)
612{
613 if (ctx->task)
614 put_task_struct(ctx->task);
615}
616
617static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
618{
619 struct perf_cpu_context *cpuctx;
620 struct perf_counter_context *ctx;
621 struct task_struct *task;
622
623 /*
624 * If cpu is not a wildcard then this is a percpu counter:
625 */
626 if (cpu != -1) {
627 /* Must be root to operate on a CPU counter: */
628 if (!capable(CAP_SYS_ADMIN))
629 return ERR_PTR(-EACCES);
630
631 if (cpu < 0 || cpu > num_possible_cpus())
632 return ERR_PTR(-EINVAL);
633
634 /*
635 * We could be clever and allow to attach a counter to an
636 * offline CPU and activate it when the CPU comes up, but
637 * that's for later.
638 */
639 if (!cpu_isset(cpu, cpu_online_map))
640 return ERR_PTR(-ENODEV);
641
642 cpuctx = &per_cpu(perf_cpu_context, cpu);
643 ctx = &cpuctx->ctx;
644
Thomas Gleixner0793a612008-12-04 20:12:29 +0100645 return ctx;
646 }
647
648 rcu_read_lock();
649 if (!pid)
650 task = current;
651 else
652 task = find_task_by_vpid(pid);
653 if (task)
654 get_task_struct(task);
655 rcu_read_unlock();
656
657 if (!task)
658 return ERR_PTR(-ESRCH);
659
660 ctx = &task->perf_counter_ctx;
661 ctx->task = task;
662
663 /* Reuse ptrace permission checks for now. */
664 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
665 put_context(ctx);
666 return ERR_PTR(-EACCES);
667 }
668
669 return ctx;
670}
671
672/*
673 * Called when the last reference to the file is gone.
674 */
675static int perf_release(struct inode *inode, struct file *file)
676{
677 struct perf_counter *counter = file->private_data;
678 struct perf_counter_context *ctx = counter->ctx;
679
680 file->private_data = NULL;
681
682 mutex_lock(&counter->mutex);
683
Ingo Molnar04289bb2008-12-11 08:38:42 +0100684 perf_counter_remove_from_context(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100685 put_context(ctx);
686
687 mutex_unlock(&counter->mutex);
688
689 kfree(counter);
690
691 return 0;
692}
693
694/*
695 * Read the performance counter - simple non blocking version for now
696 */
697static ssize_t
698perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
699{
700 u64 cntval;
701
702 if (count != sizeof(cntval))
703 return -EINVAL;
704
705 mutex_lock(&counter->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100706 cntval = perf_counter_read(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100707 mutex_unlock(&counter->mutex);
708
709 return put_user(cntval, (u64 __user *) buf) ? -EFAULT : sizeof(cntval);
710}
711
712static ssize_t
713perf_copy_usrdata(struct perf_data *usrdata, char __user *buf, size_t count)
714{
715 if (!usrdata->len)
716 return 0;
717
718 count = min(count, (size_t)usrdata->len);
719 if (copy_to_user(buf, usrdata->data + usrdata->rd_idx, count))
720 return -EFAULT;
721
722 /* Adjust the counters */
723 usrdata->len -= count;
724 if (!usrdata->len)
725 usrdata->rd_idx = 0;
726 else
727 usrdata->rd_idx += count;
728
729 return count;
730}
731
732static ssize_t
733perf_read_irq_data(struct perf_counter *counter,
734 char __user *buf,
735 size_t count,
736 int nonblocking)
737{
738 struct perf_data *irqdata, *usrdata;
739 DECLARE_WAITQUEUE(wait, current);
740 ssize_t res;
741
742 irqdata = counter->irqdata;
743 usrdata = counter->usrdata;
744
745 if (usrdata->len + irqdata->len >= count)
746 goto read_pending;
747
748 if (nonblocking)
749 return -EAGAIN;
750
751 spin_lock_irq(&counter->waitq.lock);
752 __add_wait_queue(&counter->waitq, &wait);
753 for (;;) {
754 set_current_state(TASK_INTERRUPTIBLE);
755 if (usrdata->len + irqdata->len >= count)
756 break;
757
758 if (signal_pending(current))
759 break;
760
761 spin_unlock_irq(&counter->waitq.lock);
762 schedule();
763 spin_lock_irq(&counter->waitq.lock);
764 }
765 __remove_wait_queue(&counter->waitq, &wait);
766 __set_current_state(TASK_RUNNING);
767 spin_unlock_irq(&counter->waitq.lock);
768
769 if (usrdata->len + irqdata->len < count)
770 return -ERESTARTSYS;
771read_pending:
772 mutex_lock(&counter->mutex);
773
774 /* Drain pending data first: */
775 res = perf_copy_usrdata(usrdata, buf, count);
776 if (res < 0 || res == count)
777 goto out;
778
779 /* Switch irq buffer: */
780 usrdata = perf_switch_irq_data(counter);
781 if (perf_copy_usrdata(usrdata, buf + res, count - res) < 0) {
782 if (!res)
783 res = -EFAULT;
784 } else {
785 res = count;
786 }
787out:
788 mutex_unlock(&counter->mutex);
789
790 return res;
791}
792
793static ssize_t
794perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
795{
796 struct perf_counter *counter = file->private_data;
797
Ingo Molnar9f66a382008-12-10 12:33:23 +0100798 switch (counter->hw_event.record_type) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100799 case PERF_RECORD_SIMPLE:
800 return perf_read_hw(counter, buf, count);
801
802 case PERF_RECORD_IRQ:
803 case PERF_RECORD_GROUP:
804 return perf_read_irq_data(counter, buf, count,
805 file->f_flags & O_NONBLOCK);
806 }
807 return -EINVAL;
808}
809
810static unsigned int perf_poll(struct file *file, poll_table *wait)
811{
812 struct perf_counter *counter = file->private_data;
813 unsigned int events = 0;
814 unsigned long flags;
815
816 poll_wait(file, &counter->waitq, wait);
817
818 spin_lock_irqsave(&counter->waitq.lock, flags);
819 if (counter->usrdata->len || counter->irqdata->len)
820 events |= POLLIN;
821 spin_unlock_irqrestore(&counter->waitq.lock, flags);
822
823 return events;
824}
825
826static const struct file_operations perf_fops = {
827 .release = perf_release,
828 .read = perf_read,
829 .poll = perf_poll,
830};
831
Ingo Molnar5c92d122008-12-11 13:21:10 +0100832static void cpu_clock_perf_counter_enable(struct perf_counter *counter)
833{
834}
835
836static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
837{
838}
839
840static void cpu_clock_perf_counter_read(struct perf_counter *counter)
841{
842 int cpu = raw_smp_processor_id();
843
Ingo Molnaree060942008-12-13 09:00:03 +0100844 atomic64_set(&counter->count, cpu_clock(cpu));
Ingo Molnar5c92d122008-12-11 13:21:10 +0100845}
846
847static const struct hw_perf_counter_ops perf_ops_cpu_clock = {
848 .hw_perf_counter_enable = cpu_clock_perf_counter_enable,
849 .hw_perf_counter_disable = cpu_clock_perf_counter_disable,
850 .hw_perf_counter_read = cpu_clock_perf_counter_read,
851};
852
Ingo Molnar8cb391e2008-12-14 12:22:31 +0100853static void task_clock_perf_counter_update(struct perf_counter *counter)
Ingo Molnarbae43c92008-12-11 14:03:20 +0100854{
Ingo Molnar8cb391e2008-12-14 12:22:31 +0100855 u64 prev, now;
856 s64 delta;
Ingo Molnarbae43c92008-12-11 14:03:20 +0100857
Ingo Molnar8cb391e2008-12-14 12:22:31 +0100858 prev = atomic64_read(&counter->hw.prev_count);
859 now = current->se.sum_exec_runtime;
860
861 atomic64_set(&counter->hw.prev_count, now);
862
863 delta = now - prev;
864 if (WARN_ON_ONCE(delta < 0))
865 delta = 0;
866
867 atomic64_add(delta, &counter->count);
Ingo Molnarbae43c92008-12-11 14:03:20 +0100868}
869
870static void task_clock_perf_counter_read(struct perf_counter *counter)
871{
Ingo Molnar8cb391e2008-12-14 12:22:31 +0100872 task_clock_perf_counter_update(counter);
873}
874
875static void task_clock_perf_counter_enable(struct perf_counter *counter)
876{
877 atomic64_set(&counter->hw.prev_count, current->se.sum_exec_runtime);
878}
879
880static void task_clock_perf_counter_disable(struct perf_counter *counter)
881{
882 task_clock_perf_counter_update(counter);
Ingo Molnarbae43c92008-12-11 14:03:20 +0100883}
884
885static const struct hw_perf_counter_ops perf_ops_task_clock = {
886 .hw_perf_counter_enable = task_clock_perf_counter_enable,
887 .hw_perf_counter_disable = task_clock_perf_counter_disable,
888 .hw_perf_counter_read = task_clock_perf_counter_read,
889};
890
Ingo Molnar5c92d122008-12-11 13:21:10 +0100891static const struct hw_perf_counter_ops *
892sw_perf_counter_init(struct perf_counter *counter)
893{
894 const struct hw_perf_counter_ops *hw_ops = NULL;
895
896 switch (counter->hw_event.type) {
897 case PERF_COUNT_CPU_CLOCK:
898 hw_ops = &perf_ops_cpu_clock;
899 break;
Ingo Molnarbae43c92008-12-11 14:03:20 +0100900 case PERF_COUNT_TASK_CLOCK:
901 hw_ops = &perf_ops_task_clock;
902 break;
Ingo Molnar5c92d122008-12-11 13:21:10 +0100903 default:
904 break;
905 }
906 return hw_ops;
907}
908
Thomas Gleixner0793a612008-12-04 20:12:29 +0100909/*
910 * Allocate and initialize a counter structure
911 */
912static struct perf_counter *
Ingo Molnar04289bb2008-12-11 08:38:42 +0100913perf_counter_alloc(struct perf_counter_hw_event *hw_event,
914 int cpu,
Ingo Molnar9b51f662008-12-12 13:49:45 +0100915 struct perf_counter *group_leader,
916 gfp_t gfpflags)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100917{
Ingo Molnar5c92d122008-12-11 13:21:10 +0100918 const struct hw_perf_counter_ops *hw_ops;
Ingo Molnar621a01e2008-12-11 12:46:46 +0100919 struct perf_counter *counter;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100920
Ingo Molnar9b51f662008-12-12 13:49:45 +0100921 counter = kzalloc(sizeof(*counter), gfpflags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100922 if (!counter)
923 return NULL;
924
Ingo Molnar04289bb2008-12-11 08:38:42 +0100925 /*
926 * Single counters are their own group leaders, with an
927 * empty sibling list:
928 */
929 if (!group_leader)
930 group_leader = counter;
931
Thomas Gleixner0793a612008-12-04 20:12:29 +0100932 mutex_init(&counter->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100933 INIT_LIST_HEAD(&counter->list_entry);
934 INIT_LIST_HEAD(&counter->sibling_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100935 init_waitqueue_head(&counter->waitq);
936
Ingo Molnar9f66a382008-12-10 12:33:23 +0100937 counter->irqdata = &counter->data[0];
938 counter->usrdata = &counter->data[1];
939 counter->cpu = cpu;
940 counter->hw_event = *hw_event;
941 counter->wakeup_pending = 0;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100942 counter->group_leader = group_leader;
Ingo Molnar621a01e2008-12-11 12:46:46 +0100943 counter->hw_ops = NULL;
944
Ingo Molnar5c92d122008-12-11 13:21:10 +0100945 hw_ops = NULL;
946 if (!hw_event->raw && hw_event->type < 0)
947 hw_ops = sw_perf_counter_init(counter);
Ingo Molnar9b51f662008-12-12 13:49:45 +0100948 if (!hw_ops)
Ingo Molnar5c92d122008-12-11 13:21:10 +0100949 hw_ops = hw_perf_counter_init(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +0100950
Ingo Molnar621a01e2008-12-11 12:46:46 +0100951 if (!hw_ops) {
952 kfree(counter);
953 return NULL;
954 }
955 counter->hw_ops = hw_ops;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100956
957 return counter;
958}
959
960/**
Ingo Molnar9f66a382008-12-10 12:33:23 +0100961 * sys_perf_task_open - open a performance counter, associate it to a task/cpu
962 *
963 * @hw_event_uptr: event type attributes for monitoring/sampling
Thomas Gleixner0793a612008-12-04 20:12:29 +0100964 * @pid: target pid
Ingo Molnar9f66a382008-12-10 12:33:23 +0100965 * @cpu: target cpu
966 * @group_fd: group leader counter fd
Thomas Gleixner0793a612008-12-04 20:12:29 +0100967 */
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100968asmlinkage int
969sys_perf_counter_open(struct perf_counter_hw_event *hw_event_uptr __user,
970 pid_t pid, int cpu, int group_fd)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100971{
Ingo Molnar04289bb2008-12-11 08:38:42 +0100972 struct perf_counter *counter, *group_leader;
Ingo Molnar9f66a382008-12-10 12:33:23 +0100973 struct perf_counter_hw_event hw_event;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100974 struct perf_counter_context *ctx;
Ingo Molnar9b51f662008-12-12 13:49:45 +0100975 struct file *counter_file = NULL;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100976 struct file *group_file = NULL;
977 int fput_needed = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +0100978 int fput_needed2 = 0;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100979 int ret;
980
Ingo Molnar9f66a382008-12-10 12:33:23 +0100981 if (copy_from_user(&hw_event, hw_event_uptr, sizeof(hw_event)) != 0)
Thomas Gleixnereab656a2008-12-08 19:26:59 +0100982 return -EFAULT;
983
Ingo Molnar04289bb2008-12-11 08:38:42 +0100984 /*
Ingo Molnarccff2862008-12-11 11:26:29 +0100985 * Get the target context (task or percpu):
986 */
987 ctx = find_get_context(pid, cpu);
988 if (IS_ERR(ctx))
989 return PTR_ERR(ctx);
990
991 /*
992 * Look up the group leader (we will attach this counter to it):
Ingo Molnar04289bb2008-12-11 08:38:42 +0100993 */
994 group_leader = NULL;
995 if (group_fd != -1) {
996 ret = -EINVAL;
997 group_file = fget_light(group_fd, &fput_needed);
998 if (!group_file)
Ingo Molnarccff2862008-12-11 11:26:29 +0100999 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01001000 if (group_file->f_op != &perf_fops)
Ingo Molnarccff2862008-12-11 11:26:29 +01001001 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01001002
1003 group_leader = group_file->private_data;
1004 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01001005 * Do not allow a recursive hierarchy (this new sibling
1006 * becoming part of another group-sibling):
Ingo Molnar04289bb2008-12-11 08:38:42 +01001007 */
Ingo Molnarccff2862008-12-11 11:26:29 +01001008 if (group_leader->group_leader != group_leader)
1009 goto err_put_context;
1010 /*
1011 * Do not allow to attach to a group in a different
1012 * task or CPU context:
1013 */
1014 if (group_leader->ctx != ctx)
1015 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01001016 }
1017
Ingo Molnar5c92d122008-12-11 13:21:10 +01001018 ret = -EINVAL;
Ingo Molnar9b51f662008-12-12 13:49:45 +01001019 counter = perf_counter_alloc(&hw_event, cpu, group_leader, GFP_KERNEL);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001020 if (!counter)
1021 goto err_put_context;
1022
Thomas Gleixner0793a612008-12-04 20:12:29 +01001023 ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
1024 if (ret < 0)
Ingo Molnar9b51f662008-12-12 13:49:45 +01001025 goto err_free_put_context;
1026
1027 counter_file = fget_light(ret, &fput_needed2);
1028 if (!counter_file)
1029 goto err_free_put_context;
1030
1031 counter->filp = counter_file;
1032 perf_install_in_context(ctx, counter, cpu);
1033
1034 fput_light(counter_file, fput_needed2);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001035
Ingo Molnar04289bb2008-12-11 08:38:42 +01001036out_fput:
1037 fput_light(group_file, fput_needed);
1038
Thomas Gleixner0793a612008-12-04 20:12:29 +01001039 return ret;
1040
Ingo Molnar9b51f662008-12-12 13:49:45 +01001041err_free_put_context:
Thomas Gleixner0793a612008-12-04 20:12:29 +01001042 kfree(counter);
1043
1044err_put_context:
1045 put_context(ctx);
1046
Ingo Molnar04289bb2008-12-11 08:38:42 +01001047 goto out_fput;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001048}
1049
Ingo Molnar9b51f662008-12-12 13:49:45 +01001050/*
1051 * Initialize the perf_counter context in a task_struct:
1052 */
1053static void
1054__perf_counter_init_context(struct perf_counter_context *ctx,
1055 struct task_struct *task)
1056{
1057 memset(ctx, 0, sizeof(*ctx));
1058 spin_lock_init(&ctx->lock);
1059 INIT_LIST_HEAD(&ctx->counter_list);
1060 ctx->task = task;
1061}
1062
1063/*
1064 * inherit a counter from parent task to child task:
1065 */
1066static int
1067inherit_counter(struct perf_counter *parent_counter,
1068 struct task_struct *parent,
1069 struct perf_counter_context *parent_ctx,
1070 struct task_struct *child,
1071 struct perf_counter_context *child_ctx)
1072{
1073 struct perf_counter *child_counter;
1074
1075 child_counter = perf_counter_alloc(&parent_counter->hw_event,
1076 parent_counter->cpu, NULL,
1077 GFP_ATOMIC);
1078 if (!child_counter)
1079 return -ENOMEM;
1080
1081 /*
1082 * Link it up in the child's context:
1083 */
1084 child_counter->ctx = child_ctx;
1085 child_counter->task = child;
1086 list_add_counter(child_counter, child_ctx);
1087 child_ctx->nr_counters++;
1088
1089 child_counter->parent = parent_counter;
1090 parent_counter->nr_inherited++;
1091 /*
1092 * inherit into child's child as well:
1093 */
1094 child_counter->hw_event.inherit = 1;
1095
1096 /*
1097 * Get a reference to the parent filp - we will fput it
1098 * when the child counter exits. This is safe to do because
1099 * we are in the parent and we know that the filp still
1100 * exists and has a nonzero count:
1101 */
1102 atomic_long_inc(&parent_counter->filp->f_count);
1103
1104 return 0;
1105}
1106
1107static void
1108__perf_counter_exit_task(struct task_struct *child,
1109 struct perf_counter *child_counter,
1110 struct perf_counter_context *child_ctx)
1111{
1112 struct perf_counter *parent_counter;
1113 u64 parent_val, child_val;
1114 u64 perf_flags;
1115
1116 /*
1117 * Disable and unlink this counter.
1118 *
1119 * Be careful about zapping the list - IRQ/NMI context
1120 * could still be processing it:
1121 */
1122 local_irq_disable();
1123 perf_flags = hw_perf_save_disable();
1124
1125 if (child_counter->state == PERF_COUNTER_STATE_ACTIVE)
1126 child_counter->hw_ops->hw_perf_counter_disable(child_counter);
1127 list_del_init(&child_counter->list_entry);
1128
1129 hw_perf_restore(perf_flags);
1130 local_irq_enable();
1131
1132 parent_counter = child_counter->parent;
1133 /*
1134 * It can happen that parent exits first, and has counters
1135 * that are still around due to the child reference. These
1136 * counters need to be zapped - but otherwise linger.
1137 */
1138 if (!parent_counter)
1139 return;
1140
1141 parent_val = atomic64_read(&parent_counter->count);
1142 child_val = atomic64_read(&child_counter->count);
1143
1144 /*
1145 * Add back the child's count to the parent's count:
1146 */
1147 atomic64_add(child_val, &parent_counter->count);
1148
1149 fput(parent_counter->filp);
1150
1151 kfree(child_counter);
1152}
1153
1154/*
1155 * When a child task exist, feed back counter values to parent counters.
1156 *
1157 * Note: we are running in child context, but the PID is not hashed
1158 * anymore so new counters will not be added.
1159 */
1160void perf_counter_exit_task(struct task_struct *child)
1161{
1162 struct perf_counter *child_counter, *tmp;
1163 struct perf_counter_context *child_ctx;
1164
1165 child_ctx = &child->perf_counter_ctx;
1166
1167 if (likely(!child_ctx->nr_counters))
1168 return;
1169
1170 list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list,
1171 list_entry)
1172 __perf_counter_exit_task(child, child_counter, child_ctx);
1173}
1174
1175/*
1176 * Initialize the perf_counter context in task_struct
1177 */
1178void perf_counter_init_task(struct task_struct *child)
1179{
1180 struct perf_counter_context *child_ctx, *parent_ctx;
1181 struct perf_counter *counter, *parent_counter;
1182 struct task_struct *parent = current;
1183 unsigned long flags;
1184
1185 child_ctx = &child->perf_counter_ctx;
1186 parent_ctx = &parent->perf_counter_ctx;
1187
1188 __perf_counter_init_context(child_ctx, child);
1189
1190 /*
1191 * This is executed from the parent task context, so inherit
1192 * counters that have been marked for cloning:
1193 */
1194
1195 if (likely(!parent_ctx->nr_counters))
1196 return;
1197
1198 /*
1199 * Lock the parent list. No need to lock the child - not PID
1200 * hashed yet and not running, so nobody can access it.
1201 */
1202 spin_lock_irqsave(&parent_ctx->lock, flags);
1203
1204 /*
1205 * We dont have to disable NMIs - we are only looking at
1206 * the list, not manipulating it:
1207 */
1208 list_for_each_entry(counter, &parent_ctx->counter_list, list_entry) {
1209 if (!counter->hw_event.inherit || counter->group_leader != counter)
1210 continue;
1211
1212 /*
1213 * Instead of creating recursive hierarchies of counters,
1214 * we link inheritd counters back to the original parent,
1215 * which has a filp for sure, which we use as the reference
1216 * count:
1217 */
1218 parent_counter = counter;
1219 if (counter->parent)
1220 parent_counter = counter->parent;
1221
1222 if (inherit_counter(parent_counter, parent,
1223 parent_ctx, child, child_ctx))
1224 break;
1225 }
1226
1227 spin_unlock_irqrestore(&parent_ctx->lock, flags);
1228}
1229
Ingo Molnar04289bb2008-12-11 08:38:42 +01001230static void __cpuinit perf_counter_init_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001231{
Ingo Molnar04289bb2008-12-11 08:38:42 +01001232 struct perf_cpu_context *cpuctx;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001233
Ingo Molnar04289bb2008-12-11 08:38:42 +01001234 cpuctx = &per_cpu(perf_cpu_context, cpu);
1235 __perf_counter_init_context(&cpuctx->ctx, NULL);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001236
1237 mutex_lock(&perf_resource_mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01001238 cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001239 mutex_unlock(&perf_resource_mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01001240
Thomas Gleixner0793a612008-12-04 20:12:29 +01001241 hw_perf_counter_setup();
1242}
1243
1244#ifdef CONFIG_HOTPLUG_CPU
Ingo Molnar04289bb2008-12-11 08:38:42 +01001245static void __perf_counter_exit_cpu(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001246{
1247 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1248 struct perf_counter_context *ctx = &cpuctx->ctx;
1249 struct perf_counter *counter, *tmp;
1250
Ingo Molnar04289bb2008-12-11 08:38:42 +01001251 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry)
1252 __perf_counter_remove_from_context(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001253
1254}
Ingo Molnar04289bb2008-12-11 08:38:42 +01001255static void perf_counter_exit_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001256{
Ingo Molnar04289bb2008-12-11 08:38:42 +01001257 smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001258}
1259#else
Ingo Molnar04289bb2008-12-11 08:38:42 +01001260static inline void perf_counter_exit_cpu(int cpu) { }
Thomas Gleixner0793a612008-12-04 20:12:29 +01001261#endif
1262
1263static int __cpuinit
1264perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
1265{
1266 unsigned int cpu = (long)hcpu;
1267
1268 switch (action) {
1269
1270 case CPU_UP_PREPARE:
1271 case CPU_UP_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01001272 perf_counter_init_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001273 break;
1274
1275 case CPU_DOWN_PREPARE:
1276 case CPU_DOWN_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01001277 perf_counter_exit_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001278 break;
1279
1280 default:
1281 break;
1282 }
1283
1284 return NOTIFY_OK;
1285}
1286
1287static struct notifier_block __cpuinitdata perf_cpu_nb = {
1288 .notifier_call = perf_cpu_notify,
1289};
1290
1291static int __init perf_counter_init(void)
1292{
1293 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
1294 (void *)(long)smp_processor_id());
1295 register_cpu_notifier(&perf_cpu_nb);
1296
1297 return 0;
1298}
1299early_initcall(perf_counter_init);
1300
1301static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
1302{
1303 return sprintf(buf, "%d\n", perf_reserved_percpu);
1304}
1305
1306static ssize_t
1307perf_set_reserve_percpu(struct sysdev_class *class,
1308 const char *buf,
1309 size_t count)
1310{
1311 struct perf_cpu_context *cpuctx;
1312 unsigned long val;
1313 int err, cpu, mpt;
1314
1315 err = strict_strtoul(buf, 10, &val);
1316 if (err)
1317 return err;
1318 if (val > perf_max_counters)
1319 return -EINVAL;
1320
1321 mutex_lock(&perf_resource_mutex);
1322 perf_reserved_percpu = val;
1323 for_each_online_cpu(cpu) {
1324 cpuctx = &per_cpu(perf_cpu_context, cpu);
1325 spin_lock_irq(&cpuctx->ctx.lock);
1326 mpt = min(perf_max_counters - cpuctx->ctx.nr_counters,
1327 perf_max_counters - perf_reserved_percpu);
1328 cpuctx->max_pertask = mpt;
1329 spin_unlock_irq(&cpuctx->ctx.lock);
1330 }
1331 mutex_unlock(&perf_resource_mutex);
1332
1333 return count;
1334}
1335
1336static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
1337{
1338 return sprintf(buf, "%d\n", perf_overcommit);
1339}
1340
1341static ssize_t
1342perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
1343{
1344 unsigned long val;
1345 int err;
1346
1347 err = strict_strtoul(buf, 10, &val);
1348 if (err)
1349 return err;
1350 if (val > 1)
1351 return -EINVAL;
1352
1353 mutex_lock(&perf_resource_mutex);
1354 perf_overcommit = val;
1355 mutex_unlock(&perf_resource_mutex);
1356
1357 return count;
1358}
1359
1360static SYSDEV_CLASS_ATTR(
1361 reserve_percpu,
1362 0644,
1363 perf_show_reserve_percpu,
1364 perf_set_reserve_percpu
1365 );
1366
1367static SYSDEV_CLASS_ATTR(
1368 overcommit,
1369 0644,
1370 perf_show_overcommit,
1371 perf_set_overcommit
1372 );
1373
1374static struct attribute *perfclass_attrs[] = {
1375 &attr_reserve_percpu.attr,
1376 &attr_overcommit.attr,
1377 NULL
1378};
1379
1380static struct attribute_group perfclass_attr_group = {
1381 .attrs = perfclass_attrs,
1382 .name = "perf_counters",
1383};
1384
1385static int __init perf_counter_sysfs_init(void)
1386{
1387 return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
1388 &perfclass_attr_group);
1389}
1390device_initcall(perf_counter_sysfs_init);
1391