blob: 4e679b91d8bb35d1ca9cf1f33d5adc076d3a51dd [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; }
47void __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
50#if BITS_PER_LONG == 64
51
52/*
53 * Read the cached counter in counter safe against cross CPU / NMI
54 * modifications. 64 bit version - no complications.
55 */
Ingo Molnar04289bb2008-12-11 08:38:42 +010056static inline u64 perf_counter_read_safe(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +010057{
58 return (u64) atomic64_read(&counter->count);
59}
60
Ingo Molnar5c92d122008-12-11 13:21:10 +010061void atomic64_counter_set(struct perf_counter *counter, u64 val)
62{
63 atomic64_set(&counter->count, val);
64}
65
66u64 atomic64_counter_read(struct perf_counter *counter)
67{
68 return atomic64_read(&counter->count);
69}
70
Thomas Gleixner0793a612008-12-04 20:12:29 +010071#else
72
73/*
74 * Read the cached counter in counter safe against cross CPU / NMI
75 * modifications. 32 bit version.
76 */
Ingo Molnar04289bb2008-12-11 08:38:42 +010077static u64 perf_counter_read_safe(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +010078{
79 u32 cntl, cnth;
80
81 local_irq_disable();
82 do {
83 cnth = atomic_read(&counter->count32[1]);
84 cntl = atomic_read(&counter->count32[0]);
85 } while (cnth != atomic_read(&counter->count32[1]));
86
87 local_irq_enable();
88
89 return cntl | ((u64) cnth) << 32;
90}
91
Ingo Molnar5c92d122008-12-11 13:21:10 +010092void atomic64_counter_set(struct perf_counter *counter, u64 val64)
93{
94 u32 *val32 = (void *)&val64;
95
96 atomic_set(counter->count32 + 0, *(val32 + 0));
97 atomic_set(counter->count32 + 1, *(val32 + 1));
98}
99
100u64 atomic64_counter_read(struct perf_counter *counter)
101{
102 return atomic_read(counter->count32 + 0) |
103 (u64) atomic_read(counter->count32 + 1) << 32;
104}
105
Thomas Gleixner0793a612008-12-04 20:12:29 +0100106#endif
107
Ingo Molnar04289bb2008-12-11 08:38:42 +0100108static void
109list_add_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
110{
111 struct perf_counter *group_leader = counter->group_leader;
112
113 /*
114 * Depending on whether it is a standalone or sibling counter,
115 * add it straight to the context's counter list, or to the group
116 * leader's sibling list:
117 */
118 if (counter->group_leader == counter)
119 list_add_tail(&counter->list_entry, &ctx->counter_list);
120 else
121 list_add_tail(&counter->list_entry, &group_leader->sibling_list);
122}
123
124static void
125list_del_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
126{
127 struct perf_counter *sibling, *tmp;
128
129 list_del_init(&counter->list_entry);
130
Ingo Molnar04289bb2008-12-11 08:38:42 +0100131 /*
132 * If this was a group counter with sibling counters then
133 * upgrade the siblings to singleton counters by adding them
134 * to the context list directly:
135 */
136 list_for_each_entry_safe(sibling, tmp,
137 &counter->sibling_list, list_entry) {
138
139 list_del_init(&sibling->list_entry);
140 list_add_tail(&sibling->list_entry, &ctx->counter_list);
141 WARN_ON_ONCE(!sibling->group_leader);
142 WARN_ON_ONCE(sibling->group_leader == sibling);
143 sibling->group_leader = sibling;
144 }
145}
146
Thomas Gleixner0793a612008-12-04 20:12:29 +0100147/*
148 * Cross CPU call to remove a performance counter
149 *
150 * We disable the counter on the hardware level first. After that we
151 * remove it from the context list.
152 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100153static void __perf_counter_remove_from_context(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100154{
155 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
156 struct perf_counter *counter = info;
157 struct perf_counter_context *ctx = counter->ctx;
Ingo Molnar5c92d122008-12-11 13:21:10 +0100158 u64 perf_flags;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100159
160 /*
161 * If this is a task context, we need to check whether it is
162 * the current task context of this cpu. If not it has been
163 * scheduled out before the smp call arrived.
164 */
165 if (ctx->task && cpuctx->task_ctx != ctx)
166 return;
167
168 spin_lock(&ctx->lock);
169
170 if (counter->active) {
Ingo Molnar621a01e2008-12-11 12:46:46 +0100171 counter->hw_ops->hw_perf_counter_disable(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100172 counter->active = 0;
173 ctx->nr_active--;
174 cpuctx->active_oncpu--;
175 counter->task = NULL;
176 }
177 ctx->nr_counters--;
178
179 /*
180 * Protect the list operation against NMI by disabling the
181 * counters on a global level. NOP for non NMI based counters.
182 */
Ingo Molnar01b28382008-12-11 13:45:51 +0100183 perf_flags = hw_perf_save_disable();
Ingo Molnar04289bb2008-12-11 08:38:42 +0100184 list_del_counter(counter, ctx);
Ingo Molnar01b28382008-12-11 13:45:51 +0100185 hw_perf_restore(perf_flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100186
187 if (!ctx->task) {
188 /*
189 * Allow more per task counters with respect to the
190 * reservation:
191 */
192 cpuctx->max_pertask =
193 min(perf_max_counters - ctx->nr_counters,
194 perf_max_counters - perf_reserved_percpu);
195 }
196
197 spin_unlock(&ctx->lock);
198}
199
200
201/*
202 * Remove the counter from a task's (or a CPU's) list of counters.
203 *
204 * Must be called with counter->mutex held.
205 *
206 * CPU counters are removed with a smp call. For task counters we only
207 * call when the task is on a CPU.
208 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100209static void perf_counter_remove_from_context(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100210{
211 struct perf_counter_context *ctx = counter->ctx;
212 struct task_struct *task = ctx->task;
213
214 if (!task) {
215 /*
216 * Per cpu counters are removed via an smp call and
217 * the removal is always sucessful.
218 */
219 smp_call_function_single(counter->cpu,
Ingo Molnar04289bb2008-12-11 08:38:42 +0100220 __perf_counter_remove_from_context,
Thomas Gleixner0793a612008-12-04 20:12:29 +0100221 counter, 1);
222 return;
223 }
224
225retry:
Ingo Molnar04289bb2008-12-11 08:38:42 +0100226 task_oncpu_function_call(task, __perf_counter_remove_from_context,
Thomas Gleixner0793a612008-12-04 20:12:29 +0100227 counter);
228
229 spin_lock_irq(&ctx->lock);
230 /*
231 * If the context is active we need to retry the smp call.
232 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100233 if (ctx->nr_active && !list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100234 spin_unlock_irq(&ctx->lock);
235 goto retry;
236 }
237
238 /*
239 * The lock prevents that this context is scheduled in so we
Ingo Molnar04289bb2008-12-11 08:38:42 +0100240 * can remove the counter safely, if the call above did not
Thomas Gleixner0793a612008-12-04 20:12:29 +0100241 * succeed.
242 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100243 if (!list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100244 ctx->nr_counters--;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100245 list_del_counter(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100246 counter->task = NULL;
247 }
248 spin_unlock_irq(&ctx->lock);
249}
250
251/*
252 * Cross CPU call to install and enable a preformance counter
253 */
254static void __perf_install_in_context(void *info)
255{
256 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
257 struct perf_counter *counter = info;
258 struct perf_counter_context *ctx = counter->ctx;
259 int cpu = smp_processor_id();
Ingo Molnar5c92d122008-12-11 13:21:10 +0100260 u64 perf_flags;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100261
262 /*
263 * If this is a task context, we need to check whether it is
264 * the current task context of this cpu. If not it has been
265 * scheduled out before the smp call arrived.
266 */
267 if (ctx->task && cpuctx->task_ctx != ctx)
268 return;
269
270 spin_lock(&ctx->lock);
271
272 /*
273 * Protect the list operation against NMI by disabling the
274 * counters on a global level. NOP for non NMI based counters.
275 */
Ingo Molnar01b28382008-12-11 13:45:51 +0100276 perf_flags = hw_perf_save_disable();
Ingo Molnar04289bb2008-12-11 08:38:42 +0100277 list_add_counter(counter, ctx);
Ingo Molnar01b28382008-12-11 13:45:51 +0100278 hw_perf_restore(perf_flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100279
280 ctx->nr_counters++;
281
282 if (cpuctx->active_oncpu < perf_max_counters) {
Ingo Molnar621a01e2008-12-11 12:46:46 +0100283 counter->hw_ops->hw_perf_counter_enable(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100284 counter->active = 1;
285 counter->oncpu = cpu;
286 ctx->nr_active++;
287 cpuctx->active_oncpu++;
288 }
289
290 if (!ctx->task && cpuctx->max_pertask)
291 cpuctx->max_pertask--;
292
293 spin_unlock(&ctx->lock);
294}
295
296/*
297 * Attach a performance counter to a context
298 *
299 * First we add the counter to the list with the hardware enable bit
300 * in counter->hw_config cleared.
301 *
302 * If the counter is attached to a task which is on a CPU we use a smp
303 * call to enable it in the task context. The task might have been
304 * scheduled away, but we check this in the smp call again.
305 */
306static void
307perf_install_in_context(struct perf_counter_context *ctx,
308 struct perf_counter *counter,
309 int cpu)
310{
311 struct task_struct *task = ctx->task;
312
313 counter->ctx = ctx;
314 if (!task) {
315 /*
316 * Per cpu counters are installed via an smp call and
317 * the install is always sucessful.
318 */
319 smp_call_function_single(cpu, __perf_install_in_context,
320 counter, 1);
321 return;
322 }
323
324 counter->task = task;
325retry:
326 task_oncpu_function_call(task, __perf_install_in_context,
327 counter);
328
329 spin_lock_irq(&ctx->lock);
330 /*
331 * If the context is active and the counter has not been added
332 * we need to retry the smp call.
333 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100334 if (ctx->nr_active && list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100335 spin_unlock_irq(&ctx->lock);
336 goto retry;
337 }
338
339 /*
340 * The lock prevents that this context is scheduled in so we
341 * can add the counter safely, if it the call above did not
342 * succeed.
343 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100344 if (list_empty(&counter->list_entry)) {
345 list_add_counter(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100346 ctx->nr_counters++;
347 }
348 spin_unlock_irq(&ctx->lock);
349}
350
Ingo Molnar04289bb2008-12-11 08:38:42 +0100351static void
352counter_sched_out(struct perf_counter *counter,
353 struct perf_cpu_context *cpuctx,
354 struct perf_counter_context *ctx)
355{
356 if (!counter->active)
357 return;
358
Ingo Molnar621a01e2008-12-11 12:46:46 +0100359 counter->hw_ops->hw_perf_counter_disable(counter);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100360 counter->active = 0;
361 counter->oncpu = -1;
362
363 cpuctx->active_oncpu--;
364 ctx->nr_active--;
365}
366
367static void
368group_sched_out(struct perf_counter *group_counter,
369 struct perf_cpu_context *cpuctx,
370 struct perf_counter_context *ctx)
371{
372 struct perf_counter *counter;
373
374 counter_sched_out(group_counter, cpuctx, ctx);
375
376 /*
377 * Schedule out siblings (if any):
378 */
379 list_for_each_entry(counter, &group_counter->sibling_list, list_entry)
380 counter_sched_out(counter, cpuctx, ctx);
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 *
389 * This does not protect us against NMI, but hw_perf_counter_disable()
390 * 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;
398 struct perf_counter *counter;
399
400 if (likely(!cpuctx->task_ctx))
401 return;
402
403 spin_lock(&ctx->lock);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100404 if (ctx->nr_active) {
405 list_for_each_entry(counter, &ctx->counter_list, list_entry)
406 group_sched_out(counter, cpuctx, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100407 }
408 spin_unlock(&ctx->lock);
409 cpuctx->task_ctx = NULL;
410}
411
Ingo Molnar04289bb2008-12-11 08:38:42 +0100412static void
413counter_sched_in(struct perf_counter *counter,
414 struct perf_cpu_context *cpuctx,
415 struct perf_counter_context *ctx,
416 int cpu)
417{
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100418 if (counter->active == -1)
419 return;
420
Ingo Molnar621a01e2008-12-11 12:46:46 +0100421 counter->hw_ops->hw_perf_counter_enable(counter);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100422 counter->active = 1;
423 counter->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */
424
425 cpuctx->active_oncpu++;
426 ctx->nr_active++;
427}
428
429static void
430group_sched_in(struct perf_counter *group_counter,
431 struct perf_cpu_context *cpuctx,
432 struct perf_counter_context *ctx,
433 int cpu)
434{
435 struct perf_counter *counter;
436
437 counter_sched_in(group_counter, cpuctx, ctx, cpu);
438
439 /*
440 * Schedule in siblings as one group (if any):
441 */
442 list_for_each_entry(counter, &group_counter->sibling_list, list_entry)
443 counter_sched_in(counter, cpuctx, ctx, cpu);
444}
445
Thomas Gleixner0793a612008-12-04 20:12:29 +0100446/*
447 * Called from scheduler to add the counters of the current task
448 * with interrupts disabled.
449 *
450 * We restore the counter value and then enable it.
451 *
452 * This does not protect us against NMI, but hw_perf_counter_enable()
453 * sets the enabled bit in the control field of counter _before_
454 * accessing the counter control register. If a NMI hits, then it will
455 * keep the counter running.
456 */
457void perf_counter_task_sched_in(struct task_struct *task, int cpu)
458{
459 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
460 struct perf_counter_context *ctx = &task->perf_counter_ctx;
461 struct perf_counter *counter;
462
463 if (likely(!ctx->nr_counters))
464 return;
465
466 spin_lock(&ctx->lock);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100467 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100468 if (ctx->nr_active == cpuctx->max_pertask)
469 break;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100470
471 /*
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 Molnar04289bb2008-12-11 08:38:42 +0100478 group_sched_in(counter, cpuctx, ctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100479 }
480 spin_unlock(&ctx->lock);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100481
Thomas Gleixner0793a612008-12-04 20:12:29 +0100482 cpuctx->task_ctx = ctx;
483}
484
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100485int perf_counter_task_disable(void)
486{
487 struct task_struct *curr = current;
488 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
489 struct perf_counter *counter;
490 u64 perf_flags;
491 int cpu;
492
493 if (likely(!ctx->nr_counters))
494 return 0;
495
496 local_irq_disable();
497 cpu = smp_processor_id();
498
499 perf_counter_task_sched_out(curr, cpu);
500
501 spin_lock(&ctx->lock);
502
503 /*
504 * Disable all the counters:
505 */
506 perf_flags = hw_perf_save_disable();
507
508 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
509 WARN_ON_ONCE(counter->active == 1);
510 counter->active = -1;
511 }
512 hw_perf_restore(perf_flags);
513
514 spin_unlock(&ctx->lock);
515
516 local_irq_enable();
517
518 return 0;
519}
520
521int perf_counter_task_enable(void)
522{
523 struct task_struct *curr = current;
524 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
525 struct perf_counter *counter;
526 u64 perf_flags;
527 int cpu;
528
529 if (likely(!ctx->nr_counters))
530 return 0;
531
532 local_irq_disable();
533 cpu = smp_processor_id();
534
535 spin_lock(&ctx->lock);
536
537 /*
538 * Disable all the counters:
539 */
540 perf_flags = hw_perf_save_disable();
541
542 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
543 if (counter->active != -1)
544 continue;
545 counter->active = 0;
546 }
547 hw_perf_restore(perf_flags);
548
549 spin_unlock(&ctx->lock);
550
551 perf_counter_task_sched_in(curr, cpu);
552
553 local_irq_enable();
554
555 return 0;
556}
557
Thomas Gleixner0793a612008-12-04 20:12:29 +0100558void perf_counter_task_tick(struct task_struct *curr, int cpu)
559{
560 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
561 struct perf_counter *counter;
Ingo Molnar5c92d122008-12-11 13:21:10 +0100562 u64 perf_flags;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100563
564 if (likely(!ctx->nr_counters))
565 return;
566
567 perf_counter_task_sched_out(curr, cpu);
568
569 spin_lock(&ctx->lock);
570
571 /*
Ingo Molnar04289bb2008-12-11 08:38:42 +0100572 * Rotate the first entry last (works just fine for group counters too):
Thomas Gleixner0793a612008-12-04 20:12:29 +0100573 */
Ingo Molnar01b28382008-12-11 13:45:51 +0100574 perf_flags = hw_perf_save_disable();
Ingo Molnar04289bb2008-12-11 08:38:42 +0100575 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
576 list_del(&counter->list_entry);
577 list_add_tail(&counter->list_entry, &ctx->counter_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100578 break;
579 }
Ingo Molnar01b28382008-12-11 13:45:51 +0100580 hw_perf_restore(perf_flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100581
582 spin_unlock(&ctx->lock);
583
584 perf_counter_task_sched_in(curr, cpu);
585}
586
587/*
Ingo Molnar04289bb2008-12-11 08:38:42 +0100588 * Initialize the perf_counter context in a task_struct:
589 */
590static void
591__perf_counter_init_context(struct perf_counter_context *ctx,
592 struct task_struct *task)
593{
594 spin_lock_init(&ctx->lock);
595 INIT_LIST_HEAD(&ctx->counter_list);
596 ctx->nr_counters = 0;
597 ctx->task = task;
598}
599/*
Thomas Gleixner0793a612008-12-04 20:12:29 +0100600 * Initialize the perf_counter context in task_struct
601 */
602void perf_counter_init_task(struct task_struct *task)
603{
Ingo Molnar04289bb2008-12-11 08:38:42 +0100604 __perf_counter_init_context(&task->perf_counter_ctx, task);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100605}
606
607/*
608 * Cross CPU call to read the hardware counter
609 */
610static void __hw_perf_counter_read(void *info)
611{
Ingo Molnar621a01e2008-12-11 12:46:46 +0100612 struct perf_counter *counter = info;
613
614 counter->hw_ops->hw_perf_counter_read(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100615}
616
Ingo Molnar04289bb2008-12-11 08:38:42 +0100617static u64 perf_counter_read(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100618{
619 /*
620 * If counter is enabled and currently active on a CPU, update the
621 * value in the counter structure:
622 */
623 if (counter->active) {
624 smp_call_function_single(counter->oncpu,
625 __hw_perf_counter_read, counter, 1);
626 }
627
Ingo Molnar04289bb2008-12-11 08:38:42 +0100628 return perf_counter_read_safe(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100629}
630
631/*
632 * Cross CPU call to switch performance data pointers
633 */
634static void __perf_switch_irq_data(void *info)
635{
636 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
637 struct perf_counter *counter = info;
638 struct perf_counter_context *ctx = counter->ctx;
639 struct perf_data *oldirqdata = counter->irqdata;
640
641 /*
642 * If this is a task context, we need to check whether it is
643 * the current task context of this cpu. If not it has been
644 * scheduled out before the smp call arrived.
645 */
646 if (ctx->task) {
647 if (cpuctx->task_ctx != ctx)
648 return;
649 spin_lock(&ctx->lock);
650 }
651
652 /* Change the pointer NMI safe */
653 atomic_long_set((atomic_long_t *)&counter->irqdata,
654 (unsigned long) counter->usrdata);
655 counter->usrdata = oldirqdata;
656
657 if (ctx->task)
658 spin_unlock(&ctx->lock);
659}
660
661static struct perf_data *perf_switch_irq_data(struct perf_counter *counter)
662{
663 struct perf_counter_context *ctx = counter->ctx;
664 struct perf_data *oldirqdata = counter->irqdata;
665 struct task_struct *task = ctx->task;
666
667 if (!task) {
668 smp_call_function_single(counter->cpu,
669 __perf_switch_irq_data,
670 counter, 1);
671 return counter->usrdata;
672 }
673
674retry:
675 spin_lock_irq(&ctx->lock);
676 if (!counter->active) {
677 counter->irqdata = counter->usrdata;
678 counter->usrdata = oldirqdata;
679 spin_unlock_irq(&ctx->lock);
680 return oldirqdata;
681 }
682 spin_unlock_irq(&ctx->lock);
683 task_oncpu_function_call(task, __perf_switch_irq_data, counter);
684 /* Might have failed, because task was scheduled out */
685 if (counter->irqdata == oldirqdata)
686 goto retry;
687
688 return counter->usrdata;
689}
690
691static void put_context(struct perf_counter_context *ctx)
692{
693 if (ctx->task)
694 put_task_struct(ctx->task);
695}
696
697static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
698{
699 struct perf_cpu_context *cpuctx;
700 struct perf_counter_context *ctx;
701 struct task_struct *task;
702
703 /*
704 * If cpu is not a wildcard then this is a percpu counter:
705 */
706 if (cpu != -1) {
707 /* Must be root to operate on a CPU counter: */
708 if (!capable(CAP_SYS_ADMIN))
709 return ERR_PTR(-EACCES);
710
711 if (cpu < 0 || cpu > num_possible_cpus())
712 return ERR_PTR(-EINVAL);
713
714 /*
715 * We could be clever and allow to attach a counter to an
716 * offline CPU and activate it when the CPU comes up, but
717 * that's for later.
718 */
719 if (!cpu_isset(cpu, cpu_online_map))
720 return ERR_PTR(-ENODEV);
721
722 cpuctx = &per_cpu(perf_cpu_context, cpu);
723 ctx = &cpuctx->ctx;
724
725 WARN_ON_ONCE(ctx->task);
726 return ctx;
727 }
728
729 rcu_read_lock();
730 if (!pid)
731 task = current;
732 else
733 task = find_task_by_vpid(pid);
734 if (task)
735 get_task_struct(task);
736 rcu_read_unlock();
737
738 if (!task)
739 return ERR_PTR(-ESRCH);
740
741 ctx = &task->perf_counter_ctx;
742 ctx->task = task;
743
744 /* Reuse ptrace permission checks for now. */
745 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
746 put_context(ctx);
747 return ERR_PTR(-EACCES);
748 }
749
750 return ctx;
751}
752
753/*
754 * Called when the last reference to the file is gone.
755 */
756static int perf_release(struct inode *inode, struct file *file)
757{
758 struct perf_counter *counter = file->private_data;
759 struct perf_counter_context *ctx = counter->ctx;
760
761 file->private_data = NULL;
762
763 mutex_lock(&counter->mutex);
764
Ingo Molnar04289bb2008-12-11 08:38:42 +0100765 perf_counter_remove_from_context(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100766 put_context(ctx);
767
768 mutex_unlock(&counter->mutex);
769
770 kfree(counter);
771
772 return 0;
773}
774
775/*
776 * Read the performance counter - simple non blocking version for now
777 */
778static ssize_t
779perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
780{
781 u64 cntval;
782
783 if (count != sizeof(cntval))
784 return -EINVAL;
785
786 mutex_lock(&counter->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100787 cntval = perf_counter_read(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100788 mutex_unlock(&counter->mutex);
789
790 return put_user(cntval, (u64 __user *) buf) ? -EFAULT : sizeof(cntval);
791}
792
793static ssize_t
794perf_copy_usrdata(struct perf_data *usrdata, char __user *buf, size_t count)
795{
796 if (!usrdata->len)
797 return 0;
798
799 count = min(count, (size_t)usrdata->len);
800 if (copy_to_user(buf, usrdata->data + usrdata->rd_idx, count))
801 return -EFAULT;
802
803 /* Adjust the counters */
804 usrdata->len -= count;
805 if (!usrdata->len)
806 usrdata->rd_idx = 0;
807 else
808 usrdata->rd_idx += count;
809
810 return count;
811}
812
813static ssize_t
814perf_read_irq_data(struct perf_counter *counter,
815 char __user *buf,
816 size_t count,
817 int nonblocking)
818{
819 struct perf_data *irqdata, *usrdata;
820 DECLARE_WAITQUEUE(wait, current);
821 ssize_t res;
822
823 irqdata = counter->irqdata;
824 usrdata = counter->usrdata;
825
826 if (usrdata->len + irqdata->len >= count)
827 goto read_pending;
828
829 if (nonblocking)
830 return -EAGAIN;
831
832 spin_lock_irq(&counter->waitq.lock);
833 __add_wait_queue(&counter->waitq, &wait);
834 for (;;) {
835 set_current_state(TASK_INTERRUPTIBLE);
836 if (usrdata->len + irqdata->len >= count)
837 break;
838
839 if (signal_pending(current))
840 break;
841
842 spin_unlock_irq(&counter->waitq.lock);
843 schedule();
844 spin_lock_irq(&counter->waitq.lock);
845 }
846 __remove_wait_queue(&counter->waitq, &wait);
847 __set_current_state(TASK_RUNNING);
848 spin_unlock_irq(&counter->waitq.lock);
849
850 if (usrdata->len + irqdata->len < count)
851 return -ERESTARTSYS;
852read_pending:
853 mutex_lock(&counter->mutex);
854
855 /* Drain pending data first: */
856 res = perf_copy_usrdata(usrdata, buf, count);
857 if (res < 0 || res == count)
858 goto out;
859
860 /* Switch irq buffer: */
861 usrdata = perf_switch_irq_data(counter);
862 if (perf_copy_usrdata(usrdata, buf + res, count - res) < 0) {
863 if (!res)
864 res = -EFAULT;
865 } else {
866 res = count;
867 }
868out:
869 mutex_unlock(&counter->mutex);
870
871 return res;
872}
873
874static ssize_t
875perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
876{
877 struct perf_counter *counter = file->private_data;
878
Ingo Molnar9f66a382008-12-10 12:33:23 +0100879 switch (counter->hw_event.record_type) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100880 case PERF_RECORD_SIMPLE:
881 return perf_read_hw(counter, buf, count);
882
883 case PERF_RECORD_IRQ:
884 case PERF_RECORD_GROUP:
885 return perf_read_irq_data(counter, buf, count,
886 file->f_flags & O_NONBLOCK);
887 }
888 return -EINVAL;
889}
890
891static unsigned int perf_poll(struct file *file, poll_table *wait)
892{
893 struct perf_counter *counter = file->private_data;
894 unsigned int events = 0;
895 unsigned long flags;
896
897 poll_wait(file, &counter->waitq, wait);
898
899 spin_lock_irqsave(&counter->waitq.lock, flags);
900 if (counter->usrdata->len || counter->irqdata->len)
901 events |= POLLIN;
902 spin_unlock_irqrestore(&counter->waitq.lock, flags);
903
904 return events;
905}
906
907static const struct file_operations perf_fops = {
908 .release = perf_release,
909 .read = perf_read,
910 .poll = perf_poll,
911};
912
Ingo Molnar5c92d122008-12-11 13:21:10 +0100913static void cpu_clock_perf_counter_enable(struct perf_counter *counter)
914{
915}
916
917static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
918{
919}
920
921static void cpu_clock_perf_counter_read(struct perf_counter *counter)
922{
923 int cpu = raw_smp_processor_id();
924
925 atomic64_counter_set(counter, cpu_clock(cpu));
926}
927
928static const struct hw_perf_counter_ops perf_ops_cpu_clock = {
929 .hw_perf_counter_enable = cpu_clock_perf_counter_enable,
930 .hw_perf_counter_disable = cpu_clock_perf_counter_disable,
931 .hw_perf_counter_read = cpu_clock_perf_counter_read,
932};
933
Ingo Molnarbae43c92008-12-11 14:03:20 +0100934static void task_clock_perf_counter_enable(struct perf_counter *counter)
935{
936}
937
938static void task_clock_perf_counter_disable(struct perf_counter *counter)
939{
940}
941
942static void task_clock_perf_counter_read(struct perf_counter *counter)
943{
944 atomic64_counter_set(counter, current->se.sum_exec_runtime);
945}
946
947static const struct hw_perf_counter_ops perf_ops_task_clock = {
948 .hw_perf_counter_enable = task_clock_perf_counter_enable,
949 .hw_perf_counter_disable = task_clock_perf_counter_disable,
950 .hw_perf_counter_read = task_clock_perf_counter_read,
951};
952
Ingo Molnar5c92d122008-12-11 13:21:10 +0100953static const struct hw_perf_counter_ops *
954sw_perf_counter_init(struct perf_counter *counter)
955{
956 const struct hw_perf_counter_ops *hw_ops = NULL;
957
958 switch (counter->hw_event.type) {
959 case PERF_COUNT_CPU_CLOCK:
960 hw_ops = &perf_ops_cpu_clock;
961 break;
Ingo Molnarbae43c92008-12-11 14:03:20 +0100962 case PERF_COUNT_TASK_CLOCK:
963 hw_ops = &perf_ops_task_clock;
964 break;
Ingo Molnar5c92d122008-12-11 13:21:10 +0100965 default:
966 break;
967 }
968 return hw_ops;
969}
970
Thomas Gleixner0793a612008-12-04 20:12:29 +0100971/*
972 * Allocate and initialize a counter structure
973 */
974static struct perf_counter *
Ingo Molnar04289bb2008-12-11 08:38:42 +0100975perf_counter_alloc(struct perf_counter_hw_event *hw_event,
976 int cpu,
977 struct perf_counter *group_leader)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100978{
Ingo Molnar5c92d122008-12-11 13:21:10 +0100979 const struct hw_perf_counter_ops *hw_ops;
Ingo Molnar621a01e2008-12-11 12:46:46 +0100980 struct perf_counter *counter;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100981
Ingo Molnar621a01e2008-12-11 12:46:46 +0100982 counter = kzalloc(sizeof(*counter), GFP_KERNEL);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100983 if (!counter)
984 return NULL;
985
Ingo Molnar04289bb2008-12-11 08:38:42 +0100986 /*
987 * Single counters are their own group leaders, with an
988 * empty sibling list:
989 */
990 if (!group_leader)
991 group_leader = counter;
992
Thomas Gleixner0793a612008-12-04 20:12:29 +0100993 mutex_init(&counter->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100994 INIT_LIST_HEAD(&counter->list_entry);
995 INIT_LIST_HEAD(&counter->sibling_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100996 init_waitqueue_head(&counter->waitq);
997
Ingo Molnar9f66a382008-12-10 12:33:23 +0100998 counter->irqdata = &counter->data[0];
999 counter->usrdata = &counter->data[1];
1000 counter->cpu = cpu;
1001 counter->hw_event = *hw_event;
1002 counter->wakeup_pending = 0;
Ingo Molnar04289bb2008-12-11 08:38:42 +01001003 counter->group_leader = group_leader;
Ingo Molnar621a01e2008-12-11 12:46:46 +01001004 counter->hw_ops = NULL;
1005
Ingo Molnar5c92d122008-12-11 13:21:10 +01001006 hw_ops = NULL;
1007 if (!hw_event->raw && hw_event->type < 0)
1008 hw_ops = sw_perf_counter_init(counter);
1009 if (!hw_ops) {
1010 hw_ops = hw_perf_counter_init(counter);
1011 }
1012
Ingo Molnar621a01e2008-12-11 12:46:46 +01001013 if (!hw_ops) {
1014 kfree(counter);
1015 return NULL;
1016 }
1017 counter->hw_ops = hw_ops;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001018
1019 return counter;
1020}
1021
1022/**
Ingo Molnar9f66a382008-12-10 12:33:23 +01001023 * sys_perf_task_open - open a performance counter, associate it to a task/cpu
1024 *
1025 * @hw_event_uptr: event type attributes for monitoring/sampling
Thomas Gleixner0793a612008-12-04 20:12:29 +01001026 * @pid: target pid
Ingo Molnar9f66a382008-12-10 12:33:23 +01001027 * @cpu: target cpu
1028 * @group_fd: group leader counter fd
Thomas Gleixner0793a612008-12-04 20:12:29 +01001029 */
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001030asmlinkage int
1031sys_perf_counter_open(struct perf_counter_hw_event *hw_event_uptr __user,
1032 pid_t pid, int cpu, int group_fd)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001033{
Ingo Molnar04289bb2008-12-11 08:38:42 +01001034 struct perf_counter *counter, *group_leader;
Ingo Molnar9f66a382008-12-10 12:33:23 +01001035 struct perf_counter_hw_event hw_event;
Ingo Molnar04289bb2008-12-11 08:38:42 +01001036 struct perf_counter_context *ctx;
1037 struct file *group_file = NULL;
1038 int fput_needed = 0;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001039 int ret;
1040
Ingo Molnar9f66a382008-12-10 12:33:23 +01001041 if (copy_from_user(&hw_event, hw_event_uptr, sizeof(hw_event)) != 0)
Thomas Gleixnereab656a2008-12-08 19:26:59 +01001042 return -EFAULT;
1043
Ingo Molnar04289bb2008-12-11 08:38:42 +01001044 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01001045 * Get the target context (task or percpu):
1046 */
1047 ctx = find_get_context(pid, cpu);
1048 if (IS_ERR(ctx))
1049 return PTR_ERR(ctx);
1050
1051 /*
1052 * Look up the group leader (we will attach this counter to it):
Ingo Molnar04289bb2008-12-11 08:38:42 +01001053 */
1054 group_leader = NULL;
1055 if (group_fd != -1) {
1056 ret = -EINVAL;
1057 group_file = fget_light(group_fd, &fput_needed);
1058 if (!group_file)
Ingo Molnarccff2862008-12-11 11:26:29 +01001059 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01001060 if (group_file->f_op != &perf_fops)
Ingo Molnarccff2862008-12-11 11:26:29 +01001061 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01001062
1063 group_leader = group_file->private_data;
1064 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01001065 * Do not allow a recursive hierarchy (this new sibling
1066 * becoming part of another group-sibling):
Ingo Molnar04289bb2008-12-11 08:38:42 +01001067 */
Ingo Molnarccff2862008-12-11 11:26:29 +01001068 if (group_leader->group_leader != group_leader)
1069 goto err_put_context;
1070 /*
1071 * Do not allow to attach to a group in a different
1072 * task or CPU context:
1073 */
1074 if (group_leader->ctx != ctx)
1075 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01001076 }
1077
Ingo Molnar5c92d122008-12-11 13:21:10 +01001078 ret = -EINVAL;
Ingo Molnar04289bb2008-12-11 08:38:42 +01001079 counter = perf_counter_alloc(&hw_event, cpu, group_leader);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001080 if (!counter)
1081 goto err_put_context;
1082
Thomas Gleixner0793a612008-12-04 20:12:29 +01001083 perf_install_in_context(ctx, counter, cpu);
1084
1085 ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
1086 if (ret < 0)
1087 goto err_remove_free_put_context;
1088
Ingo Molnar04289bb2008-12-11 08:38:42 +01001089out_fput:
1090 fput_light(group_file, fput_needed);
1091
Thomas Gleixner0793a612008-12-04 20:12:29 +01001092 return ret;
1093
1094err_remove_free_put_context:
1095 mutex_lock(&counter->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01001096 perf_counter_remove_from_context(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001097 mutex_unlock(&counter->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001098 kfree(counter);
1099
1100err_put_context:
1101 put_context(ctx);
1102
Ingo Molnar04289bb2008-12-11 08:38:42 +01001103 goto out_fput;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001104}
1105
Ingo Molnar04289bb2008-12-11 08:38:42 +01001106static void __cpuinit perf_counter_init_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001107{
Ingo Molnar04289bb2008-12-11 08:38:42 +01001108 struct perf_cpu_context *cpuctx;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001109
Ingo Molnar04289bb2008-12-11 08:38:42 +01001110 cpuctx = &per_cpu(perf_cpu_context, cpu);
1111 __perf_counter_init_context(&cpuctx->ctx, NULL);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001112
1113 mutex_lock(&perf_resource_mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01001114 cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001115 mutex_unlock(&perf_resource_mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01001116
Thomas Gleixner0793a612008-12-04 20:12:29 +01001117 hw_perf_counter_setup();
1118}
1119
1120#ifdef CONFIG_HOTPLUG_CPU
Ingo Molnar04289bb2008-12-11 08:38:42 +01001121static void __perf_counter_exit_cpu(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001122{
1123 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1124 struct perf_counter_context *ctx = &cpuctx->ctx;
1125 struct perf_counter *counter, *tmp;
1126
Ingo Molnar04289bb2008-12-11 08:38:42 +01001127 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry)
1128 __perf_counter_remove_from_context(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001129
1130}
Ingo Molnar04289bb2008-12-11 08:38:42 +01001131static void perf_counter_exit_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001132{
Ingo Molnar04289bb2008-12-11 08:38:42 +01001133 smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001134}
1135#else
Ingo Molnar04289bb2008-12-11 08:38:42 +01001136static inline void perf_counter_exit_cpu(int cpu) { }
Thomas Gleixner0793a612008-12-04 20:12:29 +01001137#endif
1138
1139static int __cpuinit
1140perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
1141{
1142 unsigned int cpu = (long)hcpu;
1143
1144 switch (action) {
1145
1146 case CPU_UP_PREPARE:
1147 case CPU_UP_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01001148 perf_counter_init_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001149 break;
1150
1151 case CPU_DOWN_PREPARE:
1152 case CPU_DOWN_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01001153 perf_counter_exit_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001154 break;
1155
1156 default:
1157 break;
1158 }
1159
1160 return NOTIFY_OK;
1161}
1162
1163static struct notifier_block __cpuinitdata perf_cpu_nb = {
1164 .notifier_call = perf_cpu_notify,
1165};
1166
1167static int __init perf_counter_init(void)
1168{
1169 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
1170 (void *)(long)smp_processor_id());
1171 register_cpu_notifier(&perf_cpu_nb);
1172
1173 return 0;
1174}
1175early_initcall(perf_counter_init);
1176
1177static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
1178{
1179 return sprintf(buf, "%d\n", perf_reserved_percpu);
1180}
1181
1182static ssize_t
1183perf_set_reserve_percpu(struct sysdev_class *class,
1184 const char *buf,
1185 size_t count)
1186{
1187 struct perf_cpu_context *cpuctx;
1188 unsigned long val;
1189 int err, cpu, mpt;
1190
1191 err = strict_strtoul(buf, 10, &val);
1192 if (err)
1193 return err;
1194 if (val > perf_max_counters)
1195 return -EINVAL;
1196
1197 mutex_lock(&perf_resource_mutex);
1198 perf_reserved_percpu = val;
1199 for_each_online_cpu(cpu) {
1200 cpuctx = &per_cpu(perf_cpu_context, cpu);
1201 spin_lock_irq(&cpuctx->ctx.lock);
1202 mpt = min(perf_max_counters - cpuctx->ctx.nr_counters,
1203 perf_max_counters - perf_reserved_percpu);
1204 cpuctx->max_pertask = mpt;
1205 spin_unlock_irq(&cpuctx->ctx.lock);
1206 }
1207 mutex_unlock(&perf_resource_mutex);
1208
1209 return count;
1210}
1211
1212static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
1213{
1214 return sprintf(buf, "%d\n", perf_overcommit);
1215}
1216
1217static ssize_t
1218perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
1219{
1220 unsigned long val;
1221 int err;
1222
1223 err = strict_strtoul(buf, 10, &val);
1224 if (err)
1225 return err;
1226 if (val > 1)
1227 return -EINVAL;
1228
1229 mutex_lock(&perf_resource_mutex);
1230 perf_overcommit = val;
1231 mutex_unlock(&perf_resource_mutex);
1232
1233 return count;
1234}
1235
1236static SYSDEV_CLASS_ATTR(
1237 reserve_percpu,
1238 0644,
1239 perf_show_reserve_percpu,
1240 perf_set_reserve_percpu
1241 );
1242
1243static SYSDEV_CLASS_ATTR(
1244 overcommit,
1245 0644,
1246 perf_show_overcommit,
1247 perf_set_overcommit
1248 );
1249
1250static struct attribute *perfclass_attrs[] = {
1251 &attr_reserve_percpu.attr,
1252 &attr_overcommit.attr,
1253 NULL
1254};
1255
1256static struct attribute_group perfclass_attr_group = {
1257 .attrs = perfclass_attrs,
1258 .name = "perf_counters",
1259};
1260
1261static int __init perf_counter_sysfs_init(void)
1262{
1263 return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
1264 &perfclass_attr_group);
1265}
1266device_initcall(perf_counter_sysfs_init);
1267