blob: 4c0dccb756adf08536957c38c1e8c33ea0e9e440 [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{
Paul Mackerrasff6f0542009-01-09 16:19:25 +110044 return NULL;
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; }
Yinghai Lu01ea1cc2008-12-26 21:05:06 -080048void __weak hw_perf_restore(u64 ctrl) { barrier(); }
49void __weak hw_perf_counter_setup(void) { barrier(); }
Paul Mackerras3cbed422009-01-09 16:43:42 +110050int __weak hw_perf_group_sched_in(struct perf_counter *group_leader,
51 struct perf_cpu_context *cpuctx,
52 struct perf_counter_context *ctx, int cpu)
53{
54 return 0;
55}
Thomas Gleixner0793a612008-12-04 20:12:29 +010056
Paul Mackerras4eb96fc2009-01-09 17:24:34 +110057void __weak perf_counter_print_debug(void) { }
58
Ingo Molnar04289bb2008-12-11 08:38:42 +010059static void
60list_add_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
61{
62 struct perf_counter *group_leader = counter->group_leader;
63
64 /*
65 * Depending on whether it is a standalone or sibling counter,
66 * add it straight to the context's counter list, or to the group
67 * leader's sibling list:
68 */
69 if (counter->group_leader == counter)
70 list_add_tail(&counter->list_entry, &ctx->counter_list);
71 else
72 list_add_tail(&counter->list_entry, &group_leader->sibling_list);
73}
74
75static void
76list_del_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
77{
78 struct perf_counter *sibling, *tmp;
79
80 list_del_init(&counter->list_entry);
81
Ingo Molnar04289bb2008-12-11 08:38:42 +010082 /*
83 * If this was a group counter with sibling counters then
84 * upgrade the siblings to singleton counters by adding them
85 * to the context list directly:
86 */
87 list_for_each_entry_safe(sibling, tmp,
88 &counter->sibling_list, list_entry) {
89
90 list_del_init(&sibling->list_entry);
91 list_add_tail(&sibling->list_entry, &ctx->counter_list);
Ingo Molnar04289bb2008-12-11 08:38:42 +010092 sibling->group_leader = sibling;
93 }
94}
95
Thomas Gleixner0793a612008-12-04 20:12:29 +010096/*
97 * Cross CPU call to remove a performance counter
98 *
99 * We disable the counter on the hardware level first. After that we
100 * remove it from the context list.
101 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100102static void __perf_counter_remove_from_context(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100103{
104 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
105 struct perf_counter *counter = info;
106 struct perf_counter_context *ctx = counter->ctx;
Ingo Molnar9b51f662008-12-12 13:49:45 +0100107 unsigned long flags;
Ingo Molnar5c92d122008-12-11 13:21:10 +0100108 u64 perf_flags;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100109
110 /*
111 * If this is a task context, we need to check whether it is
112 * the current task context of this cpu. If not it has been
113 * scheduled out before the smp call arrived.
114 */
115 if (ctx->task && cpuctx->task_ctx != ctx)
116 return;
117
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100118 curr_rq_lock_irq_save(&flags);
119 spin_lock(&ctx->lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100120
Ingo Molnar6a930702008-12-11 15:17:03 +0100121 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
Ingo Molnar6a930702008-12-11 15:17:03 +0100122 counter->state = PERF_COUNTER_STATE_INACTIVE;
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100123 counter->hw_ops->disable(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100124 ctx->nr_active--;
125 cpuctx->active_oncpu--;
126 counter->task = NULL;
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100127 counter->oncpu = -1;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100128 }
129 ctx->nr_counters--;
130
131 /*
132 * Protect the list operation against NMI by disabling the
133 * counters on a global level. NOP for non NMI based counters.
134 */
Ingo Molnar01b28382008-12-11 13:45:51 +0100135 perf_flags = hw_perf_save_disable();
Ingo Molnar04289bb2008-12-11 08:38:42 +0100136 list_del_counter(counter, ctx);
Ingo Molnar01b28382008-12-11 13:45:51 +0100137 hw_perf_restore(perf_flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100138
139 if (!ctx->task) {
140 /*
141 * Allow more per task counters with respect to the
142 * reservation:
143 */
144 cpuctx->max_pertask =
145 min(perf_max_counters - ctx->nr_counters,
146 perf_max_counters - perf_reserved_percpu);
147 }
148
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100149 spin_unlock(&ctx->lock);
150 curr_rq_unlock_irq_restore(&flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100151}
152
153
154/*
155 * Remove the counter from a task's (or a CPU's) list of counters.
156 *
157 * Must be called with counter->mutex held.
158 *
159 * CPU counters are removed with a smp call. For task counters we only
160 * call when the task is on a CPU.
161 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100162static void perf_counter_remove_from_context(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100163{
164 struct perf_counter_context *ctx = counter->ctx;
165 struct task_struct *task = ctx->task;
166
167 if (!task) {
168 /*
169 * Per cpu counters are removed via an smp call and
170 * the removal is always sucessful.
171 */
172 smp_call_function_single(counter->cpu,
Ingo Molnar04289bb2008-12-11 08:38:42 +0100173 __perf_counter_remove_from_context,
Thomas Gleixner0793a612008-12-04 20:12:29 +0100174 counter, 1);
175 return;
176 }
177
178retry:
Ingo Molnar04289bb2008-12-11 08:38:42 +0100179 task_oncpu_function_call(task, __perf_counter_remove_from_context,
Thomas Gleixner0793a612008-12-04 20:12:29 +0100180 counter);
181
182 spin_lock_irq(&ctx->lock);
183 /*
184 * If the context is active we need to retry the smp call.
185 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100186 if (ctx->nr_active && !list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100187 spin_unlock_irq(&ctx->lock);
188 goto retry;
189 }
190
191 /*
192 * The lock prevents that this context is scheduled in so we
Ingo Molnar04289bb2008-12-11 08:38:42 +0100193 * can remove the counter safely, if the call above did not
Thomas Gleixner0793a612008-12-04 20:12:29 +0100194 * succeed.
195 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100196 if (!list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100197 ctx->nr_counters--;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100198 list_del_counter(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100199 counter->task = NULL;
200 }
201 spin_unlock_irq(&ctx->lock);
202}
203
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100204static int
205counter_sched_in(struct perf_counter *counter,
206 struct perf_cpu_context *cpuctx,
207 struct perf_counter_context *ctx,
208 int cpu)
209{
210 if (counter->state == PERF_COUNTER_STATE_OFF)
211 return 0;
212
213 counter->state = PERF_COUNTER_STATE_ACTIVE;
214 counter->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */
215 /*
216 * The new state must be visible before we turn it on in the hardware:
217 */
218 smp_wmb();
219
220 if (counter->hw_ops->enable(counter)) {
221 counter->state = PERF_COUNTER_STATE_INACTIVE;
222 counter->oncpu = -1;
223 return -EAGAIN;
224 }
225
226 cpuctx->active_oncpu++;
227 ctx->nr_active++;
228
229 return 0;
230}
231
Thomas Gleixner0793a612008-12-04 20:12:29 +0100232/*
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100233 * Cross CPU call to install and enable a performance counter
Thomas Gleixner0793a612008-12-04 20:12:29 +0100234 */
235static void __perf_install_in_context(void *info)
236{
237 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
238 struct perf_counter *counter = info;
239 struct perf_counter_context *ctx = counter->ctx;
240 int cpu = smp_processor_id();
Ingo Molnar9b51f662008-12-12 13:49:45 +0100241 unsigned long flags;
Ingo Molnar5c92d122008-12-11 13:21:10 +0100242 u64 perf_flags;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100243
244 /*
245 * If this is a task context, we need to check whether it is
246 * the current task context of this cpu. If not it has been
247 * scheduled out before the smp call arrived.
248 */
249 if (ctx->task && cpuctx->task_ctx != ctx)
250 return;
251
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100252 curr_rq_lock_irq_save(&flags);
253 spin_lock(&ctx->lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100254
255 /*
256 * Protect the list operation against NMI by disabling the
257 * counters on a global level. NOP for non NMI based counters.
258 */
Ingo Molnar01b28382008-12-11 13:45:51 +0100259 perf_flags = hw_perf_save_disable();
Thomas Gleixner0793a612008-12-04 20:12:29 +0100260
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100261 list_add_counter(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100262 ctx->nr_counters++;
263
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100264 counter_sched_in(counter, cpuctx, ctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100265
266 if (!ctx->task && cpuctx->max_pertask)
267 cpuctx->max_pertask--;
268
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100269 hw_perf_restore(perf_flags);
270
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100271 spin_unlock(&ctx->lock);
272 curr_rq_unlock_irq_restore(&flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100273}
274
275/*
276 * Attach a performance counter to a context
277 *
278 * First we add the counter to the list with the hardware enable bit
279 * in counter->hw_config cleared.
280 *
281 * If the counter is attached to a task which is on a CPU we use a smp
282 * call to enable it in the task context. The task might have been
283 * scheduled away, but we check this in the smp call again.
284 */
285static void
286perf_install_in_context(struct perf_counter_context *ctx,
287 struct perf_counter *counter,
288 int cpu)
289{
290 struct task_struct *task = ctx->task;
291
292 counter->ctx = ctx;
293 if (!task) {
294 /*
295 * Per cpu counters are installed via an smp call and
296 * the install is always sucessful.
297 */
298 smp_call_function_single(cpu, __perf_install_in_context,
299 counter, 1);
300 return;
301 }
302
303 counter->task = task;
304retry:
305 task_oncpu_function_call(task, __perf_install_in_context,
306 counter);
307
308 spin_lock_irq(&ctx->lock);
309 /*
Thomas Gleixner0793a612008-12-04 20:12:29 +0100310 * we need to retry the smp call.
311 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100312 if (ctx->nr_active && list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100313 spin_unlock_irq(&ctx->lock);
314 goto retry;
315 }
316
317 /*
318 * The lock prevents that this context is scheduled in so we
319 * can add the counter safely, if it the call above did not
320 * succeed.
321 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100322 if (list_empty(&counter->list_entry)) {
323 list_add_counter(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100324 ctx->nr_counters++;
325 }
326 spin_unlock_irq(&ctx->lock);
327}
328
Ingo Molnar04289bb2008-12-11 08:38:42 +0100329static void
330counter_sched_out(struct perf_counter *counter,
331 struct perf_cpu_context *cpuctx,
332 struct perf_counter_context *ctx)
333{
Ingo Molnar6a930702008-12-11 15:17:03 +0100334 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
Ingo Molnar04289bb2008-12-11 08:38:42 +0100335 return;
336
Ingo Molnar6a930702008-12-11 15:17:03 +0100337 counter->state = PERF_COUNTER_STATE_INACTIVE;
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100338 counter->hw_ops->disable(counter);
Ingo Molnar6a930702008-12-11 15:17:03 +0100339 counter->oncpu = -1;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100340
341 cpuctx->active_oncpu--;
342 ctx->nr_active--;
343}
344
345static void
346group_sched_out(struct perf_counter *group_counter,
347 struct perf_cpu_context *cpuctx,
348 struct perf_counter_context *ctx)
349{
350 struct perf_counter *counter;
351
Paul Mackerras3cbed422009-01-09 16:43:42 +1100352 if (group_counter->state != PERF_COUNTER_STATE_ACTIVE)
353 return;
354
Ingo Molnar04289bb2008-12-11 08:38:42 +0100355 counter_sched_out(group_counter, cpuctx, ctx);
356
357 /*
358 * Schedule out siblings (if any):
359 */
360 list_for_each_entry(counter, &group_counter->sibling_list, list_entry)
361 counter_sched_out(counter, cpuctx, ctx);
362}
363
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100364void __perf_counter_sched_out(struct perf_counter_context *ctx,
365 struct perf_cpu_context *cpuctx)
366{
367 struct perf_counter *counter;
Paul Mackerras3cbed422009-01-09 16:43:42 +1100368 u64 flags;
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100369
370 if (likely(!ctx->nr_counters))
371 return;
372
373 spin_lock(&ctx->lock);
Paul Mackerras3cbed422009-01-09 16:43:42 +1100374 flags = hw_perf_save_disable();
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100375 if (ctx->nr_active) {
376 list_for_each_entry(counter, &ctx->counter_list, list_entry)
377 group_sched_out(counter, cpuctx, ctx);
378 }
Paul Mackerras3cbed422009-01-09 16:43:42 +1100379 hw_perf_restore(flags);
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100380 spin_unlock(&ctx->lock);
381}
382
Thomas Gleixner0793a612008-12-04 20:12:29 +0100383/*
384 * Called from scheduler to remove the counters of the current task,
385 * with interrupts disabled.
386 *
387 * We stop each counter and update the counter value in counter->count.
388 *
Ingo Molnar76715812008-12-17 14:20:28 +0100389 * This does not protect us against NMI, but disable()
Thomas Gleixner0793a612008-12-04 20:12:29 +0100390 * sets the disabled bit in the control field of counter _before_
391 * accessing the counter control register. If a NMI hits, then it will
392 * not restart the counter.
393 */
394void perf_counter_task_sched_out(struct task_struct *task, int cpu)
395{
396 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
397 struct perf_counter_context *ctx = &task->perf_counter_ctx;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100398
399 if (likely(!cpuctx->task_ctx))
400 return;
401
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100402 __perf_counter_sched_out(ctx, cpuctx);
403
Thomas Gleixner0793a612008-12-04 20:12:29 +0100404 cpuctx->task_ctx = NULL;
405}
406
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100407static void perf_counter_cpu_sched_out(struct perf_cpu_context *cpuctx)
Ingo Molnar04289bb2008-12-11 08:38:42 +0100408{
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100409 __perf_counter_sched_out(&cpuctx->ctx, cpuctx);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100410}
411
Ingo Molnar79958882008-12-17 08:54:56 +0100412static int
Ingo Molnar04289bb2008-12-11 08:38:42 +0100413group_sched_in(struct perf_counter *group_counter,
414 struct perf_cpu_context *cpuctx,
415 struct perf_counter_context *ctx,
416 int cpu)
417{
Ingo Molnar95cdd2e2008-12-21 13:50:42 +0100418 struct perf_counter *counter, *partial_group;
Paul Mackerras3cbed422009-01-09 16:43:42 +1100419 int ret;
420
421 if (group_counter->state == PERF_COUNTER_STATE_OFF)
422 return 0;
423
424 ret = hw_perf_group_sched_in(group_counter, cpuctx, ctx, cpu);
425 if (ret)
426 return ret < 0 ? ret : 0;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100427
Ingo Molnar95cdd2e2008-12-21 13:50:42 +0100428 if (counter_sched_in(group_counter, cpuctx, ctx, cpu))
429 return -EAGAIN;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100430
431 /*
432 * Schedule in siblings as one group (if any):
433 */
Ingo Molnar79958882008-12-17 08:54:56 +0100434 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
Ingo Molnar95cdd2e2008-12-21 13:50:42 +0100435 if (counter_sched_in(counter, cpuctx, ctx, cpu)) {
436 partial_group = counter;
437 goto group_error;
438 }
Ingo Molnar79958882008-12-17 08:54:56 +0100439 }
440
Paul Mackerras3cbed422009-01-09 16:43:42 +1100441 return 0;
Ingo Molnar95cdd2e2008-12-21 13:50:42 +0100442
443group_error:
444 /*
445 * Groups can be scheduled in as one unit only, so undo any
446 * partial group before returning:
447 */
448 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
449 if (counter == partial_group)
450 break;
451 counter_sched_out(counter, cpuctx, ctx);
452 }
453 counter_sched_out(group_counter, cpuctx, ctx);
454
455 return -EAGAIN;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100456}
457
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100458static void
459__perf_counter_sched_in(struct perf_counter_context *ctx,
460 struct perf_cpu_context *cpuctx, int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100461{
Thomas Gleixner0793a612008-12-04 20:12:29 +0100462 struct perf_counter *counter;
Paul Mackerras3cbed422009-01-09 16:43:42 +1100463 u64 flags;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100464
465 if (likely(!ctx->nr_counters))
466 return;
467
468 spin_lock(&ctx->lock);
Paul Mackerras3cbed422009-01-09 16:43:42 +1100469 flags = hw_perf_save_disable();
Ingo Molnar04289bb2008-12-11 08:38:42 +0100470 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Ingo Molnar04289bb2008-12-11 08:38:42 +0100471 /*
472 * Listen to the 'cpu' scheduling filter constraint
473 * of counters:
474 */
Thomas Gleixner0793a612008-12-04 20:12:29 +0100475 if (counter->cpu != -1 && counter->cpu != cpu)
476 continue;
477
Ingo Molnar79958882008-12-17 08:54:56 +0100478 /*
Paul Mackerras3cbed422009-01-09 16:43:42 +1100479 * If we scheduled in a group atomically and exclusively,
480 * or if this group can't go on, break out:
Ingo Molnar79958882008-12-17 08:54:56 +0100481 */
482 if (group_sched_in(counter, cpuctx, ctx, cpu))
483 break;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100484 }
Paul Mackerras3cbed422009-01-09 16:43:42 +1100485 hw_perf_restore(flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100486 spin_unlock(&ctx->lock);
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100487}
Ingo Molnar04289bb2008-12-11 08:38:42 +0100488
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100489/*
490 * Called from scheduler to add the counters of the current task
491 * with interrupts disabled.
492 *
493 * We restore the counter value and then enable it.
494 *
495 * This does not protect us against NMI, but enable()
496 * sets the enabled bit in the control field of counter _before_
497 * accessing the counter control register. If a NMI hits, then it will
498 * keep the counter running.
499 */
500void perf_counter_task_sched_in(struct task_struct *task, int cpu)
501{
502 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
503 struct perf_counter_context *ctx = &task->perf_counter_ctx;
504
505 __perf_counter_sched_in(ctx, cpuctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100506 cpuctx->task_ctx = ctx;
507}
508
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100509static void perf_counter_cpu_sched_in(struct perf_cpu_context *cpuctx, int cpu)
510{
511 struct perf_counter_context *ctx = &cpuctx->ctx;
512
513 __perf_counter_sched_in(ctx, cpuctx, cpu);
514}
515
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100516int perf_counter_task_disable(void)
517{
518 struct task_struct *curr = current;
519 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
520 struct perf_counter *counter;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100521 unsigned long flags;
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100522 u64 perf_flags;
523 int cpu;
524
525 if (likely(!ctx->nr_counters))
526 return 0;
527
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100528 curr_rq_lock_irq_save(&flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100529 cpu = smp_processor_id();
530
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100531 /* force the update of the task clock: */
532 __task_delta_exec(curr, 1);
533
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100534 perf_counter_task_sched_out(curr, cpu);
535
536 spin_lock(&ctx->lock);
537
538 /*
539 * Disable all the counters:
540 */
541 perf_flags = hw_perf_save_disable();
542
Ingo Molnar9b51f662008-12-12 13:49:45 +0100543 list_for_each_entry(counter, &ctx->counter_list, list_entry)
Ingo Molnar6a930702008-12-11 15:17:03 +0100544 counter->state = PERF_COUNTER_STATE_OFF;
Ingo Molnar9b51f662008-12-12 13:49:45 +0100545
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100546 hw_perf_restore(perf_flags);
547
548 spin_unlock(&ctx->lock);
549
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100550 curr_rq_unlock_irq_restore(&flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100551
552 return 0;
553}
554
555int perf_counter_task_enable(void)
556{
557 struct task_struct *curr = current;
558 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
559 struct perf_counter *counter;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100560 unsigned long flags;
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100561 u64 perf_flags;
562 int cpu;
563
564 if (likely(!ctx->nr_counters))
565 return 0;
566
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100567 curr_rq_lock_irq_save(&flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100568 cpu = smp_processor_id();
569
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100570 /* force the update of the task clock: */
571 __task_delta_exec(curr, 1);
572
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100573 perf_counter_task_sched_out(curr, cpu);
574
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100575 spin_lock(&ctx->lock);
576
577 /*
578 * Disable all the counters:
579 */
580 perf_flags = hw_perf_save_disable();
581
582 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Ingo Molnar6a930702008-12-11 15:17:03 +0100583 if (counter->state != PERF_COUNTER_STATE_OFF)
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100584 continue;
Ingo Molnar6a930702008-12-11 15:17:03 +0100585 counter->state = PERF_COUNTER_STATE_INACTIVE;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100586 counter->hw_event.disabled = 0;
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100587 }
588 hw_perf_restore(perf_flags);
589
590 spin_unlock(&ctx->lock);
591
592 perf_counter_task_sched_in(curr, cpu);
593
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100594 curr_rq_unlock_irq_restore(&flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100595
596 return 0;
597}
598
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100599/*
600 * Round-robin a context's counters:
601 */
602static void rotate_ctx(struct perf_counter_context *ctx)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100603{
Thomas Gleixner0793a612008-12-04 20:12:29 +0100604 struct perf_counter *counter;
Ingo Molnar5c92d122008-12-11 13:21:10 +0100605 u64 perf_flags;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100606
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100607 if (!ctx->nr_counters)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100608 return;
609
Thomas Gleixner0793a612008-12-04 20:12:29 +0100610 spin_lock(&ctx->lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100611 /*
Ingo Molnar04289bb2008-12-11 08:38:42 +0100612 * Rotate the first entry last (works just fine for group counters too):
Thomas Gleixner0793a612008-12-04 20:12:29 +0100613 */
Ingo Molnar01b28382008-12-11 13:45:51 +0100614 perf_flags = hw_perf_save_disable();
Ingo Molnar04289bb2008-12-11 08:38:42 +0100615 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
616 list_del(&counter->list_entry);
617 list_add_tail(&counter->list_entry, &ctx->counter_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100618 break;
619 }
Ingo Molnar01b28382008-12-11 13:45:51 +0100620 hw_perf_restore(perf_flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100621
622 spin_unlock(&ctx->lock);
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100623}
Thomas Gleixner0793a612008-12-04 20:12:29 +0100624
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100625void perf_counter_task_tick(struct task_struct *curr, int cpu)
626{
627 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
628 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
629 const int rotate_percpu = 0;
630
631 if (rotate_percpu)
632 perf_counter_cpu_sched_out(cpuctx);
633 perf_counter_task_sched_out(curr, cpu);
634
635 if (rotate_percpu)
636 rotate_ctx(&cpuctx->ctx);
637 rotate_ctx(ctx);
638
639 if (rotate_percpu)
640 perf_counter_cpu_sched_in(cpuctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100641 perf_counter_task_sched_in(curr, cpu);
642}
643
644/*
Thomas Gleixner0793a612008-12-04 20:12:29 +0100645 * Cross CPU call to read the hardware counter
646 */
Ingo Molnar76715812008-12-17 14:20:28 +0100647static void __read(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100648{
Ingo Molnar621a01e2008-12-11 12:46:46 +0100649 struct perf_counter *counter = info;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100650 unsigned long flags;
Ingo Molnar621a01e2008-12-11 12:46:46 +0100651
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100652 curr_rq_lock_irq_save(&flags);
Ingo Molnar76715812008-12-17 14:20:28 +0100653 counter->hw_ops->read(counter);
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100654 curr_rq_unlock_irq_restore(&flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100655}
656
Ingo Molnar04289bb2008-12-11 08:38:42 +0100657static u64 perf_counter_read(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100658{
659 /*
660 * If counter is enabled and currently active on a CPU, update the
661 * value in the counter structure:
662 */
Ingo Molnar6a930702008-12-11 15:17:03 +0100663 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100664 smp_call_function_single(counter->oncpu,
Ingo Molnar76715812008-12-17 14:20:28 +0100665 __read, counter, 1);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100666 }
667
Ingo Molnaree060942008-12-13 09:00:03 +0100668 return atomic64_read(&counter->count);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100669}
670
671/*
672 * Cross CPU call to switch performance data pointers
673 */
674static void __perf_switch_irq_data(void *info)
675{
676 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
677 struct perf_counter *counter = info;
678 struct perf_counter_context *ctx = counter->ctx;
679 struct perf_data *oldirqdata = counter->irqdata;
680
681 /*
682 * If this is a task context, we need to check whether it is
683 * the current task context of this cpu. If not it has been
684 * scheduled out before the smp call arrived.
685 */
686 if (ctx->task) {
687 if (cpuctx->task_ctx != ctx)
688 return;
689 spin_lock(&ctx->lock);
690 }
691
692 /* Change the pointer NMI safe */
693 atomic_long_set((atomic_long_t *)&counter->irqdata,
694 (unsigned long) counter->usrdata);
695 counter->usrdata = oldirqdata;
696
697 if (ctx->task)
698 spin_unlock(&ctx->lock);
699}
700
701static struct perf_data *perf_switch_irq_data(struct perf_counter *counter)
702{
703 struct perf_counter_context *ctx = counter->ctx;
704 struct perf_data *oldirqdata = counter->irqdata;
705 struct task_struct *task = ctx->task;
706
707 if (!task) {
708 smp_call_function_single(counter->cpu,
709 __perf_switch_irq_data,
710 counter, 1);
711 return counter->usrdata;
712 }
713
714retry:
715 spin_lock_irq(&ctx->lock);
Ingo Molnar6a930702008-12-11 15:17:03 +0100716 if (counter->state != PERF_COUNTER_STATE_ACTIVE) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100717 counter->irqdata = counter->usrdata;
718 counter->usrdata = oldirqdata;
719 spin_unlock_irq(&ctx->lock);
720 return oldirqdata;
721 }
722 spin_unlock_irq(&ctx->lock);
723 task_oncpu_function_call(task, __perf_switch_irq_data, counter);
724 /* Might have failed, because task was scheduled out */
725 if (counter->irqdata == oldirqdata)
726 goto retry;
727
728 return counter->usrdata;
729}
730
731static void put_context(struct perf_counter_context *ctx)
732{
733 if (ctx->task)
734 put_task_struct(ctx->task);
735}
736
737static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
738{
739 struct perf_cpu_context *cpuctx;
740 struct perf_counter_context *ctx;
741 struct task_struct *task;
742
743 /*
744 * If cpu is not a wildcard then this is a percpu counter:
745 */
746 if (cpu != -1) {
747 /* Must be root to operate on a CPU counter: */
748 if (!capable(CAP_SYS_ADMIN))
749 return ERR_PTR(-EACCES);
750
751 if (cpu < 0 || cpu > num_possible_cpus())
752 return ERR_PTR(-EINVAL);
753
754 /*
755 * We could be clever and allow to attach a counter to an
756 * offline CPU and activate it when the CPU comes up, but
757 * that's for later.
758 */
759 if (!cpu_isset(cpu, cpu_online_map))
760 return ERR_PTR(-ENODEV);
761
762 cpuctx = &per_cpu(perf_cpu_context, cpu);
763 ctx = &cpuctx->ctx;
764
Thomas Gleixner0793a612008-12-04 20:12:29 +0100765 return ctx;
766 }
767
768 rcu_read_lock();
769 if (!pid)
770 task = current;
771 else
772 task = find_task_by_vpid(pid);
773 if (task)
774 get_task_struct(task);
775 rcu_read_unlock();
776
777 if (!task)
778 return ERR_PTR(-ESRCH);
779
780 ctx = &task->perf_counter_ctx;
781 ctx->task = task;
782
783 /* Reuse ptrace permission checks for now. */
784 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
785 put_context(ctx);
786 return ERR_PTR(-EACCES);
787 }
788
789 return ctx;
790}
791
792/*
793 * Called when the last reference to the file is gone.
794 */
795static int perf_release(struct inode *inode, struct file *file)
796{
797 struct perf_counter *counter = file->private_data;
798 struct perf_counter_context *ctx = counter->ctx;
799
800 file->private_data = NULL;
801
802 mutex_lock(&counter->mutex);
803
Ingo Molnar04289bb2008-12-11 08:38:42 +0100804 perf_counter_remove_from_context(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100805 put_context(ctx);
806
807 mutex_unlock(&counter->mutex);
808
809 kfree(counter);
810
811 return 0;
812}
813
814/*
815 * Read the performance counter - simple non blocking version for now
816 */
817static ssize_t
818perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
819{
820 u64 cntval;
821
822 if (count != sizeof(cntval))
823 return -EINVAL;
824
825 mutex_lock(&counter->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100826 cntval = perf_counter_read(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100827 mutex_unlock(&counter->mutex);
828
829 return put_user(cntval, (u64 __user *) buf) ? -EFAULT : sizeof(cntval);
830}
831
832static ssize_t
833perf_copy_usrdata(struct perf_data *usrdata, char __user *buf, size_t count)
834{
835 if (!usrdata->len)
836 return 0;
837
838 count = min(count, (size_t)usrdata->len);
839 if (copy_to_user(buf, usrdata->data + usrdata->rd_idx, count))
840 return -EFAULT;
841
842 /* Adjust the counters */
843 usrdata->len -= count;
844 if (!usrdata->len)
845 usrdata->rd_idx = 0;
846 else
847 usrdata->rd_idx += count;
848
849 return count;
850}
851
852static ssize_t
853perf_read_irq_data(struct perf_counter *counter,
854 char __user *buf,
855 size_t count,
856 int nonblocking)
857{
858 struct perf_data *irqdata, *usrdata;
859 DECLARE_WAITQUEUE(wait, current);
860 ssize_t res;
861
862 irqdata = counter->irqdata;
863 usrdata = counter->usrdata;
864
865 if (usrdata->len + irqdata->len >= count)
866 goto read_pending;
867
868 if (nonblocking)
869 return -EAGAIN;
870
871 spin_lock_irq(&counter->waitq.lock);
872 __add_wait_queue(&counter->waitq, &wait);
873 for (;;) {
874 set_current_state(TASK_INTERRUPTIBLE);
875 if (usrdata->len + irqdata->len >= count)
876 break;
877
878 if (signal_pending(current))
879 break;
880
881 spin_unlock_irq(&counter->waitq.lock);
882 schedule();
883 spin_lock_irq(&counter->waitq.lock);
884 }
885 __remove_wait_queue(&counter->waitq, &wait);
886 __set_current_state(TASK_RUNNING);
887 spin_unlock_irq(&counter->waitq.lock);
888
889 if (usrdata->len + irqdata->len < count)
890 return -ERESTARTSYS;
891read_pending:
892 mutex_lock(&counter->mutex);
893
894 /* Drain pending data first: */
895 res = perf_copy_usrdata(usrdata, buf, count);
896 if (res < 0 || res == count)
897 goto out;
898
899 /* Switch irq buffer: */
900 usrdata = perf_switch_irq_data(counter);
901 if (perf_copy_usrdata(usrdata, buf + res, count - res) < 0) {
902 if (!res)
903 res = -EFAULT;
904 } else {
905 res = count;
906 }
907out:
908 mutex_unlock(&counter->mutex);
909
910 return res;
911}
912
913static ssize_t
914perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
915{
916 struct perf_counter *counter = file->private_data;
917
Ingo Molnar9f66a382008-12-10 12:33:23 +0100918 switch (counter->hw_event.record_type) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100919 case PERF_RECORD_SIMPLE:
920 return perf_read_hw(counter, buf, count);
921
922 case PERF_RECORD_IRQ:
923 case PERF_RECORD_GROUP:
924 return perf_read_irq_data(counter, buf, count,
925 file->f_flags & O_NONBLOCK);
926 }
927 return -EINVAL;
928}
929
930static unsigned int perf_poll(struct file *file, poll_table *wait)
931{
932 struct perf_counter *counter = file->private_data;
933 unsigned int events = 0;
934 unsigned long flags;
935
936 poll_wait(file, &counter->waitq, wait);
937
938 spin_lock_irqsave(&counter->waitq.lock, flags);
939 if (counter->usrdata->len || counter->irqdata->len)
940 events |= POLLIN;
941 spin_unlock_irqrestore(&counter->waitq.lock, flags);
942
943 return events;
944}
945
946static const struct file_operations perf_fops = {
947 .release = perf_release,
948 .read = perf_read,
949 .poll = perf_poll,
950};
951
Ingo Molnar95cdd2e2008-12-21 13:50:42 +0100952static int cpu_clock_perf_counter_enable(struct perf_counter *counter)
Ingo Molnar5c92d122008-12-11 13:21:10 +0100953{
Paul Mackerras9abf8a02009-01-09 16:26:43 +1100954 int cpu = raw_smp_processor_id();
955
956 atomic64_set(&counter->hw.prev_count, cpu_clock(cpu));
Ingo Molnar95cdd2e2008-12-21 13:50:42 +0100957 return 0;
Ingo Molnar5c92d122008-12-11 13:21:10 +0100958}
959
Paul Mackerras9abf8a02009-01-09 16:26:43 +1100960static void cpu_clock_perf_counter_update(struct perf_counter *counter)
961{
962 int cpu = raw_smp_processor_id();
963 s64 prev;
964 u64 now;
965
966 now = cpu_clock(cpu);
967 prev = atomic64_read(&counter->hw.prev_count);
968 atomic64_set(&counter->hw.prev_count, now);
969 atomic64_add(now - prev, &counter->count);
970}
971
Ingo Molnar5c92d122008-12-11 13:21:10 +0100972static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
973{
Paul Mackerras9abf8a02009-01-09 16:26:43 +1100974 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +0100975}
976
977static void cpu_clock_perf_counter_read(struct perf_counter *counter)
978{
Paul Mackerras9abf8a02009-01-09 16:26:43 +1100979 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +0100980}
981
982static const struct hw_perf_counter_ops perf_ops_cpu_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +0100983 .enable = cpu_clock_perf_counter_enable,
984 .disable = cpu_clock_perf_counter_disable,
985 .read = cpu_clock_perf_counter_read,
Ingo Molnar5c92d122008-12-11 13:21:10 +0100986};
987
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100988/*
989 * Called from within the scheduler:
990 */
991static u64 task_clock_perf_counter_val(struct perf_counter *counter, int update)
Ingo Molnarbae43c92008-12-11 14:03:20 +0100992{
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100993 struct task_struct *curr = counter->task;
994 u64 delta;
995
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100996 delta = __task_delta_exec(curr, update);
997
998 return curr->se.sum_exec_runtime + delta;
999}
1000
1001static void task_clock_perf_counter_update(struct perf_counter *counter, u64 now)
1002{
1003 u64 prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01001004 s64 delta;
Ingo Molnarbae43c92008-12-11 14:03:20 +01001005
Ingo Molnar8cb391e2008-12-14 12:22:31 +01001006 prev = atomic64_read(&counter->hw.prev_count);
Ingo Molnar8cb391e2008-12-14 12:22:31 +01001007
1008 atomic64_set(&counter->hw.prev_count, now);
1009
1010 delta = now - prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01001011
1012 atomic64_add(delta, &counter->count);
Ingo Molnarbae43c92008-12-11 14:03:20 +01001013}
1014
1015static void task_clock_perf_counter_read(struct perf_counter *counter)
1016{
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001017 u64 now = task_clock_perf_counter_val(counter, 1);
1018
1019 task_clock_perf_counter_update(counter, now);
Ingo Molnar8cb391e2008-12-14 12:22:31 +01001020}
1021
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01001022static int task_clock_perf_counter_enable(struct perf_counter *counter)
Ingo Molnar8cb391e2008-12-14 12:22:31 +01001023{
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001024 u64 now = task_clock_perf_counter_val(counter, 0);
1025
1026 atomic64_set(&counter->hw.prev_count, now);
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01001027
1028 return 0;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01001029}
1030
1031static void task_clock_perf_counter_disable(struct perf_counter *counter)
1032{
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001033 u64 now = task_clock_perf_counter_val(counter, 0);
1034
1035 task_clock_perf_counter_update(counter, now);
Ingo Molnarbae43c92008-12-11 14:03:20 +01001036}
1037
1038static const struct hw_perf_counter_ops perf_ops_task_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +01001039 .enable = task_clock_perf_counter_enable,
1040 .disable = task_clock_perf_counter_disable,
1041 .read = task_clock_perf_counter_read,
Ingo Molnarbae43c92008-12-11 14:03:20 +01001042};
1043
Ingo Molnare06c61a2008-12-14 14:44:31 +01001044static u64 get_page_faults(void)
1045{
1046 struct task_struct *curr = current;
1047
1048 return curr->maj_flt + curr->min_flt;
1049}
1050
1051static void page_faults_perf_counter_update(struct perf_counter *counter)
1052{
1053 u64 prev, now;
1054 s64 delta;
1055
1056 prev = atomic64_read(&counter->hw.prev_count);
1057 now = get_page_faults();
1058
1059 atomic64_set(&counter->hw.prev_count, now);
1060
1061 delta = now - prev;
Ingo Molnare06c61a2008-12-14 14:44:31 +01001062
1063 atomic64_add(delta, &counter->count);
1064}
1065
1066static void page_faults_perf_counter_read(struct perf_counter *counter)
1067{
1068 page_faults_perf_counter_update(counter);
1069}
1070
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01001071static int page_faults_perf_counter_enable(struct perf_counter *counter)
Ingo Molnare06c61a2008-12-14 14:44:31 +01001072{
1073 /*
1074 * page-faults is a per-task value already,
1075 * so we dont have to clear it on switch-in.
1076 */
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01001077
1078 return 0;
Ingo Molnare06c61a2008-12-14 14:44:31 +01001079}
1080
1081static void page_faults_perf_counter_disable(struct perf_counter *counter)
1082{
1083 page_faults_perf_counter_update(counter);
1084}
1085
1086static const struct hw_perf_counter_ops perf_ops_page_faults = {
Ingo Molnar76715812008-12-17 14:20:28 +01001087 .enable = page_faults_perf_counter_enable,
1088 .disable = page_faults_perf_counter_disable,
1089 .read = page_faults_perf_counter_read,
Ingo Molnare06c61a2008-12-14 14:44:31 +01001090};
1091
Ingo Molnar5d6a27d2008-12-14 12:28:33 +01001092static u64 get_context_switches(void)
1093{
1094 struct task_struct *curr = current;
1095
1096 return curr->nvcsw + curr->nivcsw;
1097}
1098
1099static void context_switches_perf_counter_update(struct perf_counter *counter)
1100{
1101 u64 prev, now;
1102 s64 delta;
1103
1104 prev = atomic64_read(&counter->hw.prev_count);
1105 now = get_context_switches();
1106
1107 atomic64_set(&counter->hw.prev_count, now);
1108
1109 delta = now - prev;
Ingo Molnar5d6a27d2008-12-14 12:28:33 +01001110
1111 atomic64_add(delta, &counter->count);
1112}
1113
1114static void context_switches_perf_counter_read(struct perf_counter *counter)
1115{
1116 context_switches_perf_counter_update(counter);
1117}
1118
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01001119static int context_switches_perf_counter_enable(struct perf_counter *counter)
Ingo Molnar5d6a27d2008-12-14 12:28:33 +01001120{
1121 /*
1122 * ->nvcsw + curr->nivcsw is a per-task value already,
1123 * so we dont have to clear it on switch-in.
1124 */
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01001125
1126 return 0;
Ingo Molnar5d6a27d2008-12-14 12:28:33 +01001127}
1128
1129static void context_switches_perf_counter_disable(struct perf_counter *counter)
1130{
1131 context_switches_perf_counter_update(counter);
1132}
1133
1134static const struct hw_perf_counter_ops perf_ops_context_switches = {
Ingo Molnar76715812008-12-17 14:20:28 +01001135 .enable = context_switches_perf_counter_enable,
1136 .disable = context_switches_perf_counter_disable,
1137 .read = context_switches_perf_counter_read,
Ingo Molnar5d6a27d2008-12-14 12:28:33 +01001138};
1139
Ingo Molnar6c594c22008-12-14 12:34:15 +01001140static inline u64 get_cpu_migrations(void)
1141{
1142 return current->se.nr_migrations;
1143}
1144
1145static void cpu_migrations_perf_counter_update(struct perf_counter *counter)
1146{
1147 u64 prev, now;
1148 s64 delta;
1149
1150 prev = atomic64_read(&counter->hw.prev_count);
1151 now = get_cpu_migrations();
1152
1153 atomic64_set(&counter->hw.prev_count, now);
1154
1155 delta = now - prev;
Ingo Molnar6c594c22008-12-14 12:34:15 +01001156
1157 atomic64_add(delta, &counter->count);
1158}
1159
1160static void cpu_migrations_perf_counter_read(struct perf_counter *counter)
1161{
1162 cpu_migrations_perf_counter_update(counter);
1163}
1164
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01001165static int cpu_migrations_perf_counter_enable(struct perf_counter *counter)
Ingo Molnar6c594c22008-12-14 12:34:15 +01001166{
1167 /*
1168 * se.nr_migrations is a per-task value already,
1169 * so we dont have to clear it on switch-in.
1170 */
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01001171
1172 return 0;
Ingo Molnar6c594c22008-12-14 12:34:15 +01001173}
1174
1175static void cpu_migrations_perf_counter_disable(struct perf_counter *counter)
1176{
1177 cpu_migrations_perf_counter_update(counter);
1178}
1179
1180static const struct hw_perf_counter_ops perf_ops_cpu_migrations = {
Ingo Molnar76715812008-12-17 14:20:28 +01001181 .enable = cpu_migrations_perf_counter_enable,
1182 .disable = cpu_migrations_perf_counter_disable,
1183 .read = cpu_migrations_perf_counter_read,
Ingo Molnar6c594c22008-12-14 12:34:15 +01001184};
1185
Ingo Molnar5c92d122008-12-11 13:21:10 +01001186static const struct hw_perf_counter_ops *
1187sw_perf_counter_init(struct perf_counter *counter)
1188{
1189 const struct hw_perf_counter_ops *hw_ops = NULL;
1190
1191 switch (counter->hw_event.type) {
1192 case PERF_COUNT_CPU_CLOCK:
1193 hw_ops = &perf_ops_cpu_clock;
1194 break;
Ingo Molnarbae43c92008-12-11 14:03:20 +01001195 case PERF_COUNT_TASK_CLOCK:
1196 hw_ops = &perf_ops_task_clock;
1197 break;
Ingo Molnare06c61a2008-12-14 14:44:31 +01001198 case PERF_COUNT_PAGE_FAULTS:
1199 hw_ops = &perf_ops_page_faults;
1200 break;
Ingo Molnar5d6a27d2008-12-14 12:28:33 +01001201 case PERF_COUNT_CONTEXT_SWITCHES:
1202 hw_ops = &perf_ops_context_switches;
1203 break;
Ingo Molnar6c594c22008-12-14 12:34:15 +01001204 case PERF_COUNT_CPU_MIGRATIONS:
1205 hw_ops = &perf_ops_cpu_migrations;
1206 break;
Ingo Molnar5c92d122008-12-11 13:21:10 +01001207 default:
1208 break;
1209 }
1210 return hw_ops;
1211}
1212
Thomas Gleixner0793a612008-12-04 20:12:29 +01001213/*
1214 * Allocate and initialize a counter structure
1215 */
1216static struct perf_counter *
Ingo Molnar04289bb2008-12-11 08:38:42 +01001217perf_counter_alloc(struct perf_counter_hw_event *hw_event,
1218 int cpu,
Ingo Molnar9b51f662008-12-12 13:49:45 +01001219 struct perf_counter *group_leader,
1220 gfp_t gfpflags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001221{
Ingo Molnar5c92d122008-12-11 13:21:10 +01001222 const struct hw_perf_counter_ops *hw_ops;
Ingo Molnar621a01e2008-12-11 12:46:46 +01001223 struct perf_counter *counter;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001224
Ingo Molnar9b51f662008-12-12 13:49:45 +01001225 counter = kzalloc(sizeof(*counter), gfpflags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001226 if (!counter)
1227 return NULL;
1228
Ingo Molnar04289bb2008-12-11 08:38:42 +01001229 /*
1230 * Single counters are their own group leaders, with an
1231 * empty sibling list:
1232 */
1233 if (!group_leader)
1234 group_leader = counter;
1235
Thomas Gleixner0793a612008-12-04 20:12:29 +01001236 mutex_init(&counter->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01001237 INIT_LIST_HEAD(&counter->list_entry);
1238 INIT_LIST_HEAD(&counter->sibling_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001239 init_waitqueue_head(&counter->waitq);
1240
Ingo Molnar9f66a382008-12-10 12:33:23 +01001241 counter->irqdata = &counter->data[0];
1242 counter->usrdata = &counter->data[1];
1243 counter->cpu = cpu;
1244 counter->hw_event = *hw_event;
1245 counter->wakeup_pending = 0;
Ingo Molnar04289bb2008-12-11 08:38:42 +01001246 counter->group_leader = group_leader;
Ingo Molnar621a01e2008-12-11 12:46:46 +01001247 counter->hw_ops = NULL;
1248
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001249 counter->state = PERF_COUNTER_STATE_INACTIVE;
Ingo Molnara86ed502008-12-17 00:43:10 +01001250 if (hw_event->disabled)
1251 counter->state = PERF_COUNTER_STATE_OFF;
1252
Ingo Molnar5c92d122008-12-11 13:21:10 +01001253 hw_ops = NULL;
1254 if (!hw_event->raw && hw_event->type < 0)
1255 hw_ops = sw_perf_counter_init(counter);
Ingo Molnar9b51f662008-12-12 13:49:45 +01001256 if (!hw_ops)
Ingo Molnar5c92d122008-12-11 13:21:10 +01001257 hw_ops = hw_perf_counter_init(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01001258
Ingo Molnar621a01e2008-12-11 12:46:46 +01001259 if (!hw_ops) {
1260 kfree(counter);
1261 return NULL;
1262 }
1263 counter->hw_ops = hw_ops;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001264
1265 return counter;
1266}
1267
1268/**
Ingo Molnar9f66a382008-12-10 12:33:23 +01001269 * sys_perf_task_open - open a performance counter, associate it to a task/cpu
1270 *
1271 * @hw_event_uptr: event type attributes for monitoring/sampling
Thomas Gleixner0793a612008-12-04 20:12:29 +01001272 * @pid: target pid
Ingo Molnar9f66a382008-12-10 12:33:23 +01001273 * @cpu: target cpu
1274 * @group_fd: group leader counter fd
Thomas Gleixner0793a612008-12-04 20:12:29 +01001275 */
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001276asmlinkage int
1277sys_perf_counter_open(struct perf_counter_hw_event *hw_event_uptr __user,
1278 pid_t pid, int cpu, int group_fd)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001279{
Ingo Molnar04289bb2008-12-11 08:38:42 +01001280 struct perf_counter *counter, *group_leader;
Ingo Molnar9f66a382008-12-10 12:33:23 +01001281 struct perf_counter_hw_event hw_event;
Ingo Molnar04289bb2008-12-11 08:38:42 +01001282 struct perf_counter_context *ctx;
Ingo Molnar9b51f662008-12-12 13:49:45 +01001283 struct file *counter_file = NULL;
Ingo Molnar04289bb2008-12-11 08:38:42 +01001284 struct file *group_file = NULL;
1285 int fput_needed = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01001286 int fput_needed2 = 0;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001287 int ret;
1288
Ingo Molnar9f66a382008-12-10 12:33:23 +01001289 if (copy_from_user(&hw_event, hw_event_uptr, sizeof(hw_event)) != 0)
Thomas Gleixnereab656a2008-12-08 19:26:59 +01001290 return -EFAULT;
1291
Ingo Molnar04289bb2008-12-11 08:38:42 +01001292 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01001293 * Get the target context (task or percpu):
1294 */
1295 ctx = find_get_context(pid, cpu);
1296 if (IS_ERR(ctx))
1297 return PTR_ERR(ctx);
1298
1299 /*
1300 * Look up the group leader (we will attach this counter to it):
Ingo Molnar04289bb2008-12-11 08:38:42 +01001301 */
1302 group_leader = NULL;
1303 if (group_fd != -1) {
1304 ret = -EINVAL;
1305 group_file = fget_light(group_fd, &fput_needed);
1306 if (!group_file)
Ingo Molnarccff2862008-12-11 11:26:29 +01001307 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01001308 if (group_file->f_op != &perf_fops)
Ingo Molnarccff2862008-12-11 11:26:29 +01001309 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01001310
1311 group_leader = group_file->private_data;
1312 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01001313 * Do not allow a recursive hierarchy (this new sibling
1314 * becoming part of another group-sibling):
Ingo Molnar04289bb2008-12-11 08:38:42 +01001315 */
Ingo Molnarccff2862008-12-11 11:26:29 +01001316 if (group_leader->group_leader != group_leader)
1317 goto err_put_context;
1318 /*
1319 * Do not allow to attach to a group in a different
1320 * task or CPU context:
1321 */
1322 if (group_leader->ctx != ctx)
1323 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01001324 }
1325
Ingo Molnar5c92d122008-12-11 13:21:10 +01001326 ret = -EINVAL;
Ingo Molnar9b51f662008-12-12 13:49:45 +01001327 counter = perf_counter_alloc(&hw_event, cpu, group_leader, GFP_KERNEL);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001328 if (!counter)
1329 goto err_put_context;
1330
Thomas Gleixner0793a612008-12-04 20:12:29 +01001331 ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
1332 if (ret < 0)
Ingo Molnar9b51f662008-12-12 13:49:45 +01001333 goto err_free_put_context;
1334
1335 counter_file = fget_light(ret, &fput_needed2);
1336 if (!counter_file)
1337 goto err_free_put_context;
1338
1339 counter->filp = counter_file;
1340 perf_install_in_context(ctx, counter, cpu);
1341
1342 fput_light(counter_file, fput_needed2);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001343
Ingo Molnar04289bb2008-12-11 08:38:42 +01001344out_fput:
1345 fput_light(group_file, fput_needed);
1346
Thomas Gleixner0793a612008-12-04 20:12:29 +01001347 return ret;
1348
Ingo Molnar9b51f662008-12-12 13:49:45 +01001349err_free_put_context:
Thomas Gleixner0793a612008-12-04 20:12:29 +01001350 kfree(counter);
1351
1352err_put_context:
1353 put_context(ctx);
1354
Ingo Molnar04289bb2008-12-11 08:38:42 +01001355 goto out_fput;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001356}
1357
Ingo Molnar9b51f662008-12-12 13:49:45 +01001358/*
1359 * Initialize the perf_counter context in a task_struct:
1360 */
1361static void
1362__perf_counter_init_context(struct perf_counter_context *ctx,
1363 struct task_struct *task)
1364{
1365 memset(ctx, 0, sizeof(*ctx));
1366 spin_lock_init(&ctx->lock);
1367 INIT_LIST_HEAD(&ctx->counter_list);
1368 ctx->task = task;
1369}
1370
1371/*
1372 * inherit a counter from parent task to child task:
1373 */
1374static int
1375inherit_counter(struct perf_counter *parent_counter,
1376 struct task_struct *parent,
1377 struct perf_counter_context *parent_ctx,
1378 struct task_struct *child,
1379 struct perf_counter_context *child_ctx)
1380{
1381 struct perf_counter *child_counter;
1382
1383 child_counter = perf_counter_alloc(&parent_counter->hw_event,
1384 parent_counter->cpu, NULL,
1385 GFP_ATOMIC);
1386 if (!child_counter)
1387 return -ENOMEM;
1388
1389 /*
1390 * Link it up in the child's context:
1391 */
1392 child_counter->ctx = child_ctx;
1393 child_counter->task = child;
1394 list_add_counter(child_counter, child_ctx);
1395 child_ctx->nr_counters++;
1396
1397 child_counter->parent = parent_counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01001398 /*
1399 * inherit into child's child as well:
1400 */
1401 child_counter->hw_event.inherit = 1;
1402
1403 /*
1404 * Get a reference to the parent filp - we will fput it
1405 * when the child counter exits. This is safe to do because
1406 * we are in the parent and we know that the filp still
1407 * exists and has a nonzero count:
1408 */
1409 atomic_long_inc(&parent_counter->filp->f_count);
1410
1411 return 0;
1412}
1413
1414static void
1415__perf_counter_exit_task(struct task_struct *child,
1416 struct perf_counter *child_counter,
1417 struct perf_counter_context *child_ctx)
1418{
1419 struct perf_counter *parent_counter;
1420 u64 parent_val, child_val;
Ingo Molnar9b51f662008-12-12 13:49:45 +01001421
1422 /*
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001423 * If we do not self-reap then we have to wait for the
1424 * child task to unschedule (it will happen for sure),
1425 * so that its counter is at its final count. (This
1426 * condition triggers rarely - child tasks usually get
1427 * off their CPU before the parent has a chance to
1428 * get this far into the reaping action)
Ingo Molnar9b51f662008-12-12 13:49:45 +01001429 */
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001430 if (child != current) {
1431 wait_task_inactive(child, 0);
1432 list_del_init(&child_counter->list_entry);
1433 } else {
Ingo Molnar0cc0c022008-12-14 23:20:36 +01001434 struct perf_cpu_context *cpuctx;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001435 unsigned long flags;
1436 u64 perf_flags;
1437
1438 /*
1439 * Disable and unlink this counter.
1440 *
1441 * Be careful about zapping the list - IRQ/NMI context
1442 * could still be processing it:
1443 */
1444 curr_rq_lock_irq_save(&flags);
1445 perf_flags = hw_perf_save_disable();
Ingo Molnar0cc0c022008-12-14 23:20:36 +01001446
1447 cpuctx = &__get_cpu_var(perf_cpu_context);
1448
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001449 if (child_counter->state == PERF_COUNTER_STATE_ACTIVE) {
1450 child_counter->state = PERF_COUNTER_STATE_INACTIVE;
1451 child_counter->hw_ops->disable(child_counter);
1452 cpuctx->active_oncpu--;
1453 child_ctx->nr_active--;
1454 child_counter->oncpu = -1;
1455 }
Ingo Molnar0cc0c022008-12-14 23:20:36 +01001456
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001457 list_del_init(&child_counter->list_entry);
1458
1459 child_ctx->nr_counters--;
1460
1461 hw_perf_restore(perf_flags);
1462 curr_rq_unlock_irq_restore(&flags);
Ingo Molnar0cc0c022008-12-14 23:20:36 +01001463 }
1464
Ingo Molnar9b51f662008-12-12 13:49:45 +01001465 parent_counter = child_counter->parent;
1466 /*
1467 * It can happen that parent exits first, and has counters
1468 * that are still around due to the child reference. These
1469 * counters need to be zapped - but otherwise linger.
1470 */
1471 if (!parent_counter)
1472 return;
1473
1474 parent_val = atomic64_read(&parent_counter->count);
1475 child_val = atomic64_read(&child_counter->count);
1476
1477 /*
1478 * Add back the child's count to the parent's count:
1479 */
1480 atomic64_add(child_val, &parent_counter->count);
1481
1482 fput(parent_counter->filp);
1483
1484 kfree(child_counter);
1485}
1486
1487/*
1488 * When a child task exist, feed back counter values to parent counters.
1489 *
1490 * Note: we are running in child context, but the PID is not hashed
1491 * anymore so new counters will not be added.
1492 */
1493void perf_counter_exit_task(struct task_struct *child)
1494{
1495 struct perf_counter *child_counter, *tmp;
1496 struct perf_counter_context *child_ctx;
1497
1498 child_ctx = &child->perf_counter_ctx;
1499
1500 if (likely(!child_ctx->nr_counters))
1501 return;
1502
1503 list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list,
1504 list_entry)
1505 __perf_counter_exit_task(child, child_counter, child_ctx);
1506}
1507
1508/*
1509 * Initialize the perf_counter context in task_struct
1510 */
1511void perf_counter_init_task(struct task_struct *child)
1512{
1513 struct perf_counter_context *child_ctx, *parent_ctx;
1514 struct perf_counter *counter, *parent_counter;
1515 struct task_struct *parent = current;
1516 unsigned long flags;
1517
1518 child_ctx = &child->perf_counter_ctx;
1519 parent_ctx = &parent->perf_counter_ctx;
1520
1521 __perf_counter_init_context(child_ctx, child);
1522
1523 /*
1524 * This is executed from the parent task context, so inherit
1525 * counters that have been marked for cloning:
1526 */
1527
1528 if (likely(!parent_ctx->nr_counters))
1529 return;
1530
1531 /*
1532 * Lock the parent list. No need to lock the child - not PID
1533 * hashed yet and not running, so nobody can access it.
1534 */
1535 spin_lock_irqsave(&parent_ctx->lock, flags);
1536
1537 /*
1538 * We dont have to disable NMIs - we are only looking at
1539 * the list, not manipulating it:
1540 */
1541 list_for_each_entry(counter, &parent_ctx->counter_list, list_entry) {
1542 if (!counter->hw_event.inherit || counter->group_leader != counter)
1543 continue;
1544
1545 /*
1546 * Instead of creating recursive hierarchies of counters,
1547 * we link inheritd counters back to the original parent,
1548 * which has a filp for sure, which we use as the reference
1549 * count:
1550 */
1551 parent_counter = counter;
1552 if (counter->parent)
1553 parent_counter = counter->parent;
1554
1555 if (inherit_counter(parent_counter, parent,
1556 parent_ctx, child, child_ctx))
1557 break;
1558 }
1559
1560 spin_unlock_irqrestore(&parent_ctx->lock, flags);
1561}
1562
Ingo Molnar04289bb2008-12-11 08:38:42 +01001563static void __cpuinit perf_counter_init_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001564{
Ingo Molnar04289bb2008-12-11 08:38:42 +01001565 struct perf_cpu_context *cpuctx;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001566
Ingo Molnar04289bb2008-12-11 08:38:42 +01001567 cpuctx = &per_cpu(perf_cpu_context, cpu);
1568 __perf_counter_init_context(&cpuctx->ctx, NULL);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001569
1570 mutex_lock(&perf_resource_mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01001571 cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001572 mutex_unlock(&perf_resource_mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01001573
Thomas Gleixner0793a612008-12-04 20:12:29 +01001574 hw_perf_counter_setup();
1575}
1576
1577#ifdef CONFIG_HOTPLUG_CPU
Ingo Molnar04289bb2008-12-11 08:38:42 +01001578static void __perf_counter_exit_cpu(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001579{
1580 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1581 struct perf_counter_context *ctx = &cpuctx->ctx;
1582 struct perf_counter *counter, *tmp;
1583
Ingo Molnar04289bb2008-12-11 08:38:42 +01001584 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry)
1585 __perf_counter_remove_from_context(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001586
1587}
Ingo Molnar04289bb2008-12-11 08:38:42 +01001588static void perf_counter_exit_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001589{
Ingo Molnar04289bb2008-12-11 08:38:42 +01001590 smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001591}
1592#else
Ingo Molnar04289bb2008-12-11 08:38:42 +01001593static inline void perf_counter_exit_cpu(int cpu) { }
Thomas Gleixner0793a612008-12-04 20:12:29 +01001594#endif
1595
1596static int __cpuinit
1597perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
1598{
1599 unsigned int cpu = (long)hcpu;
1600
1601 switch (action) {
1602
1603 case CPU_UP_PREPARE:
1604 case CPU_UP_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01001605 perf_counter_init_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001606 break;
1607
1608 case CPU_DOWN_PREPARE:
1609 case CPU_DOWN_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01001610 perf_counter_exit_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001611 break;
1612
1613 default:
1614 break;
1615 }
1616
1617 return NOTIFY_OK;
1618}
1619
1620static struct notifier_block __cpuinitdata perf_cpu_nb = {
1621 .notifier_call = perf_cpu_notify,
1622};
1623
1624static int __init perf_counter_init(void)
1625{
1626 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
1627 (void *)(long)smp_processor_id());
1628 register_cpu_notifier(&perf_cpu_nb);
1629
1630 return 0;
1631}
1632early_initcall(perf_counter_init);
1633
1634static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
1635{
1636 return sprintf(buf, "%d\n", perf_reserved_percpu);
1637}
1638
1639static ssize_t
1640perf_set_reserve_percpu(struct sysdev_class *class,
1641 const char *buf,
1642 size_t count)
1643{
1644 struct perf_cpu_context *cpuctx;
1645 unsigned long val;
1646 int err, cpu, mpt;
1647
1648 err = strict_strtoul(buf, 10, &val);
1649 if (err)
1650 return err;
1651 if (val > perf_max_counters)
1652 return -EINVAL;
1653
1654 mutex_lock(&perf_resource_mutex);
1655 perf_reserved_percpu = val;
1656 for_each_online_cpu(cpu) {
1657 cpuctx = &per_cpu(perf_cpu_context, cpu);
1658 spin_lock_irq(&cpuctx->ctx.lock);
1659 mpt = min(perf_max_counters - cpuctx->ctx.nr_counters,
1660 perf_max_counters - perf_reserved_percpu);
1661 cpuctx->max_pertask = mpt;
1662 spin_unlock_irq(&cpuctx->ctx.lock);
1663 }
1664 mutex_unlock(&perf_resource_mutex);
1665
1666 return count;
1667}
1668
1669static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
1670{
1671 return sprintf(buf, "%d\n", perf_overcommit);
1672}
1673
1674static ssize_t
1675perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
1676{
1677 unsigned long val;
1678 int err;
1679
1680 err = strict_strtoul(buf, 10, &val);
1681 if (err)
1682 return err;
1683 if (val > 1)
1684 return -EINVAL;
1685
1686 mutex_lock(&perf_resource_mutex);
1687 perf_overcommit = val;
1688 mutex_unlock(&perf_resource_mutex);
1689
1690 return count;
1691}
1692
1693static SYSDEV_CLASS_ATTR(
1694 reserve_percpu,
1695 0644,
1696 perf_show_reserve_percpu,
1697 perf_set_reserve_percpu
1698 );
1699
1700static SYSDEV_CLASS_ATTR(
1701 overcommit,
1702 0644,
1703 perf_show_overcommit,
1704 perf_set_overcommit
1705 );
1706
1707static struct attribute *perfclass_attrs[] = {
1708 &attr_reserve_percpu.attr,
1709 &attr_overcommit.attr,
1710 NULL
1711};
1712
1713static struct attribute_group perfclass_attr_group = {
1714 .attrs = perfclass_attrs,
1715 .name = "perf_counters",
1716};
1717
1718static int __init perf_counter_sysfs_init(void)
1719{
1720 return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
1721 &perfclass_attr_group);
1722}
1723device_initcall(perf_counter_sysfs_init);