perf_counter tools: optionally scale counter values in perfstat mode
[linux-2.6-block.git] / kernel / perf_counter.c
CommitLineData
0793a61d
TG
1/*
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 *
7b732a75
PZ
7 *
8 * For licensing details see kernel-base/COPYING
0793a61d
TG
9 */
10
11#include <linux/fs.h>
b9cacc7b 12#include <linux/mm.h>
0793a61d
TG
13#include <linux/cpu.h>
14#include <linux/smp.h>
04289bb9 15#include <linux/file.h>
0793a61d
TG
16#include <linux/poll.h>
17#include <linux/sysfs.h>
18#include <linux/ptrace.h>
19#include <linux/percpu.h>
b9cacc7b
PZ
20#include <linux/vmstat.h>
21#include <linux/hardirq.h>
22#include <linux/rculist.h>
0793a61d
TG
23#include <linux/uaccess.h>
24#include <linux/syscalls.h>
25#include <linux/anon_inodes.h>
aa9c4c0f 26#include <linux/kernel_stat.h>
0793a61d 27#include <linux/perf_counter.h>
0a4a9391 28#include <linux/dcache.h>
0793a61d 29
4e193bd4
TB
30#include <asm/irq_regs.h>
31
0793a61d
TG
32/*
33 * Each CPU has a list of per CPU counters:
34 */
35DEFINE_PER_CPU(struct perf_cpu_context, perf_cpu_context);
36
088e2852 37int perf_max_counters __read_mostly = 1;
0793a61d
TG
38static int perf_reserved_percpu __read_mostly;
39static int perf_overcommit __read_mostly = 1;
40
41/*
42 * Mutex for (sysadmin-configurable) counter reservations:
43 */
44static DEFINE_MUTEX(perf_resource_mutex);
45
46/*
47 * Architecture provided APIs - weak aliases:
48 */
5c92d124 49extern __weak const struct hw_perf_counter_ops *
621a01ea 50hw_perf_counter_init(struct perf_counter *counter)
0793a61d 51{
ff6f0541 52 return NULL;
0793a61d
TG
53}
54
01b2838c 55u64 __weak hw_perf_save_disable(void) { return 0; }
01ea1cca 56void __weak hw_perf_restore(u64 ctrl) { barrier(); }
01d0287f 57void __weak hw_perf_counter_setup(int cpu) { barrier(); }
3cbed429
PM
58int __weak hw_perf_group_sched_in(struct perf_counter *group_leader,
59 struct perf_cpu_context *cpuctx,
60 struct perf_counter_context *ctx, int cpu)
61{
62 return 0;
63}
0793a61d 64
4eb96fcf
PM
65void __weak perf_counter_print_debug(void) { }
66
04289bb9
IM
67static void
68list_add_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
69{
70 struct perf_counter *group_leader = counter->group_leader;
71
72 /*
73 * Depending on whether it is a standalone or sibling counter,
74 * add it straight to the context's counter list, or to the group
75 * leader's sibling list:
76 */
77 if (counter->group_leader == counter)
78 list_add_tail(&counter->list_entry, &ctx->counter_list);
5c148194 79 else {
04289bb9 80 list_add_tail(&counter->list_entry, &group_leader->sibling_list);
5c148194
PZ
81 group_leader->nr_siblings++;
82 }
592903cd
PZ
83
84 list_add_rcu(&counter->event_entry, &ctx->event_list);
04289bb9
IM
85}
86
87static void
88list_del_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
89{
90 struct perf_counter *sibling, *tmp;
91
92 list_del_init(&counter->list_entry);
592903cd 93 list_del_rcu(&counter->event_entry);
04289bb9 94
5c148194
PZ
95 if (counter->group_leader != counter)
96 counter->group_leader->nr_siblings--;
97
04289bb9
IM
98 /*
99 * If this was a group counter with sibling counters then
100 * upgrade the siblings to singleton counters by adding them
101 * to the context list directly:
102 */
103 list_for_each_entry_safe(sibling, tmp,
104 &counter->sibling_list, list_entry) {
105
75564232 106 list_move_tail(&sibling->list_entry, &ctx->counter_list);
04289bb9
IM
107 sibling->group_leader = sibling;
108 }
109}
110
3b6f9e5c
PM
111static void
112counter_sched_out(struct perf_counter *counter,
113 struct perf_cpu_context *cpuctx,
114 struct perf_counter_context *ctx)
115{
116 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
117 return;
118
119 counter->state = PERF_COUNTER_STATE_INACTIVE;
53cfbf59 120 counter->tstamp_stopped = ctx->time_now;
3b6f9e5c
PM
121 counter->hw_ops->disable(counter);
122 counter->oncpu = -1;
123
124 if (!is_software_counter(counter))
125 cpuctx->active_oncpu--;
126 ctx->nr_active--;
127 if (counter->hw_event.exclusive || !cpuctx->active_oncpu)
128 cpuctx->exclusive = 0;
129}
130
d859e29f
PM
131static void
132group_sched_out(struct perf_counter *group_counter,
133 struct perf_cpu_context *cpuctx,
134 struct perf_counter_context *ctx)
135{
136 struct perf_counter *counter;
137
138 if (group_counter->state != PERF_COUNTER_STATE_ACTIVE)
139 return;
140
141 counter_sched_out(group_counter, cpuctx, ctx);
142
143 /*
144 * Schedule out siblings (if any):
145 */
146 list_for_each_entry(counter, &group_counter->sibling_list, list_entry)
147 counter_sched_out(counter, cpuctx, ctx);
148
149 if (group_counter->hw_event.exclusive)
150 cpuctx->exclusive = 0;
151}
152
0793a61d
TG
153/*
154 * Cross CPU call to remove a performance counter
155 *
156 * We disable the counter on the hardware level first. After that we
157 * remove it from the context list.
158 */
04289bb9 159static void __perf_counter_remove_from_context(void *info)
0793a61d
TG
160{
161 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
162 struct perf_counter *counter = info;
163 struct perf_counter_context *ctx = counter->ctx;
9b51f66d 164 unsigned long flags;
5c92d124 165 u64 perf_flags;
0793a61d
TG
166
167 /*
168 * If this is a task context, we need to check whether it is
169 * the current task context of this cpu. If not it has been
170 * scheduled out before the smp call arrived.
171 */
172 if (ctx->task && cpuctx->task_ctx != ctx)
173 return;
174
aa9c4c0f
IM
175 curr_rq_lock_irq_save(&flags);
176 spin_lock(&ctx->lock);
0793a61d 177
3b6f9e5c
PM
178 counter_sched_out(counter, cpuctx, ctx);
179
180 counter->task = NULL;
0793a61d
TG
181 ctx->nr_counters--;
182
183 /*
184 * Protect the list operation against NMI by disabling the
185 * counters on a global level. NOP for non NMI based counters.
186 */
01b2838c 187 perf_flags = hw_perf_save_disable();
04289bb9 188 list_del_counter(counter, ctx);
01b2838c 189 hw_perf_restore(perf_flags);
0793a61d
TG
190
191 if (!ctx->task) {
192 /*
193 * Allow more per task counters with respect to the
194 * reservation:
195 */
196 cpuctx->max_pertask =
197 min(perf_max_counters - ctx->nr_counters,
198 perf_max_counters - perf_reserved_percpu);
199 }
200
aa9c4c0f
IM
201 spin_unlock(&ctx->lock);
202 curr_rq_unlock_irq_restore(&flags);
0793a61d
TG
203}
204
205
206/*
207 * Remove the counter from a task's (or a CPU's) list of counters.
208 *
d859e29f 209 * Must be called with counter->mutex and ctx->mutex held.
0793a61d
TG
210 *
211 * CPU counters are removed with a smp call. For task counters we only
212 * call when the task is on a CPU.
213 */
04289bb9 214static void perf_counter_remove_from_context(struct perf_counter *counter)
0793a61d
TG
215{
216 struct perf_counter_context *ctx = counter->ctx;
217 struct task_struct *task = ctx->task;
218
219 if (!task) {
220 /*
221 * Per cpu counters are removed via an smp call and
222 * the removal is always sucessful.
223 */
224 smp_call_function_single(counter->cpu,
04289bb9 225 __perf_counter_remove_from_context,
0793a61d
TG
226 counter, 1);
227 return;
228 }
229
230retry:
04289bb9 231 task_oncpu_function_call(task, __perf_counter_remove_from_context,
0793a61d
TG
232 counter);
233
234 spin_lock_irq(&ctx->lock);
235 /*
236 * If the context is active we need to retry the smp call.
237 */
04289bb9 238 if (ctx->nr_active && !list_empty(&counter->list_entry)) {
0793a61d
TG
239 spin_unlock_irq(&ctx->lock);
240 goto retry;
241 }
242
243 /*
244 * The lock prevents that this context is scheduled in so we
04289bb9 245 * can remove the counter safely, if the call above did not
0793a61d
TG
246 * succeed.
247 */
04289bb9 248 if (!list_empty(&counter->list_entry)) {
0793a61d 249 ctx->nr_counters--;
04289bb9 250 list_del_counter(counter, ctx);
0793a61d
TG
251 counter->task = NULL;
252 }
253 spin_unlock_irq(&ctx->lock);
254}
255
53cfbf59
PM
256/*
257 * Get the current time for this context.
258 * If this is a task context, we use the task's task clock,
259 * or for a per-cpu context, we use the cpu clock.
260 */
261static u64 get_context_time(struct perf_counter_context *ctx, int update)
262{
263 struct task_struct *curr = ctx->task;
264
265 if (!curr)
266 return cpu_clock(smp_processor_id());
267
268 return __task_delta_exec(curr, update) + curr->se.sum_exec_runtime;
269}
270
271/*
272 * Update the record of the current time in a context.
273 */
274static void update_context_time(struct perf_counter_context *ctx, int update)
275{
276 ctx->time_now = get_context_time(ctx, update) - ctx->time_lost;
277}
278
279/*
280 * Update the total_time_enabled and total_time_running fields for a counter.
281 */
282static void update_counter_times(struct perf_counter *counter)
283{
284 struct perf_counter_context *ctx = counter->ctx;
285 u64 run_end;
286
287 if (counter->state >= PERF_COUNTER_STATE_INACTIVE) {
288 counter->total_time_enabled = ctx->time_now -
289 counter->tstamp_enabled;
290 if (counter->state == PERF_COUNTER_STATE_INACTIVE)
291 run_end = counter->tstamp_stopped;
292 else
293 run_end = ctx->time_now;
294 counter->total_time_running = run_end - counter->tstamp_running;
295 }
296}
297
298/*
299 * Update total_time_enabled and total_time_running for all counters in a group.
300 */
301static void update_group_times(struct perf_counter *leader)
302{
303 struct perf_counter *counter;
304
305 update_counter_times(leader);
306 list_for_each_entry(counter, &leader->sibling_list, list_entry)
307 update_counter_times(counter);
308}
309
d859e29f
PM
310/*
311 * Cross CPU call to disable a performance counter
312 */
313static void __perf_counter_disable(void *info)
314{
315 struct perf_counter *counter = info;
316 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
317 struct perf_counter_context *ctx = counter->ctx;
318 unsigned long flags;
319
320 /*
321 * If this is a per-task counter, need to check whether this
322 * counter's task is the current task on this cpu.
323 */
324 if (ctx->task && cpuctx->task_ctx != ctx)
325 return;
326
327 curr_rq_lock_irq_save(&flags);
328 spin_lock(&ctx->lock);
329
330 /*
331 * If the counter is on, turn it off.
332 * If it is in error state, leave it in error state.
333 */
334 if (counter->state >= PERF_COUNTER_STATE_INACTIVE) {
53cfbf59
PM
335 update_context_time(ctx, 1);
336 update_counter_times(counter);
d859e29f
PM
337 if (counter == counter->group_leader)
338 group_sched_out(counter, cpuctx, ctx);
339 else
340 counter_sched_out(counter, cpuctx, ctx);
341 counter->state = PERF_COUNTER_STATE_OFF;
342 }
343
344 spin_unlock(&ctx->lock);
345 curr_rq_unlock_irq_restore(&flags);
346}
347
348/*
349 * Disable a counter.
350 */
351static void perf_counter_disable(struct perf_counter *counter)
352{
353 struct perf_counter_context *ctx = counter->ctx;
354 struct task_struct *task = ctx->task;
355
356 if (!task) {
357 /*
358 * Disable the counter on the cpu that it's on
359 */
360 smp_call_function_single(counter->cpu, __perf_counter_disable,
361 counter, 1);
362 return;
363 }
364
365 retry:
366 task_oncpu_function_call(task, __perf_counter_disable, counter);
367
368 spin_lock_irq(&ctx->lock);
369 /*
370 * If the counter is still active, we need to retry the cross-call.
371 */
372 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
373 spin_unlock_irq(&ctx->lock);
374 goto retry;
375 }
376
377 /*
378 * Since we have the lock this context can't be scheduled
379 * in, so we can change the state safely.
380 */
53cfbf59
PM
381 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
382 update_counter_times(counter);
d859e29f 383 counter->state = PERF_COUNTER_STATE_OFF;
53cfbf59 384 }
d859e29f
PM
385
386 spin_unlock_irq(&ctx->lock);
387}
388
389/*
390 * Disable a counter and all its children.
391 */
392static void perf_counter_disable_family(struct perf_counter *counter)
393{
394 struct perf_counter *child;
395
396 perf_counter_disable(counter);
397
398 /*
399 * Lock the mutex to protect the list of children
400 */
401 mutex_lock(&counter->mutex);
402 list_for_each_entry(child, &counter->child_list, child_list)
403 perf_counter_disable(child);
404 mutex_unlock(&counter->mutex);
405}
406
235c7fc7
IM
407static int
408counter_sched_in(struct perf_counter *counter,
409 struct perf_cpu_context *cpuctx,
410 struct perf_counter_context *ctx,
411 int cpu)
412{
3b6f9e5c 413 if (counter->state <= PERF_COUNTER_STATE_OFF)
235c7fc7
IM
414 return 0;
415
416 counter->state = PERF_COUNTER_STATE_ACTIVE;
417 counter->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */
418 /*
419 * The new state must be visible before we turn it on in the hardware:
420 */
421 smp_wmb();
422
423 if (counter->hw_ops->enable(counter)) {
424 counter->state = PERF_COUNTER_STATE_INACTIVE;
425 counter->oncpu = -1;
426 return -EAGAIN;
427 }
428
53cfbf59
PM
429 counter->tstamp_running += ctx->time_now - counter->tstamp_stopped;
430
3b6f9e5c
PM
431 if (!is_software_counter(counter))
432 cpuctx->active_oncpu++;
235c7fc7
IM
433 ctx->nr_active++;
434
3b6f9e5c
PM
435 if (counter->hw_event.exclusive)
436 cpuctx->exclusive = 1;
437
235c7fc7
IM
438 return 0;
439}
440
3b6f9e5c
PM
441/*
442 * Return 1 for a group consisting entirely of software counters,
443 * 0 if the group contains any hardware counters.
444 */
445static int is_software_only_group(struct perf_counter *leader)
446{
447 struct perf_counter *counter;
448
449 if (!is_software_counter(leader))
450 return 0;
5c148194 451
3b6f9e5c
PM
452 list_for_each_entry(counter, &leader->sibling_list, list_entry)
453 if (!is_software_counter(counter))
454 return 0;
5c148194 455
3b6f9e5c
PM
456 return 1;
457}
458
459/*
460 * Work out whether we can put this counter group on the CPU now.
461 */
462static int group_can_go_on(struct perf_counter *counter,
463 struct perf_cpu_context *cpuctx,
464 int can_add_hw)
465{
466 /*
467 * Groups consisting entirely of software counters can always go on.
468 */
469 if (is_software_only_group(counter))
470 return 1;
471 /*
472 * If an exclusive group is already on, no other hardware
473 * counters can go on.
474 */
475 if (cpuctx->exclusive)
476 return 0;
477 /*
478 * If this group is exclusive and there are already
479 * counters on the CPU, it can't go on.
480 */
481 if (counter->hw_event.exclusive && cpuctx->active_oncpu)
482 return 0;
483 /*
484 * Otherwise, try to add it if all previous groups were able
485 * to go on.
486 */
487 return can_add_hw;
488}
489
53cfbf59
PM
490static void add_counter_to_ctx(struct perf_counter *counter,
491 struct perf_counter_context *ctx)
492{
493 list_add_counter(counter, ctx);
494 ctx->nr_counters++;
495 counter->prev_state = PERF_COUNTER_STATE_OFF;
496 counter->tstamp_enabled = ctx->time_now;
497 counter->tstamp_running = ctx->time_now;
498 counter->tstamp_stopped = ctx->time_now;
499}
500
0793a61d 501/*
235c7fc7 502 * Cross CPU call to install and enable a performance counter
0793a61d
TG
503 */
504static void __perf_install_in_context(void *info)
505{
506 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
507 struct perf_counter *counter = info;
508 struct perf_counter_context *ctx = counter->ctx;
d859e29f 509 struct perf_counter *leader = counter->group_leader;
0793a61d 510 int cpu = smp_processor_id();
9b51f66d 511 unsigned long flags;
5c92d124 512 u64 perf_flags;
3b6f9e5c 513 int err;
0793a61d
TG
514
515 /*
516 * If this is a task context, we need to check whether it is
517 * the current task context of this cpu. If not it has been
518 * scheduled out before the smp call arrived.
519 */
520 if (ctx->task && cpuctx->task_ctx != ctx)
521 return;
522
aa9c4c0f
IM
523 curr_rq_lock_irq_save(&flags);
524 spin_lock(&ctx->lock);
53cfbf59 525 update_context_time(ctx, 1);
0793a61d
TG
526
527 /*
528 * Protect the list operation against NMI by disabling the
529 * counters on a global level. NOP for non NMI based counters.
530 */
01b2838c 531 perf_flags = hw_perf_save_disable();
0793a61d 532
53cfbf59 533 add_counter_to_ctx(counter, ctx);
0793a61d 534
d859e29f
PM
535 /*
536 * Don't put the counter on if it is disabled or if
537 * it is in a group and the group isn't on.
538 */
539 if (counter->state != PERF_COUNTER_STATE_INACTIVE ||
540 (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE))
541 goto unlock;
542
3b6f9e5c
PM
543 /*
544 * An exclusive counter can't go on if there are already active
545 * hardware counters, and no hardware counter can go on if there
546 * is already an exclusive counter on.
547 */
d859e29f 548 if (!group_can_go_on(counter, cpuctx, 1))
3b6f9e5c
PM
549 err = -EEXIST;
550 else
551 err = counter_sched_in(counter, cpuctx, ctx, cpu);
552
d859e29f
PM
553 if (err) {
554 /*
555 * This counter couldn't go on. If it is in a group
556 * then we have to pull the whole group off.
557 * If the counter group is pinned then put it in error state.
558 */
559 if (leader != counter)
560 group_sched_out(leader, cpuctx, ctx);
53cfbf59
PM
561 if (leader->hw_event.pinned) {
562 update_group_times(leader);
d859e29f 563 leader->state = PERF_COUNTER_STATE_ERROR;
53cfbf59 564 }
d859e29f 565 }
0793a61d 566
3b6f9e5c 567 if (!err && !ctx->task && cpuctx->max_pertask)
0793a61d
TG
568 cpuctx->max_pertask--;
569
d859e29f 570 unlock:
235c7fc7
IM
571 hw_perf_restore(perf_flags);
572
aa9c4c0f
IM
573 spin_unlock(&ctx->lock);
574 curr_rq_unlock_irq_restore(&flags);
0793a61d
TG
575}
576
577/*
578 * Attach a performance counter to a context
579 *
580 * First we add the counter to the list with the hardware enable bit
581 * in counter->hw_config cleared.
582 *
583 * If the counter is attached to a task which is on a CPU we use a smp
584 * call to enable it in the task context. The task might have been
585 * scheduled away, but we check this in the smp call again.
d859e29f
PM
586 *
587 * Must be called with ctx->mutex held.
0793a61d
TG
588 */
589static void
590perf_install_in_context(struct perf_counter_context *ctx,
591 struct perf_counter *counter,
592 int cpu)
593{
594 struct task_struct *task = ctx->task;
595
0793a61d
TG
596 if (!task) {
597 /*
598 * Per cpu counters are installed via an smp call and
599 * the install is always sucessful.
600 */
601 smp_call_function_single(cpu, __perf_install_in_context,
602 counter, 1);
603 return;
604 }
605
606 counter->task = task;
607retry:
608 task_oncpu_function_call(task, __perf_install_in_context,
609 counter);
610
611 spin_lock_irq(&ctx->lock);
612 /*
0793a61d
TG
613 * we need to retry the smp call.
614 */
d859e29f 615 if (ctx->is_active && list_empty(&counter->list_entry)) {
0793a61d
TG
616 spin_unlock_irq(&ctx->lock);
617 goto retry;
618 }
619
620 /*
621 * The lock prevents that this context is scheduled in so we
622 * can add the counter safely, if it the call above did not
623 * succeed.
624 */
53cfbf59
PM
625 if (list_empty(&counter->list_entry))
626 add_counter_to_ctx(counter, ctx);
0793a61d
TG
627 spin_unlock_irq(&ctx->lock);
628}
629
d859e29f
PM
630/*
631 * Cross CPU call to enable a performance counter
632 */
633static void __perf_counter_enable(void *info)
04289bb9 634{
d859e29f
PM
635 struct perf_counter *counter = info;
636 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
637 struct perf_counter_context *ctx = counter->ctx;
638 struct perf_counter *leader = counter->group_leader;
639 unsigned long flags;
640 int err;
04289bb9 641
d859e29f
PM
642 /*
643 * If this is a per-task counter, need to check whether this
644 * counter's task is the current task on this cpu.
645 */
646 if (ctx->task && cpuctx->task_ctx != ctx)
3cbed429
PM
647 return;
648
d859e29f
PM
649 curr_rq_lock_irq_save(&flags);
650 spin_lock(&ctx->lock);
53cfbf59 651 update_context_time(ctx, 1);
d859e29f 652
c07c99b6 653 counter->prev_state = counter->state;
d859e29f
PM
654 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
655 goto unlock;
656 counter->state = PERF_COUNTER_STATE_INACTIVE;
53cfbf59 657 counter->tstamp_enabled = ctx->time_now - counter->total_time_enabled;
04289bb9
IM
658
659 /*
d859e29f
PM
660 * If the counter is in a group and isn't the group leader,
661 * then don't put it on unless the group is on.
04289bb9 662 */
d859e29f
PM
663 if (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE)
664 goto unlock;
3b6f9e5c 665
d859e29f
PM
666 if (!group_can_go_on(counter, cpuctx, 1))
667 err = -EEXIST;
668 else
669 err = counter_sched_in(counter, cpuctx, ctx,
670 smp_processor_id());
671
672 if (err) {
673 /*
674 * If this counter can't go on and it's part of a
675 * group, then the whole group has to come off.
676 */
677 if (leader != counter)
678 group_sched_out(leader, cpuctx, ctx);
53cfbf59
PM
679 if (leader->hw_event.pinned) {
680 update_group_times(leader);
d859e29f 681 leader->state = PERF_COUNTER_STATE_ERROR;
53cfbf59 682 }
d859e29f
PM
683 }
684
685 unlock:
686 spin_unlock(&ctx->lock);
687 curr_rq_unlock_irq_restore(&flags);
688}
689
690/*
691 * Enable a counter.
692 */
693static void perf_counter_enable(struct perf_counter *counter)
694{
695 struct perf_counter_context *ctx = counter->ctx;
696 struct task_struct *task = ctx->task;
697
698 if (!task) {
699 /*
700 * Enable the counter on the cpu that it's on
701 */
702 smp_call_function_single(counter->cpu, __perf_counter_enable,
703 counter, 1);
704 return;
705 }
706
707 spin_lock_irq(&ctx->lock);
708 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
709 goto out;
710
711 /*
712 * If the counter is in error state, clear that first.
713 * That way, if we see the counter in error state below, we
714 * know that it has gone back into error state, as distinct
715 * from the task having been scheduled away before the
716 * cross-call arrived.
717 */
718 if (counter->state == PERF_COUNTER_STATE_ERROR)
719 counter->state = PERF_COUNTER_STATE_OFF;
720
721 retry:
722 spin_unlock_irq(&ctx->lock);
723 task_oncpu_function_call(task, __perf_counter_enable, counter);
724
725 spin_lock_irq(&ctx->lock);
726
727 /*
728 * If the context is active and the counter is still off,
729 * we need to retry the cross-call.
730 */
731 if (ctx->is_active && counter->state == PERF_COUNTER_STATE_OFF)
732 goto retry;
733
734 /*
735 * Since we have the lock this context can't be scheduled
736 * in, so we can change the state safely.
737 */
53cfbf59 738 if (counter->state == PERF_COUNTER_STATE_OFF) {
d859e29f 739 counter->state = PERF_COUNTER_STATE_INACTIVE;
53cfbf59
PM
740 counter->tstamp_enabled = ctx->time_now -
741 counter->total_time_enabled;
742 }
d859e29f
PM
743 out:
744 spin_unlock_irq(&ctx->lock);
745}
746
747/*
748 * Enable a counter and all its children.
749 */
750static void perf_counter_enable_family(struct perf_counter *counter)
751{
752 struct perf_counter *child;
753
754 perf_counter_enable(counter);
755
756 /*
757 * Lock the mutex to protect the list of children
758 */
759 mutex_lock(&counter->mutex);
760 list_for_each_entry(child, &counter->child_list, child_list)
761 perf_counter_enable(child);
762 mutex_unlock(&counter->mutex);
04289bb9
IM
763}
764
235c7fc7
IM
765void __perf_counter_sched_out(struct perf_counter_context *ctx,
766 struct perf_cpu_context *cpuctx)
767{
768 struct perf_counter *counter;
3cbed429 769 u64 flags;
235c7fc7 770
d859e29f
PM
771 spin_lock(&ctx->lock);
772 ctx->is_active = 0;
235c7fc7 773 if (likely(!ctx->nr_counters))
d859e29f 774 goto out;
53cfbf59 775 update_context_time(ctx, 0);
235c7fc7 776
3cbed429 777 flags = hw_perf_save_disable();
235c7fc7
IM
778 if (ctx->nr_active) {
779 list_for_each_entry(counter, &ctx->counter_list, list_entry)
780 group_sched_out(counter, cpuctx, ctx);
781 }
3cbed429 782 hw_perf_restore(flags);
d859e29f 783 out:
235c7fc7
IM
784 spin_unlock(&ctx->lock);
785}
786
0793a61d
TG
787/*
788 * Called from scheduler to remove the counters of the current task,
789 * with interrupts disabled.
790 *
791 * We stop each counter and update the counter value in counter->count.
792 *
7671581f 793 * This does not protect us against NMI, but disable()
0793a61d
TG
794 * sets the disabled bit in the control field of counter _before_
795 * accessing the counter control register. If a NMI hits, then it will
796 * not restart the counter.
797 */
798void perf_counter_task_sched_out(struct task_struct *task, int cpu)
799{
800 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
801 struct perf_counter_context *ctx = &task->perf_counter_ctx;
4a0deca6 802 struct pt_regs *regs;
0793a61d
TG
803
804 if (likely(!cpuctx->task_ctx))
805 return;
806
4a0deca6
PZ
807 regs = task_pt_regs(task);
808 perf_swcounter_event(PERF_COUNT_CONTEXT_SWITCHES, 1, 1, regs);
235c7fc7
IM
809 __perf_counter_sched_out(ctx, cpuctx);
810
0793a61d
TG
811 cpuctx->task_ctx = NULL;
812}
813
235c7fc7 814static void perf_counter_cpu_sched_out(struct perf_cpu_context *cpuctx)
04289bb9 815{
235c7fc7 816 __perf_counter_sched_out(&cpuctx->ctx, cpuctx);
04289bb9
IM
817}
818
7995888f 819static int
04289bb9
IM
820group_sched_in(struct perf_counter *group_counter,
821 struct perf_cpu_context *cpuctx,
822 struct perf_counter_context *ctx,
823 int cpu)
824{
95cdd2e7 825 struct perf_counter *counter, *partial_group;
3cbed429
PM
826 int ret;
827
828 if (group_counter->state == PERF_COUNTER_STATE_OFF)
829 return 0;
830
831 ret = hw_perf_group_sched_in(group_counter, cpuctx, ctx, cpu);
832 if (ret)
833 return ret < 0 ? ret : 0;
04289bb9 834
c07c99b6 835 group_counter->prev_state = group_counter->state;
95cdd2e7
IM
836 if (counter_sched_in(group_counter, cpuctx, ctx, cpu))
837 return -EAGAIN;
04289bb9
IM
838
839 /*
840 * Schedule in siblings as one group (if any):
841 */
7995888f 842 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
c07c99b6 843 counter->prev_state = counter->state;
95cdd2e7
IM
844 if (counter_sched_in(counter, cpuctx, ctx, cpu)) {
845 partial_group = counter;
846 goto group_error;
847 }
95cdd2e7
IM
848 }
849
3cbed429 850 return 0;
95cdd2e7
IM
851
852group_error:
853 /*
854 * Groups can be scheduled in as one unit only, so undo any
855 * partial group before returning:
856 */
857 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
858 if (counter == partial_group)
859 break;
860 counter_sched_out(counter, cpuctx, ctx);
7995888f 861 }
95cdd2e7 862 counter_sched_out(group_counter, cpuctx, ctx);
7995888f 863
95cdd2e7 864 return -EAGAIN;
04289bb9
IM
865}
866
235c7fc7
IM
867static void
868__perf_counter_sched_in(struct perf_counter_context *ctx,
869 struct perf_cpu_context *cpuctx, int cpu)
0793a61d 870{
0793a61d 871 struct perf_counter *counter;
3cbed429 872 u64 flags;
dd0e6ba2 873 int can_add_hw = 1;
0793a61d 874
d859e29f
PM
875 spin_lock(&ctx->lock);
876 ctx->is_active = 1;
0793a61d 877 if (likely(!ctx->nr_counters))
d859e29f 878 goto out;
0793a61d 879
53cfbf59
PM
880 /*
881 * Add any time since the last sched_out to the lost time
882 * so it doesn't get included in the total_time_enabled and
883 * total_time_running measures for counters in the context.
884 */
885 ctx->time_lost = get_context_time(ctx, 0) - ctx->time_now;
886
3cbed429 887 flags = hw_perf_save_disable();
3b6f9e5c
PM
888
889 /*
890 * First go through the list and put on any pinned groups
891 * in order to give them the best chance of going on.
892 */
893 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
894 if (counter->state <= PERF_COUNTER_STATE_OFF ||
895 !counter->hw_event.pinned)
896 continue;
897 if (counter->cpu != -1 && counter->cpu != cpu)
898 continue;
899
900 if (group_can_go_on(counter, cpuctx, 1))
901 group_sched_in(counter, cpuctx, ctx, cpu);
902
903 /*
904 * If this pinned group hasn't been scheduled,
905 * put it in error state.
906 */
53cfbf59
PM
907 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
908 update_group_times(counter);
3b6f9e5c 909 counter->state = PERF_COUNTER_STATE_ERROR;
53cfbf59 910 }
3b6f9e5c
PM
911 }
912
04289bb9 913 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
3b6f9e5c
PM
914 /*
915 * Ignore counters in OFF or ERROR state, and
916 * ignore pinned counters since we did them already.
917 */
918 if (counter->state <= PERF_COUNTER_STATE_OFF ||
919 counter->hw_event.pinned)
920 continue;
921
04289bb9
IM
922 /*
923 * Listen to the 'cpu' scheduling filter constraint
924 * of counters:
925 */
0793a61d
TG
926 if (counter->cpu != -1 && counter->cpu != cpu)
927 continue;
928
3b6f9e5c 929 if (group_can_go_on(counter, cpuctx, can_add_hw)) {
dd0e6ba2
PM
930 if (group_sched_in(counter, cpuctx, ctx, cpu))
931 can_add_hw = 0;
3b6f9e5c 932 }
0793a61d 933 }
3cbed429 934 hw_perf_restore(flags);
d859e29f 935 out:
0793a61d 936 spin_unlock(&ctx->lock);
235c7fc7
IM
937}
938
939/*
940 * Called from scheduler to add the counters of the current task
941 * with interrupts disabled.
942 *
943 * We restore the counter value and then enable it.
944 *
945 * This does not protect us against NMI, but enable()
946 * sets the enabled bit in the control field of counter _before_
947 * accessing the counter control register. If a NMI hits, then it will
948 * keep the counter running.
949 */
950void perf_counter_task_sched_in(struct task_struct *task, int cpu)
951{
952 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
953 struct perf_counter_context *ctx = &task->perf_counter_ctx;
04289bb9 954
235c7fc7 955 __perf_counter_sched_in(ctx, cpuctx, cpu);
0793a61d
TG
956 cpuctx->task_ctx = ctx;
957}
958
235c7fc7
IM
959static void perf_counter_cpu_sched_in(struct perf_cpu_context *cpuctx, int cpu)
960{
961 struct perf_counter_context *ctx = &cpuctx->ctx;
962
963 __perf_counter_sched_in(ctx, cpuctx, cpu);
964}
965
1d1c7ddb
IM
966int perf_counter_task_disable(void)
967{
968 struct task_struct *curr = current;
969 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
970 struct perf_counter *counter;
aa9c4c0f 971 unsigned long flags;
1d1c7ddb
IM
972 u64 perf_flags;
973 int cpu;
974
975 if (likely(!ctx->nr_counters))
976 return 0;
977
aa9c4c0f 978 curr_rq_lock_irq_save(&flags);
1d1c7ddb
IM
979 cpu = smp_processor_id();
980
aa9c4c0f
IM
981 /* force the update of the task clock: */
982 __task_delta_exec(curr, 1);
983
1d1c7ddb
IM
984 perf_counter_task_sched_out(curr, cpu);
985
986 spin_lock(&ctx->lock);
987
988 /*
989 * Disable all the counters:
990 */
991 perf_flags = hw_perf_save_disable();
992
3b6f9e5c 993 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
53cfbf59
PM
994 if (counter->state != PERF_COUNTER_STATE_ERROR) {
995 update_group_times(counter);
3b6f9e5c 996 counter->state = PERF_COUNTER_STATE_OFF;
53cfbf59 997 }
3b6f9e5c 998 }
9b51f66d 999
1d1c7ddb
IM
1000 hw_perf_restore(perf_flags);
1001
1002 spin_unlock(&ctx->lock);
1003
aa9c4c0f 1004 curr_rq_unlock_irq_restore(&flags);
1d1c7ddb
IM
1005
1006 return 0;
1007}
1008
1009int perf_counter_task_enable(void)
1010{
1011 struct task_struct *curr = current;
1012 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
1013 struct perf_counter *counter;
aa9c4c0f 1014 unsigned long flags;
1d1c7ddb
IM
1015 u64 perf_flags;
1016 int cpu;
1017
1018 if (likely(!ctx->nr_counters))
1019 return 0;
1020
aa9c4c0f 1021 curr_rq_lock_irq_save(&flags);
1d1c7ddb
IM
1022 cpu = smp_processor_id();
1023
aa9c4c0f
IM
1024 /* force the update of the task clock: */
1025 __task_delta_exec(curr, 1);
1026
235c7fc7
IM
1027 perf_counter_task_sched_out(curr, cpu);
1028
1d1c7ddb
IM
1029 spin_lock(&ctx->lock);
1030
1031 /*
1032 * Disable all the counters:
1033 */
1034 perf_flags = hw_perf_save_disable();
1035
1036 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
3b6f9e5c 1037 if (counter->state > PERF_COUNTER_STATE_OFF)
1d1c7ddb 1038 continue;
6a930700 1039 counter->state = PERF_COUNTER_STATE_INACTIVE;
53cfbf59
PM
1040 counter->tstamp_enabled = ctx->time_now -
1041 counter->total_time_enabled;
aa9c4c0f 1042 counter->hw_event.disabled = 0;
1d1c7ddb
IM
1043 }
1044 hw_perf_restore(perf_flags);
1045
1046 spin_unlock(&ctx->lock);
1047
1048 perf_counter_task_sched_in(curr, cpu);
1049
aa9c4c0f 1050 curr_rq_unlock_irq_restore(&flags);
1d1c7ddb
IM
1051
1052 return 0;
1053}
1054
235c7fc7
IM
1055/*
1056 * Round-robin a context's counters:
1057 */
1058static void rotate_ctx(struct perf_counter_context *ctx)
0793a61d 1059{
0793a61d 1060 struct perf_counter *counter;
5c92d124 1061 u64 perf_flags;
0793a61d 1062
235c7fc7 1063 if (!ctx->nr_counters)
0793a61d
TG
1064 return;
1065
0793a61d 1066 spin_lock(&ctx->lock);
0793a61d 1067 /*
04289bb9 1068 * Rotate the first entry last (works just fine for group counters too):
0793a61d 1069 */
01b2838c 1070 perf_flags = hw_perf_save_disable();
04289bb9 1071 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
75564232 1072 list_move_tail(&counter->list_entry, &ctx->counter_list);
0793a61d
TG
1073 break;
1074 }
01b2838c 1075 hw_perf_restore(perf_flags);
0793a61d
TG
1076
1077 spin_unlock(&ctx->lock);
235c7fc7
IM
1078}
1079
1080void perf_counter_task_tick(struct task_struct *curr, int cpu)
1081{
1082 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
1083 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
1084 const int rotate_percpu = 0;
1085
1086 if (rotate_percpu)
1087 perf_counter_cpu_sched_out(cpuctx);
1088 perf_counter_task_sched_out(curr, cpu);
0793a61d 1089
235c7fc7
IM
1090 if (rotate_percpu)
1091 rotate_ctx(&cpuctx->ctx);
1092 rotate_ctx(ctx);
1093
1094 if (rotate_percpu)
1095 perf_counter_cpu_sched_in(cpuctx, cpu);
0793a61d
TG
1096 perf_counter_task_sched_in(curr, cpu);
1097}
1098
0793a61d
TG
1099/*
1100 * Cross CPU call to read the hardware counter
1101 */
7671581f 1102static void __read(void *info)
0793a61d 1103{
621a01ea 1104 struct perf_counter *counter = info;
53cfbf59 1105 struct perf_counter_context *ctx = counter->ctx;
aa9c4c0f 1106 unsigned long flags;
621a01ea 1107
aa9c4c0f 1108 curr_rq_lock_irq_save(&flags);
53cfbf59
PM
1109 if (ctx->is_active)
1110 update_context_time(ctx, 1);
7671581f 1111 counter->hw_ops->read(counter);
53cfbf59 1112 update_counter_times(counter);
aa9c4c0f 1113 curr_rq_unlock_irq_restore(&flags);
0793a61d
TG
1114}
1115
04289bb9 1116static u64 perf_counter_read(struct perf_counter *counter)
0793a61d
TG
1117{
1118 /*
1119 * If counter is enabled and currently active on a CPU, update the
1120 * value in the counter structure:
1121 */
6a930700 1122 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
0793a61d 1123 smp_call_function_single(counter->oncpu,
7671581f 1124 __read, counter, 1);
53cfbf59
PM
1125 } else if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
1126 update_counter_times(counter);
0793a61d
TG
1127 }
1128
ee06094f 1129 return atomic64_read(&counter->count);
0793a61d
TG
1130}
1131
0793a61d
TG
1132static void put_context(struct perf_counter_context *ctx)
1133{
1134 if (ctx->task)
1135 put_task_struct(ctx->task);
1136}
1137
1138static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
1139{
1140 struct perf_cpu_context *cpuctx;
1141 struct perf_counter_context *ctx;
1142 struct task_struct *task;
1143
1144 /*
1145 * If cpu is not a wildcard then this is a percpu counter:
1146 */
1147 if (cpu != -1) {
1148 /* Must be root to operate on a CPU counter: */
1149 if (!capable(CAP_SYS_ADMIN))
1150 return ERR_PTR(-EACCES);
1151
1152 if (cpu < 0 || cpu > num_possible_cpus())
1153 return ERR_PTR(-EINVAL);
1154
1155 /*
1156 * We could be clever and allow to attach a counter to an
1157 * offline CPU and activate it when the CPU comes up, but
1158 * that's for later.
1159 */
1160 if (!cpu_isset(cpu, cpu_online_map))
1161 return ERR_PTR(-ENODEV);
1162
1163 cpuctx = &per_cpu(perf_cpu_context, cpu);
1164 ctx = &cpuctx->ctx;
1165
0793a61d
TG
1166 return ctx;
1167 }
1168
1169 rcu_read_lock();
1170 if (!pid)
1171 task = current;
1172 else
1173 task = find_task_by_vpid(pid);
1174 if (task)
1175 get_task_struct(task);
1176 rcu_read_unlock();
1177
1178 if (!task)
1179 return ERR_PTR(-ESRCH);
1180
1181 ctx = &task->perf_counter_ctx;
1182 ctx->task = task;
1183
1184 /* Reuse ptrace permission checks for now. */
1185 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
1186 put_context(ctx);
1187 return ERR_PTR(-EACCES);
1188 }
1189
1190 return ctx;
1191}
1192
592903cd
PZ
1193static void free_counter_rcu(struct rcu_head *head)
1194{
1195 struct perf_counter *counter;
1196
1197 counter = container_of(head, struct perf_counter, rcu_head);
1198 kfree(counter);
1199}
1200
925d519a
PZ
1201static void perf_pending_sync(struct perf_counter *counter);
1202
f1600952
PZ
1203static void free_counter(struct perf_counter *counter)
1204{
925d519a
PZ
1205 perf_pending_sync(counter);
1206
e077df4f
PZ
1207 if (counter->destroy)
1208 counter->destroy(counter);
1209
f1600952
PZ
1210 call_rcu(&counter->rcu_head, free_counter_rcu);
1211}
1212
0793a61d
TG
1213/*
1214 * Called when the last reference to the file is gone.
1215 */
1216static int perf_release(struct inode *inode, struct file *file)
1217{
1218 struct perf_counter *counter = file->private_data;
1219 struct perf_counter_context *ctx = counter->ctx;
1220
1221 file->private_data = NULL;
1222
d859e29f 1223 mutex_lock(&ctx->mutex);
0793a61d
TG
1224 mutex_lock(&counter->mutex);
1225
04289bb9 1226 perf_counter_remove_from_context(counter);
0793a61d
TG
1227
1228 mutex_unlock(&counter->mutex);
d859e29f 1229 mutex_unlock(&ctx->mutex);
0793a61d 1230
f1600952 1231 free_counter(counter);
5af75917 1232 put_context(ctx);
0793a61d
TG
1233
1234 return 0;
1235}
1236
1237/*
1238 * Read the performance counter - simple non blocking version for now
1239 */
1240static ssize_t
1241perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
1242{
53cfbf59
PM
1243 u64 values[3];
1244 int n;
0793a61d 1245
3b6f9e5c
PM
1246 /*
1247 * Return end-of-file for a read on a counter that is in
1248 * error state (i.e. because it was pinned but it couldn't be
1249 * scheduled on to the CPU at some point).
1250 */
1251 if (counter->state == PERF_COUNTER_STATE_ERROR)
1252 return 0;
1253
0793a61d 1254 mutex_lock(&counter->mutex);
53cfbf59
PM
1255 values[0] = perf_counter_read(counter);
1256 n = 1;
1257 if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1258 values[n++] = counter->total_time_enabled +
1259 atomic64_read(&counter->child_total_time_enabled);
1260 if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1261 values[n++] = counter->total_time_running +
1262 atomic64_read(&counter->child_total_time_running);
0793a61d
TG
1263 mutex_unlock(&counter->mutex);
1264
53cfbf59
PM
1265 if (count < n * sizeof(u64))
1266 return -EINVAL;
1267 count = n * sizeof(u64);
1268
1269 if (copy_to_user(buf, values, count))
1270 return -EFAULT;
1271
1272 return count;
0793a61d
TG
1273}
1274
0793a61d
TG
1275static ssize_t
1276perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
1277{
1278 struct perf_counter *counter = file->private_data;
1279
7b732a75 1280 return perf_read_hw(counter, buf, count);
0793a61d
TG
1281}
1282
1283static unsigned int perf_poll(struct file *file, poll_table *wait)
1284{
1285 struct perf_counter *counter = file->private_data;
c7138f37
PZ
1286 struct perf_mmap_data *data;
1287 unsigned int events;
1288
1289 rcu_read_lock();
1290 data = rcu_dereference(counter->data);
1291 if (data)
1292 events = atomic_xchg(&data->wakeup, 0);
1293 else
1294 events = POLL_HUP;
1295 rcu_read_unlock();
0793a61d
TG
1296
1297 poll_wait(file, &counter->waitq, wait);
1298
0793a61d
TG
1299 return events;
1300}
1301
d859e29f
PM
1302static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1303{
1304 struct perf_counter *counter = file->private_data;
1305 int err = 0;
1306
1307 switch (cmd) {
1308 case PERF_COUNTER_IOC_ENABLE:
1309 perf_counter_enable_family(counter);
1310 break;
1311 case PERF_COUNTER_IOC_DISABLE:
1312 perf_counter_disable_family(counter);
1313 break;
1314 default:
1315 err = -ENOTTY;
1316 }
1317 return err;
1318}
1319
38ff667b
PZ
1320/*
1321 * Callers need to ensure there can be no nesting of this function, otherwise
1322 * the seqlock logic goes bad. We can not serialize this because the arch
1323 * code calls this from NMI context.
1324 */
1325void perf_counter_update_userpage(struct perf_counter *counter)
37d81828 1326{
38ff667b
PZ
1327 struct perf_mmap_data *data;
1328 struct perf_counter_mmap_page *userpg;
1329
1330 rcu_read_lock();
1331 data = rcu_dereference(counter->data);
1332 if (!data)
1333 goto unlock;
1334
1335 userpg = data->user_page;
37d81828 1336
7b732a75
PZ
1337 /*
1338 * Disable preemption so as to not let the corresponding user-space
1339 * spin too long if we get preempted.
1340 */
1341 preempt_disable();
37d81828
PM
1342 ++userpg->lock;
1343 smp_wmb();
1344 userpg->index = counter->hw.idx;
1345 userpg->offset = atomic64_read(&counter->count);
1346 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
1347 userpg->offset -= atomic64_read(&counter->hw.prev_count);
7b732a75 1348
37d81828
PM
1349 smp_wmb();
1350 ++userpg->lock;
7b732a75 1351 preempt_enable();
38ff667b 1352unlock:
7b732a75 1353 rcu_read_unlock();
37d81828
PM
1354}
1355
1356static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1357{
1358 struct perf_counter *counter = vma->vm_file->private_data;
7b732a75
PZ
1359 struct perf_mmap_data *data;
1360 int ret = VM_FAULT_SIGBUS;
1361
1362 rcu_read_lock();
1363 data = rcu_dereference(counter->data);
1364 if (!data)
1365 goto unlock;
1366
1367 if (vmf->pgoff == 0) {
1368 vmf->page = virt_to_page(data->user_page);
1369 } else {
1370 int nr = vmf->pgoff - 1;
37d81828 1371
7b732a75
PZ
1372 if ((unsigned)nr > data->nr_pages)
1373 goto unlock;
37d81828 1374
7b732a75
PZ
1375 vmf->page = virt_to_page(data->data_pages[nr]);
1376 }
37d81828 1377 get_page(vmf->page);
7b732a75
PZ
1378 ret = 0;
1379unlock:
1380 rcu_read_unlock();
1381
1382 return ret;
1383}
1384
1385static int perf_mmap_data_alloc(struct perf_counter *counter, int nr_pages)
1386{
1387 struct perf_mmap_data *data;
1388 unsigned long size;
1389 int i;
1390
1391 WARN_ON(atomic_read(&counter->mmap_count));
1392
1393 size = sizeof(struct perf_mmap_data);
1394 size += nr_pages * sizeof(void *);
1395
1396 data = kzalloc(size, GFP_KERNEL);
1397 if (!data)
1398 goto fail;
1399
1400 data->user_page = (void *)get_zeroed_page(GFP_KERNEL);
1401 if (!data->user_page)
1402 goto fail_user_page;
1403
1404 for (i = 0; i < nr_pages; i++) {
1405 data->data_pages[i] = (void *)get_zeroed_page(GFP_KERNEL);
1406 if (!data->data_pages[i])
1407 goto fail_data_pages;
1408 }
1409
1410 data->nr_pages = nr_pages;
1411
1412 rcu_assign_pointer(counter->data, data);
1413
37d81828 1414 return 0;
7b732a75
PZ
1415
1416fail_data_pages:
1417 for (i--; i >= 0; i--)
1418 free_page((unsigned long)data->data_pages[i]);
1419
1420 free_page((unsigned long)data->user_page);
1421
1422fail_user_page:
1423 kfree(data);
1424
1425fail:
1426 return -ENOMEM;
1427}
1428
1429static void __perf_mmap_data_free(struct rcu_head *rcu_head)
1430{
1431 struct perf_mmap_data *data = container_of(rcu_head,
1432 struct perf_mmap_data, rcu_head);
1433 int i;
1434
1435 free_page((unsigned long)data->user_page);
1436 for (i = 0; i < data->nr_pages; i++)
1437 free_page((unsigned long)data->data_pages[i]);
1438 kfree(data);
1439}
1440
1441static void perf_mmap_data_free(struct perf_counter *counter)
1442{
1443 struct perf_mmap_data *data = counter->data;
1444
1445 WARN_ON(atomic_read(&counter->mmap_count));
1446
1447 rcu_assign_pointer(counter->data, NULL);
1448 call_rcu(&data->rcu_head, __perf_mmap_data_free);
1449}
1450
1451static void perf_mmap_open(struct vm_area_struct *vma)
1452{
1453 struct perf_counter *counter = vma->vm_file->private_data;
1454
1455 atomic_inc(&counter->mmap_count);
1456}
1457
1458static void perf_mmap_close(struct vm_area_struct *vma)
1459{
1460 struct perf_counter *counter = vma->vm_file->private_data;
1461
1462 if (atomic_dec_and_mutex_lock(&counter->mmap_count,
1463 &counter->mmap_mutex)) {
1464 perf_mmap_data_free(counter);
1465 mutex_unlock(&counter->mmap_mutex);
1466 }
37d81828
PM
1467}
1468
1469static struct vm_operations_struct perf_mmap_vmops = {
7b732a75
PZ
1470 .open = perf_mmap_open,
1471 .close = perf_mmap_close,
37d81828
PM
1472 .fault = perf_mmap_fault,
1473};
1474
1475static int perf_mmap(struct file *file, struct vm_area_struct *vma)
1476{
1477 struct perf_counter *counter = file->private_data;
7b732a75
PZ
1478 unsigned long vma_size;
1479 unsigned long nr_pages;
1480 unsigned long locked, lock_limit;
1481 int ret = 0;
37d81828
PM
1482
1483 if (!(vma->vm_flags & VM_SHARED) || (vma->vm_flags & VM_WRITE))
1484 return -EINVAL;
7b732a75
PZ
1485
1486 vma_size = vma->vm_end - vma->vm_start;
1487 nr_pages = (vma_size / PAGE_SIZE) - 1;
1488
7730d865
PZ
1489 /*
1490 * If we have data pages ensure they're a power-of-two number, so we
1491 * can do bitmasks instead of modulo.
1492 */
1493 if (nr_pages != 0 && !is_power_of_2(nr_pages))
37d81828
PM
1494 return -EINVAL;
1495
7b732a75 1496 if (vma_size != PAGE_SIZE * (1 + nr_pages))
37d81828
PM
1497 return -EINVAL;
1498
7b732a75
PZ
1499 if (vma->vm_pgoff != 0)
1500 return -EINVAL;
37d81828 1501
7b732a75
PZ
1502 locked = vma_size >> PAGE_SHIFT;
1503 locked += vma->vm_mm->locked_vm;
1504
1505 lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
1506 lock_limit >>= PAGE_SHIFT;
1507
1508 if ((locked > lock_limit) && !capable(CAP_IPC_LOCK))
1509 return -EPERM;
1510
1511 mutex_lock(&counter->mmap_mutex);
1512 if (atomic_inc_not_zero(&counter->mmap_count))
1513 goto out;
1514
1515 WARN_ON(counter->data);
1516 ret = perf_mmap_data_alloc(counter, nr_pages);
1517 if (!ret)
1518 atomic_set(&counter->mmap_count, 1);
1519out:
1520 mutex_unlock(&counter->mmap_mutex);
37d81828
PM
1521
1522 vma->vm_flags &= ~VM_MAYWRITE;
1523 vma->vm_flags |= VM_RESERVED;
1524 vma->vm_ops = &perf_mmap_vmops;
7b732a75
PZ
1525
1526 return ret;
37d81828
PM
1527}
1528
0793a61d
TG
1529static const struct file_operations perf_fops = {
1530 .release = perf_release,
1531 .read = perf_read,
1532 .poll = perf_poll,
d859e29f
PM
1533 .unlocked_ioctl = perf_ioctl,
1534 .compat_ioctl = perf_ioctl,
37d81828 1535 .mmap = perf_mmap,
0793a61d
TG
1536};
1537
925d519a
PZ
1538/*
1539 * Perf counter wakeup
1540 *
1541 * If there's data, ensure we set the poll() state and publish everything
1542 * to user-space before waking everybody up.
1543 */
1544
1545void perf_counter_wakeup(struct perf_counter *counter)
1546{
1547 struct perf_mmap_data *data;
1548
1549 rcu_read_lock();
1550 data = rcu_dereference(counter->data);
1551 if (data) {
1552 (void)atomic_xchg(&data->wakeup, POLL_IN);
38ff667b
PZ
1553 /*
1554 * Ensure all data writes are issued before updating the
1555 * user-space data head information. The matching rmb()
1556 * will be in userspace after reading this value.
1557 */
1558 smp_wmb();
1559 data->user_page->data_head = atomic_read(&data->head);
925d519a
PZ
1560 }
1561 rcu_read_unlock();
1562
1563 wake_up_all(&counter->waitq);
1564}
1565
1566/*
1567 * Pending wakeups
1568 *
1569 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
1570 *
1571 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
1572 * single linked list and use cmpxchg() to add entries lockless.
1573 */
1574
1575#define PENDING_TAIL ((struct perf_wakeup_entry *)-1UL)
1576
1577static DEFINE_PER_CPU(struct perf_wakeup_entry *, perf_wakeup_head) = {
1578 PENDING_TAIL,
1579};
1580
1581static void perf_pending_queue(struct perf_counter *counter)
1582{
1583 struct perf_wakeup_entry **head;
1584 struct perf_wakeup_entry *prev, *next;
1585
1586 if (cmpxchg(&counter->wakeup.next, NULL, PENDING_TAIL) != NULL)
1587 return;
1588
1589 head = &get_cpu_var(perf_wakeup_head);
1590
1591 do {
1592 prev = counter->wakeup.next = *head;
1593 next = &counter->wakeup;
1594 } while (cmpxchg(head, prev, next) != prev);
1595
1596 set_perf_counter_pending();
1597
1598 put_cpu_var(perf_wakeup_head);
1599}
1600
1601static int __perf_pending_run(void)
1602{
1603 struct perf_wakeup_entry *list;
1604 int nr = 0;
1605
1606 list = xchg(&__get_cpu_var(perf_wakeup_head), PENDING_TAIL);
1607 while (list != PENDING_TAIL) {
1608 struct perf_counter *counter = container_of(list,
1609 struct perf_counter, wakeup);
1610
1611 list = list->next;
1612
1613 counter->wakeup.next = NULL;
1614 /*
1615 * Ensure we observe the unqueue before we issue the wakeup,
1616 * so that we won't be waiting forever.
1617 * -- see perf_not_pending().
1618 */
1619 smp_wmb();
1620
1621 perf_counter_wakeup(counter);
1622 nr++;
1623 }
1624
1625 return nr;
1626}
1627
1628static inline int perf_not_pending(struct perf_counter *counter)
1629{
1630 /*
1631 * If we flush on whatever cpu we run, there is a chance we don't
1632 * need to wait.
1633 */
1634 get_cpu();
1635 __perf_pending_run();
1636 put_cpu();
1637
1638 /*
1639 * Ensure we see the proper queue state before going to sleep
1640 * so that we do not miss the wakeup. -- see perf_pending_handle()
1641 */
1642 smp_rmb();
1643 return counter->wakeup.next == NULL;
1644}
1645
1646static void perf_pending_sync(struct perf_counter *counter)
1647{
1648 wait_event(counter->waitq, perf_not_pending(counter));
1649}
1650
1651void perf_counter_do_pending(void)
1652{
1653 __perf_pending_run();
1654}
1655
0322cd6e
PZ
1656/*
1657 * Output
1658 */
1659
b9cacc7b
PZ
1660struct perf_output_handle {
1661 struct perf_counter *counter;
1662 struct perf_mmap_data *data;
1663 unsigned int offset;
63e35b25 1664 unsigned int head;
b9cacc7b
PZ
1665 int wakeup;
1666};
1667
1668static int perf_output_begin(struct perf_output_handle *handle,
1669 struct perf_counter *counter, unsigned int size)
0322cd6e 1670{
7b732a75 1671 struct perf_mmap_data *data;
b9cacc7b 1672 unsigned int offset, head;
0322cd6e 1673
7b732a75 1674 rcu_read_lock();
7b732a75
PZ
1675 data = rcu_dereference(counter->data);
1676 if (!data)
1677 goto out;
1678
1679 if (!data->nr_pages)
1680 goto out;
1681
7b732a75
PZ
1682 do {
1683 offset = head = atomic_read(&data->head);
c7138f37 1684 head += size;
7b732a75
PZ
1685 } while (atomic_cmpxchg(&data->head, offset, head) != offset);
1686
b9cacc7b
PZ
1687 handle->counter = counter;
1688 handle->data = data;
1689 handle->offset = offset;
63e35b25 1690 handle->head = head;
b9cacc7b 1691 handle->wakeup = (offset >> PAGE_SHIFT) != (head >> PAGE_SHIFT);
0322cd6e 1692
b9cacc7b 1693 return 0;
7b732a75 1694
b9cacc7b
PZ
1695out:
1696 rcu_read_unlock();
7b732a75 1697
b9cacc7b
PZ
1698 return -ENOSPC;
1699}
7b732a75 1700
b9cacc7b
PZ
1701static void perf_output_copy(struct perf_output_handle *handle,
1702 void *buf, unsigned int len)
1703{
1704 unsigned int pages_mask;
1705 unsigned int offset;
1706 unsigned int size;
1707 void **pages;
1708
1709 offset = handle->offset;
1710 pages_mask = handle->data->nr_pages - 1;
1711 pages = handle->data->data_pages;
1712
1713 do {
1714 unsigned int page_offset;
1715 int nr;
1716
1717 nr = (offset >> PAGE_SHIFT) & pages_mask;
1718 page_offset = offset & (PAGE_SIZE - 1);
1719 size = min_t(unsigned int, PAGE_SIZE - page_offset, len);
1720
1721 memcpy(pages[nr] + page_offset, buf, size);
1722
1723 len -= size;
1724 buf += size;
1725 offset += size;
1726 } while (len);
1727
1728 handle->offset = offset;
63e35b25
PZ
1729
1730 WARN_ON_ONCE(handle->offset > handle->head);
b9cacc7b
PZ
1731}
1732
5c148194
PZ
1733#define perf_output_put(handle, x) \
1734 perf_output_copy((handle), &(x), sizeof(x))
1735
b9cacc7b
PZ
1736static void perf_output_end(struct perf_output_handle *handle, int nmi)
1737{
1738 if (handle->wakeup) {
925d519a
PZ
1739 if (nmi)
1740 perf_pending_queue(handle->counter);
1741 else
1742 perf_counter_wakeup(handle->counter);
0322cd6e 1743 }
7b732a75 1744 rcu_read_unlock();
b9cacc7b
PZ
1745}
1746
1747static int perf_output_write(struct perf_counter *counter, int nmi,
1748 void *buf, ssize_t size)
1749{
1750 struct perf_output_handle handle;
1751 int ret;
7b732a75 1752
b9cacc7b
PZ
1753 ret = perf_output_begin(&handle, counter, size);
1754 if (ret)
1755 goto out;
1756
1757 perf_output_copy(&handle, buf, size);
1758 perf_output_end(&handle, nmi);
1759
1760out:
7b732a75
PZ
1761 return ret;
1762}
1763
1764static void perf_output_simple(struct perf_counter *counter,
1765 int nmi, struct pt_regs *regs)
1766{
ea5d20cf 1767 unsigned int size;
5c148194
PZ
1768 struct {
1769 struct perf_event_header header;
1770 u64 ip;
ea5d20cf 1771 u32 pid, tid;
5c148194 1772 } event;
7b732a75 1773
5c148194 1774 event.header.type = PERF_EVENT_IP;
5c148194 1775 event.ip = instruction_pointer(regs);
7b732a75 1776
ea5d20cf
PZ
1777 size = sizeof(event);
1778
1779 if (counter->hw_event.include_tid) {
1780 /* namespace issues */
1781 event.pid = current->group_leader->pid;
1782 event.tid = current->pid;
1783
1784 event.header.type |= __PERF_EVENT_TID;
1785 } else
1786 size -= sizeof(u64);
1787
1788 event.header.size = size;
1789
1790 perf_output_write(counter, nmi, &event, size);
0322cd6e
PZ
1791}
1792
7b732a75 1793static void perf_output_group(struct perf_counter *counter, int nmi)
0322cd6e 1794{
5c148194
PZ
1795 struct perf_output_handle handle;
1796 struct perf_event_header header;
0322cd6e 1797 struct perf_counter *leader, *sub;
5c148194
PZ
1798 unsigned int size;
1799 struct {
1800 u64 event;
1801 u64 counter;
1802 } entry;
1803 int ret;
1804
1805 size = sizeof(header) + counter->nr_siblings * sizeof(entry);
1806
1807 ret = perf_output_begin(&handle, counter, size);
1808 if (ret)
1809 return;
1810
1811 header.type = PERF_EVENT_GROUP;
1812 header.size = size;
1813
1814 perf_output_put(&handle, header);
0322cd6e
PZ
1815
1816 leader = counter->group_leader;
1817 list_for_each_entry(sub, &leader->sibling_list, list_entry) {
1818 if (sub != counter)
1819 sub->hw_ops->read(sub);
7b732a75
PZ
1820
1821 entry.event = sub->hw_event.config;
1822 entry.counter = atomic64_read(&sub->count);
1823
5c148194 1824 perf_output_put(&handle, entry);
0322cd6e 1825 }
5c148194
PZ
1826
1827 perf_output_end(&handle, nmi);
0322cd6e
PZ
1828}
1829
1830void perf_counter_output(struct perf_counter *counter,
1831 int nmi, struct pt_regs *regs)
1832{
1833 switch (counter->hw_event.record_type) {
1834 case PERF_RECORD_SIMPLE:
1835 return;
1836
1837 case PERF_RECORD_IRQ:
7b732a75 1838 perf_output_simple(counter, nmi, regs);
0322cd6e
PZ
1839 break;
1840
1841 case PERF_RECORD_GROUP:
7b732a75 1842 perf_output_group(counter, nmi);
0322cd6e
PZ
1843 break;
1844 }
0322cd6e
PZ
1845}
1846
0a4a9391
PZ
1847/*
1848 * mmap tracking
1849 */
1850
1851struct perf_mmap_event {
1852 struct file *file;
1853 char *file_name;
1854 int file_size;
1855
1856 struct {
1857 struct perf_event_header header;
1858
1859 u32 pid;
1860 u32 tid;
1861 u64 start;
1862 u64 len;
1863 u64 pgoff;
1864 } event;
1865};
1866
1867static void perf_counter_mmap_output(struct perf_counter *counter,
1868 struct perf_mmap_event *mmap_event)
1869{
1870 struct perf_output_handle handle;
1871 int size = mmap_event->event.header.size;
1872 int ret = perf_output_begin(&handle, counter, size);
1873
1874 if (ret)
1875 return;
1876
1877 perf_output_put(&handle, mmap_event->event);
1878 perf_output_copy(&handle, mmap_event->file_name,
1879 mmap_event->file_size);
1880 perf_output_end(&handle, 0);
1881}
1882
1883static int perf_counter_mmap_match(struct perf_counter *counter,
1884 struct perf_mmap_event *mmap_event)
1885{
1886 if (counter->hw_event.mmap &&
1887 mmap_event->event.header.type == PERF_EVENT_MMAP)
1888 return 1;
1889
1890 if (counter->hw_event.munmap &&
1891 mmap_event->event.header.type == PERF_EVENT_MUNMAP)
1892 return 1;
1893
1894 return 0;
1895}
1896
1897static void perf_counter_mmap_ctx(struct perf_counter_context *ctx,
1898 struct perf_mmap_event *mmap_event)
1899{
1900 struct perf_counter *counter;
1901
1902 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
1903 return;
1904
1905 rcu_read_lock();
1906 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
1907 if (perf_counter_mmap_match(counter, mmap_event))
1908 perf_counter_mmap_output(counter, mmap_event);
1909 }
1910 rcu_read_unlock();
1911}
1912
1913static void perf_counter_mmap_event(struct perf_mmap_event *mmap_event)
1914{
1915 struct perf_cpu_context *cpuctx;
1916 struct file *file = mmap_event->file;
1917 unsigned int size;
1918 char tmp[16];
1919 char *buf = NULL;
1920 char *name;
1921
1922 if (file) {
1923 buf = kzalloc(PATH_MAX, GFP_KERNEL);
1924 if (!buf) {
1925 name = strncpy(tmp, "//enomem", sizeof(tmp));
1926 goto got_name;
1927 }
1928 name = dentry_path(file->f_dentry, buf, PATH_MAX);
1929 if (IS_ERR(name)) {
1930 name = strncpy(tmp, "//toolong", sizeof(tmp));
1931 goto got_name;
1932 }
1933 } else {
1934 name = strncpy(tmp, "//anon", sizeof(tmp));
1935 goto got_name;
1936 }
1937
1938got_name:
1939 size = ALIGN(strlen(name), sizeof(u64));
1940
1941 mmap_event->file_name = name;
1942 mmap_event->file_size = size;
1943
1944 mmap_event->event.header.size = sizeof(mmap_event->event) + size;
1945
1946 cpuctx = &get_cpu_var(perf_cpu_context);
1947 perf_counter_mmap_ctx(&cpuctx->ctx, mmap_event);
1948 put_cpu_var(perf_cpu_context);
1949
1950 perf_counter_mmap_ctx(&current->perf_counter_ctx, mmap_event);
1951
1952 kfree(buf);
1953}
1954
1955void perf_counter_mmap(unsigned long addr, unsigned long len,
1956 unsigned long pgoff, struct file *file)
1957{
1958 struct perf_mmap_event mmap_event = {
1959 .file = file,
1960 .event = {
1961 .header = { .type = PERF_EVENT_MMAP, },
1962 .pid = current->group_leader->pid,
1963 .tid = current->pid,
1964 .start = addr,
1965 .len = len,
1966 .pgoff = pgoff,
1967 },
1968 };
1969
1970 perf_counter_mmap_event(&mmap_event);
1971}
1972
1973void perf_counter_munmap(unsigned long addr, unsigned long len,
1974 unsigned long pgoff, struct file *file)
1975{
1976 struct perf_mmap_event mmap_event = {
1977 .file = file,
1978 .event = {
1979 .header = { .type = PERF_EVENT_MUNMAP, },
1980 .pid = current->group_leader->pid,
1981 .tid = current->pid,
1982 .start = addr,
1983 .len = len,
1984 .pgoff = pgoff,
1985 },
1986 };
1987
1988 perf_counter_mmap_event(&mmap_event);
1989}
1990
15dbf27c
PZ
1991/*
1992 * Generic software counter infrastructure
1993 */
1994
1995static void perf_swcounter_update(struct perf_counter *counter)
1996{
1997 struct hw_perf_counter *hwc = &counter->hw;
1998 u64 prev, now;
1999 s64 delta;
2000
2001again:
2002 prev = atomic64_read(&hwc->prev_count);
2003 now = atomic64_read(&hwc->count);
2004 if (atomic64_cmpxchg(&hwc->prev_count, prev, now) != prev)
2005 goto again;
2006
2007 delta = now - prev;
2008
2009 atomic64_add(delta, &counter->count);
2010 atomic64_sub(delta, &hwc->period_left);
2011}
2012
2013static void perf_swcounter_set_period(struct perf_counter *counter)
2014{
2015 struct hw_perf_counter *hwc = &counter->hw;
2016 s64 left = atomic64_read(&hwc->period_left);
2017 s64 period = hwc->irq_period;
2018
2019 if (unlikely(left <= -period)) {
2020 left = period;
2021 atomic64_set(&hwc->period_left, left);
2022 }
2023
2024 if (unlikely(left <= 0)) {
2025 left += period;
2026 atomic64_add(period, &hwc->period_left);
2027 }
2028
2029 atomic64_set(&hwc->prev_count, -left);
2030 atomic64_set(&hwc->count, -left);
2031}
2032
d6d020e9
PZ
2033static enum hrtimer_restart perf_swcounter_hrtimer(struct hrtimer *hrtimer)
2034{
2035 struct perf_counter *counter;
2036 struct pt_regs *regs;
2037
2038 counter = container_of(hrtimer, struct perf_counter, hw.hrtimer);
2039 counter->hw_ops->read(counter);
2040
2041 regs = get_irq_regs();
2042 /*
2043 * In case we exclude kernel IPs or are somehow not in interrupt
2044 * context, provide the next best thing, the user IP.
2045 */
2046 if ((counter->hw_event.exclude_kernel || !regs) &&
2047 !counter->hw_event.exclude_user)
2048 regs = task_pt_regs(current);
2049
2050 if (regs)
0322cd6e 2051 perf_counter_output(counter, 0, regs);
d6d020e9
PZ
2052
2053 hrtimer_forward_now(hrtimer, ns_to_ktime(counter->hw.irq_period));
2054
2055 return HRTIMER_RESTART;
2056}
2057
2058static void perf_swcounter_overflow(struct perf_counter *counter,
2059 int nmi, struct pt_regs *regs)
2060{
b8e83514
PZ
2061 perf_swcounter_update(counter);
2062 perf_swcounter_set_period(counter);
0322cd6e 2063 perf_counter_output(counter, nmi, regs);
d6d020e9
PZ
2064}
2065
15dbf27c 2066static int perf_swcounter_match(struct perf_counter *counter,
b8e83514
PZ
2067 enum perf_event_types type,
2068 u32 event, struct pt_regs *regs)
15dbf27c
PZ
2069{
2070 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
2071 return 0;
2072
f4a2deb4 2073 if (perf_event_raw(&counter->hw_event))
b8e83514
PZ
2074 return 0;
2075
f4a2deb4 2076 if (perf_event_type(&counter->hw_event) != type)
15dbf27c
PZ
2077 return 0;
2078
f4a2deb4 2079 if (perf_event_id(&counter->hw_event) != event)
15dbf27c
PZ
2080 return 0;
2081
2082 if (counter->hw_event.exclude_user && user_mode(regs))
2083 return 0;
2084
2085 if (counter->hw_event.exclude_kernel && !user_mode(regs))
2086 return 0;
2087
2088 return 1;
2089}
2090
d6d020e9
PZ
2091static void perf_swcounter_add(struct perf_counter *counter, u64 nr,
2092 int nmi, struct pt_regs *regs)
2093{
2094 int neg = atomic64_add_negative(nr, &counter->hw.count);
2095 if (counter->hw.irq_period && !neg)
2096 perf_swcounter_overflow(counter, nmi, regs);
2097}
2098
15dbf27c 2099static void perf_swcounter_ctx_event(struct perf_counter_context *ctx,
b8e83514
PZ
2100 enum perf_event_types type, u32 event,
2101 u64 nr, int nmi, struct pt_regs *regs)
15dbf27c
PZ
2102{
2103 struct perf_counter *counter;
15dbf27c 2104
01ef09d9 2105 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
15dbf27c
PZ
2106 return;
2107
592903cd
PZ
2108 rcu_read_lock();
2109 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
b8e83514 2110 if (perf_swcounter_match(counter, type, event, regs))
d6d020e9 2111 perf_swcounter_add(counter, nr, nmi, regs);
15dbf27c 2112 }
592903cd 2113 rcu_read_unlock();
15dbf27c
PZ
2114}
2115
96f6d444
PZ
2116static int *perf_swcounter_recursion_context(struct perf_cpu_context *cpuctx)
2117{
2118 if (in_nmi())
2119 return &cpuctx->recursion[3];
2120
2121 if (in_irq())
2122 return &cpuctx->recursion[2];
2123
2124 if (in_softirq())
2125 return &cpuctx->recursion[1];
2126
2127 return &cpuctx->recursion[0];
2128}
2129
b8e83514
PZ
2130static void __perf_swcounter_event(enum perf_event_types type, u32 event,
2131 u64 nr, int nmi, struct pt_regs *regs)
15dbf27c
PZ
2132{
2133 struct perf_cpu_context *cpuctx = &get_cpu_var(perf_cpu_context);
96f6d444
PZ
2134 int *recursion = perf_swcounter_recursion_context(cpuctx);
2135
2136 if (*recursion)
2137 goto out;
2138
2139 (*recursion)++;
2140 barrier();
15dbf27c 2141
b8e83514
PZ
2142 perf_swcounter_ctx_event(&cpuctx->ctx, type, event, nr, nmi, regs);
2143 if (cpuctx->task_ctx) {
2144 perf_swcounter_ctx_event(cpuctx->task_ctx, type, event,
2145 nr, nmi, regs);
2146 }
15dbf27c 2147
96f6d444
PZ
2148 barrier();
2149 (*recursion)--;
2150
2151out:
15dbf27c
PZ
2152 put_cpu_var(perf_cpu_context);
2153}
2154
b8e83514
PZ
2155void perf_swcounter_event(u32 event, u64 nr, int nmi, struct pt_regs *regs)
2156{
2157 __perf_swcounter_event(PERF_TYPE_SOFTWARE, event, nr, nmi, regs);
2158}
2159
15dbf27c
PZ
2160static void perf_swcounter_read(struct perf_counter *counter)
2161{
2162 perf_swcounter_update(counter);
2163}
2164
2165static int perf_swcounter_enable(struct perf_counter *counter)
2166{
2167 perf_swcounter_set_period(counter);
2168 return 0;
2169}
2170
2171static void perf_swcounter_disable(struct perf_counter *counter)
2172{
2173 perf_swcounter_update(counter);
2174}
2175
ac17dc8e
PZ
2176static const struct hw_perf_counter_ops perf_ops_generic = {
2177 .enable = perf_swcounter_enable,
2178 .disable = perf_swcounter_disable,
2179 .read = perf_swcounter_read,
2180};
2181
15dbf27c
PZ
2182/*
2183 * Software counter: cpu wall time clock
2184 */
2185
9abf8a08
PM
2186static void cpu_clock_perf_counter_update(struct perf_counter *counter)
2187{
2188 int cpu = raw_smp_processor_id();
2189 s64 prev;
2190 u64 now;
2191
2192 now = cpu_clock(cpu);
2193 prev = atomic64_read(&counter->hw.prev_count);
2194 atomic64_set(&counter->hw.prev_count, now);
2195 atomic64_add(now - prev, &counter->count);
2196}
2197
d6d020e9
PZ
2198static int cpu_clock_perf_counter_enable(struct perf_counter *counter)
2199{
2200 struct hw_perf_counter *hwc = &counter->hw;
2201 int cpu = raw_smp_processor_id();
2202
2203 atomic64_set(&hwc->prev_count, cpu_clock(cpu));
039fc91e
PZ
2204 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2205 hwc->hrtimer.function = perf_swcounter_hrtimer;
d6d020e9 2206 if (hwc->irq_period) {
d6d020e9
PZ
2207 __hrtimer_start_range_ns(&hwc->hrtimer,
2208 ns_to_ktime(hwc->irq_period), 0,
2209 HRTIMER_MODE_REL, 0);
2210 }
2211
2212 return 0;
2213}
2214
5c92d124
IM
2215static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
2216{
d6d020e9 2217 hrtimer_cancel(&counter->hw.hrtimer);
9abf8a08 2218 cpu_clock_perf_counter_update(counter);
5c92d124
IM
2219}
2220
2221static void cpu_clock_perf_counter_read(struct perf_counter *counter)
2222{
9abf8a08 2223 cpu_clock_perf_counter_update(counter);
5c92d124
IM
2224}
2225
2226static const struct hw_perf_counter_ops perf_ops_cpu_clock = {
7671581f
IM
2227 .enable = cpu_clock_perf_counter_enable,
2228 .disable = cpu_clock_perf_counter_disable,
2229 .read = cpu_clock_perf_counter_read,
5c92d124
IM
2230};
2231
15dbf27c
PZ
2232/*
2233 * Software counter: task time clock
2234 */
2235
aa9c4c0f
IM
2236/*
2237 * Called from within the scheduler:
2238 */
2239static u64 task_clock_perf_counter_val(struct perf_counter *counter, int update)
bae43c99 2240{
aa9c4c0f
IM
2241 struct task_struct *curr = counter->task;
2242 u64 delta;
2243
aa9c4c0f
IM
2244 delta = __task_delta_exec(curr, update);
2245
2246 return curr->se.sum_exec_runtime + delta;
2247}
2248
2249static void task_clock_perf_counter_update(struct perf_counter *counter, u64 now)
2250{
2251 u64 prev;
8cb391e8
IM
2252 s64 delta;
2253
2254 prev = atomic64_read(&counter->hw.prev_count);
8cb391e8
IM
2255
2256 atomic64_set(&counter->hw.prev_count, now);
2257
2258 delta = now - prev;
8cb391e8
IM
2259
2260 atomic64_add(delta, &counter->count);
bae43c99
IM
2261}
2262
95cdd2e7 2263static int task_clock_perf_counter_enable(struct perf_counter *counter)
8cb391e8 2264{
d6d020e9
PZ
2265 struct hw_perf_counter *hwc = &counter->hw;
2266
2267 atomic64_set(&hwc->prev_count, task_clock_perf_counter_val(counter, 0));
039fc91e
PZ
2268 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2269 hwc->hrtimer.function = perf_swcounter_hrtimer;
d6d020e9 2270 if (hwc->irq_period) {
d6d020e9
PZ
2271 __hrtimer_start_range_ns(&hwc->hrtimer,
2272 ns_to_ktime(hwc->irq_period), 0,
2273 HRTIMER_MODE_REL, 0);
2274 }
95cdd2e7
IM
2275
2276 return 0;
8cb391e8
IM
2277}
2278
2279static void task_clock_perf_counter_disable(struct perf_counter *counter)
bae43c99 2280{
d6d020e9
PZ
2281 hrtimer_cancel(&counter->hw.hrtimer);
2282 task_clock_perf_counter_update(counter,
2283 task_clock_perf_counter_val(counter, 0));
2284}
aa9c4c0f 2285
d6d020e9
PZ
2286static void task_clock_perf_counter_read(struct perf_counter *counter)
2287{
2288 task_clock_perf_counter_update(counter,
2289 task_clock_perf_counter_val(counter, 1));
bae43c99
IM
2290}
2291
2292static const struct hw_perf_counter_ops perf_ops_task_clock = {
7671581f
IM
2293 .enable = task_clock_perf_counter_enable,
2294 .disable = task_clock_perf_counter_disable,
2295 .read = task_clock_perf_counter_read,
bae43c99
IM
2296};
2297
15dbf27c
PZ
2298/*
2299 * Software counter: cpu migrations
2300 */
2301
23a185ca 2302static inline u64 get_cpu_migrations(struct perf_counter *counter)
6c594c21 2303{
23a185ca
PM
2304 struct task_struct *curr = counter->ctx->task;
2305
2306 if (curr)
2307 return curr->se.nr_migrations;
2308 return cpu_nr_migrations(smp_processor_id());
6c594c21
IM
2309}
2310
2311static void cpu_migrations_perf_counter_update(struct perf_counter *counter)
2312{
2313 u64 prev, now;
2314 s64 delta;
2315
2316 prev = atomic64_read(&counter->hw.prev_count);
23a185ca 2317 now = get_cpu_migrations(counter);
6c594c21
IM
2318
2319 atomic64_set(&counter->hw.prev_count, now);
2320
2321 delta = now - prev;
6c594c21
IM
2322
2323 atomic64_add(delta, &counter->count);
2324}
2325
2326static void cpu_migrations_perf_counter_read(struct perf_counter *counter)
2327{
2328 cpu_migrations_perf_counter_update(counter);
2329}
2330
95cdd2e7 2331static int cpu_migrations_perf_counter_enable(struct perf_counter *counter)
6c594c21 2332{
c07c99b6
PM
2333 if (counter->prev_state <= PERF_COUNTER_STATE_OFF)
2334 atomic64_set(&counter->hw.prev_count,
2335 get_cpu_migrations(counter));
95cdd2e7 2336 return 0;
6c594c21
IM
2337}
2338
2339static void cpu_migrations_perf_counter_disable(struct perf_counter *counter)
2340{
2341 cpu_migrations_perf_counter_update(counter);
2342}
2343
2344static const struct hw_perf_counter_ops perf_ops_cpu_migrations = {
7671581f
IM
2345 .enable = cpu_migrations_perf_counter_enable,
2346 .disable = cpu_migrations_perf_counter_disable,
2347 .read = cpu_migrations_perf_counter_read,
6c594c21
IM
2348};
2349
e077df4f
PZ
2350#ifdef CONFIG_EVENT_PROFILE
2351void perf_tpcounter_event(int event_id)
2352{
b8e83514
PZ
2353 struct pt_regs *regs = get_irq_regs();
2354
2355 if (!regs)
2356 regs = task_pt_regs(current);
2357
2358 __perf_swcounter_event(PERF_TYPE_TRACEPOINT, event_id, 1, 1, regs);
e077df4f
PZ
2359}
2360
2361extern int ftrace_profile_enable(int);
2362extern void ftrace_profile_disable(int);
2363
2364static void tp_perf_counter_destroy(struct perf_counter *counter)
2365{
f4a2deb4 2366 ftrace_profile_disable(perf_event_id(&counter->hw_event));
e077df4f
PZ
2367}
2368
2369static const struct hw_perf_counter_ops *
2370tp_perf_counter_init(struct perf_counter *counter)
2371{
f4a2deb4 2372 int event_id = perf_event_id(&counter->hw_event);
e077df4f
PZ
2373 int ret;
2374
2375 ret = ftrace_profile_enable(event_id);
2376 if (ret)
2377 return NULL;
2378
2379 counter->destroy = tp_perf_counter_destroy;
b8e83514 2380 counter->hw.irq_period = counter->hw_event.irq_period;
e077df4f
PZ
2381
2382 return &perf_ops_generic;
2383}
2384#else
2385static const struct hw_perf_counter_ops *
2386tp_perf_counter_init(struct perf_counter *counter)
2387{
2388 return NULL;
2389}
2390#endif
2391
5c92d124
IM
2392static const struct hw_perf_counter_ops *
2393sw_perf_counter_init(struct perf_counter *counter)
2394{
15dbf27c 2395 struct perf_counter_hw_event *hw_event = &counter->hw_event;
5c92d124 2396 const struct hw_perf_counter_ops *hw_ops = NULL;
15dbf27c 2397 struct hw_perf_counter *hwc = &counter->hw;
5c92d124 2398
0475f9ea
PM
2399 /*
2400 * Software counters (currently) can't in general distinguish
2401 * between user, kernel and hypervisor events.
2402 * However, context switches and cpu migrations are considered
2403 * to be kernel events, and page faults are never hypervisor
2404 * events.
2405 */
f4a2deb4 2406 switch (perf_event_id(&counter->hw_event)) {
5c92d124 2407 case PERF_COUNT_CPU_CLOCK:
d6d020e9
PZ
2408 hw_ops = &perf_ops_cpu_clock;
2409
2410 if (hw_event->irq_period && hw_event->irq_period < 10000)
2411 hw_event->irq_period = 10000;
5c92d124 2412 break;
bae43c99 2413 case PERF_COUNT_TASK_CLOCK:
23a185ca
PM
2414 /*
2415 * If the user instantiates this as a per-cpu counter,
2416 * use the cpu_clock counter instead.
2417 */
2418 if (counter->ctx->task)
2419 hw_ops = &perf_ops_task_clock;
2420 else
2421 hw_ops = &perf_ops_cpu_clock;
d6d020e9
PZ
2422
2423 if (hw_event->irq_period && hw_event->irq_period < 10000)
2424 hw_event->irq_period = 10000;
bae43c99 2425 break;
e06c61a8 2426 case PERF_COUNT_PAGE_FAULTS:
ac17dc8e
PZ
2427 case PERF_COUNT_PAGE_FAULTS_MIN:
2428 case PERF_COUNT_PAGE_FAULTS_MAJ:
5d6a27d8 2429 case PERF_COUNT_CONTEXT_SWITCHES:
4a0deca6 2430 hw_ops = &perf_ops_generic;
5d6a27d8 2431 break;
6c594c21 2432 case PERF_COUNT_CPU_MIGRATIONS:
0475f9ea
PM
2433 if (!counter->hw_event.exclude_kernel)
2434 hw_ops = &perf_ops_cpu_migrations;
6c594c21 2435 break;
5c92d124 2436 }
15dbf27c
PZ
2437
2438 if (hw_ops)
2439 hwc->irq_period = hw_event->irq_period;
2440
5c92d124
IM
2441 return hw_ops;
2442}
2443
0793a61d
TG
2444/*
2445 * Allocate and initialize a counter structure
2446 */
2447static struct perf_counter *
04289bb9
IM
2448perf_counter_alloc(struct perf_counter_hw_event *hw_event,
2449 int cpu,
23a185ca 2450 struct perf_counter_context *ctx,
9b51f66d
IM
2451 struct perf_counter *group_leader,
2452 gfp_t gfpflags)
0793a61d 2453{
5c92d124 2454 const struct hw_perf_counter_ops *hw_ops;
621a01ea 2455 struct perf_counter *counter;
d5d2bc0d 2456 long err;
0793a61d 2457
9b51f66d 2458 counter = kzalloc(sizeof(*counter), gfpflags);
0793a61d 2459 if (!counter)
d5d2bc0d 2460 return ERR_PTR(-ENOMEM);
0793a61d 2461
04289bb9
IM
2462 /*
2463 * Single counters are their own group leaders, with an
2464 * empty sibling list:
2465 */
2466 if (!group_leader)
2467 group_leader = counter;
2468
0793a61d 2469 mutex_init(&counter->mutex);
04289bb9 2470 INIT_LIST_HEAD(&counter->list_entry);
592903cd 2471 INIT_LIST_HEAD(&counter->event_entry);
04289bb9 2472 INIT_LIST_HEAD(&counter->sibling_list);
0793a61d
TG
2473 init_waitqueue_head(&counter->waitq);
2474
7b732a75
PZ
2475 mutex_init(&counter->mmap_mutex);
2476
d859e29f
PM
2477 INIT_LIST_HEAD(&counter->child_list);
2478
9f66a381
IM
2479 counter->cpu = cpu;
2480 counter->hw_event = *hw_event;
04289bb9 2481 counter->group_leader = group_leader;
621a01ea 2482 counter->hw_ops = NULL;
23a185ca 2483 counter->ctx = ctx;
621a01ea 2484
235c7fc7 2485 counter->state = PERF_COUNTER_STATE_INACTIVE;
a86ed508
IM
2486 if (hw_event->disabled)
2487 counter->state = PERF_COUNTER_STATE_OFF;
2488
5c92d124 2489 hw_ops = NULL;
b8e83514 2490
f4a2deb4 2491 if (perf_event_raw(hw_event)) {
b8e83514 2492 hw_ops = hw_perf_counter_init(counter);
f4a2deb4
PZ
2493 goto done;
2494 }
2495
2496 switch (perf_event_type(hw_event)) {
b8e83514 2497 case PERF_TYPE_HARDWARE:
5c92d124 2498 hw_ops = hw_perf_counter_init(counter);
b8e83514
PZ
2499 break;
2500
2501 case PERF_TYPE_SOFTWARE:
2502 hw_ops = sw_perf_counter_init(counter);
2503 break;
2504
2505 case PERF_TYPE_TRACEPOINT:
2506 hw_ops = tp_perf_counter_init(counter);
2507 break;
2508 }
d5d2bc0d
PM
2509done:
2510 err = 0;
2511 if (!hw_ops)
2512 err = -EINVAL;
2513 else if (IS_ERR(hw_ops))
2514 err = PTR_ERR(hw_ops);
5c92d124 2515
d5d2bc0d 2516 if (err) {
621a01ea 2517 kfree(counter);
d5d2bc0d 2518 return ERR_PTR(err);
621a01ea 2519 }
d5d2bc0d 2520
621a01ea 2521 counter->hw_ops = hw_ops;
0793a61d
TG
2522
2523 return counter;
2524}
2525
2526/**
2743a5b0 2527 * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
9f66a381
IM
2528 *
2529 * @hw_event_uptr: event type attributes for monitoring/sampling
0793a61d 2530 * @pid: target pid
9f66a381
IM
2531 * @cpu: target cpu
2532 * @group_fd: group leader counter fd
0793a61d 2533 */
2743a5b0 2534SYSCALL_DEFINE5(perf_counter_open,
f3dfd265 2535 const struct perf_counter_hw_event __user *, hw_event_uptr,
2743a5b0 2536 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
0793a61d 2537{
04289bb9 2538 struct perf_counter *counter, *group_leader;
9f66a381 2539 struct perf_counter_hw_event hw_event;
04289bb9 2540 struct perf_counter_context *ctx;
9b51f66d 2541 struct file *counter_file = NULL;
04289bb9
IM
2542 struct file *group_file = NULL;
2543 int fput_needed = 0;
9b51f66d 2544 int fput_needed2 = 0;
0793a61d
TG
2545 int ret;
2546
2743a5b0
PM
2547 /* for future expandability... */
2548 if (flags)
2549 return -EINVAL;
2550
9f66a381 2551 if (copy_from_user(&hw_event, hw_event_uptr, sizeof(hw_event)) != 0)
eab656ae
TG
2552 return -EFAULT;
2553
04289bb9 2554 /*
ccff286d
IM
2555 * Get the target context (task or percpu):
2556 */
2557 ctx = find_get_context(pid, cpu);
2558 if (IS_ERR(ctx))
2559 return PTR_ERR(ctx);
2560
2561 /*
2562 * Look up the group leader (we will attach this counter to it):
04289bb9
IM
2563 */
2564 group_leader = NULL;
2565 if (group_fd != -1) {
2566 ret = -EINVAL;
2567 group_file = fget_light(group_fd, &fput_needed);
2568 if (!group_file)
ccff286d 2569 goto err_put_context;
04289bb9 2570 if (group_file->f_op != &perf_fops)
ccff286d 2571 goto err_put_context;
04289bb9
IM
2572
2573 group_leader = group_file->private_data;
2574 /*
ccff286d
IM
2575 * Do not allow a recursive hierarchy (this new sibling
2576 * becoming part of another group-sibling):
2577 */
2578 if (group_leader->group_leader != group_leader)
2579 goto err_put_context;
2580 /*
2581 * Do not allow to attach to a group in a different
2582 * task or CPU context:
04289bb9 2583 */
ccff286d
IM
2584 if (group_leader->ctx != ctx)
2585 goto err_put_context;
3b6f9e5c
PM
2586 /*
2587 * Only a group leader can be exclusive or pinned
2588 */
2589 if (hw_event.exclusive || hw_event.pinned)
2590 goto err_put_context;
04289bb9
IM
2591 }
2592
23a185ca
PM
2593 counter = perf_counter_alloc(&hw_event, cpu, ctx, group_leader,
2594 GFP_KERNEL);
d5d2bc0d
PM
2595 ret = PTR_ERR(counter);
2596 if (IS_ERR(counter))
0793a61d
TG
2597 goto err_put_context;
2598
0793a61d
TG
2599 ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
2600 if (ret < 0)
9b51f66d
IM
2601 goto err_free_put_context;
2602
2603 counter_file = fget_light(ret, &fput_needed2);
2604 if (!counter_file)
2605 goto err_free_put_context;
2606
2607 counter->filp = counter_file;
d859e29f 2608 mutex_lock(&ctx->mutex);
9b51f66d 2609 perf_install_in_context(ctx, counter, cpu);
d859e29f 2610 mutex_unlock(&ctx->mutex);
9b51f66d
IM
2611
2612 fput_light(counter_file, fput_needed2);
0793a61d 2613
04289bb9
IM
2614out_fput:
2615 fput_light(group_file, fput_needed);
2616
0793a61d
TG
2617 return ret;
2618
9b51f66d 2619err_free_put_context:
0793a61d
TG
2620 kfree(counter);
2621
2622err_put_context:
2623 put_context(ctx);
2624
04289bb9 2625 goto out_fput;
0793a61d
TG
2626}
2627
9b51f66d
IM
2628/*
2629 * Initialize the perf_counter context in a task_struct:
2630 */
2631static void
2632__perf_counter_init_context(struct perf_counter_context *ctx,
2633 struct task_struct *task)
2634{
2635 memset(ctx, 0, sizeof(*ctx));
2636 spin_lock_init(&ctx->lock);
d859e29f 2637 mutex_init(&ctx->mutex);
9b51f66d 2638 INIT_LIST_HEAD(&ctx->counter_list);
592903cd 2639 INIT_LIST_HEAD(&ctx->event_list);
9b51f66d
IM
2640 ctx->task = task;
2641}
2642
2643/*
2644 * inherit a counter from parent task to child task:
2645 */
d859e29f 2646static struct perf_counter *
9b51f66d
IM
2647inherit_counter(struct perf_counter *parent_counter,
2648 struct task_struct *parent,
2649 struct perf_counter_context *parent_ctx,
2650 struct task_struct *child,
d859e29f 2651 struct perf_counter *group_leader,
9b51f66d
IM
2652 struct perf_counter_context *child_ctx)
2653{
2654 struct perf_counter *child_counter;
2655
d859e29f
PM
2656 /*
2657 * Instead of creating recursive hierarchies of counters,
2658 * we link inherited counters back to the original parent,
2659 * which has a filp for sure, which we use as the reference
2660 * count:
2661 */
2662 if (parent_counter->parent)
2663 parent_counter = parent_counter->parent;
2664
9b51f66d 2665 child_counter = perf_counter_alloc(&parent_counter->hw_event,
23a185ca
PM
2666 parent_counter->cpu, child_ctx,
2667 group_leader, GFP_KERNEL);
d5d2bc0d
PM
2668 if (IS_ERR(child_counter))
2669 return child_counter;
9b51f66d
IM
2670
2671 /*
2672 * Link it up in the child's context:
2673 */
9b51f66d 2674 child_counter->task = child;
53cfbf59 2675 add_counter_to_ctx(child_counter, child_ctx);
9b51f66d
IM
2676
2677 child_counter->parent = parent_counter;
9b51f66d
IM
2678 /*
2679 * inherit into child's child as well:
2680 */
2681 child_counter->hw_event.inherit = 1;
2682
2683 /*
2684 * Get a reference to the parent filp - we will fput it
2685 * when the child counter exits. This is safe to do because
2686 * we are in the parent and we know that the filp still
2687 * exists and has a nonzero count:
2688 */
2689 atomic_long_inc(&parent_counter->filp->f_count);
2690
d859e29f
PM
2691 /*
2692 * Link this into the parent counter's child list
2693 */
2694 mutex_lock(&parent_counter->mutex);
2695 list_add_tail(&child_counter->child_list, &parent_counter->child_list);
2696
2697 /*
2698 * Make the child state follow the state of the parent counter,
2699 * not its hw_event.disabled bit. We hold the parent's mutex,
2700 * so we won't race with perf_counter_{en,dis}able_family.
2701 */
2702 if (parent_counter->state >= PERF_COUNTER_STATE_INACTIVE)
2703 child_counter->state = PERF_COUNTER_STATE_INACTIVE;
2704 else
2705 child_counter->state = PERF_COUNTER_STATE_OFF;
2706
2707 mutex_unlock(&parent_counter->mutex);
2708
2709 return child_counter;
2710}
2711
2712static int inherit_group(struct perf_counter *parent_counter,
2713 struct task_struct *parent,
2714 struct perf_counter_context *parent_ctx,
2715 struct task_struct *child,
2716 struct perf_counter_context *child_ctx)
2717{
2718 struct perf_counter *leader;
2719 struct perf_counter *sub;
d5d2bc0d 2720 struct perf_counter *child_ctr;
d859e29f
PM
2721
2722 leader = inherit_counter(parent_counter, parent, parent_ctx,
2723 child, NULL, child_ctx);
d5d2bc0d
PM
2724 if (IS_ERR(leader))
2725 return PTR_ERR(leader);
d859e29f 2726 list_for_each_entry(sub, &parent_counter->sibling_list, list_entry) {
d5d2bc0d
PM
2727 child_ctr = inherit_counter(sub, parent, parent_ctx,
2728 child, leader, child_ctx);
2729 if (IS_ERR(child_ctr))
2730 return PTR_ERR(child_ctr);
d859e29f 2731 }
9b51f66d
IM
2732 return 0;
2733}
2734
d859e29f
PM
2735static void sync_child_counter(struct perf_counter *child_counter,
2736 struct perf_counter *parent_counter)
2737{
2738 u64 parent_val, child_val;
2739
2740 parent_val = atomic64_read(&parent_counter->count);
2741 child_val = atomic64_read(&child_counter->count);
2742
2743 /*
2744 * Add back the child's count to the parent's count:
2745 */
2746 atomic64_add(child_val, &parent_counter->count);
53cfbf59
PM
2747 atomic64_add(child_counter->total_time_enabled,
2748 &parent_counter->child_total_time_enabled);
2749 atomic64_add(child_counter->total_time_running,
2750 &parent_counter->child_total_time_running);
d859e29f
PM
2751
2752 /*
2753 * Remove this counter from the parent's list
2754 */
2755 mutex_lock(&parent_counter->mutex);
2756 list_del_init(&child_counter->child_list);
2757 mutex_unlock(&parent_counter->mutex);
2758
2759 /*
2760 * Release the parent counter, if this was the last
2761 * reference to it.
2762 */
2763 fput(parent_counter->filp);
2764}
2765
9b51f66d
IM
2766static void
2767__perf_counter_exit_task(struct task_struct *child,
2768 struct perf_counter *child_counter,
2769 struct perf_counter_context *child_ctx)
2770{
2771 struct perf_counter *parent_counter;
d859e29f 2772 struct perf_counter *sub, *tmp;
9b51f66d
IM
2773
2774 /*
235c7fc7
IM
2775 * If we do not self-reap then we have to wait for the
2776 * child task to unschedule (it will happen for sure),
2777 * so that its counter is at its final count. (This
2778 * condition triggers rarely - child tasks usually get
2779 * off their CPU before the parent has a chance to
2780 * get this far into the reaping action)
9b51f66d 2781 */
235c7fc7
IM
2782 if (child != current) {
2783 wait_task_inactive(child, 0);
2784 list_del_init(&child_counter->list_entry);
53cfbf59 2785 update_counter_times(child_counter);
235c7fc7 2786 } else {
0cc0c027 2787 struct perf_cpu_context *cpuctx;
235c7fc7
IM
2788 unsigned long flags;
2789 u64 perf_flags;
2790
2791 /*
2792 * Disable and unlink this counter.
2793 *
2794 * Be careful about zapping the list - IRQ/NMI context
2795 * could still be processing it:
2796 */
2797 curr_rq_lock_irq_save(&flags);
2798 perf_flags = hw_perf_save_disable();
0cc0c027
IM
2799
2800 cpuctx = &__get_cpu_var(perf_cpu_context);
2801
d859e29f 2802 group_sched_out(child_counter, cpuctx, child_ctx);
53cfbf59 2803 update_counter_times(child_counter);
0cc0c027 2804
235c7fc7 2805 list_del_init(&child_counter->list_entry);
0cc0c027 2806
235c7fc7 2807 child_ctx->nr_counters--;
9b51f66d 2808
235c7fc7
IM
2809 hw_perf_restore(perf_flags);
2810 curr_rq_unlock_irq_restore(&flags);
2811 }
9b51f66d
IM
2812
2813 parent_counter = child_counter->parent;
2814 /*
2815 * It can happen that parent exits first, and has counters
2816 * that are still around due to the child reference. These
2817 * counters need to be zapped - but otherwise linger.
2818 */
d859e29f
PM
2819 if (parent_counter) {
2820 sync_child_counter(child_counter, parent_counter);
2821 list_for_each_entry_safe(sub, tmp, &child_counter->sibling_list,
2822 list_entry) {
4bcf349a 2823 if (sub->parent) {
d859e29f 2824 sync_child_counter(sub, sub->parent);
f1600952 2825 free_counter(sub);
4bcf349a 2826 }
d859e29f 2827 }
f1600952 2828 free_counter(child_counter);
4bcf349a 2829 }
9b51f66d
IM
2830}
2831
2832/*
d859e29f 2833 * When a child task exits, feed back counter values to parent counters.
9b51f66d 2834 *
d859e29f 2835 * Note: we may be running in child context, but the PID is not hashed
9b51f66d
IM
2836 * anymore so new counters will not be added.
2837 */
2838void perf_counter_exit_task(struct task_struct *child)
2839{
2840 struct perf_counter *child_counter, *tmp;
2841 struct perf_counter_context *child_ctx;
2842
2843 child_ctx = &child->perf_counter_ctx;
2844
2845 if (likely(!child_ctx->nr_counters))
2846 return;
2847
2848 list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list,
2849 list_entry)
2850 __perf_counter_exit_task(child, child_counter, child_ctx);
2851}
2852
2853/*
2854 * Initialize the perf_counter context in task_struct
2855 */
2856void perf_counter_init_task(struct task_struct *child)
2857{
2858 struct perf_counter_context *child_ctx, *parent_ctx;
d859e29f 2859 struct perf_counter *counter;
9b51f66d 2860 struct task_struct *parent = current;
9b51f66d
IM
2861
2862 child_ctx = &child->perf_counter_ctx;
2863 parent_ctx = &parent->perf_counter_ctx;
2864
2865 __perf_counter_init_context(child_ctx, child);
2866
2867 /*
2868 * This is executed from the parent task context, so inherit
2869 * counters that have been marked for cloning:
2870 */
2871
2872 if (likely(!parent_ctx->nr_counters))
2873 return;
2874
2875 /*
2876 * Lock the parent list. No need to lock the child - not PID
2877 * hashed yet and not running, so nobody can access it.
2878 */
d859e29f 2879 mutex_lock(&parent_ctx->mutex);
9b51f66d
IM
2880
2881 /*
2882 * We dont have to disable NMIs - we are only looking at
2883 * the list, not manipulating it:
2884 */
2885 list_for_each_entry(counter, &parent_ctx->counter_list, list_entry) {
d859e29f 2886 if (!counter->hw_event.inherit)
9b51f66d
IM
2887 continue;
2888
d859e29f 2889 if (inherit_group(counter, parent,
9b51f66d
IM
2890 parent_ctx, child, child_ctx))
2891 break;
2892 }
2893
d859e29f 2894 mutex_unlock(&parent_ctx->mutex);
9b51f66d
IM
2895}
2896
04289bb9 2897static void __cpuinit perf_counter_init_cpu(int cpu)
0793a61d 2898{
04289bb9 2899 struct perf_cpu_context *cpuctx;
0793a61d 2900
04289bb9
IM
2901 cpuctx = &per_cpu(perf_cpu_context, cpu);
2902 __perf_counter_init_context(&cpuctx->ctx, NULL);
0793a61d
TG
2903
2904 mutex_lock(&perf_resource_mutex);
04289bb9 2905 cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu;
0793a61d 2906 mutex_unlock(&perf_resource_mutex);
04289bb9 2907
01d0287f 2908 hw_perf_counter_setup(cpu);
0793a61d
TG
2909}
2910
2911#ifdef CONFIG_HOTPLUG_CPU
04289bb9 2912static void __perf_counter_exit_cpu(void *info)
0793a61d
TG
2913{
2914 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
2915 struct perf_counter_context *ctx = &cpuctx->ctx;
2916 struct perf_counter *counter, *tmp;
2917
04289bb9
IM
2918 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry)
2919 __perf_counter_remove_from_context(counter);
0793a61d 2920}
04289bb9 2921static void perf_counter_exit_cpu(int cpu)
0793a61d 2922{
d859e29f
PM
2923 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
2924 struct perf_counter_context *ctx = &cpuctx->ctx;
2925
2926 mutex_lock(&ctx->mutex);
04289bb9 2927 smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1);
d859e29f 2928 mutex_unlock(&ctx->mutex);
0793a61d
TG
2929}
2930#else
04289bb9 2931static inline void perf_counter_exit_cpu(int cpu) { }
0793a61d
TG
2932#endif
2933
2934static int __cpuinit
2935perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
2936{
2937 unsigned int cpu = (long)hcpu;
2938
2939 switch (action) {
2940
2941 case CPU_UP_PREPARE:
2942 case CPU_UP_PREPARE_FROZEN:
04289bb9 2943 perf_counter_init_cpu(cpu);
0793a61d
TG
2944 break;
2945
2946 case CPU_DOWN_PREPARE:
2947 case CPU_DOWN_PREPARE_FROZEN:
04289bb9 2948 perf_counter_exit_cpu(cpu);
0793a61d
TG
2949 break;
2950
2951 default:
2952 break;
2953 }
2954
2955 return NOTIFY_OK;
2956}
2957
2958static struct notifier_block __cpuinitdata perf_cpu_nb = {
2959 .notifier_call = perf_cpu_notify,
2960};
2961
2962static int __init perf_counter_init(void)
2963{
2964 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
2965 (void *)(long)smp_processor_id());
2966 register_cpu_notifier(&perf_cpu_nb);
2967
2968 return 0;
2969}
2970early_initcall(perf_counter_init);
2971
2972static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
2973{
2974 return sprintf(buf, "%d\n", perf_reserved_percpu);
2975}
2976
2977static ssize_t
2978perf_set_reserve_percpu(struct sysdev_class *class,
2979 const char *buf,
2980 size_t count)
2981{
2982 struct perf_cpu_context *cpuctx;
2983 unsigned long val;
2984 int err, cpu, mpt;
2985
2986 err = strict_strtoul(buf, 10, &val);
2987 if (err)
2988 return err;
2989 if (val > perf_max_counters)
2990 return -EINVAL;
2991
2992 mutex_lock(&perf_resource_mutex);
2993 perf_reserved_percpu = val;
2994 for_each_online_cpu(cpu) {
2995 cpuctx = &per_cpu(perf_cpu_context, cpu);
2996 spin_lock_irq(&cpuctx->ctx.lock);
2997 mpt = min(perf_max_counters - cpuctx->ctx.nr_counters,
2998 perf_max_counters - perf_reserved_percpu);
2999 cpuctx->max_pertask = mpt;
3000 spin_unlock_irq(&cpuctx->ctx.lock);
3001 }
3002 mutex_unlock(&perf_resource_mutex);
3003
3004 return count;
3005}
3006
3007static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
3008{
3009 return sprintf(buf, "%d\n", perf_overcommit);
3010}
3011
3012static ssize_t
3013perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
3014{
3015 unsigned long val;
3016 int err;
3017
3018 err = strict_strtoul(buf, 10, &val);
3019 if (err)
3020 return err;
3021 if (val > 1)
3022 return -EINVAL;
3023
3024 mutex_lock(&perf_resource_mutex);
3025 perf_overcommit = val;
3026 mutex_unlock(&perf_resource_mutex);
3027
3028 return count;
3029}
3030
3031static SYSDEV_CLASS_ATTR(
3032 reserve_percpu,
3033 0644,
3034 perf_show_reserve_percpu,
3035 perf_set_reserve_percpu
3036 );
3037
3038static SYSDEV_CLASS_ATTR(
3039 overcommit,
3040 0644,
3041 perf_show_overcommit,
3042 perf_set_overcommit
3043 );
3044
3045static struct attribute *perfclass_attrs[] = {
3046 &attr_reserve_percpu.attr,
3047 &attr_overcommit.attr,
3048 NULL
3049};
3050
3051static struct attribute_group perfclass_attr_group = {
3052 .attrs = perfclass_attrs,
3053 .name = "perf_counters",
3054};
3055
3056static int __init perf_counter_sysfs_init(void)
3057{
3058 return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
3059 &perfclass_attr_group);
3060}
3061device_initcall(perf_counter_sysfs_init);